blob: db7efb6b70f40c8604194a89a25939aeeec68cdb [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 {
7 protected $settings = array('use_cookies' => 0, 'use_only_cookies' => 0, 'cache_limiter' => false);
8 protected $setting_vals = array();
9 protected $cookie_vals;
10 protected $session;
11
12 /**
13 * Set up test framework
14 */
15 public function set_up()
16 {
17 // Override settings
18 foreach ($this->settings as $name => $value) {
19 $this->setting_vals[$name] = ini_get('session.'.$name);
20 ini_set('session.'.$name, $value);
21 }
22
23 // Start with clean environment
24 $this->cookie_vals = $_COOKIE;
25 $_COOKIE = array();
26
27 // Establish necessary support classes
28 $obj = new stdClass;
29 foreach (array('config' => 'cfg', 'load' => 'load', 'input' => 'in') as $name => $abbr) {
30 $class = $this->ci_core_class($abbr);
31 $obj->$name = new $class;
32 }
33 $this->ci_instance($obj);
34
35 // Attach session instance locally
36 $config = array('sess_encrypt_cookie' => FALSE, 'sess_use_database' => FALSE, 'sess_table_name' => '',
37 'sess_expiration' => 7200, 'sess_expire_on_close' => FALSE, 'sess_match_ip' => FALSE,
38 'sess_match_useragent' => TRUE, 'sess_cookie_name' => 'ci_session', 'cookie_path' => '',
39 'cookie_domain' => '', 'cookie_secure' => FALSE, 'cookie_httponly' => FALSE, 'sess_time_to_update' => 300,
40 'time_reference' => 'local', 'cookie_prefix' => '', 'encryption_key' => 'foobar',
41 'sess_valid_drivers' => array('Mock_Libraries_Session_native', 'Mock_Libraries_Session_cookie'));
42 $this->session = new Mock_Libraries_Session($config);
43 }
44
45 /**
46 * Tear down test framework
47 */
48 public function tear_down()
49 {
50 // Restore environment
51 if (session_id()) session_destroy();
52 $_SESSION = array();
53 $_COOKIE = $this->cookie_vals;
54
55 // Restore settings
56 foreach ($this->settings as $name => $value) {
57 ini_set('session.'.$name, $this->setting_vals[$name]);
58 }
59 }
60
61 /**
62 * Test set_userdata() function
63 */
64 public function test_set_userdata()
65 {
66 // Set a userdata message for each driver
67 $cmsg = 'Some test data';
68 $this->session->cookie->set_userdata('test1', $cmsg);
69 $nmsg = 'Other test data';
70 $this->session->native->set_userdata('test1', $nmsg);
71
72 // Verify independent messages
73 $this->assertEquals($this->session->cookie->userdata('test1'), $cmsg);
74 $this->assertEquals($this->session->native->userdata('test1'), $nmsg);
75 }
76
77 /**
78 * Test the has_userdata() function
79 */
80 public function test_has_userdata()
81 {
82 // Set a userdata message for each driver
83 $cmsg = 'My test data';
84 $this->session->cookie->set_userdata('test2', $cmsg);
85 $nmsg = 'Your test data';
86 $this->session->native->set_userdata('test2', $nmsg);
87
88 // Verify independent messages
89 $this->assertTrue($this->session->cookie->has_userdata('test2'));
90 $this->assertTrue($this->session->native->has_userdata('test2'));
91 }
92
93 /**
94 * Test the all_userdata() function
95 */
96 public function test_all_userdata()
97 {
98 // Set a specific series of data for each driver
99 $cdata = array('one' => 'first', 'two' => 'second', 'three' => 'third', 'foo' => 'bar', 'bar' => 'baz');
100 $this->session->cookie->set_userdata($cdata);
101 $ndata = array('one' => 'gold', 'two' => 'silver', 'three' => 'bronze', 'foo' => 'baz', 'bar' => 'foo');
102 $this->session->native->set_userdata($ndata);
103
104 // Make sure all values are present
105 $call = $this->session->cookie->all_userdata();
106 foreach ($cdata as $key => $value) {
107 $this->assertEquals($value, $call[$key]);
108 }
109 $nall = $this->session->native->all_userdata();
110 foreach ($ndata as $key => $value) {
111 $this->assertEquals($value, $nall[$key]);
112 }
113 }
114
115 /**
116 * Test the unset_userdata() function
117 */
118 public function test_unset_userdata()
119 {
120 // Set a userdata message for each driver
121 $cmsg = 'Other test data';
122 $this->session->cookie->set_userdata('test3', $cmsg);
123 $nmsg = 'Sundry test data';
124 $this->session->native->set_userdata('test3', $nmsg);
125
126 // Verify independent messages
127 $this->assertEquals($this->session->cookie->userdata('test3'), $cmsg);
128 $this->assertEquals($this->session->native->userdata('test3'), $nmsg);
129
130 // Unset them and verify absence
131 $this->session->cookie->unset_userdata('test3');
132 $this->session->native->unset_userdata('test3');
133 $this->assertEquals($this->session->cookie->userdata('test3'), NULL);
134 $this->assertEquals($this->session->native->userdata('test3'), NULL);
135 }
136
137 /**
138 * Test the set_flashdata() function
139 */
140 public function test_set_flashdata()
141 {
142 // Set flashdata message for each driver
143 $cmsg = 'Some flash data';
144 $this->session->cookie->set_flashdata('test4', $cmsg);
145 $nmsg = 'Other flash data';
146 $this->session->native->set_flashdata('test4', $nmsg);
147
148 // Simulate page reload
149 $this->session->cookie->reload();
150 $this->session->native->reload();
151
152 // Verify independent messages
153 $this->assertEquals($this->session->cookie->flashdata('test4'), $cmsg);
154 $this->assertEquals($this->session->native->flashdata('test4'), $nmsg);
155
156 // Simulate next page reload
157 $this->session->cookie->reload();
158 $this->session->native->reload();
159
160 // Verify absence of messages
161 $this->assertEquals($this->session->cookie->flashdata('test4'), NULL);
162 $this->assertEquals($this->session->native->flashdata('test4'), NULL);
163 }
164
165 /**
166 * Test the keep_flashdata() function
167 */
168 public function test_keep_flashdata()
169 {
170 // Set flashdata message for each driver
171 $cmsg = 'My flash data';
172 $this->session->cookie->set_flashdata('test5', $cmsg);
173 $nmsg = 'Your flash data';
174 $this->session->native->set_flashdata('test5', $nmsg);
175
176 // Simulate page reload and verify independent messages
177 $this->session->cookie->reload();
178 $this->session->native->reload();
179 $this->assertEquals($this->session->cookie->flashdata('test5'), $cmsg);
180 $this->assertEquals($this->session->native->flashdata('test5'), $nmsg);
181
182 // Keep messages
183 $this->session->cookie->keep_flashdata('test5');
184 $this->session->native->keep_flashdata('test5');
185
186 // Simulate next page reload and verify message persistence
187 $this->session->cookie->reload();
188 $this->session->native->reload();
189 $this->assertEquals($this->session->cookie->flashdata('test5'), $cmsg);
190 $this->assertEquals($this->session->native->flashdata('test5'), $nmsg);
191
192 // Simulate next page reload and verify absence of messages
193 $this->session->cookie->reload();
194 $this->session->native->reload();
195 $this->assertEquals($this->session->cookie->flashdata('test5'), NULL);
196 $this->assertEquals($this->session->native->flashdata('test5'), NULL);
197 }
198
199 /**
200 * Test the all_flashdata() function
201 */
202 public function test_all_flashdata()
203 {
204 // Set a specific series of data for each driver
205 $cdata = array('one' => 'first', 'two' => 'second', 'three' => 'third', 'foo' => 'bar', 'bar' => 'baz');
206 $this->session->cookie->set_flashdata($cdata);
207 $ndata = array('one' => 'gold', 'two' => 'silver', 'three' => 'bronze', 'foo' => 'baz', 'bar' => 'foo');
208 $this->session->native->set_flashdata($ndata);
209
210 // Simulate page reload and make sure all values are present
211 $this->session->cookie->reload();
212 $this->session->native->reload();
213 $this->assertEquals($cdata, $this->session->cookie->all_flashdata());
214 $this->assertEquals($ndata, $this->session->native->all_flashdata());
215 }
216
217 /**
218 * Test the set_tempdata() function
219 */
220 public function test_set_tempdata()
221 {
222 // Set tempdata message for each driver - 1 second timeout
223 $cmsg = 'Some temp data';
224 $this->session->cookie->set_tempdata('test6', $cmsg, 1);
225 $nmsg = 'Other temp data';
226 $this->session->native->set_tempdata('test6', $nmsg, 1);
227
228 // Simulate page reload and verify independent messages
229 $this->session->cookie->reload();
230 $this->session->native->reload();
231 $this->assertEquals($this->session->cookie->tempdata('test6'), $cmsg);
232 $this->assertEquals($this->session->native->tempdata('test6'), $nmsg);
233
234 // Wait 2 seconds, simulate page reload and verify message absence
235 sleep(2);
236 $this->session->cookie->reload();
237 $this->session->native->reload();
238 $this->assertEquals($this->session->cookie->tempdata('test6'), NULL);
239 $this->assertEquals($this->session->native->tempdata('test6'), NULL);
240 }
241
242 /**
243 * Test the unset_tempdata() function
244 */
245 public function test_unset_tempdata()
246 {
247 // Set tempdata message for each driver - 1 second timeout
248 $cmsg = 'My temp data';
249 $this->session->cookie->set_tempdata('test7', $cmsg, 1);
250 $nmsg = 'Your temp data';
251 $this->session->native->set_tempdata('test7', $nmsg, 1);
252
253 // Verify independent messages
254 $this->assertEquals($this->session->cookie->tempdata('test7'), $cmsg);
255 $this->assertEquals($this->session->native->tempdata('test7'), $nmsg);
256
257 // Unset data and verify message absence
258 $this->session->cookie->unset_tempdata('test7');
259 $this->session->native->unset_tempdata('test7');
260 $this->assertEquals($this->session->cookie->tempdata('test7'), NULL);
261 $this->assertEquals($this->session->native->tempdata('test7'), NULL);
262 }
263
264 /**
265 * Test the sess_regenerate() function
266 */
267 public function test_sess_regenerate()
268 {
269 // Get current session id, regenerate, and compare
270 // Cookie driver
271 $oldid = $this->session->cookie->userdata('session_id');
272 $this->session->cookie->sess_regenerate();
273 $newid = $this->session->cookie->userdata('session_id');
274 $this->assertFalse($oldid === $newid);
275
276 // Native driver - bug #55267 (https://bugs.php.net/bug.php?id=55267) prevents testing this
277 /*$oldid = session_id();
278 $this->session->native->sess_regenerate();
279 $oldid = session_id();
280 $this->assertFalse($oldid === $newid);*/
281 }
282
283 /**
284 * Test the sess_destroy() function
285 */
286 public function test_sess_destroy()
287 {
288 // Set a userdata message, destroy session, and verify absence
289 $msg = 'More test data';
290
291 // Cookie driver
292 $this->session->cookie->set_userdata('test8', $msg);
293 $this->assertEquals($this->session->cookie->userdata('test8'), $msg);
294 $this->session->cookie->sess_destroy();
295 $this->assertEquals($this->session->cookie->userdata('test8'), NULL);
296
297 // Native driver
298 $this->session->native->set_userdata('test8', $msg);
299 $this->assertEquals($this->session->native->userdata('test8'), $msg);
300 $this->session->native->sess_destroy();
301 $this->assertEquals($this->session->native->userdata('test8'), NULL);
302 }
303}
304