SessionComponentTest.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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. * @package Cake.Test.Case.Controller.Component
  15. * @since CakePHP(tm) v 1.2.0.5436
  16. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  17. */
  18. App::uses('Controller', 'Controller');
  19. App::uses('SessionComponent', 'Controller/Component');
  20. /**
  21. * SessionTestController class
  22. *
  23. * @package Cake.Test.Case.Controller.Component
  24. */
  25. class SessionTestController extends Controller {
  26. /**
  27. * uses property
  28. *
  29. * @var array
  30. */
  31. public $uses = array();
  32. /**
  33. * sessionId method
  34. *
  35. * @return string
  36. */
  37. public function sessionId() {
  38. return $this->Session->id();
  39. }
  40. }
  41. /**
  42. * OrangeSessionTestController class
  43. *
  44. * @package Cake.Test.Case.Controller.Component
  45. */
  46. class OrangeSessionTestController extends Controller {
  47. /**
  48. * uses property
  49. *
  50. * @var array
  51. */
  52. public $uses = array();
  53. /**
  54. * sessionId method
  55. *
  56. * @return string
  57. */
  58. public function sessionId() {
  59. return $this->Session->id();
  60. }
  61. }
  62. /**
  63. * SessionComponentTest class
  64. *
  65. * @package Cake.Test.Case.Controller.Component
  66. */
  67. class SessionComponentTest extends CakeTestCase {
  68. protected static $_sessionBackup;
  69. /**
  70. * fixtures
  71. *
  72. * @var string
  73. */
  74. public $fixtures = array('core.session');
  75. /**
  76. * test case startup
  77. *
  78. * @return void
  79. */
  80. public static function setupBeforeClass() {
  81. self::$_sessionBackup = Configure::read('Session');
  82. Configure::write('Session', array(
  83. 'defaults' => 'php',
  84. 'timeout' => 100,
  85. 'cookie' => 'test'
  86. ));
  87. }
  88. /**
  89. * cleanup after test case.
  90. *
  91. * @return void
  92. */
  93. public static function teardownAfterClass() {
  94. Configure::write('Session', self::$_sessionBackup);
  95. }
  96. /**
  97. * setUp method
  98. *
  99. * @return void
  100. */
  101. public function setUp() {
  102. parent::setUp();
  103. $_SESSION = null;
  104. $this->ComponentCollection = new ComponentCollection();
  105. }
  106. /**
  107. * tearDown method
  108. *
  109. * @return void
  110. */
  111. public function tearDown() {
  112. parent::tearDown();
  113. CakeSession::destroy();
  114. }
  115. /**
  116. * ensure that session ids don't change when request action is called.
  117. *
  118. * @return void
  119. */
  120. public function testSessionIdConsistentAcrossRequestAction() {
  121. $Object = new Object();
  122. $Session = new SessionComponent($this->ComponentCollection);
  123. $expected = $Session->id();
  124. $result = $Object->requestAction('/session_test/sessionId');
  125. $this->assertEquals($expected, $result);
  126. $result = $Object->requestAction('/orange_session_test/sessionId');
  127. $this->assertEquals($expected, $result);
  128. }
  129. /**
  130. * testSessionValid method
  131. *
  132. * @return void
  133. */
  134. public function testSessionValid() {
  135. $Session = new SessionComponent($this->ComponentCollection);
  136. $this->assertTrue($Session->valid());
  137. Configure::write('Session.checkAgent', true);
  138. $Session->userAgent('rweerw');
  139. $this->assertFalse($Session->valid());
  140. $Session = new SessionComponent($this->ComponentCollection);
  141. $Session->time = $Session->read('Config.time') + 1;
  142. $this->assertFalse($Session->valid());
  143. }
  144. /**
  145. * testSessionError method
  146. *
  147. * @return void
  148. */
  149. public function testSessionError() {
  150. CakeSession::$lastError = null;
  151. $Session = new SessionComponent($this->ComponentCollection);
  152. $this->assertFalse($Session->error());
  153. }
  154. /**
  155. * testSessionReadWrite method
  156. *
  157. * @return void
  158. */
  159. public function testSessionReadWrite() {
  160. $Session = new SessionComponent($this->ComponentCollection);
  161. $this->assertNull($Session->read('Test'));
  162. $this->assertTrue($Session->write('Test', 'some value'));
  163. $this->assertEquals('some value', $Session->read('Test'));
  164. $Session->delete('Test');
  165. $this->assertTrue($Session->write('Test.key.path', 'some value'));
  166. $this->assertEquals('some value', $Session->read('Test.key.path'));
  167. $this->assertEquals(array('path' => 'some value'), $Session->read('Test.key'));
  168. $this->assertTrue($Session->write('Test.key.path2', 'another value'));
  169. $this->assertEquals(array('path' => 'some value', 'path2' => 'another value'), $Session->read('Test.key'));
  170. $Session->delete('Test');
  171. $array = array('key1' => 'val1', 'key2' => 'val2', 'key3' => 'val3');
  172. $this->assertTrue($Session->write('Test', $array));
  173. $this->assertEquals($Session->read('Test'), $array);
  174. $Session->delete('Test');
  175. $this->assertTrue($Session->write(array('Test'), 'some value'));
  176. $this->assertTrue($Session->write(array('Test' => 'some value')));
  177. $this->assertEquals('some value', $Session->read('Test'));
  178. $Session->delete('Test');
  179. }
  180. /**
  181. * testSessionDelete method
  182. *
  183. * @return void
  184. */
  185. public function testSessionDelete() {
  186. $Session = new SessionComponent($this->ComponentCollection);
  187. $this->assertFalse($Session->delete('Test'));
  188. $Session->write('Test', 'some value');
  189. $this->assertTrue($Session->delete('Test'));
  190. }
  191. /**
  192. * testSessionCheck method
  193. *
  194. * @return void
  195. */
  196. public function testSessionCheck() {
  197. $Session = new SessionComponent($this->ComponentCollection);
  198. $this->assertFalse($Session->check('Test'));
  199. $Session->write('Test', 'some value');
  200. $this->assertTrue($Session->check('Test'));
  201. $Session->delete('Test');
  202. }
  203. /**
  204. * testSessionFlash method
  205. *
  206. * @return void
  207. */
  208. public function testSessionFlash() {
  209. $Session = new SessionComponent($this->ComponentCollection);
  210. $this->assertNull($Session->read('Message.flash'));
  211. $Session->setFlash('This is a test message');
  212. $this->assertEquals(array('message' => 'This is a test message', 'element' => 'default', 'params' => array()), $Session->read('Message.flash'));
  213. $Session->setFlash('This is a test message', 'test', array('name' => 'Joel Moss'));
  214. $this->assertEquals(array('message' => 'This is a test message', 'element' => 'test', 'params' => array('name' => 'Joel Moss')), $Session->read('Message.flash'));
  215. $Session->setFlash('This is a test message', 'default', array(), 'myFlash');
  216. $this->assertEquals(array('message' => 'This is a test message', 'element' => 'default', 'params' => array()), $Session->read('Message.myFlash'));
  217. $Session->setFlash('This is a test message', 'non_existing_layout');
  218. $this->assertEquals(array('message' => 'This is a test message', 'element' => 'default', 'params' => array()), $Session->read('Message.myFlash'));
  219. $Session->delete('Message');
  220. }
  221. /**
  222. * testSessionId method
  223. *
  224. * @return void
  225. */
  226. public function testSessionId() {
  227. unset($_SESSION);
  228. $Session = new SessionComponent($this->ComponentCollection);
  229. CakeSession::start();
  230. $this->assertEquals(session_id(), $Session->id());
  231. }
  232. /**
  233. * testSessionDestroy method
  234. *
  235. * @return void
  236. */
  237. public function testSessionDestroy() {
  238. $Session = new SessionComponent($this->ComponentCollection);
  239. $Session->write('Test', 'some value');
  240. $this->assertEquals('some value', $Session->read('Test'));
  241. $Session->destroy('Test');
  242. $this->assertNull($Session->read('Test'));
  243. }
  244. }