Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Updated the test cases to remove deprecated usage of the withCookie m…
…ethod
  • Loading branch information
David Yell committed Jun 7, 2018
1 parent 3cbefae commit 406dce8
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 13 deletions.
19 changes: 12 additions & 7 deletions src/Http/Middleware/CsrfProtectionMiddleware.php
Expand Up @@ -14,6 +14,7 @@
*/
namespace Cake\Http\Middleware;

use Cake\Http\Cookie\Cookie;
use Cake\Http\Exception\InvalidCsrfTokenException;
use Cake\Http\Response;
use Cake\Http\ServerRequest;
Expand Down Expand Up @@ -163,13 +164,17 @@ protected function _addTokenCookie($token, ServerRequest $request, Response $res
{
$expiry = new Time($this->_config['expiry']);

return $response->withCookie($this->_config['cookieName'], [
'value' => $token,
'expire' => $expiry->format('U'),
'path' => $request->getAttribute('webroot'),
'secure' => $this->_config['secure'],
'httpOnly' => $this->_config['httpOnly'],
]);
$cookie = new Cookie(
$this->_config['cookieName'],
$token,
$expiry,
$request->getAttribute('webroot'),
'',
(bool)$this->_config['secure'],
(bool)$this->_config['httpOnly']
);

return $response->withCookie($cookie);
}

/**
Expand Down
Expand Up @@ -439,7 +439,7 @@ public function testDeleteHttpOnly()
$expected = [
'name' => 'Testing',
'value' => '',
'expire' => (new Time('now'))->format('U') - 42000,
'expire' => '1',
'path' => '/',
'domain' => '',
'secure' => false,
Expand Down
Expand Up @@ -14,6 +14,7 @@
*/
namespace Cake\Test\TestCase\Http\Middleware;

use Cake\Http\Cookie\Cookie;
use Cake\Http\Cookie\CookieCollection;
use Cake\Http\Middleware\EncryptedCookieMiddleware;
use Cake\Http\Response;
Expand Down Expand Up @@ -110,9 +111,9 @@ public function testEncodeResponseCookieData()
$request = new ServerRequest(['url' => '/cookies/nom']);
$response = new Response();
$next = function ($req, $res) {
return $res->withCookie('secret', 'be quiet')
->withCookie('plain', 'in clear')
->withCookie('ninja', 'shuriken');
return $res->withCookie(new Cookie('secret', 'be quiet'))
->withCookie(new Cookie('plain', 'in clear'))
->withCookie(new Cookie('ninja', 'shuriken'));
};
$middleware = $this->middleware;
$response = $middleware($request, $response, $next);
Expand Down
3 changes: 2 additions & 1 deletion tests/TestCase/Http/ResponseEmitterTest.php
Expand Up @@ -15,6 +15,7 @@
namespace Cake\Test\TestCase;

use Cake\Http\CallbackStream;
use Cake\Http\Cookie\Cookie;
use Cake\Http\Response;
use Cake\Http\ResponseEmitter;
use Cake\TestSuite\TestCase;
Expand Down Expand Up @@ -110,7 +111,7 @@ public function testEmitNoContentResponse()
public function testEmitResponseArrayCookies()
{
$response = (new Response())
->withCookie('simple', ['value' => 'val', 'secure' => true])
->withCookie(new Cookie('simple', 'val', null, '/', '', true))
->withAddedHeader('Set-Cookie', 'google=not=nice;Path=/accounts; HttpOnly')
->withHeader('Content-Type', 'text/plain');
$response->getBody()->write('ok');
Expand Down
3 changes: 2 additions & 1 deletion tests/test_app/TestApp/Controller/PostsController.php
Expand Up @@ -15,6 +15,7 @@
namespace TestApp\Controller;

use Cake\Event\Event;
use Cake\Http\Cookie\Cookie;

/**
* PostsController class
Expand Down Expand Up @@ -55,7 +56,7 @@ public function beforeFilter(Event $event)
public function index($layout = 'default')
{
$this->Flash->error('An error message');
$this->response = $this->response->withCookie('remember_me', 1);
$this->response = $this->response->withCookie(new Cookie('remember_me', 1));
$this->set('test', 'value');
$this->viewBuilder()->setLayout($layout);
}
Expand Down

0 comments on commit 406dce8

Please sign in to comment.