function UserValidationTest::testUsernames

Same name in other branches
  1. 8.9.x core/modules/user/tests/src/Kernel/UserValidationTest.php \Drupal\Tests\user\Kernel\UserValidationTest::testUsernames()
  2. 10 core/modules/user/tests/src/Kernel/UserValidationTest.php \Drupal\Tests\user\Kernel\UserValidationTest::testUsernames()
  3. 11.x core/modules/user/tests/src/Kernel/UserValidationTest.php \Drupal\Tests\user\Kernel\UserValidationTest::testUsernames()

Tests user name validation.

File

core/modules/user/tests/src/Kernel/UserValidationTest.php, line 43

Class

UserValidationTest
Verify that user validity checks behave as designed.

Namespace

Drupal\Tests\user\Kernel

Code

public function testUsernames() {
    // cSpell:disable
    $test_cases = [
        // '<username>' => ['<description>', 'assert<testName>'].
'foo' => [
            'Valid username',
            'assertNull',
        ],
        'FOO' => [
            'Valid username',
            'assertNull',
        ],
        'Foo O\'Bar' => [
            'Valid username',
            'assertNull',
        ],
        'foo@bar' => [
            'Valid username',
            'assertNull',
        ],
        'foo@example.com' => [
            'Valid username',
            'assertNull',
        ],
        // invalid domains are allowed in usernames.
'foo@-example.com' => [
            'Valid username',
            'assertNull',
        ],
        'þòøÇߪř€' => [
            'Valid username',
            'assertNull',
        ],
        // '+' symbol is allowed.
'foo+bar' => [
            'Valid username',
            'assertNull',
        ],
        // runes.
'ᚠᛇᚻ᛫ᛒᛦᚦ' => [
            'Valid UTF8 username',
            'assertNull',
        ],
        ' foo' => [
            'Invalid username that starts with a space',
            'assertNotNull',
        ],
        'foo ' => [
            'Invalid username that ends with a space',
            'assertNotNull',
        ],
        'foo  bar' => [
            'Invalid username that contains 2 spaces \'&nbsp;&nbsp;\'',
            'assertNotNull',
        ],
        '' => [
            'Invalid empty username',
            'assertNotNull',
        ],
        'foo/' => [
            'Invalid username containing invalid chars',
            'assertNotNull',
        ],
        // NULL.
'foo' . chr(0) . 'bar' => [
            'Invalid username containing chr(0)',
            'assertNotNull',
        ],
        // CR.
'foo' . chr(13) . 'bar' => [
            'Invalid username containing chr(13)',
            'assertNotNull',
        ],
        str_repeat('x', UserInterface::USERNAME_MAX_LENGTH + 1) => [
            'Invalid excessively long username',
            'assertNotNull',
        ],
    ];
    // cSpell:enable
    foreach ($test_cases as $name => $test_case) {
        [
            $description,
            $test,
        ] = $test_case;
        $result = user_validate_name($name);
        $this->{$test}($result, $description . ' (' . $name . ')');
    }
}

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