BrowserTestBaseTest.php 23 KB

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