diff --git a/core/modules/simpletest/src/BrowserTestBase.php b/core/modules/simpletest/src/BrowserTestBase.php index 317f64e..c71097c 100644 --- a/core/modules/simpletest/src/BrowserTestBase.php +++ b/core/modules/simpletest/src/BrowserTestBase.php @@ -19,6 +19,9 @@ use Drupal\Core\StreamWrapper\StreamWrapperInterface; use Drupal\Core\Test\TestRunnerKernel; use Drupal\Core\Url; +use Drupal\Tests\RandomGeneratorTrait; +use Drupal\Tests\SessionTestTrait; +use Drupal\Tests\WebAssert as WebAssertClass; use Drupal\user\Entity\Role; use Drupal\user\Entity\User; use Drupal\user\UserInterface; @@ -466,11 +469,11 @@ public function getSession($name = NULL) { * @param string $name * (optional) Name of the session. Defaults to the active session. * - * @return \Drupal\simpletest\WebAssert + * @return \Drupal\Tests\WebAssert * A new web-assert option for asserting the presence of elements with. */ public function assertSession($name = NULL) { - return new WebAssert($this->getSession($name)); + return new WebAssertClass($this->getSession($name)); } /** diff --git a/core/modules/simpletest/src/RandomGeneratorTrait.php b/core/modules/simpletest/src/RandomGeneratorTrait.php index 2d8b635..be7fa0e 100644 --- a/core/modules/simpletest/src/RandomGeneratorTrait.php +++ b/core/modules/simpletest/src/RandomGeneratorTrait.php @@ -6,6 +6,11 @@ /** * Provides random generator utility methods. + * + * @deprecated in Drupal 8.0.0, will be removed before Drupal 9.0.0. + * The trait was moved to another namespace. + * + * @see \Drupal\Tests */ trait RandomGeneratorTrait { diff --git a/core/modules/simpletest/src/SessionTestTrait.php b/core/modules/simpletest/src/SessionTestTrait.php index 042956e..f041614 100644 --- a/core/modules/simpletest/src/SessionTestTrait.php +++ b/core/modules/simpletest/src/SessionTestTrait.php @@ -6,6 +6,11 @@ /** * Provides methods to generate and get session name in tests. + * + * @deprecated in Drupal 8.0.0 will be removed before 9.0.0. + * This was moved to another namespace. + * + * @see \Drupal\Tests */ trait SessionTestTrait { diff --git a/core/modules/simpletest/src/TestBase.php b/core/modules/simpletest/src/TestBase.php index 03d308d..54223f5 100644 --- a/core/modules/simpletest/src/TestBase.php +++ b/core/modules/simpletest/src/TestBase.php @@ -2,6 +2,7 @@ namespace Drupal\simpletest; +use Behat\Mink\Session; use Drupal\Component\Render\MarkupInterface; use Drupal\Component\Utility\Crypt; use Drupal\Component\Utility\SafeMarkup; @@ -13,6 +14,8 @@ use Drupal\Core\Site\Settings; use Drupal\Core\StreamWrapper\PublicStream; use Drupal\Core\Utility\Error; +use Drupal\Tests\RandomGeneratorTrait; +use Drupal\Tests\SessionTestTrait; /** * Base class for Drupal tests. diff --git a/core/modules/simpletest/src/WebAssert.php b/core/modules/simpletest/src/WebAssert.php index 063473b..56f4465 100644 --- a/core/modules/simpletest/src/WebAssert.php +++ b/core/modules/simpletest/src/WebAssert.php @@ -8,6 +8,11 @@ /** * Defines a class with methods for asserting presence of elements during tests. + * + * @deprecated in Drupal 8.0.0 will be removed before 9.0.0. + * This was moved to another namespace. + * + * @see \Drupal\Tests */ class WebAssert extends MinkWebAssert { diff --git a/core/tests/Drupal/KernelTests/KernelTestBase.php b/core/tests/Drupal/KernelTests/KernelTestBase.php index dffe81d..7e50430 100644 --- a/core/tests/Drupal/KernelTests/KernelTestBase.php +++ b/core/tests/Drupal/KernelTests/KernelTestBase.php @@ -19,7 +19,7 @@ use Drupal\Core\Site\Settings; use Drupal\simpletest\AssertContentTrait; use Drupal\simpletest\AssertHelperTrait; -use Drupal\simpletest\RandomGeneratorTrait; +use Drupal\Tests\RandomGeneratorTrait; use Drupal\simpletest\TestServiceProvider; use Symfony\Component\DependencyInjection\Reference; use Symfony\Component\HttpFoundation\Request; diff --git a/core/tests/Drupal/Tests/RandomGeneratorTrait.php b/core/tests/Drupal/Tests/RandomGeneratorTrait.php new file mode 100644 index 0000000..4fdd89a --- /dev/null +++ b/core/tests/Drupal/Tests/RandomGeneratorTrait.php @@ -0,0 +1,126 @@ +') 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; + } + +} diff --git a/core/tests/Drupal/Tests/SessionTestTrait.php b/core/tests/Drupal/Tests/SessionTestTrait.php new file mode 100644 index 0000000..e71fdf2 --- /dev/null +++ b/core/tests/Drupal/Tests/SessionTestTrait.php @@ -0,0 +1,40 @@ +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; + } + +} diff --git a/core/tests/Drupal/Tests/WebAssert.php b/core/tests/Drupal/Tests/WebAssert.php new file mode 100644 index 0000000..d863de2 --- /dev/null +++ b/core/tests/Drupal/Tests/WebAssert.php @@ -0,0 +1,67 @@ +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; + } + +}