SessionComponentTest.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. <?php
  2. /**
  3. * SessionComponentTest file
  4. *
  5. * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
  6. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  7. *
  8. * Licensed under The MIT License
  9. * For full copyright and license information, please see the LICENSE.txt
  10. * Redistributions of files must retain the above copyright notice
  11. *
  12. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  13. * @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
  14. * @since CakePHP(tm) v 1.2.0.5436
  15. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  16. */
  17. namespace Cake\Test\TestCase\Controller\Component;
  18. use Cake\Controller\ComponentRegistry;
  19. use Cake\Controller\Component\SessionComponent;
  20. use Cake\Controller\Controller;
  21. use Cake\Core\Configure;
  22. use Cake\Core\Object;
  23. use Cake\Network\Session;
  24. use Cake\Routing\Router;
  25. use Cake\TestSuite\TestCase;
  26. /**
  27. * SessionComponentTest class
  28. *
  29. */
  30. class SessionComponentTest extends TestCase {
  31. protected static $_sessionBackup;
  32. /**
  33. * fixtures
  34. *
  35. * @var string
  36. */
  37. public $fixtures = array('core.session');
  38. /**
  39. * test case startup
  40. *
  41. * @return void
  42. */
  43. public static function setupBeforeClass() {
  44. static::$_sessionBackup = Configure::read('Session');
  45. Configure::write('Session', array(
  46. 'defaults' => 'php',
  47. 'timeout' => 100,
  48. 'cookie' => 'test'
  49. ));
  50. }
  51. /**
  52. * cleanup after test case.
  53. *
  54. * @return void
  55. */
  56. public static function teardownAfterClass() {
  57. Configure::write('Session', static::$_sessionBackup);
  58. }
  59. /**
  60. * setUp method
  61. *
  62. * @return void
  63. */
  64. public function setUp() {
  65. parent::setUp();
  66. $_SESSION = null;
  67. Configure::write('App.namespace', 'TestApp');
  68. $this->ComponentRegistry = new ComponentRegistry();
  69. }
  70. /**
  71. * tearDown method
  72. *
  73. * @return void
  74. */
  75. public function tearDown() {
  76. parent::tearDown();
  77. Session::destroy();
  78. }
  79. /**
  80. * ensure that session ids don't change when request action is called.
  81. *
  82. * @return void
  83. */
  84. public function testSessionIdConsistentAcrossRequestAction() {
  85. Configure::write('App.namespace', 'TestApp');
  86. Router::connect('/:controller/:action');
  87. $Session = new SessionComponent($this->ComponentRegistry);
  88. $Session->check('Test');
  89. $this->assertTrue(isset($_SESSION));
  90. $Controller = new Controller();
  91. $Session = new SessionComponent($this->ComponentRegistry);
  92. $expected = $Session->id();
  93. $result = $Controller->requestAction('/session_test/session_id');
  94. $this->assertEquals($expected, $result);
  95. $result = $Controller->requestAction('/orange_session_test/session_id');
  96. $this->assertEquals($expected, $result);
  97. }
  98. /**
  99. * testSessionValid method
  100. *
  101. * @return void
  102. */
  103. public function testSessionValid() {
  104. $Session = new SessionComponent($this->ComponentRegistry);
  105. $this->assertTrue($Session->valid());
  106. Configure::write('Session.checkAgent', true);
  107. $Session->userAgent('rweerw');
  108. $this->assertFalse($Session->valid());
  109. $Session = new SessionComponent($this->ComponentRegistry);
  110. $Session->time = $Session->read('Config.time') + 1;
  111. $this->assertFalse($Session->valid());
  112. }
  113. /**
  114. * testSessionError method
  115. *
  116. * @return void
  117. */
  118. public function testSessionError() {
  119. Session::$lastError = null;
  120. $Session = new SessionComponent($this->ComponentRegistry);
  121. $this->assertFalse($Session->error());
  122. }
  123. /**
  124. * testSessionReadWrite method
  125. *
  126. * @return void
  127. */
  128. public function testSessionReadWrite() {
  129. $Session = new SessionComponent($this->ComponentRegistry);
  130. $this->assertNull($Session->read('Test'));
  131. $this->assertTrue($Session->write('Test', 'some value'));
  132. $this->assertEquals('some value', $Session->read('Test'));
  133. $Session->delete('Test');
  134. $this->assertTrue($Session->write('Test.key.path', 'some value'));
  135. $this->assertEquals('some value', $Session->read('Test.key.path'));
  136. $this->assertEquals(array('path' => 'some value'), $Session->read('Test.key'));
  137. $this->assertTrue($Session->write('Test.key.path2', 'another value'));
  138. $this->assertEquals(array('path' => 'some value', 'path2' => 'another value'), $Session->read('Test.key'));
  139. $Session->delete('Test');
  140. $array = array('key1' => 'val1', 'key2' => 'val2', 'key3' => 'val3');
  141. $this->assertTrue($Session->write('Test', $array));
  142. $this->assertEquals($Session->read('Test'), $array);
  143. $Session->delete('Test');
  144. $this->assertTrue($Session->write(array('Test'), 'some value'));
  145. $this->assertTrue($Session->write(array('Test' => 'some value')));
  146. $this->assertEquals('some value', $Session->read('Test'));
  147. $Session->delete('Test');
  148. }
  149. /**
  150. * testSessionDelete method
  151. *
  152. * @return void
  153. */
  154. public function testSessionDelete() {
  155. $Session = new SessionComponent($this->ComponentRegistry);
  156. $this->assertFalse($Session->delete('Test'));
  157. $Session->write('Test', 'some value');
  158. $this->assertTrue($Session->delete('Test'));
  159. }
  160. /**
  161. * testSessionCheck method
  162. *
  163. * @return void
  164. */
  165. public function testSessionCheck() {
  166. $Session = new SessionComponent($this->ComponentRegistry);
  167. $this->assertFalse($Session->check('Test'));
  168. $Session->write('Test', 'some value');
  169. $this->assertTrue($Session->check('Test'));
  170. $Session->delete('Test');
  171. }
  172. /**
  173. * testSessionFlash method
  174. *
  175. * @return void
  176. */
  177. public function testSessionFlash() {
  178. $Session = new SessionComponent($this->ComponentRegistry);
  179. $this->assertNull($Session->read('Message.flash'));
  180. $Session->setFlash('This is a test message');
  181. $this->assertEquals(array('message' => 'This is a test message', 'element' => 'default', 'params' => array()), $Session->read('Message.flash'));
  182. $Session->setFlash('This is a test message', 'test', array('name' => 'Joel Moss'));
  183. $this->assertEquals(array('message' => 'This is a test message', 'element' => 'test', 'params' => array('name' => 'Joel Moss')), $Session->read('Message.flash'));
  184. $Session->setFlash('This is a test message', 'default', array(), 'myFlash');
  185. $this->assertEquals(array('message' => 'This is a test message', 'element' => 'default', 'params' => array()), $Session->read('Message.myFlash'));
  186. $Session->setFlash('This is a test message', 'non_existing_layout');
  187. $this->assertEquals(array('message' => 'This is a test message', 'element' => 'default', 'params' => array()), $Session->read('Message.myFlash'));
  188. $Session->delete('Message');
  189. }
  190. /**
  191. * testSessionId method
  192. *
  193. * @return void
  194. */
  195. public function testSessionId() {
  196. unset($_SESSION);
  197. $Session = new SessionComponent($this->ComponentRegistry);
  198. $Session->check('test');
  199. $this->assertEquals(session_id(), $Session->id());
  200. }
  201. /**
  202. * testSessionDestroy method
  203. *
  204. * @return void
  205. */
  206. public function testSessionDestroy() {
  207. $Session = new SessionComponent($this->ComponentRegistry);
  208. $Session->write('Test', 'some value');
  209. $this->assertEquals('some value', $Session->read('Test'));
  210. $Session->destroy('Test');
  211. $this->assertNull($Session->read('Test'));
  212. }
  213. }