blob: 135f718065145536a3b209cad7d11ec1add5a3e6 [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
32 $obj = new stdClass;
dchill424153c922012-07-31 10:38:21 -040033 $classes = array(
34 'config' => 'cfg',
35 'load' => 'load',
36 'input' => 'in'
37 );
38 foreach ($classes as $name => $abbr) {
dchill4257486002012-07-31 09:39:53 -040039 $class = $this->ci_core_class($abbr);
40 $obj->$name = new $class;
41 }
42 $this->ci_instance($obj);
43
44 // Attach session instance locally
dchill424153c922012-07-31 10:38:21 -040045 $config = array(
46 'sess_encrypt_cookie' => FALSE,
47 'sess_use_database' => FALSE,
48 'sess_table_name' => '',
49 'sess_expiration' => 7200,
50 'sess_expire_on_close' => FALSE,
51 'sess_match_ip' => FALSE,
52 'sess_match_useragent' => TRUE,
53 'sess_cookie_name' => 'ci_session',
54 'cookie_path' => '',
55 'cookie_domain' => '',
56 'cookie_secure' => FALSE,
57 'cookie_httponly' => FALSE,
58 'sess_time_to_update' => 300,
59 'time_reference' => 'local',
60 'cookie_prefix' => '',
61 'encryption_key' => 'foobar',
62 'sess_valid_drivers' => array(
63 'Mock_Libraries_Session_native',
64 'Mock_Libraries_Session_cookie'
65 )
66 );
dchill4257486002012-07-31 09:39:53 -040067 $this->session = new Mock_Libraries_Session($config);
68 }
69
70 /**
71 * Tear down test framework
72 */
73 public function tear_down()
74 {
75 // Restore environment
76 if (session_id()) session_destroy();
77 $_SESSION = array();
78 $_COOKIE = $this->cookie_vals;
79
80 // Restore settings
81 foreach ($this->settings as $name => $value) {
82 ini_set('session.'.$name, $this->setting_vals[$name]);
83 }
84 }
85
86 /**
87 * Test set_userdata() function
dchill4208c83042012-08-28 17:35:56 -040088 *
89 * @covers CI_Session::set_userdata
90 * @covers CI_Session::userdata
dchill4257486002012-07-31 09:39:53 -040091 */
92 public function test_set_userdata()
93 {
dchill4208c83042012-08-28 17:35:56 -040094 // Set userdata values for each driver
95 $key1 = 'test1';
96 $ckey2 = 'test2';
97 $nkey2 = 'test3';
98 $cmsg1 = 'Some test data';
99 $cmsg2 = 42;
100 $nmsg1 = 'Other test data';
101 $nmsg2 = true;
102 $this->session->cookie->set_userdata($key1, $cmsg1);
103 $this->session->set_userdata($ckey2, $cmsg2);
104 $this->session->native->set_userdata($key1, $nmsg1);
105 $this->session->set_userdata($nkey2, $nmsg2);
dchill4257486002012-07-31 09:39:53 -0400106
107 // Verify independent messages
dchill4208c83042012-08-28 17:35:56 -0400108 $this->assertEquals($cmsg1, $this->session->cookie->userdata($key1));
109 $this->assertEquals($nmsg1, $this->session->native->userdata($key1));
110
111 // Verify pre-selected driver sets
112 $this->assertEquals($cmsg2, $this->session->cookie->userdata($ckey2));
113 $this->assertEquals($nmsg2, $this->session->native->userdata($nkey2));
114
115 // Verify no crossover
116 $this->assertNull($this->session->cookie->userdata($nkey2));
117 $this->assertNull($this->session->native->userdata($ckey2));
dchill4257486002012-07-31 09:39:53 -0400118 }
119
120 /**
121 * Test the has_userdata() function
dchill4208c83042012-08-28 17:35:56 -0400122 *
123 * @covers CI_Session::has_userdata
dchill4257486002012-07-31 09:39:53 -0400124 */
125 public function test_has_userdata()
126 {
dchill4208c83042012-08-28 17:35:56 -0400127 // Set a userdata value for each driver
128 $key = 'hastest';
dchill4257486002012-07-31 09:39:53 -0400129 $cmsg = 'My test data';
dchill4208c83042012-08-28 17:35:56 -0400130 $this->session->cookie->set_userdata($key, $cmsg);
dchill4257486002012-07-31 09:39:53 -0400131 $nmsg = 'Your test data';
dchill4208c83042012-08-28 17:35:56 -0400132 $this->session->native->set_userdata($key, $nmsg);
dchill4257486002012-07-31 09:39:53 -0400133
dchill4208c83042012-08-28 17:35:56 -0400134 // Verify values exist
135 $this->assertTrue($this->session->cookie->has_userdata($key));
136 $this->assertTrue($this->session->native->has_userdata($key));
137
138 // Verify non-existent values
139 $nokey = 'hasnot';
140 $this->assertFalse($this->session->cookie->has_userdata($nokey));
141 $this->assertFalse($this->session->native->has_userdata($nokey));
dchill4257486002012-07-31 09:39:53 -0400142 }
143
144 /**
145 * Test the all_userdata() function
dchill4208c83042012-08-28 17:35:56 -0400146 *
147 * @covers CI_Session::all_userdata
dchill4257486002012-07-31 09:39:53 -0400148 */
149 public function test_all_userdata()
150 {
151 // Set a specific series of data for each driver
dchill424153c922012-07-31 10:38:21 -0400152 $cdata = array(
153 'one' => 'first',
154 'two' => 'second',
155 'three' => 'third',
156 'foo' => 'bar',
157 'bar' => 'baz'
158 );
159 $ndata = array(
160 'one' => 'gold',
161 'two' => 'silver',
162 'three' => 'bronze',
163 'foo' => 'baz',
164 'bar' => 'foo'
165 );
dchill4257486002012-07-31 09:39:53 -0400166 $this->session->cookie->set_userdata($cdata);
dchill4257486002012-07-31 09:39:53 -0400167 $this->session->native->set_userdata($ndata);
168
169 // Make sure all values are present
170 $call = $this->session->cookie->all_userdata();
171 foreach ($cdata as $key => $value) {
172 $this->assertEquals($value, $call[$key]);
173 }
174 $nall = $this->session->native->all_userdata();
175 foreach ($ndata as $key => $value) {
176 $this->assertEquals($value, $nall[$key]);
177 }
178 }
179
180 /**
181 * Test the unset_userdata() function
dchill4208c83042012-08-28 17:35:56 -0400182 *
183 * @covers CI_Session::unset_userdata
dchill4257486002012-07-31 09:39:53 -0400184 */
185 public function test_unset_userdata()
186 {
187 // Set a userdata message for each driver
dchill4208c83042012-08-28 17:35:56 -0400188 $key = 'untest';
dchill4257486002012-07-31 09:39:53 -0400189 $cmsg = 'Other test data';
dchill4208c83042012-08-28 17:35:56 -0400190 $this->session->cookie->set_userdata($key, $cmsg);
dchill4257486002012-07-31 09:39:53 -0400191 $nmsg = 'Sundry test data';
dchill4208c83042012-08-28 17:35:56 -0400192 $this->session->native->set_userdata($key, $nmsg);
dchill4257486002012-07-31 09:39:53 -0400193
194 // Verify independent messages
dchill4208c83042012-08-28 17:35:56 -0400195 $this->assertEquals($this->session->cookie->userdata($key), $cmsg);
196 $this->assertEquals($this->session->native->userdata($key), $nmsg);
dchill4257486002012-07-31 09:39:53 -0400197
198 // Unset them and verify absence
dchill4208c83042012-08-28 17:35:56 -0400199 $this->session->cookie->unset_userdata($key);
200 $this->session->native->unset_userdata($key);
201 $this->assertNull($this->session->cookie->userdata($key));
202 $this->assertNull($this->session->native->userdata($key));
dchill4257486002012-07-31 09:39:53 -0400203 }
204
205 /**
dchill4208c83042012-08-28 17:35:56 -0400206 * Test the flashdata() functions
207 *
208 * @covers CI_Session::set_flashdata
209 * @covers CI_Session::flashdata
dchill4257486002012-07-31 09:39:53 -0400210 */
dchill4208c83042012-08-28 17:35:56 -0400211 public function test_flashdata()
dchill4257486002012-07-31 09:39:53 -0400212 {
213 // Set flashdata message for each driver
dchill4208c83042012-08-28 17:35:56 -0400214 $key = 'fltest';
dchill4257486002012-07-31 09:39:53 -0400215 $cmsg = 'Some flash data';
dchill4208c83042012-08-28 17:35:56 -0400216 $this->session->cookie->set_flashdata($key, $cmsg);
dchill4257486002012-07-31 09:39:53 -0400217 $nmsg = 'Other flash data';
dchill4208c83042012-08-28 17:35:56 -0400218 $this->session->native->set_flashdata($key, $nmsg);
dchill4257486002012-07-31 09:39:53 -0400219
220 // Simulate page reload
221 $this->session->cookie->reload();
222 $this->session->native->reload();
223
224 // Verify independent messages
dchill4208c83042012-08-28 17:35:56 -0400225 $this->assertEquals($cmsg, $this->session->cookie->flashdata($key));
226 $this->assertEquals($nmsg, $this->session->native->flashdata($key));
dchill4257486002012-07-31 09:39:53 -0400227
228 // Simulate next page reload
229 $this->session->cookie->reload();
230 $this->session->native->reload();
231
232 // Verify absence of messages
dchill4208c83042012-08-28 17:35:56 -0400233 $this->assertNull($this->session->cookie->flashdata($key));
234 $this->assertNull($this->session->native->flashdata($key));
dchill4257486002012-07-31 09:39:53 -0400235 }
236
237 /**
238 * Test the keep_flashdata() function
dchill4208c83042012-08-28 17:35:56 -0400239 *
240 * @covers CI_Session::keep_flashdata
dchill4257486002012-07-31 09:39:53 -0400241 */
242 public function test_keep_flashdata()
243 {
244 // Set flashdata message for each driver
dchill4208c83042012-08-28 17:35:56 -0400245 $key = 'kfltest';
dchill4257486002012-07-31 09:39:53 -0400246 $cmsg = 'My flash data';
dchill4208c83042012-08-28 17:35:56 -0400247 $this->session->cookie->set_flashdata($key, $cmsg);
dchill4257486002012-07-31 09:39:53 -0400248 $nmsg = 'Your flash data';
dchill4208c83042012-08-28 17:35:56 -0400249 $this->session->native->set_flashdata($key, $nmsg);
dchill4257486002012-07-31 09:39:53 -0400250
251 // Simulate page reload and verify independent messages
252 $this->session->cookie->reload();
253 $this->session->native->reload();
dchill4208c83042012-08-28 17:35:56 -0400254 $this->assertEquals($cmsg, $this->session->cookie->flashdata($key));
255 $this->assertEquals($nmsg, $this->session->native->flashdata($key));
dchill4257486002012-07-31 09:39:53 -0400256
257 // Keep messages
dchill4208c83042012-08-28 17:35:56 -0400258 $this->session->cookie->keep_flashdata($key);
259 $this->session->native->keep_flashdata($key);
dchill4257486002012-07-31 09:39:53 -0400260
261 // Simulate next page reload and verify message persistence
262 $this->session->cookie->reload();
263 $this->session->native->reload();
dchill4208c83042012-08-28 17:35:56 -0400264 $this->assertEquals($cmsg, $this->session->cookie->flashdata($key));
265 $this->assertEquals($nmsg, $this->session->native->flashdata($key));
dchill4257486002012-07-31 09:39:53 -0400266
267 // Simulate next page reload and verify absence of messages
268 $this->session->cookie->reload();
269 $this->session->native->reload();
dchill4208c83042012-08-28 17:35:56 -0400270 $this->assertNull($this->session->cookie->flashdata($key));
271 $this->assertNull($this->session->native->flashdata($key));
dchill4257486002012-07-31 09:39:53 -0400272 }
273
274 /**
275 * Test the all_flashdata() function
dchill4208c83042012-08-28 17:35:56 -0400276 *
277 * @covers CI_Session::all_flashdata
dchill4257486002012-07-31 09:39:53 -0400278 */
279 public function test_all_flashdata()
280 {
281 // Set a specific series of data for each driver
dchill424153c922012-07-31 10:38:21 -0400282 $cdata = array(
283 'one' => 'first',
284 'two' => 'second',
285 'three' => 'third',
286 'foo' => 'bar',
287 'bar' => 'baz'
288 );
289 $ndata = array(
290 'one' => 'gold',
291 'two' => 'silver',
292 'three' => 'bronze',
293 'foo' => 'baz',
294 'bar' => 'foo'
295 );
dchill4257486002012-07-31 09:39:53 -0400296 $this->session->cookie->set_flashdata($cdata);
dchill4257486002012-07-31 09:39:53 -0400297 $this->session->native->set_flashdata($ndata);
298
299 // Simulate page reload and make sure all values are present
300 $this->session->cookie->reload();
301 $this->session->native->reload();
302 $this->assertEquals($cdata, $this->session->cookie->all_flashdata());
303 $this->assertEquals($ndata, $this->session->native->all_flashdata());
304 }
305
306 /**
dchill4208c83042012-08-28 17:35:56 -0400307 * Test the tempdata() functions
308 *
309 * @covers CI_Session::set_tempdata
310 * @covers CI_Session::tempdata
dchill4257486002012-07-31 09:39:53 -0400311 */
312 public function test_set_tempdata()
313 {
314 // Set tempdata message for each driver - 1 second timeout
dchill4208c83042012-08-28 17:35:56 -0400315 $key = 'tmptest';
dchill4257486002012-07-31 09:39:53 -0400316 $cmsg = 'Some temp data';
dchill4208c83042012-08-28 17:35:56 -0400317 $this->session->cookie->set_tempdata($key, $cmsg, 1);
dchill4257486002012-07-31 09:39:53 -0400318 $nmsg = 'Other temp data';
dchill4208c83042012-08-28 17:35:56 -0400319 $this->session->native->set_tempdata($key, $nmsg, 1);
dchill4257486002012-07-31 09:39:53 -0400320
321 // Simulate page reload and verify independent messages
322 $this->session->cookie->reload();
323 $this->session->native->reload();
dchill4208c83042012-08-28 17:35:56 -0400324 $this->assertEquals($cmsg, $this->session->cookie->tempdata($key));
325 $this->assertEquals($nmsg, $this->session->native->tempdata($key));
dchill4257486002012-07-31 09:39:53 -0400326
327 // Wait 2 seconds, simulate page reload and verify message absence
328 sleep(2);
329 $this->session->cookie->reload();
330 $this->session->native->reload();
dchill4208c83042012-08-28 17:35:56 -0400331 $this->assertNull($this->session->cookie->tempdata($key));
332 $this->assertNull($this->session->native->tempdata($key));
dchill4257486002012-07-31 09:39:53 -0400333 }
334
335 /**
336 * Test the unset_tempdata() function
dchill4208c83042012-08-28 17:35:56 -0400337 *
338 * @covers CI_Session::unset_tempdata
dchill4257486002012-07-31 09:39:53 -0400339 */
340 public function test_unset_tempdata()
341 {
342 // Set tempdata message for each driver - 1 second timeout
dchill4208c83042012-08-28 17:35:56 -0400343 $key = 'utmptest';
dchill4257486002012-07-31 09:39:53 -0400344 $cmsg = 'My temp data';
dchill4208c83042012-08-28 17:35:56 -0400345 $this->session->cookie->set_tempdata($key, $cmsg, 1);
dchill4257486002012-07-31 09:39:53 -0400346 $nmsg = 'Your temp data';
dchill4208c83042012-08-28 17:35:56 -0400347 $this->session->native->set_tempdata($key, $nmsg, 1);
dchill4257486002012-07-31 09:39:53 -0400348
349 // Verify independent messages
dchill4208c83042012-08-28 17:35:56 -0400350 $this->assertEquals($cmsg, $this->session->cookie->tempdata($key));
351 $this->assertEquals($nmsg, $this->session->native->tempdata($key));
dchill4257486002012-07-31 09:39:53 -0400352
353 // Unset data and verify message absence
dchill4208c83042012-08-28 17:35:56 -0400354 $this->session->cookie->unset_tempdata($key);
355 $this->session->native->unset_tempdata($key);
356 $this->assertNull($this->session->cookie->tempdata($key));
357 $this->assertNull($this->session->native->tempdata($key));
dchill4257486002012-07-31 09:39:53 -0400358 }
359
360 /**
361 * Test the sess_regenerate() function
dchill4208c83042012-08-28 17:35:56 -0400362 *
363 * @covers CI_Session::sess_regenerate
dchill4257486002012-07-31 09:39:53 -0400364 */
365 public function test_sess_regenerate()
366 {
367 // Get current session id, regenerate, and compare
368 // Cookie driver
369 $oldid = $this->session->cookie->userdata('session_id');
370 $this->session->cookie->sess_regenerate();
371 $newid = $this->session->cookie->userdata('session_id');
dchill4208c83042012-08-28 17:35:56 -0400372 $this->assertNotEquals($oldid, $newid);
dchill4257486002012-07-31 09:39:53 -0400373
374 // Native driver - bug #55267 (https://bugs.php.net/bug.php?id=55267) prevents testing this
dchill4208c83042012-08-28 17:35:56 -0400375 // $oldid = session_id();
376 // $this->session->native->sess_regenerate();
377 // $oldid = session_id();
378 // $this->assertNotEquals($oldid, $newid);
dchill4257486002012-07-31 09:39:53 -0400379 }
380
381 /**
382 * Test the sess_destroy() function
dchill4208c83042012-08-28 17:35:56 -0400383 *
384 * @covers CI_Session::sess_destroy
dchill4257486002012-07-31 09:39:53 -0400385 */
386 public function test_sess_destroy()
387 {
388 // Set a userdata message, destroy session, and verify absence
dchill4208c83042012-08-28 17:35:56 -0400389 $key = 'dsttest';
dchill4257486002012-07-31 09:39:53 -0400390 $msg = 'More test data';
391
392 // Cookie driver
dchill4208c83042012-08-28 17:35:56 -0400393 $this->session->cookie->set_userdata($key, $msg);
394 $this->assertEquals($msg, $this->session->cookie->userdata($key));
dchill4257486002012-07-31 09:39:53 -0400395 $this->session->cookie->sess_destroy();
dchill4208c83042012-08-28 17:35:56 -0400396 $this->assertNull($this->session->cookie->userdata($key));
dchill4257486002012-07-31 09:39:53 -0400397
398 // Native driver
dchill4208c83042012-08-28 17:35:56 -0400399 $this->session->native->set_userdata($key, $msg);
400 $this->assertEquals($msg, $this->session->native->userdata($key));
dchill4257486002012-07-31 09:39:53 -0400401 $this->session->native->sess_destroy();
dchill4208c83042012-08-28 17:35:56 -0400402 $this->assertNull($this->session->native->userdata($key));
dchill4257486002012-07-31 09:39:53 -0400403 }
404}
405