2019-10-21 11:11:52 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Controller Tests.
|
|
|
|
*/
|
|
|
|
|
2021-08-20 13:58:32 +00:00
|
|
|
namespace Automattic\WooCommerce\Blocks\Tests\StoreApi\Routes;
|
2019-10-21 11:11:52 +00:00
|
|
|
|
2021-08-20 13:58:32 +00:00
|
|
|
use Automattic\WooCommerce\Blocks\Tests\StoreApi\Routes\ControllerTestCase;
|
|
|
|
use Automattic\WooCommerce\Blocks\Tests\Helpers\FixtureData;
|
2020-01-17 11:34:15 +00:00
|
|
|
use Automattic\WooCommerce\Blocks\Tests\Helpers\ValidateSchema;
|
2019-10-21 11:11:52 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Cart Controller Tests.
|
|
|
|
*/
|
2021-08-20 13:58:32 +00:00
|
|
|
class Cart extends ControllerTestCase {
|
2020-11-16 12:31:27 +00:00
|
|
|
|
2019-10-21 11:11:52 +00:00
|
|
|
/**
|
|
|
|
* Setup test products data. Called before every test.
|
|
|
|
*/
|
|
|
|
public function setUp() {
|
|
|
|
parent::setUp();
|
|
|
|
|
2021-08-20 13:58:32 +00:00
|
|
|
$fixtures = new FixtureData();
|
|
|
|
$fixtures->shipping_add_flat_rate();
|
|
|
|
|
2021-12-13 13:57:45 +00:00
|
|
|
$this->products = array(
|
|
|
|
$fixtures->get_simple_product(
|
|
|
|
array(
|
|
|
|
'name' => 'Test Product 1',
|
|
|
|
'stock_status' => 'instock',
|
|
|
|
'regular_price' => 10,
|
|
|
|
'weight' => 10,
|
|
|
|
)
|
|
|
|
),
|
|
|
|
$fixtures->get_simple_product(
|
|
|
|
array(
|
|
|
|
'name' => 'Test Product 2',
|
|
|
|
'stock_status' => 'instock',
|
|
|
|
'regular_price' => 10,
|
|
|
|
'weight' => 10,
|
|
|
|
)
|
|
|
|
),
|
|
|
|
);
|
2021-08-20 13:58:32 +00:00
|
|
|
|
|
|
|
$this->coupon = $fixtures->get_coupon(
|
2021-12-13 13:57:45 +00:00
|
|
|
array(
|
|
|
|
'code' => 'test_coupon',
|
2021-08-20 13:58:32 +00:00
|
|
|
'discount_type' => 'fixed_cart',
|
2021-12-13 13:57:45 +00:00
|
|
|
'amount' => 1,
|
|
|
|
)
|
2021-08-20 13:58:32 +00:00
|
|
|
);
|
2020-01-17 11:34:15 +00:00
|
|
|
|
2019-10-21 11:11:52 +00:00
|
|
|
wc_empty_cart();
|
2021-12-13 13:57:45 +00:00
|
|
|
$this->keys = array();
|
2019-10-21 11:11:52 +00:00
|
|
|
$this->keys[] = wc()->cart->add_to_cart( $this->products[0]->get_id(), 2 );
|
|
|
|
$this->keys[] = wc()->cart->add_to_cart( $this->products[1]->get_id(), 1 );
|
2020-01-17 11:34:15 +00:00
|
|
|
wc()->cart->apply_coupon( $this->coupon->get_code() );
|
2021-12-13 13:57:45 +00:00
|
|
|
|
|
|
|
// Draft order.
|
|
|
|
$order = new \WC_Order();
|
|
|
|
$order->set_status( 'checkout-draft' );
|
|
|
|
$order->save();
|
|
|
|
wc()->session->set( 'store_api_draft_order', $order->get_id() );
|
2019-10-21 11:11:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test route registration.
|
|
|
|
*/
|
|
|
|
public function test_register_routes() {
|
2021-08-20 13:58:32 +00:00
|
|
|
$routes = rest_get_server()->get_routes();
|
2019-10-21 11:11:52 +00:00
|
|
|
$this->assertArrayHasKey( '/wc/store/cart', $routes );
|
2020-03-05 19:54:05 +00:00
|
|
|
$this->assertArrayHasKey( '/wc/store/cart/apply-coupon', $routes );
|
|
|
|
$this->assertArrayHasKey( '/wc/store/cart/remove-coupon', $routes );
|
2020-11-20 15:13:35 +00:00
|
|
|
$this->assertArrayHasKey( '/wc/store/cart/update-customer', $routes );
|
2021-01-28 14:24:01 +00:00
|
|
|
$this->assertArrayHasKey( '/wc/store/cart/select-shipping-rate', $routes );
|
2019-10-21 11:11:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test getting cart.
|
|
|
|
*/
|
|
|
|
public function test_get_item() {
|
2021-08-20 13:58:32 +00:00
|
|
|
$response = rest_get_server()->dispatch( new \WP_REST_Request( 'GET', '/wc/store/cart' ) );
|
2019-10-21 11:11:52 +00:00
|
|
|
$data = $response->get_data();
|
|
|
|
|
|
|
|
$this->assertEquals( 200, $response->get_status() );
|
2019-12-12 10:31:25 +00:00
|
|
|
$this->assertEquals( 3, $data['items_count'] );
|
2019-10-21 11:11:52 +00:00
|
|
|
$this->assertEquals( 2, count( $data['items'] ) );
|
2020-04-09 14:01:11 +00:00
|
|
|
$this->assertEquals( true, $data['needs_payment'] );
|
2020-03-05 19:54:05 +00:00
|
|
|
$this->assertEquals( true, $data['needs_shipping'] );
|
2019-12-12 10:31:25 +00:00
|
|
|
$this->assertEquals( '30', $data['items_weight'] );
|
|
|
|
|
2021-05-13 10:14:15 +00:00
|
|
|
$this->assertEquals( 'USD', $data['totals']->currency_code );
|
2020-01-17 11:34:15 +00:00
|
|
|
$this->assertEquals( 2, $data['totals']->currency_minor_unit );
|
|
|
|
$this->assertEquals( '3000', $data['totals']->total_items );
|
|
|
|
$this->assertEquals( '0', $data['totals']->total_items_tax );
|
|
|
|
$this->assertEquals( '0', $data['totals']->total_fees );
|
|
|
|
$this->assertEquals( '0', $data['totals']->total_fees_tax );
|
|
|
|
$this->assertEquals( '100', $data['totals']->total_discount );
|
|
|
|
$this->assertEquals( '0', $data['totals']->total_discount_tax );
|
|
|
|
$this->assertEquals( '0', $data['totals']->total_shipping );
|
|
|
|
$this->assertEquals( '0', $data['totals']->total_shipping_tax );
|
|
|
|
$this->assertEquals( '0', $data['totals']->total_tax );
|
|
|
|
$this->assertEquals( '2900', $data['totals']->total_price );
|
2019-10-21 11:11:52 +00:00
|
|
|
}
|
|
|
|
|
2020-03-05 19:11:39 +00:00
|
|
|
/**
|
|
|
|
* Test removing a nonexistent cart item.
|
|
|
|
*/
|
|
|
|
public function test_remove_bad_cart_item() {
|
|
|
|
// Test removing a bad cart item - should return 404.
|
2021-12-13 13:57:45 +00:00
|
|
|
$request = new \WP_REST_Request( 'POST', '/wc/store/cart/remove-item' );
|
2020-03-19 11:50:51 +00:00
|
|
|
$request->set_header( 'X-WC-Store-API-Nonce', wp_create_nonce( 'wc_store_api' ) );
|
2020-03-05 19:11:39 +00:00
|
|
|
$request->set_body_params(
|
|
|
|
array(
|
|
|
|
'key' => 'bad_item_key_123',
|
|
|
|
)
|
|
|
|
);
|
2021-08-20 13:58:32 +00:00
|
|
|
$response = rest_get_server()->dispatch( $request );
|
2020-03-05 19:11:39 +00:00
|
|
|
$data = $response->get_data();
|
|
|
|
|
2020-04-09 12:52:31 +00:00
|
|
|
$this->assertEquals( 409, $response->get_status() );
|
2020-03-05 19:11:39 +00:00
|
|
|
$this->assertEquals( 'woocommerce_rest_cart_invalid_key', $data['code'] );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test remove cart item.
|
|
|
|
*/
|
|
|
|
public function test_remove_cart_item() {
|
|
|
|
// Test removing a valid cart item - should return updated cart.
|
2021-12-13 13:57:45 +00:00
|
|
|
$request = new \WP_REST_Request( 'POST', '/wc/store/cart/remove-item' );
|
2020-03-19 11:50:51 +00:00
|
|
|
$request->set_header( 'X-WC-Store-API-Nonce', wp_create_nonce( 'wc_store_api' ) );
|
2020-03-05 19:11:39 +00:00
|
|
|
$request->set_body_params(
|
|
|
|
array(
|
|
|
|
'key' => $this->keys[0],
|
|
|
|
)
|
|
|
|
);
|
2021-08-20 13:58:32 +00:00
|
|
|
$response = rest_get_server()->dispatch( $request );
|
2020-03-05 19:11:39 +00:00
|
|
|
$data = $response->get_data();
|
|
|
|
|
|
|
|
$this->assertEquals( 200, $response->get_status() );
|
|
|
|
$this->assertEquals( 1, $data['items_count'] );
|
|
|
|
$this->assertEquals( 1, count( $data['items'] ) );
|
|
|
|
$this->assertEquals( '10', $data['items_weight'] );
|
|
|
|
$this->assertEquals( '1000', $data['totals']->total_items );
|
|
|
|
|
|
|
|
// Test removing same item again - should return 404 (item is already removed).
|
2021-08-20 13:58:32 +00:00
|
|
|
$response = rest_get_server()->dispatch( $request );
|
2020-03-05 19:11:39 +00:00
|
|
|
$data = $response->get_data();
|
|
|
|
|
2020-04-09 12:52:31 +00:00
|
|
|
$this->assertEquals( 409, $response->get_status() );
|
2020-03-05 19:11:39 +00:00
|
|
|
$this->assertEquals( 'woocommerce_rest_cart_invalid_key', $data['code'] );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test changing the quantity of a cart item.
|
|
|
|
*/
|
|
|
|
public function test_update_item() {
|
2021-08-20 13:58:32 +00:00
|
|
|
$request = new \WP_REST_Request( 'POST', '/wc/store/cart/update-item' );
|
2020-03-19 11:50:51 +00:00
|
|
|
$request->set_header( 'X-WC-Store-API-Nonce', wp_create_nonce( 'wc_store_api' ) );
|
2020-03-05 19:11:39 +00:00
|
|
|
$request->set_body_params(
|
|
|
|
array(
|
|
|
|
'key' => $this->keys[0],
|
|
|
|
'quantity' => 10,
|
|
|
|
)
|
|
|
|
);
|
2021-08-20 13:58:32 +00:00
|
|
|
$response = rest_get_server()->dispatch( $request );
|
2020-03-05 19:11:39 +00:00
|
|
|
$data = $response->get_data();
|
|
|
|
|
|
|
|
$this->assertEquals( 200, $response->get_status() );
|
|
|
|
$this->assertEquals( 10, $data['items'][0]['quantity'] );
|
|
|
|
$this->assertEquals( 11, $data['items_count'] );
|
|
|
|
$this->assertEquals( '11000', $data['totals']->total_items );
|
|
|
|
}
|
|
|
|
|
2020-12-17 14:52:44 +00:00
|
|
|
/**
|
2020-03-05 19:54:05 +00:00
|
|
|
* Test getting updated shipping.
|
|
|
|
*/
|
2020-11-20 15:13:35 +00:00
|
|
|
public function test_update_customer() {
|
2021-08-20 13:58:32 +00:00
|
|
|
$request = new \WP_REST_Request( 'POST', '/wc/store/cart/update-customer' );
|
2020-03-19 11:50:51 +00:00
|
|
|
$request->set_header( 'X-WC-Store-API-Nonce', wp_create_nonce( 'wc_store_api' ) );
|
2020-03-05 19:54:05 +00:00
|
|
|
$request->set_body_params(
|
|
|
|
array(
|
2020-11-20 15:13:35 +00:00
|
|
|
'shipping_address' => (object) array(
|
|
|
|
'country' => 'US',
|
2021-12-13 13:57:45 +00:00
|
|
|
),
|
2020-03-05 19:54:05 +00:00
|
|
|
)
|
|
|
|
);
|
2021-12-13 13:57:45 +00:00
|
|
|
|
|
|
|
$action_callback = \Mockery::mock( 'ActionCallback' );
|
|
|
|
$action_callback->shouldReceive( 'do_customer_callback' )->once();
|
|
|
|
|
|
|
|
add_action( 'woocommerce_blocks_cart_update_customer_from_request', array( $action_callback, 'do_customer_callback' ) );
|
|
|
|
|
2021-08-20 13:58:32 +00:00
|
|
|
$response = rest_get_server()->dispatch( $request );
|
2020-03-05 19:54:05 +00:00
|
|
|
$data = $response->get_data();
|
|
|
|
|
2021-12-13 13:57:45 +00:00
|
|
|
remove_action( 'woocommerce_blocks_cart_update_customer_from_request', array( $action_callback, 'do_customer_callback' ) );
|
|
|
|
|
2020-11-20 15:13:35 +00:00
|
|
|
$this->assertEquals( 200, $response->get_status(), print_r( $response, true ) );
|
2020-03-05 19:54:05 +00:00
|
|
|
$this->assertArrayHasKey( 'shipping_rates', $data );
|
|
|
|
|
|
|
|
$this->assertEquals( null, $data['shipping_rates'][0]['destination']->address_1 );
|
|
|
|
$this->assertEquals( null, $data['shipping_rates'][0]['destination']->address_2 );
|
|
|
|
$this->assertEquals( null, $data['shipping_rates'][0]['destination']->city );
|
|
|
|
$this->assertEquals( null, $data['shipping_rates'][0]['destination']->state );
|
|
|
|
$this->assertEquals( null, $data['shipping_rates'][0]['destination']->postcode );
|
|
|
|
$this->assertEquals( 'US', $data['shipping_rates'][0]['destination']->country );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test shipping address validation.
|
|
|
|
*/
|
2020-11-20 15:13:35 +00:00
|
|
|
public function test_update_customer_address() {
|
2020-03-05 19:54:05 +00:00
|
|
|
// US address.
|
2021-08-20 13:58:32 +00:00
|
|
|
$request = new \WP_REST_Request( 'POST', '/wc/store/cart/update-customer' );
|
2020-03-19 11:50:51 +00:00
|
|
|
$request->set_header( 'X-WC-Store-API-Nonce', wp_create_nonce( 'wc_store_api' ) );
|
2020-03-05 19:54:05 +00:00
|
|
|
$request->set_body_params(
|
|
|
|
array(
|
2020-11-20 15:13:35 +00:00
|
|
|
'shipping_address' => (object) array(
|
2020-12-17 14:52:44 +00:00
|
|
|
'first_name' => 'Han',
|
2021-12-13 13:57:45 +00:00
|
|
|
'last_name' => 'Solo',
|
|
|
|
'address_1' => 'Test address 1',
|
|
|
|
'address_2' => 'Test address 2',
|
|
|
|
'city' => 'Test City',
|
|
|
|
'state' => 'AL',
|
|
|
|
'postcode' => '90210',
|
|
|
|
'country' => 'US',
|
|
|
|
),
|
2020-03-05 19:54:05 +00:00
|
|
|
)
|
|
|
|
);
|
2021-08-20 13:58:32 +00:00
|
|
|
$response = rest_get_server()->dispatch( $request );
|
2020-03-05 19:54:05 +00:00
|
|
|
$data = $response->get_data();
|
|
|
|
|
|
|
|
$this->assertEquals( 200, $response->get_status() );
|
|
|
|
$this->assertEquals( 'Test address 1', $data['shipping_rates'][0]['destination']->address_1 );
|
|
|
|
$this->assertEquals( 'Test address 2', $data['shipping_rates'][0]['destination']->address_2 );
|
|
|
|
$this->assertEquals( 'Test City', $data['shipping_rates'][0]['destination']->city );
|
|
|
|
$this->assertEquals( 'AL', $data['shipping_rates'][0]['destination']->state );
|
|
|
|
$this->assertEquals( '90210', $data['shipping_rates'][0]['destination']->postcode );
|
|
|
|
$this->assertEquals( 'US', $data['shipping_rates'][0]['destination']->country );
|
|
|
|
|
|
|
|
// Address with invalid country.
|
2021-08-20 13:58:32 +00:00
|
|
|
$request = new \WP_REST_Request( 'POST', '/wc/store/cart/update-customer' );
|
2020-03-19 11:50:51 +00:00
|
|
|
$request->set_header( 'X-WC-Store-API-Nonce', wp_create_nonce( 'wc_store_api' ) );
|
2020-03-05 19:54:05 +00:00
|
|
|
$request->set_body_params(
|
|
|
|
array(
|
2020-11-20 15:13:35 +00:00
|
|
|
'shipping_address' => (object) array(
|
2020-12-17 14:52:44 +00:00
|
|
|
'first_name' => 'Han',
|
2021-12-13 13:57:45 +00:00
|
|
|
'last_name' => 'Solo',
|
|
|
|
'address_1' => 'Test address 1',
|
|
|
|
'address_2' => 'Test address 2',
|
|
|
|
'city' => 'Test City',
|
|
|
|
'state' => 'AL',
|
|
|
|
'postcode' => '90210',
|
|
|
|
'country' => 'ZZZZZZZZ',
|
|
|
|
),
|
2020-03-05 19:54:05 +00:00
|
|
|
)
|
|
|
|
);
|
2021-08-20 13:58:32 +00:00
|
|
|
$response = rest_get_server()->dispatch( $request );
|
2020-03-05 19:54:05 +00:00
|
|
|
$data = $response->get_data();
|
|
|
|
|
|
|
|
$this->assertEquals( 400, $response->get_status() );
|
|
|
|
|
|
|
|
// US address with named state.
|
2021-08-20 13:58:32 +00:00
|
|
|
$request = new \WP_REST_Request( 'POST', '/wc/store/cart/update-customer' );
|
2020-03-19 11:50:51 +00:00
|
|
|
$request->set_header( 'X-WC-Store-API-Nonce', wp_create_nonce( 'wc_store_api' ) );
|
2020-03-05 19:54:05 +00:00
|
|
|
$request->set_body_params(
|
|
|
|
array(
|
2020-11-20 15:13:35 +00:00
|
|
|
'shipping_address' => (object) array(
|
2020-12-17 14:52:44 +00:00
|
|
|
'first_name' => 'Han',
|
2021-12-13 13:57:45 +00:00
|
|
|
'last_name' => 'Solo',
|
|
|
|
'address_1' => 'Test address 1',
|
|
|
|
'address_2' => 'Test address 2',
|
|
|
|
'city' => 'Test City',
|
|
|
|
'state' => 'Alabama',
|
|
|
|
'postcode' => '90210',
|
|
|
|
'country' => 'US',
|
|
|
|
),
|
2020-03-05 19:54:05 +00:00
|
|
|
)
|
|
|
|
);
|
2021-08-20 13:58:32 +00:00
|
|
|
$response = rest_get_server()->dispatch( $request );
|
2020-03-05 19:54:05 +00:00
|
|
|
$data = $response->get_data();
|
|
|
|
|
|
|
|
$this->assertEquals( 200, $response->get_status() );
|
|
|
|
$this->assertEquals( 'AL', $data['shipping_rates'][0]['destination']->state );
|
|
|
|
$this->assertEquals( 'US', $data['shipping_rates'][0]['destination']->country );
|
|
|
|
|
|
|
|
// US address with invalid state.
|
2021-08-20 13:58:32 +00:00
|
|
|
$request = new \WP_REST_Request( 'POST', '/wc/store/cart/update-customer' );
|
2020-03-19 11:50:51 +00:00
|
|
|
$request->set_header( 'X-WC-Store-API-Nonce', wp_create_nonce( 'wc_store_api' ) );
|
2020-03-05 19:54:05 +00:00
|
|
|
$request->set_body_params(
|
|
|
|
array(
|
2020-11-20 15:13:35 +00:00
|
|
|
'shipping_address' => (object) array(
|
2020-12-17 14:52:44 +00:00
|
|
|
'first_name' => 'Han',
|
2021-12-13 13:57:45 +00:00
|
|
|
'last_name' => 'Solo',
|
|
|
|
'address_1' => 'Test address 1',
|
|
|
|
'address_2' => 'Test address 2',
|
|
|
|
'city' => 'Test City',
|
|
|
|
'state' => 'ZZZZZZZZ',
|
|
|
|
'postcode' => '90210',
|
|
|
|
'country' => 'US',
|
|
|
|
),
|
2020-12-17 14:52:44 +00:00
|
|
|
)
|
|
|
|
);
|
2021-08-20 13:58:32 +00:00
|
|
|
$response = rest_get_server()->dispatch( $request );
|
2020-12-17 14:52:44 +00:00
|
|
|
$data = $response->get_data();
|
|
|
|
|
|
|
|
$this->assertEquals( 400, $response->get_status() );
|
|
|
|
|
|
|
|
// US address with invalid postcode.
|
2021-08-20 13:58:32 +00:00
|
|
|
$request = new \WP_REST_Request( 'POST', '/wc/store/cart/update-customer' );
|
2020-12-17 14:52:44 +00:00
|
|
|
$request->set_header( 'X-WC-Store-API-Nonce', wp_create_nonce( 'wc_store_api' ) );
|
|
|
|
$request->set_body_params(
|
|
|
|
array(
|
|
|
|
'shipping_address' => (object) array(
|
|
|
|
'first_name' => 'Han',
|
2021-12-13 13:57:45 +00:00
|
|
|
'last_name' => 'Solo',
|
|
|
|
'address_1' => 'Test address 1',
|
|
|
|
'address_2' => 'Test address 2',
|
|
|
|
'city' => 'Test City',
|
|
|
|
'state' => 'AL',
|
|
|
|
'postcode' => 'ABCDE',
|
|
|
|
'country' => 'US',
|
|
|
|
),
|
2020-03-05 19:54:05 +00:00
|
|
|
)
|
|
|
|
);
|
2021-08-20 13:58:32 +00:00
|
|
|
$response = rest_get_server()->dispatch( $request );
|
2020-03-05 19:54:05 +00:00
|
|
|
$data = $response->get_data();
|
|
|
|
|
|
|
|
$this->assertEquals( 400, $response->get_status() );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-02-25 11:36:53 +00:00
|
|
|
/**
|
|
|
|
* Test applying coupon to cart.
|
|
|
|
*/
|
|
|
|
public function test_apply_coupon() {
|
|
|
|
wc()->cart->remove_coupon( $this->coupon->get_code() );
|
|
|
|
|
2021-08-20 13:58:32 +00:00
|
|
|
$request = new \WP_REST_Request( 'POST', '/wc/store/cart/apply-coupon' );
|
2020-03-19 11:50:51 +00:00
|
|
|
$request->set_header( 'X-WC-Store-API-Nonce', wp_create_nonce( 'wc_store_api' ) );
|
2020-02-26 11:46:58 +00:00
|
|
|
$request->set_body_params(
|
|
|
|
array(
|
|
|
|
'code' => $this->coupon->get_code(),
|
|
|
|
)
|
|
|
|
);
|
2021-08-20 13:58:32 +00:00
|
|
|
$response = rest_get_server()->dispatch( $request );
|
2020-02-25 11:36:53 +00:00
|
|
|
$data = $response->get_data();
|
|
|
|
$this->assertEquals( 200, $response->get_status() );
|
|
|
|
$this->assertEquals( '100', $data['totals']->total_discount );
|
2020-02-26 11:46:58 +00:00
|
|
|
|
2021-08-20 13:58:32 +00:00
|
|
|
$fixtures = new FixtureData();
|
|
|
|
|
2020-02-26 11:46:58 +00:00
|
|
|
// Test coupons with different case.
|
2021-12-13 13:57:45 +00:00
|
|
|
$newcoupon = $fixtures->get_coupon( array( 'code' => 'testCoupon' ) );
|
|
|
|
$request = new \WP_REST_Request( 'POST', '/wc/store/cart/apply-coupon' );
|
2020-03-19 11:50:51 +00:00
|
|
|
$request->set_header( 'X-WC-Store-API-Nonce', wp_create_nonce( 'wc_store_api' ) );
|
2020-02-26 11:46:58 +00:00
|
|
|
$request->set_body_params(
|
|
|
|
array(
|
|
|
|
'code' => 'testCoupon',
|
|
|
|
)
|
|
|
|
);
|
2021-08-20 13:58:32 +00:00
|
|
|
$response = rest_get_server()->dispatch( $request );
|
2020-02-26 11:46:58 +00:00
|
|
|
$data = $response->get_data();
|
|
|
|
$this->assertEquals( 200, $response->get_status() );
|
|
|
|
|
|
|
|
// Test coupons with special chars in the code.
|
2021-12-13 13:57:45 +00:00
|
|
|
$newcoupon = $fixtures->get_coupon( array( 'code' => '$5 off' ) );
|
|
|
|
$request = new \WP_REST_Request( 'POST', '/wc/store/cart/apply-coupon' );
|
2020-03-19 11:50:51 +00:00
|
|
|
$request->set_header( 'X-WC-Store-API-Nonce', wp_create_nonce( 'wc_store_api' ) );
|
2020-02-26 11:46:58 +00:00
|
|
|
$request->set_body_params(
|
|
|
|
array(
|
|
|
|
'code' => '$5 off',
|
|
|
|
)
|
|
|
|
);
|
2021-08-20 13:58:32 +00:00
|
|
|
$response = rest_get_server()->dispatch( $request );
|
2020-02-26 11:46:58 +00:00
|
|
|
$data = $response->get_data();
|
|
|
|
$this->assertEquals( 200, $response->get_status() );
|
2020-02-25 11:36:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test removing coupon from cart.
|
|
|
|
*/
|
|
|
|
public function test_remove_coupon() {
|
|
|
|
// Invalid coupon.
|
2021-08-20 13:58:32 +00:00
|
|
|
$request = new \WP_REST_Request( 'POST', '/wc/store/cart/remove-coupon' );
|
2020-03-19 11:50:51 +00:00
|
|
|
$request->set_header( 'X-WC-Store-API-Nonce', wp_create_nonce( 'wc_store_api' ) );
|
2020-02-26 11:46:58 +00:00
|
|
|
$request->set_body_params(
|
|
|
|
array(
|
|
|
|
'code' => 'doesnotexist',
|
|
|
|
)
|
|
|
|
);
|
2021-08-20 13:58:32 +00:00
|
|
|
$response = rest_get_server()->dispatch( $request );
|
2020-02-25 11:36:53 +00:00
|
|
|
$data = $response->get_data();
|
2020-04-29 14:47:05 +00:00
|
|
|
$this->assertEquals( 400, $response->get_status() );
|
2020-02-25 11:36:53 +00:00
|
|
|
|
|
|
|
// Applied coupon.
|
2021-08-20 13:58:32 +00:00
|
|
|
$request = new \WP_REST_Request( 'POST', '/wc/store/cart/remove-coupon' );
|
2020-03-19 11:50:51 +00:00
|
|
|
$request->set_header( 'X-WC-Store-API-Nonce', wp_create_nonce( 'wc_store_api' ) );
|
2020-02-26 11:46:58 +00:00
|
|
|
$request->set_body_params(
|
|
|
|
array(
|
|
|
|
'code' => $this->coupon->get_code(),
|
|
|
|
)
|
|
|
|
);
|
2021-08-20 13:58:32 +00:00
|
|
|
$response = rest_get_server()->dispatch( $request );
|
2020-02-25 11:36:53 +00:00
|
|
|
$data = $response->get_data();
|
|
|
|
$this->assertEquals( 200, $response->get_status() );
|
|
|
|
$this->assertEquals( '0', $data['totals']->total_discount );
|
|
|
|
}
|
|
|
|
|
2019-10-21 11:11:52 +00:00
|
|
|
/**
|
|
|
|
* Test conversion of cart item to rest response.
|
|
|
|
*/
|
2021-08-20 13:58:32 +00:00
|
|
|
public function test_prepare_item() {
|
2020-11-16 12:31:27 +00:00
|
|
|
$routes = new \Automattic\WooCommerce\Blocks\StoreApi\RoutesController( new \Automattic\WooCommerce\Blocks\StoreApi\SchemaController( $this->mock_extend ) );
|
2020-04-06 10:36:28 +00:00
|
|
|
$controller = $routes->get( 'cart' );
|
2019-10-21 11:11:52 +00:00
|
|
|
$cart = wc()->cart;
|
2020-03-13 12:41:01 +00:00
|
|
|
$response = $controller->prepare_item_for_response( $cart, new \WP_REST_Request() );
|
2020-01-17 11:34:15 +00:00
|
|
|
$data = $response->get_data();
|
|
|
|
|
|
|
|
$this->assertArrayHasKey( 'items_count', $data );
|
|
|
|
$this->assertArrayHasKey( 'items', $data );
|
2020-03-05 19:54:05 +00:00
|
|
|
$this->assertArrayHasKey( 'shipping_rates', $data );
|
|
|
|
$this->assertArrayHasKey( 'coupons', $data );
|
2020-04-09 14:01:11 +00:00
|
|
|
$this->assertArrayHasKey( 'needs_payment', $data );
|
2020-01-17 11:34:15 +00:00
|
|
|
$this->assertArrayHasKey( 'needs_shipping', $data );
|
|
|
|
$this->assertArrayHasKey( 'items_weight', $data );
|
|
|
|
$this->assertArrayHasKey( 'totals', $data );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test schema matches responses.
|
|
|
|
*/
|
2021-08-20 13:58:32 +00:00
|
|
|
public function test_get_item_schema() {
|
2020-11-16 12:31:27 +00:00
|
|
|
$routes = new \Automattic\WooCommerce\Blocks\StoreApi\RoutesController( new \Automattic\WooCommerce\Blocks\StoreApi\SchemaController( $this->mock_extend ) );
|
2020-04-06 10:36:28 +00:00
|
|
|
$controller = $routes->get( 'cart' );
|
|
|
|
$schema = $controller->get_item_schema();
|
2020-01-17 11:34:15 +00:00
|
|
|
$cart = wc()->cart;
|
2020-03-13 12:41:01 +00:00
|
|
|
$response = $controller->prepare_item_for_response( $cart, new \WP_REST_Request() );
|
2020-01-17 11:34:15 +00:00
|
|
|
$schema = $controller->get_item_schema();
|
|
|
|
$validate = new ValidateSchema( $schema );
|
2019-10-21 11:11:52 +00:00
|
|
|
|
2020-01-17 11:34:15 +00:00
|
|
|
$diff = $validate->get_diff_from_object( $response->get_data() );
|
|
|
|
$this->assertEmpty( $diff, print_r( $diff, true ) );
|
2019-10-21 11:11:52 +00:00
|
|
|
}
|
|
|
|
}
|