/lib/Cake/Test/Case/Controller/Component/SessionComponentTest.php

https://github.com/bellthoven/cakephp · PHP · 293 lines · 126 code · 45 blank · 122 comment · 0 complexity · e5b61bff9ba61a726646a79c0a7523d4 MD5 · raw file

  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-2011, 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-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
  14. * @link http://book.cakephp.org/view/1196/Testing 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. * session_id method
  35. *
  36. * @return string
  37. */
  38. public function session_id() {
  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. * session_id method
  56. *
  57. * @return string
  58. */
  59. public function session_id() {
  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/session_id');
  129. $this->assertEquals($expected, $result);
  130. $result = $Object->requestAction('/orange_session_test/session_id');
  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($Session->read('Test'), 'some value');
  167. $this->assertFalse($Session->write('Test.key', 'some value'));
  168. $Session->delete('Test');
  169. $this->assertTrue($Session->write('Test.key.path', 'some value'));
  170. $this->assertEquals($Session->read('Test.key.path'), 'some value');
  171. $this->assertEquals($Session->read('Test.key'), array('path' => 'some value'));
  172. $this->assertTrue($Session->write('Test.key.path2', 'another value'));
  173. $this->assertEquals($Session->read('Test.key'), array('path' => 'some value', 'path2' => 'another value'));
  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->assertFalse($Session->write(array('Test'), 'some value'));
  180. $this->assertTrue($Session->write(array('Test' => 'some value')));
  181. $this->assertEquals($Session->read('Test'), 'some value');
  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($Session->read('Message.flash'), array('message' => 'This is a test message', 'element' => 'default', 'params' => array()));
  217. $Session->setFlash('This is a test message', 'test', array('name' => 'Joel Moss'));
  218. $this->assertEquals($Session->read('Message.flash'), array('message' => 'This is a test message', 'element' => 'test', 'params' => array('name' => 'Joel Moss')));
  219. $Session->setFlash('This is a test message', 'default', array(), 'myFlash');
  220. $this->assertEquals($Session->read('Message.myFlash'), array('message' => 'This is a test message', 'element' => 'default', 'params' => array()));
  221. $Session->setFlash('This is a test message', 'non_existing_layout');
  222. $this->assertEquals($Session->read('Message.myFlash'), array('message' => 'This is a test message', 'element' => 'default', 'params' => array()));
  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($Session->read('Test'), 'some value');
  245. $Session->destroy('Test');
  246. $this->assertNull($Session->read('Test'));
  247. }
  248. }