Upgraded drupal core with security updates
[yaffs-website] / web / core / tests / Drupal / FunctionalTests / BrowserTestBaseTest.php
1 <?php
2
3 namespace Drupal\FunctionalTests;
4
5 use Behat\Mink\Exception\ExpectationException;
6 use Drupal\Component\Utility\Html;
7 use Drupal\Core\Url;
8 use Drupal\Tests\BrowserTestBase;
9 use Drupal\Tests\Traits\Core\CronRunTrait;
10
11 /**
12  * Tests BrowserTestBase functionality.
13  *
14  * @group browsertestbase
15  */
16 class BrowserTestBaseTest extends BrowserTestBase {
17
18   use CronRunTrait;
19
20   /**
21    * Modules to enable.
22    *
23    * @var array
24    */
25   public static $modules = ['test_page_test', 'form_test', 'system_test'];
26
27   /**
28    * Tests basic page test.
29    */
30   public function testGoTo() {
31     $account = $this->drupalCreateUser();
32     $this->drupalLogin($account);
33
34     // Visit a Drupal page that requires login.
35     $this->drupalGet('test-page');
36     $this->assertSession()->statusCodeEquals(200);
37
38     // Test page contains some text.
39     $this->assertSession()->pageTextContains('Test page text.');
40
41     // Check that returned plain text is correct.
42     $text = $this->getTextContent();
43     $this->assertContains('Test page text.', $text);
44     $this->assertNotContains('</html>', $text);
45
46     // Response includes cache tags that we can assert.
47     $this->assertSession()->responseHeaderEquals('X-Drupal-Cache-Tags', 'http_response rendered');
48
49     // Test that we can read the JS settings.
50     $js_settings = $this->getDrupalSettings();
51     $this->assertSame('azAZ09();.,\\\/-_{}', $js_settings['test-setting']);
52
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);
57
58     // Test page contains some text.
59     $this->assertSession()->pageTextContains('Hello Drupal');
60
61     // Test that setting headers with drupalGet() works.
62     $this->drupalGet('system-test/header', [], [
63       'Test-Header' => 'header value',
64     ]);
65     $returned_header = $this->getSession()->getResponseHeader('Test-Header');
66     $this->assertSame('header value', $returned_header);
67   }
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
77     // Ensure the form and text field exist.
78     $this->assertSession()->elementExists('css', 'form#form-test-form-test-object');
79     $this->assertSession()->fieldExists('bananas');
80
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');
87
88     // Check that a hidden field does not exist.
89     $this->assertSession()->hiddenFieldNotExists('bananas');
90     $this->assertSession()->hiddenFieldNotExists('pineapple');
91
92     $edit = ['bananas' => 'green'];
93     $this->submitForm($edit, 'Save', 'form-test-form-test-object');
94
95     $config_factory = $this->container->get('config.factory');
96     $value = $config_factory->get('form_test.object')->get('bananas');
97     $this->assertSame('green', $value);
98
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);
104
105     $this->drupalPostForm('form-test/object-builder', NULL, 'Save');
106     $value = $config_factory->get('form_test.object')->get('bananas');
107     $this->assertSame('', $value);
108   }
109
110   /**
111    * Tests clickLink() functionality.
112    */
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());
123   }
124
125   public function testError() {
126     $this->setExpectedException('\Exception', 'User notice: foo');
127     $this->drupalGet('test-error');
128   }
129
130   /**
131    * Tests linkExists() with pipe character (|) in locator.
132    *
133    * @see \Drupal\Tests\WebAssert::linkExists()
134    */
135   public function testPipeCharInLocator() {
136     $this->drupalGet('test-pipe-char');
137     $this->assertSession()->linkExists('foo|bar|baz');
138   }
139
140   /**
141    * Tests legacy text asserts.
142    */
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);
149
150     // Test getRawContent().
151     $this->assertSame($this->getSession()->getPage()->getContent(), $this->getRawContent());
152   }
153
154   /**
155    * Tests legacy field asserts which use xpath directly.
156    */
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');
162
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');
167
168     $this->assertNoFieldByXPath('//notexisting');
169     $this->assertNoFieldByXPath("//input[@id = 'edit-name']", 'wrong value');
170
171     // Test that the assertion fails correctly.
172     try {
173       $this->assertFieldByXPath("//input[@id = 'notexisting']");
174       $this->fail('The "notexisting" field was found.');
175     }
176     catch (\PHPUnit_Framework_ExpectationFailedException $e) {
177       $this->pass('assertFieldByXPath correctly failed. The "notexisting" field was not found.');
178     }
179
180     try {
181       $this->assertNoFieldByXPath("//input[@id = 'edit-name']");
182       $this->fail('The "edit-name" field was not found.');
183     }
184     catch (\PHPUnit_Framework_ExpectationFailedException $e) {
185       $this->pass('assertNoFieldByXPath correctly failed. The "edit-name" field was found.');
186     }
187
188     try {
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".');
191     }
192     catch (\PHPUnit_Framework_ExpectationFailedException $e) {
193       $this->pass('The "edit-name" field is not found with the value "not the value".');
194     }
195   }
196
197   /**
198    * Tests legacy field asserts using textfields.
199    */
200   public function testLegacyFieldAssertsWithTextfields() {
201     $this->drupalGet('test-field-xpath');
202
203     // *** 1. assertNoField().
204     $this->assertNoField('invalid_name_and_id');
205
206     // Test that the assertion fails correctly when searching by name.
207     try {
208       $this->assertNoField('name');
209       $this->fail('The "name" field was not found based on name.');
210     }
211     catch (ExpectationException $e) {
212       $this->pass('assertNoField correctly failed. The "name" field was found by name.');
213     }
214
215     // Test that the assertion fails correctly when searching by id.
216     try {
217       $this->assertNoField('edit-name');
218       $this->fail('The "name" field was not found based on id.');
219     }
220     catch (ExpectationException $e) {
221       $this->pass('assertNoField correctly failed. The "name" field was found by id.');
222     }
223
224     // *** 2. assertField().
225     $this->assertField('name');
226     $this->assertField('edit-name');
227
228     // Test that the assertion fails correctly if the field does not exist.
229     try {
230       $this->assertField('invalid_name_and_id');
231       $this->fail('The "invalid_name_and_id" field was found.');
232     }
233     catch (ExpectationException $e) {
234       $this->pass('assertField correctly failed. The "invalid_name_and_id" field was not found.');
235     }
236
237     // *** 3. assertNoFieldById().
238     $this->assertNoFieldById('name');
239     $this->assertNoFieldById('name', 'not the value');
240     $this->assertNoFieldById('notexisting');
241     $this->assertNoFieldById('notexisting', NULL);
242
243     // Test that the assertion fails correctly if no value is passed in.
244     try {
245       $this->assertNoFieldById('edit-description');
246       $this->fail('The "description" field, with no value was not found.');
247     }
248     catch (ExpectationException $e) {
249       $this->pass('The "description" field, with no value was found.');
250     }
251
252     // Test that the assertion fails correctly if a NULL value is passed in.
253     try {
254       $this->assertNoFieldById('edit-name', NULL);
255       $this->fail('The "name" field was not found.');
256     }
257     catch (ExpectationException $e) {
258       $this->pass('The "name" field was found.');
259     }
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
267     // Test that the assertion fails correctly if no value is passed in.
268     try {
269       $this->assertFieldById('edit-name');
270       $this->fail('The "edit-name" field with no value was found.');
271     }
272     catch (\PHPUnit_Framework_ExpectationFailedException $e) {
273       $this->pass('The "edit-name" field with no value was not found.');
274     }
275
276     // Test that the assertion fails correctly if the wrong value is passed in.
277     try {
278       $this->assertFieldById('edit-name', 'not the value');
279       $this->fail('The "name" field was found, using the wrong value.');
280     }
281     catch (\PHPUnit_Framework_ExpectationFailedException $e) {
282       $this->pass('The "name" field was not found, using the wrong value.');
283     }
284
285     // *** 5. assertNoFieldByName().
286     $this->assertNoFieldByName('name');
287     $this->assertNoFieldByName('name', 'not the value');
288     $this->assertNoFieldByName('notexisting');
289     $this->assertNoFieldByName('notexisting', NULL);
290
291     // Test that the assertion fails correctly if no value is passed in.
292     try {
293       $this->assertNoFieldByName('description');
294       $this->fail('The "description" field, with no value was not found.');
295     }
296     catch (ExpectationException $e) {
297       $this->pass('The "description" field, with no value was found.');
298     }
299
300     // Test that the assertion fails correctly if a NULL value is passed in.
301     try {
302       $this->assertNoFieldByName('name', NULL);
303       $this->fail('The "name" field was not found.');
304     }
305     catch (ExpectationException $e) {
306       $this->pass('The "name" field was found.');
307     }
308
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);
316
317     // Test that the assertion fails correctly if given the wrong name.
318     try {
319       $this->assertFieldByName('non-existing-name');
320       $this->fail('The "non-existing-name" field was found.');
321     }
322     catch (ExpectationException $e) {
323       $this->pass('The "non-existing-name" field was not found');
324     }
325
326     // Test that the assertion fails correctly if given the wrong value.
327     try {
328       $this->assertFieldByName('name', 'not the value');
329       $this->fail('The "name" field with incorrect value was found.');
330     }
331     catch (ExpectationException $e) {
332       $this->pass('assertFieldByName correctly failed. The "name" field with incorrect value was not found.');
333     }
334   }
335
336   /**
337    * Tests legacy field asserts on other types of field.
338    */
339   public function testLegacyFieldAssertsWithNonTextfields() {
340     $this->drupalGet('test-field-xpath');
341
342     // Option field type.
343     $this->assertOptionByText('options', 'one');
344     try {
345       $this->assertOptionByText('options', 'four');
346       $this->fail('The select option "four" was found.');
347     }
348     catch (ExpectationException $e) {
349       $this->pass($e->getMessage());
350     }
351
352     $this->assertOption('options', 1);
353     try {
354       $this->assertOption('options', 4);
355       $this->fail('The select option "4" was found.');
356     }
357     catch (ExpectationException $e) {
358       $this->pass($e->getMessage());
359     }
360
361     $this->assertNoOption('options', 'non-existing');
362     try {
363       $this->assertNoOption('options', 'one');
364       $this->fail('The select option "one" was not found.');
365     }
366     catch (ExpectationException $e) {
367       $this->pass($e->getMessage());
368     }
369
370     $this->assertOptionSelected('options', 2);
371     try {
372       $this->assertOptionSelected('options', 4);
373       $this->fail('The select option "4" was selected.');
374     }
375     catch (ExpectationException $e) {
376       $this->pass($e->getMessage());
377     }
378
379     try {
380       $this->assertOptionSelected('options', 1);
381       $this->fail('The select option "1" was selected.');
382     }
383     catch (\PHPUnit_Framework_ExpectationFailedException $e) {
384       $this->pass($e->getMessage());
385     }
386
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.
391     try {
392       $this->assertFieldById('Save', NULL);
393       $this->fail('The field with id of "Save" was found.');
394     }
395     catch (ExpectationException $e) {
396       $this->pass($e->getMessage());
397     }
398
399     $this->assertNoFieldById('Save', NULL);
400     // Test that the assertion fails correctly if the id of an actual field is
401     // passed in.
402     try {
403       $this->assertNoFieldById('edit-save', NULL);
404       $this->fail('The field with id of "edit-save" was not found.');
405     }
406     catch (ExpectationException $e) {
407       $this->pass($e->getMessage());
408     }
409
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);
417
418     // Test that checkboxes are found by name when using NULL to ignore the
419     // 'checked' state.
420     $this->assertFieldByName('checkbox_enabled', NULL);
421     $this->assertFieldByName('checkbox_disabled', NULL);
422
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);
429
430     // Test that checkboxes are found by by ID, when using NULL to ignore the
431     // 'checked' state.
432     $this->assertFieldById('edit-checkbox-enabled', NULL);
433     $this->assertFieldById('edit-checkbox-disabled', NULL);
434
435     // Test that the assertion fails correctly when using NULL to ignore state.
436     try {
437       $this->assertNoFieldByName('checkbox_enabled', NULL);
438       $this->fail('The "checkbox_enabled" field was not found by name, using NULL value.');
439     }
440     catch (ExpectationException $e) {
441       $this->pass('assertNoFieldByName failed correctly. The "checkbox_enabled" field was found using NULL value.');
442     }
443
444     // Test that the assertion fails correctly when using NULL to ignore state.
445     try {
446       $this->assertNoFieldById('edit-checkbox-disabled', NULL);
447       $this->fail('The "edit-checkbox-disabled" field was not found by ID, using NULL value.');
448     }
449     catch (ExpectationException $e) {
450       $this->pass('assertNoFieldById failed correctly. The "edit-checkbox-disabled" field was found by ID using NULL value.');
451     }
452
453     // Test the specific 'checked' assertions.
454     $this->assertFieldChecked('edit-checkbox-enabled');
455     $this->assertNoFieldChecked('edit-checkbox-disabled');
456
457     // Test that the assertion fails correctly with non-existant field id.
458     try {
459       $this->assertNoFieldChecked('incorrect_checkbox_id');
460       $this->fail('The "incorrect_checkbox_id" field was found');
461     }
462     catch (ExpectationException $e) {
463       $this->pass('assertNoFieldChecked correctly failed. The "incorrect_checkbox_id" field was not found.');
464     }
465
466     // Test that the assertion fails correctly for a checkbox that is checked.
467     try {
468       $this->assertNoFieldChecked('edit-checkbox-enabled');
469       $this->fail('The "edit-checkbox-enabled" field was not found in a checked state.');
470     }
471     catch (ExpectationException $e) {
472       $this->pass('assertNoFieldChecked correctly failed. The "edit-checkbox-enabled" field was found in a checked state.');
473     }
474
475     // Test that the assertion fails correctly for a checkbox that is not
476     // checked.
477     try {
478       $this->assertFieldChecked('edit-checkbox-disabled');
479       $this->fail('The "edit-checkbox-disabled" field was found and checked.');
480     }
481     catch (ExpectationException $e) {
482       $this->pass('assertFieldChecked correctly failed. The "edit-checkbox-disabled" field was not found in a checked state.');
483     }
484   }
485
486   /**
487    * Tests the ::cronRun() method.
488    */
489   public function testCronRun() {
490     $last_cron_time = \Drupal::state()->get('system.cron_last');
491     $this->cronRun();
492     $this->assertSession()->statusCodeEquals(204);
493     $next_cron_time = \Drupal::state()->get('system.cron_last');
494
495     $this->assertGreaterThan($last_cron_time, $next_cron_time);
496   }
497
498   /**
499    * Tests the Drupal install done in \Drupal\Tests\BrowserTestBase::setUp().
500    */
501   public function testInstall() {
502     $htaccess_filename = $this->tempFilesDirectory . '/.htaccess';
503     $this->assertTrue(file_exists($htaccess_filename), "$htaccess_filename exists");
504   }
505
506   /**
507    * Tests the assumption that local time is in 'Australia/Sydney'.
508    */
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());
512
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);
518   }
519
520 }