PageRenderTime 37ms CodeModel.GetById 10ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://github.com/hikmanet/HnsAutomobiles
PHP | 400 lines | 216 code | 37 blank | 147 comment | 1 complexity | 5b126bf9497917829da55d113cabd954 MD5 | raw file
  1. <?php
  2. /* SVN FILE: $Id: session.test.php 8130 2009-03-26 14:25:47Z renan.saddam $ */
  3. /**
  4. * SessionTest file
  5. *
  6. * Long description for file
  7. *
  8. * PHP versions 4 and 5
  9. *
  10. * CakePHP(tm) Tests <https://trac.cakephp.org/wiki/Developement/TestSuite>
  11. * Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
  12. *
  13. * Licensed under The Open Group Test Suite License
  14. * Redistributions of files must retain the above copyright notice.
  15. *
  16. * @filesource
  17. * @copyright Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
  18. * @link https://trac.cakephp.org/wiki/Developement/TestSuite CakePHP(tm) Tests
  19. * @package cake
  20. * @subpackage cake.tests.cases.libs
  21. * @since CakePHP(tm) v 1.2.0.4206
  22. * @version $Revision: 8130 $
  23. * @modifiedby $LastChangedBy: renan.saddam $
  24. * @lastmodified $Date: 2009-03-26 09:25:47 -0500 (Thu, 26 Mar 2009) $
  25. * @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License
  26. */
  27. if (!class_exists('CakeSession')) {
  28. App::import('Core', 'Session');
  29. }
  30. /**
  31. * SessionTest class
  32. *
  33. * @package cake
  34. * @subpackage cake.tests.cases.libs
  35. */
  36. class SessionTest extends CakeTestCase {
  37. var $fixtures = array('core.session');
  38. /**
  39. * startCase method
  40. *
  41. * @access public
  42. * @return void
  43. */
  44. function startCase() {
  45. // Make sure garbage colector will be called
  46. $this->__gc_divisor = ini_get('session.gc_divisor');
  47. ini_set('session.gc_divisor', '1');
  48. }
  49. /**
  50. * endCase method
  51. *
  52. * @access public
  53. * @return void
  54. */
  55. function endCase() {
  56. // Revert to the default setting
  57. ini_set('session.gc_divisor', $this->__gc_divisor);
  58. }
  59. /**
  60. * setUp method
  61. *
  62. * @access public
  63. * @return void
  64. */
  65. function setUp() {
  66. $this->Session =& new CakeSession();
  67. $this->Session->start();
  68. $this->Session->_checkValid();
  69. }
  70. /**
  71. * testCheck method
  72. *
  73. * @access public
  74. * @return void
  75. */
  76. function testCheck() {
  77. $this->Session->write('SessionTestCase', 'value');
  78. $this->assertTrue($this->Session->check('SessionTestCase'));
  79. $this->assertFalse($this->Session->check('NotExistingSessionTestCase'), false);
  80. }
  81. /**
  82. * testSimpleRead method
  83. *
  84. * @access public
  85. * @return void
  86. */
  87. function testSimpleRead() {
  88. $this->Session->write('testing', '1,2,3');
  89. $result = $this->Session->read('testing');
  90. $this->assertEqual($result, '1,2,3');
  91. $this->Session->write('testing', array('1' => 'one', '2' => 'two','3' => 'three'));
  92. $result = $this->Session->read('testing.1');
  93. $this->assertEqual($result, 'one');
  94. $result = $this->Session->read('testing');
  95. $this->assertEqual($result, array('1' => 'one', '2' => 'two', '3' => 'three'));
  96. $result = $this->Session->read();
  97. $this->assertTrue(isset($result['testing']));
  98. $this->assertTrue(isset($result['Config']));
  99. $this->assertTrue(isset($result['Config']['userAgent']));
  100. $this->Session->write('This.is.a.deep.array.my.friend', 'value');
  101. $result = $this->Session->read('This.is.a.deep.array.my.friend');
  102. $this->assertEqual('value', $result);
  103. }
  104. /**
  105. * testId method
  106. *
  107. * @access public
  108. * @return void
  109. */
  110. function testId() {
  111. $expected = session_id();
  112. $result = $this->Session->id();
  113. $this->assertEqual($result, $expected);
  114. $this->Session->id('MySessionId');
  115. $result = $this->Session->id();
  116. $this->assertEqual($result, 'MySessionId');
  117. }
  118. /**
  119. * testStarted method
  120. *
  121. * @access public
  122. * @return void
  123. */
  124. function testStarted() {
  125. $this->assertTrue($this->Session->started());
  126. unset($_SESSION);
  127. $this->assertFalse($this->Session->started());
  128. $this->assertTrue($this->Session->start());
  129. }
  130. /**
  131. * testError method
  132. *
  133. * @access public
  134. * @return void
  135. */
  136. function testError() {
  137. $this->Session->read('Does.not.exist');
  138. $result = $this->Session->error();
  139. $this->assertEqual($result, "Does.not.exist doesn't exist");
  140. $this->Session->del('Failing.delete');
  141. $result = $this->Session->error();
  142. $this->assertEqual($result, "Failing.delete doesn't exist");
  143. }
  144. /**
  145. * testDel method
  146. *
  147. * @access public
  148. * @return void
  149. */
  150. function testDel() {
  151. $this->assertTrue($this->Session->write('Delete.me', 'Clearing out'));
  152. $this->assertTrue($this->Session->del('Delete.me'));
  153. $this->assertFalse($this->Session->check('Delete.me'));
  154. $this->assertTrue($this->Session->check('Delete'));
  155. $this->assertTrue($this->Session->write('Clearing.sale', 'everything must go'));
  156. $this->assertTrue($this->Session->del('Clearing'));
  157. $this->assertFalse($this->Session->check('Clearing.sale'));
  158. $this->assertFalse($this->Session->check('Clearing'));
  159. }
  160. /**
  161. * testWatchVar method
  162. *
  163. * @access public
  164. * @return void
  165. */
  166. function testWatchVar() {
  167. $this->assertFalse($this->Session->watch(null));
  168. $this->Session->write('Watching', "I'm watching you");
  169. $this->Session->watch('Watching');
  170. $this->expectError('Writing session key {Watching}: "They found us!"');
  171. $this->Session->write('Watching', 'They found us!');
  172. $this->expectError('Deleting session key {Watching}');
  173. $this->Session->del('Watching');
  174. $this->assertFalse($this->Session->watch('Invalid.key'));
  175. }
  176. /**
  177. * testIgnore method
  178. *
  179. * @access public
  180. * @return void
  181. */
  182. function testIgnore() {
  183. $this->Session->write('Watching', "I'm watching you");
  184. $this->Session->watch('Watching');
  185. $this->Session->ignore('Watching');
  186. $this->assertTrue($this->Session->write('Watching', 'They found us!'));
  187. }
  188. /**
  189. * testDestroy method
  190. *
  191. * @access public
  192. * @return void
  193. */
  194. function testDestroy() {
  195. $this->Session->write('bulletProof', 'invicible');
  196. $id = $this->Session->id();
  197. $this->Session->destroy();
  198. $this->assertFalse($this->Session->check('bulletProof'));
  199. $this->assertNotEqual($id, $this->Session->id());
  200. }
  201. /**
  202. * testCheckingSavedEmpty method
  203. *
  204. * @access public
  205. * @return void
  206. */
  207. function testCheckingSavedEmpty() {
  208. $this->assertTrue($this->Session->write('SessionTestCase', 0));
  209. $this->assertTrue($this->Session->check('SessionTestCase'));
  210. $this->assertTrue($this->Session->write('SessionTestCase', '0'));
  211. $this->assertTrue($this->Session->check('SessionTestCase'));
  212. $this->assertTrue($this->Session->write('SessionTestCase', false));
  213. $this->assertTrue($this->Session->check('SessionTestCase'));
  214. $this->assertTrue($this->Session->write('SessionTestCase', null));
  215. $this->assertFalse($this->Session->check('SessionTestCase'));
  216. }
  217. /**
  218. * testCheckKeyWithSpaces method
  219. *
  220. * @access public
  221. * @return void
  222. */
  223. function testCheckKeyWithSpaces() {
  224. $this->assertTrue($this->Session->write('Session Test', "test"));
  225. $this->assertEqual($this->Session->check('Session Test'), 'test');
  226. $this->Session->del('Session Test');
  227. $this->assertTrue($this->Session->write('Session Test.Test Case', "test"));
  228. $this->assertTrue($this->Session->check('Session Test.Test Case'));
  229. }
  230. /**
  231. * testReadingSavedEmpty method
  232. *
  233. * @access public
  234. * @return void
  235. */
  236. function testReadingSavedEmpty() {
  237. $this->Session->write('SessionTestCase', 0);
  238. $this->assertEqual($this->Session->read('SessionTestCase'), 0);
  239. $this->Session->write('SessionTestCase', '0');
  240. $this->assertEqual($this->Session->read('SessionTestCase'), '0');
  241. $this->assertFalse($this->Session->read('SessionTestCase') === 0);
  242. $this->Session->write('SessionTestCase', false);
  243. $this->assertFalse($this->Session->read('SessionTestCase'));
  244. $this->Session->write('SessionTestCase', null);
  245. $this->assertEqual($this->Session->read('SessionTestCase'), null);
  246. }
  247. /**
  248. * testCheckUserAgentFalse method
  249. *
  250. * @access public
  251. * @return void
  252. */
  253. function testCheckUserAgentFalse() {
  254. Configure::write('Session.checkAgent', false);
  255. $this->Session->_userAgent = md5('http://randomdomainname.com' . Configure::read('Security.salt'));
  256. $this->assertTrue($this->Session->valid());
  257. }
  258. /**
  259. * testCheckUserAgentTrue method
  260. *
  261. * @access public
  262. * @return void
  263. */
  264. function testCheckUserAgentTrue() {
  265. Configure::write('Session.checkAgent', true);
  266. $this->Session->_userAgent = md5('http://randomdomainname.com' . Configure::read('Security.salt'));
  267. $this->assertFalse($this->Session->valid());
  268. }
  269. /**
  270. * testReadAndWriteWithDatabaseStorage method
  271. *
  272. * @access public
  273. * @return void
  274. */
  275. function testReadAndWriteWithCakeStorage() {
  276. unset($_SESSION);
  277. session_destroy();
  278. ini_set('session.save_handler', 'files');
  279. Configure::write('Session.save', 'cake');
  280. $this->setUp();
  281. $this->Session->write('SessionTestCase', 0);
  282. $this->assertEqual($this->Session->read('SessionTestCase'), 0);
  283. $this->Session->write('SessionTestCase', '0');
  284. $this->assertEqual($this->Session->read('SessionTestCase'), '0');
  285. $this->assertFalse($this->Session->read('SessionTestCase') === 0);
  286. $this->Session->write('SessionTestCase', false);
  287. $this->assertFalse($this->Session->read('SessionTestCase'));
  288. $this->Session->write('SessionTestCase', null);
  289. $this->assertEqual($this->Session->read('SessionTestCase'), null);
  290. $this->Session->write('SessionTestCase', 'This is a Test');
  291. $this->assertEqual($this->Session->read('SessionTestCase'), 'This is a Test');
  292. $this->Session->write('SessionTestCase', 'This is a Test');
  293. $this->Session->write('SessionTestCase', 'This was updated');
  294. $this->assertEqual($this->Session->read('SessionTestCase'), 'This was updated');
  295. $this->Session->destroy();
  296. $this->assertFalse($this->Session->read('SessionTestCase'));
  297. }
  298. /**
  299. * testReadAndWriteWithDatabaseStorage method
  300. *
  301. * @access public
  302. * @return void
  303. */
  304. function testReadAndWriteWithCacheStorage() {
  305. unset($_SESSION);
  306. session_destroy();
  307. ini_set('session.save_handler', 'files');
  308. Configure::write('Session.save', 'cache');
  309. $this->setUp();
  310. $this->Session->write('SessionTestCase', 0);
  311. $this->assertEqual($this->Session->read('SessionTestCase'), 0);
  312. $this->Session->write('SessionTestCase', '0');
  313. $this->assertEqual($this->Session->read('SessionTestCase'), '0');
  314. $this->assertFalse($this->Session->read('SessionTestCase') === 0);
  315. $this->Session->write('SessionTestCase', false);
  316. $this->assertFalse($this->Session->read('SessionTestCase'));
  317. $this->Session->write('SessionTestCase', null);
  318. $this->assertEqual($this->Session->read('SessionTestCase'), null);
  319. $this->Session->write('SessionTestCase', 'This is a Test');
  320. $this->assertEqual($this->Session->read('SessionTestCase'), 'This is a Test');
  321. $this->Session->write('SessionTestCase', 'This is a Test');
  322. $this->Session->write('SessionTestCase', 'This was updated');
  323. $this->assertEqual($this->Session->read('SessionTestCase'), 'This was updated');
  324. $this->Session->destroy();
  325. $this->assertFalse($this->Session->read('SessionTestCase'));
  326. }
  327. /**
  328. * testReadAndWriteWithDatabaseStorage method
  329. *
  330. * @access public
  331. * @return void
  332. */
  333. function testReadAndWriteWithDatabaseStorage() {
  334. unset($_SESSION);
  335. session_destroy();
  336. Configure::write('Session.table', 'sessions');
  337. Configure::write('Session.database', 'test');
  338. Configure::write('Session.save', 'database');
  339. $this->setUp();
  340. $this->Session->write('SessionTestCase', 0);
  341. $this->assertEqual($this->Session->read('SessionTestCase'), 0);
  342. $this->Session->write('SessionTestCase', '0');
  343. $this->assertEqual($this->Session->read('SessionTestCase'), '0');
  344. $this->assertFalse($this->Session->read('SessionTestCase') === 0);
  345. $this->Session->write('SessionTestCase', false);
  346. $this->assertFalse($this->Session->read('SessionTestCase'));
  347. $this->Session->write('SessionTestCase', null);
  348. $this->assertEqual($this->Session->read('SessionTestCase'), null);
  349. $this->Session->write('SessionTestCase', 'This is a Test');
  350. $this->assertEqual($this->Session->read('SessionTestCase'), 'This is a Test');
  351. $this->Session->destroy();
  352. $this->assertFalse($this->Session->read('SessionTestCase'));
  353. session_write_close();
  354. unset($_SESSION);
  355. ini_set('session.save_handler', 'files');
  356. Configure::write('Session.save', 'php');
  357. $this->setUp();
  358. }
  359. }
  360. ?>