SessionTest.php 14 KB

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