RandomGeneratorTrait.php 3.6 KB

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