Skip to content

Commit fb5281f

Browse files
oprsenadir
authored andcommitted
Prevent orders being placed when no shipping options are available (#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
1 parent c8c7a95 commit fb5281f

File tree

1 file changed

+24
-0
lines changed
  • plugins/woocommerce/tests/php/src/Blocks/StoreApi/Routes

1 file changed

+24
-0
lines changed

plugins/woocommerce/tests/php/src/Blocks/StoreApi/Routes/Checkout.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,12 @@ public function test_checkout_create_account() {
334334
// Save settings to reset them later.
335335
$enable_guest_checkout = get_option( 'woocommerce_enable_guest_checkout' );
336336
$enable_signup_login = get_option( 'woocommerce_enable_signup_and_login_from_checkout' );
337+
// We need to replace the WC_Session with a mock because this test relies on cookies being set which
338+
// is not easy with PHPUnit. This is a simpler approach.
339+
$old_session = WC()->session;
340+
WC()->session = new MockSessionHandler();
341+
WC()->session->init();
342+
337343
update_option( 'woocommerce_enable_guest_checkout', 'yes' );
338344
update_option( 'woocommerce_enable_signup_and_login_from_checkout', 'yes' );
339345

@@ -392,6 +398,8 @@ public function test_checkout_create_account() {
392398
WC()->session = $old_session;
393399
update_option( 'woocommerce_enable_guest_checkout', $enable_guest_checkout );
394400
update_option( 'woocommerce_enable_signup_and_login_from_checkout', $enable_signup_login );
401+
// Return WC_Session to original state.
402+
WC()->session = $old_session;
395403
}
396404

397405
/**
@@ -407,6 +415,12 @@ public function test_checkout_do_not_create_account() {
407415
// Save settings to reset them later.
408416
$enable_guest_checkout = get_option( 'woocommerce_enable_guest_checkout' );
409417
$enable_signup_login = get_option( 'woocommerce_enable_signup_and_login_from_checkout' );
418+
// We need to replace the WC_Session with a mock because this test relies on cookies being set which
419+
// is not easy with PHPUnit. This is a simpler approach.
420+
$old_session = WC()->session;
421+
WC()->session = new MockSessionHandler();
422+
WC()->session->init();
423+
410424
update_option( 'woocommerce_enable_guest_checkout', 'yes' );
411425
update_option( 'woocommerce_enable_signup_and_login_from_checkout', 'yes' );
412426

@@ -462,6 +476,8 @@ public function test_checkout_do_not_create_account() {
462476
WC()->session = $old_session;
463477
update_option( 'woocommerce_enable_guest_checkout', $enable_guest_checkout );
464478
update_option( 'woocommerce_enable_signup_and_login_from_checkout', $enable_signup_login );
479+
// Return WC_Session to original state.
480+
WC()->session = $old_session;
465481
}
466482

467483
/**
@@ -477,6 +493,12 @@ public function test_checkout_force_create_account() {
477493
// Save settings to reset them later.
478494
$enable_guest_checkout = get_option( 'woocommerce_enable_guest_checkout' );
479495
$enable_signup_login = get_option( 'woocommerce_enable_signup_and_login_from_checkout' );
496+
// We need to replace the WC_Session with a mock because this test relies on cookies being set which
497+
// is not easy with PHPUnit. This is a simpler approach.
498+
$old_session = WC()->session;
499+
WC()->session = new MockSessionHandler();
500+
WC()->session->init();
501+
480502
update_option( 'woocommerce_enable_guest_checkout', 'no' );
481503
update_option( 'woocommerce_enable_signup_and_login_from_checkout', 'yes' );
482504

@@ -534,6 +556,8 @@ public function test_checkout_force_create_account() {
534556
WC()->session = $old_session;
535557
update_option( 'woocommerce_enable_guest_checkout', $enable_guest_checkout );
536558
update_option( 'woocommerce_enable_signup_and_login_from_checkout', $enable_signup_login );
559+
// Return WC_Session to original state.
560+
WC()->session = $old_session;
537561
}
538562

539563
/**

0 commit comments

Comments
 (0)