No Description

NativeSessionStorageTest.php 8.0KB

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