Chris@0: ') character to ensure coverage for special Chris@0: * characters and avoid the introduction of random test failures. Chris@0: * Chris@0: * @param int $length Chris@0: * Length of random string to generate. Chris@0: * Chris@0: * @return string Chris@0: * Pseudo-randomly generated unique string including special characters. Chris@0: * Chris@0: * @see \Drupal\Component\Utility\Random::string() Chris@0: */ Chris@0: public function randomString($length = 8) { Chris@0: if ($length < 4) { Chris@0: return $this->getRandomGenerator()->string($length, TRUE, [$this, 'randomStringValidate']); Chris@0: } Chris@0: Chris@0: // To prevent the introduction of random test failures, ensure that the Chris@0: // returned string contains a character that needs to be escaped in HTML by Chris@0: // injecting an ampersand into it. Chris@0: $replacement_pos = floor($length / 2); Chris@0: // Remove 2 from the length to account for the ampersand and greater than Chris@0: // characters. Chris@0: $string = $this->getRandomGenerator()->string($length - 2, TRUE, [$this, 'randomStringValidate']); Chris@0: return substr_replace($string, '>&', $replacement_pos, 0); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Callback for random string validation. Chris@0: * Chris@0: * @see \Drupal\Component\Utility\Random::string() Chris@0: * Chris@0: * @param string $string Chris@0: * The random string to validate. Chris@0: * Chris@0: * @return bool Chris@0: * TRUE if the random string is valid, FALSE if not. Chris@0: */ Chris@0: public function randomStringValidate($string) { Chris@0: // Consecutive spaces causes issues for Chris@0: // \Drupal\simpletest\WebTestBase::assertLink(). Chris@0: if (preg_match('/\s{2,}/', $string)) { Chris@0: return FALSE; Chris@0: } Chris@0: Chris@0: // Starting or ending with a space means that length might not be what is Chris@0: // expected. Chris@0: if (preg_match('/^\s|\s$/', $string)) { Chris@0: return FALSE; Chris@0: } Chris@0: Chris@0: return TRUE; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Generates a unique random string containing letters and numbers. Chris@0: * Chris@0: * Do not use this method when testing unvalidated user input. Instead, use Chris@0: * \Drupal\simpletest\TestBase::randomString(). Chris@0: * Chris@0: * @param int $length Chris@0: * Length of random string to generate. Chris@0: * Chris@0: * @return string Chris@0: * Randomly generated unique string. Chris@0: * Chris@0: * @see \Drupal\Component\Utility\Random::name() Chris@0: */ Chris@0: protected function randomMachineName($length = 8) { Chris@0: return $this->getRandomGenerator()->name($length, TRUE); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Generates a random PHP object. Chris@0: * Chris@0: * @param int $size Chris@0: * The number of random keys to add to the object. Chris@0: * Chris@0: * @return \stdClass Chris@0: * The generated object, with the specified number of random keys. Each key Chris@0: * has a random string value. Chris@0: * Chris@0: * @see \Drupal\Component\Utility\Random::object() Chris@0: */ Chris@0: public function randomObject($size = 4) { Chris@0: return $this->getRandomGenerator()->object($size); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Gets the random generator for the utility methods. Chris@0: * Chris@0: * @return \Drupal\Component\Utility\Random Chris@0: * The random generator. Chris@0: */ Chris@0: protected function getRandomGenerator() { Chris@0: if (!is_object($this->randomGenerator)) { Chris@0: $this->randomGenerator = new Random(); Chris@0: } Chris@0: return $this->randomGenerator; Chris@0: } Chris@0: Chris@0: }