SessionTest.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. * @link http://cakephp.org CakePHP(tm) Project
  12. * @since 1.2.0
  13. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\Network;
  16. use Cake\Core\Configure;
  17. use Cake\Network\Session;
  18. use Cake\Network\Session\CacheSession;
  19. use Cake\Network\Session\DatabaseSession;
  20. use Cake\TestSuite\TestCase;
  21. /**
  22. * Class TestCacheSession
  23. *
  24. */
  25. class TestCacheSession extends CacheSession
  26. {
  27. protected function _writeSession()
  28. {
  29. return true;
  30. }
  31. }
  32. /**
  33. * Class TestDatabaseSession
  34. *
  35. */
  36. class TestDatabaseSession extends DatabaseSession
  37. {
  38. protected function _writeSession()
  39. {
  40. return true;
  41. }
  42. }
  43. /**
  44. * SessionTest class
  45. *
  46. */
  47. class SessionTest extends TestCase
  48. {
  49. protected static $_gcDivisor;
  50. /**
  51. * Fixtures used in the SessionTest
  52. *
  53. * @var array
  54. */
  55. public $fixtures = ['core.cake_sessions', 'core.sessions'];
  56. /**
  57. * setup before class.
  58. *
  59. * @return void
  60. */
  61. public static function setupBeforeClass()
  62. {
  63. // Make sure garbage collector will be called
  64. static::$_gcDivisor = ini_get('session.gc_divisor');
  65. ini_set('session.gc_divisor', '1');
  66. }
  67. /**
  68. * teardown after class
  69. *
  70. * @return void
  71. */
  72. public static function teardownAfterClass()
  73. {
  74. // Revert to the default setting
  75. ini_set('session.gc_divisor', static::$_gcDivisor);
  76. }
  77. /**
  78. * setUp method
  79. *
  80. * @return void
  81. */
  82. public function setUp()
  83. {
  84. parent::setUp();
  85. }
  86. /**
  87. * tearDown method
  88. *
  89. * @return void
  90. */
  91. public function tearDown()
  92. {
  93. unset($_SESSION);
  94. parent::tearDown();
  95. }
  96. /**
  97. * test setting ini properties with Session configuration.
  98. *
  99. * @return void
  100. */
  101. public function testSessionConfigIniSetting()
  102. {
  103. $_SESSION = null;
  104. $config = [
  105. 'cookie' => 'test',
  106. 'checkAgent' => false,
  107. 'timeout' => 86400,
  108. 'ini' => [
  109. 'session.referer_check' => 'example.com',
  110. 'session.use_trans_sid' => false
  111. ]
  112. ];
  113. $session = Session::create($config);
  114. $this->assertEquals('', ini_get('session.use_trans_sid'), 'Ini value is incorrect');
  115. $this->assertEquals('example.com', ini_get('session.referer_check'), 'Ini value is incorrect');
  116. $this->assertEquals('test', ini_get('session.name'), 'Ini value is incorrect');
  117. }
  118. /**
  119. * test session cookie path setting
  120. *
  121. * @return void
  122. */
  123. public function testCookiePath()
  124. {
  125. ini_set('session.cookie_path', '/foo');
  126. $session = new Session();
  127. $this->assertEquals('/', ini_get('session.cookie_path'));
  128. $session = new Session(['cookiePath' => '/base']);
  129. $this->assertEquals('/base', ini_get('session.cookie_path'));
  130. }
  131. /**
  132. * testCheck method
  133. *
  134. * @return void
  135. */
  136. public function testCheck()
  137. {
  138. $session = new Session();
  139. $session->write('SessionTestCase', 'value');
  140. $this->assertTrue($session->check('SessionTestCase'));
  141. $this->assertFalse($session->check('NotExistingSessionTestCase'));
  142. $this->assertFalse($session->check('Crazy.foo'));
  143. $session->write('Crazy.foo', ['bar' => 'baz']);
  144. $this->assertTrue($session->check('Crazy.foo'));
  145. $this->assertTrue($session->check('Crazy.foo.bar'));
  146. }
  147. /**
  148. * testSimpleRead method
  149. *
  150. * @return void
  151. */
  152. public function testSimpleRead()
  153. {
  154. $session = new Session();
  155. $session->write('testing', '1,2,3');
  156. $result = $session->read('testing');
  157. $this->assertEquals('1,2,3', $result);
  158. $session->write('testing', ['1' => 'one', '2' => 'two', '3' => 'three']);
  159. $result = $session->read('testing.1');
  160. $this->assertEquals('one', $result);
  161. $result = $session->read('testing');
  162. $this->assertEquals(['1' => 'one', '2' => 'two', '3' => 'three'], $result);
  163. $result = $session->read();
  164. $this->assertTrue(isset($result['testing']));
  165. $session->write('This.is.a.deep.array.my.friend', 'value');
  166. $result = $session->read('This.is.a.deep.array');
  167. $this->assertEquals(['my' => ['friend' => 'value']], $result);
  168. }
  169. /**
  170. * testReadEmpty
  171. *
  172. * @return void
  173. */
  174. public function testReadEmpty()
  175. {
  176. $session = new Session();
  177. $this->assertNull($session->read(''));
  178. }
  179. /**
  180. * test writing a hash of values/
  181. *
  182. * @return void
  183. */
  184. public function testWriteArray()
  185. {
  186. $session = new Session();
  187. $session->write([
  188. 'one' => 1,
  189. 'two' => 2,
  190. 'three' => ['something'],
  191. 'null' => null
  192. ]);
  193. $this->assertEquals(1, $session->read('one'));
  194. $this->assertEquals(['something'], $session->read('three'));
  195. $this->assertEquals(null, $session->read('null'));
  196. }
  197. /**
  198. * Test overwriting a string value as if it were an array.
  199. *
  200. * @return void
  201. */
  202. public function testWriteOverwriteStringValue()
  203. {
  204. $session = new Session();
  205. $session->write('Some.string', 'value');
  206. $this->assertEquals('value', $session->read('Some.string'));
  207. $session->write('Some.string.array', ['values']);
  208. $this->assertEquals(['values'], $session->read('Some.string.array'));
  209. }
  210. /**
  211. * Test consuming session data.
  212. *
  213. * @return void
  214. */
  215. public function testConsume()
  216. {
  217. $session = new Session();
  218. $session->write('Some.string', 'value');
  219. $session->write('Some.array', ['key1' => 'value1', 'key2' => 'value2']);
  220. $this->assertEquals('value', $session->read('Some.string'));
  221. $value = $session->consume('Some.string');
  222. $this->assertEquals('value', $value);
  223. $this->assertFalse($session->check('Some.string'));
  224. $value = $session->consume('');
  225. $this->assertNull($value);
  226. $value = $session->consume(null);
  227. $this->assertNull($value);
  228. $value = $session->consume('Some.array');
  229. $expected = ['key1' => 'value1', 'key2' => 'value2'];
  230. $this->assertEquals($expected, $value);
  231. $this->assertFalse($session->check('Some.array'));
  232. }
  233. /**
  234. * testId method
  235. *
  236. * @return void
  237. */
  238. public function testId()
  239. {
  240. $session = new Session();
  241. $result = $session->id();
  242. $expected = session_id();
  243. $this->assertEquals($expected, $result);
  244. $session->id('MySessionId');
  245. $this->assertEquals('MySessionId', $session->id());
  246. $this->assertEquals('MySessionId', session_id());
  247. $session->id('');
  248. $this->assertEquals('', session_id());
  249. }
  250. /**
  251. * testStarted method
  252. *
  253. * @return void
  254. */
  255. public function testStarted()
  256. {
  257. $session = new Session();
  258. $this->assertFalse($session->started());
  259. $this->assertTrue($session->start());
  260. $this->assertTrue($session->started());
  261. }
  262. /**
  263. * testClear method
  264. *
  265. * @return void
  266. */
  267. public function testClear()
  268. {
  269. $session = new Session();
  270. $session->write('Delete.me', 'Clearing out');
  271. $session->clear();
  272. $this->assertFalse($session->check('Delete.me'));
  273. $this->assertFalse($session->check('Delete'));
  274. }
  275. /**
  276. * testDelete method
  277. *
  278. * @return void
  279. */
  280. public function testDelete()
  281. {
  282. $session = new Session();
  283. $session->write('Delete.me', 'Clearing out');
  284. $session->delete('Delete.me');
  285. $this->assertFalse($session->check('Delete.me'));
  286. $this->assertTrue($session->check('Delete'));
  287. $session->write('Clearing.sale', 'everything must go');
  288. $session->delete('');
  289. $this->assertTrue($session->check('Clearing.sale'));
  290. $session->delete(null);
  291. $this->assertTrue($session->check('Clearing.sale'));
  292. $session->delete('Clearing');
  293. $this->assertFalse($session->check('Clearing.sale'));
  294. $this->assertFalse($session->check('Clearing'));
  295. }
  296. /**
  297. * testDestroy method
  298. *
  299. * @return void
  300. */
  301. public function testDestroy()
  302. {
  303. $session = new Session();
  304. $session->start();
  305. $session->write('bulletProof', 'invincible');
  306. $session->id('foo');
  307. $session->destroy();
  308. $this->assertFalse($session->check('bulletProof'));
  309. }
  310. /**
  311. * testCheckingSavedEmpty method
  312. *
  313. * @return void
  314. */
  315. public function testCheckingSavedEmpty()
  316. {
  317. $session = new Session();
  318. $session->write('SessionTestCase', 0);
  319. $this->assertTrue($session->check('SessionTestCase'));
  320. $session->write('SessionTestCase', '0');
  321. $this->assertTrue($session->check('SessionTestCase'));
  322. $session->write('SessionTestCase', false);
  323. $this->assertTrue($session->check('SessionTestCase'));
  324. $session->write('SessionTestCase', null);
  325. $this->assertFalse($session->check('SessionTestCase'));
  326. }
  327. /**
  328. * testCheckKeyWithSpaces method
  329. *
  330. * @return void
  331. */
  332. public function testCheckKeyWithSpaces()
  333. {
  334. $session = new Session();
  335. $session->write('Session Test', "test");
  336. $this->assertTrue($session->check('Session Test'));
  337. $session->delete('Session Test');
  338. $session->write('Session Test.Test Case', "test");
  339. $this->assertTrue($session->check('Session Test.Test Case'));
  340. }
  341. /**
  342. * testCheckEmpty
  343. *
  344. * @return void
  345. */
  346. public function testCheckEmpty()
  347. {
  348. $session = new Session();
  349. $this->assertFalse($session->check());
  350. }
  351. /**
  352. * test key exploitation
  353. *
  354. * @return void
  355. */
  356. public function testKeyExploit()
  357. {
  358. $session = new Session();
  359. $key = "a'] = 1; phpinfo(); \$_SESSION['a";
  360. $session->write($key, 'haxored');
  361. $result = $session->read($key);
  362. $this->assertNull($result);
  363. }
  364. /**
  365. * testReadingSavedEmpty method
  366. *
  367. * @return void
  368. */
  369. public function testReadingSavedEmpty()
  370. {
  371. $session = new Session();
  372. $session->write('SessionTestCase', 0);
  373. $this->assertEquals(0, $session->read('SessionTestCase'));
  374. $session->write('SessionTestCase', '0');
  375. $this->assertEquals('0', $session->read('SessionTestCase'));
  376. $this->assertFalse($session->read('SessionTestCase') === 0);
  377. $session->write('SessionTestCase', false);
  378. $this->assertFalse($session->read('SessionTestCase'));
  379. $session->write('SessionTestCase', null);
  380. $this->assertEquals(null, $session->read('SessionTestCase'));
  381. }
  382. /**
  383. * test using a handler from app/Model/Datasource/Session.
  384. *
  385. * @return void
  386. */
  387. public function testUsingAppLibsHandler()
  388. {
  389. Configure::write('App.namespace', 'TestApp');
  390. $config = [
  391. 'defaults' => 'cake',
  392. 'handler' => [
  393. 'engine' => 'TestAppLibSession',
  394. 'these' => 'are',
  395. 'a few' => 'options'
  396. ]
  397. ];
  398. $session = Session::create($config);
  399. $this->assertInstanceOf('TestApp\Network\Session\TestAppLibSession', $session->engine());
  400. $this->assertEquals('user', ini_get('session.save_handler'));
  401. $this->assertEquals(['these' => 'are', 'a few' => 'options'], $session->engine()->options);
  402. }
  403. /**
  404. * test using a handler from a plugin.
  405. *
  406. * @return void
  407. */
  408. public function testUsingPluginHandler()
  409. {
  410. Configure::write('App.namespace', 'TestApp');
  411. \Cake\Core\Plugin::load('TestPlugin');
  412. $config = [
  413. 'defaults' => 'cake',
  414. 'handler' => [
  415. 'engine' => 'TestPlugin.TestPluginSession'
  416. ]
  417. ];
  418. $session = Session::create($config);
  419. $this->assertInstanceOf('TestPlugin\Network\Session\TestPluginSession', $session->engine());
  420. $this->assertEquals('user', ini_get('session.save_handler'));
  421. }
  422. /**
  423. * Tests that it is possible to pass an already made instance as the session engine
  424. *
  425. * @return void
  426. */
  427. public function testEngineWithPreMadeInstance()
  428. {
  429. Configure::write('App.namespace', 'TestApp');
  430. $engine = new \TestApp\Network\Session\TestAppLibSession;
  431. $session = new Session(['handler' => ['engine' => $engine]]);
  432. $this->assertSame($engine, $session->engine());
  433. $session = new Session();
  434. $session->engine($engine);
  435. $this->assertSame($engine, $session->engine());
  436. }
  437. /**
  438. * Tests instantiating a missing engine
  439. *
  440. * @expectedException \InvalidArgumentException
  441. * @expectedExceptionMessage The class "Derp" does not exist and cannot be used as a session engine
  442. * @return void
  443. */
  444. public function testBadEngine()
  445. {
  446. $session = new Session();
  447. $session->engine('Derp');
  448. }
  449. /**
  450. * Test that cookieTimeout matches timeout when unspecified.
  451. *
  452. * @return void
  453. */
  454. public function testCookieTimeoutFallback()
  455. {
  456. $config = [
  457. 'defaults' => 'cake',
  458. 'timeout' => 400,
  459. ];
  460. new Session($config);
  461. $this->assertEquals(0, ini_get('session.cookie_lifetime'));
  462. $this->assertEquals(400 * 60, ini_get('session.gc_maxlifetime'));
  463. }
  464. /**
  465. * Tests that the cookie name can be changed with configuration
  466. *
  467. * @return void
  468. */
  469. public function testSessionName()
  470. {
  471. new Session(['cookie' => 'made_up_name']);
  472. $this->assertEquals('made_up_name', session_name());
  473. }
  474. }