NativeSessionStorageTest.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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\NativeSessionHandler;
  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. * @param array $options
  50. *
  51. * @return NativeSessionStorage
  52. */
  53. protected function getStorage(array $options = array())
  54. {
  55. $storage = new NativeSessionStorage($options);
  56. $storage->registerBag(new AttributeBag());
  57. return $storage;
  58. }
  59. public function testBag()
  60. {
  61. $storage = $this->getStorage();
  62. $bag = new FlashBag();
  63. $storage->registerBag($bag);
  64. $this->assertSame($bag, $storage->getBag($bag->getName()));
  65. }
  66. /**
  67. * @expectedException \InvalidArgumentException
  68. */
  69. public function testRegisterBagException()
  70. {
  71. $storage = $this->getStorage();
  72. $storage->getBag('non_existing');
  73. }
  74. /**
  75. * @expectedException \LogicException
  76. */
  77. public function testRegisterBagForAStartedSessionThrowsException()
  78. {
  79. $storage = $this->getStorage();
  80. $storage->start();
  81. $storage->registerBag(new AttributeBag());
  82. }
  83. public function testGetId()
  84. {
  85. $storage = $this->getStorage();
  86. $this->assertSame('', $storage->getId(), 'Empty ID before starting session');
  87. $storage->start();
  88. $id = $storage->getId();
  89. $this->assertInternalType('string', $id);
  90. $this->assertNotSame('', $id);
  91. $storage->save();
  92. $this->assertSame($id, $storage->getId(), 'ID stays after saving session');
  93. }
  94. public function testRegenerate()
  95. {
  96. $storage = $this->getStorage();
  97. $storage->start();
  98. $id = $storage->getId();
  99. $storage->getBag('attributes')->set('lucky', 7);
  100. $storage->regenerate();
  101. $this->assertNotEquals($id, $storage->getId());
  102. $this->assertEquals(7, $storage->getBag('attributes')->get('lucky'));
  103. }
  104. public function testRegenerateDestroy()
  105. {
  106. $storage = $this->getStorage();
  107. $storage->start();
  108. $id = $storage->getId();
  109. $storage->getBag('attributes')->set('legs', 11);
  110. $storage->regenerate(true);
  111. $this->assertNotEquals($id, $storage->getId());
  112. $this->assertEquals(11, $storage->getBag('attributes')->get('legs'));
  113. }
  114. public function testSessionGlobalIsUpToDateAfterIdRegeneration()
  115. {
  116. $storage = $this->getStorage();
  117. $storage->start();
  118. $storage->getBag('attributes')->set('lucky', 7);
  119. $storage->regenerate();
  120. $storage->getBag('attributes')->set('lucky', 42);
  121. $this->assertEquals(42, $_SESSION['_sf2_attributes']['lucky']);
  122. }
  123. public function testRegenerationFailureDoesNotFlagStorageAsStarted()
  124. {
  125. $storage = $this->getStorage();
  126. $this->assertFalse($storage->regenerate());
  127. $this->assertFalse($storage->isStarted());
  128. }
  129. public function testDefaultSessionCacheLimiter()
  130. {
  131. $this->iniSet('session.cache_limiter', 'nocache');
  132. $storage = new NativeSessionStorage();
  133. $this->assertEquals('', ini_get('session.cache_limiter'));
  134. }
  135. public function testExplicitSessionCacheLimiter()
  136. {
  137. $this->iniSet('session.cache_limiter', 'nocache');
  138. $storage = new NativeSessionStorage(array('cache_limiter' => 'public'));
  139. $this->assertEquals('public', ini_get('session.cache_limiter'));
  140. }
  141. public function testCookieOptions()
  142. {
  143. $options = array(
  144. 'cookie_lifetime' => 123456,
  145. 'cookie_path' => '/my/cookie/path',
  146. 'cookie_domain' => 'symfony.example.com',
  147. 'cookie_secure' => true,
  148. 'cookie_httponly' => false,
  149. );
  150. $this->getStorage($options);
  151. $temp = session_get_cookie_params();
  152. $gco = array();
  153. foreach ($temp as $key => $value) {
  154. $gco['cookie_'.$key] = $value;
  155. }
  156. $this->assertEquals($options, $gco);
  157. }
  158. /**
  159. * @expectedException \InvalidArgumentException
  160. */
  161. public function testSetSaveHandlerException()
  162. {
  163. $storage = $this->getStorage();
  164. $storage->setSaveHandler(new \stdClass());
  165. }
  166. public function testSetSaveHandler()
  167. {
  168. $this->iniSet('session.save_handler', 'files');
  169. $storage = $this->getStorage();
  170. $storage->setSaveHandler();
  171. $this->assertInstanceOf('Symfony\Component\HttpFoundation\Session\Storage\Proxy\SessionHandlerProxy', $storage->getSaveHandler());
  172. $storage->setSaveHandler(null);
  173. $this->assertInstanceOf('Symfony\Component\HttpFoundation\Session\Storage\Proxy\SessionHandlerProxy', $storage->getSaveHandler());
  174. $storage->setSaveHandler(new SessionHandlerProxy(new NativeSessionHandler()));
  175. $this->assertInstanceOf('Symfony\Component\HttpFoundation\Session\Storage\Proxy\SessionHandlerProxy', $storage->getSaveHandler());
  176. $storage->setSaveHandler(new NativeSessionHandler());
  177. $this->assertInstanceOf('Symfony\Component\HttpFoundation\Session\Storage\Proxy\SessionHandlerProxy', $storage->getSaveHandler());
  178. $storage->setSaveHandler(new SessionHandlerProxy(new NullSessionHandler()));
  179. $this->assertInstanceOf('Symfony\Component\HttpFoundation\Session\Storage\Proxy\SessionHandlerProxy', $storage->getSaveHandler());
  180. $storage->setSaveHandler(new NullSessionHandler());
  181. $this->assertInstanceOf('Symfony\Component\HttpFoundation\Session\Storage\Proxy\SessionHandlerProxy', $storage->getSaveHandler());
  182. }
  183. /**
  184. * @expectedException \RuntimeException
  185. */
  186. public function testStarted()
  187. {
  188. $storage = $this->getStorage();
  189. $this->assertFalse($storage->getSaveHandler()->isActive());
  190. $this->assertFalse($storage->isStarted());
  191. session_start();
  192. $this->assertTrue(isset($_SESSION));
  193. $this->assertTrue($storage->getSaveHandler()->isActive());
  194. // PHP session might have started, but the storage driver has not, so false is correct here
  195. $this->assertFalse($storage->isStarted());
  196. $key = $storage->getMetadataBag()->getStorageKey();
  197. $this->assertFalse(isset($_SESSION[$key]));
  198. $storage->start();
  199. }
  200. public function testRestart()
  201. {
  202. $storage = $this->getStorage();
  203. $storage->start();
  204. $id = $storage->getId();
  205. $storage->getBag('attributes')->set('lucky', 7);
  206. $storage->save();
  207. $storage->start();
  208. $this->assertSame($id, $storage->getId(), 'Same session ID after restarting');
  209. $this->assertSame(7, $storage->getBag('attributes')->get('lucky'), 'Data still available');
  210. }
  211. }