Skip to content
Permalink
Browse files

Added tests for check() starting the session if needed

  • Loading branch information...
Clemens Weiß
Clemens Weiß committed Jun 28, 2017
1 parent f209ef9 commit ea001bc9edb8415a937d4552da94156dab60a1ce
Showing with 103 additions and 4 deletions.
  1. +1 −1 src/Network/Session.php
  2. +102 −3 tests/TestCase/Network/SessionTest.php
@@ -371,7 +371,7 @@ public function check($name = null)
* Returns given session variable, or all of them, if no parameters given.
*
* @param string|null $name The name of the session variable (or a path as sent to Hash.extract)
* @return string|null The value of the session variable, null if session not available,
* @return string|array|null The value of the session variable, null if session not available,
* session not started, or provided name not found in the session.
*/
public function read($name = null)
@@ -43,6 +43,25 @@ protected function _writeSession()
}
}
/**
* Overwrite Session to simulate a web session even if the test runs on CLI.
*/
class TestWebSession extends Session
{
protected function _hasSession()
{
$isCLI = $this->_isCLI;
$this->_isCLI = false;
$result = parent::_hasSession();
$this->_isCLI = $isCLI;
return $result;
}
}
/**
* SessionTest class
*/
@@ -121,7 +140,7 @@ public function testSessionConfigIniSetting()
]
];
$session = Session::create($config);
Session::create($config);
$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');
$this->assertEquals('test', ini_get('session.name'), 'Ini value is incorrect');
@@ -136,10 +155,10 @@ public function testCookiePath()
{
ini_set('session.cookie_path', '/foo');
$session = new Session();
new Session();
$this->assertEquals('/', ini_get('session.cookie_path'));
$session = new Session(['cookiePath' => '/base']);
new Session(['cookiePath' => '/base']);
$this->assertEquals('/base', ini_get('session.cookie_path'));
}
@@ -569,4 +588,84 @@ public function testSessionName()
new Session(['cookie' => 'made_up_name']);
$this->assertEquals('made_up_name', session_name());
}
/**
* Test that a call of check() starts the session when cookies are disabled in php.ini
*/
public function testCheckStartsSessionWithCookiesDisabled()
{
$_COOKIE = [];
$_GET = [];
$session = new TestWebSession([
'ini' => [
'session.use_cookies' => 0,
'session.use_trans_sid' => 0,
]
]);
$this->assertFalse($session->started());
$session->check('something');
$this->assertTrue($session->started());
}
/**
* Test that a call of check() starts the session when a cookie is already set
*/
public function testCheckStartsSessionWithCookie()
{
$_COOKIE[session_name()] = '123abc';
$_GET = [];
$session = new TestWebSession([
'ini' => [
'session.use_cookies' => 1,
'session.use_trans_sid' => 0,
]
]);
$this->assertFalse($session->started());
$session->check('something');
$this->assertTrue($session->started());
}
/**
* Test that a call of check() starts the session when the session ID is passed via URL and session.use_trans_sid is enabled
*/
public function testCheckStartsSessionWithSIDinURL()
{
$_COOKIE = [];
$_GET[session_name()] = '123abc';
$session = new TestWebSession([
'ini' => [
'session.use_cookies' => 1,
'session.use_trans_sid' => 1,
]
]);
$this->assertFalse($session->started());
$session->check('something');
$this->assertTrue($session->started());
}
/**
* Test that a call of check() does not start the session when the session ID is passed via URL and session.use_trans_sid is disabled
*/
public function testCheckDoesntStartSessionWithoutTransSID()
{
$_COOKIE = [];
$_GET[session_name()] = '123abc';
$session = new TestWebSession([
'ini' => [
'session.use_cookies' => 1,
'session.use_trans_sid' => 0,
]
]);
$this->assertFalse($session->started());
$session->check('something');
$this->assertFalse($session->started());
}
}

0 comments on commit ea001bc

Please sign in to comment.
You can’t perform that action at this time.