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