pictcode / lib / Cake / Test / Case / Model / Datasource / CakeSessionTest.php @ 635eef61
履歴 | 表示 | アノテート | ダウンロード (22.474 KB)
1 |
<?php
|
---|---|
2 |
/**
|
3 |
* SessionTest file
|
4 |
*
|
5 |
* CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
|
6 |
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
7 |
*
|
8 |
* Licensed under The MIT License
|
9 |
* For full copyright and license information, please see the LICENSE.txt
|
10 |
* Redistributions of files must retain the above copyright notice
|
11 |
*
|
12 |
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
13 |
* @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
|
14 |
* @package Cake.Test.Case.Model.Datasource
|
15 |
* @since CakePHP(tm) v 1.2.0.4206
|
16 |
* @license http://www.opensource.org/licenses/mit-license.php MIT License
|
17 |
*/
|
18 |
|
19 |
App::uses('CakeSession', 'Model/Datasource'); |
20 |
App::uses('DatabaseSession', 'Model/Datasource/Session'); |
21 |
App::uses('CacheSession', 'Model/Datasource/Session'); |
22 |
|
23 |
/**
|
24 |
* Class TestCakeSession
|
25 |
*
|
26 |
* @package Cake.Test.Case.Model.Datasource
|
27 |
*/
|
28 |
class TestCakeSession extends CakeSession { |
29 |
|
30 |
public static function setUserAgent($value) { |
31 |
static::$_userAgent = $value; |
32 |
} |
33 |
|
34 |
public static function setHost($host) { |
35 |
static::_setHost($host); |
36 |
} |
37 |
|
38 |
} |
39 |
|
40 |
/**
|
41 |
* Class TestCacheSession
|
42 |
*
|
43 |
* @package Cake.Test.Case.Model.Datasource
|
44 |
*/
|
45 |
class TestCacheSession extends CacheSession { |
46 |
|
47 |
protected function _writeSession() { |
48 |
return true; |
49 |
} |
50 |
|
51 |
} |
52 |
|
53 |
/**
|
54 |
* Class TestDatabaseSession
|
55 |
*
|
56 |
* @package Cake.Test.Case.Model.Datasource
|
57 |
*/
|
58 |
class TestDatabaseSession extends DatabaseSession { |
59 |
|
60 |
protected function _writeSession() { |
61 |
return true; |
62 |
} |
63 |
|
64 |
} |
65 |
|
66 |
/**
|
67 |
* CakeSessionTest class
|
68 |
*
|
69 |
* @package Cake.Test.Case.Model.Datasource
|
70 |
*/
|
71 |
class CakeSessionTest extends CakeTestCase { |
72 |
|
73 |
protected static $_gcDivisor; |
74 |
|
75 |
/**
|
76 |
* Fixtures used in the SessionTest
|
77 |
*
|
78 |
* @var array
|
79 |
*/
|
80 |
public $fixtures = array('core.session'); |
81 |
|
82 |
/**
|
83 |
* setup before class.
|
84 |
*
|
85 |
* @return void
|
86 |
*/
|
87 |
public static function setupBeforeClass() { |
88 |
// Make sure garbage colector will be called
|
89 |
static::$_gcDivisor = ini_get('session.gc_divisor'); |
90 |
ini_set('session.gc_divisor', '1'); |
91 |
} |
92 |
|
93 |
/**
|
94 |
* teardown after class
|
95 |
*
|
96 |
* @return void
|
97 |
*/
|
98 |
public static function teardownAfterClass() { |
99 |
// Revert to the default setting
|
100 |
ini_set('session.gc_divisor', static::$_gcDivisor); |
101 |
} |
102 |
|
103 |
/**
|
104 |
* setUp method
|
105 |
*
|
106 |
* @return void
|
107 |
*/
|
108 |
public function setUp() { |
109 |
parent::setUp();
|
110 |
Configure::write('Session', array( |
111 |
'defaults' => 'php', |
112 |
'cookie' => 'cakephp', |
113 |
'timeout' => 120, |
114 |
'cookieTimeout' => 120, |
115 |
'ini' => array(), |
116 |
)); |
117 |
} |
118 |
|
119 |
/**
|
120 |
* tearDown method
|
121 |
*
|
122 |
* @return void
|
123 |
*/
|
124 |
public function tearDown() { |
125 |
if (TestCakeSession::started()) { |
126 |
session_write_close(); |
127 |
} |
128 |
unset($_SESSION); |
129 |
parent::tearDown();
|
130 |
} |
131 |
|
132 |
/**
|
133 |
* test setting ini properties with Session configuration.
|
134 |
*
|
135 |
* @return void
|
136 |
*/
|
137 |
public function testSessionConfigIniSetting() { |
138 |
$_SESSION = null; |
139 |
|
140 |
Configure::write('Session', array( |
141 |
'cookie' => 'test', |
142 |
'checkAgent' => false, |
143 |
'timeout' => 86400, |
144 |
'ini' => array( |
145 |
'session.referer_check' => 'example.com', |
146 |
'session.use_trans_sid' => false |
147 |
) |
148 |
)); |
149 |
TestCakeSession::start();
|
150 |
$this->assertEquals('', ini_get('session.use_trans_sid'), 'Ini value is incorrect'); |
151 |
$this->assertEquals('example.com', ini_get('session.referer_check'), 'Ini value is incorrect'); |
152 |
$this->assertEquals('test', ini_get('session.name'), 'Ini value is incorrect'); |
153 |
} |
154 |
|
155 |
/**
|
156 |
* testSessionPath
|
157 |
*
|
158 |
* @return void
|
159 |
*/
|
160 |
public function testSessionPath() { |
161 |
TestCakeSession::init('/index.php'); |
162 |
$this->assertEquals('/', TestCakeSession::$path); |
163 |
|
164 |
TestCakeSession::init('/sub_dir/index.php'); |
165 |
$this->assertEquals('/sub_dir/', TestCakeSession::$path); |
166 |
} |
167 |
|
168 |
/**
|
169 |
* testCakeSessionPathEmpty
|
170 |
*
|
171 |
* @return void
|
172 |
*/
|
173 |
public function testCakeSessionPathEmpty() { |
174 |
TestCakeSession::init(''); |
175 |
$this->assertEquals('/', TestCakeSession::$path, 'Session path is empty, with "" as $base needs to be /'); |
176 |
} |
177 |
|
178 |
/**
|
179 |
* testCakeSessionPathContainsParams
|
180 |
*
|
181 |
* @return void
|
182 |
*/
|
183 |
public function testCakeSessionPathContainsQuestion() { |
184 |
TestCakeSession::init('/index.php?'); |
185 |
$this->assertEquals('/', TestCakeSession::$path); |
186 |
} |
187 |
|
188 |
/**
|
189 |
* testSetHost
|
190 |
*
|
191 |
* @return void
|
192 |
*/
|
193 |
public function testSetHost() { |
194 |
TestCakeSession::init();
|
195 |
TestCakeSession::setHost('cakephp.org'); |
196 |
$this->assertEquals('cakephp.org', TestCakeSession::$host); |
197 |
} |
198 |
|
199 |
/**
|
200 |
* testSetHostWithPort
|
201 |
*
|
202 |
* @return void
|
203 |
*/
|
204 |
public function testSetHostWithPort() { |
205 |
TestCakeSession::init();
|
206 |
TestCakeSession::setHost('cakephp.org:443'); |
207 |
$this->assertEquals('cakephp.org', TestCakeSession::$host); |
208 |
} |
209 |
|
210 |
/**
|
211 |
* test valid with bogus user agent.
|
212 |
*
|
213 |
* @return void
|
214 |
*/
|
215 |
public function testValidBogusUserAgent() { |
216 |
Configure::write('Session.checkAgent', true); |
217 |
TestCakeSession::start();
|
218 |
$this->assertTrue(TestCakeSession::valid(), 'Newly started session should be valid'); |
219 |
|
220 |
TestCakeSession::userAgent('bogus!'); |
221 |
$this->assertFalse(TestCakeSession::valid(), 'user agent mismatch should fail.'); |
222 |
} |
223 |
|
224 |
/**
|
225 |
* test valid with bogus user agent.
|
226 |
*
|
227 |
* @return void
|
228 |
*/
|
229 |
public function testValidTimeExpiry() { |
230 |
Configure::write('Session.checkAgent', true); |
231 |
TestCakeSession::start();
|
232 |
$this->assertTrue(TestCakeSession::valid(), 'Newly started session should be valid'); |
233 |
|
234 |
TestCakeSession::$time = strtotime('next year'); |
235 |
$this->assertFalse(TestCakeSession::valid(), 'time should cause failure.'); |
236 |
} |
237 |
|
238 |
/**
|
239 |
* testCheck method
|
240 |
*
|
241 |
* @return void
|
242 |
*/
|
243 |
public function testCheck() { |
244 |
TestCakeSession::write('SessionTestCase', 'value'); |
245 |
$this->assertTrue(TestCakeSession::check('SessionTestCase')); |
246 |
|
247 |
$this->assertFalse(TestCakeSession::check('NotExistingSessionTestCase')); |
248 |
} |
249 |
|
250 |
/**
|
251 |
* testSimpleRead method
|
252 |
*
|
253 |
* @return void
|
254 |
*/
|
255 |
public function testSimpleRead() { |
256 |
TestCakeSession::write('testing', '1,2,3'); |
257 |
$result = TestCakeSession::read('testing'); |
258 |
$this->assertEquals('1,2,3', $result); |
259 |
|
260 |
TestCakeSession::write('testing', array('1' => 'one', '2' => 'two', '3' => 'three')); |
261 |
$result = TestCakeSession::read('testing.1'); |
262 |
$this->assertEquals('one', $result); |
263 |
|
264 |
$result = TestCakeSession::read('testing'); |
265 |
$this->assertEquals(array('1' => 'one', '2' => 'two', '3' => 'three'), $result); |
266 |
|
267 |
$result = TestCakeSession::read(); |
268 |
$this->assertTrue(isset($result['testing'])); |
269 |
$this->assertTrue(isset($result['Config'])); |
270 |
$this->assertTrue(isset($result['Config']['userAgent'])); |
271 |
|
272 |
TestCakeSession::write('This.is.a.deep.array.my.friend', 'value'); |
273 |
$result = TestCakeSession::read('This.is.a.deep.array.my.friend'); |
274 |
$this->assertEquals('value', $result); |
275 |
} |
276 |
|
277 |
/**
|
278 |
* testReadyEmpty
|
279 |
*
|
280 |
* @return void
|
281 |
*/
|
282 |
public function testReadyEmpty() { |
283 |
$this->assertNull(TestCakeSession::read('')); |
284 |
} |
285 |
|
286 |
/**
|
287 |
* test writing a hash of values/
|
288 |
*
|
289 |
* @return void
|
290 |
*/
|
291 |
public function testWriteArray() { |
292 |
$result = TestCakeSession::write(array( |
293 |
'one' => 1, |
294 |
'two' => 2, |
295 |
'three' => array('something'), |
296 |
'null' => null |
297 |
)); |
298 |
$this->assertTrue($result); |
299 |
$this->assertEquals(1, TestCakeSession::read('one')); |
300 |
$this->assertEquals(array('something'), TestCakeSession::read('three')); |
301 |
$this->assertEquals(null, TestCakeSession::read('null')); |
302 |
} |
303 |
|
304 |
/**
|
305 |
* testWriteEmptyKey
|
306 |
*
|
307 |
* @return void
|
308 |
*/
|
309 |
public function testWriteEmptyKey() { |
310 |
$this->assertFalse(TestCakeSession::write('', 'graham')); |
311 |
$this->assertFalse(TestCakeSession::write('', '')); |
312 |
$this->assertFalse(TestCakeSession::write('')); |
313 |
} |
314 |
|
315 |
/**
|
316 |
* Test overwriting a string value as if it were an array.
|
317 |
*
|
318 |
* @return void
|
319 |
*/
|
320 |
public function testWriteOverwriteStringValue() { |
321 |
TestCakeSession::write('Some.string', 'value'); |
322 |
$this->assertEquals('value', TestCakeSession::read('Some.string')); |
323 |
|
324 |
TestCakeSession::write('Some.string.array', array('values')); |
325 |
$this->assertEquals(
|
326 |
array('values'), |
327 |
TestCakeSession::read('Some.string.array') |
328 |
); |
329 |
} |
330 |
|
331 |
/**
|
332 |
* Test consuming session data.
|
333 |
*
|
334 |
* @return void
|
335 |
*/
|
336 |
public function testConsume() { |
337 |
TestCakeSession::write('Some.string', 'value'); |
338 |
TestCakeSession::write('Some.array', array('key1' => 'value1', 'key2' => 'value2')); |
339 |
$this->assertEquals('value', TestCakeSession::read('Some.string')); |
340 |
$value = TestCakeSession::consume('Some.string'); |
341 |
$this->assertEquals('value', $value); |
342 |
$this->assertFalse(TestCakeSession::check('Some.string')); |
343 |
$value = TestCakeSession::consume(''); |
344 |
$this->assertNull($value); |
345 |
$value = TestCakeSession::consume(null); |
346 |
$this->assertNull($value); |
347 |
$value = TestCakeSession::consume('Some.array'); |
348 |
$expected = array('key1' => 'value1', 'key2' => 'value2'); |
349 |
$this->assertEquals($expected, $value); |
350 |
$this->assertFalse(TestCakeSession::check('Some.array')); |
351 |
} |
352 |
|
353 |
/**
|
354 |
* testId method
|
355 |
*
|
356 |
* @return void
|
357 |
*/
|
358 |
public function testId() { |
359 |
TestCakeSession::destroy();
|
360 |
|
361 |
$result = TestCakeSession::id(); |
362 |
$expected = session_id();
|
363 |
$this->assertEquals($expected, $result); |
364 |
|
365 |
TestCakeSession::id('MySessionId'); |
366 |
$result = TestCakeSession::id(); |
367 |
$this->assertEquals('MySessionId', $result); |
368 |
} |
369 |
|
370 |
/**
|
371 |
* testStarted method
|
372 |
*
|
373 |
* @return void
|
374 |
*/
|
375 |
public function testStarted() { |
376 |
unset($_SESSION); |
377 |
$_SESSION = null; |
378 |
|
379 |
$this->assertFalse(TestCakeSession::started()); |
380 |
$this->assertTrue(TestCakeSession::start()); |
381 |
$this->assertTrue(TestCakeSession::started()); |
382 |
} |
383 |
|
384 |
/**
|
385 |
* testDel method
|
386 |
*
|
387 |
* @return void
|
388 |
*/
|
389 |
public function testDelete() { |
390 |
$this->assertTrue(TestCakeSession::write('Delete.me', 'Clearing out')); |
391 |
$this->assertTrue(TestCakeSession::delete('Delete.me')); |
392 |
$this->assertFalse(TestCakeSession::check('Delete.me')); |
393 |
$this->assertTrue(TestCakeSession::check('Delete')); |
394 |
|
395 |
$this->assertTrue(TestCakeSession::write('Clearing.sale', 'everything must go')); |
396 |
$this->assertFalse(TestCakeSession::delete('')); |
397 |
$this->assertTrue(TestCakeSession::check('Clearing.sale')); |
398 |
$this->assertFalse(TestCakeSession::delete(null)); |
399 |
$this->assertTrue(TestCakeSession::check('Clearing.sale')); |
400 |
|
401 |
$this->assertTrue(TestCakeSession::delete('Clearing')); |
402 |
$this->assertFalse(TestCakeSession::check('Clearing.sale')); |
403 |
$this->assertFalse(TestCakeSession::check('Clearing')); |
404 |
} |
405 |
|
406 |
/**
|
407 |
* testClear method
|
408 |
*
|
409 |
* @return void
|
410 |
*/
|
411 |
public function testClear() { |
412 |
$this->assertTrue(TestCakeSession::write('Delete.me', 'Clearing out')); |
413 |
TestCakeSession::clear(false); |
414 |
$this->assertFalse(TestCakeSession::check('Delete.me')); |
415 |
$this->assertFalse(TestCakeSession::check('Delete')); |
416 |
|
417 |
TestCakeSession::write('Some.string', 'value'); |
418 |
TestCakeSession::clear(false); |
419 |
$this->assertNull(TestCakeSession::read('Some')); |
420 |
|
421 |
TestCakeSession::write('Some.string.array', array('values')); |
422 |
TestCakeSession::clear(false); |
423 |
$this->assertFalse(TestCakeSession::read()); |
424 |
} |
425 |
|
426 |
/**
|
427 |
* testDestroy method
|
428 |
*
|
429 |
* @return void
|
430 |
*/
|
431 |
public function testDestroy() { |
432 |
TestCakeSession::write('bulletProof', 'invincible'); |
433 |
$id = TestCakeSession::id(); |
434 |
TestCakeSession::destroy();
|
435 |
|
436 |
$this->assertFalse(TestCakeSession::check('bulletProof')); |
437 |
$this->assertNotEquals(TestCakeSession::id(), $id); |
438 |
} |
439 |
|
440 |
/**
|
441 |
* testCheckingSavedEmpty method
|
442 |
*
|
443 |
* @return void
|
444 |
*/
|
445 |
public function testCheckingSavedEmpty() { |
446 |
$this->assertTrue(TestCakeSession::write('SessionTestCase', 0)); |
447 |
$this->assertTrue(TestCakeSession::check('SessionTestCase')); |
448 |
|
449 |
$this->assertTrue(TestCakeSession::write('SessionTestCase', '0')); |
450 |
$this->assertTrue(TestCakeSession::check('SessionTestCase')); |
451 |
|
452 |
$this->assertTrue(TestCakeSession::write('SessionTestCase', false)); |
453 |
$this->assertTrue(TestCakeSession::check('SessionTestCase')); |
454 |
|
455 |
$this->assertTrue(TestCakeSession::write('SessionTestCase', null)); |
456 |
$this->assertFalse(TestCakeSession::check('SessionTestCase')); |
457 |
} |
458 |
|
459 |
/**
|
460 |
* testCheckKeyWithSpaces method
|
461 |
*
|
462 |
* @return void
|
463 |
*/
|
464 |
public function testCheckKeyWithSpaces() { |
465 |
$this->assertTrue(TestCakeSession::write('Session Test', "test")); |
466 |
$this->assertTrue(TestCakeSession::check('Session Test')); |
467 |
TestCakeSession::delete('Session Test'); |
468 |
|
469 |
$this->assertTrue(TestCakeSession::write('Session Test.Test Case', "test")); |
470 |
$this->assertTrue(TestCakeSession::check('Session Test.Test Case')); |
471 |
} |
472 |
|
473 |
/**
|
474 |
* testCheckEmpty
|
475 |
*
|
476 |
* @return void
|
477 |
*/
|
478 |
public function testCheckEmpty() { |
479 |
$this->assertFalse(TestCakeSession::check('')); |
480 |
$this->assertFalse(TestCakeSession::check(null)); |
481 |
} |
482 |
|
483 |
/**
|
484 |
* test key exploitation
|
485 |
*
|
486 |
* @return void
|
487 |
*/
|
488 |
public function testKeyExploit() { |
489 |
$key = "a'] = 1; phpinfo(); \$_SESSION['a"; |
490 |
$result = TestCakeSession::write($key, 'haxored'); |
491 |
$this->assertFalse($result); |
492 |
|
493 |
$result = TestCakeSession::read($key); |
494 |
$this->assertNull($result); |
495 |
} |
496 |
|
497 |
/**
|
498 |
* testReadingSavedEmpty method
|
499 |
*
|
500 |
* @return void
|
501 |
*/
|
502 |
public function testReadingSavedEmpty() { |
503 |
TestCakeSession::write('SessionTestCase', 0); |
504 |
$this->assertEquals(0, TestCakeSession::read('SessionTestCase')); |
505 |
|
506 |
TestCakeSession::write('SessionTestCase', '0'); |
507 |
$this->assertEquals('0', TestCakeSession::read('SessionTestCase')); |
508 |
$this->assertFalse(TestCakeSession::read('SessionTestCase') === 0); |
509 |
|
510 |
TestCakeSession::write('SessionTestCase', false); |
511 |
$this->assertFalse(TestCakeSession::read('SessionTestCase')); |
512 |
|
513 |
TestCakeSession::write('SessionTestCase', null); |
514 |
$this->assertEquals(null, TestCakeSession::read('SessionTestCase')); |
515 |
} |
516 |
|
517 |
/**
|
518 |
* testCheckUserAgentFalse method
|
519 |
*
|
520 |
* @return void
|
521 |
*/
|
522 |
public function testCheckUserAgentFalse() { |
523 |
Configure::write('Session.checkAgent', false); |
524 |
TestCakeSession::setUserAgent(md5('http://randomdomainname.com' . Configure::read('Security.salt'))); |
525 |
$this->assertTrue(TestCakeSession::valid()); |
526 |
} |
527 |
|
528 |
/**
|
529 |
* testCheckUserAgentTrue method
|
530 |
*
|
531 |
* @return void
|
532 |
*/
|
533 |
public function testCheckUserAgentTrue() { |
534 |
Configure::write('Session.checkAgent', true); |
535 |
TestCakeSession::$error = false; |
536 |
$agent = md5('http://randomdomainname.com' . Configure::read('Security.salt')); |
537 |
|
538 |
TestCakeSession::write('Config.userAgent', md5('Hacking you!')); |
539 |
TestCakeSession::setUserAgent($agent); |
540 |
$this->assertFalse(TestCakeSession::valid()); |
541 |
} |
542 |
|
543 |
/**
|
544 |
* testReadAndWriteWithCakeStorage method
|
545 |
*
|
546 |
* @return void
|
547 |
*/
|
548 |
public function testReadAndWriteWithCakeStorage() { |
549 |
Configure::write('Session.defaults', 'cake'); |
550 |
|
551 |
TestCakeSession::init();
|
552 |
TestCakeSession::start();
|
553 |
|
554 |
TestCakeSession::write('SessionTestCase', 0); |
555 |
$this->assertEquals(0, TestCakeSession::read('SessionTestCase')); |
556 |
|
557 |
TestCakeSession::write('SessionTestCase', '0'); |
558 |
$this->assertEquals('0', TestCakeSession::read('SessionTestCase')); |
559 |
$this->assertFalse(TestCakeSession::read('SessionTestCase') === 0); |
560 |
|
561 |
TestCakeSession::write('SessionTestCase', false); |
562 |
$this->assertFalse(TestCakeSession::read('SessionTestCase')); |
563 |
|
564 |
TestCakeSession::write('SessionTestCase', null); |
565 |
$this->assertEquals(null, TestCakeSession::read('SessionTestCase')); |
566 |
|
567 |
TestCakeSession::write('SessionTestCase', 'This is a Test'); |
568 |
$this->assertEquals('This is a Test', TestCakeSession::read('SessionTestCase')); |
569 |
|
570 |
TestCakeSession::write('SessionTestCase', 'This is a Test'); |
571 |
TestCakeSession::write('SessionTestCase', 'This was updated'); |
572 |
$this->assertEquals('This was updated', TestCakeSession::read('SessionTestCase')); |
573 |
|
574 |
TestCakeSession::destroy();
|
575 |
$this->assertNull(TestCakeSession::read('SessionTestCase')); |
576 |
} |
577 |
|
578 |
/**
|
579 |
* test using a handler from app/Model/Datasource/Session.
|
580 |
*
|
581 |
* @return void
|
582 |
*/
|
583 |
public function testUsingAppLibsHandler() { |
584 |
App::build(array( |
585 |
'Model/Datasource/Session' => array( |
586 |
CAKE . 'Test' . DS . 'test_app' . DS . 'Model' . DS . 'Datasource' . DS . 'Session' . DS |
587 |
), |
588 |
'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS) |
589 |
), App::RESET); |
590 |
Configure::write('Session', array( |
591 |
'defaults' => 'cake', |
592 |
'handler' => array( |
593 |
'engine' => 'TestAppLibSession' |
594 |
) |
595 |
)); |
596 |
|
597 |
TestCakeSession::start();
|
598 |
$this->assertTrue(TestCakeSession::started()); |
599 |
|
600 |
TestCakeSession::destroy();
|
601 |
$this->assertFalse(TestCakeSession::started()); |
602 |
|
603 |
App::build();
|
604 |
} |
605 |
|
606 |
/**
|
607 |
* test using a handler from a plugin.
|
608 |
*
|
609 |
* @return void
|
610 |
*/
|
611 |
public function testUsingPluginHandler() { |
612 |
App::build(array( |
613 |
'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS) |
614 |
), App::RESET); |
615 |
CakePlugin::load('TestPlugin'); |
616 |
|
617 |
Configure::write('Session', array( |
618 |
'defaults' => 'cake', |
619 |
'handler' => array( |
620 |
'engine' => 'TestPlugin.TestPluginSession' |
621 |
) |
622 |
)); |
623 |
|
624 |
TestCakeSession::start();
|
625 |
$this->assertTrue(TestCakeSession::started()); |
626 |
|
627 |
TestCakeSession::destroy();
|
628 |
$this->assertFalse(TestCakeSession::started()); |
629 |
|
630 |
App::build();
|
631 |
} |
632 |
|
633 |
/**
|
634 |
* testReadAndWriteWithCacheStorage method
|
635 |
*
|
636 |
* @return void
|
637 |
*/
|
638 |
public function testReadAndWriteWithCacheStorage() { |
639 |
Configure::write('Session.defaults', 'cache'); |
640 |
Configure::write('Session.handler.engine', 'TestCacheSession'); |
641 |
|
642 |
TestCakeSession::init();
|
643 |
TestCakeSession::destroy();
|
644 |
|
645 |
TestCakeSession::write('SessionTestCase', 0); |
646 |
$this->assertEquals(0, TestCakeSession::read('SessionTestCase')); |
647 |
|
648 |
TestCakeSession::write('SessionTestCase', '0'); |
649 |
$this->assertEquals('0', TestCakeSession::read('SessionTestCase')); |
650 |
$this->assertFalse(TestCakeSession::read('SessionTestCase') === 0); |
651 |
|
652 |
TestCakeSession::write('SessionTestCase', false); |
653 |
$this->assertFalse(TestCakeSession::read('SessionTestCase')); |
654 |
|
655 |
TestCakeSession::write('SessionTestCase', null); |
656 |
$this->assertEquals(null, TestCakeSession::read('SessionTestCase')); |
657 |
|
658 |
TestCakeSession::write('SessionTestCase', 'This is a Test'); |
659 |
$this->assertEquals('This is a Test', TestCakeSession::read('SessionTestCase')); |
660 |
|
661 |
TestCakeSession::write('SessionTestCase', 'This is a Test'); |
662 |
TestCakeSession::write('SessionTestCase', 'This was updated'); |
663 |
$this->assertEquals('This was updated', TestCakeSession::read('SessionTestCase')); |
664 |
|
665 |
TestCakeSession::destroy();
|
666 |
$this->assertNull(TestCakeSession::read('SessionTestCase')); |
667 |
} |
668 |
|
669 |
/**
|
670 |
* test that changing the config name of the cache config works.
|
671 |
*
|
672 |
* @return void
|
673 |
*/
|
674 |
public function testReadAndWriteWithCustomCacheConfig() { |
675 |
Configure::write('Session.defaults', 'cache'); |
676 |
Configure::write('Session.handler.engine', 'TestCacheSession'); |
677 |
Configure::write('Session.handler.config', 'session_test'); |
678 |
|
679 |
Cache::config('session_test', array( |
680 |
'engine' => 'File', |
681 |
'prefix' => 'session_test_', |
682 |
)); |
683 |
|
684 |
TestCakeSession::init();
|
685 |
TestCakeSession::start();
|
686 |
|
687 |
TestCakeSession::write('SessionTestCase', 'Some value'); |
688 |
$this->assertEquals('Some value', TestCakeSession::read('SessionTestCase')); |
689 |
$id = TestCakeSession::id(); |
690 |
|
691 |
Cache::delete($id, 'session_test'); |
692 |
} |
693 |
|
694 |
/**
|
695 |
* testReadAndWriteWithDatabaseStorage method
|
696 |
*
|
697 |
* @return void
|
698 |
*/
|
699 |
public function testReadAndWriteWithDatabaseStorage() { |
700 |
Configure::write('Session.defaults', 'database'); |
701 |
Configure::write('Session.handler.engine', 'TestDatabaseSession'); |
702 |
Configure::write('Session.handler.table', 'sessions'); |
703 |
Configure::write('Session.handler.model', 'Session'); |
704 |
Configure::write('Session.handler.database', 'test'); |
705 |
|
706 |
TestCakeSession::init();
|
707 |
$this->assertNull(TestCakeSession::id()); |
708 |
|
709 |
TestCakeSession::start();
|
710 |
$expected = session_id();
|
711 |
$this->assertEquals($expected, TestCakeSession::id()); |
712 |
|
713 |
TestCakeSession::renew();
|
714 |
$this->assertFalse($expected === TestCakeSession::id()); |
715 |
|
716 |
$expected = session_id();
|
717 |
$this->assertEquals($expected, TestCakeSession::id()); |
718 |
|
719 |
TestCakeSession::write('SessionTestCase', 0); |
720 |
$this->assertEquals(0, TestCakeSession::read('SessionTestCase')); |
721 |
|
722 |
TestCakeSession::write('SessionTestCase', '0'); |
723 |
$this->assertEquals('0', TestCakeSession::read('SessionTestCase')); |
724 |
$this->assertFalse(TestCakeSession::read('SessionTestCase') === 0); |
725 |
|
726 |
TestCakeSession::write('SessionTestCase', false); |
727 |
$this->assertFalse(TestCakeSession::read('SessionTestCase')); |
728 |
|
729 |
TestCakeSession::write('SessionTestCase', null); |
730 |
$this->assertEquals(null, TestCakeSession::read('SessionTestCase')); |
731 |
|
732 |
TestCakeSession::write('SessionTestCase', 'This is a Test'); |
733 |
$this->assertEquals('This is a Test', TestCakeSession::read('SessionTestCase')); |
734 |
|
735 |
TestCakeSession::write('SessionTestCase', 'Some additional data'); |
736 |
$this->assertEquals('Some additional data', TestCakeSession::read('SessionTestCase')); |
737 |
|
738 |
TestCakeSession::destroy();
|
739 |
$this->assertNull(TestCakeSession::read('SessionTestCase')); |
740 |
|
741 |
Configure::write('Session', array( |
742 |
'defaults' => 'php' |
743 |
)); |
744 |
TestCakeSession::init();
|
745 |
} |
746 |
|
747 |
/**
|
748 |
* testSessionTimeout method
|
749 |
*
|
750 |
* @return void
|
751 |
*/
|
752 |
public function testSessionTimeout() { |
753 |
Configure::write('debug', 2); |
754 |
Configure::write('Session.defaults', 'cake'); |
755 |
Configure::write('Session.autoRegenerate', false); |
756 |
|
757 |
$timeoutSeconds = Configure::read('Session.timeout') * 60; |
758 |
|
759 |
TestCakeSession::destroy();
|
760 |
TestCakeSession::write('Test', 'some value'); |
761 |
|
762 |
$this->assertWithinMargin(time() + $timeoutSeconds, CakeSession::$sessionTime, 1); |
763 |
$this->assertEquals(10, $_SESSION['Config']['countdown']); |
764 |
$this->assertWithinMargin(CakeSession::$sessionTime, $_SESSION['Config']['time'], 1); |
765 |
$this->assertWithinMargin(time(), CakeSession::$time, 1); |
766 |
$this->assertWithinMargin(time() + $timeoutSeconds, $_SESSION['Config']['time'], 1); |
767 |
|
768 |
Configure::write('Session.harden', true); |
769 |
TestCakeSession::destroy();
|
770 |
|
771 |
TestCakeSession::write('Test', 'some value'); |
772 |
$this->assertWithinMargin(time() + $timeoutSeconds, CakeSession::$sessionTime, 1); |
773 |
$this->assertEquals(10, $_SESSION['Config']['countdown']); |
774 |
$this->assertWithinMargin(CakeSession::$sessionTime, $_SESSION['Config']['time'], 1); |
775 |
$this->assertWithinMargin(time(), CakeSession::$time, 1); |
776 |
$this->assertWithinMargin(CakeSession::$time + $timeoutSeconds, $_SESSION['Config']['time'], 1); |
777 |
} |
778 |
|
779 |
/**
|
780 |
* Test that cookieTimeout matches timeout when unspecified.
|
781 |
*
|
782 |
* @return void
|
783 |
*/
|
784 |
public function testCookieTimeoutFallback() { |
785 |
$_SESSION = null; |
786 |
Configure::write('Session', array( |
787 |
'defaults' => 'cake', |
788 |
'timeout' => 400, |
789 |
)); |
790 |
TestCakeSession::start();
|
791 |
$this->assertEquals(400, Configure::read('Session.cookieTimeout')); |
792 |
$this->assertEquals(400, Configure::read('Session.timeout')); |
793 |
$this->assertEquals(400 * 60, ini_get('session.cookie_lifetime')); |
794 |
$this->assertEquals(400 * 60, ini_get('session.gc_maxlifetime')); |
795 |
|
796 |
$_SESSION = null; |
797 |
Configure::write('Session', array( |
798 |
'defaults' => 'cake', |
799 |
'timeout' => 400, |
800 |
'cookieTimeout' => 600 |
801 |
)); |
802 |
TestCakeSession::start();
|
803 |
$this->assertEquals(600, Configure::read('Session.cookieTimeout')); |
804 |
$this->assertEquals(400, Configure::read('Session.timeout')); |
805 |
} |
806 |
|
807 |
/**
|
808 |
* Proves that invalid sessions will be destroyed and re-created
|
809 |
* if invalid
|
810 |
*
|
811 |
* @return void
|
812 |
*/
|
813 |
public function testInvalidSessionRenew() { |
814 |
TestCakeSession::start();
|
815 |
$this->assertNotEmpty($_SESSION['Config']); |
816 |
$data = $_SESSION; |
817 |
|
818 |
session_write_close(); |
819 |
$_SESSION = null; |
820 |
|
821 |
TestCakeSession::start();
|
822 |
$this->assertEquals($data, $_SESSION); |
823 |
TestCakeSession::write('Foo', 'Bar'); |
824 |
|
825 |
session_write_close(); |
826 |
$_SESSION = null; |
827 |
|
828 |
TestCakeSession::userAgent('bogus!'); |
829 |
TestCakeSession::start();
|
830 |
$this->assertNotEquals($data, $_SESSION); |
831 |
$this->assertEquals('bogus!', $_SESSION['Config']['userAgent']); |
832 |
} |
833 |
|
834 |
} |