comparison core/tests/Drupal/FunctionalTests/BrowserTestBaseUserAgentTest.php @ 18:af1871eacc83

Update to Drupal core 8.7.1
author Chris Cannam
date Thu, 09 May 2019 15:33:08 +0100
parents
children
comparison
equal deleted inserted replaced
17:129ea1e6d783 18:af1871eacc83
1 <?php
2
3 namespace Drupal\FunctionalTests;
4
5 use Drupal\Tests\BrowserTestBase;
6
7 /**
8 * Tests BrowserTestBase functionality.
9 *
10 * @group browsertestbase
11 */
12 class BrowserTestBaseUserAgentTest extends BrowserTestBase {
13
14 /**
15 * The user agent string to use.
16 *
17 * @var string
18 */
19 protected $agent;
20
21 /**
22 * Test validation of the User-Agent header we use to perform test requests.
23 */
24 public function testUserAgentValidation() {
25 $assert_session = $this->assertSession();
26 $system_path = $this->buildUrl(drupal_get_path('module', 'system'));
27 $http_path = $system_path . '/tests/http.php/user/login';
28 $https_path = $system_path . '/tests/https.php/user/login';
29 // Generate a valid simpletest User-Agent to pass validation.
30 $this->assertTrue(preg_match('/test\d+/', $this->databasePrefix, $matches), 'Database prefix contains test prefix.');
31 $this->agent = drupal_generate_test_ua($matches[0]);
32
33 // Test pages only available for testing.
34 $this->drupalGet($http_path);
35 $assert_session->statusCodeEquals(200);
36 $this->drupalGet($https_path);
37 $assert_session->statusCodeEquals(200);
38
39 // Now slightly modify the HMAC on the header, which should not validate.
40 $this->agent = 'X';
41 $this->drupalGet($http_path);
42 $assert_session->statusCodeEquals(403);
43 $this->drupalGet($https_path);
44 $assert_session->statusCodeEquals(403);
45
46 // Use a real User-Agent and verify that the special files http.php and
47 // https.php can't be accessed.
48 $this->agent = 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.6; en-US; rv:1.9.2.12) Gecko/20101026 Firefox/3.6.12';
49 $this->drupalGet($http_path);
50 $assert_session->statusCodeEquals(403);
51 $this->drupalGet($https_path);
52 $assert_session->statusCodeEquals(403);
53 }
54
55 /**
56 * {@inheritdoc}
57 */
58 protected function prepareRequest() {
59 $session = $this->getSession();
60 if ($this->agent) {
61 $session->setCookie('SIMPLETEST_USER_AGENT', $this->agent);
62 }
63 else {
64 $session->setCookie('SIMPLETEST_USER_AGENT', drupal_generate_test_ua($this->databasePrefix));
65 }
66 }
67
68 }