Skip to content

Commit

Permalink
Fix deprecated method use in CookieComponent.
Browse files Browse the repository at this point in the history
  • Loading branch information
markstory committed Nov 16, 2017
1 parent c0d360d commit 035f583
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 20 deletions.
18 changes: 8 additions & 10 deletions src/Controller/Component/CookieComponent.php
Expand Up @@ -15,7 +15,7 @@
namespace Cake\Controller\Component;

use Cake\Controller\Component;
use Cake\Http\ServerRequest;
use Cake\Http\ServerRequestFactory;
use Cake\I18n\Time;
use Cake\Utility\CookieCryptTrait;
use Cake\Utility\Hash;
Expand Down Expand Up @@ -124,7 +124,7 @@ public function initialize(array $config)
$controller = $this->_registry->getController();

if ($controller === null) {
$this->request = ServerRequest::createFromGlobals();
$this->request = ServerRequestFactory::fromGlobals();
}

if (empty($this->_config['path'])) {
Expand Down Expand Up @@ -246,10 +246,10 @@ protected function _load($key)
if (isset($this->_loaded[$first])) {
return;
}
if (!isset($this->request->cookies[$first])) {
$cookie = $this->request->getCookie($first);
if ($cookie === null) {
return;
}
$cookie = $this->request->cookies[$first];
$config = $this->configKey($first);
$this->_loaded[$first] = true;
$this->_values[$first] = $this->_decrypt($cookie, $config['encryption'], $config['key']);
Expand Down Expand Up @@ -309,9 +309,8 @@ protected function _write($name, $value)
$config = $this->configKey($name);
$expires = new Time($config['expires']);

$response = $this->getController()->response;
$response->cookie([
'name' => $name,
$controller = $this->getController();
$controller->response = $controller->response->withCookie($name, [
'value' => $this->_encrypt($value, $config['encryption'], $config['key']),
'expire' => $expires->format('U'),
'path' => $config['path'],
Expand All @@ -334,10 +333,9 @@ protected function _delete($name)
{
$config = $this->configKey($name);
$expires = new Time('now');
$controller = $this->getController();

$response = $this->getController()->response;
$response->cookie([
'name' => $name,
$controller->response = $controller->response->withCookie($name, [
'value' => '',
'expire' => $expires->format('U') - 42000,
'path' => $config['path'],
Expand Down
20 changes: 10 additions & 10 deletions tests/TestCase/Controller/Component/CookieComponentTest.php
Expand Up @@ -324,7 +324,7 @@ public function testWriteFarFuture()
'domain' => '',
'secure' => false,
'httpOnly' => false];
$result = $this->Controller->response->cookie('Testing');
$result = $this->Controller->response->getCookie('Testing');

$this->assertEquals($future->format('U'), $result['expire'], '', 3);
unset($result['expire']);
Expand Down Expand Up @@ -352,7 +352,7 @@ public function testWriteHttpOnly()
'domain' => '',
'secure' => false,
'httpOnly' => true];
$result = $this->Controller->response->cookie('Testing');
$result = $this->Controller->response->getCookie('Testing');
$this->assertEquals($expected, $result);
}

Expand All @@ -377,11 +377,11 @@ public function testWriteMulitMixedEncryption()
'secure' => false,
'httpOnly' => false
];
$result = $this->Controller->response->cookie('Open');
$result = $this->Controller->response->getCookie('Open');
unset($result['expire']);
$this->assertEquals($expected, $result);

$result = $this->Controller->response->cookie('Closed');
$result = $this->Controller->response->getCookie('Closed');
$this->assertContains('Q2FrZQ==.', $result['value']);
}

Expand All @@ -401,7 +401,7 @@ public function testWriteConfigKeyWithCustomEncryptionKey()
$this->Cookie->configKey($name, compact('key', 'encryption'));
$this->Cookie->write($name, $value);

$cookie = $this->Controller->response->cookie($name);
$cookie = $this->Controller->response->getCookie($name);

$this->assertEquals($value, Security::decrypt(base64_decode(substr($cookie['value'], strlen($prefix))), $key));
}
Expand Down Expand Up @@ -444,7 +444,7 @@ public function testDeleteHttpOnly()
'domain' => '',
'secure' => false,
'httpOnly' => true];
$result = $this->Controller->response->cookie('Testing');
$result = $this->Controller->response->getCookie('Testing');
$this->assertEquals($expected, $result);
}

Expand All @@ -464,7 +464,7 @@ public function testWriteArrayValues()
'secure' => false,
'httpOnly' => false
];
$result = $this->Controller->response->cookie('Testing');
$result = $this->Controller->response->getCookie('Testing');

$time = new Time('now');
$this->assertWithinRange($time->format('U') + 10, $result['expire'], 1);
Expand All @@ -489,7 +489,7 @@ public function testWriteMixedArray()
'secure' => false,
'httpOnly' => false
];
$result = $this->Controller->response->cookie('User');
$result = $this->Controller->response->getCookie('User');
unset($result['expire']);

$this->assertEquals($expected, $result);
Expand All @@ -504,7 +504,7 @@ public function testWriteMixedArray()
'secure' => false,
'httpOnly' => false
];
$result = $this->Controller->response->cookie('User');
$result = $this->Controller->response->getCookie('User');
unset($result['expire']);

$this->assertEquals($expected, $result);
Expand Down Expand Up @@ -753,7 +753,7 @@ public function testDeleteRemovesChildren()
$this->assertNull($this->Cookie->read('User.email'));
$this->assertNull($this->Cookie->read('User.name'));

$result = $this->Controller->response->cookie('User');
$result = $this->Controller->response->getCookie('User');
$this->assertEquals('', $result['value']);
$this->assertLessThan(time(), $result['expire']);
}
Expand Down

0 comments on commit 035f583

Please sign in to comment.