annotate core/tests/Drupal/FunctionalTests/AssertLegacyTrait.php @ 0:c75dbcec494b

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