SessionTest.php 11 KB

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