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('@(.*)@', $this->getRawContent(), $matches); Chris@0: if (isset($matches[1])) { Chris@0: $actual = $matches[1]; Chris@0: $actual = $this->castSafeStrings($actual); Chris@0: $title = $this->castSafeStrings($title); Chris@0: if (!$message) { Chris@0: $message = SafeMarkup::format('Page title @actual is equal to @expected.', [ Chris@0: '@actual' => var_export($actual, TRUE), Chris@0: '@expected' => var_export($title, TRUE), Chris@0: ]); Chris@0: } Chris@0: return $this->assertEqual($actual, $title, $message, $group); Chris@0: } Chris@0: return $this->fail('No title element found on the page.'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Pass if the page title is not the given string. Chris@0: * Chris@0: * @param string $title Chris@0: * The string the title should not 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 assertNoTitle($title, $message = '', $group = 'Other') { Chris@0: $actual = (string) current($this->xpath('//title')); Chris@0: if (!$message) { Chris@0: $message = SafeMarkup::format('Page title @actual is not equal to @unexpected.', [ Chris@0: '@actual' => var_export($actual, TRUE), Chris@0: '@unexpected' => var_export($title, TRUE), Chris@0: ]); Chris@0: } Chris@0: return $this->assertNotEqual($actual, $title, $message, $group); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Asserts themed output. Chris@0: * Chris@0: * @param string $callback Chris@0: * The name of the theme hook to invoke; e.g. 'links' for links.html.twig. Chris@0: * @param string $variables Chris@0: * An array of variables to pass to the theme function. Chris@0: * @param string $expected Chris@0: * The expected themed output string. 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 assertThemeOutput($callback, array $variables = [], $expected = '', $message = '', $group = 'Other') { Chris@0: /** @var \Drupal\Core\Render\RendererInterface $renderer */ Chris@0: $renderer = \Drupal::service('renderer'); Chris@0: Chris@0: // The string cast is necessary because theme functions return Chris@0: // MarkupInterface objects. This means we can assert that $expected Chris@0: // matches the theme output without having to worry about 0 == ''. Chris@0: $output = (string) $renderer->executeInRenderContext(new RenderContext(), function () use ($callback, $variables) { Chris@0: return \Drupal::theme()->render($callback, $variables); Chris@0: }); Chris@0: $this->verbose( Chris@0: '
' . 'Result:' . '
' . Html::escape(var_export($output, TRUE)) . '
' Chris@0: . '
' . 'Expected:' . '
' . Html::escape(var_export($expected, TRUE)) . '
' Chris@0: . '
' . $output Chris@0: ); Chris@0: if (!$message) { Chris@0: $message = '%callback rendered correctly.'; Chris@0: } Chris@0: $message = format_string($message, ['%callback' => 'theme_' . $callback . '()']); Chris@0: return $this->assertIdentical($output, $expected, $message, $group); 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 \SimpleXmlElement[] $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: 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 assertFieldsByValue($fields, $value = NULL, $message = '', $group = 'Other') { 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 (isset($field['value']) && $field['value'] == $value) { Chris@0: // Input element with correct value. Chris@0: $found = TRUE; Chris@0: } Chris@0: elseif (isset($field->option) || isset($field->optgroup)) { Chris@0: // Select element found. Chris@0: $selected = $this->getSelectedItem($field); Chris@0: if ($selected === FALSE) { Chris@0: // No item selected so use first item. Chris@0: $items = $this->getAllOptions($field); Chris@0: if (!empty($items) && $items[0]['value'] == $value) { Chris@0: $found = TRUE; Chris@0: } Chris@0: } Chris@0: elseif ($selected == $value) { Chris@0: $found = TRUE; Chris@0: } Chris@0: } Chris@0: elseif ((string) $field == $value) { Chris@0: // Text area with correct text. Chris@0: $found = TRUE; Chris@0: } Chris@0: } Chris@0: } Chris@0: } Chris@0: return $this->assertTrue($fields && $found, $message, $group); 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: 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 assertFieldByXPath($xpath, $value = NULL, $message = '', $group = 'Other') { Chris@0: $fields = $this->xpath($xpath); Chris@0: Chris@0: return $this->assertFieldsByValue($fields, $value, $message, $group); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Get the selected value from a select field. Chris@0: * Chris@0: * @param \SimpleXmlElement $element Chris@0: * SimpleXMLElement select element. Chris@0: * Chris@0: * @return bool Chris@0: * The selected value or FALSE. Chris@0: */ Chris@0: protected function getSelectedItem(\SimpleXMLElement $element) { Chris@0: foreach ($element->children() as $item) { Chris@0: if (isset($item['selected'])) { Chris@0: return $item['value']; Chris@0: } Chris@0: elseif ($item->getName() == 'optgroup') { Chris@0: if ($value = $this->getSelectedItem($item)) { Chris@0: return $value; Chris@0: } Chris@0: } Chris@0: } Chris@0: return FALSE; 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: 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 assertNoFieldByXPath($xpath, $value = NULL, $message = '', $group = 'Other') { Chris@0: $fields = $this->xpath($xpath); Chris@0: 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['value'] == $value) { Chris@0: $found = TRUE; Chris@0: } Chris@0: } Chris@0: } Chris@0: } Chris@0: return $this->assertFalse($fields && $found, $message, $group); 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: * @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 'Browser'; 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 assertFieldByName($name, $value = NULL, $message = NULL, $group = 'Browser') { Chris@0: if (!isset($message)) { Chris@0: if (!isset($value)) { Chris@0: $message = SafeMarkup::format('Found field with name @name', [ Chris@0: '@name' => var_export($name, TRUE), Chris@0: ]); Chris@0: } Chris@0: else { Chris@0: $message = SafeMarkup::format('Found field with name @name and value @value', [ Chris@0: '@name' => var_export($name, TRUE), Chris@0: '@value' => var_export($value, TRUE), Chris@0: ]); Chris@0: } Chris@0: } Chris@0: return $this->assertFieldByXPath($this->constructFieldXpath('name', $name), $value, $message, $group); 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 doesn't match it. You may pass in NULL to skip checking the Chris@0: * value, while still checking that the field doesn't exist. However, the Chris@0: * default value ('') asserts that the field value is not an empty string. 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 'Browser'; 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 assertNoFieldByName($name, $value = '', $message = '', $group = 'Browser') { Chris@0: return $this->assertNoFieldByXPath($this->constructFieldXpath('name', $name), $value, $message ? $message : SafeMarkup::format('Did not find field by name @name', ['@name' => $name]), $group); 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: * @param string|\Drupal\Component\Render\MarkupInterface $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 'Browser'; 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 assertFieldById($id, $value = '', $message = '', $group = 'Browser') { Chris@0: // Cast MarkupInterface objects to string. Chris@0: if (isset($value)) { Chris@0: $value = (string) $value; Chris@0: } Chris@0: $message = (string) $message; Chris@0: return $this->assertFieldByXPath($this->constructFieldXpath('id', $id), $value, $message ? $message : SafeMarkup::format('Found field by id @id', ['@id' => $id]), $group); 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: * @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 'Browser'; 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 assertNoFieldById($id, $value = '', $message = '', $group = 'Browser') { Chris@0: return $this->assertNoFieldByXPath($this->constructFieldXpath('id', $id), $value, $message ? $message : SafeMarkup::format('Did not find field by id @id', ['@id' => $id]), $group); 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: * @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 'Browser'; 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 assertFieldChecked($id, $message = '', $group = 'Browser') { Chris@0: $elements = $this->xpath('//input[@id=:id]', [':id' => $id]); Chris@0: return $this->assertTrue(isset($elements[0]) && !empty($elements[0]['checked']), $message ? $message : SafeMarkup::format('Checkbox field @id is checked.', ['@id' => $id]), $group); 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: * @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 'Browser'; 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 assertNoFieldChecked($id, $message = '', $group = 'Browser') { Chris@0: $elements = $this->xpath('//input[@id=:id]', [':id' => $id]); Chris@0: return $this->assertTrue(isset($elements[0]) && empty($elements[0]['checked']), $message ? $message : SafeMarkup::format('Checkbox field @id is not checked.', ['@id' => $id]), $group); 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: * @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 'Browser'; 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 assertOption($id, $option, $message = '', $group = 'Browser') { Chris@0: $options = $this->xpath('//select[@id=:id]//option[@value=:option]', [':id' => $id, ':option' => $option]); Chris@0: return $this->assertTrue(isset($options[0]), $message ? $message : SafeMarkup::format('Option @option for field @id exists.', ['@option' => $option, '@id' => $id]), $group); 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: * @param string $message Chris@0: * (optional) A message to display with the assertion. Chris@0: * Chris@0: * @return bool Chris@0: * TRUE on pass, FALSE on fail. Chris@0: */ Chris@0: protected function assertOptionByText($id, $text, $message = '') { Chris@0: $options = $this->xpath('//select[@id=:id]//option[normalize-space(text())=:text]', [':id' => $id, ':text' => $text]); Chris@0: return $this->assertTrue(isset($options[0]), $message ?: 'Option with text label ' . $text . ' for select field ' . $id . ' exits.'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Asserts that a select option in the current page exists. Chris@0: * Chris@0: * @param string $drupal_selector Chris@0: * The data drupal selector 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: 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 'Browser'; 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 assertOptionWithDrupalSelector($drupal_selector, $option, $message = '', $group = 'Browser') { Chris@0: $options = $this->xpath('//select[@data-drupal-selector=:data_drupal_selector]//option[@value=:option]', [':data_drupal_selector' => $drupal_selector, ':option' => $option]); Chris@0: return $this->assertTrue(isset($options[0]), $message ? $message : SafeMarkup::format('Option @option for field @data_drupal_selector exists.', ['@option' => $option, '@data_drupal_selector' => $drupal_selector]), $group); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Asserts that a select option in the current page does not exist. 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: 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 'Browser'; 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 assertNoOption($id, $option, $message = '', $group = 'Browser') { Chris@0: $selects = $this->xpath('//select[@id=:id]', [':id' => $id]); Chris@0: $options = $this->xpath('//select[@id=:id]//option[@value=:option]', [':id' => $id, ':option' => $option]); Chris@0: return $this->assertTrue(isset($selects[0]) && !isset($options[0]), $message ? $message : SafeMarkup::format('Option @option for field @id does not exist.', ['@option' => $option, '@id' => $id]), $group); 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: 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 'Browser'; 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: * @todo $id is unusable. Replace with $name. Chris@0: */ Chris@0: protected function assertOptionSelected($id, $option, $message = '', $group = 'Browser') { Chris@0: $elements = $this->xpath('//select[@id=:id]//option[@value=:option]', [':id' => $id, ':option' => $option]); Chris@0: return $this->assertTrue(isset($elements[0]) && !empty($elements[0]['selected']), $message ? $message : SafeMarkup::format('Option @option for field @id is selected.', ['@option' => $option, '@id' => $id]), $group); 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 $drupal_selector Chris@0: * The data drupal selector 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: 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 'Browser'; 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: * @todo $id is unusable. Replace with $name. Chris@0: */ Chris@0: protected function assertOptionSelectedWithDrupalSelector($drupal_selector, $option, $message = '', $group = 'Browser') { Chris@0: $elements = $this->xpath('//select[@data-drupal-selector=:data_drupal_selector]//option[@value=:option]', [':data_drupal_selector' => $drupal_selector, ':option' => $option]); Chris@0: return $this->assertTrue(isset($elements[0]) && !empty($elements[0]['selected']), $message ? $message : SafeMarkup::format('Option @option for field @data_drupal_selector is selected.', ['@option' => $option, '@data_drupal_selector' => $drupal_selector]), $group); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Asserts that a select option in the current page is not 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: 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 'Browser'; 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 assertNoOptionSelected($id, $option, $message = '', $group = 'Browser') { Chris@0: $elements = $this->xpath('//select[@id=:id]//option[@value=:option]', [':id' => $id, ':option' => $option]); Chris@0: return $this->assertTrue(isset($elements[0]) && empty($elements[0]['selected']), $message ? $message : SafeMarkup::format('Option @option for field @id is not selected.', ['@option' => $option, '@id' => $id]), $group); 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: * @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 assertField($field, $message = '', $group = 'Other') { Chris@0: return $this->assertFieldByXPath($this->constructFieldXpath('name', $field) . '|' . $this->constructFieldXpath('id', $field), NULL, $message, $group); 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: * @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 assertNoField($field, $message = '', $group = 'Other') { Chris@0: return $this->assertNoFieldByXPath($this->constructFieldXpath('name', $field) . '|' . $this->constructFieldXpath('id', $field), NULL, $message, $group); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Asserts that each HTML ID is used for just a single element. Chris@0: * 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: * @param array $ids_to_skip Chris@0: * An optional array of ids to skip when checking for duplicates. It is Chris@0: * always a bug to have duplicate HTML IDs, so this parameter is to enable Chris@0: * incremental fixing of core code. Whenever a test passes this parameter, Chris@0: * it should add a "todo" comment above the call to this function explaining Chris@0: * the legacy bug that the test wishes to ignore and including a link to an Chris@0: * issue that is working to fix that legacy bug. Chris@0: * Chris@0: * @return bool Chris@0: * TRUE on pass, FALSE on fail. Chris@0: */ Chris@0: protected function assertNoDuplicateIds($message = '', $group = 'Other', $ids_to_skip = []) { Chris@0: $status = TRUE; Chris@0: foreach ($this->xpath('//*[@id]') as $element) { Chris@0: $id = (string) $element['id']; Chris@0: if (isset($seen_ids[$id]) && !in_array($id, $ids_to_skip)) { Chris@0: $this->fail(SafeMarkup::format('The HTML ID %id is unique.', ['%id' => $id]), $group); Chris@0: $status = FALSE; Chris@0: } Chris@0: $seen_ids[$id] = TRUE; Chris@0: } Chris@0: return $this->assert($status, $message, $group); 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@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: }