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\Helpers\FixtureData;
|
2020-01-17 11:34:15 +00:00
|
|
|
use Automattic\WooCommerce\Blocks\Tests\Helpers\ValidateSchema;
|
2022-10-03 10:49:32 +00:00
|
|
|
use Automattic\WooCommerce\StoreApi\SessionHandler;
|
|
|
|
use Automattic\WooCommerce\StoreApi\Utilities\JsonWebToken;
|
|
|
|
use Spy_REST_Server;
|
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
|
|
|
/**
|
2022-11-09 15:28:08 +00:00
|
|
|
* Setup test product data. Called before every test.
|
2019-10-21 11:11:52 +00:00
|
|
|
*/
|
2022-11-09 15:28:08 +00:00
|
|
|
protected function setUp(): void {
|
2019-10-21 11:11:52 +00:00
|
|
|
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,
|
|
|
|
)
|
|
|
|
),
|
2022-07-13 14:02:49 +00:00
|
|
|
$fixtures->get_simple_product(
|
|
|
|
array(
|
|
|
|
'name' => 'Test Product 3',
|
|
|
|
'stock_status' => 'instock',
|
|
|
|
'regular_price' => 10,
|
|
|
|
'weight' => 10,
|
|
|
|
)
|
|
|
|
),
|
2021-12-13 13:57:45 +00:00
|
|
|
);
|
2021-08-20 13:58:32 +00:00
|
|
|
|
2022-07-13 14:02:49 +00:00
|
|
|
// Add product #3 as a cross-sell for product #1.
|
|
|
|
$this->products[0]->set_cross_sell_ids( array( $this->products[2]->get_id() ) );
|
|
|
|
$this->products[0]->save();
|
|
|
|
|
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 );
|
2022-07-13 14:02:49 +00:00
|
|
|
$this->keys[] = wc()->cart->add_to_cart( $this->products[1]->get_id() );
|
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 getting cart.
|
|
|
|
*/
|
|
|
|
public function test_get_item() {
|
2022-02-23 12:00:45 +00:00
|
|
|
$this->assertAPIResponse(
|
|
|
|
'/wc/store/v1/cart',
|
|
|
|
200,
|
|
|
|
array(
|
|
|
|
'items_count' => 3,
|
|
|
|
'needs_payment' => true,
|
|
|
|
'needs_shipping' => true,
|
|
|
|
'items_weight' => '30',
|
2022-10-03 10:49:32 +00:00
|
|
|
'items' => function ( $value ) {
|
2022-02-23 12:00:45 +00:00
|
|
|
return count( $value ) === 2;
|
|
|
|
},
|
2022-07-13 14:02:49 +00:00
|
|
|
'cross_sells' => array(
|
|
|
|
array(
|
|
|
|
'id' => $this->products[2]->get_id(),
|
|
|
|
),
|
|
|
|
),
|
2022-02-23 12:00:45 +00:00
|
|
|
'totals' => array(
|
|
|
|
'currency_code' => 'USD',
|
|
|
|
'currency_minor_unit' => 2,
|
|
|
|
'currency_symbol' => '$',
|
|
|
|
'currency_decimal_separator' => '.',
|
|
|
|
'currency_thousand_separator' => ',',
|
|
|
|
'currency_prefix' => '$',
|
|
|
|
'currency_suffix' => '',
|
|
|
|
'total_items' => '3000',
|
|
|
|
'total_items_tax' => '0',
|
|
|
|
'total_fees' => '0',
|
|
|
|
'total_fees_tax' => '0',
|
|
|
|
'total_discount' => '100',
|
|
|
|
'total_discount_tax' => '0',
|
|
|
|
'total_shipping' => '0',
|
|
|
|
'total_shipping_tax' => '0',
|
|
|
|
'total_tax' => '0',
|
|
|
|
'total_price' => '2900',
|
|
|
|
'tax_lines' => array(),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
);
|
2019-10-21 11:11:52 +00:00
|
|
|
}
|
|
|
|
|
2020-03-05 19:11:39 +00:00
|
|
|
/**
|
2022-02-23 12:00:45 +00:00
|
|
|
* Test removing a nonexistent cart item. This should return 409 conflict with updated cart data.
|
2020-03-05 19:11:39 +00:00
|
|
|
*/
|
|
|
|
public function test_remove_bad_cart_item() {
|
2022-02-23 12:00:45 +00:00
|
|
|
$request = new \WP_REST_Request( 'POST', '/wc/store/v1/cart/remove-item' );
|
2022-03-11 12:07:08 +00:00
|
|
|
$request->set_header( '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',
|
|
|
|
)
|
|
|
|
);
|
2022-02-23 12:00:45 +00:00
|
|
|
$this->assertAPIResponse(
|
|
|
|
$request,
|
|
|
|
409,
|
|
|
|
array(
|
|
|
|
'code' => 'woocommerce_rest_cart_invalid_key',
|
|
|
|
)
|
|
|
|
);
|
2020-03-05 19:11:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test remove cart item.
|
|
|
|
*/
|
|
|
|
public function test_remove_cart_item() {
|
2022-02-23 12:00:45 +00:00
|
|
|
$request = new \WP_REST_Request( 'POST', '/wc/store/v1/cart/remove-item' );
|
2022-03-11 12:07:08 +00:00
|
|
|
$request->set_header( 'Nonce', wp_create_nonce( 'wc_store_api' ) );
|
2020-03-05 19:11:39 +00:00
|
|
|
$request->set_body_params(
|
|
|
|
array(
|
|
|
|
'key' => $this->keys[0],
|
|
|
|
)
|
|
|
|
);
|
2022-02-23 12:00:45 +00:00
|
|
|
$this->assertAPIResponse(
|
|
|
|
$request,
|
|
|
|
200,
|
|
|
|
array(
|
|
|
|
'items_count' => 1,
|
2022-10-03 10:49:32 +00:00
|
|
|
'items' => function ( $value ) {
|
2022-02-23 12:00:45 +00:00
|
|
|
return count( $value ) === 1;
|
|
|
|
},
|
|
|
|
'items_weight' => '10',
|
|
|
|
'totals' => array(
|
|
|
|
'total_items' => '1000',
|
|
|
|
),
|
|
|
|
)
|
|
|
|
);
|
2020-03-05 19:11:39 +00:00
|
|
|
|
|
|
|
// Test removing same item again - should return 404 (item is already removed).
|
2022-02-23 12:00:45 +00:00
|
|
|
$this->assertAPIResponse(
|
|
|
|
$request,
|
|
|
|
409,
|
|
|
|
array(
|
|
|
|
'code' => 'woocommerce_rest_cart_invalid_key',
|
|
|
|
)
|
|
|
|
);
|
2020-03-05 19:11:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test changing the quantity of a cart item.
|
|
|
|
*/
|
|
|
|
public function test_update_item() {
|
2022-02-23 12:00:45 +00:00
|
|
|
$request = new \WP_REST_Request( 'POST', '/wc/store/v1/cart/update-item' );
|
2022-03-11 12:07:08 +00:00
|
|
|
$request->set_header( '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,
|
|
|
|
)
|
|
|
|
);
|
2022-02-23 12:00:45 +00:00
|
|
|
$this->assertAPIResponse(
|
|
|
|
$request,
|
|
|
|
200,
|
|
|
|
array(
|
|
|
|
'items_count' => 11,
|
|
|
|
'totals' => array(
|
|
|
|
'total_items' => '11000',
|
|
|
|
),
|
|
|
|
'items' => array(
|
|
|
|
0 => array(
|
|
|
|
'quantity' => 10,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
);
|
2020-03-05 19:11:39 +00:00
|
|
|
}
|
|
|
|
|
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() {
|
2022-02-23 12:00:45 +00:00
|
|
|
$request = new \WP_REST_Request( 'POST', '/wc/store/v1/cart/update-customer' );
|
2022-03-11 12:07:08 +00:00
|
|
|
$request->set_header( '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();
|
|
|
|
|
2022-10-03 10:49:32 +00:00
|
|
|
add_action(
|
|
|
|
'woocommerce_store_api_cart_update_customer_from_request',
|
|
|
|
array(
|
|
|
|
$action_callback,
|
|
|
|
'do_customer_callback',
|
|
|
|
)
|
|
|
|
);
|
2021-12-13 13:57:45 +00:00
|
|
|
|
2022-02-23 12:00:45 +00:00
|
|
|
$this->assertAPIResponse(
|
|
|
|
$request,
|
|
|
|
200,
|
|
|
|
array(
|
|
|
|
'shipping_rates' => array(
|
|
|
|
0 => array(
|
|
|
|
'destination' => array(
|
|
|
|
'address_1' => '',
|
|
|
|
'address_2' => '',
|
|
|
|
'city' => '',
|
|
|
|
'state' => '',
|
|
|
|
'postcode' => '',
|
|
|
|
'country' => 'US',
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
);
|
2020-03-05 19:54:05 +00:00
|
|
|
|
2022-10-03 10:49:32 +00:00
|
|
|
remove_action(
|
|
|
|
'woocommerce_store_api_cart_update_customer_from_request',
|
|
|
|
array(
|
|
|
|
$action_callback,
|
|
|
|
'do_customer_callback',
|
|
|
|
)
|
|
|
|
);
|
2020-03-05 19:54:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test shipping address validation.
|
|
|
|
*/
|
2020-11-20 15:13:35 +00:00
|
|
|
public function test_update_customer_address() {
|
2022-02-23 12:00:45 +00:00
|
|
|
$request = new \WP_REST_Request( 'POST', '/wc/store/v1/cart/update-customer' );
|
2022-03-11 12:07:08 +00:00
|
|
|
$request->set_header( '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
|
|
|
)
|
|
|
|
);
|
2022-02-23 12:00:45 +00:00
|
|
|
$this->assertAPIResponse(
|
|
|
|
$request,
|
|
|
|
200,
|
|
|
|
array(
|
|
|
|
'shipping_rates' => array(
|
|
|
|
0 => array(
|
|
|
|
'destination' => array(
|
|
|
|
'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
|
|
|
|
|
|
|
// Address with invalid country.
|
2022-02-23 12:00:45 +00:00
|
|
|
$request = new \WP_REST_Request( 'POST', '/wc/store/v1/cart/update-customer' );
|
2022-03-11 12:07:08 +00:00
|
|
|
$request->set_header( '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
|
|
|
)
|
|
|
|
);
|
2022-02-23 12:00:45 +00:00
|
|
|
$this->assertAPIResponse(
|
|
|
|
$request,
|
|
|
|
400
|
|
|
|
);
|
2020-03-05 19:54:05 +00:00
|
|
|
|
|
|
|
// US address with named state.
|
2022-02-23 12:00:45 +00:00
|
|
|
$request = new \WP_REST_Request( 'POST', '/wc/store/v1/cart/update-customer' );
|
2022-03-11 12:07:08 +00:00
|
|
|
$request->set_header( '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
|
|
|
)
|
|
|
|
);
|
2022-02-23 12:00:45 +00:00
|
|
|
$this->assertAPIResponse(
|
|
|
|
$request,
|
|
|
|
200,
|
|
|
|
array(
|
|
|
|
'shipping_rates' => array(
|
|
|
|
0 => array(
|
|
|
|
'destination' => array(
|
|
|
|
'state' => 'AL',
|
|
|
|
'country' => 'US',
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
);
|
2020-03-05 19:54:05 +00:00
|
|
|
|
|
|
|
// US address with invalid state.
|
2022-02-23 12:00:45 +00:00
|
|
|
$request = new \WP_REST_Request( 'POST', '/wc/store/v1/cart/update-customer' );
|
2022-03-11 12:07:08 +00:00
|
|
|
$request->set_header( '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
|
|
|
)
|
|
|
|
);
|
2022-02-23 12:00:45 +00:00
|
|
|
$this->assertAPIResponse(
|
|
|
|
$request,
|
|
|
|
400
|
|
|
|
);
|
2020-12-17 14:52:44 +00:00
|
|
|
|
|
|
|
// US address with invalid postcode.
|
2022-02-23 12:00:45 +00:00
|
|
|
$request = new \WP_REST_Request( 'POST', '/wc/store/v1/cart/update-customer' );
|
2022-03-11 12:07:08 +00:00
|
|
|
$request->set_header( 'Nonce', wp_create_nonce( 'wc_store_api' ) );
|
2020-12-17 14:52:44 +00:00
|
|
|
$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
|
|
|
)
|
|
|
|
);
|
2022-02-23 12:00:45 +00:00
|
|
|
$this->assertAPIResponse(
|
|
|
|
$request,
|
|
|
|
400
|
|
|
|
);
|
2020-03-05 19:54:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
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() );
|
|
|
|
|
2022-02-23 12:00:45 +00:00
|
|
|
$request = new \WP_REST_Request( 'POST', '/wc/store/v1/cart/apply-coupon' );
|
2022-03-11 12:07:08 +00:00
|
|
|
$request->set_header( '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(),
|
|
|
|
)
|
|
|
|
);
|
2022-02-23 12:00:45 +00:00
|
|
|
$this->assertAPIResponse(
|
|
|
|
$request,
|
|
|
|
200,
|
|
|
|
array(
|
|
|
|
'totals' => array(
|
|
|
|
'total_discount' => '100',
|
|
|
|
),
|
|
|
|
)
|
|
|
|
);
|
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' ) );
|
2022-02-23 12:00:45 +00:00
|
|
|
$request = new \WP_REST_Request( 'POST', '/wc/store/v1/cart/apply-coupon' );
|
2022-03-11 12:07:08 +00:00
|
|
|
$request->set_header( 'Nonce', wp_create_nonce( 'wc_store_api' ) );
|
2020-02-26 11:46:58 +00:00
|
|
|
$request->set_body_params(
|
|
|
|
array(
|
|
|
|
'code' => 'testCoupon',
|
|
|
|
)
|
|
|
|
);
|
2022-02-23 12:00:45 +00:00
|
|
|
$this->assertAPIResponse(
|
|
|
|
$request,
|
|
|
|
200
|
|
|
|
);
|
2020-02-26 11:46:58 +00:00
|
|
|
|
|
|
|
// Test coupons with special chars in the code.
|
2021-12-13 13:57:45 +00:00
|
|
|
$newcoupon = $fixtures->get_coupon( array( 'code' => '$5 off' ) );
|
2022-02-23 12:00:45 +00:00
|
|
|
$request = new \WP_REST_Request( 'POST', '/wc/store/v1/cart/apply-coupon' );
|
2022-03-11 12:07:08 +00:00
|
|
|
$request->set_header( 'Nonce', wp_create_nonce( 'wc_store_api' ) );
|
2020-02-26 11:46:58 +00:00
|
|
|
$request->set_body_params(
|
|
|
|
array(
|
|
|
|
'code' => '$5 off',
|
|
|
|
)
|
|
|
|
);
|
2022-02-23 12:00:45 +00:00
|
|
|
$this->assertAPIResponse(
|
|
|
|
$request,
|
|
|
|
200
|
|
|
|
);
|
2020-02-25 11:36:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test removing coupon from cart.
|
|
|
|
*/
|
|
|
|
public function test_remove_coupon() {
|
|
|
|
// Invalid coupon.
|
2022-02-23 12:00:45 +00:00
|
|
|
$request = new \WP_REST_Request( 'POST', '/wc/store/v1/cart/remove-coupon' );
|
2022-03-11 12:07:08 +00:00
|
|
|
$request->set_header( 'Nonce', wp_create_nonce( 'wc_store_api' ) );
|
2020-02-26 11:46:58 +00:00
|
|
|
$request->set_body_params(
|
|
|
|
array(
|
|
|
|
'code' => 'doesnotexist',
|
|
|
|
)
|
|
|
|
);
|
2022-02-23 12:00:45 +00:00
|
|
|
$this->assertAPIResponse(
|
|
|
|
$request,
|
|
|
|
400
|
|
|
|
);
|
2020-02-25 11:36:53 +00:00
|
|
|
|
|
|
|
// Applied coupon.
|
2022-02-23 12:00:45 +00:00
|
|
|
$request = new \WP_REST_Request( 'POST', '/wc/store/v1/cart/remove-coupon' );
|
2022-03-11 12:07:08 +00:00
|
|
|
$request->set_header( '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(),
|
|
|
|
)
|
|
|
|
);
|
2022-02-23 12:00:45 +00:00
|
|
|
$this->assertAPIResponse(
|
|
|
|
$request,
|
|
|
|
200,
|
|
|
|
array(
|
|
|
|
'totals' => array(
|
|
|
|
'total_discount' => '0',
|
|
|
|
),
|
|
|
|
)
|
|
|
|
);
|
2020-02-25 11:36:53 +00:00
|
|
|
}
|
|
|
|
|
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() {
|
2022-03-04 13:28:37 +00:00
|
|
|
$routes = new \Automattic\WooCommerce\StoreApi\RoutesController( new \Automattic\WooCommerce\StoreApi\SchemaController( $this->mock_extend ) );
|
2022-02-23 12:00:45 +00:00
|
|
|
$controller = $routes->get( 'cart', 'v1' );
|
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 );
|
2022-07-13 14:02:49 +00:00
|
|
|
$this->assertArrayHasKey( 'cross_sells', $data );
|
2020-01-17 11:34:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test schema matches responses.
|
|
|
|
*/
|
2021-08-20 13:58:32 +00:00
|
|
|
public function test_get_item_schema() {
|
2022-03-04 13:28:37 +00:00
|
|
|
$routes = new \Automattic\WooCommerce\StoreApi\RoutesController( new \Automattic\WooCommerce\StoreApi\SchemaController( $this->mock_extend ) );
|
2022-02-23 12:00:45 +00:00
|
|
|
$controller = $routes->get( 'cart', 'v1' );
|
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() );
|
2022-02-23 12:00:45 +00:00
|
|
|
$this->assertEmpty( $diff );
|
2019-10-21 11:11:52 +00:00
|
|
|
}
|
2022-10-03 10:49:32 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Tests for Cart-Token header presence and validity.
|
|
|
|
*/
|
|
|
|
public function test_cart_token_header() {
|
|
|
|
|
|
|
|
/** @var Spy_REST_Server $server */
|
|
|
|
$server = rest_get_server();
|
|
|
|
|
|
|
|
$server->serve_request( '/wc/store/cart' );
|
|
|
|
|
|
|
|
$this->assertArrayHasKey( 'Cart-Token', $server->sent_headers );
|
|
|
|
|
|
|
|
$this->assertTrue(
|
|
|
|
JsonWebToken::validate(
|
|
|
|
$server->sent_headers['Cart-Token'],
|
|
|
|
'@' . wp_salt()
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
2023-01-25 13:03:44 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Test adding a variable product to cart returns proper variation data.
|
|
|
|
*/
|
|
|
|
public function test_add_variable_product_to_cart_returns_variation_data() {
|
|
|
|
wc_empty_cart();
|
|
|
|
|
|
|
|
$fixtures = new FixtureData();
|
|
|
|
|
|
|
|
$variable_product = $fixtures->get_variable_product(
|
|
|
|
array(
|
|
|
|
'name' => 'Test Variable Product 4',
|
|
|
|
'stock_status' => 'instock',
|
|
|
|
'regular_price' => 10,
|
|
|
|
'weight' => 10,
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
$fixtures->get_product_attribute( 'color', array( 'red', 'green', 'blue' ) ),
|
|
|
|
$fixtures->get_product_attribute( 'size', array( 'small', 'medium', 'large' ) ),
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
$variable_product->save();
|
|
|
|
|
|
|
|
$variation = $fixtures->get_variation_product(
|
|
|
|
$variable_product->get_id(),
|
|
|
|
array(
|
|
|
|
'pa_color' => 'red',
|
|
|
|
'pa_size' => 'small',
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
$request = new \WP_REST_Request( 'POST', '/wc/store/v1/cart/add-item' );
|
|
|
|
$request->set_header( 'Nonce', wp_create_nonce( 'wc_store_api' ) );
|
|
|
|
|
|
|
|
$request->set_body_params(
|
|
|
|
array(
|
|
|
|
'id' => $variation->get_id(),
|
|
|
|
'quantity' => 1,
|
|
|
|
'variation' => array( // intentionally alphabetically disordered.
|
|
|
|
array(
|
|
|
|
'attribute' => 'pa_color',
|
|
|
|
'value' => 'red',
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'attribute' => 'pa_size',
|
|
|
|
'value' => 'small',
|
|
|
|
),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertAPIResponse(
|
|
|
|
$request,
|
|
|
|
201,
|
|
|
|
array(
|
|
|
|
'items' => array(
|
|
|
|
array(
|
|
|
|
'variation' => array( // order matters, alphabetical attribute order.
|
|
|
|
array(
|
|
|
|
'attribute' => 'color',
|
|
|
|
'value' => 'red',
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'attribute' => 'size',
|
|
|
|
'value' => 'small',
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
2019-10-21 11:11:52 +00:00
|
|
|
}
|