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
|
|
|
/**
|
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();
|
|
|
|
|
2022-02-23 12:00:45 +00:00
|
|
|
$this->products = array(
|
|
|
|
$fixtures->get_simple_product(
|
|
|
|
array(
|
|
|
|
'name' => 'Test Product 1',
|
|
|
|
'stock_status' => 'instock',
|
|
|
|
'regular_price' => 10,
|
|
|
|
'weight' => 10,
|
|
|
|
'image_id' => $fixtures->sideload_image(),
|
|
|
|
)
|
|
|
|
),
|
|
|
|
);
|
2021-08-20 13:58:32 +00:00
|
|
|
|
|
|
|
$variable_product = $fixtures->get_variable_product(
|
2022-02-23 12:00:45 +00:00
|
|
|
array(
|
|
|
|
'name' => 'Test Product 2',
|
|
|
|
'stock_status' => 'instock',
|
2021-08-20 13:58:32 +00:00
|
|
|
'regular_price' => 10,
|
2022-02-23 12:00:45 +00:00
|
|
|
'weight' => 10,
|
|
|
|
'image_id' => $fixtures->sideload_image(),
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
$fixtures->get_product_attribute( 'color', array( 'red', 'green', 'blue' ) ),
|
|
|
|
$fixtures->get_product_attribute( 'size', array( 'small', 'medium', 'large' ) ),
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$variation = $fixtures->get_variation_product(
|
|
|
|
$variable_product->get_id(),
|
|
|
|
array(
|
|
|
|
'pa_color' => 'red',
|
|
|
|
'pa_size' => 'small',
|
|
|
|
)
|
2021-08-20 13:58:32 +00:00
|
|
|
);
|
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();
|
2022-02-23 12:00: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 );
|
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',
|
2022-02-23 12:00:45 +00:00
|
|
|
'attribute_pa_size' => 'small',
|
2020-08-25 08:33:41 +00:00
|
|
|
)
|
|
|
|
);
|
2019-10-21 11:11:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test getting cart.
|
|
|
|
*/
|
|
|
|
public function test_get_items() {
|
2022-02-23 12:00:45 +00:00
|
|
|
$this->assertAPIResponse(
|
|
|
|
'/wc/store/v1/cart/items',
|
|
|
|
200,
|
|
|
|
array(
|
|
|
|
0 => array(
|
|
|
|
'key' => $this->keys[0],
|
|
|
|
'id' => $this->products[0]->get_id(),
|
|
|
|
'name' => $this->products[0]->get_name(),
|
|
|
|
'sku' => $this->products[0]->get_sku(),
|
|
|
|
'permalink' => $this->products[0]->get_permalink(),
|
|
|
|
'quantity' => 2,
|
|
|
|
'totals' => array(
|
|
|
|
'line_subtotal' => '2000',
|
|
|
|
'line_total' => '2000',
|
|
|
|
),
|
|
|
|
),
|
|
|
|
1 => array(
|
|
|
|
'key' => $this->keys[1],
|
|
|
|
'quantity' => 1,
|
|
|
|
'totals' => array(
|
|
|
|
'line_subtotal' => '1000',
|
|
|
|
'line_total' => '1000',
|
|
|
|
),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
);
|
2019-10-21 11:11:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test getting cart item by key.
|
|
|
|
*/
|
|
|
|
public function test_get_item() {
|
2022-02-23 12:00:45 +00:00
|
|
|
$this->assertAPIResponse(
|
|
|
|
'/wc/store/v1/cart/items/' . $this->keys[0],
|
|
|
|
200,
|
|
|
|
array(
|
|
|
|
'key' => $this->keys[0],
|
|
|
|
'id' => $this->products[0]->get_id(),
|
|
|
|
'name' => $this->products[0]->get_name(),
|
|
|
|
'sku' => $this->products[0]->get_sku(),
|
|
|
|
'permalink' => $this->products[0]->get_permalink(),
|
|
|
|
'quantity' => 2,
|
|
|
|
'totals' => array(
|
|
|
|
'line_subtotal' => '2000',
|
|
|
|
'line_total' => '2000',
|
|
|
|
),
|
|
|
|
)
|
|
|
|
);
|
2019-10-21 11:11:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test add to cart.
|
|
|
|
*/
|
|
|
|
public function test_create_item() {
|
|
|
|
wc_empty_cart();
|
|
|
|
|
2022-02-23 12:00:45 +00:00
|
|
|
$request = new \WP_REST_Request( 'POST', '/wc/store/v1/cart/items' );
|
2022-03-11 12:07:08 +00:00
|
|
|
$request->set_header( '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',
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
2022-02-23 12:00:45 +00:00
|
|
|
$this->assertAPIResponse(
|
|
|
|
$request,
|
|
|
|
201,
|
|
|
|
array(
|
|
|
|
'id' => $this->products[0]->get_id(),
|
|
|
|
'quantity' => 10,
|
|
|
|
)
|
|
|
|
);
|
2019-10-21 11:11:52 +00:00
|
|
|
|
2022-02-23 12:00:45 +00:00
|
|
|
$this->assertAPIResponse(
|
|
|
|
$request,
|
|
|
|
201,
|
|
|
|
array(
|
|
|
|
'id' => $this->products[0]->get_id(),
|
|
|
|
'quantity' => 20,
|
|
|
|
)
|
|
|
|
);
|
2019-10-21 11:11:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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();
|
2022-02-23 12:00:45 +00:00
|
|
|
$invalid_product = $fixtures->get_simple_product(
|
|
|
|
array(
|
|
|
|
'name' => 'Invalid Product',
|
|
|
|
'regular_price' => '',
|
|
|
|
)
|
|
|
|
);
|
2019-10-21 11:11:52 +00:00
|
|
|
|
2022-02-23 12:00:45 +00:00
|
|
|
$request = new \WP_REST_Request( 'POST', '/wc/store/v1/cart/items' );
|
2022-03-11 12:07:08 +00:00
|
|
|
$request->set_header( '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',
|
|
|
|
)
|
|
|
|
);
|
2022-02-23 12:00:45 +00:00
|
|
|
$this->assertAPIResponse(
|
|
|
|
$request,
|
|
|
|
400
|
|
|
|
);
|
2019-10-21 11:11:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test updating an item.
|
|
|
|
*/
|
|
|
|
public function test_update_item() {
|
2022-02-23 12:00:45 +00:00
|
|
|
$request = new \WP_REST_Request( 'PUT', '/wc/store/v1/cart/items/' . $this->keys[0] );
|
2022-03-11 12:07:08 +00:00
|
|
|
$request->set_header( '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() {
|
2022-02-23 12:00:45 +00:00
|
|
|
$request = new \WP_REST_Request( 'DELETE', '/wc/store/v1/cart/items/' . $this->keys[0] );
|
2022-03-11 12:07:08 +00:00
|
|
|
$request->set_header( 'Nonce', wp_create_nonce( 'wc_store_api' ) );
|
2022-02-23 12:00:45 +00:00
|
|
|
$this->assertAPIResponse(
|
|
|
|
$request,
|
|
|
|
204,
|
|
|
|
array()
|
|
|
|
);
|
2019-10-21 11:11:52 +00:00
|
|
|
|
2022-02-23 12:00:45 +00:00
|
|
|
$request = new \WP_REST_Request( 'DELETE', '/wc/store/v1/cart/items/' . $this->keys[0] );
|
2022-03-11 12:07:08 +00:00
|
|
|
$request->set_header( 'Nonce', wp_create_nonce( 'wc_store_api' ) );
|
2022-02-23 12:00:45 +00:00
|
|
|
$this->assertAPIResponse(
|
|
|
|
$request,
|
Add notice on quantity change and update `wc/store/cart` to use thunks (https://github.com/woocommerce/woocommerce-blocks/pull/7938)
* Add receiveCart thunk
* Add mapCartResponseToCart helper
* Add getItemsPendingQuantityUpdate selector
* Update cart resolvers to be thunks
* Remove RECEIVE_CART action and replace with SET_CART_DATA
receiveCart will turn into a thunk.
* Add notifyQuantityChanges functions
* Remove receiveCart from action type definition, replace with setCartData
* Move apiFetchWithHeaders out of controls
This will just be a normal function since we'll be updating actions to thunks which will use this instead of a control.
* Include thunks in actions file
* Update receiveCart action to setCartData
* Update applyCoupon action to a thunk
* Update useStoreCartCoupons to get action from correct place
* Update StoreCartCoupon types
* Add types for Thunk and ThunkReturnType in mapped-types
* Change applyCoupon to a thunk
* Get applyCoupon, removeCoupon, receiveApplyingCoupon from useDispatch
This is to separate the concerns of actions vs. selectors. Previously the actions were fetched during useSelect which is not a pattern we use anywhere else in the codebase. Since we updated the MapToDispatch type, we can now get correctly typed thunks from the data store.
* Improve apiFetchWithHeaders typings
* Convert removeCoupon from generator to thunk
* Add applyCoupon and removeCoupon to CartAction type
* Remove unused old-style type-def
* Add receiveApplyingCoupon & receiveRemovingCoupon to StoreCartCoupon
* Correct issues with StoreCartCoupon type
These were not intended to reflect the actions in data store, rather the functions offered by the useStoreCartCoupons hook.
* Update applyExtensionCartUpdate to a thunk
* Update addItemToCart to thunk
* Add ResolveSelectFromMap type that works with thunks
* Add CartDispatchFromMap and CartResolveSelectFromMap types
We can add this to all data stores to get them working with thunks properly.
* Add docs and update generic name in ResolveSelectFromMap
* Add correct types for thunk resolvers in cart data store
* Update removeItemFromCart to thunk
* Update apiFetchWithHeaders to use generic
* Update selectShippingRate to thunk
* Update resolver tests to test correct thunk functionality
* Update updateCustomerData to thunk
* Update reducer test to reflect new action name
* Update comments on CartDispatchFromMap and CartResolveSelectFromMap
* Add quantity_limits to preview cart
* Make notices speak when shown
* Remove copilot comment
* Add isWithinQuantityLimits function
This is because we shouldn't show a notice if the quantity limits change, but the item's quantity is still OK.
* Add tests for notifyQuantityChanges
* Show notice when multiple_of is updated
* Update test to test for multiple_of changes
* Remove empty export
* Remove controls from cart data store
Not needed anymore since the exported value from the shared-controls file was empty.
* Export a control and async function for apiFetchWithHeaders
This is required because async functions cannot be called from sync generators.
* Use control version of apiFetchWithHeaders in the collections store
* Improve comments and remove incorrect TypeScript
* Update assets/js/data/cart/actions.ts
Co-authored-by: Mike Jolley <mike.jolley@me.com>
* Update ResolveSelectFromMap to include selectors too
* Update TS in actions
* Use finally to remove duplicate code
* remove item pending delete/qty update after action runs in all cases
This will also reset the state when the request to remove it/change quantity errors
* Remove unnecessary type from param.
Not needed because we have TS now. The description can stay though, it is useful.
* Update snackbar wording to use active voice
* Remove old WP version check
* Set max quantity to high number instead of null
This would only happen in a niche case, and would require several TS changes to fix, so it's better to set it as a number here. 9999 should be high enough, and is the default quantity limit set below in get_product_quantity_limit
* Set code on woocommerce_rest_cart_invalid_key to 409
This is so the cart is returned in the response, so the client can update.
* Fix typo in comment and add CartSelectFromMap
* Remove unnecessary docblock
* Add getItemsPendingDelete selector
This is needed so we can show a notice for items that are unexpectedly removed from the cart. We need to know which ones are pending delete so we can skip showing the notice for them.
* Add type for notifyQuantityChanges args and change args to object
* Add notifyIfRemoved function
This will check items that have been removed and show a notice for them.
* Fix TS in receiveCart & pass itemsPendingDelete to notifyQuantiyChanges
* Update wording on removal notice
* Update types for notifyQuantityChanges args
* Update tests to reflect new wording and args being an object
* Check item is truth before running comparison of keys
* Update tests for unexpectedly and expectedly removed items
* Ignore print_r to satisfy phpcs
* Update PHP tests to reflect correct response code when deleting items
* Remove unnecessary controls and dispatch events directly from thunk
Co-authored-by: Mike Jolley <mike.jolley@me.com>
2022-12-16 16:06:37 +00:00
|
|
|
409
|
2022-02-23 12:00:45 +00:00
|
|
|
);
|
2019-10-21 11:11:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test delete all items.
|
|
|
|
*/
|
|
|
|
public function test_delete_items() {
|
2022-02-23 12:00:45 +00:00
|
|
|
$request = new \WP_REST_Request( 'DELETE', '/wc/store/v1/cart/items' );
|
2022-03-11 12:07:08 +00:00
|
|
|
$request->set_header( '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() );
|
2022-02-23 12:00:45 +00:00
|
|
|
$this->assertEquals( array(), $data );
|
2019-10-21 11:11:52 +00:00
|
|
|
|
2022-02-23 12:00:45 +00:00
|
|
|
$response = rest_get_server()->dispatch( new \WP_REST_Request( 'GET', '/wc/store/v1/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() {
|
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-items', 'v1' );
|
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() {
|
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-items', 'v1' );
|
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() );
|
Add notice on quantity change and update `wc/store/cart` to use thunks (https://github.com/woocommerce/woocommerce-blocks/pull/7938)
* Add receiveCart thunk
* Add mapCartResponseToCart helper
* Add getItemsPendingQuantityUpdate selector
* Update cart resolvers to be thunks
* Remove RECEIVE_CART action and replace with SET_CART_DATA
receiveCart will turn into a thunk.
* Add notifyQuantityChanges functions
* Remove receiveCart from action type definition, replace with setCartData
* Move apiFetchWithHeaders out of controls
This will just be a normal function since we'll be updating actions to thunks which will use this instead of a control.
* Include thunks in actions file
* Update receiveCart action to setCartData
* Update applyCoupon action to a thunk
* Update useStoreCartCoupons to get action from correct place
* Update StoreCartCoupon types
* Add types for Thunk and ThunkReturnType in mapped-types
* Change applyCoupon to a thunk
* Get applyCoupon, removeCoupon, receiveApplyingCoupon from useDispatch
This is to separate the concerns of actions vs. selectors. Previously the actions were fetched during useSelect which is not a pattern we use anywhere else in the codebase. Since we updated the MapToDispatch type, we can now get correctly typed thunks from the data store.
* Improve apiFetchWithHeaders typings
* Convert removeCoupon from generator to thunk
* Add applyCoupon and removeCoupon to CartAction type
* Remove unused old-style type-def
* Add receiveApplyingCoupon & receiveRemovingCoupon to StoreCartCoupon
* Correct issues with StoreCartCoupon type
These were not intended to reflect the actions in data store, rather the functions offered by the useStoreCartCoupons hook.
* Update applyExtensionCartUpdate to a thunk
* Update addItemToCart to thunk
* Add ResolveSelectFromMap type that works with thunks
* Add CartDispatchFromMap and CartResolveSelectFromMap types
We can add this to all data stores to get them working with thunks properly.
* Add docs and update generic name in ResolveSelectFromMap
* Add correct types for thunk resolvers in cart data store
* Update removeItemFromCart to thunk
* Update apiFetchWithHeaders to use generic
* Update selectShippingRate to thunk
* Update resolver tests to test correct thunk functionality
* Update updateCustomerData to thunk
* Update reducer test to reflect new action name
* Update comments on CartDispatchFromMap and CartResolveSelectFromMap
* Add quantity_limits to preview cart
* Make notices speak when shown
* Remove copilot comment
* Add isWithinQuantityLimits function
This is because we shouldn't show a notice if the quantity limits change, but the item's quantity is still OK.
* Add tests for notifyQuantityChanges
* Show notice when multiple_of is updated
* Update test to test for multiple_of changes
* Remove empty export
* Remove controls from cart data store
Not needed anymore since the exported value from the shared-controls file was empty.
* Export a control and async function for apiFetchWithHeaders
This is required because async functions cannot be called from sync generators.
* Use control version of apiFetchWithHeaders in the collections store
* Improve comments and remove incorrect TypeScript
* Update assets/js/data/cart/actions.ts
Co-authored-by: Mike Jolley <mike.jolley@me.com>
* Update ResolveSelectFromMap to include selectors too
* Update TS in actions
* Use finally to remove duplicate code
* remove item pending delete/qty update after action runs in all cases
This will also reset the state when the request to remove it/change quantity errors
* Remove unnecessary type from param.
Not needed because we have TS now. The description can stay though, it is useful.
* Update snackbar wording to use active voice
* Remove old WP version check
* Set max quantity to high number instead of null
This would only happen in a niche case, and would require several TS changes to fix, so it's better to set it as a number here. 9999 should be high enough, and is the default quantity limit set below in get_product_quantity_limit
* Set code on woocommerce_rest_cart_invalid_key to 409
This is so the cart is returned in the response, so the client can update.
* Fix typo in comment and add CartSelectFromMap
* Remove unnecessary docblock
* Add getItemsPendingDelete selector
This is needed so we can show a notice for items that are unexpectedly removed from the cart. We need to know which ones are pending delete so we can skip showing the notice for them.
* Add type for notifyQuantityChanges args and change args to object
* Add notifyIfRemoved function
This will check items that have been removed and show a notice for them.
* Fix TS in receiveCart & pass itemsPendingDelete to notifyQuantiyChanges
* Update wording on removal notice
* Update types for notifyQuantityChanges args
* Update tests to reflect new wording and args being an object
* Check item is truth before running comparison of keys
* Update tests for unexpectedly and expectedly removed items
* Ignore print_r to satisfy phpcs
* Update PHP tests to reflect correct response code when deleting items
* Remove unnecessary controls and dispatch events directly from thunk
Co-authored-by: Mike Jolley <mike.jolley@me.com>
2022-12-16 16:06:37 +00:00
|
|
|
// phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_print_r
|
2020-01-17 11:34:15 +00:00
|
|
|
$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() );
|
Add notice on quantity change and update `wc/store/cart` to use thunks (https://github.com/woocommerce/woocommerce-blocks/pull/7938)
* Add receiveCart thunk
* Add mapCartResponseToCart helper
* Add getItemsPendingQuantityUpdate selector
* Update cart resolvers to be thunks
* Remove RECEIVE_CART action and replace with SET_CART_DATA
receiveCart will turn into a thunk.
* Add notifyQuantityChanges functions
* Remove receiveCart from action type definition, replace with setCartData
* Move apiFetchWithHeaders out of controls
This will just be a normal function since we'll be updating actions to thunks which will use this instead of a control.
* Include thunks in actions file
* Update receiveCart action to setCartData
* Update applyCoupon action to a thunk
* Update useStoreCartCoupons to get action from correct place
* Update StoreCartCoupon types
* Add types for Thunk and ThunkReturnType in mapped-types
* Change applyCoupon to a thunk
* Get applyCoupon, removeCoupon, receiveApplyingCoupon from useDispatch
This is to separate the concerns of actions vs. selectors. Previously the actions were fetched during useSelect which is not a pattern we use anywhere else in the codebase. Since we updated the MapToDispatch type, we can now get correctly typed thunks from the data store.
* Improve apiFetchWithHeaders typings
* Convert removeCoupon from generator to thunk
* Add applyCoupon and removeCoupon to CartAction type
* Remove unused old-style type-def
* Add receiveApplyingCoupon & receiveRemovingCoupon to StoreCartCoupon
* Correct issues with StoreCartCoupon type
These were not intended to reflect the actions in data store, rather the functions offered by the useStoreCartCoupons hook.
* Update applyExtensionCartUpdate to a thunk
* Update addItemToCart to thunk
* Add ResolveSelectFromMap type that works with thunks
* Add CartDispatchFromMap and CartResolveSelectFromMap types
We can add this to all data stores to get them working with thunks properly.
* Add docs and update generic name in ResolveSelectFromMap
* Add correct types for thunk resolvers in cart data store
* Update removeItemFromCart to thunk
* Update apiFetchWithHeaders to use generic
* Update selectShippingRate to thunk
* Update resolver tests to test correct thunk functionality
* Update updateCustomerData to thunk
* Update reducer test to reflect new action name
* Update comments on CartDispatchFromMap and CartResolveSelectFromMap
* Add quantity_limits to preview cart
* Make notices speak when shown
* Remove copilot comment
* Add isWithinQuantityLimits function
This is because we shouldn't show a notice if the quantity limits change, but the item's quantity is still OK.
* Add tests for notifyQuantityChanges
* Show notice when multiple_of is updated
* Update test to test for multiple_of changes
* Remove empty export
* Remove controls from cart data store
Not needed anymore since the exported value from the shared-controls file was empty.
* Export a control and async function for apiFetchWithHeaders
This is required because async functions cannot be called from sync generators.
* Use control version of apiFetchWithHeaders in the collections store
* Improve comments and remove incorrect TypeScript
* Update assets/js/data/cart/actions.ts
Co-authored-by: Mike Jolley <mike.jolley@me.com>
* Update ResolveSelectFromMap to include selectors too
* Update TS in actions
* Use finally to remove duplicate code
* remove item pending delete/qty update after action runs in all cases
This will also reset the state when the request to remove it/change quantity errors
* Remove unnecessary type from param.
Not needed because we have TS now. The description can stay though, it is useful.
* Update snackbar wording to use active voice
* Remove old WP version check
* Set max quantity to high number instead of null
This would only happen in a niche case, and would require several TS changes to fix, so it's better to set it as a number here. 9999 should be high enough, and is the default quantity limit set below in get_product_quantity_limit
* Set code on woocommerce_rest_cart_invalid_key to 409
This is so the cart is returned in the response, so the client can update.
* Fix typo in comment and add CartSelectFromMap
* Remove unnecessary docblock
* Add getItemsPendingDelete selector
This is needed so we can show a notice for items that are unexpectedly removed from the cart. We need to know which ones are pending delete so we can skip showing the notice for them.
* Add type for notifyQuantityChanges args and change args to object
* Add notifyIfRemoved function
This will check items that have been removed and show a notice for them.
* Fix TS in receiveCart & pass itemsPendingDelete to notifyQuantiyChanges
* Update wording on removal notice
* Update types for notifyQuantityChanges args
* Update tests to reflect new wording and args being an object
* Check item is truth before running comparison of keys
* Update tests for unexpectedly and expectedly removed items
* Ignore print_r to satisfy phpcs
* Update PHP tests to reflect correct response code when deleting items
* Remove unnecessary controls and dispatch events directly from thunk
Co-authored-by: Mike Jolley <mike.jolley@me.com>
2022-12-16 16:06:37 +00:00
|
|
|
// phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_print_r
|
2020-01-17 11:34:15 +00:00
|
|
|
$this->assertEmpty( $diff, print_r( $diff, true ) );
|
2019-10-21 11:11:52 +00:00
|
|
|
}
|
|
|
|
}
|