AssertLegacyTrait.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560
  1. <?php
  2. namespace Drupal\FunctionalTests;
  3. use Drupal\KernelTests\AssertLegacyTrait as BaseAssertLegacyTrait;
  4. /**
  5. * Provides convenience methods for assertions in browser tests.
  6. *
  7. * @deprecated Scheduled for removal in Drupal 9.0.0. Use the methods on
  8. * \Drupal\Tests\WebAssert instead, for example
  9. * @code
  10. * $this->assertSession()->statusCodeEquals(200);
  11. * @endcode
  12. */
  13. trait AssertLegacyTrait {
  14. use BaseAssertLegacyTrait;
  15. /**
  16. * Asserts that the element with the given CSS selector is present.
  17. *
  18. * @param string $css_selector
  19. * The CSS selector identifying the element to check.
  20. *
  21. * @deprecated Scheduled for removal in Drupal 9.0.0.
  22. * Use $this->assertSession()->elementExists() instead.
  23. */
  24. protected function assertElementPresent($css_selector) {
  25. $this->assertSession()->elementExists('css', $css_selector);
  26. }
  27. /**
  28. * Asserts that the element with the given CSS selector is not present.
  29. *
  30. * @param string $css_selector
  31. * The CSS selector identifying the element to check.
  32. *
  33. * @deprecated Scheduled for removal in Drupal 9.0.0.
  34. * Use $this->assertSession()->elementNotExists() instead.
  35. */
  36. protected function assertElementNotPresent($css_selector) {
  37. $this->assertSession()->elementNotExists('css', $css_selector);
  38. }
  39. /**
  40. * Passes if the page (with HTML stripped) contains the text.
  41. *
  42. * Note that stripping HTML tags also removes their attributes, such as
  43. * the values of text fields.
  44. *
  45. * @param string $text
  46. * Plain text to look for.
  47. *
  48. * @deprecated Scheduled for removal in Drupal 9.0.0.
  49. * Use $this->assertSession()->pageTextContains() or
  50. * $this->assertSession()->responseContains() instead.
  51. */
  52. protected function assertText($text) {
  53. $content_type = $this->getSession()->getResponseHeader('Content-type');
  54. // In case of a Non-HTML response (example: XML) check the original
  55. // response.
  56. if (strpos($content_type, 'html') === FALSE) {
  57. $this->assertSession()->responseContains($text);
  58. }
  59. else {
  60. $this->assertSession()->pageTextContains($text);
  61. }
  62. }
  63. /**
  64. * Passes if the page (with HTML stripped) does not contains the text.
  65. *
  66. * Note that stripping HTML tags also removes their attributes, such as
  67. * the values of text fields.
  68. *
  69. * @param string $text
  70. * Plain text to look for.
  71. *
  72. * @deprecated Scheduled for removal in Drupal 9.0.0.
  73. * Use $this->assertSession()->pageTextNotContains() or
  74. * $this->assertSession()->responseNotContains() instead.
  75. */
  76. protected function assertNoText($text) {
  77. $content_type = $this->getSession()->getResponseHeader('Content-type');
  78. // In case of a Non-HTML response (example: XML) check the original
  79. // response.
  80. if (strpos($content_type, 'html') === FALSE) {
  81. $this->assertSession()->responseNotContains($text);
  82. }
  83. else {
  84. $this->assertSession()->pageTextNotContains($text);
  85. }
  86. }
  87. /**
  88. * Passes if the text is found ONLY ONCE on the text version of the page.
  89. *
  90. * The text version is the equivalent of what a user would see when viewing
  91. * through a web browser. In other words the HTML has been filtered out of
  92. * the contents.
  93. *
  94. * @param string|\Drupal\Component\Render\MarkupInterface $text
  95. * Plain text to look for.
  96. * @param string $message
  97. * (optional) A message to display with the assertion. Do not translate
  98. * messages with t(). If left blank, a default message will be displayed.
  99. *
  100. * @deprecated Scheduled for removal in Drupal 9.0.0.
  101. * Use $this->getSession()->getPage()->getText() and substr_count() instead.
  102. */
  103. protected function assertUniqueText($text, $message = NULL) {
  104. // Cast MarkupInterface objects to string.
  105. $text = (string) $text;
  106. $message = $message ?: "'$text' found only once on the page";
  107. $page_text = $this->getSession()->getPage()->getText();
  108. $nr_found = substr_count($page_text, $text);
  109. $this->assertSame(1, $nr_found, $message);
  110. }
  111. /**
  112. * Passes if the text is found MORE THAN ONCE on the text version of the page.
  113. *
  114. * The text version is the equivalent of what a user would see when viewing
  115. * through a web browser. In other words the HTML has been filtered out of
  116. * the contents.
  117. *
  118. * @param string|\Drupal\Component\Render\MarkupInterface $text
  119. * Plain text to look for.
  120. * @param string $message
  121. * (optional) A message to display with the assertion. Do not translate
  122. * messages with t(). If left blank, a default message will be displayed.
  123. *
  124. * @deprecated Scheduled for removal in Drupal 9.0.0.
  125. * Use $this->getSession()->getPage()->getText() and substr_count() instead.
  126. */
  127. protected function assertNoUniqueText($text, $message = '') {
  128. // Cast MarkupInterface objects to string.
  129. $text = (string) $text;
  130. $message = $message ?: "'$text' found more than once on the page";
  131. $page_text = $this->getSession()->getPage()->getText();
  132. $nr_found = substr_count($page_text, $text);
  133. $this->assertGreaterThan(1, $nr_found, $message);
  134. }
  135. /**
  136. * Asserts the page responds with the specified response code.
  137. *
  138. * @param int $code
  139. * Response code. For example 200 is a successful page request. For a list
  140. * of all codes see http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html.
  141. *
  142. * @deprecated Scheduled for removal in Drupal 9.0.0.
  143. * Use $this->assertSession()->statusCodeEquals() instead.
  144. */
  145. protected function assertResponse($code) {
  146. $this->assertSession()->statusCodeEquals($code);
  147. }
  148. /**
  149. * Asserts that a field exists with the given name and value.
  150. *
  151. * @param string $name
  152. * Name of field to assert.
  153. * @param string $value
  154. * (optional) Value of the field to assert. You may pass in NULL (default)
  155. * to skip checking the actual value, while still checking that the field
  156. * exists.
  157. *
  158. * @deprecated Scheduled for removal in Drupal 9.0.0.
  159. * Use $this->assertSession()->fieldExists() or
  160. * $this->assertSession()->fieldValueEquals() instead.
  161. */
  162. protected function assertFieldByName($name, $value = NULL) {
  163. $this->assertSession()->fieldExists($name);
  164. if ($value !== NULL) {
  165. $this->assertSession()->fieldValueEquals($name, (string) $value);
  166. }
  167. }
  168. /**
  169. * Asserts that a field exists with the given name and value.
  170. *
  171. * @param string $name
  172. * Name of field to assert.
  173. * @param string $value
  174. * (optional) Value of the field to assert. You may pass in NULL (default)
  175. * to skip checking the actual value, while still checking that the field
  176. * exists.
  177. *
  178. * @deprecated Scheduled for removal in Drupal 9.0.0.
  179. * Use $this->assertSession()->fieldNotExists() or
  180. * $this->assertSession()->fieldValueNotEquals() instead.
  181. */
  182. protected function assertNoFieldByName($name, $value = NULL) {
  183. $this->assertSession()->fieldNotExists($name);
  184. if ($value !== NULL) {
  185. $this->assertSession()->fieldValueNotEquals($name, (string) $value);
  186. }
  187. }
  188. /**
  189. * Asserts that a field exists with the given ID and value.
  190. *
  191. * @param string $id
  192. * ID of field to assert.
  193. * @param string|\Drupal\Component\Render\MarkupInterface $value
  194. * (optional) Value for the field to assert. You may pass in NULL to skip
  195. * checking the value, while still checking that the field exists.
  196. * However, the default value ('') asserts that the field value is an empty
  197. * string.
  198. *
  199. * @deprecated Scheduled for removal in Drupal 9.0.0.
  200. * Use $this->assertSession()->fieldExists() or
  201. * $this->assertSession()->fieldValueEquals() instead.
  202. */
  203. protected function assertFieldById($id, $value = NULL) {
  204. $this->assertFieldByName($id, $value);
  205. }
  206. /**
  207. * Asserts that a field exists with the given name or ID.
  208. *
  209. * @param string $field
  210. * Name or ID of field to assert.
  211. *
  212. * @deprecated Scheduled for removal in Drupal 9.0.0.
  213. * Use $this->assertSession()->fieldExists() instead.
  214. */
  215. protected function assertField($field) {
  216. $this->assertSession()->fieldExists($field);
  217. }
  218. /**
  219. * Asserts that a field exists with the given name or ID does NOT exist.
  220. *
  221. * @param string $field
  222. * Name or ID of field to assert.
  223. *
  224. * @deprecated Scheduled for removal in Drupal 9.0.0.
  225. * Use $this->assertSession()->fieldNotExists() instead.
  226. */
  227. protected function assertNoField($field) {
  228. $this->assertSession()->fieldNotExists($field);
  229. }
  230. /**
  231. * Passes if the raw text IS found on the loaded page, fail otherwise.
  232. *
  233. * Raw text refers to the raw HTML that the page generated.
  234. *
  235. * @param string $raw
  236. * Raw (HTML) string to look for.
  237. *
  238. * @deprecated Scheduled for removal in Drupal 9.0.0.
  239. * Use $this->assertSession()->responseContains() instead.
  240. */
  241. protected function assertRaw($raw) {
  242. $this->assertSession()->responseContains($raw);
  243. }
  244. /**
  245. * Passes if the raw text IS not found on the loaded page, fail otherwise.
  246. *
  247. * Raw text refers to the raw HTML that the page generated.
  248. *
  249. * @param string $raw
  250. * Raw (HTML) string to look for.
  251. *
  252. * @deprecated Scheduled for removal in Drupal 9.0.0.
  253. * Use $this->assertSession()->responseNotContains() instead.
  254. */
  255. protected function assertNoRaw($raw) {
  256. $this->assertSession()->responseNotContains($raw);
  257. }
  258. /**
  259. * Pass if the page title is the given string.
  260. *
  261. * @param string $expected_title
  262. * The string the page title should be.
  263. *
  264. * @deprecated Scheduled for removal in Drupal 9.0.0.
  265. * Use $this->assertSession()->titleEquals() instead.
  266. */
  267. protected function assertTitle($expected_title) {
  268. // Cast MarkupInterface to string.
  269. $expected_title = (string) $expected_title;
  270. return $this->assertSession()->titleEquals($expected_title);
  271. }
  272. /**
  273. * Passes if a link with the specified label is found.
  274. *
  275. * An optional link index may be passed.
  276. *
  277. * @param string|\Drupal\Component\Render\MarkupInterface $label
  278. * Text between the anchor tags.
  279. * @param int $index
  280. * Link position counting from zero.
  281. *
  282. * @deprecated Scheduled for removal in Drupal 9.0.0.
  283. * Use $this->assertSession()->linkExists() instead.
  284. */
  285. protected function assertLink($label, $index = 0) {
  286. return $this->assertSession()->linkExists($label, $index);
  287. }
  288. /**
  289. * Passes if a link with the specified label is not found.
  290. *
  291. * @param string|\Drupal\Component\Render\MarkupInterface $label
  292. * Text between the anchor tags.
  293. *
  294. * @deprecated Scheduled for removal in Drupal 9.0.0.
  295. * Use $this->assertSession()->linkNotExists() instead.
  296. */
  297. protected function assertNoLink($label) {
  298. return $this->assertSession()->linkNotExists($label);
  299. }
  300. /**
  301. * Passes if a link containing a given href (part) is found.
  302. *
  303. * @param string $href
  304. * The full or partial value of the 'href' attribute of the anchor tag.
  305. * @param int $index
  306. * Link position counting from zero.
  307. *
  308. * @deprecated Scheduled for removal in Drupal 9.0.0.
  309. * Use $this->assertSession()->linkByHref() instead.
  310. */
  311. protected function assertLinkByHref($href, $index = 0) {
  312. $this->assertSession()->linkByHrefExists($href, $index);
  313. }
  314. /**
  315. * Passes if a link containing a given href (part) is not found.
  316. *
  317. * @param string $href
  318. * The full or partial value of the 'href' attribute of the anchor tag.
  319. *
  320. * @deprecated Scheduled for removal in Drupal 9.0.0.
  321. * Use $this->assertSession()->linkByHrefNotExists() instead.
  322. */
  323. protected function assertNoLinkByHref($href) {
  324. $this->assertSession()->linkByHrefNotExists($href);
  325. }
  326. /**
  327. * Asserts that a field does not exist with the given ID and value.
  328. *
  329. * @param string $id
  330. * ID of field to assert.
  331. * @param string $value
  332. * (optional) Value for the field, to assert that the field's value on the
  333. * page doesn't match it. You may pass in NULL to skip checking the value,
  334. * while still checking that the field doesn't exist. However, the default
  335. * value ('') asserts that the field value is not an empty string.
  336. *
  337. * @deprecated Scheduled for removal in Drupal 9.0.0.
  338. * Use $this->assertSession()->fieldNotExists() or
  339. * $this->assertSession()->fieldValueNotEquals() instead.
  340. */
  341. protected function assertNoFieldById($id, $value = '') {
  342. if ($this->getSession()->getPage()->findField($id)) {
  343. $this->assertSession()->fieldValueNotEquals($id, (string) $value);
  344. }
  345. else {
  346. $this->assertSession()->fieldNotExists($id);
  347. }
  348. }
  349. /**
  350. * Passes if the internal browser's URL matches the given path.
  351. *
  352. * @param \Drupal\Core\Url|string $path
  353. * The expected system path or URL.
  354. *
  355. * @deprecated Scheduled for removal in Drupal 9.0.0.
  356. * Use $this->assertSession()->addressEquals() instead.
  357. */
  358. protected function assertUrl($path) {
  359. $this->assertSession()->addressEquals($path);
  360. }
  361. /**
  362. * Asserts that a select option in the current page exists.
  363. *
  364. * @param string $id
  365. * ID of select field to assert.
  366. * @param string $option
  367. * Option to assert.
  368. *
  369. * @deprecated Scheduled for removal in Drupal 9.0.0.
  370. * Use $this->assertSession()->optionExists() instead.
  371. */
  372. protected function assertOption($id, $option) {
  373. return $this->assertSession()->optionExists($id, $option);
  374. }
  375. /**
  376. * Asserts that a select option does NOT exist in the current page.
  377. *
  378. * @param string $id
  379. * ID of select field to assert.
  380. * @param string $option
  381. * Option to assert.
  382. *
  383. * @deprecated Scheduled for removal in Drupal 9.0.0.
  384. * Use $this->assertSession()->optionNotExists() instead.
  385. */
  386. protected function assertNoOption($id, $option) {
  387. return $this->assertSession()->optionNotExists($id, $option);
  388. }
  389. /**
  390. * Asserts that a select option in the current page is checked.
  391. *
  392. * @param string $id
  393. * ID of select field to assert.
  394. * @param string $option
  395. * Option to assert.
  396. * @param string $message
  397. * (optional) A message to display with the assertion. Do not translate
  398. * messages with t(). If left blank, a default message will be displayed.
  399. *
  400. * @deprecated Scheduled for removal in Drupal 9.0.0.
  401. * Use $this->assertSession()->optionExists() instead and check the
  402. * "selected" attribute yourself.
  403. */
  404. protected function assertOptionSelected($id, $option, $message = NULL) {
  405. $option_field = $this->assertSession()->optionExists($id, $option);
  406. $message = $message ?: "Option $option for field $id is selected.";
  407. $this->assertTrue($option_field->hasAttribute('selected'), $message);
  408. }
  409. /**
  410. * Asserts that a checkbox field in the current page is checked.
  411. *
  412. * @param string $id
  413. * ID of field to assert.
  414. *
  415. * @deprecated Scheduled for removal in Drupal 9.0.0.
  416. * Use $this->assertSession()->checkboxChecked() instead.
  417. */
  418. protected function assertFieldChecked($id) {
  419. $this->assertSession()->checkboxChecked($id);
  420. }
  421. /**
  422. * Asserts that a checkbox field in the current page is not checked.
  423. *
  424. * @param string $id
  425. * ID of field to assert.
  426. *
  427. * @deprecated Scheduled for removal in Drupal 9.0.0.
  428. * Use $this->assertSession()->checkboxNotChecked() instead.
  429. */
  430. protected function assertNoFieldChecked($id) {
  431. $this->assertSession()->checkboxNotChecked($id);
  432. }
  433. /**
  434. * Passes if the raw text IS found escaped on the loaded page, fail otherwise.
  435. *
  436. * Raw text refers to the raw HTML that the page generated.
  437. *
  438. * @param string $raw
  439. * Raw (HTML) string to look for.
  440. *
  441. * @deprecated Scheduled for removal in Drupal 9.0.0.
  442. * Use $this->assertSession()->assertEscaped() instead.
  443. */
  444. protected function assertEscaped($raw) {
  445. $this->assertSession()->assertEscaped($raw);
  446. }
  447. /**
  448. * Passes if the raw text is not found escaped on the loaded page.
  449. *
  450. * Raw text refers to the raw HTML that the page generated.
  451. *
  452. * @param string $raw
  453. * Raw (HTML) string to look for.
  454. *
  455. * @deprecated Scheduled for removal in Drupal 9.0.0.
  456. * Use $this->assertSession()->assertNoEscaped() instead.
  457. */
  458. protected function assertNoEscaped($raw) {
  459. $this->assertSession()->assertNoEscaped($raw);
  460. }
  461. /**
  462. * Triggers a pass if the Perl regex pattern is found in the raw content.
  463. *
  464. * @param string $pattern
  465. * Perl regex to look for including the regex delimiters.
  466. *
  467. * @deprecated Scheduled for removal in Drupal 9.0.0.
  468. * Use $this->assertSession()->responseMatches() instead.
  469. */
  470. protected function assertPattern($pattern) {
  471. $this->assertSession()->responseMatches($pattern);
  472. }
  473. /**
  474. * Asserts whether an expected cache tag was present in the last response.
  475. *
  476. * @param string $expected_cache_tag
  477. * The expected cache tag.
  478. *
  479. * @deprecated Scheduled for removal in Drupal 9.0.0.
  480. * Use $this->assertSession()->responseHeaderContains() instead.
  481. */
  482. protected function assertCacheTag($expected_cache_tag) {
  483. $this->assertSession()->responseHeaderContains('X-Drupal-Cache-Tags', $expected_cache_tag);
  484. }
  485. /**
  486. * Returns WebAssert object.
  487. *
  488. * @param string $name
  489. * (optional) Name of the session. Defaults to the active session.
  490. *
  491. * @return \Drupal\Tests\WebAssert
  492. * A new web-assert option for asserting the presence of elements with.
  493. */
  494. abstract public function assertSession($name = NULL);
  495. /**
  496. * Builds an XPath query.
  497. *
  498. * Builds an XPath query by replacing placeholders in the query by the value
  499. * of the arguments.
  500. *
  501. * XPath 1.0 (the version supported by libxml2, the underlying XML library
  502. * used by PHP) doesn't support any form of quotation. This function
  503. * simplifies the building of XPath expression.
  504. *
  505. * @param string $xpath
  506. * An XPath query, possibly with placeholders in the form ':name'.
  507. * @param array $args
  508. * An array of arguments with keys in the form ':name' matching the
  509. * placeholders in the query. The values may be either strings or numeric
  510. * values.
  511. *
  512. * @return string
  513. * An XPath query with arguments replaced.
  514. *
  515. * @deprecated Scheduled for removal in Drupal 9.0.0.
  516. * Use $this->assertSession()->buildXPathQuery() instead.
  517. */
  518. protected function buildXPathQuery($xpath, array $args = array()) {
  519. return $this->assertSession()->buildXPathQuery($xpath, $args);
  520. }
  521. }