function RandomGeneratorTrait::randomString

Same name and namespace in other branches
  1. 11.x core/tests/Drupal/Tests/RandomGeneratorTrait.php \Drupal\Tests\RandomGeneratorTrait::randomString()
  2. 10 core/tests/Drupal/Tests/RandomGeneratorTrait.php \Drupal\Tests\RandomGeneratorTrait::randomString()
  3. 8.9.x core/tests/Drupal/Tests/RandomGeneratorTrait.php \Drupal\Tests\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()

12 calls to RandomGeneratorTrait::randomString()
BookUninstallTest::testBookUninstall in core/modules/book/tests/src/Kernel/BookUninstallTest.php
Tests the book_system_info_alter() method.
EntityFormDisplayTest::testFieldComponent in core/modules/field_ui/tests/src/Kernel/EntityFormDisplayTest.php
Tests the behavior of a field component within an EntityFormDisplay object.
EntityTranslationNormalizeTest::testNodeTranslation in core/modules/hal/tests/src/Kernel/EntityTranslationNormalizeTest.php
Tests the normalization of node translations.
EntityValidationTest::checkValidation in core/tests/Drupal/KernelTests/Core/Entity/EntityValidationTest.php
Executes the validation test set for a defined entity type.
EntityValidationTest::testEntityChangedConstraintOnConcurrentMultilingualEditing in core/tests/Drupal/KernelTests/Core/Entity/EntityValidationTest.php
Tests the EntityChangedConstraintValidator with multiple translations.

... See full list

File

core/tests/Drupal/Tests/RandomGeneratorTrait.php, line 37

Class

RandomGeneratorTrait
Provides random generator utility methods.

Namespace

Drupal\Tests

Code

public function randomString($length = 8) {
  if ($length < 4) {
    return $this->getRandomGenerator()
      ->string($length, TRUE, [
      $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, [
    $this,
    'randomStringValidate',
  ]);
  return substr_replace($string, '>&', $replacement_pos, 0);
}

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