diff --git a/core/tests/Drupal/FunctionalTests/AssertLegacyTrait.php b/core/tests/Drupal/FunctionalTests/AssertLegacyTrait.php index 248a931..05ab275 100644 --- a/core/tests/Drupal/FunctionalTests/AssertLegacyTrait.php +++ b/core/tests/Drupal/FunctionalTests/AssertLegacyTrait.php @@ -3,43 +3,30 @@ namespace Drupal\FunctionalTests; use Drupal\KernelTests\AssertLegacyTrait as BaseAssertLegacyTrait; -use Drupal\Tests\WebAssert; /** * Provides convenience methods for assertions in browser tests. * - * @deprecated Scheduled for removal in Drupal 9.0.0. Use the wrapped methods - * instead. + * @deprecated Scheduled for removal in Drupal 9.0.0. Use the methods on + * \Drupal\Tests\WebAssert instead, for example + * @code + * $this->assertSession()->statusCodeEquals(200); + * @endcode */ trait AssertLegacyTrait { use BaseAssertLegacyTrait; /** - * Returns WebAssert object. - * - * @param string $name - * (optional) Name of the session. Defaults to the active session. - * - * @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)); - } - - /** * Asserts that the element with the given CSS selector is present. * * @param string $css_selector * 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->elementExists() instead. + * Use $this->assertSession()->elementExists() instead. */ - protected function assertElementPresent($css_selector, $message = '') { + protected function assertElementPresent($css_selector) { $this->assertSession()->elementExists('css', $css_selector); } @@ -48,13 +35,11 @@ protected function assertElementPresent($css_selector, $message = '') { * * @param string $css_selector * 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. + * Use $this->assertSession()->elementNotExists() instead. */ - protected function assertElementNotPresent($css_selector, $message = '') { + protected function assertElementNotPresent($css_selector) { $this->assertSession()->elementNotExists('css', $css_selector); } @@ -68,7 +53,7 @@ protected function assertElementNotPresent($css_selector, $message = '') { * Plain text to look for. * * @deprecated Scheduled for removal in Drupal 9.0.0. - * Use $assert->responseContains() instead. + * Use $this->assertSession()->responseContains() instead. */ protected function assertText($text) { $this->assertSession()->responseContains($text); @@ -84,7 +69,7 @@ protected function assertText($text) { * Plain text to look for. * * @deprecated Scheduled for removal in Drupal 9.0.0. - * Use $assert->responseNotContains() instead. + * Use $this->assertSession()->responseNotContains() instead. */ protected function assertNoText($text) { $this->assertSession()->responseNotContains($text); @@ -98,7 +83,7 @@ protected function assertNoText($text) { * 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. + * Use $this->assertSession()->statusCodeEquals() instead. */ protected function assertResponse($code) { $this->assertSession()->statusCodeEquals($code); @@ -115,7 +100,8 @@ protected function assertResponse($code) { * exists. * * @deprecated Scheduled for removal in Drupal 9.0.0. - * Use $assert->fieldExists() or $assert->fieldValueEquals() instead. + * Use $this->assertSession()->fieldExists() or + * $this->assertSession()->fieldValueEquals() instead. */ protected function assertFieldByName($name, $value = NULL) { $this->assertSession()->fieldExists($name); @@ -133,7 +119,7 @@ protected function assertFieldByName($name, $value = NULL) { * Raw (HTML) string to look for. * * @deprecated Scheduled for removal in Drupal 9.0.0. - * Use $assert->responseContains() instead. + * Use $this->assertSession()->responseContains() instead. */ protected function assertRaw($raw) { $this->assertSession()->responseContains($raw); @@ -170,13 +156,9 @@ protected function assertTitle($expected_title) { * 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 assertLink($label, $index = 0, $message = '') { - return $this->assertSession()->linkExists($label, $index, $message); + public function assertLink($label, $index = 0) { + return $this->assertSession()->linkExists($label, $index); } } diff --git a/core/tests/Drupal/KernelTests/KernelTestBase.php b/core/tests/Drupal/KernelTests/KernelTestBase.php index d849d9a..8c88165 100644 --- a/core/tests/Drupal/KernelTests/KernelTestBase.php +++ b/core/tests/Drupal/KernelTests/KernelTestBase.php @@ -1221,6 +1221,9 @@ public function __sleep() { * {@inheritdoc} */ public static function assertEquals($expected, $actual, $message = '', $delta = 0.0, $maxDepth = 10, $canonicalize = FALSE, $ignoreCase = FALSE) { + // Cast objects implementing MarkupInterface to string instead of + // relying on PHP casting them to string depending on what they are being + // comparing with. $expected = static::castSafeStrings($expected); $actual = static::castSafeStrings($actual); parent::assertEquals($expected, $actual, $message, $delta, $maxDepth, $canonicalize, $ignoreCase); diff --git a/core/tests/Drupal/Tests/BrowserTestBase.php b/core/tests/Drupal/Tests/BrowserTestBase.php index 378bca4..a4af147 100644 --- a/core/tests/Drupal/Tests/BrowserTestBase.php +++ b/core/tests/Drupal/Tests/BrowserTestBase.php @@ -541,6 +541,19 @@ public function getSession($name = NULL) { } /** + * Returns WebAssert object. + * + * @param string $name + * (optional) Name of the session. Defaults to the active session. + * + * @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)); + } + + /** * Prepare for a request to testing site. * * The testing site is protected via a SIMPLETEST_USER_AGENT cookie that is @@ -1752,6 +1765,9 @@ protected function drupalGetHeader($name) { * {@inheritdoc} */ public static function assertEquals($expected, $actual, $message = '', $delta = 0.0, $maxDepth = 10, $canonicalize = FALSE, $ignoreCase = FALSE) { + // Cast objects implementing MarkupInterface to string instead of + // relying on PHP casting them to string depending on what they are being + // comparing with. $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 fdbb249..6c03126 100644 --- a/core/tests/Drupal/Tests/WebAssert.php +++ b/core/tests/Drupal/Tests/WebAssert.php @@ -71,11 +71,14 @@ public function selectExists($select, TraversableElement $container = NULL) { * * @param string $expected_title * The string the page title should be. + * + * @throws \Behat\Mink\Exception\ExpectationException + * Thrown when element doesn't exist, or the title is a different one. */ 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'); + throw new ExpectationException('No title element found on the page', $this->session); } $actual_title = $title_element->getText(); $this->assert($expected_title === $actual_title, 'Title found'); @@ -94,6 +97,9 @@ public function titleEquals($expected_title) { * (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. + * + * @throws \Behat\Mink\Exception\ExpectationException + * Thrown when element doesn't exist, or the link label is a different one. */ public function linkExists($label, $index = 0, $message = '') { // Cast MarkupInterface objects to string. @@ -101,7 +107,7 @@ public function linkExists($label, $index = 0, $message = '') { $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); + throw new ExpectationException($message); } $this->assert($links[$index] !== NULL, $message); } @@ -117,11 +123,16 @@ public function linkExists($label, $index = 0, $message = '') { public function assertNoEscaped($raw) { $this->pageTextNotContains(Html::escape($raw)); } + /** * Asserts a condition. * - * @param bool $condition - * @param string $message Failure message + * The parent method is overridden because its a private method. + * + * @param bool $condition + * The condition + * @param string $message + * The failure message * * @throws \Behat\Mink\Exception\ExpectationException * when the condition is not fulfilled.