Further Drupal 8.6.4 changes. Some core files were not committed before a commit...
[yaffs-website] / web / core / tests / Drupal / Tests / Core / Session / SessionConfigurationTest.php
1 <?php
2
3 namespace Drupal\Tests\Core\Session;
4
5 use Drupal\Tests\UnitTestCase;
6 use Symfony\Component\HttpFoundation\Request;
7
8 /**
9  * @coversDefaultClass \Drupal\Core\Session\SessionConfiguration
10  * @group Session
11  */
12 class SessionConfigurationTest extends UnitTestCase {
13
14   /**
15    * Constructs a partially mocked SUT.
16    *
17    * @returns \Drupal\Core\Session\SessionConfiguration|\PHPUnit_Framework_MockObject_MockObject
18    */
19   protected function createSessionConfiguration($options = []) {
20     return $this->getMock('Drupal\Core\Session\SessionConfiguration', ['drupalValidTestUa'], [$options]);
21   }
22
23   /**
24    * Tests whether the session.cookie_domain ini settings is computed correctly.
25    *
26    * @covers ::getOptions
27    *
28    * @dataProvider providerTestGeneratedCookieDomain
29    */
30   public function testGeneratedCookieDomain($uri, $expected_domain) {
31     $config = $this->createSessionConfiguration();
32
33     $request = Request::create($uri);
34     $options = $config->getOptions($request);
35
36     $this->assertEquals($expected_domain, $options['cookie_domain']);
37   }
38
39   /**
40    * Data provider for the cookie domain test.
41    *
42    * @returns array
43    *   Test data
44    */
45   public function providerTestGeneratedCookieDomain() {
46     return [
47       ['http://example.com/path/index.php', '.example.com'],
48       ['http://www.example.com/path/index.php', '.www.example.com'],
49       ['http://subdomain.example.com/path/index.php', '.subdomain.example.com'],
50       ['http://example.com:8080/path/index.php', '.example.com'],
51       ['https://example.com/path/index.php', '.example.com'],
52       ['http://localhost/path/index.php', ''],
53       ['http://127.0.0.1/path/index.php', ''],
54       ['http://127.0.0.1:8888/path/index.php', ''],
55       ['http://1.1.1.1/path/index.php', ''],
56       ['http://[::1]/path/index.php', ''],
57       ['http://[::1]:8888/path/index.php', ''],
58     ];
59   }
60
61   /**
62    * Tests the constructor injected session.cookie_domain ini setting.
63    *
64    * @covers ::__construct
65    * @covers ::getOptions
66    *
67    * @dataProvider providerTestEnforcedCookieDomain
68    */
69   public function testEnforcedCookieDomain($uri, $expected_domain) {
70     $config = $this->createSessionConfiguration(['cookie_domain' => '.example.com']);
71
72     $request = Request::create($uri);
73     $options = $config->getOptions($request);
74
75     $this->assertEquals($expected_domain, $options['cookie_domain']);
76   }
77
78   /**
79    * Data provider for the cookie domain test.
80    *
81    * @returns array
82    *   Test data
83    */
84   public function providerTestEnforcedCookieDomain() {
85     return [
86       ['http://example.com/path/index.php', '.example.com'],
87       ['http://www.example.com/path/index.php', '.example.com'],
88       ['http://subdomain.example.com/path/index.php', '.example.com'],
89       ['http://example.com:8080/path/index.php', '.example.com'],
90       ['https://example.com/path/index.php', '.example.com'],
91       ['http://localhost/path/index.php', '.example.com'],
92       ['http://127.0.0.1/path/index.php', '.example.com'],
93       ['http://127.0.0.1:8888/path/index.php', '.example.com'],
94       ['http://1.1.1.1/path/index.php', '.example.com'],
95       ['http://[::1]/path/index.php', '.example.com'],
96       ['http://[::1]:8888/path/index.php', '.example.com'],
97     ];
98   }
99
100   /**
101    * Tests whether the session.cookie_secure ini settings is computed correctly.
102    *
103    * @covers ::getOptions
104    *
105    * @dataProvider providerTestCookieSecure
106    */
107   public function testCookieSecure($uri, $expected_secure) {
108     $config = $this->createSessionConfiguration();
109
110     $request = Request::create($uri);
111     $options = $config->getOptions($request);
112
113     $this->assertEquals($expected_secure, $options['cookie_secure']);
114   }
115
116   /**
117    * Tests that session.cookie_secure ini settings cannot be overridden.
118    *
119    * @covers ::__construct
120    * @covers ::getOptions
121    *
122    * @dataProvider providerTestCookieSecure
123    */
124   public function testCookieSecureNotOverridable($uri, $expected_secure) {
125     $config = $this->createSessionConfiguration(['cookie_secure' => FALSE]);
126
127     $request = Request::create($uri);
128     $options = $config->getOptions($request);
129
130     $this->assertEquals($expected_secure, $options['cookie_secure']);
131   }
132
133   /**
134    * Data provider for the cookie secure test.
135    *
136    * @returns array
137    *   Test data
138    */
139   public function providerTestCookieSecure() {
140     return [
141       ['http://example.com/path/index.php', FALSE],
142       ['https://www.example.com/path/index.php', TRUE],
143       ['http://127.0.0.1/path/index.php', FALSE],
144       ['https://127.0.0.1:8888/path/index.php', TRUE],
145       ['http://[::1]/path/index.php', FALSE],
146       ['https://[::1]:8888/path/index.php', TRUE],
147     ];
148   }
149
150   /**
151    * Tests whether the session.name ini settings is computed correctly.
152    *
153    * @covers ::getOptions
154    *
155    * @dataProvider providerTestGeneratedSessionName
156    */
157   public function testGeneratedSessionName($uri, $expected_name) {
158     $config = $this->createSessionConfiguration();
159
160     $request = Request::create($uri);
161     $options = $config->getOptions($request);
162
163     $this->assertEquals($expected_name, $options['name']);
164   }
165
166   /**
167    * Data provider for the cookie name test.
168    *
169    * @returns array
170    *   Test data
171    */
172   public function providerTestGeneratedSessionName() {
173     $data = [
174       ['http://example.com/path/index.php', 'SESS', 'example.com'],
175       ['http://www.example.com/path/index.php', 'SESS', 'www.example.com'],
176       ['http://subdomain.example.com/path/index.php', 'SESS', 'subdomain.example.com'],
177       ['http://example.com:8080/path/index.php', 'SESS', 'example.com'],
178       ['https://example.com/path/index.php', 'SSESS', 'example.com'],
179       ['http://example.com/path/core/install.php', 'SESS', 'example.com'],
180       ['http://localhost/path/index.php', 'SESS', 'localhost'],
181       ['http://127.0.0.1/path/index.php', 'SESS', '127.0.0.1'],
182       ['http://127.0.0.1:8888/path/index.php', 'SESS', '127.0.0.1'],
183       ['https://127.0.0.1/path/index.php', 'SSESS', '127.0.0.1'],
184       ['https://127.0.0.1:8443/path/index.php', 'SSESS', '127.0.0.1'],
185       ['http://1.1.1.1/path/index.php', 'SESS', '1.1.1.1'],
186       ['https://1.1.1.1/path/index.php', 'SSESS', '1.1.1.1'],
187       ['http://[::1]/path/index.php', 'SESS', '[::1]'],
188       ['http://[::1]:8888/path/index.php', 'SESS', '[::1]'],
189       ['https://[::1]/path/index.php', 'SSESS', '[::1]'],
190       ['https://[::1]:8443/path/index.php', 'SSESS', '[::1]'],
191     ];
192
193     return array_map(function ($record) {
194       return [$record[0], $record[1] . substr(hash('sha256', $record[2]), 0, 32)];
195     }, $data);
196   }
197
198   /**
199    * Tests whether the session.name ini settings is computed correctly.
200    *
201    * @covers ::getOptions
202    *
203    * @dataProvider providerTestEnforcedSessionName
204    */
205   public function testEnforcedSessionNameViaCookieDomain($uri, $expected_name) {
206     $config = $this->createSessionConfiguration(['cookie_domain' => '.example.com']);
207
208     $request = Request::create($uri);
209     $options = $config->getOptions($request);
210
211     $this->assertEquals($expected_name, $options['name']);
212   }
213
214   /**
215    * Data provider for the cookie name test.
216    *
217    * @returns array
218    *   Test data
219    */
220   public function providerTestEnforcedSessionName() {
221     $data = [
222       ['http://example.com/path/index.php', 'SESS', '.example.com'],
223       ['http://www.example.com/path/index.php', 'SESS', '.example.com'],
224       ['http://subdomain.example.com/path/index.php', 'SESS', '.example.com'],
225       ['http://example.com:8080/path/index.php', 'SESS', '.example.com'],
226       ['https://example.com/path/index.php', 'SSESS', '.example.com'],
227       ['http://example.com/path/core/install.php', 'SESS', '.example.com'],
228       ['http://localhost/path/index.php', 'SESS', '.example.com'],
229       ['http://127.0.0.1/path/index.php', 'SESS', '.example.com'],
230       ['http://127.0.0.1:8888/path/index.php', 'SESS', '.example.com'],
231       ['https://127.0.0.1/path/index.php', 'SSESS', '.example.com'],
232       ['https://127.0.0.1:8443/path/index.php', 'SSESS', '.example.com'],
233       ['http://1.1.1.1/path/index.php', 'SESS', '.example.com'],
234       ['https://1.1.1.1/path/index.php', 'SSESS', '.example.com'],
235       ['http://[::1]/path/index.php', 'SESS', '.example.com'],
236       ['http://[::1]:8888/path/index.php', 'SESS', '.example.com'],
237       ['https://[::1]/path/index.php', 'SSESS', '.example.com'],
238       ['https://[::1]:8443/path/index.php', 'SSESS', '.example.com'],
239     ];
240
241     return array_map(function ($record) {
242       return [$record[0], $record[1] . substr(hash('sha256', $record[2]), 0, 32)];
243     }, $data);
244   }
245
246 }