woocommerce/plugins/woocommerce-blocks/tests/php/StoreApi/Utilities/CartController.php

82 lines
2.9 KiB
PHP
Raw Normal View History

Catch errors during cart validation (https://github.com/woocommerce/woocommerce-blocks/pull/3703) * Add new exceptions for out of stock scenarios These are needed to differentiate between the different stock validation errors, and so we can create the correct error message. * Catch new out of stock exceptions when checking the cart for errors This is so we can get the cart sent back to the client, if we don't catch these, then the route will just return a 500 error and crash. * Add ArrayUtils class This will contain methods used to operate on arrays that don't fit anywhere else. * Handle the case in Checkout where the error is already a WP_Error This will happen when the cart fails validation. * Handle StockAvailabilityException in AbstractRoute This will happen when an item or number of items in the cart are out of stock/insufficient stock. * Throw exceptions for each type of invalid stock in validate_cart_items This will allow us to create an error message for each type of violation to display to the user. * Display additional error notices returned by the API * Fix wording when throwing exceptions relating to stock * Handle TooManyInCartException in CartController * Abstract the merging of cart, status, and additional data into new fn This allows us to simplify the way errors are returned from the API. The reason we have to add all of the data at once is because of how WP_Error works with the additional data, if there is already existing data in a WP_Error object, it gets moved into additional_data. By adding all of the data in one place, we stop this from happening. Also since we're only adding status and/or cart explicitly, it makes sense to just do it in one place. * Add get_route_error_response_from_object method This is so we can differentiate between a string and WP_Error object. * Remove unnecessary slashes from WP_Error instantiation * Add option to enclose each item in quotes in natural_language_join * Abstract adding error messages to error object into single function A lot of code was repeated, so doing this cuts down on that and ensures any changes only need to be made in one place. * Create new parent exception for each type of out of stock exception This is so we don't have to repeat code inside each different exception and we can simply inherit StockAvailabilityException. * Catch the generic StockAvailabilityException in get_cart_item_errors * No longer recalculate totals in validate function It is not needed, the totals are recalculated elsewhere. This call was superfluous. * Reduce nesting, and only throw exception if error object has errors * Improve comment on get_route_error_response_from_object method * Fix nesting when throwing the InvalidStockLevelsInCartException * Catch errors during cart validation * Add get_error_message_for_stock_exception_type function This will be used to get an error message depending on the type of extension, and whether the plural form, or singular form is needed. This abstraction is better because the code is used in more than one place in the API. * Use error message generation function instead of creating errors individually * Use specific error codes for each type of error & fix PHPDoc Now the stock_exceptions_to_wp_errors function will return an array of WP_Errors this is better as the client needs a distinct error code to display and replace notices. * No longer remove items from cart or modify quantity if there is an error * Return the WP_Error from RouteException or all Stock WP_Errors * Fix error in PHPDoc * No longer necessary to add get_cart_item_errors into an array This is because the method always returns an array now. * Rename variables in get_cart_item_errors to be more descriptive * Improve PHPDoc on get_cart_item_errors * Replace underscores with hyphens in error code * Only return WP_Errors that have an actual error in them * Add unit test for ensure the cart errors relating to stock are handled * Fix typo in comment * Use optional chaining to add errors to cart
2021-02-15 14:03:04 +00:00
<?php
/**
* CartController Tests.
Catch errors during cart validation (https://github.com/woocommerce/woocommerce-blocks/pull/3703) * Add new exceptions for out of stock scenarios These are needed to differentiate between the different stock validation errors, and so we can create the correct error message. * Catch new out of stock exceptions when checking the cart for errors This is so we can get the cart sent back to the client, if we don't catch these, then the route will just return a 500 error and crash. * Add ArrayUtils class This will contain methods used to operate on arrays that don't fit anywhere else. * Handle the case in Checkout where the error is already a WP_Error This will happen when the cart fails validation. * Handle StockAvailabilityException in AbstractRoute This will happen when an item or number of items in the cart are out of stock/insufficient stock. * Throw exceptions for each type of invalid stock in validate_cart_items This will allow us to create an error message for each type of violation to display to the user. * Display additional error notices returned by the API * Fix wording when throwing exceptions relating to stock * Handle TooManyInCartException in CartController * Abstract the merging of cart, status, and additional data into new fn This allows us to simplify the way errors are returned from the API. The reason we have to add all of the data at once is because of how WP_Error works with the additional data, if there is already existing data in a WP_Error object, it gets moved into additional_data. By adding all of the data in one place, we stop this from happening. Also since we're only adding status and/or cart explicitly, it makes sense to just do it in one place. * Add get_route_error_response_from_object method This is so we can differentiate between a string and WP_Error object. * Remove unnecessary slashes from WP_Error instantiation * Add option to enclose each item in quotes in natural_language_join * Abstract adding error messages to error object into single function A lot of code was repeated, so doing this cuts down on that and ensures any changes only need to be made in one place. * Create new parent exception for each type of out of stock exception This is so we don't have to repeat code inside each different exception and we can simply inherit StockAvailabilityException. * Catch the generic StockAvailabilityException in get_cart_item_errors * No longer recalculate totals in validate function It is not needed, the totals are recalculated elsewhere. This call was superfluous. * Reduce nesting, and only throw exception if error object has errors * Improve comment on get_route_error_response_from_object method * Fix nesting when throwing the InvalidStockLevelsInCartException * Catch errors during cart validation * Add get_error_message_for_stock_exception_type function This will be used to get an error message depending on the type of extension, and whether the plural form, or singular form is needed. This abstraction is better because the code is used in more than one place in the API. * Use error message generation function instead of creating errors individually * Use specific error codes for each type of error & fix PHPDoc Now the stock_exceptions_to_wp_errors function will return an array of WP_Errors this is better as the client needs a distinct error code to display and replace notices. * No longer remove items from cart or modify quantity if there is an error * Return the WP_Error from RouteException or all Stock WP_Errors * Fix error in PHPDoc * No longer necessary to add get_cart_item_errors into an array This is because the method always returns an array now. * Rename variables in get_cart_item_errors to be more descriptive * Improve PHPDoc on get_cart_item_errors * Replace underscores with hyphens in error code * Only return WP_Errors that have an actual error in them * Add unit test for ensure the cart errors relating to stock are handled * Fix typo in comment * Use optional chaining to add errors to cart
2021-02-15 14:03:04 +00:00
*/
namespace Automattic\WooCommerce\Blocks\Tests\StoreApi\Utilities;
use Automattic\WooCommerce\Blocks\Tests\Helpers\FixtureData;
use Automattic\WooCommerce\StoreApi\Utilities\CartController;
use Yoast\PHPUnitPolyfills\TestCases\TestCase;
Catch errors during cart validation (https://github.com/woocommerce/woocommerce-blocks/pull/3703) * Add new exceptions for out of stock scenarios These are needed to differentiate between the different stock validation errors, and so we can create the correct error message. * Catch new out of stock exceptions when checking the cart for errors This is so we can get the cart sent back to the client, if we don't catch these, then the route will just return a 500 error and crash. * Add ArrayUtils class This will contain methods used to operate on arrays that don't fit anywhere else. * Handle the case in Checkout where the error is already a WP_Error This will happen when the cart fails validation. * Handle StockAvailabilityException in AbstractRoute This will happen when an item or number of items in the cart are out of stock/insufficient stock. * Throw exceptions for each type of invalid stock in validate_cart_items This will allow us to create an error message for each type of violation to display to the user. * Display additional error notices returned by the API * Fix wording when throwing exceptions relating to stock * Handle TooManyInCartException in CartController * Abstract the merging of cart, status, and additional data into new fn This allows us to simplify the way errors are returned from the API. The reason we have to add all of the data at once is because of how WP_Error works with the additional data, if there is already existing data in a WP_Error object, it gets moved into additional_data. By adding all of the data in one place, we stop this from happening. Also since we're only adding status and/or cart explicitly, it makes sense to just do it in one place. * Add get_route_error_response_from_object method This is so we can differentiate between a string and WP_Error object. * Remove unnecessary slashes from WP_Error instantiation * Add option to enclose each item in quotes in natural_language_join * Abstract adding error messages to error object into single function A lot of code was repeated, so doing this cuts down on that and ensures any changes only need to be made in one place. * Create new parent exception for each type of out of stock exception This is so we don't have to repeat code inside each different exception and we can simply inherit StockAvailabilityException. * Catch the generic StockAvailabilityException in get_cart_item_errors * No longer recalculate totals in validate function It is not needed, the totals are recalculated elsewhere. This call was superfluous. * Reduce nesting, and only throw exception if error object has errors * Improve comment on get_route_error_response_from_object method * Fix nesting when throwing the InvalidStockLevelsInCartException * Catch errors during cart validation * Add get_error_message_for_stock_exception_type function This will be used to get an error message depending on the type of extension, and whether the plural form, or singular form is needed. This abstraction is better because the code is used in more than one place in the API. * Use error message generation function instead of creating errors individually * Use specific error codes for each type of error & fix PHPDoc Now the stock_exceptions_to_wp_errors function will return an array of WP_Errors this is better as the client needs a distinct error code to display and replace notices. * No longer remove items from cart or modify quantity if there is an error * Return the WP_Error from RouteException or all Stock WP_Errors * Fix error in PHPDoc * No longer necessary to add get_cart_item_errors into an array This is because the method always returns an array now. * Rename variables in get_cart_item_errors to be more descriptive * Improve PHPDoc on get_cart_item_errors * Replace underscores with hyphens in error code * Only return WP_Errors that have an actual error in them * Add unit test for ensure the cart errors relating to stock are handled * Fix typo in comment * Use optional chaining to add errors to cart
2021-02-15 14:03:04 +00:00
class CartControllerTests extends TestCase {
Catch errors during cart validation (https://github.com/woocommerce/woocommerce-blocks/pull/3703) * Add new exceptions for out of stock scenarios These are needed to differentiate between the different stock validation errors, and so we can create the correct error message. * Catch new out of stock exceptions when checking the cart for errors This is so we can get the cart sent back to the client, if we don't catch these, then the route will just return a 500 error and crash. * Add ArrayUtils class This will contain methods used to operate on arrays that don't fit anywhere else. * Handle the case in Checkout where the error is already a WP_Error This will happen when the cart fails validation. * Handle StockAvailabilityException in AbstractRoute This will happen when an item or number of items in the cart are out of stock/insufficient stock. * Throw exceptions for each type of invalid stock in validate_cart_items This will allow us to create an error message for each type of violation to display to the user. * Display additional error notices returned by the API * Fix wording when throwing exceptions relating to stock * Handle TooManyInCartException in CartController * Abstract the merging of cart, status, and additional data into new fn This allows us to simplify the way errors are returned from the API. The reason we have to add all of the data at once is because of how WP_Error works with the additional data, if there is already existing data in a WP_Error object, it gets moved into additional_data. By adding all of the data in one place, we stop this from happening. Also since we're only adding status and/or cart explicitly, it makes sense to just do it in one place. * Add get_route_error_response_from_object method This is so we can differentiate between a string and WP_Error object. * Remove unnecessary slashes from WP_Error instantiation * Add option to enclose each item in quotes in natural_language_join * Abstract adding error messages to error object into single function A lot of code was repeated, so doing this cuts down on that and ensures any changes only need to be made in one place. * Create new parent exception for each type of out of stock exception This is so we don't have to repeat code inside each different exception and we can simply inherit StockAvailabilityException. * Catch the generic StockAvailabilityException in get_cart_item_errors * No longer recalculate totals in validate function It is not needed, the totals are recalculated elsewhere. This call was superfluous. * Reduce nesting, and only throw exception if error object has errors * Improve comment on get_route_error_response_from_object method * Fix nesting when throwing the InvalidStockLevelsInCartException * Catch errors during cart validation * Add get_error_message_for_stock_exception_type function This will be used to get an error message depending on the type of extension, and whether the plural form, or singular form is needed. This abstraction is better because the code is used in more than one place in the API. * Use error message generation function instead of creating errors individually * Use specific error codes for each type of error & fix PHPDoc Now the stock_exceptions_to_wp_errors function will return an array of WP_Errors this is better as the client needs a distinct error code to display and replace notices. * No longer remove items from cart or modify quantity if there is an error * Return the WP_Error from RouteException or all Stock WP_Errors * Fix error in PHPDoc * No longer necessary to add get_cart_item_errors into an array This is because the method always returns an array now. * Rename variables in get_cart_item_errors to be more descriptive * Improve PHPDoc on get_cart_item_errors * Replace underscores with hyphens in error code * Only return WP_Errors that have an actual error in them * Add unit test for ensure the cart errors relating to stock are handled * Fix typo in comment * Use optional chaining to add errors to cart
2021-02-15 14:03:04 +00:00
Store API: Refactored validation handling and introduced `woocommerce_store_api_cart_errors` hook (https://github.com/woocommerce/woocommerce-blocks/pull/5904) * Introduce 'wooocommerce_store_api_validate_cart' action This action can be used by third party developers to add extra 'errors' in cart route responses. This is meant to function as a replacement of the legacy 'woocommerce_check_cart_items' hook. * Introduce a new 'wooocommerce_blocks_draft_order_updated' action The new action can be used to modify the finalized draft order object. Additionally, developers can use it to throw a RouteException in order to prevent access to the checkout block. The PR also shuffles some existing code that checks the order instance and sets the draft order ID before the 'woocommerce_blocks_checkout_update_order_meta' gets fired. * Fix PHPCS * Revert changes to Checkout route * Add some extra content to the 'woocommerce_blocks_checkout_update_order_meta' docblock on the effect of exceptions thrown from callbacks * Add __experimental prefix to 'wooocommerce_store_api_validate_cart' action * Remove long ooo * Add example * Refactor cart error getters as wrappers to the validate functions * Refactor stock_exceptions_to_wp_errors to return a single wp error. * Add type hint in custom exception * Fix namespace versioning and some minor improvements * Move InvalidCartException catcher from AbstractCartRoute to AbstractRoute * Introduce the __experimental_woocommerce_store_api_cart_errors filter * Handle all notices and group them into a single WP_Error instance * Cleanup doc and update example * Cleanup NoticeHandler * Fix doc * Fix WP_Error namespace * Update NoticeHandler unit tests * Fix Unit tests typo * Add docs to tests to bypass precommit scripts * Refactor wp error grouping with merge_from * Remove previous cart getters and refactor the get_cart_errors * Fix CartSchema error getter and minor fixes * Revert NoticeHandler exception namespace * Unit test fix for get_cart_errors * Switch back to do_action * Update the example doc * Duplicate use statement * Update docblock in validate_cart() method for validation hooks * Update action docs * Remove __experimental prefix Co-authored-by: Manos Psychogyiopoulos <psyx@somewherewarm.com> Co-authored-by: Mike Jolley <mike.jolley@me.com>
2022-03-02 12:21:31 +00:00
public function test_get_cart_errors() {
$class = new CartController();
$fixtures = new FixtureData();
Catch errors during cart validation (https://github.com/woocommerce/woocommerce-blocks/pull/3703) * Add new exceptions for out of stock scenarios These are needed to differentiate between the different stock validation errors, and so we can create the correct error message. * Catch new out of stock exceptions when checking the cart for errors This is so we can get the cart sent back to the client, if we don't catch these, then the route will just return a 500 error and crash. * Add ArrayUtils class This will contain methods used to operate on arrays that don't fit anywhere else. * Handle the case in Checkout where the error is already a WP_Error This will happen when the cart fails validation. * Handle StockAvailabilityException in AbstractRoute This will happen when an item or number of items in the cart are out of stock/insufficient stock. * Throw exceptions for each type of invalid stock in validate_cart_items This will allow us to create an error message for each type of violation to display to the user. * Display additional error notices returned by the API * Fix wording when throwing exceptions relating to stock * Handle TooManyInCartException in CartController * Abstract the merging of cart, status, and additional data into new fn This allows us to simplify the way errors are returned from the API. The reason we have to add all of the data at once is because of how WP_Error works with the additional data, if there is already existing data in a WP_Error object, it gets moved into additional_data. By adding all of the data in one place, we stop this from happening. Also since we're only adding status and/or cart explicitly, it makes sense to just do it in one place. * Add get_route_error_response_from_object method This is so we can differentiate between a string and WP_Error object. * Remove unnecessary slashes from WP_Error instantiation * Add option to enclose each item in quotes in natural_language_join * Abstract adding error messages to error object into single function A lot of code was repeated, so doing this cuts down on that and ensures any changes only need to be made in one place. * Create new parent exception for each type of out of stock exception This is so we don't have to repeat code inside each different exception and we can simply inherit StockAvailabilityException. * Catch the generic StockAvailabilityException in get_cart_item_errors * No longer recalculate totals in validate function It is not needed, the totals are recalculated elsewhere. This call was superfluous. * Reduce nesting, and only throw exception if error object has errors * Improve comment on get_route_error_response_from_object method * Fix nesting when throwing the InvalidStockLevelsInCartException * Catch errors during cart validation * Add get_error_message_for_stock_exception_type function This will be used to get an error message depending on the type of extension, and whether the plural form, or singular form is needed. This abstraction is better because the code is used in more than one place in the API. * Use error message generation function instead of creating errors individually * Use specific error codes for each type of error & fix PHPDoc Now the stock_exceptions_to_wp_errors function will return an array of WP_Errors this is better as the client needs a distinct error code to display and replace notices. * No longer remove items from cart or modify quantity if there is an error * Return the WP_Error from RouteException or all Stock WP_Errors * Fix error in PHPDoc * No longer necessary to add get_cart_item_errors into an array This is because the method always returns an array now. * Rename variables in get_cart_item_errors to be more descriptive * Improve PHPDoc on get_cart_item_errors * Replace underscores with hyphens in error code * Only return WP_Errors that have an actual error in them * Add unit test for ensure the cart errors relating to stock are handled * Fix typo in comment * Use optional chaining to add errors to cart
2021-02-15 14:03:04 +00:00
// This product will simply be in/out of stock.
$out_of_stock_product = $fixtures->get_simple_product( [
'name' => 'Test Product 1',
'regular_price' => 10,
] );
Catch errors during cart validation (https://github.com/woocommerce/woocommerce-blocks/pull/3703) * Add new exceptions for out of stock scenarios These are needed to differentiate between the different stock validation errors, and so we can create the correct error message. * Catch new out of stock exceptions when checking the cart for errors This is so we can get the cart sent back to the client, if we don't catch these, then the route will just return a 500 error and crash. * Add ArrayUtils class This will contain methods used to operate on arrays that don't fit anywhere else. * Handle the case in Checkout where the error is already a WP_Error This will happen when the cart fails validation. * Handle StockAvailabilityException in AbstractRoute This will happen when an item or number of items in the cart are out of stock/insufficient stock. * Throw exceptions for each type of invalid stock in validate_cart_items This will allow us to create an error message for each type of violation to display to the user. * Display additional error notices returned by the API * Fix wording when throwing exceptions relating to stock * Handle TooManyInCartException in CartController * Abstract the merging of cart, status, and additional data into new fn This allows us to simplify the way errors are returned from the API. The reason we have to add all of the data at once is because of how WP_Error works with the additional data, if there is already existing data in a WP_Error object, it gets moved into additional_data. By adding all of the data in one place, we stop this from happening. Also since we're only adding status and/or cart explicitly, it makes sense to just do it in one place. * Add get_route_error_response_from_object method This is so we can differentiate between a string and WP_Error object. * Remove unnecessary slashes from WP_Error instantiation * Add option to enclose each item in quotes in natural_language_join * Abstract adding error messages to error object into single function A lot of code was repeated, so doing this cuts down on that and ensures any changes only need to be made in one place. * Create new parent exception for each type of out of stock exception This is so we don't have to repeat code inside each different exception and we can simply inherit StockAvailabilityException. * Catch the generic StockAvailabilityException in get_cart_item_errors * No longer recalculate totals in validate function It is not needed, the totals are recalculated elsewhere. This call was superfluous. * Reduce nesting, and only throw exception if error object has errors * Improve comment on get_route_error_response_from_object method * Fix nesting when throwing the InvalidStockLevelsInCartException * Catch errors during cart validation * Add get_error_message_for_stock_exception_type function This will be used to get an error message depending on the type of extension, and whether the plural form, or singular form is needed. This abstraction is better because the code is used in more than one place in the API. * Use error message generation function instead of creating errors individually * Use specific error codes for each type of error & fix PHPDoc Now the stock_exceptions_to_wp_errors function will return an array of WP_Errors this is better as the client needs a distinct error code to display and replace notices. * No longer remove items from cart or modify quantity if there is an error * Return the WP_Error from RouteException or all Stock WP_Errors * Fix error in PHPDoc * No longer necessary to add get_cart_item_errors into an array This is because the method always returns an array now. * Rename variables in get_cart_item_errors to be more descriptive * Improve PHPDoc on get_cart_item_errors * Replace underscores with hyphens in error code * Only return WP_Errors that have an actual error in them * Add unit test for ensure the cart errors relating to stock are handled * Fix typo in comment * Use optional chaining to add errors to cart
2021-02-15 14:03:04 +00:00
$out_of_stock_product_key = wc()->cart->add_to_cart( $out_of_stock_product->get_id(), 2 );
$out_of_stock_in_cart = wc()->cart->get_cart_item( $out_of_stock_product_key )['data'];
// This product will have exact levels of stock known
$partially_out_of_stock_product = $fixtures->get_simple_product( [
'name' => 'Test Product 2',
'regular_price' => 10,
] );
Catch errors during cart validation (https://github.com/woocommerce/woocommerce-blocks/pull/3703) * Add new exceptions for out of stock scenarios These are needed to differentiate between the different stock validation errors, and so we can create the correct error message. * Catch new out of stock exceptions when checking the cart for errors This is so we can get the cart sent back to the client, if we don't catch these, then the route will just return a 500 error and crash. * Add ArrayUtils class This will contain methods used to operate on arrays that don't fit anywhere else. * Handle the case in Checkout where the error is already a WP_Error This will happen when the cart fails validation. * Handle StockAvailabilityException in AbstractRoute This will happen when an item or number of items in the cart are out of stock/insufficient stock. * Throw exceptions for each type of invalid stock in validate_cart_items This will allow us to create an error message for each type of violation to display to the user. * Display additional error notices returned by the API * Fix wording when throwing exceptions relating to stock * Handle TooManyInCartException in CartController * Abstract the merging of cart, status, and additional data into new fn This allows us to simplify the way errors are returned from the API. The reason we have to add all of the data at once is because of how WP_Error works with the additional data, if there is already existing data in a WP_Error object, it gets moved into additional_data. By adding all of the data in one place, we stop this from happening. Also since we're only adding status and/or cart explicitly, it makes sense to just do it in one place. * Add get_route_error_response_from_object method This is so we can differentiate between a string and WP_Error object. * Remove unnecessary slashes from WP_Error instantiation * Add option to enclose each item in quotes in natural_language_join * Abstract adding error messages to error object into single function A lot of code was repeated, so doing this cuts down on that and ensures any changes only need to be made in one place. * Create new parent exception for each type of out of stock exception This is so we don't have to repeat code inside each different exception and we can simply inherit StockAvailabilityException. * Catch the generic StockAvailabilityException in get_cart_item_errors * No longer recalculate totals in validate function It is not needed, the totals are recalculated elsewhere. This call was superfluous. * Reduce nesting, and only throw exception if error object has errors * Improve comment on get_route_error_response_from_object method * Fix nesting when throwing the InvalidStockLevelsInCartException * Catch errors during cart validation * Add get_error_message_for_stock_exception_type function This will be used to get an error message depending on the type of extension, and whether the plural form, or singular form is needed. This abstraction is better because the code is used in more than one place in the API. * Use error message generation function instead of creating errors individually * Use specific error codes for each type of error & fix PHPDoc Now the stock_exceptions_to_wp_errors function will return an array of WP_Errors this is better as the client needs a distinct error code to display and replace notices. * No longer remove items from cart or modify quantity if there is an error * Return the WP_Error from RouteException or all Stock WP_Errors * Fix error in PHPDoc * No longer necessary to add get_cart_item_errors into an array This is because the method always returns an array now. * Rename variables in get_cart_item_errors to be more descriptive * Improve PHPDoc on get_cart_item_errors * Replace underscores with hyphens in error code * Only return WP_Errors that have an actual error in them * Add unit test for ensure the cart errors relating to stock are handled * Fix typo in comment * Use optional chaining to add errors to cart
2021-02-15 14:03:04 +00:00
$partially_out_of_stock_key = wc()->cart->add_to_cart( $partially_out_of_stock_product->get_id(), 4 );
$partially_out_of_stock_in_cart = wc()->cart->get_cart_item( $partially_out_of_stock_key )['data'];
// This product will have exact levels of stock known
$too_many_in_cart_product = $fixtures->get_simple_product( [
'name' => 'Test Product 3',
'regular_price' => 10,
] );
Catch errors during cart validation (https://github.com/woocommerce/woocommerce-blocks/pull/3703) * Add new exceptions for out of stock scenarios These are needed to differentiate between the different stock validation errors, and so we can create the correct error message. * Catch new out of stock exceptions when checking the cart for errors This is so we can get the cart sent back to the client, if we don't catch these, then the route will just return a 500 error and crash. * Add ArrayUtils class This will contain methods used to operate on arrays that don't fit anywhere else. * Handle the case in Checkout where the error is already a WP_Error This will happen when the cart fails validation. * Handle StockAvailabilityException in AbstractRoute This will happen when an item or number of items in the cart are out of stock/insufficient stock. * Throw exceptions for each type of invalid stock in validate_cart_items This will allow us to create an error message for each type of violation to display to the user. * Display additional error notices returned by the API * Fix wording when throwing exceptions relating to stock * Handle TooManyInCartException in CartController * Abstract the merging of cart, status, and additional data into new fn This allows us to simplify the way errors are returned from the API. The reason we have to add all of the data at once is because of how WP_Error works with the additional data, if there is already existing data in a WP_Error object, it gets moved into additional_data. By adding all of the data in one place, we stop this from happening. Also since we're only adding status and/or cart explicitly, it makes sense to just do it in one place. * Add get_route_error_response_from_object method This is so we can differentiate between a string and WP_Error object. * Remove unnecessary slashes from WP_Error instantiation * Add option to enclose each item in quotes in natural_language_join * Abstract adding error messages to error object into single function A lot of code was repeated, so doing this cuts down on that and ensures any changes only need to be made in one place. * Create new parent exception for each type of out of stock exception This is so we don't have to repeat code inside each different exception and we can simply inherit StockAvailabilityException. * Catch the generic StockAvailabilityException in get_cart_item_errors * No longer recalculate totals in validate function It is not needed, the totals are recalculated elsewhere. This call was superfluous. * Reduce nesting, and only throw exception if error object has errors * Improve comment on get_route_error_response_from_object method * Fix nesting when throwing the InvalidStockLevelsInCartException * Catch errors during cart validation * Add get_error_message_for_stock_exception_type function This will be used to get an error message depending on the type of extension, and whether the plural form, or singular form is needed. This abstraction is better because the code is used in more than one place in the API. * Use error message generation function instead of creating errors individually * Use specific error codes for each type of error & fix PHPDoc Now the stock_exceptions_to_wp_errors function will return an array of WP_Errors this is better as the client needs a distinct error code to display and replace notices. * No longer remove items from cart or modify quantity if there is an error * Return the WP_Error from RouteException or all Stock WP_Errors * Fix error in PHPDoc * No longer necessary to add get_cart_item_errors into an array This is because the method always returns an array now. * Rename variables in get_cart_item_errors to be more descriptive * Improve PHPDoc on get_cart_item_errors * Replace underscores with hyphens in error code * Only return WP_Errors that have an actual error in them * Add unit test for ensure the cart errors relating to stock are handled * Fix typo in comment * Use optional chaining to add errors to cart
2021-02-15 14:03:04 +00:00
$too_many_in_cart_product_key = wc()->cart->add_to_cart( $too_many_in_cart_product->get_id(), 4 );
$too_many_in_cart_in_cart = wc()->cart->get_cart_item( $too_many_in_cart_product_key )['data'];
$out_of_stock_in_cart->set_stock_status( 'outofstock' );
$partially_out_of_stock_in_cart->set_manage_stock( true );
$partially_out_of_stock_in_cart->set_stock_quantity( 2 );
$too_many_in_cart_in_cart->set_sold_individually( true );
// This product will not be purchasable
$not_purchasable_product = $fixtures->get_simple_product( [
'name' => 'Test Product 4',
'regular_price' => 10,
] );
Catch errors during cart validation (https://github.com/woocommerce/woocommerce-blocks/pull/3703) * Add new exceptions for out of stock scenarios These are needed to differentiate between the different stock validation errors, and so we can create the correct error message. * Catch new out of stock exceptions when checking the cart for errors This is so we can get the cart sent back to the client, if we don't catch these, then the route will just return a 500 error and crash. * Add ArrayUtils class This will contain methods used to operate on arrays that don't fit anywhere else. * Handle the case in Checkout where the error is already a WP_Error This will happen when the cart fails validation. * Handle StockAvailabilityException in AbstractRoute This will happen when an item or number of items in the cart are out of stock/insufficient stock. * Throw exceptions for each type of invalid stock in validate_cart_items This will allow us to create an error message for each type of violation to display to the user. * Display additional error notices returned by the API * Fix wording when throwing exceptions relating to stock * Handle TooManyInCartException in CartController * Abstract the merging of cart, status, and additional data into new fn This allows us to simplify the way errors are returned from the API. The reason we have to add all of the data at once is because of how WP_Error works with the additional data, if there is already existing data in a WP_Error object, it gets moved into additional_data. By adding all of the data in one place, we stop this from happening. Also since we're only adding status and/or cart explicitly, it makes sense to just do it in one place. * Add get_route_error_response_from_object method This is so we can differentiate between a string and WP_Error object. * Remove unnecessary slashes from WP_Error instantiation * Add option to enclose each item in quotes in natural_language_join * Abstract adding error messages to error object into single function A lot of code was repeated, so doing this cuts down on that and ensures any changes only need to be made in one place. * Create new parent exception for each type of out of stock exception This is so we don't have to repeat code inside each different exception and we can simply inherit StockAvailabilityException. * Catch the generic StockAvailabilityException in get_cart_item_errors * No longer recalculate totals in validate function It is not needed, the totals are recalculated elsewhere. This call was superfluous. * Reduce nesting, and only throw exception if error object has errors * Improve comment on get_route_error_response_from_object method * Fix nesting when throwing the InvalidStockLevelsInCartException * Catch errors during cart validation * Add get_error_message_for_stock_exception_type function This will be used to get an error message depending on the type of extension, and whether the plural form, or singular form is needed. This abstraction is better because the code is used in more than one place in the API. * Use error message generation function instead of creating errors individually * Use specific error codes for each type of error & fix PHPDoc Now the stock_exceptions_to_wp_errors function will return an array of WP_Errors this is better as the client needs a distinct error code to display and replace notices. * No longer remove items from cart or modify quantity if there is an error * Return the WP_Error from RouteException or all Stock WP_Errors * Fix error in PHPDoc * No longer necessary to add get_cart_item_errors into an array This is because the method always returns an array now. * Rename variables in get_cart_item_errors to be more descriptive * Improve PHPDoc on get_cart_item_errors * Replace underscores with hyphens in error code * Only return WP_Errors that have an actual error in them * Add unit test for ensure the cart errors relating to stock are handled * Fix typo in comment * Use optional chaining to add errors to cart
2021-02-15 14:03:04 +00:00
wc()->cart->add_to_cart( $not_purchasable_product->get_id(), 2 );
// This function will force the $product->is_purchasable() function to return false for our $not_purchasable_product
add_filter( 'woocommerce_is_purchasable', function( $is_purchasable, $product ) use ( $not_purchasable_product ) {
if ( $product->get_id() === $not_purchasable_product->get_id() ) {
return false;
}
return true;
}, 10, 2 );
Store API: Refactored validation handling and introduced `woocommerce_store_api_cart_errors` hook (https://github.com/woocommerce/woocommerce-blocks/pull/5904) * Introduce 'wooocommerce_store_api_validate_cart' action This action can be used by third party developers to add extra 'errors' in cart route responses. This is meant to function as a replacement of the legacy 'woocommerce_check_cart_items' hook. * Introduce a new 'wooocommerce_blocks_draft_order_updated' action The new action can be used to modify the finalized draft order object. Additionally, developers can use it to throw a RouteException in order to prevent access to the checkout block. The PR also shuffles some existing code that checks the order instance and sets the draft order ID before the 'woocommerce_blocks_checkout_update_order_meta' gets fired. * Fix PHPCS * Revert changes to Checkout route * Add some extra content to the 'woocommerce_blocks_checkout_update_order_meta' docblock on the effect of exceptions thrown from callbacks * Add __experimental prefix to 'wooocommerce_store_api_validate_cart' action * Remove long ooo * Add example * Refactor cart error getters as wrappers to the validate functions * Refactor stock_exceptions_to_wp_errors to return a single wp error. * Add type hint in custom exception * Fix namespace versioning and some minor improvements * Move InvalidCartException catcher from AbstractCartRoute to AbstractRoute * Introduce the __experimental_woocommerce_store_api_cart_errors filter * Handle all notices and group them into a single WP_Error instance * Cleanup doc and update example * Cleanup NoticeHandler * Fix doc * Fix WP_Error namespace * Update NoticeHandler unit tests * Fix Unit tests typo * Add docs to tests to bypass precommit scripts * Refactor wp error grouping with merge_from * Remove previous cart getters and refactor the get_cart_errors * Fix CartSchema error getter and minor fixes * Revert NoticeHandler exception namespace * Unit test fix for get_cart_errors * Switch back to do_action * Update the example doc * Duplicate use statement * Update docblock in validate_cart() method for validation hooks * Update action docs * Remove __experimental prefix Co-authored-by: Manos Psychogyiopoulos <psyx@somewherewarm.com> Co-authored-by: Mike Jolley <mike.jolley@me.com>
2022-03-02 12:21:31 +00:00
$errors = $class->get_cart_errors();
Catch errors during cart validation (https://github.com/woocommerce/woocommerce-blocks/pull/3703) * Add new exceptions for out of stock scenarios These are needed to differentiate between the different stock validation errors, and so we can create the correct error message. * Catch new out of stock exceptions when checking the cart for errors This is so we can get the cart sent back to the client, if we don't catch these, then the route will just return a 500 error and crash. * Add ArrayUtils class This will contain methods used to operate on arrays that don't fit anywhere else. * Handle the case in Checkout where the error is already a WP_Error This will happen when the cart fails validation. * Handle StockAvailabilityException in AbstractRoute This will happen when an item or number of items in the cart are out of stock/insufficient stock. * Throw exceptions for each type of invalid stock in validate_cart_items This will allow us to create an error message for each type of violation to display to the user. * Display additional error notices returned by the API * Fix wording when throwing exceptions relating to stock * Handle TooManyInCartException in CartController * Abstract the merging of cart, status, and additional data into new fn This allows us to simplify the way errors are returned from the API. The reason we have to add all of the data at once is because of how WP_Error works with the additional data, if there is already existing data in a WP_Error object, it gets moved into additional_data. By adding all of the data in one place, we stop this from happening. Also since we're only adding status and/or cart explicitly, it makes sense to just do it in one place. * Add get_route_error_response_from_object method This is so we can differentiate between a string and WP_Error object. * Remove unnecessary slashes from WP_Error instantiation * Add option to enclose each item in quotes in natural_language_join * Abstract adding error messages to error object into single function A lot of code was repeated, so doing this cuts down on that and ensures any changes only need to be made in one place. * Create new parent exception for each type of out of stock exception This is so we don't have to repeat code inside each different exception and we can simply inherit StockAvailabilityException. * Catch the generic StockAvailabilityException in get_cart_item_errors * No longer recalculate totals in validate function It is not needed, the totals are recalculated elsewhere. This call was superfluous. * Reduce nesting, and only throw exception if error object has errors * Improve comment on get_route_error_response_from_object method * Fix nesting when throwing the InvalidStockLevelsInCartException * Catch errors during cart validation * Add get_error_message_for_stock_exception_type function This will be used to get an error message depending on the type of extension, and whether the plural form, or singular form is needed. This abstraction is better because the code is used in more than one place in the API. * Use error message generation function instead of creating errors individually * Use specific error codes for each type of error & fix PHPDoc Now the stock_exceptions_to_wp_errors function will return an array of WP_Errors this is better as the client needs a distinct error code to display and replace notices. * No longer remove items from cart or modify quantity if there is an error * Return the WP_Error from RouteException or all Stock WP_Errors * Fix error in PHPDoc * No longer necessary to add get_cart_item_errors into an array This is because the method always returns an array now. * Rename variables in get_cart_item_errors to be more descriptive * Improve PHPDoc on get_cart_item_errors * Replace underscores with hyphens in error code * Only return WP_Errors that have an actual error in them * Add unit test for ensure the cart errors relating to stock are handled * Fix typo in comment * Use optional chaining to add errors to cart
2021-02-15 14:03:04 +00:00
Store API: Refactored validation handling and introduced `woocommerce_store_api_cart_errors` hook (https://github.com/woocommerce/woocommerce-blocks/pull/5904) * Introduce 'wooocommerce_store_api_validate_cart' action This action can be used by third party developers to add extra 'errors' in cart route responses. This is meant to function as a replacement of the legacy 'woocommerce_check_cart_items' hook. * Introduce a new 'wooocommerce_blocks_draft_order_updated' action The new action can be used to modify the finalized draft order object. Additionally, developers can use it to throw a RouteException in order to prevent access to the checkout block. The PR also shuffles some existing code that checks the order instance and sets the draft order ID before the 'woocommerce_blocks_checkout_update_order_meta' gets fired. * Fix PHPCS * Revert changes to Checkout route * Add some extra content to the 'woocommerce_blocks_checkout_update_order_meta' docblock on the effect of exceptions thrown from callbacks * Add __experimental prefix to 'wooocommerce_store_api_validate_cart' action * Remove long ooo * Add example * Refactor cart error getters as wrappers to the validate functions * Refactor stock_exceptions_to_wp_errors to return a single wp error. * Add type hint in custom exception * Fix namespace versioning and some minor improvements * Move InvalidCartException catcher from AbstractCartRoute to AbstractRoute * Introduce the __experimental_woocommerce_store_api_cart_errors filter * Handle all notices and group them into a single WP_Error instance * Cleanup doc and update example * Cleanup NoticeHandler * Fix doc * Fix WP_Error namespace * Update NoticeHandler unit tests * Fix Unit tests typo * Add docs to tests to bypass precommit scripts * Refactor wp error grouping with merge_from * Remove previous cart getters and refactor the get_cart_errors * Fix CartSchema error getter and minor fixes * Revert NoticeHandler exception namespace * Unit test fix for get_cart_errors * Switch back to do_action * Update the example doc * Duplicate use statement * Update docblock in validate_cart() method for validation hooks * Update action docs * Remove __experimental prefix Co-authored-by: Manos Psychogyiopoulos <psyx@somewherewarm.com> Co-authored-by: Mike Jolley <mike.jolley@me.com>
2022-03-02 12:21:31 +00:00
$this->assertTrue( is_wp_error( $errors ) );
$this->assertTrue( $errors->has_errors() );
$error_codes = $errors->get_error_codes();
Catch errors during cart validation (https://github.com/woocommerce/woocommerce-blocks/pull/3703) * Add new exceptions for out of stock scenarios These are needed to differentiate between the different stock validation errors, and so we can create the correct error message. * Catch new out of stock exceptions when checking the cart for errors This is so we can get the cart sent back to the client, if we don't catch these, then the route will just return a 500 error and crash. * Add ArrayUtils class This will contain methods used to operate on arrays that don't fit anywhere else. * Handle the case in Checkout where the error is already a WP_Error This will happen when the cart fails validation. * Handle StockAvailabilityException in AbstractRoute This will happen when an item or number of items in the cart are out of stock/insufficient stock. * Throw exceptions for each type of invalid stock in validate_cart_items This will allow us to create an error message for each type of violation to display to the user. * Display additional error notices returned by the API * Fix wording when throwing exceptions relating to stock * Handle TooManyInCartException in CartController * Abstract the merging of cart, status, and additional data into new fn This allows us to simplify the way errors are returned from the API. The reason we have to add all of the data at once is because of how WP_Error works with the additional data, if there is already existing data in a WP_Error object, it gets moved into additional_data. By adding all of the data in one place, we stop this from happening. Also since we're only adding status and/or cart explicitly, it makes sense to just do it in one place. * Add get_route_error_response_from_object method This is so we can differentiate between a string and WP_Error object. * Remove unnecessary slashes from WP_Error instantiation * Add option to enclose each item in quotes in natural_language_join * Abstract adding error messages to error object into single function A lot of code was repeated, so doing this cuts down on that and ensures any changes only need to be made in one place. * Create new parent exception for each type of out of stock exception This is so we don't have to repeat code inside each different exception and we can simply inherit StockAvailabilityException. * Catch the generic StockAvailabilityException in get_cart_item_errors * No longer recalculate totals in validate function It is not needed, the totals are recalculated elsewhere. This call was superfluous. * Reduce nesting, and only throw exception if error object has errors * Improve comment on get_route_error_response_from_object method * Fix nesting when throwing the InvalidStockLevelsInCartException * Catch errors during cart validation * Add get_error_message_for_stock_exception_type function This will be used to get an error message depending on the type of extension, and whether the plural form, or singular form is needed. This abstraction is better because the code is used in more than one place in the API. * Use error message generation function instead of creating errors individually * Use specific error codes for each type of error & fix PHPDoc Now the stock_exceptions_to_wp_errors function will return an array of WP_Errors this is better as the client needs a distinct error code to display and replace notices. * No longer remove items from cart or modify quantity if there is an error * Return the WP_Error from RouteException or all Stock WP_Errors * Fix error in PHPDoc * No longer necessary to add get_cart_item_errors into an array This is because the method always returns an array now. * Rename variables in get_cart_item_errors to be more descriptive * Improve PHPDoc on get_cart_item_errors * Replace underscores with hyphens in error code * Only return WP_Errors that have an actual error in them * Add unit test for ensure the cart errors relating to stock are handled * Fix typo in comment * Use optional chaining to add errors to cart
2021-02-15 14:03:04 +00:00
$expected_errors = [
'woocommerce-blocks-product-partially-out-of-stock',
'woocommerce-blocks-product-out-of-stock',
'woocommerce-blocks-product-not-purchasable',
'woocommerce-blocks-too-many-of-product-in-cart',
];
foreach( $expected_errors as $expected_error ) {
Store API: Refactored validation handling and introduced `woocommerce_store_api_cart_errors` hook (https://github.com/woocommerce/woocommerce-blocks/pull/5904) * Introduce 'wooocommerce_store_api_validate_cart' action This action can be used by third party developers to add extra 'errors' in cart route responses. This is meant to function as a replacement of the legacy 'woocommerce_check_cart_items' hook. * Introduce a new 'wooocommerce_blocks_draft_order_updated' action The new action can be used to modify the finalized draft order object. Additionally, developers can use it to throw a RouteException in order to prevent access to the checkout block. The PR also shuffles some existing code that checks the order instance and sets the draft order ID before the 'woocommerce_blocks_checkout_update_order_meta' gets fired. * Fix PHPCS * Revert changes to Checkout route * Add some extra content to the 'woocommerce_blocks_checkout_update_order_meta' docblock on the effect of exceptions thrown from callbacks * Add __experimental prefix to 'wooocommerce_store_api_validate_cart' action * Remove long ooo * Add example * Refactor cart error getters as wrappers to the validate functions * Refactor stock_exceptions_to_wp_errors to return a single wp error. * Add type hint in custom exception * Fix namespace versioning and some minor improvements * Move InvalidCartException catcher from AbstractCartRoute to AbstractRoute * Introduce the __experimental_woocommerce_store_api_cart_errors filter * Handle all notices and group them into a single WP_Error instance * Cleanup doc and update example * Cleanup NoticeHandler * Fix doc * Fix WP_Error namespace * Update NoticeHandler unit tests * Fix Unit tests typo * Add docs to tests to bypass precommit scripts * Refactor wp error grouping with merge_from * Remove previous cart getters and refactor the get_cart_errors * Fix CartSchema error getter and minor fixes * Revert NoticeHandler exception namespace * Unit test fix for get_cart_errors * Switch back to do_action * Update the example doc * Duplicate use statement * Update docblock in validate_cart() method for validation hooks * Update action docs * Remove __experimental prefix Co-authored-by: Manos Psychogyiopoulos <psyx@somewherewarm.com> Co-authored-by: Mike Jolley <mike.jolley@me.com>
2022-03-02 12:21:31 +00:00
$this->assertContains( $expected_error, $error_codes );
Catch errors during cart validation (https://github.com/woocommerce/woocommerce-blocks/pull/3703) * Add new exceptions for out of stock scenarios These are needed to differentiate between the different stock validation errors, and so we can create the correct error message. * Catch new out of stock exceptions when checking the cart for errors This is so we can get the cart sent back to the client, if we don't catch these, then the route will just return a 500 error and crash. * Add ArrayUtils class This will contain methods used to operate on arrays that don't fit anywhere else. * Handle the case in Checkout where the error is already a WP_Error This will happen when the cart fails validation. * Handle StockAvailabilityException in AbstractRoute This will happen when an item or number of items in the cart are out of stock/insufficient stock. * Throw exceptions for each type of invalid stock in validate_cart_items This will allow us to create an error message for each type of violation to display to the user. * Display additional error notices returned by the API * Fix wording when throwing exceptions relating to stock * Handle TooManyInCartException in CartController * Abstract the merging of cart, status, and additional data into new fn This allows us to simplify the way errors are returned from the API. The reason we have to add all of the data at once is because of how WP_Error works with the additional data, if there is already existing data in a WP_Error object, it gets moved into additional_data. By adding all of the data in one place, we stop this from happening. Also since we're only adding status and/or cart explicitly, it makes sense to just do it in one place. * Add get_route_error_response_from_object method This is so we can differentiate between a string and WP_Error object. * Remove unnecessary slashes from WP_Error instantiation * Add option to enclose each item in quotes in natural_language_join * Abstract adding error messages to error object into single function A lot of code was repeated, so doing this cuts down on that and ensures any changes only need to be made in one place. * Create new parent exception for each type of out of stock exception This is so we don't have to repeat code inside each different exception and we can simply inherit StockAvailabilityException. * Catch the generic StockAvailabilityException in get_cart_item_errors * No longer recalculate totals in validate function It is not needed, the totals are recalculated elsewhere. This call was superfluous. * Reduce nesting, and only throw exception if error object has errors * Improve comment on get_route_error_response_from_object method * Fix nesting when throwing the InvalidStockLevelsInCartException * Catch errors during cart validation * Add get_error_message_for_stock_exception_type function This will be used to get an error message depending on the type of extension, and whether the plural form, or singular form is needed. This abstraction is better because the code is used in more than one place in the API. * Use error message generation function instead of creating errors individually * Use specific error codes for each type of error & fix PHPDoc Now the stock_exceptions_to_wp_errors function will return an array of WP_Errors this is better as the client needs a distinct error code to display and replace notices. * No longer remove items from cart or modify quantity if there is an error * Return the WP_Error from RouteException or all Stock WP_Errors * Fix error in PHPDoc * No longer necessary to add get_cart_item_errors into an array This is because the method always returns an array now. * Rename variables in get_cart_item_errors to be more descriptive * Improve PHPDoc on get_cart_item_errors * Replace underscores with hyphens in error code * Only return WP_Errors that have an actual error in them * Add unit test for ensure the cart errors relating to stock are handled * Fix typo in comment * Use optional chaining to add errors to cart
2021-02-15 14:03:04 +00:00
}
}
}