You are here

public function RandomGeneratorTrait::randomString in Zircon Profile 8.0

Same name and namespace in other branches
  1. 8 core/modules/simpletest/src/RandomGeneratorTrait.php \Drupal\simpletest\RandomGeneratorTrait::randomString()

Generates a pseudo-random string of ASCII characters of codes 32 to 126.

Do not use this method when special characters are not possible (e.g., in machine or file names that have already been validated); instead, use \Drupal\simpletest\TestBase::randomMachineName(). If $length is greater than 3 the random string will include at least one ampersand ('&') and at least one greater than ('>') character to ensure coverage for special characters and avoid the introduction of random test failures.

Parameters

int $length: Length of random string to generate.

Return value

string Pseudo-randomly generated unique string including special characters.

See also

\Drupal\Component\Utility\Random::string()

168 calls to RandomGeneratorTrait::randomString()
AreaTextTest::testAreaText in core/modules/views/src/Tests/Handler/AreaTextTest.php
ArgumentValidatorTest::testArgumentValidateNumeric in core/modules/views/src/Tests/Plugin/ArgumentValidatorTest.php
BlockLanguageCacheTest::setUp in core/modules/block/src/Tests/BlockLanguageCacheTest.php
Sets up a Drupal site for running functional and integration tests.
BlockTest::testBlockUserRoleDelete in core/modules/block/src/Tests/BlockTest.php
Tests block_user_role_delete.
BookUninstallTest::testBookUninstall in core/modules/book/src/Tests/BookUninstallTest.php
Tests the book_system_info_alter() method.

... See full list

File

core/modules/simpletest/src/RandomGeneratorTrait.php, line 42
Contains \Drupal\simpletest\RandomGeneratorTrait.

Class

RandomGeneratorTrait
Provides random generator utility methods.

Namespace

Drupal\simpletest

Code

public function randomString($length = 8) {
  if ($length < 4) {
    return $this
      ->getRandomGenerator()
      ->string($length, TRUE, array(
      $this,
      'randomStringValidate',
    ));
  }

  // To prevent the introduction of random test failures, ensure that the
  // returned string contains a character that needs to be escaped in HTML by
  // injecting an ampersand into it.
  $replacement_pos = floor($length / 2);

  // Remove 2 from the length to account for the ampersand and greater than
  // characters.
  $string = $this
    ->getRandomGenerator()
    ->string($length - 2, TRUE, array(
    $this,
    'randomStringValidate',
  ));
  return substr_replace($string, '>&', $replacement_pos, 0);
}