Skip to content

Commit 8591ea1

Browse files
authored
Merge pull request #381 from kidunot89/feature/register-customer-without-username
Make the username field optional in registerCustomer mutation
2 parents d2315ad + 4d24037 commit 8591ea1

File tree

2 files changed

+56
-9
lines changed

2 files changed

+56
-9
lines changed

includes/mutation/class-customer-register.php

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public static function register_mutation() {
4343
* @return array
4444
*/
4545
public static function get_input_fields() {
46-
return array_merge(
46+
$result = array_merge(
4747
UserRegister::get_input_fields(),
4848
array(
4949
'billing' => array(
@@ -59,7 +59,12 @@ public static function get_input_fields() {
5959
'description' => __( 'Customer shipping is identical to billing address', 'wp-graphql-woocommerce' ),
6060
),
6161
)
62-
);
62+
);
63+
64+
// Make the username field optional.
65+
$result['username']['type'] = 'String';
66+
67+
return $result;
6368
}
6469

6570
/**
@@ -93,9 +98,6 @@ public static function get_output_fields() {
9398
public static function mutate_and_get_payload() {
9499
return function( $input, AppContext $context, ResolveInfo $info ) {
95100
// Validate input.
96-
if ( empty( $input['username'] ) ) {
97-
throw new UserError( __( 'Please enter a valid account username.', 'wp-graphql-woocommerce' ) );
98-
}
99101
if ( empty( $input['email'] ) ) {
100102
throw new UserError( __( 'Please provide a valid email address.', 'wp-graphql-woocommerce' ) );
101103
}
@@ -114,10 +116,10 @@ public static function mutate_and_get_payload() {
114116
$user_args = UserMutation::prepare_user_object( $input, 'registerCustomer' );
115117

116118
// Create the user using native WooCommerce function.
117-
$user_id = wc_create_new_customer(
119+
$user_id = \wc_create_new_customer(
118120
$user_args['user_email'],
119-
$user_args['user_login'],
120-
isset($user_args['user_pass']) ? $user_args['user_pass'] : '',
121+
isset( $user_args['user_login'] ) ? $user_args['user_login'] : '',
122+
isset( $user_args['user_pass'] ) ? $user_args['user_pass'] : '',
121123
$user_args
122124
);
123125

tests/wpunit/CustomerMutationsTest.php

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,6 @@ private function updateCustomer( $input ) {
201201
return $actual;
202202
}
203203

204-
// tests
205204
public function testRegisterMutationWithoutCustomerInfo() {
206205
/**
207206
* Assertion One
@@ -518,4 +517,50 @@ public function testUpdateMutationWithShippingSameAsBilling() {
518517

519518
$this->assertEquals( $expected, $actual );
520519
}
520+
521+
public function testRegisterMutationWithoutAnyInfo() {
522+
/**
523+
* Assertion One
524+
*
525+
* Tests mutation without a providing an username and password.
526+
*/
527+
$actual = $this->registerCustomer(
528+
array(
529+
'clientMutationId' => 'someId',
530+
'email' => $this->email,
531+
'firstName' => $this->first_name,
532+
'lastName' => $this->last_name,
533+
)
534+
);
535+
536+
// use --debug flag to view.
537+
codecept_debug( $actual );
538+
539+
$user = get_user_by( 'email', '[email protected]' );
540+
$this->assertTrue( is_a( $user, WP_User::class ) );
541+
542+
$expected = array(
543+
'data' => array(
544+
'registerCustomer' => array(
545+
'clientMutationId' => 'someId',
546+
'authToken' => \WPGraphQL\JWT_Authentication\Auth::get_token( $user ),
547+
'refreshToken' => \WPGraphQL\JWT_Authentication\Auth::get_refresh_token( $user ),
548+
'customer' => array(
549+
'databaseId' => $user->ID,
550+
'email' => $this->email,
551+
'username' => $user->user_login,
552+
'firstName' => $this->first_name,
553+
'lastName' => $this->last_name,
554+
'billing' => $this->empty_billing(),
555+
'shipping' => $this->empty_shipping(),
556+
),
557+
'viewer' => array(
558+
'userId' => $user->ID,
559+
)
560+
),
561+
),
562+
);
563+
564+
$this->assertEquals( $expected, $actual );
565+
}
521566
}

0 commit comments

Comments
 (0)