annotate core/tests/Drupal/Tests/RandomGeneratorTrait.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents 4c8ae668cc8c
children
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\Tests;
Chris@0 4
Chris@0 5 use Drupal\Component\Utility\Random;
Chris@0 6
Chris@0 7 /**
Chris@0 8 * Provides random generator utility methods.
Chris@0 9 */
Chris@0 10 trait RandomGeneratorTrait {
Chris@0 11
Chris@0 12 /**
Chris@0 13 * The random generator.
Chris@0 14 *
Chris@0 15 * @var \Drupal\Component\Utility\Random
Chris@0 16 */
Chris@0 17 protected $randomGenerator;
Chris@0 18
Chris@0 19 /**
Chris@0 20 * Generates a pseudo-random string of ASCII characters of codes 32 to 126.
Chris@0 21 *
Chris@0 22 * Do not use this method when special characters are not possible (e.g., in
Chris@0 23 * machine or file names that have already been validated); instead, use
Chris@0 24 * \Drupal\simpletest\TestBase::randomMachineName(). If $length is greater
Chris@0 25 * than 3 the random string will include at least one ampersand ('&') and
Chris@0 26 * at least one greater than ('>') character to ensure coverage for special
Chris@0 27 * characters and avoid the introduction of random test failures.
Chris@0 28 *
Chris@0 29 * @param int $length
Chris@0 30 * Length of random string to generate.
Chris@0 31 *
Chris@0 32 * @return string
Chris@0 33 * Pseudo-randomly generated unique string including special characters.
Chris@0 34 *
Chris@0 35 * @see \Drupal\Component\Utility\Random::string()
Chris@0 36 */
Chris@0 37 public function randomString($length = 8) {
Chris@0 38 if ($length < 4) {
Chris@0 39 return $this->getRandomGenerator()->string($length, TRUE, [$this, 'randomStringValidate']);
Chris@0 40 }
Chris@0 41
Chris@0 42 // To prevent the introduction of random test failures, ensure that the
Chris@0 43 // returned string contains a character that needs to be escaped in HTML by
Chris@0 44 // injecting an ampersand into it.
Chris@0 45 $replacement_pos = floor($length / 2);
Chris@0 46 // Remove 2 from the length to account for the ampersand and greater than
Chris@0 47 // characters.
Chris@0 48 $string = $this->getRandomGenerator()->string($length - 2, TRUE, [$this, 'randomStringValidate']);
Chris@0 49 return substr_replace($string, '>&', $replacement_pos, 0);
Chris@0 50 }
Chris@0 51
Chris@0 52 /**
Chris@0 53 * Callback for random string validation.
Chris@0 54 *
Chris@0 55 * @see \Drupal\Component\Utility\Random::string()
Chris@0 56 *
Chris@0 57 * @param string $string
Chris@0 58 * The random string to validate.
Chris@0 59 *
Chris@0 60 * @return bool
Chris@0 61 * TRUE if the random string is valid, FALSE if not.
Chris@0 62 */
Chris@0 63 public function randomStringValidate($string) {
Chris@0 64 // Consecutive spaces causes issues for
Chris@0 65 // \Drupal\simpletest\WebTestBase::assertLink().
Chris@0 66 if (preg_match('/\s{2,}/', $string)) {
Chris@0 67 return FALSE;
Chris@0 68 }
Chris@0 69
Chris@0 70 // Starting or ending with a space means that length might not be what is
Chris@0 71 // expected.
Chris@0 72 if (preg_match('/^\s|\s$/', $string)) {
Chris@0 73 return FALSE;
Chris@0 74 }
Chris@0 75
Chris@0 76 return TRUE;
Chris@0 77 }
Chris@0 78
Chris@0 79 /**
Chris@0 80 * Generates a unique random string containing letters and numbers.
Chris@0 81 *
Chris@0 82 * Do not use this method when testing unvalidated user input. Instead, use
Chris@0 83 * \Drupal\simpletest\TestBase::randomString().
Chris@0 84 *
Chris@0 85 * @param int $length
Chris@0 86 * Length of random string to generate.
Chris@0 87 *
Chris@0 88 * @return string
Chris@0 89 * Randomly generated unique string.
Chris@0 90 *
Chris@0 91 * @see \Drupal\Component\Utility\Random::name()
Chris@0 92 */
Chris@0 93 protected function randomMachineName($length = 8) {
Chris@0 94 return $this->getRandomGenerator()->name($length, TRUE);
Chris@0 95 }
Chris@0 96
Chris@0 97 /**
Chris@0 98 * Generates a random PHP object.
Chris@0 99 *
Chris@0 100 * @param int $size
Chris@0 101 * The number of random keys to add to the object.
Chris@0 102 *
Chris@0 103 * @return \stdClass
Chris@0 104 * The generated object, with the specified number of random keys. Each key
Chris@0 105 * has a random string value.
Chris@0 106 *
Chris@0 107 * @see \Drupal\Component\Utility\Random::object()
Chris@0 108 */
Chris@0 109 public function randomObject($size = 4) {
Chris@0 110 return $this->getRandomGenerator()->object($size);
Chris@0 111 }
Chris@0 112
Chris@0 113 /**
Chris@0 114 * Gets the random generator for the utility methods.
Chris@0 115 *
Chris@0 116 * @return \Drupal\Component\Utility\Random
Chris@0 117 * The random generator.
Chris@0 118 */
Chris@0 119 protected function getRandomGenerator() {
Chris@0 120 if (!is_object($this->randomGenerator)) {
Chris@0 121 $this->randomGenerator = new Random();
Chris@0 122 }
Chris@0 123 return $this->randomGenerator;
Chris@0 124 }
Chris@0 125
Chris@0 126 }