3 namespace Drupal\Tests\Component\Utility;
5 use Drupal\Component\Utility\Random;
6 use Drupal\Tests\UnitTestCase;
9 * Tests random data generation.
13 * @coversDefaultClass \Drupal\Component\Utility\Random
15 class RandomTest extends UnitTestCase {
18 * The first random string passed to the test callback.
20 * @see \Drupal\Tests\Component\Utility\RandomTest::_RandomStringValidate()
24 protected $firstStringGenerated = '';
27 * Tests unique random string generation.
31 public function testRandomStringUniqueness() {
33 $random = new Random();
34 for ($i = 0; $i <= 50; $i++) {
35 $str = $random->string(1, TRUE);
36 $this->assertFalse(isset($strings[$str]), 'Generated duplicate random string ' . $str);
37 $strings[$str] = TRUE;
42 * Tests unique random name generation.
46 public function testRandomNamesUniqueness() {
48 $random = new Random();
49 for ($i = 0; $i <= 10; $i++) {
50 $str = $random->name(1, TRUE);
51 $this->assertFalse(isset($names[$str]), 'Generated duplicate random name ' . $str);
57 * Tests infinite loop prevention whilst generating random names.
61 public function testRandomNameException() {
62 // There are fewer than 100 possibilities so an exception should occur to
63 // prevent infinite loops.
64 $random = new Random();
65 $this->setExpectedException(\RuntimeException::class);
66 for ($i = 0; $i <= 100; $i++) {
67 $str = $random->name(1, TRUE);
73 * Tests infinite loop prevention whilst generating random strings.
77 public function testRandomStringException() {
78 // There are fewer than 100 possibilities so an exception should occur to
79 // prevent infinite loops.
80 $random = new Random();
81 $this->setExpectedException(\RuntimeException::class);
82 for ($i = 0; $i <= 100; $i++) {
83 $str = $random->string(1, TRUE);
89 * Tests random name generation if uniqueness is not enforced.
93 public function testRandomNameNonUnique() {
94 // There are fewer than 100 possibilities if we were forcing uniqueness so
95 // exception would occur.
96 $random = new Random();
97 for ($i = 0; $i <= 100; $i++) {
100 $this->assertTrue(TRUE, 'No exception thrown when uniqueness is not enforced.');
104 * Tests random string if uniqueness is not enforced.
108 public function testRandomStringNonUnique() {
109 // There are fewer than 100 possibilities if we were forcing uniqueness so
110 // exception would occur.
111 $random = new Random();
112 for ($i = 0; $i <= 100; $i++) {
115 $this->assertTrue(TRUE, 'No exception thrown when uniqueness is not enforced.');
119 * Tests random object generation to ensure the expected number of properties.
123 public function testRandomObject() {
124 // For values of 0 and 1 \Drupal\Component\Utility\Random::object() will
125 // have different execution paths.
126 $random = new Random();
127 for ($i = 0; $i <= 1; $i++) {
128 $obj = $random->object($i);
129 $this->assertEquals($i, count(get_object_vars($obj)), 'Generated random object has expected number of properties');
134 * Tests random string validation callbacks.
138 public function testRandomStringValidator() {
139 $random = new Random();
140 $this->firstStringGenerated = '';
141 $str = $random->string(1, TRUE, [$this, '_RandomStringValidate']);
142 $this->assertNotEquals($this->firstStringGenerated, $str);
146 * Callback for random string validation.
148 * @see \Drupal\Component\Utility\Random::name()
149 * @see \Drupal\Tests\Component\Utility\RandomTest::testRandomStringValidator()
151 * @param string $string
152 * The random string to validate.
155 * TRUE if the random string is valid, FALSE if not.
157 public function _RandomStringValidate($string) {
158 // Return FALSE for the first generated string and any string that is the
159 // same, as the test expects a different string to be returned.
160 if (empty($this->firstStringGenerated) || $string == $this->firstStringGenerated) {
161 $this->firstStringGenerated = $string;