diff --git a/core/tests/Drupal/FunctionalTests/AssertLegacyTrait.php b/core/tests/Drupal/FunctionalTests/AssertLegacyTrait.php index 81da04a..5785a7d 100644 --- a/core/tests/Drupal/FunctionalTests/AssertLegacyTrait.php +++ b/core/tests/Drupal/FunctionalTests/AssertLegacyTrait.php @@ -94,6 +94,58 @@ protected function assertNoText($text) { } /** + * Passes if the text is found ONLY ONCE on the text version of the page. + * + * The text version is the equivalent of what a user would see when viewing + * through a web browser. In other words the HTML has been filtered out of + * the contents. + * + * @param string|\Drupal\Component\Render\MarkupInterface $text + * Plain text to look for. + * @param string $message + * (optional) A message to display with the assertion. Do not translate + * messages with t(). If left blank, a default message will be displayed. + * + * @deprecated Scheduled for removal in Drupal 9.0.0. + * Use $this->getSession()->getPage()->getText() and substr_count() instead. + */ + protected function assertUniqueText($text, $message = NULL) { + // Cast MarkupInterface objects to string. + $text = (string) $text; + + $message = $message ?: "'$text' found only once on the page"; + $page_text = $this->getSession()->getPage()->getText(); + $nr_found = substr_count($page_text, $text); + $this->assertSame(1, $nr_found, $message); + } + + /** + * Passes if the text is found MORE THAN ONCE on the text version of the page. + * + * The text version is the equivalent of what a user would see when viewing + * through a web browser. In other words the HTML has been filtered out of + * the contents. + * + * @param string|\Drupal\Component\Render\MarkupInterface $text + * Plain text to look for. + * @param string $message + * (optional) A message to display with the assertion. Do not translate + * messages with t(). If left blank, a default message will be displayed. + * + * @deprecated Scheduled for removal in Drupal 9.0.0. + * Use $this->getSession()->getPage()->getText() and substr_count() instead. + */ + protected function assertNoUniqueText($text, $message = '') { + // Cast MarkupInterface objects to string. + $text = (string) $text; + + $message = $message ?: "'$text' found more than once on the page"; + $page_text = $this->getSession()->getPage()->getText(); + $nr_found = substr_count($page_text, $text); + $this->assertGreaterThan(1, $nr_found, $message); + } + + /** * Asserts the page responds with the specified response code. * * @param int $code @@ -371,12 +423,18 @@ protected function assertNoOption($id, $option) { * ID of select field to assert. * @param string $option * Option to assert. + * @param string $message + * (optional) A message to display with the assertion. Do not translate + * messages with t(). If left blank, a default message will be displayed. * * @deprecated Scheduled for removal in Drupal 9.0.0. - * Use $this->assertSession()->optionSelected() instead. + * Use $this->assertSession()->optionExists() instead and check the + * "selected" attribute yourself. */ - protected function assertOptionSelected($id, $option) { - $this->assertSession()->optionSelected($id, $option); + protected function assertOptionSelected($id, $option, $message = NULL) { + $option_field = $this->assertSession()->optionExists($id, $option); + $message = $message ?: "Option $option for field $id is selected."; + $this->assertTrue($option_field->hasAttribute('selected'), $message); } /** @@ -436,6 +494,19 @@ protected function assertNoEscaped($raw) { } /** + * Triggers a pass if the Perl regex pattern is found in the raw content. + * + * @param string $pattern + * Perl regex to look for including the regex delimiters. + * + * @deprecated Scheduled for removal in Drupal 9.0.0. + * Use $this->assertSession()->responseMatches() instead. + */ + protected function assertPattern($pattern) { + $this->assertSession()->responseMatches($pattern); + } + + /** * Asserts whether an expected cache tag was present in the last response. * * @param string $expected_cache_tag diff --git a/core/tests/Drupal/Tests/WebAssert.php b/core/tests/Drupal/Tests/WebAssert.php index d4cffd8..6156dda 100644 --- a/core/tests/Drupal/Tests/WebAssert.php +++ b/core/tests/Drupal/Tests/WebAssert.php @@ -191,29 +191,6 @@ public function optionNotExists($select, $option, TraversableElement $container } /** - * Checks that a specific option in a select field is selected. - * - * @param string $select - * One of id|name|label|value for the select field. - * @param string $option - * The option value. - * @param \Behat\Mink\Element\TraversableElement $container - * (optional) The document to check against. Defaults to the current page. - * - * @return \Behat\Mink\Element\NodeElement - * The matching option element - * - * @throws \Behat\Mink\Exception\ElementNotFoundException - * When the element doesn't exist. - */ - public function optionSelected($select, $option, TraversableElement $container = NULL) { - $option_field = $this->optionExists($select, $option, $container); - $this->assert(!empty($option_field->getAttribute('selected')), sprintf('Option "%s" in select "%s" is not selected as expected.', $option, $select)); - - return $option_field; - } - - /** * Pass if the page title is the given string. * * @param string $expected_title