From eee450f1797fdd4271237c89df786439dac64eca Mon Sep 17 00:00:00 2001 From: mark_story Date: Wed, 28 May 2014 23:41:23 -0400 Subject: [PATCH] Update delete() to work with new implementation of cookies. Update delete method, and fix typos in test data. --- src/Controller/Component/CookieComponent.php | 37 ++++----- .../Component/CookieComponentTest.php | 77 +++++++------------ 2 files changed, 44 insertions(+), 70 deletions(-) diff --git a/src/Controller/Component/CookieComponent.php b/src/Controller/Component/CookieComponent.php index 7a53339e701..73b701d776b 100644 --- a/src/Controller/Component/CookieComponent.php +++ b/src/Controller/Component/CookieComponent.php @@ -245,33 +245,25 @@ public function check($key = null) { * This method will delete both the top level and 2nd level cookies set. * For example assuming that $name = App, deleting `User` will delete * both `App[User]` and any other cookie values like `App[User][email]` - * This is done to clean up cookie storage from before 2.4.3, where cookies - * were stored inconsistently. * * @param string $key Key of the value to be deleted * @return void * @link http://book.cakephp.org/2.0/en/core-libraries/components/cookie.html#CookieComponent::delete */ public function delete($key) { - $cookieName = $this->config('name'); - if (empty($this->_values[$cookieName])) { - $this->read(); - } - if (strpos($key, '.') === false) { - if (isset($this->_values[$cookieName][$key]) && is_array($this->_values[$cookieName][$key])) { - foreach ($this->_values[$cookieName][$key] as $idx => $val) { - $this->_delete("[$key][$idx]"); - } - } - $this->_delete("[$key]"); - unset($this->_values[$cookieName][$key]); - return; + if (empty($this->_values)) { + $this->_load(); } - $names = explode('.', $key, 2); - if (isset($this->_values[$cookieName][$names[0]])) { - $this->_values[$cookieName][$names[0]] = Hash::remove($this->_values[$cookieName][$names[0]], $names[1]); + + $this->_values = Hash::remove($this->_values, $key); + $parts = explode('.', $key); + $top = $parts[0]; + + if (isset($this->_values[$top])) { + $this->_write($top, $this->_values[$top]); + } else { + $this->_delete($top); } - $this->_delete('[' . implode('][', $names) . ']'); } /** @@ -347,14 +339,17 @@ protected function _write($name, $value) { } /** - * Sets a cookie expire time to remove cookie value + * Sets a cookie expire time to remove cookie value. + * + * This is only done once all values in a cookie key have been + * removed with delete. * * @param string $name Name of cookie * @return void */ protected function _delete($name) { $config = $this->configKey($name); - $expires = new \DateTime($config['expires']); + $expires = new \DateTime('now'); $this->_response->cookie(array( 'name' => $name, diff --git a/tests/TestCase/Controller/Component/CookieComponentTest.php b/tests/TestCase/Controller/Component/CookieComponentTest.php index 092a7136e1d..9bcddb7a0ab 100644 --- a/tests/TestCase/Controller/Component/CookieComponentTest.php +++ b/tests/TestCase/Controller/Component/CookieComponentTest.php @@ -106,9 +106,9 @@ public function testConfigKeyArray() { */ protected function _setCookieData() { $this->Cookie->write(array('Encrytped_array' => array('name' => 'CakePHP', 'version' => '1.2.0.x', 'tag' => 'CakePHP Rocks!'))); - $this->Cookie->write(array('Encrytped_multi_cookies.name' => 'CakePHP')); - $this->Cookie->write(array('Encrytped_multi_cookies.version' => '1.2.0.x')); - $this->Cookie->write(array('Encrytped_multi_cookies.tag' => 'CakePHP Rocks!')); + $this->Cookie->write(array('Encrypted_multi_cookies.name' => 'CakePHP')); + $this->Cookie->write(array('Encrypted_multi_cookies.version' => '1.2.0.x')); + $this->Cookie->write(array('Encrypted_multi_cookies.tag' => 'CakePHP Rocks!')); $this->Cookie->write(array('Plain_array' => array('name' => 'CakePHP', 'version' => '1.2.0.x', 'tag' => 'CakePHP Rocks!')), null, false); $this->Cookie->write(array('Plain_multi_cookies.name' => 'CakePHP'), null, false); @@ -142,7 +142,7 @@ public function testReadEncryptedCookieData() { $expected = array('name' => 'CakePHP', 'version' => '1.2.0.x', 'tag' => 'CakePHP Rocks!'); $this->assertEquals($expected, $data); - $data = $this->Cookie->read('Encrytped_multi_cookies'); + $data = $this->Cookie->read('Encrypted_multi_cookies'); $expected = array('name' => 'CakePHP', 'version' => '1.2.0.x', 'tag' => 'CakePHP Rocks!'); $this->assertEquals($expected, $data); } @@ -311,59 +311,39 @@ public function testWriteMulitMixedEncryption() { * @return void */ public function testDeleteHttpOnly() { - $this->markTestIncomplete(); $this->Cookie->config([ 'httpOnly' => true, 'secure' => false ]); - $this->Cookie->delete('Testing', false); + $this->Cookie->delete('Testing'); $expected = array( - 'name' => $this->Cookie->config('name') . '[Testing]', + 'name' => 'Testing', 'value' => '', 'expire' => time() - 42000, 'path' => '/', 'domain' => '', 'secure' => false, 'httpOnly' => true); - $result = $this->Controller->response->cookie($this->Cookie->config('name') . '[Testing]'); + $result = $this->Controller->response->cookie('Testing'); $this->assertEquals($expected, $result); } -/** - * testWritePlainCookieArray - * - * @return void - */ - public function testWritePlainCookieArray() { - $this->markTestIncomplete(); - $this->Cookie->write(array('name' => 'CakePHP', 'version' => '1.2.0.x', 'tag' => 'CakePHP Rocks!'), null, false); - - $this->assertEquals('CakePHP', $this->Cookie->read('name')); - $this->assertEquals('1.2.0.x', $this->Cookie->read('version')); - $this->assertEquals('CakePHP Rocks!', $this->Cookie->read('tag')); - - $this->Cookie->delete('name'); - $this->Cookie->delete('version'); - $this->Cookie->delete('tag'); - } - /** * test writing values that are not scalars * * @return void */ public function testWriteArrayValues() { - $this->markTestIncomplete(); - $this->Cookie->config('secure', false); - $this->Cookie->write('Testing', array(1, 2, 3), false); + $this->Cookie->write('Testing', array(1, 2, 3)); $expected = array( - 'name' => $this->Cookie->config('name') . '[Testing]', + 'name' => 'Testing', 'value' => '[1,2,3]', 'path' => '/', 'domain' => '', 'secure' => false, - 'httpOnly' => false); - $result = $this->Controller->response->cookie($this->Cookie->config('name') . '[Testing]'); + 'httpOnly' => false + ); + $result = $this->Controller->response->cookie('Testing'); $this->assertWithinMargin($result['expire'], time() + 10, 1); unset($result['expire']); @@ -376,19 +356,17 @@ public function testWriteArrayValues() { * @return void */ public function testWriteMixedArray() { - $this->markTestIncomplete(); - $this->Cookie->config('encrypt', false); $this->Cookie->write('User', array('name' => 'mark'), false); $this->Cookie->write('User.email', 'mark@example.com', false); $expected = array( - 'name' => $this->Cookie->config('name') . '[User]', + 'name' => 'User', 'value' => '{"name":"mark","email":"mark@example.com"}', 'path' => '/', 'domain' => '', 'secure' => false, 'httpOnly' => false ); - $result = $this->Controller->response->cookie($this->Cookie->config('name') . '[User]'); + $result = $this->Controller->response->cookie('User'); unset($result['expire']); $this->assertEquals($expected, $result); @@ -396,14 +374,14 @@ public function testWriteMixedArray() { $this->Cookie->write('User.email', 'mark@example.com', false); $this->Cookie->write('User', array('name' => 'mark'), false); $expected = array( - 'name' => $this->Cookie->config('name') . '[User]', + 'name' => 'User', 'value' => '{"name":"mark"}', 'path' => '/', 'domain' => '', 'secure' => false, 'httpOnly' => false ); - $result = $this->Controller->response->cookie($this->Cookie->config('name') . '[User]'); + $result = $this->Controller->response->cookie('User'); unset($result['expire']); $this->assertEquals($expected, $result); @@ -422,7 +400,7 @@ public function testReadingAllValues() { 'name' => 'CakePHP', 'version' => '1.2.0.x', 'tag' => 'CakePHP Rocks!'), - 'Encrytped_multi_cookies' => array( + 'Encrypted_multi_cookies' => array( 'name' => 'CakePHP', 'version' => '1.2.0.x', 'tag' => 'CakePHP Rocks!'), @@ -443,10 +421,9 @@ public function testReadingAllValues() { * @return void */ public function testDeleteCookieValue() { - $this->markTestIncomplete(); $this->_setCookieData(); - $this->Cookie->delete('Encrytped_multi_cookies.name'); - $data = $this->Cookie->read('Encrytped_multi_cookies'); + $this->Cookie->delete('Encrypted_multi_cookies.name'); + $data = $this->Cookie->read('Encrypted_multi_cookies'); $expected = array('version' => '1.2.0.x', 'tag' => 'CakePHP Rocks!'); $this->assertEquals($expected, $data); @@ -484,15 +461,15 @@ public function testReadingCookieArray() { $expected = 'CakePHP Rocks!'; $this->assertEquals($expected, $data); - $data = $this->Cookie->read('Encrytped_multi_cookies.name'); + $data = $this->Cookie->read('Encrypted_multi_cookies.name'); $expected = 'CakePHP'; $this->assertEquals($expected, $data); - $data = $this->Cookie->read('Encrytped_multi_cookies.version'); + $data = $this->Cookie->read('Encrypted_multi_cookies.version'); $expected = '1.2.0.x'; $this->assertEquals($expected, $data); - $data = $this->Cookie->read('Encrytped_multi_cookies.tag'); + $data = $this->Cookie->read('Encrypted_multi_cookies.tag'); $expected = 'CakePHP Rocks!'; $this->assertEquals($expected, $data); @@ -661,8 +638,7 @@ public function testCheckEmpty() { * @return void */ public function testDeleteRemovesChildren() { - $this->markTestIncomplete(); - $this->request->cookies['CakeTestCookie'] = array( + $this->request->cookies = array( 'User' => array('email' => 'example@example.com', 'name' => 'mark'), 'other' => 'value' ); @@ -670,7 +646,11 @@ public function testDeleteRemovesChildren() { $this->Cookie->delete('User'); $this->assertNull($this->Cookie->read('User.email')); - $this->Cookie->destroy(); + $this->assertNull($this->Cookie->read('User.name')); + + $result = $this->Controller->response->cookie('User'); + $this->assertEquals('', $result['value']); + $this->assertLessThan(time(), $result['expire']); } /** @@ -679,7 +659,6 @@ public function testDeleteRemovesChildren() { * @return void */ public function testDeleteChildrenNotExist() { - $this->markTestIncomplete(); $this->assertNull($this->Cookie->delete('NotFound')); $this->assertNull($this->Cookie->delete('Not.Found')); }