You are here

function UserValidationTest::testUsernames in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/user/src/Tests/UserValidationTest.php \Drupal\user\Tests\UserValidationTest::testUsernames()

Tests user name validation.

File

core/modules/user/src/Tests/UserValidationTest.php, line 47
Contains \Drupal\user\Tests\UserValidationTest.

Class

UserValidationTest
Verify that user validity checks behave as designed.

Namespace

Drupal\user\Tests

Code

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