PageRenderTime 24ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/symfony/http-foundation/Tests/Session/Storage/NativeSessionStorageTest.php

https://gitlab.com/judielsm/Handora
PHP | 258 lines | 188 code | 36 blank | 34 comment | 5 complexity | e4da834e7626c493278df26989bc327c MD5 | raw file
  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\NativeProxy;
  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 \PHPUnit_Framework_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. public function testGetId()
  75. {
  76. $storage = $this->getStorage();
  77. $this->assertSame('', $storage->getId(), 'Empty ID before starting session');
  78. $storage->start();
  79. $id = $storage->getId();
  80. $this->assertInternalType('string', $id);
  81. $this->assertNotSame('', $id);
  82. $storage->save();
  83. $this->assertSame($id, $storage->getId(), 'ID stays after saving session');
  84. }
  85. public function testRegenerate()
  86. {
  87. $storage = $this->getStorage();
  88. $storage->start();
  89. $id = $storage->getId();
  90. $storage->getBag('attributes')->set('lucky', 7);
  91. $storage->regenerate();
  92. $this->assertNotEquals($id, $storage->getId());
  93. $this->assertEquals(7, $storage->getBag('attributes')->get('lucky'));
  94. }
  95. public function testRegenerateDestroy()
  96. {
  97. $storage = $this->getStorage();
  98. $storage->start();
  99. $id = $storage->getId();
  100. $storage->getBag('attributes')->set('legs', 11);
  101. $storage->regenerate(true);
  102. $this->assertNotEquals($id, $storage->getId());
  103. $this->assertEquals(11, $storage->getBag('attributes')->get('legs'));
  104. }
  105. public function testSessionGlobalIsUpToDateAfterIdRegeneration()
  106. {
  107. $storage = $this->getStorage();
  108. $storage->start();
  109. $storage->getBag('attributes')->set('lucky', 7);
  110. $storage->regenerate();
  111. $storage->getBag('attributes')->set('lucky', 42);
  112. $this->assertEquals(42, $_SESSION['_sf2_attributes']['lucky']);
  113. }
  114. public function testDefaultSessionCacheLimiter()
  115. {
  116. $this->iniSet('session.cache_limiter', 'nocache');
  117. $storage = new NativeSessionStorage();
  118. $this->assertEquals('', ini_get('session.cache_limiter'));
  119. }
  120. public function testExplicitSessionCacheLimiter()
  121. {
  122. $this->iniSet('session.cache_limiter', 'nocache');
  123. $storage = new NativeSessionStorage(array('cache_limiter' => 'public'));
  124. $this->assertEquals('public', ini_get('session.cache_limiter'));
  125. }
  126. public function testCookieOptions()
  127. {
  128. $options = array(
  129. 'cookie_lifetime' => 123456,
  130. 'cookie_path' => '/my/cookie/path',
  131. 'cookie_domain' => 'symfony.example.com',
  132. 'cookie_secure' => true,
  133. 'cookie_httponly' => false,
  134. );
  135. $this->getStorage($options);
  136. $temp = session_get_cookie_params();
  137. $gco = array();
  138. foreach ($temp as $key => $value) {
  139. $gco['cookie_'.$key] = $value;
  140. }
  141. $this->assertEquals($options, $gco);
  142. }
  143. /**
  144. * @expectedException \InvalidArgumentException
  145. */
  146. public function testSetSaveHandlerException()
  147. {
  148. $storage = $this->getStorage();
  149. $storage->setSaveHandler(new \stdClass());
  150. }
  151. public function testSetSaveHandler53()
  152. {
  153. if (PHP_VERSION_ID >= 50400) {
  154. $this->markTestSkipped('Test skipped, for PHP 5.3 only.');
  155. }
  156. $this->iniSet('session.save_handler', 'files');
  157. $storage = $this->getStorage();
  158. $storage->setSaveHandler();
  159. $this->assertInstanceOf('Symfony\Component\HttpFoundation\Session\Storage\Proxy\NativeProxy', $storage->getSaveHandler());
  160. $storage->setSaveHandler(null);
  161. $this->assertInstanceOf('Symfony\Component\HttpFoundation\Session\Storage\Proxy\NativeProxy', $storage->getSaveHandler());
  162. $storage->setSaveHandler(new NativeSessionHandler());
  163. $this->assertInstanceOf('Symfony\Component\HttpFoundation\Session\Storage\Proxy\NativeProxy', $storage->getSaveHandler());
  164. $storage->setSaveHandler(new SessionHandlerProxy(new NullSessionHandler()));
  165. $this->assertInstanceOf('Symfony\Component\HttpFoundation\Session\Storage\Proxy\SessionHandlerProxy', $storage->getSaveHandler());
  166. $storage->setSaveHandler(new NullSessionHandler());
  167. $this->assertInstanceOf('Symfony\Component\HttpFoundation\Session\Storage\Proxy\SessionHandlerProxy', $storage->getSaveHandler());
  168. $storage->setSaveHandler(new NativeProxy());
  169. $this->assertInstanceOf('Symfony\Component\HttpFoundation\Session\Storage\Proxy\NativeProxy', $storage->getSaveHandler());
  170. }
  171. public function testSetSaveHandler54()
  172. {
  173. if (PHP_VERSION_ID < 50400) {
  174. $this->markTestSkipped('Test skipped, for PHP 5.4 only.');
  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 NativeSessionHandler()));
  183. $this->assertInstanceOf('Symfony\Component\HttpFoundation\Session\Storage\Proxy\SessionHandlerProxy', $storage->getSaveHandler());
  184. $storage->setSaveHandler(new NativeSessionHandler());
  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. /**
  192. * @expectedException \RuntimeException
  193. */
  194. public function testStartedOutside()
  195. {
  196. $storage = $this->getStorage();
  197. $this->assertFalse($storage->getSaveHandler()->isActive());
  198. $this->assertFalse($storage->isStarted());
  199. session_start();
  200. $this->assertTrue(isset($_SESSION));
  201. if (PHP_VERSION_ID >= 50400) {
  202. // this only works in PHP >= 5.4 where session_status is available
  203. $this->assertTrue($storage->getSaveHandler()->isActive());
  204. }
  205. // PHP session might have started, but the storage driver has not, so false is correct here
  206. $this->assertFalse($storage->isStarted());
  207. $key = $storage->getMetadataBag()->getStorageKey();
  208. $this->assertFalse(isset($_SESSION[$key]));
  209. $storage->start();
  210. }
  211. public function testRestart()
  212. {
  213. $storage = $this->getStorage();
  214. $storage->start();
  215. $id = $storage->getId();
  216. $storage->getBag('attributes')->set('lucky', 7);
  217. $storage->save();
  218. $storage->start();
  219. $this->assertSame($id, $storage->getId(), 'Same session ID after restarting');
  220. $this->assertSame(7, $storage->getBag('attributes')->get('lucky'), 'Data still available');
  221. }
  222. }