CakeSessionTest.php 20 KB

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