2019-06-07 15:32:42 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Shipping Zones API Tests
|
|
|
|
*
|
|
|
|
* @package WooCommerce\Tests\API
|
|
|
|
* @since 3.5.0
|
|
|
|
*/
|
2019-06-10 11:36:11 +00:00
|
|
|
|
|
|
|
namespace WooCommerce\RestApi\UnitTests\Tests\Version4;
|
|
|
|
|
|
|
|
defined( 'ABSPATH' ) || exit;
|
|
|
|
|
2019-06-10 12:39:46 +00:00
|
|
|
use \WP_REST_Request;
|
2019-06-10 11:36:11 +00:00
|
|
|
use \WC_REST_Unit_Test_Case;
|
|
|
|
|
|
|
|
class ShippingZones extends WC_REST_Unit_Test_Case {
|
2019-06-07 15:32:42 +00:00
|
|
|
|
|
|
|
protected $server;
|
|
|
|
|
|
|
|
protected $endpoint;
|
|
|
|
|
|
|
|
protected $zones;
|
|
|
|
|
|
|
|
/**
|
2019-06-10 14:08:29 +00:00
|
|
|
* User variable.
|
|
|
|
*
|
|
|
|
* @var WP_User
|
2019-06-07 15:32:42 +00:00
|
|
|
*/
|
2019-06-10 14:08:29 +00:00
|
|
|
protected static $user;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Setup once before running tests.
|
|
|
|
*
|
|
|
|
* @param object $factory Factory object.
|
|
|
|
*/
|
|
|
|
public static function wpSetUpBeforeClass( $factory ) {
|
|
|
|
self::$user = $factory->user->create(
|
2019-06-07 15:32:42 +00:00
|
|
|
array(
|
|
|
|
'role' => 'administrator',
|
|
|
|
)
|
|
|
|
);
|
2019-06-10 14:08:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Setup our test server, endpoints, and user info.
|
|
|
|
*/
|
|
|
|
public function setUp() {
|
|
|
|
parent::setUp();
|
|
|
|
wp_set_current_user( self::$user );
|
|
|
|
$this->zones = array();
|
2019-06-07 15:32:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper method to create a Shipping Zone.
|
|
|
|
*
|
|
|
|
* @param string $name Zone name.
|
|
|
|
* @param int $order Optional. Zone sort order.
|
|
|
|
* @return WC_Shipping_Zone
|
|
|
|
*/
|
|
|
|
protected function create_shipping_zone( $name, $order = 0, $locations = array() ) {
|
2019-06-10 12:39:46 +00:00
|
|
|
$zone = new \WC_Shipping_Zone( null );
|
2019-06-07 15:32:42 +00:00
|
|
|
$zone->set_zone_name( $name );
|
|
|
|
$zone->set_zone_order( $order );
|
|
|
|
$zone->set_locations( $locations );
|
|
|
|
$zone->save();
|
|
|
|
|
|
|
|
$this->zones[] = $zone;
|
|
|
|
|
|
|
|
return $zone;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test route registration.
|
|
|
|
*
|
|
|
|
* @since 3.5.0
|
|
|
|
*/
|
|
|
|
public function test_register_routes() {
|
|
|
|
$routes = $this->server->get_routes();
|
2019-06-10 11:36:11 +00:00
|
|
|
$this->assertArrayHasKey( '/wc/v4/shipping/zones', $routes );
|
|
|
|
$this->assertArrayHasKey( '/wc/v4/shipping/zones/(?P<id>[\d-]+)', $routes );
|
|
|
|
$this->assertArrayHasKey( '/wc/v4/shipping/zones/(?P<id>[\d]+)/locations', $routes );
|
|
|
|
$this->assertArrayHasKey( '/wc/v4/shipping/zones/(?P<zone_id>[\d]+)/methods', $routes );
|
|
|
|
$this->assertArrayHasKey( '/wc/v4/shipping/zones/(?P<zone_id>[\d]+)/methods/(?P<instance_id>[\d]+)', $routes );
|
2019-06-07 15:32:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test getting all Shipping Zones.
|
|
|
|
*
|
|
|
|
* @since 3.5.0
|
|
|
|
*/
|
|
|
|
public function test_get_zones() {
|
|
|
|
// "Rest of the World" zone exists by default
|
2019-06-10 11:36:11 +00:00
|
|
|
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v4/shipping/zones' ) );
|
2019-06-07 15:32:42 +00:00
|
|
|
$data = $response->get_data();
|
|
|
|
|
|
|
|
$this->assertEquals( 200, $response->get_status() );
|
|
|
|
$this->assertEquals( count( $data ), 1 );
|
|
|
|
$this->assertContains(
|
|
|
|
array(
|
|
|
|
'id' => $data[0]['id'],
|
|
|
|
'name' => 'Locations not covered by your other zones',
|
|
|
|
'order' => 0,
|
|
|
|
'_links' => array(
|
|
|
|
'self' => array(
|
|
|
|
array(
|
2019-06-10 11:36:11 +00:00
|
|
|
'href' => rest_url( '/wc/v4/shipping/zones/' . $data[0]['id'] ),
|
2019-06-07 15:32:42 +00:00
|
|
|
),
|
|
|
|
),
|
|
|
|
'collection' => array(
|
|
|
|
array(
|
2019-06-10 11:36:11 +00:00
|
|
|
'href' => rest_url( '/wc/v4/shipping/zones' ),
|
2019-06-07 15:32:42 +00:00
|
|
|
),
|
|
|
|
),
|
|
|
|
'describedby' => array(
|
|
|
|
array(
|
2019-06-10 11:36:11 +00:00
|
|
|
'href' => rest_url( '/wc/v4/shipping/zones/' . $data[0]['id'] . '/locations' ),
|
2019-06-07 15:32:42 +00:00
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
$data
|
|
|
|
);
|
|
|
|
|
|
|
|
// Create a zone and make sure it's in the response
|
|
|
|
$this->create_shipping_zone( 'Zone 1' );
|
|
|
|
|
2019-06-10 11:36:11 +00:00
|
|
|
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v4/shipping/zones' ) );
|
2019-06-07 15:32:42 +00:00
|
|
|
$data = $response->get_data();
|
|
|
|
|
|
|
|
$this->assertEquals( 200, $response->get_status() );
|
|
|
|
$this->assertEquals( count( $data ), 2 );
|
|
|
|
$this->assertContains(
|
|
|
|
array(
|
|
|
|
'id' => $data[1]['id'],
|
|
|
|
'name' => 'Zone 1',
|
|
|
|
'order' => 0,
|
|
|
|
'_links' => array(
|
|
|
|
'self' => array(
|
|
|
|
array(
|
2019-06-10 11:36:11 +00:00
|
|
|
'href' => rest_url( '/wc/v4/shipping/zones/' . $data[1]['id'] ),
|
2019-06-07 15:32:42 +00:00
|
|
|
),
|
|
|
|
),
|
|
|
|
'collection' => array(
|
|
|
|
array(
|
2019-06-10 11:36:11 +00:00
|
|
|
'href' => rest_url( '/wc/v4/shipping/zones' ),
|
2019-06-07 15:32:42 +00:00
|
|
|
),
|
|
|
|
),
|
|
|
|
'describedby' => array(
|
|
|
|
array(
|
2019-06-10 11:36:11 +00:00
|
|
|
'href' => rest_url( '/wc/v4/shipping/zones/' . $data[1]['id'] . '/locations' ),
|
2019-06-07 15:32:42 +00:00
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
$data
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test /shipping/zones without valid permissions/creds.
|
|
|
|
*
|
|
|
|
* @since 3.5.0
|
|
|
|
*/
|
|
|
|
public function test_get_shipping_zones_without_permission() {
|
|
|
|
wp_set_current_user( 0 );
|
|
|
|
|
2019-06-10 11:36:11 +00:00
|
|
|
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v4/shipping/zones' ) );
|
2019-06-07 15:32:42 +00:00
|
|
|
$this->assertEquals( 401, $response->get_status() );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test /shipping/zones while Shipping is disabled in WooCommerce.
|
|
|
|
*
|
|
|
|
* @since 3.5.0
|
|
|
|
*/
|
|
|
|
public function test_get_shipping_zones_disabled_shipping() {
|
|
|
|
add_filter( 'wc_shipping_enabled', '__return_false' );
|
|
|
|
|
2019-06-10 11:36:11 +00:00
|
|
|
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v4/shipping/zones' ) );
|
2019-06-07 15:32:42 +00:00
|
|
|
$this->assertEquals( 404, $response->get_status() );
|
|
|
|
|
|
|
|
remove_filter( 'wc_shipping_enabled', '__return_false' );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test Shipping Zone schema.
|
|
|
|
*
|
|
|
|
* @since 3.5.0
|
|
|
|
*/
|
|
|
|
public function test_get_shipping_zone_schema() {
|
2019-06-10 11:36:11 +00:00
|
|
|
$request = new WP_REST_Request( 'OPTIONS', '/wc/v4/shipping/zones' );
|
2019-06-07 15:32:42 +00:00
|
|
|
$response = $this->server->dispatch( $request );
|
|
|
|
$data = $response->get_data();
|
|
|
|
$properties = $data['schema']['properties'];
|
|
|
|
$this->assertEquals( 3, count( $properties ) );
|
|
|
|
$this->assertArrayHasKey( 'id', $properties );
|
|
|
|
$this->assertTrue( $properties['id']['readonly'] );
|
|
|
|
$this->assertArrayHasKey( 'name', $properties );
|
|
|
|
$this->assertArrayHasKey( 'order', $properties );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test Shipping Zone create endpoint.
|
|
|
|
*
|
|
|
|
* @since 3.5.0
|
|
|
|
*/
|
|
|
|
public function test_create_shipping_zone() {
|
2019-06-10 11:36:11 +00:00
|
|
|
$request = new WP_REST_Request( 'POST', '/wc/v4/shipping/zones' );
|
2019-06-07 15:32:42 +00:00
|
|
|
$request->set_body_params(
|
|
|
|
array(
|
|
|
|
'name' => 'Test Zone',
|
|
|
|
'order' => 1,
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$response = $this->server->dispatch( $request );
|
|
|
|
$data = $response->get_data();
|
|
|
|
|
|
|
|
$this->assertEquals( 201, $response->get_status() );
|
|
|
|
$this->assertEquals(
|
|
|
|
array(
|
|
|
|
'id' => $data['id'],
|
|
|
|
'name' => 'Test Zone',
|
|
|
|
'order' => 1,
|
|
|
|
'_links' => array(
|
|
|
|
'self' => array(
|
|
|
|
array(
|
2019-06-10 11:36:11 +00:00
|
|
|
'href' => rest_url( '/wc/v4/shipping/zones/' . $data['id'] ),
|
2019-06-07 15:32:42 +00:00
|
|
|
),
|
|
|
|
),
|
|
|
|
'collection' => array(
|
|
|
|
array(
|
2019-06-10 11:36:11 +00:00
|
|
|
'href' => rest_url( '/wc/v4/shipping/zones' ),
|
2019-06-07 15:32:42 +00:00
|
|
|
),
|
|
|
|
),
|
|
|
|
'describedby' => array(
|
|
|
|
array(
|
2019-06-10 11:36:11 +00:00
|
|
|
'href' => rest_url( '/wc/v4/shipping/zones/' . $data['id'] . '/locations' ),
|
2019-06-07 15:32:42 +00:00
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
$data
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test Shipping Zone create endpoint.
|
|
|
|
*
|
|
|
|
* @since 3.5.0
|
|
|
|
*/
|
|
|
|
public function test_create_shipping_zone_without_permission() {
|
|
|
|
wp_set_current_user( 0 );
|
|
|
|
|
2019-06-10 11:36:11 +00:00
|
|
|
$request = new WP_REST_Request( 'POST', '/wc/v4/shipping/zones' );
|
2019-06-07 15:32:42 +00:00
|
|
|
$request->set_body_params(
|
|
|
|
array(
|
|
|
|
'name' => 'Test Zone',
|
|
|
|
'order' => 1,
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$response = $this->server->dispatch( $request );
|
|
|
|
$this->assertEquals( 401, $response->get_status() );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test Shipping Zone update endpoint.
|
|
|
|
*
|
|
|
|
* @since 3.5.0
|
|
|
|
*/
|
|
|
|
public function test_update_shipping_zone() {
|
|
|
|
$zone = $this->create_shipping_zone( 'Test Zone' );
|
|
|
|
|
2019-06-10 11:36:11 +00:00
|
|
|
$request = new WP_REST_Request( 'PUT', '/wc/v4/shipping/zones/' . $zone->get_id() );
|
2019-06-07 15:32:42 +00:00
|
|
|
$request->set_body_params(
|
|
|
|
array(
|
|
|
|
'name' => 'Zone Test',
|
|
|
|
'order' => 2,
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$response = $this->server->dispatch( $request );
|
|
|
|
$data = $response->get_data();
|
|
|
|
|
|
|
|
$this->assertEquals( 200, $response->get_status() );
|
|
|
|
$this->assertEquals(
|
|
|
|
array(
|
|
|
|
'id' => $zone->get_id(),
|
|
|
|
'name' => 'Zone Test',
|
|
|
|
'order' => 2,
|
|
|
|
'_links' => array(
|
|
|
|
'self' => array(
|
|
|
|
array(
|
2019-06-10 11:36:11 +00:00
|
|
|
'href' => rest_url( '/wc/v4/shipping/zones/' . $zone->get_id() ),
|
2019-06-07 15:32:42 +00:00
|
|
|
),
|
|
|
|
),
|
|
|
|
'collection' => array(
|
|
|
|
array(
|
2019-06-10 11:36:11 +00:00
|
|
|
'href' => rest_url( '/wc/v4/shipping/zones' ),
|
2019-06-07 15:32:42 +00:00
|
|
|
),
|
|
|
|
),
|
|
|
|
'describedby' => array(
|
|
|
|
array(
|
2019-06-10 11:36:11 +00:00
|
|
|
'href' => rest_url( '/wc/v4/shipping/zones/' . $zone->get_id() . '/locations' ),
|
2019-06-07 15:32:42 +00:00
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
$data
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test Shipping Zone update endpoint with a bad zone ID.
|
|
|
|
*
|
|
|
|
* @since 3.5.0
|
|
|
|
*/
|
|
|
|
public function test_update_shipping_zone_invalid_id() {
|
2019-06-10 11:36:11 +00:00
|
|
|
$request = new WP_REST_Request( 'PUT', '/wc/v4/shipping/zones/555555' );
|
2019-06-07 15:32:42 +00:00
|
|
|
$request->set_body_params(
|
|
|
|
array(
|
|
|
|
'name' => 'Zone Test',
|
|
|
|
'order' => 2,
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$response = $this->server->dispatch( $request );
|
|
|
|
|
|
|
|
$this->assertEquals( 404, $response->get_status() );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test Shipping Zone delete endpoint.
|
|
|
|
*
|
|
|
|
* @since 3.5.0
|
|
|
|
*/
|
|
|
|
public function test_delete_shipping_zone() {
|
|
|
|
$zone = $this->create_shipping_zone( 'Zone 1' );
|
|
|
|
|
2019-06-10 11:36:11 +00:00
|
|
|
$request = new WP_REST_Request( 'DELETE', '/wc/v4/shipping/zones/' . $zone->get_id() );
|
2019-06-07 15:32:42 +00:00
|
|
|
$request->set_param( 'force', true );
|
|
|
|
$response = $this->server->dispatch( $request );
|
|
|
|
$data = $response->get_data();
|
|
|
|
|
|
|
|
$this->assertEquals( 200, $response->get_status() );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test Shipping Zone delete endpoint without permissions.
|
|
|
|
*
|
|
|
|
* @since 3.5.0
|
|
|
|
*/
|
|
|
|
public function test_delete_shipping_zone_without_permission() {
|
|
|
|
wp_set_current_user( 0 );
|
|
|
|
$zone = $this->create_shipping_zone( 'Zone 1' );
|
|
|
|
|
2019-06-10 11:36:11 +00:00
|
|
|
$request = new WP_REST_Request( 'DELETE', '/wc/v4/shipping/zones/' . $zone->get_id() );
|
2019-06-07 15:32:42 +00:00
|
|
|
$request->set_param( 'force', true );
|
|
|
|
$response = $this->server->dispatch( $request );
|
|
|
|
$this->assertEquals( 401, $response->get_status() );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test Shipping Zone delete endpoint with a bad zone ID.
|
|
|
|
*
|
|
|
|
* @since 3.5.0
|
|
|
|
*/
|
|
|
|
public function test_delete_shipping_zone_invalid_id() {
|
2019-06-10 11:36:11 +00:00
|
|
|
$request = new WP_REST_Request( 'DELETE', '/wc/v4/shipping/zones/555555' );
|
2019-06-07 15:32:42 +00:00
|
|
|
$response = $this->server->dispatch( $request );
|
|
|
|
$this->assertEquals( 404, $response->get_status() );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test getting a single Shipping Zone.
|
|
|
|
*
|
|
|
|
* @since 3.5.0
|
|
|
|
*/
|
|
|
|
public function test_get_single_shipping_zone() {
|
|
|
|
$zone = $this->create_shipping_zone( 'Test Zone' );
|
2019-06-10 11:36:11 +00:00
|
|
|
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v4/shipping/zones/' . $zone->get_id() ) );
|
2019-06-07 15:32:42 +00:00
|
|
|
$data = $response->get_data();
|
|
|
|
|
|
|
|
$this->assertEquals( 200, $response->get_status() );
|
|
|
|
$this->assertEquals(
|
|
|
|
array(
|
|
|
|
'id' => $zone->get_id(),
|
|
|
|
'name' => 'Test Zone',
|
|
|
|
'order' => 0,
|
|
|
|
'_links' => array(
|
|
|
|
'self' => array(
|
|
|
|
array(
|
2019-06-10 11:36:11 +00:00
|
|
|
'href' => rest_url( '/wc/v4/shipping/zones/' . $zone->get_id() ),
|
2019-06-07 15:32:42 +00:00
|
|
|
),
|
|
|
|
),
|
|
|
|
'collection' => array(
|
|
|
|
array(
|
2019-06-10 11:36:11 +00:00
|
|
|
'href' => rest_url( '/wc/v4/shipping/zones' ),
|
2019-06-07 15:32:42 +00:00
|
|
|
),
|
|
|
|
),
|
|
|
|
'describedby' => array(
|
|
|
|
array(
|
2019-06-10 11:36:11 +00:00
|
|
|
'href' => rest_url( '/wc/v4/shipping/zones/' . $zone->get_id() . '/locations' ),
|
2019-06-07 15:32:42 +00:00
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
$data
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test getting a single Shipping Zone with a bad zone ID.
|
|
|
|
*
|
|
|
|
* @since 3.5.0
|
|
|
|
*/
|
|
|
|
public function test_get_single_shipping_zone_invalid_id() {
|
2019-06-10 11:36:11 +00:00
|
|
|
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v4/shipping/zones/1' ) );
|
2019-06-07 15:32:42 +00:00
|
|
|
|
|
|
|
$this->assertEquals( 404, $response->get_status() );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test getting Shipping Zone Locations.
|
|
|
|
*
|
|
|
|
* @since 3.5.0
|
|
|
|
*/
|
|
|
|
public function test_get_locations() {
|
|
|
|
// Create a zone
|
|
|
|
$zone = $this->create_shipping_zone(
|
|
|
|
'Zone 1',
|
|
|
|
0,
|
|
|
|
array(
|
|
|
|
array(
|
|
|
|
'code' => 'US',
|
|
|
|
'type' => 'country',
|
|
|
|
),
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
2019-06-10 11:36:11 +00:00
|
|
|
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v4/shipping/zones/' . $zone->get_id() . '/locations' ) );
|
2019-06-07 15:32:42 +00:00
|
|
|
$data = $response->get_data();
|
|
|
|
|
|
|
|
$this->assertEquals( 200, $response->get_status() );
|
|
|
|
$this->assertEquals( count( $data ), 1 );
|
|
|
|
$this->assertEquals(
|
|
|
|
array(
|
|
|
|
array(
|
|
|
|
'code' => 'US',
|
|
|
|
'type' => 'country',
|
|
|
|
'_links' => array(
|
|
|
|
'collection' => array(
|
|
|
|
array(
|
2019-06-10 11:36:11 +00:00
|
|
|
'href' => rest_url( '/wc/v4/shipping/zones/' . $zone->get_id() . '/locations' ),
|
2019-06-07 15:32:42 +00:00
|
|
|
),
|
|
|
|
),
|
|
|
|
'describes' => array(
|
|
|
|
array(
|
2019-06-10 11:36:11 +00:00
|
|
|
'href' => rest_url( '/wc/v4/shipping/zones/' . $zone->get_id() ),
|
2019-06-07 15:32:42 +00:00
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
$data
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test getting Shipping Zone Locations with a bad zone ID.
|
|
|
|
*
|
|
|
|
* @since 3.5.0
|
|
|
|
*/
|
|
|
|
public function test_get_locations_invalid_id() {
|
2019-06-10 11:36:11 +00:00
|
|
|
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v4/shipping/zones/1/locations' ) );
|
2019-06-07 15:32:42 +00:00
|
|
|
|
|
|
|
$this->assertEquals( 404, $response->get_status() );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test Shipping Zone Locations update endpoint.
|
|
|
|
*
|
|
|
|
* @since 3.5.0
|
|
|
|
*/
|
|
|
|
public function test_update_locations() {
|
|
|
|
$zone = $this->create_shipping_zone( 'Test Zone' );
|
|
|
|
|
2019-06-10 11:36:11 +00:00
|
|
|
$request = new WP_REST_Request( 'PUT', '/wc/v4/shipping/zones/' . $zone->get_id() . '/locations' );
|
2019-06-07 15:32:42 +00:00
|
|
|
$request->add_header( 'Content-Type', 'application/json' );
|
|
|
|
$request->set_body(
|
|
|
|
json_encode(
|
|
|
|
array(
|
|
|
|
array(
|
|
|
|
'code' => 'UK',
|
|
|
|
'type' => 'country',
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'code' => 'US', // test that locations missing "type" treated as country.
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'code' => 'SW1A0AA',
|
|
|
|
'type' => 'postcode',
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'type' => 'continent', // test that locations missing "code" aren't saved
|
|
|
|
),
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$response = $this->server->dispatch( $request );
|
|
|
|
$data = $response->get_data();
|
|
|
|
|
|
|
|
$this->assertEquals( 3, count( $data ) );
|
|
|
|
$this->assertEquals(
|
|
|
|
array(
|
|
|
|
array(
|
|
|
|
'code' => 'UK',
|
|
|
|
'type' => 'country',
|
|
|
|
'_links' => array(
|
|
|
|
'collection' => array(
|
|
|
|
array(
|
2019-06-10 11:36:11 +00:00
|
|
|
'href' => rest_url( '/wc/v4/shipping/zones/' . $zone->get_id() . '/locations' ),
|
2019-06-07 15:32:42 +00:00
|
|
|
),
|
|
|
|
),
|
|
|
|
'describes' => array(
|
|
|
|
array(
|
2019-06-10 11:36:11 +00:00
|
|
|
'href' => rest_url( '/wc/v4/shipping/zones/' . $zone->get_id() ),
|
2019-06-07 15:32:42 +00:00
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'code' => 'US',
|
|
|
|
'type' => 'country',
|
|
|
|
'_links' => array(
|
|
|
|
'collection' => array(
|
|
|
|
array(
|
2019-06-10 11:36:11 +00:00
|
|
|
'href' => rest_url( '/wc/v4/shipping/zones/' . $zone->get_id() . '/locations' ),
|
2019-06-07 15:32:42 +00:00
|
|
|
),
|
|
|
|
),
|
|
|
|
'describes' => array(
|
|
|
|
array(
|
2019-06-10 11:36:11 +00:00
|
|
|
'href' => rest_url( '/wc/v4/shipping/zones/' . $zone->get_id() ),
|
2019-06-07 15:32:42 +00:00
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'code' => 'SW1A0AA',
|
|
|
|
'type' => 'postcode',
|
|
|
|
'_links' => array(
|
|
|
|
'collection' => array(
|
|
|
|
array(
|
2019-06-10 11:36:11 +00:00
|
|
|
'href' => rest_url( '/wc/v4/shipping/zones/' . $zone->get_id() . '/locations' ),
|
2019-06-07 15:32:42 +00:00
|
|
|
),
|
|
|
|
),
|
|
|
|
'describes' => array(
|
|
|
|
array(
|
2019-06-10 11:36:11 +00:00
|
|
|
'href' => rest_url( '/wc/v4/shipping/zones/' . $zone->get_id() ),
|
2019-06-07 15:32:42 +00:00
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
$data
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test updating Shipping Zone Locations with a bad zone ID.
|
|
|
|
*
|
|
|
|
* @since 3.5.0
|
|
|
|
*/
|
|
|
|
public function test_update_locations_invalid_id() {
|
2019-06-10 11:36:11 +00:00
|
|
|
$response = $this->server->dispatch( new WP_REST_Request( 'PUT', '/wc/v4/shipping/zones/1/locations' ) );
|
2019-06-07 15:32:42 +00:00
|
|
|
|
|
|
|
$this->assertEquals( 404, $response->get_status() );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test getting all Shipping Zone Methods and getting a single Shipping Zone Method.
|
|
|
|
*
|
|
|
|
* @since 3.5.0
|
|
|
|
*/
|
|
|
|
public function test_get_methods() {
|
|
|
|
// Create a shipping method and make sure it's in the response
|
|
|
|
$zone = $this->create_shipping_zone( 'Zone 1' );
|
|
|
|
$instance_id = $zone->add_shipping_method( 'flat_rate' );
|
|
|
|
$methods = $zone->get_shipping_methods();
|
|
|
|
$method = $methods[ $instance_id ];
|
|
|
|
|
|
|
|
$settings = array();
|
|
|
|
$method->init_instance_settings();
|
|
|
|
foreach ( $method->get_instance_form_fields() as $id => $field ) {
|
|
|
|
$data = array(
|
|
|
|
'id' => $id,
|
|
|
|
'label' => $field['title'],
|
|
|
|
'description' => ( empty( $field['description'] ) ? '' : $field['description'] ),
|
|
|
|
'type' => $field['type'],
|
|
|
|
'value' => $method->instance_settings[ $id ],
|
|
|
|
'default' => ( empty( $field['default'] ) ? '' : $field['default'] ),
|
|
|
|
'tip' => ( empty( $field['description'] ) ? '' : $field['description'] ),
|
|
|
|
'placeholder' => ( empty( $field['placeholder'] ) ? '' : $field['placeholder'] ),
|
|
|
|
);
|
|
|
|
if ( ! empty( $field['options'] ) ) {
|
|
|
|
$data['options'] = $field['options'];
|
|
|
|
}
|
|
|
|
$settings[ $id ] = $data;
|
|
|
|
}
|
|
|
|
|
2019-06-10 11:36:11 +00:00
|
|
|
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v4/shipping/zones/' . $zone->get_id() . '/methods' ) );
|
2019-06-07 15:32:42 +00:00
|
|
|
$data = $response->get_data();
|
|
|
|
$expected = array(
|
|
|
|
'id' => $instance_id,
|
|
|
|
'instance_id' => $instance_id,
|
|
|
|
'title' => $method->instance_settings['title'],
|
|
|
|
'order' => $method->method_order,
|
|
|
|
'enabled' => ( 'yes' === $method->enabled ),
|
|
|
|
'method_id' => $method->id,
|
|
|
|
'method_title' => $method->method_title,
|
|
|
|
'method_description' => $method->method_description,
|
|
|
|
'settings' => $settings,
|
|
|
|
'_links' => array(
|
|
|
|
'self' => array(
|
|
|
|
array(
|
2019-06-10 11:36:11 +00:00
|
|
|
'href' => rest_url( '/wc/v4/shipping/zones/' . $zone->get_id() . '/methods/' . $instance_id ),
|
2019-06-07 15:32:42 +00:00
|
|
|
),
|
|
|
|
),
|
|
|
|
'collection' => array(
|
|
|
|
array(
|
2019-06-10 11:36:11 +00:00
|
|
|
'href' => rest_url( '/wc/v4/shipping/zones/' . $zone->get_id() . '/methods' ),
|
2019-06-07 15:32:42 +00:00
|
|
|
),
|
|
|
|
),
|
|
|
|
'describes' => array(
|
|
|
|
array(
|
2019-06-10 11:36:11 +00:00
|
|
|
'href' => rest_url( '/wc/v4/shipping/zones/' . $zone->get_id() ),
|
2019-06-07 15:32:42 +00:00
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertEquals( 200, $response->get_status() );
|
|
|
|
$this->assertEquals( count( $data ), 1 );
|
|
|
|
$this->assertContains( $expected, $data );
|
|
|
|
|
2019-06-10 11:36:11 +00:00
|
|
|
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v4/shipping/zones/' . $zone->get_id() . '/methods/' . $instance_id ) );
|
2019-06-07 15:32:42 +00:00
|
|
|
$data = $response->get_data();
|
|
|
|
|
|
|
|
$this->assertEquals( 200, $response->get_status() );
|
|
|
|
$this->assertEquals( $expected, $data );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test getting all Shipping Zone Methods with a bad zone ID.
|
|
|
|
*
|
|
|
|
* @since 3.5.0
|
|
|
|
*/
|
|
|
|
public function test_get_methods_invalid_zone_id() {
|
2019-06-10 11:36:11 +00:00
|
|
|
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v4/shipping/zones/1/methods' ) );
|
2019-06-07 15:32:42 +00:00
|
|
|
|
|
|
|
$this->assertEquals( 404, $response->get_status() );
|
|
|
|
|
2019-06-10 11:36:11 +00:00
|
|
|
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v4/shipping/zones/1/methods/1' ) );
|
2019-06-07 15:32:42 +00:00
|
|
|
|
|
|
|
$this->assertEquals( 404, $response->get_status() );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test getting a single Shipping Zone Method with a bad ID.
|
|
|
|
*
|
|
|
|
* @since 3.5.0
|
|
|
|
*/
|
|
|
|
public function test_get_methods_invalid_method_id() {
|
|
|
|
$zone = $this->create_shipping_zone( 'Zone 1' );
|
2019-06-10 11:36:11 +00:00
|
|
|
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v4/shipping/zones/' . $zone->get_id() . '/methods/1' ) );
|
2019-06-07 15:32:42 +00:00
|
|
|
|
|
|
|
$this->assertEquals( 404, $response->get_status() );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test updating a Shipping Zone Method.
|
|
|
|
*
|
|
|
|
* @since 3.5.0
|
|
|
|
*/
|
|
|
|
public function test_update_methods() {
|
|
|
|
$zone = $this->create_shipping_zone( 'Zone 1' );
|
|
|
|
$instance_id = $zone->add_shipping_method( 'flat_rate' );
|
|
|
|
$methods = $zone->get_shipping_methods();
|
|
|
|
$method = $methods[ $instance_id ];
|
|
|
|
|
|
|
|
// Test defaults
|
2019-06-10 11:36:11 +00:00
|
|
|
$request = new WP_REST_Request( 'GET', '/wc/v4/shipping/zones/' . $zone->get_id() . '/methods/' . $instance_id );
|
2019-06-07 15:32:42 +00:00
|
|
|
$response = $this->server->dispatch( $request );
|
|
|
|
$data = $response->get_data();
|
|
|
|
|
|
|
|
$this->assertArrayHasKey( 'title', $data['settings'] );
|
|
|
|
$this->assertEquals( 'Flat rate', $data['settings']['title']['value'] );
|
|
|
|
$this->assertArrayHasKey( 'tax_status', $data['settings'] );
|
|
|
|
$this->assertEquals( 'taxable', $data['settings']['tax_status']['value'] );
|
|
|
|
$this->assertArrayHasKey( 'cost', $data['settings'] );
|
|
|
|
$this->assertEquals( '0', $data['settings']['cost']['value'] );
|
|
|
|
|
|
|
|
// Update a single value
|
2019-06-10 11:36:11 +00:00
|
|
|
$request = new WP_REST_Request( 'POST', '/wc/v4/shipping/zones/' . $zone->get_id() . '/methods/' . $instance_id );
|
2019-06-07 15:32:42 +00:00
|
|
|
$request->set_body_params(
|
|
|
|
array(
|
|
|
|
'settings' => array(
|
|
|
|
'cost' => 5,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$response = $this->server->dispatch( $request );
|
|
|
|
$data = $response->get_data();
|
|
|
|
|
|
|
|
$this->assertArrayHasKey( 'title', $data['settings'] );
|
|
|
|
$this->assertEquals( 'Flat rate', $data['settings']['title']['value'] );
|
|
|
|
$this->assertArrayHasKey( 'tax_status', $data['settings'] );
|
|
|
|
$this->assertEquals( 'taxable', $data['settings']['tax_status']['value'] );
|
|
|
|
$this->assertArrayHasKey( 'cost', $data['settings'] );
|
|
|
|
$this->assertEquals( '5', $data['settings']['cost']['value'] );
|
|
|
|
|
|
|
|
// Test multiple settings
|
2019-06-10 11:36:11 +00:00
|
|
|
$request = new WP_REST_Request( 'POST', '/wc/v4/shipping/zones/' . $zone->get_id() . '/methods/' . $instance_id );
|
2019-06-07 15:32:42 +00:00
|
|
|
$request->set_body_params(
|
|
|
|
array(
|
|
|
|
'settings' => array(
|
|
|
|
'cost' => 10,
|
|
|
|
'tax_status' => 'none',
|
|
|
|
),
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$response = $this->server->dispatch( $request );
|
|
|
|
$data = $response->get_data();
|
|
|
|
|
|
|
|
$this->assertArrayHasKey( 'title', $data['settings'] );
|
|
|
|
$this->assertEquals( 'Flat rate', $data['settings']['title']['value'] );
|
|
|
|
$this->assertArrayHasKey( 'tax_status', $data['settings'] );
|
|
|
|
$this->assertEquals( 'none', $data['settings']['tax_status']['value'] );
|
|
|
|
$this->assertArrayHasKey( 'cost', $data['settings'] );
|
|
|
|
$this->assertEquals( '10', $data['settings']['cost']['value'] );
|
|
|
|
|
|
|
|
// Test bogus
|
2019-06-10 11:36:11 +00:00
|
|
|
$request = new WP_REST_Request( 'POST', '/wc/v4/shipping/zones/' . $zone->get_id() . '/methods/' . $instance_id );
|
2019-06-07 15:32:42 +00:00
|
|
|
$request->set_body_params(
|
|
|
|
array(
|
|
|
|
'settings' => array(
|
|
|
|
'cost' => 10,
|
|
|
|
'tax_status' => 'this_is_not_a_valid_option',
|
|
|
|
),
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$response = $this->server->dispatch( $request );
|
|
|
|
$this->assertEquals( 400, $response->get_status() );
|
|
|
|
|
|
|
|
// Test other parameters
|
|
|
|
$this->assertTrue( $data['enabled'] );
|
|
|
|
$this->assertEquals( 1, $data['order'] );
|
|
|
|
|
2019-06-10 11:36:11 +00:00
|
|
|
$request = new WP_REST_Request( 'POST', '/wc/v4/shipping/zones/' . $zone->get_id() . '/methods/' . $instance_id );
|
2019-06-07 15:32:42 +00:00
|
|
|
$request->set_body_params(
|
|
|
|
array(
|
|
|
|
'enabled' => false,
|
|
|
|
'order' => 2,
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$response = $this->server->dispatch( $request );
|
|
|
|
$data = $response->get_data();
|
|
|
|
|
|
|
|
$this->assertFalse( $data['enabled'] );
|
|
|
|
$this->assertEquals( 2, $data['order'] );
|
|
|
|
$this->assertArrayHasKey( 'cost', $data['settings'] );
|
|
|
|
$this->assertEquals( '10', $data['settings']['cost']['value'] );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test creating a Shipping Zone Method.
|
|
|
|
*
|
|
|
|
* @since 3.5.0
|
|
|
|
*/
|
|
|
|
public function test_create_method() {
|
|
|
|
$zone = $this->create_shipping_zone( 'Zone 1' );
|
2019-06-10 11:36:11 +00:00
|
|
|
$request = new WP_REST_Request( 'POST', '/wc/v4/shipping/zones/' . $zone->get_id() . '/methods' );
|
2019-06-07 15:32:42 +00:00
|
|
|
$request->set_body_params(
|
|
|
|
array(
|
|
|
|
'method_id' => 'flat_rate',
|
|
|
|
'enabled' => false,
|
|
|
|
'order' => 2,
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$response = $this->server->dispatch( $request );
|
|
|
|
$data = $response->get_data();
|
|
|
|
|
|
|
|
$this->assertFalse( $data['enabled'] );
|
|
|
|
$this->assertEquals( 2, $data['order'] );
|
|
|
|
$this->assertArrayHasKey( 'cost', $data['settings'] );
|
|
|
|
$this->assertEquals( '0', $data['settings']['cost']['value'] );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test deleting a Shipping Zone Method.
|
|
|
|
*
|
|
|
|
* @since 3.5.0
|
|
|
|
*/
|
|
|
|
public function test_delete_method() {
|
|
|
|
$zone = $this->create_shipping_zone( 'Zone 1' );
|
|
|
|
$instance_id = $zone->add_shipping_method( 'flat_rate' );
|
|
|
|
$methods = $zone->get_shipping_methods();
|
|
|
|
$method = $methods[ $instance_id ];
|
2019-06-10 11:36:11 +00:00
|
|
|
$request = new WP_REST_Request( 'DELETE', '/wc/v4/shipping/zones/' . $zone->get_id() . '/methods/' . $instance_id );
|
2019-06-07 15:32:42 +00:00
|
|
|
$request->set_param( 'force', true );
|
|
|
|
$response = $this->server->dispatch( $request );
|
|
|
|
$this->assertEquals( 200, $response->get_status() );
|
|
|
|
}
|
|
|
|
}
|