3 namespace Drupal\Tests;
5 use Drupal\Component\Utility\Random;
8 * Provides random generator utility methods.
10 trait RandomGeneratorTrait {
13 * The random generator.
15 * @var \Drupal\Component\Utility\Random
17 protected $randomGenerator;
20 * Generates a pseudo-random string of ASCII characters of codes 32 to 126.
22 * Do not use this method when special characters are not possible (e.g., in
23 * machine or file names that have already been validated); instead, use
24 * \Drupal\simpletest\TestBase::randomMachineName(). If $length is greater
25 * than 3 the random string will include at least one ampersand ('&') and
26 * at least one greater than ('>') character to ensure coverage for special
27 * characters and avoid the introduction of random test failures.
30 * Length of random string to generate.
33 * Pseudo-randomly generated unique string including special characters.
35 * @see \Drupal\Component\Utility\Random::string()
37 public function randomString($length = 8) {
39 return $this->getRandomGenerator()->string($length, TRUE, [$this, 'randomStringValidate']);
42 // To prevent the introduction of random test failures, ensure that the
43 // returned string contains a character that needs to be escaped in HTML by
44 // injecting an ampersand into it.
45 $replacement_pos = floor($length / 2);
46 // Remove 2 from the length to account for the ampersand and greater than
48 $string = $this->getRandomGenerator()->string($length - 2, TRUE, [$this, 'randomStringValidate']);
49 return substr_replace($string, '>&', $replacement_pos, 0);
53 * Callback for random string validation.
55 * @see \Drupal\Component\Utility\Random::string()
57 * @param string $string
58 * The random string to validate.
61 * TRUE if the random string is valid, FALSE if not.
63 public function randomStringValidate($string) {
64 // Consecutive spaces causes issues for
65 // \Drupal\simpletest\WebTestBase::assertLink().
66 if (preg_match('/\s{2,}/', $string)) {
70 // Starting or ending with a space means that length might not be what is
72 if (preg_match('/^\s|\s$/', $string)) {
80 * Generates a unique random string containing letters and numbers.
82 * Do not use this method when testing unvalidated user input. Instead, use
83 * \Drupal\simpletest\TestBase::randomString().
86 * Length of random string to generate.
89 * Randomly generated unique string.
91 * @see \Drupal\Component\Utility\Random::name()
93 protected function randomMachineName($length = 8) {
94 return $this->getRandomGenerator()->name($length, TRUE);
98 * Generates a random PHP object.
101 * The number of random keys to add to the object.
104 * The generated object, with the specified number of random keys. Each key
105 * has a random string value.
107 * @see \Drupal\Component\Utility\Random::object()
109 public function randomObject($size = 4) {
110 return $this->getRandomGenerator()->object($size);
114 * Gets the random generator for the utility methods.
116 * @return \Drupal\Component\Utility\Random
117 * The random generator.
119 protected function getRandomGenerator() {
120 if (!is_object($this->randomGenerator)) {
121 $this->randomGenerator = new Random();
123 return $this->randomGenerator;