BrowserTestBaseTest.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675
  1. <?php
  2. namespace Drupal\FunctionalTests;
  3. use Behat\Mink\Exception\ExpectationException;
  4. use Drupal\Component\Serialization\Json;
  5. use Drupal\Component\Utility\Html;
  6. use Drupal\Core\Url;
  7. use Drupal\Tests\BrowserTestBase;
  8. use Drupal\Tests\Traits\Core\CronRunTrait;
  9. /**
  10. * Tests BrowserTestBase functionality.
  11. *
  12. * @group browsertestbase
  13. */
  14. class BrowserTestBaseTest extends BrowserTestBase {
  15. use CronRunTrait;
  16. /**
  17. * Modules to enable.
  18. *
  19. * @var array
  20. */
  21. public static $modules = ['test_page_test', 'form_test', 'system_test', 'node'];
  22. /**
  23. * Tests basic page test.
  24. */
  25. public function testGoTo() {
  26. $account = $this->drupalCreateUser();
  27. $this->drupalLogin($account);
  28. // Visit a Drupal page that requires login.
  29. $this->drupalGet('test-page');
  30. $this->assertSession()->statusCodeEquals(200);
  31. // Test page contains some text.
  32. $this->assertSession()->pageTextContains('Test page text.');
  33. // Check that returned plain text is correct.
  34. $text = $this->getTextContent();
  35. $this->assertContains('Test page text.', $text);
  36. $this->assertNotContains('</html>', $text);
  37. // Response includes cache tags that we can assert.
  38. $this->assertSession()->responseHeaderEquals('X-Drupal-Cache-Tags', 'http_response rendered');
  39. // Test that we can read the JS settings.
  40. $js_settings = $this->getDrupalSettings();
  41. $this->assertSame('azAZ09();.,\\\/-_{}', $js_settings['test-setting']);
  42. // Test drupalGet with a url object.
  43. $url = Url::fromRoute('test_page_test.render_title');
  44. $this->drupalGet($url);
  45. $this->assertSession()->statusCodeEquals(200);
  46. // Test page contains some text.
  47. $this->assertSession()->pageTextContains('Hello Drupal');
  48. // Test that setting headers with drupalGet() works.
  49. $this->drupalGet('system-test/header', [], [
  50. 'Test-Header' => 'header value',
  51. ]);
  52. $returned_header = $this->getSession()->getResponseHeader('Test-Header');
  53. $this->assertSame('header value', $returned_header);
  54. }
  55. /**
  56. * Tests drupalGet().
  57. */
  58. public function testDrupalGet() {
  59. $this->drupalGet('test-page');
  60. $this->assertSession()->statusCodeEquals(200);
  61. $this->assertSession()->addressEquals('test-page');
  62. $this->drupalGet('/test-page');
  63. $this->assertSession()->statusCodeEquals(200);
  64. $this->assertSession()->addressEquals('test-page');
  65. $this->drupalGet('/test-page/');
  66. $this->assertSession()->statusCodeEquals(200);
  67. $this->assertSession()->addressEquals('/test-page/');
  68. }
  69. /**
  70. * Tests basic form functionality.
  71. */
  72. public function testForm() {
  73. // Ensure the proper response code for a _form route.
  74. $this->drupalGet('form-test/object-builder');
  75. $this->assertSession()->statusCodeEquals(200);
  76. // Ensure the form and text field exist.
  77. $this->assertSession()->elementExists('css', 'form#form-test-form-test-object');
  78. $this->assertSession()->fieldExists('bananas');
  79. // Check that the hidden field exists and has a specific value.
  80. $this->assertSession()->hiddenFieldExists('strawberry');
  81. $this->assertSession()->hiddenFieldExists('red');
  82. $this->assertSession()->hiddenFieldExists('redstrawberryhiddenfield');
  83. $this->assertSession()->hiddenFieldValueNotEquals('strawberry', 'brown');
  84. $this->assertSession()->hiddenFieldValueEquals('strawberry', 'red');
  85. // Check that a hidden field does not exist.
  86. $this->assertSession()->hiddenFieldNotExists('bananas');
  87. $this->assertSession()->hiddenFieldNotExists('pineapple');
  88. $edit = ['bananas' => 'green'];
  89. $this->submitForm($edit, 'Save', 'form-test-form-test-object');
  90. $config_factory = $this->container->get('config.factory');
  91. $value = $config_factory->get('form_test.object')->get('bananas');
  92. $this->assertSame('green', $value);
  93. // Test drupalPostForm().
  94. $edit = ['bananas' => 'red'];
  95. $result = $this->drupalPostForm('form-test/object-builder', $edit, 'Save');
  96. $this->assertSame($this->getSession()->getPage()->getContent(), $result);
  97. $value = $config_factory->get('form_test.object')->get('bananas');
  98. $this->assertSame('red', $value);
  99. $this->drupalPostForm('form-test/object-builder', NULL, 'Save');
  100. $value = $config_factory->get('form_test.object')->get('bananas');
  101. $this->assertSame('', $value);
  102. // Test drupalPostForm() with no-html response.
  103. $values = Json::decode($this->drupalPostForm('form_test/form-state-values-clean', [], t('Submit')));
  104. $this->assertTrue(1000, $values['beer']);
  105. // Test drupalPostForm() with form by HTML id.
  106. $this->drupalCreateContentType(['type' => 'page']);
  107. $this->drupalLogin($this->drupalCreateUser(['create page content']));
  108. $this->drupalGet('form-test/two-instances-of-same-form');
  109. $this->getSession()->getPage()->fillField('edit-title-0-value', 'form1');
  110. $this->getSession()->getPage()->fillField('edit-title-0-value--2', 'form2');
  111. $this->drupalPostForm(NULL, [], 'Save', [], 'node-page-form--2');
  112. $this->assertSession()->pageTextContains('Page form2 has been created.');
  113. }
  114. /**
  115. * Tests clickLink() functionality.
  116. */
  117. public function testClickLink() {
  118. $this->drupalGet('test-page');
  119. $this->clickLink('Visually identical test links');
  120. $this->assertContains('user/login', $this->getSession()->getCurrentUrl());
  121. $this->drupalGet('test-page');
  122. $this->clickLink('Visually identical test links', 0);
  123. $this->assertContains('user/login', $this->getSession()->getCurrentUrl());
  124. $this->drupalGet('test-page');
  125. $this->clickLink('Visually identical test links', 1);
  126. $this->assertContains('user/register', $this->getSession()->getCurrentUrl());
  127. }
  128. public function testError() {
  129. $this->setExpectedException('\Exception', 'User notice: foo');
  130. $this->drupalGet('test-error');
  131. }
  132. /**
  133. * Tests linkExists() with pipe character (|) in locator.
  134. *
  135. * @see \Drupal\Tests\WebAssert::linkExists()
  136. */
  137. public function testPipeCharInLocator() {
  138. $this->drupalGet('test-pipe-char');
  139. $this->assertSession()->linkExists('foo|bar|baz');
  140. }
  141. /**
  142. * Tests linkExistsExact() functionality.
  143. *
  144. * @see \Drupal\Tests\WebAssert::linkExistsExact()
  145. */
  146. public function testLinkExistsExact() {
  147. $this->drupalGet('test-pipe-char');
  148. $this->assertSession()->linkExistsExact('foo|bar|baz');
  149. }
  150. /**
  151. * Tests linkExistsExact() functionality fail.
  152. *
  153. * @see \Drupal\Tests\WebAssert::linkExistsExact()
  154. */
  155. public function testInvalidLinkExistsExact() {
  156. $this->drupalGet('test-pipe-char');
  157. $this->setExpectedException(ExpectationException::class, 'Link with label foo|bar found');
  158. $this->assertSession()->linkExistsExact('foo|bar');
  159. }
  160. /**
  161. * Tests linkNotExistsExact() functionality.
  162. *
  163. * @see \Drupal\Tests\WebAssert::linkNotExistsExact()
  164. */
  165. public function testLinkNotExistsExact() {
  166. $this->drupalGet('test-pipe-char');
  167. $this->assertSession()->linkNotExistsExact('foo|bar');
  168. }
  169. /**
  170. * Tests linkNotExistsExact() functionality fail.
  171. *
  172. * @see \Drupal\Tests\WebAssert::linkNotExistsExact()
  173. */
  174. public function testInvalidLinkNotExistsExact() {
  175. $this->drupalGet('test-pipe-char');
  176. $this->setExpectedException(ExpectationException::class, 'Link with label foo|bar|baz not found');
  177. $this->assertSession()->linkNotExistsExact('foo|bar|baz');
  178. }
  179. /**
  180. * Tests legacy text asserts.
  181. */
  182. public function testTextAsserts() {
  183. $this->drupalGet('test-encoded');
  184. $dangerous = 'Bad html <script>alert(123);</script>';
  185. $sanitized = Html::escape($dangerous);
  186. $this->assertNoText($dangerous);
  187. $this->assertText($sanitized);
  188. // Test getRawContent().
  189. $this->assertSame($this->getSession()->getPage()->getContent(), $this->getSession()->getPage()->getContent());
  190. }
  191. /**
  192. * Tests legacy field asserts which use xpath directly.
  193. */
  194. public function testXpathAsserts() {
  195. $this->drupalGet('test-field-xpath');
  196. $this->assertFieldsByValue($this->xpath("//h1[@class = 'page-title']"), NULL);
  197. $this->assertFieldsByValue($this->xpath('//table/tbody/tr[2]/td[1]'), 'one');
  198. $this->assertFieldByXPath('//table/tbody/tr[2]/td[1]', 'one');
  199. $this->assertFieldsByValue($this->xpath("//input[@id = 'edit-name']"), 'Test name');
  200. $this->assertFieldByXPath("//input[@id = 'edit-name']", 'Test name');
  201. $this->assertFieldsByValue($this->xpath("//select[@id = 'edit-options']"), '2');
  202. $this->assertFieldByXPath("//select[@id = 'edit-options']", '2');
  203. $this->assertNoFieldByXPath('//notexisting');
  204. $this->assertNoFieldByXPath("//input[@id = 'edit-name']", 'wrong value');
  205. // Test that the assertion fails correctly.
  206. try {
  207. $this->assertFieldByXPath("//input[@id = 'notexisting']");
  208. $this->fail('The "notexisting" field was found.');
  209. }
  210. catch (\PHPUnit_Framework_ExpectationFailedException $e) {
  211. $this->pass('assertFieldByXPath correctly failed. The "notexisting" field was not found.');
  212. }
  213. try {
  214. $this->assertNoFieldByXPath("//input[@id = 'edit-name']");
  215. $this->fail('The "edit-name" field was not found.');
  216. }
  217. catch (ExpectationException $e) {
  218. $this->pass('assertNoFieldByXPath correctly failed. The "edit-name" field was found.');
  219. }
  220. try {
  221. $this->assertFieldsByValue($this->xpath("//input[@id = 'edit-name']"), 'not the value');
  222. $this->fail('The "edit-name" field is found with the value "not the value".');
  223. }
  224. catch (\PHPUnit_Framework_ExpectationFailedException $e) {
  225. $this->pass('The "edit-name" field is not found with the value "not the value".');
  226. }
  227. }
  228. /**
  229. * Tests legacy field asserts using textfields.
  230. */
  231. public function testFieldAssertsForTextfields() {
  232. $this->drupalGet('test-field-xpath');
  233. // *** 1. assertNoField().
  234. $this->assertNoField('invalid_name_and_id');
  235. // Test that the assertion fails correctly when searching by name.
  236. try {
  237. $this->assertNoField('name');
  238. $this->fail('The "name" field was not found based on name.');
  239. }
  240. catch (ExpectationException $e) {
  241. $this->pass('assertNoField correctly failed. The "name" field was found by name.');
  242. }
  243. // Test that the assertion fails correctly when searching by id.
  244. try {
  245. $this->assertNoField('edit-name');
  246. $this->fail('The "name" field was not found based on id.');
  247. }
  248. catch (ExpectationException $e) {
  249. $this->pass('assertNoField correctly failed. The "name" field was found by id.');
  250. }
  251. // *** 2. assertField().
  252. $this->assertField('name');
  253. $this->assertField('edit-name');
  254. // Test that the assertion fails correctly if the field does not exist.
  255. try {
  256. $this->assertField('invalid_name_and_id');
  257. $this->fail('The "invalid_name_and_id" field was found.');
  258. }
  259. catch (\PHPUnit_Framework_ExpectationFailedException $e) {
  260. $this->pass('assertField correctly failed. The "invalid_name_and_id" field was not found.');
  261. }
  262. // *** 3. assertNoFieldById().
  263. $this->assertNoFieldById('name');
  264. $this->assertNoFieldById('name', 'not the value');
  265. $this->assertNoFieldById('notexisting');
  266. $this->assertNoFieldById('notexisting', NULL);
  267. // Test that the assertion fails correctly if no value is passed in.
  268. try {
  269. $this->assertNoFieldById('edit-description');
  270. $this->fail('The "description" field, with no value was not found.');
  271. }
  272. catch (ExpectationException $e) {
  273. $this->pass('The "description" field, with no value was found.');
  274. }
  275. // Test that the assertion fails correctly if a NULL value is passed in.
  276. try {
  277. $this->assertNoFieldById('edit-name', NULL);
  278. $this->fail('The "name" field was not found.');
  279. }
  280. catch (ExpectationException $e) {
  281. $this->pass('The "name" field was found.');
  282. }
  283. // *** 4. assertFieldById().
  284. $this->assertFieldById('edit-name', NULL);
  285. $this->assertFieldById('edit-name', 'Test name');
  286. $this->assertFieldById('edit-description', NULL);
  287. $this->assertFieldById('edit-description');
  288. // Test that the assertion fails correctly if no value is passed in.
  289. try {
  290. $this->assertFieldById('edit-name');
  291. $this->fail('The "edit-name" field with no value was found.');
  292. }
  293. catch (\PHPUnit_Framework_ExpectationFailedException $e) {
  294. $this->pass('The "edit-name" field with no value was not found.');
  295. }
  296. // Test that the assertion fails correctly if the wrong value is passed in.
  297. try {
  298. $this->assertFieldById('edit-name', 'not the value');
  299. $this->fail('The "name" field was found, using the wrong value.');
  300. }
  301. catch (\PHPUnit_Framework_ExpectationFailedException $e) {
  302. $this->pass('The "name" field was not found, using the wrong value.');
  303. }
  304. // *** 5. assertNoFieldByName().
  305. $this->assertNoFieldByName('name');
  306. $this->assertNoFieldByName('name', 'not the value');
  307. $this->assertNoFieldByName('notexisting');
  308. $this->assertNoFieldByName('notexisting', NULL);
  309. // Test that the assertion fails correctly if no value is passed in.
  310. try {
  311. $this->assertNoFieldByName('description');
  312. $this->fail('The "description" field, with no value was not found.');
  313. }
  314. catch (ExpectationException $e) {
  315. $this->pass('The "description" field, with no value was found.');
  316. }
  317. // Test that the assertion fails correctly if a NULL value is passed in.
  318. try {
  319. $this->assertNoFieldByName('name', NULL);
  320. $this->fail('The "name" field was not found.');
  321. }
  322. catch (ExpectationException $e) {
  323. $this->pass('The "name" field was found.');
  324. }
  325. // *** 6. assertFieldByName().
  326. $this->assertFieldByName('name');
  327. $this->assertFieldByName('name', NULL);
  328. $this->assertFieldByName('name', 'Test name');
  329. $this->assertFieldByName('description');
  330. $this->assertFieldByName('description', '');
  331. $this->assertFieldByName('description', NULL);
  332. // Test that the assertion fails correctly if given the wrong name.
  333. try {
  334. $this->assertFieldByName('non-existing-name');
  335. $this->fail('The "non-existing-name" field was found.');
  336. }
  337. catch (\PHPUnit_Framework_ExpectationFailedException $e) {
  338. $this->pass('The "non-existing-name" field was not found');
  339. }
  340. // Test that the assertion fails correctly if given the wrong value.
  341. try {
  342. $this->assertFieldByName('name', 'not the value');
  343. $this->fail('The "name" field with incorrect value was found.');
  344. }
  345. catch (\PHPUnit_Framework_ExpectationFailedException $e) {
  346. $this->pass('assertFieldByName correctly failed. The "name" field with incorrect value was not found.');
  347. }
  348. // Test that text areas can contain new lines.
  349. $this->assertFieldsByValue($this->xpath("//textarea[@id = 'edit-test-textarea-with-newline']"), "Test text with\nnewline");
  350. }
  351. /**
  352. * Tests legacy field asserts for options field type.
  353. */
  354. public function testFieldAssertsForOptions() {
  355. $this->drupalGet('test-field-xpath');
  356. // Option field type.
  357. $this->assertOptionByText('options', 'one');
  358. try {
  359. $this->assertOptionByText('options', 'four');
  360. $this->fail('The select option "four" was found.');
  361. }
  362. catch (ExpectationException $e) {
  363. $this->pass($e->getMessage());
  364. }
  365. $this->assertOption('options', 1);
  366. try {
  367. $this->assertOption('options', 4);
  368. $this->fail('The select option "4" was found.');
  369. }
  370. catch (ExpectationException $e) {
  371. $this->pass($e->getMessage());
  372. }
  373. $this->assertNoOption('options', 'non-existing');
  374. try {
  375. $this->assertNoOption('options', 'one');
  376. $this->fail('The select option "one" was not found.');
  377. }
  378. catch (ExpectationException $e) {
  379. $this->pass($e->getMessage());
  380. }
  381. $this->assertOptionSelected('options', 2);
  382. try {
  383. $this->assertOptionSelected('options', 4);
  384. $this->fail('The select option "4" was selected.');
  385. }
  386. catch (ExpectationException $e) {
  387. $this->pass($e->getMessage());
  388. }
  389. try {
  390. $this->assertOptionSelected('options', 1);
  391. $this->fail('The select option "1" was selected.');
  392. }
  393. catch (\PHPUnit_Framework_ExpectationFailedException $e) {
  394. $this->pass($e->getMessage());
  395. }
  396. // Test \Drupal\FunctionalTests\AssertLegacyTrait::getAllOptions.
  397. $this->drupalGet('/form-test/select');
  398. $this->assertCount(6, $this->getAllOptions($this->cssSelect('select[name="opt_groups"]')[0]));
  399. }
  400. /**
  401. * Tests legacy field asserts for button field type.
  402. */
  403. public function testFieldAssertsForButton() {
  404. $this->drupalGet('test-field-xpath');
  405. $this->assertFieldById('edit-save', NULL);
  406. // Test that the assertion fails correctly if the field value is passed in
  407. // rather than the id.
  408. try {
  409. $this->assertFieldById('Save', NULL);
  410. $this->fail('The field with id of "Save" was found.');
  411. }
  412. catch (\PHPUnit_Framework_ExpectationFailedException $e) {
  413. $this->pass($e->getMessage());
  414. }
  415. $this->assertNoFieldById('Save', NULL);
  416. // Test that the assertion fails correctly if the id of an actual field is
  417. // passed in.
  418. try {
  419. $this->assertNoFieldById('edit-save', NULL);
  420. $this->fail('The field with id of "edit-save" was not found.');
  421. }
  422. catch (ExpectationException $e) {
  423. $this->pass($e->getMessage());
  424. }
  425. // Test that multiple fields with the same name are validated correctly.
  426. $this->assertFieldByName('duplicate_button', 'Duplicate button 1');
  427. $this->assertFieldByName('duplicate_button', 'Duplicate button 2');
  428. $this->assertNoFieldByName('duplicate_button', 'Rabbit');
  429. try {
  430. $this->assertNoFieldByName('duplicate_button', 'Duplicate button 2');
  431. $this->fail('The "duplicate_button" field with the value Duplicate button 2 was not found.');
  432. }
  433. catch (ExpectationException $e) {
  434. $this->pass('assertNoFieldByName correctly failed. The "duplicate_button" field with the value Duplicate button 2 was found.');
  435. }
  436. }
  437. /**
  438. * Tests legacy field asserts for checkbox field type.
  439. */
  440. public function testFieldAssertsForCheckbox() {
  441. $this->drupalGet('test-field-xpath');
  442. // Part 1 - Test by name.
  443. // Test that checkboxes are found/not found correctly by name, when using
  444. // TRUE or FALSE to match their 'checked' state.
  445. $this->assertFieldByName('checkbox_enabled', TRUE);
  446. $this->assertFieldByName('checkbox_disabled', FALSE);
  447. $this->assertNoFieldByName('checkbox_enabled', FALSE);
  448. $this->assertNoFieldByName('checkbox_disabled', TRUE);
  449. // Test that checkboxes are found by name when using NULL to ignore the
  450. // 'checked' state.
  451. $this->assertFieldByName('checkbox_enabled', NULL);
  452. $this->assertFieldByName('checkbox_disabled', NULL);
  453. // Test that checkboxes are found by name when passing no second parameter.
  454. $this->assertFieldByName('checkbox_enabled');
  455. $this->assertFieldByName('checkbox_disabled');
  456. // Test that we have legacy support.
  457. $this->assertFieldByName('checkbox_enabled', '1');
  458. $this->assertFieldByName('checkbox_disabled', '');
  459. // Test that the assertion fails correctly when using NULL to ignore state.
  460. try {
  461. $this->assertNoFieldByName('checkbox_enabled', NULL);
  462. $this->fail('The "checkbox_enabled" field was not found by name, using NULL value.');
  463. }
  464. catch (ExpectationException $e) {
  465. $this->pass('assertNoFieldByName failed correctly. The "checkbox_enabled" field was found using NULL value.');
  466. }
  467. // Part 2 - Test by ID.
  468. // Test that checkboxes are found/not found correctly by ID, when using
  469. // TRUE or FALSE to match their 'checked' state.
  470. $this->assertFieldById('edit-checkbox-enabled', TRUE);
  471. $this->assertFieldById('edit-checkbox-disabled', FALSE);
  472. $this->assertNoFieldById('edit-checkbox-enabled', FALSE);
  473. $this->assertNoFieldById('edit-checkbox-disabled', TRUE);
  474. // Test that checkboxes are found by ID, when using NULL to ignore the
  475. // 'checked' state.
  476. $this->assertFieldById('edit-checkbox-enabled', NULL);
  477. $this->assertFieldById('edit-checkbox-disabled', NULL);
  478. // Test that checkboxes are found by ID when passing no second parameter.
  479. $this->assertFieldById('edit-checkbox-enabled');
  480. $this->assertFieldById('edit-checkbox-disabled');
  481. // Test that we have legacy support.
  482. $this->assertFieldById('edit-checkbox-enabled', '1');
  483. $this->assertFieldById('edit-checkbox-disabled', '');
  484. // Test that the assertion fails correctly when using NULL to ignore state.
  485. try {
  486. $this->assertNoFieldById('edit-checkbox-disabled', NULL);
  487. $this->fail('The "edit-checkbox-disabled" field was not found by ID, using NULL value.');
  488. }
  489. catch (ExpectationException $e) {
  490. $this->pass('assertNoFieldById failed correctly. The "edit-checkbox-disabled" field was found by ID using NULL value.');
  491. }
  492. // Part 3 - Test the specific 'checked' assertions.
  493. $this->assertFieldChecked('edit-checkbox-enabled');
  494. $this->assertNoFieldChecked('edit-checkbox-disabled');
  495. // Test that the assertion fails correctly with non-existent field id.
  496. try {
  497. $this->assertNoFieldChecked('incorrect_checkbox_id');
  498. $this->fail('The "incorrect_checkbox_id" field was found');
  499. }
  500. catch (ExpectationException $e) {
  501. $this->pass('assertNoFieldChecked correctly failed. The "incorrect_checkbox_id" field was not found.');
  502. }
  503. // Test that the assertion fails correctly for a checkbox that is checked.
  504. try {
  505. $this->assertNoFieldChecked('edit-checkbox-enabled');
  506. $this->fail('The "edit-checkbox-enabled" field was not found in a checked state.');
  507. }
  508. catch (ExpectationException $e) {
  509. $this->pass('assertNoFieldChecked correctly failed. The "edit-checkbox-enabled" field was found in a checked state.');
  510. }
  511. // Test that the assertion fails correctly for a checkbox that is not
  512. // checked.
  513. try {
  514. $this->assertFieldChecked('edit-checkbox-disabled');
  515. $this->fail('The "edit-checkbox-disabled" field was found and checked.');
  516. }
  517. catch (ExpectationException $e) {
  518. $this->pass('assertFieldChecked correctly failed. The "edit-checkbox-disabled" field was not found in a checked state.');
  519. }
  520. }
  521. /**
  522. * Tests the ::cronRun() method.
  523. */
  524. public function testCronRun() {
  525. $last_cron_time = \Drupal::state()->get('system.cron_last');
  526. $this->cronRun();
  527. $this->assertSession()->statusCodeEquals(204);
  528. $next_cron_time = \Drupal::state()->get('system.cron_last');
  529. $this->assertGreaterThan($last_cron_time, $next_cron_time);
  530. }
  531. /**
  532. * Tests the Drupal install done in \Drupal\Tests\BrowserTestBase::setUp().
  533. */
  534. public function testInstall() {
  535. $htaccess_filename = $this->tempFilesDirectory . '/.htaccess';
  536. $this->assertTrue(file_exists($htaccess_filename), "$htaccess_filename exists");
  537. }
  538. /**
  539. * Tests the assumption that local time is in 'Australia/Sydney'.
  540. */
  541. public function testLocalTimeZone() {
  542. // The 'Australia/Sydney' time zone is set in core/tests/bootstrap.php
  543. $this->assertEquals('Australia/Sydney', date_default_timezone_get());
  544. // The 'Australia/Sydney' time zone is also set in
  545. // FunctionalTestSetupTrait::initConfig().
  546. $config_factory = $this->container->get('config.factory');
  547. $value = $config_factory->get('system.date')->get('timezone.default');
  548. $this->assertEquals('Australia/Sydney', $value);
  549. }
  550. /**
  551. * Tests the ::checkForMetaRefresh() method.
  552. */
  553. public function testCheckForMetaRefresh() {
  554. // Disable following redirects in the client.
  555. $this->getSession()->getDriver()->getClient()->followRedirects(FALSE);
  556. // Set the maximumMetaRefreshCount to zero to make sure the redirect doesn't
  557. // happen when doing a drupalGet.
  558. $this->maximumMetaRefreshCount = 0;
  559. $this->drupalGet('test-meta-refresh');
  560. $this->assertNotEmpty($this->cssSelect('meta[http-equiv="refresh"]'));
  561. // Allow one redirect to happen.
  562. $this->maximumMetaRefreshCount = 1;
  563. $this->checkForMetaRefresh();
  564. // Check that we are now on the test page.
  565. $this->assertSession()->pageTextContains('Test page text.');
  566. }
  567. public function testGetDefaultDriveInstance() {
  568. putenv('MINK_DRIVER_ARGS=' . json_encode([NULL, ['key1' => ['key2' => ['key3' => 3, 'key3.1' => 3.1]]]]));
  569. $this->getDefaultDriverInstance();
  570. $this->assertEquals([NULL, ['key1' => ['key2' => ['key3' => 3, 'key3.1' => 3.1]]]], $this->minkDefaultDriverArgs);
  571. }
  572. /**
  573. * Ensures we can't access modules we shouldn't be able to after install.
  574. */
  575. public function testProfileModules() {
  576. $this->setExpectedException(\InvalidArgumentException::class, 'The module demo_umami_content does not exist.');
  577. $this->assertFileExists('core/profiles/demo_umami/modules/demo_umami_content/demo_umami_content.info.yml');
  578. \Drupal::service('extension.list.module')->getPathname('demo_umami_content');
  579. }
  580. }