function UserValidationTest::testUsernames

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

Tests user name validation.

@group legacy

File

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

Class

UserValidationTest
Verify that user validity checks behave as designed.

Namespace

Drupal\Tests\user\Kernel

Code

public function testUsernames() : void {
    // 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',
        ],
    ];
    $this->expectDeprecation('user_validate_name() is deprecated in drupal:10.3.0 and is removed from drupal:12.0.0. Use \\Drupal\\user\\UserValidator::validateName() instead. See https://www.drupal.org/node/3431205');
    // 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.