Commit 0534e4cd authored by catch's avatar catch

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) {
* 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
* \Drupal\simpletest\TestBase::randomMachineName(). If $length is greater
* than 2 the random string will include at least one ampersand ('&')
* character to ensure coverage for special characters and avoid the
* introduction of random test failures.
* than 3 the random string will include at least one ampersand ('&') and
* at least one greater than ('>') character to ensure coverage for special
* characters and avoid the introduction of random test failures.
*
* @param int $length
* Length of random string to generate.
......@@ -1436,7 +1436,7 @@ protected function settingsSet($name, $value) {
* @see \Drupal\Component\Utility\Random::string()
*/
public function randomString($length = 8) {
if ($length < 3) {
if ($length < 4) {
return $this->getRandomGenerator()->string($length, TRUE, array($this, 'randomStringValidate'));
}
......@@ -1444,9 +1444,10 @@ public function randomString($length = 8) {
// returned string contains a character that needs to be escaped in HTML by
// injecting an ampersand into it.
$replacement_pos = floor($length / 2);
// Remove 1 from the length to account for the ampersand character.
$string = $this->getRandomGenerator()->string($length - 1, TRUE, array($this, 'randomStringValidate'));
return substr_replace($string, '&', $replacement_pos, 0);
// Remove 2 from the length to account for the ampersand and greater than
// characters.
$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) {
$mock_test_base = $this->getMockForAbstractClass('Drupal\simpletest\TestBase');
$string = $mock_test_base->randomString($length);
$this->assertEquals($length, strlen($string));
// randomString() should always include an ampersand ('&') if $length is
// greater than 2.
if ($length > 2) {
// randomString() should always include an ampersand ('&') and a
// greater than ('>') if $length is greater than 3.
if ($length > 3) {
$this->assertContains('&', $string);
$this->assertContains('>', $string);
}
}
......
Markdown is supported
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