From 880d568c067839413964bead9bce422491e8f112 Mon Sep 17 00:00:00 2001 From: Alex Pott Date: Wed, 26 Jun 2013 08:47:11 +0100 Subject: [PATCH] Issue #2020209 by alexpott, YesCT: Fixed random failures due to non uniqueness of randomString() and randomName(). --- .../lib/Drupal/simpletest/TestBase.php | 52 +++++++++++++------ 1 file changed, 35 insertions(+), 17 deletions(-) diff --git a/core/modules/simpletest/lib/Drupal/simpletest/TestBase.php b/core/modules/simpletest/lib/Drupal/simpletest/TestBase.php index d3ba7b30351..1b746e612ab 100644 --- a/core/modules/simpletest/lib/Drupal/simpletest/TestBase.php +++ b/core/modules/simpletest/lib/Drupal/simpletest/TestBase.php @@ -178,6 +178,20 @@ abstract class TestBase { */ 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. * @@ -1152,7 +1166,7 @@ abstract class TestBase { } /** - * 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 @@ abstract class TestBase { * * @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 @@ abstract class TestBase { * * @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 @@ abstract class TestBase { * 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;