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'];
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);
71 * Tests basic form functionality.
73 public function testForm() {
74 // Ensure the proper response code for a _form route.
75 $this->drupalGet('form-test/object-builder');
76 $this->assertSession()->statusCodeEquals(200);
78 // Ensure the form and text field exist.
79 $this->assertSession()->elementExists('css', 'form#form-test-form-test-object');
80 $this->assertSession()->fieldExists('bananas');
82 // Check that the hidden field exists and has a specific value.
83 $this->assertSession()->hiddenFieldExists('strawberry');
84 $this->assertSession()->hiddenFieldExists('red');
85 $this->assertSession()->hiddenFieldExists('redstrawberryhiddenfield');
86 $this->assertSession()->hiddenFieldValueNotEquals('strawberry', 'brown');
87 $this->assertSession()->hiddenFieldValueEquals('strawberry', 'red');
89 // Check that a hidden field does not exist.
90 $this->assertSession()->hiddenFieldNotExists('bananas');
91 $this->assertSession()->hiddenFieldNotExists('pineapple');
93 $edit = ['bananas' => 'green'];
94 $this->submitForm($edit, 'Save', 'form-test-form-test-object');
96 $config_factory = $this->container->get('config.factory');
97 $value = $config_factory->get('form_test.object')->get('bananas');
98 $this->assertSame('green', $value);
100 // Test drupalPostForm().
101 $edit = ['bananas' => 'red'];
102 $result = $this->drupalPostForm('form-test/object-builder', $edit, 'Save');
103 $this->assertSame($this->getSession()->getPage()->getContent(), $result);
104 $value = $config_factory->get('form_test.object')->get('bananas');
105 $this->assertSame('red', $value);
107 $this->drupalPostForm('form-test/object-builder', NULL, 'Save');
108 $value = $config_factory->get('form_test.object')->get('bananas');
109 $this->assertSame('', $value);
111 // Test drupalPostForm() with no-html response.
112 $values = Json::decode($this->drupalPostForm('form_test/form-state-values-clean', [], t('Submit')));
113 $this->assertTrue(1000, $values['beer']);
117 * Tests clickLink() functionality.
119 public function testClickLink() {
120 $this->drupalGet('test-page');
121 $this->clickLink('Visually identical test links');
122 $this->assertContains('user/login', $this->getSession()->getCurrentUrl());
123 $this->drupalGet('test-page');
124 $this->clickLink('Visually identical test links', 0);
125 $this->assertContains('user/login', $this->getSession()->getCurrentUrl());
126 $this->drupalGet('test-page');
127 $this->clickLink('Visually identical test links', 1);
128 $this->assertContains('user/register', $this->getSession()->getCurrentUrl());
131 public function testError() {
132 $this->setExpectedException('\Exception', 'User notice: foo');
133 $this->drupalGet('test-error');
137 * Tests linkExists() with pipe character (|) in locator.
139 * @see \Drupal\Tests\WebAssert::linkExists()
141 public function testPipeCharInLocator() {
142 $this->drupalGet('test-pipe-char');
143 $this->assertSession()->linkExists('foo|bar|baz');
147 * Tests linkExistsExact() functionality.
149 * @see \Drupal\Tests\WebAssert::linkExistsExact()
151 public function testLinkExistsExact() {
152 $this->drupalGet('test-pipe-char');
153 $this->assertSession()->linkExistsExact('foo|bar|baz');
157 * Tests linkExistsExact() functionality fail.
159 * @see \Drupal\Tests\WebAssert::linkExistsExact()
161 public function testInvalidLinkExistsExact() {
162 $this->drupalGet('test-pipe-char');
163 $this->setExpectedException(ExpectationException::class, 'Link with label foo|bar found');
164 $this->assertSession()->linkExistsExact('foo|bar');
168 * Tests linkNotExistsExact() functionality.
170 * @see \Drupal\Tests\WebAssert::linkNotExistsExact()
172 public function testLinkNotExistsExact() {
173 $this->drupalGet('test-pipe-char');
174 $this->assertSession()->linkNotExistsExact('foo|bar');
178 * Tests linkNotExistsExact() functionality fail.
180 * @see \Drupal\Tests\WebAssert::linkNotExistsExact()
182 public function testInvalidLinkNotExistsExact() {
183 $this->drupalGet('test-pipe-char');
184 $this->setExpectedException(ExpectationException::class, 'Link with label foo|bar|baz not found');
185 $this->assertSession()->linkNotExistsExact('foo|bar|baz');
189 * Tests legacy text asserts.
191 public function testLegacyTextAsserts() {
192 $this->drupalGet('test-encoded');
193 $dangerous = 'Bad html <script>alert(123);</script>';
194 $sanitized = Html::escape($dangerous);
195 $this->assertNoText($dangerous);
196 $this->assertText($sanitized);
198 // Test getRawContent().
199 $this->assertSame($this->getSession()->getPage()->getContent(), $this->getRawContent());
203 * Tests legacy field asserts which use xpath directly.
205 public function testLegacyXpathAsserts() {
206 $this->drupalGet('test-field-xpath');
207 $this->assertFieldsByValue($this->xpath("//h1[@class = 'page-title']"), NULL);
208 $this->assertFieldsByValue($this->xpath('//table/tbody/tr[2]/td[1]'), 'one');
209 $this->assertFieldByXPath('//table/tbody/tr[2]/td[1]', 'one');
211 $this->assertFieldsByValue($this->xpath("//input[@id = 'edit-name']"), 'Test name');
212 $this->assertFieldByXPath("//input[@id = 'edit-name']", 'Test name');
213 $this->assertFieldsByValue($this->xpath("//select[@id = 'edit-options']"), '2');
214 $this->assertFieldByXPath("//select[@id = 'edit-options']", '2');
216 $this->assertNoFieldByXPath('//notexisting');
217 $this->assertNoFieldByXPath("//input[@id = 'edit-name']", 'wrong value');
219 // Test that the assertion fails correctly.
221 $this->assertFieldByXPath("//input[@id = 'notexisting']");
222 $this->fail('The "notexisting" field was found.');
224 catch (\PHPUnit_Framework_ExpectationFailedException $e) {
225 $this->pass('assertFieldByXPath correctly failed. The "notexisting" field was not found.');
229 $this->assertNoFieldByXPath("//input[@id = 'edit-name']");
230 $this->fail('The "edit-name" field was not found.');
232 catch (ExpectationException $e) {
233 $this->pass('assertNoFieldByXPath correctly failed. The "edit-name" field was found.');
237 $this->assertFieldsByValue($this->xpath("//input[@id = 'edit-name']"), 'not the value');
238 $this->fail('The "edit-name" field is found with the value "not the value".');
240 catch (\PHPUnit_Framework_ExpectationFailedException $e) {
241 $this->pass('The "edit-name" field is not found with the value "not the value".');
246 * Tests legacy field asserts using textfields.
248 public function testLegacyFieldAssertsForTextfields() {
249 $this->drupalGet('test-field-xpath');
251 // *** 1. assertNoField().
252 $this->assertNoField('invalid_name_and_id');
254 // Test that the assertion fails correctly when searching by name.
256 $this->assertNoField('name');
257 $this->fail('The "name" field was not found based on name.');
259 catch (ExpectationException $e) {
260 $this->pass('assertNoField correctly failed. The "name" field was found by name.');
263 // Test that the assertion fails correctly when searching by id.
265 $this->assertNoField('edit-name');
266 $this->fail('The "name" field was not found based on id.');
268 catch (ExpectationException $e) {
269 $this->pass('assertNoField correctly failed. The "name" field was found by id.');
272 // *** 2. assertField().
273 $this->assertField('name');
274 $this->assertField('edit-name');
276 // Test that the assertion fails correctly if the field does not exist.
278 $this->assertField('invalid_name_and_id');
279 $this->fail('The "invalid_name_and_id" field was found.');
281 catch (\PHPUnit_Framework_ExpectationFailedException $e) {
282 $this->pass('assertField correctly failed. The "invalid_name_and_id" field was not found.');
285 // *** 3. assertNoFieldById().
286 $this->assertNoFieldById('name');
287 $this->assertNoFieldById('name', 'not the value');
288 $this->assertNoFieldById('notexisting');
289 $this->assertNoFieldById('notexisting', NULL);
291 // Test that the assertion fails correctly if no value is passed in.
293 $this->assertNoFieldById('edit-description');
294 $this->fail('The "description" field, with no value was not found.');
296 catch (ExpectationException $e) {
297 $this->pass('The "description" field, with no value was found.');
300 // Test that the assertion fails correctly if a NULL value is passed in.
302 $this->assertNoFieldById('edit-name', NULL);
303 $this->fail('The "name" field was not found.');
305 catch (ExpectationException $e) {
306 $this->pass('The "name" field was found.');
309 // *** 4. assertFieldById().
310 $this->assertFieldById('edit-name', NULL);
311 $this->assertFieldById('edit-name', 'Test name');
312 $this->assertFieldById('edit-description', NULL);
313 $this->assertFieldById('edit-description');
315 // Test that the assertion fails correctly if no value is passed in.
317 $this->assertFieldById('edit-name');
318 $this->fail('The "edit-name" field with no value was found.');
320 catch (\PHPUnit_Framework_ExpectationFailedException $e) {
321 $this->pass('The "edit-name" field with no value was not found.');
324 // Test that the assertion fails correctly if the wrong value is passed in.
326 $this->assertFieldById('edit-name', 'not the value');
327 $this->fail('The "name" field was found, using the wrong value.');
329 catch (\PHPUnit_Framework_ExpectationFailedException $e) {
330 $this->pass('The "name" field was not found, using the wrong value.');
333 // *** 5. assertNoFieldByName().
334 $this->assertNoFieldByName('name');
335 $this->assertNoFieldByName('name', 'not the value');
336 $this->assertNoFieldByName('notexisting');
337 $this->assertNoFieldByName('notexisting', NULL);
339 // Test that the assertion fails correctly if no value is passed in.
341 $this->assertNoFieldByName('description');
342 $this->fail('The "description" field, with no value was not found.');
344 catch (ExpectationException $e) {
345 $this->pass('The "description" field, with no value was found.');
348 // Test that the assertion fails correctly if a NULL value is passed in.
350 $this->assertNoFieldByName('name', NULL);
351 $this->fail('The "name" field was not found.');
353 catch (ExpectationException $e) {
354 $this->pass('The "name" field was found.');
357 // *** 6. assertFieldByName().
358 $this->assertFieldByName('name');
359 $this->assertFieldByName('name', NULL);
360 $this->assertFieldByName('name', 'Test name');
361 $this->assertFieldByName('description');
362 $this->assertFieldByName('description', '');
363 $this->assertFieldByName('description', NULL);
365 // Test that the assertion fails correctly if given the wrong name.
367 $this->assertFieldByName('non-existing-name');
368 $this->fail('The "non-existing-name" field was found.');
370 catch (\PHPUnit_Framework_ExpectationFailedException $e) {
371 $this->pass('The "non-existing-name" field was not found');
374 // Test that the assertion fails correctly if given the wrong value.
376 $this->assertFieldByName('name', 'not the value');
377 $this->fail('The "name" field with incorrect value was found.');
379 catch (\PHPUnit_Framework_ExpectationFailedException $e) {
380 $this->pass('assertFieldByName correctly failed. The "name" field with incorrect value was not found.');
383 // Test that text areas can contain new lines.
384 $this->assertFieldsByValue($this->xpath("//textarea[@id = 'edit-test-textarea-with-newline']"), "Test text with\nnewline");
388 * Tests legacy field asserts for options field type.
390 public function testLegacyFieldAssertsForOptions() {
391 $this->drupalGet('test-field-xpath');
393 // Option field type.
394 $this->assertOptionByText('options', 'one');
396 $this->assertOptionByText('options', 'four');
397 $this->fail('The select option "four" was found.');
399 catch (ExpectationException $e) {
400 $this->pass($e->getMessage());
403 $this->assertOption('options', 1);
405 $this->assertOption('options', 4);
406 $this->fail('The select option "4" was found.');
408 catch (ExpectationException $e) {
409 $this->pass($e->getMessage());
412 $this->assertNoOption('options', 'non-existing');
414 $this->assertNoOption('options', 'one');
415 $this->fail('The select option "one" was not found.');
417 catch (ExpectationException $e) {
418 $this->pass($e->getMessage());
421 $this->assertOptionSelected('options', 2);
423 $this->assertOptionSelected('options', 4);
424 $this->fail('The select option "4" was selected.');
426 catch (ExpectationException $e) {
427 $this->pass($e->getMessage());
431 $this->assertOptionSelected('options', 1);
432 $this->fail('The select option "1" was selected.');
434 catch (\PHPUnit_Framework_ExpectationFailedException $e) {
435 $this->pass($e->getMessage());
438 // Test \Drupal\FunctionalTests\AssertLegacyTrait::getAllOptions.
439 $this->drupalGet('/form-test/select');
440 $this->assertCount(6, $this->getAllOptions($this->cssSelect('select[name="opt_groups"]')[0]));
444 * Tests legacy field asserts for button field type.
446 public function testLegacyFieldAssertsForButton() {
447 $this->drupalGet('test-field-xpath');
449 $this->assertFieldById('edit-save', NULL);
450 // Test that the assertion fails correctly if the field value is passed in
451 // rather than the id.
453 $this->assertFieldById('Save', NULL);
454 $this->fail('The field with id of "Save" was found.');
456 catch (\PHPUnit_Framework_ExpectationFailedException $e) {
457 $this->pass($e->getMessage());
460 $this->assertNoFieldById('Save', NULL);
461 // Test that the assertion fails correctly if the id of an actual field is
464 $this->assertNoFieldById('edit-save', NULL);
465 $this->fail('The field with id of "edit-save" was not found.');
467 catch (ExpectationException $e) {
468 $this->pass($e->getMessage());
471 // Test that multiple fields with the same name are validated correctly.
472 $this->assertFieldByName('duplicate_button', 'Duplicate button 1');
473 $this->assertFieldByName('duplicate_button', 'Duplicate button 2');
474 $this->assertNoFieldByName('duplicate_button', 'Rabbit');
477 $this->assertNoFieldByName('duplicate_button', 'Duplicate button 2');
478 $this->fail('The "duplicate_button" field with the value Duplicate button 2 was not found.');
480 catch (ExpectationException $e) {
481 $this->pass('assertNoFieldByName correctly failed. The "duplicate_button" field with the value Duplicate button 2 was found.');
486 * Tests legacy field asserts for checkbox field type.
488 public function testLegacyFieldAssertsForCheckbox() {
489 $this->drupalGet('test-field-xpath');
491 // Part 1 - Test by name.
492 // Test that checkboxes are found/not found correctly by name, when using
493 // TRUE or FALSE to match their 'checked' state.
494 $this->assertFieldByName('checkbox_enabled', TRUE);
495 $this->assertFieldByName('checkbox_disabled', FALSE);
496 $this->assertNoFieldByName('checkbox_enabled', FALSE);
497 $this->assertNoFieldByName('checkbox_disabled', TRUE);
499 // Test that checkboxes are found by name when using NULL to ignore the
501 $this->assertFieldByName('checkbox_enabled', NULL);
502 $this->assertFieldByName('checkbox_disabled', NULL);
504 // Test that checkboxes are found by name when passing no second parameter.
505 $this->assertFieldByName('checkbox_enabled');
506 $this->assertFieldByName('checkbox_disabled');
508 // Test that we have legacy support.
509 $this->assertFieldByName('checkbox_enabled', '1');
510 $this->assertFieldByName('checkbox_disabled', '');
512 // Test that the assertion fails correctly when using NULL to ignore state.
514 $this->assertNoFieldByName('checkbox_enabled', NULL);
515 $this->fail('The "checkbox_enabled" field was not found by name, using NULL value.');
517 catch (ExpectationException $e) {
518 $this->pass('assertNoFieldByName failed correctly. The "checkbox_enabled" field was found using NULL value.');
521 // Part 2 - Test by ID.
522 // Test that checkboxes are found/not found correctly by ID, when using
523 // TRUE or FALSE to match their 'checked' state.
524 $this->assertFieldById('edit-checkbox-enabled', TRUE);
525 $this->assertFieldById('edit-checkbox-disabled', FALSE);
526 $this->assertNoFieldById('edit-checkbox-enabled', FALSE);
527 $this->assertNoFieldById('edit-checkbox-disabled', TRUE);
529 // Test that checkboxes are found by ID, when using NULL to ignore the
531 $this->assertFieldById('edit-checkbox-enabled', NULL);
532 $this->assertFieldById('edit-checkbox-disabled', NULL);
534 // Test that checkboxes are found by ID when passing no second parameter.
535 $this->assertFieldById('edit-checkbox-enabled');
536 $this->assertFieldById('edit-checkbox-disabled');
538 // Test that we have legacy support.
539 $this->assertFieldById('edit-checkbox-enabled', '1');
540 $this->assertFieldById('edit-checkbox-disabled', '');
542 // Test that the assertion fails correctly when using NULL to ignore state.
544 $this->assertNoFieldById('edit-checkbox-disabled', NULL);
545 $this->fail('The "edit-checkbox-disabled" field was not found by ID, using NULL value.');
547 catch (ExpectationException $e) {
548 $this->pass('assertNoFieldById failed correctly. The "edit-checkbox-disabled" field was found by ID using NULL value.');
551 // Part 3 - Test the specific 'checked' assertions.
552 $this->assertFieldChecked('edit-checkbox-enabled');
553 $this->assertNoFieldChecked('edit-checkbox-disabled');
555 // Test that the assertion fails correctly with non-existant field id.
557 $this->assertNoFieldChecked('incorrect_checkbox_id');
558 $this->fail('The "incorrect_checkbox_id" field was found');
560 catch (ExpectationException $e) {
561 $this->pass('assertNoFieldChecked correctly failed. The "incorrect_checkbox_id" field was not found.');
564 // Test that the assertion fails correctly for a checkbox that is checked.
566 $this->assertNoFieldChecked('edit-checkbox-enabled');
567 $this->fail('The "edit-checkbox-enabled" field was not found in a checked state.');
569 catch (ExpectationException $e) {
570 $this->pass('assertNoFieldChecked correctly failed. The "edit-checkbox-enabled" field was found in a checked state.');
573 // Test that the assertion fails correctly for a checkbox that is not
576 $this->assertFieldChecked('edit-checkbox-disabled');
577 $this->fail('The "edit-checkbox-disabled" field was found and checked.');
579 catch (ExpectationException $e) {
580 $this->pass('assertFieldChecked correctly failed. The "edit-checkbox-disabled" field was not found in a checked state.');
585 * Tests the ::cronRun() method.
587 public function testCronRun() {
588 $last_cron_time = \Drupal::state()->get('system.cron_last');
590 $this->assertSession()->statusCodeEquals(204);
591 $next_cron_time = \Drupal::state()->get('system.cron_last');
593 $this->assertGreaterThan($last_cron_time, $next_cron_time);
597 * Tests the Drupal install done in \Drupal\Tests\BrowserTestBase::setUp().
599 public function testInstall() {
600 $htaccess_filename = $this->tempFilesDirectory . '/.htaccess';
601 $this->assertTrue(file_exists($htaccess_filename), "$htaccess_filename exists");
605 * Tests the assumption that local time is in 'Australia/Sydney'.
607 public function testLocalTimeZone() {
608 // The 'Australia/Sydney' time zone is set in core/tests/bootstrap.php
609 $this->assertEquals('Australia/Sydney', date_default_timezone_get());
611 // The 'Australia/Sydney' time zone is also set in
612 // FunctionalTestSetupTrait::initConfig().
613 $config_factory = $this->container->get('config.factory');
614 $value = $config_factory->get('system.date')->get('timezone.default');
615 $this->assertEquals('Australia/Sydney', $value);
619 * Tests the ::checkForMetaRefresh() method.
621 public function testCheckForMetaRefresh() {
622 // Disable following redirects in the client.
623 $this->getSession()->getDriver()->getClient()->followRedirects(FALSE);
624 // Set the maximumMetaRefreshCount to zero to make sure the redirect doesn't
625 // happen when doing a drupalGet.
626 $this->maximumMetaRefreshCount = 0;
627 $this->drupalGet('test-meta-refresh');
628 $this->assertNotEmpty($this->cssSelect('meta[http-equiv="refresh"]'));
629 // Allow one redirect to happen.
630 $this->maximumMetaRefreshCount = 1;
631 $this->checkForMetaRefresh();
632 // Check that we are now on the test page.
633 $this->assertSession()->pageTextContains('Test page text.');
636 public function testGetDefaultDriveInstance() {
637 putenv('MINK_DRIVER_ARGS=' . json_encode([NULL, ['key1' => ['key2' => ['key3' => 3, 'key3.1' => 3.1]]]]));
638 $this->getDefaultDriverInstance();
639 $this->assertEquals([NULL, ['key1' => ['key2' => ['key3' => 3, 'key3.1' => 3.1]]]], $this->minkDefaultDriverArgs);