You are here

public function UserRegistrationTest::testUniqueFields in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/user/tests/src/Functional/UserRegistrationTest.php \Drupal\Tests\user\Functional\UserRegistrationTest::testUniqueFields()

Tests username and email field constraints on user registration.

See also

\Drupal\user\Plugin\Validation\Constraint\UserNameUnique

\Drupal\user\Plugin\Validation\Constraint\UserMailUnique

File

core/modules/user/tests/src/Functional/UserRegistrationTest.php, line 289

Class

UserRegistrationTest
Tests registration of user under different configurations.

Namespace

Drupal\Tests\user\Functional

Code

public function testUniqueFields() {
  $account = $this
    ->drupalCreateUser();
  $edit = [
    'mail' => 'test@example.com',
    'name' => $account
      ->getAccountName(),
  ];
  $this
    ->drupalGet('user/register');
  $this
    ->submitForm($edit, 'Create new account');
  $this
    ->assertSession()
    ->pageTextContains("The username {$account->getAccountName()} is already taken.");
  $edit = [
    'mail' => $account
      ->getEmail(),
    'name' => $this
      ->randomString(),
  ];
  $this
    ->drupalGet('user/register');
  $this
    ->submitForm($edit, 'Create new account');
  $this
    ->assertSession()
    ->pageTextContains("The email address {$account->getEmail()} is already taken.");
}