Version 1
[yaffs-website] / web / core / tests / Drupal / Tests / RandomGeneratorTrait.php
1 <?php
2
3 namespace Drupal\Tests;
4
5 use Drupal\Component\Utility\Random;
6
7 /**
8  * Provides random generator utility methods.
9  */
10 trait RandomGeneratorTrait {
11
12   /**
13    * The random generator.
14    *
15    * @var \Drupal\Component\Utility\Random
16    */
17   protected $randomGenerator;
18
19   /**
20    * Generates a pseudo-random string of ASCII characters of codes 32 to 126.
21    *
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.
28    *
29    * @param int $length
30    *   Length of random string to generate.
31    *
32    * @return string
33    *   Pseudo-randomly generated unique string including special characters.
34    *
35    * @see \Drupal\Component\Utility\Random::string()
36    */
37   public function randomString($length = 8) {
38     if ($length < 4) {
39       return $this->getRandomGenerator()->string($length, TRUE, [$this, 'randomStringValidate']);
40     }
41
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
47     // characters.
48     $string = $this->getRandomGenerator()->string($length - 2, TRUE, [$this, 'randomStringValidate']);
49     return substr_replace($string, '>&', $replacement_pos, 0);
50   }
51
52   /**
53    * Callback for random string validation.
54    *
55    * @see \Drupal\Component\Utility\Random::string()
56    *
57    * @param string $string
58    *   The random string to validate.
59    *
60    * @return bool
61    *   TRUE if the random string is valid, FALSE if not.
62    */
63   public function randomStringValidate($string) {
64     // Consecutive spaces causes issues for
65     // \Drupal\simpletest\WebTestBase::assertLink().
66     if (preg_match('/\s{2,}/', $string)) {
67       return FALSE;
68     }
69
70     // Starting or ending with a space means that length might not be what is
71     // expected.
72     if (preg_match('/^\s|\s$/', $string)) {
73       return FALSE;
74     }
75
76     return TRUE;
77   }
78
79   /**
80    * Generates a unique random string containing letters and numbers.
81    *
82    * Do not use this method when testing unvalidated user input. Instead, use
83    * \Drupal\simpletest\TestBase::randomString().
84    *
85    * @param int $length
86    *   Length of random string to generate.
87    *
88    * @return string
89    *   Randomly generated unique string.
90    *
91    * @see \Drupal\Component\Utility\Random::name()
92    */
93   protected function randomMachineName($length = 8) {
94     return $this->getRandomGenerator()->name($length, TRUE);
95   }
96
97   /**
98    * Generates a random PHP object.
99    *
100    * @param int $size
101    *   The number of random keys to add to the object.
102    *
103    * @return \stdClass
104    *   The generated object, with the specified number of random keys. Each key
105    *   has a random string value.
106    *
107    * @see \Drupal\Component\Utility\Random::object()
108    */
109   public function randomObject($size = 4) {
110     return $this->getRandomGenerator()->object($size);
111   }
112
113   /**
114    * Gets the random generator for the utility methods.
115    *
116    * @return \Drupal\Component\Utility\Random
117    *   The random generator.
118    */
119   protected function getRandomGenerator() {
120     if (!is_object($this->randomGenerator)) {
121       $this->randomGenerator = new Random();
122     }
123     return $this->randomGenerator;
124   }
125
126 }