* 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 <rua.haszard@automattic.com>
This commit is contained in:
Darren Ethier 2020-11-04 21:22:43 -05:00 committed by GitHub
parent f11cd7a50f
commit f3fdbd7b59
7 changed files with 85 additions and 32 deletions

View File

@ -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',

View File

@ -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 (
<>

View File

@ -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 (
<InspectorControls>
{ currentPostId !== CHECKOUT_PAGE_ID && (

View File

@ -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 && (
<CheckboxControl
className="wc-block-checkout__create-account"
label={ __(

View File

@ -142,6 +142,7 @@ class Assets {
'privacy' => 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' ),

View File

@ -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.

View File

@ -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',
],
);