commit ddeae8d5302fb6b1dc9c04372de88719ddb30c02 Author: Lee Rowlands Date: Wed Apr 27 11:38:54 2016 +1000 p22 diff --git a/core/modules/simpletest/src/RandomGeneratorTrait.php b/core/modules/simpletest/src/RandomGeneratorTrait.php index d0f48de..fbd4290 100644 --- a/core/modules/simpletest/src/RandomGeneratorTrait.php +++ b/core/modules/simpletest/src/RandomGeneratorTrait.php @@ -2,7 +2,7 @@ namespace Drupal\simpletest; -use Drupal\Component\Utility\Random; +use Drupal\Tests\RandomGeneratorTrait as BaseGeneratorTrait; /** * Provides random generator utility methods. @@ -13,119 +13,5 @@ * @see \Drupal\Tests */ trait RandomGeneratorTrait { - - /** - * The random generator. - * - * @var \Drupal\Component\Utility\Random - */ - protected $randomGenerator; - - /** - * 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. - * - * @see \Drupal\Component\Utility\Random::string() - * - * @param string $string - * The random string to validate. - * - * @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; - } - + use BaseGeneratorTrait; } diff --git a/core/modules/simpletest/src/SessionTestTrait.php b/core/modules/simpletest/src/SessionTestTrait.php index 11fc149..15158d2 100644 --- a/core/modules/simpletest/src/SessionTestTrait.php +++ b/core/modules/simpletest/src/SessionTestTrait.php @@ -2,7 +2,7 @@ namespace Drupal\simpletest; -use Symfony\Component\HttpFoundation\Request; +use Drupal\Tests\SessionTestTrait as BaseSessionTestTrait; /** * Provides methods to generate and get session name in tests. @@ -14,32 +14,6 @@ */ trait SessionTestTrait { - /** - * The name of the session cookie. - * - * @var string - */ - protected $sessionName; - - /** - * 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; - } + use BaseSessionTestTrait; } diff --git a/core/modules/simpletest/src/WebAssert.php b/core/modules/simpletest/src/WebAssert.php index 5f6f309..ab287a8 100644 --- a/core/modules/simpletest/src/WebAssert.php +++ b/core/modules/simpletest/src/WebAssert.php @@ -2,9 +2,7 @@ namespace Drupal\simpletest; -use Behat\Mink\WebAssert as MinkWebAssert; -use Behat\Mink\Element\TraversableElement; -use Behat\Mink\Exception\ElementNotFoundException; +use Drupal\Tests\WebAssert as BaseWebAssert; /** * Defines a class with methods for asserting presence of elements during tests. @@ -14,59 +12,5 @@ * * @see \Drupal\Tests */ -class WebAssert extends MinkWebAssert { - - /** - * Checks that specific button exists on the current page. - * - * @param string $button - * One of id|name|label|value for the button. - * @param \Behat\Mink\Element\TraversableElement $container - * (optional) The document to check against. Defaults to the current page. - * - * @return \Behat\Mink\Element\NodeElement - * The matching element. - * - * @throws \Behat\Mink\Exception\ElementNotFoundException - * When the element doesn't exist. - */ - public function buttonExists($button, TraversableElement $container = NULL) { - $container = $container ?: $this->session->getPage(); - $node = $container->findButton($button); - - if ($node === NULL) { - throw new ElementNotFoundException($this->session, 'button', 'id|name|label|value', $button); - } - - return $node; - } - - /** - * Checks that specific select field exists on the current page. - * - * @param string $select - * One of id|name|label|value for the select field. - * @param \Behat\Mink\Element\TraversableElement $container - * (optional) The document to check against. Defaults to the current page. - * - * @return \Behat\Mink\Element\NodeElement - * The matching element - * - * @throws \Behat\Mink\Exception\ElementNotFoundException - * When the element doesn't exist. - */ - public function selectExists($select, TraversableElement $container = NULL) { - $container = $container ?: $this->session->getPage(); - $node = $container->find('named', array( - 'select', - $this->session->getSelectorsHandler()->xpathLiteral($select), - )); - - if ($node === NULL) { - throw new ElementNotFoundException($this->session, 'select', 'id|name|label|value', $select); - } - - return $node; - } - +class WebAssert extends BaseWebAssert { }