SessionComponentTest.php 7.2 KB

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