You are here

public function UserRegistrationTest::testUniqueFields in Drupal 8

Same name and namespace in other branches
  1. 9 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 278

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
    ->drupalPostForm('user/register', $edit, t('Create new account'));
  $this
    ->assertRaw(new FormattableMarkup('The username %value is already taken.', [
    '%value' => $account
      ->getAccountName(),
  ]));
  $edit = [
    'mail' => $account
      ->getEmail(),
    'name' => $this
      ->randomString(),
  ];
  $this
    ->drupalPostForm('user/register', $edit, t('Create new account'));
  $this
    ->assertRaw(new FormattableMarkup('The email address %value is already taken.', [
    '%value' => $account
      ->getEmail(),
  ]));
}