diff --git a/core/modules/simpletest/lib/Drupal/simpletest/TestBase.php b/core/modules/simpletest/lib/Drupal/simpletest/TestBase.php index d3ba7b3..860cc3f 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 randomStringUnique(). + * + * @var array + */ + protected $uniqueStrings = array(); + + /** + * A list of unique strings generated by randomStringUnique(). + * + * @var array + */ + protected $uniqueNames = array(); + + /** * Constructor for Test. * * @param $test_id @@ -1179,6 +1193,26 @@ public static function randomString($length = 8) { } /** + * Generates a unique random string of ASCII characters of codes 32 to 126. + * + * @param int $length + * Length of random name to generate. + * + * @return string + * A unique randomly generated string. + * + * @see Drupal\simpletest\TestBase::randomName() + */ + public function randomStringUnique($length = 8) { + do { + $string = $this->randomString($length); + } while (isset($this->uniqueStrings[$string])); + + $this->uniqueStrings[$string] = TRUE; + return $string; + } + + /** * Generates a random string containing letters and numbers. * * The string will always start with a letter. The letters may be upper or @@ -1209,6 +1243,26 @@ public static function randomName($length = 8) { } /** + * Generates a unique random string containing letters and numbers. + * + * @param int $length + * Length of random name to generate. + * + * @return string + * A unique randomly generated string. + * + * @see Drupal\simpletest\TestBase::randomName() + */ + public function randomNameUnique($length = 8) { + do { + $name = $this->randomName($length); + } while (isset($this->uniqueNames[$name])); + + $this->uniqueNames[$name] = TRUE; + return $name; + } + + /** * Generates a random PHP object. * * @param int $size diff --git a/core/modules/simpletest/lib/Drupal/simpletest/WebTestBase.php b/core/modules/simpletest/lib/Drupal/simpletest/WebTestBase.php index 8c46872..c906ae6 100644 --- a/core/modules/simpletest/lib/Drupal/simpletest/WebTestBase.php +++ b/core/modules/simpletest/lib/Drupal/simpletest/WebTestBase.php @@ -487,7 +487,7 @@ protected function drupalCreateUser(array $permissions = array(), $name = NULL) // Create a user assigned to that role. $edit = array(); - $edit['name'] = !empty($name) ? $name : $this->randomName(); + $edit['name'] = !empty($name) ? $name : $this->randomNameUnique(); $edit['mail'] = $edit['name'] . '@example.com'; $edit['pass'] = user_password(); $edit['status'] = 1; @@ -527,13 +527,13 @@ protected function drupalCreateUser(array $permissions = array(), $name = NULL) protected function drupalCreateRole(array $permissions, $rid = NULL, $name = NULL, $weight = NULL) { // Generate a random, lowercase machine name if none was passed. if (!isset($rid)) { - $rid = strtolower($this->randomName(8)); + $rid = strtolower($this->randomNameUnique(8)); } // Generate a random label. if (!isset($name)) { // In the role UI role names are trimmed and random string can start or // end with a space. - $name = trim($this->randomString(8)); + $name = trim($this->randomStringUnique(8)); } // Check the all the permissions strings are valid.