AssertLegacyTrait.php 26 KB

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