woocommerce/plugins/woocommerce-blocks/tests/php/StoreApi/Routes/Cart.php

496 lines
13 KiB
PHP
Raw Normal View History

<?php
/**
* Controller Tests.
*/
namespace Automattic\WooCommerce\Blocks\Tests\StoreApi\Routes;
use Automattic\WooCommerce\Blocks\Tests\StoreApi\Routes\ControllerTestCase;
use Automattic\WooCommerce\Blocks\Tests\Helpers\FixtureData;
use Automattic\WooCommerce\Blocks\Tests\Helpers\ValidateSchema;
/**
* Cart Controller Tests.
*/
class Cart extends ControllerTestCase {
/**
* Setup test products data. Called before every test.
*/
public function setUp() {
parent::setUp();
$fixtures = new FixtureData();
$fixtures->shipping_add_flat_rate();
$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,
)
),
);
$this->coupon = $fixtures->get_coupon(
array(
'code' => 'test_coupon',
'discount_type' => 'fixed_cart',
'amount' => 1,
)
);
wc_empty_cart();
$this->keys = array();
$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 );
wc()->cart->apply_coupon( $this->coupon->get_code() );
// Draft order.
$order = new \WC_Order();
$order->set_status( 'checkout-draft' );
$order->save();
wc()->session->set( 'store_api_draft_order', $order->get_id() );
}
/**
* Test getting cart.
*/
public function test_get_item() {
$this->assertAPIResponse(
'/wc/store/v1/cart',
200,
array(
'items_count' => 3,
'needs_payment' => true,
'needs_shipping' => true,
'items_weight' => '30',
'items' => function( $value ) {
return count( $value ) === 2;
},
'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(),
),
)
);
}
ensure cart totals update when items are removed or quantity changed (https://github.com/woocommerce/woocommerce-blocks/pull/1840) * ensure cart totals update when items are removed (prototype): - return complete cart object from DELETE cart/items/:key - use receiveCart on client to update whole cart including total * move API endpoint for removing cart items to cart controller: - returns full cart schema so should be part of cart controller - is now a POST request with param - not strict REST - fix up client action to use new API * move API test for removing cart items (API has moved) * use correct path for remove API in tests (doh!) * add extra API test for remove_cart_item with bad key => 404 (experiment) * experiment: delete test_remove_cart_item, does test_remove_bad_cart_item work? * reinstate test_remove_cart_item with single valid request * remove unnecessary newline * tidy comments in PHP api tests, rerun travis? * remove test_remove_cart_item which may be causing problems * reinstate troublesome remove cart item api test (experiment): - see if this works now travis issue is resolved * whitespace * show correct total when changing cart item quantity: - move update cart item quantity API to cart controller - & return full cart response - update js action to new API route & receive full cart response * simplify test_remove_cart_item API test - now just tests a valid remove succeeds * remove test_update_item (API has moved) + + experimentally remove test_remove_bad_cart_item - testing if cart remove tests interact with each other * fix tests (🤞) - pass params in body, not as query params * reinstate test for re-deleting same item * update API docs - update/delete cart item have moved to /cart * add response data checks to new cart API tests: - extra protection against expected 404 "false positives" * reinstate API test for changing cart item quantity * fix remove cart item body tests - MIA items return error code, not cart * fix test - quantity param is int not string * attempt fix 404ing test_update_item: - only allow POST, remove trailing `/` - align array equals for good measure :) * fix action for update-item - method=POST, now takes body params * fix response body asserts in test_update_item * reinstate update_item and delete_item on CartItems controller * typos + examples in new cart items API docs * reorder previous cart item docs to minimise diff/churn * reinstate tabs in docs response example
2020-03-05 19:11:39 +00:00
/**
* Test removing a nonexistent cart item. This should return 409 conflict with updated cart data.
ensure cart totals update when items are removed or quantity changed (https://github.com/woocommerce/woocommerce-blocks/pull/1840) * ensure cart totals update when items are removed (prototype): - return complete cart object from DELETE cart/items/:key - use receiveCart on client to update whole cart including total * move API endpoint for removing cart items to cart controller: - returns full cart schema so should be part of cart controller - is now a POST request with param - not strict REST - fix up client action to use new API * move API test for removing cart items (API has moved) * use correct path for remove API in tests (doh!) * add extra API test for remove_cart_item with bad key => 404 (experiment) * experiment: delete test_remove_cart_item, does test_remove_bad_cart_item work? * reinstate test_remove_cart_item with single valid request * remove unnecessary newline * tidy comments in PHP api tests, rerun travis? * remove test_remove_cart_item which may be causing problems * reinstate troublesome remove cart item api test (experiment): - see if this works now travis issue is resolved * whitespace * show correct total when changing cart item quantity: - move update cart item quantity API to cart controller - & return full cart response - update js action to new API route & receive full cart response * simplify test_remove_cart_item API test - now just tests a valid remove succeeds * remove test_update_item (API has moved) + + experimentally remove test_remove_bad_cart_item - testing if cart remove tests interact with each other * fix tests (🤞) - pass params in body, not as query params * reinstate test for re-deleting same item * update API docs - update/delete cart item have moved to /cart * add response data checks to new cart API tests: - extra protection against expected 404 "false positives" * reinstate API test for changing cart item quantity * fix remove cart item body tests - MIA items return error code, not cart * fix test - quantity param is int not string * attempt fix 404ing test_update_item: - only allow POST, remove trailing `/` - align array equals for good measure :) * fix action for update-item - method=POST, now takes body params * fix response body asserts in test_update_item * reinstate update_item and delete_item on CartItems controller * typos + examples in new cart items API docs * reorder previous cart item docs to minimise diff/churn * reinstate tabs in docs response example
2020-03-05 19:11:39 +00:00
*/
public function test_remove_bad_cart_item() {
$request = new \WP_REST_Request( 'POST', '/wc/store/v1/cart/remove-item' );
$request->set_header( 'X-WC-Store-API-Nonce', wp_create_nonce( 'wc_store_api' ) );
ensure cart totals update when items are removed or quantity changed (https://github.com/woocommerce/woocommerce-blocks/pull/1840) * ensure cart totals update when items are removed (prototype): - return complete cart object from DELETE cart/items/:key - use receiveCart on client to update whole cart including total * move API endpoint for removing cart items to cart controller: - returns full cart schema so should be part of cart controller - is now a POST request with param - not strict REST - fix up client action to use new API * move API test for removing cart items (API has moved) * use correct path for remove API in tests (doh!) * add extra API test for remove_cart_item with bad key => 404 (experiment) * experiment: delete test_remove_cart_item, does test_remove_bad_cart_item work? * reinstate test_remove_cart_item with single valid request * remove unnecessary newline * tidy comments in PHP api tests, rerun travis? * remove test_remove_cart_item which may be causing problems * reinstate troublesome remove cart item api test (experiment): - see if this works now travis issue is resolved * whitespace * show correct total when changing cart item quantity: - move update cart item quantity API to cart controller - & return full cart response - update js action to new API route & receive full cart response * simplify test_remove_cart_item API test - now just tests a valid remove succeeds * remove test_update_item (API has moved) + + experimentally remove test_remove_bad_cart_item - testing if cart remove tests interact with each other * fix tests (🤞) - pass params in body, not as query params * reinstate test for re-deleting same item * update API docs - update/delete cart item have moved to /cart * add response data checks to new cart API tests: - extra protection against expected 404 "false positives" * reinstate API test for changing cart item quantity * fix remove cart item body tests - MIA items return error code, not cart * fix test - quantity param is int not string * attempt fix 404ing test_update_item: - only allow POST, remove trailing `/` - align array equals for good measure :) * fix action for update-item - method=POST, now takes body params * fix response body asserts in test_update_item * reinstate update_item and delete_item on CartItems controller * typos + examples in new cart items API docs * reorder previous cart item docs to minimise diff/churn * reinstate tabs in docs response example
2020-03-05 19:11:39 +00:00
$request->set_body_params(
array(
'key' => 'bad_item_key_123',
)
);
$this->assertAPIResponse(
$request,
409,
array(
'code' => 'woocommerce_rest_cart_invalid_key',
)
);
ensure cart totals update when items are removed or quantity changed (https://github.com/woocommerce/woocommerce-blocks/pull/1840) * ensure cart totals update when items are removed (prototype): - return complete cart object from DELETE cart/items/:key - use receiveCart on client to update whole cart including total * move API endpoint for removing cart items to cart controller: - returns full cart schema so should be part of cart controller - is now a POST request with param - not strict REST - fix up client action to use new API * move API test for removing cart items (API has moved) * use correct path for remove API in tests (doh!) * add extra API test for remove_cart_item with bad key => 404 (experiment) * experiment: delete test_remove_cart_item, does test_remove_bad_cart_item work? * reinstate test_remove_cart_item with single valid request * remove unnecessary newline * tidy comments in PHP api tests, rerun travis? * remove test_remove_cart_item which may be causing problems * reinstate troublesome remove cart item api test (experiment): - see if this works now travis issue is resolved * whitespace * show correct total when changing cart item quantity: - move update cart item quantity API to cart controller - & return full cart response - update js action to new API route & receive full cart response * simplify test_remove_cart_item API test - now just tests a valid remove succeeds * remove test_update_item (API has moved) + + experimentally remove test_remove_bad_cart_item - testing if cart remove tests interact with each other * fix tests (🤞) - pass params in body, not as query params * reinstate test for re-deleting same item * update API docs - update/delete cart item have moved to /cart * add response data checks to new cart API tests: - extra protection against expected 404 "false positives" * reinstate API test for changing cart item quantity * fix remove cart item body tests - MIA items return error code, not cart * fix test - quantity param is int not string * attempt fix 404ing test_update_item: - only allow POST, remove trailing `/` - align array equals for good measure :) * fix action for update-item - method=POST, now takes body params * fix response body asserts in test_update_item * reinstate update_item and delete_item on CartItems controller * typos + examples in new cart items API docs * reorder previous cart item docs to minimise diff/churn * reinstate tabs in docs response example
2020-03-05 19:11:39 +00:00
}
/**
* Test remove cart item.
*/
public function test_remove_cart_item() {
$request = new \WP_REST_Request( 'POST', '/wc/store/v1/cart/remove-item' );
$request->set_header( 'X-WC-Store-API-Nonce', wp_create_nonce( 'wc_store_api' ) );
ensure cart totals update when items are removed or quantity changed (https://github.com/woocommerce/woocommerce-blocks/pull/1840) * ensure cart totals update when items are removed (prototype): - return complete cart object from DELETE cart/items/:key - use receiveCart on client to update whole cart including total * move API endpoint for removing cart items to cart controller: - returns full cart schema so should be part of cart controller - is now a POST request with param - not strict REST - fix up client action to use new API * move API test for removing cart items (API has moved) * use correct path for remove API in tests (doh!) * add extra API test for remove_cart_item with bad key => 404 (experiment) * experiment: delete test_remove_cart_item, does test_remove_bad_cart_item work? * reinstate test_remove_cart_item with single valid request * remove unnecessary newline * tidy comments in PHP api tests, rerun travis? * remove test_remove_cart_item which may be causing problems * reinstate troublesome remove cart item api test (experiment): - see if this works now travis issue is resolved * whitespace * show correct total when changing cart item quantity: - move update cart item quantity API to cart controller - & return full cart response - update js action to new API route & receive full cart response * simplify test_remove_cart_item API test - now just tests a valid remove succeeds * remove test_update_item (API has moved) + + experimentally remove test_remove_bad_cart_item - testing if cart remove tests interact with each other * fix tests (🤞) - pass params in body, not as query params * reinstate test for re-deleting same item * update API docs - update/delete cart item have moved to /cart * add response data checks to new cart API tests: - extra protection against expected 404 "false positives" * reinstate API test for changing cart item quantity * fix remove cart item body tests - MIA items return error code, not cart * fix test - quantity param is int not string * attempt fix 404ing test_update_item: - only allow POST, remove trailing `/` - align array equals for good measure :) * fix action for update-item - method=POST, now takes body params * fix response body asserts in test_update_item * reinstate update_item and delete_item on CartItems controller * typos + examples in new cart items API docs * reorder previous cart item docs to minimise diff/churn * reinstate tabs in docs response example
2020-03-05 19:11:39 +00:00
$request->set_body_params(
array(
'key' => $this->keys[0],
)
);
$this->assertAPIResponse(
$request,
200,
array(
'items_count' => 1,
'items' => function( $value ) {
return count( $value ) === 1;
},
'items_weight' => '10',
'totals' => array(
'total_items' => '1000',
),
)
);
ensure cart totals update when items are removed or quantity changed (https://github.com/woocommerce/woocommerce-blocks/pull/1840) * ensure cart totals update when items are removed (prototype): - return complete cart object from DELETE cart/items/:key - use receiveCart on client to update whole cart including total * move API endpoint for removing cart items to cart controller: - returns full cart schema so should be part of cart controller - is now a POST request with param - not strict REST - fix up client action to use new API * move API test for removing cart items (API has moved) * use correct path for remove API in tests (doh!) * add extra API test for remove_cart_item with bad key => 404 (experiment) * experiment: delete test_remove_cart_item, does test_remove_bad_cart_item work? * reinstate test_remove_cart_item with single valid request * remove unnecessary newline * tidy comments in PHP api tests, rerun travis? * remove test_remove_cart_item which may be causing problems * reinstate troublesome remove cart item api test (experiment): - see if this works now travis issue is resolved * whitespace * show correct total when changing cart item quantity: - move update cart item quantity API to cart controller - & return full cart response - update js action to new API route & receive full cart response * simplify test_remove_cart_item API test - now just tests a valid remove succeeds * remove test_update_item (API has moved) + + experimentally remove test_remove_bad_cart_item - testing if cart remove tests interact with each other * fix tests (🤞) - pass params in body, not as query params * reinstate test for re-deleting same item * update API docs - update/delete cart item have moved to /cart * add response data checks to new cart API tests: - extra protection against expected 404 "false positives" * reinstate API test for changing cart item quantity * fix remove cart item body tests - MIA items return error code, not cart * fix test - quantity param is int not string * attempt fix 404ing test_update_item: - only allow POST, remove trailing `/` - align array equals for good measure :) * fix action for update-item - method=POST, now takes body params * fix response body asserts in test_update_item * reinstate update_item and delete_item on CartItems controller * typos + examples in new cart items API docs * reorder previous cart item docs to minimise diff/churn * reinstate tabs in docs response example
2020-03-05 19:11:39 +00:00
// Test removing same item again - should return 404 (item is already removed).
$this->assertAPIResponse(
$request,
409,
array(
'code' => 'woocommerce_rest_cart_invalid_key',
)
);
ensure cart totals update when items are removed or quantity changed (https://github.com/woocommerce/woocommerce-blocks/pull/1840) * ensure cart totals update when items are removed (prototype): - return complete cart object from DELETE cart/items/:key - use receiveCart on client to update whole cart including total * move API endpoint for removing cart items to cart controller: - returns full cart schema so should be part of cart controller - is now a POST request with param - not strict REST - fix up client action to use new API * move API test for removing cart items (API has moved) * use correct path for remove API in tests (doh!) * add extra API test for remove_cart_item with bad key => 404 (experiment) * experiment: delete test_remove_cart_item, does test_remove_bad_cart_item work? * reinstate test_remove_cart_item with single valid request * remove unnecessary newline * tidy comments in PHP api tests, rerun travis? * remove test_remove_cart_item which may be causing problems * reinstate troublesome remove cart item api test (experiment): - see if this works now travis issue is resolved * whitespace * show correct total when changing cart item quantity: - move update cart item quantity API to cart controller - & return full cart response - update js action to new API route & receive full cart response * simplify test_remove_cart_item API test - now just tests a valid remove succeeds * remove test_update_item (API has moved) + + experimentally remove test_remove_bad_cart_item - testing if cart remove tests interact with each other * fix tests (🤞) - pass params in body, not as query params * reinstate test for re-deleting same item * update API docs - update/delete cart item have moved to /cart * add response data checks to new cart API tests: - extra protection against expected 404 "false positives" * reinstate API test for changing cart item quantity * fix remove cart item body tests - MIA items return error code, not cart * fix test - quantity param is int not string * attempt fix 404ing test_update_item: - only allow POST, remove trailing `/` - align array equals for good measure :) * fix action for update-item - method=POST, now takes body params * fix response body asserts in test_update_item * reinstate update_item and delete_item on CartItems controller * typos + examples in new cart items API docs * reorder previous cart item docs to minimise diff/churn * reinstate tabs in docs response example
2020-03-05 19:11:39 +00:00
}
/**
* Test changing the quantity of a cart item.
*/
public function test_update_item() {
$request = new \WP_REST_Request( 'POST', '/wc/store/v1/cart/update-item' );
$request->set_header( 'X-WC-Store-API-Nonce', wp_create_nonce( 'wc_store_api' ) );
ensure cart totals update when items are removed or quantity changed (https://github.com/woocommerce/woocommerce-blocks/pull/1840) * ensure cart totals update when items are removed (prototype): - return complete cart object from DELETE cart/items/:key - use receiveCart on client to update whole cart including total * move API endpoint for removing cart items to cart controller: - returns full cart schema so should be part of cart controller - is now a POST request with param - not strict REST - fix up client action to use new API * move API test for removing cart items (API has moved) * use correct path for remove API in tests (doh!) * add extra API test for remove_cart_item with bad key => 404 (experiment) * experiment: delete test_remove_cart_item, does test_remove_bad_cart_item work? * reinstate test_remove_cart_item with single valid request * remove unnecessary newline * tidy comments in PHP api tests, rerun travis? * remove test_remove_cart_item which may be causing problems * reinstate troublesome remove cart item api test (experiment): - see if this works now travis issue is resolved * whitespace * show correct total when changing cart item quantity: - move update cart item quantity API to cart controller - & return full cart response - update js action to new API route & receive full cart response * simplify test_remove_cart_item API test - now just tests a valid remove succeeds * remove test_update_item (API has moved) + + experimentally remove test_remove_bad_cart_item - testing if cart remove tests interact with each other * fix tests (🤞) - pass params in body, not as query params * reinstate test for re-deleting same item * update API docs - update/delete cart item have moved to /cart * add response data checks to new cart API tests: - extra protection against expected 404 "false positives" * reinstate API test for changing cart item quantity * fix remove cart item body tests - MIA items return error code, not cart * fix test - quantity param is int not string * attempt fix 404ing test_update_item: - only allow POST, remove trailing `/` - align array equals for good measure :) * fix action for update-item - method=POST, now takes body params * fix response body asserts in test_update_item * reinstate update_item and delete_item on CartItems controller * typos + examples in new cart items API docs * reorder previous cart item docs to minimise diff/churn * reinstate tabs in docs response example
2020-03-05 19:11:39 +00:00
$request->set_body_params(
array(
'key' => $this->keys[0],
'quantity' => 10,
)
);
$this->assertAPIResponse(
$request,
200,
array(
'items_count' => 11,
'totals' => array(
'total_items' => '11000',
),
'items' => array(
0 => array(
'quantity' => 10,
),
),
)
);
ensure cart totals update when items are removed or quantity changed (https://github.com/woocommerce/woocommerce-blocks/pull/1840) * ensure cart totals update when items are removed (prototype): - return complete cart object from DELETE cart/items/:key - use receiveCart on client to update whole cart including total * move API endpoint for removing cart items to cart controller: - returns full cart schema so should be part of cart controller - is now a POST request with param - not strict REST - fix up client action to use new API * move API test for removing cart items (API has moved) * use correct path for remove API in tests (doh!) * add extra API test for remove_cart_item with bad key => 404 (experiment) * experiment: delete test_remove_cart_item, does test_remove_bad_cart_item work? * reinstate test_remove_cart_item with single valid request * remove unnecessary newline * tidy comments in PHP api tests, rerun travis? * remove test_remove_cart_item which may be causing problems * reinstate troublesome remove cart item api test (experiment): - see if this works now travis issue is resolved * whitespace * show correct total when changing cart item quantity: - move update cart item quantity API to cart controller - & return full cart response - update js action to new API route & receive full cart response * simplify test_remove_cart_item API test - now just tests a valid remove succeeds * remove test_update_item (API has moved) + + experimentally remove test_remove_bad_cart_item - testing if cart remove tests interact with each other * fix tests (🤞) - pass params in body, not as query params * reinstate test for re-deleting same item * update API docs - update/delete cart item have moved to /cart * add response data checks to new cart API tests: - extra protection against expected 404 "false positives" * reinstate API test for changing cart item quantity * fix remove cart item body tests - MIA items return error code, not cart * fix test - quantity param is int not string * attempt fix 404ing test_update_item: - only allow POST, remove trailing `/` - align array equals for good measure :) * fix action for update-item - method=POST, now takes body params * fix response body asserts in test_update_item * reinstate update_item and delete_item on CartItems controller * typos + examples in new cart items API docs * reorder previous cart item docs to minimise diff/churn * reinstate tabs in docs response example
2020-03-05 19:11:39 +00:00
}
/**
Update and select shipping rates dynamically (https://github.com/woocommerce/woocommerce-blocks/pull/1794) * add select shipping endpoint to router * add select shipping method * add selected rates to cart * better select rates * move schema function to seperate function * move validation to Cart Controller * fix wrong session key * Update shipping/cart endpoints (https://github.com/woocommerce/woocommerce-blocks/pull/1833) * Items should not have keys in API response * Include package ID in response (this is just a basic index) * /cart/select-shipping-rate/package_id * Add package_id to package array * Update responses and add shipping-rates to main cart endpoint * update-shipping endpoint * Add querying selected shipping rate to the store (https://github.com/woocommerce/woocommerce-blocks/pull/1829) * add selecting shipping to store * directly call useSelectShippingRate * refactor cart keys transformation to reducer * remove selecting first result and accept selecting * move update shipping to new endpoint * pass selected rates down * select shipping right directly and fix editor issues * fix some broken prop types * key -> package id * Update and fix cart/shipping-rate tests * fix case for when rates are set * Update useShippingRates test * add args to rest endpoint * move selecting shipping rate logic to hook * fix some naming issues * update propTypes * update action call * fully watch cart state * address review issues * fix prop type issues * fix issue with rates not loading in checkout * remove extra package for shipping * move ShippingCalculatorOptions to outside Co-authored-by: Mike Jolley <mike.jolley@me.com> Co-authored-by: Albert Juhé Lluveras <aljullu@gmail.com>
2020-03-05 19:54:05 +00:00
* Test getting updated shipping.
*/
Sync shipping address with billing address when shipping address fields are disabled (https://github.com/woocommerce/woocommerce-blocks/pull/3358) * Correct docblock description * Sync shipping address changes with billing data * Update inline documentation * Revert address sync because it fails when shipping is disabled explicitely * Avoid loading shipping address from customer is shipping is disabled * Rather than update order from the wc/store/checkout request, update the customer object This is turn is synced to order, but also allows the cart calcultions to use the posted data. This means that taxes will be updated based on address data even if not displayed on the checkout. * Add action that combines billing and shipping updates * Add route for updating billing and shipping address * Sync billing data to server on change * Shared constants for billing data * Skip address update if missing country * Allow null values to skip formatting * Add billing to cart schema * Removed unwanted hooks from previous commit * Decoding is handled in useStoreCart * Remove hook * Make shipping context hold state * Make billing context hold state * Add address processors * Cart does not have billing * Update tests, remove some unrelated changes affecting the diff * Revert "Update inline documentation" This reverts commit 0393f49316de3152c6dcf6fda1192c06a74f1b55. * Make shippingRatesAreResolving conditonal based on API request * Shared address processor in cart and checkout * Rename REST endpoint * CustomerDataProvider and hook * Update shipping address type defs * Rename customer address endpoint, and remove update-shipping * Update tests * Fix tests by restoring country validation * typo * Update assets/js/base/hooks/cart/use-store-cart.js Co-authored-by: Darren Ethier <darren@roughsmootheng.in> * Simplify debounce and request handling * Remove state from address sync This will mean billing is "forgotten" if using the checbox, but this greatly simplifies logic. * Rename shipping rates loading to customer data loading * Sync based on useStoreCart data * Made cart API less strict on addresses * Fix useCheckoutAddress sync * Add note on currentShippingAsBilling * Use incoming isCart * Add more detailed inline comment for shippingAsBilling toggle event * Combine customer billing and shipping ref * Update address docblock * Error handling in pluckAddress * Fix cart response after rebase * Update customer tests * Update src/StoreApi/Routes/CartUpdateCustomer.php Co-authored-by: Darren Ethier <darren@roughsmootheng.in> Co-authored-by: Darren Ethier <darren@roughsmootheng.in>
2020-11-20 15:13:35 +00:00
public function test_update_customer() {
$request = new \WP_REST_Request( 'POST', '/wc/store/v1/cart/update-customer' );
$request->set_header( 'X-WC-Store-API-Nonce', wp_create_nonce( 'wc_store_api' ) );
Update and select shipping rates dynamically (https://github.com/woocommerce/woocommerce-blocks/pull/1794) * add select shipping endpoint to router * add select shipping method * add selected rates to cart * better select rates * move schema function to seperate function * move validation to Cart Controller * fix wrong session key * Update shipping/cart endpoints (https://github.com/woocommerce/woocommerce-blocks/pull/1833) * Items should not have keys in API response * Include package ID in response (this is just a basic index) * /cart/select-shipping-rate/package_id * Add package_id to package array * Update responses and add shipping-rates to main cart endpoint * update-shipping endpoint * Add querying selected shipping rate to the store (https://github.com/woocommerce/woocommerce-blocks/pull/1829) * add selecting shipping to store * directly call useSelectShippingRate * refactor cart keys transformation to reducer * remove selecting first result and accept selecting * move update shipping to new endpoint * pass selected rates down * select shipping right directly and fix editor issues * fix some broken prop types * key -> package id * Update and fix cart/shipping-rate tests * fix case for when rates are set * Update useShippingRates test * add args to rest endpoint * move selecting shipping rate logic to hook * fix some naming issues * update propTypes * update action call * fully watch cart state * address review issues * fix prop type issues * fix issue with rates not loading in checkout * remove extra package for shipping * move ShippingCalculatorOptions to outside Co-authored-by: Mike Jolley <mike.jolley@me.com> Co-authored-by: Albert Juhé Lluveras <aljullu@gmail.com>
2020-03-05 19:54:05 +00:00
$request->set_body_params(
array(
Sync shipping address with billing address when shipping address fields are disabled (https://github.com/woocommerce/woocommerce-blocks/pull/3358) * Correct docblock description * Sync shipping address changes with billing data * Update inline documentation * Revert address sync because it fails when shipping is disabled explicitely * Avoid loading shipping address from customer is shipping is disabled * Rather than update order from the wc/store/checkout request, update the customer object This is turn is synced to order, but also allows the cart calcultions to use the posted data. This means that taxes will be updated based on address data even if not displayed on the checkout. * Add action that combines billing and shipping updates * Add route for updating billing and shipping address * Sync billing data to server on change * Shared constants for billing data * Skip address update if missing country * Allow null values to skip formatting * Add billing to cart schema * Removed unwanted hooks from previous commit * Decoding is handled in useStoreCart * Remove hook * Make shipping context hold state * Make billing context hold state * Add address processors * Cart does not have billing * Update tests, remove some unrelated changes affecting the diff * Revert "Update inline documentation" This reverts commit 0393f49316de3152c6dcf6fda1192c06a74f1b55. * Make shippingRatesAreResolving conditonal based on API request * Shared address processor in cart and checkout * Rename REST endpoint * CustomerDataProvider and hook * Update shipping address type defs * Rename customer address endpoint, and remove update-shipping * Update tests * Fix tests by restoring country validation * typo * Update assets/js/base/hooks/cart/use-store-cart.js Co-authored-by: Darren Ethier <darren@roughsmootheng.in> * Simplify debounce and request handling * Remove state from address sync This will mean billing is "forgotten" if using the checbox, but this greatly simplifies logic. * Rename shipping rates loading to customer data loading * Sync based on useStoreCart data * Made cart API less strict on addresses * Fix useCheckoutAddress sync * Add note on currentShippingAsBilling * Use incoming isCart * Add more detailed inline comment for shippingAsBilling toggle event * Combine customer billing and shipping ref * Update address docblock * Error handling in pluckAddress * Fix cart response after rebase * Update customer tests * Update src/StoreApi/Routes/CartUpdateCustomer.php Co-authored-by: Darren Ethier <darren@roughsmootheng.in> Co-authored-by: Darren Ethier <darren@roughsmootheng.in>
2020-11-20 15:13:35 +00:00
'shipping_address' => (object) array(
'country' => 'US',
),
Update and select shipping rates dynamically (https://github.com/woocommerce/woocommerce-blocks/pull/1794) * add select shipping endpoint to router * add select shipping method * add selected rates to cart * better select rates * move schema function to seperate function * move validation to Cart Controller * fix wrong session key * Update shipping/cart endpoints (https://github.com/woocommerce/woocommerce-blocks/pull/1833) * Items should not have keys in API response * Include package ID in response (this is just a basic index) * /cart/select-shipping-rate/package_id * Add package_id to package array * Update responses and add shipping-rates to main cart endpoint * update-shipping endpoint * Add querying selected shipping rate to the store (https://github.com/woocommerce/woocommerce-blocks/pull/1829) * add selecting shipping to store * directly call useSelectShippingRate * refactor cart keys transformation to reducer * remove selecting first result and accept selecting * move update shipping to new endpoint * pass selected rates down * select shipping right directly and fix editor issues * fix some broken prop types * key -> package id * Update and fix cart/shipping-rate tests * fix case for when rates are set * Update useShippingRates test * add args to rest endpoint * move selecting shipping rate logic to hook * fix some naming issues * update propTypes * update action call * fully watch cart state * address review issues * fix prop type issues * fix issue with rates not loading in checkout * remove extra package for shipping * move ShippingCalculatorOptions to outside Co-authored-by: Mike Jolley <mike.jolley@me.com> Co-authored-by: Albert Juhé Lluveras <aljullu@gmail.com>
2020-03-05 19:54:05 +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' ) );
$this->assertAPIResponse(
$request,
200,
array(
'shipping_rates' => array(
0 => array(
'destination' => array(
'address_1' => '',
'address_2' => '',
'city' => '',
'state' => '',
'postcode' => '',
'country' => 'US',
),
),
),
)
);
Update and select shipping rates dynamically (https://github.com/woocommerce/woocommerce-blocks/pull/1794) * add select shipping endpoint to router * add select shipping method * add selected rates to cart * better select rates * move schema function to seperate function * move validation to Cart Controller * fix wrong session key * Update shipping/cart endpoints (https://github.com/woocommerce/woocommerce-blocks/pull/1833) * Items should not have keys in API response * Include package ID in response (this is just a basic index) * /cart/select-shipping-rate/package_id * Add package_id to package array * Update responses and add shipping-rates to main cart endpoint * update-shipping endpoint * Add querying selected shipping rate to the store (https://github.com/woocommerce/woocommerce-blocks/pull/1829) * add selecting shipping to store * directly call useSelectShippingRate * refactor cart keys transformation to reducer * remove selecting first result and accept selecting * move update shipping to new endpoint * pass selected rates down * select shipping right directly and fix editor issues * fix some broken prop types * key -> package id * Update and fix cart/shipping-rate tests * fix case for when rates are set * Update useShippingRates test * add args to rest endpoint * move selecting shipping rate logic to hook * fix some naming issues * update propTypes * update action call * fully watch cart state * address review issues * fix prop type issues * fix issue with rates not loading in checkout * remove extra package for shipping * move ShippingCalculatorOptions to outside Co-authored-by: Mike Jolley <mike.jolley@me.com> Co-authored-by: Albert Juhé Lluveras <aljullu@gmail.com>
2020-03-05 19:54:05 +00:00
remove_action( 'woocommerce_blocks_cart_update_customer_from_request', array( $action_callback, 'do_customer_callback' ) );
Update and select shipping rates dynamically (https://github.com/woocommerce/woocommerce-blocks/pull/1794) * add select shipping endpoint to router * add select shipping method * add selected rates to cart * better select rates * move schema function to seperate function * move validation to Cart Controller * fix wrong session key * Update shipping/cart endpoints (https://github.com/woocommerce/woocommerce-blocks/pull/1833) * Items should not have keys in API response * Include package ID in response (this is just a basic index) * /cart/select-shipping-rate/package_id * Add package_id to package array * Update responses and add shipping-rates to main cart endpoint * update-shipping endpoint * Add querying selected shipping rate to the store (https://github.com/woocommerce/woocommerce-blocks/pull/1829) * add selecting shipping to store * directly call useSelectShippingRate * refactor cart keys transformation to reducer * remove selecting first result and accept selecting * move update shipping to new endpoint * pass selected rates down * select shipping right directly and fix editor issues * fix some broken prop types * key -> package id * Update and fix cart/shipping-rate tests * fix case for when rates are set * Update useShippingRates test * add args to rest endpoint * move selecting shipping rate logic to hook * fix some naming issues * update propTypes * update action call * fully watch cart state * address review issues * fix prop type issues * fix issue with rates not loading in checkout * remove extra package for shipping * move ShippingCalculatorOptions to outside Co-authored-by: Mike Jolley <mike.jolley@me.com> Co-authored-by: Albert Juhé Lluveras <aljullu@gmail.com>
2020-03-05 19:54:05 +00:00
}
/**
* Test shipping address validation.
*/
Sync shipping address with billing address when shipping address fields are disabled (https://github.com/woocommerce/woocommerce-blocks/pull/3358) * Correct docblock description * Sync shipping address changes with billing data * Update inline documentation * Revert address sync because it fails when shipping is disabled explicitely * Avoid loading shipping address from customer is shipping is disabled * Rather than update order from the wc/store/checkout request, update the customer object This is turn is synced to order, but also allows the cart calcultions to use the posted data. This means that taxes will be updated based on address data even if not displayed on the checkout. * Add action that combines billing and shipping updates * Add route for updating billing and shipping address * Sync billing data to server on change * Shared constants for billing data * Skip address update if missing country * Allow null values to skip formatting * Add billing to cart schema * Removed unwanted hooks from previous commit * Decoding is handled in useStoreCart * Remove hook * Make shipping context hold state * Make billing context hold state * Add address processors * Cart does not have billing * Update tests, remove some unrelated changes affecting the diff * Revert "Update inline documentation" This reverts commit 0393f49316de3152c6dcf6fda1192c06a74f1b55. * Make shippingRatesAreResolving conditonal based on API request * Shared address processor in cart and checkout * Rename REST endpoint * CustomerDataProvider and hook * Update shipping address type defs * Rename customer address endpoint, and remove update-shipping * Update tests * Fix tests by restoring country validation * typo * Update assets/js/base/hooks/cart/use-store-cart.js Co-authored-by: Darren Ethier <darren@roughsmootheng.in> * Simplify debounce and request handling * Remove state from address sync This will mean billing is "forgotten" if using the checbox, but this greatly simplifies logic. * Rename shipping rates loading to customer data loading * Sync based on useStoreCart data * Made cart API less strict on addresses * Fix useCheckoutAddress sync * Add note on currentShippingAsBilling * Use incoming isCart * Add more detailed inline comment for shippingAsBilling toggle event * Combine customer billing and shipping ref * Update address docblock * Error handling in pluckAddress * Fix cart response after rebase * Update customer tests * Update src/StoreApi/Routes/CartUpdateCustomer.php Co-authored-by: Darren Ethier <darren@roughsmootheng.in> Co-authored-by: Darren Ethier <darren@roughsmootheng.in>
2020-11-20 15:13:35 +00:00
public function test_update_customer_address() {
$request = new \WP_REST_Request( 'POST', '/wc/store/v1/cart/update-customer' );
$request->set_header( 'X-WC-Store-API-Nonce', wp_create_nonce( 'wc_store_api' ) );
Update and select shipping rates dynamically (https://github.com/woocommerce/woocommerce-blocks/pull/1794) * add select shipping endpoint to router * add select shipping method * add selected rates to cart * better select rates * move schema function to seperate function * move validation to Cart Controller * fix wrong session key * Update shipping/cart endpoints (https://github.com/woocommerce/woocommerce-blocks/pull/1833) * Items should not have keys in API response * Include package ID in response (this is just a basic index) * /cart/select-shipping-rate/package_id * Add package_id to package array * Update responses and add shipping-rates to main cart endpoint * update-shipping endpoint * Add querying selected shipping rate to the store (https://github.com/woocommerce/woocommerce-blocks/pull/1829) * add selecting shipping to store * directly call useSelectShippingRate * refactor cart keys transformation to reducer * remove selecting first result and accept selecting * move update shipping to new endpoint * pass selected rates down * select shipping right directly and fix editor issues * fix some broken prop types * key -> package id * Update and fix cart/shipping-rate tests * fix case for when rates are set * Update useShippingRates test * add args to rest endpoint * move selecting shipping rate logic to hook * fix some naming issues * update propTypes * update action call * fully watch cart state * address review issues * fix prop type issues * fix issue with rates not loading in checkout * remove extra package for shipping * move ShippingCalculatorOptions to outside Co-authored-by: Mike Jolley <mike.jolley@me.com> Co-authored-by: Albert Juhé Lluveras <aljullu@gmail.com>
2020-03-05 19:54:05 +00:00
$request->set_body_params(
array(
Sync shipping address with billing address when shipping address fields are disabled (https://github.com/woocommerce/woocommerce-blocks/pull/3358) * Correct docblock description * Sync shipping address changes with billing data * Update inline documentation * Revert address sync because it fails when shipping is disabled explicitely * Avoid loading shipping address from customer is shipping is disabled * Rather than update order from the wc/store/checkout request, update the customer object This is turn is synced to order, but also allows the cart calcultions to use the posted data. This means that taxes will be updated based on address data even if not displayed on the checkout. * Add action that combines billing and shipping updates * Add route for updating billing and shipping address * Sync billing data to server on change * Shared constants for billing data * Skip address update if missing country * Allow null values to skip formatting * Add billing to cart schema * Removed unwanted hooks from previous commit * Decoding is handled in useStoreCart * Remove hook * Make shipping context hold state * Make billing context hold state * Add address processors * Cart does not have billing * Update tests, remove some unrelated changes affecting the diff * Revert "Update inline documentation" This reverts commit 0393f49316de3152c6dcf6fda1192c06a74f1b55. * Make shippingRatesAreResolving conditonal based on API request * Shared address processor in cart and checkout * Rename REST endpoint * CustomerDataProvider and hook * Update shipping address type defs * Rename customer address endpoint, and remove update-shipping * Update tests * Fix tests by restoring country validation * typo * Update assets/js/base/hooks/cart/use-store-cart.js Co-authored-by: Darren Ethier <darren@roughsmootheng.in> * Simplify debounce and request handling * Remove state from address sync This will mean billing is "forgotten" if using the checbox, but this greatly simplifies logic. * Rename shipping rates loading to customer data loading * Sync based on useStoreCart data * Made cart API less strict on addresses * Fix useCheckoutAddress sync * Add note on currentShippingAsBilling * Use incoming isCart * Add more detailed inline comment for shippingAsBilling toggle event * Combine customer billing and shipping ref * Update address docblock * Error handling in pluckAddress * Fix cart response after rebase * Update customer tests * Update src/StoreApi/Routes/CartUpdateCustomer.php Co-authored-by: Darren Ethier <darren@roughsmootheng.in> Co-authored-by: Darren Ethier <darren@roughsmootheng.in>
2020-11-20 15:13:35 +00:00
'shipping_address' => (object) array(
'first_name' => 'Han',
'last_name' => 'Solo',
'address_1' => 'Test address 1',
'address_2' => 'Test address 2',
'city' => 'Test City',
'state' => 'AL',
'postcode' => '90210',
'country' => 'US',
),
Update and select shipping rates dynamically (https://github.com/woocommerce/woocommerce-blocks/pull/1794) * add select shipping endpoint to router * add select shipping method * add selected rates to cart * better select rates * move schema function to seperate function * move validation to Cart Controller * fix wrong session key * Update shipping/cart endpoints (https://github.com/woocommerce/woocommerce-blocks/pull/1833) * Items should not have keys in API response * Include package ID in response (this is just a basic index) * /cart/select-shipping-rate/package_id * Add package_id to package array * Update responses and add shipping-rates to main cart endpoint * update-shipping endpoint * Add querying selected shipping rate to the store (https://github.com/woocommerce/woocommerce-blocks/pull/1829) * add selecting shipping to store * directly call useSelectShippingRate * refactor cart keys transformation to reducer * remove selecting first result and accept selecting * move update shipping to new endpoint * pass selected rates down * select shipping right directly and fix editor issues * fix some broken prop types * key -> package id * Update and fix cart/shipping-rate tests * fix case for when rates are set * Update useShippingRates test * add args to rest endpoint * move selecting shipping rate logic to hook * fix some naming issues * update propTypes * update action call * fully watch cart state * address review issues * fix prop type issues * fix issue with rates not loading in checkout * remove extra package for shipping * move ShippingCalculatorOptions to outside Co-authored-by: Mike Jolley <mike.jolley@me.com> Co-authored-by: Albert Juhé Lluveras <aljullu@gmail.com>
2020-03-05 19:54:05 +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',
),
),
),
)
);
Update and select shipping rates dynamically (https://github.com/woocommerce/woocommerce-blocks/pull/1794) * add select shipping endpoint to router * add select shipping method * add selected rates to cart * better select rates * move schema function to seperate function * move validation to Cart Controller * fix wrong session key * Update shipping/cart endpoints (https://github.com/woocommerce/woocommerce-blocks/pull/1833) * Items should not have keys in API response * Include package ID in response (this is just a basic index) * /cart/select-shipping-rate/package_id * Add package_id to package array * Update responses and add shipping-rates to main cart endpoint * update-shipping endpoint * Add querying selected shipping rate to the store (https://github.com/woocommerce/woocommerce-blocks/pull/1829) * add selecting shipping to store * directly call useSelectShippingRate * refactor cart keys transformation to reducer * remove selecting first result and accept selecting * move update shipping to new endpoint * pass selected rates down * select shipping right directly and fix editor issues * fix some broken prop types * key -> package id * Update and fix cart/shipping-rate tests * fix case for when rates are set * Update useShippingRates test * add args to rest endpoint * move selecting shipping rate logic to hook * fix some naming issues * update propTypes * update action call * fully watch cart state * address review issues * fix prop type issues * fix issue with rates not loading in checkout * remove extra package for shipping * move ShippingCalculatorOptions to outside Co-authored-by: Mike Jolley <mike.jolley@me.com> Co-authored-by: Albert Juhé Lluveras <aljullu@gmail.com>
2020-03-05 19:54:05 +00:00
// Address with invalid country.
$request = new \WP_REST_Request( 'POST', '/wc/store/v1/cart/update-customer' );
$request->set_header( 'X-WC-Store-API-Nonce', wp_create_nonce( 'wc_store_api' ) );
Update and select shipping rates dynamically (https://github.com/woocommerce/woocommerce-blocks/pull/1794) * add select shipping endpoint to router * add select shipping method * add selected rates to cart * better select rates * move schema function to seperate function * move validation to Cart Controller * fix wrong session key * Update shipping/cart endpoints (https://github.com/woocommerce/woocommerce-blocks/pull/1833) * Items should not have keys in API response * Include package ID in response (this is just a basic index) * /cart/select-shipping-rate/package_id * Add package_id to package array * Update responses and add shipping-rates to main cart endpoint * update-shipping endpoint * Add querying selected shipping rate to the store (https://github.com/woocommerce/woocommerce-blocks/pull/1829) * add selecting shipping to store * directly call useSelectShippingRate * refactor cart keys transformation to reducer * remove selecting first result and accept selecting * move update shipping to new endpoint * pass selected rates down * select shipping right directly and fix editor issues * fix some broken prop types * key -> package id * Update and fix cart/shipping-rate tests * fix case for when rates are set * Update useShippingRates test * add args to rest endpoint * move selecting shipping rate logic to hook * fix some naming issues * update propTypes * update action call * fully watch cart state * address review issues * fix prop type issues * fix issue with rates not loading in checkout * remove extra package for shipping * move ShippingCalculatorOptions to outside Co-authored-by: Mike Jolley <mike.jolley@me.com> Co-authored-by: Albert Juhé Lluveras <aljullu@gmail.com>
2020-03-05 19:54:05 +00:00
$request->set_body_params(
array(
Sync shipping address with billing address when shipping address fields are disabled (https://github.com/woocommerce/woocommerce-blocks/pull/3358) * Correct docblock description * Sync shipping address changes with billing data * Update inline documentation * Revert address sync because it fails when shipping is disabled explicitely * Avoid loading shipping address from customer is shipping is disabled * Rather than update order from the wc/store/checkout request, update the customer object This is turn is synced to order, but also allows the cart calcultions to use the posted data. This means that taxes will be updated based on address data even if not displayed on the checkout. * Add action that combines billing and shipping updates * Add route for updating billing and shipping address * Sync billing data to server on change * Shared constants for billing data * Skip address update if missing country * Allow null values to skip formatting * Add billing to cart schema * Removed unwanted hooks from previous commit * Decoding is handled in useStoreCart * Remove hook * Make shipping context hold state * Make billing context hold state * Add address processors * Cart does not have billing * Update tests, remove some unrelated changes affecting the diff * Revert "Update inline documentation" This reverts commit 0393f49316de3152c6dcf6fda1192c06a74f1b55. * Make shippingRatesAreResolving conditonal based on API request * Shared address processor in cart and checkout * Rename REST endpoint * CustomerDataProvider and hook * Update shipping address type defs * Rename customer address endpoint, and remove update-shipping * Update tests * Fix tests by restoring country validation * typo * Update assets/js/base/hooks/cart/use-store-cart.js Co-authored-by: Darren Ethier <darren@roughsmootheng.in> * Simplify debounce and request handling * Remove state from address sync This will mean billing is "forgotten" if using the checbox, but this greatly simplifies logic. * Rename shipping rates loading to customer data loading * Sync based on useStoreCart data * Made cart API less strict on addresses * Fix useCheckoutAddress sync * Add note on currentShippingAsBilling * Use incoming isCart * Add more detailed inline comment for shippingAsBilling toggle event * Combine customer billing and shipping ref * Update address docblock * Error handling in pluckAddress * Fix cart response after rebase * Update customer tests * Update src/StoreApi/Routes/CartUpdateCustomer.php Co-authored-by: Darren Ethier <darren@roughsmootheng.in> Co-authored-by: Darren Ethier <darren@roughsmootheng.in>
2020-11-20 15:13:35 +00:00
'shipping_address' => (object) array(
'first_name' => 'Han',
'last_name' => 'Solo',
'address_1' => 'Test address 1',
'address_2' => 'Test address 2',
'city' => 'Test City',
'state' => 'AL',
'postcode' => '90210',
'country' => 'ZZZZZZZZ',
),
Update and select shipping rates dynamically (https://github.com/woocommerce/woocommerce-blocks/pull/1794) * add select shipping endpoint to router * add select shipping method * add selected rates to cart * better select rates * move schema function to seperate function * move validation to Cart Controller * fix wrong session key * Update shipping/cart endpoints (https://github.com/woocommerce/woocommerce-blocks/pull/1833) * Items should not have keys in API response * Include package ID in response (this is just a basic index) * /cart/select-shipping-rate/package_id * Add package_id to package array * Update responses and add shipping-rates to main cart endpoint * update-shipping endpoint * Add querying selected shipping rate to the store (https://github.com/woocommerce/woocommerce-blocks/pull/1829) * add selecting shipping to store * directly call useSelectShippingRate * refactor cart keys transformation to reducer * remove selecting first result and accept selecting * move update shipping to new endpoint * pass selected rates down * select shipping right directly and fix editor issues * fix some broken prop types * key -> package id * Update and fix cart/shipping-rate tests * fix case for when rates are set * Update useShippingRates test * add args to rest endpoint * move selecting shipping rate logic to hook * fix some naming issues * update propTypes * update action call * fully watch cart state * address review issues * fix prop type issues * fix issue with rates not loading in checkout * remove extra package for shipping * move ShippingCalculatorOptions to outside Co-authored-by: Mike Jolley <mike.jolley@me.com> Co-authored-by: Albert Juhé Lluveras <aljullu@gmail.com>
2020-03-05 19:54:05 +00:00
)
);
$this->assertAPIResponse(
$request,
400
);
Update and select shipping rates dynamically (https://github.com/woocommerce/woocommerce-blocks/pull/1794) * add select shipping endpoint to router * add select shipping method * add selected rates to cart * better select rates * move schema function to seperate function * move validation to Cart Controller * fix wrong session key * Update shipping/cart endpoints (https://github.com/woocommerce/woocommerce-blocks/pull/1833) * Items should not have keys in API response * Include package ID in response (this is just a basic index) * /cart/select-shipping-rate/package_id * Add package_id to package array * Update responses and add shipping-rates to main cart endpoint * update-shipping endpoint * Add querying selected shipping rate to the store (https://github.com/woocommerce/woocommerce-blocks/pull/1829) * add selecting shipping to store * directly call useSelectShippingRate * refactor cart keys transformation to reducer * remove selecting first result and accept selecting * move update shipping to new endpoint * pass selected rates down * select shipping right directly and fix editor issues * fix some broken prop types * key -> package id * Update and fix cart/shipping-rate tests * fix case for when rates are set * Update useShippingRates test * add args to rest endpoint * move selecting shipping rate logic to hook * fix some naming issues * update propTypes * update action call * fully watch cart state * address review issues * fix prop type issues * fix issue with rates not loading in checkout * remove extra package for shipping * move ShippingCalculatorOptions to outside Co-authored-by: Mike Jolley <mike.jolley@me.com> Co-authored-by: Albert Juhé Lluveras <aljullu@gmail.com>
2020-03-05 19:54:05 +00:00
// US address with named state.
$request = new \WP_REST_Request( 'POST', '/wc/store/v1/cart/update-customer' );
$request->set_header( 'X-WC-Store-API-Nonce', wp_create_nonce( 'wc_store_api' ) );
Update and select shipping rates dynamically (https://github.com/woocommerce/woocommerce-blocks/pull/1794) * add select shipping endpoint to router * add select shipping method * add selected rates to cart * better select rates * move schema function to seperate function * move validation to Cart Controller * fix wrong session key * Update shipping/cart endpoints (https://github.com/woocommerce/woocommerce-blocks/pull/1833) * Items should not have keys in API response * Include package ID in response (this is just a basic index) * /cart/select-shipping-rate/package_id * Add package_id to package array * Update responses and add shipping-rates to main cart endpoint * update-shipping endpoint * Add querying selected shipping rate to the store (https://github.com/woocommerce/woocommerce-blocks/pull/1829) * add selecting shipping to store * directly call useSelectShippingRate * refactor cart keys transformation to reducer * remove selecting first result and accept selecting * move update shipping to new endpoint * pass selected rates down * select shipping right directly and fix editor issues * fix some broken prop types * key -> package id * Update and fix cart/shipping-rate tests * fix case for when rates are set * Update useShippingRates test * add args to rest endpoint * move selecting shipping rate logic to hook * fix some naming issues * update propTypes * update action call * fully watch cart state * address review issues * fix prop type issues * fix issue with rates not loading in checkout * remove extra package for shipping * move ShippingCalculatorOptions to outside Co-authored-by: Mike Jolley <mike.jolley@me.com> Co-authored-by: Albert Juhé Lluveras <aljullu@gmail.com>
2020-03-05 19:54:05 +00:00
$request->set_body_params(
array(
Sync shipping address with billing address when shipping address fields are disabled (https://github.com/woocommerce/woocommerce-blocks/pull/3358) * Correct docblock description * Sync shipping address changes with billing data * Update inline documentation * Revert address sync because it fails when shipping is disabled explicitely * Avoid loading shipping address from customer is shipping is disabled * Rather than update order from the wc/store/checkout request, update the customer object This is turn is synced to order, but also allows the cart calcultions to use the posted data. This means that taxes will be updated based on address data even if not displayed on the checkout. * Add action that combines billing and shipping updates * Add route for updating billing and shipping address * Sync billing data to server on change * Shared constants for billing data * Skip address update if missing country * Allow null values to skip formatting * Add billing to cart schema * Removed unwanted hooks from previous commit * Decoding is handled in useStoreCart * Remove hook * Make shipping context hold state * Make billing context hold state * Add address processors * Cart does not have billing * Update tests, remove some unrelated changes affecting the diff * Revert "Update inline documentation" This reverts commit 0393f49316de3152c6dcf6fda1192c06a74f1b55. * Make shippingRatesAreResolving conditonal based on API request * Shared address processor in cart and checkout * Rename REST endpoint * CustomerDataProvider and hook * Update shipping address type defs * Rename customer address endpoint, and remove update-shipping * Update tests * Fix tests by restoring country validation * typo * Update assets/js/base/hooks/cart/use-store-cart.js Co-authored-by: Darren Ethier <darren@roughsmootheng.in> * Simplify debounce and request handling * Remove state from address sync This will mean billing is "forgotten" if using the checbox, but this greatly simplifies logic. * Rename shipping rates loading to customer data loading * Sync based on useStoreCart data * Made cart API less strict on addresses * Fix useCheckoutAddress sync * Add note on currentShippingAsBilling * Use incoming isCart * Add more detailed inline comment for shippingAsBilling toggle event * Combine customer billing and shipping ref * Update address docblock * Error handling in pluckAddress * Fix cart response after rebase * Update customer tests * Update src/StoreApi/Routes/CartUpdateCustomer.php Co-authored-by: Darren Ethier <darren@roughsmootheng.in> Co-authored-by: Darren Ethier <darren@roughsmootheng.in>
2020-11-20 15:13:35 +00:00
'shipping_address' => (object) array(
'first_name' => 'Han',
'last_name' => 'Solo',
'address_1' => 'Test address 1',
'address_2' => 'Test address 2',
'city' => 'Test City',
'state' => 'Alabama',
'postcode' => '90210',
'country' => 'US',
),
Update and select shipping rates dynamically (https://github.com/woocommerce/woocommerce-blocks/pull/1794) * add select shipping endpoint to router * add select shipping method * add selected rates to cart * better select rates * move schema function to seperate function * move validation to Cart Controller * fix wrong session key * Update shipping/cart endpoints (https://github.com/woocommerce/woocommerce-blocks/pull/1833) * Items should not have keys in API response * Include package ID in response (this is just a basic index) * /cart/select-shipping-rate/package_id * Add package_id to package array * Update responses and add shipping-rates to main cart endpoint * update-shipping endpoint * Add querying selected shipping rate to the store (https://github.com/woocommerce/woocommerce-blocks/pull/1829) * add selecting shipping to store * directly call useSelectShippingRate * refactor cart keys transformation to reducer * remove selecting first result and accept selecting * move update shipping to new endpoint * pass selected rates down * select shipping right directly and fix editor issues * fix some broken prop types * key -> package id * Update and fix cart/shipping-rate tests * fix case for when rates are set * Update useShippingRates test * add args to rest endpoint * move selecting shipping rate logic to hook * fix some naming issues * update propTypes * update action call * fully watch cart state * address review issues * fix prop type issues * fix issue with rates not loading in checkout * remove extra package for shipping * move ShippingCalculatorOptions to outside Co-authored-by: Mike Jolley <mike.jolley@me.com> Co-authored-by: Albert Juhé Lluveras <aljullu@gmail.com>
2020-03-05 19:54:05 +00:00
)
);
$this->assertAPIResponse(
$request,
200,
array(
'shipping_rates' => array(
0 => array(
'destination' => array(
'state' => 'AL',
'country' => 'US',
),
),
),
)
);
Update and select shipping rates dynamically (https://github.com/woocommerce/woocommerce-blocks/pull/1794) * add select shipping endpoint to router * add select shipping method * add selected rates to cart * better select rates * move schema function to seperate function * move validation to Cart Controller * fix wrong session key * Update shipping/cart endpoints (https://github.com/woocommerce/woocommerce-blocks/pull/1833) * Items should not have keys in API response * Include package ID in response (this is just a basic index) * /cart/select-shipping-rate/package_id * Add package_id to package array * Update responses and add shipping-rates to main cart endpoint * update-shipping endpoint * Add querying selected shipping rate to the store (https://github.com/woocommerce/woocommerce-blocks/pull/1829) * add selecting shipping to store * directly call useSelectShippingRate * refactor cart keys transformation to reducer * remove selecting first result and accept selecting * move update shipping to new endpoint * pass selected rates down * select shipping right directly and fix editor issues * fix some broken prop types * key -> package id * Update and fix cart/shipping-rate tests * fix case for when rates are set * Update useShippingRates test * add args to rest endpoint * move selecting shipping rate logic to hook * fix some naming issues * update propTypes * update action call * fully watch cart state * address review issues * fix prop type issues * fix issue with rates not loading in checkout * remove extra package for shipping * move ShippingCalculatorOptions to outside Co-authored-by: Mike Jolley <mike.jolley@me.com> Co-authored-by: Albert Juhé Lluveras <aljullu@gmail.com>
2020-03-05 19:54:05 +00:00
// US address with invalid state.
$request = new \WP_REST_Request( 'POST', '/wc/store/v1/cart/update-customer' );
$request->set_header( 'X-WC-Store-API-Nonce', wp_create_nonce( 'wc_store_api' ) );
Update and select shipping rates dynamically (https://github.com/woocommerce/woocommerce-blocks/pull/1794) * add select shipping endpoint to router * add select shipping method * add selected rates to cart * better select rates * move schema function to seperate function * move validation to Cart Controller * fix wrong session key * Update shipping/cart endpoints (https://github.com/woocommerce/woocommerce-blocks/pull/1833) * Items should not have keys in API response * Include package ID in response (this is just a basic index) * /cart/select-shipping-rate/package_id * Add package_id to package array * Update responses and add shipping-rates to main cart endpoint * update-shipping endpoint * Add querying selected shipping rate to the store (https://github.com/woocommerce/woocommerce-blocks/pull/1829) * add selecting shipping to store * directly call useSelectShippingRate * refactor cart keys transformation to reducer * remove selecting first result and accept selecting * move update shipping to new endpoint * pass selected rates down * select shipping right directly and fix editor issues * fix some broken prop types * key -> package id * Update and fix cart/shipping-rate tests * fix case for when rates are set * Update useShippingRates test * add args to rest endpoint * move selecting shipping rate logic to hook * fix some naming issues * update propTypes * update action call * fully watch cart state * address review issues * fix prop type issues * fix issue with rates not loading in checkout * remove extra package for shipping * move ShippingCalculatorOptions to outside Co-authored-by: Mike Jolley <mike.jolley@me.com> Co-authored-by: Albert Juhé Lluveras <aljullu@gmail.com>
2020-03-05 19:54:05 +00:00
$request->set_body_params(
array(
Sync shipping address with billing address when shipping address fields are disabled (https://github.com/woocommerce/woocommerce-blocks/pull/3358) * Correct docblock description * Sync shipping address changes with billing data * Update inline documentation * Revert address sync because it fails when shipping is disabled explicitely * Avoid loading shipping address from customer is shipping is disabled * Rather than update order from the wc/store/checkout request, update the customer object This is turn is synced to order, but also allows the cart calcultions to use the posted data. This means that taxes will be updated based on address data even if not displayed on the checkout. * Add action that combines billing and shipping updates * Add route for updating billing and shipping address * Sync billing data to server on change * Shared constants for billing data * Skip address update if missing country * Allow null values to skip formatting * Add billing to cart schema * Removed unwanted hooks from previous commit * Decoding is handled in useStoreCart * Remove hook * Make shipping context hold state * Make billing context hold state * Add address processors * Cart does not have billing * Update tests, remove some unrelated changes affecting the diff * Revert "Update inline documentation" This reverts commit 0393f49316de3152c6dcf6fda1192c06a74f1b55. * Make shippingRatesAreResolving conditonal based on API request * Shared address processor in cart and checkout * Rename REST endpoint * CustomerDataProvider and hook * Update shipping address type defs * Rename customer address endpoint, and remove update-shipping * Update tests * Fix tests by restoring country validation * typo * Update assets/js/base/hooks/cart/use-store-cart.js Co-authored-by: Darren Ethier <darren@roughsmootheng.in> * Simplify debounce and request handling * Remove state from address sync This will mean billing is "forgotten" if using the checbox, but this greatly simplifies logic. * Rename shipping rates loading to customer data loading * Sync based on useStoreCart data * Made cart API less strict on addresses * Fix useCheckoutAddress sync * Add note on currentShippingAsBilling * Use incoming isCart * Add more detailed inline comment for shippingAsBilling toggle event * Combine customer billing and shipping ref * Update address docblock * Error handling in pluckAddress * Fix cart response after rebase * Update customer tests * Update src/StoreApi/Routes/CartUpdateCustomer.php Co-authored-by: Darren Ethier <darren@roughsmootheng.in> Co-authored-by: Darren Ethier <darren@roughsmootheng.in>
2020-11-20 15:13:35 +00:00
'shipping_address' => (object) array(
'first_name' => 'Han',
'last_name' => 'Solo',
'address_1' => 'Test address 1',
'address_2' => 'Test address 2',
'city' => 'Test City',
'state' => 'ZZZZZZZZ',
'postcode' => '90210',
'country' => 'US',
),
)
);
$this->assertAPIResponse(
$request,
400
);
// US address with invalid postcode.
$request = new \WP_REST_Request( 'POST', '/wc/store/v1/cart/update-customer' );
$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',
'last_name' => 'Solo',
'address_1' => 'Test address 1',
'address_2' => 'Test address 2',
'city' => 'Test City',
'state' => 'AL',
'postcode' => 'ABCDE',
'country' => 'US',
),
Update and select shipping rates dynamically (https://github.com/woocommerce/woocommerce-blocks/pull/1794) * add select shipping endpoint to router * add select shipping method * add selected rates to cart * better select rates * move schema function to seperate function * move validation to Cart Controller * fix wrong session key * Update shipping/cart endpoints (https://github.com/woocommerce/woocommerce-blocks/pull/1833) * Items should not have keys in API response * Include package ID in response (this is just a basic index) * /cart/select-shipping-rate/package_id * Add package_id to package array * Update responses and add shipping-rates to main cart endpoint * update-shipping endpoint * Add querying selected shipping rate to the store (https://github.com/woocommerce/woocommerce-blocks/pull/1829) * add selecting shipping to store * directly call useSelectShippingRate * refactor cart keys transformation to reducer * remove selecting first result and accept selecting * move update shipping to new endpoint * pass selected rates down * select shipping right directly and fix editor issues * fix some broken prop types * key -> package id * Update and fix cart/shipping-rate tests * fix case for when rates are set * Update useShippingRates test * add args to rest endpoint * move selecting shipping rate logic to hook * fix some naming issues * update propTypes * update action call * fully watch cart state * address review issues * fix prop type issues * fix issue with rates not loading in checkout * remove extra package for shipping * move ShippingCalculatorOptions to outside Co-authored-by: Mike Jolley <mike.jolley@me.com> Co-authored-by: Albert Juhé Lluveras <aljullu@gmail.com>
2020-03-05 19:54:05 +00:00
)
);
$this->assertAPIResponse(
$request,
400
);
Update and select shipping rates dynamically (https://github.com/woocommerce/woocommerce-blocks/pull/1794) * add select shipping endpoint to router * add select shipping method * add selected rates to cart * better select rates * move schema function to seperate function * move validation to Cart Controller * fix wrong session key * Update shipping/cart endpoints (https://github.com/woocommerce/woocommerce-blocks/pull/1833) * Items should not have keys in API response * Include package ID in response (this is just a basic index) * /cart/select-shipping-rate/package_id * Add package_id to package array * Update responses and add shipping-rates to main cart endpoint * update-shipping endpoint * Add querying selected shipping rate to the store (https://github.com/woocommerce/woocommerce-blocks/pull/1829) * add selecting shipping to store * directly call useSelectShippingRate * refactor cart keys transformation to reducer * remove selecting first result and accept selecting * move update shipping to new endpoint * pass selected rates down * select shipping right directly and fix editor issues * fix some broken prop types * key -> package id * Update and fix cart/shipping-rate tests * fix case for when rates are set * Update useShippingRates test * add args to rest endpoint * move selecting shipping rate logic to hook * fix some naming issues * update propTypes * update action call * fully watch cart state * address review issues * fix prop type issues * fix issue with rates not loading in checkout * remove extra package for shipping * move ShippingCalculatorOptions to outside Co-authored-by: Mike Jolley <mike.jolley@me.com> Co-authored-by: Albert Juhé Lluveras <aljullu@gmail.com>
2020-03-05 19:54:05 +00:00
}
/**
* Test applying coupon to cart.
*/
public function test_apply_coupon() {
wc()->cart->remove_coupon( $this->coupon->get_code() );
$request = new \WP_REST_Request( 'POST', '/wc/store/v1/cart/apply-coupon' );
$request->set_header( 'X-WC-Store-API-Nonce', wp_create_nonce( 'wc_store_api' ) );
$request->set_body_params(
array(
'code' => $this->coupon->get_code(),
)
);
$this->assertAPIResponse(
$request,
200,
array(
'totals' => array(
'total_discount' => '100',
),
)
);
$fixtures = new FixtureData();
// Test coupons with different case.
$newcoupon = $fixtures->get_coupon( array( 'code' => 'testCoupon' ) );
$request = new \WP_REST_Request( 'POST', '/wc/store/v1/cart/apply-coupon' );
$request->set_header( 'X-WC-Store-API-Nonce', wp_create_nonce( 'wc_store_api' ) );
$request->set_body_params(
array(
'code' => 'testCoupon',
)
);
$this->assertAPIResponse(
$request,
200
);
// Test coupons with special chars in the code.
$newcoupon = $fixtures->get_coupon( array( 'code' => '$5 off' ) );
$request = new \WP_REST_Request( 'POST', '/wc/store/v1/cart/apply-coupon' );
$request->set_header( 'X-WC-Store-API-Nonce', wp_create_nonce( 'wc_store_api' ) );
$request->set_body_params(
array(
'code' => '$5 off',
)
);
$this->assertAPIResponse(
$request,
200
);
}
/**
* Test removing coupon from cart.
*/
public function test_remove_coupon() {
// Invalid coupon.
$request = new \WP_REST_Request( 'POST', '/wc/store/v1/cart/remove-coupon' );
$request->set_header( 'X-WC-Store-API-Nonce', wp_create_nonce( 'wc_store_api' ) );
$request->set_body_params(
array(
'code' => 'doesnotexist',
)
);
$this->assertAPIResponse(
$request,
400
);
// Applied coupon.
$request = new \WP_REST_Request( 'POST', '/wc/store/v1/cart/remove-coupon' );
$request->set_header( 'X-WC-Store-API-Nonce', wp_create_nonce( 'wc_store_api' ) );
$request->set_body_params(
array(
'code' => $this->coupon->get_code(),
)
);
$this->assertAPIResponse(
$request,
200,
array(
'totals' => array(
'total_discount' => '0',
),
)
);
}
/**
* Test conversion of cart item to rest response.
*/
public function test_prepare_item() {
$routes = new \Automattic\WooCommerce\Blocks\StoreApi\RoutesController( new \Automattic\WooCommerce\Blocks\StoreApi\SchemaController( $this->mock_extend ) );
$controller = $routes->get( 'cart', 'v1' );
$cart = wc()->cart;
$response = $controller->prepare_item_for_response( $cart, new \WP_REST_Request() );
$data = $response->get_data();
$this->assertArrayHasKey( 'items_count', $data );
$this->assertArrayHasKey( 'items', $data );
Update and select shipping rates dynamically (https://github.com/woocommerce/woocommerce-blocks/pull/1794) * add select shipping endpoint to router * add select shipping method * add selected rates to cart * better select rates * move schema function to seperate function * move validation to Cart Controller * fix wrong session key * Update shipping/cart endpoints (https://github.com/woocommerce/woocommerce-blocks/pull/1833) * Items should not have keys in API response * Include package ID in response (this is just a basic index) * /cart/select-shipping-rate/package_id * Add package_id to package array * Update responses and add shipping-rates to main cart endpoint * update-shipping endpoint * Add querying selected shipping rate to the store (https://github.com/woocommerce/woocommerce-blocks/pull/1829) * add selecting shipping to store * directly call useSelectShippingRate * refactor cart keys transformation to reducer * remove selecting first result and accept selecting * move update shipping to new endpoint * pass selected rates down * select shipping right directly and fix editor issues * fix some broken prop types * key -> package id * Update and fix cart/shipping-rate tests * fix case for when rates are set * Update useShippingRates test * add args to rest endpoint * move selecting shipping rate logic to hook * fix some naming issues * update propTypes * update action call * fully watch cart state * address review issues * fix prop type issues * fix issue with rates not loading in checkout * remove extra package for shipping * move ShippingCalculatorOptions to outside Co-authored-by: Mike Jolley <mike.jolley@me.com> Co-authored-by: Albert Juhé Lluveras <aljullu@gmail.com>
2020-03-05 19:54:05 +00:00
$this->assertArrayHasKey( 'shipping_rates', $data );
$this->assertArrayHasKey( 'coupons', $data );
$this->assertArrayHasKey( 'needs_payment', $data );
$this->assertArrayHasKey( 'needs_shipping', $data );
$this->assertArrayHasKey( 'items_weight', $data );
$this->assertArrayHasKey( 'totals', $data );
}
/**
* Test schema matches responses.
*/
public function test_get_item_schema() {
$routes = new \Automattic\WooCommerce\Blocks\StoreApi\RoutesController( new \Automattic\WooCommerce\Blocks\StoreApi\SchemaController( $this->mock_extend ) );
$controller = $routes->get( 'cart', 'v1' );
$schema = $controller->get_item_schema();
$cart = wc()->cart;
$response = $controller->prepare_item_for_response( $cart, new \WP_REST_Request() );
$schema = $controller->get_item_schema();
$validate = new ValidateSchema( $schema );
$diff = $validate->get_diff_from_object( $response->get_data() );
$this->assertEmpty( $diff );
}
}