PageRenderTime 48ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/cake/tests/cases/libs/controller/components/session.test.php

https://github.com/cgajardo/repositorium
PHP | 385 lines | 192 code | 55 blank | 138 comment | 0 complexity | 146bde09f5d47ca5c2004c2dfb36c173 MD5 | raw file
  1. <?php
  2. /**
  3. * SessionComponentTest file
  4. *
  5. * PHP versions 4 and 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 Open Group Test Suite 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
  16. * @subpackage cake.tests.cases.libs.controller.components
  17. * @since CakePHP(tm) v 1.2.0.5436
  18. * @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License
  19. */
  20. App::import('Controller', 'Controller', false);
  21. App::import('Component', 'Session');
  22. /**
  23. * SessionTestController class
  24. *
  25. * @package cake
  26. * @subpackage cake.tests.cases.libs.controller.components
  27. */
  28. class SessionTestController extends Controller {
  29. /**
  30. * uses property
  31. *
  32. * @var array
  33. * @access public
  34. */
  35. var $uses = array();
  36. /**
  37. * session_id method
  38. *
  39. * @return string
  40. * @access public
  41. */
  42. function session_id() {
  43. return $this->Session->id();
  44. }
  45. }
  46. /**
  47. * OrangeSessionTestController class
  48. *
  49. * @package cake
  50. * @subpackage cake.tests.cases.libs.controller.components
  51. */
  52. class OrangeSessionTestController extends Controller {
  53. /**
  54. * uses property
  55. *
  56. * @var array
  57. * @access public
  58. */
  59. var $uses = array();
  60. /**
  61. * session_id method
  62. *
  63. * @return string
  64. * @access public
  65. */
  66. function session_id() {
  67. return $this->Session->id();
  68. }
  69. }
  70. /**
  71. * SessionComponentTest class
  72. *
  73. * @package cake
  74. * @subpackage cake.tests.cases.libs.controller.components
  75. */
  76. class SessionComponentTest extends CakeTestCase {
  77. /**
  78. * setUp method
  79. *
  80. * @access public
  81. * @return void
  82. */
  83. function setUp() {
  84. $this->_session = Configure::read('Session');
  85. }
  86. /**
  87. * tearDown method
  88. *
  89. * @access public
  90. * @return void
  91. */
  92. function tearDown() {
  93. Configure::write('Session', $this->_session);
  94. }
  95. /**
  96. * testSessionAutoStart method
  97. *
  98. * @access public
  99. * @return void
  100. */
  101. function testSessionAutoStart() {
  102. Configure::write('Session.start', false);
  103. $Session =& new SessionComponent();
  104. $this->assertFalse($Session->__active);
  105. $this->assertFalse($Session->started());
  106. $Session->startup(new SessionTestController());
  107. Configure::write('Session.start', true);
  108. $Session =& new SessionComponent();
  109. $this->assertTrue($Session->__active);
  110. $this->assertFalse($Session->started());
  111. $Session->startup(new SessionTestController());
  112. $this->assertTrue(isset($_SESSION));
  113. $Object = new Object();
  114. $Session =& new SessionComponent();
  115. $Session->start();
  116. $expected = $Session->id();
  117. $result = $Object->requestAction('/session_test/session_id');
  118. $this->assertEqual($result, $expected);
  119. $result = $Object->requestAction('/orange_session_test/session_id');
  120. $this->assertEqual($result, $expected);
  121. }
  122. /**
  123. * testSessionActivate method
  124. *
  125. * @access public
  126. * @return void
  127. */
  128. function testSessionActivate() {
  129. $Session =& new SessionComponent();
  130. $this->assertTrue($Session->__active);
  131. $this->assertNull($Session->activate());
  132. $this->assertTrue($Session->__active);
  133. Configure::write('Session.start', false);
  134. $Session =& new SessionComponent();
  135. $this->assertFalse($Session->__active);
  136. $this->assertNull($Session->activate());
  137. $this->assertTrue($Session->__active);
  138. Configure::write('Session.start', true);
  139. $Session->destroy();
  140. }
  141. /**
  142. * testSessionValid method
  143. *
  144. * @access public
  145. * @return void
  146. */
  147. function testSessionValid() {
  148. $Session =& new SessionComponent();
  149. $this->assertTrue($Session->valid());
  150. $Session->_userAgent = 'rweerw';
  151. $this->assertFalse($Session->valid());
  152. Configure::write('Session.start', false);
  153. $Session =& new SessionComponent();
  154. $this->assertFalse($Session->__active);
  155. $this->assertFalse($Session->valid());
  156. Configure::write('Session.start', true);
  157. $Session =& new SessionComponent();
  158. $Session->time = $Session->read('Config.time') + 1;
  159. $this->assertFalse($Session->valid());
  160. Configure::write('Session.checkAgent', false);
  161. $Session =& new SessionComponent();
  162. $Session->time = $Session->read('Config.time') + 1;
  163. $this->assertFalse($Session->valid());
  164. Configure::write('Session.checkAgent', true);
  165. }
  166. /**
  167. * testSessionError method
  168. *
  169. * @access public
  170. * @return void
  171. */
  172. function testSessionError() {
  173. $Session =& new SessionComponent();
  174. $this->assertFalse($Session->error());
  175. Configure::write('Session.start', false);
  176. $Session =& new SessionComponent();
  177. $this->assertFalse($Session->__active);
  178. $this->assertFalse($Session->error());
  179. Configure::write('Session.start', true);
  180. }
  181. /**
  182. * testSessionReadWrite method
  183. *
  184. * @access public
  185. * @return void
  186. */
  187. function testSessionReadWrite() {
  188. $Session =& new SessionComponent();
  189. $this->assertFalse($Session->read('Test'));
  190. $this->assertTrue($Session->write('Test', 'some value'));
  191. $this->assertEqual($Session->read('Test'), 'some value');
  192. $this->assertFalse($Session->write('Test.key', 'some value'));
  193. $Session->delete('Test');
  194. $this->assertTrue($Session->write('Test.key.path', 'some value'));
  195. $this->assertEqual($Session->read('Test.key.path'), 'some value');
  196. $this->assertEqual($Session->read('Test.key'), array('path' => 'some value'));
  197. $this->assertTrue($Session->write('Test.key.path2', 'another value'));
  198. $this->assertEqual($Session->read('Test.key'), array('path' => 'some value', 'path2' => 'another value'));
  199. $Session->delete('Test');
  200. $array = array('key1' => 'val1', 'key2' => 'val2', 'key3' => 'val3');
  201. $this->assertTrue($Session->write('Test', $array));
  202. $this->assertEqual($Session->read('Test'), $array);
  203. $Session->delete('Test');
  204. $this->assertFalse($Session->write(array('Test'), 'some value'));
  205. $this->assertTrue($Session->write(array('Test' => 'some value')));
  206. $this->assertEqual($Session->read('Test'), 'some value');
  207. $Session->delete('Test');
  208. Configure::write('Session.start', false);
  209. $Session =& new SessionComponent();
  210. $this->assertFalse($Session->write('Test', 'some value'));
  211. $Session->write('Test', 'some value');
  212. $this->assertFalse($Session->read('Test'));
  213. Configure::write('Session.start', true);
  214. }
  215. /**
  216. * testSessionDelete method
  217. *
  218. * @access public
  219. * @return void
  220. */
  221. function testSessionDelete() {
  222. $Session =& new SessionComponent();
  223. $this->assertFalse($Session->delete('Test'));
  224. $Session->write('Test', 'some value');
  225. $this->assertTrue($Session->delete('Test'));
  226. Configure::write('Session.start', false);
  227. $Session =& new SessionComponent();
  228. $Session->write('Test', 'some value');
  229. $this->assertFalse($Session->delete('Test'));
  230. Configure::write('Session.start', true);
  231. }
  232. /**
  233. * testSessionCheck method
  234. *
  235. * @access public
  236. * @return void
  237. */
  238. function testSessionCheck() {
  239. $Session =& new SessionComponent();
  240. $this->assertFalse($Session->check('Test'));
  241. $Session->write('Test', 'some value');
  242. $this->assertTrue($Session->check('Test'));
  243. $Session->delete('Test');
  244. Configure::write('Session.start', false);
  245. $Session =& new SessionComponent();
  246. $Session->write('Test', 'some value');
  247. $this->assertFalse($Session->check('Test'));
  248. Configure::write('Session.start', true);
  249. }
  250. /**
  251. * testSessionFlash method
  252. *
  253. * @access public
  254. * @return void
  255. */
  256. function testSessionFlash() {
  257. $Session =& new SessionComponent();
  258. $this->assertNull($Session->read('Message.flash'));
  259. $Session->setFlash('This is a test message');
  260. $this->assertEqual($Session->read('Message.flash'), array('message' => 'This is a test message', 'element' => 'default', 'params' => array()));
  261. $Session->setFlash('This is a test message', 'test', array('name' => 'Joel Moss'));
  262. $this->assertEqual($Session->read('Message.flash'), array('message' => 'This is a test message', 'element' => 'test', 'params' => array('name' => 'Joel Moss')));
  263. $Session->setFlash('This is a test message', 'default', array(), 'myFlash');
  264. $this->assertEqual($Session->read('Message.myFlash'), array('message' => 'This is a test message', 'element' => 'default', 'params' => array()));
  265. $Session->setFlash('This is a test message', 'non_existing_layout');
  266. $this->assertEqual($Session->read('Message.myFlash'), array('message' => 'This is a test message', 'element' => 'default', 'params' => array()));
  267. $Session->delete('Message');
  268. }
  269. /**
  270. * testSessionId method
  271. *
  272. * @access public
  273. * @return void
  274. */
  275. function testSessionId() {
  276. unset($_SESSION);
  277. $Session =& new SessionComponent();
  278. $this->assertNull($Session->id());
  279. }
  280. /**
  281. * testSessionDestroy method
  282. *
  283. * @access public
  284. * @return void
  285. */
  286. function testSessionDestroy() {
  287. $Session =& new SessionComponent();
  288. $Session->write('Test', 'some value');
  289. $this->assertEqual($Session->read('Test'), 'some value');
  290. $Session->destroy('Test');
  291. $this->assertNull($Session->read('Test'));
  292. }
  293. /**
  294. * testSessionTimeout method
  295. *
  296. * @access public
  297. * @return void
  298. */
  299. function testSessionTimeout() {
  300. Configure::write('debug', 2);
  301. Configure::write('Security.level', 'low');
  302. session_destroy();
  303. $Session =& new SessionComponent();
  304. $Session->destroy();
  305. $Session->write('Test', 'some value');
  306. $this->assertEqual($Session->sessionTime, time() + (300 * Configure::read('Session.timeout')));
  307. $this->assertEqual($_SESSION['Config']['timeout'], 10);
  308. $this->assertEqual($_SESSION['Config']['time'], $Session->sessionTime);
  309. $this->assertEqual($Session->time, time());
  310. $this->assertEqual($_SESSION['Config']['time'], $Session->time + (300 * Configure::read('Session.timeout')));
  311. Configure::write('Security.level', 'medium');
  312. $Session =& new SessionComponent();
  313. $Session->destroy();
  314. $Session->write('Test', 'some value');
  315. $this->assertEqual($Session->sessionTime, mktime() + (100 * Configure::read('Session.timeout')));
  316. $this->assertEqual($_SESSION['Config']['timeout'], 10);
  317. $this->assertEqual($_SESSION['Config']['time'], $Session->sessionTime);
  318. $this->assertEqual($Session->time, time());
  319. $this->assertEqual($_SESSION['Config']['time'], $Session->time + (Security::inactiveMins() * Configure::read('Session.timeout')));
  320. Configure::write('Security.level', 'high');
  321. $Session =& new SessionComponent();
  322. $Session->destroy();
  323. $Session->write('Test', 'some value');
  324. $this->assertEqual($Session->sessionTime, time() + (10 * Configure::read('Session.timeout')));
  325. $this->assertEqual($_SESSION['Config']['timeout'], 10);
  326. $this->assertEqual($_SESSION['Config']['time'], $Session->sessionTime);
  327. $this->assertEqual($Session->time, time());
  328. $this->assertEqual($_SESSION['Config']['time'], $Session->time + (Security::inactiveMins() * Configure::read('Session.timeout')));
  329. }
  330. }