diff --git a/core/modules/user/lib/Drupal/user/AccountFormController.php b/core/modules/user/lib/Drupal/user/AccountFormController.php index 6002298..2f91f60 100644 --- a/core/modules/user/lib/Drupal/user/AccountFormController.php +++ b/core/modules/user/lib/Drupal/user/AccountFormController.php @@ -308,9 +308,13 @@ public function validate(array $form, array &$form_state) { parent::validate($form, $form_state); $account = $this->entity; + $mail = $form_state['values']['mail']; + // Validate new or changing username. if (isset($form_state['values']['name'])) { - if ($error = user_validate_name($form_state['values']['name'])) { + $name = $form_state['values']['name']; + + if ($error = user_validate_name($name)) { $this->setFormError('name', $form_state, $error); } // Cast the user ID as an integer. It might have been set to NULL, which @@ -318,19 +322,23 @@ public function validate(array $form, array &$form_state) { else { $name_taken = (bool) $this->entityQuery->get('user') ->condition('uid', (int) $account->id(), '<>') - ->condition('name', $form_state['values']['name']) + ->condition('name', $name) ->range(0, 1) ->count() ->execute(); if ($name_taken) { - $this->setFormError('name', $form_state, $this->t('The name %name is already taken.', array('%name' => $form_state['values']['name']))); + $this->setFormError('name', $form_state, $this->t('The name %name is already taken.', array('%name' => $name))); + } + + // Check whether the user name provided is an email address, if so, make + // sure it matches the mail value. + if (filter_var($name, FILTER_VALIDATE_EMAIL) && ($name !== $mail)) { + $this->setFormError('name', $form_state, $this->t('If your username is an email address, it must match your email.')); } } } - $mail = $form_state['values']['mail']; - if (!empty($mail)) { $mail_taken = (bool) $this->entityQuery->get('user') ->condition('uid', (int) $account->id(), '<>') diff --git a/core/modules/user/lib/Drupal/user/Tests/UserEditedOwnAccountTest.php b/core/modules/user/lib/Drupal/user/Tests/UserEditedOwnAccountTest.php index 50f1081b..7726168 100644 --- a/core/modules/user/lib/Drupal/user/Tests/UserEditedOwnAccountTest.php +++ b/core/modules/user/lib/Drupal/user/Tests/UserEditedOwnAccountTest.php @@ -47,14 +47,15 @@ function testUserEditedOwnAccount() { $edit['name'] = $this->randomName() . '@example.com'; $this->drupalPostForm('user/' . $account->id() . '/edit', $edit, t('Save')); $this->assertText(t('If your username is an email address, it must match your email.'), 'Error message found when an email username does not match user email.'); + $this->assertNoText(t('The changes have been saved.'), 'The user account was not saved.'); // Lookup user by name to make sure we didn't actually change the name. - $accounts = entity_load_multiple_by_properties('user', array('name' => $edit['name'])); + $accounts = \Drupal::entityManager()->getStorageController('user')->loadByProperties(array('name' => $edit['name'])); $this->assertTrue(empty($accounts), 'Username was not changed to email address other than my own.'); // Change username to my email address. $edit['name'] = $account->getEmail(); $this->drupalPostForm('user/' . $account->id() . '/edit', $edit, t('Save')); - + $this->assertText(t('The changes have been saved.'), 'The user account was saved.'); } } diff --git a/core/modules/user/lib/Drupal/user/Tests/UserRegistrationTest.php b/core/modules/user/lib/Drupal/user/Tests/UserRegistrationTest.php index b7ce21f..240eb53 100644 --- a/core/modules/user/lib/Drupal/user/Tests/UserRegistrationTest.php +++ b/core/modules/user/lib/Drupal/user/Tests/UserRegistrationTest.php @@ -175,12 +175,14 @@ function testRegistrationEmailAsUsername() { $this->drupalPostForm('user/register', $edit, t('Create new account')); $this->assertText(t('If your username is an email address, it must match your email.'), 'Email username does not match user email error message found.'); $this->assertNoText(t('Registration successful. You are now logged in.'), 'The user was not created and logged in.'); - $this->drupalLogout(); // Attempt to create new account using matching email address. $edit['name'] = $edit['mail'] = $this->randomName() . '@example.com'; + $this->drupalPostForm('user/register', $edit, t('Create new account')); - $accounts = entity_load_multiple_by_properties('user', array('name' => $edit['name'])); + $this->assertText(t('Registration successful. You are now logged in.'), 'The user was created and logged in with matching email.'); + + $accounts = \Drupal::entityManager()->getStorageController('user')->loadByProperties(array('name' => $edit['name'])); $new_user = reset($accounts); $this->assertTrue(($new_user->getUsername() === $edit['name']) && ($new_user->getEmail() === $edit['mail']), 'Created user with matching username and email address.');