Chris@0: assertSession()->statusCodeEquals(200); Chris@0: * @endcode Chris@18: * Chris@18: * @todo https://www.drupal.org/project/drupal/issues/3031580 Note that Chris@18: * deprecations in this file do not use the @ symbol in Drupal 8 because this Chris@18: * will be removed in Drupal 10.0.0. Adding the @ back should re-enable coding Chris@18: * stanadrds checks. Chris@0: */ Chris@0: trait AssertLegacyTrait { Chris@0: Chris@0: use BaseAssertLegacyTrait; Chris@0: Chris@0: /** Chris@0: * Asserts that the element with the given CSS selector is present. Chris@0: * Chris@0: * @param string $css_selector Chris@0: * The CSS selector identifying the element to check. Chris@0: * Chris@18: * Deprecated Scheduled for removal in Drupal 10.0.0. Chris@0: * Use $this->assertSession()->elementExists() instead. Chris@0: */ Chris@0: protected function assertElementPresent($css_selector) { Chris@0: $this->assertSession()->elementExists('css', $css_selector); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Asserts that the element with the given CSS selector is not present. Chris@0: * Chris@0: * @param string $css_selector Chris@0: * The CSS selector identifying the element to check. Chris@0: * Chris@18: * Deprecated Scheduled for removal in Drupal 10.0.0. Chris@0: * Use $this->assertSession()->elementNotExists() instead. Chris@0: */ Chris@0: protected function assertElementNotPresent($css_selector) { Chris@0: $this->assertSession()->elementNotExists('css', $css_selector); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Passes if the page (with HTML stripped) contains the text. Chris@0: * Chris@0: * Note that stripping HTML tags also removes their attributes, such as Chris@0: * the values of text fields. Chris@0: * Chris@0: * @param string $text Chris@0: * Plain text to look for. Chris@0: * Chris@18: * Deprecated Scheduled for removal in Drupal 10.0.0. Chris@0: * Use instead: Chris@0: * - $this->assertSession()->responseContains() for non-HTML responses, Chris@0: * like XML or Json. Chris@0: * - $this->assertSession()->pageTextContains() for HTML responses. Unlike Chris@0: * the deprecated assertText(), the passed text should be HTML decoded, Chris@0: * exactly as a human sees it in the browser. Chris@0: */ Chris@0: protected function assertText($text) { Chris@0: // Cast MarkupInterface to string. Chris@0: $text = (string) $text; Chris@0: Chris@0: $content_type = $this->getSession()->getResponseHeader('Content-type'); Chris@0: // In case of a Non-HTML response (example: XML) check the original Chris@0: // response. Chris@0: if (strpos($content_type, 'html') === FALSE) { Chris@0: $this->assertSession()->responseContains($text); Chris@0: } Chris@0: else { Chris@0: $this->assertTextHelper($text, FALSE); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Passes if the page (with HTML stripped) does not contains the text. Chris@0: * Chris@0: * Note that stripping HTML tags also removes their attributes, such as Chris@0: * the values of text fields. Chris@0: * Chris@0: * @param string $text Chris@0: * Plain text to look for. Chris@0: * Chris@18: * Deprecated Scheduled for removal in Drupal 10.0.0. Chris@0: * Use instead: Chris@0: * - $this->assertSession()->responseNotContains() for non-HTML responses, Chris@0: * like XML or Json. Chris@0: * - $this->assertSession()->pageTextNotContains() for HTML responses. Chris@0: * Unlike the deprecated assertNoText(), the passed text should be HTML Chris@0: * decoded, exactly as a human sees it in the browser. Chris@0: */ Chris@0: protected function assertNoText($text) { Chris@0: // Cast MarkupInterface to string. Chris@0: $text = (string) $text; Chris@0: Chris@0: $content_type = $this->getSession()->getResponseHeader('Content-type'); Chris@0: // In case of a Non-HTML response (example: XML) check the original Chris@0: // response. Chris@0: if (strpos($content_type, 'html') === FALSE) { Chris@0: $this->assertSession()->responseNotContains($text); Chris@0: } Chris@0: else { Chris@0: $this->assertTextHelper($text); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Helper for assertText and assertNoText. Chris@0: * Chris@0: * @param string $text Chris@0: * Plain text to look for. Chris@0: * @param bool $not_exists Chris@0: * (optional) TRUE if this text should not exist, FALSE if it should. Chris@0: * Defaults to TRUE. Chris@0: * Chris@0: * @return bool Chris@0: * TRUE on pass, FALSE on fail. Chris@0: */ Chris@0: protected function assertTextHelper($text, $not_exists = TRUE) { Chris@0: $args = ['@text' => $text]; Chris@0: $message = $not_exists ? new FormattableMarkup('"@text" not found', $args) : new FormattableMarkup('"@text" found', $args); Chris@0: Chris@0: $raw_content = $this->getSession()->getPage()->getContent(); Chris@0: // Trying to simulate what the user sees, given that it removes all text Chris@0: // inside the head tags, removes inline Javascript, fix all HTML entities, Chris@0: // removes dangerous protocols and filtering out all HTML tags, as they are Chris@0: // not visible in a normal browser. Chris@0: $raw_content = preg_replace('@(.+?)@si', '', $raw_content); Chris@0: $page_text = Xss::filter($raw_content, []); Chris@0: Chris@0: $actual = $not_exists == (strpos($page_text, (string) $text) === FALSE); Chris@0: $this->assertTrue($actual, $message); Chris@0: Chris@0: return $actual; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Passes if the text is found ONLY ONCE on the text version of the page. Chris@0: * Chris@0: * The text version is the equivalent of what a user would see when viewing Chris@0: * through a web browser. In other words the HTML has been filtered out of Chris@0: * the contents. Chris@0: * Chris@0: * @param string|\Drupal\Component\Render\MarkupInterface $text Chris@0: * Plain text to look for. Chris@0: * @param string $message Chris@0: * (optional) A message to display with the assertion. Do not translate Chris@0: * messages with t(). If left blank, a default message will be displayed. Chris@0: * Chris@18: * Deprecated Scheduled for removal in Drupal 10.0.0. Chris@0: * Use $this->getSession()->getPage()->getText() and substr_count() instead. Chris@0: */ Chris@0: protected function assertUniqueText($text, $message = NULL) { Chris@0: // Cast MarkupInterface objects to string. Chris@0: $text = (string) $text; Chris@0: Chris@0: $message = $message ?: "'$text' found only once on the page"; Chris@0: $page_text = $this->getSession()->getPage()->getText(); Chris@0: $nr_found = substr_count($page_text, $text); Chris@0: $this->assertSame(1, $nr_found, $message); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Passes if the text is found MORE THAN ONCE on the text version of the page. Chris@0: * Chris@0: * The text version is the equivalent of what a user would see when viewing Chris@0: * through a web browser. In other words the HTML has been filtered out of Chris@0: * the contents. Chris@0: * Chris@0: * @param string|\Drupal\Component\Render\MarkupInterface $text Chris@0: * Plain text to look for. Chris@0: * @param string $message Chris@0: * (optional) A message to display with the assertion. Do not translate Chris@0: * messages with t(). If left blank, a default message will be displayed. Chris@0: * Chris@18: * Deprecated Scheduled for removal in Drupal 10.0.0. Chris@0: * Use $this->getSession()->getPage()->getText() and substr_count() instead. Chris@0: */ Chris@0: protected function assertNoUniqueText($text, $message = '') { Chris@0: // Cast MarkupInterface objects to string. Chris@0: $text = (string) $text; Chris@0: Chris@0: $message = $message ?: "'$text' found more than once on the page"; Chris@0: $page_text = $this->getSession()->getPage()->getText(); Chris@0: $nr_found = substr_count($page_text, $text); Chris@0: $this->assertGreaterThan(1, $nr_found, $message); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Asserts the page responds with the specified response code. Chris@0: * Chris@0: * @param int $code Chris@0: * Response code. For example 200 is a successful page request. For a list Chris@0: * of all codes see http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html. Chris@0: * Chris@18: * Deprecated Scheduled for removal in Drupal 10.0.0. Chris@0: * Use $this->assertSession()->statusCodeEquals() instead. Chris@0: */ Chris@0: protected function assertResponse($code) { Chris@0: $this->assertSession()->statusCodeEquals($code); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Asserts that a field exists with the given name and value. Chris@0: * Chris@0: * @param string $name Chris@0: * Name of field to assert. Chris@0: * @param string $value Chris@0: * (optional) Value of the field to assert. You may pass in NULL (default) Chris@0: * to skip checking the actual value, while still checking that the field Chris@0: * exists. Chris@0: * Chris@18: * Deprecated Scheduled for removal in Drupal 10.0.0. Chris@0: * Use $this->assertSession()->fieldExists() or Chris@0: * $this->assertSession()->buttonExists() or Chris@0: * $this->assertSession()->fieldValueEquals() instead. Chris@0: */ Chris@0: protected function assertFieldByName($name, $value = NULL) { Chris@0: $this->assertFieldByXPath($this->constructFieldXpath('name', $name), $value); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Asserts that a field does not exist with the given name and value. Chris@0: * Chris@0: * @param string $name Chris@0: * Name of field to assert. Chris@0: * @param string $value Chris@0: * (optional) Value for the field, to assert that the field's value on the Chris@0: * page does not match it. You may pass in NULL to skip checking the Chris@0: * value, while still checking that the field does not exist. However, the Chris@0: * default value ('') asserts that the field value is not an empty string. Chris@0: * Chris@18: * Deprecated Scheduled for removal in Drupal 10.0.0. Chris@0: * Use $this->assertSession()->fieldNotExists() or Chris@0: * $this->assertSession()->buttonNotExists() or Chris@0: * $this->assertSession()->fieldValueNotEquals() instead. Chris@0: */ Chris@0: protected function assertNoFieldByName($name, $value = '') { Chris@0: $this->assertNoFieldByXPath($this->constructFieldXpath('name', $name), $value); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Asserts that a field exists with the given ID and value. Chris@0: * Chris@0: * @param string $id Chris@0: * ID of field to assert. Chris@0: * @param string|\Drupal\Component\Render\MarkupInterface $value Chris@0: * (optional) Value for the field to assert. You may pass in NULL to skip Chris@0: * checking the value, while still checking that the field exists. Chris@0: * However, the default value ('') asserts that the field value is an empty Chris@0: * string. Chris@0: * Chris@0: * @throws \Behat\Mink\Exception\ElementNotFoundException Chris@0: * Chris@18: * Deprecated Scheduled for removal in Drupal 10.0.0. Chris@0: * Use $this->assertSession()->fieldExists() or Chris@0: * $this->assertSession()->buttonExists() or Chris@0: * $this->assertSession()->fieldValueEquals() instead. Chris@0: */ Chris@0: protected function assertFieldById($id, $value = '') { Chris@0: $this->assertFieldByXPath($this->constructFieldXpath('id', $id), $value); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Asserts that a field exists with the given name or ID. Chris@0: * Chris@0: * @param string $field Chris@0: * Name or ID of field to assert. Chris@0: * Chris@18: * Deprecated Scheduled for removal in Drupal 10.0.0. Chris@0: * Use $this->assertSession()->fieldExists() or Chris@0: * $this->assertSession()->buttonExists() instead. Chris@0: */ Chris@0: protected function assertField($field) { Chris@0: $this->assertFieldByXPath($this->constructFieldXpath('name', $field) . '|' . $this->constructFieldXpath('id', $field)); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Asserts that a field does NOT exist with the given name or ID. Chris@0: * Chris@0: * @param string $field Chris@0: * Name or ID of field to assert. Chris@0: * Chris@18: * Deprecated Scheduled for removal in Drupal 10.0.0. Chris@0: * Use $this->assertSession()->fieldNotExists() or Chris@0: * $this->assertSession()->buttonNotExists() instead. Chris@0: */ Chris@0: protected function assertNoField($field) { Chris@0: $this->assertNoFieldByXPath($this->constructFieldXpath('name', $field) . '|' . $this->constructFieldXpath('id', $field)); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Passes if the raw text IS found on the loaded page, fail otherwise. Chris@0: * Chris@0: * Raw text refers to the raw HTML that the page generated. Chris@0: * Chris@0: * @param string $raw Chris@0: * Raw (HTML) string to look for. Chris@0: * Chris@18: * Deprecated Scheduled for removal in Drupal 10.0.0. Chris@0: * Use $this->assertSession()->responseContains() instead. Chris@0: */ Chris@0: protected function assertRaw($raw) { Chris@0: $this->assertSession()->responseContains($raw); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Passes if the raw text IS not found on the loaded page, fail otherwise. Chris@0: * Chris@0: * Raw text refers to the raw HTML that the page generated. Chris@0: * Chris@0: * @param string $raw Chris@0: * Raw (HTML) string to look for. Chris@0: * Chris@18: * Deprecated Scheduled for removal in Drupal 10.0.0. Chris@0: * Use $this->assertSession()->responseNotContains() instead. Chris@0: */ Chris@0: protected function assertNoRaw($raw) { Chris@0: $this->assertSession()->responseNotContains($raw); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Pass if the page title is the given string. Chris@0: * Chris@0: * @param string $expected_title Chris@0: * The string the page title should be. Chris@0: * Chris@18: * Deprecated Scheduled for removal in Drupal 10.0.0. Chris@0: * Use $this->assertSession()->titleEquals() instead. Chris@0: */ Chris@0: protected function assertTitle($expected_title) { Chris@0: // Cast MarkupInterface to string. Chris@0: $expected_title = (string) $expected_title; Chris@0: return $this->assertSession()->titleEquals($expected_title); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Passes if a link with the specified label is found. Chris@0: * Chris@0: * An optional link index may be passed. Chris@0: * Chris@0: * @param string|\Drupal\Component\Render\MarkupInterface $label Chris@0: * Text between the anchor tags. Chris@0: * @param int $index Chris@0: * Link position counting from zero. Chris@0: * Chris@18: * Deprecated Scheduled for removal in Drupal 10.0.0. Chris@0: * Use $this->assertSession()->linkExists() instead. Chris@0: */ Chris@0: protected function assertLink($label, $index = 0) { Chris@0: return $this->assertSession()->linkExists($label, $index); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Passes if a link with the specified label is not found. Chris@0: * Chris@0: * @param string|\Drupal\Component\Render\MarkupInterface $label Chris@0: * Text between the anchor tags. Chris@0: * Chris@18: * Deprecated Scheduled for removal in Drupal 10.0.0. Chris@0: * Use $this->assertSession()->linkNotExists() instead. Chris@0: */ Chris@0: protected function assertNoLink($label) { Chris@0: return $this->assertSession()->linkNotExists($label); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Passes if a link containing a given href (part) is found. Chris@0: * Chris@0: * @param string $href Chris@0: * The full or partial value of the 'href' attribute of the anchor tag. Chris@0: * @param int $index Chris@0: * Link position counting from zero. Chris@0: * Chris@18: * Deprecated Scheduled for removal in Drupal 10.0.0. Chris@0: * Use $this->assertSession()->linkByHrefExists() instead. Chris@0: */ Chris@0: protected function assertLinkByHref($href, $index = 0) { Chris@0: $this->assertSession()->linkByHrefExists($href, $index); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Passes if a link containing a given href (part) is not found. Chris@0: * Chris@0: * @param string $href Chris@0: * The full or partial value of the 'href' attribute of the anchor tag. Chris@0: * Chris@18: * Deprecated Scheduled for removal in Drupal 10.0.0. Chris@0: * Use $this->assertSession()->linkByHrefNotExists() instead. Chris@0: */ Chris@0: protected function assertNoLinkByHref($href) { Chris@0: $this->assertSession()->linkByHrefNotExists($href); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Asserts that a field does not exist with the given ID and value. Chris@0: * Chris@0: * @param string $id Chris@0: * ID of field to assert. Chris@0: * @param string $value Chris@0: * (optional) Value for the field, to assert that the field's value on the Chris@0: * page doesn't match it. You may pass in NULL to skip checking the value, Chris@0: * while still checking that the field doesn't exist. However, the default Chris@0: * value ('') asserts that the field value is not an empty string. Chris@0: * Chris@0: * @throws \Behat\Mink\Exception\ExpectationException Chris@0: * Chris@18: * Deprecated Scheduled for removal in Drupal 10.0.0. Chris@0: * Use $this->assertSession()->fieldNotExists() or Chris@0: * $this->assertSession()->buttonNotExists() or Chris@0: * $this->assertSession()->fieldValueNotEquals() instead. Chris@0: */ Chris@0: protected function assertNoFieldById($id, $value = '') { Chris@0: $this->assertNoFieldByXPath($this->constructFieldXpath('id', $id), $value); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Passes if the internal browser's URL matches the given path. Chris@0: * Chris@0: * @param \Drupal\Core\Url|string $path Chris@0: * The expected system path or URL. Chris@0: * Chris@18: * Deprecated Scheduled for removal in Drupal 10.0.0. Chris@0: * Use $this->assertSession()->addressEquals() instead. Chris@0: */ Chris@0: protected function assertUrl($path) { Chris@0: $this->assertSession()->addressEquals($path); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Asserts that a select option in the current page exists. Chris@0: * Chris@0: * @param string $id Chris@0: * ID of select field to assert. Chris@0: * @param string $option Chris@0: * Option to assert. Chris@0: * Chris@18: * Deprecated Scheduled for removal in Drupal 10.0.0. Chris@0: * Use $this->assertSession()->optionExists() instead. Chris@0: */ Chris@0: protected function assertOption($id, $option) { Chris@0: return $this->assertSession()->optionExists($id, $option); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Asserts that a select option with the visible text exists. Chris@0: * Chris@0: * @param string $id Chris@0: * The ID of the select field to assert. Chris@0: * @param string $text Chris@0: * The text for the option tag to assert. Chris@0: * Chris@18: * Deprecated Scheduled for removal in Drupal 10.0.0. Chris@0: * Use $this->assertSession()->optionExists() instead. Chris@0: */ Chris@0: protected function assertOptionByText($id, $text) { Chris@0: return $this->assertSession()->optionExists($id, $text); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Asserts that a select option does NOT exist in the current page. Chris@0: * Chris@0: * @param string $id Chris@0: * ID of select field to assert. Chris@0: * @param string $option Chris@0: * Option to assert. Chris@0: * Chris@18: * Deprecated Scheduled for removal in Drupal 10.0.0. Chris@0: * Use $this->assertSession()->optionNotExists() instead. Chris@0: */ Chris@0: protected function assertNoOption($id, $option) { Chris@0: return $this->assertSession()->optionNotExists($id, $option); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Asserts that a select option in the current page is checked. Chris@0: * Chris@0: * @param string $id Chris@0: * ID of select field to assert. Chris@0: * @param string $option Chris@0: * Option to assert. Chris@0: * @param string $message Chris@0: * (optional) A message to display with the assertion. Do not translate Chris@0: * messages with t(). If left blank, a default message will be displayed. Chris@0: * Chris@18: * Deprecated Scheduled for removal in Drupal 10.0.0. Chris@0: * Use $this->assertSession()->optionExists() instead and check the Chris@0: * "selected" attribute yourself. Chris@0: */ Chris@0: protected function assertOptionSelected($id, $option, $message = NULL) { Chris@0: $option_field = $this->assertSession()->optionExists($id, $option); Chris@0: $message = $message ?: "Option $option for field $id is selected."; Chris@0: $this->assertTrue($option_field->hasAttribute('selected'), $message); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Asserts that a checkbox field in the current page is checked. Chris@0: * Chris@0: * @param string $id Chris@0: * ID of field to assert. Chris@0: * Chris@18: * Deprecated Scheduled for removal in Drupal 10.0.0. Chris@0: * Use $this->assertSession()->checkboxChecked() instead. Chris@0: */ Chris@0: protected function assertFieldChecked($id) { Chris@0: $this->assertSession()->checkboxChecked($id); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Asserts that a checkbox field in the current page is not checked. Chris@0: * Chris@0: * @param string $id Chris@0: * ID of field to assert. Chris@0: * Chris@18: * Deprecated Scheduled for removal in Drupal 10.0.0. Chris@0: * Use $this->assertSession()->checkboxNotChecked() instead. Chris@0: */ Chris@0: protected function assertNoFieldChecked($id) { Chris@0: $this->assertSession()->checkboxNotChecked($id); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Asserts that a field exists in the current page by the given XPath. Chris@0: * Chris@0: * @param string $xpath Chris@0: * XPath used to find the field. Chris@0: * @param string $value Chris@0: * (optional) Value of the field to assert. You may pass in NULL (default) Chris@0: * to skip checking the actual value, while still checking that the field Chris@0: * exists. Chris@0: * @param string $message Chris@0: * (optional) A message to display with the assertion. Do not translate Chris@0: * messages with t(). Chris@0: * Chris@18: * Deprecated Scheduled for removal in Drupal 10.0.0. Chris@0: * Use $this->xpath() instead and check the values directly in the test. Chris@0: */ Chris@0: protected function assertFieldByXPath($xpath, $value = NULL, $message = '') { Chris@0: $fields = $this->xpath($xpath); Chris@0: Chris@0: $this->assertFieldsByValue($fields, $value, $message); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Asserts that a field does not exist or its value does not match, by XPath. Chris@0: * Chris@0: * @param string $xpath Chris@0: * XPath used to find the field. Chris@0: * @param string $value Chris@0: * (optional) Value of the field, to assert that the field's value on the Chris@0: * page does not match it. Chris@0: * @param string $message Chris@0: * (optional) A message to display with the assertion. Do not translate Chris@0: * messages with t(). Chris@0: * Chris@0: * @throws \Behat\Mink\Exception\ExpectationException Chris@0: * Chris@18: * Deprecated Scheduled for removal in Drupal 10.0.0. Chris@0: * Use $this->xpath() instead and assert that the result is empty. Chris@0: */ Chris@0: protected function assertNoFieldByXPath($xpath, $value = NULL, $message = '') { Chris@0: $fields = $this->xpath($xpath); Chris@0: Chris@0: if (!empty($fields)) { Chris@0: if (isset($value)) { Chris@0: $found = FALSE; Chris@0: try { Chris@0: $this->assertFieldsByValue($fields, $value); Chris@0: $found = TRUE; Chris@0: } Chris@0: catch (\Exception $e) { Chris@0: } Chris@0: Chris@0: if ($found) { Chris@0: throw new ExpectationException(sprintf('The field resulting from %s was found with the provided value %s.', $xpath, $value), $this->getSession()->getDriver()); Chris@0: } Chris@0: } Chris@0: else { Chris@0: throw new ExpectationException(sprintf('The field resulting from %s was found.', $xpath), $this->getSession()->getDriver()); Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Asserts that a field exists in the current page with a given Xpath result. Chris@0: * Chris@0: * @param \Behat\Mink\Element\NodeElement[] $fields Chris@0: * Xml elements. Chris@0: * @param string $value Chris@0: * (optional) Value of the field to assert. You may pass in NULL (default) to skip Chris@0: * checking the actual value, while still checking that the field exists. Chris@0: * @param string $message Chris@0: * (optional) A message to display with the assertion. Do not translate Chris@0: * messages with t(). Chris@0: * Chris@18: * Deprecated Scheduled for removal in Drupal 10.0.0. Chris@0: * Iterate over the fields yourself instead and directly check the values in Chris@0: * the test. Chris@0: */ Chris@0: protected function assertFieldsByValue($fields, $value = NULL, $message = '') { Chris@0: // If value specified then check array for match. Chris@0: $found = TRUE; Chris@0: if (isset($value)) { Chris@0: $found = FALSE; Chris@0: if ($fields) { Chris@0: foreach ($fields as $field) { Chris@0: if ($field->getAttribute('type') == 'checkbox') { Chris@0: if (is_bool($value)) { Chris@0: $found = $field->isChecked() == $value; Chris@0: } Chris@0: else { Chris@0: $found = TRUE; Chris@0: } Chris@0: } Chris@0: elseif ($field->getAttribute('value') == $value) { Chris@0: // Input element with correct value. Chris@0: $found = TRUE; Chris@0: } Chris@0: elseif ($field->find('xpath', '//option[@value = ' . (new Escaper())->escapeLiteral($value) . ' and @selected = "selected"]')) { Chris@0: // Select element with an option. Chris@0: $found = TRUE; Chris@0: } Chris@0: elseif ($field->getTagName() === 'textarea' && $field->getValue() == $value) { Chris@0: // Text area with correct text. Use getValue() here because Chris@0: // getText() would remove any newlines in the value. Chris@0: $found = TRUE; Chris@0: } Chris@0: elseif ($field->getTagName() !== 'input' && $field->getText() == $value) { Chris@0: $found = TRUE; Chris@0: } Chris@0: } Chris@0: } Chris@0: } Chris@0: $this->assertTrue($fields && $found, $message); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Passes if the raw text IS found escaped on the loaded page, fail otherwise. Chris@0: * Chris@0: * Raw text refers to the raw HTML that the page generated. Chris@0: * Chris@0: * @param string $raw Chris@0: * Raw (HTML) string to look for. Chris@0: * Chris@18: * Deprecated Scheduled for removal in Drupal 10.0.0. Chris@0: * Use $this->assertSession()->assertEscaped() instead. Chris@0: */ Chris@0: protected function assertEscaped($raw) { Chris@0: $this->assertSession()->assertEscaped($raw); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Passes if the raw text is not found escaped on the loaded page. Chris@0: * Chris@0: * Raw text refers to the raw HTML that the page generated. Chris@0: * Chris@0: * @param string $raw Chris@0: * Raw (HTML) string to look for. Chris@0: * Chris@18: * Deprecated Scheduled for removal in Drupal 10.0.0. Chris@0: * Use $this->assertSession()->assertNoEscaped() instead. Chris@0: */ Chris@0: protected function assertNoEscaped($raw) { Chris@0: $this->assertSession()->assertNoEscaped($raw); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Triggers a pass if the Perl regex pattern is found in the raw content. Chris@0: * Chris@0: * @param string $pattern Chris@0: * Perl regex to look for including the regex delimiters. Chris@0: * Chris@18: * Deprecated Scheduled for removal in Drupal 10.0.0. Chris@0: * Use $this->assertSession()->responseMatches() instead. Chris@0: */ Chris@0: protected function assertPattern($pattern) { Chris@0: $this->assertSession()->responseMatches($pattern); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Triggers a pass if the Perl regex pattern is not found in the raw content. Chris@0: * Chris@0: * @param string $pattern Chris@0: * Perl regex to look for including the regex delimiters. Chris@0: * Chris@18: * Deprecated Scheduled for removal in Drupal 10.0.0. Chris@0: * Use $this->assertSession()->responseNotMatches() instead. Chris@0: * Chris@0: * @see https://www.drupal.org/node/2864262 Chris@0: */ Chris@0: protected function assertNoPattern($pattern) { Chris@0: @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); Chris@0: $this->assertSession()->responseNotMatches($pattern); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Asserts whether an expected cache tag was present in the last response. Chris@0: * Chris@0: * @param string $expected_cache_tag Chris@0: * The expected cache tag. Chris@0: * Chris@18: * Deprecated Scheduled for removal in Drupal 10.0.0. Chris@0: * Use $this->assertSession()->responseHeaderContains() instead. Chris@0: */ Chris@0: protected function assertCacheTag($expected_cache_tag) { Chris@0: $this->assertSession()->responseHeaderContains('X-Drupal-Cache-Tags', $expected_cache_tag); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Asserts whether an expected cache tag was absent in the last response. Chris@0: * Chris@0: * @param string $cache_tag Chris@0: * The cache tag to check. Chris@0: * Chris@18: * Deprecated Scheduled for removal in Drupal 10.0.0. Chris@0: * Use $this->assertSession()->responseHeaderNotContains() instead. Chris@0: * Chris@0: * @see https://www.drupal.org/node/2864029 Chris@0: */ Chris@0: protected function assertNoCacheTag($cache_tag) { Chris@0: @trigger_error('assertNoCacheTag() is deprecated and scheduled for removal in Drupal 9.0.0. Use $this->assertSession()->responseHeaderNotContains() instead. See https://www.drupal.org/node/2864029.', E_USER_DEPRECATED); Chris@0: $this->assertSession()->responseHeaderNotContains('X-Drupal-Cache-Tags', $cache_tag); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Checks that current response header equals value. Chris@0: * Chris@0: * @param string $name Chris@0: * Name of header to assert. Chris@0: * @param string $value Chris@0: * Value of the header to assert Chris@0: * Chris@18: * Deprecated Scheduled for removal in Drupal 10.0.0. Chris@0: * Use $this->assertSession()->responseHeaderEquals() instead. Chris@0: */ Chris@0: protected function assertHeader($name, $value) { Chris@0: $this->assertSession()->responseHeaderEquals($name, $value); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Returns WebAssert object. Chris@0: * Chris@0: * @param string $name Chris@0: * (optional) Name of the session. Defaults to the active session. Chris@0: * Chris@0: * @return \Drupal\Tests\WebAssert Chris@0: * A new web-assert option for asserting the presence of elements with. Chris@0: */ Chris@0: abstract public function assertSession($name = NULL); Chris@0: Chris@0: /** Chris@0: * Builds an XPath query. Chris@0: * Chris@0: * Builds an XPath query by replacing placeholders in the query by the value Chris@0: * of the arguments. Chris@0: * Chris@0: * XPath 1.0 (the version supported by libxml2, the underlying XML library Chris@0: * used by PHP) doesn't support any form of quotation. This function Chris@0: * simplifies the building of XPath expression. Chris@0: * Chris@0: * @param string $xpath Chris@0: * An XPath query, possibly with placeholders in the form ':name'. Chris@0: * @param array $args Chris@0: * An array of arguments with keys in the form ':name' matching the Chris@0: * placeholders in the query. The values may be either strings or numeric Chris@0: * values. Chris@0: * Chris@0: * @return string Chris@0: * An XPath query with arguments replaced. Chris@0: * Chris@18: * Deprecated Scheduled for removal in Drupal 10.0.0. Chris@0: * Use $this->assertSession()->buildXPathQuery() instead. Chris@0: */ Chris@0: protected function buildXPathQuery($xpath, array $args = []) { Chris@0: return $this->assertSession()->buildXPathQuery($xpath, $args); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Helper: Constructs an XPath for the given set of attributes and value. Chris@0: * Chris@0: * @param string $attribute Chris@0: * Field attributes. Chris@0: * @param string $value Chris@0: * Value of field. Chris@0: * Chris@0: * @return string Chris@0: * XPath for specified values. Chris@0: * Chris@18: * Deprecated Scheduled for removal in Drupal 10.0.0. Chris@0: * Use $this->getSession()->getPage()->findField() instead. Chris@0: */ Chris@0: protected function constructFieldXpath($attribute, $value) { Chris@0: $xpath = '//textarea[@' . $attribute . '=:value]|//input[@' . $attribute . '=:value]|//select[@' . $attribute . '=:value]'; Chris@0: return $this->buildXPathQuery($xpath, [':value' => $value]); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Gets the current raw content. Chris@0: * Chris@18: * Deprecated Scheduled for removal in Drupal 10.0.0. Chris@0: * Use $this->getSession()->getPage()->getContent() instead. Chris@0: */ Chris@0: protected function getRawContent() { Chris@0: @trigger_error('AssertLegacyTrait::getRawContent() is scheduled for removal in Drupal 9.0.0. Use $this->getSession()->getPage()->getContent() instead.', E_USER_DEPRECATED); Chris@0: return $this->getSession()->getPage()->getContent(); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Get all option elements, including nested options, in a select. Chris@0: * Chris@0: * @param \Behat\Mink\Element\NodeElement $element Chris@0: * The element for which to get the options. Chris@0: * Chris@0: * @return \Behat\Mink\Element\NodeElement[] Chris@0: * Option elements in select. Chris@0: * Chris@18: * Deprecated Scheduled for removal in Drupal 10.0.0. Chris@0: * Use $element->findAll('xpath', 'option') instead. Chris@0: */ Chris@0: protected function getAllOptions(NodeElement $element) { Chris@0: @trigger_error('AssertLegacyTrait::getAllOptions() is scheduled for removal in Drupal 9.0.0. Use $element->findAll(\'xpath\', \'option\') instead.', E_USER_DEPRECATED); Chris@0: return $element->findAll('xpath', '//option'); Chris@0: } Chris@0: Chris@0: }