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 c8c7a95 commit fb5281f
Showing 1 changed file with 24 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,12 @@ public function test_checkout_create_account() {
// 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' );
// 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();

update_option( 'woocommerce_enable_guest_checkout', 'yes' );
update_option( 'woocommerce_enable_signup_and_login_from_checkout', 'yes' );

Expand Down Expand Up @@ -392,6 +398,8 @@ public function test_checkout_create_account() {
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 );
// Return WC_Session to original state.
WC()->session = $old_session;
}

/**
Expand All @@ -407,6 +415,12 @@ public function test_checkout_do_not_create_account() {
// 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' );
// 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();

update_option( 'woocommerce_enable_guest_checkout', 'yes' );
update_option( 'woocommerce_enable_signup_and_login_from_checkout', 'yes' );

Expand Down Expand Up @@ -462,6 +476,8 @@ public function test_checkout_do_not_create_account() {
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 );
// Return WC_Session to original state.
WC()->session = $old_session;
}

/**
Expand All @@ -477,6 +493,12 @@ public function test_checkout_force_create_account() {
// 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' );
// 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();

update_option( 'woocommerce_enable_guest_checkout', 'no' );
update_option( 'woocommerce_enable_signup_and_login_from_checkout', 'yes' );

Expand Down Expand Up @@ -534,6 +556,8 @@ public function test_checkout_force_create_account() {
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 );
// Return WC_Session to original state.
WC()->session = $old_session;
}

/**
Expand Down

0 comments on commit fb5281f

Please sign in to comment.