SessionTest.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. <?php
  2. /**
  3. * Lithium: the most rad php framework
  4. *
  5. * @copyright Copyright 2013, Union of RAD (http://union-of-rad.org)
  6. * @license http://opensource.org/licenses/bsd-license.php The BSD License
  7. */
  8. namespace lithium\tests\cases\storage;
  9. use lithium\storage\Session;
  10. use lithium\storage\session\adapter\Memory;
  11. use lithium\tests\mocks\storage\session\adapter\SessionStorageConditional;
  12. use lithium\tests\mocks\storage\session\strategy\MockEncrypt;
  13. /**
  14. *
  15. * @todo Refactor this to get rid of the very integration-style tests.
  16. */
  17. class SessionTest extends \lithium\test\Unit {
  18. public function setUp() {
  19. Session::config(array(
  20. 'default' => array('adapter' => new Memory())
  21. ));
  22. }
  23. public function testSessionInitialization() {
  24. $store1 = new Memory();
  25. $store2 = new Memory();
  26. $config = array(
  27. 'store1' => array('adapter' => &$store1, 'filters' => array()),
  28. 'store2' => array('adapter' => &$store2, 'filters' => array())
  29. );
  30. Session::config($config);
  31. $result = Session::config();
  32. $this->assertEqual($config, $result);
  33. Session::reset();
  34. Session::config(array('store1' => array(
  35. 'adapter' => 'lithium\storage\session\adapter\Memory',
  36. 'filters' => array()
  37. )));
  38. $this->assertTrue(Session::write('key', 'value'));
  39. $result = Session::read('key');
  40. $expected = 'value';
  41. $this->assertEqual($expected, $result);
  42. }
  43. public function testSingleStoreReadWrite() {
  44. $this->assertNull(Session::read('key'));
  45. $this->assertTrue(Session::write('key', 'value'));
  46. $this->assertEqual(Session::read('key'), 'value');
  47. Session::reset();
  48. $this->assertNull(Session::read('key'));
  49. $this->assertIdentical(false, Session::write('key', 'value'));
  50. }
  51. public function testNamedConfigurationReadWrite() {
  52. $store1 = new Memory();
  53. $store2 = new Memory();
  54. $config = array(
  55. 'store1' => array('adapter' => &$store1, 'filters' => array()),
  56. 'store2' => array('adapter' => &$store2, 'filters' => array())
  57. );
  58. Session::reset();
  59. Session::config($config);
  60. $result = Session::config();
  61. $this->assertEqual($config, $result);
  62. $result = Session::write('key', 'value', array('name' => 'store1'));
  63. $this->assertTrue($result);
  64. $result = Session::read('key', array('name' => 'store1'));
  65. $this->assertEqual($result, 'value');
  66. $result = Session::read('key', array('name' => 'store2'));
  67. $this->assertFalse($result);
  68. }
  69. public function testSessionConfigReset() {
  70. $this->assertTrue(Session::write('key', 'value'));
  71. $this->assertEqual(Session::read('key'), 'value');
  72. Session::reset();
  73. $this->assertFalse(Session::config());
  74. $this->assertFalse(Session::read('key'));
  75. $this->assertFalse(Session::write('key', 'value'));
  76. }
  77. /**
  78. * Tests a scenario where no session handler is available that matches the passed parameters.
  79. *
  80. * @return void
  81. */
  82. public function testUnhandledWrite() {
  83. Session::config(array(
  84. 'conditional' => array('adapter' => new SessionStorageConditional())
  85. ));
  86. $result = Session::write('key', 'value', array('fail' => true));
  87. $this->assertFalse($result);
  88. }
  89. /**
  90. * Tests deleting a session key from one or all adapters.
  91. *
  92. * @return void
  93. */
  94. public function testSessionKeyCheckAndDelete() {
  95. Session::config(array(
  96. 'temp' => array('adapter' => new Memory(), 'filters' => array()),
  97. 'persistent' => array('adapter' => new Memory(), 'filters' => array())
  98. ));
  99. Session::write('key1', 'value', array('name' => 'persistent'));
  100. Session::write('key2', 'value', array('name' => 'temp'));
  101. $this->assertTrue(Session::check('key1'));
  102. $this->assertTrue(Session::check('key2'));
  103. $this->assertTrue(Session::check('key1', array('name' => 'persistent')));
  104. $this->assertFalse(Session::check('key1', array('name' => 'temp')));
  105. $this->assertFalse(Session::check('key2', array('name' => 'persistent')));
  106. $this->assertTrue(Session::check('key2', array('name' => 'temp')));
  107. Session::delete('key1');
  108. $this->assertFalse(Session::check('key1'));
  109. Session::write('key1', 'value', array('name' => 'persistent'));
  110. $this->assertTrue(Session::check('key1'));
  111. Session::delete('key1', array('name' => 'temp'));
  112. $this->assertTrue(Session::check('key1'));
  113. Session::delete('key1', array('name' => 'persistent'));
  114. $this->assertFalse(Session::check('key1'));
  115. }
  116. /**
  117. * Tests clearing all session data from one or all adapters.
  118. *
  119. * @return void
  120. */
  121. public function testSessionClear() {
  122. Session::config(array(
  123. 'primary' => array('adapter' => new Memory(), 'filters' => array()),
  124. 'secondary' => array('adapter' => new Memory(), 'filters' => array())
  125. ));
  126. Session::write('key1', 'value', array('name' => 'primary'));
  127. Session::write('key2', 'value', array('name' => 'secondary'));
  128. Session::clear(array('name' => 'secondary'));
  129. $this->assertTrue(Session::check('key1'));
  130. $this->assertFalse(Session::check('key2'));
  131. Session::write('key2', 'value', array('name' => 'secondary'));
  132. Session::clear();
  133. $this->assertFalse(Session::check('key1'));
  134. $this->assertFalse(Session::check('key2'));
  135. }
  136. /**
  137. * Tests querying session keys from the primary adapter.
  138. * The memory adapter returns a UUID.
  139. *
  140. * @return void
  141. */
  142. public function testKey() {
  143. $result = Session::key();
  144. $pattern = "/^[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$/";
  145. $this->assertPattern($pattern, $result);
  146. }
  147. public function testConfigNoAdapters() {
  148. Session::config(array(
  149. 'conditional' => array('adapter' => new SessionStorageConditional())
  150. ));
  151. $this->assertTrue(Session::write('key', 'value'));
  152. $this->assertEqual(Session::read('key'), 'value');
  153. $this->assertFalse(Session::read('key', array('fail' => true)));
  154. }
  155. public function testSessionState() {
  156. $this->assertTrue(Session::isStarted());
  157. $this->assertTrue(Session::isStarted('default'));
  158. $this->expectException("Configuration `invalid` has not been defined.");
  159. $this->assertFalse(Session::isStarted('invalid'));
  160. }
  161. public function testSessionStateReset() {
  162. Session::reset();
  163. $this->assertFalse(Session::isStarted());
  164. }
  165. public function testSessionStateResetNamed() {
  166. Session::reset();
  167. $this->expectException("Configuration `default` has not been defined.");
  168. $this->assertFalse(Session::isStarted('default'));
  169. }
  170. public function testReadFilter() {
  171. Session::config(array(
  172. 'primary' => array('adapter' => new Memory(), 'filters' => array()),
  173. 'secondary' => array('adapter' => new Memory(), 'filters' => array())
  174. ));
  175. Session::applyFilter('read', function($self, $params, $chain) {
  176. $result = $chain->next($self, $params, $chain);
  177. if (isset($params['options']['increment'])) {
  178. $result += $params['options']['increment'];
  179. }
  180. return $result;
  181. });
  182. Session::write('foo', 'bar');
  183. $this->assertEqual('bar', Session::read('foo'));
  184. Session::write('bar', 1);
  185. $this->assertEqual(2, Session::read('bar', array('increment' => 1)));
  186. }
  187. public function testStrategies() {
  188. Session::config(array('primary' => array(
  189. 'adapter' => new Memory(), 'filters' => array(), 'strategies' => array(
  190. 'lithium\storage\cache\strategy\Json'
  191. )
  192. )));
  193. Session::write('test', array('foo' => 'bar'));
  194. $this->assertEqual(array('foo' => 'bar'), Session::read('test'));
  195. $this->assertTrue(Session::check('test'));
  196. $this->assertTrue(Session::check('test', array('strategies' => false)));
  197. $result = Session::read('test', array('strategies' => false));
  198. $this->assertEqual('{"foo":"bar"}', $result);
  199. $result = Session::clear(array('strategies' => false));
  200. $this->assertNull(Session::read('test'));
  201. $this->assertFalse(Session::check('test'));
  202. $this->assertFalse(Session::check('test', array('strategies' => false)));
  203. }
  204. public function testMultipleStrategies() {
  205. Session::config(array(
  206. 'primary' => array(
  207. 'adapter' => new Memory(),
  208. 'filters' => array(),
  209. 'strategies' => array()
  210. ),
  211. 'secondary' => array(
  212. 'adapter' => new Memory(),
  213. 'filters' => array(),
  214. 'strategies' => array('lithium\storage\cache\strategy\Json')
  215. )
  216. ));
  217. Session::write('test', array('foo' => 'bar'));
  218. $result = Session::read('test');
  219. $this->assertEqual(array('foo' => 'bar'), $result);
  220. $result = Session::read('test', array('name' => 'primary', 'strategies' => false));
  221. $this->assertEqual(array('foo' => 'bar'), $result);
  222. $result = Session::read('test', array('name' => 'secondary', 'strategies' => false));
  223. $this->assertEqual('{"foo":"bar"}', $result);
  224. }
  225. public function testEncryptedStrategy() {
  226. $this->skipIf(!MockEncrypt::enabled(), 'The Mcrypt extension is not installed or enabled.');
  227. $key = 'foobar';
  228. $adapter = new Memory();
  229. Session::config(array('primary' => array(
  230. 'adapter' => $adapter, 'filters' => array(), 'strategies' => array(
  231. 'lithium\tests\mocks\storage\session\strategy\MockEncrypt' => array(
  232. 'secret' => $key
  233. )
  234. )
  235. )));
  236. $value = array('foo' => 'bar');
  237. Session::write('test', $value);
  238. $this->assertEqual(array('foo' => 'bar'), Session::read('test'));
  239. $this->assertTrue(Session::check('test'));
  240. $this->assertTrue(Session::check('test', array('strategies' => false)));
  241. $encrypted = Session::read('test', array('strategies' => false));
  242. $this->assertNotEqual($value, $encrypted);
  243. $this->assertTrue(is_string($encrypted));
  244. $result = Session::read('test');
  245. $this->assertEqual($value, $result);
  246. $result = Session::clear(array('strategies' => false));
  247. $this->assertNull(Session::read('test'));
  248. $this->assertFalse(Session::check('test'));
  249. $this->assertFalse(Session::check('test', array('strategies' => false)));
  250. $savedData = array('test' => $value);
  251. $encrypt = new MockEncrypt(array('secret' => $key));
  252. $result = $encrypt->encrypt($savedData);
  253. $this->assertEqual($encrypted, $result);
  254. $result = $encrypt->decrypt($encrypted);
  255. $this->assertEqual($savedData, $result);
  256. }
  257. }
  258. ?>