3 namespace Drupal\FunctionalTests;
5 use Behat\Mink\Exception\ExpectationException;
6 use Drupal\Component\Serialization\Json;
7 use Drupal\Component\Utility\Html;
9 use Drupal\Tests\BrowserTestBase;
10 use Drupal\Tests\Traits\Core\CronRunTrait;
13 * Tests BrowserTestBase functionality.
15 * @group browsertestbase
17 class BrowserTestBaseTest extends BrowserTestBase {
26 public static $modules = ['test_page_test', 'form_test', 'system_test', 'node'];
29 * Tests basic page test.
31 public function testGoTo() {
32 $account = $this->drupalCreateUser();
33 $this->drupalLogin($account);
35 // Visit a Drupal page that requires login.
36 $this->drupalGet('test-page');
37 $this->assertSession()->statusCodeEquals(200);
39 // Test page contains some text.
40 $this->assertSession()->pageTextContains('Test page text.');
42 // Check that returned plain text is correct.
43 $text = $this->getTextContent();
44 $this->assertContains('Test page text.', $text);
45 $this->assertNotContains('</html>', $text);
47 // Response includes cache tags that we can assert.
48 $this->assertSession()->responseHeaderEquals('X-Drupal-Cache-Tags', 'http_response rendered');
50 // Test that we can read the JS settings.
51 $js_settings = $this->getDrupalSettings();
52 $this->assertSame('azAZ09();.,\\\/-_{}', $js_settings['test-setting']);
54 // Test drupalGet with a url object.
55 $url = Url::fromRoute('test_page_test.render_title');
56 $this->drupalGet($url);
57 $this->assertSession()->statusCodeEquals(200);
59 // Test page contains some text.
60 $this->assertSession()->pageTextContains('Hello Drupal');
62 // Test that setting headers with drupalGet() works.
63 $this->drupalGet('system-test/header', [], [
64 'Test-Header' => 'header value',
66 $returned_header = $this->getSession()->getResponseHeader('Test-Header');
67 $this->assertSame('header value', $returned_header);
73 public function testDrupalGet() {
74 $this->drupalGet('test-page');
75 $this->assertSession()->statusCodeEquals(200);
76 $this->assertSession()->addressEquals('test-page');
77 $this->drupalGet('/test-page');
78 $this->assertSession()->statusCodeEquals(200);
79 $this->assertSession()->addressEquals('test-page');
80 $this->drupalGet('/test-page/');
81 $this->assertSession()->statusCodeEquals(200);
82 $this->assertSession()->addressEquals('/test-page/');
86 * Tests basic form functionality.
88 public function testForm() {
89 // Ensure the proper response code for a _form route.
90 $this->drupalGet('form-test/object-builder');
91 $this->assertSession()->statusCodeEquals(200);
93 // Ensure the form and text field exist.
94 $this->assertSession()->elementExists('css', 'form#form-test-form-test-object');
95 $this->assertSession()->fieldExists('bananas');
97 // Check that the hidden field exists and has a specific value.
98 $this->assertSession()->hiddenFieldExists('strawberry');
99 $this->assertSession()->hiddenFieldExists('red');
100 $this->assertSession()->hiddenFieldExists('redstrawberryhiddenfield');
101 $this->assertSession()->hiddenFieldValueNotEquals('strawberry', 'brown');
102 $this->assertSession()->hiddenFieldValueEquals('strawberry', 'red');
104 // Check that a hidden field does not exist.
105 $this->assertSession()->hiddenFieldNotExists('bananas');
106 $this->assertSession()->hiddenFieldNotExists('pineapple');
108 $edit = ['bananas' => 'green'];
109 $this->submitForm($edit, 'Save', 'form-test-form-test-object');
111 $config_factory = $this->container->get('config.factory');
112 $value = $config_factory->get('form_test.object')->get('bananas');
113 $this->assertSame('green', $value);
115 // Test drupalPostForm().
116 $edit = ['bananas' => 'red'];
117 $result = $this->drupalPostForm('form-test/object-builder', $edit, 'Save');
118 $this->assertSame($this->getSession()->getPage()->getContent(), $result);
119 $value = $config_factory->get('form_test.object')->get('bananas');
120 $this->assertSame('red', $value);
122 $this->drupalPostForm('form-test/object-builder', NULL, 'Save');
123 $value = $config_factory->get('form_test.object')->get('bananas');
124 $this->assertSame('', $value);
126 // Test drupalPostForm() with no-html response.
127 $values = Json::decode($this->drupalPostForm('form_test/form-state-values-clean', [], t('Submit')));
128 $this->assertTrue(1000, $values['beer']);
130 // Test drupalPostForm() with form by HTML id.
131 $this->drupalCreateContentType(['type' => 'page']);
132 $this->drupalLogin($this->drupalCreateUser(['create page content']));
133 $this->drupalGet('form-test/two-instances-of-same-form');
134 $this->getSession()->getPage()->fillField('edit-title-0-value', 'form1');
135 $this->getSession()->getPage()->fillField('edit-title-0-value--2', 'form2');
136 $this->drupalPostForm(NULL, [], 'Save', [], 'node-page-form--2');
137 $this->assertSession()->pageTextContains('Page form2 has been created.');
141 * Tests clickLink() functionality.
143 public function testClickLink() {
144 $this->drupalGet('test-page');
145 $this->clickLink('Visually identical test links');
146 $this->assertContains('user/login', $this->getSession()->getCurrentUrl());
147 $this->drupalGet('test-page');
148 $this->clickLink('Visually identical test links', 0);
149 $this->assertContains('user/login', $this->getSession()->getCurrentUrl());
150 $this->drupalGet('test-page');
151 $this->clickLink('Visually identical test links', 1);
152 $this->assertContains('user/register', $this->getSession()->getCurrentUrl());
155 public function testError() {
156 $this->setExpectedException('\Exception', 'User notice: foo');
157 $this->drupalGet('test-error');
161 * Tests linkExists() with pipe character (|) in locator.
163 * @see \Drupal\Tests\WebAssert::linkExists()
165 public function testPipeCharInLocator() {
166 $this->drupalGet('test-pipe-char');
167 $this->assertSession()->linkExists('foo|bar|baz');
171 * Tests linkExistsExact() functionality.
173 * @see \Drupal\Tests\WebAssert::linkExistsExact()
175 public function testLinkExistsExact() {
176 $this->drupalGet('test-pipe-char');
177 $this->assertSession()->linkExistsExact('foo|bar|baz');
181 * Tests linkExistsExact() functionality fail.
183 * @see \Drupal\Tests\WebAssert::linkExistsExact()
185 public function testInvalidLinkExistsExact() {
186 $this->drupalGet('test-pipe-char');
187 $this->setExpectedException(ExpectationException::class, 'Link with label foo|bar found');
188 $this->assertSession()->linkExistsExact('foo|bar');
192 * Tests linkNotExistsExact() functionality.
194 * @see \Drupal\Tests\WebAssert::linkNotExistsExact()
196 public function testLinkNotExistsExact() {
197 $this->drupalGet('test-pipe-char');
198 $this->assertSession()->linkNotExistsExact('foo|bar');
202 * Tests linkNotExistsExact() functionality fail.
204 * @see \Drupal\Tests\WebAssert::linkNotExistsExact()
206 public function testInvalidLinkNotExistsExact() {
207 $this->drupalGet('test-pipe-char');
208 $this->setExpectedException(ExpectationException::class, 'Link with label foo|bar|baz not found');
209 $this->assertSession()->linkNotExistsExact('foo|bar|baz');
213 * Tests legacy text asserts.
215 public function testTextAsserts() {
216 $this->drupalGet('test-encoded');
217 $dangerous = 'Bad html <script>alert(123);</script>';
218 $sanitized = Html::escape($dangerous);
219 $this->assertNoText($dangerous);
220 $this->assertText($sanitized);
222 // Test getRawContent().
223 $this->assertSame($this->getSession()->getPage()->getContent(), $this->getSession()->getPage()->getContent());
227 * Tests legacy field asserts which use xpath directly.
229 public function testXpathAsserts() {
230 $this->drupalGet('test-field-xpath');
231 $this->assertFieldsByValue($this->xpath("//h1[@class = 'page-title']"), NULL);
232 $this->assertFieldsByValue($this->xpath('//table/tbody/tr[2]/td[1]'), 'one');
233 $this->assertFieldByXPath('//table/tbody/tr[2]/td[1]', 'one');
235 $this->assertFieldsByValue($this->xpath("//input[@id = 'edit-name']"), 'Test name');
236 $this->assertFieldByXPath("//input[@id = 'edit-name']", 'Test name');
237 $this->assertFieldsByValue($this->xpath("//select[@id = 'edit-options']"), '2');
238 $this->assertFieldByXPath("//select[@id = 'edit-options']", '2');
240 $this->assertNoFieldByXPath('//notexisting');
241 $this->assertNoFieldByXPath("//input[@id = 'edit-name']", 'wrong value');
243 // Test that the assertion fails correctly.
245 $this->assertFieldByXPath("//input[@id = 'notexisting']");
246 $this->fail('The "notexisting" field was found.');
248 catch (\PHPUnit_Framework_ExpectationFailedException $e) {
249 $this->pass('assertFieldByXPath correctly failed. The "notexisting" field was not found.');
253 $this->assertNoFieldByXPath("//input[@id = 'edit-name']");
254 $this->fail('The "edit-name" field was not found.');
256 catch (ExpectationException $e) {
257 $this->pass('assertNoFieldByXPath correctly failed. The "edit-name" field was found.');
261 $this->assertFieldsByValue($this->xpath("//input[@id = 'edit-name']"), 'not the value');
262 $this->fail('The "edit-name" field is found with the value "not the value".');
264 catch (\PHPUnit_Framework_ExpectationFailedException $e) {
265 $this->pass('The "edit-name" field is not found with the value "not the value".');
270 * Tests legacy field asserts using textfields.
272 public function testFieldAssertsForTextfields() {
273 $this->drupalGet('test-field-xpath');
275 // *** 1. assertNoField().
276 $this->assertNoField('invalid_name_and_id');
278 // Test that the assertion fails correctly when searching by name.
280 $this->assertNoField('name');
281 $this->fail('The "name" field was not found based on name.');
283 catch (ExpectationException $e) {
284 $this->pass('assertNoField correctly failed. The "name" field was found by name.');
287 // Test that the assertion fails correctly when searching by id.
289 $this->assertNoField('edit-name');
290 $this->fail('The "name" field was not found based on id.');
292 catch (ExpectationException $e) {
293 $this->pass('assertNoField correctly failed. The "name" field was found by id.');
296 // *** 2. assertField().
297 $this->assertField('name');
298 $this->assertField('edit-name');
300 // Test that the assertion fails correctly if the field does not exist.
302 $this->assertField('invalid_name_and_id');
303 $this->fail('The "invalid_name_and_id" field was found.');
305 catch (\PHPUnit_Framework_ExpectationFailedException $e) {
306 $this->pass('assertField correctly failed. The "invalid_name_and_id" field was not found.');
309 // *** 3. assertNoFieldById().
310 $this->assertNoFieldById('name');
311 $this->assertNoFieldById('name', 'not the value');
312 $this->assertNoFieldById('notexisting');
313 $this->assertNoFieldById('notexisting', NULL);
315 // Test that the assertion fails correctly if no value is passed in.
317 $this->assertNoFieldById('edit-description');
318 $this->fail('The "description" field, with no value was not found.');
320 catch (ExpectationException $e) {
321 $this->pass('The "description" field, with no value was found.');
324 // Test that the assertion fails correctly if a NULL value is passed in.
326 $this->assertNoFieldById('edit-name', NULL);
327 $this->fail('The "name" field was not found.');
329 catch (ExpectationException $e) {
330 $this->pass('The "name" field was found.');
333 // *** 4. assertFieldById().
334 $this->assertFieldById('edit-name', NULL);
335 $this->assertFieldById('edit-name', 'Test name');
336 $this->assertFieldById('edit-description', NULL);
337 $this->assertFieldById('edit-description');
339 // Test that the assertion fails correctly if no value is passed in.
341 $this->assertFieldById('edit-name');
342 $this->fail('The "edit-name" field with no value was found.');
344 catch (\PHPUnit_Framework_ExpectationFailedException $e) {
345 $this->pass('The "edit-name" field with no value was not found.');
348 // Test that the assertion fails correctly if the wrong value is passed in.
350 $this->assertFieldById('edit-name', 'not the value');
351 $this->fail('The "name" field was found, using the wrong value.');
353 catch (\PHPUnit_Framework_ExpectationFailedException $e) {
354 $this->pass('The "name" field was not found, using the wrong value.');
357 // *** 5. assertNoFieldByName().
358 $this->assertNoFieldByName('name');
359 $this->assertNoFieldByName('name', 'not the value');
360 $this->assertNoFieldByName('notexisting');
361 $this->assertNoFieldByName('notexisting', NULL);
363 // Test that the assertion fails correctly if no value is passed in.
365 $this->assertNoFieldByName('description');
366 $this->fail('The "description" field, with no value was not found.');
368 catch (ExpectationException $e) {
369 $this->pass('The "description" field, with no value was found.');
372 // Test that the assertion fails correctly if a NULL value is passed in.
374 $this->assertNoFieldByName('name', NULL);
375 $this->fail('The "name" field was not found.');
377 catch (ExpectationException $e) {
378 $this->pass('The "name" field was found.');
381 // *** 6. assertFieldByName().
382 $this->assertFieldByName('name');
383 $this->assertFieldByName('name', NULL);
384 $this->assertFieldByName('name', 'Test name');
385 $this->assertFieldByName('description');
386 $this->assertFieldByName('description', '');
387 $this->assertFieldByName('description', NULL);
389 // Test that the assertion fails correctly if given the wrong name.
391 $this->assertFieldByName('non-existing-name');
392 $this->fail('The "non-existing-name" field was found.');
394 catch (\PHPUnit_Framework_ExpectationFailedException $e) {
395 $this->pass('The "non-existing-name" field was not found');
398 // Test that the assertion fails correctly if given the wrong value.
400 $this->assertFieldByName('name', 'not the value');
401 $this->fail('The "name" field with incorrect value was found.');
403 catch (\PHPUnit_Framework_ExpectationFailedException $e) {
404 $this->pass('assertFieldByName correctly failed. The "name" field with incorrect value was not found.');
407 // Test that text areas can contain new lines.
408 $this->assertFieldsByValue($this->xpath("//textarea[@id = 'edit-test-textarea-with-newline']"), "Test text with\nnewline");
412 * Tests legacy field asserts for options field type.
414 public function testFieldAssertsForOptions() {
415 $this->drupalGet('test-field-xpath');
417 // Option field type.
418 $this->assertOptionByText('options', 'one');
420 $this->assertOptionByText('options', 'four');
421 $this->fail('The select option "four" was found.');
423 catch (ExpectationException $e) {
424 $this->pass($e->getMessage());
427 $this->assertOption('options', 1);
429 $this->assertOption('options', 4);
430 $this->fail('The select option "4" was found.');
432 catch (ExpectationException $e) {
433 $this->pass($e->getMessage());
436 $this->assertNoOption('options', 'non-existing');
438 $this->assertNoOption('options', 'one');
439 $this->fail('The select option "one" was not found.');
441 catch (ExpectationException $e) {
442 $this->pass($e->getMessage());
445 $this->assertOptionSelected('options', 2);
447 $this->assertOptionSelected('options', 4);
448 $this->fail('The select option "4" was selected.');
450 catch (ExpectationException $e) {
451 $this->pass($e->getMessage());
455 $this->assertOptionSelected('options', 1);
456 $this->fail('The select option "1" was selected.');
458 catch (\PHPUnit_Framework_ExpectationFailedException $e) {
459 $this->pass($e->getMessage());
462 // Test \Drupal\FunctionalTests\AssertLegacyTrait::getAllOptions.
463 $this->drupalGet('/form-test/select');
464 $this->assertCount(6, $this->getAllOptions($this->cssSelect('select[name="opt_groups"]')[0]));
468 * Tests legacy field asserts for button field type.
470 public function testFieldAssertsForButton() {
471 $this->drupalGet('test-field-xpath');
473 $this->assertFieldById('edit-save', NULL);
474 // Test that the assertion fails correctly if the field value is passed in
475 // rather than the id.
477 $this->assertFieldById('Save', NULL);
478 $this->fail('The field with id of "Save" was found.');
480 catch (\PHPUnit_Framework_ExpectationFailedException $e) {
481 $this->pass($e->getMessage());
484 $this->assertNoFieldById('Save', NULL);
485 // Test that the assertion fails correctly if the id of an actual field is
488 $this->assertNoFieldById('edit-save', NULL);
489 $this->fail('The field with id of "edit-save" was not found.');
491 catch (ExpectationException $e) {
492 $this->pass($e->getMessage());
495 // Test that multiple fields with the same name are validated correctly.
496 $this->assertFieldByName('duplicate_button', 'Duplicate button 1');
497 $this->assertFieldByName('duplicate_button', 'Duplicate button 2');
498 $this->assertNoFieldByName('duplicate_button', 'Rabbit');
501 $this->assertNoFieldByName('duplicate_button', 'Duplicate button 2');
502 $this->fail('The "duplicate_button" field with the value Duplicate button 2 was not found.');
504 catch (ExpectationException $e) {
505 $this->pass('assertNoFieldByName correctly failed. The "duplicate_button" field with the value Duplicate button 2 was found.');
510 * Tests legacy field asserts for checkbox field type.
512 public function testFieldAssertsForCheckbox() {
513 $this->drupalGet('test-field-xpath');
515 // Part 1 - Test by name.
516 // Test that checkboxes are found/not found correctly by name, when using
517 // TRUE or FALSE to match their 'checked' state.
518 $this->assertFieldByName('checkbox_enabled', TRUE);
519 $this->assertFieldByName('checkbox_disabled', FALSE);
520 $this->assertNoFieldByName('checkbox_enabled', FALSE);
521 $this->assertNoFieldByName('checkbox_disabled', TRUE);
523 // Test that checkboxes are found by name when using NULL to ignore the
525 $this->assertFieldByName('checkbox_enabled', NULL);
526 $this->assertFieldByName('checkbox_disabled', NULL);
528 // Test that checkboxes are found by name when passing no second parameter.
529 $this->assertFieldByName('checkbox_enabled');
530 $this->assertFieldByName('checkbox_disabled');
532 // Test that we have legacy support.
533 $this->assertFieldByName('checkbox_enabled', '1');
534 $this->assertFieldByName('checkbox_disabled', '');
536 // Test that the assertion fails correctly when using NULL to ignore state.
538 $this->assertNoFieldByName('checkbox_enabled', NULL);
539 $this->fail('The "checkbox_enabled" field was not found by name, using NULL value.');
541 catch (ExpectationException $e) {
542 $this->pass('assertNoFieldByName failed correctly. The "checkbox_enabled" field was found using NULL value.');
545 // Part 2 - Test by ID.
546 // Test that checkboxes are found/not found correctly by ID, when using
547 // TRUE or FALSE to match their 'checked' state.
548 $this->assertFieldById('edit-checkbox-enabled', TRUE);
549 $this->assertFieldById('edit-checkbox-disabled', FALSE);
550 $this->assertNoFieldById('edit-checkbox-enabled', FALSE);
551 $this->assertNoFieldById('edit-checkbox-disabled', TRUE);
553 // Test that checkboxes are found by ID, when using NULL to ignore the
555 $this->assertFieldById('edit-checkbox-enabled', NULL);
556 $this->assertFieldById('edit-checkbox-disabled', NULL);
558 // Test that checkboxes are found by ID when passing no second parameter.
559 $this->assertFieldById('edit-checkbox-enabled');
560 $this->assertFieldById('edit-checkbox-disabled');
562 // Test that we have legacy support.
563 $this->assertFieldById('edit-checkbox-enabled', '1');
564 $this->assertFieldById('edit-checkbox-disabled', '');
566 // Test that the assertion fails correctly when using NULL to ignore state.
568 $this->assertNoFieldById('edit-checkbox-disabled', NULL);
569 $this->fail('The "edit-checkbox-disabled" field was not found by ID, using NULL value.');
571 catch (ExpectationException $e) {
572 $this->pass('assertNoFieldById failed correctly. The "edit-checkbox-disabled" field was found by ID using NULL value.');
575 // Part 3 - Test the specific 'checked' assertions.
576 $this->assertFieldChecked('edit-checkbox-enabled');
577 $this->assertNoFieldChecked('edit-checkbox-disabled');
579 // Test that the assertion fails correctly with non-existent field id.
581 $this->assertNoFieldChecked('incorrect_checkbox_id');
582 $this->fail('The "incorrect_checkbox_id" field was found');
584 catch (ExpectationException $e) {
585 $this->pass('assertNoFieldChecked correctly failed. The "incorrect_checkbox_id" field was not found.');
588 // Test that the assertion fails correctly for a checkbox that is checked.
590 $this->assertNoFieldChecked('edit-checkbox-enabled');
591 $this->fail('The "edit-checkbox-enabled" field was not found in a checked state.');
593 catch (ExpectationException $e) {
594 $this->pass('assertNoFieldChecked correctly failed. The "edit-checkbox-enabled" field was found in a checked state.');
597 // Test that the assertion fails correctly for a checkbox that is not
600 $this->assertFieldChecked('edit-checkbox-disabled');
601 $this->fail('The "edit-checkbox-disabled" field was found and checked.');
603 catch (ExpectationException $e) {
604 $this->pass('assertFieldChecked correctly failed. The "edit-checkbox-disabled" field was not found in a checked state.');
609 * Tests the ::cronRun() method.
611 public function testCronRun() {
612 $last_cron_time = \Drupal::state()->get('system.cron_last');
614 $this->assertSession()->statusCodeEquals(204);
615 $next_cron_time = \Drupal::state()->get('system.cron_last');
617 $this->assertGreaterThan($last_cron_time, $next_cron_time);
621 * Tests the Drupal install done in \Drupal\Tests\BrowserTestBase::setUp().
623 public function testInstall() {
624 $htaccess_filename = $this->tempFilesDirectory . '/.htaccess';
625 $this->assertTrue(file_exists($htaccess_filename), "$htaccess_filename exists");
629 * Tests the assumption that local time is in 'Australia/Sydney'.
631 public function testLocalTimeZone() {
632 // The 'Australia/Sydney' time zone is set in core/tests/bootstrap.php
633 $this->assertEquals('Australia/Sydney', date_default_timezone_get());
635 // The 'Australia/Sydney' time zone is also set in
636 // FunctionalTestSetupTrait::initConfig().
637 $config_factory = $this->container->get('config.factory');
638 $value = $config_factory->get('system.date')->get('timezone.default');
639 $this->assertEquals('Australia/Sydney', $value);
643 * Tests the ::checkForMetaRefresh() method.
645 public function testCheckForMetaRefresh() {
646 // Disable following redirects in the client.
647 $this->getSession()->getDriver()->getClient()->followRedirects(FALSE);
648 // Set the maximumMetaRefreshCount to zero to make sure the redirect doesn't
649 // happen when doing a drupalGet.
650 $this->maximumMetaRefreshCount = 0;
651 $this->drupalGet('test-meta-refresh');
652 $this->assertNotEmpty($this->cssSelect('meta[http-equiv="refresh"]'));
653 // Allow one redirect to happen.
654 $this->maximumMetaRefreshCount = 1;
655 $this->checkForMetaRefresh();
656 // Check that we are now on the test page.
657 $this->assertSession()->pageTextContains('Test page text.');
660 public function testGetDefaultDriveInstance() {
661 putenv('MINK_DRIVER_ARGS=' . json_encode([NULL, ['key1' => ['key2' => ['key3' => 3, 'key3.1' => 3.1]]]]));
662 $this->getDefaultDriverInstance();
663 $this->assertEquals([NULL, ['key1' => ['key2' => ['key3' => 3, 'key3.1' => 3.1]]]], $this->minkDefaultDriverArgs);
667 * Ensures we can't access modules we shouldn't be able to after install.
669 public function testProfileModules() {
670 $this->setExpectedException(\InvalidArgumentException::class, 'The module demo_umami_content does not exist.');
671 $this->assertFileExists('core/profiles/demo_umami/modules/demo_umami_content/demo_umami_content.info.yml');
672 \Drupal::service('extension.list.module')->getPathname('demo_umami_content');