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 03e71ac commit 707ba90
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 30 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 @@ -330,14 +330,6 @@ public function test_checkout_create_account() {
WC()->session = new MockSessionHandler();
WC()->session->init();

$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;
WC()->session = new MockSessionHandler();
WC()->session->init();

// Save settings to reset them later.
$enable_guest_checkout = get_option( 'woocommerce_enable_guest_checkout' );
$enable_signup_login = get_option( 'woocommerce_enable_signup_and_login_from_checkout' );
Expand Down Expand Up @@ -399,8 +391,6 @@ 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 );
}

/**
Expand All @@ -413,14 +403,6 @@ public function test_checkout_do_not_create_account() {
WC()->session = new MockSessionHandler();
WC()->session->init();

$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;
WC()->session = new MockSessionHandler();
WC()->session->init();

// Save settings to reset them later.
$enable_guest_checkout = get_option( 'woocommerce_enable_guest_checkout' );
$enable_signup_login = get_option( 'woocommerce_enable_signup_and_login_from_checkout' );
Expand Down Expand Up @@ -479,8 +461,6 @@ 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 );
}

/**
Expand All @@ -493,14 +473,6 @@ public function test_checkout_force_create_account() {
WC()->session = new MockSessionHandler();
WC()->session->init();

$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;
WC()->session = new MockSessionHandler();
WC()->session->init();

// Save settings to reset them later.
$enable_guest_checkout = get_option( 'woocommerce_enable_guest_checkout' );
$enable_signup_login = get_option( 'woocommerce_enable_signup_and_login_from_checkout' );
Expand Down Expand Up @@ -561,8 +533,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 707ba90

Please sign in to comment.