SessionComponentTest.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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. $Controller = new Controller();
  88. $Session = new SessionComponent($this->ComponentRegistry);
  89. $expected = $Session->id();
  90. $result = $Controller->requestAction('/session_test/session_id');
  91. $this->assertEquals($expected, $result);
  92. $result = $Controller->requestAction('/orange_session_test/session_id');
  93. $this->assertEquals($expected, $result);
  94. }
  95. /**
  96. * testSessionValid method
  97. *
  98. * @return void
  99. */
  100. public function testSessionValid() {
  101. $Session = new SessionComponent($this->ComponentRegistry);
  102. $this->assertTrue($Session->valid());
  103. Configure::write('Session.checkAgent', true);
  104. $Session->userAgent('rweerw');
  105. $this->assertFalse($Session->valid());
  106. $Session = new SessionComponent($this->ComponentRegistry);
  107. $Session->time = $Session->read('Config.time') + 1;
  108. $this->assertFalse($Session->valid());
  109. }
  110. /**
  111. * testSessionError method
  112. *
  113. * @return void
  114. */
  115. public function testSessionError() {
  116. Session::$lastError = null;
  117. $Session = new SessionComponent($this->ComponentRegistry);
  118. $this->assertFalse($Session->error());
  119. }
  120. /**
  121. * testSessionReadWrite method
  122. *
  123. * @return void
  124. */
  125. public function testSessionReadWrite() {
  126. $Session = new SessionComponent($this->ComponentRegistry);
  127. $this->assertNull($Session->read('Test'));
  128. $this->assertTrue($Session->write('Test', 'some value'));
  129. $this->assertEquals('some value', $Session->read('Test'));
  130. $Session->delete('Test');
  131. $this->assertTrue($Session->write('Test.key.path', 'some value'));
  132. $this->assertEquals('some value', $Session->read('Test.key.path'));
  133. $this->assertEquals(array('path' => 'some value'), $Session->read('Test.key'));
  134. $this->assertTrue($Session->write('Test.key.path2', 'another value'));
  135. $this->assertEquals(array('path' => 'some value', 'path2' => 'another value'), $Session->read('Test.key'));
  136. $Session->delete('Test');
  137. $array = array('key1' => 'val1', 'key2' => 'val2', 'key3' => 'val3');
  138. $this->assertTrue($Session->write('Test', $array));
  139. $this->assertEquals($Session->read('Test'), $array);
  140. $Session->delete('Test');
  141. $this->assertTrue($Session->write(array('Test'), 'some value'));
  142. $this->assertTrue($Session->write(array('Test' => 'some value')));
  143. $this->assertEquals('some value', $Session->read('Test'));
  144. $Session->delete('Test');
  145. }
  146. /**
  147. * testSessionDelete method
  148. *
  149. * @return void
  150. */
  151. public function testSessionDelete() {
  152. $Session = new SessionComponent($this->ComponentRegistry);
  153. $this->assertFalse($Session->delete('Test'));
  154. $Session->write('Test', 'some value');
  155. $this->assertTrue($Session->delete('Test'));
  156. }
  157. /**
  158. * testSessionCheck method
  159. *
  160. * @return void
  161. */
  162. public function testSessionCheck() {
  163. $Session = new SessionComponent($this->ComponentRegistry);
  164. $this->assertFalse($Session->check('Test'));
  165. $Session->write('Test', 'some value');
  166. $this->assertTrue($Session->check('Test'));
  167. $Session->delete('Test');
  168. }
  169. /**
  170. * testSessionFlash method
  171. *
  172. * @return void
  173. */
  174. public function testSessionFlash() {
  175. $Session = new SessionComponent($this->ComponentRegistry);
  176. $this->assertNull($Session->read('Message.flash'));
  177. $Session->setFlash('This is a test message');
  178. $this->assertEquals(array('message' => 'This is a test message', 'element' => 'default', 'params' => array()), $Session->read('Message.flash'));
  179. $Session->setFlash('This is a test message', 'test', array('name' => 'Joel Moss'));
  180. $this->assertEquals(array('message' => 'This is a test message', 'element' => 'test', 'params' => array('name' => 'Joel Moss')), $Session->read('Message.flash'));
  181. $Session->setFlash('This is a test message', 'default', array(), 'myFlash');
  182. $this->assertEquals(array('message' => 'This is a test message', 'element' => 'default', 'params' => array()), $Session->read('Message.myFlash'));
  183. $Session->setFlash('This is a test message', 'non_existing_layout');
  184. $this->assertEquals(array('message' => 'This is a test message', 'element' => 'default', 'params' => array()), $Session->read('Message.myFlash'));
  185. $Session->delete('Message');
  186. }
  187. /**
  188. * testSessionId method
  189. *
  190. * @return void
  191. */
  192. public function testSessionId() {
  193. unset($_SESSION);
  194. $Session = new SessionComponent($this->ComponentRegistry);
  195. Session::start();
  196. $this->assertEquals(session_id(), $Session->id());
  197. }
  198. /**
  199. * testSessionDestroy method
  200. *
  201. * @return void
  202. */
  203. public function testSessionDestroy() {
  204. $Session = new SessionComponent($this->ComponentRegistry);
  205. $Session->write('Test', 'some value');
  206. $this->assertEquals('some value', $Session->read('Test'));
  207. $Session->destroy('Test');
  208. $this->assertNull($Session->read('Test'));
  209. }
  210. }