NativeSessionStorageTest.php 9.3 KB

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