class UserNameValidatorTest

Same name and namespace in other branches
  1. 11.x core/modules/user/tests/src/Kernel/UserNameValidatorTest.php \Drupal\Tests\user\Kernel\UserNameValidatorTest

Verify that user validity checks behave as designed.

@group user

Hierarchy

Expanded class hierarchy of UserNameValidatorTest

File

core/modules/user/tests/src/Kernel/UserNameValidatorTest.php, line 16

Namespace

Drupal\Tests\user\Kernel
View source
class UserNameValidatorTest extends KernelTestBase {
  
  /**
   * {@inheritdoc}
   */
  protected static $modules = [
    'user',
  ];
  
  /**
   * The user validator under test.
   */
  protected UserNameValidator $userValidator;
  
  /**
   * {@inheritdoc}
   */
  protected function setUp() : void {
    parent::setUp();
    $this->userValidator = $this->container
      ->get('user.name_validator');
  }
  
  /**
   * Tests valid user name validation.
   *
   * @dataProvider validUsernameProvider
   */
  public function testValidUsernames($name) : void {
    $violations = $this->userValidator
      ->validateName($name);
    $this->assertEmpty($violations);
  }
  
  /**
   * Tests invalid user name validation.
   *
   * @dataProvider invalidUserNameProvider
   */
  public function testInvalidUsernames($name, $expectedMessage) : void {
    $violations = $this->userValidator
      ->validateName($name);
    $this->assertNotEmpty($violations);
    $this->assertEquals($expectedMessage, $violations[0]->getMessage());
  }
  
  /**
   * Provides valid user names.
   */
  public static function validUsernameProvider() : array {
    // cSpell:disable
    return [
      'lowercase' => [
        'foo',
      ],
      'uppercase' => [
        'FOO',
      ],
      'contains space' => [
        'Foo O\'Bar',
      ],
      'contains @' => [
        'foo@bar',
      ],
      'allow email' => [
        'foo@example.com',
      ],
      'allow invalid domain' => [
        'foo@-example.com',
      ],
      'allow special chars' => [
        'þòøÇߪř€',
      ],
      'allow plus' => [
        'foo+bar',
      ],
      'utf8 runes' => [
        'ᚠᛇᚻ᛫ᛒᛦᚦ',
      ],
    ];
    // cSpell:enable
  }
  
  /**
   * Provides invalid user names.
   */
  public static function invalidUserNameProvider() : array {
    return [
      'starts with space' => [
        ' foo',
        'The username cannot begin with a space.',
      ],
      'ends with space' => [
        'foo ',
        'The username cannot end with a space.',
      ],
      'contains 2 spaces' => [
        'foo  bar',
        'The username cannot contain multiple spaces in a row.',
      ],
      'empty string' => [
        '',
        'You must enter a username.',
      ],
      'invalid chars' => [
        'foo/',
        'The username contains an illegal character.',
      ],
      // NULL.
'contains chr(0)' => [
        'foo' . chr(0) . 'bar',
        'The username contains an illegal character.',
      ],
      // CR.
'contains chr(13)' => [
        'foo' . chr(13) . 'bar',
        'The username contains an illegal character.',
      ],
      'excessively long' => [
        str_repeat('x', UserInterface::USERNAME_MAX_LENGTH + 1),
        'The username xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx is too long: it must be 60 characters or less.',
      ],
    ];
  }

}

Members

Title Sort descending Modifiers Object type Summary
UserNameValidatorTest::$modules protected static property Modules to install.
UserNameValidatorTest::$userValidator protected property The user validator under test.
UserNameValidatorTest::invalidUserNameProvider public static function Provides invalid user names.
UserNameValidatorTest::setUp protected function
UserNameValidatorTest::testInvalidUsernames public function Tests invalid user name validation.
UserNameValidatorTest::testValidUsernames public function Tests valid user name validation.
UserNameValidatorTest::validUsernameProvider public static function Provides valid user names.

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