Version 1
[yaffs-website] / web / core / modules / simpletest / src / Tests / SimpleTestBrowserTest.php
1 <?php
2
3 namespace Drupal\simpletest\Tests;
4
5 use Drupal\Core\Url;
6 use Drupal\simpletest\WebTestBase;
7 use Drupal\Tests\simpletest\Functional\ThroughUITest;
8
9 /**
10  * Tests the Simpletest UI internal browser.
11  *
12  * @group simpletest
13  */
14 class SimpleTestBrowserTest extends WebTestBase {
15
16   /**
17    * Modules to enable.
18    *
19    * @var array
20    */
21   public static $modules = ['simpletest', 'test_page_test'];
22
23   protected function setUp() {
24     parent::setUp();
25     // Create and log in an admin user.
26     $this->drupalLogin($this->drupalCreateUser(['administer unit tests']));
27   }
28
29   /**
30    * Test the internal browsers functionality.
31    */
32   public function testInternalBrowser() {
33     // Retrieve the test page and check its title and headers.
34     $this->drupalGet('test-page');
35     $this->assertTrue($this->drupalGetHeader('Date'), 'An HTTP header was received.');
36     $this->assertTitle(t('Test page | @site-name', [
37       '@site-name' => $this->config('system.site')->get('name'),
38     ]));
39     $this->assertNoTitle('Foo');
40
41     $old_user_id = $this->container->get('current_user')->id();
42     $user = $this->drupalCreateUser();
43     $this->drupalLogin($user);
44     // Check that current user service updated.
45     $this->assertNotEqual($old_user_id, $this->container->get('current_user')->id(), 'Current user service updated.');
46     $headers = $this->drupalGetHeaders(TRUE);
47     $this->assertEqual(count($headers), 2, 'There was one intermediate request.');
48     $this->assertTrue(strpos($headers[0][':status'], '303') !== FALSE, 'Intermediate response code was 303.');
49     $this->assertFalse(empty($headers[0]['location']), 'Intermediate request contained a Location header.');
50     $this->assertEqual($this->getUrl(), $headers[0]['location'], 'HTTP redirect was followed');
51     $this->assertFalse($this->drupalGetHeader('Location'), 'Headers from intermediate request were reset.');
52     $this->assertResponse(200, 'Response code from intermediate request was reset.');
53
54     $this->drupalLogout();
55     // Check that current user service updated to anonymous user.
56     $this->assertEqual(0, $this->container->get('current_user')->id(), 'Current user service updated.');
57
58     // Test the maximum redirection option.
59     $this->maximumRedirects = 1;
60     $edit = [
61       'name' => $user->getUsername(),
62       'pass' => $user->pass_raw
63     ];
64     $this->drupalPostForm('user/login', $edit, t('Log in'), [
65       'query' => ['destination' => 'user/logout'],
66     ]);
67     $headers = $this->drupalGetHeaders(TRUE);
68     $this->assertEqual(count($headers), 2, 'Simpletest stopped following redirects after the first one.');
69
70     // Remove the Simpletest private key file so we can test the protection
71     // against requests that forge a valid testing user agent to gain access
72     // to the installer.
73     // @see drupal_valid_test_ua()
74     // Not using File API; a potential error must trigger a PHP warning.
75     unlink($this->siteDirectory . '/.htkey');
76     $this->drupalGet(Url::fromUri('base:core/install.php', ['external' => TRUE, 'absolute' => TRUE])->toString());
77     $this->assertResponse(403, 'Cannot access install.php.');
78   }
79
80   /**
81    * Test validation of the User-Agent header we use to perform test requests.
82    */
83   public function testUserAgentValidation() {
84     global $base_url;
85
86     // Logout the user which was logged in during test-setup.
87     $this->drupalLogout();
88
89     $system_path = $base_url . '/' . drupal_get_path('module', 'system');
90     $HTTP_path = $system_path . '/tests/http.php/user/login';
91     $https_path = $system_path . '/tests/https.php/user/login';
92     // Generate a valid simpletest User-Agent to pass validation.
93     $this->assertTrue(preg_match('/test\d+/', $this->databasePrefix, $matches), 'Database prefix contains test prefix.');
94     $test_ua = drupal_generate_test_ua($matches[0]);
95     $this->additionalCurlOptions = [CURLOPT_USERAGENT => $test_ua];
96
97     // Test pages only available for testing.
98     $this->drupalGet($HTTP_path);
99     $this->assertResponse(200, 'Requesting http.php with a legitimate simpletest User-Agent returns OK.');
100     $this->drupalGet($https_path);
101     $this->assertResponse(200, 'Requesting https.php with a legitimate simpletest User-Agent returns OK.');
102
103     // Now slightly modify the HMAC on the header, which should not validate.
104     $this->additionalCurlOptions = [CURLOPT_USERAGENT => $test_ua . 'X'];
105     $this->drupalGet($HTTP_path);
106     $this->assertResponse(403, 'Requesting http.php with a bad simpletest User-Agent fails.');
107     $this->drupalGet($https_path);
108     $this->assertResponse(403, 'Requesting https.php with a bad simpletest User-Agent fails.');
109
110     // Use a real User-Agent and verify that the special files http.php and
111     // https.php can't be accessed.
112     $this->additionalCurlOptions = [CURLOPT_USERAGENT => 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.6; en-US; rv:1.9.2.12) Gecko/20101026 Firefox/3.6.12'];
113     $this->drupalGet($HTTP_path);
114     $this->assertResponse(403, 'Requesting http.php with a normal User-Agent fails.');
115     $this->drupalGet($https_path);
116     $this->assertResponse(403, 'Requesting https.php with a normal User-Agent fails.');
117   }
118
119   /**
120    * Tests that PHPUnit and KernelTestBase tests work through the UI.
121    */
122   public function testTestingThroughUI() {
123     $this->drupalGet('admin/config/development/testing');
124     $this->assertTrue(strpos($this->drupalSettings['simpleTest']['images'][0], 'core/misc/menu-collapsed.png') > 0, 'drupalSettings contains a link to core/misc/menu-collapsed.png.');
125     // We can not test WebTestBase tests here since they require a valid .htkey
126     // to be created. However this scenario is covered by the testception of
127     // \Drupal\simpletest\Tests\SimpleTestTest.
128
129     $tests = [
130       // A KernelTestBase test.
131       'Drupal\KernelTests\KernelTestBaseTest',
132       // A PHPUnit unit test.
133       'Drupal\Tests\action\Unit\Menu\ActionLocalTasksTest',
134       // A PHPUnit functional test.
135       ThroughUITest::class,
136     ];
137
138     foreach ($tests as $test) {
139       $this->drupalGet('admin/config/development/testing');
140       $edit = [
141         "tests[$test]" => TRUE,
142       ];
143       $this->drupalPostForm(NULL, $edit, t('Run tests'));
144       $this->assertText('0 fails, 0 exceptions');
145     }
146   }
147
148 }