CakeSessionTest.php 20 KB

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