Skip to content

Commit

Permalink
Prevent orders being placed when no shipping options are available (#…
Browse files Browse the repository at this point in the history
…46026)

* Prevent orders being placed with invalid shipping options

* Add changelog

* Add shipping_disable_flat_rate fixture function

* Test checking out with no valid shipping methods selected

* Update tests to add a default shipping method

* Update test_checkout_invalid_shipping_method to disable method

* If shipping methods is null, return an array with an empty string inside

* Replace WC session in tests that rely on setting cookies

* Add MockSessionHandler to handle test cases using cookies

* Add docblock comment

* Expect shipping validation to fail if chosen methods are null

* Add shipping method before testing validate_selected_shipping_methods

* Update MockSessionHandler to handle caching

* Show error when test fails

* Default the chosen shipping methods to an empty array if not set

* Split checks for needs_shipping and valid shipping apart

* Remove unnecessary session set and total calculation

* Fix lint errors

* Init session in each test

* Reimplement required methods (those that are private or use cookies)

* Update phpcs ignore comment to be inline

* Prevent error when accessing unset variable in mock cache

* Fix lint error
  • Loading branch information
opr authored and senadir committed Apr 4, 2024
1 parent 269027b commit c803aca
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 12 deletions.
20 changes: 20 additions & 0 deletions plugins/woocommerce/tests/php/src/Blocks/Helpers/FixtureData.php
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,26 @@ public function shipping_add_flat_rate( $cost = 10 ) {
WC()->shipping()->load_shipping_methods();
}

/**
* Disables the flat rate method.
*
* @param float $cost Optional. Cost of flat rate method.
*/
public function shipping_disable_flat_rate( $cost = 10 ) {
$flat_rate_settings = array(
'enabled' => 'no',
'title' => 'Flat rate',
'availability' => 'all',
'countries' => '',
'tax_status' => 'taxable',
'cost' => $cost,
);
update_option( 'woocommerce_flat_rate_settings', $flat_rate_settings );
update_option( 'woocommerce_flat_rate', array() );
\WC_Cache_Helper::get_transient_version( 'shipping', true );
WC()->shipping()->load_shipping_methods();
}

/**
* Enable bacs payment method.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -324,8 +324,6 @@ public function test_not_passing_extensions_data() {
* Check that accounts are created on request.
*/
public function test_checkout_create_account() {
$enable_guest_checkout = get_option( 'woocommerce_enable_guest_checkout' );
$enable_signup_login = get_option( 'woocommerce_enable_signup_and_login_from_checkout' );
// We need to replace the WC_Session with a mock because this test relies on cookies being set which
// is not easy with PHPUnit. This is a simpler approach.
$old_session = WC()->session;
Expand Down Expand Up @@ -391,16 +389,12 @@ public function test_checkout_create_account() {
update_option( 'woocommerce_enable_signup_and_login_from_checkout', $enable_signup_login );
// Return WC_Session to original state.
WC()->session = $old_session;
update_option( 'woocommerce_enable_guest_checkout', $enable_guest_checkout );
update_option( 'woocommerce_enable_signup_and_login_from_checkout', $enable_signup_login );
}

/**
* Test account creation options.
*/
public function test_checkout_do_not_create_account() {
$enable_guest_checkout = get_option( 'woocommerce_enable_guest_checkout' );
$enable_signup_login = get_option( 'woocommerce_enable_signup_and_login_from_checkout' );
// We need to replace the WC_Session with a mock because this test relies on cookies being set which
// is not easy with PHPUnit. This is a simpler approach.
$old_session = WC()->session;
Expand Down Expand Up @@ -463,16 +457,12 @@ public function test_checkout_do_not_create_account() {
update_option( 'woocommerce_enable_signup_and_login_from_checkout', $enable_signup_login );
// Return WC_Session to original state.
WC()->session = $old_session;
update_option( 'woocommerce_enable_guest_checkout', $enable_guest_checkout );
update_option( 'woocommerce_enable_signup_and_login_from_checkout', $enable_signup_login );
}

/**
* Test account creation options.
*/
public function test_checkout_force_create_account() {
$enable_guest_checkout = get_option( 'woocommerce_enable_guest_checkout' );
$enable_signup_login = get_option( 'woocommerce_enable_signup_and_login_from_checkout' );
// We need to replace the WC_Session with a mock because this test relies on cookies being set which
// is not easy with PHPUnit. This is a simpler approach.
$old_session = WC()->session;
Expand Down Expand Up @@ -537,8 +527,6 @@ public function test_checkout_force_create_account() {
update_option( 'woocommerce_enable_signup_and_login_from_checkout', $enable_signup_login );
// Return WC_Session to original state.
WC()->session = $old_session;
update_option( 'woocommerce_enable_guest_checkout', $enable_guest_checkout );
update_option( 'woocommerce_enable_signup_and_login_from_checkout', $enable_signup_login );
}

/**
Expand Down

0 comments on commit c803aca

Please sign in to comment.