4da9a1bc515cb00aee9116b57d54eebd4a7783c5
[yaffs-website] / vendor / symfony / http-foundation / Tests / Session / Storage / NativeSessionStorageTest.php
1 <?php
2
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 namespace Symfony\Component\HttpFoundation\Tests\Session\Storage;
13
14 use PHPUnit\Framework\TestCase;
15 use Symfony\Component\HttpFoundation\Session\Attribute\AttributeBag;
16 use Symfony\Component\HttpFoundation\Session\Flash\FlashBag;
17 use Symfony\Component\HttpFoundation\Session\Storage\Handler\NativeSessionHandler;
18 use Symfony\Component\HttpFoundation\Session\Storage\Handler\NullSessionHandler;
19 use Symfony\Component\HttpFoundation\Session\Storage\NativeSessionStorage;
20 use Symfony\Component\HttpFoundation\Session\Storage\Proxy\NativeProxy;
21 use Symfony\Component\HttpFoundation\Session\Storage\Proxy\SessionHandlerProxy;
22
23 /**
24  * Test class for NativeSessionStorage.
25  *
26  * @author Drak <drak@zikula.org>
27  *
28  * These tests require separate processes.
29  *
30  * @runTestsInSeparateProcesses
31  * @preserveGlobalState disabled
32  */
33 class NativeSessionStorageTest extends TestCase
34 {
35     private $savePath;
36
37     protected function setUp()
38     {
39         $this->iniSet('session.save_handler', 'files');
40         $this->iniSet('session.save_path', $this->savePath = sys_get_temp_dir().'/sf2test');
41         if (!is_dir($this->savePath)) {
42             mkdir($this->savePath);
43         }
44     }
45
46     protected function tearDown()
47     {
48         session_write_close();
49         array_map('unlink', glob($this->savePath.'/*'));
50         if (is_dir($this->savePath)) {
51             rmdir($this->savePath);
52         }
53
54         $this->savePath = null;
55     }
56
57     /**
58      * @param array $options
59      *
60      * @return NativeSessionStorage
61      */
62     protected function getStorage(array $options = array())
63     {
64         $storage = new NativeSessionStorage($options);
65         $storage->registerBag(new AttributeBag());
66
67         return $storage;
68     }
69
70     public function testBag()
71     {
72         $storage = $this->getStorage();
73         $bag = new FlashBag();
74         $storage->registerBag($bag);
75         $this->assertSame($bag, $storage->getBag($bag->getName()));
76     }
77
78     /**
79      * @expectedException \InvalidArgumentException
80      */
81     public function testRegisterBagException()
82     {
83         $storage = $this->getStorage();
84         $storage->getBag('non_existing');
85     }
86
87     /**
88      * @expectedException \LogicException
89      */
90     public function testRegisterBagForAStartedSessionThrowsException()
91     {
92         $storage = $this->getStorage();
93         $storage->start();
94         $storage->registerBag(new AttributeBag());
95     }
96
97     public function testGetId()
98     {
99         $storage = $this->getStorage();
100         $this->assertSame('', $storage->getId(), 'Empty ID before starting session');
101
102         $storage->start();
103         $id = $storage->getId();
104         $this->assertInternalType('string', $id);
105         $this->assertNotSame('', $id);
106
107         $storage->save();
108         $this->assertSame($id, $storage->getId(), 'ID stays after saving session');
109     }
110
111     public function testRegenerate()
112     {
113         $storage = $this->getStorage();
114         $storage->start();
115         $id = $storage->getId();
116         $storage->getBag('attributes')->set('lucky', 7);
117         $storage->regenerate();
118         $this->assertNotEquals($id, $storage->getId());
119         $this->assertEquals(7, $storage->getBag('attributes')->get('lucky'));
120     }
121
122     public function testRegenerateDestroy()
123     {
124         $storage = $this->getStorage();
125         $storage->start();
126         $id = $storage->getId();
127         $storage->getBag('attributes')->set('legs', 11);
128         $storage->regenerate(true);
129         $this->assertNotEquals($id, $storage->getId());
130         $this->assertEquals(11, $storage->getBag('attributes')->get('legs'));
131     }
132
133     public function testSessionGlobalIsUpToDateAfterIdRegeneration()
134     {
135         $storage = $this->getStorage();
136         $storage->start();
137         $storage->getBag('attributes')->set('lucky', 7);
138         $storage->regenerate();
139         $storage->getBag('attributes')->set('lucky', 42);
140
141         $this->assertEquals(42, $_SESSION['_sf2_attributes']['lucky']);
142     }
143
144     public function testRegenerationFailureDoesNotFlagStorageAsStarted()
145     {
146         $storage = $this->getStorage();
147         $this->assertFalse($storage->regenerate());
148         $this->assertFalse($storage->isStarted());
149     }
150
151     public function testDefaultSessionCacheLimiter()
152     {
153         $this->iniSet('session.cache_limiter', 'nocache');
154
155         $storage = new NativeSessionStorage();
156         $this->assertEquals('', ini_get('session.cache_limiter'));
157     }
158
159     public function testExplicitSessionCacheLimiter()
160     {
161         $this->iniSet('session.cache_limiter', 'nocache');
162
163         $storage = new NativeSessionStorage(array('cache_limiter' => 'public'));
164         $this->assertEquals('public', ini_get('session.cache_limiter'));
165     }
166
167     public function testCookieOptions()
168     {
169         $options = array(
170             'cookie_lifetime' => 123456,
171             'cookie_path' => '/my/cookie/path',
172             'cookie_domain' => 'symfony.example.com',
173             'cookie_secure' => true,
174             'cookie_httponly' => false,
175         );
176
177         $this->getStorage($options);
178         $temp = session_get_cookie_params();
179         $gco = array();
180
181         foreach ($temp as $key => $value) {
182             $gco['cookie_'.$key] = $value;
183         }
184
185         $this->assertEquals($options, $gco);
186     }
187
188     /**
189      * @expectedException \InvalidArgumentException
190      */
191     public function testSetSaveHandlerException()
192     {
193         $storage = $this->getStorage();
194         $storage->setSaveHandler(new \stdClass());
195     }
196
197     public function testSetSaveHandler53()
198     {
199         if (PHP_VERSION_ID >= 50400) {
200             $this->markTestSkipped('Test skipped, for PHP 5.3 only.');
201         }
202
203         $this->iniSet('session.save_handler', 'files');
204         $storage = $this->getStorage();
205         $storage->setSaveHandler();
206         $this->assertInstanceOf('Symfony\Component\HttpFoundation\Session\Storage\Proxy\NativeProxy', $storage->getSaveHandler());
207         $storage->setSaveHandler(null);
208         $this->assertInstanceOf('Symfony\Component\HttpFoundation\Session\Storage\Proxy\NativeProxy', $storage->getSaveHandler());
209         $storage->setSaveHandler(new NativeSessionHandler());
210         $this->assertInstanceOf('Symfony\Component\HttpFoundation\Session\Storage\Proxy\NativeProxy', $storage->getSaveHandler());
211         $storage->setSaveHandler(new SessionHandlerProxy(new NullSessionHandler()));
212         $this->assertInstanceOf('Symfony\Component\HttpFoundation\Session\Storage\Proxy\SessionHandlerProxy', $storage->getSaveHandler());
213         $storage->setSaveHandler(new NullSessionHandler());
214         $this->assertInstanceOf('Symfony\Component\HttpFoundation\Session\Storage\Proxy\SessionHandlerProxy', $storage->getSaveHandler());
215         $storage->setSaveHandler(new NativeProxy());
216         $this->assertInstanceOf('Symfony\Component\HttpFoundation\Session\Storage\Proxy\NativeProxy', $storage->getSaveHandler());
217     }
218
219     /**
220      * @requires PHP 5.4
221      */
222     public function testSetSaveHandler54()
223     {
224         $this->iniSet('session.save_handler', 'files');
225         $storage = $this->getStorage();
226         $storage->setSaveHandler();
227         $this->assertInstanceOf('Symfony\Component\HttpFoundation\Session\Storage\Proxy\SessionHandlerProxy', $storage->getSaveHandler());
228         $storage->setSaveHandler(null);
229         $this->assertInstanceOf('Symfony\Component\HttpFoundation\Session\Storage\Proxy\SessionHandlerProxy', $storage->getSaveHandler());
230         $storage->setSaveHandler(new SessionHandlerProxy(new NativeSessionHandler()));
231         $this->assertInstanceOf('Symfony\Component\HttpFoundation\Session\Storage\Proxy\SessionHandlerProxy', $storage->getSaveHandler());
232         $storage->setSaveHandler(new NativeSessionHandler());
233         $this->assertInstanceOf('Symfony\Component\HttpFoundation\Session\Storage\Proxy\SessionHandlerProxy', $storage->getSaveHandler());
234         $storage->setSaveHandler(new SessionHandlerProxy(new NullSessionHandler()));
235         $this->assertInstanceOf('Symfony\Component\HttpFoundation\Session\Storage\Proxy\SessionHandlerProxy', $storage->getSaveHandler());
236         $storage->setSaveHandler(new NullSessionHandler());
237         $this->assertInstanceOf('Symfony\Component\HttpFoundation\Session\Storage\Proxy\SessionHandlerProxy', $storage->getSaveHandler());
238     }
239
240     /**
241      * @expectedException \RuntimeException
242      */
243     public function testStartedOutside()
244     {
245         $storage = $this->getStorage();
246
247         $this->assertFalse($storage->getSaveHandler()->isActive());
248         $this->assertFalse($storage->isStarted());
249
250         session_start();
251         $this->assertTrue(isset($_SESSION));
252         if (PHP_VERSION_ID >= 50400) {
253             // this only works in PHP >= 5.4 where session_status is available
254             $this->assertTrue($storage->getSaveHandler()->isActive());
255         }
256         // PHP session might have started, but the storage driver has not, so false is correct here
257         $this->assertFalse($storage->isStarted());
258
259         $key = $storage->getMetadataBag()->getStorageKey();
260         $this->assertFalse(isset($_SESSION[$key]));
261         $storage->start();
262     }
263
264     public function testRestart()
265     {
266         $storage = $this->getStorage();
267         $storage->start();
268         $id = $storage->getId();
269         $storage->getBag('attributes')->set('lucky', 7);
270         $storage->save();
271         $storage->start();
272         $this->assertSame($id, $storage->getId(), 'Same session ID after restarting');
273         $this->assertSame(7, $storage->getBag('attributes')->get('lucky'), 'Data still available');
274     }
275 }