SessionComponentTest.php 7.4 KB

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