AssertLegacyTrait.php 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833
  1. <?php
  2. namespace Drupal\FunctionalTests;
  3. use Behat\Mink\Element\NodeElement;
  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()->buttonExists() or
  204. * $this->assertSession()->fieldValueEquals() instead.
  205. */
  206. protected function assertFieldByName($name, $value = NULL) {
  207. $this->assertFieldByXPath($this->constructFieldXpath('name', $name), $value);
  208. }
  209. /**
  210. * Asserts that a field does not exist with the given name and value.
  211. *
  212. * @param string $name
  213. * Name of field to assert.
  214. * @param string $value
  215. * (optional) Value for the field, to assert that the field's value on the
  216. * page does not match it. You may pass in NULL to skip checking the
  217. * value, while still checking that the field does not exist. However, the
  218. * default value ('') asserts that the field value is not an empty string.
  219. *
  220. * @deprecated Scheduled for removal in Drupal 9.0.0.
  221. * Use $this->assertSession()->fieldNotExists() or
  222. * $this->assertSession()->buttonNotExists() or
  223. * $this->assertSession()->fieldValueNotEquals() instead.
  224. */
  225. protected function assertNoFieldByName($name, $value = '') {
  226. $this->assertNoFieldByXPath($this->constructFieldXpath('name', $name), $value);
  227. }
  228. /**
  229. * Asserts that a field exists with the given ID and value.
  230. *
  231. * @param string $id
  232. * ID of field to assert.
  233. * @param string|\Drupal\Component\Render\MarkupInterface $value
  234. * (optional) Value for the field to assert. You may pass in NULL to skip
  235. * checking the value, while still checking that the field exists.
  236. * However, the default value ('') asserts that the field value is an empty
  237. * string.
  238. *
  239. * @throws \Behat\Mink\Exception\ElementNotFoundException
  240. *
  241. * @deprecated Scheduled for removal in Drupal 9.0.0.
  242. * Use $this->assertSession()->fieldExists() or
  243. * $this->assertSession()->buttonExists() or
  244. * $this->assertSession()->fieldValueEquals() instead.
  245. */
  246. protected function assertFieldById($id, $value = '') {
  247. $this->assertFieldByXPath($this->constructFieldXpath('id', $id), $value);
  248. }
  249. /**
  250. * Asserts that a field exists with the given name or ID.
  251. *
  252. * @param string $field
  253. * Name or ID of field to assert.
  254. *
  255. * @deprecated Scheduled for removal in Drupal 9.0.0.
  256. * Use $this->assertSession()->fieldExists() or
  257. * $this->assertSession()->buttonExists() instead.
  258. */
  259. protected function assertField($field) {
  260. $this->assertFieldByXPath($this->constructFieldXpath('name', $field) . '|' . $this->constructFieldXpath('id', $field));
  261. }
  262. /**
  263. * Asserts that a field does NOT exist with the given name or ID.
  264. *
  265. * @param string $field
  266. * Name or ID of field to assert.
  267. *
  268. * @deprecated Scheduled for removal in Drupal 9.0.0.
  269. * Use $this->assertSession()->fieldNotExists() or
  270. * $this->assertSession()->buttonNotExists() instead.
  271. */
  272. protected function assertNoField($field) {
  273. $this->assertNoFieldByXPath($this->constructFieldXpath('name', $field) . '|' . $this->constructFieldXpath('id', $field));
  274. }
  275. /**
  276. * Passes if the raw text IS found on the loaded page, fail otherwise.
  277. *
  278. * Raw text refers to the raw HTML that the page generated.
  279. *
  280. * @param string $raw
  281. * Raw (HTML) string to look for.
  282. *
  283. * @deprecated Scheduled for removal in Drupal 9.0.0.
  284. * Use $this->assertSession()->responseContains() instead.
  285. */
  286. protected function assertRaw($raw) {
  287. $this->assertSession()->responseContains($raw);
  288. }
  289. /**
  290. * Passes if the raw text IS not found on the loaded page, fail otherwise.
  291. *
  292. * Raw text refers to the raw HTML that the page generated.
  293. *
  294. * @param string $raw
  295. * Raw (HTML) string to look for.
  296. *
  297. * @deprecated Scheduled for removal in Drupal 9.0.0.
  298. * Use $this->assertSession()->responseNotContains() instead.
  299. */
  300. protected function assertNoRaw($raw) {
  301. $this->assertSession()->responseNotContains($raw);
  302. }
  303. /**
  304. * Pass if the page title is the given string.
  305. *
  306. * @param string $expected_title
  307. * The string the page title should be.
  308. *
  309. * @deprecated Scheduled for removal in Drupal 9.0.0.
  310. * Use $this->assertSession()->titleEquals() instead.
  311. */
  312. protected function assertTitle($expected_title) {
  313. // Cast MarkupInterface to string.
  314. $expected_title = (string) $expected_title;
  315. return $this->assertSession()->titleEquals($expected_title);
  316. }
  317. /**
  318. * Passes if a link with the specified label is found.
  319. *
  320. * An optional link index may be passed.
  321. *
  322. * @param string|\Drupal\Component\Render\MarkupInterface $label
  323. * Text between the anchor tags.
  324. * @param int $index
  325. * Link position counting from zero.
  326. *
  327. * @deprecated Scheduled for removal in Drupal 9.0.0.
  328. * Use $this->assertSession()->linkExists() instead.
  329. */
  330. protected function assertLink($label, $index = 0) {
  331. return $this->assertSession()->linkExists($label, $index);
  332. }
  333. /**
  334. * Passes if a link with the specified label is not found.
  335. *
  336. * @param string|\Drupal\Component\Render\MarkupInterface $label
  337. * Text between the anchor tags.
  338. *
  339. * @deprecated Scheduled for removal in Drupal 9.0.0.
  340. * Use $this->assertSession()->linkNotExists() instead.
  341. */
  342. protected function assertNoLink($label) {
  343. return $this->assertSession()->linkNotExists($label);
  344. }
  345. /**
  346. * Passes if a link containing a given href (part) is found.
  347. *
  348. * @param string $href
  349. * The full or partial value of the 'href' attribute of the anchor tag.
  350. * @param int $index
  351. * Link position counting from zero.
  352. *
  353. * @deprecated Scheduled for removal in Drupal 9.0.0.
  354. * Use $this->assertSession()->linkByHrefExists() instead.
  355. */
  356. protected function assertLinkByHref($href, $index = 0) {
  357. $this->assertSession()->linkByHrefExists($href, $index);
  358. }
  359. /**
  360. * Passes if a link containing a given href (part) is not found.
  361. *
  362. * @param string $href
  363. * The full or partial value of the 'href' attribute of the anchor tag.
  364. *
  365. * @deprecated Scheduled for removal in Drupal 9.0.0.
  366. * Use $this->assertSession()->linkByHrefNotExists() instead.
  367. */
  368. protected function assertNoLinkByHref($href) {
  369. $this->assertSession()->linkByHrefNotExists($href);
  370. }
  371. /**
  372. * Asserts that a field does not exist with the given ID and value.
  373. *
  374. * @param string $id
  375. * ID of field to assert.
  376. * @param string $value
  377. * (optional) Value for the field, to assert that the field's value on the
  378. * page doesn't match it. You may pass in NULL to skip checking the value,
  379. * while still checking that the field doesn't exist. However, the default
  380. * value ('') asserts that the field value is not an empty string.
  381. *
  382. * @throws \Behat\Mink\Exception\ExpectationException
  383. *
  384. * @deprecated Scheduled for removal in Drupal 9.0.0.
  385. * Use $this->assertSession()->fieldNotExists() or
  386. * $this->assertSession()->buttonNotExists() or
  387. * $this->assertSession()->fieldValueNotEquals() instead.
  388. */
  389. protected function assertNoFieldById($id, $value = '') {
  390. $this->assertNoFieldByXPath($this->constructFieldXpath('id', $id), $value);
  391. }
  392. /**
  393. * Passes if the internal browser's URL matches the given path.
  394. *
  395. * @param \Drupal\Core\Url|string $path
  396. * The expected system path or URL.
  397. *
  398. * @deprecated Scheduled for removal in Drupal 9.0.0.
  399. * Use $this->assertSession()->addressEquals() instead.
  400. */
  401. protected function assertUrl($path) {
  402. $this->assertSession()->addressEquals($path);
  403. }
  404. /**
  405. * Asserts that a select option in the current page exists.
  406. *
  407. * @param string $id
  408. * ID of select field to assert.
  409. * @param string $option
  410. * Option to assert.
  411. *
  412. * @deprecated Scheduled for removal in Drupal 9.0.0.
  413. * Use $this->assertSession()->optionExists() instead.
  414. */
  415. protected function assertOption($id, $option) {
  416. return $this->assertSession()->optionExists($id, $option);
  417. }
  418. /**
  419. * Asserts that a select option with the visible text exists.
  420. *
  421. * @param string $id
  422. * The ID of the select field to assert.
  423. * @param string $text
  424. * The text for the option tag to assert.
  425. *
  426. * @deprecated Scheduled for removal in Drupal 9.0.0.
  427. * Use $this->assertSession()->optionExists() instead.
  428. */
  429. protected function assertOptionByText($id, $text) {
  430. return $this->assertSession()->optionExists($id, $text);
  431. }
  432. /**
  433. * Asserts that a select option does NOT exist in the current page.
  434. *
  435. * @param string $id
  436. * ID of select field to assert.
  437. * @param string $option
  438. * Option to assert.
  439. *
  440. * @deprecated Scheduled for removal in Drupal 9.0.0.
  441. * Use $this->assertSession()->optionNotExists() instead.
  442. */
  443. protected function assertNoOption($id, $option) {
  444. return $this->assertSession()->optionNotExists($id, $option);
  445. }
  446. /**
  447. * Asserts that a select option in the current page is checked.
  448. *
  449. * @param string $id
  450. * ID of select field to assert.
  451. * @param string $option
  452. * Option to assert.
  453. * @param string $message
  454. * (optional) A message to display with the assertion. Do not translate
  455. * messages with t(). If left blank, a default message will be displayed.
  456. *
  457. * @deprecated Scheduled for removal in Drupal 9.0.0.
  458. * Use $this->assertSession()->optionExists() instead and check the
  459. * "selected" attribute yourself.
  460. */
  461. protected function assertOptionSelected($id, $option, $message = NULL) {
  462. $option_field = $this->assertSession()->optionExists($id, $option);
  463. $message = $message ?: "Option $option for field $id is selected.";
  464. $this->assertTrue($option_field->hasAttribute('selected'), $message);
  465. }
  466. /**
  467. * Asserts that a checkbox field in the current page is checked.
  468. *
  469. * @param string $id
  470. * ID of field to assert.
  471. *
  472. * @deprecated Scheduled for removal in Drupal 9.0.0.
  473. * Use $this->assertSession()->checkboxChecked() instead.
  474. */
  475. protected function assertFieldChecked($id) {
  476. $this->assertSession()->checkboxChecked($id);
  477. }
  478. /**
  479. * Asserts that a checkbox field in the current page is not checked.
  480. *
  481. * @param string $id
  482. * ID of field to assert.
  483. *
  484. * @deprecated Scheduled for removal in Drupal 9.0.0.
  485. * Use $this->assertSession()->checkboxNotChecked() instead.
  486. */
  487. protected function assertNoFieldChecked($id) {
  488. $this->assertSession()->checkboxNotChecked($id);
  489. }
  490. /**
  491. * Asserts that a field exists in the current page by the given XPath.
  492. *
  493. * @param string $xpath
  494. * XPath used to find the field.
  495. * @param string $value
  496. * (optional) Value of the field to assert. You may pass in NULL (default)
  497. * to skip checking the actual value, while still checking that the field
  498. * exists.
  499. * @param string $message
  500. * (optional) A message to display with the assertion. Do not translate
  501. * messages with t().
  502. *
  503. * @deprecated Scheduled for removal in Drupal 9.0.0.
  504. * Use $this->xpath() instead and check the values directly in the test.
  505. */
  506. protected function assertFieldByXPath($xpath, $value = NULL, $message = '') {
  507. $fields = $this->xpath($xpath);
  508. $this->assertFieldsByValue($fields, $value, $message);
  509. }
  510. /**
  511. * Asserts that a field does not exist or its value does not match, by 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 that the field's value on the
  517. * page does not match it.
  518. * @param string $message
  519. * (optional) A message to display with the assertion. Do not translate
  520. * messages with t().
  521. *
  522. * @throws \Behat\Mink\Exception\ExpectationException
  523. *
  524. * @deprecated Scheduled for removal in Drupal 9.0.0.
  525. * Use $this->xpath() instead and assert that the result is empty.
  526. */
  527. protected function assertNoFieldByXPath($xpath, $value = NULL, $message = '') {
  528. $fields = $this->xpath($xpath);
  529. if (!empty($fields)) {
  530. if (isset($value)) {
  531. $found = FALSE;
  532. try {
  533. $this->assertFieldsByValue($fields, $value);
  534. $found = TRUE;
  535. }
  536. catch (\Exception $e) {
  537. }
  538. if ($found) {
  539. throw new ExpectationException(sprintf('The field resulting from %s was found with the provided value %s.', $xpath, $value), $this->getSession()->getDriver());
  540. }
  541. }
  542. else {
  543. throw new ExpectationException(sprintf('The field resulting from %s was found.', $xpath), $this->getSession()->getDriver());
  544. }
  545. }
  546. }
  547. /**
  548. * Asserts that a field exists in the current page with a given Xpath result.
  549. *
  550. * @param \Behat\Mink\Element\NodeElement[] $fields
  551. * Xml elements.
  552. * @param string $value
  553. * (optional) Value of the field to assert. You may pass in NULL (default) to skip
  554. * checking the actual value, while still checking that the field exists.
  555. * @param string $message
  556. * (optional) A message to display with the assertion. Do not translate
  557. * messages with t().
  558. *
  559. * @deprecated Scheduled for removal in Drupal 9.0.0.
  560. * Iterate over the fields yourself instead and directly check the values in
  561. * the test.
  562. */
  563. protected function assertFieldsByValue($fields, $value = NULL, $message = '') {
  564. // If value specified then check array for match.
  565. $found = TRUE;
  566. if (isset($value)) {
  567. $found = FALSE;
  568. if ($fields) {
  569. foreach ($fields as $field) {
  570. if ($field->getAttribute('type') == 'checkbox') {
  571. if (is_bool($value)) {
  572. $found = $field->isChecked() == $value;
  573. }
  574. else {
  575. $found = TRUE;
  576. }
  577. }
  578. elseif ($field->getAttribute('value') == $value) {
  579. // Input element with correct value.
  580. $found = TRUE;
  581. }
  582. elseif ($field->find('xpath', '//option[@value = ' . (new Escaper())->escapeLiteral($value) . ' and @selected = "selected"]')) {
  583. // Select element with an option.
  584. $found = TRUE;
  585. }
  586. elseif ($field->getTagName() === 'textarea' && $field->getValue() == $value) {
  587. // Text area with correct text. Use getValue() here because
  588. // getText() would remove any newlines in the value.
  589. $found = TRUE;
  590. }
  591. elseif ($field->getTagName() !== 'input' && $field->getText() == $value) {
  592. $found = TRUE;
  593. }
  594. }
  595. }
  596. }
  597. $this->assertTrue($fields && $found, $message);
  598. }
  599. /**
  600. * Passes if the raw text IS found escaped on the loaded page, fail otherwise.
  601. *
  602. * Raw text refers to the raw HTML that the page generated.
  603. *
  604. * @param string $raw
  605. * Raw (HTML) string to look for.
  606. *
  607. * @deprecated Scheduled for removal in Drupal 9.0.0.
  608. * Use $this->assertSession()->assertEscaped() instead.
  609. */
  610. protected function assertEscaped($raw) {
  611. $this->assertSession()->assertEscaped($raw);
  612. }
  613. /**
  614. * Passes if the raw text is not found escaped on the loaded page.
  615. *
  616. * Raw text refers to the raw HTML that the page generated.
  617. *
  618. * @param string $raw
  619. * Raw (HTML) string to look for.
  620. *
  621. * @deprecated Scheduled for removal in Drupal 9.0.0.
  622. * Use $this->assertSession()->assertNoEscaped() instead.
  623. */
  624. protected function assertNoEscaped($raw) {
  625. $this->assertSession()->assertNoEscaped($raw);
  626. }
  627. /**
  628. * Triggers a pass if the Perl regex pattern is found in the raw content.
  629. *
  630. * @param string $pattern
  631. * Perl regex to look for including the regex delimiters.
  632. *
  633. * @deprecated Scheduled for removal in Drupal 9.0.0.
  634. * Use $this->assertSession()->responseMatches() instead.
  635. */
  636. protected function assertPattern($pattern) {
  637. $this->assertSession()->responseMatches($pattern);
  638. }
  639. /**
  640. * Triggers a pass if the Perl regex pattern is not found in the raw content.
  641. *
  642. * @param string $pattern
  643. * Perl regex to look for including the regex delimiters.
  644. *
  645. * @deprecated Scheduled for removal in Drupal 9.0.0.
  646. * Use $this->assertSession()->responseNotMatches() instead.
  647. *
  648. * @see https://www.drupal.org/node/2864262
  649. */
  650. protected function assertNoPattern($pattern) {
  651. @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);
  652. $this->assertSession()->responseNotMatches($pattern);
  653. }
  654. /**
  655. * Asserts whether an expected cache tag was present in the last response.
  656. *
  657. * @param string $expected_cache_tag
  658. * The expected cache tag.
  659. *
  660. * @deprecated Scheduled for removal in Drupal 9.0.0.
  661. * Use $this->assertSession()->responseHeaderContains() instead.
  662. */
  663. protected function assertCacheTag($expected_cache_tag) {
  664. $this->assertSession()->responseHeaderContains('X-Drupal-Cache-Tags', $expected_cache_tag);
  665. }
  666. /**
  667. * Asserts whether an expected cache tag was absent in the last response.
  668. *
  669. * @param string $cache_tag
  670. * The cache tag to check.
  671. *
  672. * @deprecated Scheduled for removal in Drupal 9.0.0.
  673. * Use $this->assertSession()->responseHeaderNotContains() instead.
  674. *
  675. * @see https://www.drupal.org/node/2864029
  676. */
  677. protected function assertNoCacheTag($cache_tag) {
  678. @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);
  679. $this->assertSession()->responseHeaderNotContains('X-Drupal-Cache-Tags', $cache_tag);
  680. }
  681. /**
  682. * Checks that current response header equals value.
  683. *
  684. * @param string $name
  685. * Name of header to assert.
  686. * @param string $value
  687. * Value of the header to assert
  688. *
  689. * @deprecated Scheduled for removal in Drupal 9.0.0.
  690. * Use $this->assertSession()->responseHeaderEquals() instead.
  691. */
  692. protected function assertHeader($name, $value) {
  693. $this->assertSession()->responseHeaderEquals($name, $value);
  694. }
  695. /**
  696. * Returns WebAssert object.
  697. *
  698. * @param string $name
  699. * (optional) Name of the session. Defaults to the active session.
  700. *
  701. * @return \Drupal\Tests\WebAssert
  702. * A new web-assert option for asserting the presence of elements with.
  703. */
  704. abstract public function assertSession($name = NULL);
  705. /**
  706. * Builds an XPath query.
  707. *
  708. * Builds an XPath query by replacing placeholders in the query by the value
  709. * of the arguments.
  710. *
  711. * XPath 1.0 (the version supported by libxml2, the underlying XML library
  712. * used by PHP) doesn't support any form of quotation. This function
  713. * simplifies the building of XPath expression.
  714. *
  715. * @param string $xpath
  716. * An XPath query, possibly with placeholders in the form ':name'.
  717. * @param array $args
  718. * An array of arguments with keys in the form ':name' matching the
  719. * placeholders in the query. The values may be either strings or numeric
  720. * values.
  721. *
  722. * @return string
  723. * An XPath query with arguments replaced.
  724. *
  725. * @deprecated Scheduled for removal in Drupal 9.0.0.
  726. * Use $this->assertSession()->buildXPathQuery() instead.
  727. */
  728. protected function buildXPathQuery($xpath, array $args = []) {
  729. return $this->assertSession()->buildXPathQuery($xpath, $args);
  730. }
  731. /**
  732. * Helper: Constructs an XPath for the given set of attributes and value.
  733. *
  734. * @param string $attribute
  735. * Field attributes.
  736. * @param string $value
  737. * Value of field.
  738. *
  739. * @return string
  740. * XPath for specified values.
  741. *
  742. * @deprecated Scheduled for removal in Drupal 9.0.0.
  743. * Use $this->getSession()->getPage()->findField() instead.
  744. */
  745. protected function constructFieldXpath($attribute, $value) {
  746. $xpath = '//textarea[@' . $attribute . '=:value]|//input[@' . $attribute . '=:value]|//select[@' . $attribute . '=:value]';
  747. return $this->buildXPathQuery($xpath, [':value' => $value]);
  748. }
  749. /**
  750. * Gets the current raw content.
  751. *
  752. * @deprecated Scheduled for removal in Drupal 9.0.0.
  753. * Use $this->getSession()->getPage()->getContent() instead.
  754. */
  755. protected function getRawContent() {
  756. @trigger_error('AssertLegacyTrait::getRawContent() is scheduled for removal in Drupal 9.0.0. Use $this->getSession()->getPage()->getContent() instead.', E_USER_DEPRECATED);
  757. return $this->getSession()->getPage()->getContent();
  758. }
  759. /**
  760. * Get all option elements, including nested options, in a select.
  761. *
  762. * @param \Behat\Mink\Element\NodeElement $element
  763. * The element for which to get the options.
  764. *
  765. * @return \Behat\Mink\Element\NodeElement[]
  766. * Option elements in select.
  767. *
  768. * @deprecated Scheduled for removal in Drupal 9.0.0.
  769. * Use $element->findAll('xpath', 'option') instead.
  770. */
  771. protected function getAllOptions(NodeElement $element) {
  772. @trigger_error('AssertLegacyTrait::getAllOptions() is scheduled for removal in Drupal 9.0.0. Use $element->findAll(\'xpath\', \'option\') instead.', E_USER_DEPRECATED);
  773. return $element->findAll('xpath', '//option');
  774. }
  775. }