2008-05-30 11:40:08 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2009-03-18 17:55:58 +00:00
|
|
|
* SessionTest file
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2017-06-10 22:15:34 +00:00
|
|
|
* CakePHP(tm) Tests <https://book.cakephp.org/2.0/en/development/testing.html>
|
2017-06-10 22:10:52 +00:00
|
|
|
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2010-10-03 16:31:21 +00:00
|
|
|
* Licensed under The MIT License
|
2013-02-08 12:22:51 +00:00
|
|
|
* For full copyright and license information, please see the LICENSE.txt
|
2010-10-03 16:31:21 +00:00
|
|
|
* Redistributions of files must retain the above copyright notice
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2017-06-10 22:10:52 +00:00
|
|
|
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
|
2017-06-10 22:15:34 +00:00
|
|
|
* @link https://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
|
2011-07-26 06:16:14 +00:00
|
|
|
* @package Cake.Test.Case.Model.Datasource
|
2008-10-30 17:30:26 +00:00
|
|
|
* @since CakePHP(tm) v 1.2.0.4206
|
2017-06-10 22:23:14 +00:00
|
|
|
* @license https://opensource.org/licenses/mit-license.php MIT License
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-12-09 05:13:11 +00:00
|
|
|
|
|
|
|
App::uses('CakeSession', 'Model/Datasource');
|
2012-03-24 20:32:31 +00:00
|
|
|
App::uses('DatabaseSession', 'Model/Datasource/Session');
|
|
|
|
App::uses('CacheSession', 'Model/Datasource/Session');
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2013-05-30 22:11:14 +00:00
|
|
|
/**
|
2016-08-10 10:22:09 +00:00
|
|
|
* TestCakeSession
|
2013-05-30 22:11:14 +00:00
|
|
|
*
|
|
|
|
* @package Cake.Test.Case.Model.Datasource
|
|
|
|
*/
|
2010-04-04 07:21:01 +00:00
|
|
|
class TestCakeSession extends CakeSession {
|
2012-03-11 12:12:56 +00:00
|
|
|
|
2010-07-05 07:01:22 +00:00
|
|
|
public static function setUserAgent($value) {
|
2015-07-21 08:22:53 +00:00
|
|
|
static::$_userAgent = $value;
|
2010-04-04 07:21:01 +00:00
|
|
|
}
|
2011-04-17 10:35:21 +00:00
|
|
|
|
2010-07-22 09:09:48 +00:00
|
|
|
public static function setHost($host) {
|
2015-07-21 08:22:53 +00:00
|
|
|
static::_setHost($host);
|
2010-07-22 09:09:48 +00:00
|
|
|
}
|
2012-03-11 12:12:56 +00:00
|
|
|
|
2010-04-04 07:21:01 +00:00
|
|
|
}
|
|
|
|
|
2013-05-30 22:11:14 +00:00
|
|
|
/**
|
2016-08-10 10:22:09 +00:00
|
|
|
* TestCacheSession
|
2013-05-30 22:11:14 +00:00
|
|
|
*
|
|
|
|
* @package Cake.Test.Case.Model.Datasource
|
|
|
|
*/
|
2012-03-24 20:32:31 +00:00
|
|
|
class TestCacheSession extends CacheSession {
|
|
|
|
|
|
|
|
protected function _writeSession() {
|
|
|
|
return true;
|
|
|
|
}
|
2012-04-03 03:23:53 +00:00
|
|
|
|
2012-03-24 20:32:31 +00:00
|
|
|
}
|
|
|
|
|
2013-05-30 22:11:14 +00:00
|
|
|
/**
|
2016-08-10 10:22:09 +00:00
|
|
|
* TestDatabaseSession
|
2013-05-30 22:11:14 +00:00
|
|
|
*
|
|
|
|
* @package Cake.Test.Case.Model.Datasource
|
|
|
|
*/
|
2012-03-24 20:32:31 +00:00
|
|
|
class TestDatabaseSession extends DatabaseSession {
|
|
|
|
|
|
|
|
protected function _writeSession() {
|
|
|
|
return true;
|
|
|
|
}
|
2012-04-03 03:23:53 +00:00
|
|
|
|
2012-03-24 20:32:31 +00:00
|
|
|
}
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2010-01-17 00:19:12 +00:00
|
|
|
* CakeSessionTest class
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2011-07-26 06:16:14 +00:00
|
|
|
* @package Cake.Test.Case.Model.Datasource
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-01-17 00:19:12 +00:00
|
|
|
class CakeSessionTest extends CakeTestCase {
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2010-07-25 04:42:28 +00:00
|
|
|
protected static $_gcDivisor;
|
|
|
|
|
2009-04-13 19:47:40 +00:00
|
|
|
/**
|
|
|
|
* Fixtures used in the SessionTest
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $fixtures = array('core.session');
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-06-05 15:20:45 +00:00
|
|
|
/**
|
2010-07-25 04:42:28 +00:00
|
|
|
* setup before class.
|
2009-03-26 14:25:47 +00:00
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2022-12-28 07:03:39 +00:00
|
|
|
public static function setupBeforeClass() : void {
|
2009-03-26 14:25:47 +00:00
|
|
|
// Make sure garbage colector will be called
|
2015-07-21 08:22:53 +00:00
|
|
|
static::$_gcDivisor = ini_get('session.gc_divisor');
|
2009-03-26 14:25:47 +00:00
|
|
|
ini_set('session.gc_divisor', '1');
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2009-03-26 14:25:47 +00:00
|
|
|
/**
|
2010-07-25 04:42:28 +00:00
|
|
|
* teardown after class
|
2009-03-26 14:25:47 +00:00
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2022-12-28 07:03:39 +00:00
|
|
|
public static function teardownAfterClass() : void {
|
2009-03-26 14:25:47 +00:00
|
|
|
// Revert to the default setting
|
2015-07-21 08:22:53 +00:00
|
|
|
ini_set('session.gc_divisor', static::$_gcDivisor);
|
2009-03-26 14:25:47 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2009-03-26 14:25:47 +00:00
|
|
|
/**
|
2008-06-02 19:22:55 +00:00
|
|
|
* setUp method
|
2008-06-05 15:20:45 +00:00
|
|
|
*
|
2008-06-02 19:22:55 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2022-12-28 07:03:39 +00:00
|
|
|
public function setUp() : void {
|
2011-12-04 21:27:51 +00:00
|
|
|
parent::setUp();
|
2010-07-25 04:42:28 +00:00
|
|
|
Configure::write('Session', array(
|
|
|
|
'defaults' => 'php',
|
|
|
|
'cookie' => 'cakephp',
|
|
|
|
'timeout' => 120,
|
|
|
|
'cookieTimeout' => 120,
|
|
|
|
'ini' => array(),
|
|
|
|
));
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2009-04-13 19:47:40 +00:00
|
|
|
/**
|
|
|
|
* tearDown method
|
|
|
|
*
|
|
|
|
* @return void
|
2009-07-24 19:18:37 +00:00
|
|
|
*/
|
2022-12-28 07:03:39 +00:00
|
|
|
public function tearDown() : void {
|
2010-07-27 02:38:40 +00:00
|
|
|
if (TestCakeSession::started()) {
|
2012-03-24 20:43:25 +00:00
|
|
|
session_write_close();
|
2010-07-27 02:38:40 +00:00
|
|
|
}
|
2010-07-25 04:42:28 +00:00
|
|
|
unset($_SESSION);
|
2013-10-07 18:24:28 +00:00
|
|
|
parent::tearDown();
|
2010-07-25 04:42:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* test setting ini properties with Session configuration.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testSessionConfigIniSetting() {
|
2010-07-27 02:38:40 +00:00
|
|
|
$_SESSION = null;
|
2010-07-25 04:42:28 +00:00
|
|
|
|
|
|
|
Configure::write('Session', array(
|
2010-09-20 02:58:30 +00:00
|
|
|
'cookie' => 'test',
|
2010-07-27 02:38:40 +00:00
|
|
|
'checkAgent' => false,
|
2010-07-25 04:42:28 +00:00
|
|
|
'timeout' => 86400,
|
|
|
|
'ini' => array(
|
|
|
|
'session.referer_check' => 'example.com',
|
|
|
|
'session.use_trans_sid' => false
|
|
|
|
)
|
|
|
|
));
|
|
|
|
TestCakeSession::start();
|
|
|
|
$this->assertEquals('', ini_get('session.use_trans_sid'), 'Ini value is incorrect');
|
|
|
|
$this->assertEquals('example.com', ini_get('session.referer_check'), 'Ini value is incorrect');
|
2010-09-20 02:58:30 +00:00
|
|
|
$this->assertEquals('test', ini_get('session.name'), 'Ini value is incorrect');
|
2010-07-25 04:42:28 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2009-06-01 16:52:05 +00:00
|
|
|
/**
|
|
|
|
* testSessionPath
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testSessionPath() {
|
2010-07-05 07:01:22 +00:00
|
|
|
TestCakeSession::init('/index.php');
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals('/', TestCakeSession::$path);
|
2009-06-03 16:22:00 +00:00
|
|
|
|
2010-07-05 07:01:22 +00:00
|
|
|
TestCakeSession::init('/sub_dir/index.php');
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals('/sub_dir/', TestCakeSession::$path);
|
2010-07-22 08:42:56 +00:00
|
|
|
}
|
2009-06-03 16:22:00 +00:00
|
|
|
|
2010-07-22 08:42:56 +00:00
|
|
|
/**
|
|
|
|
* testCakeSessionPathEmpty
|
|
|
|
*
|
|
|
|
* @return void
|
2011-04-17 10:35:21 +00:00
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testCakeSessionPathEmpty() {
|
2010-07-05 07:01:22 +00:00
|
|
|
TestCakeSession::init('');
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals('/', TestCakeSession::$path, 'Session path is empty, with "" as $base needs to be /');
|
2009-06-01 16:52:05 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2010-07-22 08:42:56 +00:00
|
|
|
/**
|
|
|
|
* testCakeSessionPathContainsParams
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testCakeSessionPathContainsQuestion() {
|
2010-07-22 08:42:56 +00:00
|
|
|
TestCakeSession::init('/index.php?');
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals('/', TestCakeSession::$path);
|
2010-07-22 08:42:56 +00:00
|
|
|
}
|
|
|
|
|
2010-07-22 09:09:48 +00:00
|
|
|
/**
|
|
|
|
* testSetHost
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testSetHost() {
|
2010-07-22 09:09:48 +00:00
|
|
|
TestCakeSession::init();
|
|
|
|
TestCakeSession::setHost('cakephp.org');
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals('cakephp.org', TestCakeSession::$host);
|
2010-07-22 09:09:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* testSetHostWithPort
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testSetHostWithPort() {
|
2010-07-22 09:09:48 +00:00
|
|
|
TestCakeSession::init();
|
|
|
|
TestCakeSession::setHost('cakephp.org:443');
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals('cakephp.org', TestCakeSession::$host);
|
2010-07-22 09:09:48 +00:00
|
|
|
}
|
|
|
|
|
2010-07-25 23:55:02 +00:00
|
|
|
/**
|
|
|
|
* test valid with bogus user agent.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testValidBogusUserAgent() {
|
2010-07-25 23:55:02 +00:00
|
|
|
Configure::write('Session.checkAgent', true);
|
|
|
|
TestCakeSession::start();
|
|
|
|
$this->assertTrue(TestCakeSession::valid(), 'Newly started session should be valid');
|
|
|
|
|
|
|
|
TestCakeSession::userAgent('bogus!');
|
|
|
|
$this->assertFalse(TestCakeSession::valid(), 'user agent mismatch should fail.');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* test valid with bogus user agent.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testValidTimeExpiry() {
|
2010-07-25 23:55:02 +00:00
|
|
|
Configure::write('Session.checkAgent', true);
|
|
|
|
TestCakeSession::start();
|
|
|
|
$this->assertTrue(TestCakeSession::valid(), 'Newly started session should be valid');
|
|
|
|
|
|
|
|
TestCakeSession::$time = strtotime('next year');
|
|
|
|
$this->assertFalse(TestCakeSession::valid(), 'time should cause failure.');
|
|
|
|
}
|
|
|
|
|
2008-06-02 19:22:55 +00:00
|
|
|
/**
|
|
|
|
* testCheck method
|
2008-06-05 15:20:45 +00:00
|
|
|
*
|
2008-06-02 19:22:55 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testCheck() {
|
2010-07-05 07:01:22 +00:00
|
|
|
TestCakeSession::write('SessionTestCase', 'value');
|
|
|
|
$this->assertTrue(TestCakeSession::check('SessionTestCase'));
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2012-09-03 23:04:48 +00:00
|
|
|
$this->assertFalse(TestCakeSession::check('NotExistingSessionTestCase'));
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-06-05 15:20:45 +00:00
|
|
|
/**
|
2008-06-02 19:22:55 +00:00
|
|
|
* testSimpleRead method
|
2008-06-05 15:20:45 +00:00
|
|
|
*
|
2008-06-02 19:22:55 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testSimpleRead() {
|
2010-07-05 07:01:22 +00:00
|
|
|
TestCakeSession::write('testing', '1,2,3');
|
|
|
|
$result = TestCakeSession::read('testing');
|
2010-12-11 03:03:53 +00:00
|
|
|
$this->assertEquals('1,2,3', $result);
|
2008-06-05 15:20:45 +00:00
|
|
|
|
2013-06-09 15:39:48 +00:00
|
|
|
TestCakeSession::write('testing', array('1' => 'one', '2' => 'two', '3' => 'three'));
|
2010-07-05 07:01:22 +00:00
|
|
|
$result = TestCakeSession::read('testing.1');
|
2010-12-11 03:03:53 +00:00
|
|
|
$this->assertEquals('one', $result);
|
2008-06-05 15:20:45 +00:00
|
|
|
|
2010-07-05 07:01:22 +00:00
|
|
|
$result = TestCakeSession::read('testing');
|
2010-12-11 03:03:53 +00:00
|
|
|
$this->assertEquals(array('1' => 'one', '2' => 'two', '3' => 'three'), $result);
|
2008-06-05 15:20:45 +00:00
|
|
|
|
2010-07-05 07:01:22 +00:00
|
|
|
$result = TestCakeSession::read();
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->assertTrue(isset($result['testing']));
|
|
|
|
$this->assertTrue(isset($result['Config']));
|
|
|
|
$this->assertTrue(isset($result['Config']['userAgent']));
|
2008-11-08 02:58:37 +00:00
|
|
|
|
2010-07-05 07:01:22 +00:00
|
|
|
TestCakeSession::write('This.is.a.deep.array.my.friend', 'value');
|
|
|
|
$result = TestCakeSession::read('This.is.a.deep.array.my.friend');
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals('value', $result);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2010-07-22 09:19:53 +00:00
|
|
|
/**
|
|
|
|
* testReadyEmpty
|
|
|
|
*
|
2010-07-22 09:24:11 +00:00
|
|
|
* @return void
|
2010-07-22 09:19:53 +00:00
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testReadyEmpty() {
|
2014-10-09 13:49:53 +00:00
|
|
|
$this->assertNull(TestCakeSession::read(''));
|
2010-07-22 09:19:53 +00:00
|
|
|
}
|
|
|
|
|
2010-07-16 03:54:01 +00:00
|
|
|
/**
|
|
|
|
* test writing a hash of values/
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testWriteArray() {
|
2010-07-16 03:54:01 +00:00
|
|
|
$result = TestCakeSession::write(array(
|
|
|
|
'one' => 1,
|
|
|
|
'two' => 2,
|
|
|
|
'three' => array('something'),
|
|
|
|
'null' => null
|
|
|
|
));
|
|
|
|
$this->assertTrue($result);
|
|
|
|
$this->assertEquals(1, TestCakeSession::read('one'));
|
|
|
|
$this->assertEquals(array('something'), TestCakeSession::read('three'));
|
|
|
|
$this->assertEquals(null, TestCakeSession::read('null'));
|
|
|
|
}
|
|
|
|
|
2010-07-22 09:24:11 +00:00
|
|
|
/**
|
|
|
|
* testWriteEmptyKey
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testWriteEmptyKey() {
|
2016-10-21 14:54:39 +00:00
|
|
|
$this->assertTrue(TestCakeSession::write('', 'graham'));
|
|
|
|
$this->assertTrue(TestCakeSession::write('', ''));
|
|
|
|
$this->assertTrue(TestCakeSession::write(''));
|
2010-07-22 09:24:11 +00:00
|
|
|
}
|
|
|
|
|
2012-03-27 01:50:53 +00:00
|
|
|
/**
|
|
|
|
* Test overwriting a string value as if it were an array.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testWriteOverwriteStringValue() {
|
|
|
|
TestCakeSession::write('Some.string', 'value');
|
|
|
|
$this->assertEquals('value', TestCakeSession::read('Some.string'));
|
|
|
|
|
|
|
|
TestCakeSession::write('Some.string.array', array('values'));
|
|
|
|
$this->assertEquals(
|
|
|
|
array('values'),
|
|
|
|
TestCakeSession::read('Some.string.array')
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2014-12-23 02:39:05 +00:00
|
|
|
/**
|
|
|
|
* Test consuming session data.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testConsume() {
|
|
|
|
TestCakeSession::write('Some.string', 'value');
|
|
|
|
TestCakeSession::write('Some.array', array('key1' => 'value1', 'key2' => 'value2'));
|
|
|
|
$this->assertEquals('value', TestCakeSession::read('Some.string'));
|
|
|
|
$value = TestCakeSession::consume('Some.string');
|
|
|
|
$this->assertEquals('value', $value);
|
|
|
|
$this->assertFalse(TestCakeSession::check('Some.string'));
|
|
|
|
$value = TestCakeSession::consume('');
|
|
|
|
$this->assertNull($value);
|
|
|
|
$value = TestCakeSession::consume(null);
|
|
|
|
$this->assertNull($value);
|
|
|
|
$value = TestCakeSession::consume('Some.array');
|
|
|
|
$expected = array('key1' => 'value1', 'key2' => 'value2');
|
|
|
|
$this->assertEquals($expected, $value);
|
|
|
|
$this->assertFalse(TestCakeSession::check('Some.array'));
|
|
|
|
}
|
|
|
|
|
2008-06-05 15:20:45 +00:00
|
|
|
/**
|
2008-06-02 19:22:55 +00:00
|
|
|
* testId method
|
2008-06-05 15:20:45 +00:00
|
|
|
*
|
2008-06-02 19:22:55 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testId() {
|
2010-12-11 03:00:44 +00:00
|
|
|
TestCakeSession::destroy();
|
2011-04-17 10:35:21 +00:00
|
|
|
|
2010-07-05 07:01:22 +00:00
|
|
|
$result = TestCakeSession::id();
|
2010-12-10 04:55:38 +00:00
|
|
|
$expected = session_id();
|
2010-12-11 03:00:44 +00:00
|
|
|
$this->assertEquals($expected, $result);
|
2008-06-05 15:20:45 +00:00
|
|
|
|
2010-07-05 07:01:22 +00:00
|
|
|
TestCakeSession::id('MySessionId');
|
|
|
|
$result = TestCakeSession::id();
|
2010-12-11 03:00:44 +00:00
|
|
|
$this->assertEquals('MySessionId', $result);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-06-05 15:20:45 +00:00
|
|
|
/**
|
2008-06-02 19:22:55 +00:00
|
|
|
* testStarted method
|
2008-06-05 15:20:45 +00:00
|
|
|
*
|
2008-06-02 19:22:55 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testStarted() {
|
2008-05-30 11:40:08 +00:00
|
|
|
unset($_SESSION);
|
2010-01-15 03:47:49 +00:00
|
|
|
$_SESSION = null;
|
2010-12-10 04:55:38 +00:00
|
|
|
|
2010-07-05 07:01:22 +00:00
|
|
|
$this->assertFalse(TestCakeSession::started());
|
|
|
|
$this->assertTrue(TestCakeSession::start());
|
2010-07-25 23:55:02 +00:00
|
|
|
$this->assertTrue(TestCakeSession::started());
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-06-05 15:20:45 +00:00
|
|
|
/**
|
2008-06-02 19:22:55 +00:00
|
|
|
* testDel method
|
2008-06-05 15:20:45 +00:00
|
|
|
*
|
2008-06-02 19:22:55 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testDelete() {
|
2010-07-05 07:01:22 +00:00
|
|
|
$this->assertTrue(TestCakeSession::write('Delete.me', 'Clearing out'));
|
|
|
|
$this->assertTrue(TestCakeSession::delete('Delete.me'));
|
|
|
|
$this->assertFalse(TestCakeSession::check('Delete.me'));
|
|
|
|
$this->assertTrue(TestCakeSession::check('Delete'));
|
|
|
|
|
|
|
|
$this->assertTrue(TestCakeSession::write('Clearing.sale', 'everything must go'));
|
2015-01-04 00:40:09 +00:00
|
|
|
$this->assertFalse(TestCakeSession::delete(''));
|
|
|
|
$this->assertTrue(TestCakeSession::check('Clearing.sale'));
|
|
|
|
$this->assertFalse(TestCakeSession::delete(null));
|
|
|
|
$this->assertTrue(TestCakeSession::check('Clearing.sale'));
|
|
|
|
|
2010-07-05 07:01:22 +00:00
|
|
|
$this->assertTrue(TestCakeSession::delete('Clearing'));
|
|
|
|
$this->assertFalse(TestCakeSession::check('Clearing.sale'));
|
|
|
|
$this->assertFalse(TestCakeSession::check('Clearing'));
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2016-10-21 14:54:39 +00:00
|
|
|
/**
|
|
|
|
* test delete
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testDeleteEmptyString() {
|
|
|
|
TestCakeSession::write('', 'empty string');
|
|
|
|
$this->assertTrue(TestCakeSession::delete(''));
|
|
|
|
$this->assertFalse(TestCakeSession::check(''));
|
|
|
|
}
|
|
|
|
|
2015-01-04 01:22:37 +00:00
|
|
|
/**
|
|
|
|
* testClear method
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2015-01-04 00:40:09 +00:00
|
|
|
public function testClear() {
|
|
|
|
$this->assertTrue(TestCakeSession::write('Delete.me', 'Clearing out'));
|
2015-01-04 09:52:18 +00:00
|
|
|
TestCakeSession::clear(false);
|
2015-01-04 00:40:09 +00:00
|
|
|
$this->assertFalse(TestCakeSession::check('Delete.me'));
|
|
|
|
$this->assertFalse(TestCakeSession::check('Delete'));
|
2015-01-28 22:54:32 +00:00
|
|
|
|
|
|
|
TestCakeSession::write('Some.string', 'value');
|
|
|
|
TestCakeSession::clear(false);
|
|
|
|
$this->assertNull(TestCakeSession::read('Some'));
|
|
|
|
|
|
|
|
TestCakeSession::write('Some.string.array', array('values'));
|
|
|
|
TestCakeSession::clear(false);
|
|
|
|
$this->assertFalse(TestCakeSession::read());
|
2015-01-04 00:40:09 +00:00
|
|
|
}
|
|
|
|
|
2008-06-05 15:20:45 +00:00
|
|
|
/**
|
2008-06-02 19:22:55 +00:00
|
|
|
* testDestroy method
|
2008-06-05 15:20:45 +00:00
|
|
|
*
|
2008-06-02 19:22:55 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testDestroy() {
|
2012-02-23 23:29:53 +00:00
|
|
|
TestCakeSession::write('bulletProof', 'invincible');
|
2010-07-05 07:01:22 +00:00
|
|
|
$id = TestCakeSession::id();
|
|
|
|
TestCakeSession::destroy();
|
2010-07-08 03:33:38 +00:00
|
|
|
|
2010-07-05 07:01:22 +00:00
|
|
|
$this->assertFalse(TestCakeSession::check('bulletProof'));
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertNotEquals(TestCakeSession::id(), $id);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-06-05 15:20:45 +00:00
|
|
|
/**
|
2008-06-02 19:22:55 +00:00
|
|
|
* testCheckingSavedEmpty method
|
2008-06-05 15:20:45 +00:00
|
|
|
*
|
2008-06-02 19:22:55 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testCheckingSavedEmpty() {
|
2010-07-05 07:01:22 +00:00
|
|
|
$this->assertTrue(TestCakeSession::write('SessionTestCase', 0));
|
|
|
|
$this->assertTrue(TestCakeSession::check('SessionTestCase'));
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2010-07-05 07:01:22 +00:00
|
|
|
$this->assertTrue(TestCakeSession::write('SessionTestCase', '0'));
|
|
|
|
$this->assertTrue(TestCakeSession::check('SessionTestCase'));
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2010-07-05 07:01:22 +00:00
|
|
|
$this->assertTrue(TestCakeSession::write('SessionTestCase', false));
|
|
|
|
$this->assertTrue(TestCakeSession::check('SessionTestCase'));
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2010-07-05 07:01:22 +00:00
|
|
|
$this->assertTrue(TestCakeSession::write('SessionTestCase', null));
|
|
|
|
$this->assertFalse(TestCakeSession::check('SessionTestCase'));
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-06-02 19:22:55 +00:00
|
|
|
/**
|
|
|
|
* testCheckKeyWithSpaces method
|
2008-06-05 15:20:45 +00:00
|
|
|
*
|
2008-06-02 19:22:55 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testCheckKeyWithSpaces() {
|
2010-07-05 07:01:22 +00:00
|
|
|
$this->assertTrue(TestCakeSession::write('Session Test', "test"));
|
2011-12-04 20:43:27 +00:00
|
|
|
$this->assertTrue(TestCakeSession::check('Session Test'));
|
2010-07-05 07:01:22 +00:00
|
|
|
TestCakeSession::delete('Session Test');
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2010-07-05 07:01:22 +00:00
|
|
|
$this->assertTrue(TestCakeSession::write('Session Test.Test Case', "test"));
|
|
|
|
$this->assertTrue(TestCakeSession::check('Session Test.Test Case'));
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2010-07-22 09:11:53 +00:00
|
|
|
/**
|
|
|
|
* testCheckEmpty
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testCheckEmpty() {
|
2015-01-04 00:40:09 +00:00
|
|
|
$this->assertFalse(TestCakeSession::check(''));
|
|
|
|
$this->assertFalse(TestCakeSession::check(null));
|
2010-07-22 09:11:53 +00:00
|
|
|
}
|
|
|
|
|
2009-11-22 01:44:35 +00:00
|
|
|
/**
|
|
|
|
* test key exploitation
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testKeyExploit() {
|
2009-11-22 01:44:35 +00:00
|
|
|
$key = "a'] = 1; phpinfo(); \$_SESSION['a";
|
2010-07-05 07:01:22 +00:00
|
|
|
$result = TestCakeSession::write($key, 'haxored');
|
2013-08-23 10:27:06 +00:00
|
|
|
$this->assertFalse($result);
|
2009-11-22 01:44:35 +00:00
|
|
|
|
2010-07-05 07:01:22 +00:00
|
|
|
$result = TestCakeSession::read($key);
|
2013-08-23 10:27:06 +00:00
|
|
|
$this->assertNull($result);
|
2009-11-22 01:44:35 +00:00
|
|
|
}
|
|
|
|
|
2008-06-02 19:22:55 +00:00
|
|
|
/**
|
|
|
|
* testReadingSavedEmpty method
|
2008-06-05 15:20:45 +00:00
|
|
|
*
|
2008-06-02 19:22:55 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testReadingSavedEmpty() {
|
2016-10-21 14:54:39 +00:00
|
|
|
TestCakeSession::write('', 'empty string');
|
|
|
|
$this->assertTrue(TestCakeSession::check(''));
|
|
|
|
$this->assertEquals('empty string', TestCakeSession::read(''));
|
|
|
|
|
2010-07-05 07:01:22 +00:00
|
|
|
TestCakeSession::write('SessionTestCase', 0);
|
2010-12-11 03:03:53 +00:00
|
|
|
$this->assertEquals(0, TestCakeSession::read('SessionTestCase'));
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2010-07-05 07:01:22 +00:00
|
|
|
TestCakeSession::write('SessionTestCase', '0');
|
2010-12-11 03:03:53 +00:00
|
|
|
$this->assertEquals('0', TestCakeSession::read('SessionTestCase'));
|
2010-07-05 07:01:22 +00:00
|
|
|
$this->assertFalse(TestCakeSession::read('SessionTestCase') === 0);
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2010-07-05 07:01:22 +00:00
|
|
|
TestCakeSession::write('SessionTestCase', false);
|
|
|
|
$this->assertFalse(TestCakeSession::read('SessionTestCase'));
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2010-07-05 07:01:22 +00:00
|
|
|
TestCakeSession::write('SessionTestCase', null);
|
2016-10-21 14:54:39 +00:00
|
|
|
$this->assertNull(TestCakeSession::read('SessionTestCase'));
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2016-01-19 03:17:30 +00:00
|
|
|
/**
|
|
|
|
* Test te cacheLimiter settings.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testCacheLimiter() {
|
|
|
|
Configure::write('Session.cacheLimiter', 'public');
|
|
|
|
TestCakeSession::start();
|
|
|
|
$this->assertSame('public', session_cache_limiter());
|
|
|
|
|
|
|
|
Configure::write('Session.cacheLimiter', 'private');
|
|
|
|
TestCakeSession::destroy();
|
|
|
|
TestCakeSession::start();
|
|
|
|
$this->assertSame('private', session_cache_limiter());
|
|
|
|
}
|
|
|
|
|
2008-06-02 23:57:30 +00:00
|
|
|
/**
|
|
|
|
* testCheckUserAgentFalse method
|
2008-06-05 15:20:45 +00:00
|
|
|
*
|
2008-06-02 23:57:30 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testCheckUserAgentFalse() {
|
2008-05-30 11:40:08 +00:00
|
|
|
Configure::write('Session.checkAgent', false);
|
2010-07-05 07:01:22 +00:00
|
|
|
TestCakeSession::setUserAgent(md5('http://randomdomainname.com' . Configure::read('Security.salt')));
|
|
|
|
$this->assertTrue(TestCakeSession::valid());
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-06-02 23:57:30 +00:00
|
|
|
/**
|
|
|
|
* testCheckUserAgentTrue method
|
2008-06-05 15:20:45 +00:00
|
|
|
*
|
2008-06-02 23:57:30 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testCheckUserAgentTrue() {
|
2008-05-30 11:40:08 +00:00
|
|
|
Configure::write('Session.checkAgent', true);
|
2010-07-08 03:33:38 +00:00
|
|
|
TestCakeSession::$error = false;
|
|
|
|
$agent = md5('http://randomdomainname.com' . Configure::read('Security.salt'));
|
|
|
|
|
|
|
|
TestCakeSession::write('Config.userAgent', md5('Hacking you!'));
|
|
|
|
TestCakeSession::setUserAgent($agent);
|
2010-07-05 07:01:22 +00:00
|
|
|
$this->assertFalse(TestCakeSession::valid());
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-06-02 23:57:30 +00:00
|
|
|
/**
|
2012-03-30 19:10:44 +00:00
|
|
|
* testReadAndWriteWithCakeStorage method
|
2008-06-05 15:20:45 +00:00
|
|
|
*
|
2008-06-02 23:57:30 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testReadAndWriteWithCakeStorage() {
|
2010-07-25 05:14:41 +00:00
|
|
|
Configure::write('Session.defaults', 'cake');
|
|
|
|
|
|
|
|
TestCakeSession::init();
|
2010-07-27 02:38:40 +00:00
|
|
|
TestCakeSession::start();
|
2008-06-10 22:38:05 +00:00
|
|
|
|
2010-07-05 07:01:22 +00:00
|
|
|
TestCakeSession::write('SessionTestCase', 0);
|
2010-12-11 03:03:53 +00:00
|
|
|
$this->assertEquals(0, TestCakeSession::read('SessionTestCase'));
|
2008-06-10 22:38:05 +00:00
|
|
|
|
2010-07-05 07:01:22 +00:00
|
|
|
TestCakeSession::write('SessionTestCase', '0');
|
2010-12-11 03:03:53 +00:00
|
|
|
$this->assertEquals('0', TestCakeSession::read('SessionTestCase'));
|
2010-07-05 07:01:22 +00:00
|
|
|
$this->assertFalse(TestCakeSession::read('SessionTestCase') === 0);
|
2008-06-10 22:38:05 +00:00
|
|
|
|
2010-07-05 07:01:22 +00:00
|
|
|
TestCakeSession::write('SessionTestCase', false);
|
|
|
|
$this->assertFalse(TestCakeSession::read('SessionTestCase'));
|
2008-06-10 22:38:05 +00:00
|
|
|
|
2010-07-05 07:01:22 +00:00
|
|
|
TestCakeSession::write('SessionTestCase', null);
|
2010-12-11 03:03:53 +00:00
|
|
|
$this->assertEquals(null, TestCakeSession::read('SessionTestCase'));
|
2008-06-10 22:38:05 +00:00
|
|
|
|
2010-07-05 07:01:22 +00:00
|
|
|
TestCakeSession::write('SessionTestCase', 'This is a Test');
|
2010-12-11 03:03:53 +00:00
|
|
|
$this->assertEquals('This is a Test', TestCakeSession::read('SessionTestCase'));
|
2008-06-10 22:38:05 +00:00
|
|
|
|
2010-07-05 07:01:22 +00:00
|
|
|
TestCakeSession::write('SessionTestCase', 'This is a Test');
|
|
|
|
TestCakeSession::write('SessionTestCase', 'This was updated');
|
2010-12-11 03:03:53 +00:00
|
|
|
$this->assertEquals('This was updated', TestCakeSession::read('SessionTestCase'));
|
2008-06-10 22:38:05 +00:00
|
|
|
|
2010-07-05 07:01:22 +00:00
|
|
|
TestCakeSession::destroy();
|
2010-07-16 03:48:02 +00:00
|
|
|
$this->assertNull(TestCakeSession::read('SessionTestCase'));
|
2008-06-10 22:38:05 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2010-07-25 23:09:29 +00:00
|
|
|
/**
|
2011-06-20 00:28:40 +00:00
|
|
|
* test using a handler from app/Model/Datasource/Session.
|
2010-07-25 23:09:29 +00:00
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testUsingAppLibsHandler() {
|
2010-07-25 23:09:29 +00:00
|
|
|
App::build(array(
|
2011-04-10 19:32:19 +00:00
|
|
|
'Model/Datasource/Session' => array(
|
2011-04-17 10:35:21 +00:00
|
|
|
CAKE . 'Test' . DS . 'test_app' . DS . 'Model' . DS . 'Datasource' . DS . 'Session' . DS
|
2011-04-10 19:32:19 +00:00
|
|
|
),
|
2012-02-18 12:31:29 +00:00
|
|
|
'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
|
2012-02-18 12:04:54 +00:00
|
|
|
), App::RESET);
|
2010-07-25 23:09:29 +00:00
|
|
|
Configure::write('Session', array(
|
|
|
|
'defaults' => 'cake',
|
|
|
|
'handler' => array(
|
|
|
|
'engine' => 'TestAppLibSession'
|
|
|
|
)
|
|
|
|
));
|
2013-12-07 21:25:33 +00:00
|
|
|
|
|
|
|
TestCakeSession::start();
|
2010-07-25 23:09:29 +00:00
|
|
|
$this->assertTrue(TestCakeSession::started());
|
|
|
|
|
2013-12-07 21:25:33 +00:00
|
|
|
TestCakeSession::destroy();
|
|
|
|
$this->assertFalse(TestCakeSession::started());
|
|
|
|
|
2010-07-25 23:09:29 +00:00
|
|
|
App::build();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* test using a handler from a plugin.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testUsingPluginHandler() {
|
2010-07-25 23:09:29 +00:00
|
|
|
App::build(array(
|
2012-02-18 12:31:29 +00:00
|
|
|
'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
|
2012-02-18 12:04:54 +00:00
|
|
|
), App::RESET);
|
2011-12-04 20:43:27 +00:00
|
|
|
CakePlugin::load('TestPlugin');
|
2011-04-17 10:35:21 +00:00
|
|
|
|
2010-07-25 23:09:29 +00:00
|
|
|
Configure::write('Session', array(
|
|
|
|
'defaults' => 'cake',
|
|
|
|
'handler' => array(
|
|
|
|
'engine' => 'TestPlugin.TestPluginSession'
|
|
|
|
)
|
|
|
|
));
|
2011-04-17 10:35:21 +00:00
|
|
|
|
2013-12-07 21:25:33 +00:00
|
|
|
TestCakeSession::start();
|
2010-07-25 23:09:29 +00:00
|
|
|
$this->assertTrue(TestCakeSession::started());
|
|
|
|
|
2013-12-07 21:25:33 +00:00
|
|
|
TestCakeSession::destroy();
|
|
|
|
$this->assertFalse(TestCakeSession::started());
|
|
|
|
|
2010-07-25 23:09:29 +00:00
|
|
|
App::build();
|
|
|
|
}
|
|
|
|
|
2008-06-10 22:38:05 +00:00
|
|
|
/**
|
2012-03-30 19:10:44 +00:00
|
|
|
* testReadAndWriteWithCacheStorage method
|
2008-06-10 22:38:05 +00:00
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testReadAndWriteWithCacheStorage() {
|
2010-07-25 05:14:41 +00:00
|
|
|
Configure::write('Session.defaults', 'cache');
|
2012-03-24 20:32:31 +00:00
|
|
|
Configure::write('Session.handler.engine', 'TestCacheSession');
|
2010-07-25 05:14:41 +00:00
|
|
|
|
|
|
|
TestCakeSession::init();
|
|
|
|
TestCakeSession::destroy();
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2010-07-05 07:01:22 +00:00
|
|
|
TestCakeSession::write('SessionTestCase', 0);
|
2010-12-11 03:03:53 +00:00
|
|
|
$this->assertEquals(0, TestCakeSession::read('SessionTestCase'));
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2010-07-05 07:01:22 +00:00
|
|
|
TestCakeSession::write('SessionTestCase', '0');
|
2010-12-11 03:03:53 +00:00
|
|
|
$this->assertEquals('0', TestCakeSession::read('SessionTestCase'));
|
2010-07-05 07:01:22 +00:00
|
|
|
$this->assertFalse(TestCakeSession::read('SessionTestCase') === 0);
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2010-07-05 07:01:22 +00:00
|
|
|
TestCakeSession::write('SessionTestCase', false);
|
|
|
|
$this->assertFalse(TestCakeSession::read('SessionTestCase'));
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2010-07-05 07:01:22 +00:00
|
|
|
TestCakeSession::write('SessionTestCase', null);
|
2010-12-11 03:03:53 +00:00
|
|
|
$this->assertEquals(null, TestCakeSession::read('SessionTestCase'));
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2010-07-05 07:01:22 +00:00
|
|
|
TestCakeSession::write('SessionTestCase', 'This is a Test');
|
2010-12-11 03:03:53 +00:00
|
|
|
$this->assertEquals('This is a Test', TestCakeSession::read('SessionTestCase'));
|
2008-06-10 22:38:05 +00:00
|
|
|
|
2010-07-05 07:01:22 +00:00
|
|
|
TestCakeSession::write('SessionTestCase', 'This is a Test');
|
|
|
|
TestCakeSession::write('SessionTestCase', 'This was updated');
|
2010-12-11 03:03:53 +00:00
|
|
|
$this->assertEquals('This was updated', TestCakeSession::read('SessionTestCase'));
|
2008-06-10 22:38:05 +00:00
|
|
|
|
2010-07-05 07:01:22 +00:00
|
|
|
TestCakeSession::destroy();
|
2010-07-16 03:48:02 +00:00
|
|
|
$this->assertNull(TestCakeSession::read('SessionTestCase'));
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2010-07-25 17:37:56 +00:00
|
|
|
/**
|
|
|
|
* test that changing the config name of the cache config works.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testReadAndWriteWithCustomCacheConfig() {
|
2010-07-25 17:37:56 +00:00
|
|
|
Configure::write('Session.defaults', 'cache');
|
2012-03-24 20:32:31 +00:00
|
|
|
Configure::write('Session.handler.engine', 'TestCacheSession');
|
2010-07-25 17:37:56 +00:00
|
|
|
Configure::write('Session.handler.config', 'session_test');
|
2011-04-17 10:35:21 +00:00
|
|
|
|
2010-07-25 17:37:56 +00:00
|
|
|
Cache::config('session_test', array(
|
|
|
|
'engine' => 'File',
|
|
|
|
'prefix' => 'session_test_',
|
|
|
|
));
|
|
|
|
|
|
|
|
TestCakeSession::init();
|
2010-07-27 02:38:40 +00:00
|
|
|
TestCakeSession::start();
|
2010-07-25 17:37:56 +00:00
|
|
|
|
|
|
|
TestCakeSession::write('SessionTestCase', 'Some value');
|
|
|
|
$this->assertEquals('Some value', TestCakeSession::read('SessionTestCase'));
|
2010-07-27 02:38:40 +00:00
|
|
|
$id = TestCakeSession::id();
|
2010-07-25 17:37:56 +00:00
|
|
|
|
2010-07-27 02:38:40 +00:00
|
|
|
Cache::delete($id, 'session_test');
|
2010-07-25 17:37:56 +00:00
|
|
|
}
|
|
|
|
|
2008-06-05 15:20:45 +00:00
|
|
|
/**
|
2008-06-10 22:38:05 +00:00
|
|
|
* testReadAndWriteWithDatabaseStorage method
|
|
|
|
*
|
2008-06-05 15:20:45 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testReadAndWriteWithDatabaseStorage() {
|
2010-07-25 05:14:41 +00:00
|
|
|
Configure::write('Session.defaults', 'database');
|
2012-03-24 20:32:31 +00:00
|
|
|
Configure::write('Session.handler.engine', 'TestDatabaseSession');
|
2010-07-25 15:42:05 +00:00
|
|
|
Configure::write('Session.handler.table', 'sessions');
|
|
|
|
Configure::write('Session.handler.model', 'Session');
|
2010-09-20 02:58:30 +00:00
|
|
|
Configure::write('Session.handler.database', 'test');
|
2010-07-25 05:14:41 +00:00
|
|
|
|
|
|
|
TestCakeSession::init();
|
2012-03-30 19:10:44 +00:00
|
|
|
$this->assertNull(TestCakeSession::id());
|
|
|
|
|
2010-07-27 02:38:40 +00:00
|
|
|
TestCakeSession::start();
|
2012-03-30 19:10:44 +00:00
|
|
|
$expected = session_id();
|
|
|
|
$this->assertEquals($expected, TestCakeSession::id());
|
|
|
|
|
|
|
|
TestCakeSession::renew();
|
2014-04-29 12:19:33 +00:00
|
|
|
$this->assertFalse($expected === TestCakeSession::id());
|
2012-03-30 19:10:44 +00:00
|
|
|
|
|
|
|
$expected = session_id();
|
|
|
|
$this->assertEquals($expected, TestCakeSession::id());
|
2008-06-10 22:38:05 +00:00
|
|
|
|
2010-07-05 07:01:22 +00:00
|
|
|
TestCakeSession::write('SessionTestCase', 0);
|
2010-12-11 03:03:53 +00:00
|
|
|
$this->assertEquals(0, TestCakeSession::read('SessionTestCase'));
|
2008-06-10 22:38:05 +00:00
|
|
|
|
2010-07-05 07:01:22 +00:00
|
|
|
TestCakeSession::write('SessionTestCase', '0');
|
2010-12-11 03:03:53 +00:00
|
|
|
$this->assertEquals('0', TestCakeSession::read('SessionTestCase'));
|
2010-07-05 07:01:22 +00:00
|
|
|
$this->assertFalse(TestCakeSession::read('SessionTestCase') === 0);
|
2008-06-10 22:38:05 +00:00
|
|
|
|
2010-07-05 07:01:22 +00:00
|
|
|
TestCakeSession::write('SessionTestCase', false);
|
|
|
|
$this->assertFalse(TestCakeSession::read('SessionTestCase'));
|
2008-06-10 22:38:05 +00:00
|
|
|
|
2010-07-05 07:01:22 +00:00
|
|
|
TestCakeSession::write('SessionTestCase', null);
|
2010-12-11 03:03:53 +00:00
|
|
|
$this->assertEquals(null, TestCakeSession::read('SessionTestCase'));
|
2008-06-10 22:38:05 +00:00
|
|
|
|
2010-07-05 07:01:22 +00:00
|
|
|
TestCakeSession::write('SessionTestCase', 'This is a Test');
|
2010-12-11 03:03:53 +00:00
|
|
|
$this->assertEquals('This is a Test', TestCakeSession::read('SessionTestCase'));
|
2008-06-10 22:38:05 +00:00
|
|
|
|
2010-07-27 02:38:40 +00:00
|
|
|
TestCakeSession::write('SessionTestCase', 'Some additional data');
|
2010-12-11 03:03:53 +00:00
|
|
|
$this->assertEquals('Some additional data', TestCakeSession::read('SessionTestCase'));
|
2009-04-13 19:47:40 +00:00
|
|
|
|
2010-07-05 07:01:22 +00:00
|
|
|
TestCakeSession::destroy();
|
2010-07-16 03:48:02 +00:00
|
|
|
$this->assertNull(TestCakeSession::read('SessionTestCase'));
|
2011-04-17 10:35:21 +00:00
|
|
|
|
2010-09-30 03:01:23 +00:00
|
|
|
Configure::write('Session', array(
|
|
|
|
'defaults' => 'php'
|
|
|
|
));
|
|
|
|
TestCakeSession::init();
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2010-01-21 16:59:49 +00:00
|
|
|
|
2010-07-25 21:50:08 +00:00
|
|
|
/**
|
|
|
|
* testSessionTimeout method
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testSessionTimeout() {
|
2010-07-25 21:50:08 +00:00
|
|
|
Configure::write('debug', 2);
|
2012-03-24 20:32:31 +00:00
|
|
|
Configure::write('Session.defaults', 'cake');
|
2010-07-27 02:42:32 +00:00
|
|
|
Configure::write('Session.autoRegenerate', false);
|
2010-07-25 21:50:08 +00:00
|
|
|
|
|
|
|
$timeoutSeconds = Configure::read('Session.timeout') * 60;
|
|
|
|
|
|
|
|
TestCakeSession::destroy();
|
|
|
|
TestCakeSession::write('Test', 'some value');
|
|
|
|
|
2012-04-18 00:51:52 +00:00
|
|
|
$this->assertWithinMargin(time() + $timeoutSeconds, CakeSession::$sessionTime, 1);
|
2010-12-11 03:03:53 +00:00
|
|
|
$this->assertEquals(10, $_SESSION['Config']['countdown']);
|
2012-04-18 00:51:52 +00:00
|
|
|
$this->assertWithinMargin(CakeSession::$sessionTime, $_SESSION['Config']['time'], 1);
|
|
|
|
$this->assertWithinMargin(time(), CakeSession::$time, 1);
|
|
|
|
$this->assertWithinMargin(time() + $timeoutSeconds, $_SESSION['Config']['time'], 1);
|
2010-07-25 21:50:08 +00:00
|
|
|
|
|
|
|
Configure::write('Session.harden', true);
|
|
|
|
TestCakeSession::destroy();
|
|
|
|
|
|
|
|
TestCakeSession::write('Test', 'some value');
|
2012-04-18 00:51:52 +00:00
|
|
|
$this->assertWithinMargin(time() + $timeoutSeconds, CakeSession::$sessionTime, 1);
|
2010-12-11 03:03:53 +00:00
|
|
|
$this->assertEquals(10, $_SESSION['Config']['countdown']);
|
2012-04-18 00:51:52 +00:00
|
|
|
$this->assertWithinMargin(CakeSession::$sessionTime, $_SESSION['Config']['time'], 1);
|
|
|
|
$this->assertWithinMargin(time(), CakeSession::$time, 1);
|
|
|
|
$this->assertWithinMargin(CakeSession::$time + $timeoutSeconds, $_SESSION['Config']['time'], 1);
|
2010-07-25 21:50:08 +00:00
|
|
|
}
|
|
|
|
|
2012-03-11 12:12:56 +00:00
|
|
|
/**
|
|
|
|
* Test that cookieTimeout matches timeout when unspecified.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testCookieTimeoutFallback() {
|
|
|
|
$_SESSION = null;
|
|
|
|
Configure::write('Session', array(
|
2012-03-24 20:32:31 +00:00
|
|
|
'defaults' => 'cake',
|
2012-03-11 12:12:56 +00:00
|
|
|
'timeout' => 400,
|
|
|
|
));
|
|
|
|
TestCakeSession::start();
|
|
|
|
$this->assertEquals(400, Configure::read('Session.cookieTimeout'));
|
|
|
|
$this->assertEquals(400, Configure::read('Session.timeout'));
|
2012-05-24 02:25:19 +00:00
|
|
|
$this->assertEquals(400 * 60, ini_get('session.cookie_lifetime'));
|
|
|
|
$this->assertEquals(400 * 60, ini_get('session.gc_maxlifetime'));
|
2012-03-11 12:12:56 +00:00
|
|
|
|
|
|
|
$_SESSION = null;
|
|
|
|
Configure::write('Session', array(
|
2012-03-24 20:32:31 +00:00
|
|
|
'defaults' => 'cake',
|
2012-03-11 12:12:56 +00:00
|
|
|
'timeout' => 400,
|
|
|
|
'cookieTimeout' => 600
|
|
|
|
));
|
|
|
|
TestCakeSession::start();
|
|
|
|
$this->assertEquals(600, Configure::read('Session.cookieTimeout'));
|
|
|
|
$this->assertEquals(400, Configure::read('Session.timeout'));
|
|
|
|
}
|
|
|
|
|
2013-12-07 21:25:33 +00:00
|
|
|
/**
|
|
|
|
* Proves that invalid sessions will be destroyed and re-created
|
|
|
|
* if invalid
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testInvalidSessionRenew() {
|
|
|
|
TestCakeSession::start();
|
|
|
|
$this->assertNotEmpty($_SESSION['Config']);
|
|
|
|
$data = $_SESSION;
|
|
|
|
|
|
|
|
session_write_close();
|
|
|
|
$_SESSION = null;
|
|
|
|
|
|
|
|
TestCakeSession::start();
|
|
|
|
$this->assertEquals($data, $_SESSION);
|
|
|
|
TestCakeSession::write('Foo', 'Bar');
|
|
|
|
|
|
|
|
session_write_close();
|
|
|
|
$_SESSION = null;
|
|
|
|
|
|
|
|
TestCakeSession::userAgent('bogus!');
|
|
|
|
TestCakeSession::start();
|
|
|
|
$this->assertNotEquals($data, $_SESSION);
|
|
|
|
$this->assertEquals('bogus!', $_SESSION['Config']['userAgent']);
|
|
|
|
}
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|