From 875dd300759dbb18fd3454940b3a9eaeb773bbb6 Mon Sep 17 00:00:00 2001 From: mark_story Date: Tue, 27 May 2014 22:38:37 -0400 Subject: [PATCH] Fix read() and check(). Both appear to be working now. --- src/Controller/Component/CookieComponent.php | 14 ++-- .../Component/CookieComponentTest.php | 78 +------------------ 2 files changed, 11 insertions(+), 81 deletions(-) diff --git a/src/Controller/Component/CookieComponent.php b/src/Controller/Component/CookieComponent.php index 0e5e7bfbed7..b9e5a251af5 100644 --- a/src/Controller/Component/CookieComponent.php +++ b/src/Controller/Component/CookieComponent.php @@ -326,7 +326,6 @@ public function encryption($type = null) { */ protected function _write($name, $value) { $config = $this->config(); - $expires = new \DateTime($config['expires']); $this->_response->cookie(array( @@ -348,10 +347,12 @@ protected function _write($name, $value) { */ protected function _delete($name) { $config = $this->config(); + $expires = new \DateTime($config['expires']); + $this->_response->cookie(array( - 'name' => $config['name'] . $name, + 'name' => $name, 'value' => '', - 'expire' => time() - 42000, + 'expire' => $expires->format('U') - 42000, 'path' => $config['path'], 'domain' => $config['domain'], 'secure' => $config['secure'], @@ -389,9 +390,12 @@ protected function _encrypt($value) { * @return string decrypted string */ protected function _decrypt($values) { - $decrypted = array(); + if (is_string($values)) { + return $this->_decode($values); + } - foreach ((array)$values as $name => $value) { + $decrypted = array(); + foreach ($values as $name => $value) { if (is_array($value)) { foreach ($value as $key => $val) { $decrypted[$name][$key] = $this->_decode($val); diff --git a/tests/TestCase/Controller/Component/CookieComponentTest.php b/tests/TestCase/Controller/Component/CookieComponentTest.php index 567fceb0b62..2da71ddb665 100644 --- a/tests/TestCase/Controller/Component/CookieComponentTest.php +++ b/tests/TestCase/Controller/Component/CookieComponentTest.php @@ -566,70 +566,13 @@ public function testReadingDataFromRequest() { $this->assertEquals($expected, $data); } -/** - * testReadingCookieDataWithoutStartup - * - * @return void - */ - public function testReadingCookieDataWithoutStartup() { - $this->markTestIncomplete(); - $data = $this->Cookie->read('Encrytped_array'); - $expected = null; - $this->assertEquals($expected, $data); - - $data = $this->Cookie->read('Encrytped_multi_cookies'); - $expected = null; - $this->assertEquals($expected, $data); - - $data = $this->Cookie->read('Plain_array'); - $expected = null; - $this->assertEquals($expected, $data); - - $data = $this->Cookie->read('Plain_multi_cookies'); - $expected = null; - $this->assertEquals($expected, $data); - - $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!'); - $this->assertEquals($expected, $data); - - $data = $this->Cookie->read('Encrytped_multi_cookies'); - $expected = array('name' => 'CakePHP', 'version' => '1.2.0.x', 'tag' => 'CakePHP Rocks!'); - $this->assertEquals($expected, $data); - - $data = $this->Cookie->read('Plain_array'); - $expected = array('name' => 'CakePHP', 'version' => '1.2.0.x', 'tag' => 'CakePHP Rocks!'); - $this->assertEquals($expected, $data); - - $data = $this->Cookie->read('Plain_multi_cookies'); - $expected = array('name' => 'CakePHP', 'version' => '1.2.0.x', 'tag' => 'CakePHP Rocks!'); - $this->assertEquals($expected, $data); - $this->Cookie->destroy(); - } - /** * Test Reading legacy cookie values. * * @return void */ public function testReadLegacyCookieValue() { - $this->markTestIncomplete(); - $this->request->cookies['CakeTestCookie'] = array( + $this->request->cookies = array( 'Legacy' => array('value' => $this->_oldImplode(array(1, 2, 3))) ); $result = $this->Cookie->read('Legacy.value'); @@ -643,8 +586,7 @@ public function testReadLegacyCookieValue() { * @return void */ public function testReadEmpty() { - $this->markTestIncomplete(); - $this->request->cookies['CakeTestCookie'] = array( + $this->request->cookies = array( 'JSON' => '{"name":"value"}', 'Empty' => '', 'String' => '{"somewhat:"broken"}', @@ -657,25 +599,12 @@ public function testReadEmpty() { $this->assertEquals(array(), $this->Cookie->read('Array')); } -/** - * test that no error is issued for non array data. - * - * @return void - */ - public function testNoErrorOnNonArrayData() { - $this->markTestIncomplete(); - $this->request->cookies['CakeTestCookie'] = 'kaboom'; - - $this->assertNull($this->Cookie->read('value')); - } - /** * testCheck method * * @return void */ public function testCheck() { - $this->markTestIncomplete(); $this->Cookie->write('CookieComponentTestCase', 'value'); $this->assertTrue($this->Cookie->check('CookieComponentTestCase')); @@ -688,7 +617,6 @@ public function testCheck() { * @return void */ public function testCheckingSavedEmpty() { - $this->markTestIncomplete(); $this->Cookie->write('CookieComponentTestCase', 0); $this->assertTrue($this->Cookie->check('CookieComponentTestCase')); @@ -702,7 +630,6 @@ public function testCheckingSavedEmpty() { * @return void */ public function testCheckKeyWithSpaces() { - $this->markTestIncomplete(); $this->Cookie->write('CookieComponent Test', "test"); $this->assertTrue($this->Cookie->check('CookieComponent Test')); $this->Cookie->delete('CookieComponent Test'); @@ -717,7 +644,6 @@ public function testCheckKeyWithSpaces() { * @return void */ public function testCheckEmpty() { - $this->markTestIncomplete(); $this->assertFalse($this->Cookie->check()); }