3 namespace Drupal\Tests\Core\Session;
5 use Drupal\Tests\UnitTestCase;
6 use Symfony\Component\HttpFoundation\Request;
9 * @coversDefaultClass \Drupal\Core\Session\SessionConfiguration
12 class SessionConfigurationTest extends UnitTestCase {
15 * Constructs a partially mocked SUT.
17 * @returns \Drupal\Core\Session\SessionConfiguration|\PHPUnit_Framework_MockObject_MockObject
19 protected function createSessionConfiguration($options = []) {
20 return $this->getMock('Drupal\Core\Session\SessionConfiguration', ['drupalValidTestUa'], [$options]);
24 * Tests whether the session.cookie_domain ini settings is computed correctly.
26 * @covers ::getOptions
28 * @dataProvider providerTestGeneratedCookieDomain
30 public function testGeneratedCookieDomain($uri, $expected_domain) {
31 $config = $this->createSessionConfiguration();
33 $request = Request::create($uri);
34 $options = $config->getOptions($request);
36 $this->assertEquals($expected_domain, $options['cookie_domain']);
40 * Data provider for the cookie domain test.
45 public function providerTestGeneratedCookieDomain() {
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', ''],
62 * Tests the constructor injected session.cookie_domain ini setting.
64 * @covers ::__construct
65 * @covers ::getOptions
67 * @dataProvider providerTestEnforcedCookieDomain
69 public function testEnforcedCookieDomain($uri, $expected_domain) {
70 $config = $this->createSessionConfiguration(['cookie_domain' => '.example.com']);
72 $request = Request::create($uri);
73 $options = $config->getOptions($request);
75 $this->assertEquals($expected_domain, $options['cookie_domain']);
79 * Data provider for the cookie domain test.
84 public function providerTestEnforcedCookieDomain() {
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'],
101 * Tests whether the session.cookie_secure ini settings is computed correctly.
103 * @covers ::getOptions
105 * @dataProvider providerTestCookieSecure
107 public function testCookieSecure($uri, $expected_secure) {
108 $config = $this->createSessionConfiguration();
110 $request = Request::create($uri);
111 $options = $config->getOptions($request);
113 $this->assertEquals($expected_secure, $options['cookie_secure']);
117 * Tests that session.cookie_secure ini settings cannot be overridden.
119 * @covers ::__construct
120 * @covers ::getOptions
122 * @dataProvider providerTestCookieSecure
124 public function testCookieSecureNotOverridable($uri, $expected_secure) {
125 $config = $this->createSessionConfiguration(['cookie_secure' => FALSE]);
127 $request = Request::create($uri);
128 $options = $config->getOptions($request);
130 $this->assertEquals($expected_secure, $options['cookie_secure']);
134 * Data provider for the cookie secure test.
139 public function providerTestCookieSecure() {
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],
151 * Tests whether the session.name ini settings is computed correctly.
153 * @covers ::getOptions
155 * @dataProvider providerTestGeneratedSessionName
157 public function testGeneratedSessionName($uri, $expected_name) {
158 $config = $this->createSessionConfiguration();
160 $request = Request::create($uri);
161 $options = $config->getOptions($request);
163 $this->assertEquals($expected_name, $options['name']);
167 * Data provider for the cookie name test.
172 public function providerTestGeneratedSessionName() {
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]'],
193 return array_map(function ($record) {
194 return [$record[0], $record[1] . substr(hash('sha256', $record[2]), 0, 32)];
199 * Tests whether the session.name ini settings is computed correctly.
201 * @covers ::getOptions
203 * @dataProvider providerTestEnforcedSessionName
205 public function testEnforcedSessionNameViaCookieDomain($uri, $expected_name) {
206 $config = $this->createSessionConfiguration(['cookie_domain' => '.example.com']);
208 $request = Request::create($uri);
209 $options = $config->getOptions($request);
211 $this->assertEquals($expected_name, $options['name']);
215 * Data provider for the cookie name test.
220 public function providerTestEnforcedSessionName() {
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'],
241 return array_map(function ($record) {
242 return [$record[0], $record[1] . substr(hash('sha256', $record[2]), 0, 32)];