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