diff --git a/core/tests/Drupal/Tests/BrowserAssertTrait.php b/core/tests/Drupal/FunctionalTests/AssertLegacyTrait.php similarity index 64% rename from core/tests/Drupal/Tests/BrowserAssertTrait.php rename to core/tests/Drupal/FunctionalTests/AssertLegacyTrait.php index e108b6d..248a931 100644 --- a/core/tests/Drupal/Tests/BrowserAssertTrait.php +++ b/core/tests/Drupal/FunctionalTests/AssertLegacyTrait.php @@ -1,13 +1,19 @@ elementExists() instead. */ protected function assertElementPresent($css_selector, $message = '') { - $this->assertNotEmpty($this->getSession()->getDriver()->find($this->cssSelectToXpath($css_selector)), $message); + $this->assertSession()->elementExists('css', $css_selector); } /** @@ -41,9 +50,12 @@ protected function assertElementPresent($css_selector, $message = '') { * The CSS selector identifying the element to check. * @param string $message * Optional message to show alongside the assertion. + * + * @deprecated Scheduled for removal in Drupal 9.0.0. + * Use $assert->elementNotExists() instead. */ protected function assertElementNotPresent($css_selector, $message = '') { - $this->assertEmpty($this->getSession()->getDriver()->find($this->cssSelectToXpath($css_selector)), $message); + $this->assertSession()->elementNotExists('css', $css_selector); } /** @@ -54,6 +66,9 @@ protected function assertElementNotPresent($css_selector, $message = '') { * * @param string $text * Plain text to look for. + * + * @deprecated Scheduled for removal in Drupal 9.0.0. + * Use $assert->responseContains() instead. */ protected function assertText($text) { $this->assertSession()->responseContains($text); @@ -67,6 +82,9 @@ protected function assertText($text) { * * @param string $text * Plain text to look for. + * + * @deprecated Scheduled for removal in Drupal 9.0.0. + * Use $assert->responseNotContains() instead. */ protected function assertNoText($text) { $this->assertSession()->responseNotContains($text); @@ -78,62 +96,32 @@ protected function assertNoText($text) { * @param int $code * Response code. For example 200 is a successful page request. For a list * of all codes see http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html. + * + * @deprecated Scheduled for removal in Drupal 9.0.0. + * Use $assert->statusCodeEquals() instead. */ protected function assertResponse($code) { $this->assertSession()->statusCodeEquals($code); } /** - * Pass if the page title is the given string. - * - * @param string $expected_title - * The string the page title should be. - */ - protected function assertTitle($expected_title) { - $title_element = $this->getSession()->getPage()->find('css', 'title'); - if (!$title_element) { - $this->fail('No title element found on the page.'); - return; - } - $actual_title = $title_element->getText(); - $this->assertSame($expected_title, $actual_title); - } - - /** - * Passes if a link with the specified label is found. + * Asserts that a field exists with the given name and value. * - * An optional link index may be passed. + * @param string $name + * Name of field to assert. + * @param string $value + * (optional) Value of the field to assert. You may pass in NULL (default) + * to skip checking the actual value, while still checking that the field + * exists. * - * @param string|\Drupal\Component\Render\MarkupInterface $label - * Text between the anchor tags. - * @param int $index - * Link position counting from zero. - * @param string $message - * (optional) A message to display with the assertion. Do not translate - * messages: use strtr() to embed variables in the message text, not - * t(). If left blank, a default message will be displayed. + * @deprecated Scheduled for removal in Drupal 9.0.0. + * Use $assert->fieldExists() or $assert->fieldValueEquals() instead. */ - protected function assertLink($label, $index = 0, $message = '') { - // Cast MarkupInterface objects to string. - $label = (string) $label; - $message = ($message ? $message : strtr('Link with label %label found.', ['%label' => $label])); - $links = $this->getSession()->getPage()->findAll('named', ['link', $label]); - if (empty($links[$index])) { - $this->fail($message); + protected function assertFieldByName($name, $value = NULL) { + $this->assertSession()->fieldExists($name); + if ($value !== NULL) { + $this->assertSession()->fieldValueEquals($name, $value); } - $this->assertNotNull($links[$index], $message); - } - - /** - * Passes if the raw text IS NOT found escaped on the loaded page. - * - * Raw text refers to the raw HTML that the page generated. - * - * @param string $raw - * Raw (HTML) string to look for. - */ - protected function assertNoEscaped($raw) { - $this->assertSession()->pageTextNotContains(Html::escape($raw)); } /** @@ -143,63 +131,52 @@ protected function assertNoEscaped($raw) { * * @param string $raw * Raw (HTML) string to look for. + * + * @deprecated Scheduled for removal in Drupal 9.0.0. + * Use $assert->responseContains() instead. */ protected function assertRaw($raw) { $this->assertSession()->responseContains($raw); } /** - * Asserts that a field exists with the given name and value. + * Fire an assertion that is always positive. * - * @param string $name - * Name of field to assert. - * @param string $value - * (optional) Value of the field to assert. You may pass in NULL (default) - * to skip checking the actual value, while still checking that the field - * exists. + * @param string $message + * (optional) A message to display with the assertion. Do not translate + * messages: use \Drupal\Component\Utility\SafeMarkup::format() to embed + * variables in the message text, not t(). If left blank, a default message + * will be displayed. */ - protected function assertFieldByName($name, $value = NULL) { - $this->assertSession()->fieldExists($name); - if ($value !== NULL) { - $this->assertSession()->fieldValueEquals($name, $value); - } + protected function pass($message = '') { } /** - * Check to see if two values are equal. - * - * Compatibility function for ::assertEquals(). + * Pass if the page title is the given string. * - * @param $first - * The first value to check. - * @param $second - * The second value to check. - * @param $message - * (optional) A message to display with the assertion. Do not translate - * messages: use \Drupal\Component\Utility\SafeMarkup::format() to embed - * variables in the message text, not t(). If left blank, a default message - * will be displayed. + * @param string $expected_title + * The string the page title should be. */ - protected function assertEqual($first, $second, $message = '') { - // Cast objects implementing MarkupInterface to string instead of - // relying on PHP casting them to string depending on what they are being - // comparing with. - $first = $this->castSafeStrings($first); - $second = $this->castSafeStrings($second); - $this->assertEquals($second, $first, $message); + protected function assertTitle($expected_title) { + return $this->assertSession()->titleEquals($expected_title); } /** - * Fire an assertion that is always positive. + * Passes if a link with the specified label is found. + * + * An optional link index may be passed. * + * @param string|\Drupal\Component\Render\MarkupInterface $label + * Text between the anchor tags. + * @param int $index + * Link position counting from zero. * @param string $message * (optional) A message to display with the assertion. Do not translate - * messages: use \Drupal\Component\Utility\SafeMarkup::format() to embed - * variables in the message text, not t(). If left blank, a default message - * will be displayed. + * messages: use strtr() to embed variables in the message text, not + * t(). If left blank, a default message will be displayed. */ - protected function pass($message = '') { - return $this->assertTrue(TRUE, $message); + public function assertLink($label, $index = 0, $message = '') { + return $this->assertSession()->linkExists($label, $index, $message); } } diff --git a/core/tests/Drupal/Tests/BrowserTestBase.php b/core/tests/Drupal/Tests/BrowserTestBase.php index 56c0cce..378bca4 100644 --- a/core/tests/Drupal/Tests/BrowserTestBase.php +++ b/core/tests/Drupal/Tests/BrowserTestBase.php @@ -20,6 +20,7 @@ use Drupal\Core\Test\TestRunnerKernel; use Drupal\Core\Url; use Drupal\Core\Test\TestDatabase; +use Drupal\FunctionalTests\AssertLegacyTrait; use Drupal\simpletest\AssertHelperTrait; use Drupal\simpletest\BlockCreationTrait; use Drupal\simpletest\NodeCreationTrait; @@ -40,7 +41,7 @@ abstract class BrowserTestBase extends \PHPUnit_Framework_TestCase { use AssertHelperTrait; use BlockCreationTrait; - use BrowserAssertTrait; + use AssertLegacyTrait; use RandomGeneratorTrait; use SessionTestTrait; use NodeCreationTrait { @@ -1747,4 +1748,13 @@ protected function drupalGetHeader($name) { return $this->getSession()->getResponseHeader($name); } + /** + * {@inheritdoc} + */ + public static function assertEquals($expected, $actual, $message = '', $delta = 0.0, $maxDepth = 10, $canonicalize = FALSE, $ignoreCase = FALSE) { + $expected = static::castSafeStrings($expected); + $actual = static::castSafeStrings($actual); + parent::assertEquals($expected, $actual, $message, $delta, $maxDepth, $canonicalize, $ignoreCase); + } + } diff --git a/core/tests/Drupal/Tests/WebAssert.php b/core/tests/Drupal/Tests/WebAssert.php index d863de2..fdbb249 100644 --- a/core/tests/Drupal/Tests/WebAssert.php +++ b/core/tests/Drupal/Tests/WebAssert.php @@ -2,9 +2,11 @@ namespace Drupal\Tests; +use Behat\Mink\Exception\ExpectationException; use Behat\Mink\WebAssert as MinkWebAssert; use Behat\Mink\Element\TraversableElement; use Behat\Mink\Exception\ElementNotFoundException; +use Drupal\Component\Utility\Html; /** * Defines a class with methods for asserting presence of elements during tests. @@ -64,4 +66,72 @@ public function selectExists($select, TraversableElement $container = NULL) { return $node; } + /** + * Pass if the page title is the given string. + * + * @param string $expected_title + * The string the page title should be. + */ + public function titleEquals($expected_title) { + $title_element = $this->session->getPage()->find('css', 'title'); + if (!$title_element) { + throw new \Exception('No title element found on the page'); + } + $actual_title = $title_element->getText(); + $this->assert($expected_title === $actual_title, 'Title found'); + } + + /** + * Passes if a link with the specified label is found. + * + * An optional link index may be passed. + * + * @param string|\Drupal\Component\Render\MarkupInterface $label + * Text between the anchor tags. + * @param int $index + * Link position counting from zero. + * @param string $message + * (optional) A message to display with the assertion. Do not translate + * messages: use strtr() to embed variables in the message text, not + * t(). If left blank, a default message will be displayed. + */ + public function linkExists($label, $index = 0, $message = '') { + // Cast MarkupInterface objects to string. + $label = (string) $label; + $message = ($message ? $message : strtr('Link with label %label found.', ['%label' => $label])); + $links = $this->session->getPage()->findAll('named', ['link', $label]); + if (empty($links[$index])) { + throw new \Exception($message); + } + $this->assert($links[$index] !== NULL, $message); + } + + /** + * Passes if the raw text IS NOT found escaped on the loaded page. + * + * Raw text refers to the raw HTML that the page generated. + * + * @param string $raw + * Raw (HTML) string to look for. + */ + public function assertNoEscaped($raw) { + $this->pageTextNotContains(Html::escape($raw)); + } + /** + * Asserts a condition. + * + * @param bool $condition + * @param string $message Failure message + * + * @throws \Behat\Mink\Exception\ExpectationException + * when the condition is not fulfilled. + */ + public function assert($condition, $message) { + if ($condition) { + return; + } + + throw new ExpectationException($message, $this->session->getDriver()); + } + }