function UserRegistrationTest::testUniqueFields

Same name in other branches
  1. 9 core/modules/user/tests/src/Functional/UserRegistrationTest.php \Drupal\Tests\user\Functional\UserRegistrationTest::testUniqueFields()
  2. 10 core/modules/user/tests/src/Functional/UserRegistrationTest.php \Drupal\Tests\user\Functional\UserRegistrationTest::testUniqueFields()
  3. 11.x 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(),
    ]));
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.