diff --git a/core/modules/simpletest/src/TestBase.php b/core/modules/simpletest/src/TestBase.php index 944810a..5c2319a 100644 --- a/core/modules/simpletest/src/TestBase.php +++ b/core/modules/simpletest/src/TestBase.php @@ -5,6 +5,7 @@ use Drupal\Component\Assertion\Handle; use Drupal\Component\Render\MarkupInterface; use Drupal\Component\Utility\Crypt; +use Drupal\Component\Utility\Random; use Drupal\Component\Utility\SafeMarkup; use Drupal\Core\Database\Database; use Drupal\Core\Site\Settings; @@ -12,8 +13,7 @@ use Drupal\Core\Test\TestDatabase; use Drupal\Core\Utility\Error; use Drupal\Tests\ConfigTestTrait; -use Drupal\Tests\RandomGeneratorTrait; -use Drupal\Tests\SessionTestTrait; +use Symfony\Component\HttpFoundation\Request; /** * Base class for Drupal tests. @@ -22,9 +22,6 @@ */ abstract class TestBase { - use SessionTestTrait; - use RandomGeneratorTrait; - use AssertHelperTrait; // For backwards compatibility switch the visbility of the methods to public. use ConfigTestTrait { configImporter as public; @@ -58,6 +55,20 @@ protected $timeLimit = 500; /** + * The name of the session cookie. + * + * @var string + */ + protected $sessionName; + + /** + * The random generator. + * + * @var \Drupal\Component\Utility\Random + */ + protected $randomGenerator; + + /** * Current results of this test case. * * @var Array @@ -1592,4 +1603,159 @@ protected function getConfigSchemaExclusions() { return array_unique($exceptions); } + /** + * Generates a session cookie name. + * + * @param string $data + * The data to generate session name. + */ + protected function generateSessionName($data) { + $prefix = (Request::createFromGlobals()->isSecure() ? 'SSESS' : 'SESS'); + $this->sessionName = $prefix . substr(hash('sha256', $data), 0, 32); + } + + /** + * Returns the session name in use on the child site. + * + * @return string + * The name of the session cookie. + */ + protected function getSessionName() { + return $this->sessionName; + } + + /** + * Generates a pseudo-random string of ASCII characters of codes 32 to 126. + * + * 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 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. + * + * @return string + * Pseudo-randomly generated unique string including special characters. + * + * @see \Drupal\Component\Utility\Random::string() + */ + public function randomString($length = 8) { + if ($length < 4) { + return $this->getRandomGenerator()->string($length, TRUE, array( + $this, + 'randomStringValidate', + )); + } + + // To prevent the introduction of random test failures, ensure that the + // returned string contains a character that needs to be escaped in HTML by + // injecting an ampersand into it. + $replacement_pos = floor($length / 2); + // 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); + } + + /** + * Callback for random string validation. + * + * @param string $string + * The random string to validate. + * + * @see \Drupal\Component\Utility\Random::string() + * + * @return bool + * TRUE if the random string is valid, FALSE if not. + */ + public function randomStringValidate($string) { + // Consecutive spaces causes issues for + // \Drupal\simpletest\WebTestBase::assertLink(). + if (preg_match('/\s{2,}/', $string)) { + return FALSE; + } + + // Starting or ending with a space means that length might not be what is + // expected. + if (preg_match('/^\s|\s$/', $string)) { + return FALSE; + } + + return TRUE; + } + + /** + * Generates a unique random string containing letters and numbers. + * + * Do not use this method when testing unvalidated user input. Instead, use + * \Drupal\simpletest\TestBase::randomString(). + * + * @param int $length + * Length of random string to generate. + * + * @return string + * Randomly generated unique string. + * + * @see \Drupal\Component\Utility\Random::name() + */ + protected function randomMachineName($length = 8) { + return $this->getRandomGenerator()->name($length, TRUE); + } + + /** + * Generates a random PHP object. + * + * @param int $size + * The number of random keys to add to the object. + * + * @return \stdClass + * The generated object, with the specified number of random keys. Each key + * has a random string value. + * + * @see \Drupal\Component\Utility\Random::object() + */ + public function randomObject($size = 4) { + return $this->getRandomGenerator()->object($size); + } + + /** + * Gets the random generator for the utility methods. + * + * @return \Drupal\Component\Utility\Random + * The random generator. + */ + protected function getRandomGenerator() { + if (!is_object($this->randomGenerator)) { + $this->randomGenerator = new Random(); + } + return $this->randomGenerator; + } + + /** + * Casts MarkupInterface objects into strings. + * + * @param string|array $value + * The value to act on. + * + * @return mixed + * The input value, with MarkupInterface objects casted to string. + */ + protected static function castSafeStrings($value) { + if ($value instanceof MarkupInterface) { + $value = (string) $value; + } + if (is_array($value)) { + array_walk_recursive($value, function (&$item) { + if ($item instanceof MarkupInterface) { + $item = (string) $item; + } + }); + } + return $value; + } + }