3 namespace Drupal\FunctionalTests;
5 use Behat\Mink\Exception\ElementNotFoundException;
6 use Behat\Mink\Exception\ExpectationException;
7 use Behat\Mink\Selector\Xpath\Escaper;
8 use Drupal\Component\Render\FormattableMarkup;
9 use Drupal\Component\Utility\Xss;
10 use Drupal\KernelTests\AssertLegacyTrait as BaseAssertLegacyTrait;
13 * Provides convenience methods for assertions in browser tests.
15 * @deprecated Scheduled for removal in Drupal 9.0.0. Use the methods on
16 * \Drupal\Tests\WebAssert instead, for example
18 * $this->assertSession()->statusCodeEquals(200);
21 trait AssertLegacyTrait {
23 use BaseAssertLegacyTrait;
26 * Asserts that the element with the given CSS selector is present.
28 * @param string $css_selector
29 * The CSS selector identifying the element to check.
31 * @deprecated Scheduled for removal in Drupal 9.0.0.
32 * Use $this->assertSession()->elementExists() instead.
34 protected function assertElementPresent($css_selector) {
35 $this->assertSession()->elementExists('css', $css_selector);
39 * Asserts that the element with the given CSS selector is not present.
41 * @param string $css_selector
42 * The CSS selector identifying the element to check.
44 * @deprecated Scheduled for removal in Drupal 9.0.0.
45 * Use $this->assertSession()->elementNotExists() instead.
47 protected function assertElementNotPresent($css_selector) {
48 $this->assertSession()->elementNotExists('css', $css_selector);
52 * Passes if the page (with HTML stripped) contains the text.
54 * Note that stripping HTML tags also removes their attributes, such as
55 * the values of text fields.
58 * Plain text to look for.
60 * @deprecated Scheduled for removal in Drupal 9.0.0.
62 * - $this->assertSession()->responseContains() for non-HTML responses,
64 * - $this->assertSession()->pageTextContains() for HTML responses. Unlike
65 * the deprecated assertText(), the passed text should be HTML decoded,
66 * exactly as a human sees it in the browser.
68 protected function assertText($text) {
69 // Cast MarkupInterface to string.
70 $text = (string) $text;
72 $content_type = $this->getSession()->getResponseHeader('Content-type');
73 // In case of a Non-HTML response (example: XML) check the original
75 if (strpos($content_type, 'html') === FALSE) {
76 $this->assertSession()->responseContains($text);
79 $this->assertTextHelper($text, FALSE);
84 * Passes if the page (with HTML stripped) does not contains the text.
86 * Note that stripping HTML tags also removes their attributes, such as
87 * the values of text fields.
90 * Plain text to look for.
92 * @deprecated Scheduled for removal in Drupal 9.0.0.
94 * - $this->assertSession()->responseNotContains() for non-HTML responses,
96 * - $this->assertSession()->pageTextNotContains() for HTML responses.
97 * Unlike the deprecated assertNoText(), the passed text should be HTML
98 * decoded, exactly as a human sees it in the browser.
100 protected function assertNoText($text) {
101 // Cast MarkupInterface to string.
102 $text = (string) $text;
104 $content_type = $this->getSession()->getResponseHeader('Content-type');
105 // In case of a Non-HTML response (example: XML) check the original
107 if (strpos($content_type, 'html') === FALSE) {
108 $this->assertSession()->responseNotContains($text);
111 $this->assertTextHelper($text);
116 * Helper for assertText and assertNoText.
118 * @param string $text
119 * Plain text to look for.
120 * @param bool $not_exists
121 * (optional) TRUE if this text should not exist, FALSE if it should.
125 * TRUE on pass, FALSE on fail.
127 protected function assertTextHelper($text, $not_exists = TRUE) {
128 $args = ['@text' => $text];
129 $message = $not_exists ? new FormattableMarkup('"@text" not found', $args) : new FormattableMarkup('"@text" found', $args);
131 $raw_content = $this->getSession()->getPage()->getContent();
132 // Trying to simulate what the user sees, given that it removes all text
133 // inside the head tags, removes inline Javascript, fix all HTML entities,
134 // removes dangerous protocols and filtering out all HTML tags, as they are
135 // not visible in a normal browser.
136 $raw_content = preg_replace('@<head>(.+?)</head>@si', '', $raw_content);
137 $page_text = Xss::filter($raw_content, []);
139 $actual = $not_exists == (strpos($page_text, (string) $text) === FALSE);
140 $this->assertTrue($actual, $message);
146 * Passes if the text is found ONLY ONCE on the text version of the page.
148 * The text version is the equivalent of what a user would see when viewing
149 * through a web browser. In other words the HTML has been filtered out of
152 * @param string|\Drupal\Component\Render\MarkupInterface $text
153 * Plain text to look for.
154 * @param string $message
155 * (optional) A message to display with the assertion. Do not translate
156 * messages with t(). If left blank, a default message will be displayed.
158 * @deprecated Scheduled for removal in Drupal 9.0.0.
159 * Use $this->getSession()->getPage()->getText() and substr_count() instead.
161 protected function assertUniqueText($text, $message = NULL) {
162 // Cast MarkupInterface objects to string.
163 $text = (string) $text;
165 $message = $message ?: "'$text' found only once on the page";
166 $page_text = $this->getSession()->getPage()->getText();
167 $nr_found = substr_count($page_text, $text);
168 $this->assertSame(1, $nr_found, $message);
172 * Passes if the text is found MORE THAN ONCE on the text version of the page.
174 * The text version is the equivalent of what a user would see when viewing
175 * through a web browser. In other words the HTML has been filtered out of
178 * @param string|\Drupal\Component\Render\MarkupInterface $text
179 * Plain text to look for.
180 * @param string $message
181 * (optional) A message to display with the assertion. Do not translate
182 * messages with t(). If left blank, a default message will be displayed.
184 * @deprecated Scheduled for removal in Drupal 9.0.0.
185 * Use $this->getSession()->getPage()->getText() and substr_count() instead.
187 protected function assertNoUniqueText($text, $message = '') {
188 // Cast MarkupInterface objects to string.
189 $text = (string) $text;
191 $message = $message ?: "'$text' found more than once on the page";
192 $page_text = $this->getSession()->getPage()->getText();
193 $nr_found = substr_count($page_text, $text);
194 $this->assertGreaterThan(1, $nr_found, $message);
198 * Asserts the page responds with the specified response code.
201 * Response code. For example 200 is a successful page request. For a list
202 * of all codes see http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html.
204 * @deprecated Scheduled for removal in Drupal 9.0.0.
205 * Use $this->assertSession()->statusCodeEquals() instead.
207 protected function assertResponse($code) {
208 $this->assertSession()->statusCodeEquals($code);
212 * Asserts that a field exists with the given name and value.
214 * @param string $name
215 * Name of field to assert.
216 * @param string $value
217 * (optional) Value of the field to assert. You may pass in NULL (default)
218 * to skip checking the actual value, while still checking that the field
221 * @deprecated Scheduled for removal in Drupal 9.0.0.
222 * Use $this->assertSession()->fieldExists() or
223 * $this->assertSession()->fieldValueEquals() instead.
225 protected function assertFieldByName($name, $value = NULL) {
226 $this->assertSession()->fieldExists($name);
227 if ($value !== NULL) {
228 $this->assertSession()->fieldValueEquals($name, (string) $value);
233 * Asserts that a field does not exist with the given name and value.
235 * @param string $name
236 * Name of field to assert.
237 * @param string $value
238 * (optional) Value for the field, to assert that the field's value on the
239 * page does not match it. You may pass in NULL to skip checking the
240 * value, while still checking that the field does not exist. However, the
241 * default value ('') asserts that the field value is not an empty string.
243 * @deprecated Scheduled for removal in Drupal 9.0.0.
244 * Use $this->assertSession()->fieldNotExists() or
245 * $this->assertSession()->fieldValueNotEquals() instead.
247 protected function assertNoFieldByName($name, $value = '') {
248 if ($this->getSession()->getPage()->findField($name) && isset($value)) {
249 $this->assertSession()->fieldValueNotEquals($name, (string) $value);
252 $this->assertSession()->fieldNotExists($name);
257 * Asserts that a field exists with the given ID and value.
260 * ID of field to assert.
261 * @param string|\Drupal\Component\Render\MarkupInterface $value
262 * (optional) Value for the field to assert. You may pass in NULL to skip
263 * checking the value, while still checking that the field exists.
264 * However, the default value ('') asserts that the field value is an empty
267 * @throws \Behat\Mink\Exception\ElementNotFoundException
269 * @deprecated Scheduled for removal in Drupal 9.0.0.
270 * Use $this->assertSession()->fieldExists() or
271 * $this->assertSession()->fieldValueEquals() instead.
273 protected function assertFieldById($id, $value = '') {
274 $xpath = $this->assertSession()->buildXPathQuery('//textarea[@id=:value]|//input[@id=:value]|//select[@id=:value]', [':value' => $id]);
275 $field = $this->getSession()->getPage()->find('xpath', $xpath);
278 throw new ElementNotFoundException($this->getSession()->getDriver(), 'form field', 'id', $field);
281 if ($value !== NULL) {
282 $this->assertEquals($value, $field->getValue());
287 * Asserts that a field exists with the given name or ID.
289 * @param string $field
290 * Name or ID of field to assert.
292 * @deprecated Scheduled for removal in Drupal 9.0.0.
293 * Use $this->assertSession()->fieldExists() instead.
295 protected function assertField($field) {
296 $this->assertSession()->fieldExists($field);
300 * Asserts that a field exists with the given name or ID does NOT exist.
302 * @param string $field
303 * Name or ID of field to assert.
305 * @deprecated Scheduled for removal in Drupal 9.0.0.
306 * Use $this->assertSession()->fieldNotExists() instead.
308 protected function assertNoField($field) {
309 $this->assertSession()->fieldNotExists($field);
313 * Passes if the raw text IS found on the loaded page, fail otherwise.
315 * Raw text refers to the raw HTML that the page generated.
318 * Raw (HTML) string to look for.
320 * @deprecated Scheduled for removal in Drupal 9.0.0.
321 * Use $this->assertSession()->responseContains() instead.
323 protected function assertRaw($raw) {
324 $this->assertSession()->responseContains($raw);
328 * Passes if the raw text IS not found on the loaded page, fail otherwise.
330 * Raw text refers to the raw HTML that the page generated.
333 * Raw (HTML) string to look for.
335 * @deprecated Scheduled for removal in Drupal 9.0.0.
336 * Use $this->assertSession()->responseNotContains() instead.
338 protected function assertNoRaw($raw) {
339 $this->assertSession()->responseNotContains($raw);
343 * Pass if the page title is the given string.
345 * @param string $expected_title
346 * The string the page title should be.
348 * @deprecated Scheduled for removal in Drupal 9.0.0.
349 * Use $this->assertSession()->titleEquals() instead.
351 protected function assertTitle($expected_title) {
352 // Cast MarkupInterface to string.
353 $expected_title = (string) $expected_title;
354 return $this->assertSession()->titleEquals($expected_title);
358 * Passes if a link with the specified label is found.
360 * An optional link index may be passed.
362 * @param string|\Drupal\Component\Render\MarkupInterface $label
363 * Text between the anchor tags.
365 * Link position counting from zero.
367 * @deprecated Scheduled for removal in Drupal 9.0.0.
368 * Use $this->assertSession()->linkExists() instead.
370 protected function assertLink($label, $index = 0) {
371 return $this->assertSession()->linkExists($label, $index);
375 * Passes if a link with the specified label is not found.
377 * @param string|\Drupal\Component\Render\MarkupInterface $label
378 * Text between the anchor tags.
380 * @deprecated Scheduled for removal in Drupal 9.0.0.
381 * Use $this->assertSession()->linkNotExists() instead.
383 protected function assertNoLink($label) {
384 return $this->assertSession()->linkNotExists($label);
388 * Passes if a link containing a given href (part) is found.
390 * @param string $href
391 * The full or partial value of the 'href' attribute of the anchor tag.
393 * Link position counting from zero.
395 * @deprecated Scheduled for removal in Drupal 9.0.0.
396 * Use $this->assertSession()->linkByHrefExists() instead.
398 protected function assertLinkByHref($href, $index = 0) {
399 $this->assertSession()->linkByHrefExists($href, $index);
403 * Passes if a link containing a given href (part) is not found.
405 * @param string $href
406 * The full or partial value of the 'href' attribute of the anchor tag.
408 * @deprecated Scheduled for removal in Drupal 9.0.0.
409 * Use $this->assertSession()->linkByHrefNotExists() instead.
411 protected function assertNoLinkByHref($href) {
412 $this->assertSession()->linkByHrefNotExists($href);
416 * Asserts that a field does not exist with the given ID and value.
419 * ID of field to assert.
420 * @param string $value
421 * (optional) Value for the field, to assert that the field's value on the
422 * page doesn't match it. You may pass in NULL to skip checking the value,
423 * while still checking that the field doesn't exist. However, the default
424 * value ('') asserts that the field value is not an empty string.
426 * @throws \Behat\Mink\Exception\ExpectationException
428 * @deprecated Scheduled for removal in Drupal 9.0.0.
429 * Use $this->assertSession()->fieldNotExists() or
430 * $this->assertSession()->fieldValueNotEquals() instead.
432 protected function assertNoFieldById($id, $value = '') {
433 $xpath = $this->assertSession()->buildXPathQuery('//textarea[@id=:value]|//input[@id=:value]|//select[@id=:value]', [':value' => $id]);
434 $field = $this->getSession()->getPage()->find('xpath', $xpath);
436 // Return early if the field could not be found as expected.
437 if ($field === NULL) {
441 if (!isset($value)) {
442 throw new ExpectationException(sprintf('Id "%s" appears on this page, but it should not.', $id), $this->getSession()->getDriver());
444 elseif ($value === $field->getValue()) {
445 throw new ExpectationException(sprintf('Failed asserting that %s is not equal to %s', $field->getValue(), $value), $this->getSession()->getDriver());
450 * Passes if the internal browser's URL matches the given path.
452 * @param \Drupal\Core\Url|string $path
453 * The expected system path or URL.
455 * @deprecated Scheduled for removal in Drupal 9.0.0.
456 * Use $this->assertSession()->addressEquals() instead.
458 protected function assertUrl($path) {
459 $this->assertSession()->addressEquals($path);
463 * Asserts that a select option in the current page exists.
466 * ID of select field to assert.
467 * @param string $option
470 * @deprecated Scheduled for removal in Drupal 9.0.0.
471 * Use $this->assertSession()->optionExists() instead.
473 protected function assertOption($id, $option) {
474 return $this->assertSession()->optionExists($id, $option);
478 * Asserts that a select option with the visible text exists.
481 * The ID of the select field to assert.
482 * @param string $text
483 * The text for the option tag to assert.
485 * @deprecated Scheduled for removal in Drupal 9.0.0.
486 * Use $this->assertSession()->optionExists() instead.
488 protected function assertOptionByText($id, $text) {
489 return $this->assertSession()->optionExists($id, $text);
493 * Asserts that a select option does NOT exist in the current page.
496 * ID of select field to assert.
497 * @param string $option
500 * @deprecated Scheduled for removal in Drupal 9.0.0.
501 * Use $this->assertSession()->optionNotExists() instead.
503 protected function assertNoOption($id, $option) {
504 return $this->assertSession()->optionNotExists($id, $option);
508 * Asserts that a select option in the current page is checked.
511 * ID of select field to assert.
512 * @param string $option
514 * @param string $message
515 * (optional) A message to display with the assertion. Do not translate
516 * messages with t(). If left blank, a default message will be displayed.
518 * @deprecated Scheduled for removal in Drupal 9.0.0.
519 * Use $this->assertSession()->optionExists() instead and check the
520 * "selected" attribute yourself.
522 protected function assertOptionSelected($id, $option, $message = NULL) {
523 $option_field = $this->assertSession()->optionExists($id, $option);
524 $message = $message ?: "Option $option for field $id is selected.";
525 $this->assertTrue($option_field->hasAttribute('selected'), $message);
529 * Asserts that a checkbox field in the current page is checked.
532 * ID of field to assert.
534 * @deprecated Scheduled for removal in Drupal 9.0.0.
535 * Use $this->assertSession()->checkboxChecked() instead.
537 protected function assertFieldChecked($id) {
538 $this->assertSession()->checkboxChecked($id);
542 * Asserts that a checkbox field in the current page is not checked.
545 * ID of field to assert.
547 * @deprecated Scheduled for removal in Drupal 9.0.0.
548 * Use $this->assertSession()->checkboxNotChecked() instead.
550 protected function assertNoFieldChecked($id) {
551 $this->assertSession()->checkboxNotChecked($id);
555 * Asserts that a field exists in the current page by the given XPath.
557 * @param string $xpath
558 * XPath used to find the field.
559 * @param string $value
560 * (optional) Value of the field to assert. You may pass in NULL (default)
561 * to skip checking the actual value, while still checking that the field
563 * @param string $message
564 * (optional) A message to display with the assertion. Do not translate
567 * @deprecated Scheduled for removal in Drupal 9.0.0.
568 * Use $this->xpath() instead and check the values directly in the test.
570 protected function assertFieldByXPath($xpath, $value = NULL, $message = '') {
571 $fields = $this->xpath($xpath);
573 $this->assertFieldsByValue($fields, $value, $message);
577 * Asserts that a field does not exist or its value does not match, by XPath.
579 * @param string $xpath
580 * XPath used to find the field.
581 * @param string $value
582 * (optional) Value of the field, to assert that the field's value on the
583 * page does not match it.
584 * @param string $message
585 * (optional) A message to display with the assertion. Do not translate
588 * @deprecated Scheduled for removal in Drupal 9.0.0.
589 * Use $this->xpath() instead and assert that the result is empty.
591 protected function assertNoFieldByXPath($xpath, $value = NULL, $message = '') {
592 $fields = $this->xpath($xpath);
594 // If value specified then check array for match.
599 foreach ($fields as $field) {
600 if ($field->getAttribute('value') == $value) {
606 return $this->assertFalse($fields && $found, $message);
610 * Asserts that a field exists in the current page with a given Xpath result.
612 * @param \Behat\Mink\Element\NodeElement[] $fields
614 * @param string $value
615 * (optional) Value of the field to assert. You may pass in NULL (default) to skip
616 * checking the actual value, while still checking that the field exists.
617 * @param string $message
618 * (optional) A message to display with the assertion. Do not translate
621 * @deprecated Scheduled for removal in Drupal 9.0.0.
622 * Iterate over the fields yourself instead and directly check the values in
625 protected function assertFieldsByValue($fields, $value = NULL, $message = '') {
626 // If value specified then check array for match.
631 foreach ($fields as $field) {
632 if ($field->getAttribute('value') == $value) {
633 // Input element with correct value.
636 elseif ($field->find('xpath', '//option[@value = ' . (new Escaper())->escapeLiteral($value) . ' and @selected = "selected"]')) {
637 // Select element with an option.
640 elseif ($field->getText() == $value) {
641 // Text area with correct text.
647 $this->assertTrue($fields && $found, $message);
651 * Passes if the raw text IS found escaped on the loaded page, fail otherwise.
653 * Raw text refers to the raw HTML that the page generated.
656 * Raw (HTML) string to look for.
658 * @deprecated Scheduled for removal in Drupal 9.0.0.
659 * Use $this->assertSession()->assertEscaped() instead.
661 protected function assertEscaped($raw) {
662 $this->assertSession()->assertEscaped($raw);
666 * Passes if the raw text is not found escaped on the loaded page.
668 * Raw text refers to the raw HTML that the page generated.
671 * Raw (HTML) string to look for.
673 * @deprecated Scheduled for removal in Drupal 9.0.0.
674 * Use $this->assertSession()->assertNoEscaped() instead.
676 protected function assertNoEscaped($raw) {
677 $this->assertSession()->assertNoEscaped($raw);
681 * Triggers a pass if the Perl regex pattern is found in the raw content.
683 * @param string $pattern
684 * Perl regex to look for including the regex delimiters.
686 * @deprecated Scheduled for removal in Drupal 9.0.0.
687 * Use $this->assertSession()->responseMatches() instead.
689 protected function assertPattern($pattern) {
690 $this->assertSession()->responseMatches($pattern);
694 * Triggers a pass if the Perl regex pattern is not found in the raw content.
696 * @param string $pattern
697 * Perl regex to look for including the regex delimiters.
699 * @deprecated Scheduled for removal in Drupal 9.0.0.
700 * Use $this->assertSession()->responseNotMatches() instead.
702 * @see https://www.drupal.org/node/2864262
704 protected function assertNoPattern($pattern) {
705 @trigger_error('assertNoPattern() is deprecated and scheduled for removal in Drupal 9.0.0. Use $this->assertSession()->responseNotMatches($pattern) instead. See https://www.drupal.org/node/2864262.', E_USER_DEPRECATED);
706 $this->assertSession()->responseNotMatches($pattern);
710 * Asserts whether an expected cache tag was present in the last response.
712 * @param string $expected_cache_tag
713 * The expected cache tag.
715 * @deprecated Scheduled for removal in Drupal 9.0.0.
716 * Use $this->assertSession()->responseHeaderContains() instead.
718 protected function assertCacheTag($expected_cache_tag) {
719 $this->assertSession()->responseHeaderContains('X-Drupal-Cache-Tags', $expected_cache_tag);
723 * Checks that current response header equals value.
725 * @param string $name
726 * Name of header to assert.
727 * @param string $value
728 * Value of the header to assert
730 * @deprecated Scheduled for removal in Drupal 9.0.0.
731 * Use $this->assertSession()->responseHeaderEquals() instead.
733 protected function assertHeader($name, $value) {
734 $this->assertSession()->responseHeaderEquals($name, $value);
738 * Returns WebAssert object.
740 * @param string $name
741 * (optional) Name of the session. Defaults to the active session.
743 * @return \Drupal\Tests\WebAssert
744 * A new web-assert option for asserting the presence of elements with.
746 abstract public function assertSession($name = NULL);
749 * Builds an XPath query.
751 * Builds an XPath query by replacing placeholders in the query by the value
754 * XPath 1.0 (the version supported by libxml2, the underlying XML library
755 * used by PHP) doesn't support any form of quotation. This function
756 * simplifies the building of XPath expression.
758 * @param string $xpath
759 * An XPath query, possibly with placeholders in the form ':name'.
761 * An array of arguments with keys in the form ':name' matching the
762 * placeholders in the query. The values may be either strings or numeric
766 * An XPath query with arguments replaced.
768 * @deprecated Scheduled for removal in Drupal 9.0.0.
769 * Use $this->assertSession()->buildXPathQuery() instead.
771 protected function buildXPathQuery($xpath, array $args = []) {
772 return $this->assertSession()->buildXPathQuery($xpath, $args);
776 * Gets the current raw content.
778 * @deprecated Scheduled for removal in Drupal 9.0.0.
779 * Use $this->getSession()->getPage()->getContent() instead.
781 protected function getRawContent() {
782 @trigger_error('AssertLegacyTrait::getRawContent() is scheduled for removal in Drupal 9.0.0. Use $this->getSession()->getPage()->getContent() instead.', E_USER_DEPRECATED);
783 return $this->getSession()->getPage()->getContent();