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 CartItems 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();
|
|
|
|
|
|
|
|
$this->products = [
|
|
|
|
$fixtures->get_simple_product( [
|
|
|
|
'name' => 'Test Product 1',
|
|
|
|
'stock_status' => 'instock',
|
|
|
|
'regular_price' => 10,
|
|
|
|
'weight' => 10,
|
|
|
|
'image_id' => $fixtures->sideload_image(),
|
|
|
|
] ),
|
|
|
|
];
|
|
|
|
|
|
|
|
$variable_product = $fixtures->get_variable_product(
|
|
|
|
[
|
|
|
|
'name' => 'Test Product 2',
|
|
|
|
'stock_status' => 'instock',
|
|
|
|
'regular_price' => 10,
|
|
|
|
'weight' => 10,
|
|
|
|
'image_id' => $fixtures->sideload_image(),
|
|
|
|
],
|
|
|
|
[
|
|
|
|
$fixtures->get_product_attribute( 'color', [ 'red', 'green', 'blue' ] ),
|
|
|
|
$fixtures->get_product_attribute( 'size', [ 'small', 'medium', 'large' ] )
|
|
|
|
]
|
|
|
|
);
|
|
|
|
$variation = $fixtures->get_variation_product( $variable_product->get_id(), [ 'pa_color' => 'red', 'pa_size' => 'small' ] );
|
2020-01-17 11:34:15 +00:00
|
|
|
|
2021-08-20 13:58:32 +00:00
|
|
|
$this->products[] = $variable_product;
|
2019-10-21 11:11:52 +00:00
|
|
|
|
|
|
|
wc_empty_cart();
|
2020-01-17 11:34:15 +00:00
|
|
|
$this->keys = [];
|
2019-10-21 11:11:52 +00:00
|
|
|
$this->keys[] = wc()->cart->add_to_cart( $this->products[0]->get_id(), 2 );
|
2020-08-25 08:33:41 +00:00
|
|
|
$this->keys[] = wc()->cart->add_to_cart(
|
|
|
|
$this->products[1]->get_id(),
|
|
|
|
1,
|
2021-08-20 13:58:32 +00:00
|
|
|
$variation->get_id(),
|
2020-08-25 08:33:41 +00:00
|
|
|
array(
|
2021-08-20 13:58:32 +00:00
|
|
|
'attribute_pa_color' => 'red',
|
|
|
|
'attribute_pa_size' => 'small',
|
2020-08-25 08:33:41 +00:00
|
|
|
)
|
|
|
|
);
|
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/items', $routes );
|
|
|
|
$this->assertArrayHasKey( '/wc/store/cart/items/(?P<key>[\w-]{32})', $routes );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test getting cart.
|
|
|
|
*/
|
|
|
|
public function test_get_items() {
|
2021-08-20 13:58:32 +00:00
|
|
|
$response = rest_get_server()->dispatch( new \WP_REST_Request( 'GET', '/wc/store/cart/items' ) );
|
2019-10-21 11:11:52 +00:00
|
|
|
$data = $response->get_data();
|
|
|
|
|
|
|
|
$this->assertEquals( 200, $response->get_status() );
|
|
|
|
$this->assertEquals( 2, count( $data ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test getting cart item by key.
|
|
|
|
*/
|
|
|
|
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/items/' . $this->keys[0] ) );
|
2019-10-21 11:11:52 +00:00
|
|
|
$data = $response->get_data();
|
|
|
|
|
|
|
|
$this->assertEquals( 200, $response->get_status() );
|
|
|
|
$this->assertEquals( $this->keys[0], $data['key'] );
|
|
|
|
$this->assertEquals( $this->products[0]->get_id(), $data['id'] );
|
|
|
|
$this->assertEquals( $this->products[0]->get_name(), $data['name'] );
|
|
|
|
$this->assertEquals( $this->products[0]->get_sku(), $data['sku'] );
|
|
|
|
$this->assertEquals( $this->products[0]->get_permalink(), $data['permalink'] );
|
|
|
|
$this->assertEquals( 2, $data['quantity'] );
|
2020-01-17 11:34:15 +00:00
|
|
|
$this->assertEquals( '2000', $data['totals']->line_subtotal );
|
|
|
|
$this->assertEquals( '2000', $data['totals']->line_total );
|
2019-10-21 11:11:52 +00:00
|
|
|
|
2021-08-20 13:58:32 +00:00
|
|
|
$request = new \WP_REST_Request( 'DELETE', '/wc/store/cart/items/XXX815416f775098fe977004015c6193' );
|
2020-03-19 11:50:51 +00:00
|
|
|
$request->set_header( 'X-WC-Store-API-Nonce', wp_create_nonce( 'wc_store_api' ) );
|
2021-08-20 13:58:32 +00:00
|
|
|
$response = rest_get_server()->dispatch( $request );
|
2019-10-21 11:11:52 +00:00
|
|
|
$data = $response->get_data();
|
|
|
|
|
|
|
|
$this->assertEquals( 404, $response->get_status() );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test add to cart.
|
|
|
|
*/
|
|
|
|
public function test_create_item() {
|
|
|
|
wc_empty_cart();
|
|
|
|
|
2021-08-20 13:58:32 +00:00
|
|
|
$request = new \WP_REST_Request( 'POST', '/wc/store/cart/items' );
|
2020-03-19 11:50:51 +00:00
|
|
|
$request->set_header( 'X-WC-Store-API-Nonce', wp_create_nonce( 'wc_store_api' ) );
|
2019-10-21 11:11:52 +00:00
|
|
|
$request->set_body_params(
|
|
|
|
array(
|
|
|
|
'id' => $this->products[0]->get_id(),
|
|
|
|
'quantity' => '10',
|
|
|
|
)
|
|
|
|
);
|
2021-08-20 13:58:32 +00:00
|
|
|
$response = rest_get_server()->dispatch( $request );
|
2019-10-21 11:11:52 +00:00
|
|
|
$data = $response->get_data();
|
|
|
|
|
2019-10-25 09:43:52 +00:00
|
|
|
$this->assertEquals( 201, $response->get_status() );
|
2019-10-21 11:11:52 +00:00
|
|
|
$this->assertEquals( $this->products[0]->get_id(), $data['id'] );
|
|
|
|
$this->assertEquals( 10, $data['quantity'] );
|
|
|
|
|
2021-08-20 13:58:32 +00:00
|
|
|
$response = rest_get_server()->dispatch( $request );
|
2019-10-21 11:11:52 +00:00
|
|
|
$data = $response->get_data();
|
|
|
|
|
2019-10-25 09:43:52 +00:00
|
|
|
$this->assertEquals( 201, $response->get_status() );
|
2019-10-21 11:11:52 +00:00
|
|
|
$this->assertEquals( $this->products[0]->get_id(), $data['id'] );
|
|
|
|
$this->assertEquals( 20, $data['quantity'] );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test add to cart does not allow invalid items.
|
|
|
|
*/
|
|
|
|
public function test_invalid_create_item() {
|
|
|
|
wc_empty_cart();
|
|
|
|
|
2021-08-20 13:58:32 +00:00
|
|
|
$fixtures = new FixtureData();
|
|
|
|
$invalid_product = $fixtures->get_simple_product( [
|
|
|
|
'name' => 'Invalid Product',
|
|
|
|
'regular_price' => '',
|
|
|
|
] );
|
2019-10-21 11:11:52 +00:00
|
|
|
|
2021-08-20 13:58:32 +00:00
|
|
|
$request = new \WP_REST_Request( 'POST', '/wc/store/cart/items' );
|
2020-03-19 11:50:51 +00:00
|
|
|
$request->set_header( 'X-WC-Store-API-Nonce', wp_create_nonce( 'wc_store_api' ) );
|
2019-10-21 11:11:52 +00:00
|
|
|
$request->set_body_params(
|
|
|
|
array(
|
|
|
|
'id' => $invalid_product->get_id(),
|
|
|
|
'quantity' => '10',
|
|
|
|
)
|
|
|
|
);
|
2021-08-20 13:58:32 +00:00
|
|
|
$response = rest_get_server()->dispatch( $request );
|
2019-10-21 11:11:52 +00:00
|
|
|
|
2020-04-29 14:47:05 +00:00
|
|
|
$this->assertEquals( 400, $response->get_status() );
|
2019-10-21 11:11:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test updating an item.
|
|
|
|
*/
|
|
|
|
public function test_update_item() {
|
2021-08-20 13:58:32 +00:00
|
|
|
$request = new \WP_REST_Request( 'PUT', '/wc/store/cart/items/' . $this->keys[0] );
|
2020-03-19 11:50:51 +00:00
|
|
|
$request->set_header( 'X-WC-Store-API-Nonce', wp_create_nonce( 'wc_store_api' ) );
|
2019-10-21 11:11:52 +00:00
|
|
|
$request->set_body_params(
|
|
|
|
array(
|
|
|
|
'quantity' => '10',
|
|
|
|
)
|
|
|
|
);
|
2021-08-20 13:58:32 +00:00
|
|
|
$response = rest_get_server()->dispatch( $request );
|
2019-10-21 11:11:52 +00:00
|
|
|
$data = $response->get_data();
|
|
|
|
|
|
|
|
$this->assertEquals( 200, $response->get_status() );
|
|
|
|
$this->assertEquals( 10, $data['quantity'] );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test delete item.
|
|
|
|
*/
|
|
|
|
public function test_delete_item() {
|
2021-08-20 13:58:32 +00:00
|
|
|
$request = new \WP_REST_Request( 'DELETE', '/wc/store/cart/items/' . $this->keys[0] );
|
2020-03-19 11:50:51 +00:00
|
|
|
$request->set_header( 'X-WC-Store-API-Nonce', wp_create_nonce( 'wc_store_api' ) );
|
2021-08-20 13:58:32 +00:00
|
|
|
$response = rest_get_server()->dispatch( $request );
|
2019-10-21 11:11:52 +00:00
|
|
|
$data = $response->get_data();
|
|
|
|
|
|
|
|
$this->assertEquals( 204, $response->get_status() );
|
|
|
|
$this->assertEmpty( $data );
|
|
|
|
|
2021-08-20 13:58:32 +00:00
|
|
|
$request = new \WP_REST_Request( 'DELETE', '/wc/store/cart/items/' . $this->keys[0] );
|
2020-03-19 11:50:51 +00:00
|
|
|
$request->set_header( 'X-WC-Store-API-Nonce', wp_create_nonce( 'wc_store_api' ) );
|
2021-08-20 13:58:32 +00:00
|
|
|
$response = rest_get_server()->dispatch( $request );
|
2019-10-21 11:11:52 +00:00
|
|
|
$data = $response->get_data();
|
|
|
|
|
|
|
|
$this->assertEquals( 404, $response->get_status() );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test delete all items.
|
|
|
|
*/
|
|
|
|
public function test_delete_items() {
|
2021-08-20 13:58:32 +00:00
|
|
|
$request = new \WP_REST_Request( 'DELETE', '/wc/store/cart/items' );
|
2020-03-19 11:50:51 +00:00
|
|
|
$request->set_header( 'X-WC-Store-API-Nonce', wp_create_nonce( 'wc_store_api' ) );
|
2021-08-20 13:58:32 +00:00
|
|
|
$response = rest_get_server()->dispatch( $request );
|
2019-10-21 11:11:52 +00:00
|
|
|
$data = $response->get_data();
|
|
|
|
|
|
|
|
$this->assertEquals( 200, $response->get_status() );
|
|
|
|
$this->assertEquals( [], $data );
|
|
|
|
|
2021-08-20 13:58:32 +00:00
|
|
|
$response = rest_get_server()->dispatch( new \WP_REST_Request( 'GET', '/wc/store/cart/items' ) );
|
2019-10-21 11:11:52 +00:00
|
|
|
$data = $response->get_data();
|
|
|
|
|
|
|
|
$this->assertEquals( 200, $response->get_status() );
|
|
|
|
$this->assertEquals( 0, count( $data ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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-items' );
|
2019-10-21 11:11:52 +00:00
|
|
|
$cart = wc()->cart->get_cart();
|
2020-03-13 12:41:01 +00:00
|
|
|
$response = $controller->prepare_item_for_response( current( $cart ), new \WP_REST_Request() );
|
2020-01-17 11:34:15 +00:00
|
|
|
$data = $response->get_data();
|
|
|
|
|
|
|
|
$this->assertArrayHasKey( 'key', $data );
|
|
|
|
$this->assertArrayHasKey( 'id', $data );
|
|
|
|
$this->assertArrayHasKey( 'quantity', $data );
|
|
|
|
$this->assertArrayHasKey( 'name', $data );
|
|
|
|
$this->assertArrayHasKey( 'sku', $data );
|
|
|
|
$this->assertArrayHasKey( 'permalink', $data );
|
|
|
|
$this->assertArrayHasKey( 'images', $data );
|
|
|
|
$this->assertArrayHasKey( 'totals', $data );
|
|
|
|
$this->assertArrayHasKey( 'variation', $data );
|
2021-01-12 10:04:53 +00:00
|
|
|
$this->assertArrayHasKey( 'item_data', $data );
|
2020-01-17 11:46:17 +00:00
|
|
|
$this->assertArrayHasKey( 'low_stock_remaining', $data );
|
2020-03-10 11:43:57 +00:00
|
|
|
$this->assertArrayHasKey( 'backorders_allowed', $data );
|
2020-07-14 15:25:53 +00:00
|
|
|
$this->assertArrayHasKey( 'show_backorder_badge', $data );
|
2020-01-17 11:46:17 +00:00
|
|
|
$this->assertArrayHasKey( 'short_description', $data );
|
2020-11-23 13:03:08 +00:00
|
|
|
$this->assertArrayHasKey( 'catalog_visibility', $data );
|
2020-01-17 11:34:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test schema matches responses.
|
|
|
|
*
|
|
|
|
* Tests schema of both products in cart to cover as much schema as possible.
|
|
|
|
*/
|
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-items' );
|
2020-01-17 11:34:15 +00:00
|
|
|
$schema = $controller->get_item_schema();
|
2020-04-06 10:36:28 +00:00
|
|
|
$cart = wc()->cart->get_cart();
|
2020-01-17 11:34:15 +00:00
|
|
|
$validate = new ValidateSchema( $schema );
|
|
|
|
|
|
|
|
// Simple product.
|
2020-03-13 12:41:01 +00:00
|
|
|
$response = $controller->prepare_item_for_response( current( $cart ), new \WP_REST_Request() );
|
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
|
|
|
|
2020-01-17 11:34:15 +00:00
|
|
|
// Variable product.
|
2020-03-13 12:41:01 +00:00
|
|
|
$response = $controller->prepare_item_for_response( end( $cart ), new \WP_REST_Request() );
|
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
|
|
|
}
|
|
|
|
}
|