NativeSessionStorageTest.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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().'/sf2test');
  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. $this->getStorage($options);
  145. $temp = session_get_cookie_params();
  146. $gco = [];
  147. foreach ($temp as $key => $value) {
  148. $gco['cookie_'.$key] = $value;
  149. }
  150. unset($gco['cookie_samesite']);
  151. $this->assertEquals($options, $gco);
  152. }
  153. public function testSessionOptions()
  154. {
  155. if (\defined('HHVM_VERSION')) {
  156. $this->markTestSkipped('HHVM is not handled in this test case.');
  157. }
  158. $options = [
  159. 'url_rewriter.tags' => 'a=href',
  160. 'cache_expire' => '200',
  161. ];
  162. $this->getStorage($options);
  163. $this->assertSame('a=href', ini_get('url_rewriter.tags'));
  164. $this->assertSame('200', ini_get('session.cache_expire'));
  165. }
  166. public function testSetSaveHandlerException()
  167. {
  168. $this->expectException('InvalidArgumentException');
  169. $storage = $this->getStorage();
  170. $storage->setSaveHandler(new \stdClass());
  171. }
  172. public function testSetSaveHandler()
  173. {
  174. $this->iniSet('session.save_handler', 'files');
  175. $storage = $this->getStorage();
  176. $storage->setSaveHandler();
  177. $this->assertInstanceOf('Symfony\Component\HttpFoundation\Session\Storage\Proxy\SessionHandlerProxy', $storage->getSaveHandler());
  178. $storage->setSaveHandler(null);
  179. $this->assertInstanceOf('Symfony\Component\HttpFoundation\Session\Storage\Proxy\SessionHandlerProxy', $storage->getSaveHandler());
  180. $storage->setSaveHandler(new SessionHandlerProxy(new NativeFileSessionHandler()));
  181. $this->assertInstanceOf('Symfony\Component\HttpFoundation\Session\Storage\Proxy\SessionHandlerProxy', $storage->getSaveHandler());
  182. $storage->setSaveHandler(new NativeFileSessionHandler());
  183. $this->assertInstanceOf('Symfony\Component\HttpFoundation\Session\Storage\Proxy\SessionHandlerProxy', $storage->getSaveHandler());
  184. $storage->setSaveHandler(new SessionHandlerProxy(new NullSessionHandler()));
  185. $this->assertInstanceOf('Symfony\Component\HttpFoundation\Session\Storage\Proxy\SessionHandlerProxy', $storage->getSaveHandler());
  186. $storage->setSaveHandler(new NullSessionHandler());
  187. $this->assertInstanceOf('Symfony\Component\HttpFoundation\Session\Storage\Proxy\SessionHandlerProxy', $storage->getSaveHandler());
  188. }
  189. public function testStarted()
  190. {
  191. $this->expectException('RuntimeException');
  192. $storage = $this->getStorage();
  193. $this->assertFalse($storage->getSaveHandler()->isActive());
  194. $this->assertFalse($storage->isStarted());
  195. session_start();
  196. $this->assertTrue(isset($_SESSION));
  197. $this->assertTrue($storage->getSaveHandler()->isActive());
  198. // PHP session might have started, but the storage driver has not, so false is correct here
  199. $this->assertFalse($storage->isStarted());
  200. $key = $storage->getMetadataBag()->getStorageKey();
  201. $this->assertArrayNotHasKey($key, $_SESSION);
  202. $storage->start();
  203. }
  204. public function testRestart()
  205. {
  206. $storage = $this->getStorage();
  207. $storage->start();
  208. $id = $storage->getId();
  209. $storage->getBag('attributes')->set('lucky', 7);
  210. $storage->save();
  211. $storage->start();
  212. $this->assertSame($id, $storage->getId(), 'Same session ID after restarting');
  213. $this->assertSame(7, $storage->getBag('attributes')->get('lucky'), 'Data still available');
  214. }
  215. public function testCanCreateNativeSessionStorageWhenSessionAlreadyStarted()
  216. {
  217. session_start();
  218. $this->getStorage();
  219. // Assert no exception has been thrown by `getStorage()`
  220. $this->addToAssertionCount(1);
  221. }
  222. public function testSetSessionOptionsOnceSessionStartedIsIgnored()
  223. {
  224. session_start();
  225. $this->getStorage([
  226. 'name' => 'something-else',
  227. ]);
  228. // Assert no exception has been thrown by `getStorage()`
  229. $this->addToAssertionCount(1);
  230. }
  231. public function testGetBagsOnceSessionStartedIsIgnored()
  232. {
  233. session_start();
  234. $bag = new AttributeBag();
  235. $bag->setName('flashes');
  236. $storage = $this->getStorage();
  237. $storage->registerBag($bag);
  238. $this->assertEquals($storage->getBag('flashes'), $bag);
  239. }
  240. }