woocommerce/tests/unit-tests/api/shipping-zones.php

792 lines
22 KiB
PHP
Raw Normal View History

<?php
/**
* Shipping Zones API Tests
* @package WooCommerce\Tests\API
2017-03-15 16:36:53 +00:00
* @since 3.0.0
*/
class WC_Tests_API_Shipping_Zones extends WC_REST_Unit_Test_Case {
protected $server;
protected $endpoint;
protected $user;
protected $zones;
/**
* Setup our test server, endpoints, and user info.
*/
public function setUp() {
parent::setUp();
$this->endpoint = new WC_REST_Shipping_Zones_Controller();
2018-05-04 14:42:45 +00:00
$this->user = $this->factory->user->create(
array(
'role' => 'administrator',
)
);
$this->zones = array();
}
/**
* 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() ) {
$zone = new WC_Shipping_Zone( null );
$zone->set_zone_name( $name );
$zone->set_zone_order( $order );
$zone->set_locations( $locations );
$zone->save();
$this->zones[] = $zone;
return $zone;
}
/**
* Test route registration.
2017-03-15 16:36:53 +00:00
* @since 3.0.0
*/
public function test_register_routes() {
$routes = $this->server->get_routes();
2017-02-09 20:21:52 +00:00
$this->assertArrayHasKey( '/wc/v2/shipping/zones', $routes );
$this->assertArrayHasKey( '/wc/v2/shipping/zones/(?P<id>[\d-]+)', $routes );
2017-04-28 14:49:51 +00:00
$this->assertArrayHasKey( '/wc/v2/shipping/zones/(?P<id>[\d]+)/locations', $routes );
$this->assertArrayHasKey( '/wc/v2/shipping/zones/(?P<zone_id>[\d]+)/methods', $routes );
$this->assertArrayHasKey( '/wc/v2/shipping/zones/(?P<zone_id>[\d]+)/methods/(?P<instance_id>[\d]+)', $routes );
}
/**
* Test getting all Shipping Zones.
2017-03-15 16:36:53 +00:00
* @since 3.0.0
*/
public function test_get_zones() {
wp_set_current_user( $this->user );
// "Rest of the World" zone exists by default
2017-02-09 20:21:52 +00:00
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/shipping/zones' ) );
2018-05-04 14:42:45 +00:00
$data = $response->get_data();
$this->assertEquals( 200, $response->get_status() );
$this->assertEquals( count( $data ), 1 );
2018-05-04 14:42:45 +00:00
$this->assertContains(
array(
'id' => 0,
'name' => 'Locations not covered by your other zones',
'order' => 0,
'_links' => array(
'self' => array(
array(
'href' => rest_url( '/wc/v2/shipping/zones/0' ),
),
),
2018-05-04 14:42:45 +00:00
'collection' => array(
array(
'href' => rest_url( '/wc/v2/shipping/zones' ),
),
),
2018-05-04 14:42:45 +00:00
'describedby' => array(
array(
'href' => rest_url( '/wc/v2/shipping/zones/0/locations' ),
),
),
),
2018-05-04 14:42:45 +00:00
), $data
);
// Create a zone and make sure it's in the response
$this->create_shipping_zone( 'Zone 1' );
2017-02-09 20:21:52 +00:00
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/shipping/zones' ) );
2018-05-04 14:42:45 +00:00
$data = $response->get_data();
$this->assertEquals( 200, $response->get_status() );
$this->assertEquals( count( $data ), 2 );
2018-05-04 14:42:45 +00:00
$this->assertContains(
array(
'id' => 1,
'name' => 'Zone 1',
'order' => 0,
'_links' => array(
'self' => array(
array(
'href' => rest_url( '/wc/v2/shipping/zones/1' ),
),
),
2018-05-04 14:42:45 +00:00
'collection' => array(
array(
'href' => rest_url( '/wc/v2/shipping/zones' ),
),
),
2018-05-04 14:42:45 +00:00
'describedby' => array(
array(
'href' => rest_url( '/wc/v2/shipping/zones/1/locations' ),
),
),
),
2018-05-04 14:42:45 +00:00
), $data
);
}
/**
* Test /shipping/zones without valid permissions/creds.
2017-03-15 16:36:53 +00:00
* @since 3.0.0
*/
public function test_get_shipping_zones_without_permission() {
wp_set_current_user( 0 );
2017-02-09 20:21:52 +00:00
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/shipping/zones' ) );
$this->assertEquals( 401, $response->get_status() );
}
/**
* Test /shipping/zones while Shipping is disabled in WooCommerce.
2017-03-15 16:36:53 +00:00
* @since 3.0.0
*/
public function test_get_shipping_zones_disabled_shipping() {
wp_set_current_user( $this->user );
add_filter( 'wc_shipping_enabled', '__return_false' );
2017-02-09 20:21:52 +00:00
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/shipping/zones' ) );
$this->assertEquals( 404, $response->get_status() );
remove_filter( 'wc_shipping_enabled', '__return_false' );
}
/**
* Test Shipping Zone schema.
2017-03-15 16:36:53 +00:00
* @since 3.0.0
*/
public function test_get_shipping_zone_schema() {
2018-05-04 14:42:45 +00:00
$request = new WP_REST_Request( 'OPTIONS', '/wc/v2/shipping/zones' );
$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.
2017-03-15 16:36:53 +00:00
* @since 3.0.0
*/
public function test_create_shipping_zone() {
wp_set_current_user( $this->user );
2017-02-09 20:21:52 +00:00
$request = new WP_REST_Request( 'POST', '/wc/v2/shipping/zones' );
2018-05-04 14:42:45 +00:00
$request->set_body_params(
array(
'name' => 'Test Zone',
'order' => 1,
)
);
$response = $this->server->dispatch( $request );
2018-05-04 14:42:45 +00:00
$data = $response->get_data();
$this->assertEquals( 201, $response->get_status() );
2018-05-04 14:42:45 +00:00
$this->assertEquals(
array(
'id' => $data['id'],
'name' => 'Test Zone',
'order' => 1,
'_links' => array(
'self' => array(
array(
'href' => rest_url( '/wc/v2/shipping/zones/' . $data['id'] ),
),
),
2018-05-04 14:42:45 +00:00
'collection' => array(
array(
'href' => rest_url( '/wc/v2/shipping/zones' ),
),
),
2018-05-04 14:42:45 +00:00
'describedby' => array(
array(
'href' => rest_url( '/wc/v2/shipping/zones/' . $data['id'] . '/locations' ),
),
),
),
2018-05-04 14:42:45 +00:00
), $data
);
}
/**
* Test Shipping Zone create endpoint.
2017-03-15 16:36:53 +00:00
* @since 3.0.0
*/
public function test_create_shipping_zone_without_permission() {
wp_set_current_user( 0 );
2017-02-09 20:21:52 +00:00
$request = new WP_REST_Request( 'POST', '/wc/v2/shipping/zones' );
2018-05-04 14:42:45 +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.
2017-03-15 16:36:53 +00:00
* @since 3.0.0
*/
public function test_update_shipping_zone() {
wp_set_current_user( $this->user );
$zone = $this->create_shipping_zone( 'Test Zone' );
2017-02-09 20:21:52 +00:00
$request = new WP_REST_Request( 'PUT', '/wc/v2/shipping/zones/' . $zone->get_id() );
2018-05-04 14:42:45 +00:00
$request->set_body_params(
array(
'name' => 'Zone Test',
'order' => 2,
)
);
$response = $this->server->dispatch( $request );
2018-05-04 14:42:45 +00:00
$data = $response->get_data();
$this->assertEquals( 200, $response->get_status() );
2018-05-04 14:42:45 +00:00
$this->assertEquals(
array(
'id' => $zone->get_id(),
'name' => 'Zone Test',
'order' => 2,
'_links' => array(
'self' => array(
array(
'href' => rest_url( '/wc/v2/shipping/zones/' . $zone->get_id() ),
),
),
2018-05-04 14:42:45 +00:00
'collection' => array(
array(
'href' => rest_url( '/wc/v2/shipping/zones' ),
),
),
2018-05-04 14:42:45 +00:00
'describedby' => array(
array(
'href' => rest_url( '/wc/v2/shipping/zones/' . $zone->get_id() . '/locations' ),
),
),
),
2018-05-04 14:42:45 +00:00
), $data
);
}
/**
* Test Shipping Zone update endpoint with a bad zone ID.
2017-03-15 16:36:53 +00:00
* @since 3.0.0
*/
public function test_update_shipping_zone_invalid_id() {
wp_set_current_user( $this->user );
2017-02-09 20:21:52 +00:00
$request = new WP_REST_Request( 'PUT', '/wc/v2/shipping/zones/555555' );
2018-05-04 14:42:45 +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.
2017-03-15 16:36:53 +00:00
* @since 3.0.0
*/
public function test_delete_shipping_zone() {
wp_set_current_user( $this->user );
$zone = $this->create_shipping_zone( 'Zone 1' );
2018-05-04 14:42:45 +00:00
$request = new WP_REST_Request( 'DELETE', '/wc/v2/shipping/zones/' . $zone->get_id() );
$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.
2017-03-15 16:36:53 +00:00
* @since 3.0.0
*/
public function test_delete_shipping_zone_without_permission() {
wp_set_current_user( 0 );
$zone = $this->create_shipping_zone( 'Zone 1' );
2017-02-09 20:21:52 +00:00
$request = new WP_REST_Request( 'DELETE', '/wc/v2/shipping/zones/' . $zone->get_id() );
$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.
2017-03-15 16:36:53 +00:00
* @since 3.0.0
*/
public function test_delete_shipping_zone_invalid_id() {
wp_set_current_user( $this->user );
2017-02-09 20:21:52 +00:00
$request = new WP_REST_Request( 'DELETE', '/wc/v2/shipping/zones/555555' );
$response = $this->server->dispatch( $request );
$this->assertEquals( 404, $response->get_status() );
}
/**
* Test getting a single Shipping Zone.
2017-03-15 16:36:53 +00:00
* @since 3.0.0
*/
public function test_get_single_shipping_zone() {
wp_set_current_user( $this->user );
$zone = $this->create_shipping_zone( 'Test Zone' );
2017-02-09 20:21:52 +00:00
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/shipping/zones/' . $zone->get_id() ) );
$data = $response->get_data();
$this->assertEquals( 200, $response->get_status() );
2018-05-04 14:42:45 +00:00
$this->assertEquals(
array(
'id' => $zone->get_id(),
'name' => 'Test Zone',
'order' => 0,
'_links' => array(
'self' => array(
array(
'href' => rest_url( '/wc/v2/shipping/zones/' . $zone->get_id() ),
),
),
2018-05-04 14:42:45 +00:00
'collection' => array(
array(
'href' => rest_url( '/wc/v2/shipping/zones' ),
),
),
2018-05-04 14:42:45 +00:00
'describedby' => array(
array(
'href' => rest_url( '/wc/v2/shipping/zones/' . $zone->get_id() . '/locations' ),
),
),
),
2018-05-04 14:42:45 +00:00
), $data
);
}
/**
* Test getting a single Shipping Zone with a bad zone ID.
2017-03-15 16:36:53 +00:00
* @since 3.0.0
*/
public function test_get_single_shipping_zone_invalid_id() {
wp_set_current_user( $this->user );
2017-02-09 20:21:52 +00:00
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/shipping/zones/1' ) );
$this->assertEquals( 404, $response->get_status() );
}
/**
* Test getting Shipping Zone Locations.
2017-03-15 16:36:53 +00:00
* @since 3.0.0
*/
public function test_get_locations() {
wp_set_current_user( $this->user );
// Create a zone
2018-05-04 14:42:45 +00:00
$zone = $this->create_shipping_zone(
'Zone 1', 0, array(
array(
'code' => 'US',
'type' => 'country',
),
)
);
2017-02-09 20:21:52 +00:00
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/shipping/zones/' . $zone->get_id() . '/locations' ) );
2018-05-04 14:42:45 +00:00
$data = $response->get_data();
$this->assertEquals( 200, $response->get_status() );
$this->assertEquals( count( $data ), 1 );
2018-05-04 14:42:45 +00:00
$this->assertEquals(
array(
2018-05-04 14:42:45 +00:00
array(
'code' => 'US',
'type' => 'country',
'_links' => array(
'collection' => array(
array(
'href' => rest_url( '/wc/v2/shipping/zones/' . $zone->get_id() . '/locations' ),
),
),
2018-05-04 14:42:45 +00:00
'describes' => array(
array(
'href' => rest_url( '/wc/v2/shipping/zones/' . $zone->get_id() ),
),
),
),
),
2018-05-04 14:42:45 +00:00
), $data
);
}
/**
* Test getting Shipping Zone Locations with a bad zone ID.
2017-03-15 16:36:53 +00:00
* @since 3.0.0
*/
public function test_get_locations_invalid_id() {
wp_set_current_user( $this->user );
2017-02-09 20:21:52 +00:00
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/shipping/zones/1/locations' ) );
$this->assertEquals( 404, $response->get_status() );
}
/**
* Test Shipping Zone Locations update endpoint.
2017-03-15 16:36:53 +00:00
* @since 3.0.0
*/
public function test_update_locations() {
wp_set_current_user( $this->user );
$zone = $this->create_shipping_zone( 'Test Zone' );
2017-02-09 20:21:52 +00:00
$request = new WP_REST_Request( 'PUT', '/wc/v2/shipping/zones/' . $zone->get_id() . '/locations' );
$request->add_header( 'Content-Type', 'application/json' );
2018-05-04 14:42:45 +00:00
$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 );
2018-05-04 14:42:45 +00:00
$data = $response->get_data();
$this->assertEquals( 3, count( $data ) );
2018-05-04 14:42:45 +00:00
$this->assertEquals(
array(
2018-05-04 14:42:45 +00:00
array(
'code' => 'UK',
'type' => 'country',
'_links' => array(
'collection' => array(
array(
'href' => rest_url( '/wc/v2/shipping/zones/' . $zone->get_id() . '/locations' ),
),
),
2018-05-04 14:42:45 +00:00
'describes' => array(
array(
'href' => rest_url( '/wc/v2/shipping/zones/' . $zone->get_id() ),
),
),
),
2017-03-24 11:35:54 +00:00
),
2018-05-04 14:42:45 +00:00
array(
'code' => 'US',
'type' => 'country',
'_links' => array(
'collection' => array(
array(
'href' => rest_url( '/wc/v2/shipping/zones/' . $zone->get_id() . '/locations' ),
),
2017-03-24 11:35:54 +00:00
),
2018-05-04 14:42:45 +00:00
'describes' => array(
array(
'href' => rest_url( '/wc/v2/shipping/zones/' . $zone->get_id() ),
),
2017-03-24 11:35:54 +00:00
),
),
),
2018-05-04 14:42:45 +00:00
array(
'code' => 'SW1A0AA',
'type' => 'postcode',
'_links' => array(
'collection' => array(
array(
'href' => rest_url( '/wc/v2/shipping/zones/' . $zone->get_id() . '/locations' ),
),
),
2018-05-04 14:42:45 +00:00
'describes' => array(
array(
'href' => rest_url( '/wc/v2/shipping/zones/' . $zone->get_id() ),
),
),
),
),
2018-05-04 14:42:45 +00:00
), $data
);
}
/**
* Test updating Shipping Zone Locations with a bad zone ID.
2017-03-15 16:36:53 +00:00
* @since 3.0.0
*/
public function test_update_locations_invalid_id() {
wp_set_current_user( $this->user );
2017-02-09 20:21:52 +00:00
$response = $this->server->dispatch( new WP_REST_Request( 'PUT', '/wc/v2/shipping/zones/1/locations' ) );
$this->assertEquals( 404, $response->get_status() );
}
/**
* Test getting all Shipping Zone Methods and getting a single Shipping Zone Method.
2017-03-15 16:36:53 +00:00
* @since 3.0.0
*/
public function test_get_methods() {
wp_set_current_user( $this->user );
// 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;
}
2017-02-09 20:21:52 +00:00
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/shipping/zones/' . $zone->get_id() . '/methods' ) );
$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(
2017-02-09 20:21:52 +00:00
'href' => rest_url( '/wc/v2/shipping/zones/' . $zone->get_id() . '/methods/' . $instance_id ),
),
),
'collection' => array(
array(
2017-02-09 20:21:52 +00:00
'href' => rest_url( '/wc/v2/shipping/zones/' . $zone->get_id() . '/methods' ),
),
),
2018-05-04 14:42:45 +00:00
'describes' => array(
array(
2017-02-09 20:21:52 +00:00
'href' => rest_url( '/wc/v2/shipping/zones/' . $zone->get_id() ),
),
),
),
);
$this->assertEquals( 200, $response->get_status() );
$this->assertEquals( count( $data ), 1 );
$this->assertContains( $expected, $data );
2017-02-09 20:21:52 +00:00
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/shipping/zones/' . $zone->get_id() . '/methods/' . $instance_id ) );
$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.
2017-03-15 16:36:53 +00:00
* @since 3.0.0
*/
public function test_get_methods_invalid_zone_id() {
wp_set_current_user( $this->user );
2017-02-09 20:21:52 +00:00
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/shipping/zones/1/methods' ) );
$this->assertEquals( 404, $response->get_status() );
2017-02-09 20:21:52 +00:00
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/shipping/zones/1/methods/1' ) );
$this->assertEquals( 404, $response->get_status() );
}
/**
* Test getting a single Shipping Zone Method with a bad ID.
2017-03-15 16:36:53 +00:00
* @since 3.0.0
*/
public function test_get_methods_invalid_method_id() {
wp_set_current_user( $this->user );
$zone = $this->create_shipping_zone( 'Zone 1' );
2017-02-09 20:21:52 +00:00
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/shipping/zones/' . $zone->get_id() . '/methods/1' ) );
$this->assertEquals( 404, $response->get_status() );
}
/**
* Test updating a Shipping Zone Method.
2017-03-15 16:36:53 +00:00
* @since 3.0.0
*/
public function test_update_methods() {
wp_set_current_user( $this->user );
$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
2018-05-04 14:42:45 +00:00
$request = new WP_REST_Request( 'GET', '/wc/v2/shipping/zones/' . $zone->get_id() . '/methods/' . $instance_id );
$response = $this->server->dispatch( $request );
2018-05-04 14:42:45 +00:00
$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
2017-02-09 20:21:52 +00:00
$request = new WP_REST_Request( 'POST', '/wc/v2/shipping/zones/' . $zone->get_id() . '/methods/' . $instance_id );
2018-05-04 14:42:45 +00:00
$request->set_body_params(
array(
'settings' => array(
'cost' => 5,
),
)
);
$response = $this->server->dispatch( $request );
2018-05-04 14:42:45 +00:00
$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
2017-02-09 20:21:52 +00:00
$request = new WP_REST_Request( 'POST', '/wc/v2/shipping/zones/' . $zone->get_id() . '/methods/' . $instance_id );
2018-05-04 14:42:45 +00:00
$request->set_body_params(
array(
'settings' => array(
'cost' => 10,
'tax_status' => 'none',
),
)
);
$response = $this->server->dispatch( $request );
2018-05-04 14:42:45 +00:00
$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'] );
2016-09-08 22:14:40 +00:00
// Test bogus
2017-02-09 20:21:52 +00:00
$request = new WP_REST_Request( 'POST', '/wc/v2/shipping/zones/' . $zone->get_id() . '/methods/' . $instance_id );
2018-05-04 14:42:45 +00:00
$request->set_body_params(
array(
'settings' => array(
'cost' => 10,
'tax_status' => 'this_is_not_a_valid_option',
),
)
);
2016-09-08 22:14:40 +00:00
$response = $this->server->dispatch( $request );
$this->assertEquals( 400, $response->get_status() );
// Test other parameters
$this->assertTrue( $data['enabled'] );
$this->assertEquals( 1, $data['order'] );
2017-02-09 20:21:52 +00:00
$request = new WP_REST_Request( 'POST', '/wc/v2/shipping/zones/' . $zone->get_id() . '/methods/' . $instance_id );
2018-05-04 14:42:45 +00:00
$request->set_body_params(
array(
'enabled' => false,
'order' => 2,
)
);
$response = $this->server->dispatch( $request );
2018-05-04 14:42:45 +00:00
$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.
2017-03-15 16:36:53 +00:00
* @since 3.0.0
*/
public function test_create_method() {
wp_set_current_user( $this->user );
$zone = $this->create_shipping_zone( 'Zone 1' );
2017-02-09 20:21:52 +00:00
$request = new WP_REST_Request( 'POST', '/wc/v2/shipping/zones/' . $zone->get_id() . '/methods' );
2018-05-04 14:42:45 +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.
2017-03-15 16:36:53 +00:00
* @since 3.0.0
*/
public function test_delete_method() {
wp_set_current_user( $this->user );
$zone = $this->create_shipping_zone( 'Zone 1' );
$instance_id = $zone->add_shipping_method( 'flat_rate' );
$methods = $zone->get_shipping_methods();
$method = $methods[ $instance_id ];
2017-02-09 20:21:52 +00:00
$request = new WP_REST_Request( 'DELETE', '/wc/v2/shipping/zones/' . $zone->get_id() . '/methods/' . $instance_id );
$request->set_param( 'force', true );
2018-05-04 14:42:45 +00:00
$response = $this->server->dispatch( $request );
$this->assertEquals( 200, $response->get_status() );
}
2016-07-26 18:42:57 +00:00
}