annotate core/modules/simpletest/src/AssertContentTrait.php @ 0:c75dbcec494b

Initial commit from drush-created site
author Chris Cannam
date Thu, 05 Jul 2018 14:24:15 +0000
parents
children a9cd425dd02b
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\simpletest;
Chris@0 4
Chris@0 5 use Drupal\Component\Serialization\Json;
Chris@0 6 use Drupal\Component\Utility\Html;
Chris@0 7 use Drupal\Component\Utility\SafeMarkup;
Chris@0 8 use Drupal\Component\Utility\Xss;
Chris@0 9 use Drupal\Core\Render\RenderContext;
Chris@0 10 use Symfony\Component\CssSelector\CssSelectorConverter;
Chris@0 11
Chris@0 12 /**
Chris@0 13 * Provides test methods to assert content.
Chris@0 14 */
Chris@0 15 trait AssertContentTrait {
Chris@0 16
Chris@0 17 /**
Chris@0 18 * The current raw content.
Chris@0 19 *
Chris@0 20 * @var string
Chris@0 21 */
Chris@0 22 protected $content;
Chris@0 23
Chris@0 24 /**
Chris@0 25 * The plain-text content of raw $content (text nodes).
Chris@0 26 *
Chris@0 27 * @var string
Chris@0 28 */
Chris@0 29 protected $plainTextContent;
Chris@0 30
Chris@0 31 /**
Chris@0 32 * The drupalSettings value from the current raw $content.
Chris@0 33 *
Chris@0 34 * drupalSettings refers to the drupalSettings JavaScript variable.
Chris@0 35 *
Chris@0 36 * @var array
Chris@0 37 */
Chris@0 38 protected $drupalSettings;
Chris@0 39
Chris@0 40 /**
Chris@0 41 * The XML structure parsed from the current raw $content.
Chris@0 42 *
Chris@0 43 * @var \SimpleXMLElement
Chris@0 44 */
Chris@0 45 protected $elements;
Chris@0 46
Chris@0 47 /**
Chris@0 48 * Gets the current raw content.
Chris@0 49 */
Chris@0 50 protected function getRawContent() {
Chris@0 51 return $this->content;
Chris@0 52 }
Chris@0 53
Chris@0 54 /**
Chris@0 55 * Sets the raw content (e.g. HTML).
Chris@0 56 *
Chris@0 57 * @param string $content
Chris@0 58 * The raw content to set.
Chris@0 59 */
Chris@0 60 protected function setRawContent($content) {
Chris@0 61 $this->content = $content;
Chris@0 62 $this->plainTextContent = NULL;
Chris@0 63 $this->elements = NULL;
Chris@0 64 $this->drupalSettings = [];
Chris@0 65 if (preg_match('@<script type="application/json" data-drupal-selector="drupal-settings-json">([^<]*)</script>@', $content, $matches)) {
Chris@0 66 $this->drupalSettings = Json::decode($matches[1]);
Chris@0 67 }
Chris@0 68 }
Chris@0 69
Chris@0 70 /**
Chris@0 71 * Retrieves the plain-text content from the current raw content.
Chris@0 72 */
Chris@0 73 protected function getTextContent() {
Chris@0 74 if (!isset($this->plainTextContent)) {
Chris@0 75 $raw_content = $this->getRawContent();
Chris@0 76 // Strip everything between the HEAD tags.
Chris@0 77 $raw_content = preg_replace('@<head>(.+?)</head>@si', '', $raw_content);
Chris@0 78 $this->plainTextContent = Xss::filter($raw_content, []);
Chris@0 79 }
Chris@0 80 return $this->plainTextContent;
Chris@0 81 }
Chris@0 82
Chris@0 83 /**
Chris@0 84 * Removes all white-space between HTML tags from the raw content.
Chris@0 85 *
Chris@0 86 * White-space is only removed if there are no non-white-space characters
Chris@0 87 * between HTML tags.
Chris@0 88 *
Chris@0 89 * Use this (once) after performing an operation that sets new raw content,
Chris@0 90 * and when you want to use e.g. assertText() but ignore potential white-space
Chris@0 91 * caused by HTML output templates.
Chris@0 92 */
Chris@0 93 protected function removeWhiteSpace() {
Chris@0 94 $this->content = preg_replace('@>\s+<@', '><', $this->content);
Chris@0 95 $this->plainTextContent = NULL;
Chris@0 96 $this->elements = NULL;
Chris@0 97 }
Chris@0 98
Chris@0 99 /**
Chris@0 100 * Gets the value of drupalSettings for the currently-loaded page.
Chris@0 101 *
Chris@0 102 * drupalSettings refers to the drupalSettings JavaScript variable.
Chris@0 103 */
Chris@0 104 protected function getDrupalSettings() {
Chris@0 105 return $this->drupalSettings;
Chris@0 106 }
Chris@0 107
Chris@0 108 /**
Chris@0 109 * Sets the value of drupalSettings for the currently-loaded page.
Chris@0 110 *
Chris@0 111 * drupalSettings refers to the drupalSettings JavaScript variable.
Chris@0 112 */
Chris@0 113 protected function setDrupalSettings($settings) {
Chris@0 114 $this->drupalSettings = $settings;
Chris@0 115 }
Chris@0 116
Chris@0 117 /**
Chris@0 118 * Parse content returned from curlExec using DOM and SimpleXML.
Chris@0 119 *
Chris@0 120 * @return \SimpleXMLElement|false
Chris@0 121 * A SimpleXMLElement or FALSE on failure.
Chris@0 122 */
Chris@0 123 protected function parse() {
Chris@0 124 if (!isset($this->elements)) {
Chris@0 125 // DOM can load HTML soup. But, HTML soup can throw warnings, suppress
Chris@0 126 // them.
Chris@0 127 $html_dom = new \DOMDocument();
Chris@0 128 @$html_dom->loadHTML('<?xml encoding="UTF-8">' . $this->getRawContent());
Chris@0 129 if ($html_dom) {
Chris@0 130 $this->pass(SafeMarkup::format('Valid HTML found on "@path"', ['@path' => $this->getUrl()]), 'Browser');
Chris@0 131 // It's much easier to work with simplexml than DOM, luckily enough
Chris@0 132 // we can just simply import our DOM tree.
Chris@0 133 $this->elements = simplexml_import_dom($html_dom);
Chris@0 134 }
Chris@0 135 }
Chris@0 136 if ($this->elements === FALSE) {
Chris@0 137 $this->fail('Parsed page successfully.', 'Browser');
Chris@0 138 }
Chris@0 139
Chris@0 140 return $this->elements;
Chris@0 141 }
Chris@0 142
Chris@0 143 /**
Chris@0 144 * Get the current URL from the cURL handler.
Chris@0 145 *
Chris@0 146 * @return string
Chris@0 147 * The current URL.
Chris@0 148 */
Chris@0 149 protected function getUrl() {
Chris@0 150 return isset($this->url) ? $this->url : 'no-url';
Chris@0 151 }
Chris@0 152
Chris@0 153 /**
Chris@0 154 * Builds an XPath query.
Chris@0 155 *
Chris@0 156 * Builds an XPath query by replacing placeholders in the query by the value
Chris@0 157 * of the arguments.
Chris@0 158 *
Chris@0 159 * XPath 1.0 (the version supported by libxml2, the underlying XML library
Chris@0 160 * used by PHP) doesn't support any form of quotation. This function
Chris@0 161 * simplifies the building of XPath expression.
Chris@0 162 *
Chris@0 163 * @param string $xpath
Chris@0 164 * An XPath query, possibly with placeholders in the form ':name'.
Chris@0 165 * @param array $args
Chris@0 166 * An array of arguments with keys in the form ':name' matching the
Chris@0 167 * placeholders in the query. The values may be either strings or numeric
Chris@0 168 * values.
Chris@0 169 *
Chris@0 170 * @return string
Chris@0 171 * An XPath query with arguments replaced.
Chris@0 172 */
Chris@0 173 protected function buildXPathQuery($xpath, array $args = []) {
Chris@0 174 // Replace placeholders.
Chris@0 175 foreach ($args as $placeholder => $value) {
Chris@0 176 // Cast MarkupInterface objects to string.
Chris@0 177 if (is_object($value)) {
Chris@0 178 $value = (string) $value;
Chris@0 179 }
Chris@0 180 // XPath 1.0 doesn't support a way to escape single or double quotes in a
Chris@0 181 // string literal. We split double quotes out of the string, and encode
Chris@0 182 // them separately.
Chris@0 183 if (is_string($value)) {
Chris@0 184 // Explode the text at the quote characters.
Chris@0 185 $parts = explode('"', $value);
Chris@0 186
Chris@0 187 // Quote the parts.
Chris@0 188 foreach ($parts as &$part) {
Chris@0 189 $part = '"' . $part . '"';
Chris@0 190 }
Chris@0 191
Chris@0 192 // Return the string.
Chris@0 193 $value = count($parts) > 1 ? 'concat(' . implode(', \'"\', ', $parts) . ')' : $parts[0];
Chris@0 194 }
Chris@0 195
Chris@0 196 // Use preg_replace_callback() instead of preg_replace() to prevent the
Chris@0 197 // regular expression engine from trying to substitute backreferences.
Chris@0 198 $replacement = function ($matches) use ($value) {
Chris@0 199 return $value;
Chris@0 200 };
Chris@0 201 $xpath = preg_replace_callback('/' . preg_quote($placeholder) . '\b/', $replacement, $xpath);
Chris@0 202 }
Chris@0 203 return $xpath;
Chris@0 204 }
Chris@0 205
Chris@0 206 /**
Chris@0 207 * Performs an xpath search on the contents of the internal browser.
Chris@0 208 *
Chris@0 209 * The search is relative to the root element (HTML tag normally) of the page.
Chris@0 210 *
Chris@0 211 * @param string $xpath
Chris@0 212 * The xpath string to use in the search.
Chris@0 213 * @param array $arguments
Chris@0 214 * An array of arguments with keys in the form ':name' matching the
Chris@0 215 * placeholders in the query. The values may be either strings or numeric
Chris@0 216 * values.
Chris@0 217 *
Chris@0 218 * @return \SimpleXMLElement[]|bool
Chris@0 219 * The return value of the xpath search or FALSE on failure. For details on
Chris@0 220 * the xpath string format and return values see the SimpleXML
Chris@0 221 * documentation.
Chris@0 222 *
Chris@0 223 * @see http://php.net/manual/function.simplexml-element-xpath.php
Chris@0 224 */
Chris@0 225 protected function xpath($xpath, array $arguments = []) {
Chris@0 226 if ($this->parse()) {
Chris@0 227 $xpath = $this->buildXPathQuery($xpath, $arguments);
Chris@0 228 $result = $this->elements->xpath($xpath);
Chris@0 229 // Some combinations of PHP / libxml versions return an empty array
Chris@0 230 // instead of the documented FALSE. Forcefully convert any falsish values
Chris@0 231 // to an empty array to allow foreach(...) constructions.
Chris@0 232 return $result ?: [];
Chris@0 233 }
Chris@0 234 return FALSE;
Chris@0 235 }
Chris@0 236
Chris@0 237 /**
Chris@0 238 * Searches elements using a CSS selector in the raw content.
Chris@0 239 *
Chris@0 240 * The search is relative to the root element (HTML tag normally) of the page.
Chris@0 241 *
Chris@0 242 * @param string $selector
Chris@0 243 * CSS selector to use in the search.
Chris@0 244 *
Chris@0 245 * @return \SimpleXMLElement[]
Chris@0 246 * The return value of the XPath search performed after converting the CSS
Chris@0 247 * selector to an XPath selector.
Chris@0 248 */
Chris@0 249 protected function cssSelect($selector) {
Chris@0 250 return $this->xpath((new CssSelectorConverter())->toXPath($selector));
Chris@0 251 }
Chris@0 252
Chris@0 253 /**
Chris@0 254 * Get all option elements, including nested options, in a select.
Chris@0 255 *
Chris@0 256 * @param \SimpleXMLElement $element
Chris@0 257 * The element for which to get the options.
Chris@0 258 *
Chris@0 259 * @return \SimpleXmlElement[]
Chris@0 260 * Option elements in select.
Chris@0 261 */
Chris@0 262 protected function getAllOptions(\SimpleXMLElement $element) {
Chris@0 263 $options = [];
Chris@0 264 // Add all options items.
Chris@0 265 foreach ($element->option as $option) {
Chris@0 266 $options[] = $option;
Chris@0 267 }
Chris@0 268
Chris@0 269 // Search option group children.
Chris@0 270 if (isset($element->optgroup)) {
Chris@0 271 foreach ($element->optgroup as $group) {
Chris@0 272 $options = array_merge($options, $this->getAllOptions($group));
Chris@0 273 }
Chris@0 274 }
Chris@0 275 return $options;
Chris@0 276 }
Chris@0 277
Chris@0 278 /**
Chris@0 279 * Passes if a link with the specified label is found.
Chris@0 280 *
Chris@0 281 * An optional link index may be passed.
Chris@0 282 *
Chris@0 283 * @param string|\Drupal\Component\Render\MarkupInterface $label
Chris@0 284 * Text between the anchor tags.
Chris@0 285 * @param int $index
Chris@0 286 * Link position counting from zero.
Chris@0 287 * @param string $message
Chris@0 288 * (optional) A message to display with the assertion. Do not translate
Chris@0 289 * messages: use strtr() to embed variables in the message text, not
Chris@0 290 * t(). If left blank, a default message will be displayed.
Chris@0 291 * @param string $group
Chris@0 292 * (optional) The group this message is in, which is displayed in a column
Chris@0 293 * in test output. Use 'Debug' to indicate this is debugging output. Do not
Chris@0 294 * translate this string. Defaults to 'Other'; most tests do not override
Chris@0 295 * this default.
Chris@0 296 *
Chris@0 297 * @return bool
Chris@0 298 * TRUE if the assertion succeeded, FALSE otherwise.
Chris@0 299 */
Chris@0 300 protected function assertLink($label, $index = 0, $message = '', $group = 'Other') {
Chris@0 301 // Cast MarkupInterface objects to string.
Chris@0 302 $label = (string) $label;
Chris@0 303 $links = $this->xpath('//a[normalize-space(text())=:label]', [':label' => $label]);
Chris@0 304 $message = ($message ? $message : strtr('Link with label %label found.', ['%label' => $label]));
Chris@0 305 return $this->assert(isset($links[$index]), $message, $group);
Chris@0 306 }
Chris@0 307
Chris@0 308 /**
Chris@0 309 * Passes if a link with the specified label is not found.
Chris@0 310 *
Chris@0 311 * @param string|\Drupal\Component\Render\MarkupInterface $label
Chris@0 312 * Text between the anchor tags.
Chris@0 313 * @param string $message
Chris@0 314 * (optional) A message to display with the assertion. Do not translate
Chris@0 315 * messages: use \Drupal\Component\Utility\SafeMarkup::format() to embed
Chris@0 316 * variables in the message text, not t(). If left blank, a default message
Chris@0 317 * will be displayed.
Chris@0 318 * @param string $group
Chris@0 319 * (optional) The group this message is in, which is displayed in a column
Chris@0 320 * in test output. Use 'Debug' to indicate this is debugging output. Do not
Chris@0 321 * translate this string. Defaults to 'Other'; most tests do not override
Chris@0 322 * this default.
Chris@0 323 *
Chris@0 324 * @return bool
Chris@0 325 * TRUE if the assertion succeeded, FALSE otherwise.
Chris@0 326 */
Chris@0 327 protected function assertNoLink($label, $message = '', $group = 'Other') {
Chris@0 328 // Cast MarkupInterface objects to string.
Chris@0 329 $label = (string) $label;
Chris@0 330 $links = $this->xpath('//a[normalize-space(text())=:label]', [':label' => $label]);
Chris@0 331 $message = ($message ? $message : SafeMarkup::format('Link with label %label not found.', ['%label' => $label]));
Chris@0 332 return $this->assert(empty($links), $message, $group);
Chris@0 333 }
Chris@0 334
Chris@0 335 /**
Chris@0 336 * Passes if a link containing a given href (part) is found.
Chris@0 337 *
Chris@0 338 * @param string $href
Chris@0 339 * The full or partial value of the 'href' attribute of the anchor tag.
Chris@0 340 * @param string $index
Chris@0 341 * Link position counting from zero.
Chris@0 342 * @param string $message
Chris@0 343 * (optional) A message to display with the assertion. Do not translate
Chris@0 344 * messages: use \Drupal\Component\Utility\SafeMarkup::format() to embed
Chris@0 345 * variables in the message text, not t(). If left blank, a default message
Chris@0 346 * will be displayed.
Chris@0 347 * @param string $group
Chris@0 348 * (optional) The group this message is in, which is displayed in a column
Chris@0 349 * in test output. Use 'Debug' to indicate this is debugging output. Do not
Chris@0 350 * translate this string. Defaults to 'Other'; most tests do not override
Chris@0 351 * this default.
Chris@0 352 *
Chris@0 353 * @return bool
Chris@0 354 * TRUE if the assertion succeeded, FALSE otherwise.
Chris@0 355 */
Chris@0 356 protected function assertLinkByHref($href, $index = 0, $message = '', $group = 'Other') {
Chris@0 357 $links = $this->xpath('//a[contains(@href, :href)]', [':href' => $href]);
Chris@0 358 $message = ($message ? $message : SafeMarkup::format('Link containing href %href found.', ['%href' => $href]));
Chris@0 359 return $this->assert(isset($links[$index]), $message, $group);
Chris@0 360 }
Chris@0 361
Chris@0 362 /**
Chris@0 363 * Passes if a link containing a given href (part) is not found.
Chris@0 364 *
Chris@0 365 * @param string $href
Chris@0 366 * The full or partial value of the 'href' attribute of the anchor tag.
Chris@0 367 * @param string $message
Chris@0 368 * (optional) A message to display with the assertion. Do not translate
Chris@0 369 * messages: use \Drupal\Component\Utility\SafeMarkup::format() to embed
Chris@0 370 * variables in the message text, not t(). If left blank, a default message
Chris@0 371 * will be displayed.
Chris@0 372 * @param string $group
Chris@0 373 * (optional) The group this message is in, which is displayed in a column
Chris@0 374 * in test output. Use 'Debug' to indicate this is debugging output. Do not
Chris@0 375 * translate this string. Defaults to 'Other'; most tests do not override
Chris@0 376 * this default.
Chris@0 377 *
Chris@0 378 * @return bool
Chris@0 379 * TRUE if the assertion succeeded, FALSE otherwise.
Chris@0 380 */
Chris@0 381 protected function assertNoLinkByHref($href, $message = '', $group = 'Other') {
Chris@0 382 $links = $this->xpath('//a[contains(@href, :href)]', [':href' => $href]);
Chris@0 383 $message = ($message ? $message : SafeMarkup::format('No link containing href %href found.', ['%href' => $href]));
Chris@0 384 return $this->assert(empty($links), $message, $group);
Chris@0 385 }
Chris@0 386
Chris@0 387 /**
Chris@0 388 * Passes if a link containing a given href is not found in the main region.
Chris@0 389 *
Chris@0 390 * @param string $href
Chris@0 391 * The full or partial value of the 'href' attribute of the anchor tag.
Chris@0 392 * @param string $message
Chris@0 393 * (optional) A message to display with the assertion. Do not translate
Chris@0 394 * messages: use \Drupal\Component\Utility\SafeMarkup::format() to embed
Chris@0 395 * variables in the message text, not t(). If left blank, a default message
Chris@0 396 * will be displayed.
Chris@0 397 * @param string $group
Chris@0 398 * (optional) The group this message is in, which is displayed in a column
Chris@0 399 * in test output. Use 'Debug' to indicate this is debugging output. Do not
Chris@0 400 * translate this string. Defaults to 'Other'; most tests do not override
Chris@0 401 * this default.
Chris@0 402 *
Chris@0 403 * @return bool
Chris@0 404 * TRUE if the assertion succeeded, FALSE otherwise.
Chris@0 405 */
Chris@0 406 protected function assertNoLinkByHrefInMainRegion($href, $message = '', $group = 'Other') {
Chris@0 407 $links = $this->xpath('//main//a[contains(@href, :href)]', [':href' => $href]);
Chris@0 408 $message = ($message ? $message : SafeMarkup::format('No link containing href %href found.', ['%href' => $href]));
Chris@0 409 return $this->assert(empty($links), $message, $group);
Chris@0 410 }
Chris@0 411
Chris@0 412 /**
Chris@0 413 * Passes if the raw text IS found on the loaded page, fail otherwise.
Chris@0 414 *
Chris@0 415 * Raw text refers to the raw HTML that the page generated.
Chris@0 416 *
Chris@0 417 * @param string $raw
Chris@0 418 * Raw (HTML) string to look for.
Chris@0 419 * @param string $message
Chris@0 420 * (optional) A message to display with the assertion. Do not translate
Chris@0 421 * messages: use \Drupal\Component\Utility\SafeMarkup::format() to embed
Chris@0 422 * variables in the message text, not t(). If left blank, a default message
Chris@0 423 * will be displayed.
Chris@0 424 * @param string $group
Chris@0 425 * (optional) The group this message is in, which is displayed in a column
Chris@0 426 * in test output. Use 'Debug' to indicate this is debugging output. Do not
Chris@0 427 * translate this string. Defaults to 'Other'; most tests do not override
Chris@0 428 * this default.
Chris@0 429 *
Chris@0 430 * @return bool
Chris@0 431 * TRUE on pass, FALSE on fail.
Chris@0 432 */
Chris@0 433 protected function assertRaw($raw, $message = '', $group = 'Other') {
Chris@0 434 if (!$message) {
Chris@0 435 $message = 'Raw "' . Html::escape($raw) . '" found';
Chris@0 436 }
Chris@0 437 return $this->assert(strpos($this->getRawContent(), (string) $raw) !== FALSE, $message, $group);
Chris@0 438 }
Chris@0 439
Chris@0 440 /**
Chris@0 441 * Passes if the raw text is NOT found on the loaded page, fail otherwise.
Chris@0 442 *
Chris@0 443 * Raw text refers to the raw HTML that the page generated.
Chris@0 444 *
Chris@0 445 * @param string $raw
Chris@0 446 * Raw (HTML) string to look for.
Chris@0 447 * @param string $message
Chris@0 448 * (optional) A message to display with the assertion. Do not translate
Chris@0 449 * messages: use \Drupal\Component\Utility\SafeMarkup::format() to embed
Chris@0 450 * variables in the message text, not t(). If left blank, a default message
Chris@0 451 * will be displayed.
Chris@0 452 * @param string $group
Chris@0 453 * (optional) The group this message is in, which is displayed in a column
Chris@0 454 * in test output. Use 'Debug' to indicate this is debugging output. Do not
Chris@0 455 * translate this string. Defaults to 'Other'; most tests do not override
Chris@0 456 * this default.
Chris@0 457 *
Chris@0 458 * @return bool
Chris@0 459 * TRUE on pass, FALSE on fail.
Chris@0 460 */
Chris@0 461 protected function assertNoRaw($raw, $message = '', $group = 'Other') {
Chris@0 462 if (!$message) {
Chris@0 463 $message = 'Raw "' . Html::escape($raw) . '" not found';
Chris@0 464 }
Chris@0 465 return $this->assert(strpos($this->getRawContent(), (string) $raw) === FALSE, $message, $group);
Chris@0 466 }
Chris@0 467
Chris@0 468 /**
Chris@0 469 * Passes if the raw text IS found escaped on the loaded page, fail otherwise.
Chris@0 470 *
Chris@0 471 * Raw text refers to the raw HTML that the page generated.
Chris@0 472 *
Chris@0 473 * @param string $raw
Chris@0 474 * Raw (HTML) string to look for.
Chris@0 475 * @param string $message
Chris@0 476 * (optional) A message to display with the assertion. Do not translate
Chris@0 477 * messages: use \Drupal\Component\Utility\SafeMarkup::format() to embed
Chris@0 478 * variables in the message text, not t(). If left blank, a default message
Chris@0 479 * will be displayed.
Chris@0 480 * @param string $group
Chris@0 481 * (optional) The group this message is in, which is displayed in a column
Chris@0 482 * in test output. Use 'Debug' to indicate this is debugging output. Do not
Chris@0 483 * translate this string. Defaults to 'Other'; most tests do not override
Chris@0 484 * this default.
Chris@0 485 *
Chris@0 486 * @return bool
Chris@0 487 * TRUE on pass, FALSE on fail.
Chris@0 488 */
Chris@0 489 protected function assertEscaped($raw, $message = '', $group = 'Other') {
Chris@0 490 if (!$message) {
Chris@0 491 $message = 'Escaped "' . Html::escape($raw) . '" found';
Chris@0 492 }
Chris@0 493 return $this->assert(strpos($this->getRawContent(), Html::escape($raw)) !== FALSE, $message, $group);
Chris@0 494 }
Chris@0 495
Chris@0 496 /**
Chris@0 497 * Passes if the raw text IS NOT found escaped on the loaded page, fail
Chris@0 498 * otherwise.
Chris@0 499 *
Chris@0 500 * Raw text refers to the raw HTML that the page generated.
Chris@0 501 *
Chris@0 502 * @param string $raw
Chris@0 503 * Raw (HTML) string to look for.
Chris@0 504 * @param string $message
Chris@0 505 * (optional) A message to display with the assertion. Do not translate
Chris@0 506 * messages: use \Drupal\Component\Utility\SafeMarkup::format() to embed
Chris@0 507 * variables in the message text, not t(). If left blank, a default message
Chris@0 508 * will be displayed.
Chris@0 509 * @param string $group
Chris@0 510 * (optional) The group this message is in, which is displayed in a column
Chris@0 511 * in test output. Use 'Debug' to indicate this is debugging output. Do not
Chris@0 512 * translate this string. Defaults to 'Other'; most tests do not override
Chris@0 513 * this default.
Chris@0 514 *
Chris@0 515 * @return bool
Chris@0 516 * TRUE on pass, FALSE on fail.
Chris@0 517 */
Chris@0 518 protected function assertNoEscaped($raw, $message = '', $group = 'Other') {
Chris@0 519 if (!$message) {
Chris@0 520 $message = 'Escaped "' . Html::escape($raw) . '" not found';
Chris@0 521 }
Chris@0 522 return $this->assert(strpos($this->getRawContent(), Html::escape($raw)) === FALSE, $message, $group);
Chris@0 523 }
Chris@0 524
Chris@0 525 /**
Chris@0 526 * Passes if the page (with HTML stripped) contains the text.
Chris@0 527 *
Chris@0 528 * Note that stripping HTML tags also removes their attributes, such as
Chris@0 529 * the values of text fields.
Chris@0 530 *
Chris@0 531 * @param string $text
Chris@0 532 * Plain text to look for.
Chris@0 533 * @param string $message
Chris@0 534 * (optional) A message to display with the assertion. Do not translate
Chris@0 535 * messages: use \Drupal\Component\Utility\SafeMarkup::format() to embed
Chris@0 536 * variables in the message text, not t(). If left blank, a default message
Chris@0 537 * will be displayed.
Chris@0 538 * @param string $group
Chris@0 539 * (optional) The group this message is in, which is displayed in a column
Chris@0 540 * in test output. Use 'Debug' to indicate this is debugging output. Do not
Chris@0 541 * translate this string. Defaults to 'Other'; most tests do not override
Chris@0 542 * this default.
Chris@0 543 *
Chris@0 544 * @return bool
Chris@0 545 * TRUE on pass, FALSE on fail.
Chris@0 546 *
Chris@0 547 * @see \Drupal\simpletest\AssertContentTrait::assertRaw()
Chris@0 548 */
Chris@0 549 protected function assertText($text, $message = '', $group = 'Other') {
Chris@0 550 return $this->assertTextHelper($text, $message, $group, FALSE);
Chris@0 551 }
Chris@0 552
Chris@0 553 /**
Chris@0 554 * Passes if the page (with HTML stripped) does not contains the text.
Chris@0 555 *
Chris@0 556 * Note that stripping HTML tags also removes their attributes, such as
Chris@0 557 * the values of text fields.
Chris@0 558 *
Chris@0 559 * @param string $text
Chris@0 560 * Plain text to look for.
Chris@0 561 * @param string $message
Chris@0 562 * (optional) A message to display with the assertion. Do not translate
Chris@0 563 * messages: use \Drupal\Component\Utility\SafeMarkup::format() to embed
Chris@0 564 * variables in the message text, not t(). If left blank, a default message
Chris@0 565 * will be displayed.
Chris@0 566 * @param string $group
Chris@0 567 * (optional) The group this message is in, which is displayed in a column
Chris@0 568 * in test output. Use 'Debug' to indicate this is debugging output. Do not
Chris@0 569 * translate this string. Defaults to 'Other'; most tests do not override
Chris@0 570 * this default.
Chris@0 571 *
Chris@0 572 * @return bool
Chris@0 573 * TRUE on pass, FALSE on fail.
Chris@0 574 *
Chris@0 575 * @see \Drupal\simpletest\AssertContentTrait::assertNoRaw()
Chris@0 576 */
Chris@0 577 protected function assertNoText($text, $message = '', $group = 'Other') {
Chris@0 578 return $this->assertTextHelper($text, $message, $group, TRUE);
Chris@0 579 }
Chris@0 580
Chris@0 581 /**
Chris@0 582 * Helper for assertText and assertNoText.
Chris@0 583 *
Chris@0 584 * It is not recommended to call this function directly.
Chris@0 585 *
Chris@0 586 * @param string $text
Chris@0 587 * Plain text to look for.
Chris@0 588 * @param string $message
Chris@0 589 * (optional) A message to display with the assertion. Do not translate
Chris@0 590 * messages: use \Drupal\Component\Utility\SafeMarkup::format() to embed
Chris@0 591 * variables in the message text, not t(). If left blank, a default message
Chris@0 592 * will be displayed.
Chris@0 593 * @param string $group
Chris@0 594 * (optional) The group this message is in, which is displayed in a column
Chris@0 595 * in test output. Use 'Debug' to indicate this is debugging output. Do not
Chris@0 596 * translate this string. Defaults to 'Other'; most tests do not override
Chris@0 597 * this default. Defaults to 'Other'.
Chris@0 598 * @param bool $not_exists
Chris@0 599 * (optional) TRUE if this text should not exist, FALSE if it should.
Chris@0 600 * Defaults to TRUE.
Chris@0 601 *
Chris@0 602 * @return bool
Chris@0 603 * TRUE on pass, FALSE on fail.
Chris@0 604 */
Chris@0 605 protected function assertTextHelper($text, $message = '', $group = 'Other', $not_exists = TRUE) {
Chris@0 606 if (!$message) {
Chris@0 607 $message = !$not_exists ? SafeMarkup::format('"@text" found', ['@text' => $text]) : SafeMarkup::format('"@text" not found', ['@text' => $text]);
Chris@0 608 }
Chris@0 609 return $this->assert($not_exists == (strpos($this->getTextContent(), (string) $text) === FALSE), $message, $group);
Chris@0 610 }
Chris@0 611
Chris@0 612 /**
Chris@0 613 * Passes if the text is found ONLY ONCE on the text version of the page.
Chris@0 614 *
Chris@0 615 * The text version is the equivalent of what a user would see when viewing
Chris@0 616 * through a web browser. In other words the HTML has been filtered out of
Chris@0 617 * the contents.
Chris@0 618 *
Chris@0 619 * @param string|\Drupal\Component\Render\MarkupInterface $text
Chris@0 620 * Plain text to look for.
Chris@0 621 * @param string $message
Chris@0 622 * (optional) A message to display with the assertion. Do not translate
Chris@0 623 * messages: use \Drupal\Component\Utility\SafeMarkup::format() to embed
Chris@0 624 * variables in the message text, not t(). If left blank, a default message
Chris@0 625 * will be displayed.
Chris@0 626 * @param string $group
Chris@0 627 * (optional) The group this message is in, which is displayed in a column
Chris@0 628 * in test output. Use 'Debug' to indicate this is debugging output. Do not
Chris@0 629 * translate this string. Defaults to 'Other'; most tests do not override
Chris@0 630 * this default.
Chris@0 631 *
Chris@0 632 * @return bool
Chris@0 633 * TRUE on pass, FALSE on fail.
Chris@0 634 */
Chris@0 635 protected function assertUniqueText($text, $message = '', $group = 'Other') {
Chris@0 636 return $this->assertUniqueTextHelper($text, $message, $group, TRUE);
Chris@0 637 }
Chris@0 638
Chris@0 639 /**
Chris@0 640 * Passes if the text is found MORE THAN ONCE on the text version of the page.
Chris@0 641 *
Chris@0 642 * The text version is the equivalent of what a user would see when viewing
Chris@0 643 * through a web browser. In other words the HTML has been filtered out of
Chris@0 644 * the contents.
Chris@0 645 *
Chris@0 646 * @param string|\Drupal\Component\Render\MarkupInterface $text
Chris@0 647 * Plain text to look for.
Chris@0 648 * @param string $message
Chris@0 649 * (optional) A message to display with the assertion. Do not translate
Chris@0 650 * messages: use \Drupal\Component\Utility\SafeMarkup::format() to embed
Chris@0 651 * variables in the message text, not t(). If left blank, a default message
Chris@0 652 * will be displayed.
Chris@0 653 * @param string $group
Chris@0 654 * (optional) The group this message is in, which is displayed in a column
Chris@0 655 * in test output. Use 'Debug' to indicate this is debugging output. Do not
Chris@0 656 * translate this string. Defaults to 'Other'; most tests do not override
Chris@0 657 * this default.
Chris@0 658 *
Chris@0 659 * @return bool
Chris@0 660 * TRUE on pass, FALSE on fail.
Chris@0 661 */
Chris@0 662 protected function assertNoUniqueText($text, $message = '', $group = 'Other') {
Chris@0 663 return $this->assertUniqueTextHelper($text, $message, $group, FALSE);
Chris@0 664 }
Chris@0 665
Chris@0 666 /**
Chris@0 667 * Helper for assertUniqueText and assertNoUniqueText.
Chris@0 668 *
Chris@0 669 * It is not recommended to call this function directly.
Chris@0 670 *
Chris@0 671 * @param string|\Drupal\Component\Render\MarkupInterface $text
Chris@0 672 * Plain text to look for.
Chris@0 673 * @param string $message
Chris@0 674 * (optional) A message to display with the assertion. Do not translate
Chris@0 675 * messages: use \Drupal\Component\Utility\SafeMarkup::format() to embed
Chris@0 676 * variables in the message text, not t(). If left blank, a default message
Chris@0 677 * will be displayed.
Chris@0 678 * @param string $group
Chris@0 679 * (optional) The group this message is in, which is displayed in a column
Chris@0 680 * in test output. Use 'Debug' to indicate this is debugging output. Do not
Chris@0 681 * translate this string. Defaults to 'Other'; most tests do not override
Chris@0 682 * this default. Defaults to 'Other'.
Chris@0 683 * @param bool $be_unique
Chris@0 684 * (optional) TRUE if this text should be found only once, FALSE if it
Chris@0 685 * should be found more than once. Defaults to FALSE.
Chris@0 686 *
Chris@0 687 * @return bool
Chris@0 688 * TRUE on pass, FALSE on fail.
Chris@0 689 */
Chris@0 690 protected function assertUniqueTextHelper($text, $message = '', $group = 'Other', $be_unique = FALSE) {
Chris@0 691 // Cast MarkupInterface objects to string.
Chris@0 692 $text = (string) $text;
Chris@0 693 if (!$message) {
Chris@0 694 $message = '"' . $text . '"' . ($be_unique ? ' found only once' : ' found more than once');
Chris@0 695 }
Chris@0 696 $first_occurrence = strpos($this->getTextContent(), $text);
Chris@0 697 if ($first_occurrence === FALSE) {
Chris@0 698 return $this->assert(FALSE, $message, $group);
Chris@0 699 }
Chris@0 700 $offset = $first_occurrence + strlen($text);
Chris@0 701 $second_occurrence = strpos($this->getTextContent(), $text, $offset);
Chris@0 702 return $this->assert($be_unique == ($second_occurrence === FALSE), $message, $group);
Chris@0 703 }
Chris@0 704
Chris@0 705 /**
Chris@0 706 * Triggers a pass if the Perl regex pattern is found in the raw content.
Chris@0 707 *
Chris@0 708 * @param string $pattern
Chris@0 709 * Perl regex to look for including the regex delimiters.
Chris@0 710 * @param string $message
Chris@0 711 * (optional) A message to display with the assertion. Do not translate
Chris@0 712 * messages: use \Drupal\Component\Utility\SafeMarkup::format() to embed
Chris@0 713 * variables in the message text, not t(). If left blank, a default message
Chris@0 714 * will be displayed.
Chris@0 715 * @param string $group
Chris@0 716 * (optional) The group this message is in, which is displayed in a column
Chris@0 717 * in test output. Use 'Debug' to indicate this is debugging output. Do not
Chris@0 718 * translate this string. Defaults to 'Other'; most tests do not override
Chris@0 719 * this default.
Chris@0 720 *
Chris@0 721 * @return bool
Chris@0 722 * TRUE on pass, FALSE on fail.
Chris@0 723 */
Chris@0 724 protected function assertPattern($pattern, $message = '', $group = 'Other') {
Chris@0 725 if (!$message) {
Chris@0 726 $message = SafeMarkup::format('Pattern "@pattern" found', ['@pattern' => $pattern]);
Chris@0 727 }
Chris@0 728 return $this->assert((bool) preg_match($pattern, $this->getRawContent()), $message, $group);
Chris@0 729 }
Chris@0 730
Chris@0 731 /**
Chris@0 732 * Triggers a pass if the perl regex pattern is not found in raw content.
Chris@0 733 *
Chris@0 734 * @param string $pattern
Chris@0 735 * Perl regex to look for including the regex delimiters.
Chris@0 736 * @param string $message
Chris@0 737 * (optional) A message to display with the assertion. Do not translate
Chris@0 738 * messages: use \Drupal\Component\Utility\SafeMarkup::format() to embed
Chris@0 739 * variables in the message text, not t(). If left blank, a default message
Chris@0 740 * will be displayed.
Chris@0 741 * @param string $group
Chris@0 742 * (optional) The group this message is in, which is displayed in a column
Chris@0 743 * in test output. Use 'Debug' to indicate this is debugging output. Do not
Chris@0 744 * translate this string. Defaults to 'Other'; most tests do not override
Chris@0 745 * this default.
Chris@0 746 *
Chris@0 747 * @return bool
Chris@0 748 * TRUE on pass, FALSE on fail.
Chris@0 749 */
Chris@0 750 protected function assertNoPattern($pattern, $message = '', $group = 'Other') {
Chris@0 751 if (!$message) {
Chris@0 752 $message = SafeMarkup::format('Pattern "@pattern" not found', ['@pattern' => $pattern]);
Chris@0 753 }
Chris@0 754 return $this->assert(!preg_match($pattern, $this->getRawContent()), $message, $group);
Chris@0 755 }
Chris@0 756
Chris@0 757 /**
Chris@0 758 * Asserts that a Perl regex pattern is found in the plain-text content.
Chris@0 759 *
Chris@0 760 * @param string $pattern
Chris@0 761 * Perl regex to look for including the regex delimiters.
Chris@0 762 * @param string $message
Chris@0 763 * (optional) A message to display with the assertion.
Chris@0 764 * @param string $group
Chris@0 765 * (optional) The group this message is in, which is displayed in a column
Chris@0 766 * in test output. Use 'Debug' to indicate this is debugging output. Do not
Chris@0 767 * translate this string. Defaults to 'Other'; most tests do not override
Chris@0 768 * this default.
Chris@0 769 *
Chris@0 770 * @return bool
Chris@0 771 * TRUE on pass, FALSE on failure.
Chris@0 772 */
Chris@0 773 protected function assertTextPattern($pattern, $message = NULL, $group = 'Other') {
Chris@0 774 if (!isset($message)) {
Chris@0 775 $message = SafeMarkup::format('Pattern "@pattern" found', ['@pattern' => $pattern]);
Chris@0 776 }
Chris@0 777 return $this->assert((bool) preg_match($pattern, $this->getTextContent()), $message, $group);
Chris@0 778 }
Chris@0 779
Chris@0 780 /**
Chris@0 781 * Pass if the page title is the given string.
Chris@0 782 *
Chris@0 783 * @param string $title
Chris@0 784 * The string the title should be.
Chris@0 785 * @param string $message
Chris@0 786 * (optional) A message to display with the assertion. Do not translate
Chris@0 787 * messages: use \Drupal\Component\Utility\SafeMarkup::format() to embed
Chris@0 788 * variables in the message text, not t(). If left blank, a default message
Chris@0 789 * will be displayed.
Chris@0 790 * @param string $group
Chris@0 791 * (optional) The group this message is in, which is displayed in a column
Chris@0 792 * in test output. Use 'Debug' to indicate this is debugging output. Do not
Chris@0 793 * translate this string. Defaults to 'Other'; most tests do not override
Chris@0 794 * this default.
Chris@0 795 *
Chris@0 796 * @return bool
Chris@0 797 * TRUE on pass, FALSE on fail.
Chris@0 798 */
Chris@0 799 protected function assertTitle($title, $message = '', $group = 'Other') {
Chris@0 800 // Don't use xpath as it messes with HTML escaping.
Chris@0 801 preg_match('@<title>(.*)</title>@', $this->getRawContent(), $matches);
Chris@0 802 if (isset($matches[1])) {
Chris@0 803 $actual = $matches[1];
Chris@0 804 $actual = $this->castSafeStrings($actual);
Chris@0 805 $title = $this->castSafeStrings($title);
Chris@0 806 if (!$message) {
Chris@0 807 $message = SafeMarkup::format('Page title @actual is equal to @expected.', [
Chris@0 808 '@actual' => var_export($actual, TRUE),
Chris@0 809 '@expected' => var_export($title, TRUE),
Chris@0 810 ]);
Chris@0 811 }
Chris@0 812 return $this->assertEqual($actual, $title, $message, $group);
Chris@0 813 }
Chris@0 814 return $this->fail('No title element found on the page.');
Chris@0 815 }
Chris@0 816
Chris@0 817 /**
Chris@0 818 * Pass if the page title is not the given string.
Chris@0 819 *
Chris@0 820 * @param string $title
Chris@0 821 * The string the title should not be.
Chris@0 822 * @param string $message
Chris@0 823 * (optional) A message to display with the assertion. Do not translate
Chris@0 824 * messages: use \Drupal\Component\Utility\SafeMarkup::format() to embed
Chris@0 825 * variables in the message text, not t(). If left blank, a default message
Chris@0 826 * will be displayed.
Chris@0 827 * @param string $group
Chris@0 828 * (optional) The group this message is in, which is displayed in a column
Chris@0 829 * in test output. Use 'Debug' to indicate this is debugging output. Do not
Chris@0 830 * translate this string. Defaults to 'Other'; most tests do not override
Chris@0 831 * this default.
Chris@0 832 *
Chris@0 833 * @return bool
Chris@0 834 * TRUE on pass, FALSE on fail.
Chris@0 835 */
Chris@0 836 protected function assertNoTitle($title, $message = '', $group = 'Other') {
Chris@0 837 $actual = (string) current($this->xpath('//title'));
Chris@0 838 if (!$message) {
Chris@0 839 $message = SafeMarkup::format('Page title @actual is not equal to @unexpected.', [
Chris@0 840 '@actual' => var_export($actual, TRUE),
Chris@0 841 '@unexpected' => var_export($title, TRUE),
Chris@0 842 ]);
Chris@0 843 }
Chris@0 844 return $this->assertNotEqual($actual, $title, $message, $group);
Chris@0 845 }
Chris@0 846
Chris@0 847 /**
Chris@0 848 * Asserts themed output.
Chris@0 849 *
Chris@0 850 * @param string $callback
Chris@0 851 * The name of the theme hook to invoke; e.g. 'links' for links.html.twig.
Chris@0 852 * @param string $variables
Chris@0 853 * An array of variables to pass to the theme function.
Chris@0 854 * @param string $expected
Chris@0 855 * The expected themed output string.
Chris@0 856 * @param string $message
Chris@0 857 * (optional) A message to display with the assertion. Do not translate
Chris@0 858 * messages: use \Drupal\Component\Utility\SafeMarkup::format() to embed
Chris@0 859 * variables in the message text, not t(). If left blank, a default message
Chris@0 860 * will be displayed.
Chris@0 861 * @param string $group
Chris@0 862 * (optional) The group this message is in, which is displayed in a column
Chris@0 863 * in test output. Use 'Debug' to indicate this is debugging output. Do not
Chris@0 864 * translate this string. Defaults to 'Other'; most tests do not override
Chris@0 865 * this default.
Chris@0 866 *
Chris@0 867 * @return bool
Chris@0 868 * TRUE on pass, FALSE on fail.
Chris@0 869 */
Chris@0 870 protected function assertThemeOutput($callback, array $variables = [], $expected = '', $message = '', $group = 'Other') {
Chris@0 871 /** @var \Drupal\Core\Render\RendererInterface $renderer */
Chris@0 872 $renderer = \Drupal::service('renderer');
Chris@0 873
Chris@0 874 // The string cast is necessary because theme functions return
Chris@0 875 // MarkupInterface objects. This means we can assert that $expected
Chris@0 876 // matches the theme output without having to worry about 0 == ''.
Chris@0 877 $output = (string) $renderer->executeInRenderContext(new RenderContext(), function () use ($callback, $variables) {
Chris@0 878 return \Drupal::theme()->render($callback, $variables);
Chris@0 879 });
Chris@0 880 $this->verbose(
Chris@0 881 '<hr />' . 'Result:' . '<pre>' . Html::escape(var_export($output, TRUE)) . '</pre>'
Chris@0 882 . '<hr />' . 'Expected:' . '<pre>' . Html::escape(var_export($expected, TRUE)) . '</pre>'
Chris@0 883 . '<hr />' . $output
Chris@0 884 );
Chris@0 885 if (!$message) {
Chris@0 886 $message = '%callback rendered correctly.';
Chris@0 887 }
Chris@0 888 $message = format_string($message, ['%callback' => 'theme_' . $callback . '()']);
Chris@0 889 return $this->assertIdentical($output, $expected, $message, $group);
Chris@0 890 }
Chris@0 891
Chris@0 892 /**
Chris@0 893 * Asserts that a field exists in the current page with a given Xpath result.
Chris@0 894 *
Chris@0 895 * @param \SimpleXmlElement[] $fields
Chris@0 896 * Xml elements.
Chris@0 897 * @param string $value
Chris@0 898 * (optional) Value of the field to assert. You may pass in NULL (default) to skip
Chris@0 899 * checking the actual value, while still checking that the field exists.
Chris@0 900 * @param string $message
Chris@0 901 * (optional) A message to display with the assertion. Do not translate
Chris@0 902 * messages: use \Drupal\Component\Utility\SafeMarkup::format() to embed
Chris@0 903 * variables in the message text, not t(). If left blank, a default message
Chris@0 904 * will be displayed.
Chris@0 905 * @param string $group
Chris@0 906 * (optional) The group this message is in, which is displayed in a column
Chris@0 907 * in test output. Use 'Debug' to indicate this is debugging output. Do not
Chris@0 908 * translate this string. Defaults to 'Other'; most tests do not override
Chris@0 909 * this default.
Chris@0 910 *
Chris@0 911 * @return bool
Chris@0 912 * TRUE on pass, FALSE on fail.
Chris@0 913 */
Chris@0 914 protected function assertFieldsByValue($fields, $value = NULL, $message = '', $group = 'Other') {
Chris@0 915 // If value specified then check array for match.
Chris@0 916 $found = TRUE;
Chris@0 917 if (isset($value)) {
Chris@0 918 $found = FALSE;
Chris@0 919 if ($fields) {
Chris@0 920 foreach ($fields as $field) {
Chris@0 921 if (isset($field['value']) && $field['value'] == $value) {
Chris@0 922 // Input element with correct value.
Chris@0 923 $found = TRUE;
Chris@0 924 }
Chris@0 925 elseif (isset($field->option) || isset($field->optgroup)) {
Chris@0 926 // Select element found.
Chris@0 927 $selected = $this->getSelectedItem($field);
Chris@0 928 if ($selected === FALSE) {
Chris@0 929 // No item selected so use first item.
Chris@0 930 $items = $this->getAllOptions($field);
Chris@0 931 if (!empty($items) && $items[0]['value'] == $value) {
Chris@0 932 $found = TRUE;
Chris@0 933 }
Chris@0 934 }
Chris@0 935 elseif ($selected == $value) {
Chris@0 936 $found = TRUE;
Chris@0 937 }
Chris@0 938 }
Chris@0 939 elseif ((string) $field == $value) {
Chris@0 940 // Text area with correct text.
Chris@0 941 $found = TRUE;
Chris@0 942 }
Chris@0 943 }
Chris@0 944 }
Chris@0 945 }
Chris@0 946 return $this->assertTrue($fields && $found, $message, $group);
Chris@0 947 }
Chris@0 948
Chris@0 949 /**
Chris@0 950 * Asserts that a field exists in the current page by the given XPath.
Chris@0 951 *
Chris@0 952 * @param string $xpath
Chris@0 953 * XPath used to find the field.
Chris@0 954 * @param string $value
Chris@0 955 * (optional) Value of the field to assert. You may pass in NULL (default)
Chris@0 956 * to skip checking the actual value, while still checking that the field
Chris@0 957 * exists.
Chris@0 958 * @param string $message
Chris@0 959 * (optional) A message to display with the assertion. Do not translate
Chris@0 960 * messages: use \Drupal\Component\Utility\SafeMarkup::format() to embed
Chris@0 961 * variables in the message text, not t(). If left blank, a default message
Chris@0 962 * will be displayed.
Chris@0 963 * @param string $group
Chris@0 964 * (optional) The group this message is in, which is displayed in a column
Chris@0 965 * in test output. Use 'Debug' to indicate this is debugging output. Do not
Chris@0 966 * translate this string. Defaults to 'Other'; most tests do not override
Chris@0 967 * this default.
Chris@0 968 *
Chris@0 969 * @return bool
Chris@0 970 * TRUE on pass, FALSE on fail.
Chris@0 971 */
Chris@0 972 protected function assertFieldByXPath($xpath, $value = NULL, $message = '', $group = 'Other') {
Chris@0 973 $fields = $this->xpath($xpath);
Chris@0 974
Chris@0 975 return $this->assertFieldsByValue($fields, $value, $message, $group);
Chris@0 976 }
Chris@0 977
Chris@0 978 /**
Chris@0 979 * Get the selected value from a select field.
Chris@0 980 *
Chris@0 981 * @param \SimpleXmlElement $element
Chris@0 982 * SimpleXMLElement select element.
Chris@0 983 *
Chris@0 984 * @return bool
Chris@0 985 * The selected value or FALSE.
Chris@0 986 */
Chris@0 987 protected function getSelectedItem(\SimpleXMLElement $element) {
Chris@0 988 foreach ($element->children() as $item) {
Chris@0 989 if (isset($item['selected'])) {
Chris@0 990 return $item['value'];
Chris@0 991 }
Chris@0 992 elseif ($item->getName() == 'optgroup') {
Chris@0 993 if ($value = $this->getSelectedItem($item)) {
Chris@0 994 return $value;
Chris@0 995 }
Chris@0 996 }
Chris@0 997 }
Chris@0 998 return FALSE;
Chris@0 999 }
Chris@0 1000
Chris@0 1001 /**
Chris@0 1002 * Asserts that a field does not exist or its value does not match, by XPath.
Chris@0 1003 *
Chris@0 1004 * @param string $xpath
Chris@0 1005 * XPath used to find the field.
Chris@0 1006 * @param string $value
Chris@0 1007 * (optional) Value of the field, to assert that the field's value on the
Chris@0 1008 * page does not match it.
Chris@0 1009 * @param string $message
Chris@0 1010 * (optional) A message to display with the assertion. Do not translate
Chris@0 1011 * messages: use \Drupal\Component\Utility\SafeMarkup::format() to embed
Chris@0 1012 * variables in the message text, not t(). If left blank, a default message
Chris@0 1013 * will be displayed.
Chris@0 1014 * @param string $group
Chris@0 1015 * (optional) The group this message is in, which is displayed in a column
Chris@0 1016 * in test output. Use 'Debug' to indicate this is debugging output. Do not
Chris@0 1017 * translate this string. Defaults to 'Other'; most tests do not override
Chris@0 1018 * this default.
Chris@0 1019 *
Chris@0 1020 * @return bool
Chris@0 1021 * TRUE on pass, FALSE on fail.
Chris@0 1022 */
Chris@0 1023 protected function assertNoFieldByXPath($xpath, $value = NULL, $message = '', $group = 'Other') {
Chris@0 1024 $fields = $this->xpath($xpath);
Chris@0 1025
Chris@0 1026 // If value specified then check array for match.
Chris@0 1027 $found = TRUE;
Chris@0 1028 if (isset($value)) {
Chris@0 1029 $found = FALSE;
Chris@0 1030 if ($fields) {
Chris@0 1031 foreach ($fields as $field) {
Chris@0 1032 if ($field['value'] == $value) {
Chris@0 1033 $found = TRUE;
Chris@0 1034 }
Chris@0 1035 }
Chris@0 1036 }
Chris@0 1037 }
Chris@0 1038 return $this->assertFalse($fields && $found, $message, $group);
Chris@0 1039 }
Chris@0 1040
Chris@0 1041 /**
Chris@0 1042 * Asserts that a field exists with the given name and value.
Chris@0 1043 *
Chris@0 1044 * @param string $name
Chris@0 1045 * Name of field to assert.
Chris@0 1046 * @param string $value
Chris@0 1047 * (optional) Value of the field to assert. You may pass in NULL (default)
Chris@0 1048 * to skip checking the actual value, while still checking that the field
Chris@0 1049 * exists.
Chris@0 1050 * @param string $message
Chris@0 1051 * (optional) A message to display with the assertion. Do not translate
Chris@0 1052 * messages: use \Drupal\Component\Utility\SafeMarkup::format() to embed
Chris@0 1053 * variables in the message text, not t(). If left blank, a default message
Chris@0 1054 * will be displayed.
Chris@0 1055 * @param string $group
Chris@0 1056 * (optional) The group this message is in, which is displayed in a column
Chris@0 1057 * in test output. Use 'Debug' to indicate this is debugging output. Do not
Chris@0 1058 * translate this string. Defaults to 'Browser'; most tests do not override
Chris@0 1059 * this default.
Chris@0 1060 *
Chris@0 1061 * @return bool
Chris@0 1062 * TRUE on pass, FALSE on fail.
Chris@0 1063 */
Chris@0 1064 protected function assertFieldByName($name, $value = NULL, $message = NULL, $group = 'Browser') {
Chris@0 1065 if (!isset($message)) {
Chris@0 1066 if (!isset($value)) {
Chris@0 1067 $message = SafeMarkup::format('Found field with name @name', [
Chris@0 1068 '@name' => var_export($name, TRUE),
Chris@0 1069 ]);
Chris@0 1070 }
Chris@0 1071 else {
Chris@0 1072 $message = SafeMarkup::format('Found field with name @name and value @value', [
Chris@0 1073 '@name' => var_export($name, TRUE),
Chris@0 1074 '@value' => var_export($value, TRUE),
Chris@0 1075 ]);
Chris@0 1076 }
Chris@0 1077 }
Chris@0 1078 return $this->assertFieldByXPath($this->constructFieldXpath('name', $name), $value, $message, $group);
Chris@0 1079 }
Chris@0 1080
Chris@0 1081 /**
Chris@0 1082 * Asserts that a field does not exist with the given name and value.
Chris@0 1083 *
Chris@0 1084 * @param string $name
Chris@0 1085 * Name of field to assert.
Chris@0 1086 * @param string $value
Chris@0 1087 * (optional) Value for the field, to assert that the field's value on the
Chris@0 1088 * page doesn't match it. You may pass in NULL to skip checking the
Chris@0 1089 * value, while still checking that the field doesn't exist. However, the
Chris@0 1090 * default value ('') asserts that the field value is not an empty string.
Chris@0 1091 * @param string $message
Chris@0 1092 * (optional) A message to display with the assertion. Do not translate
Chris@0 1093 * messages: use \Drupal\Component\Utility\SafeMarkup::format() to embed
Chris@0 1094 * variables in the message text, not t(). If left blank, a default message
Chris@0 1095 * will be displayed.
Chris@0 1096 * @param string $group
Chris@0 1097 * (optional) The group this message is in, which is displayed in a column
Chris@0 1098 * in test output. Use 'Debug' to indicate this is debugging output. Do not
Chris@0 1099 * translate this string. Defaults to 'Browser'; most tests do not override
Chris@0 1100 * this default.
Chris@0 1101 *
Chris@0 1102 * @return bool
Chris@0 1103 * TRUE on pass, FALSE on fail.
Chris@0 1104 */
Chris@0 1105 protected function assertNoFieldByName($name, $value = '', $message = '', $group = 'Browser') {
Chris@0 1106 return $this->assertNoFieldByXPath($this->constructFieldXpath('name', $name), $value, $message ? $message : SafeMarkup::format('Did not find field by name @name', ['@name' => $name]), $group);
Chris@0 1107 }
Chris@0 1108
Chris@0 1109 /**
Chris@0 1110 * Asserts that a field exists with the given ID and value.
Chris@0 1111 *
Chris@0 1112 * @param string $id
Chris@0 1113 * ID of field to assert.
Chris@0 1114 * @param string|\Drupal\Component\Render\MarkupInterface $value
Chris@0 1115 * (optional) Value for the field to assert. You may pass in NULL to skip
Chris@0 1116 * checking the value, while still checking that the field exists.
Chris@0 1117 * However, the default value ('') asserts that the field value is an empty
Chris@0 1118 * string.
Chris@0 1119 * @param string|\Drupal\Component\Render\MarkupInterface $message
Chris@0 1120 * (optional) A message to display with the assertion. Do not translate
Chris@0 1121 * messages: use \Drupal\Component\Utility\SafeMarkup::format() to embed
Chris@0 1122 * variables in the message text, not t(). If left blank, a default message
Chris@0 1123 * will be displayed.
Chris@0 1124 * @param string $group
Chris@0 1125 * (optional) The group this message is in, which is displayed in a column
Chris@0 1126 * in test output. Use 'Debug' to indicate this is debugging output. Do not
Chris@0 1127 * translate this string. Defaults to 'Browser'; most tests do not override
Chris@0 1128 * this default.
Chris@0 1129 *
Chris@0 1130 * @return bool
Chris@0 1131 * TRUE on pass, FALSE on fail.
Chris@0 1132 */
Chris@0 1133 protected function assertFieldById($id, $value = '', $message = '', $group = 'Browser') {
Chris@0 1134 // Cast MarkupInterface objects to string.
Chris@0 1135 if (isset($value)) {
Chris@0 1136 $value = (string) $value;
Chris@0 1137 }
Chris@0 1138 $message = (string) $message;
Chris@0 1139 return $this->assertFieldByXPath($this->constructFieldXpath('id', $id), $value, $message ? $message : SafeMarkup::format('Found field by id @id', ['@id' => $id]), $group);
Chris@0 1140 }
Chris@0 1141
Chris@0 1142 /**
Chris@0 1143 * Asserts that a field does not exist with the given ID and value.
Chris@0 1144 *
Chris@0 1145 * @param string $id
Chris@0 1146 * ID of field to assert.
Chris@0 1147 * @param string $value
Chris@0 1148 * (optional) Value for the field, to assert that the field's value on the
Chris@0 1149 * page doesn't match it. You may pass in NULL to skip checking the value,
Chris@0 1150 * while still checking that the field doesn't exist. However, the default
Chris@0 1151 * value ('') asserts that the field value is not an empty string.
Chris@0 1152 * @param string $message
Chris@0 1153 * (optional) A message to display with the assertion. Do not translate
Chris@0 1154 * messages: use \Drupal\Component\Utility\SafeMarkup::format() to embed
Chris@0 1155 * variables in the message text, not t(). If left blank, a default message
Chris@0 1156 * will be displayed.
Chris@0 1157 * @param string $group
Chris@0 1158 * (optional) The group this message is in, which is displayed in a column
Chris@0 1159 * in test output. Use 'Debug' to indicate this is debugging output. Do not
Chris@0 1160 * translate this string. Defaults to 'Browser'; most tests do not override
Chris@0 1161 * this default.
Chris@0 1162 *
Chris@0 1163 * @return bool
Chris@0 1164 * TRUE on pass, FALSE on fail.
Chris@0 1165 */
Chris@0 1166 protected function assertNoFieldById($id, $value = '', $message = '', $group = 'Browser') {
Chris@0 1167 return $this->assertNoFieldByXPath($this->constructFieldXpath('id', $id), $value, $message ? $message : SafeMarkup::format('Did not find field by id @id', ['@id' => $id]), $group);
Chris@0 1168 }
Chris@0 1169
Chris@0 1170 /**
Chris@0 1171 * Asserts that a checkbox field in the current page is checked.
Chris@0 1172 *
Chris@0 1173 * @param string $id
Chris@0 1174 * ID of field to assert.
Chris@0 1175 * @param string $message
Chris@0 1176 * (optional) A message to display with the assertion. Do not translate
Chris@0 1177 * messages: use \Drupal\Component\Utility\SafeMarkup::format() to embed
Chris@0 1178 * variables in the message text, not t(). If left blank, a default message
Chris@0 1179 * will be displayed.
Chris@0 1180 * @param string $group
Chris@0 1181 * (optional) The group this message is in, which is displayed in a column
Chris@0 1182 * in test output. Use 'Debug' to indicate this is debugging output. Do not
Chris@0 1183 * translate this string. Defaults to 'Browser'; most tests do not override
Chris@0 1184 * this default.
Chris@0 1185 *
Chris@0 1186 * @return bool
Chris@0 1187 * TRUE on pass, FALSE on fail.
Chris@0 1188 */
Chris@0 1189 protected function assertFieldChecked($id, $message = '', $group = 'Browser') {
Chris@0 1190 $elements = $this->xpath('//input[@id=:id]', [':id' => $id]);
Chris@0 1191 return $this->assertTrue(isset($elements[0]) && !empty($elements[0]['checked']), $message ? $message : SafeMarkup::format('Checkbox field @id is checked.', ['@id' => $id]), $group);
Chris@0 1192 }
Chris@0 1193
Chris@0 1194 /**
Chris@0 1195 * Asserts that a checkbox field in the current page is not checked.
Chris@0 1196 *
Chris@0 1197 * @param string $id
Chris@0 1198 * ID of field to assert.
Chris@0 1199 * @param string $message
Chris@0 1200 * (optional) A message to display with the assertion. Do not translate
Chris@0 1201 * messages: use \Drupal\Component\Utility\SafeMarkup::format() to embed
Chris@0 1202 * variables in the message text, not t(). If left blank, a default message
Chris@0 1203 * will be displayed.
Chris@0 1204 * @param string $group
Chris@0 1205 * (optional) The group this message is in, which is displayed in a column
Chris@0 1206 * in test output. Use 'Debug' to indicate this is debugging output. Do not
Chris@0 1207 * translate this string. Defaults to 'Browser'; most tests do not override
Chris@0 1208 * this default.
Chris@0 1209 *
Chris@0 1210 * @return bool
Chris@0 1211 * TRUE on pass, FALSE on fail.
Chris@0 1212 */
Chris@0 1213 protected function assertNoFieldChecked($id, $message = '', $group = 'Browser') {
Chris@0 1214 $elements = $this->xpath('//input[@id=:id]', [':id' => $id]);
Chris@0 1215 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 1216 }
Chris@0 1217
Chris@0 1218 /**
Chris@0 1219 * Asserts that a select option in the current page exists.
Chris@0 1220 *
Chris@0 1221 * @param string $id
Chris@0 1222 * ID of select field to assert.
Chris@0 1223 * @param string $option
Chris@0 1224 * Option to assert.
Chris@0 1225 * @param string $message
Chris@0 1226 * (optional) A message to display with the assertion. Do not translate
Chris@0 1227 * messages: use \Drupal\Component\Utility\SafeMarkup::format() to embed
Chris@0 1228 * variables in the message text, not t(). If left blank, a default message
Chris@0 1229 * will be displayed.
Chris@0 1230 * @param string $group
Chris@0 1231 * (optional) The group this message is in, which is displayed in a column
Chris@0 1232 * in test output. Use 'Debug' to indicate this is debugging output. Do not
Chris@0 1233 * translate this string. Defaults to 'Browser'; most tests do not override
Chris@0 1234 * this default.
Chris@0 1235 *
Chris@0 1236 * @return bool
Chris@0 1237 * TRUE on pass, FALSE on fail.
Chris@0 1238 */
Chris@0 1239 protected function assertOption($id, $option, $message = '', $group = 'Browser') {
Chris@0 1240 $options = $this->xpath('//select[@id=:id]//option[@value=:option]', [':id' => $id, ':option' => $option]);
Chris@0 1241 return $this->assertTrue(isset($options[0]), $message ? $message : SafeMarkup::format('Option @option for field @id exists.', ['@option' => $option, '@id' => $id]), $group);
Chris@0 1242 }
Chris@0 1243
Chris@0 1244 /**
Chris@0 1245 * Asserts that a select option with the visible text exists.
Chris@0 1246 *
Chris@0 1247 * @param string $id
Chris@0 1248 * The ID of the select field to assert.
Chris@0 1249 * @param string $text
Chris@0 1250 * The text for the option tag to assert.
Chris@0 1251 * @param string $message
Chris@0 1252 * (optional) A message to display with the assertion.
Chris@0 1253 *
Chris@0 1254 * @return bool
Chris@0 1255 * TRUE on pass, FALSE on fail.
Chris@0 1256 */
Chris@0 1257 protected function assertOptionByText($id, $text, $message = '') {
Chris@0 1258 $options = $this->xpath('//select[@id=:id]//option[normalize-space(text())=:text]', [':id' => $id, ':text' => $text]);
Chris@0 1259 return $this->assertTrue(isset($options[0]), $message ?: 'Option with text label ' . $text . ' for select field ' . $id . ' exits.');
Chris@0 1260 }
Chris@0 1261
Chris@0 1262 /**
Chris@0 1263 * Asserts that a select option in the current page exists.
Chris@0 1264 *
Chris@0 1265 * @param string $drupal_selector
Chris@0 1266 * The data drupal selector of select field to assert.
Chris@0 1267 * @param string $option
Chris@0 1268 * Option to assert.
Chris@0 1269 * @param string $message
Chris@0 1270 * (optional) A message to display with the assertion. Do not translate
Chris@0 1271 * messages: use \Drupal\Component\Utility\SafeMarkup::format() to embed
Chris@0 1272 * variables in the message text, not t(). If left blank, a default message
Chris@0 1273 * will be displayed.
Chris@0 1274 * @param string $group
Chris@0 1275 * (optional) The group this message is in, which is displayed in a column
Chris@0 1276 * in test output. Use 'Debug' to indicate this is debugging output. Do not
Chris@0 1277 * translate this string. Defaults to 'Browser'; most tests do not override
Chris@0 1278 * this default.
Chris@0 1279 *
Chris@0 1280 * @return bool
Chris@0 1281 * TRUE on pass, FALSE on fail.
Chris@0 1282 */
Chris@0 1283 protected function assertOptionWithDrupalSelector($drupal_selector, $option, $message = '', $group = 'Browser') {
Chris@0 1284 $options = $this->xpath('//select[@data-drupal-selector=:data_drupal_selector]//option[@value=:option]', [':data_drupal_selector' => $drupal_selector, ':option' => $option]);
Chris@0 1285 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 1286 }
Chris@0 1287
Chris@0 1288 /**
Chris@0 1289 * Asserts that a select option in the current page does not exist.
Chris@0 1290 *
Chris@0 1291 * @param string $id
Chris@0 1292 * ID of select field to assert.
Chris@0 1293 * @param string $option
Chris@0 1294 * Option to assert.
Chris@0 1295 * @param string $message
Chris@0 1296 * (optional) A message to display with the assertion. Do not translate
Chris@0 1297 * messages: use \Drupal\Component\Utility\SafeMarkup::format() to embed
Chris@0 1298 * variables in the message text, not t(). If left blank, a default message
Chris@0 1299 * will be displayed.
Chris@0 1300 * @param string $group
Chris@0 1301 * (optional) The group this message is in, which is displayed in a column
Chris@0 1302 * in test output. Use 'Debug' to indicate this is debugging output. Do not
Chris@0 1303 * translate this string. Defaults to 'Browser'; most tests do not override
Chris@0 1304 * this default.
Chris@0 1305 *
Chris@0 1306 * @return bool
Chris@0 1307 * TRUE on pass, FALSE on fail.
Chris@0 1308 */
Chris@0 1309 protected function assertNoOption($id, $option, $message = '', $group = 'Browser') {
Chris@0 1310 $selects = $this->xpath('//select[@id=:id]', [':id' => $id]);
Chris@0 1311 $options = $this->xpath('//select[@id=:id]//option[@value=:option]', [':id' => $id, ':option' => $option]);
Chris@0 1312 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 1313 }
Chris@0 1314
Chris@0 1315 /**
Chris@0 1316 * Asserts that a select option in the current page is checked.
Chris@0 1317 *
Chris@0 1318 * @param string $id
Chris@0 1319 * ID of select field to assert.
Chris@0 1320 * @param string $option
Chris@0 1321 * Option to assert.
Chris@0 1322 * @param string $message
Chris@0 1323 * (optional) A message to display with the assertion. Do not translate
Chris@0 1324 * messages: use \Drupal\Component\Utility\SafeMarkup::format() to embed
Chris@0 1325 * variables in the message text, not t(). If left blank, a default message
Chris@0 1326 * will be displayed.
Chris@0 1327 * @param string $group
Chris@0 1328 * (optional) The group this message is in, which is displayed in a column
Chris@0 1329 * in test output. Use 'Debug' to indicate this is debugging output. Do not
Chris@0 1330 * translate this string. Defaults to 'Browser'; most tests do not override
Chris@0 1331 * this default.
Chris@0 1332 *
Chris@0 1333 * @return bool
Chris@0 1334 * TRUE on pass, FALSE on fail.
Chris@0 1335 *
Chris@0 1336 * @todo $id is unusable. Replace with $name.
Chris@0 1337 */
Chris@0 1338 protected function assertOptionSelected($id, $option, $message = '', $group = 'Browser') {
Chris@0 1339 $elements = $this->xpath('//select[@id=:id]//option[@value=:option]', [':id' => $id, ':option' => $option]);
Chris@0 1340 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 1341 }
Chris@0 1342
Chris@0 1343 /**
Chris@0 1344 * Asserts that a select option in the current page is checked.
Chris@0 1345 *
Chris@0 1346 * @param string $drupal_selector
Chris@0 1347 * The data drupal selector of select field to assert.
Chris@0 1348 * @param string $option
Chris@0 1349 * Option to assert.
Chris@0 1350 * @param string $message
Chris@0 1351 * (optional) A message to display with the assertion. Do not translate
Chris@0 1352 * messages: use \Drupal\Component\Utility\SafeMarkup::format() to embed
Chris@0 1353 * variables in the message text, not t(). If left blank, a default message
Chris@0 1354 * will be displayed.
Chris@0 1355 * @param string $group
Chris@0 1356 * (optional) The group this message is in, which is displayed in a column
Chris@0 1357 * in test output. Use 'Debug' to indicate this is debugging output. Do not
Chris@0 1358 * translate this string. Defaults to 'Browser'; most tests do not override
Chris@0 1359 * this default.
Chris@0 1360 *
Chris@0 1361 * @return bool
Chris@0 1362 * TRUE on pass, FALSE on fail.
Chris@0 1363 *
Chris@0 1364 * @todo $id is unusable. Replace with $name.
Chris@0 1365 */
Chris@0 1366 protected function assertOptionSelectedWithDrupalSelector($drupal_selector, $option, $message = '', $group = 'Browser') {
Chris@0 1367 $elements = $this->xpath('//select[@data-drupal-selector=:data_drupal_selector]//option[@value=:option]', [':data_drupal_selector' => $drupal_selector, ':option' => $option]);
Chris@0 1368 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 1369 }
Chris@0 1370
Chris@0 1371 /**
Chris@0 1372 * Asserts that a select option in the current page is not checked.
Chris@0 1373 *
Chris@0 1374 * @param string $id
Chris@0 1375 * ID of select field to assert.
Chris@0 1376 * @param string $option
Chris@0 1377 * Option to assert.
Chris@0 1378 * @param string $message
Chris@0 1379 * (optional) A message to display with the assertion. Do not translate
Chris@0 1380 * messages: use \Drupal\Component\Utility\SafeMarkup::format() to embed
Chris@0 1381 * variables in the message text, not t(). If left blank, a default message
Chris@0 1382 * will be displayed.
Chris@0 1383 * @param string $group
Chris@0 1384 * (optional) The group this message is in, which is displayed in a column
Chris@0 1385 * in test output. Use 'Debug' to indicate this is debugging output. Do not
Chris@0 1386 * translate this string. Defaults to 'Browser'; most tests do not override
Chris@0 1387 * this default.
Chris@0 1388 *
Chris@0 1389 * @return bool
Chris@0 1390 * TRUE on pass, FALSE on fail.
Chris@0 1391 */
Chris@0 1392 protected function assertNoOptionSelected($id, $option, $message = '', $group = 'Browser') {
Chris@0 1393 $elements = $this->xpath('//select[@id=:id]//option[@value=:option]', [':id' => $id, ':option' => $option]);
Chris@0 1394 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 1395 }
Chris@0 1396
Chris@0 1397 /**
Chris@0 1398 * Asserts that a field exists with the given name or ID.
Chris@0 1399 *
Chris@0 1400 * @param string $field
Chris@0 1401 * Name or ID of field to assert.
Chris@0 1402 * @param string $message
Chris@0 1403 * (optional) A message to display with the assertion. Do not translate
Chris@0 1404 * messages: use \Drupal\Component\Utility\SafeMarkup::format() to embed
Chris@0 1405 * variables in the message text, not t(). If left blank, a default message
Chris@0 1406 * will be displayed.
Chris@0 1407 * @param string $group
Chris@0 1408 * (optional) The group this message is in, which is displayed in a column
Chris@0 1409 * in test output. Use 'Debug' to indicate this is debugging output. Do not
Chris@0 1410 * translate this string. Defaults to 'Other'; most tests do not override
Chris@0 1411 * this default.
Chris@0 1412 *
Chris@0 1413 * @return bool
Chris@0 1414 * TRUE on pass, FALSE on fail.
Chris@0 1415 */
Chris@0 1416 protected function assertField($field, $message = '', $group = 'Other') {
Chris@0 1417 return $this->assertFieldByXPath($this->constructFieldXpath('name', $field) . '|' . $this->constructFieldXpath('id', $field), NULL, $message, $group);
Chris@0 1418 }
Chris@0 1419
Chris@0 1420 /**
Chris@0 1421 * Asserts that a field does not exist with the given name or ID.
Chris@0 1422 *
Chris@0 1423 * @param string $field
Chris@0 1424 * Name or ID of field to assert.
Chris@0 1425 * @param string $message
Chris@0 1426 * (optional) A message to display with the assertion. Do not translate
Chris@0 1427 * messages: use \Drupal\Component\Utility\SafeMarkup::format() to embed
Chris@0 1428 * variables in the message text, not t(). If left blank, a default message
Chris@0 1429 * will be displayed.
Chris@0 1430 * @param string $group
Chris@0 1431 * (optional) The group this message is in, which is displayed in a column
Chris@0 1432 * in test output. Use 'Debug' to indicate this is debugging output. Do not
Chris@0 1433 * translate this string. Defaults to 'Other'; most tests do not override
Chris@0 1434 * this default.
Chris@0 1435 *
Chris@0 1436 * @return bool
Chris@0 1437 * TRUE on pass, FALSE on fail.
Chris@0 1438 */
Chris@0 1439 protected function assertNoField($field, $message = '', $group = 'Other') {
Chris@0 1440 return $this->assertNoFieldByXPath($this->constructFieldXpath('name', $field) . '|' . $this->constructFieldXpath('id', $field), NULL, $message, $group);
Chris@0 1441 }
Chris@0 1442
Chris@0 1443 /**
Chris@0 1444 * Asserts that each HTML ID is used for just a single element.
Chris@0 1445 *
Chris@0 1446 * @param string $message
Chris@0 1447 * (optional) A message to display with the assertion. Do not translate
Chris@0 1448 * messages: use \Drupal\Component\Utility\SafeMarkup::format() to embed
Chris@0 1449 * variables in the message text, not t(). If left blank, a default message
Chris@0 1450 * will be displayed.
Chris@0 1451 * @param string $group
Chris@0 1452 * (optional) The group this message is in, which is displayed in a column
Chris@0 1453 * in test output. Use 'Debug' to indicate this is debugging output. Do not
Chris@0 1454 * translate this string. Defaults to 'Other'; most tests do not override
Chris@0 1455 * this default.
Chris@0 1456 * @param array $ids_to_skip
Chris@0 1457 * An optional array of ids to skip when checking for duplicates. It is
Chris@0 1458 * always a bug to have duplicate HTML IDs, so this parameter is to enable
Chris@0 1459 * incremental fixing of core code. Whenever a test passes this parameter,
Chris@0 1460 * it should add a "todo" comment above the call to this function explaining
Chris@0 1461 * the legacy bug that the test wishes to ignore and including a link to an
Chris@0 1462 * issue that is working to fix that legacy bug.
Chris@0 1463 *
Chris@0 1464 * @return bool
Chris@0 1465 * TRUE on pass, FALSE on fail.
Chris@0 1466 */
Chris@0 1467 protected function assertNoDuplicateIds($message = '', $group = 'Other', $ids_to_skip = []) {
Chris@0 1468 $status = TRUE;
Chris@0 1469 foreach ($this->xpath('//*[@id]') as $element) {
Chris@0 1470 $id = (string) $element['id'];
Chris@0 1471 if (isset($seen_ids[$id]) && !in_array($id, $ids_to_skip)) {
Chris@0 1472 $this->fail(SafeMarkup::format('The HTML ID %id is unique.', ['%id' => $id]), $group);
Chris@0 1473 $status = FALSE;
Chris@0 1474 }
Chris@0 1475 $seen_ids[$id] = TRUE;
Chris@0 1476 }
Chris@0 1477 return $this->assert($status, $message, $group);
Chris@0 1478 }
Chris@0 1479
Chris@0 1480 /**
Chris@0 1481 * Helper: Constructs an XPath for the given set of attributes and value.
Chris@0 1482 *
Chris@0 1483 * @param string $attribute
Chris@0 1484 * Field attributes.
Chris@0 1485 * @param string $value
Chris@0 1486 * Value of field.
Chris@0 1487 *
Chris@0 1488 * @return string
Chris@0 1489 * XPath for specified values.
Chris@0 1490 */
Chris@0 1491 protected function constructFieldXpath($attribute, $value) {
Chris@0 1492 $xpath = '//textarea[@' . $attribute . '=:value]|//input[@' . $attribute . '=:value]|//select[@' . $attribute . '=:value]';
Chris@0 1493 return $this->buildXPathQuery($xpath, [':value' => $value]);
Chris@0 1494 }
Chris@0 1495
Chris@0 1496 }