diff -u b/core/lib/Drupal/Component/Utility/Random.php b/core/lib/Drupal/Component/Utility/Random.php --- b/core/lib/Drupal/Component/Utility/Random.php +++ b/core/lib/Drupal/Component/Utility/Random.php @@ -37,7 +37,7 @@ static protected $names = array(); /** - * Generates a unique random string of ASCII characters of codes 32 to 126. + * Generates a 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 @@ -45,13 +45,15 @@ * * @param int $length * Length of random string to generate. + * @param bool $unique + * Ensure that the random string returned is unique. * * @return string - * Randomly generated unique string. + * Randomly generated string. * * @see \Drupal\Component\Utility\Random::name() */ - public static function string($length = 8) { + public static function string($length = 8, $unique = FALSE) { $counter = 0; do { @@ -63,14 +65,17 @@ $str .= chr(mt_rand(32, 126)); } $counter++; - } while (isset(static::$strings[$str])); - static::$strings[$str] = TRUE; + } while ($unique && isset(static::$strings[$str])); + + if ($unique) { + static::$strings[$str] = TRUE; + } return $str; } /** - * Generates a unique random string containing letters and numbers. + * Generates a 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 @@ -80,28 +85,33 @@ * * @param int $length * Length of random string to generate. + * @param bool $unique + * Ensure that the random name returned is unique. * * @return string - * Randomly generated unique string. + * Randomly generated string. * * @see \Drupal\Component\Utility\Random::string() */ - public static function name($length = 8) { + public static function name($length = 8, $unique = FALSE) { $values = array_merge(range(65, 90), range(97, 122), range(48, 57)); + $max = count($values) - 1; $counter = 0; do { if ($counter == static::MAXIMUM_TRIES) { throw new \RuntimeException('Unable to generate a unique random name'); } - $max = count($values) - 1; $str = chr(mt_rand(97, 122)); for ($i = 1; $i < $length; $i++) { $str .= chr($values[mt_rand(0, $max)]); } $counter++; - } while (isset(static::$names[$str])); - static::$names[$str] = TRUE; + } while ($unique && isset(static::$names[$str])); + + if ($unique) { + static::$names[$str] = TRUE; + } return $str; } diff -u b/core/modules/simpletest/lib/Drupal/simpletest/TestBase.php b/core/modules/simpletest/lib/Drupal/simpletest/TestBase.php --- b/core/modules/simpletest/lib/Drupal/simpletest/TestBase.php +++ b/core/modules/simpletest/lib/Drupal/simpletest/TestBase.php @@ -1168,7 +1168,7 @@ * @see \Drupal\Component\Utility\Random::string() */ public function randomString($length = 8) { - return Random::string($length); + return Random::string($length, TRUE); } /** @@ -1186,7 +1186,7 @@ * @see \Drupal\Component\Utility\Random::name() */ public function randomName($length = 8) { - return Random::name($length); + return Random::name($length, TRUE); } /** diff -u b/core/tests/Drupal/Tests/Component/Utility/RandomTest.php b/core/tests/Drupal/Tests/Component/Utility/RandomTest.php --- b/core/tests/Drupal/Tests/Component/Utility/RandomTest.php +++ b/core/tests/Drupal/Tests/Component/Utility/RandomTest.php @@ -32,25 +32,10 @@ function testRandomStringUniqueness() { $strings = array(); for ($i = 0; $i <= 50; $i++) { - $str = Random::string(1); - $this->assertFalse(isset($strings[$str]), ''); + $str = Random::string(1, TRUE); + $this->assertFalse(isset($strings[$str]), String::format('Generated duplicate random string !string', array('!string' => $str))); $strings[$str] = TRUE; } - - // There are fewer than 100 possibilities so an exception should occur to - // prevent infinite loops. - $runtime_exception = FALSE; - try { - for ($i = 0; $i <= 100; $i++) { - $str = Random::string(1); - $this->assertFalse(isset($names[$str]), String::format('Generated duplicate random string !string', array('!string' => $str))); - $names[$str] = TRUE; - } - } - catch (\RuntimeException $e) { - $runtime_exception = TRUE; - } - $this->assertTrue($runtime_exception, ''); } /** @@ -61,24 +46,59 @@ for ($i = 0; $i <= 10; $i++) { - $str = Random::name(1); - $this->assertFalse(isset($names[$str]), ''); + $str = Random::name(1, TRUE); + $this->assertFalse(isset($names[$str]), String::format('Generated duplicate random name !name', array('!name' => $str))); $names[$str] = TRUE; } + } + /** + * Tests infinite loop prevention in Random::name(). + * + * @expectedException RuntimeException + */ + public function testRandomNameException() { // There are fewer than 100 possibilities so an exception should occur to // prevent infinite loops. - $runtime_exception = FALSE; - try { - for ($i = 0; $i <= 100; $i++) { - $str = Random::name(1); - $this->assertFalse(isset($names[$str]), String::format('Generated duplicate random name !name', array('!name' => $str))); - $names[$str] = TRUE; - } + for ($i = 0; $i <= 100; $i++) { + $str = Random::name(1, TRUE); + $names[$str] = TRUE; } - catch (\RuntimeException $e) { - $runtime_exception = TRUE; + } + + /** + * Tests infinite loop prevention in Random::string(). + * + * @expectedException RuntimeException + */ + public function testRandomStringException() { + // There are fewer than 100 possibilities so an exception should occur to + // prevent infinite loops. + for ($i = 0; $i <= 100; $i++) { + $str = Random::string(1, TRUE); + $names[$str] = TRUE; } - $this->assertTrue($runtime_exception, ''); + } + /** + * Tests Random::name() if uniqueness is not enforced. + */ + public function testRandomNameNonUnique() { + // There are fewer than 100 possibilities if we were forcing uniqueness so + // exception would occur. + for ($i = 0; $i <= 100; $i++) { + Random::name(1); + } + $this->assertTrue(TRUE, 'No exception thrown when uniqueness is not enforced.'); } + /** + * Tests Random::string() if uniqueness is not enforced. + */ + public function testRandomStringNonUnique() { + // There are fewer than 100 possibilities if we were forcing uniqueness so + // exception would occur. + for ($i = 0; $i <= 100; $i++) { + Random::string(1); + } + $this->assertTrue(TRUE, 'No exception thrown when uniqueness is not enforced.'); + } } diff -u b/core/tests/Drupal/Tests/UnitTestCase.php b/core/tests/Drupal/Tests/UnitTestCase.php --- b/core/tests/Drupal/Tests/UnitTestCase.php +++ b/core/tests/Drupal/Tests/UnitTestCase.php @@ -41,12 +41,12 @@ * Length of random string to generate. * * @return string - * Randomly generated string. + * Randomly generated unique string. * * @see \Drupal\Component\Utility::string() */ public function randomName($length = 8) { - return Random::name($length); + return Random::name($length, TRUE); } /**