PageRenderTime 45ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/api/tests/codeigniter/libraries/Session_test.php

https://gitlab.com/snead/pinmo-legacy
PHP | 440 lines | 294 code | 51 blank | 95 comment | 1 complexity | e98794fb347135be4c37ea0bbe5690ac MD5 | raw file
  1. <?php
  2. /**
  3. * Session driver library unit test
  4. */
  5. class Session_test extends CI_TestCase {
  6. protected $settings = array(
  7. 'use_cookies' => 0,
  8. 'use_only_cookies' => 0,
  9. 'cache_limiter' => FALSE
  10. );
  11. protected $setting_vals = array();
  12. protected $cookie_vals;
  13. protected $session;
  14. /**
  15. * Set up test framework
  16. */
  17. public function set_up()
  18. {
  19. return;
  20. // Override settings
  21. foreach ($this->settings as $name => $value) {
  22. $this->setting_vals[$name] = ini_get('session.'.$name);
  23. ini_set('session.'.$name, $value);
  24. }
  25. // Start with clean environment
  26. $this->cookie_vals = $_COOKIE;
  27. $_COOKIE = array();
  28. // Set subclass prefix to match our mock
  29. $this->ci_set_config('subclass_prefix', 'Mock_Libraries_');
  30. // Establish necessary support classes
  31. $ci = $this->ci_instance();
  32. $ldr = $this->ci_core_class('load');
  33. $ci->load = new $ldr();
  34. $ci->input = new Mock_Core_Input(NULL, NULL);
  35. // Make sure string helper is available
  36. $this->ci_vfs_clone('system/helpers/string_helper.php');
  37. // Attach session instance locally
  38. $config = array(
  39. 'sess_encrypt_cookie' => FALSE,
  40. 'sess_use_database' => FALSE,
  41. 'sess_table_name' => '',
  42. 'sess_expiration' => 7200,
  43. 'sess_expire_on_close' => FALSE,
  44. 'sess_match_ip' => FALSE,
  45. 'sess_match_useragent' => TRUE,
  46. 'sess_cookie_name' => 'ci_session',
  47. 'cookie_path' => '',
  48. 'cookie_domain' => '',
  49. 'cookie_secure' => FALSE,
  50. 'cookie_httponly' => FALSE,
  51. 'sess_time_to_update' => 300,
  52. 'time_reference' => 'local',
  53. 'cookie_prefix' => '',
  54. 'encryption_key' => 'foobar'
  55. );
  56. $this->session = new Mock_Libraries_Session($config);
  57. }
  58. /**
  59. * Tear down test framework
  60. */
  61. public function tear_down()
  62. {
  63. return;
  64. // Restore environment
  65. if (session_id()) session_destroy();
  66. $_SESSION = array();
  67. $_COOKIE = $this->cookie_vals;
  68. // Restore settings
  69. foreach ($this->settings as $name => $value) {
  70. ini_set('session.'.$name, $this->setting_vals[$name]);
  71. }
  72. }
  73. /**
  74. * Test set_userdata() function
  75. */
  76. public function test_set_userdata()
  77. {
  78. return;
  79. // Set userdata values for each driver
  80. $key1 = 'test1';
  81. $ckey2 = 'test2';
  82. $nkey2 = 'test3';
  83. $cmsg1 = 'Some test data';
  84. $cmsg2 = 42;
  85. $nmsg1 = 'Other test data';
  86. $nmsg2 = TRUE;
  87. $this->session->cookie->set_userdata($key1, $cmsg1);
  88. $this->session->set_userdata($ckey2, $cmsg2);
  89. $this->session->native->set_userdata($key1, $nmsg1);
  90. $this->session->set_userdata($nkey2, $nmsg2);
  91. // Verify independent messages
  92. $this->assertEquals($cmsg1, $this->session->cookie->userdata($key1));
  93. $this->assertEquals($nmsg1, $this->session->native->userdata($key1));
  94. // Verify pre-selected driver sets
  95. $this->assertEquals($cmsg2, $this->session->cookie->userdata($ckey2));
  96. $this->assertEquals($nmsg2, $this->session->native->userdata($nkey2));
  97. // Verify no crossover
  98. $this->assertNull($this->session->cookie->userdata($nkey2));
  99. $this->assertNull($this->session->native->userdata($ckey2));
  100. }
  101. /**
  102. * Test the has_userdata() function
  103. */
  104. public function test_has_userdata()
  105. {
  106. return;
  107. // Set a userdata value for each driver
  108. $key = 'hastest';
  109. $cmsg = 'My test data';
  110. $this->session->cookie->set_userdata($key, $cmsg);
  111. $nmsg = 'Your test data';
  112. $this->session->native->set_userdata($key, $nmsg);
  113. // Verify values exist
  114. $this->assertTrue($this->session->cookie->has_userdata($key));
  115. $this->assertTrue($this->session->native->has_userdata($key));
  116. // Verify non-existent values
  117. $nokey = 'hasnot';
  118. $this->assertFalse($this->session->cookie->has_userdata($nokey));
  119. $this->assertFalse($this->session->native->has_userdata($nokey));
  120. }
  121. /**
  122. * Test the all_userdata() function
  123. */
  124. public function test_all_userdata()
  125. {
  126. return;
  127. // Set a specific series of data for each driver
  128. $cdata = array(
  129. 'one' => 'first',
  130. 'two' => 'second',
  131. 'three' => 'third',
  132. 'foo' => 'bar',
  133. 'bar' => 'baz'
  134. );
  135. $ndata = array(
  136. 'one' => 'gold',
  137. 'two' => 'silver',
  138. 'three' => 'bronze',
  139. 'foo' => 'baz',
  140. 'bar' => 'foo'
  141. );
  142. $this->session->cookie->set_userdata($cdata);
  143. $this->session->native->set_userdata($ndata);
  144. // Make sure all values are present
  145. $call = $this->session->cookie->userdata();
  146. foreach ($cdata as $key => $value) {
  147. $this->assertEquals($value, $call[$key]);
  148. }
  149. $nall = $this->session->native->userdata();
  150. foreach ($ndata as $key => $value) {
  151. $this->assertEquals($value, $nall[$key]);
  152. }
  153. }
  154. /**
  155. * Test the unset_userdata() function
  156. */
  157. public function test_unset_userdata()
  158. {
  159. return;
  160. // Set a userdata message for each driver
  161. $key = 'untest';
  162. $cmsg = 'Other test data';
  163. $this->session->cookie->set_userdata($key, $cmsg);
  164. $nmsg = 'Sundry test data';
  165. $this->session->native->set_userdata($key, $nmsg);
  166. // Verify independent messages
  167. $this->assertEquals($this->session->cookie->userdata($key), $cmsg);
  168. $this->assertEquals($this->session->native->userdata($key), $nmsg);
  169. // Unset them and verify absence
  170. $this->session->cookie->unset_userdata($key);
  171. $this->session->native->unset_userdata($key);
  172. $this->assertNull($this->session->cookie->userdata($key));
  173. $this->assertNull($this->session->native->userdata($key));
  174. }
  175. /**
  176. * Test the flashdata() functions
  177. */
  178. public function test_flashdata()
  179. {
  180. return;
  181. // Set flashdata message for each driver
  182. $key = 'fltest';
  183. $cmsg = 'Some flash data';
  184. $this->session->cookie->set_flashdata($key, $cmsg);
  185. $nmsg = 'Other flash data';
  186. $this->session->native->set_flashdata($key, $nmsg);
  187. // Simulate page reload
  188. $this->session->cookie->reload();
  189. $this->session->native->reload();
  190. // Verify independent messages
  191. $this->assertEquals($cmsg, $this->session->cookie->flashdata($key));
  192. $this->assertEquals($nmsg, $this->session->native->flashdata($key));
  193. // Simulate next page reload
  194. $this->session->cookie->reload();
  195. $this->session->native->reload();
  196. // Verify absence of messages
  197. $this->assertNull($this->session->cookie->flashdata($key));
  198. $this->assertNull($this->session->native->flashdata($key));
  199. }
  200. /**
  201. * Test the keep_flashdata() function
  202. */
  203. public function test_keep_flashdata()
  204. {
  205. return;
  206. // Set flashdata message for each driver
  207. $key = 'kfltest';
  208. $cmsg = 'My flash data';
  209. $this->session->cookie->set_flashdata($key, $cmsg);
  210. $nmsg = 'Your flash data';
  211. $this->session->native->set_flashdata($key, $nmsg);
  212. // Simulate page reload and verify independent messages
  213. $this->session->cookie->reload();
  214. $this->session->native->reload();
  215. $this->assertEquals($cmsg, $this->session->cookie->flashdata($key));
  216. $this->assertEquals($nmsg, $this->session->native->flashdata($key));
  217. // Keep messages
  218. $this->session->cookie->keep_flashdata($key);
  219. $this->session->native->keep_flashdata($key);
  220. // Simulate next page reload and verify message persistence
  221. $this->session->cookie->reload();
  222. $this->session->native->reload();
  223. $this->assertEquals($cmsg, $this->session->cookie->flashdata($key));
  224. $this->assertEquals($nmsg, $this->session->native->flashdata($key));
  225. // Simulate next page reload and verify absence of messages
  226. $this->session->cookie->reload();
  227. $this->session->native->reload();
  228. $this->assertNull($this->session->cookie->flashdata($key));
  229. $this->assertNull($this->session->native->flashdata($key));
  230. }
  231. public function test_keep_flashdata_with_array()
  232. {
  233. return;
  234. // Set flashdata array for each driver
  235. $cdata = array(
  236. 'one' => 'first',
  237. 'two' => 'second',
  238. 'three' => 'third',
  239. 'foo' => 'bar',
  240. 'bar' => 'baz'
  241. );
  242. $ndata = array(
  243. 'one' => 'gold',
  244. 'two' => 'silver',
  245. 'three' => 'bronze',
  246. 'foo' => 'baz',
  247. 'bar' => 'foo'
  248. );
  249. $kdata = array(
  250. 'one',
  251. 'two',
  252. 'three',
  253. 'foo',
  254. 'bar'
  255. );
  256. $this->session->cookie->set_flashdata($cdata);
  257. $this->session->native->set_flashdata($ndata);
  258. // Simulate page reload and verify independent messages
  259. $this->session->cookie->reload();
  260. $this->session->native->reload();
  261. $this->assertEquals($cdata, $this->session->cookie->flashdata());
  262. $this->assertEquals($ndata, $this->session->native->flashdata());
  263. // Keep messages
  264. $this->session->cookie->keep_flashdata($kdata);
  265. $this->session->native->keep_flashdata($kdata);
  266. // Simulate next page reload and verify message persistence
  267. $this->session->cookie->reload();
  268. $this->session->native->reload();
  269. $this->assertEquals($cdata, $this->session->cookie->flashdata());
  270. $this->assertEquals($ndata, $this->session->native->flashdata());
  271. // Simulate next page reload and verify absence of messages
  272. $this->session->cookie->reload();
  273. $this->session->native->reload();
  274. $this->assertEmpty($this->session->cookie->flashdata());
  275. $this->assertEmpty($this->session->native->flashdata());
  276. }
  277. /**
  278. * Test the all_flashdata() function
  279. */
  280. public function test_all_flashdata()
  281. {
  282. return;
  283. // Set a specific series of data for each driver
  284. $cdata = array(
  285. 'one' => 'first',
  286. 'two' => 'second',
  287. 'three' => 'third',
  288. 'foo' => 'bar',
  289. 'bar' => 'baz'
  290. );
  291. $ndata = array(
  292. 'one' => 'gold',
  293. 'two' => 'silver',
  294. 'three' => 'bronze',
  295. 'foo' => 'baz',
  296. 'bar' => 'foo'
  297. );
  298. $this->session->cookie->set_flashdata($cdata);
  299. $this->session->native->set_flashdata($ndata);
  300. // Simulate page reload and make sure all values are present
  301. $this->session->cookie->reload();
  302. $this->session->native->reload();
  303. $this->assertEquals($cdata, $this->session->cookie->flashdata());
  304. $this->assertEquals($ndata, $this->session->native->flashdata());
  305. }
  306. /**
  307. * Test the tempdata() functions
  308. */
  309. public function test_set_tempdata()
  310. {
  311. return;
  312. // Set tempdata message for each driver - 1 second timeout
  313. $key = 'tmptest';
  314. $cmsg = 'Some temp data';
  315. $this->session->cookie->set_tempdata($key, $cmsg, 1);
  316. $nmsg = 'Other temp data';
  317. $this->session->native->set_tempdata($key, $nmsg, 1);
  318. // Simulate page reload and verify independent messages
  319. $this->session->cookie->reload();
  320. $this->session->native->reload();
  321. $this->assertEquals($cmsg, $this->session->cookie->tempdata($key));
  322. $this->assertEquals($nmsg, $this->session->native->tempdata($key));
  323. // Wait 2 seconds, simulate page reload and verify message absence
  324. sleep(2);
  325. $this->session->cookie->reload();
  326. $this->session->native->reload();
  327. $this->assertNull($this->session->cookie->tempdata($key));
  328. $this->assertNull($this->session->native->tempdata($key));
  329. }
  330. /**
  331. * Test the unset_tempdata() function
  332. */
  333. public function test_unset_tempdata()
  334. {
  335. return;
  336. // Set tempdata message for each driver - 1 second timeout
  337. $key = 'utmptest';
  338. $cmsg = 'My temp data';
  339. $this->session->cookie->set_tempdata($key, $cmsg, 1);
  340. $nmsg = 'Your temp data';
  341. $this->session->native->set_tempdata($key, $nmsg, 1);
  342. // Verify independent messages
  343. $this->assertEquals($cmsg, $this->session->cookie->tempdata($key));
  344. $this->assertEquals($nmsg, $this->session->native->tempdata($key));
  345. // Unset data and verify message absence
  346. $this->session->cookie->unset_tempdata($key);
  347. $this->session->native->unset_tempdata($key);
  348. $this->assertNull($this->session->cookie->tempdata($key));
  349. $this->assertNull($this->session->native->tempdata($key));
  350. }
  351. /**
  352. * Test the sess_regenerate() function
  353. */
  354. public function test_sess_regenerate()
  355. {
  356. return;
  357. // Get current session id, regenerate, and compare
  358. // Cookie driver
  359. $oldid = $this->session->cookie->userdata('session_id');
  360. $this->session->cookie->sess_regenerate();
  361. $newid = $this->session->cookie->userdata('session_id');
  362. $this->assertNotEquals($oldid, $newid);
  363. // Native driver - bug #55267 (https://bugs.php.net/bug.php?id=55267) prevents testing this
  364. // $oldid = session_id();
  365. // $this->session->native->sess_regenerate();
  366. // $oldid = session_id();
  367. // $this->assertNotEquals($oldid, $newid);
  368. }
  369. /**
  370. * Test the sess_destroy() function
  371. */
  372. public function test_sess_destroy()
  373. {
  374. return;
  375. // Set a userdata message, destroy session, and verify absence
  376. $key = 'dsttest';
  377. $msg = 'More test data';
  378. // Cookie driver
  379. $this->session->cookie->set_userdata($key, $msg);
  380. $this->assertEquals($msg, $this->session->cookie->userdata($key));
  381. $this->session->cookie->sess_destroy();
  382. $this->assertNull($this->session->cookie->userdata($key));
  383. // Native driver
  384. $this->session->native->set_userdata($key, $msg);
  385. $this->assertEquals($msg, $this->session->native->userdata($key));
  386. $this->session->native->sess_destroy();
  387. $this->assertNull($this->session->native->userdata($key));
  388. }
  389. }