CakeSessionTest.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732
  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. class TestCakeSession extends CakeSession {
  21. public static function setUserAgent($value) {
  22. self::$_userAgent = $value;
  23. }
  24. public static function setHost($host) {
  25. self::_setHost($host);
  26. }
  27. }
  28. /**
  29. * CakeSessionTest class
  30. *
  31. * @package Cake.Test.Case.Model.Datasource
  32. */
  33. class CakeSessionTest extends CakeTestCase {
  34. protected static $_gcDivisor;
  35. /**
  36. * Fixtures used in the SessionTest
  37. *
  38. * @var array
  39. */
  40. public $fixtures = array('core.session');
  41. /**
  42. * setup before class.
  43. *
  44. * @return void
  45. */
  46. public static function setupBeforeClass() {
  47. // Make sure garbage colector will be called
  48. self::$_gcDivisor = ini_get('session.gc_divisor');
  49. ini_set('session.gc_divisor', '1');
  50. }
  51. /**
  52. * teardown after class
  53. *
  54. * @return void
  55. */
  56. public static function teardownAfterClass() {
  57. // Revert to the default setting
  58. ini_set('session.gc_divisor', self::$_gcDivisor);
  59. }
  60. /**
  61. * setUp method
  62. *
  63. * @return void
  64. */
  65. public function setUp() {
  66. parent::setUp();
  67. Configure::write('Session', array(
  68. 'defaults' => 'php',
  69. 'cookie' => 'cakephp',
  70. 'timeout' => 120,
  71. 'cookieTimeout' => 120,
  72. 'ini' => array(),
  73. ));
  74. TestCakeSession::init();
  75. }
  76. /**
  77. * tearDown method
  78. *
  79. * @return void
  80. */
  81. public function teardown() {
  82. if (TestCakeSession::started()) {
  83. TestCakeSession::clear();
  84. }
  85. unset($_SESSION);
  86. parent::teardown();
  87. }
  88. /**
  89. * test setting ini properties with Session configuration.
  90. *
  91. * @return void
  92. */
  93. public function testSessionConfigIniSetting() {
  94. $_SESSION = null;
  95. Configure::write('Session', array(
  96. 'cookie' => 'test',
  97. 'checkAgent' => false,
  98. 'timeout' => 86400,
  99. 'ini' => array(
  100. 'session.referer_check' => 'example.com',
  101. 'session.use_trans_sid' => false
  102. )
  103. ));
  104. TestCakeSession::start();
  105. $this->assertEquals('', ini_get('session.use_trans_sid'), 'Ini value is incorrect');
  106. $this->assertEquals('example.com', ini_get('session.referer_check'), 'Ini value is incorrect');
  107. $this->assertEquals('test', ini_get('session.name'), 'Ini value is incorrect');
  108. }
  109. /**
  110. * testSessionPath
  111. *
  112. * @return void
  113. */
  114. public function testSessionPath() {
  115. TestCakeSession::init('/index.php');
  116. $this->assertEquals('/', TestCakeSession::$path);
  117. TestCakeSession::init('/sub_dir/index.php');
  118. $this->assertEquals('/sub_dir/', TestCakeSession::$path);
  119. }
  120. /**
  121. * testCakeSessionPathEmpty
  122. *
  123. * @return void
  124. */
  125. public function testCakeSessionPathEmpty() {
  126. TestCakeSession::init('');
  127. $this->assertEquals('/', TestCakeSession::$path, 'Session path is empty, with "" as $base needs to be /');
  128. }
  129. /**
  130. * testCakeSessionPathContainsParams
  131. *
  132. * @return void
  133. */
  134. public function testCakeSessionPathContainsQuestion() {
  135. TestCakeSession::init('/index.php?');
  136. $this->assertEquals('/', TestCakeSession::$path);
  137. }
  138. /**
  139. * testSetHost
  140. *
  141. * @return void
  142. */
  143. public function testSetHost() {
  144. TestCakeSession::init();
  145. TestCakeSession::setHost('cakephp.org');
  146. $this->assertEquals('cakephp.org', TestCakeSession::$host);
  147. }
  148. /**
  149. * testSetHostWithPort
  150. *
  151. * @return void
  152. */
  153. public function testSetHostWithPort() {
  154. TestCakeSession::init();
  155. TestCakeSession::setHost('cakephp.org:443');
  156. $this->assertEquals('cakephp.org', TestCakeSession::$host);
  157. }
  158. /**
  159. * test valid with bogus user agent.
  160. *
  161. * @return void
  162. */
  163. public function testValidBogusUserAgent() {
  164. Configure::write('Session.checkAgent', true);
  165. TestCakeSession::start();
  166. $this->assertTrue(TestCakeSession::valid(), 'Newly started session should be valid');
  167. TestCakeSession::userAgent('bogus!');
  168. $this->assertFalse(TestCakeSession::valid(), 'user agent mismatch should fail.');
  169. }
  170. /**
  171. * test valid with bogus user agent.
  172. *
  173. * @return void
  174. */
  175. public function testValidTimeExpiry() {
  176. Configure::write('Session.checkAgent', true);
  177. TestCakeSession::start();
  178. $this->assertTrue(TestCakeSession::valid(), 'Newly started session should be valid');
  179. TestCakeSession::$time = strtotime('next year');
  180. $this->assertFalse(TestCakeSession::valid(), 'time should cause failure.');
  181. }
  182. /**
  183. * testCheck method
  184. *
  185. * @return void
  186. */
  187. public function testCheck() {
  188. TestCakeSession::write('SessionTestCase', 'value');
  189. $this->assertTrue(TestCakeSession::check('SessionTestCase'));
  190. $this->assertFalse(TestCakeSession::check('NotExistingSessionTestCase'), false);
  191. }
  192. /**
  193. * testSimpleRead method
  194. *
  195. * @return void
  196. */
  197. public function testSimpleRead() {
  198. TestCakeSession::write('testing', '1,2,3');
  199. $result = TestCakeSession::read('testing');
  200. $this->assertEquals('1,2,3', $result);
  201. TestCakeSession::write('testing', array('1' => 'one', '2' => 'two','3' => 'three'));
  202. $result = TestCakeSession::read('testing.1');
  203. $this->assertEquals('one', $result);
  204. $result = TestCakeSession::read('testing');
  205. $this->assertEquals(array('1' => 'one', '2' => 'two', '3' => 'three'), $result);
  206. $result = TestCakeSession::read();
  207. $this->assertTrue(isset($result['testing']));
  208. $this->assertTrue(isset($result['Config']));
  209. $this->assertTrue(isset($result['Config']['userAgent']));
  210. TestCakeSession::write('This.is.a.deep.array.my.friend', 'value');
  211. $result = TestCakeSession::read('This.is.a.deep.array.my.friend');
  212. $this->assertEquals('value', $result);
  213. }
  214. /**
  215. * testReadyEmpty
  216. *
  217. * @return void
  218. */
  219. public function testReadyEmpty() {
  220. $this->assertFalse(TestCakeSession::read(''));
  221. }
  222. /**
  223. * test writing a hash of values/
  224. *
  225. * @return void
  226. */
  227. public function testWriteArray() {
  228. $result = TestCakeSession::write(array(
  229. 'one' => 1,
  230. 'two' => 2,
  231. 'three' => array('something'),
  232. 'null' => null
  233. ));
  234. $this->assertTrue($result);
  235. $this->assertEquals(1, TestCakeSession::read('one'));
  236. $this->assertEquals(array('something'), TestCakeSession::read('three'));
  237. $this->assertEquals(null, TestCakeSession::read('null'));
  238. }
  239. /**
  240. * testWriteEmptyKey
  241. *
  242. * @return void
  243. */
  244. public function testWriteEmptyKey() {
  245. $this->assertFalse(TestCakeSession::write('', 'graham'));
  246. $this->assertFalse(TestCakeSession::write('', ''));
  247. $this->assertFalse(TestCakeSession::write(''));
  248. }
  249. /**
  250. * Test overwriting a string value as if it were an array.
  251. *
  252. * @return void
  253. */
  254. public function testWriteOverwriteStringValue() {
  255. TestCakeSession::write('Some.string', 'value');
  256. $this->assertEquals('value', TestCakeSession::read('Some.string'));
  257. TestCakeSession::write('Some.string.array', array('values'));
  258. $this->assertEquals(
  259. array('values'),
  260. TestCakeSession::read('Some.string.array')
  261. );
  262. }
  263. /**
  264. * testId method
  265. *
  266. * @return void
  267. */
  268. public function testId() {
  269. TestCakeSession::destroy();
  270. $result = TestCakeSession::id();
  271. $expected = session_id();
  272. $this->assertEquals($expected, $result);
  273. TestCakeSession::id('MySessionId');
  274. $result = TestCakeSession::id();
  275. $this->assertEquals('MySessionId', $result);
  276. }
  277. /**
  278. * testStarted method
  279. *
  280. * @return void
  281. */
  282. public function testStarted() {
  283. unset($_SESSION);
  284. $_SESSION = null;
  285. $this->assertFalse(TestCakeSession::started());
  286. $this->assertTrue(TestCakeSession::start());
  287. $this->assertTrue(TestCakeSession::started());
  288. }
  289. /**
  290. * testError method
  291. *
  292. * @return void
  293. */
  294. public function testError() {
  295. TestCakeSession::read('Does.not.exist');
  296. $result = TestCakeSession::error();
  297. $this->assertEquals("Does.not.exist doesn't exist", $result);
  298. TestCakeSession::delete('Failing.delete');
  299. $result = TestCakeSession::error();
  300. $this->assertEquals("Failing.delete doesn't exist", $result);
  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. TestCakeSession::init();
  491. TestCakeSession::destroy();
  492. TestCakeSession::write('SessionTestCase', 0);
  493. $this->assertEquals(0, TestCakeSession::read('SessionTestCase'));
  494. TestCakeSession::write('SessionTestCase', '0');
  495. $this->assertEquals('0', TestCakeSession::read('SessionTestCase'));
  496. $this->assertFalse(TestCakeSession::read('SessionTestCase') === 0);
  497. TestCakeSession::write('SessionTestCase', false);
  498. $this->assertFalse(TestCakeSession::read('SessionTestCase'));
  499. TestCakeSession::write('SessionTestCase', null);
  500. $this->assertEquals(null, TestCakeSession::read('SessionTestCase'));
  501. TestCakeSession::write('SessionTestCase', 'This is a Test');
  502. $this->assertEquals('This is a Test', TestCakeSession::read('SessionTestCase'));
  503. TestCakeSession::write('SessionTestCase', 'This is a Test');
  504. TestCakeSession::write('SessionTestCase', 'This was updated');
  505. $this->assertEquals('This was updated', TestCakeSession::read('SessionTestCase'));
  506. TestCakeSession::destroy();
  507. $this->assertNull(TestCakeSession::read('SessionTestCase'));
  508. }
  509. /**
  510. * test that changing the config name of the cache config works.
  511. *
  512. * @return void
  513. */
  514. public function testReadAndWriteWithCustomCacheConfig() {
  515. Configure::write('Session.defaults', 'cache');
  516. Configure::write('Session.handler.config', 'session_test');
  517. Cache::config('session_test', array(
  518. 'engine' => 'File',
  519. 'prefix' => 'session_test_',
  520. ));
  521. TestCakeSession::init();
  522. TestCakeSession::start();
  523. TestCakeSession::write('SessionTestCase', 'Some value');
  524. $this->assertEquals('Some value', TestCakeSession::read('SessionTestCase'));
  525. $id = TestCakeSession::id();
  526. Cache::delete($id, 'session_test');
  527. }
  528. /**
  529. * testReadAndWriteWithDatabaseStorage method
  530. *
  531. * @return void
  532. */
  533. public function testReadAndWriteWithDatabaseStorage() {
  534. Configure::write('Session.defaults', 'database');
  535. Configure::write('Session.handler.table', 'sessions');
  536. Configure::write('Session.handler.model', 'Session');
  537. Configure::write('Session.handler.database', 'test');
  538. TestCakeSession::init();
  539. $this->assertNull(TestCakeSession::id());
  540. TestCakeSession::start();
  541. $expected = session_id();
  542. $this->assertEquals($expected, TestCakeSession::id());
  543. TestCakeSession::renew();
  544. $this->assertFalse($expected == TestCakeSession::id());
  545. $expected = session_id();
  546. $this->assertEquals($expected, TestCakeSession::id());
  547. TestCakeSession::write('SessionTestCase', 0);
  548. $this->assertEquals(0, TestCakeSession::read('SessionTestCase'));
  549. TestCakeSession::write('SessionTestCase', '0');
  550. $this->assertEquals('0', TestCakeSession::read('SessionTestCase'));
  551. $this->assertFalse(TestCakeSession::read('SessionTestCase') === 0);
  552. TestCakeSession::write('SessionTestCase', false);
  553. $this->assertFalse(TestCakeSession::read('SessionTestCase'));
  554. TestCakeSession::write('SessionTestCase', null);
  555. $this->assertEquals(null, TestCakeSession::read('SessionTestCase'));
  556. TestCakeSession::write('SessionTestCase', 'This is a Test');
  557. $this->assertEquals('This is a Test', TestCakeSession::read('SessionTestCase'));
  558. TestCakeSession::write('SessionTestCase', 'Some additional data');
  559. $this->assertEquals('Some additional data', TestCakeSession::read('SessionTestCase'));
  560. TestCakeSession::destroy();
  561. $this->assertNull(TestCakeSession::read('SessionTestCase'));
  562. Configure::write('Session', array(
  563. 'defaults' => 'php'
  564. ));
  565. TestCakeSession::init();
  566. }
  567. /**
  568. * testSessionTimeout method
  569. *
  570. * @return void
  571. */
  572. public function testSessionTimeout() {
  573. Configure::write('debug', 2);
  574. Configure::write('Session.autoRegenerate', false);
  575. $timeoutSeconds = Configure::read('Session.timeout') * 60;
  576. TestCakeSession::destroy();
  577. TestCakeSession::write('Test', 'some value');
  578. $this->assertWithinMargin(time() + $timeoutSeconds, CakeSession::$sessionTime, 1);
  579. $this->assertEquals(10, $_SESSION['Config']['countdown']);
  580. $this->assertWithinMargin(CakeSession::$sessionTime, $_SESSION['Config']['time'], 1);
  581. $this->assertWithinMargin(time(), CakeSession::$time, 1);
  582. $this->assertWithinMargin(time() + $timeoutSeconds, $_SESSION['Config']['time'], 1);
  583. Configure::write('Session.harden', true);
  584. TestCakeSession::destroy();
  585. TestCakeSession::write('Test', 'some value');
  586. $this->assertWithinMargin(time() + $timeoutSeconds, CakeSession::$sessionTime, 1);
  587. $this->assertEquals(10, $_SESSION['Config']['countdown']);
  588. $this->assertWithinMargin(CakeSession::$sessionTime, $_SESSION['Config']['time'], 1);
  589. $this->assertWithinMargin(time(), CakeSession::$time, 1);
  590. $this->assertWithinMargin(CakeSession::$time + $timeoutSeconds, $_SESSION['Config']['time'], 1);
  591. }
  592. /**
  593. * Test that cookieTimeout matches timeout when unspecified.
  594. *
  595. * @return void
  596. */
  597. public function testCookieTimeoutFallback() {
  598. $_SESSION = null;
  599. Configure::write('Session', array(
  600. 'defaults' => 'php',
  601. 'timeout' => 400,
  602. ));
  603. TestCakeSession::start();
  604. $this->assertEquals(400, Configure::read('Session.cookieTimeout'));
  605. $this->assertEquals(400, Configure::read('Session.timeout'));
  606. $this->assertEquals(400 * 60, ini_get('session.cookie_lifetime'));
  607. $this->assertEquals(400 * 60, ini_get('session.gc_maxlifetime'));
  608. $_SESSION = null;
  609. Configure::write('Session', array(
  610. 'defaults' => 'php',
  611. 'timeout' => 400,
  612. 'cookieTimeout' => 600
  613. ));
  614. TestCakeSession::start();
  615. $this->assertEquals(600, Configure::read('Session.cookieTimeout'));
  616. $this->assertEquals(400, Configure::read('Session.timeout'));
  617. }
  618. }