diff --git a/src/Http/Cookie/Cookie.php b/src/Http/Cookie/Cookie.php index 6e4382b25e4..8188f957067 100644 --- a/src/Http/Cookie/Cookie.php +++ b/src/Http/Cookie/Cookie.php @@ -292,7 +292,7 @@ public function check($path) * Writes data to the cookie * * @param string $path Path to write to - * @param mixer $value Value to write + * @param mixed $value Value to write * @return $this */ public function write($path, $value) @@ -332,7 +332,7 @@ public function read($path = null) */ public function willNeverExpire() { - $this->expiresAt = Chronos::now()->addYears(50)->format('U'); + $this->expiresAt = Chronos::now()->setDate(2038, 1, 1)->format('U'); return $this; } diff --git a/src/Http/Cookie/CookieCryptTrait.php b/src/Http/Cookie/CookieCryptTrait.php index 5fde91901ca..2a657740649 100644 --- a/src/Http/Cookie/CookieCryptTrait.php +++ b/src/Http/Cookie/CookieCryptTrait.php @@ -104,12 +104,11 @@ protected function _encrypt($value, $encrypt, $key = null) if (is_array($value)) { $value = $this->_flatten($value); } - if ($encrypt === false) { - return $value; - } + $this->checkCipher($encrypt); $prefix = 'Q2FrZQ==.'; $cipher = null; + if ($key === null) { $key = $this->getCryptoKey(); } diff --git a/tests/TestCase/Http/Cookie/CookieTest.php b/tests/TestCase/Http/Cookie/CookieTest.php index dbf79075c7c..2df41c1e0e7 100644 --- a/tests/TestCase/Http/Cookie/CookieTest.php +++ b/tests/TestCase/Http/Cookie/CookieTest.php @@ -22,6 +22,13 @@ class CookieTest extends TestCase { + /** + * Encryption key used in the tests + * + * @var string + */ + protected $encryptionKey = 'someverysecretkeythatisatleast32charslong'; + /** * Test invalid cookie name * @@ -58,7 +65,7 @@ public function testDecrypt() $cookie = new Cookie('cakephp', $encryptedCookieValue); $this->assertEquals($expected, $cookie->getValue()); - $cookie->decrypt('someverysecretkeythatisatleast32charslong'); + $cookie->decrypt($this->encryptionKey); $this->assertEquals('cakephp-rocks-and-is-awesome', $cookie->getValue()); } @@ -69,11 +76,13 @@ public function testDecrypt() */ public function testEncrypt() { - $cookie = new Cookie('cakephp', 'cakephp-rocks-and-is-awesome'); - $cookie->encrypt('someverysecretkeythatisatleast32charslong'); + $value = 'cakephp-rocks-and-is-awesome'; + + $cookie = new Cookie('cakephp', $value); + $cookie->encrypt($this->encryptionKey); - $expected = 'cakephp-rocks-and-is-awesome'; - $this->assertNotEmpty($expected, $cookie->getValue()); + $this->assertNotEquals($value, $cookie->getValue()); + $this->assertNotEmpty($cookie->getValue()); } /** @@ -87,14 +96,14 @@ public function testToHeaderValue() $result = $cookie->toHeaderValue(); $this->assertEquals('cakephp=cakephp-rocks', $result); - $date = Chronos::createFromFormat('m/d/Y h:m:s', '12/1/2050 12:00:00'); + $date = Chronos::createFromFormat('m/d/Y h:m:s', '12/1/2027 12:00:00'); $cookie = new Cookie('cakephp', 'cakephp-rocks'); $cookie->setDomain('cakephp.org'); $cookie->expiresAt($date); $result = $cookie->toHeaderValue(); - $expected = 'cakephp=cakephp-rocks; expires=Wed, 01-Dec-2049 12:00:00 GMT; domain=cakephp.org'; + $expected = 'cakephp=cakephp-rocks; expires=Tue, 01-Dec-2026 12:00:00 GMT; domain=cakephp.org'; $this->assertEquals($expected, $result); }