Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
15403 manish.sha 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
		self::$_userAgent = $value;
32
	}
33
 
34
	public static function setHost($host) {
35
		self::_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
		self::$_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', self::$_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
 * testId method
333
 *
334
 * @return void
335
 */
336
	public function testId() {
337
		TestCakeSession::destroy();
338
 
339
		$result = TestCakeSession::id();
340
		$expected = session_id();
341
		$this->assertEquals($expected, $result);
342
 
343
		TestCakeSession::id('MySessionId');
344
		$result = TestCakeSession::id();
345
		$this->assertEquals('MySessionId', $result);
346
	}
347
 
348
/**
349
 * testStarted method
350
 *
351
 * @return void
352
 */
353
	public function testStarted() {
354
		unset($_SESSION);
355
		$_SESSION = null;
356
 
357
		$this->assertFalse(TestCakeSession::started());
358
		$this->assertTrue(TestCakeSession::start());
359
		$this->assertTrue(TestCakeSession::started());
360
	}
361
 
362
/**
363
 * testDel method
364
 *
365
 * @return void
366
 */
367
	public function testDelete() {
368
		$this->assertTrue(TestCakeSession::write('Delete.me', 'Clearing out'));
369
		$this->assertTrue(TestCakeSession::delete('Delete.me'));
370
		$this->assertFalse(TestCakeSession::check('Delete.me'));
371
		$this->assertTrue(TestCakeSession::check('Delete'));
372
 
373
		$this->assertTrue(TestCakeSession::write('Clearing.sale', 'everything must go'));
374
		$this->assertTrue(TestCakeSession::delete('Clearing'));
375
		$this->assertFalse(TestCakeSession::check('Clearing.sale'));
376
		$this->assertFalse(TestCakeSession::check('Clearing'));
377
	}
378
 
379
/**
380
 * testDestroy method
381
 *
382
 * @return void
383
 */
384
	public function testDestroy() {
385
		TestCakeSession::write('bulletProof', 'invincible');
386
		$id = TestCakeSession::id();
387
		TestCakeSession::destroy();
388
 
389
		$this->assertFalse(TestCakeSession::check('bulletProof'));
390
		$this->assertNotEquals(TestCakeSession::id(), $id);
391
	}
392
 
393
/**
394
 * testCheckingSavedEmpty method
395
 *
396
 * @return void
397
 */
398
	public function testCheckingSavedEmpty() {
399
		$this->assertTrue(TestCakeSession::write('SessionTestCase', 0));
400
		$this->assertTrue(TestCakeSession::check('SessionTestCase'));
401
 
402
		$this->assertTrue(TestCakeSession::write('SessionTestCase', '0'));
403
		$this->assertTrue(TestCakeSession::check('SessionTestCase'));
404
 
405
		$this->assertTrue(TestCakeSession::write('SessionTestCase', false));
406
		$this->assertTrue(TestCakeSession::check('SessionTestCase'));
407
 
408
		$this->assertTrue(TestCakeSession::write('SessionTestCase', null));
409
		$this->assertFalse(TestCakeSession::check('SessionTestCase'));
410
	}
411
 
412
/**
413
 * testCheckKeyWithSpaces method
414
 *
415
 * @return void
416
 */
417
	public function testCheckKeyWithSpaces() {
418
		$this->assertTrue(TestCakeSession::write('Session Test', "test"));
419
		$this->assertTrue(TestCakeSession::check('Session Test'));
420
		TestCakeSession::delete('Session Test');
421
 
422
		$this->assertTrue(TestCakeSession::write('Session Test.Test Case', "test"));
423
		$this->assertTrue(TestCakeSession::check('Session Test.Test Case'));
424
	}
425
 
426
/**
427
 * testCheckEmpty
428
 *
429
 * @return void
430
 */
431
	public function testCheckEmpty() {
432
		$this->assertFalse(TestCakeSession::check());
433
	}
434
 
435
/**
436
 * test key exploitation
437
 *
438
 * @return void
439
 */
440
	public function testKeyExploit() {
441
		$key = "a'] = 1; phpinfo(); \$_SESSION['a";
442
		$result = TestCakeSession::write($key, 'haxored');
443
		$this->assertFalse($result);
444
 
445
		$result = TestCakeSession::read($key);
446
		$this->assertNull($result);
447
	}
448
 
449
/**
450
 * testReadingSavedEmpty method
451
 *
452
 * @return void
453
 */
454
	public function testReadingSavedEmpty() {
455
		TestCakeSession::write('SessionTestCase', 0);
456
		$this->assertEquals(0, TestCakeSession::read('SessionTestCase'));
457
 
458
		TestCakeSession::write('SessionTestCase', '0');
459
		$this->assertEquals('0', TestCakeSession::read('SessionTestCase'));
460
		$this->assertFalse(TestCakeSession::read('SessionTestCase') === 0);
461
 
462
		TestCakeSession::write('SessionTestCase', false);
463
		$this->assertFalse(TestCakeSession::read('SessionTestCase'));
464
 
465
		TestCakeSession::write('SessionTestCase', null);
466
		$this->assertEquals(null, TestCakeSession::read('SessionTestCase'));
467
	}
468
 
469
/**
470
 * testCheckUserAgentFalse method
471
 *
472
 * @return void
473
 */
474
	public function testCheckUserAgentFalse() {
475
		Configure::write('Session.checkAgent', false);
476
		TestCakeSession::setUserAgent(md5('http://randomdomainname.com' . Configure::read('Security.salt')));
477
		$this->assertTrue(TestCakeSession::valid());
478
	}
479
 
480
/**
481
 * testCheckUserAgentTrue method
482
 *
483
 * @return void
484
 */
485
	public function testCheckUserAgentTrue() {
486
		Configure::write('Session.checkAgent', true);
487
		TestCakeSession::$error = false;
488
		$agent = md5('http://randomdomainname.com' . Configure::read('Security.salt'));
489
 
490
		TestCakeSession::write('Config.userAgent', md5('Hacking you!'));
491
		TestCakeSession::setUserAgent($agent);
492
		$this->assertFalse(TestCakeSession::valid());
493
	}
494
 
495
/**
496
 * testReadAndWriteWithCakeStorage method
497
 *
498
 * @return void
499
 */
500
	public function testReadAndWriteWithCakeStorage() {
501
		Configure::write('Session.defaults', 'cake');
502
 
503
		TestCakeSession::init();
504
		TestCakeSession::start();
505
 
506
		TestCakeSession::write('SessionTestCase', 0);
507
		$this->assertEquals(0, TestCakeSession::read('SessionTestCase'));
508
 
509
		TestCakeSession::write('SessionTestCase', '0');
510
		$this->assertEquals('0', TestCakeSession::read('SessionTestCase'));
511
		$this->assertFalse(TestCakeSession::read('SessionTestCase') === 0);
512
 
513
		TestCakeSession::write('SessionTestCase', false);
514
		$this->assertFalse(TestCakeSession::read('SessionTestCase'));
515
 
516
		TestCakeSession::write('SessionTestCase', null);
517
		$this->assertEquals(null, TestCakeSession::read('SessionTestCase'));
518
 
519
		TestCakeSession::write('SessionTestCase', 'This is a Test');
520
		$this->assertEquals('This is a Test', TestCakeSession::read('SessionTestCase'));
521
 
522
		TestCakeSession::write('SessionTestCase', 'This is a Test');
523
		TestCakeSession::write('SessionTestCase', 'This was updated');
524
		$this->assertEquals('This was updated', TestCakeSession::read('SessionTestCase'));
525
 
526
		TestCakeSession::destroy();
527
		$this->assertNull(TestCakeSession::read('SessionTestCase'));
528
	}
529
 
530
/**
531
 * test using a handler from app/Model/Datasource/Session.
532
 *
533
 * @return void
534
 */
535
	public function testUsingAppLibsHandler() {
536
		App::build(array(
537
			'Model/Datasource/Session' => array(
538
				CAKE . 'Test' . DS . 'test_app' . DS . 'Model' . DS . 'Datasource' . DS . 'Session' . DS
539
			),
540
			'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
541
		), App::RESET);
542
		Configure::write('Session', array(
543
			'defaults' => 'cake',
544
			'handler' => array(
545
				'engine' => 'TestAppLibSession'
546
			)
547
		));
548
 
549
		TestCakeSession::start();
550
		$this->assertTrue(TestCakeSession::started());
551
 
552
		TestCakeSession::destroy();
553
		$this->assertFalse(TestCakeSession::started());
554
 
555
		App::build();
556
	}
557
 
558
/**
559
 * test using a handler from a plugin.
560
 *
561
 * @return void
562
 */
563
	public function testUsingPluginHandler() {
564
		App::build(array(
565
			'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
566
		), App::RESET);
567
		CakePlugin::load('TestPlugin');
568
 
569
		Configure::write('Session', array(
570
			'defaults' => 'cake',
571
			'handler' => array(
572
				'engine' => 'TestPlugin.TestPluginSession'
573
			)
574
		));
575
 
576
		TestCakeSession::start();
577
		$this->assertTrue(TestCakeSession::started());
578
 
579
		TestCakeSession::destroy();
580
		$this->assertFalse(TestCakeSession::started());
581
 
582
		App::build();
583
	}
584
 
585
/**
586
 * testReadAndWriteWithCacheStorage method
587
 *
588
 * @return void
589
 */
590
	public function testReadAndWriteWithCacheStorage() {
591
		Configure::write('Session.defaults', 'cache');
592
		Configure::write('Session.handler.engine', 'TestCacheSession');
593
 
594
		TestCakeSession::init();
595
		TestCakeSession::destroy();
596
 
597
		TestCakeSession::write('SessionTestCase', 0);
598
		$this->assertEquals(0, TestCakeSession::read('SessionTestCase'));
599
 
600
		TestCakeSession::write('SessionTestCase', '0');
601
		$this->assertEquals('0', TestCakeSession::read('SessionTestCase'));
602
		$this->assertFalse(TestCakeSession::read('SessionTestCase') === 0);
603
 
604
		TestCakeSession::write('SessionTestCase', false);
605
		$this->assertFalse(TestCakeSession::read('SessionTestCase'));
606
 
607
		TestCakeSession::write('SessionTestCase', null);
608
		$this->assertEquals(null, TestCakeSession::read('SessionTestCase'));
609
 
610
		TestCakeSession::write('SessionTestCase', 'This is a Test');
611
		$this->assertEquals('This is a Test', TestCakeSession::read('SessionTestCase'));
612
 
613
		TestCakeSession::write('SessionTestCase', 'This is a Test');
614
		TestCakeSession::write('SessionTestCase', 'This was updated');
615
		$this->assertEquals('This was updated', TestCakeSession::read('SessionTestCase'));
616
 
617
		TestCakeSession::destroy();
618
		$this->assertNull(TestCakeSession::read('SessionTestCase'));
619
	}
620
 
621
/**
622
 * test that changing the config name of the cache config works.
623
 *
624
 * @return void
625
 */
626
	public function testReadAndWriteWithCustomCacheConfig() {
627
		Configure::write('Session.defaults', 'cache');
628
		Configure::write('Session.handler.engine', 'TestCacheSession');
629
		Configure::write('Session.handler.config', 'session_test');
630
 
631
		Cache::config('session_test', array(
632
			'engine' => 'File',
633
			'prefix' => 'session_test_',
634
		));
635
 
636
		TestCakeSession::init();
637
		TestCakeSession::start();
638
 
639
		TestCakeSession::write('SessionTestCase', 'Some value');
640
		$this->assertEquals('Some value', TestCakeSession::read('SessionTestCase'));
641
		$id = TestCakeSession::id();
642
 
643
		Cache::delete($id, 'session_test');
644
	}
645
 
646
/**
647
 * testReadAndWriteWithDatabaseStorage method
648
 *
649
 * @return void
650
 */
651
	public function testReadAndWriteWithDatabaseStorage() {
652
		Configure::write('Session.defaults', 'database');
653
		Configure::write('Session.handler.engine', 'TestDatabaseSession');
654
		Configure::write('Session.handler.table', 'sessions');
655
		Configure::write('Session.handler.model', 'Session');
656
		Configure::write('Session.handler.database', 'test');
657
 
658
		TestCakeSession::init();
659
		$this->assertNull(TestCakeSession::id());
660
 
661
		TestCakeSession::start();
662
		$expected = session_id();
663
		$this->assertEquals($expected, TestCakeSession::id());
664
 
665
		TestCakeSession::renew();
666
		$this->assertFalse($expected === TestCakeSession::id());
667
 
668
		$expected = session_id();
669
		$this->assertEquals($expected, TestCakeSession::id());
670
 
671
		TestCakeSession::write('SessionTestCase', 0);
672
		$this->assertEquals(0, TestCakeSession::read('SessionTestCase'));
673
 
674
		TestCakeSession::write('SessionTestCase', '0');
675
		$this->assertEquals('0', TestCakeSession::read('SessionTestCase'));
676
		$this->assertFalse(TestCakeSession::read('SessionTestCase') === 0);
677
 
678
		TestCakeSession::write('SessionTestCase', false);
679
		$this->assertFalse(TestCakeSession::read('SessionTestCase'));
680
 
681
		TestCakeSession::write('SessionTestCase', null);
682
		$this->assertEquals(null, TestCakeSession::read('SessionTestCase'));
683
 
684
		TestCakeSession::write('SessionTestCase', 'This is a Test');
685
		$this->assertEquals('This is a Test', TestCakeSession::read('SessionTestCase'));
686
 
687
		TestCakeSession::write('SessionTestCase', 'Some additional data');
688
		$this->assertEquals('Some additional data', TestCakeSession::read('SessionTestCase'));
689
 
690
		TestCakeSession::destroy();
691
		$this->assertNull(TestCakeSession::read('SessionTestCase'));
692
 
693
		Configure::write('Session', array(
694
			'defaults' => 'php'
695
		));
696
		TestCakeSession::init();
697
	}
698
 
699
/**
700
 * testSessionTimeout method
701
 *
702
 * @return void
703
 */
704
	public function testSessionTimeout() {
705
		Configure::write('debug', 2);
706
		Configure::write('Session.defaults', 'cake');
707
		Configure::write('Session.autoRegenerate', false);
708
 
709
		$timeoutSeconds = Configure::read('Session.timeout') * 60;
710
 
711
		TestCakeSession::destroy();
712
		TestCakeSession::write('Test', 'some value');
713
 
714
		$this->assertWithinMargin(time() + $timeoutSeconds, CakeSession::$sessionTime, 1);
715
		$this->assertEquals(10, $_SESSION['Config']['countdown']);
716
		$this->assertWithinMargin(CakeSession::$sessionTime, $_SESSION['Config']['time'], 1);
717
		$this->assertWithinMargin(time(), CakeSession::$time, 1);
718
		$this->assertWithinMargin(time() + $timeoutSeconds, $_SESSION['Config']['time'], 1);
719
 
720
		Configure::write('Session.harden', true);
721
		TestCakeSession::destroy();
722
 
723
		TestCakeSession::write('Test', 'some value');
724
		$this->assertWithinMargin(time() + $timeoutSeconds, CakeSession::$sessionTime, 1);
725
		$this->assertEquals(10, $_SESSION['Config']['countdown']);
726
		$this->assertWithinMargin(CakeSession::$sessionTime, $_SESSION['Config']['time'], 1);
727
		$this->assertWithinMargin(time(), CakeSession::$time, 1);
728
		$this->assertWithinMargin(CakeSession::$time + $timeoutSeconds, $_SESSION['Config']['time'], 1);
729
	}
730
 
731
/**
732
 * Test that cookieTimeout matches timeout when unspecified.
733
 *
734
 * @return void
735
 */
736
	public function testCookieTimeoutFallback() {
737
		$_SESSION = null;
738
		Configure::write('Session', array(
739
			'defaults' => 'cake',
740
			'timeout' => 400,
741
		));
742
		TestCakeSession::start();
743
		$this->assertEquals(400, Configure::read('Session.cookieTimeout'));
744
		$this->assertEquals(400, Configure::read('Session.timeout'));
745
		$this->assertEquals(400 * 60, ini_get('session.cookie_lifetime'));
746
		$this->assertEquals(400 * 60, ini_get('session.gc_maxlifetime'));
747
 
748
		$_SESSION = null;
749
		Configure::write('Session', array(
750
			'defaults' => 'cake',
751
			'timeout' => 400,
752
			'cookieTimeout' => 600
753
		));
754
		TestCakeSession::start();
755
		$this->assertEquals(600, Configure::read('Session.cookieTimeout'));
756
		$this->assertEquals(400, Configure::read('Session.timeout'));
757
	}
758
 
759
/**
760
 * Proves that invalid sessions will be destroyed and re-created
761
 * if invalid
762
 *
763
 * @return void
764
 */
765
	public function testInvalidSessionRenew() {
766
		TestCakeSession::start();
767
		$this->assertNotEmpty($_SESSION['Config']);
768
		$data = $_SESSION;
769
 
770
		session_write_close();
771
		$_SESSION = null;
772
 
773
		TestCakeSession::start();
774
		$this->assertEquals($data, $_SESSION);
775
		TestCakeSession::write('Foo', 'Bar');
776
 
777
		session_write_close();
778
		$_SESSION = null;
779
 
780
		TestCakeSession::userAgent('bogus!');
781
		TestCakeSession::start();
782
		$this->assertNotEquals($data, $_SESSION);
783
		$this->assertEquals('bogus!', $_SESSION['Config']['userAgent']);
784
	}
785
 
786
}