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