diff --git a/core/tests/Drupal/FunctionalTests/AssertLegacyTrait.php b/core/tests/Drupal/FunctionalTests/AssertLegacyTrait.php index 6ca9f85..f0ee8e7 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(); + $occurrences = substr_count($page_text, $text); + $this->assertSame(1, $occurrences, $message); + } + + /** + * Passes if the text is found more tjan 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(); + $occurrences = substr_count($page_text, $text); + $this->assertGreaterThan(1, $occurrences, $message); + } + + /** * Asserts the page responds with the specified response code. * * @param int $code @@ -129,6 +181,32 @@ protected function assertFieldByName($name, $value = NULL) { } /** + * Asserts that a field exists with the given name and value. + * + * @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. + * + * @deprecated Scheduled for removal in Drupal 9.0.0. + * Use $this->assertSession()->fieldNotExists() or + * $this->assertSession()->fieldValueNotEquals() instead. + */ + protected function assertNoFieldByName($name, $value = NULL, $message = '') { + $this->assertSession()->fieldNotExists($name); + if ($value !== NULL) { + $this->assertSession()->fieldValueNotEquals($name, (string) $value); + } + } + + /** * Asserts that a field exists with the given ID and value. * * @param string $id @@ -344,6 +422,63 @@ protected function assertNoOption($id, $option) { } /** + * Asserts that a select option in the current page is checked. + * + * @param string $id + * 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()->optionExists() instead and check the + * "selected" attribute yourself. + */ + 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); + } + + /** + * Asserts that a checkbox field in the current page is checked. + * + * @param string $id + * ID of field to assert. + * @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. + * + * @deprecated Scheduled for removal in Drupal 9.0.0. + * Use $this->assertSession()->checkboxChecked() instead. + */ + protected function assertFieldChecked($id, $message = '') { + $this->assertSession()->checkboxChecked($id); + } + + /** + * Asserts that a checkbox field in the current page is not checked. + * + * @param string $id + * ID of field to assert. + * @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. + * + * @deprecated Scheduled for removal in Drupal 9.0.0. + * Use $this->assertSession()->checkboxNotChecked() instead. + */ + protected function assertNoFieldChecked($id, $message = '') { + $this->assertSession()->checkboxNotChecked($id); + } + + /** * Passes if the raw text IS found escaped on the loaded page, fail otherwise. * * Raw text refers to the raw HTML that the page generated. @@ -374,6 +509,24 @@ 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. + * @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. + * + * @deprecated Scheduled for removal in Drupal 9.0.0. + * Use $this->assertSession()->responseMatches() instead. + */ + protected function assertPattern($pattern, $message = '') { + $this->assertSession()->responseMatches($pattern); + } + + /** * Asserts whether an expected cache tag was present in the last response. * * @param string $expected_cache_tag