AssertLegacyTrait.php 29 KB

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