-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathFeedsTest.php
More file actions
196 lines (175 loc) · 5.44 KB
/
FeedsTest.php
File metadata and controls
196 lines (175 loc) · 5.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
<?php
namespace Automattic\WooCommerce\Pinterest\Tests\Unit;
use Automattic\WooCommerce\Pinterest\Exception\FeedNotFoundException;
use Automattic\WooCommerce\Pinterest\Feeds;
use Automattic\WooCommerce\Pinterest\Notes\FeedDeletionFailure;
use Pinterest_For_Woocommerce;
use WP_UnitTestCase;
class FeedsTest extends WP_UnitTestCase {
public function setUp(): void {
parent::setUp();
Pinterest_For_Woocommerce::set_default_settings();
Pinterest_For_Woocommerce::save_setting( 'tracking_advertiser', '114141241212' );
}
public function tearDown(): void {
parent::tearDown();
remove_all_filters( 'pre_http_request' );
remove_all_filters( 'site_url' );
remove_all_filters( 'upload_dir' );
}
/**
* Tests feed deletion produces an admin notice in case feed deletion has failed.
*
* @return void
*/
public function test_feed_delete_produces_an_admin_notification() {
add_filter( 'pre_http_request', array( self::class, 'feed_delete_failure' ), 10, 3 );
$result = Feeds::delete_feed( '1574695656968' );
$this->assertFalse( $result );
$this->assertTrue( FeedDeletionFailure::note_exists() );
}
public function test_maybe_remote_feed_returns_feed_id() {
add_filter(
'upload_dir',
function ( $uploads ) {
$uploads['baseurl'] = 'https://example-1.com';
return $uploads;
}
);
add_filter( 'pre_http_request', array( self::class, 'get_feeds' ), 10, 3 );
$feed = Feeds::maybe_remote_feed();
$this->assertEquals( 'fIOasjj', $feed );
}
public function test_maybe_remote_feed_returns_empty_feed_id() {
add_filter(
'upload_dir',
function ( $uploads ) {
$uploads['baseurl'] = 'https://example-11.com';
return $uploads;
}
);
add_filter( 'pre_http_request', array( self::class, 'get_feeds_with_empty_tail_for_the_feed_location' ), 10, 3 );
$feed = Feeds::maybe_remote_feed();
$this->assertEquals( '', $feed );
}
public function test_maybe_remote_feed_returns_empty_string() {
add_filter( 'pre_http_request', array( self::class, 'get_empty_feeds' ), 10, 3 );
$this->expectException( FeedNotFoundException::class );
Feeds::maybe_remote_feed();
}
public static function feed_delete_failure( $response, $parsed_args, $url ): array {
if ( 'https://api.pinterest.com/v5/catalogs/feeds/1574695656968?ad_account_id=114141241212' === $url ) {
return array(
'headers' => array(
'content-type' => 'application/json',
),
'body' => json_encode(
array(
'code' => 4162,
'message' => 'We can\'t disable a Product Group with active promotions.',
)
),
'response' => array(
'code' => 409,
'message' => 'Conflict. Can\'t delete a feed with active promotions.',
),
'cookies' => array(),
'filename' => '',
);
}
return $response;
}
public static function get_feeds( $response, $parsed_args, $url ): array {
if ( 'https://api.pinterest.com/v5/catalogs/feeds?ad_account_id=114141241212' === $url ) {
return array(
'headers' => array(
'content-type' => 'application/json',
),
'body' => json_encode(
array(
'items' => array(
array(
"created_at" => "2022-03-14T15:15:22Z",
"id" => "278913891236895123895",
"updated_at" => "2022-03-14T15:16:34Z",
"name" => "Created by Pinterest for WooCommerce at pinterest.dima.works US|en-US|USD",
"format" => "TSV",
"catalog_type" => "RETAIL",
"location" => "https://example-1.com/pinterest-for-woocommerce-fIOasjj.xml",
"status" => "ACTIVE",
"default_currency" => "USD",
"default_locale" => "en-US",
"default_country" => "US",
"default_availability" => "IN_STOCK",
),
),
),
),
'response' => array(
'code' => 200,
'message' => 'OK',
),
'cookies' => array(),
'filename' => '',
);
}
return $response;
}
public static function get_empty_feeds( $response, $parsed_args, $url ): array {
if ( 'https://api.pinterest.com/v5/catalogs/feeds?ad_account_id=114141241212' === $url ) {
return array(
'headers' => array(
'content-type' => 'application/json',
),
'body' => json_encode(
array(
'items' => array(),
),
),
'response' => array(
'code' => 200,
'message' => 'OK',
),
'cookies' => array(),
'filename' => '',
);
}
return $response;
}
public static function get_feeds_with_empty_tail_for_the_feed_location( $response, $parsed_args, $url ): array {
if ( 'https://api.pinterest.com/v5/catalogs/feeds?ad_account_id=114141241212' === $url ) {
return array(
'headers' => array(
'content-type' => 'application/json',
),
'body' => json_encode(
array(
'items' => array(
array(
"created_at" => "2022-03-14T15:15:22Z",
"id" => "278913891236895123895",
"updated_at" => "2022-03-14T15:16:34Z",
"name" => "WooCommerce",
"format" => "TSV",
"catalog_type" => "RETAIL",
"location" => "https://example-11.com/pinterest-for-woocommerce-.xml",
"status" => "ACTIVE",
"default_currency" => "USD",
"default_locale" => "en-US",
"default_country" => "US",
"default_availability" => "IN_STOCK",
),
),
),
),
'response' => array(
'code' => 200,
'message' => 'OK',
),
'cookies' => array(),
'filename' => '',
);
}
return $response;
}
}