SessionComponentTest.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. <?php
  2. /**
  3. * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
  4. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. * @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
  12. * @since CakePHP(tm) v 1.2.0.5436
  13. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\Controller\Component;
  16. use Cake\Controller\ComponentRegistry;
  17. use Cake\Controller\Component\SessionComponent;
  18. use Cake\Controller\Controller;
  19. use Cake\Core\Configure;
  20. use Cake\Core\Object;
  21. use Cake\Model\Datasource\Session;
  22. use Cake\Routing\Router;
  23. use Cake\TestSuite\TestCase;
  24. /**
  25. * SessionComponentTest class
  26. *
  27. */
  28. class SessionComponentTest extends TestCase {
  29. protected static $_sessionBackup;
  30. /**
  31. * fixtures
  32. *
  33. * @var string
  34. */
  35. public $fixtures = array('core.session');
  36. /**
  37. * test case startup
  38. *
  39. * @return void
  40. */
  41. public static function setupBeforeClass() {
  42. static::$_sessionBackup = Configure::read('Session');
  43. Configure::write('Session', array(
  44. 'defaults' => 'php',
  45. 'timeout' => 100,
  46. 'cookie' => 'test'
  47. ));
  48. }
  49. /**
  50. * cleanup after test case.
  51. *
  52. * @return void
  53. */
  54. public static function teardownAfterClass() {
  55. Configure::write('Session', static::$_sessionBackup);
  56. }
  57. /**
  58. * setUp method
  59. *
  60. * @return void
  61. */
  62. public function setUp() {
  63. parent::setUp();
  64. $_SESSION = null;
  65. Configure::write('App.namespace', 'TestApp');
  66. $this->ComponentRegistry = new ComponentRegistry();
  67. }
  68. /**
  69. * tearDown method
  70. *
  71. * @return void
  72. */
  73. public function tearDown() {
  74. parent::tearDown();
  75. Session::destroy();
  76. }
  77. /**
  78. * ensure that session ids don't change when request action is called.
  79. *
  80. * @return void
  81. */
  82. public function testSessionIdConsistentAcrossRequestAction() {
  83. Configure::write('App.namespace', 'TestApp');
  84. Router::connect('/:controller/:action');
  85. $Session = new SessionComponent($this->ComponentRegistry);
  86. $Session->check('Test');
  87. $this->assertTrue(isset($_SESSION));
  88. $Controller = new Controller();
  89. $Session = new SessionComponent($this->ComponentRegistry);
  90. $expected = $Session->id();
  91. $result = $Controller->requestAction('/session_test/session_id');
  92. $this->assertEquals($expected, $result);
  93. $result = $Controller->requestAction('/orange_session_test/session_id');
  94. $this->assertEquals($expected, $result);
  95. }
  96. /**
  97. * testSessionValid method
  98. *
  99. * @return void
  100. */
  101. public function testSessionValid() {
  102. $Session = new SessionComponent($this->ComponentRegistry);
  103. $this->assertTrue($Session->valid());
  104. Configure::write('Session.checkAgent', true);
  105. $Session->userAgent('rweerw');
  106. $this->assertFalse($Session->valid());
  107. $Session = new SessionComponent($this->ComponentRegistry);
  108. $Session->time = $Session->read('Config.time') + 1;
  109. $this->assertFalse($Session->valid());
  110. }
  111. /**
  112. * testSessionError method
  113. *
  114. * @return void
  115. */
  116. public function testSessionError() {
  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->check('test');
  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. }