SessionComponentTest.php 7.2 KB

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