From f3fdbd7b59507b4ac890369e3efaeac0fc2ace13 Mon Sep 17 00:00:00 2001 From: Darren Ethier Date: Wed, 4 Nov 2020 21:22:43 -0500 Subject: [PATCH] Fix account creation bugs (https://github.com/woocommerce/woocommerce-blocks/pull/3371) * account for registration enabled setting when creating accounts * default to false instead of global setting for initial allowCreateAccount attribute * consider global allows signup value for whether account creation checkbox shows * include existing hook when determining if checkout signup is available: - woocommerce_checkout_registration_enabled hook * use public api for registration settings (via WC_Checkout) * initial round of test fixes * fix logic flaw exposed by tests and align tests with actual values in real use * return early if WC_Checkout is not available * remove temporary test group and uncomment new test. * improve test method name * fix comment to match new routine name * ensure block shows login prompt when store disables checkout signup * fix incorrect conditional order and cover with tests Co-authored-by: Rua Haszard --- .../cart-checkout/checkout/attributes.js | 7 +- .../js/blocks/cart-checkout/checkout/block.js | 7 +- .../js/blocks/cart-checkout/checkout/edit.js | 4 +- .../checkout/form/contact-fields-step.js | 8 ++- plugins/woocommerce-blocks/src/Assets.php | 11 ++- .../src/Domain/Services/CreateAccount.php | 13 +++- .../php/Domain/Services/CreateAccount.php | 67 +++++++++++++------ 7 files changed, 85 insertions(+), 32 deletions(-) diff --git a/plugins/woocommerce-blocks/assets/js/blocks/cart-checkout/checkout/attributes.js b/plugins/woocommerce-blocks/assets/js/blocks/cart-checkout/checkout/attributes.js index 0cffab21f9b..9abfbd5d06f 100644 --- a/plugins/woocommerce-blocks/assets/js/blocks/cart-checkout/checkout/attributes.js +++ b/plugins/woocommerce-blocks/assets/js/blocks/cart-checkout/checkout/attributes.js @@ -1,10 +1,7 @@ /** * External dependencies */ -import { - HAS_DARK_EDITOR_STYLE_SUPPORT, - CHECKOUT_ALLOWS_SIGNUP, -} from '@woocommerce/block-settings'; +import { HAS_DARK_EDITOR_STYLE_SUPPORT } from '@woocommerce/block-settings'; const blockAttributes = { isPreview: { @@ -22,7 +19,7 @@ const blockAttributes = { }, allowCreateAccount: { type: 'boolean', - default: CHECKOUT_ALLOWS_SIGNUP, + default: false, }, showApartmentField: { type: 'boolean', diff --git a/plugins/woocommerce-blocks/assets/js/blocks/cart-checkout/checkout/block.js b/plugins/woocommerce-blocks/assets/js/blocks/cart-checkout/checkout/block.js index 1c4ec7d145f..f2cd25eb3a2 100644 --- a/plugins/woocommerce-blocks/assets/js/blocks/cart-checkout/checkout/block.js +++ b/plugins/woocommerce-blocks/assets/js/blocks/cart-checkout/checkout/block.js @@ -23,7 +23,10 @@ import { Main, } from '@woocommerce/base-components/sidebar-layout'; import withScrollToTop from '@woocommerce/base-hocs/with-scroll-to-top'; -import { CHECKOUT_ALLOWS_GUEST } from '@woocommerce/block-settings'; +import { + CHECKOUT_ALLOWS_GUEST, + CHECKOUT_ALLOWS_SIGNUP, +} from '@woocommerce/block-settings'; import { compareWithWooVersion, getSetting } from '@woocommerce/settings'; /** @@ -102,7 +105,7 @@ const Checkout = ( { attributes, scrollToTop } ) => { ! isEditor && ! customerId && ! CHECKOUT_ALLOWS_GUEST && - ! allowCreateAccount + ! ( allowCreateAccount && CHECKOUT_ALLOWS_SIGNUP ) ) { return ( <> diff --git a/plugins/woocommerce-blocks/assets/js/blocks/cart-checkout/checkout/edit.js b/plugins/woocommerce-blocks/assets/js/blocks/cart-checkout/checkout/edit.js index 1e1e4a32c4b..13bb4cda399 100644 --- a/plugins/woocommerce-blocks/assets/js/blocks/cart-checkout/checkout/edit.js +++ b/plugins/woocommerce-blocks/assets/js/blocks/cart-checkout/checkout/edit.js @@ -17,6 +17,7 @@ import { PRIVACY_URL, TERMS_URL, CHECKOUT_PAGE_ID, + CHECKOUT_ALLOWS_SIGNUP, } from '@woocommerce/block-settings'; import { compareWithWooVersion, getAdminLink } from '@woocommerce/settings'; import { createInterpolateElement } from 'wordpress-element'; @@ -59,7 +60,8 @@ const BlockSettings = ( { attributes, setAttributes } ) => { // setting initial password. // Also implicitly gated to feature plugin, because Checkout // block is gated to plugin - const showCreateAccountOption = compareWithWooVersion( '4.7.0', '<=' ); + const showCreateAccountOption = + CHECKOUT_ALLOWS_SIGNUP && compareWithWooVersion( '4.7.0', '<=' ); return ( { currentPostId !== CHECKOUT_PAGE_ID && ( diff --git a/plugins/woocommerce-blocks/assets/js/blocks/cart-checkout/checkout/form/contact-fields-step.js b/plugins/woocommerce-blocks/assets/js/blocks/cart-checkout/checkout/form/contact-fields-step.js index 996e2eed211..9b1d2098c12 100644 --- a/plugins/woocommerce-blocks/assets/js/blocks/cart-checkout/checkout/form/contact-fields-step.js +++ b/plugins/woocommerce-blocks/assets/js/blocks/cart-checkout/checkout/form/contact-fields-step.js @@ -5,7 +5,10 @@ import { __ } from '@wordpress/i18n'; import { FormStep } from '@woocommerce/base-components/cart-checkout'; import { DebouncedValidatedTextInput } from '@woocommerce/base-components/text-input'; import { useCheckoutContext } from '@woocommerce/base-context'; -import { CHECKOUT_ALLOWS_GUEST } from '@woocommerce/block-settings'; +import { + CHECKOUT_ALLOWS_GUEST, + CHECKOUT_ALLOWS_SIGNUP, +} from '@woocommerce/block-settings'; import CheckboxControl from '@woocommerce/base-components/checkbox-control'; /** @@ -26,7 +29,8 @@ const ContactFieldsStep = ( { const createAccountUI = ! customerId && allowCreateAccount && - CHECKOUT_ALLOWS_GUEST && ( + CHECKOUT_ALLOWS_GUEST && + CHECKOUT_ALLOWS_SIGNUP && ( wc_privacy_policy_page_id(), 'terms' => wc_terms_and_conditions_page_id(), ]; + $checkout = WC()->checkout(); // Global settings used in each block. return array_merge( @@ -187,8 +188,14 @@ class Assets { 'privacy' => self::format_page_resource( $page_ids['privacy'] ), 'terms' => self::format_page_resource( $page_ids['terms'] ), ], - 'checkoutAllowsGuest' => filter_var( get_option( 'woocommerce_enable_guest_checkout' ), FILTER_VALIDATE_BOOLEAN ), - 'checkoutAllowsSignup' => filter_var( get_option( 'woocommerce_enable_signup_and_login_from_checkout' ), FILTER_VALIDATE_BOOLEAN ), + 'checkoutAllowsGuest' => $checkout instanceof \WC_Checkout && false === filter_var( + $checkout->is_registration_required(), + FILTER_VALIDATE_BOOLEAN + ), + 'checkoutAllowsSignup' => $checkout instanceof \WC_Checkout && filter_var( + $checkout->is_registration_enabled(), + FILTER_VALIDATE_BOOLEAN + ), 'baseLocation' => wc_get_base_location(), 'woocommerceBlocksPhase' => WOOCOMMERCE_BLOCKS_PHASE, 'hasDarkEditorStyleSupport' => current_theme_supports( 'dark-editor-style' ), diff --git a/plugins/woocommerce-blocks/src/Domain/Services/CreateAccount.php b/plugins/woocommerce-blocks/src/Domain/Services/CreateAccount.php index 49a5623bba9..0a81d490a95 100644 --- a/plugins/woocommerce-blocks/src/Domain/Services/CreateAccount.php +++ b/plugins/woocommerce-blocks/src/Domain/Services/CreateAccount.php @@ -139,8 +139,19 @@ class CreateAccount { } // From here we know that the shopper is not logged in. + // check for whether account creation is enabled at the global level. + $checkout = WC()->checkout(); + if ( ! $checkout instanceof \WC_Checkout ) { + // If checkout class is not available, we have major problems, don't create account. + return false; + } - if ( false === filter_var( get_option( 'woocommerce_enable_guest_checkout' ), FILTER_VALIDATE_BOOLEAN ) ) { + if ( false === filter_var( $checkout->is_registration_enabled(), FILTER_VALIDATE_BOOLEAN ) ) { + // Registration is not enabled for the store, so return false. + return false; + } + + if ( true === filter_var( $checkout->is_registration_required(), FILTER_VALIDATE_BOOLEAN ) ) { // Store requires an account for all checkouts (purchases). // Create an account independent of shopper option in $request. // Note - checkbox is not displayed to shopper in this case. diff --git a/plugins/woocommerce-blocks/tests/php/Domain/Services/CreateAccount.php b/plugins/woocommerce-blocks/tests/php/Domain/Services/CreateAccount.php index e57da27c7ac..04fd9acca25 100644 --- a/plugins/woocommerce-blocks/tests/php/Domain/Services/CreateAccount.php +++ b/plugins/woocommerce-blocks/tests/php/Domain/Services/CreateAccount.php @@ -36,11 +36,14 @@ class CreateAccount extends WP_UnitTestCase { /// -- Test-specific setup start. $tmp_enable_guest_checkout = get_option( 'woocommerce_enable_guest_checkout' ); - $enable_guest_checkout = array_key_exists( 'enable_guest_checkout', $options ) ? $options['enable_guest_checkout'] : false; + $tmp_can_register = get_option('woocommerce_enable_signup_and_login_from_checkout'); + $enable_guest_checkout = array_key_exists( 'enable_guest_checkout', $options ) ? $options['enable_guest_checkout'] : 'no'; + $can_register = array_key_exists( 'can_register', $options ) ? $options['can_register'] : 'yes'; update_option( 'woocommerce_enable_guest_checkout', $enable_guest_checkout ); + update_option( 'woocommerce_enable_signup_and_login_from_checkout', $can_register ); $test_request = new \WP_REST_Request(); - $should_create_account = array_key_exists( 'should_create_account', $options ) ? $options['should_create_account'] : false; + $should_create_account = array_key_exists( 'should_create_account', $options ) ? $options['should_create_account'] : 'no'; $test_request->set_param( 'should_create_account', $should_create_account ); $test_request->set_param( 'billing_address', [ 'email' => $email, @@ -57,6 +60,7 @@ class CreateAccount extends WP_UnitTestCase { /// -- Undo test-specific setup; restore previous state. update_option( 'woocommerce_enable_guest_checkout', $tmp_enable_guest_checkout ); + update_option( 'woocommerce_enable_signup_and_login_from_checkout', $tmp_can_register ); return [ 'user_id' => $user_id, @@ -98,8 +102,8 @@ class CreateAccount extends WP_UnitTestCase { 'Mary', 'Jones', [ - 'should_create_account' => true, - 'enable_guest_checkout' => true, + 'should_create_account' => 'yes', + 'enable_guest_checkout' => 'yes', ], ], // User requested an account + site doesn't allow guest. @@ -108,8 +112,8 @@ class CreateAccount extends WP_UnitTestCase { 'Mary', 'Jones', [ - 'should_create_account' => true, - 'enable_guest_checkout' => false, + 'should_create_account' => 'yes', + 'enable_guest_checkout' => 'no', ], ], // User requested an account; name fields are not required. @@ -118,8 +122,8 @@ class CreateAccount extends WP_UnitTestCase { '', '', [ - 'should_create_account' => true, - 'enable_guest_checkout' => true, + 'should_create_account' => 'yes', + 'enable_guest_checkout' => 'yes', ], ], // Store does not allow guest - signup is required (automatic). @@ -128,8 +132,8 @@ class CreateAccount extends WP_UnitTestCase { 'Henry', 'Kissinger', [ - 'should_create_account' => false, - 'enable_guest_checkout' => false, + 'should_create_account' => 'no', + 'enable_guest_checkout' => 'no', ], ], ]; @@ -150,8 +154,8 @@ class CreateAccount extends WP_UnitTestCase { 'Mary', 'Jones', [ - 'should_create_account' => true, - 'enable_guest_checkout' => true, + 'should_create_account' => 'yes', + 'enable_guest_checkout' => 'yes', ], ); } @@ -169,8 +173,8 @@ class CreateAccount extends WP_UnitTestCase { 'Mary', 'Jones', [ - 'should_create_account' => true, - 'enable_guest_checkout' => true, + 'should_create_account' => 'yes', + 'enable_guest_checkout' => 'yes', ], ); } @@ -185,18 +189,43 @@ class CreateAccount extends WP_UnitTestCase { } /** - * Test that a user is not created if not requested (and the site allows guest checkout). + * Test cases where a user should not be created (no signup should occur). */ - public function test_no_account_requested() { + public function test_no_account_created() { $site_user_counts = count_users(); - $result = $this->execute_create_customer_from_order( + $this->execute_create_customer_from_order( 'maryjones@testperson.net', 'Mary', 'Jones', [ - 'should_create_account' => false, - 'enable_guest_checkout' => true, + 'should_create_account' => 'no', + 'enable_guest_checkout' => 'yes', + ], + ); + + + // test with explicitly turning off global registration + $this->execute_create_customer_from_order( + 'maryjones@testperson.net', + 'Mary', + 'Jones', + [ + 'can_register' => 'no', + 'should_create_account' => 'yes', + 'enable_guest_checkout' => 'yes', + ], + ); + + // test with guest checkout off and global registration off. + $this->execute_create_customer_from_order( + 'maryjones@testperson.net', + 'Mary', + 'Jones', + [ + 'can_register' => 'no', + 'should_create_account' => 'yes', + 'enable_guest_checkout' => 'no', ], );