Issue #2020209 by alexpott, YesCT: Fixed random failures due to non uniqueness of randomString() and randomName().

8.0.x
Alex Pott 2013-06-26 08:47:11 +01:00
parent 1356c25683
commit 880d568c06
1 changed files with 35 additions and 17 deletions

View File

@ -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;