PageRenderTime 45ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/src/Symfony/Component/HttpFoundation/Tests/Session/Storage/NativeSessionStorageTest.php

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