blob: 14469f7fa663a01a9cee07f2979d157a5e9507d8 [file] [log] [blame]
dchill4257486002012-07-31 09:39:53 -04001<?php
2
3/**
4 * Session driver library unit test
5 */
6class Session_test extends CI_TestCase {
dchill424153c922012-07-31 10:38:21 -04007 protected $settings = array(
8 'use_cookies' => 0,
9 'use_only_cookies' => 0,
10 'cache_limiter' => false
11 );
dchill4257486002012-07-31 09:39:53 -040012 protected $setting_vals = array();
13 protected $cookie_vals;
14 protected $session;
15
16 /**
17 * Set up test framework
18 */
19 public function set_up()
20 {
21 // Override settings
22 foreach ($this->settings as $name => $value) {
23 $this->setting_vals[$name] = ini_get('session.'.$name);
24 ini_set('session.'.$name, $value);
25 }
26
27 // Start with clean environment
28 $this->cookie_vals = $_COOKIE;
29 $_COOKIE = array();
30
31 // Establish necessary support classes
dchill4282003da2012-10-09 13:28:17 -040032 $cfg = $this->ci_core_class('cfg');
dchill4282003da2012-10-09 13:28:17 -040033 $ldr = $this->ci_core_class('load');
dchill427ecc5cd2012-10-12 16:25:51 -040034 $ci = $this->ci_instance();
35 $ci->config = new $cfg();
36 $ci->load = new $ldr();
37 $ci->input = new Mock_Core_Input(NULL, NULL);
38
39 // Make sure string helper is available
40 $this->ci_vfs_clone('system/helpers/string_helper.php');
dchill4257486002012-07-31 09:39:53 -040041
42 // Attach session instance locally
dchill424153c922012-07-31 10:38:21 -040043 $config = array(
44 'sess_encrypt_cookie' => FALSE,
45 'sess_use_database' => FALSE,
46 'sess_table_name' => '',
47 'sess_expiration' => 7200,
48 'sess_expire_on_close' => FALSE,
49 'sess_match_ip' => FALSE,
50 'sess_match_useragent' => TRUE,
51 'sess_cookie_name' => 'ci_session',
52 'cookie_path' => '',
53 'cookie_domain' => '',
54 'cookie_secure' => FALSE,
55 'cookie_httponly' => FALSE,
56 'sess_time_to_update' => 300,
57 'time_reference' => 'local',
58 'cookie_prefix' => '',
59 'encryption_key' => 'foobar',
60 'sess_valid_drivers' => array(
61 'Mock_Libraries_Session_native',
62 'Mock_Libraries_Session_cookie'
63 )
64 );
dchill4257486002012-07-31 09:39:53 -040065 $this->session = new Mock_Libraries_Session($config);
66 }
67
68 /**
69 * Tear down test framework
70 */
71 public function tear_down()
72 {
73 // Restore environment
74 if (session_id()) session_destroy();
75 $_SESSION = array();
76 $_COOKIE = $this->cookie_vals;
77
78 // Restore settings
79 foreach ($this->settings as $name => $value) {
80 ini_set('session.'.$name, $this->setting_vals[$name]);
81 }
82 }
83
84 /**
85 * Test set_userdata() function
dchill4208c83042012-08-28 17:35:56 -040086 *
87 * @covers CI_Session::set_userdata
88 * @covers CI_Session::userdata
dchill4257486002012-07-31 09:39:53 -040089 */
90 public function test_set_userdata()
91 {
dchill4208c83042012-08-28 17:35:56 -040092 // Set userdata values for each driver
93 $key1 = 'test1';
94 $ckey2 = 'test2';
95 $nkey2 = 'test3';
96 $cmsg1 = 'Some test data';
97 $cmsg2 = 42;
98 $nmsg1 = 'Other test data';
99 $nmsg2 = true;
100 $this->session->cookie->set_userdata($key1, $cmsg1);
101 $this->session->set_userdata($ckey2, $cmsg2);
102 $this->session->native->set_userdata($key1, $nmsg1);
103 $this->session->set_userdata($nkey2, $nmsg2);
dchill4257486002012-07-31 09:39:53 -0400104
105 // Verify independent messages
dchill4208c83042012-08-28 17:35:56 -0400106 $this->assertEquals($cmsg1, $this->session->cookie->userdata($key1));
107 $this->assertEquals($nmsg1, $this->session->native->userdata($key1));
108
109 // Verify pre-selected driver sets
110 $this->assertEquals($cmsg2, $this->session->cookie->userdata($ckey2));
111 $this->assertEquals($nmsg2, $this->session->native->userdata($nkey2));
112
113 // Verify no crossover
114 $this->assertNull($this->session->cookie->userdata($nkey2));
115 $this->assertNull($this->session->native->userdata($ckey2));
dchill4257486002012-07-31 09:39:53 -0400116 }
117
118 /**
119 * Test the has_userdata() function
dchill4208c83042012-08-28 17:35:56 -0400120 *
121 * @covers CI_Session::has_userdata
dchill4257486002012-07-31 09:39:53 -0400122 */
123 public function test_has_userdata()
124 {
dchill4208c83042012-08-28 17:35:56 -0400125 // Set a userdata value for each driver
126 $key = 'hastest';
dchill4257486002012-07-31 09:39:53 -0400127 $cmsg = 'My test data';
dchill4208c83042012-08-28 17:35:56 -0400128 $this->session->cookie->set_userdata($key, $cmsg);
dchill4257486002012-07-31 09:39:53 -0400129 $nmsg = 'Your test data';
dchill4208c83042012-08-28 17:35:56 -0400130 $this->session->native->set_userdata($key, $nmsg);
dchill4257486002012-07-31 09:39:53 -0400131
dchill4208c83042012-08-28 17:35:56 -0400132 // Verify values exist
133 $this->assertTrue($this->session->cookie->has_userdata($key));
134 $this->assertTrue($this->session->native->has_userdata($key));
135
136 // Verify non-existent values
137 $nokey = 'hasnot';
138 $this->assertFalse($this->session->cookie->has_userdata($nokey));
139 $this->assertFalse($this->session->native->has_userdata($nokey));
dchill4257486002012-07-31 09:39:53 -0400140 }
141
142 /**
143 * Test the all_userdata() function
dchill4208c83042012-08-28 17:35:56 -0400144 *
145 * @covers CI_Session::all_userdata
dchill4257486002012-07-31 09:39:53 -0400146 */
147 public function test_all_userdata()
148 {
149 // Set a specific series of data for each driver
dchill424153c922012-07-31 10:38:21 -0400150 $cdata = array(
151 'one' => 'first',
152 'two' => 'second',
153 'three' => 'third',
154 'foo' => 'bar',
155 'bar' => 'baz'
156 );
157 $ndata = array(
158 'one' => 'gold',
159 'two' => 'silver',
160 'three' => 'bronze',
161 'foo' => 'baz',
162 'bar' => 'foo'
163 );
dchill4257486002012-07-31 09:39:53 -0400164 $this->session->cookie->set_userdata($cdata);
dchill4257486002012-07-31 09:39:53 -0400165 $this->session->native->set_userdata($ndata);
166
167 // Make sure all values are present
168 $call = $this->session->cookie->all_userdata();
169 foreach ($cdata as $key => $value) {
170 $this->assertEquals($value, $call[$key]);
171 }
172 $nall = $this->session->native->all_userdata();
173 foreach ($ndata as $key => $value) {
174 $this->assertEquals($value, $nall[$key]);
175 }
176 }
177
178 /**
179 * Test the unset_userdata() function
dchill4208c83042012-08-28 17:35:56 -0400180 *
181 * @covers CI_Session::unset_userdata
dchill4257486002012-07-31 09:39:53 -0400182 */
183 public function test_unset_userdata()
184 {
185 // Set a userdata message for each driver
dchill4208c83042012-08-28 17:35:56 -0400186 $key = 'untest';
dchill4257486002012-07-31 09:39:53 -0400187 $cmsg = 'Other test data';
dchill4208c83042012-08-28 17:35:56 -0400188 $this->session->cookie->set_userdata($key, $cmsg);
dchill4257486002012-07-31 09:39:53 -0400189 $nmsg = 'Sundry test data';
dchill4208c83042012-08-28 17:35:56 -0400190 $this->session->native->set_userdata($key, $nmsg);
dchill4257486002012-07-31 09:39:53 -0400191
192 // Verify independent messages
dchill4208c83042012-08-28 17:35:56 -0400193 $this->assertEquals($this->session->cookie->userdata($key), $cmsg);
194 $this->assertEquals($this->session->native->userdata($key), $nmsg);
dchill4257486002012-07-31 09:39:53 -0400195
196 // Unset them and verify absence
dchill4208c83042012-08-28 17:35:56 -0400197 $this->session->cookie->unset_userdata($key);
198 $this->session->native->unset_userdata($key);
199 $this->assertNull($this->session->cookie->userdata($key));
200 $this->assertNull($this->session->native->userdata($key));
dchill4257486002012-07-31 09:39:53 -0400201 }
202
203 /**
dchill4208c83042012-08-28 17:35:56 -0400204 * Test the flashdata() functions
205 *
206 * @covers CI_Session::set_flashdata
207 * @covers CI_Session::flashdata
dchill4257486002012-07-31 09:39:53 -0400208 */
dchill4208c83042012-08-28 17:35:56 -0400209 public function test_flashdata()
dchill4257486002012-07-31 09:39:53 -0400210 {
211 // Set flashdata message for each driver
dchill4208c83042012-08-28 17:35:56 -0400212 $key = 'fltest';
dchill4257486002012-07-31 09:39:53 -0400213 $cmsg = 'Some flash data';
dchill4208c83042012-08-28 17:35:56 -0400214 $this->session->cookie->set_flashdata($key, $cmsg);
dchill4257486002012-07-31 09:39:53 -0400215 $nmsg = 'Other flash data';
dchill4208c83042012-08-28 17:35:56 -0400216 $this->session->native->set_flashdata($key, $nmsg);
dchill4257486002012-07-31 09:39:53 -0400217
218 // Simulate page reload
219 $this->session->cookie->reload();
220 $this->session->native->reload();
221
222 // Verify independent messages
dchill4208c83042012-08-28 17:35:56 -0400223 $this->assertEquals($cmsg, $this->session->cookie->flashdata($key));
224 $this->assertEquals($nmsg, $this->session->native->flashdata($key));
dchill4257486002012-07-31 09:39:53 -0400225
226 // Simulate next page reload
227 $this->session->cookie->reload();
228 $this->session->native->reload();
229
230 // Verify absence of messages
dchill4208c83042012-08-28 17:35:56 -0400231 $this->assertNull($this->session->cookie->flashdata($key));
232 $this->assertNull($this->session->native->flashdata($key));
dchill4257486002012-07-31 09:39:53 -0400233 }
234
235 /**
236 * Test the keep_flashdata() function
dchill4208c83042012-08-28 17:35:56 -0400237 *
238 * @covers CI_Session::keep_flashdata
dchill4257486002012-07-31 09:39:53 -0400239 */
240 public function test_keep_flashdata()
241 {
242 // Set flashdata message for each driver
dchill4208c83042012-08-28 17:35:56 -0400243 $key = 'kfltest';
dchill4257486002012-07-31 09:39:53 -0400244 $cmsg = 'My flash data';
dchill4208c83042012-08-28 17:35:56 -0400245 $this->session->cookie->set_flashdata($key, $cmsg);
dchill4257486002012-07-31 09:39:53 -0400246 $nmsg = 'Your flash data';
dchill4208c83042012-08-28 17:35:56 -0400247 $this->session->native->set_flashdata($key, $nmsg);
dchill4257486002012-07-31 09:39:53 -0400248
249 // Simulate page reload and verify independent messages
250 $this->session->cookie->reload();
251 $this->session->native->reload();
dchill4208c83042012-08-28 17:35:56 -0400252 $this->assertEquals($cmsg, $this->session->cookie->flashdata($key));
253 $this->assertEquals($nmsg, $this->session->native->flashdata($key));
dchill4257486002012-07-31 09:39:53 -0400254
255 // Keep messages
dchill4208c83042012-08-28 17:35:56 -0400256 $this->session->cookie->keep_flashdata($key);
257 $this->session->native->keep_flashdata($key);
dchill4257486002012-07-31 09:39:53 -0400258
259 // Simulate next page reload and verify message persistence
260 $this->session->cookie->reload();
261 $this->session->native->reload();
dchill4208c83042012-08-28 17:35:56 -0400262 $this->assertEquals($cmsg, $this->session->cookie->flashdata($key));
263 $this->assertEquals($nmsg, $this->session->native->flashdata($key));
dchill4257486002012-07-31 09:39:53 -0400264
265 // Simulate next page reload and verify absence of messages
266 $this->session->cookie->reload();
267 $this->session->native->reload();
dchill4208c83042012-08-28 17:35:56 -0400268 $this->assertNull($this->session->cookie->flashdata($key));
269 $this->assertNull($this->session->native->flashdata($key));
dchill4257486002012-07-31 09:39:53 -0400270 }
271
272 /**
273 * Test the all_flashdata() function
dchill4208c83042012-08-28 17:35:56 -0400274 *
275 * @covers CI_Session::all_flashdata
dchill4257486002012-07-31 09:39:53 -0400276 */
277 public function test_all_flashdata()
278 {
279 // Set a specific series of data for each driver
dchill424153c922012-07-31 10:38:21 -0400280 $cdata = array(
281 'one' => 'first',
282 'two' => 'second',
283 'three' => 'third',
284 'foo' => 'bar',
285 'bar' => 'baz'
286 );
287 $ndata = array(
288 'one' => 'gold',
289 'two' => 'silver',
290 'three' => 'bronze',
291 'foo' => 'baz',
292 'bar' => 'foo'
293 );
dchill4257486002012-07-31 09:39:53 -0400294 $this->session->cookie->set_flashdata($cdata);
dchill4257486002012-07-31 09:39:53 -0400295 $this->session->native->set_flashdata($ndata);
296
297 // Simulate page reload and make sure all values are present
298 $this->session->cookie->reload();
299 $this->session->native->reload();
300 $this->assertEquals($cdata, $this->session->cookie->all_flashdata());
301 $this->assertEquals($ndata, $this->session->native->all_flashdata());
302 }
303
304 /**
dchill4208c83042012-08-28 17:35:56 -0400305 * Test the tempdata() functions
306 *
307 * @covers CI_Session::set_tempdata
308 * @covers CI_Session::tempdata
dchill4257486002012-07-31 09:39:53 -0400309 */
310 public function test_set_tempdata()
311 {
312 // Set tempdata message for each driver - 1 second timeout
dchill4208c83042012-08-28 17:35:56 -0400313 $key = 'tmptest';
dchill4257486002012-07-31 09:39:53 -0400314 $cmsg = 'Some temp data';
dchill4208c83042012-08-28 17:35:56 -0400315 $this->session->cookie->set_tempdata($key, $cmsg, 1);
dchill4257486002012-07-31 09:39:53 -0400316 $nmsg = 'Other temp data';
dchill4208c83042012-08-28 17:35:56 -0400317 $this->session->native->set_tempdata($key, $nmsg, 1);
dchill4257486002012-07-31 09:39:53 -0400318
319 // Simulate page reload and verify independent messages
320 $this->session->cookie->reload();
321 $this->session->native->reload();
dchill4208c83042012-08-28 17:35:56 -0400322 $this->assertEquals($cmsg, $this->session->cookie->tempdata($key));
323 $this->assertEquals($nmsg, $this->session->native->tempdata($key));
dchill4257486002012-07-31 09:39:53 -0400324
325 // Wait 2 seconds, simulate page reload and verify message absence
326 sleep(2);
327 $this->session->cookie->reload();
328 $this->session->native->reload();
dchill4208c83042012-08-28 17:35:56 -0400329 $this->assertNull($this->session->cookie->tempdata($key));
330 $this->assertNull($this->session->native->tempdata($key));
dchill4257486002012-07-31 09:39:53 -0400331 }
332
333 /**
334 * Test the unset_tempdata() function
dchill4208c83042012-08-28 17:35:56 -0400335 *
336 * @covers CI_Session::unset_tempdata
dchill4257486002012-07-31 09:39:53 -0400337 */
338 public function test_unset_tempdata()
339 {
340 // Set tempdata message for each driver - 1 second timeout
dchill4208c83042012-08-28 17:35:56 -0400341 $key = 'utmptest';
dchill4257486002012-07-31 09:39:53 -0400342 $cmsg = 'My temp data';
dchill4208c83042012-08-28 17:35:56 -0400343 $this->session->cookie->set_tempdata($key, $cmsg, 1);
dchill4257486002012-07-31 09:39:53 -0400344 $nmsg = 'Your temp data';
dchill4208c83042012-08-28 17:35:56 -0400345 $this->session->native->set_tempdata($key, $nmsg, 1);
dchill4257486002012-07-31 09:39:53 -0400346
347 // Verify independent messages
dchill4208c83042012-08-28 17:35:56 -0400348 $this->assertEquals($cmsg, $this->session->cookie->tempdata($key));
349 $this->assertEquals($nmsg, $this->session->native->tempdata($key));
dchill4257486002012-07-31 09:39:53 -0400350
351 // Unset data and verify message absence
dchill4208c83042012-08-28 17:35:56 -0400352 $this->session->cookie->unset_tempdata($key);
353 $this->session->native->unset_tempdata($key);
354 $this->assertNull($this->session->cookie->tempdata($key));
355 $this->assertNull($this->session->native->tempdata($key));
dchill4257486002012-07-31 09:39:53 -0400356 }
357
358 /**
359 * Test the sess_regenerate() function
dchill4208c83042012-08-28 17:35:56 -0400360 *
361 * @covers CI_Session::sess_regenerate
dchill4257486002012-07-31 09:39:53 -0400362 */
363 public function test_sess_regenerate()
364 {
365 // Get current session id, regenerate, and compare
366 // Cookie driver
367 $oldid = $this->session->cookie->userdata('session_id');
368 $this->session->cookie->sess_regenerate();
369 $newid = $this->session->cookie->userdata('session_id');
dchill4208c83042012-08-28 17:35:56 -0400370 $this->assertNotEquals($oldid, $newid);
dchill4257486002012-07-31 09:39:53 -0400371
372 // Native driver - bug #55267 (https://bugs.php.net/bug.php?id=55267) prevents testing this
dchill4208c83042012-08-28 17:35:56 -0400373 // $oldid = session_id();
374 // $this->session->native->sess_regenerate();
375 // $oldid = session_id();
376 // $this->assertNotEquals($oldid, $newid);
dchill4257486002012-07-31 09:39:53 -0400377 }
378
379 /**
380 * Test the sess_destroy() function
dchill4208c83042012-08-28 17:35:56 -0400381 *
382 * @covers CI_Session::sess_destroy
dchill4257486002012-07-31 09:39:53 -0400383 */
384 public function test_sess_destroy()
385 {
386 // Set a userdata message, destroy session, and verify absence
dchill4208c83042012-08-28 17:35:56 -0400387 $key = 'dsttest';
dchill4257486002012-07-31 09:39:53 -0400388 $msg = 'More test data';
389
390 // Cookie driver
dchill4208c83042012-08-28 17:35:56 -0400391 $this->session->cookie->set_userdata($key, $msg);
392 $this->assertEquals($msg, $this->session->cookie->userdata($key));
dchill4257486002012-07-31 09:39:53 -0400393 $this->session->cookie->sess_destroy();
dchill4208c83042012-08-28 17:35:56 -0400394 $this->assertNull($this->session->cookie->userdata($key));
dchill4257486002012-07-31 09:39:53 -0400395
396 // Native driver
dchill4208c83042012-08-28 17:35:56 -0400397 $this->session->native->set_userdata($key, $msg);
398 $this->assertEquals($msg, $this->session->native->userdata($key));
dchill4257486002012-07-31 09:39:53 -0400399 $this->session->native->sess_destroy();
dchill4208c83042012-08-28 17:35:56 -0400400 $this->assertNull($this->session->native->userdata($key));
dchill4257486002012-07-31 09:39:53 -0400401 }
dchill4282003da2012-10-09 13:28:17 -0400402}