diff --git a/core/modules/user/src/AccountForm.php b/core/modules/user/src/AccountForm.php index b35a0f9..1a38a2d 100644 --- a/core/modules/user/src/AccountForm.php +++ b/core/modules/user/src/AccountForm.php @@ -415,6 +415,7 @@ public function validateForm(array &$form, FormStateInterface $form_state) { $mail_taken = FALSE; $account = $this->entity; $name = $form_state->getValue('name'); + $mail = $form_state->getValue('mail'); // For new registrations, make sure the username does not conflict with // an existing user's email address. @@ -455,35 +456,37 @@ public function validateForm(array &$form, FormStateInterface $form_state) { // For new registrations, make sure the email address does not conflict // with an existing user's username. - if ($account->isAuthenticated()) { - $mail_taken = (bool) db_select('users_field_data', 'ufd') - ->condition( - db_or() - ->condition('ufd.mail', db_like($form_state->getValue('mail')), 'LIKE') - ->condition('ufd.name', db_like($form_state->getValue('mail')), 'LIKE') - ) - ->condition('ufd.uid', $account->id(), '<>') - ->range(0, 1) - ->countQuery() - ->execute() - ->fetchField(); - } - else { - $mail_taken = (bool) db_select('users_field_data', 'ufd') - ->condition( - db_or() - ->condition('ufd.mail', db_like($form_state->getValue('mail')), 'LIKE') - ->condition('ufd.name', db_like($form_state->getValue('mail')), 'LIKE') - ) - ->condition('ufd.status', 1) - ->range(0, 1) - ->countQuery() - ->execute() - ->fetchField(); - } + if (!empty($mail)) { + if ($account->isAuthenticated()) { + $mail_taken = (bool) db_select('users_field_data', 'ufd') + ->condition( + db_or() + ->condition('ufd.mail', db_like($form_state->getValue('mail')), 'LIKE') + ->condition('ufd.name', db_like($form_state->getValue('mail')), 'LIKE') + ) + ->condition('ufd.uid', $account->id(), '<>') + ->range(0, 1) + ->countQuery() + ->execute() + ->fetchField(); + } + else { + $mail_taken = (bool) db_select('users_field_data', 'ufd') + ->condition( + db_or() + ->condition('ufd.mail', db_like($form_state->getValue('mail')), 'LIKE') + ->condition('ufd.name', db_like($form_state->getValue('mail')), 'LIKE') + ) + ->condition('ufd.status', 1) + ->range(0, 1) + ->countQuery() + ->execute() + ->fetchField(); + } - if ($mail_taken) { - $form_state->setErrorByName('mail', $this->t('The email address @email is already registered.', array('@email' => $form_state->getValue('mail')))); + if ($mail_taken) { + $form_state->setErrorByName('mail', $this->t('The email address @email is already registered.', array('@email' => $form_state->getValue('mail')))); + } } } diff --git a/core/modules/user/src/Tests/UserEditTest.php b/core/modules/user/src/Tests/UserEditTest.php index 83c7ecd..457c169 100644 --- a/core/modules/user/src/Tests/UserEditTest.php +++ b/core/modules/user/src/Tests/UserEditTest.php @@ -103,29 +103,4 @@ function testUserWithoutEmailEdit() { $this->assertRaw(t("The changes have been saved.")); } - /** - * Check that existing users whose username matches another user's email - * address or vice versa are not forced to update their username or email - * address. - */ - function testUserEditWithConflicts() { - $user_with_email = $this->drupalCreateUser(); - $user_with_name = $this->drupalCreateUser(); - - // Change the second user's username to the same value as the first user's - // email address. - $user_with_name->name = $user_with_email->mail; - $user_with_name->save(); - - // Test that the first user can save their account with no errors. - $this->drupalLogin($user_with_email); - $this->drupalPostForm("user/" . $user_with_email->id() . "/edit", array(), t('Save')); - $this->assertText(t("The changes have been saved."), "The user does not need to change their username if it matches another user's email address."); - $this->drupalLogout(); - - // Test that the second user can save their account with no errors. - $this->drupalLogin($user_with_name); - $this->drupalPostForm("user/" . $user_with_name->id() . "/edit", array(), t('Save')); - $this->assertText(t("The changes have been saved."), "The user does not need to change their email address if it matches another user's username."); - } }