CakeSessionTest.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754
  1. <?php
  2. /**
  3. * SessionTest 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.Model.Datasource
  16. * @since CakePHP(tm) v 1.2.0.4206
  17. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  18. */
  19. App::uses('CakeSession', 'Model/Datasource');
  20. App::uses('DatabaseSession', 'Model/Datasource/Session');
  21. App::uses('CacheSession', 'Model/Datasource/Session');
  22. class TestCakeSession extends CakeSession {
  23. public static function setUserAgent($value) {
  24. self::$_userAgent = $value;
  25. }
  26. public static function setHost($host) {
  27. self::_setHost($host);
  28. }
  29. }
  30. class TestCacheSession extends CacheSession {
  31. protected function _writeSession() {
  32. return true;
  33. }
  34. }
  35. class TestDatabaseSession extends DatabaseSession {
  36. protected function _writeSession() {
  37. return true;
  38. }
  39. }
  40. /**
  41. * CakeSessionTest class
  42. *
  43. * @package Cake.Test.Case.Model.Datasource
  44. */
  45. class CakeSessionTest extends CakeTestCase {
  46. protected static $_gcDivisor;
  47. /**
  48. * Fixtures used in the SessionTest
  49. *
  50. * @var array
  51. */
  52. public $fixtures = array('core.session');
  53. /**
  54. * setup before class.
  55. *
  56. * @return void
  57. */
  58. public static function setupBeforeClass() {
  59. // Make sure garbage colector will be called
  60. self::$_gcDivisor = ini_get('session.gc_divisor');
  61. ini_set('session.gc_divisor', '1');
  62. }
  63. /**
  64. * teardown after class
  65. *
  66. * @return void
  67. */
  68. public static function teardownAfterClass() {
  69. // Revert to the default setting
  70. ini_set('session.gc_divisor', self::$_gcDivisor);
  71. }
  72. /**
  73. * setUp method
  74. *
  75. * @return void
  76. */
  77. public function setUp() {
  78. parent::setUp();
  79. Configure::write('Session', array(
  80. 'defaults' => 'php',
  81. 'cookie' => 'cakephp',
  82. 'timeout' => 120,
  83. 'cookieTimeout' => 120,
  84. 'ini' => array(),
  85. ));
  86. TestCakeSession::init();
  87. }
  88. /**
  89. * tearDown method
  90. *
  91. * @return void
  92. */
  93. public function teardown() {
  94. if (TestCakeSession::started()) {
  95. session_write_close();
  96. }
  97. unset($_SESSION);
  98. parent::teardown();
  99. }
  100. /**
  101. * test setting ini properties with Session configuration.
  102. *
  103. * @return void
  104. */
  105. public function testSessionConfigIniSetting() {
  106. $_SESSION = null;
  107. Configure::write('Session', array(
  108. 'cookie' => 'test',
  109. 'checkAgent' => false,
  110. 'timeout' => 86400,
  111. 'ini' => array(
  112. 'session.referer_check' => 'example.com',
  113. 'session.use_trans_sid' => false
  114. )
  115. ));
  116. TestCakeSession::start();
  117. $this->assertEquals('', ini_get('session.use_trans_sid'), 'Ini value is incorrect');
  118. $this->assertEquals('example.com', ini_get('session.referer_check'), 'Ini value is incorrect');
  119. $this->assertEquals('test', ini_get('session.name'), 'Ini value is incorrect');
  120. }
  121. /**
  122. * testSessionPath
  123. *
  124. * @return void
  125. */
  126. public function testSessionPath() {
  127. TestCakeSession::init('/index.php');
  128. $this->assertEquals('/', TestCakeSession::$path);
  129. TestCakeSession::init('/sub_dir/index.php');
  130. $this->assertEquals('/sub_dir/', TestCakeSession::$path);
  131. }
  132. /**
  133. * testCakeSessionPathEmpty
  134. *
  135. * @return void
  136. */
  137. public function testCakeSessionPathEmpty() {
  138. TestCakeSession::init('');
  139. $this->assertEquals('/', TestCakeSession::$path, 'Session path is empty, with "" as $base needs to be /');
  140. }
  141. /**
  142. * testCakeSessionPathContainsParams
  143. *
  144. * @return void
  145. */
  146. public function testCakeSessionPathContainsQuestion() {
  147. TestCakeSession::init('/index.php?');
  148. $this->assertEquals('/', TestCakeSession::$path);
  149. }
  150. /**
  151. * testSetHost
  152. *
  153. * @return void
  154. */
  155. public function testSetHost() {
  156. TestCakeSession::init();
  157. TestCakeSession::setHost('cakephp.org');
  158. $this->assertEquals('cakephp.org', TestCakeSession::$host);
  159. }
  160. /**
  161. * testSetHostWithPort
  162. *
  163. * @return void
  164. */
  165. public function testSetHostWithPort() {
  166. TestCakeSession::init();
  167. TestCakeSession::setHost('cakephp.org:443');
  168. $this->assertEquals('cakephp.org', TestCakeSession::$host);
  169. }
  170. /**
  171. * test valid with bogus user agent.
  172. *
  173. * @return void
  174. */
  175. public function testValidBogusUserAgent() {
  176. Configure::write('Session.checkAgent', true);
  177. TestCakeSession::start();
  178. $this->assertTrue(TestCakeSession::valid(), 'Newly started session should be valid');
  179. TestCakeSession::userAgent('bogus!');
  180. $this->assertFalse(TestCakeSession::valid(), 'user agent mismatch should fail.');
  181. }
  182. /**
  183. * test valid with bogus user agent.
  184. *
  185. * @return void
  186. */
  187. public function testValidTimeExpiry() {
  188. Configure::write('Session.checkAgent', true);
  189. TestCakeSession::start();
  190. $this->assertTrue(TestCakeSession::valid(), 'Newly started session should be valid');
  191. TestCakeSession::$time = strtotime('next year');
  192. $this->assertFalse(TestCakeSession::valid(), 'time should cause failure.');
  193. }
  194. /**
  195. * testCheck method
  196. *
  197. * @return void
  198. */
  199. public function testCheck() {
  200. TestCakeSession::write('SessionTestCase', 'value');
  201. $this->assertTrue(TestCakeSession::check('SessionTestCase'));
  202. $this->assertFalse(TestCakeSession::check('NotExistingSessionTestCase'));
  203. }
  204. /**
  205. * testSimpleRead method
  206. *
  207. * @return void
  208. */
  209. public function testSimpleRead() {
  210. TestCakeSession::write('testing', '1,2,3');
  211. $result = TestCakeSession::read('testing');
  212. $this->assertEquals('1,2,3', $result);
  213. TestCakeSession::write('testing', array('1' => 'one', '2' => 'two','3' => 'three'));
  214. $result = TestCakeSession::read('testing.1');
  215. $this->assertEquals('one', $result);
  216. $result = TestCakeSession::read('testing');
  217. $this->assertEquals(array('1' => 'one', '2' => 'two', '3' => 'three'), $result);
  218. $result = TestCakeSession::read();
  219. $this->assertTrue(isset($result['testing']));
  220. $this->assertTrue(isset($result['Config']));
  221. $this->assertTrue(isset($result['Config']['userAgent']));
  222. TestCakeSession::write('This.is.a.deep.array.my.friend', 'value');
  223. $result = TestCakeSession::read('This.is.a.deep.array.my.friend');
  224. $this->assertEquals('value', $result);
  225. }
  226. /**
  227. * testReadyEmpty
  228. *
  229. * @return void
  230. */
  231. public function testReadyEmpty() {
  232. $this->assertFalse(TestCakeSession::read(''));
  233. }
  234. /**
  235. * test writing a hash of values/
  236. *
  237. * @return void
  238. */
  239. public function testWriteArray() {
  240. $result = TestCakeSession::write(array(
  241. 'one' => 1,
  242. 'two' => 2,
  243. 'three' => array('something'),
  244. 'null' => null
  245. ));
  246. $this->assertTrue($result);
  247. $this->assertEquals(1, TestCakeSession::read('one'));
  248. $this->assertEquals(array('something'), TestCakeSession::read('three'));
  249. $this->assertEquals(null, TestCakeSession::read('null'));
  250. }
  251. /**
  252. * testWriteEmptyKey
  253. *
  254. * @return void
  255. */
  256. public function testWriteEmptyKey() {
  257. $this->assertFalse(TestCakeSession::write('', 'graham'));
  258. $this->assertFalse(TestCakeSession::write('', ''));
  259. $this->assertFalse(TestCakeSession::write(''));
  260. }
  261. /**
  262. * Test overwriting a string value as if it were an array.
  263. *
  264. * @return void
  265. */
  266. public function testWriteOverwriteStringValue() {
  267. TestCakeSession::write('Some.string', 'value');
  268. $this->assertEquals('value', TestCakeSession::read('Some.string'));
  269. TestCakeSession::write('Some.string.array', array('values'));
  270. $this->assertEquals(
  271. array('values'),
  272. TestCakeSession::read('Some.string.array')
  273. );
  274. }
  275. /**
  276. * testId method
  277. *
  278. * @return void
  279. */
  280. public function testId() {
  281. TestCakeSession::destroy();
  282. $result = TestCakeSession::id();
  283. $expected = session_id();
  284. $this->assertEquals($expected, $result);
  285. TestCakeSession::id('MySessionId');
  286. $result = TestCakeSession::id();
  287. $this->assertEquals('MySessionId', $result);
  288. }
  289. /**
  290. * testStarted method
  291. *
  292. * @return void
  293. */
  294. public function testStarted() {
  295. unset($_SESSION);
  296. $_SESSION = null;
  297. $this->assertFalse(TestCakeSession::started());
  298. $this->assertTrue(TestCakeSession::start());
  299. $this->assertTrue(TestCakeSession::started());
  300. }
  301. /**
  302. * testError method
  303. *
  304. * @return void
  305. */
  306. public function testError() {
  307. TestCakeSession::read('Does.not.exist');
  308. $result = TestCakeSession::error();
  309. $this->assertEquals("Does.not.exist doesn't exist", $result);
  310. TestCakeSession::delete('Failing.delete');
  311. $result = TestCakeSession::error();
  312. $this->assertEquals("Failing.delete doesn't exist", $result);
  313. }
  314. /**
  315. * testDel method
  316. *
  317. * @return void
  318. */
  319. public function testDelete() {
  320. $this->assertTrue(TestCakeSession::write('Delete.me', 'Clearing out'));
  321. $this->assertTrue(TestCakeSession::delete('Delete.me'));
  322. $this->assertFalse(TestCakeSession::check('Delete.me'));
  323. $this->assertTrue(TestCakeSession::check('Delete'));
  324. $this->assertTrue(TestCakeSession::write('Clearing.sale', 'everything must go'));
  325. $this->assertTrue(TestCakeSession::delete('Clearing'));
  326. $this->assertFalse(TestCakeSession::check('Clearing.sale'));
  327. $this->assertFalse(TestCakeSession::check('Clearing'));
  328. }
  329. /**
  330. * testDestroy method
  331. *
  332. * @return void
  333. */
  334. public function testDestroy() {
  335. TestCakeSession::write('bulletProof', 'invincible');
  336. $id = TestCakeSession::id();
  337. TestCakeSession::destroy();
  338. $this->assertFalse(TestCakeSession::check('bulletProof'));
  339. $this->assertNotEquals(TestCakeSession::id(), $id);
  340. }
  341. /**
  342. * testCheckingSavedEmpty method
  343. *
  344. * @return void
  345. */
  346. public function testCheckingSavedEmpty() {
  347. $this->assertTrue(TestCakeSession::write('SessionTestCase', 0));
  348. $this->assertTrue(TestCakeSession::check('SessionTestCase'));
  349. $this->assertTrue(TestCakeSession::write('SessionTestCase', '0'));
  350. $this->assertTrue(TestCakeSession::check('SessionTestCase'));
  351. $this->assertTrue(TestCakeSession::write('SessionTestCase', false));
  352. $this->assertTrue(TestCakeSession::check('SessionTestCase'));
  353. $this->assertTrue(TestCakeSession::write('SessionTestCase', null));
  354. $this->assertFalse(TestCakeSession::check('SessionTestCase'));
  355. }
  356. /**
  357. * testCheckKeyWithSpaces method
  358. *
  359. * @return void
  360. */
  361. public function testCheckKeyWithSpaces() {
  362. $this->assertTrue(TestCakeSession::write('Session Test', "test"));
  363. $this->assertTrue(TestCakeSession::check('Session Test'));
  364. TestCakeSession::delete('Session Test');
  365. $this->assertTrue(TestCakeSession::write('Session Test.Test Case', "test"));
  366. $this->assertTrue(TestCakeSession::check('Session Test.Test Case'));
  367. }
  368. /**
  369. * testCheckEmpty
  370. *
  371. * @return void
  372. */
  373. public function testCheckEmpty() {
  374. $this->assertFalse(TestCakeSession::check());
  375. }
  376. /**
  377. * test key exploitation
  378. *
  379. * @return void
  380. */
  381. public function testKeyExploit() {
  382. $key = "a'] = 1; phpinfo(); \$_SESSION['a";
  383. $result = TestCakeSession::write($key, 'haxored');
  384. $this->assertTrue($result);
  385. $result = TestCakeSession::read($key);
  386. $this->assertEquals('haxored', $result);
  387. }
  388. /**
  389. * testReadingSavedEmpty method
  390. *
  391. * @return void
  392. */
  393. public function testReadingSavedEmpty() {
  394. TestCakeSession::write('SessionTestCase', 0);
  395. $this->assertEquals(0, TestCakeSession::read('SessionTestCase'));
  396. TestCakeSession::write('SessionTestCase', '0');
  397. $this->assertEquals('0', TestCakeSession::read('SessionTestCase'));
  398. $this->assertFalse(TestCakeSession::read('SessionTestCase') === 0);
  399. TestCakeSession::write('SessionTestCase', false);
  400. $this->assertFalse(TestCakeSession::read('SessionTestCase'));
  401. TestCakeSession::write('SessionTestCase', null);
  402. $this->assertEquals(null, TestCakeSession::read('SessionTestCase'));
  403. }
  404. /**
  405. * testCheckUserAgentFalse method
  406. *
  407. * @return void
  408. */
  409. public function testCheckUserAgentFalse() {
  410. Configure::write('Session.checkAgent', false);
  411. TestCakeSession::setUserAgent(md5('http://randomdomainname.com' . Configure::read('Security.salt')));
  412. $this->assertTrue(TestCakeSession::valid());
  413. }
  414. /**
  415. * testCheckUserAgentTrue method
  416. *
  417. * @return void
  418. */
  419. public function testCheckUserAgentTrue() {
  420. Configure::write('Session.checkAgent', true);
  421. TestCakeSession::$error = false;
  422. $agent = md5('http://randomdomainname.com' . Configure::read('Security.salt'));
  423. TestCakeSession::write('Config.userAgent', md5('Hacking you!'));
  424. TestCakeSession::setUserAgent($agent);
  425. $this->assertFalse(TestCakeSession::valid());
  426. }
  427. /**
  428. * testReadAndWriteWithCakeStorage method
  429. *
  430. * @return void
  431. */
  432. public function testReadAndWriteWithCakeStorage() {
  433. Configure::write('Session.defaults', 'cake');
  434. TestCakeSession::init();
  435. TestCakeSession::start();
  436. TestCakeSession::write('SessionTestCase', 0);
  437. $this->assertEquals(0, TestCakeSession::read('SessionTestCase'));
  438. TestCakeSession::write('SessionTestCase', '0');
  439. $this->assertEquals('0', TestCakeSession::read('SessionTestCase'));
  440. $this->assertFalse(TestCakeSession::read('SessionTestCase') === 0);
  441. TestCakeSession::write('SessionTestCase', false);
  442. $this->assertFalse(TestCakeSession::read('SessionTestCase'));
  443. TestCakeSession::write('SessionTestCase', null);
  444. $this->assertEquals(null, TestCakeSession::read('SessionTestCase'));
  445. TestCakeSession::write('SessionTestCase', 'This is a Test');
  446. $this->assertEquals('This is a Test', TestCakeSession::read('SessionTestCase'));
  447. TestCakeSession::write('SessionTestCase', 'This is a Test');
  448. TestCakeSession::write('SessionTestCase', 'This was updated');
  449. $this->assertEquals('This was updated', TestCakeSession::read('SessionTestCase'));
  450. TestCakeSession::destroy();
  451. $this->assertNull(TestCakeSession::read('SessionTestCase'));
  452. }
  453. /**
  454. * test using a handler from app/Model/Datasource/Session.
  455. *
  456. * @return void
  457. */
  458. public function testUsingAppLibsHandler() {
  459. App::build(array(
  460. 'Model/Datasource/Session' => array(
  461. CAKE . 'Test' . DS . 'test_app' . DS . 'Model' . DS . 'Datasource' . DS . 'Session' . DS
  462. ),
  463. 'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
  464. ), App::RESET);
  465. Configure::write('Session', array(
  466. 'defaults' => 'cake',
  467. 'handler' => array(
  468. 'engine' => 'TestAppLibSession'
  469. )
  470. ));
  471. TestCakeSession::destroy();
  472. $this->assertTrue(TestCakeSession::started());
  473. App::build();
  474. }
  475. /**
  476. * test using a handler from a plugin.
  477. *
  478. * @return void
  479. */
  480. public function testUsingPluginHandler() {
  481. App::build(array(
  482. 'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
  483. ), App::RESET);
  484. CakePlugin::load('TestPlugin');
  485. Configure::write('Session', array(
  486. 'defaults' => 'cake',
  487. 'handler' => array(
  488. 'engine' => 'TestPlugin.TestPluginSession'
  489. )
  490. ));
  491. TestCakeSession::destroy();
  492. $this->assertTrue(TestCakeSession::started());
  493. App::build();
  494. }
  495. /**
  496. * testReadAndWriteWithCacheStorage method
  497. *
  498. * @return void
  499. */
  500. public function testReadAndWriteWithCacheStorage() {
  501. Configure::write('Session.defaults', 'cache');
  502. Configure::write('Session.handler.engine', 'TestCacheSession');
  503. TestCakeSession::init();
  504. TestCakeSession::destroy();
  505. TestCakeSession::write('SessionTestCase', 0);
  506. $this->assertEquals(0, TestCakeSession::read('SessionTestCase'));
  507. TestCakeSession::write('SessionTestCase', '0');
  508. $this->assertEquals('0', TestCakeSession::read('SessionTestCase'));
  509. $this->assertFalse(TestCakeSession::read('SessionTestCase') === 0);
  510. TestCakeSession::write('SessionTestCase', false);
  511. $this->assertFalse(TestCakeSession::read('SessionTestCase'));
  512. TestCakeSession::write('SessionTestCase', null);
  513. $this->assertEquals(null, TestCakeSession::read('SessionTestCase'));
  514. TestCakeSession::write('SessionTestCase', 'This is a Test');
  515. $this->assertEquals('This is a Test', TestCakeSession::read('SessionTestCase'));
  516. TestCakeSession::write('SessionTestCase', 'This is a Test');
  517. TestCakeSession::write('SessionTestCase', 'This was updated');
  518. $this->assertEquals('This was updated', TestCakeSession::read('SessionTestCase'));
  519. TestCakeSession::destroy();
  520. $this->assertNull(TestCakeSession::read('SessionTestCase'));
  521. }
  522. /**
  523. * test that changing the config name of the cache config works.
  524. *
  525. * @return void
  526. */
  527. public function testReadAndWriteWithCustomCacheConfig() {
  528. Configure::write('Session.defaults', 'cache');
  529. Configure::write('Session.handler.engine', 'TestCacheSession');
  530. Configure::write('Session.handler.config', 'session_test');
  531. Cache::config('session_test', array(
  532. 'engine' => 'File',
  533. 'prefix' => 'session_test_',
  534. ));
  535. TestCakeSession::init();
  536. TestCakeSession::start();
  537. TestCakeSession::write('SessionTestCase', 'Some value');
  538. $this->assertEquals('Some value', TestCakeSession::read('SessionTestCase'));
  539. $id = TestCakeSession::id();
  540. Cache::delete($id, 'session_test');
  541. }
  542. /**
  543. * testReadAndWriteWithDatabaseStorage method
  544. *
  545. * @return void
  546. */
  547. public function testReadAndWriteWithDatabaseStorage() {
  548. Configure::write('Session.defaults', 'database');
  549. Configure::write('Session.handler.engine', 'TestDatabaseSession');
  550. Configure::write('Session.handler.table', 'sessions');
  551. Configure::write('Session.handler.model', 'Session');
  552. Configure::write('Session.handler.database', 'test');
  553. TestCakeSession::init();
  554. $this->assertNull(TestCakeSession::id());
  555. TestCakeSession::start();
  556. $expected = session_id();
  557. $this->assertEquals($expected, TestCakeSession::id());
  558. TestCakeSession::renew();
  559. $this->assertFalse($expected == TestCakeSession::id());
  560. $expected = session_id();
  561. $this->assertEquals($expected, TestCakeSession::id());
  562. TestCakeSession::write('SessionTestCase', 0);
  563. $this->assertEquals(0, TestCakeSession::read('SessionTestCase'));
  564. TestCakeSession::write('SessionTestCase', '0');
  565. $this->assertEquals('0', TestCakeSession::read('SessionTestCase'));
  566. $this->assertFalse(TestCakeSession::read('SessionTestCase') === 0);
  567. TestCakeSession::write('SessionTestCase', false);
  568. $this->assertFalse(TestCakeSession::read('SessionTestCase'));
  569. TestCakeSession::write('SessionTestCase', null);
  570. $this->assertEquals(null, TestCakeSession::read('SessionTestCase'));
  571. TestCakeSession::write('SessionTestCase', 'This is a Test');
  572. $this->assertEquals('This is a Test', TestCakeSession::read('SessionTestCase'));
  573. TestCakeSession::write('SessionTestCase', 'Some additional data');
  574. $this->assertEquals('Some additional data', TestCakeSession::read('SessionTestCase'));
  575. TestCakeSession::destroy();
  576. $this->assertNull(TestCakeSession::read('SessionTestCase'));
  577. Configure::write('Session', array(
  578. 'defaults' => 'php'
  579. ));
  580. TestCakeSession::init();
  581. }
  582. /**
  583. * testSessionTimeout method
  584. *
  585. * @return void
  586. */
  587. public function testSessionTimeout() {
  588. Configure::write('debug', 2);
  589. Configure::write('Session.defaults', 'cake');
  590. Configure::write('Session.autoRegenerate', false);
  591. $timeoutSeconds = Configure::read('Session.timeout') * 60;
  592. TestCakeSession::destroy();
  593. TestCakeSession::write('Test', 'some value');
  594. $this->assertWithinMargin(time() + $timeoutSeconds, CakeSession::$sessionTime, 1);
  595. $this->assertEquals(10, $_SESSION['Config']['countdown']);
  596. $this->assertWithinMargin(CakeSession::$sessionTime, $_SESSION['Config']['time'], 1);
  597. $this->assertWithinMargin(time(), CakeSession::$time, 1);
  598. $this->assertWithinMargin(time() + $timeoutSeconds, $_SESSION['Config']['time'], 1);
  599. Configure::write('Session.harden', true);
  600. TestCakeSession::destroy();
  601. TestCakeSession::write('Test', 'some value');
  602. $this->assertWithinMargin(time() + $timeoutSeconds, CakeSession::$sessionTime, 1);
  603. $this->assertEquals(10, $_SESSION['Config']['countdown']);
  604. $this->assertWithinMargin(CakeSession::$sessionTime, $_SESSION['Config']['time'], 1);
  605. $this->assertWithinMargin(time(), CakeSession::$time, 1);
  606. $this->assertWithinMargin(CakeSession::$time + $timeoutSeconds, $_SESSION['Config']['time'], 1);
  607. }
  608. /**
  609. * Test that cookieTimeout matches timeout when unspecified.
  610. *
  611. * @return void
  612. */
  613. public function testCookieTimeoutFallback() {
  614. $_SESSION = null;
  615. Configure::write('Session', array(
  616. 'defaults' => 'cake',
  617. 'timeout' => 400,
  618. ));
  619. TestCakeSession::start();
  620. $this->assertEquals(400, Configure::read('Session.cookieTimeout'));
  621. $this->assertEquals(400, Configure::read('Session.timeout'));
  622. $this->assertEquals(400 * 60, ini_get('session.cookie_lifetime'));
  623. $this->assertEquals(400 * 60, ini_get('session.gc_maxlifetime'));
  624. $_SESSION = null;
  625. Configure::write('Session', array(
  626. 'defaults' => 'cake',
  627. 'timeout' => 400,
  628. 'cookieTimeout' => 600
  629. ));
  630. TestCakeSession::start();
  631. $this->assertEquals(600, Configure::read('Session.cookieTimeout'));
  632. $this->assertEquals(400, Configure::read('Session.timeout'));
  633. }
  634. }