2019-12-10 13:54:18 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Controller Tests.
|
|
|
|
*
|
|
|
|
* @package WooCommerce\Blocks\Tests
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Automattic\WooCommerce\Blocks\Tests\RestApi\StoreApi\Controllers;
|
|
|
|
|
|
|
|
use \WP_REST_Request;
|
|
|
|
use \WC_REST_Unit_Test_Case as TestCase;
|
|
|
|
use \WC_Helper_Product as ProductHelper;
|
2020-01-17 11:34:15 +00:00
|
|
|
use \WC_Helper_Shipping;
|
|
|
|
use Automattic\WooCommerce\Blocks\Tests\Helpers\ValidateSchema;
|
2019-12-10 13:54:18 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Cart Shipping Rates Controller Tests.
|
|
|
|
*/
|
|
|
|
class CartShippingRates extends TestCase {
|
|
|
|
/**
|
|
|
|
* Setup test products data. Called before every test.
|
|
|
|
*/
|
|
|
|
public function setUp() {
|
|
|
|
parent::setUp();
|
|
|
|
|
|
|
|
wp_set_current_user( 0 );
|
|
|
|
|
|
|
|
$this->products = [];
|
|
|
|
|
|
|
|
// Create some test products.
|
|
|
|
$this->products[0] = ProductHelper::create_simple_product( false );
|
|
|
|
$this->products[0]->save();
|
|
|
|
|
|
|
|
$this->products[1] = ProductHelper::create_simple_product( false );
|
|
|
|
$this->products[1]->save();
|
|
|
|
|
|
|
|
wc_empty_cart();
|
|
|
|
wc()->cart->add_to_cart( $this->products[0]->get_id(), 2 );
|
|
|
|
wc()->cart->add_to_cart( $this->products[1]->get_id(), 1 );
|
2020-01-17 11:34:15 +00:00
|
|
|
|
|
|
|
WC_Helper_Shipping::create_simple_flat_rate();
|
2019-12-10 13:54:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test route registration.
|
|
|
|
*/
|
|
|
|
public function test_register_routes() {
|
|
|
|
$routes = $this->server->get_routes();
|
|
|
|
$this->assertArrayHasKey( '/wc/store/cart/shipping-rates', $routes );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test getting shipping.
|
|
|
|
*/
|
|
|
|
public function test_get_items() {
|
|
|
|
$request = new WP_REST_Request( 'GET', '/wc/store/cart/shipping-rates' );
|
|
|
|
$request->set_param( 'country', 'US' );
|
|
|
|
$response = $this->server->dispatch( $request );
|
|
|
|
$data = $response->get_data();
|
|
|
|
|
|
|
|
$this->assertEquals( 200, $response->get_status() );
|
|
|
|
$this->assertEquals( 1, count( $data ) );
|
|
|
|
|
|
|
|
$this->assertArrayHasKey( 'destination', $data[0] );
|
|
|
|
$this->assertArrayHasKey( 'items', $data[0] );
|
2020-02-27 18:28:36 +00:00
|
|
|
$this->assertArrayHasKey( 'name', $data[0] );
|
2020-01-10 13:53:16 +00:00
|
|
|
$this->assertArrayHasKey( 'shipping_rates', $data[0] );
|
2019-12-10 13:54:18 +00:00
|
|
|
|
2020-01-17 11:34:15 +00:00
|
|
|
$this->assertEquals( null, $data[0]['destination']->address_1 );
|
|
|
|
$this->assertEquals( null, $data[0]['destination']->address_2 );
|
|
|
|
$this->assertEquals( null, $data[0]['destination']->city );
|
|
|
|
$this->assertEquals( null, $data[0]['destination']->state );
|
|
|
|
$this->assertEquals( null, $data[0]['destination']->postcode );
|
|
|
|
$this->assertEquals( 'US', $data[0]['destination']->country );
|
2019-12-10 13:54:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test getting shipping.
|
|
|
|
*/
|
|
|
|
public function test_get_items_address_validation() {
|
|
|
|
// US address.
|
|
|
|
$request = new WP_REST_Request( 'GET', '/wc/store/cart/shipping-rates' );
|
|
|
|
$request->set_param( 'address_1', 'Test address 1' );
|
|
|
|
$request->set_param( 'address_2', 'Test address 2' );
|
|
|
|
$request->set_param( 'city', 'Test City' );
|
|
|
|
$request->set_param( 'state', 'AL' );
|
|
|
|
$request->set_param( 'postcode', '90210' );
|
|
|
|
$request->set_param( 'country', 'US' );
|
|
|
|
$response = $this->server->dispatch( $request );
|
|
|
|
$data = $response->get_data();
|
|
|
|
|
|
|
|
$this->assertEquals( 200, $response->get_status() );
|
2020-01-17 11:34:15 +00:00
|
|
|
$this->assertEquals( 'Test address 1', $data[0]['destination']->address_1 );
|
|
|
|
$this->assertEquals( 'Test address 2', $data[0]['destination']->address_2 );
|
|
|
|
$this->assertEquals( 'Test City', $data[0]['destination']->city );
|
|
|
|
$this->assertEquals( 'AL', $data[0]['destination']->state );
|
|
|
|
$this->assertEquals( '90210', $data[0]['destination']->postcode );
|
|
|
|
$this->assertEquals( 'US', $data[0]['destination']->country );
|
2020-02-14 03:43:13 +00:00
|
|
|
|
|
|
|
// Address with empty country.
|
|
|
|
$request = new WP_REST_Request( 'GET', '/wc/store/cart/shipping-rates' );
|
|
|
|
$request->set_param( 'country', '' );
|
|
|
|
$response = $this->server->dispatch( $request );
|
|
|
|
$data = $response->get_data();
|
|
|
|
|
2020-03-05 19:54:05 +00:00
|
|
|
$this->assertEquals( 400, $response->get_status() );
|
2020-02-14 03:43:13 +00:00
|
|
|
|
|
|
|
// Address with invalid country.
|
|
|
|
$request = new WP_REST_Request( 'GET', '/wc/store/cart/shipping-rates' );
|
|
|
|
$request->set_param( 'country', 'ZZZZZZZZ' );
|
|
|
|
$response = $this->server->dispatch( $request );
|
|
|
|
$data = $response->get_data();
|
|
|
|
|
|
|
|
$this->assertEquals( 400, $response->get_status() );
|
|
|
|
|
|
|
|
// US address with named state.
|
|
|
|
$request = new WP_REST_Request( 'GET', '/wc/store/cart/shipping-rates' );
|
|
|
|
$request->set_param( 'state', 'Alabama' );
|
|
|
|
$request->set_param( 'country', 'US' );
|
|
|
|
$response = $this->server->dispatch( $request );
|
|
|
|
$data = $response->get_data();
|
|
|
|
|
|
|
|
$this->assertEquals( 200, $response->get_status() );
|
|
|
|
$this->assertEquals( 'AL', $data[0]['destination']->state );
|
|
|
|
$this->assertEquals( 'US', $data[0]['destination']->country );
|
2019-12-10 13:54:18 +00:00
|
|
|
|
|
|
|
// US address with invalid state.
|
|
|
|
$request = new WP_REST_Request( 'GET', '/wc/store/cart/shipping-rates' );
|
|
|
|
$request->set_param( 'state', 'ZZZZZZZZ' );
|
|
|
|
$request->set_param( 'country', 'US' );
|
|
|
|
$response = $this->server->dispatch( $request );
|
|
|
|
$data = $response->get_data();
|
|
|
|
|
|
|
|
$this->assertEquals( 400, $response->get_status() );
|
|
|
|
|
|
|
|
// US address with named state.
|
|
|
|
$request = new WP_REST_Request( 'GET', '/wc/store/cart/shipping-rates' );
|
|
|
|
$request->set_param( 'state', 'Alabama' );
|
|
|
|
$request->set_param( 'country', 'US' );
|
|
|
|
$response = $this->server->dispatch( $request );
|
|
|
|
$data = $response->get_data();
|
|
|
|
|
|
|
|
$this->assertEquals( 200, $response->get_status() );
|
2020-01-17 11:34:15 +00:00
|
|
|
$this->assertEquals( 'AL', $data[0]['destination']->state );
|
|
|
|
$this->assertEquals( 'US', $data[0]['destination']->country );
|
2019-12-10 13:54:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test schema retrieval.
|
|
|
|
*/
|
|
|
|
public function test_get_item_schema() {
|
|
|
|
$controller = new \Automattic\WooCommerce\Blocks\RestApi\StoreApi\Controllers\CartShippingRates();
|
|
|
|
$schema = $controller->get_item_schema();
|
|
|
|
|
|
|
|
$this->assertArrayHasKey( 'destination', $schema['properties'] );
|
|
|
|
$this->assertArrayHasKey( 'items', $schema['properties'] );
|
2020-02-27 18:28:36 +00:00
|
|
|
$this->assertArrayHasKey( 'name', $schema['properties'] );
|
2020-01-10 13:53:16 +00:00
|
|
|
$this->assertArrayHasKey( 'shipping_rates', $schema['properties'] );
|
|
|
|
$this->assertArrayHasKey( 'name', $schema['properties']['shipping_rates']['items']['properties'] );
|
|
|
|
$this->assertArrayHasKey( 'description', $schema['properties']['shipping_rates']['items']['properties'] );
|
|
|
|
$this->assertArrayHasKey( 'delivery_time', $schema['properties']['shipping_rates']['items']['properties'] );
|
|
|
|
$this->assertArrayHasKey( 'price', $schema['properties']['shipping_rates']['items']['properties'] );
|
|
|
|
$this->assertArrayHasKey( 'rate_id', $schema['properties']['shipping_rates']['items']['properties'] );
|
|
|
|
$this->assertArrayHasKey( 'instance_id', $schema['properties']['shipping_rates']['items']['properties'] );
|
|
|
|
$this->assertArrayHasKey( 'method_id', $schema['properties']['shipping_rates']['items']['properties'] );
|
|
|
|
$this->assertArrayHasKey( 'meta_data', $schema['properties']['shipping_rates']['items']['properties'] );
|
2019-12-10 13:54:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test conversion of cart item to rest response.
|
|
|
|
*/
|
|
|
|
public function test_prepare_item_for_response() {
|
2020-02-27 18:28:36 +00:00
|
|
|
$controller = new \Automattic\WooCommerce\Blocks\RestApi\StoreApi\Controllers\CartShippingRates();
|
2020-03-05 19:54:05 +00:00
|
|
|
$cart_controller = new \Automattic\WooCommerce\Blocks\RestApi\StoreApi\Utilities\CartController();
|
|
|
|
$packages = $cart_controller->get_shipping_packages();
|
2020-02-27 18:28:36 +00:00
|
|
|
$response = $controller->prepare_item_for_response( current( $packages ), [] );
|
|
|
|
$data = $response->get_data();
|
2019-12-10 13:54:18 +00:00
|
|
|
|
2020-01-17 11:34:15 +00:00
|
|
|
$this->assertArrayHasKey( 'destination', $data );
|
|
|
|
$this->assertArrayHasKey( 'items', $data );
|
2020-02-27 18:28:36 +00:00
|
|
|
$this->assertArrayHasKey( 'name', $data );
|
2020-01-17 11:34:15 +00:00
|
|
|
$this->assertArrayHasKey( 'shipping_rates', $data );
|
2019-12-10 13:54:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test collection params getter.
|
|
|
|
*/
|
|
|
|
public function test_get_collection_params() {
|
|
|
|
$controller = new \Automattic\WooCommerce\Blocks\RestApi\StoreApi\Controllers\CartShippingRates();
|
|
|
|
$params = $controller->get_collection_params();
|
|
|
|
|
|
|
|
$this->assertArrayHasKey( 'address_1', $params );
|
|
|
|
$this->assertArrayHasKey( 'address_2', $params );
|
|
|
|
$this->assertArrayHasKey( 'city', $params );
|
|
|
|
$this->assertArrayHasKey( 'state', $params );
|
|
|
|
$this->assertArrayHasKey( 'country', $params );
|
|
|
|
$this->assertArrayHasKey( 'postcode', $params );
|
|
|
|
}
|
2020-01-17 11:34:15 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Test schema matches responses.
|
|
|
|
*/
|
|
|
|
public function test_schema_matches_response() {
|
|
|
|
$controller = new \Automattic\WooCommerce\Blocks\RestApi\StoreApi\Controllers\CartShippingRates();
|
|
|
|
$request = new WP_REST_Request( 'GET', '/wc/store/cart/shipping-rates' );
|
|
|
|
$request->set_param( 'address_1', 'Test address 1' );
|
|
|
|
$request->set_param( 'address_2', 'Test address 2' );
|
|
|
|
$request->set_param( 'city', 'Test City' );
|
|
|
|
$request->set_param( 'state', 'AL' );
|
|
|
|
$request->set_param( 'postcode', '90210' );
|
|
|
|
$request->set_param( 'country', 'US' );
|
|
|
|
$response = $this->server->dispatch( $request );
|
|
|
|
$schema = $controller->get_item_schema();
|
|
|
|
$validate = new ValidateSchema( $schema );
|
|
|
|
|
|
|
|
$diff = $validate->get_diff_from_object( current( $response->get_data() ) );
|
|
|
|
$this->assertEmpty( $diff, print_r( $diff, true ) );
|
|
|
|
}
|
2019-12-10 13:54:18 +00:00
|
|
|
}
|