NativeSessionStorageTest.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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()
  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()
  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. /**
  49. * @return NativeSessionStorage
  50. */
  51. protected function getStorage(array $options = [])
  52. {
  53. $storage = new NativeSessionStorage($options);
  54. $storage->registerBag(new AttributeBag());
  55. return $storage;
  56. }
  57. public function testBag()
  58. {
  59. $storage = $this->getStorage();
  60. $bag = new FlashBag();
  61. $storage->registerBag($bag);
  62. $this->assertSame($bag, $storage->getBag($bag->getName()));
  63. }
  64. public function testRegisterBagException()
  65. {
  66. $this->expectException('InvalidArgumentException');
  67. $storage = $this->getStorage();
  68. $storage->getBag('non_existing');
  69. }
  70. public function testRegisterBagForAStartedSessionThrowsException()
  71. {
  72. $this->expectException('LogicException');
  73. $storage = $this->getStorage();
  74. $storage->start();
  75. $storage->registerBag(new AttributeBag());
  76. }
  77. public function testGetId()
  78. {
  79. $storage = $this->getStorage();
  80. $this->assertSame('', $storage->getId(), 'Empty ID before starting session');
  81. $storage->start();
  82. $id = $storage->getId();
  83. $this->assertIsString($id);
  84. $this->assertNotSame('', $id);
  85. $storage->save();
  86. $this->assertSame($id, $storage->getId(), 'ID stays after saving session');
  87. }
  88. public function testRegenerate()
  89. {
  90. $storage = $this->getStorage();
  91. $storage->start();
  92. $id = $storage->getId();
  93. $storage->getBag('attributes')->set('lucky', 7);
  94. $storage->regenerate();
  95. $this->assertNotEquals($id, $storage->getId());
  96. $this->assertEquals(7, $storage->getBag('attributes')->get('lucky'));
  97. }
  98. public function testRegenerateDestroy()
  99. {
  100. $storage = $this->getStorage();
  101. $storage->start();
  102. $id = $storage->getId();
  103. $storage->getBag('attributes')->set('legs', 11);
  104. $storage->regenerate(true);
  105. $this->assertNotEquals($id, $storage->getId());
  106. $this->assertEquals(11, $storage->getBag('attributes')->get('legs'));
  107. }
  108. public function testSessionGlobalIsUpToDateAfterIdRegeneration()
  109. {
  110. $storage = $this->getStorage();
  111. $storage->start();
  112. $storage->getBag('attributes')->set('lucky', 7);
  113. $storage->regenerate();
  114. $storage->getBag('attributes')->set('lucky', 42);
  115. $this->assertEquals(42, $_SESSION['_sf2_attributes']['lucky']);
  116. }
  117. public function testRegenerationFailureDoesNotFlagStorageAsStarted()
  118. {
  119. $storage = $this->getStorage();
  120. $this->assertFalse($storage->regenerate());
  121. $this->assertFalse($storage->isStarted());
  122. }
  123. public function testDefaultSessionCacheLimiter()
  124. {
  125. $this->iniSet('session.cache_limiter', 'nocache');
  126. new NativeSessionStorage();
  127. $this->assertEquals('', ini_get('session.cache_limiter'));
  128. }
  129. public function testExplicitSessionCacheLimiter()
  130. {
  131. $this->iniSet('session.cache_limiter', 'nocache');
  132. new NativeSessionStorage(['cache_limiter' => 'public']);
  133. $this->assertEquals('public', ini_get('session.cache_limiter'));
  134. }
  135. public function testCookieOptions()
  136. {
  137. $options = [
  138. 'cookie_lifetime' => 123456,
  139. 'cookie_path' => '/my/cookie/path',
  140. 'cookie_domain' => 'symfony.example.com',
  141. 'cookie_secure' => true,
  142. 'cookie_httponly' => false,
  143. ];
  144. if (\PHP_VERSION_ID >= 70300) {
  145. $options['cookie_samesite'] = 'lax';
  146. }
  147. $this->getStorage($options);
  148. $temp = session_get_cookie_params();
  149. $gco = [];
  150. foreach ($temp as $key => $value) {
  151. $gco['cookie_'.$key] = $value;
  152. }
  153. $this->assertEquals($options, $gco);
  154. }
  155. public function testSessionOptions()
  156. {
  157. if (\defined('HHVM_VERSION')) {
  158. $this->markTestSkipped('HHVM is not handled in this test case.');
  159. }
  160. $options = [
  161. 'url_rewriter.tags' => 'a=href',
  162. 'cache_expire' => '200',
  163. ];
  164. $this->getStorage($options);
  165. $this->assertSame('a=href', ini_get('url_rewriter.tags'));
  166. $this->assertSame('200', ini_get('session.cache_expire'));
  167. }
  168. public function testSetSaveHandlerException()
  169. {
  170. $this->expectException('InvalidArgumentException');
  171. $storage = $this->getStorage();
  172. $storage->setSaveHandler(new \stdClass());
  173. }
  174. public function testSetSaveHandler()
  175. {
  176. $this->iniSet('session.save_handler', 'files');
  177. $storage = $this->getStorage();
  178. $storage->setSaveHandler();
  179. $this->assertInstanceOf('Symfony\Component\HttpFoundation\Session\Storage\Proxy\SessionHandlerProxy', $storage->getSaveHandler());
  180. $storage->setSaveHandler(null);
  181. $this->assertInstanceOf('Symfony\Component\HttpFoundation\Session\Storage\Proxy\SessionHandlerProxy', $storage->getSaveHandler());
  182. $storage->setSaveHandler(new SessionHandlerProxy(new NativeFileSessionHandler()));
  183. $this->assertInstanceOf('Symfony\Component\HttpFoundation\Session\Storage\Proxy\SessionHandlerProxy', $storage->getSaveHandler());
  184. $storage->setSaveHandler(new NativeFileSessionHandler());
  185. $this->assertInstanceOf('Symfony\Component\HttpFoundation\Session\Storage\Proxy\SessionHandlerProxy', $storage->getSaveHandler());
  186. $storage->setSaveHandler(new SessionHandlerProxy(new NullSessionHandler()));
  187. $this->assertInstanceOf('Symfony\Component\HttpFoundation\Session\Storage\Proxy\SessionHandlerProxy', $storage->getSaveHandler());
  188. $storage->setSaveHandler(new NullSessionHandler());
  189. $this->assertInstanceOf('Symfony\Component\HttpFoundation\Session\Storage\Proxy\SessionHandlerProxy', $storage->getSaveHandler());
  190. }
  191. public function testStarted()
  192. {
  193. $this->expectException('RuntimeException');
  194. $storage = $this->getStorage();
  195. $this->assertFalse($storage->getSaveHandler()->isActive());
  196. $this->assertFalse($storage->isStarted());
  197. session_start();
  198. $this->assertTrue(isset($_SESSION));
  199. $this->assertTrue($storage->getSaveHandler()->isActive());
  200. // PHP session might have started, but the storage driver has not, so false is correct here
  201. $this->assertFalse($storage->isStarted());
  202. $key = $storage->getMetadataBag()->getStorageKey();
  203. $this->assertArrayNotHasKey($key, $_SESSION);
  204. $storage->start();
  205. }
  206. public function testRestart()
  207. {
  208. $storage = $this->getStorage();
  209. $storage->start();
  210. $id = $storage->getId();
  211. $storage->getBag('attributes')->set('lucky', 7);
  212. $storage->save();
  213. $storage->start();
  214. $this->assertSame($id, $storage->getId(), 'Same session ID after restarting');
  215. $this->assertSame(7, $storage->getBag('attributes')->get('lucky'), 'Data still available');
  216. }
  217. public function testCanCreateNativeSessionStorageWhenSessionAlreadyStarted()
  218. {
  219. session_start();
  220. $this->getStorage();
  221. // Assert no exception has been thrown by `getStorage()`
  222. $this->addToAssertionCount(1);
  223. }
  224. public function testSetSessionOptionsOnceSessionStartedIsIgnored()
  225. {
  226. session_start();
  227. $this->getStorage([
  228. 'name' => 'something-else',
  229. ]);
  230. // Assert no exception has been thrown by `getStorage()`
  231. $this->addToAssertionCount(1);
  232. }
  233. public function testGetBagsOnceSessionStartedIsIgnored()
  234. {
  235. session_start();
  236. $bag = new AttributeBag();
  237. $bag->setName('flashes');
  238. $storage = $this->getStorage();
  239. $storage->registerBag($bag);
  240. $this->assertEquals($storage->getBag('flashes'), $bag);
  241. }
  242. }