diff --git a/core/modules/simpletest/src/RandomGeneratorTrait.php b/core/modules/simpletest/src/RandomGeneratorTrait.php index 2a22de2..5a50439 100644 --- a/core/modules/simpletest/src/RandomGeneratorTrait.php +++ b/core/modules/simpletest/src/RandomGeneratorTrait.php @@ -22,22 +22,25 @@ protected $randomGenerator; /** - * Generates a unique random string of ASCII characters of codes 32 to 126. + * 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 - * randomMachineName(). + * \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. * * @param int $length * Length of random string to generate. * * @return string - * Randomly generated unique string. + * Pseudo-randomly generated unique string including special characters. * * @see \Drupal\Component\Utility\Random::string() */ public function randomString($length = 8) { - if ($length < 3) { + if ($length < 4) { return $this->getRandomGenerator()->string($length, TRUE, array($this, 'randomStringValidate')); } @@ -45,9 +48,10 @@ public function randomString($length = 8) { // returned string contains a character that needs to be escaped in HTML by // injecting an ampersand into it. $replacement_pos = floor($length / 2); - // Remove 1 from the length to account for the ampersand character. - $string = $this->getRandomGenerator()->string($length - 1, TRUE, array($this, 'randomStringValidate')); - return substr_replace($string, '&', $replacement_pos, 0); + // 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); } /** diff --git a/core/modules/simpletest/tests/src/Unit/TestBaseTest.php b/core/modules/simpletest/tests/src/Unit/TestBaseTest.php index f4a005f..cea275c 100644 --- a/core/modules/simpletest/tests/src/Unit/TestBaseTest.php +++ b/core/modules/simpletest/tests/src/Unit/TestBaseTest.php @@ -117,7 +117,7 @@ public function testRandomString($length) { $this->assertEquals($length, strlen($string)); // randomString() should always include an ampersand ('&') and a // greater than ('>') if $length is greater than 3. - if ($length > 3) { + if ($length > 4) { $this->assertContains('&', $string); $this->assertContains('>', $string); }