Skip to content

Commit

Permalink
Updating and deprecating the SessionComponent
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed May 18, 2014
1 parent 497e367 commit 386f9d0
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 92 deletions.
73 changes: 32 additions & 41 deletions src/Controller/Component/SessionComponent.php
Expand Up @@ -17,26 +17,37 @@
namespace Cake\Controller\Component;

use Cake\Controller\Component;
use Cake\Network\Session;
use Cake\Controller\ComponentRegistry;

/**
* The CakePHP SessionComponent provides a way to persist client data between
* page requests. It acts as a wrapper for the `$_SESSION` as well as providing
* convenience methods for several `$_SESSION` related functions.
*
* This class is here for backwards compatibility with CakePHP 2.x
*
* @link http://book.cakephp.org/2.0/en/core-libraries/components/sessions.html
* @link http://book.cakephp.org/2.0/en/development/sessions.html
* @deprecated
*/
class SessionComponent extends Component {

/**
* Get / Set the userAgent
* The Session object instance
*
* @param string $userAgent Set the userAgent
* @return void
* @var Cake\Network\Session
*/
protected $_session;

/**
* Constructor. Parses the accepted content types accepted by the client using HTTP_ACCEPT
*
* @param ComponentRegistry $collection ComponentRegistry object.
* @param array $config Array of config.
*/
public function userAgent($userAgent = null) {
return Session::userAgent($userAgent);
public function __construct(ComponentRegistry $collection, array $config = array()) {
parent::__construct($collection, $config);
$this->_session = $collection->getController()->request->session();
}

/**
Expand All @@ -47,11 +58,11 @@ public function userAgent($userAgent = null) {
* @param string $name The name of the key your are setting in the session.
* This should be in a Controller.key format for better organizing
* @param string $value The value you want to store in a session.
* @return bool Success
* @return void
* @link http://book.cakephp.org/2.0/en/core-libraries/components/sessions.html#SessionComponent::write
*/
public function write($name, $value = null) {
return Session::write($name, $value);
$this->_session->write($name, $value);
}

/**
Expand All @@ -65,7 +76,7 @@ public function write($name, $value = null) {
* @link http://book.cakephp.org/2.0/en/core-libraries/components/sessions.html#SessionComponent::read
*/
public function read($name = null) {
return Session::read($name);
return $this->_session->read($name);
}

/**
Expand All @@ -74,11 +85,11 @@ public function read($name = null) {
* In your controller: $this->Session->delete('Controller.sessKey');
*
* @param string $name the name of the session key you want to delete
* @return bool true is session variable is set and can be deleted, false is variable was not set.
* @return void
* @link http://book.cakephp.org/2.0/en/core-libraries/components/sessions.html#SessionComponent::delete
*/
public function delete($name) {
return Session::delete($name);
$this->_session->delete($name);
}

/**
Expand All @@ -91,18 +102,7 @@ public function delete($name) {
* @link http://book.cakephp.org/2.0/en/core-libraries/components/sessions.html#SessionComponent::check
*/
public function check($name) {
return Session::check($name);
}

/**
* Used to determine the last error in a session.
*
* In your controller: $this->Session->error();
*
* @return string Last session error
*/
public function error() {
return Session::error();
return $this->_session->check($name);
}

/**
Expand All @@ -122,7 +122,7 @@ public function error() {
* @link http://book.cakephp.org/2.0/en/core-libraries/components/sessions.html#creating-notification-messages
*/
public function setFlash($message, $element = 'default', array $params = array(), $key = 'flash') {
Session::write('Message.' . $key, compact('message', 'element', 'params'));
$this->write('Message.' . $key, compact('message', 'element', 'params'));
}

/**
Expand All @@ -133,18 +133,7 @@ public function setFlash($message, $element = 'default', array $params = array()
* @return void
*/
public function renew() {
return Session::renew();
}

/**
* Used to check for a valid session.
*
* In your controller: $this->Session->valid();
*
* @return bool true is session is valid, false is session is invalid
*/
public function valid() {
return Session::valid();
$this->_session->renew();
}

/**
Expand All @@ -156,7 +145,7 @@ public function valid() {
* @link http://book.cakephp.org/2.0/en/core-libraries/components/sessions.html#SessionComponent::destroy
*/
public function destroy() {
return Session::destroy();
$this->_session->destroy();
}

/**
Expand All @@ -170,10 +159,12 @@ public function destroy() {
* @return string The current session id.
*/
public function id($id = null) {
if (empty($id)) {
Session::start();
if ($id === null) {
$session = $this->_session;
$session->start();
return $session->id();
}
return Session::id($id);
$this->_session->id($id);
}

/**
Expand All @@ -182,7 +173,7 @@ public function id($id = null) {
* @return bool
*/
public function started() {
return Session::started();
return $this->_session->started();
}

/**
Expand Down
63 changes: 12 additions & 51 deletions tests/TestCase/Controller/Component/SessionComponentTest.php
Expand Up @@ -18,6 +18,7 @@
use Cake\Controller\Component\SessionComponent;
use Cake\Controller\Controller;
use Cake\Core\Configure;
use Cake\Network\Request;
use Cake\Network\Session;
use Cake\Routing\DispatcherFactory;
use Cake\Routing\Router;
Expand All @@ -44,12 +45,6 @@ class SessionComponentTest extends TestCase {
* @return void
*/
public static function setupBeforeClass() {
static::$_sessionBackup = Configure::read('Session');
Configure::write('Session', array(
'defaults' => 'php',
'timeout' => 100,
'cookie' => 'test'
));
DispatcherFactory::add('Routing');
DispatcherFactory::add('ControllerFactory');
}
Expand All @@ -60,7 +55,6 @@ public static function setupBeforeClass() {
* @return void
*/
public static function teardownAfterClass() {
Configure::write('Session', static::$_sessionBackup);
DispatcherFactory::clear();
}

Expand All @@ -71,9 +65,10 @@ public static function teardownAfterClass() {
*/
public function setUp() {
parent::setUp();
$_SESSION = null;
$_SESSION = [];
Configure::write('App.namespace', 'TestApp');
$this->ComponentRegistry = new ComponentRegistry();
$controller = new Controller(new Request(['session' => new Session()]));
$this->ComponentRegistry = new ComponentRegistry($controller);
}

/**
Expand All @@ -83,7 +78,6 @@ public function setUp() {
*/
public function tearDown() {
parent::tearDown();
Session::destroy();
}

/**
Expand All @@ -106,36 +100,6 @@ public function testSessionIdConsistentAcrossRequestAction() {
$this->assertEquals($expected, $result);
}

/**
* testSessionValid method
*
* @return void
*/
public function testSessionValid() {
$Session = new SessionComponent($this->ComponentRegistry);

$this->assertTrue($Session->valid());

Configure::write('Session.checkAgent', true);
$Session->userAgent('rweerw');
$this->assertFalse($Session->valid());

$Session = new SessionComponent($this->ComponentRegistry);
$Session->time = $Session->read('Config.time') + 1;
$this->assertFalse($Session->valid());
}

/**
* testSessionError method
*
* @return void
*/
public function testSessionError() {
Session::$lastError = null;
$Session = new SessionComponent($this->ComponentRegistry);
$this->assertFalse($Session->error());
}

/**
* testSessionReadWrite method
*
Expand All @@ -146,24 +110,24 @@ public function testSessionReadWrite() {

$this->assertNull($Session->read('Test'));

$this->assertTrue($Session->write('Test', 'some value'));
$Session->write('Test', 'some value');
$this->assertEquals('some value', $Session->read('Test'));
$Session->delete('Test');

$this->assertTrue($Session->write('Test.key.path', 'some value'));
$Session->write('Test.key.path', 'some value');
$this->assertEquals('some value', $Session->read('Test.key.path'));
$this->assertEquals(array('path' => 'some value'), $Session->read('Test.key'));
$this->assertTrue($Session->write('Test.key.path2', 'another value'));
$Session->write('Test.key.path2', 'another value');
$this->assertEquals(array('path' => 'some value', 'path2' => 'another value'), $Session->read('Test.key'));
$Session->delete('Test');

$array = array('key1' => 'val1', 'key2' => 'val2', 'key3' => 'val3');
$this->assertTrue($Session->write('Test', $array));
$Session->write('Test', $array);
$this->assertEquals($Session->read('Test'), $array);
$Session->delete('Test');

$this->assertTrue($Session->write(array('Test'), 'some value'));
$this->assertTrue($Session->write(array('Test' => 'some value')));
$Session->write(array('Test'), 'some value');
$Session->write(array('Test' => 'some value'));
$this->assertEquals('some value', $Session->read('Test'));
$Session->delete('Test');
}
Expand All @@ -176,10 +140,9 @@ public function testSessionReadWrite() {
public function testSessionDelete() {
$Session = new SessionComponent($this->ComponentRegistry);

$this->assertFalse($Session->delete('Test'));

$Session->write('Test', 'some value');
$this->assertTrue($Session->delete('Test'));
$Session->delete('Test');
$this->assertNull($Session->read('Test'));
}

/**
Expand Down Expand Up @@ -228,9 +191,7 @@ public function testSessionFlash() {
* @return void
*/
public function testSessionId() {
unset($_SESSION);
$Session = new SessionComponent($this->ComponentRegistry);
Session::start();
$this->assertEquals(session_id(), $Session->id());
}

Expand Down

0 comments on commit 386f9d0

Please sign in to comment.