Commit 0534e4cd authored by catch's avatar catch
Browse files

Issue #2487498 by Cristian.Andrei, Scott Weston, iMiksu: Make randomString...

Issue #2487498 by Cristian.Andrei, Scott Weston, iMiksu: Make randomString always return a > to avoid random test fails
parent 6b371cc5
...@@ -1423,9 +1423,9 @@ protected function settingsSet($name, $value) { ...@@ -1423,9 +1423,9 @@ protected function settingsSet($name, $value) {
* Do not use this method when special characters are not possible (e.g., in * Do not use this method when special characters are not possible (e.g., in
* machine or file names that have already been validated); instead, use * machine or file names that have already been validated); instead, use
* \Drupal\simpletest\TestBase::randomMachineName(). If $length is greater * \Drupal\simpletest\TestBase::randomMachineName(). If $length is greater
* than 2 the random string will include at least one ampersand ('&') * than 3 the random string will include at least one ampersand ('&') and
* character to ensure coverage for special characters and avoid the * at least one greater than ('>') character to ensure coverage for special
* introduction of random test failures. * characters and avoid the introduction of random test failures.
* *
* @param int $length * @param int $length
* Length of random string to generate. * Length of random string to generate.
...@@ -1436,7 +1436,7 @@ protected function settingsSet($name, $value) { ...@@ -1436,7 +1436,7 @@ protected function settingsSet($name, $value) {
* @see \Drupal\Component\Utility\Random::string() * @see \Drupal\Component\Utility\Random::string()
*/ */
public function randomString($length = 8) { public function randomString($length = 8) {
if ($length < 3) { if ($length < 4) {
return $this->getRandomGenerator()->string($length, TRUE, array($this, 'randomStringValidate')); return $this->getRandomGenerator()->string($length, TRUE, array($this, 'randomStringValidate'));
} }
...@@ -1444,9 +1444,10 @@ public function randomString($length = 8) { ...@@ -1444,9 +1444,10 @@ public function randomString($length = 8) {
// returned string contains a character that needs to be escaped in HTML by // returned string contains a character that needs to be escaped in HTML by
// injecting an ampersand into it. // injecting an ampersand into it.
$replacement_pos = floor($length / 2); $replacement_pos = floor($length / 2);
// Remove 1 from the length to account for the ampersand character. // Remove 2 from the length to account for the ampersand and greater than
$string = $this->getRandomGenerator()->string($length - 1, TRUE, array($this, 'randomStringValidate')); // characters.
return substr_replace($string, '&', $replacement_pos, 0); $string = $this->getRandomGenerator()->string($length - 2, TRUE, array($this, 'randomStringValidate'));
return substr_replace($string, '>&', $replacement_pos, 0);
} }
/** /**
......
...@@ -115,10 +115,11 @@ public function testRandomString($length) { ...@@ -115,10 +115,11 @@ public function testRandomString($length) {
$mock_test_base = $this->getMockForAbstractClass('Drupal\simpletest\TestBase'); $mock_test_base = $this->getMockForAbstractClass('Drupal\simpletest\TestBase');
$string = $mock_test_base->randomString($length); $string = $mock_test_base->randomString($length);
$this->assertEquals($length, strlen($string)); $this->assertEquals($length, strlen($string));
// randomString() should always include an ampersand ('&') if $length is // randomString() should always include an ampersand ('&') and a
// greater than 2. // greater than ('>') if $length is greater than 3.
if ($length > 2) { if ($length > 3) {
$this->assertContains('&', $string); $this->assertContains('&', $string);
$this->assertContains('>', $string);
} }
} }
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment