Skip to content

Commit

Permalink
Make CookieComponent use Request::cookie()
Browse files Browse the repository at this point in the history
This makes reading/testing cookies easier in integration tests and
requestAction()
  • Loading branch information
markstory committed Jul 4, 2012
1 parent c2f1553 commit d3a41a9
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 46 deletions.
32 changes: 24 additions & 8 deletions lib/Cake/Controller/Component/CookieComponent.php
Expand Up @@ -16,12 +16,13 @@
* @since CakePHP(tm) v 1.2.0.4213
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/

namespace Cake\Controller\Component;

use Cake\Controller\Component;
use Cake\Controller\ComponentCollection;
use Cake\Controller\Controller;
use Cake\Core\Configure;
use Cake\Network\Request;
use Cake\Network\Response;
use Cake\Utility\Hash;
use Cake\Utility\Security;
Expand Down Expand Up @@ -161,11 +162,18 @@ class CookieComponent extends Component {

/**
* A reference to the Controller's Cake\Network\Response object
*
*
* @var Cake\Network\Response
*/
protected $_response = null;

/**
* The request from the controller.
*
* @var Cake\Network\Request
*/
protected $_request;

/**
* Constructor
*
Expand All @@ -180,6 +188,12 @@ public function __construct(ComponentCollection $collection, $settings = array()
}

$controller = $collection->getController();
if ($controller && isset($controller->request)) {
$this->_request = $controller->request;
} else {
$this->_request = Request::createFromGlobals();
}

if ($controller && isset($controller->response)) {
$this->_response = $controller->response;
} else {
Expand All @@ -197,8 +211,9 @@ public function startup(Controller $controller) {
$this->_expire($this->time);

$this->_values[$this->name] = array();
if (isset($_COOKIE[$this->name])) {
$this->_values[$this->name] = $this->_decrypt($_COOKIE[$this->name]);
$values = $this->_request->cookie($this->name);
if ($values) {
$this->_values[$this->name] = $this->_decrypt($values);
}
}

Expand Down Expand Up @@ -263,8 +278,9 @@ public function write($key, $value = null, $encrypt = true, $expires = null) {
* @link http://book.cakephp.org/2.0/en/core-libraries/components/cookie.html#CookieComponent::read
*/
public function read($key = null) {
if (empty($this->_values[$this->name]) && isset($_COOKIE[$this->name])) {
$this->_values[$this->name] = $this->_decrypt($_COOKIE[$this->name]);
$values = $this->_request->cookie($this->name);
if (empty($this->_values[$this->name]) && $values) {
$this->_values[$this->name] = $this->_decrypt($values);
}
if (empty($this->_values[$this->name])) {
$this->_values[$this->name] = array();
Expand Down Expand Up @@ -331,8 +347,8 @@ public function delete($key) {
* @link http://book.cakephp.org/2.0/en/core-libraries/components/cookie.html#CookieComponent::destroy
*/
public function destroy() {
if (isset($_COOKIE[$this->name])) {
$this->_values[$this->name] = $this->_decrypt($_COOKIE[$this->name]);
if (empty($this->_values[$this->name])) {
$this->read();
}

foreach ($this->_values[$this->name] as $name => $value) {
Expand Down
72 changes: 34 additions & 38 deletions lib/Cake/Test/TestCase/Controller/Component/CookieComponentTest.php
Expand Up @@ -33,20 +33,12 @@
*/
class CookieComponentTest extends TestCase {

/**
* Controller property
*
* @var CookieComponentTestController
*/
public $Controller;

/**
* start
*
* @return void
*/
public function setUp() {
$_COOKIE = array();
$controller = $this->getMock(
'Cake\Controller\Controller',
array('redirect'),
Expand All @@ -56,6 +48,7 @@ public function setUp() {
$controller->constructClasses();
$this->Controller = $controller;
$this->Cookie = $controller->Cookie;
$this->request = $controller->request;

$this->Cookie->name = 'CakeTestCookie';
$this->Cookie->time = 10;
Expand Down Expand Up @@ -155,7 +148,7 @@ public function testReadPlainCookieData() {
* @return void
*/
public function testReadWithNameSwitch() {
$_COOKIE = array(
$this->request->cookies = array(
'CakeTestCookie' => array(
'key' => 'value'
),
Expand Down Expand Up @@ -389,17 +382,20 @@ public function testReadingCookieDataOnStartup() {
$data = $this->Cookie->read('Plain_multi_cookies');
$this->assertNull($data);

$_COOKIE['CakeTestCookie'] = array(
'Encrytped_array' => $this->__encrypt(array('name' => 'CakePHP', 'version' => '1.2.0.x', 'tag' => 'CakePHP Rocks!')),
'Encrytped_multi_cookies' => array(
'name' => $this->__encrypt('CakePHP'),
'version' => $this->__encrypt('1.2.0.x'),
'tag' => $this->__encrypt('CakePHP Rocks!')),
'Plain_array' => '{"name":"CakePHP","version":"1.2.0.x","tag":"CakePHP Rocks!"}',
'Plain_multi_cookies' => array(
'name' => 'CakePHP',
'version' => '1.2.0.x',
'tag' => 'CakePHP Rocks!'));
$this->request->cookies['CakeTestCookie'] = array(
'Encrytped_array' => $this->__encrypt(array('name' => 'CakePHP', 'version' => '1.2.0.x', 'tag' => 'CakePHP Rocks!')),
'Encrytped_multi_cookies' => array(
'name' => $this->__encrypt('CakePHP'),
'version' => $this->__encrypt('1.2.0.x'),
'tag' => $this->__encrypt('CakePHP Rocks!')
),
'Plain_array' => '{"name":"CakePHP","version":"1.2.0.x","tag":"CakePHP Rocks!"}',
'Plain_multi_cookies' => array(
'name' => 'CakePHP',
'version' => '1.2.0.x',
'tag' => 'CakePHP Rocks!'
)
);

$data = $this->Cookie->read('Encrytped_array');
$expected = array('name' => 'CakePHP', 'version' => '1.2.0.x', 'tag' => 'CakePHP Rocks!');
Expand All @@ -417,7 +413,6 @@ public function testReadingCookieDataOnStartup() {
$expected = array('name' => 'CakePHP', 'version' => '1.2.0.x', 'tag' => 'CakePHP Rocks!');
$this->assertEquals($expected, $data);
$this->Cookie->destroy();
unset($_COOKIE['CakeTestCookie']);
}

/**
Expand All @@ -442,17 +437,20 @@ public function testReadingCookieDataWithoutStartup() {
$expected = null;
$this->assertEquals($expected, $data);

$_COOKIE['CakeTestCookie'] = array(
'Encrytped_array' => $this->__encrypt(array('name' => 'CakePHP', 'version' => '1.2.0.x', 'tag' => 'CakePHP Rocks!')),
'Encrytped_multi_cookies' => array(
'name' => $this->__encrypt('CakePHP'),
'version' => $this->__encrypt('1.2.0.x'),
'tag' => $this->__encrypt('CakePHP Rocks!')),
'Plain_array' => '{"name":"CakePHP","version":"1.2.0.x","tag":"CakePHP Rocks!"}',
'Plain_multi_cookies' => array(
'name' => 'CakePHP',
'version' => '1.2.0.x',
'tag' => 'CakePHP Rocks!'));
$this->request->cookies['CakeTestCookie'] = array(
'Encrytped_array' => $this->__encrypt(array('name' => 'CakePHP', 'version' => '1.2.0.x', 'tag' => 'CakePHP Rocks!')),
'Encrytped_multi_cookies' => array(
'name' => $this->__encrypt('CakePHP'),
'version' => $this->__encrypt('1.2.0.x'),
'tag' => $this->__encrypt('CakePHP Rocks!')
),
'Plain_array' => '{"name":"CakePHP","version":"1.2.0.x","tag":"CakePHP Rocks!"}',
'Plain_multi_cookies' => array(
'name' => 'CakePHP',
'version' => '1.2.0.x',
'tag' => 'CakePHP Rocks!'
)
);

$data = $this->Cookie->read('Encrytped_array');
$expected = array('name' => 'CakePHP', 'version' => '1.2.0.x', 'tag' => 'CakePHP Rocks!');
Expand All @@ -470,7 +468,6 @@ public function testReadingCookieDataWithoutStartup() {
$expected = array('name' => 'CakePHP', 'version' => '1.2.0.x', 'tag' => 'CakePHP Rocks!');
$this->assertEquals($expected, $data);
$this->Cookie->destroy();
unset($_COOKIE['CakeTestCookie']);
}

/**
Expand All @@ -479,7 +476,7 @@ public function testReadingCookieDataWithoutStartup() {
* @return void
*/
public function testReadLegacyCookieValue() {
$_COOKIE['CakeTestCookie'] = array(
$this->request->cookies['CakeTestCookie'] = array(
'Legacy' => array('value' => $this->_oldImplode(array(1, 2, 3)))
);
$result = $this->Cookie->read('Legacy.value');
Expand All @@ -491,7 +488,7 @@ public function testReadLegacyCookieValue() {
* Test reading empty values.
*/
public function testReadEmpty() {
$_COOKIE['CakeTestCookie'] = array(
$this->request->cookies['CakeTestCookie'] = array(
'JSON' => '{"name":"value"}',
'Empty' => '',
'String' => '{"somewhat:"broken"}'
Expand All @@ -508,8 +505,7 @@ public function testReadEmpty() {
* @return void
*/
public function testNoErrorOnNonArrayData() {
$this->Cookie->destroy();
$_COOKIE['CakeTestCookie'] = 'kaboom';
$this->request->cookies['CakeTestCookie'] = 'kaboom';

$this->assertNull($this->Cookie->read('value'));
}
Expand All @@ -520,7 +516,7 @@ public function testNoErrorOnNonArrayData() {
* @return void
*/
public function testDeleteRemovesChildren() {
$_COOKIE['CakeTestCookie'] = array(
$this->request->cookies['CakeTestCookie'] = array(
'User' => array('email' => 'example@example.com', 'name' => 'mark'),
'other' => 'value'
);
Expand Down

0 comments on commit d3a41a9

Please sign in to comment.