1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Shipping Zones API Tests |
5
|
|
|
* @package WooCommerce\Tests\API |
6
|
|
|
* @since 3.0.0 |
7
|
|
|
*/ |
8
|
|
|
class WC_Tests_API_Shipping_Zones_V2 extends WC_REST_Unit_Test_Case { |
|
|
|
|
9
|
|
|
|
10
|
|
|
protected $server; |
11
|
|
|
|
12
|
|
|
protected $endpoint; |
13
|
|
|
|
14
|
|
|
protected $user; |
15
|
|
|
|
16
|
|
|
protected $zones; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Setup our test server, endpoints, and user info. |
20
|
|
|
*/ |
21
|
|
|
public function setUp() { |
22
|
|
|
parent::setUp(); |
23
|
|
|
$this->endpoint = new WC_REST_Shipping_Zones_Controller(); |
24
|
|
|
$this->user = $this->factory->user->create( |
25
|
|
|
array( |
26
|
|
|
'role' => 'administrator', |
27
|
|
|
) |
28
|
|
|
); |
29
|
|
|
$this->zones = array(); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Helper method to create a Shipping Zone. |
34
|
|
|
* |
35
|
|
|
* @param string $name Zone name. |
36
|
|
|
* @param int $order Optional. Zone sort order. |
37
|
|
|
* @return WC_Shipping_Zone |
38
|
|
|
*/ |
39
|
|
|
protected function create_shipping_zone( $name, $order = 0, $locations = array() ) { |
40
|
|
|
$zone = new WC_Shipping_Zone( null ); |
41
|
|
|
$zone->set_zone_name( $name ); |
42
|
|
|
$zone->set_zone_order( $order ); |
43
|
|
|
$zone->set_locations( $locations ); |
44
|
|
|
$zone->save(); |
45
|
|
|
|
46
|
|
|
$this->zones[] = $zone; |
47
|
|
|
|
48
|
|
|
return $zone; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Test route registration. |
53
|
|
|
* @since 3.0.0 |
54
|
|
|
*/ |
55
|
|
|
public function test_register_routes() { |
56
|
|
|
$routes = $this->server->get_routes(); |
57
|
|
|
$this->assertArrayHasKey( '/wc/v2/shipping/zones', $routes ); |
58
|
|
|
$this->assertArrayHasKey( '/wc/v2/shipping/zones/(?P<id>[\d-]+)', $routes ); |
59
|
|
|
$this->assertArrayHasKey( '/wc/v2/shipping/zones/(?P<id>[\d]+)/locations', $routes ); |
60
|
|
|
$this->assertArrayHasKey( '/wc/v2/shipping/zones/(?P<zone_id>[\d]+)/methods', $routes ); |
61
|
|
|
$this->assertArrayHasKey( '/wc/v2/shipping/zones/(?P<zone_id>[\d]+)/methods/(?P<instance_id>[\d]+)', $routes ); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Test getting all Shipping Zones. |
66
|
|
|
* @since 3.0.0 |
67
|
|
|
*/ |
68
|
|
|
public function test_get_zones() { |
69
|
|
|
wp_set_current_user( $this->user ); |
70
|
|
|
|
71
|
|
|
// "Rest of the World" zone exists by default |
72
|
|
|
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/shipping/zones' ) ); |
73
|
|
|
$data = $response->get_data(); |
74
|
|
|
|
75
|
|
|
$this->assertEquals( 200, $response->get_status() ); |
76
|
|
|
$this->assertEquals( count( $data ), 1 ); |
77
|
|
|
$this->assertContains( |
78
|
|
|
array( |
79
|
|
|
'id' => $data[0]['id'], |
80
|
|
|
'name' => 'Locations not covered by your other zones', |
81
|
|
|
'order' => 0, |
82
|
|
|
'_links' => array( |
83
|
|
|
'self' => array( |
84
|
|
|
array( |
85
|
|
|
'href' => rest_url( '/wc/v2/shipping/zones/' . $data[0]['id'] ), |
86
|
|
|
), |
87
|
|
|
), |
88
|
|
|
'collection' => array( |
89
|
|
|
array( |
90
|
|
|
'href' => rest_url( '/wc/v2/shipping/zones' ), |
91
|
|
|
), |
92
|
|
|
), |
93
|
|
|
'describedby' => array( |
94
|
|
|
array( |
95
|
|
|
'href' => rest_url( '/wc/v2/shipping/zones/' . $data[0]['id'] . '/locations' ), |
96
|
|
|
), |
97
|
|
|
), |
98
|
|
|
), |
99
|
|
|
), |
100
|
|
|
$data |
101
|
|
|
); |
102
|
|
|
|
103
|
|
|
// Create a zone and make sure it's in the response |
104
|
|
|
$this->create_shipping_zone( 'Zone 1' ); |
105
|
|
|
|
106
|
|
|
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/shipping/zones' ) ); |
107
|
|
|
$data = $response->get_data(); |
108
|
|
|
|
109
|
|
|
$this->assertEquals( 200, $response->get_status() ); |
110
|
|
|
$this->assertEquals( count( $data ), 2 ); |
111
|
|
|
$this->assertContains( |
112
|
|
|
array( |
113
|
|
|
'id' => $data[1]['id'], |
114
|
|
|
'name' => 'Zone 1', |
115
|
|
|
'order' => 0, |
116
|
|
|
'_links' => array( |
117
|
|
|
'self' => array( |
118
|
|
|
array( |
119
|
|
|
'href' => rest_url( '/wc/v2/shipping/zones/' . $data[1]['id'] ), |
120
|
|
|
), |
121
|
|
|
), |
122
|
|
|
'collection' => array( |
123
|
|
|
array( |
124
|
|
|
'href' => rest_url( '/wc/v2/shipping/zones' ), |
125
|
|
|
), |
126
|
|
|
), |
127
|
|
|
'describedby' => array( |
128
|
|
|
array( |
129
|
|
|
'href' => rest_url( '/wc/v2/shipping/zones/' . $data[1]['id'] . '/locations' ), |
130
|
|
|
), |
131
|
|
|
), |
132
|
|
|
), |
133
|
|
|
), |
134
|
|
|
$data |
135
|
|
|
); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Test /shipping/zones without valid permissions/creds. |
140
|
|
|
* @since 3.0.0 |
141
|
|
|
*/ |
142
|
|
|
public function test_get_shipping_zones_without_permission() { |
143
|
|
|
wp_set_current_user( 0 ); |
144
|
|
|
|
145
|
|
|
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/shipping/zones' ) ); |
146
|
|
|
$this->assertEquals( 401, $response->get_status() ); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Test /shipping/zones while Shipping is disabled in WooCommerce. |
151
|
|
|
* @since 3.0.0 |
152
|
|
|
*/ |
153
|
|
|
public function test_get_shipping_zones_disabled_shipping() { |
154
|
|
|
wp_set_current_user( $this->user ); |
155
|
|
|
|
156
|
|
|
add_filter( 'wc_shipping_enabled', '__return_false' ); |
157
|
|
|
|
158
|
|
|
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/shipping/zones' ) ); |
159
|
|
|
$this->assertEquals( 404, $response->get_status() ); |
160
|
|
|
|
161
|
|
|
remove_filter( 'wc_shipping_enabled', '__return_false' ); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Test Shipping Zone schema. |
166
|
|
|
* @since 3.0.0 |
167
|
|
|
*/ |
168
|
|
|
public function test_get_shipping_zone_schema() { |
169
|
|
|
$request = new WP_REST_Request( 'OPTIONS', '/wc/v2/shipping/zones' ); |
170
|
|
|
$response = $this->server->dispatch( $request ); |
171
|
|
|
$data = $response->get_data(); |
172
|
|
|
$properties = $data['schema']['properties']; |
173
|
|
|
$this->assertEquals( 3, count( $properties ) ); |
174
|
|
|
$this->assertArrayHasKey( 'id', $properties ); |
175
|
|
|
$this->assertTrue( $properties['id']['readonly'] ); |
176
|
|
|
$this->assertArrayHasKey( 'name', $properties ); |
177
|
|
|
$this->assertArrayHasKey( 'order', $properties ); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* Test Shipping Zone create endpoint. |
182
|
|
|
* @since 3.0.0 |
183
|
|
|
*/ |
184
|
|
|
public function test_create_shipping_zone() { |
185
|
|
|
wp_set_current_user( $this->user ); |
186
|
|
|
|
187
|
|
|
$request = new WP_REST_Request( 'POST', '/wc/v2/shipping/zones' ); |
188
|
|
|
$request->set_body_params( |
189
|
|
|
array( |
190
|
|
|
'name' => 'Test Zone', |
191
|
|
|
'order' => 1, |
192
|
|
|
) |
193
|
|
|
); |
194
|
|
|
$response = $this->server->dispatch( $request ); |
195
|
|
|
$data = $response->get_data(); |
196
|
|
|
|
197
|
|
|
$this->assertEquals( 201, $response->get_status() ); |
198
|
|
|
$this->assertEquals( |
199
|
|
|
array( |
200
|
|
|
'id' => $data['id'], |
201
|
|
|
'name' => 'Test Zone', |
202
|
|
|
'order' => 1, |
203
|
|
|
'_links' => array( |
204
|
|
|
'self' => array( |
205
|
|
|
array( |
206
|
|
|
'href' => rest_url( '/wc/v2/shipping/zones/' . $data['id'] ), |
207
|
|
|
), |
208
|
|
|
), |
209
|
|
|
'collection' => array( |
210
|
|
|
array( |
211
|
|
|
'href' => rest_url( '/wc/v2/shipping/zones' ), |
212
|
|
|
), |
213
|
|
|
), |
214
|
|
|
'describedby' => array( |
215
|
|
|
array( |
216
|
|
|
'href' => rest_url( '/wc/v2/shipping/zones/' . $data['id'] . '/locations' ), |
217
|
|
|
), |
218
|
|
|
), |
219
|
|
|
), |
220
|
|
|
), |
221
|
|
|
$data |
222
|
|
|
); |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* Test Shipping Zone create endpoint. |
227
|
|
|
* @since 3.0.0 |
228
|
|
|
*/ |
229
|
|
|
public function test_create_shipping_zone_without_permission() { |
230
|
|
|
wp_set_current_user( 0 ); |
231
|
|
|
|
232
|
|
|
$request = new WP_REST_Request( 'POST', '/wc/v2/shipping/zones' ); |
233
|
|
|
$request->set_body_params( |
234
|
|
|
array( |
235
|
|
|
'name' => 'Test Zone', |
236
|
|
|
'order' => 1, |
237
|
|
|
) |
238
|
|
|
); |
239
|
|
|
$response = $this->server->dispatch( $request ); |
240
|
|
|
$this->assertEquals( 401, $response->get_status() ); |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
/** |
244
|
|
|
* Test Shipping Zone update endpoint. |
245
|
|
|
* @since 3.0.0 |
246
|
|
|
*/ |
247
|
|
|
public function test_update_shipping_zone() { |
248
|
|
|
wp_set_current_user( $this->user ); |
249
|
|
|
|
250
|
|
|
$zone = $this->create_shipping_zone( 'Test Zone' ); |
251
|
|
|
|
252
|
|
|
$request = new WP_REST_Request( 'PUT', '/wc/v2/shipping/zones/' . $zone->get_id() ); |
253
|
|
|
$request->set_body_params( |
254
|
|
|
array( |
255
|
|
|
'name' => 'Zone Test', |
256
|
|
|
'order' => 2, |
257
|
|
|
) |
258
|
|
|
); |
259
|
|
|
$response = $this->server->dispatch( $request ); |
260
|
|
|
$data = $response->get_data(); |
261
|
|
|
|
262
|
|
|
$this->assertEquals( 200, $response->get_status() ); |
263
|
|
|
$this->assertEquals( |
264
|
|
|
array( |
265
|
|
|
'id' => $zone->get_id(), |
266
|
|
|
'name' => 'Zone Test', |
267
|
|
|
'order' => 2, |
268
|
|
|
'_links' => array( |
269
|
|
|
'self' => array( |
270
|
|
|
array( |
271
|
|
|
'href' => rest_url( '/wc/v2/shipping/zones/' . $zone->get_id() ), |
272
|
|
|
), |
273
|
|
|
), |
274
|
|
|
'collection' => array( |
275
|
|
|
array( |
276
|
|
|
'href' => rest_url( '/wc/v2/shipping/zones' ), |
277
|
|
|
), |
278
|
|
|
), |
279
|
|
|
'describedby' => array( |
280
|
|
|
array( |
281
|
|
|
'href' => rest_url( '/wc/v2/shipping/zones/' . $zone->get_id() . '/locations' ), |
282
|
|
|
), |
283
|
|
|
), |
284
|
|
|
), |
285
|
|
|
), |
286
|
|
|
$data |
287
|
|
|
); |
288
|
|
|
} |
289
|
|
|
|
290
|
|
|
/** |
291
|
|
|
* Test Shipping Zone update endpoint with a bad zone ID. |
292
|
|
|
* @since 3.0.0 |
293
|
|
|
*/ |
294
|
|
|
public function test_update_shipping_zone_invalid_id() { |
295
|
|
|
wp_set_current_user( $this->user ); |
296
|
|
|
|
297
|
|
|
$request = new WP_REST_Request( 'PUT', '/wc/v2/shipping/zones/555555' ); |
298
|
|
|
$request->set_body_params( |
299
|
|
|
array( |
300
|
|
|
'name' => 'Zone Test', |
301
|
|
|
'order' => 2, |
302
|
|
|
) |
303
|
|
|
); |
304
|
|
|
$response = $this->server->dispatch( $request ); |
305
|
|
|
|
306
|
|
|
$this->assertEquals( 404, $response->get_status() ); |
307
|
|
|
} |
308
|
|
|
|
309
|
|
|
/** |
310
|
|
|
* Test Shipping Zone delete endpoint. |
311
|
|
|
* @since 3.0.0 |
312
|
|
|
*/ |
313
|
|
|
public function test_delete_shipping_zone() { |
314
|
|
|
wp_set_current_user( $this->user ); |
315
|
|
|
$zone = $this->create_shipping_zone( 'Zone 1' ); |
316
|
|
|
|
317
|
|
|
$request = new WP_REST_Request( 'DELETE', '/wc/v2/shipping/zones/' . $zone->get_id() ); |
318
|
|
|
$request->set_param( 'force', true ); |
319
|
|
|
$response = $this->server->dispatch( $request ); |
320
|
|
|
$data = $response->get_data(); |
321
|
|
|
|
322
|
|
|
$this->assertEquals( 200, $response->get_status() ); |
323
|
|
|
} |
324
|
|
|
|
325
|
|
|
/** |
326
|
|
|
* Test Shipping Zone delete endpoint without permissions. |
327
|
|
|
* @since 3.0.0 |
328
|
|
|
*/ |
329
|
|
|
public function test_delete_shipping_zone_without_permission() { |
330
|
|
|
wp_set_current_user( 0 ); |
331
|
|
|
$zone = $this->create_shipping_zone( 'Zone 1' ); |
332
|
|
|
|
333
|
|
|
$request = new WP_REST_Request( 'DELETE', '/wc/v2/shipping/zones/' . $zone->get_id() ); |
334
|
|
|
$request->set_param( 'force', true ); |
335
|
|
|
$response = $this->server->dispatch( $request ); |
336
|
|
|
$this->assertEquals( 401, $response->get_status() ); |
337
|
|
|
} |
338
|
|
|
|
339
|
|
|
/** |
340
|
|
|
* Test Shipping Zone delete endpoint with a bad zone ID. |
341
|
|
|
* @since 3.0.0 |
342
|
|
|
*/ |
343
|
|
|
public function test_delete_shipping_zone_invalid_id() { |
344
|
|
|
wp_set_current_user( $this->user ); |
345
|
|
|
$request = new WP_REST_Request( 'DELETE', '/wc/v2/shipping/zones/555555' ); |
346
|
|
|
$response = $this->server->dispatch( $request ); |
347
|
|
|
$this->assertEquals( 404, $response->get_status() ); |
348
|
|
|
} |
349
|
|
|
|
350
|
|
|
/** |
351
|
|
|
* Test getting a single Shipping Zone. |
352
|
|
|
* @since 3.0.0 |
353
|
|
|
*/ |
354
|
|
|
public function test_get_single_shipping_zone() { |
355
|
|
|
wp_set_current_user( $this->user ); |
356
|
|
|
|
357
|
|
|
$zone = $this->create_shipping_zone( 'Test Zone' ); |
358
|
|
|
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/shipping/zones/' . $zone->get_id() ) ); |
359
|
|
|
$data = $response->get_data(); |
360
|
|
|
|
361
|
|
|
$this->assertEquals( 200, $response->get_status() ); |
362
|
|
|
$this->assertEquals( |
363
|
|
|
array( |
364
|
|
|
'id' => $zone->get_id(), |
365
|
|
|
'name' => 'Test Zone', |
366
|
|
|
'order' => 0, |
367
|
|
|
'_links' => array( |
368
|
|
|
'self' => array( |
369
|
|
|
array( |
370
|
|
|
'href' => rest_url( '/wc/v2/shipping/zones/' . $zone->get_id() ), |
371
|
|
|
), |
372
|
|
|
), |
373
|
|
|
'collection' => array( |
374
|
|
|
array( |
375
|
|
|
'href' => rest_url( '/wc/v2/shipping/zones' ), |
376
|
|
|
), |
377
|
|
|
), |
378
|
|
|
'describedby' => array( |
379
|
|
|
array( |
380
|
|
|
'href' => rest_url( '/wc/v2/shipping/zones/' . $zone->get_id() . '/locations' ), |
381
|
|
|
), |
382
|
|
|
), |
383
|
|
|
), |
384
|
|
|
), |
385
|
|
|
$data |
386
|
|
|
); |
387
|
|
|
} |
388
|
|
|
|
389
|
|
|
/** |
390
|
|
|
* Test getting a single Shipping Zone with a bad zone ID. |
391
|
|
|
* @since 3.0.0 |
392
|
|
|
*/ |
393
|
|
|
public function test_get_single_shipping_zone_invalid_id() { |
394
|
|
|
wp_set_current_user( $this->user ); |
395
|
|
|
|
396
|
|
|
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/shipping/zones/1' ) ); |
397
|
|
|
|
398
|
|
|
$this->assertEquals( 404, $response->get_status() ); |
399
|
|
|
} |
400
|
|
|
|
401
|
|
|
/** |
402
|
|
|
* Test getting Shipping Zone Locations. |
403
|
|
|
* @since 3.0.0 |
404
|
|
|
*/ |
405
|
|
|
public function test_get_locations() { |
406
|
|
|
wp_set_current_user( $this->user ); |
407
|
|
|
|
408
|
|
|
// Create a zone |
409
|
|
|
$zone = $this->create_shipping_zone( |
410
|
|
|
'Zone 1', |
411
|
|
|
0, |
412
|
|
|
array( |
413
|
|
|
array( |
414
|
|
|
'code' => 'US', |
415
|
|
|
'type' => 'country', |
416
|
|
|
), |
417
|
|
|
) |
418
|
|
|
); |
419
|
|
|
|
420
|
|
|
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/shipping/zones/' . $zone->get_id() . '/locations' ) ); |
421
|
|
|
$data = $response->get_data(); |
422
|
|
|
|
423
|
|
|
$this->assertEquals( 200, $response->get_status() ); |
424
|
|
|
$this->assertEquals( count( $data ), 1 ); |
425
|
|
|
$this->assertEquals( |
426
|
|
|
array( |
427
|
|
|
array( |
428
|
|
|
'code' => 'US', |
429
|
|
|
'type' => 'country', |
430
|
|
|
'_links' => array( |
431
|
|
|
'collection' => array( |
432
|
|
|
array( |
433
|
|
|
'href' => rest_url( '/wc/v2/shipping/zones/' . $zone->get_id() . '/locations' ), |
434
|
|
|
), |
435
|
|
|
), |
436
|
|
|
'describes' => array( |
437
|
|
|
array( |
438
|
|
|
'href' => rest_url( '/wc/v2/shipping/zones/' . $zone->get_id() ), |
439
|
|
|
), |
440
|
|
|
), |
441
|
|
|
), |
442
|
|
|
), |
443
|
|
|
), |
444
|
|
|
$data |
445
|
|
|
); |
446
|
|
|
} |
447
|
|
|
|
448
|
|
|
/** |
449
|
|
|
* Test getting Shipping Zone Locations with a bad zone ID. |
450
|
|
|
* @since 3.0.0 |
451
|
|
|
*/ |
452
|
|
|
public function test_get_locations_invalid_id() { |
453
|
|
|
wp_set_current_user( $this->user ); |
454
|
|
|
|
455
|
|
|
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/shipping/zones/1/locations' ) ); |
456
|
|
|
|
457
|
|
|
$this->assertEquals( 404, $response->get_status() ); |
458
|
|
|
} |
459
|
|
|
|
460
|
|
|
/** |
461
|
|
|
* Test Shipping Zone Locations update endpoint. |
462
|
|
|
* @since 3.0.0 |
463
|
|
|
*/ |
464
|
|
|
public function test_update_locations() { |
465
|
|
|
wp_set_current_user( $this->user ); |
466
|
|
|
|
467
|
|
|
$zone = $this->create_shipping_zone( 'Test Zone' ); |
468
|
|
|
|
469
|
|
|
$request = new WP_REST_Request( 'PUT', '/wc/v2/shipping/zones/' . $zone->get_id() . '/locations' ); |
470
|
|
|
$request->add_header( 'Content-Type', 'application/json' ); |
471
|
|
|
$request->set_body( |
472
|
|
|
json_encode( |
473
|
|
|
array( |
474
|
|
|
array( |
475
|
|
|
'code' => 'UK', |
476
|
|
|
'type' => 'country', |
477
|
|
|
), |
478
|
|
|
array( |
479
|
|
|
'code' => 'US', // test that locations missing "type" treated as country. |
480
|
|
|
), |
481
|
|
|
array( |
482
|
|
|
'code' => 'SW1A0AA', |
483
|
|
|
'type' => 'postcode', |
484
|
|
|
), |
485
|
|
|
array( |
486
|
|
|
'type' => 'continent', // test that locations missing "code" aren't saved |
487
|
|
|
), |
488
|
|
|
) |
489
|
|
|
) |
490
|
|
|
); |
491
|
|
|
$response = $this->server->dispatch( $request ); |
492
|
|
|
$data = $response->get_data(); |
493
|
|
|
|
494
|
|
|
$this->assertEquals( 3, count( $data ) ); |
495
|
|
|
$this->assertEquals( |
496
|
|
|
array( |
497
|
|
|
array( |
498
|
|
|
'code' => 'UK', |
499
|
|
|
'type' => 'country', |
500
|
|
|
'_links' => array( |
501
|
|
|
'collection' => array( |
502
|
|
|
array( |
503
|
|
|
'href' => rest_url( '/wc/v2/shipping/zones/' . $zone->get_id() . '/locations' ), |
504
|
|
|
), |
505
|
|
|
), |
506
|
|
|
'describes' => array( |
507
|
|
|
array( |
508
|
|
|
'href' => rest_url( '/wc/v2/shipping/zones/' . $zone->get_id() ), |
509
|
|
|
), |
510
|
|
|
), |
511
|
|
|
), |
512
|
|
|
), |
513
|
|
|
array( |
514
|
|
|
'code' => 'US', |
515
|
|
|
'type' => 'country', |
516
|
|
|
'_links' => array( |
517
|
|
|
'collection' => array( |
518
|
|
|
array( |
519
|
|
|
'href' => rest_url( '/wc/v2/shipping/zones/' . $zone->get_id() . '/locations' ), |
520
|
|
|
), |
521
|
|
|
), |
522
|
|
|
'describes' => array( |
523
|
|
|
array( |
524
|
|
|
'href' => rest_url( '/wc/v2/shipping/zones/' . $zone->get_id() ), |
525
|
|
|
), |
526
|
|
|
), |
527
|
|
|
), |
528
|
|
|
), |
529
|
|
|
array( |
530
|
|
|
'code' => 'SW1A0AA', |
531
|
|
|
'type' => 'postcode', |
532
|
|
|
'_links' => array( |
533
|
|
|
'collection' => array( |
534
|
|
|
array( |
535
|
|
|
'href' => rest_url( '/wc/v2/shipping/zones/' . $zone->get_id() . '/locations' ), |
536
|
|
|
), |
537
|
|
|
), |
538
|
|
|
'describes' => array( |
539
|
|
|
array( |
540
|
|
|
'href' => rest_url( '/wc/v2/shipping/zones/' . $zone->get_id() ), |
541
|
|
|
), |
542
|
|
|
), |
543
|
|
|
), |
544
|
|
|
), |
545
|
|
|
), |
546
|
|
|
$data |
547
|
|
|
); |
548
|
|
|
} |
549
|
|
|
|
550
|
|
|
/** |
551
|
|
|
* Test updating Shipping Zone Locations with a bad zone ID. |
552
|
|
|
* @since 3.0.0 |
553
|
|
|
*/ |
554
|
|
|
public function test_update_locations_invalid_id() { |
555
|
|
|
wp_set_current_user( $this->user ); |
556
|
|
|
|
557
|
|
|
$response = $this->server->dispatch( new WP_REST_Request( 'PUT', '/wc/v2/shipping/zones/1/locations' ) ); |
558
|
|
|
|
559
|
|
|
$this->assertEquals( 404, $response->get_status() ); |
560
|
|
|
} |
561
|
|
|
|
562
|
|
|
/** |
563
|
|
|
* Test getting all Shipping Zone Methods and getting a single Shipping Zone Method. |
564
|
|
|
* @since 3.0.0 |
565
|
|
|
*/ |
566
|
|
|
public function test_get_methods() { |
567
|
|
|
wp_set_current_user( $this->user ); |
568
|
|
|
|
569
|
|
|
// Create a shipping method and make sure it's in the response |
570
|
|
|
$zone = $this->create_shipping_zone( 'Zone 1' ); |
571
|
|
|
$instance_id = $zone->add_shipping_method( 'flat_rate' ); |
572
|
|
|
$methods = $zone->get_shipping_methods(); |
573
|
|
|
$method = $methods[ $instance_id ]; |
574
|
|
|
|
575
|
|
|
$settings = array(); |
576
|
|
|
$method->init_instance_settings(); |
577
|
|
|
foreach ( $method->get_instance_form_fields() as $id => $field ) { |
578
|
|
|
$data = array( |
579
|
|
|
'id' => $id, |
580
|
|
|
'label' => $field['title'], |
581
|
|
|
'description' => ( empty( $field['description'] ) ? '' : $field['description'] ), |
582
|
|
|
'type' => $field['type'], |
583
|
|
|
'value' => $method->instance_settings[ $id ], |
584
|
|
|
'default' => ( empty( $field['default'] ) ? '' : $field['default'] ), |
585
|
|
|
'tip' => ( empty( $field['description'] ) ? '' : $field['description'] ), |
586
|
|
|
'placeholder' => ( empty( $field['placeholder'] ) ? '' : $field['placeholder'] ), |
587
|
|
|
); |
588
|
|
|
if ( ! empty( $field['options'] ) ) { |
589
|
|
|
$data['options'] = $field['options']; |
590
|
|
|
} |
591
|
|
|
$settings[ $id ] = $data; |
592
|
|
|
} |
593
|
|
|
|
594
|
|
|
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/shipping/zones/' . $zone->get_id() . '/methods' ) ); |
595
|
|
|
$data = $response->get_data(); |
596
|
|
|
$expected = array( |
597
|
|
|
'id' => $instance_id, |
598
|
|
|
'instance_id' => $instance_id, |
599
|
|
|
'title' => $method->instance_settings['title'], |
600
|
|
|
'order' => $method->method_order, |
601
|
|
|
'enabled' => ( 'yes' === $method->enabled ), |
602
|
|
|
'method_id' => $method->id, |
603
|
|
|
'method_title' => $method->method_title, |
604
|
|
|
'method_description' => $method->method_description, |
605
|
|
|
'settings' => $settings, |
606
|
|
|
'_links' => array( |
607
|
|
|
'self' => array( |
608
|
|
|
array( |
609
|
|
|
'href' => rest_url( '/wc/v2/shipping/zones/' . $zone->get_id() . '/methods/' . $instance_id ), |
610
|
|
|
), |
611
|
|
|
), |
612
|
|
|
'collection' => array( |
613
|
|
|
array( |
614
|
|
|
'href' => rest_url( '/wc/v2/shipping/zones/' . $zone->get_id() . '/methods' ), |
615
|
|
|
), |
616
|
|
|
), |
617
|
|
|
'describes' => array( |
618
|
|
|
array( |
619
|
|
|
'href' => rest_url( '/wc/v2/shipping/zones/' . $zone->get_id() ), |
620
|
|
|
), |
621
|
|
|
), |
622
|
|
|
), |
623
|
|
|
); |
624
|
|
|
|
625
|
|
|
$this->assertEquals( 200, $response->get_status() ); |
626
|
|
|
$this->assertEquals( count( $data ), 1 ); |
627
|
|
|
$this->assertContains( $expected, $data ); |
628
|
|
|
|
629
|
|
|
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/shipping/zones/' . $zone->get_id() . '/methods/' . $instance_id ) ); |
630
|
|
|
$data = $response->get_data(); |
631
|
|
|
|
632
|
|
|
$this->assertEquals( 200, $response->get_status() ); |
633
|
|
|
$this->assertEquals( $expected, $data ); |
634
|
|
|
} |
635
|
|
|
|
636
|
|
|
/** |
637
|
|
|
* Test getting all Shipping Zone Methods with a bad zone ID. |
638
|
|
|
* @since 3.0.0 |
639
|
|
|
*/ |
640
|
|
|
public function test_get_methods_invalid_zone_id() { |
641
|
|
|
wp_set_current_user( $this->user ); |
642
|
|
|
|
643
|
|
|
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/shipping/zones/1/methods' ) ); |
644
|
|
|
|
645
|
|
|
$this->assertEquals( 404, $response->get_status() ); |
646
|
|
|
|
647
|
|
|
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/shipping/zones/1/methods/1' ) ); |
648
|
|
|
|
649
|
|
|
$this->assertEquals( 404, $response->get_status() ); |
650
|
|
|
} |
651
|
|
|
|
652
|
|
|
/** |
653
|
|
|
* Test getting a single Shipping Zone Method with a bad ID. |
654
|
|
|
* @since 3.0.0 |
655
|
|
|
*/ |
656
|
|
|
public function test_get_methods_invalid_method_id() { |
657
|
|
|
wp_set_current_user( $this->user ); |
658
|
|
|
|
659
|
|
|
$zone = $this->create_shipping_zone( 'Zone 1' ); |
660
|
|
|
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/shipping/zones/' . $zone->get_id() . '/methods/1' ) ); |
661
|
|
|
|
662
|
|
|
$this->assertEquals( 404, $response->get_status() ); |
663
|
|
|
} |
664
|
|
|
|
665
|
|
|
/** |
666
|
|
|
* Test updating a Shipping Zone Method. |
667
|
|
|
* @since 3.0.0 |
668
|
|
|
*/ |
669
|
|
|
public function test_update_methods() { |
670
|
|
|
wp_set_current_user( $this->user ); |
671
|
|
|
|
672
|
|
|
$zone = $this->create_shipping_zone( 'Zone 1' ); |
673
|
|
|
$instance_id = $zone->add_shipping_method( 'flat_rate' ); |
674
|
|
|
$methods = $zone->get_shipping_methods(); |
675
|
|
|
$method = $methods[ $instance_id ]; |
676
|
|
|
|
677
|
|
|
// Test defaults |
678
|
|
|
$request = new WP_REST_Request( 'GET', '/wc/v2/shipping/zones/' . $zone->get_id() . '/methods/' . $instance_id ); |
679
|
|
|
$response = $this->server->dispatch( $request ); |
680
|
|
|
$data = $response->get_data(); |
681
|
|
|
|
682
|
|
|
$this->assertArrayHasKey( 'title', $data['settings'] ); |
683
|
|
|
$this->assertEquals( 'Flat rate', $data['settings']['title']['value'] ); |
684
|
|
|
$this->assertArrayHasKey( 'tax_status', $data['settings'] ); |
685
|
|
|
$this->assertEquals( 'taxable', $data['settings']['tax_status']['value'] ); |
686
|
|
|
$this->assertArrayHasKey( 'cost', $data['settings'] ); |
687
|
|
|
$this->assertEquals( '0', $data['settings']['cost']['value'] ); |
688
|
|
|
|
689
|
|
|
// Update a single value |
690
|
|
|
$request = new WP_REST_Request( 'POST', '/wc/v2/shipping/zones/' . $zone->get_id() . '/methods/' . $instance_id ); |
691
|
|
|
$request->set_body_params( |
692
|
|
|
array( |
693
|
|
|
'settings' => array( |
694
|
|
|
'cost' => 5, |
695
|
|
|
), |
696
|
|
|
) |
697
|
|
|
); |
698
|
|
|
$response = $this->server->dispatch( $request ); |
699
|
|
|
$data = $response->get_data(); |
700
|
|
|
|
701
|
|
|
$this->assertArrayHasKey( 'title', $data['settings'] ); |
702
|
|
|
$this->assertEquals( 'Flat rate', $data['settings']['title']['value'] ); |
703
|
|
|
$this->assertArrayHasKey( 'tax_status', $data['settings'] ); |
704
|
|
|
$this->assertEquals( 'taxable', $data['settings']['tax_status']['value'] ); |
705
|
|
|
$this->assertArrayHasKey( 'cost', $data['settings'] ); |
706
|
|
|
$this->assertEquals( '5', $data['settings']['cost']['value'] ); |
707
|
|
|
|
708
|
|
|
// Test multiple settings |
709
|
|
|
$request = new WP_REST_Request( 'POST', '/wc/v2/shipping/zones/' . $zone->get_id() . '/methods/' . $instance_id ); |
710
|
|
|
$request->set_body_params( |
711
|
|
|
array( |
712
|
|
|
'settings' => array( |
713
|
|
|
'cost' => 10, |
714
|
|
|
'tax_status' => 'none', |
715
|
|
|
), |
716
|
|
|
) |
717
|
|
|
); |
718
|
|
|
$response = $this->server->dispatch( $request ); |
719
|
|
|
$data = $response->get_data(); |
720
|
|
|
|
721
|
|
|
$this->assertArrayHasKey( 'title', $data['settings'] ); |
722
|
|
|
$this->assertEquals( 'Flat rate', $data['settings']['title']['value'] ); |
723
|
|
|
$this->assertArrayHasKey( 'tax_status', $data['settings'] ); |
724
|
|
|
$this->assertEquals( 'none', $data['settings']['tax_status']['value'] ); |
725
|
|
|
$this->assertArrayHasKey( 'cost', $data['settings'] ); |
726
|
|
|
$this->assertEquals( '10', $data['settings']['cost']['value'] ); |
727
|
|
|
|
728
|
|
|
// Test bogus |
729
|
|
|
$request = new WP_REST_Request( 'POST', '/wc/v2/shipping/zones/' . $zone->get_id() . '/methods/' . $instance_id ); |
730
|
|
|
$request->set_body_params( |
731
|
|
|
array( |
732
|
|
|
'settings' => array( |
733
|
|
|
'cost' => 10, |
734
|
|
|
'tax_status' => 'this_is_not_a_valid_option', |
735
|
|
|
), |
736
|
|
|
) |
737
|
|
|
); |
738
|
|
|
$response = $this->server->dispatch( $request ); |
739
|
|
|
$this->assertEquals( 400, $response->get_status() ); |
740
|
|
|
|
741
|
|
|
// Test other parameters |
742
|
|
|
$this->assertTrue( $data['enabled'] ); |
743
|
|
|
$this->assertEquals( 1, $data['order'] ); |
744
|
|
|
|
745
|
|
|
$request = new WP_REST_Request( 'POST', '/wc/v2/shipping/zones/' . $zone->get_id() . '/methods/' . $instance_id ); |
746
|
|
|
$request->set_body_params( |
747
|
|
|
array( |
748
|
|
|
'enabled' => false, |
749
|
|
|
'order' => 2, |
750
|
|
|
) |
751
|
|
|
); |
752
|
|
|
$response = $this->server->dispatch( $request ); |
753
|
|
|
$data = $response->get_data(); |
754
|
|
|
|
755
|
|
|
$this->assertFalse( $data['enabled'] ); |
756
|
|
|
$this->assertEquals( 2, $data['order'] ); |
757
|
|
|
$this->assertArrayHasKey( 'cost', $data['settings'] ); |
758
|
|
|
$this->assertEquals( '10', $data['settings']['cost']['value'] ); |
759
|
|
|
} |
760
|
|
|
|
761
|
|
|
/** |
762
|
|
|
* Test creating a Shipping Zone Method. |
763
|
|
|
* @since 3.0.0 |
764
|
|
|
*/ |
765
|
|
|
public function test_create_method() { |
766
|
|
|
wp_set_current_user( $this->user ); |
767
|
|
|
$zone = $this->create_shipping_zone( 'Zone 1' ); |
768
|
|
|
$request = new WP_REST_Request( 'POST', '/wc/v2/shipping/zones/' . $zone->get_id() . '/methods' ); |
769
|
|
|
$request->set_body_params( |
770
|
|
|
array( |
771
|
|
|
'method_id' => 'flat_rate', |
772
|
|
|
'enabled' => false, |
773
|
|
|
'order' => 2, |
774
|
|
|
) |
775
|
|
|
); |
776
|
|
|
$response = $this->server->dispatch( $request ); |
777
|
|
|
$data = $response->get_data(); |
778
|
|
|
|
779
|
|
|
$this->assertFalse( $data['enabled'] ); |
780
|
|
|
$this->assertEquals( 2, $data['order'] ); |
781
|
|
|
$this->assertArrayHasKey( 'cost', $data['settings'] ); |
782
|
|
|
$this->assertEquals( '0', $data['settings']['cost']['value'] ); |
783
|
|
|
} |
784
|
|
|
|
785
|
|
|
/** |
786
|
|
|
* Test deleting a Shipping Zone Method. |
787
|
|
|
* @since 3.0.0 |
788
|
|
|
*/ |
789
|
|
|
public function test_delete_method() { |
790
|
|
|
wp_set_current_user( $this->user ); |
791
|
|
|
$zone = $this->create_shipping_zone( 'Zone 1' ); |
792
|
|
|
$instance_id = $zone->add_shipping_method( 'flat_rate' ); |
793
|
|
|
$methods = $zone->get_shipping_methods(); |
794
|
|
|
$method = $methods[ $instance_id ]; |
795
|
|
|
$request = new WP_REST_Request( 'DELETE', '/wc/v2/shipping/zones/' . $zone->get_id() . '/methods/' . $instance_id ); |
796
|
|
|
$request->set_param( 'force', true ); |
797
|
|
|
$response = $this->server->dispatch( $request ); |
798
|
|
|
$this->assertEquals( 200, $response->get_status() ); |
799
|
|
|
} |
800
|
|
|
} |
801
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths