SessionComponentTest.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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 1.2.0
  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\Network\Session;
  23. use Cake\Routing\Router;
  24. use Cake\TestSuite\TestCase;
  25. /**
  26. * SessionComponentTest class
  27. *
  28. */
  29. class SessionComponentTest extends TestCase {
  30. protected static $_sessionBackup;
  31. /**
  32. * fixtures
  33. *
  34. * @var string
  35. */
  36. public $fixtures = array('core.session');
  37. /**
  38. * test case startup
  39. *
  40. * @return void
  41. */
  42. public static function setupBeforeClass() {
  43. static::$_sessionBackup = Configure::read('Session');
  44. Configure::write('Session', array(
  45. 'defaults' => 'php',
  46. 'timeout' => 100,
  47. 'cookie' => 'test'
  48. ));
  49. }
  50. /**
  51. * cleanup after test case.
  52. *
  53. * @return void
  54. */
  55. public static function teardownAfterClass() {
  56. Configure::write('Session', static::$_sessionBackup);
  57. }
  58. /**
  59. * setUp method
  60. *
  61. * @return void
  62. */
  63. public function setUp() {
  64. parent::setUp();
  65. $_SESSION = null;
  66. Configure::write('App.namespace', 'TestApp');
  67. $this->ComponentRegistry = new ComponentRegistry();
  68. }
  69. /**
  70. * tearDown method
  71. *
  72. * @return void
  73. */
  74. public function tearDown() {
  75. parent::tearDown();
  76. Session::destroy();
  77. }
  78. /**
  79. * ensure that session ids don't change when request action is called.
  80. *
  81. * @return void
  82. */
  83. public function testSessionIdConsistentAcrossRequestAction() {
  84. Configure::write('App.namespace', 'TestApp');
  85. Router::connect('/:controller/:action');
  86. $Controller = new Controller();
  87. $Session = new SessionComponent($this->ComponentRegistry);
  88. $expected = $Session->id();
  89. $result = $Controller->requestAction('/session_test/session_id');
  90. $this->assertEquals($expected, $result);
  91. $result = $Controller->requestAction('/orange_session_test/session_id');
  92. $this->assertEquals($expected, $result);
  93. }
  94. /**
  95. * testSessionValid method
  96. *
  97. * @return void
  98. */
  99. public function testSessionValid() {
  100. $Session = new SessionComponent($this->ComponentRegistry);
  101. $this->assertTrue($Session->valid());
  102. Configure::write('Session.checkAgent', true);
  103. $Session->userAgent('rweerw');
  104. $this->assertFalse($Session->valid());
  105. $Session = new SessionComponent($this->ComponentRegistry);
  106. $Session->time = $Session->read('Config.time') + 1;
  107. $this->assertFalse($Session->valid());
  108. }
  109. /**
  110. * testSessionError method
  111. *
  112. * @return void
  113. */
  114. public function testSessionError() {
  115. Session::$lastError = null;
  116. $Session = new SessionComponent($this->ComponentRegistry);
  117. $this->assertFalse($Session->error());
  118. }
  119. /**
  120. * testSessionReadWrite method
  121. *
  122. * @return void
  123. */
  124. public function testSessionReadWrite() {
  125. $Session = new SessionComponent($this->ComponentRegistry);
  126. $this->assertNull($Session->read('Test'));
  127. $this->assertTrue($Session->write('Test', 'some value'));
  128. $this->assertEquals('some value', $Session->read('Test'));
  129. $Session->delete('Test');
  130. $this->assertTrue($Session->write('Test.key.path', 'some value'));
  131. $this->assertEquals('some value', $Session->read('Test.key.path'));
  132. $this->assertEquals(array('path' => 'some value'), $Session->read('Test.key'));
  133. $this->assertTrue($Session->write('Test.key.path2', 'another value'));
  134. $this->assertEquals(array('path' => 'some value', 'path2' => 'another value'), $Session->read('Test.key'));
  135. $Session->delete('Test');
  136. $array = array('key1' => 'val1', 'key2' => 'val2', 'key3' => 'val3');
  137. $this->assertTrue($Session->write('Test', $array));
  138. $this->assertEquals($Session->read('Test'), $array);
  139. $Session->delete('Test');
  140. $this->assertTrue($Session->write(array('Test'), 'some value'));
  141. $this->assertTrue($Session->write(array('Test' => 'some value')));
  142. $this->assertEquals('some value', $Session->read('Test'));
  143. $Session->delete('Test');
  144. }
  145. /**
  146. * testSessionDelete method
  147. *
  148. * @return void
  149. */
  150. public function testSessionDelete() {
  151. $Session = new SessionComponent($this->ComponentRegistry);
  152. $this->assertFalse($Session->delete('Test'));
  153. $Session->write('Test', 'some value');
  154. $this->assertTrue($Session->delete('Test'));
  155. }
  156. /**
  157. * testSessionCheck method
  158. *
  159. * @return void
  160. */
  161. public function testSessionCheck() {
  162. $Session = new SessionComponent($this->ComponentRegistry);
  163. $this->assertFalse($Session->check('Test'));
  164. $Session->write('Test', 'some value');
  165. $this->assertTrue($Session->check('Test'));
  166. $Session->delete('Test');
  167. }
  168. /**
  169. * testSessionFlash method
  170. *
  171. * @return void
  172. */
  173. public function testSessionFlash() {
  174. $Session = new SessionComponent($this->ComponentRegistry);
  175. $this->assertNull($Session->read('Message.flash'));
  176. $Session->setFlash('This is a test message');
  177. $this->assertEquals(array('message' => 'This is a test message', 'element' => 'default', 'params' => array()), $Session->read('Message.flash'));
  178. $Session->setFlash('This is a test message', 'test', array('name' => 'Joel Moss'));
  179. $this->assertEquals(array('message' => 'This is a test message', 'element' => 'test', 'params' => array('name' => 'Joel Moss')), $Session->read('Message.flash'));
  180. $Session->setFlash('This is a test message', 'default', array(), 'myFlash');
  181. $this->assertEquals(array('message' => 'This is a test message', 'element' => 'default', 'params' => array()), $Session->read('Message.myFlash'));
  182. $Session->setFlash('This is a test message', 'non_existing_layout');
  183. $this->assertEquals(array('message' => 'This is a test message', 'element' => 'default', 'params' => array()), $Session->read('Message.myFlash'));
  184. $Session->delete('Message');
  185. }
  186. /**
  187. * testSessionId method
  188. *
  189. * @return void
  190. */
  191. public function testSessionId() {
  192. unset($_SESSION);
  193. $Session = new SessionComponent($this->ComponentRegistry);
  194. Session::start();
  195. $this->assertEquals(session_id(), $Session->id());
  196. }
  197. /**
  198. * testSessionDestroy method
  199. *
  200. * @return void
  201. */
  202. public function testSessionDestroy() {
  203. $Session = new SessionComponent($this->ComponentRegistry);
  204. $Session->write('Test', 'some value');
  205. $this->assertEquals('some value', $Session->read('Test'));
  206. $Session->destroy('Test');
  207. $this->assertNull($Session->read('Test'));
  208. }
  209. }