3 namespace Drupal\FunctionalTests;
5 use Behat\Mink\Exception\ExpectationException;
6 use Drupal\Component\Utility\Html;
8 use Drupal\Tests\BrowserTestBase;
9 use Drupal\Tests\Traits\Core\CronRunTrait;
12 * Tests BrowserTestBase functionality.
14 * @group browsertestbase
16 class BrowserTestBaseTest extends BrowserTestBase {
25 public static $modules = ['test_page_test', 'form_test', 'system_test'];
28 * Tests basic page test.
30 public function testGoTo() {
31 $account = $this->drupalCreateUser();
32 $this->drupalLogin($account);
34 // Visit a Drupal page that requires login.
35 $this->drupalGet('test-page');
36 $this->assertSession()->statusCodeEquals(200);
38 // Test page contains some text.
39 $this->assertSession()->pageTextContains('Test page text.');
41 // Check that returned plain text is correct.
42 $text = $this->getTextContent();
43 $this->assertContains('Test page text.', $text);
44 $this->assertNotContains('</html>', $text);
46 // Response includes cache tags that we can assert.
47 $this->assertSession()->responseHeaderEquals('X-Drupal-Cache-Tags', 'http_response rendered');
49 // Test that we can read the JS settings.
50 $js_settings = $this->getDrupalSettings();
51 $this->assertSame('azAZ09();.,\\\/-_{}', $js_settings['test-setting']);
53 // Test drupalGet with a url object.
54 $url = Url::fromRoute('test_page_test.render_title');
55 $this->drupalGet($url);
56 $this->assertSession()->statusCodeEquals(200);
58 // Test page contains some text.
59 $this->assertSession()->pageTextContains('Hello Drupal');
61 // Test that setting headers with drupalGet() works.
62 $this->drupalGet('system-test/header', [], [
63 'Test-Header' => 'header value',
65 $returned_header = $this->getSession()->getResponseHeader('Test-Header');
66 $this->assertSame('header value', $returned_header);
70 * Tests basic form functionality.
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);
77 // Ensure the form and text field exist.
78 $this->assertSession()->elementExists('css', 'form#form-test-form-test-object');
79 $this->assertSession()->fieldExists('bananas');
81 // Check that the hidden field exists and has a specific value.
82 $this->assertSession()->hiddenFieldExists('strawberry');
83 $this->assertSession()->hiddenFieldExists('red');
84 $this->assertSession()->hiddenFieldExists('redstrawberryhiddenfield');
85 $this->assertSession()->hiddenFieldValueNotEquals('strawberry', 'brown');
86 $this->assertSession()->hiddenFieldValueEquals('strawberry', 'red');
88 // Check that a hidden field does not exist.
89 $this->assertSession()->hiddenFieldNotExists('bananas');
90 $this->assertSession()->hiddenFieldNotExists('pineapple');
92 $edit = ['bananas' => 'green'];
93 $this->submitForm($edit, 'Save', 'form-test-form-test-object');
95 $config_factory = $this->container->get('config.factory');
96 $value = $config_factory->get('form_test.object')->get('bananas');
97 $this->assertSame('green', $value);
99 // Test drupalPostForm().
100 $edit = ['bananas' => 'red'];
101 $this->drupalPostForm('form-test/object-builder', $edit, 'Save');
102 $value = $config_factory->get('form_test.object')->get('bananas');
103 $this->assertSame('red', $value);
105 $this->drupalPostForm('form-test/object-builder', NULL, 'Save');
106 $value = $config_factory->get('form_test.object')->get('bananas');
107 $this->assertSame('', $value);
111 * Tests clickLink() functionality.
113 public function testClickLink() {
114 $this->drupalGet('test-page');
115 $this->clickLink('Visually identical test links');
116 $this->assertContains('user/login', $this->getSession()->getCurrentUrl());
117 $this->drupalGet('test-page');
118 $this->clickLink('Visually identical test links', 0);
119 $this->assertContains('user/login', $this->getSession()->getCurrentUrl());
120 $this->drupalGet('test-page');
121 $this->clickLink('Visually identical test links', 1);
122 $this->assertContains('user/register', $this->getSession()->getCurrentUrl());
125 public function testError() {
126 $this->setExpectedException('\Exception', 'User notice: foo');
127 $this->drupalGet('test-error');
131 * Tests linkExists() with pipe character (|) in locator.
133 * @see \Drupal\Tests\WebAssert::linkExists()
135 public function testPipeCharInLocator() {
136 $this->drupalGet('test-pipe-char');
137 $this->assertSession()->linkExists('foo|bar|baz');
141 * Tests legacy text asserts.
143 public function testLegacyTextAsserts() {
144 $this->drupalGet('test-encoded');
145 $dangerous = 'Bad html <script>alert(123);</script>';
146 $sanitized = Html::escape($dangerous);
147 $this->assertNoText($dangerous);
148 $this->assertText($sanitized);
150 // Test getRawContent().
151 $this->assertSame($this->getSession()->getPage()->getContent(), $this->getRawContent());
155 * Tests legacy field asserts which use xpath directly.
157 public function testLegacyXpathAsserts() {
158 $this->drupalGet('test-field-xpath');
159 $this->assertFieldsByValue($this->xpath("//h1[@class = 'page-title']"), NULL);
160 $this->assertFieldsByValue($this->xpath('//table/tbody/tr[2]/td[1]'), 'one');
161 $this->assertFieldByXPath('//table/tbody/tr[2]/td[1]', 'one');
163 $this->assertFieldsByValue($this->xpath("//input[@id = 'edit-name']"), 'Test name');
164 $this->assertFieldByXPath("//input[@id = 'edit-name']", 'Test name');
165 $this->assertFieldsByValue($this->xpath("//select[@id = 'edit-options']"), '2');
166 $this->assertFieldByXPath("//select[@id = 'edit-options']", '2');
168 $this->assertNoFieldByXPath('//notexisting');
169 $this->assertNoFieldByXPath("//input[@id = 'edit-name']", 'wrong value');
171 // Test that the assertion fails correctly.
173 $this->assertFieldByXPath("//input[@id = 'notexisting']");
174 $this->fail('The "notexisting" field was found.');
176 catch (\PHPUnit_Framework_ExpectationFailedException $e) {
177 $this->pass('assertFieldByXPath correctly failed. The "notexisting" field was not found.');
181 $this->assertNoFieldByXPath("//input[@id = 'edit-name']");
182 $this->fail('The "edit-name" field was not found.');
184 catch (\PHPUnit_Framework_ExpectationFailedException $e) {
185 $this->pass('assertNoFieldByXPath correctly failed. The "edit-name" field was found.');
189 $this->assertFieldsByValue($this->xpath("//input[@id = 'edit-name']"), 'not the value');
190 $this->fail('The "edit-name" field is found with the value "not the value".');
192 catch (\PHPUnit_Framework_ExpectationFailedException $e) {
193 $this->pass('The "edit-name" field is not found with the value "not the value".');
198 * Tests legacy field asserts using textfields.
200 public function testLegacyFieldAssertsWithTextfields() {
201 $this->drupalGet('test-field-xpath');
203 // *** 1. assertNoField().
204 $this->assertNoField('invalid_name_and_id');
206 // Test that the assertion fails correctly when searching by name.
208 $this->assertNoField('name');
209 $this->fail('The "name" field was not found based on name.');
211 catch (ExpectationException $e) {
212 $this->pass('assertNoField correctly failed. The "name" field was found by name.');
215 // Test that the assertion fails correctly when searching by id.
217 $this->assertNoField('edit-name');
218 $this->fail('The "name" field was not found based on id.');
220 catch (ExpectationException $e) {
221 $this->pass('assertNoField correctly failed. The "name" field was found by id.');
224 // *** 2. assertField().
225 $this->assertField('name');
226 $this->assertField('edit-name');
228 // Test that the assertion fails correctly if the field does not exist.
230 $this->assertField('invalid_name_and_id');
231 $this->fail('The "invalid_name_and_id" field was found.');
233 catch (ExpectationException $e) {
234 $this->pass('assertField correctly failed. The "invalid_name_and_id" field was not found.');
237 // *** 3. assertNoFieldById().
238 $this->assertNoFieldById('name');
239 $this->assertNoFieldById('name', 'not the value');
240 $this->assertNoFieldById('notexisting');
241 $this->assertNoFieldById('notexisting', NULL);
243 // Test that the assertion fails correctly if no value is passed in.
245 $this->assertNoFieldById('edit-description');
246 $this->fail('The "description" field, with no value was not found.');
248 catch (ExpectationException $e) {
249 $this->pass('The "description" field, with no value was found.');
252 // Test that the assertion fails correctly if a NULL value is passed in.
254 $this->assertNoFieldById('edit-name', NULL);
255 $this->fail('The "name" field was not found.');
257 catch (ExpectationException $e) {
258 $this->pass('The "name" field was found.');
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');
267 // Test that the assertion fails correctly if no value is passed in.
269 $this->assertFieldById('edit-name');
270 $this->fail('The "edit-name" field with no value was found.');
272 catch (\PHPUnit_Framework_ExpectationFailedException $e) {
273 $this->pass('The "edit-name" field with no value was not found.');
276 // Test that the assertion fails correctly if the wrong value is passed in.
278 $this->assertFieldById('edit-name', 'not the value');
279 $this->fail('The "name" field was found, using the wrong value.');
281 catch (\PHPUnit_Framework_ExpectationFailedException $e) {
282 $this->pass('The "name" field was not found, using the wrong value.');
285 // *** 5. assertNoFieldByName().
286 $this->assertNoFieldByName('name');
287 $this->assertNoFieldByName('name', 'not the value');
288 $this->assertNoFieldByName('notexisting');
289 $this->assertNoFieldByName('notexisting', NULL);
291 // Test that the assertion fails correctly if no value is passed in.
293 $this->assertNoFieldByName('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->assertNoFieldByName('name', NULL);
303 $this->fail('The "name" field was not found.');
305 catch (ExpectationException $e) {
306 $this->pass('The "name" field was found.');
309 // *** 6. assertFieldByName().
310 $this->assertFieldByName('name');
311 $this->assertFieldByName('name', NULL);
312 $this->assertFieldByName('name', 'Test name');
313 $this->assertFieldByName('description');
314 $this->assertFieldByName('description', '');
315 $this->assertFieldByName('description', NULL);
317 // Test that the assertion fails correctly if given the wrong name.
319 $this->assertFieldByName('non-existing-name');
320 $this->fail('The "non-existing-name" field was found.');
322 catch (ExpectationException $e) {
323 $this->pass('The "non-existing-name" field was not found');
326 // Test that the assertion fails correctly if given the wrong value.
328 $this->assertFieldByName('name', 'not the value');
329 $this->fail('The "name" field with incorrect value was found.');
331 catch (ExpectationException $e) {
332 $this->pass('assertFieldByName correctly failed. The "name" field with incorrect value was not found.');
337 * Tests legacy field asserts on other types of field.
339 public function testLegacyFieldAssertsWithNonTextfields() {
340 $this->drupalGet('test-field-xpath');
342 // Option field type.
343 $this->assertOptionByText('options', 'one');
345 $this->assertOptionByText('options', 'four');
346 $this->fail('The select option "four" was found.');
348 catch (ExpectationException $e) {
349 $this->pass($e->getMessage());
352 $this->assertOption('options', 1);
354 $this->assertOption('options', 4);
355 $this->fail('The select option "4" was found.');
357 catch (ExpectationException $e) {
358 $this->pass($e->getMessage());
361 $this->assertNoOption('options', 'non-existing');
363 $this->assertNoOption('options', 'one');
364 $this->fail('The select option "one" was not found.');
366 catch (ExpectationException $e) {
367 $this->pass($e->getMessage());
370 $this->assertOptionSelected('options', 2);
372 $this->assertOptionSelected('options', 4);
373 $this->fail('The select option "4" was selected.');
375 catch (ExpectationException $e) {
376 $this->pass($e->getMessage());
380 $this->assertOptionSelected('options', 1);
381 $this->fail('The select option "1" was selected.');
383 catch (\PHPUnit_Framework_ExpectationFailedException $e) {
384 $this->pass($e->getMessage());
387 // Button field type.
388 $this->assertFieldById('edit-save', NULL);
389 // Test that the assertion fails correctly if the field value is passed in
390 // rather than the id.
392 $this->assertFieldById('Save', NULL);
393 $this->fail('The field with id of "Save" was found.');
395 catch (ExpectationException $e) {
396 $this->pass($e->getMessage());
399 $this->assertNoFieldById('Save', NULL);
400 // Test that the assertion fails correctly if the id of an actual field is
403 $this->assertNoFieldById('edit-save', NULL);
404 $this->fail('The field with id of "edit-save" was not found.');
406 catch (ExpectationException $e) {
407 $this->pass($e->getMessage());
410 // Checkbox field type.
411 // Test that checkboxes are found/not found correctly by name, when using
412 // TRUE or FALSE to match their 'checked' state.
413 $this->assertFieldByName('checkbox_enabled', TRUE);
414 $this->assertFieldByName('checkbox_disabled', FALSE);
415 $this->assertNoFieldByName('checkbox_enabled', FALSE);
416 $this->assertNoFieldByName('checkbox_disabled', TRUE);
418 // Test that checkboxes are found by name when using NULL to ignore the
420 $this->assertFieldByName('checkbox_enabled', NULL);
421 $this->assertFieldByName('checkbox_disabled', NULL);
423 // Test that checkboxes are found/not found correctly by ID, when using
424 // TRUE or FALSE to match their 'checked' state.
425 $this->assertFieldById('edit-checkbox-enabled', TRUE);
426 $this->assertFieldById('edit-checkbox-disabled', FALSE);
427 $this->assertNoFieldById('edit-checkbox-enabled', FALSE);
428 $this->assertNoFieldById('edit-checkbox-disabled', TRUE);
430 // Test that checkboxes are found by by ID, when using NULL to ignore the
432 $this->assertFieldById('edit-checkbox-enabled', NULL);
433 $this->assertFieldById('edit-checkbox-disabled', NULL);
435 // Test that the assertion fails correctly when using NULL to ignore state.
437 $this->assertNoFieldByName('checkbox_enabled', NULL);
438 $this->fail('The "checkbox_enabled" field was not found by name, using NULL value.');
440 catch (ExpectationException $e) {
441 $this->pass('assertNoFieldByName failed correctly. The "checkbox_enabled" field was found using NULL value.');
444 // Test that the assertion fails correctly when using NULL to ignore state.
446 $this->assertNoFieldById('edit-checkbox-disabled', NULL);
447 $this->fail('The "edit-checkbox-disabled" field was not found by ID, using NULL value.');
449 catch (ExpectationException $e) {
450 $this->pass('assertNoFieldById failed correctly. The "edit-checkbox-disabled" field was found by ID using NULL value.');
453 // Test the specific 'checked' assertions.
454 $this->assertFieldChecked('edit-checkbox-enabled');
455 $this->assertNoFieldChecked('edit-checkbox-disabled');
457 // Test that the assertion fails correctly with non-existant field id.
459 $this->assertNoFieldChecked('incorrect_checkbox_id');
460 $this->fail('The "incorrect_checkbox_id" field was found');
462 catch (ExpectationException $e) {
463 $this->pass('assertNoFieldChecked correctly failed. The "incorrect_checkbox_id" field was not found.');
466 // Test that the assertion fails correctly for a checkbox that is checked.
468 $this->assertNoFieldChecked('edit-checkbox-enabled');
469 $this->fail('The "edit-checkbox-enabled" field was not found in a checked state.');
471 catch (ExpectationException $e) {
472 $this->pass('assertNoFieldChecked correctly failed. The "edit-checkbox-enabled" field was found in a checked state.');
475 // Test that the assertion fails correctly for a checkbox that is not
478 $this->assertFieldChecked('edit-checkbox-disabled');
479 $this->fail('The "edit-checkbox-disabled" field was found and checked.');
481 catch (ExpectationException $e) {
482 $this->pass('assertFieldChecked correctly failed. The "edit-checkbox-disabled" field was not found in a checked state.');
487 * Tests the ::cronRun() method.
489 public function testCronRun() {
490 $last_cron_time = \Drupal::state()->get('system.cron_last');
492 $this->assertSession()->statusCodeEquals(204);
493 $next_cron_time = \Drupal::state()->get('system.cron_last');
495 $this->assertGreaterThan($last_cron_time, $next_cron_time);
499 * Tests the Drupal install done in \Drupal\Tests\BrowserTestBase::setUp().
501 public function testInstall() {
502 $htaccess_filename = $this->tempFilesDirectory . '/.htaccess';
503 $this->assertTrue(file_exists($htaccess_filename), "$htaccess_filename exists");
507 * Tests the assumption that local time is in 'Australia/Sydney'.
509 public function testLocalTimeZone() {
510 // The 'Australia/Sydney' time zone is set in core/tests/bootstrap.php
511 $this->assertEquals('Australia/Sydney', date_default_timezone_get());
513 // The 'Australia/Sydney' time zone is also set in
514 // FunctionalTestSetupTrait::initConfig().
515 $config_factory = $this->container->get('config.factory');
516 $value = $config_factory->get('system.date')->get('timezone.default');
517 $this->assertEquals('Australia/Sydney', $value);