public static function UnitTestCase::randomName

Generates a random string containing letters and numbers.

The string will always start with a letter. The letters may be upper or lower case. This method is better for restricted inputs that do not accept certain characters. For example, when testing input fields that require machine readable values (i.e. without spaces and non-standard characters) this method is best.

Do not use this method when testing unvalidated user input. Instead, use Drupal\simpletest\TestBase::randomString().

Parameters

int $length: Length of random string to generate.

Return value

string Randomly generated string.

See also

Drupal\simpletest\TestBase::randomString()

5 calls to UnitTestCase::randomName()
FileStorageTest::testReadOnly in drupal/core/tests/Drupal/Tests/Component/PhpStorage/FileStorageTest.php
Tests writing with one class and reading with another.
HttpKernelTest::testSetupSubrequest in drupal/core/tests/Drupal/Tests/Core/HttpKernelTest.php
Tests the forward method.
MTimeProtectedFileStorageTest::setUp in drupal/core/tests/Drupal/Tests/Component/PhpStorage/MTimeProtectedFileStorageTest.php
Overrides \Drupal\Tests\UnitTestCase::setUp()
NullBackendTest::testNullBackend in drupal/core/tests/Drupal/Tests/Core/Cache/NullBackendTest.php
Tests that the NullBackend does not actually store variables.
PhpStorageTestBase::assertCRUD in drupal/core/tests/Drupal/Tests/Component/PhpStorage/PhpStorageTestBase.php
Assert that a PHP storage controller's load/save/delete operations work.

File

drupal/core/tests/Drupal/Tests/UnitTestCase.php, line 55
Contains \Drupal\Tests\UnitTestCase.

Class

UnitTestCase
Provides a base class and helpers for Drupal unit tests.

Namespace

Drupal\Tests

Code

public static function randomName($length = 8) {
  $values = array_merge(range(65, 90), range(97, 122), range(48, 57));
  $max = count($values) - 1;
  $str = chr(mt_rand(97, 122));
  for ($i = 1; $i < $length; $i++) {
    $str .= chr($values[mt_rand(0, $max)]);
  }
  return $str;
}