annotate core/tests/Drupal/FunctionalTests/AssertLegacyTrait.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents af1871eacc83
children
rev   line source
Chris@0 1 <?php
Chris@18 2 // @codingStandardsIgnoreFile
Chris@0 3 namespace Drupal\FunctionalTests;
Chris@0 4
Chris@0 5 use Behat\Mink\Element\NodeElement;
Chris@0 6 use Behat\Mink\Exception\ExpectationException;
Chris@0 7 use Behat\Mink\Selector\Xpath\Escaper;
Chris@0 8 use Drupal\Component\Render\FormattableMarkup;
Chris@0 9 use Drupal\Component\Utility\Xss;
Chris@0 10 use Drupal\KernelTests\AssertLegacyTrait as BaseAssertLegacyTrait;
Chris@0 11
Chris@0 12 /**
Chris@0 13 * Provides convenience methods for assertions in browser tests.
Chris@0 14 *
Chris@18 15 * Deprecated Scheduled for removal in Drupal 10.0.0. Use the methods on
Chris@0 16 * \Drupal\Tests\WebAssert instead, for example
Chris@0 17 * @code
Chris@0 18 * $this->assertSession()->statusCodeEquals(200);
Chris@0 19 * @endcode
Chris@18 20 *
Chris@18 21 * @todo https://www.drupal.org/project/drupal/issues/3031580 Note that
Chris@18 22 * deprecations in this file do not use the @ symbol in Drupal 8 because this
Chris@18 23 * will be removed in Drupal 10.0.0. Adding the @ back should re-enable coding
Chris@18 24 * stanadrds checks.
Chris@0 25 */
Chris@0 26 trait AssertLegacyTrait {
Chris@0 27
Chris@0 28 use BaseAssertLegacyTrait;
Chris@0 29
Chris@0 30 /**
Chris@0 31 * Asserts that the element with the given CSS selector is present.
Chris@0 32 *
Chris@0 33 * @param string $css_selector
Chris@0 34 * The CSS selector identifying the element to check.
Chris@0 35 *
Chris@18 36 * Deprecated Scheduled for removal in Drupal 10.0.0.
Chris@0 37 * Use $this->assertSession()->elementExists() instead.
Chris@0 38 */
Chris@0 39 protected function assertElementPresent($css_selector) {
Chris@0 40 $this->assertSession()->elementExists('css', $css_selector);
Chris@0 41 }
Chris@0 42
Chris@0 43 /**
Chris@0 44 * Asserts that the element with the given CSS selector is not present.
Chris@0 45 *
Chris@0 46 * @param string $css_selector
Chris@0 47 * The CSS selector identifying the element to check.
Chris@0 48 *
Chris@18 49 * Deprecated Scheduled for removal in Drupal 10.0.0.
Chris@0 50 * Use $this->assertSession()->elementNotExists() instead.
Chris@0 51 */
Chris@0 52 protected function assertElementNotPresent($css_selector) {
Chris@0 53 $this->assertSession()->elementNotExists('css', $css_selector);
Chris@0 54 }
Chris@0 55
Chris@0 56 /**
Chris@0 57 * Passes if the page (with HTML stripped) contains the text.
Chris@0 58 *
Chris@0 59 * Note that stripping HTML tags also removes their attributes, such as
Chris@0 60 * the values of text fields.
Chris@0 61 *
Chris@0 62 * @param string $text
Chris@0 63 * Plain text to look for.
Chris@0 64 *
Chris@18 65 * Deprecated Scheduled for removal in Drupal 10.0.0.
Chris@0 66 * Use instead:
Chris@0 67 * - $this->assertSession()->responseContains() for non-HTML responses,
Chris@0 68 * like XML or Json.
Chris@0 69 * - $this->assertSession()->pageTextContains() for HTML responses. Unlike
Chris@0 70 * the deprecated assertText(), the passed text should be HTML decoded,
Chris@0 71 * exactly as a human sees it in the browser.
Chris@0 72 */
Chris@0 73 protected function assertText($text) {
Chris@0 74 // Cast MarkupInterface to string.
Chris@0 75 $text = (string) $text;
Chris@0 76
Chris@0 77 $content_type = $this->getSession()->getResponseHeader('Content-type');
Chris@0 78 // In case of a Non-HTML response (example: XML) check the original
Chris@0 79 // response.
Chris@0 80 if (strpos($content_type, 'html') === FALSE) {
Chris@0 81 $this->assertSession()->responseContains($text);
Chris@0 82 }
Chris@0 83 else {
Chris@0 84 $this->assertTextHelper($text, FALSE);
Chris@0 85 }
Chris@0 86 }
Chris@0 87
Chris@0 88 /**
Chris@0 89 * Passes if the page (with HTML stripped) does not contains the text.
Chris@0 90 *
Chris@0 91 * Note that stripping HTML tags also removes their attributes, such as
Chris@0 92 * the values of text fields.
Chris@0 93 *
Chris@0 94 * @param string $text
Chris@0 95 * Plain text to look for.
Chris@0 96 *
Chris@18 97 * Deprecated Scheduled for removal in Drupal 10.0.0.
Chris@0 98 * Use instead:
Chris@0 99 * - $this->assertSession()->responseNotContains() for non-HTML responses,
Chris@0 100 * like XML or Json.
Chris@0 101 * - $this->assertSession()->pageTextNotContains() for HTML responses.
Chris@0 102 * Unlike the deprecated assertNoText(), the passed text should be HTML
Chris@0 103 * decoded, exactly as a human sees it in the browser.
Chris@0 104 */
Chris@0 105 protected function assertNoText($text) {
Chris@0 106 // Cast MarkupInterface to string.
Chris@0 107 $text = (string) $text;
Chris@0 108
Chris@0 109 $content_type = $this->getSession()->getResponseHeader('Content-type');
Chris@0 110 // In case of a Non-HTML response (example: XML) check the original
Chris@0 111 // response.
Chris@0 112 if (strpos($content_type, 'html') === FALSE) {
Chris@0 113 $this->assertSession()->responseNotContains($text);
Chris@0 114 }
Chris@0 115 else {
Chris@0 116 $this->assertTextHelper($text);
Chris@0 117 }
Chris@0 118 }
Chris@0 119
Chris@0 120 /**
Chris@0 121 * Helper for assertText and assertNoText.
Chris@0 122 *
Chris@0 123 * @param string $text
Chris@0 124 * Plain text to look for.
Chris@0 125 * @param bool $not_exists
Chris@0 126 * (optional) TRUE if this text should not exist, FALSE if it should.
Chris@0 127 * Defaults to TRUE.
Chris@0 128 *
Chris@0 129 * @return bool
Chris@0 130 * TRUE on pass, FALSE on fail.
Chris@0 131 */
Chris@0 132 protected function assertTextHelper($text, $not_exists = TRUE) {
Chris@0 133 $args = ['@text' => $text];
Chris@0 134 $message = $not_exists ? new FormattableMarkup('"@text" not found', $args) : new FormattableMarkup('"@text" found', $args);
Chris@0 135
Chris@0 136 $raw_content = $this->getSession()->getPage()->getContent();
Chris@0 137 // Trying to simulate what the user sees, given that it removes all text
Chris@0 138 // inside the head tags, removes inline Javascript, fix all HTML entities,
Chris@0 139 // removes dangerous protocols and filtering out all HTML tags, as they are
Chris@0 140 // not visible in a normal browser.
Chris@0 141 $raw_content = preg_replace('@<head>(.+?)</head>@si', '', $raw_content);
Chris@0 142 $page_text = Xss::filter($raw_content, []);
Chris@0 143
Chris@0 144 $actual = $not_exists == (strpos($page_text, (string) $text) === FALSE);
Chris@0 145 $this->assertTrue($actual, $message);
Chris@0 146
Chris@0 147 return $actual;
Chris@0 148 }
Chris@0 149
Chris@0 150 /**
Chris@0 151 * Passes if the text is found ONLY ONCE on the text version of the page.
Chris@0 152 *
Chris@0 153 * The text version is the equivalent of what a user would see when viewing
Chris@0 154 * through a web browser. In other words the HTML has been filtered out of
Chris@0 155 * the contents.
Chris@0 156 *
Chris@0 157 * @param string|\Drupal\Component\Render\MarkupInterface $text
Chris@0 158 * Plain text to look for.
Chris@0 159 * @param string $message
Chris@0 160 * (optional) A message to display with the assertion. Do not translate
Chris@0 161 * messages with t(). If left blank, a default message will be displayed.
Chris@0 162 *
Chris@18 163 * Deprecated Scheduled for removal in Drupal 10.0.0.
Chris@0 164 * Use $this->getSession()->getPage()->getText() and substr_count() instead.
Chris@0 165 */
Chris@0 166 protected function assertUniqueText($text, $message = NULL) {
Chris@0 167 // Cast MarkupInterface objects to string.
Chris@0 168 $text = (string) $text;
Chris@0 169
Chris@0 170 $message = $message ?: "'$text' found only once on the page";
Chris@0 171 $page_text = $this->getSession()->getPage()->getText();
Chris@0 172 $nr_found = substr_count($page_text, $text);
Chris@0 173 $this->assertSame(1, $nr_found, $message);
Chris@0 174 }
Chris@0 175
Chris@0 176 /**
Chris@0 177 * Passes if the text is found MORE THAN ONCE on the text version of the page.
Chris@0 178 *
Chris@0 179 * The text version is the equivalent of what a user would see when viewing
Chris@0 180 * through a web browser. In other words the HTML has been filtered out of
Chris@0 181 * the contents.
Chris@0 182 *
Chris@0 183 * @param string|\Drupal\Component\Render\MarkupInterface $text
Chris@0 184 * Plain text to look for.
Chris@0 185 * @param string $message
Chris@0 186 * (optional) A message to display with the assertion. Do not translate
Chris@0 187 * messages with t(). If left blank, a default message will be displayed.
Chris@0 188 *
Chris@18 189 * Deprecated Scheduled for removal in Drupal 10.0.0.
Chris@0 190 * Use $this->getSession()->getPage()->getText() and substr_count() instead.
Chris@0 191 */
Chris@0 192 protected function assertNoUniqueText($text, $message = '') {
Chris@0 193 // Cast MarkupInterface objects to string.
Chris@0 194 $text = (string) $text;
Chris@0 195
Chris@0 196 $message = $message ?: "'$text' found more than once on the page";
Chris@0 197 $page_text = $this->getSession()->getPage()->getText();
Chris@0 198 $nr_found = substr_count($page_text, $text);
Chris@0 199 $this->assertGreaterThan(1, $nr_found, $message);
Chris@0 200 }
Chris@0 201
Chris@0 202 /**
Chris@0 203 * Asserts the page responds with the specified response code.
Chris@0 204 *
Chris@0 205 * @param int $code
Chris@0 206 * Response code. For example 200 is a successful page request. For a list
Chris@0 207 * of all codes see http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html.
Chris@0 208 *
Chris@18 209 * Deprecated Scheduled for removal in Drupal 10.0.0.
Chris@0 210 * Use $this->assertSession()->statusCodeEquals() instead.
Chris@0 211 */
Chris@0 212 protected function assertResponse($code) {
Chris@0 213 $this->assertSession()->statusCodeEquals($code);
Chris@0 214 }
Chris@0 215
Chris@0 216 /**
Chris@0 217 * Asserts that a field exists with the given name and value.
Chris@0 218 *
Chris@0 219 * @param string $name
Chris@0 220 * Name of field to assert.
Chris@0 221 * @param string $value
Chris@0 222 * (optional) Value of the field to assert. You may pass in NULL (default)
Chris@0 223 * to skip checking the actual value, while still checking that the field
Chris@0 224 * exists.
Chris@0 225 *
Chris@18 226 * Deprecated Scheduled for removal in Drupal 10.0.0.
Chris@0 227 * Use $this->assertSession()->fieldExists() or
Chris@0 228 * $this->assertSession()->buttonExists() or
Chris@0 229 * $this->assertSession()->fieldValueEquals() instead.
Chris@0 230 */
Chris@0 231 protected function assertFieldByName($name, $value = NULL) {
Chris@0 232 $this->assertFieldByXPath($this->constructFieldXpath('name', $name), $value);
Chris@0 233 }
Chris@0 234
Chris@0 235 /**
Chris@0 236 * Asserts that a field does not exist with the given name and value.
Chris@0 237 *
Chris@0 238 * @param string $name
Chris@0 239 * Name of field to assert.
Chris@0 240 * @param string $value
Chris@0 241 * (optional) Value for the field, to assert that the field's value on the
Chris@0 242 * page does not match it. You may pass in NULL to skip checking the
Chris@0 243 * value, while still checking that the field does not exist. However, the
Chris@0 244 * default value ('') asserts that the field value is not an empty string.
Chris@0 245 *
Chris@18 246 * Deprecated Scheduled for removal in Drupal 10.0.0.
Chris@0 247 * Use $this->assertSession()->fieldNotExists() or
Chris@0 248 * $this->assertSession()->buttonNotExists() or
Chris@0 249 * $this->assertSession()->fieldValueNotEquals() instead.
Chris@0 250 */
Chris@0 251 protected function assertNoFieldByName($name, $value = '') {
Chris@0 252 $this->assertNoFieldByXPath($this->constructFieldXpath('name', $name), $value);
Chris@0 253 }
Chris@0 254
Chris@0 255 /**
Chris@0 256 * Asserts that a field exists with the given ID and value.
Chris@0 257 *
Chris@0 258 * @param string $id
Chris@0 259 * ID of field to assert.
Chris@0 260 * @param string|\Drupal\Component\Render\MarkupInterface $value
Chris@0 261 * (optional) Value for the field to assert. You may pass in NULL to skip
Chris@0 262 * checking the value, while still checking that the field exists.
Chris@0 263 * However, the default value ('') asserts that the field value is an empty
Chris@0 264 * string.
Chris@0 265 *
Chris@0 266 * @throws \Behat\Mink\Exception\ElementNotFoundException
Chris@0 267 *
Chris@18 268 * Deprecated Scheduled for removal in Drupal 10.0.0.
Chris@0 269 * Use $this->assertSession()->fieldExists() or
Chris@0 270 * $this->assertSession()->buttonExists() or
Chris@0 271 * $this->assertSession()->fieldValueEquals() instead.
Chris@0 272 */
Chris@0 273 protected function assertFieldById($id, $value = '') {
Chris@0 274 $this->assertFieldByXPath($this->constructFieldXpath('id', $id), $value);
Chris@0 275 }
Chris@0 276
Chris@0 277 /**
Chris@0 278 * Asserts that a field exists with the given name or ID.
Chris@0 279 *
Chris@0 280 * @param string $field
Chris@0 281 * Name or ID of field to assert.
Chris@0 282 *
Chris@18 283 * Deprecated Scheduled for removal in Drupal 10.0.0.
Chris@0 284 * Use $this->assertSession()->fieldExists() or
Chris@0 285 * $this->assertSession()->buttonExists() instead.
Chris@0 286 */
Chris@0 287 protected function assertField($field) {
Chris@0 288 $this->assertFieldByXPath($this->constructFieldXpath('name', $field) . '|' . $this->constructFieldXpath('id', $field));
Chris@0 289 }
Chris@0 290
Chris@0 291 /**
Chris@0 292 * Asserts that a field does NOT exist with the given name or ID.
Chris@0 293 *
Chris@0 294 * @param string $field
Chris@0 295 * Name or ID of field to assert.
Chris@0 296 *
Chris@18 297 * Deprecated Scheduled for removal in Drupal 10.0.0.
Chris@0 298 * Use $this->assertSession()->fieldNotExists() or
Chris@0 299 * $this->assertSession()->buttonNotExists() instead.
Chris@0 300 */
Chris@0 301 protected function assertNoField($field) {
Chris@0 302 $this->assertNoFieldByXPath($this->constructFieldXpath('name', $field) . '|' . $this->constructFieldXpath('id', $field));
Chris@0 303 }
Chris@0 304
Chris@0 305 /**
Chris@0 306 * Passes if the raw text IS found on the loaded page, fail otherwise.
Chris@0 307 *
Chris@0 308 * Raw text refers to the raw HTML that the page generated.
Chris@0 309 *
Chris@0 310 * @param string $raw
Chris@0 311 * Raw (HTML) string to look for.
Chris@0 312 *
Chris@18 313 * Deprecated Scheduled for removal in Drupal 10.0.0.
Chris@0 314 * Use $this->assertSession()->responseContains() instead.
Chris@0 315 */
Chris@0 316 protected function assertRaw($raw) {
Chris@0 317 $this->assertSession()->responseContains($raw);
Chris@0 318 }
Chris@0 319
Chris@0 320 /**
Chris@0 321 * Passes if the raw text IS not found on the loaded page, fail otherwise.
Chris@0 322 *
Chris@0 323 * Raw text refers to the raw HTML that the page generated.
Chris@0 324 *
Chris@0 325 * @param string $raw
Chris@0 326 * Raw (HTML) string to look for.
Chris@0 327 *
Chris@18 328 * Deprecated Scheduled for removal in Drupal 10.0.0.
Chris@0 329 * Use $this->assertSession()->responseNotContains() instead.
Chris@0 330 */
Chris@0 331 protected function assertNoRaw($raw) {
Chris@0 332 $this->assertSession()->responseNotContains($raw);
Chris@0 333 }
Chris@0 334
Chris@0 335 /**
Chris@0 336 * Pass if the page title is the given string.
Chris@0 337 *
Chris@0 338 * @param string $expected_title
Chris@0 339 * The string the page title should be.
Chris@0 340 *
Chris@18 341 * Deprecated Scheduled for removal in Drupal 10.0.0.
Chris@0 342 * Use $this->assertSession()->titleEquals() instead.
Chris@0 343 */
Chris@0 344 protected function assertTitle($expected_title) {
Chris@0 345 // Cast MarkupInterface to string.
Chris@0 346 $expected_title = (string) $expected_title;
Chris@0 347 return $this->assertSession()->titleEquals($expected_title);
Chris@0 348 }
Chris@0 349
Chris@0 350 /**
Chris@0 351 * Passes if a link with the specified label is found.
Chris@0 352 *
Chris@0 353 * An optional link index may be passed.
Chris@0 354 *
Chris@0 355 * @param string|\Drupal\Component\Render\MarkupInterface $label
Chris@0 356 * Text between the anchor tags.
Chris@0 357 * @param int $index
Chris@0 358 * Link position counting from zero.
Chris@0 359 *
Chris@18 360 * Deprecated Scheduled for removal in Drupal 10.0.0.
Chris@0 361 * Use $this->assertSession()->linkExists() instead.
Chris@0 362 */
Chris@0 363 protected function assertLink($label, $index = 0) {
Chris@0 364 return $this->assertSession()->linkExists($label, $index);
Chris@0 365 }
Chris@0 366
Chris@0 367 /**
Chris@0 368 * Passes if a link with the specified label is not found.
Chris@0 369 *
Chris@0 370 * @param string|\Drupal\Component\Render\MarkupInterface $label
Chris@0 371 * Text between the anchor tags.
Chris@0 372 *
Chris@18 373 * Deprecated Scheduled for removal in Drupal 10.0.0.
Chris@0 374 * Use $this->assertSession()->linkNotExists() instead.
Chris@0 375 */
Chris@0 376 protected function assertNoLink($label) {
Chris@0 377 return $this->assertSession()->linkNotExists($label);
Chris@0 378 }
Chris@0 379
Chris@0 380 /**
Chris@0 381 * Passes if a link containing a given href (part) is found.
Chris@0 382 *
Chris@0 383 * @param string $href
Chris@0 384 * The full or partial value of the 'href' attribute of the anchor tag.
Chris@0 385 * @param int $index
Chris@0 386 * Link position counting from zero.
Chris@0 387 *
Chris@18 388 * Deprecated Scheduled for removal in Drupal 10.0.0.
Chris@0 389 * Use $this->assertSession()->linkByHrefExists() instead.
Chris@0 390 */
Chris@0 391 protected function assertLinkByHref($href, $index = 0) {
Chris@0 392 $this->assertSession()->linkByHrefExists($href, $index);
Chris@0 393 }
Chris@0 394
Chris@0 395 /**
Chris@0 396 * Passes if a link containing a given href (part) is not found.
Chris@0 397 *
Chris@0 398 * @param string $href
Chris@0 399 * The full or partial value of the 'href' attribute of the anchor tag.
Chris@0 400 *
Chris@18 401 * Deprecated Scheduled for removal in Drupal 10.0.0.
Chris@0 402 * Use $this->assertSession()->linkByHrefNotExists() instead.
Chris@0 403 */
Chris@0 404 protected function assertNoLinkByHref($href) {
Chris@0 405 $this->assertSession()->linkByHrefNotExists($href);
Chris@0 406 }
Chris@0 407
Chris@0 408 /**
Chris@0 409 * Asserts that a field does not exist with the given ID and value.
Chris@0 410 *
Chris@0 411 * @param string $id
Chris@0 412 * ID of field to assert.
Chris@0 413 * @param string $value
Chris@0 414 * (optional) Value for the field, to assert that the field's value on the
Chris@0 415 * page doesn't match it. You may pass in NULL to skip checking the value,
Chris@0 416 * while still checking that the field doesn't exist. However, the default
Chris@0 417 * value ('') asserts that the field value is not an empty string.
Chris@0 418 *
Chris@0 419 * @throws \Behat\Mink\Exception\ExpectationException
Chris@0 420 *
Chris@18 421 * Deprecated Scheduled for removal in Drupal 10.0.0.
Chris@0 422 * Use $this->assertSession()->fieldNotExists() or
Chris@0 423 * $this->assertSession()->buttonNotExists() or
Chris@0 424 * $this->assertSession()->fieldValueNotEquals() instead.
Chris@0 425 */
Chris@0 426 protected function assertNoFieldById($id, $value = '') {
Chris@0 427 $this->assertNoFieldByXPath($this->constructFieldXpath('id', $id), $value);
Chris@0 428 }
Chris@0 429
Chris@0 430 /**
Chris@0 431 * Passes if the internal browser's URL matches the given path.
Chris@0 432 *
Chris@0 433 * @param \Drupal\Core\Url|string $path
Chris@0 434 * The expected system path or URL.
Chris@0 435 *
Chris@18 436 * Deprecated Scheduled for removal in Drupal 10.0.0.
Chris@0 437 * Use $this->assertSession()->addressEquals() instead.
Chris@0 438 */
Chris@0 439 protected function assertUrl($path) {
Chris@0 440 $this->assertSession()->addressEquals($path);
Chris@0 441 }
Chris@0 442
Chris@0 443 /**
Chris@0 444 * Asserts that a select option in the current page exists.
Chris@0 445 *
Chris@0 446 * @param string $id
Chris@0 447 * ID of select field to assert.
Chris@0 448 * @param string $option
Chris@0 449 * Option to assert.
Chris@0 450 *
Chris@18 451 * Deprecated Scheduled for removal in Drupal 10.0.0.
Chris@0 452 * Use $this->assertSession()->optionExists() instead.
Chris@0 453 */
Chris@0 454 protected function assertOption($id, $option) {
Chris@0 455 return $this->assertSession()->optionExists($id, $option);
Chris@0 456 }
Chris@0 457
Chris@0 458 /**
Chris@0 459 * Asserts that a select option with the visible text exists.
Chris@0 460 *
Chris@0 461 * @param string $id
Chris@0 462 * The ID of the select field to assert.
Chris@0 463 * @param string $text
Chris@0 464 * The text for the option tag to assert.
Chris@0 465 *
Chris@18 466 * Deprecated Scheduled for removal in Drupal 10.0.0.
Chris@0 467 * Use $this->assertSession()->optionExists() instead.
Chris@0 468 */
Chris@0 469 protected function assertOptionByText($id, $text) {
Chris@0 470 return $this->assertSession()->optionExists($id, $text);
Chris@0 471 }
Chris@0 472
Chris@0 473 /**
Chris@0 474 * Asserts that a select option does NOT exist in the current page.
Chris@0 475 *
Chris@0 476 * @param string $id
Chris@0 477 * ID of select field to assert.
Chris@0 478 * @param string $option
Chris@0 479 * Option to assert.
Chris@0 480 *
Chris@18 481 * Deprecated Scheduled for removal in Drupal 10.0.0.
Chris@0 482 * Use $this->assertSession()->optionNotExists() instead.
Chris@0 483 */
Chris@0 484 protected function assertNoOption($id, $option) {
Chris@0 485 return $this->assertSession()->optionNotExists($id, $option);
Chris@0 486 }
Chris@0 487
Chris@0 488 /**
Chris@0 489 * Asserts that a select option in the current page is checked.
Chris@0 490 *
Chris@0 491 * @param string $id
Chris@0 492 * ID of select field to assert.
Chris@0 493 * @param string $option
Chris@0 494 * Option to assert.
Chris@0 495 * @param string $message
Chris@0 496 * (optional) A message to display with the assertion. Do not translate
Chris@0 497 * messages with t(). If left blank, a default message will be displayed.
Chris@0 498 *
Chris@18 499 * Deprecated Scheduled for removal in Drupal 10.0.0.
Chris@0 500 * Use $this->assertSession()->optionExists() instead and check the
Chris@0 501 * "selected" attribute yourself.
Chris@0 502 */
Chris@0 503 protected function assertOptionSelected($id, $option, $message = NULL) {
Chris@0 504 $option_field = $this->assertSession()->optionExists($id, $option);
Chris@0 505 $message = $message ?: "Option $option for field $id is selected.";
Chris@0 506 $this->assertTrue($option_field->hasAttribute('selected'), $message);
Chris@0 507 }
Chris@0 508
Chris@0 509 /**
Chris@0 510 * Asserts that a checkbox field in the current page is checked.
Chris@0 511 *
Chris@0 512 * @param string $id
Chris@0 513 * ID of field to assert.
Chris@0 514 *
Chris@18 515 * Deprecated Scheduled for removal in Drupal 10.0.0.
Chris@0 516 * Use $this->assertSession()->checkboxChecked() instead.
Chris@0 517 */
Chris@0 518 protected function assertFieldChecked($id) {
Chris@0 519 $this->assertSession()->checkboxChecked($id);
Chris@0 520 }
Chris@0 521
Chris@0 522 /**
Chris@0 523 * Asserts that a checkbox field in the current page is not checked.
Chris@0 524 *
Chris@0 525 * @param string $id
Chris@0 526 * ID of field to assert.
Chris@0 527 *
Chris@18 528 * Deprecated Scheduled for removal in Drupal 10.0.0.
Chris@0 529 * Use $this->assertSession()->checkboxNotChecked() instead.
Chris@0 530 */
Chris@0 531 protected function assertNoFieldChecked($id) {
Chris@0 532 $this->assertSession()->checkboxNotChecked($id);
Chris@0 533 }
Chris@0 534
Chris@0 535 /**
Chris@0 536 * Asserts that a field exists in the current page by the given XPath.
Chris@0 537 *
Chris@0 538 * @param string $xpath
Chris@0 539 * XPath used to find the field.
Chris@0 540 * @param string $value
Chris@0 541 * (optional) Value of the field to assert. You may pass in NULL (default)
Chris@0 542 * to skip checking the actual value, while still checking that the field
Chris@0 543 * exists.
Chris@0 544 * @param string $message
Chris@0 545 * (optional) A message to display with the assertion. Do not translate
Chris@0 546 * messages with t().
Chris@0 547 *
Chris@18 548 * Deprecated Scheduled for removal in Drupal 10.0.0.
Chris@0 549 * Use $this->xpath() instead and check the values directly in the test.
Chris@0 550 */
Chris@0 551 protected function assertFieldByXPath($xpath, $value = NULL, $message = '') {
Chris@0 552 $fields = $this->xpath($xpath);
Chris@0 553
Chris@0 554 $this->assertFieldsByValue($fields, $value, $message);
Chris@0 555 }
Chris@0 556
Chris@0 557 /**
Chris@0 558 * Asserts that a field does not exist or its value does not match, by XPath.
Chris@0 559 *
Chris@0 560 * @param string $xpath
Chris@0 561 * XPath used to find the field.
Chris@0 562 * @param string $value
Chris@0 563 * (optional) Value of the field, to assert that the field's value on the
Chris@0 564 * page does not match it.
Chris@0 565 * @param string $message
Chris@0 566 * (optional) A message to display with the assertion. Do not translate
Chris@0 567 * messages with t().
Chris@0 568 *
Chris@0 569 * @throws \Behat\Mink\Exception\ExpectationException
Chris@0 570 *
Chris@18 571 * Deprecated Scheduled for removal in Drupal 10.0.0.
Chris@0 572 * Use $this->xpath() instead and assert that the result is empty.
Chris@0 573 */
Chris@0 574 protected function assertNoFieldByXPath($xpath, $value = NULL, $message = '') {
Chris@0 575 $fields = $this->xpath($xpath);
Chris@0 576
Chris@0 577 if (!empty($fields)) {
Chris@0 578 if (isset($value)) {
Chris@0 579 $found = FALSE;
Chris@0 580 try {
Chris@0 581 $this->assertFieldsByValue($fields, $value);
Chris@0 582 $found = TRUE;
Chris@0 583 }
Chris@0 584 catch (\Exception $e) {
Chris@0 585 }
Chris@0 586
Chris@0 587 if ($found) {
Chris@0 588 throw new ExpectationException(sprintf('The field resulting from %s was found with the provided value %s.', $xpath, $value), $this->getSession()->getDriver());
Chris@0 589 }
Chris@0 590 }
Chris@0 591 else {
Chris@0 592 throw new ExpectationException(sprintf('The field resulting from %s was found.', $xpath), $this->getSession()->getDriver());
Chris@0 593 }
Chris@0 594 }
Chris@0 595 }
Chris@0 596
Chris@0 597 /**
Chris@0 598 * Asserts that a field exists in the current page with a given Xpath result.
Chris@0 599 *
Chris@0 600 * @param \Behat\Mink\Element\NodeElement[] $fields
Chris@0 601 * Xml elements.
Chris@0 602 * @param string $value
Chris@0 603 * (optional) Value of the field to assert. You may pass in NULL (default) to skip
Chris@0 604 * checking the actual value, while still checking that the field exists.
Chris@0 605 * @param string $message
Chris@0 606 * (optional) A message to display with the assertion. Do not translate
Chris@0 607 * messages with t().
Chris@0 608 *
Chris@18 609 * Deprecated Scheduled for removal in Drupal 10.0.0.
Chris@0 610 * Iterate over the fields yourself instead and directly check the values in
Chris@0 611 * the test.
Chris@0 612 */
Chris@0 613 protected function assertFieldsByValue($fields, $value = NULL, $message = '') {
Chris@0 614 // If value specified then check array for match.
Chris@0 615 $found = TRUE;
Chris@0 616 if (isset($value)) {
Chris@0 617 $found = FALSE;
Chris@0 618 if ($fields) {
Chris@0 619 foreach ($fields as $field) {
Chris@0 620 if ($field->getAttribute('type') == 'checkbox') {
Chris@0 621 if (is_bool($value)) {
Chris@0 622 $found = $field->isChecked() == $value;
Chris@0 623 }
Chris@0 624 else {
Chris@0 625 $found = TRUE;
Chris@0 626 }
Chris@0 627 }
Chris@0 628 elseif ($field->getAttribute('value') == $value) {
Chris@0 629 // Input element with correct value.
Chris@0 630 $found = TRUE;
Chris@0 631 }
Chris@0 632 elseif ($field->find('xpath', '//option[@value = ' . (new Escaper())->escapeLiteral($value) . ' and @selected = "selected"]')) {
Chris@0 633 // Select element with an option.
Chris@0 634 $found = TRUE;
Chris@0 635 }
Chris@0 636 elseif ($field->getTagName() === 'textarea' && $field->getValue() == $value) {
Chris@0 637 // Text area with correct text. Use getValue() here because
Chris@0 638 // getText() would remove any newlines in the value.
Chris@0 639 $found = TRUE;
Chris@0 640 }
Chris@0 641 elseif ($field->getTagName() !== 'input' && $field->getText() == $value) {
Chris@0 642 $found = TRUE;
Chris@0 643 }
Chris@0 644 }
Chris@0 645 }
Chris@0 646 }
Chris@0 647 $this->assertTrue($fields && $found, $message);
Chris@0 648 }
Chris@0 649
Chris@0 650 /**
Chris@0 651 * Passes if the raw text IS found escaped on the loaded page, fail otherwise.
Chris@0 652 *
Chris@0 653 * Raw text refers to the raw HTML that the page generated.
Chris@0 654 *
Chris@0 655 * @param string $raw
Chris@0 656 * Raw (HTML) string to look for.
Chris@0 657 *
Chris@18 658 * Deprecated Scheduled for removal in Drupal 10.0.0.
Chris@0 659 * Use $this->assertSession()->assertEscaped() instead.
Chris@0 660 */
Chris@0 661 protected function assertEscaped($raw) {
Chris@0 662 $this->assertSession()->assertEscaped($raw);
Chris@0 663 }
Chris@0 664
Chris@0 665 /**
Chris@0 666 * Passes if the raw text is not found escaped on the loaded page.
Chris@0 667 *
Chris@0 668 * Raw text refers to the raw HTML that the page generated.
Chris@0 669 *
Chris@0 670 * @param string $raw
Chris@0 671 * Raw (HTML) string to look for.
Chris@0 672 *
Chris@18 673 * Deprecated Scheduled for removal in Drupal 10.0.0.
Chris@0 674 * Use $this->assertSession()->assertNoEscaped() instead.
Chris@0 675 */
Chris@0 676 protected function assertNoEscaped($raw) {
Chris@0 677 $this->assertSession()->assertNoEscaped($raw);
Chris@0 678 }
Chris@0 679
Chris@0 680 /**
Chris@0 681 * Triggers a pass if the Perl regex pattern is found in the raw content.
Chris@0 682 *
Chris@0 683 * @param string $pattern
Chris@0 684 * Perl regex to look for including the regex delimiters.
Chris@0 685 *
Chris@18 686 * Deprecated Scheduled for removal in Drupal 10.0.0.
Chris@0 687 * Use $this->assertSession()->responseMatches() instead.
Chris@0 688 */
Chris@0 689 protected function assertPattern($pattern) {
Chris@0 690 $this->assertSession()->responseMatches($pattern);
Chris@0 691 }
Chris@0 692
Chris@0 693 /**
Chris@0 694 * Triggers a pass if the Perl regex pattern is not found in the raw content.
Chris@0 695 *
Chris@0 696 * @param string $pattern
Chris@0 697 * Perl regex to look for including the regex delimiters.
Chris@0 698 *
Chris@18 699 * Deprecated Scheduled for removal in Drupal 10.0.0.
Chris@0 700 * Use $this->assertSession()->responseNotMatches() instead.
Chris@0 701 *
Chris@0 702 * @see https://www.drupal.org/node/2864262
Chris@0 703 */
Chris@0 704 protected function assertNoPattern($pattern) {
Chris@0 705 @trigger_error('assertNoPattern() is deprecated and scheduled for removal in Drupal 9.0.0. Use $this->assertSession()->responseNotMatches($pattern) instead. See https://www.drupal.org/node/2864262.', E_USER_DEPRECATED);
Chris@0 706 $this->assertSession()->responseNotMatches($pattern);
Chris@0 707 }
Chris@0 708
Chris@0 709 /**
Chris@0 710 * Asserts whether an expected cache tag was present in the last response.
Chris@0 711 *
Chris@0 712 * @param string $expected_cache_tag
Chris@0 713 * The expected cache tag.
Chris@0 714 *
Chris@18 715 * Deprecated Scheduled for removal in Drupal 10.0.0.
Chris@0 716 * Use $this->assertSession()->responseHeaderContains() instead.
Chris@0 717 */
Chris@0 718 protected function assertCacheTag($expected_cache_tag) {
Chris@0 719 $this->assertSession()->responseHeaderContains('X-Drupal-Cache-Tags', $expected_cache_tag);
Chris@0 720 }
Chris@0 721
Chris@0 722 /**
Chris@0 723 * Asserts whether an expected cache tag was absent in the last response.
Chris@0 724 *
Chris@0 725 * @param string $cache_tag
Chris@0 726 * The cache tag to check.
Chris@0 727 *
Chris@18 728 * Deprecated Scheduled for removal in Drupal 10.0.0.
Chris@0 729 * Use $this->assertSession()->responseHeaderNotContains() instead.
Chris@0 730 *
Chris@0 731 * @see https://www.drupal.org/node/2864029
Chris@0 732 */
Chris@0 733 protected function assertNoCacheTag($cache_tag) {
Chris@0 734 @trigger_error('assertNoCacheTag() is deprecated and scheduled for removal in Drupal 9.0.0. Use $this->assertSession()->responseHeaderNotContains() instead. See https://www.drupal.org/node/2864029.', E_USER_DEPRECATED);
Chris@0 735 $this->assertSession()->responseHeaderNotContains('X-Drupal-Cache-Tags', $cache_tag);
Chris@0 736 }
Chris@0 737
Chris@0 738 /**
Chris@0 739 * Checks that current response header equals value.
Chris@0 740 *
Chris@0 741 * @param string $name
Chris@0 742 * Name of header to assert.
Chris@0 743 * @param string $value
Chris@0 744 * Value of the header to assert
Chris@0 745 *
Chris@18 746 * Deprecated Scheduled for removal in Drupal 10.0.0.
Chris@0 747 * Use $this->assertSession()->responseHeaderEquals() instead.
Chris@0 748 */
Chris@0 749 protected function assertHeader($name, $value) {
Chris@0 750 $this->assertSession()->responseHeaderEquals($name, $value);
Chris@0 751 }
Chris@0 752
Chris@0 753 /**
Chris@0 754 * Returns WebAssert object.
Chris@0 755 *
Chris@0 756 * @param string $name
Chris@0 757 * (optional) Name of the session. Defaults to the active session.
Chris@0 758 *
Chris@0 759 * @return \Drupal\Tests\WebAssert
Chris@0 760 * A new web-assert option for asserting the presence of elements with.
Chris@0 761 */
Chris@0 762 abstract public function assertSession($name = NULL);
Chris@0 763
Chris@0 764 /**
Chris@0 765 * Builds an XPath query.
Chris@0 766 *
Chris@0 767 * Builds an XPath query by replacing placeholders in the query by the value
Chris@0 768 * of the arguments.
Chris@0 769 *
Chris@0 770 * XPath 1.0 (the version supported by libxml2, the underlying XML library
Chris@0 771 * used by PHP) doesn't support any form of quotation. This function
Chris@0 772 * simplifies the building of XPath expression.
Chris@0 773 *
Chris@0 774 * @param string $xpath
Chris@0 775 * An XPath query, possibly with placeholders in the form ':name'.
Chris@0 776 * @param array $args
Chris@0 777 * An array of arguments with keys in the form ':name' matching the
Chris@0 778 * placeholders in the query. The values may be either strings or numeric
Chris@0 779 * values.
Chris@0 780 *
Chris@0 781 * @return string
Chris@0 782 * An XPath query with arguments replaced.
Chris@0 783 *
Chris@18 784 * Deprecated Scheduled for removal in Drupal 10.0.0.
Chris@0 785 * Use $this->assertSession()->buildXPathQuery() instead.
Chris@0 786 */
Chris@0 787 protected function buildXPathQuery($xpath, array $args = []) {
Chris@0 788 return $this->assertSession()->buildXPathQuery($xpath, $args);
Chris@0 789 }
Chris@0 790
Chris@0 791 /**
Chris@0 792 * Helper: Constructs an XPath for the given set of attributes and value.
Chris@0 793 *
Chris@0 794 * @param string $attribute
Chris@0 795 * Field attributes.
Chris@0 796 * @param string $value
Chris@0 797 * Value of field.
Chris@0 798 *
Chris@0 799 * @return string
Chris@0 800 * XPath for specified values.
Chris@0 801 *
Chris@18 802 * Deprecated Scheduled for removal in Drupal 10.0.0.
Chris@0 803 * Use $this->getSession()->getPage()->findField() instead.
Chris@0 804 */
Chris@0 805 protected function constructFieldXpath($attribute, $value) {
Chris@0 806 $xpath = '//textarea[@' . $attribute . '=:value]|//input[@' . $attribute . '=:value]|//select[@' . $attribute . '=:value]';
Chris@0 807 return $this->buildXPathQuery($xpath, [':value' => $value]);
Chris@0 808 }
Chris@0 809
Chris@0 810 /**
Chris@0 811 * Gets the current raw content.
Chris@0 812 *
Chris@18 813 * Deprecated Scheduled for removal in Drupal 10.0.0.
Chris@0 814 * Use $this->getSession()->getPage()->getContent() instead.
Chris@0 815 */
Chris@0 816 protected function getRawContent() {
Chris@0 817 @trigger_error('AssertLegacyTrait::getRawContent() is scheduled for removal in Drupal 9.0.0. Use $this->getSession()->getPage()->getContent() instead.', E_USER_DEPRECATED);
Chris@0 818 return $this->getSession()->getPage()->getContent();
Chris@0 819 }
Chris@0 820
Chris@0 821 /**
Chris@0 822 * Get all option elements, including nested options, in a select.
Chris@0 823 *
Chris@0 824 * @param \Behat\Mink\Element\NodeElement $element
Chris@0 825 * The element for which to get the options.
Chris@0 826 *
Chris@0 827 * @return \Behat\Mink\Element\NodeElement[]
Chris@0 828 * Option elements in select.
Chris@0 829 *
Chris@18 830 * Deprecated Scheduled for removal in Drupal 10.0.0.
Chris@0 831 * Use $element->findAll('xpath', 'option') instead.
Chris@0 832 */
Chris@0 833 protected function getAllOptions(NodeElement $element) {
Chris@0 834 @trigger_error('AssertLegacyTrait::getAllOptions() is scheduled for removal in Drupal 9.0.0. Use $element->findAll(\'xpath\', \'option\') instead.', E_USER_DEPRECATED);
Chris@0 835 return $element->findAll('xpath', '//option');
Chris@0 836 }
Chris@0 837
Chris@0 838 }