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