SessionTest.php 11 KB

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