Skip to content

Commit

Permalink
Update delete() to work with new implementation of cookies.
Browse files Browse the repository at this point in the history
Update delete method, and fix typos in test data.
  • Loading branch information
markstory committed May 29, 2014
1 parent c79533c commit eee450f
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 70 deletions.
37 changes: 16 additions & 21 deletions src/Controller/Component/CookieComponent.php
Expand Up @@ -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) . ']');
}

/**
Expand Down Expand Up @@ -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,
Expand Down
77 changes: 28 additions & 49 deletions tests/TestCase/Controller/Component/CookieComponentTest.php
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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']);
Expand All @@ -376,34 +356,32 @@ 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);

$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);
Expand All @@ -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!'),
Expand All @@ -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);

Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -661,16 +638,19 @@ 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'
);
$this->assertEquals('mark', $this->Cookie->read('User.name'));

$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']);
}

/**
Expand All @@ -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'));
}
Expand Down

0 comments on commit eee450f

Please sign in to comment.