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