SessionTest.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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\integration\storage;
  9. use lithium\storage\Session;
  10. class SessionTest extends \lithium\test\Integration {
  11. public function skip() {
  12. $this->skipIf(PHP_SAPI === 'cli', 'No session support in cli SAPI');
  13. }
  14. public function tearDown() {
  15. Session::clear();
  16. }
  17. public function testPhpReadWriteDelete() {
  18. $config = array('name' => 'phpInt');
  19. Session::config(array(
  20. $config['name'] => array(
  21. 'adapter' => 'Php'
  22. )
  23. ));
  24. Session::clear($config);
  25. $key1 = 'key_one';
  26. $value1 = 'value_one';
  27. $key2 = 'key_two';
  28. $value2 = 'value_two';
  29. $this->assertNull(Session::read($key1, $config));
  30. $this->assertTrue(Session::write($key1, $value1, $config));
  31. $this->assertEqual($value1, Session::read($key1, $config));
  32. $this->assertNull(Session::read($key2, $config));
  33. $this->assertTrue(Session::delete($key1, $config));
  34. $this->assertNull(Session::read($key1, $config));
  35. }
  36. public function testCookieReadWriteDelete() {
  37. $config = array('name' => 'cookieInt');
  38. Session::config(array(
  39. $config['name'] => array(
  40. 'adapter' => 'Cookie'
  41. )
  42. ));
  43. Session::clear($config);
  44. $key1 = 'key_one';
  45. $value1 = 'value_one';
  46. $key2 = 'key_two';
  47. $value2 = 'value_two';
  48. $this->assertNull(Session::read($key1, $config));
  49. $this->assertTrue(Session::write($key1, $value1, $config));
  50. $this->assertCookie(array('key' => $key1, 'value' => $value1));
  51. $this->assertNull(Session::read($key2, $config));
  52. $this->assertTrue(Session::delete($key1, $config));
  53. $this->assertCookie(array('key' => $key1, 'value' => 'deleted'));
  54. $this->assertNoCookie(array('key' => $key2, 'value' => $value2));
  55. $this->assertNull(Session::read($key1, $config));
  56. }
  57. public function testMemoryReadWriteDelete() {
  58. $config = array('name' => 'memoryInt');
  59. Session::config(array(
  60. $config['name'] => array(
  61. 'adapter' => 'Memory'
  62. )
  63. ));
  64. Session::clear($config);
  65. $key1 = 'key_one';
  66. $value1 = 'value_one';
  67. $key2 = 'key_two';
  68. $value2 = 'value_two';
  69. $this->assertNull(Session::read($key1, $config));
  70. $this->assertTrue(Session::write($key1, $value1, $config));
  71. $this->assertEqual($value1, Session::read($key1, $config));
  72. $this->assertNull(Session::read($key2, $config));
  73. $this->assertTrue(Session::delete($key1, $config));
  74. $this->assertNull(Session::read($key1, $config));
  75. }
  76. public function testNamespacesWithPhpAdapter() {
  77. $config = array('name' => 'namespaceInt');
  78. Session::config(array(
  79. $config['name'] => array(
  80. 'adapter' => 'Php'
  81. )
  82. ));
  83. Session::clear($config);
  84. $key1 = 'really.deep.nested.key';
  85. $value1 = 'nested_val';
  86. $key2 = 'shallow.key';
  87. $value2 = 'shallow_val';
  88. $this->assertTrue(Session::write($key1, $value1, $config));
  89. $this->assertTrue(Session::write($key2, $value2, $config));
  90. $this->assertEqual($value1, Session::read($key1, $config));
  91. $this->assertEqual($value2, Session::read($key2, $config));
  92. $expected = array('nested' => array('key' => $value1));
  93. $this->assertEqual($expected, Session::read('really.deep', $config));
  94. }
  95. public function testHmacStrategyWithPhpAdapter() {
  96. $config = array('name' => 'hmacInt');
  97. Session::config(array(
  98. $config['name'] => array(
  99. 'adapter' => 'Php',
  100. 'strategies' => array(
  101. 'Hmac' => array(
  102. 'secret' => 's3cr3t'
  103. )
  104. )
  105. )
  106. ));
  107. Session::clear($config);
  108. $key = 'test';
  109. $value = 'value';
  110. $this->assertTrue(Session::write($key, $value, $config));
  111. $this->assertEqual($value, Session::read($key, $config));
  112. $this->assertTrue(Session::delete($key, $config));
  113. $this->assertNull(Session::read($key, $config));
  114. Session::clear($config);
  115. $this->assertTrue(Session::write('foo', 'bar', $config));
  116. $this->assertEqual('bar', Session::read('foo', $config));
  117. $this->assertTrue(Session::write('foo', 'bar1', $config));
  118. $this->assertEqual('bar1', Session::read('foo', $config));
  119. Session::clear($config);
  120. $this->assertTrue(Session::write($key, $value, $config));
  121. $this->assertEqual($value, Session::read($key, $config));
  122. $cache = $_SESSION;
  123. $_SESSION['injectedkey'] = 'hax0r';
  124. $this->expectException('/Possible data tampering: HMAC signature does not match data./');
  125. Session::read($key, $config);
  126. $_SESSION = $cache;
  127. }
  128. public function testEncryptStrategyWithPhpAdapter() {
  129. $config = array('name' => 'encryptInt');
  130. Session::config(array(
  131. $config['name'] => array(
  132. 'adapter' => 'Php',
  133. 'strategies' => array(
  134. 'Encrypt' => array(
  135. 'secret' => 's3cr3t'
  136. )
  137. )
  138. )
  139. ));
  140. Session::clear($config);
  141. $key = 'test';
  142. $value = 'value';
  143. $this->assertTrue(Session::write($key, $value, $config));
  144. $this->assertEqual($value, Session::read($key, $config));
  145. $this->assertTrue(Session::delete($key, $config));
  146. $this->assertNull(Session::read($key, $config));
  147. Session::clear($config);
  148. $this->assertTrue(Session::write('foo', 'bar', $config));
  149. $this->assertEqual('bar', Session::read('foo', $config));
  150. $this->assertTrue(Session::write('foo', 'bar1', $config));
  151. $this->assertEqual('bar1', Session::read('foo', $config));
  152. Session::clear($config);
  153. $this->assertTrue(Session::write($key, $value, $config));
  154. $this->assertEqual($value, Session::read($key, $config));
  155. }
  156. }
  157. ?>