diff --git a/core/modules/user/src/Tests/UserRegistrationTest.php b/core/modules/user/src/Tests/UserRegistrationTest.php index bba6849..3ed2682 100644 --- a/core/modules/user/src/Tests/UserRegistrationTest.php +++ b/core/modules/user/src/Tests/UserRegistrationTest.php @@ -8,6 +8,7 @@ namespace Drupal\user\Tests; use Drupal\Core\Entity\Entity\EntityFormDisplay; +use Drupal\Component\Utility\SafeMarkup; use Drupal\Core\Field\FieldStorageDefinitionInterface; use Drupal\field\Entity\FieldConfig; use Drupal\field\Entity\FieldStorageConfig; @@ -265,6 +266,23 @@ function testRegistrationDefaultValues() { } /** + * Tests registration errors when trying to register with an existing user + * name or an existing email address. + * + * @see \Drupal\user\Plugin\Validation\Constraint\UserNameUnique + * @see \Drupal\user\Plugin\Validation\Constraint\UserMailUnique + */ + public function testUniqueFields() { + $account = $this->drupalCreateUser(); + + $this->drupalPostForm('user/register', ['mail' => 'test@example.com', 'name' => $account->getUsername()], t('Create new account')); + $this->assertRaw(Safemarkup::format('The username %value is already taken.', ['%value' => $account->getUsername()])); + + $this->drupalPostForm('user/register', ['mail' => $account->getEmail(), 'name' => $this->randomString()], t('Create new account')); + $this->assertRaw(Safemarkup::format('The email address %value is already taken.', ['%value' => $account->getEmail()])); + } + + /** * Tests Field API fields on user registration forms. */ function testRegistrationWithUserFields() { diff --git a/core/modules/user/src/UserAccessControlHandler.php b/core/modules/user/src/UserAccessControlHandler.php index 4b29b4e..416cb9a 100644 --- a/core/modules/user/src/UserAccessControlHandler.php +++ b/core/modules/user/src/UserAccessControlHandler.php @@ -81,7 +81,8 @@ protected function checkFieldAccess($operation, FieldDefinitionInterface $field_ switch ($field_definition->getName()) { case 'name': // Allow view access to anyone with access to the entity. - if ($operation == 'view') { + // Anonymous users should be able to change their username. + if ($operation == 'view' || ($items && $account->isAnonymous() && $items->getEntity()->isAnonymous())) { return AccessResult::allowed()->cachePerPermissions(); } // Allow edit access for the own user name if the permission is