Chris@17: content; Chris@17: } Chris@17: Chris@17: /** Chris@17: * Sets the raw content (e.g. HTML). Chris@17: * Chris@17: * @param string $content Chris@17: * The raw content to set. Chris@17: */ Chris@17: protected function setRawContent($content) { Chris@17: $this->content = $content; Chris@17: $this->plainTextContent = NULL; Chris@17: $this->elements = NULL; Chris@17: $this->drupalSettings = []; Chris@17: if (preg_match('@@', $content, $matches)) { Chris@17: $this->drupalSettings = Json::decode($matches[1]); Chris@17: } Chris@17: } Chris@17: Chris@17: /** Chris@17: * Retrieves the plain-text content from the current raw content. Chris@17: */ Chris@17: protected function getTextContent() { Chris@17: if (!isset($this->plainTextContent)) { Chris@17: $raw_content = $this->getRawContent(); Chris@17: // Strip everything between the HEAD tags. Chris@17: $raw_content = preg_replace('@
(.+?)@si', '', $raw_content); Chris@17: $this->plainTextContent = Xss::filter($raw_content, []); Chris@17: } Chris@17: return $this->plainTextContent; Chris@17: } Chris@17: Chris@17: /** Chris@17: * Removes all white-space between HTML tags from the raw content. Chris@17: * Chris@17: * White-space is only removed if there are no non-white-space characters Chris@17: * between HTML tags. Chris@17: * Chris@17: * Use this (once) after performing an operation that sets new raw content, Chris@17: * and when you want to use e.g. assertText() but ignore potential white-space Chris@17: * caused by HTML output templates. Chris@17: */ Chris@17: protected function removeWhiteSpace() { Chris@17: $this->content = preg_replace('@>\s+<@', '><', $this->content); Chris@17: $this->plainTextContent = NULL; Chris@17: $this->elements = NULL; Chris@17: } Chris@17: Chris@17: /** Chris@17: * Gets the value of drupalSettings for the currently-loaded page. Chris@17: * Chris@17: * Variable drupalSettings refers to the drupalSettings JavaScript variable. Chris@17: */ Chris@17: protected function getDrupalSettings() { Chris@17: return $this->drupalSettings; Chris@17: } Chris@17: Chris@17: /** Chris@17: * Sets the value of drupalSettings for the currently-loaded page. Chris@17: * Chris@17: * Variable drupalSettings refers to the drupalSettings JavaScript variable. Chris@17: */ Chris@17: protected function setDrupalSettings($settings) { Chris@17: $this->drupalSettings = $settings; Chris@17: } Chris@17: Chris@17: /** Chris@17: * Parse content returned from curlExec using DOM and SimpleXML. Chris@17: * Chris@17: * @return \SimpleXMLElement|false Chris@17: * A SimpleXMLElement or FALSE on failure. Chris@17: */ Chris@17: protected function parse() { Chris@17: if (!isset($this->elements)) { Chris@17: // DOM can load HTML soup. But, HTML soup can throw warnings, suppress Chris@17: // them. Chris@17: $html_dom = new \DOMDocument(); Chris@17: @$html_dom->loadHTML('' . $this->getRawContent()); Chris@17: if ($html_dom) { Chris@17: $this->pass(new FormattableMarkup('Valid HTML found on "@path"', ['@path' => $this->getUrl()]), 'Browser'); Chris@17: // It's much easier to work with simplexml than DOM, luckily enough Chris@17: // we can just simply import our DOM tree. Chris@17: $this->elements = simplexml_import_dom($html_dom); Chris@17: } Chris@17: } Chris@17: if ($this->elements === FALSE) { Chris@17: $this->fail('Parsed page successfully.', 'Browser'); Chris@17: } Chris@17: Chris@17: return $this->elements; Chris@17: } Chris@17: Chris@17: /** Chris@17: * Get the current URL from the cURL handler. Chris@17: * Chris@17: * @return string Chris@17: * The current URL. Chris@17: */ Chris@17: protected function getUrl() { Chris@17: return isset($this->url) ? $this->url : 'no-url'; Chris@17: } Chris@17: Chris@17: /** Chris@17: * Builds an XPath query. Chris@17: * Chris@17: * Builds an XPath query by replacing placeholders in the query by the value Chris@17: * of the arguments. Chris@17: * Chris@17: * XPath 1.0 (the version supported by libxml2, the underlying XML library Chris@17: * used by PHP) doesn't support any form of quotation. This function Chris@17: * simplifies the building of XPath expression. Chris@17: * Chris@17: * @param string $xpath Chris@17: * An XPath query, possibly with placeholders in the form ':name'. Chris@17: * @param array $args Chris@17: * An array of arguments with keys in the form ':name' matching the Chris@17: * placeholders in the query. The values may be either strings or numeric Chris@17: * values. Chris@17: * Chris@17: * @return string Chris@17: * An XPath query with arguments replaced. Chris@17: */ Chris@17: protected function buildXPathQuery($xpath, array $args = []) { Chris@17: // Replace placeholders. Chris@17: foreach ($args as $placeholder => $value) { Chris@17: // Cast MarkupInterface objects to string. Chris@17: if (is_object($value)) { Chris@17: $value = (string) $value; Chris@17: } Chris@17: // XPath 1.0 doesn't support a way to escape single or double quotes in a Chris@17: // string literal. We split double quotes out of the string, and encode Chris@17: // them separately. Chris@17: if (is_string($value)) { Chris@17: // Explode the text at the quote characters. Chris@17: $parts = explode('"', $value); Chris@17: Chris@17: // Quote the parts. Chris@17: foreach ($parts as &$part) { Chris@17: $part = '"' . $part . '"'; Chris@17: } Chris@17: Chris@17: // Return the string. Chris@17: $value = count($parts) > 1 ? 'concat(' . implode(', \'"\', ', $parts) . ')' : $parts[0]; Chris@17: } Chris@17: Chris@17: // Use preg_replace_callback() instead of preg_replace() to prevent the Chris@17: // regular expression engine from trying to substitute backreferences. Chris@17: $replacement = function ($matches) use ($value) { Chris@17: return $value; Chris@17: }; Chris@17: $xpath = preg_replace_callback('/' . preg_quote($placeholder) . '\b/', $replacement, $xpath); Chris@17: } Chris@17: return $xpath; Chris@17: } Chris@17: Chris@17: /** Chris@17: * Performs an xpath search on the contents of the internal browser. Chris@17: * Chris@17: * The search is relative to the root element (HTML tag normally) of the page. Chris@17: * Chris@17: * @param string $xpath Chris@17: * The xpath string to use in the search. Chris@17: * @param array $arguments Chris@17: * An array of arguments with keys in the form ':name' matching the Chris@17: * placeholders in the query. The values may be either strings or numeric Chris@17: * values. Chris@17: * Chris@17: * @return \SimpleXMLElement[]|bool Chris@17: * The return value of the xpath search or FALSE on failure. For details on Chris@17: * the xpath string format and return values see the SimpleXML Chris@17: * documentation. Chris@17: * Chris@17: * @see http://php.net/manual/function.simplexml-element-xpath.php Chris@17: */ Chris@17: protected function xpath($xpath, array $arguments = []) { Chris@17: if ($this->parse()) { Chris@17: $xpath = $this->buildXPathQuery($xpath, $arguments); Chris@17: $result = $this->elements->xpath($xpath); Chris@17: // Some combinations of PHP / libxml versions return an empty array Chris@17: // instead of the documented FALSE. Forcefully convert any falsish values Chris@17: // to an empty array to allow foreach(...) constructions. Chris@17: return $result ?: []; Chris@17: } Chris@17: return FALSE; Chris@17: } Chris@17: Chris@17: /** Chris@17: * Searches elements using a CSS selector in the raw content. Chris@17: * Chris@17: * The search is relative to the root element (HTML tag normally) of the page. Chris@17: * Chris@17: * @param string $selector Chris@17: * CSS selector to use in the search. Chris@17: * Chris@17: * @return \SimpleXMLElement[] Chris@17: * The return value of the XPath search performed after converting the CSS Chris@17: * selector to an XPath selector. Chris@17: */ Chris@17: protected function cssSelect($selector) { Chris@17: return $this->xpath((new CssSelectorConverter())->toXPath($selector)); Chris@17: } Chris@17: Chris@17: /** Chris@17: * Get all option elements, including nested options, in a select. Chris@17: * Chris@17: * @param \SimpleXMLElement $element Chris@17: * The element for which to get the options. Chris@17: * Chris@17: * @return \SimpleXmlElement[] Chris@17: * Option elements in select. Chris@17: */ Chris@17: protected function getAllOptions(\SimpleXMLElement $element) { Chris@17: $options = []; Chris@17: // Add all options items. Chris@17: foreach ($element->option as $option) { Chris@17: $options[] = $option; Chris@17: } Chris@17: Chris@17: // Search option group children. Chris@17: if (isset($element->optgroup)) { Chris@17: foreach ($element->optgroup as $group) { Chris@17: $options = array_merge($options, $this->getAllOptions($group)); Chris@17: } Chris@17: } Chris@17: return $options; Chris@17: } Chris@17: Chris@17: /** Chris@17: * Passes if a link with the specified label is found. Chris@17: * Chris@17: * An optional link index may be passed. Chris@17: * Chris@17: * @param string|\Drupal\Component\Render\MarkupInterface $label Chris@17: * Text between the anchor tags. Chris@17: * @param int $index Chris@17: * Link position counting from zero. Chris@17: * @param string $message Chris@17: * (optional) A message to display with the assertion. Do not translate Chris@17: * messages: use strtr() to embed variables in the message text, not Chris@17: * t(). If left blank, a default message will be displayed. Chris@17: * @param string $group Chris@17: * (optional) The group this message is in, which is displayed in a column Chris@17: * in test output. Use 'Debug' to indicate this is debugging output. Do not Chris@17: * translate this string. Defaults to 'Other'; most tests do not override Chris@17: * this default. Chris@17: * Chris@17: * @return bool Chris@17: * TRUE if the assertion succeeded, FALSE otherwise. Chris@17: */ Chris@17: protected function assertLink($label, $index = 0, $message = '', $group = 'Other') { Chris@17: // Cast MarkupInterface objects to string. Chris@17: $label = (string) $label; Chris@17: $links = $this->xpath('//a[normalize-space(text())=:label]', [':label' => $label]); Chris@17: $message = ($message ? $message : strtr('Link with label %label found.', ['%label' => $label])); Chris@17: return $this->assert(isset($links[$index]), $message, $group); Chris@17: } Chris@17: Chris@17: /** Chris@17: * Passes if a link with the specified label is not found. Chris@17: * Chris@17: * @param string|\Drupal\Component\Render\MarkupInterface $label Chris@17: * Text between the anchor tags. Chris@17: * @param string $message Chris@17: * (optional) A message to display with the assertion. Do not translate Chris@17: * messages: use \Drupal\Component\Render\FormattableMarkup to embed Chris@17: * variables in the message text, not t(). If left blank, a default message Chris@17: * will be displayed. Chris@17: * @param string $group Chris@17: * (optional) The group this message is in, which is displayed in a column Chris@17: * in test output. Use 'Debug' to indicate this is debugging output. Do not Chris@17: * translate this string. Defaults to 'Other'; most tests do not override Chris@17: * this default. Chris@17: * Chris@17: * @return bool Chris@17: * TRUE if the assertion succeeded, FALSE otherwise. Chris@17: */ Chris@17: protected function assertNoLink($label, $message = '', $group = 'Other') { Chris@17: // Cast MarkupInterface objects to string. Chris@17: $label = (string) $label; Chris@17: $links = $this->xpath('//a[normalize-space(text())=:label]', [':label' => $label]); Chris@17: $message = ($message ? $message : new FormattableMarkup('Link with label %label not found.', ['%label' => $label])); Chris@17: return $this->assert(empty($links), $message, $group); Chris@17: } Chris@17: Chris@17: /** Chris@17: * Passes if a link containing a given href (part) is found. Chris@17: * Chris@17: * @param string $href Chris@17: * The full or partial value of the 'href' attribute of the anchor tag. Chris@17: * @param string $index Chris@17: * Link position counting from zero. Chris@17: * @param string $message Chris@17: * (optional) A message to display with the assertion. Do not translate Chris@17: * messages: use \Drupal\Component\Render\FormattableMarkup to embed Chris@17: * variables in the message text, not t(). If left blank, a default message Chris@17: * will be displayed. Chris@17: * @param string $group Chris@17: * (optional) The group this message is in, which is displayed in a column Chris@17: * in test output. Use 'Debug' to indicate this is debugging output. Do not Chris@17: * translate this string. Defaults to 'Other'; most tests do not override Chris@17: * this default. Chris@17: * Chris@17: * @return bool Chris@17: * TRUE if the assertion succeeded, FALSE otherwise. Chris@17: */ Chris@17: protected function assertLinkByHref($href, $index = 0, $message = '', $group = 'Other') { Chris@17: $links = $this->xpath('//a[contains(@href, :href)]', [':href' => $href]); Chris@17: $message = ($message ? $message : new FormattableMarkup('Link containing href %href found.', ['%href' => $href])); Chris@17: return $this->assert(isset($links[$index]), $message, $group); Chris@17: } Chris@17: Chris@17: /** Chris@17: * Passes if a link containing a given href (part) is not found. Chris@17: * Chris@17: * @param string $href Chris@17: * The full or partial value of the 'href' attribute of the anchor tag. Chris@17: * @param string $message Chris@17: * (optional) A message to display with the assertion. Do not translate Chris@17: * messages: use \Drupal\Component\Render\FormattableMarkup to embed Chris@17: * variables in the message text, not t(). If left blank, a default message Chris@17: * will be displayed. Chris@17: * @param string $group Chris@17: * (optional) The group this message is in, which is displayed in a column Chris@17: * in test output. Use 'Debug' to indicate this is debugging output. Do not Chris@17: * translate this string. Defaults to 'Other'; most tests do not override Chris@17: * this default. Chris@17: * Chris@17: * @return bool Chris@17: * TRUE if the assertion succeeded, FALSE otherwise. Chris@17: */ Chris@17: protected function assertNoLinkByHref($href, $message = '', $group = 'Other') { Chris@17: $links = $this->xpath('//a[contains(@href, :href)]', [':href' => $href]); Chris@17: $message = ($message ? $message : new FormattableMarkup('No link containing href %href found.', ['%href' => $href])); Chris@17: return $this->assert(empty($links), $message, $group); Chris@17: } Chris@17: Chris@17: /** Chris@17: * Passes if a link containing a given href is not found in the main region. Chris@17: * Chris@17: * @param string $href Chris@17: * The full or partial value of the 'href' attribute of the anchor tag. Chris@17: * @param string $message Chris@17: * (optional) A message to display with the assertion. Do not translate Chris@17: * messages: use \Drupal\Component\Render\FormattableMarkup to embed Chris@17: * variables in the message text, not t(). If left blank, a default message Chris@17: * will be displayed. Chris@17: * @param string $group Chris@17: * (optional) The group this message is in, which is displayed in a column Chris@17: * in test output. Use 'Debug' to indicate this is debugging output. Do not Chris@17: * translate this string. Defaults to 'Other'; most tests do not override Chris@17: * this default. Chris@17: * Chris@17: * @return bool Chris@17: * TRUE if the assertion succeeded, FALSE otherwise. Chris@17: */ Chris@17: protected function assertNoLinkByHrefInMainRegion($href, $message = '', $group = 'Other') { Chris@17: $links = $this->xpath('//main//a[contains(@href, :href)]', [':href' => $href]); Chris@17: $message = ($message ? $message : new FormattableMarkup('No link containing href %href found.', ['%href' => $href])); Chris@17: return $this->assert(empty($links), $message, $group); Chris@17: } Chris@17: Chris@17: /** Chris@17: * Passes if the raw text IS found on the loaded page, fail otherwise. Chris@17: * Chris@17: * Raw text refers to the raw HTML that the page generated. Chris@17: * Chris@17: * @param string $raw Chris@17: * Raw (HTML) string to look for. Chris@17: * @param string $message Chris@17: * (optional) A message to display with the assertion. Do not translate Chris@17: * messages: use \Drupal\Component\Render\FormattableMarkup to embed Chris@17: * variables in the message text, not t(). If left blank, a default message Chris@17: * will be displayed. Chris@17: * @param string $group Chris@17: * (optional) The group this message is in, which is displayed in a column Chris@17: * in test output. Use 'Debug' to indicate this is debugging output. Do not Chris@17: * translate this string. Defaults to 'Other'; most tests do not override Chris@17: * this default. Chris@17: * Chris@17: * @return bool Chris@17: * TRUE on pass, FALSE on fail. Chris@17: */ Chris@17: protected function assertRaw($raw, $message = '', $group = 'Other') { Chris@17: if (!$message) { Chris@17: $message = 'Raw "' . Html::escape($raw) . '" found'; Chris@17: } Chris@17: return $this->assert(strpos($this->getRawContent(), (string) $raw) !== FALSE, $message, $group); Chris@17: } Chris@17: Chris@17: /** Chris@17: * Passes if the raw text is NOT found on the loaded page, fail otherwise. Chris@17: * Chris@17: * Raw text refers to the raw HTML that the page generated. Chris@17: * Chris@17: * @param string $raw Chris@17: * Raw (HTML) string to look for. Chris@17: * @param string $message Chris@17: * (optional) A message to display with the assertion. Do not translate Chris@17: * messages: use \Drupal\Component\Render\FormattableMarkup to embed Chris@17: * variables in the message text, not t(). If left blank, a default message Chris@17: * will be displayed. Chris@17: * @param string $group Chris@17: * (optional) The group this message is in, which is displayed in a column Chris@17: * in test output. Use 'Debug' to indicate this is debugging output. Do not Chris@17: * translate this string. Defaults to 'Other'; most tests do not override Chris@17: * this default. Chris@17: * Chris@17: * @return bool Chris@17: * TRUE on pass, FALSE on fail. Chris@17: */ Chris@17: protected function assertNoRaw($raw, $message = '', $group = 'Other') { Chris@17: if (!$message) { Chris@17: $message = 'Raw "' . Html::escape($raw) . '" not found'; Chris@17: } Chris@17: return $this->assert(strpos($this->getRawContent(), (string) $raw) === FALSE, $message, $group); Chris@17: } Chris@17: Chris@17: /** Chris@17: * Passes if the raw text IS found escaped on the loaded page, fail otherwise. Chris@17: * Chris@17: * Raw text refers to the raw HTML that the page generated. Chris@17: * Chris@17: * @param string $raw Chris@17: * Raw (HTML) string to look for. Chris@17: * @param string $message Chris@17: * (optional) A message to display with the assertion. Do not translate Chris@17: * messages: use \Drupal\Component\Render\FormattableMarkup to embed Chris@17: * variables in the message text, not t(). If left blank, a default message Chris@17: * will be displayed. Chris@17: * @param string $group Chris@17: * (optional) The group this message is in, which is displayed in a column Chris@17: * in test output. Use 'Debug' to indicate this is debugging output. Do not Chris@17: * translate this string. Defaults to 'Other'; most tests do not override Chris@17: * this default. Chris@17: * Chris@17: * @return bool Chris@17: * TRUE on pass, FALSE on fail. Chris@17: */ Chris@17: protected function assertEscaped($raw, $message = '', $group = 'Other') { Chris@17: if (!$message) { Chris@17: $message = 'Escaped "' . Html::escape($raw) . '" found'; Chris@17: } Chris@17: return $this->assert(strpos($this->getRawContent(), Html::escape($raw)) !== FALSE, $message, $group); Chris@17: } Chris@17: Chris@17: /** Chris@17: * Passes if the raw text IS NOT found escaped on the loaded page, fail Chris@17: * otherwise. Chris@17: * Chris@17: * Raw text refers to the raw HTML that the page generated. Chris@17: * Chris@17: * @param string $raw Chris@17: * Raw (HTML) string to look for. Chris@17: * @param string $message Chris@17: * (optional) A message to display with the assertion. Do not translate Chris@17: * messages: use \Drupal\Component\Render\FormattableMarkup to embed Chris@17: * variables in the message text, not t(). If left blank, a default message Chris@17: * will be displayed. Chris@17: * @param string $group Chris@17: * (optional) The group this message is in, which is displayed in a column Chris@17: * in test output. Use 'Debug' to indicate this is debugging output. Do not Chris@17: * translate this string. Defaults to 'Other'; most tests do not override Chris@17: * this default. Chris@17: * Chris@17: * @return bool Chris@17: * TRUE on pass, FALSE on fail. Chris@17: */ Chris@17: protected function assertNoEscaped($raw, $message = '', $group = 'Other') { Chris@17: if (!$message) { Chris@17: $message = 'Escaped "' . Html::escape($raw) . '" not found'; Chris@17: } Chris@17: return $this->assert(strpos($this->getRawContent(), Html::escape($raw)) === FALSE, $message, $group); Chris@17: } Chris@17: Chris@17: /** Chris@17: * Passes if the page (with HTML stripped) contains the text. Chris@17: * Chris@17: * Note that stripping HTML tags also removes their attributes, such as Chris@17: * the values of text fields. Chris@17: * Chris@17: * @param string $text Chris@17: * Plain text to look for. Chris@17: * @param string $message Chris@17: * (optional) A message to display with the assertion. Do not translate Chris@17: * messages: use \Drupal\Component\Render\FormattableMarkup to embed Chris@17: * variables in the message text, not t(). If left blank, a default message Chris@17: * will be displayed. Chris@17: * @param string $group Chris@17: * (optional) The group this message is in, which is displayed in a column Chris@17: * in test output. Use 'Debug' to indicate this is debugging output. Do not Chris@17: * translate this string. Defaults to 'Other'; most tests do not override Chris@17: * this default. Chris@17: * Chris@17: * @return bool Chris@17: * TRUE on pass, FALSE on fail. Chris@17: * Chris@17: * @see \Drupal\simpletest\AssertContentTrait::assertRaw() Chris@17: */ Chris@17: protected function assertText($text, $message = '', $group = 'Other') { Chris@17: return $this->assertTextHelper($text, $message, $group, FALSE); Chris@17: } Chris@17: Chris@17: /** Chris@17: * Passes if the page (with HTML stripped) does not contains the text. Chris@17: * Chris@17: * Note that stripping HTML tags also removes their attributes, such as Chris@17: * the values of text fields. Chris@17: * Chris@17: * @param string $text Chris@17: * Plain text to look for. Chris@17: * @param string $message Chris@17: * (optional) A message to display with the assertion. Do not translate Chris@17: * messages: use \Drupal\Component\Render\FormattableMarkup to embed Chris@17: * variables in the message text, not t(). If left blank, a default message Chris@17: * will be displayed. Chris@17: * @param string $group Chris@17: * (optional) The group this message is in, which is displayed in a column Chris@17: * in test output. Use 'Debug' to indicate this is debugging output. Do not Chris@17: * translate this string. Defaults to 'Other'; most tests do not override Chris@17: * this default. Chris@17: * Chris@17: * @return bool Chris@17: * TRUE on pass, FALSE on fail. Chris@17: * Chris@17: * @see \Drupal\simpletest\AssertContentTrait::assertNoRaw() Chris@17: */ Chris@17: protected function assertNoText($text, $message = '', $group = 'Other') { Chris@17: return $this->assertTextHelper($text, $message, $group, TRUE); Chris@17: } Chris@17: Chris@17: /** Chris@17: * Helper for assertText and assertNoText. Chris@17: * Chris@17: * It is not recommended to call this function directly. Chris@17: * Chris@17: * @param string $text Chris@17: * Plain text to look for. Chris@17: * @param string $message Chris@17: * (optional) A message to display with the assertion. Do not translate Chris@17: * messages: use \Drupal\Component\Render\FormattableMarkup to embed Chris@17: * variables in the message text, not t(). If left blank, a default message Chris@17: * will be displayed. Chris@17: * @param string $group Chris@17: * (optional) The group this message is in, which is displayed in a column Chris@17: * in test output. Use 'Debug' to indicate this is debugging output. Do not Chris@17: * translate this string. Defaults to 'Other'; most tests do not override Chris@17: * this default. Defaults to 'Other'. Chris@17: * @param bool $not_exists Chris@17: * (optional) TRUE if this text should not exist, FALSE if it should. Chris@17: * Defaults to TRUE. Chris@17: * Chris@17: * @return bool Chris@17: * TRUE on pass, FALSE on fail. Chris@17: */ Chris@17: protected function assertTextHelper($text, $message = '', $group = 'Other', $not_exists = TRUE) { Chris@17: if (!$message) { Chris@17: $message = !$not_exists ? new FormattableMarkup('"@text" found', ['@text' => $text]) : new FormattableMarkup('"@text" not found', ['@text' => $text]); Chris@17: } Chris@17: return $this->assert($not_exists == (strpos($this->getTextContent(), (string) $text) === FALSE), $message, $group); Chris@17: } Chris@17: Chris@17: /** Chris@17: * Passes if the text is found ONLY ONCE on the text version of the page. Chris@17: * Chris@17: * The text version is the equivalent of what a user would see when viewing Chris@17: * through a web browser. In other words the HTML has been filtered out of Chris@17: * the contents. Chris@17: * Chris@17: * @param string|\Drupal\Component\Render\MarkupInterface $text Chris@17: * Plain text to look for. Chris@17: * @param string $message Chris@17: * (optional) A message to display with the assertion. Do not translate Chris@17: * messages: use \Drupal\Component\Render\FormattableMarkup to embed Chris@17: * variables in the message text, not t(). If left blank, a default message Chris@17: * will be displayed. Chris@17: * @param string $group Chris@17: * (optional) The group this message is in, which is displayed in a column Chris@17: * in test output. Use 'Debug' to indicate this is debugging output. Do not Chris@17: * translate this string. Defaults to 'Other'; most tests do not override Chris@17: * this default. Chris@17: * Chris@17: * @return bool Chris@17: * TRUE on pass, FALSE on fail. Chris@17: */ Chris@17: protected function assertUniqueText($text, $message = '', $group = 'Other') { Chris@17: return $this->assertUniqueTextHelper($text, $message, $group, TRUE); Chris@17: } Chris@17: Chris@17: /** Chris@17: * Passes if the text is found MORE THAN ONCE on the text version of the page. Chris@17: * Chris@17: * The text version is the equivalent of what a user would see when viewing Chris@17: * through a web browser. In other words the HTML has been filtered out of Chris@17: * the contents. Chris@17: * Chris@17: * @param string|\Drupal\Component\Render\MarkupInterface $text Chris@17: * Plain text to look for. Chris@17: * @param string $message Chris@17: * (optional) A message to display with the assertion. Do not translate Chris@17: * messages: use \Drupal\Component\Render\FormattableMarkup to embed Chris@17: * variables in the message text, not t(). If left blank, a default message Chris@17: * will be displayed. Chris@17: * @param string $group Chris@17: * (optional) The group this message is in, which is displayed in a column Chris@17: * in test output. Use 'Debug' to indicate this is debugging output. Do not Chris@17: * translate this string. Defaults to 'Other'; most tests do not override Chris@17: * this default. Chris@17: * Chris@17: * @return bool Chris@17: * TRUE on pass, FALSE on fail. Chris@17: */ Chris@17: protected function assertNoUniqueText($text, $message = '', $group = 'Other') { Chris@17: return $this->assertUniqueTextHelper($text, $message, $group, FALSE); Chris@17: } Chris@17: Chris@17: /** Chris@17: * Helper for assertUniqueText and assertNoUniqueText. Chris@17: * Chris@17: * It is not recommended to call this function directly. Chris@17: * Chris@17: * @param string|\Drupal\Component\Render\MarkupInterface $text Chris@17: * Plain text to look for. Chris@17: * @param string $message Chris@17: * (optional) A message to display with the assertion. Do not translate Chris@17: * messages: use \Drupal\Component\Render\FormattableMarkup to embed Chris@17: * variables in the message text, not t(). If left blank, a default message Chris@17: * will be displayed. Chris@17: * @param string $group Chris@17: * (optional) The group this message is in, which is displayed in a column Chris@17: * in test output. Use 'Debug' to indicate this is debugging output. Do not Chris@17: * translate this string. Defaults to 'Other'; most tests do not override Chris@17: * this default. Defaults to 'Other'. Chris@17: * @param bool $be_unique Chris@17: * (optional) TRUE if this text should be found only once, FALSE if it Chris@17: * should be found more than once. Defaults to FALSE. Chris@17: * Chris@17: * @return bool Chris@17: * TRUE on pass, FALSE on fail. Chris@17: */ Chris@17: protected function assertUniqueTextHelper($text, $message = '', $group = 'Other', $be_unique = FALSE) { Chris@17: // Cast MarkupInterface objects to string. Chris@17: $text = (string) $text; Chris@17: if (!$message) { Chris@17: $message = '"' . $text . '"' . ($be_unique ? ' found only once' : ' found more than once'); Chris@17: } Chris@17: $first_occurrence = strpos($this->getTextContent(), $text); Chris@17: if ($first_occurrence === FALSE) { Chris@17: return $this->assert(FALSE, $message, $group); Chris@17: } Chris@17: $offset = $first_occurrence + strlen($text); Chris@17: $second_occurrence = strpos($this->getTextContent(), $text, $offset); Chris@17: return $this->assert($be_unique == ($second_occurrence === FALSE), $message, $group); Chris@17: } Chris@17: Chris@17: /** Chris@17: * Triggers a pass if the Perl regex pattern is found in the raw content. Chris@17: * Chris@17: * @param string $pattern Chris@17: * Perl regex to look for including the regex delimiters. Chris@17: * @param string $message Chris@17: * (optional) A message to display with the assertion. Do not translate Chris@17: * messages: use \Drupal\Component\Render\FormattableMarkup to embed Chris@17: * variables in the message text, not t(). If left blank, a default message Chris@17: * will be displayed. Chris@17: * @param string $group Chris@17: * (optional) The group this message is in, which is displayed in a column Chris@17: * in test output. Use 'Debug' to indicate this is debugging output. Do not Chris@17: * translate this string. Defaults to 'Other'; most tests do not override Chris@17: * this default. Chris@17: * Chris@17: * @return bool Chris@17: * TRUE on pass, FALSE on fail. Chris@17: */ Chris@17: protected function assertPattern($pattern, $message = '', $group = 'Other') { Chris@17: if (!$message) { Chris@17: $message = new FormattableMarkup('Pattern "@pattern" found', ['@pattern' => $pattern]); Chris@17: } Chris@17: return $this->assert((bool) preg_match($pattern, $this->getRawContent()), $message, $group); Chris@17: } Chris@17: Chris@17: /** Chris@17: * Triggers a pass if the perl regex pattern is not found in raw content. Chris@17: * Chris@17: * @param string $pattern Chris@17: * Perl regex to look for including the regex delimiters. Chris@17: * @param string $message Chris@17: * (optional) A message to display with the assertion. Do not translate Chris@17: * messages: use \Drupal\Component\Render\FormattableMarkup to embed Chris@17: * variables in the message text, not t(). If left blank, a default message Chris@17: * will be displayed. Chris@17: * @param string $group Chris@17: * (optional) The group this message is in, which is displayed in a column Chris@17: * in test output. Use 'Debug' to indicate this is debugging output. Do not Chris@17: * translate this string. Defaults to 'Other'; most tests do not override Chris@17: * this default. Chris@17: * Chris@17: * @return bool Chris@17: * TRUE on pass, FALSE on fail. Chris@17: */ Chris@17: protected function assertNoPattern($pattern, $message = '', $group = 'Other') { Chris@17: if (!$message) { Chris@17: $message = new FormattableMarkup('Pattern "@pattern" not found', ['@pattern' => $pattern]); Chris@17: } Chris@17: return $this->assert(!preg_match($pattern, $this->getRawContent()), $message, $group); Chris@17: } Chris@17: Chris@17: /** Chris@17: * Asserts that a Perl regex pattern is found in the plain-text content. Chris@17: * Chris@17: * @param string $pattern Chris@17: * Perl regex to look for including the regex delimiters. Chris@17: * @param string $message Chris@17: * (optional) A message to display with the assertion. Chris@17: * @param string $group Chris@17: * (optional) The group this message is in, which is displayed in a column Chris@17: * in test output. Use 'Debug' to indicate this is debugging output. Do not Chris@17: * translate this string. Defaults to 'Other'; most tests do not override Chris@17: * this default. Chris@17: * Chris@17: * @return bool Chris@17: * TRUE on pass, FALSE on failure. Chris@17: */ Chris@17: protected function assertTextPattern($pattern, $message = NULL, $group = 'Other') { Chris@17: if (!isset($message)) { Chris@17: $message = new FormattableMarkup('Pattern "@pattern" found', ['@pattern' => $pattern]); Chris@17: } Chris@17: return $this->assert((bool) preg_match($pattern, $this->getTextContent()), $message, $group); Chris@17: } Chris@17: Chris@17: /** Chris@17: * Pass if the page title is the given string. Chris@17: * Chris@17: * @param string $title Chris@17: * The string the title should be. Chris@17: * @param string $message Chris@17: * (optional) A message to display with the assertion. Do not translate Chris@17: * messages: use \Drupal\Component\Render\FormattableMarkup to embed Chris@17: * variables in the message text, not t(). If left blank, a default message Chris@17: * will be displayed. Chris@17: * @param string $group Chris@17: * (optional) The group this message is in, which is displayed in a column Chris@17: * in test output. Use 'Debug' to indicate this is debugging output. Do not Chris@17: * translate this string. Defaults to 'Other'; most tests do not override Chris@17: * this default. Chris@17: * Chris@17: * @return bool Chris@17: * TRUE on pass, FALSE on fail. Chris@17: */ Chris@17: protected function assertTitle($title, $message = '', $group = 'Other') { Chris@17: // Don't use xpath as it messes with HTML escaping. Chris@17: preg_match('@' . Html::escape(var_export($output, TRUE)) . '' Chris@17: . '
' . Html::escape(var_export($expected, TRUE)) . '' Chris@17: . '