diff --git a/core/modules/simpletest/lib/Drupal/simpletest/TestBase.php b/core/modules/simpletest/lib/Drupal/simpletest/TestBase.php index d3ba7b3..1b746e6 100644 --- a/core/modules/simpletest/lib/Drupal/simpletest/TestBase.php +++ b/core/modules/simpletest/lib/Drupal/simpletest/TestBase.php @@ -179,6 +179,20 @@ protected $configImporter; /** + * A list of unique strings generated by randomString(). + * + * @var array + */ + protected $randomStrings = array(); + + /** + * A list of unique names generated by randomName(). + * + * @var array + */ + protected $randomNames = array(); + + /** * Constructor for Test. * * @param $test_id @@ -1152,7 +1166,7 @@ protected function settingsSet($name, $value) { } /** - * Generates a random string of ASCII characters of codes 32 to 126. + * Generates a unique random string of ASCII characters of codes 32 to 126. * * The generated string includes alpha-numeric characters and common * miscellaneous characters. Use this method when testing general input @@ -1170,16 +1184,18 @@ protected function settingsSet($name, $value) { * * @see Drupal\simpletest\TestBase::randomName() */ - public static function randomString($length = 8) { - $str = ''; - for ($i = 0; $i < $length; $i++) { - $str .= chr(mt_rand(32, 126)); - } + public function randomString($length = 8) { + do { + $str = ''; + for ($i = 0; $i < $length; $i++) { + $str .= chr(mt_rand(32, 126)); + } + } while (isset($this->randomStrings[$str])); return $str; } /** - * Generates a random string containing letters and numbers. + * Generates a unique random string containing letters and numbers. * * The string will always start with a letter. The letters may be upper or * lower case. This method is better for restricted inputs that do not @@ -1198,13 +1214,15 @@ public static function randomString($length = 8) { * * @see Drupal\simpletest\TestBase::randomString() */ - public static function randomName($length = 8) { - $values = array_merge(range(65, 90), range(97, 122), range(48, 57)); - $max = count($values) - 1; - $str = chr(mt_rand(97, 122)); - for ($i = 1; $i < $length; $i++) { - $str .= chr($values[mt_rand(0, $max)]); - } + public function randomName($length = 8) { + do { + $values = array_merge(range(65, 90), range(97, 122), range(48, 57)); + $max = count($values) - 1; + $str = chr(mt_rand(97, 122)); + for ($i = 1; $i < $length; $i++) { + $str .= chr($values[mt_rand(0, $max)]); + } + } while (isset($this->randomNames[$str])); return $str; } @@ -1218,11 +1236,11 @@ public static function randomName($length = 8) { * The generated object, with the specified number of random keys. Each key * has a random string value. */ - public static function randomObject($size = 4) { + public function randomObject($size = 4) { $object = new \stdClass(); for ($i = 0; $i < $size; $i++) { - $random_key = self::randomName(); - $random_value = self::randomString(); + $random_key = $this->randomName(); + $random_value = $this->randomString(); $object->{$random_key} = $random_value; } return $object;