Skip to content

Commit bbea910

Browse files
committed
Fix CookieComponent::delete() not working for deep children
1 parent e85f489 commit bbea910

File tree

2 files changed

+92
-20
lines changed

2 files changed

+92
-20
lines changed

lib/Cake/Controller/Component/CookieComponent.php

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -316,20 +316,16 @@ public function delete($key) {
316316
$this->read();
317317
}
318318
if (strpos($key, '.') === false) {
319-
if (isset($this->_values[$this->name][$key]) && is_array($this->_values[$this->name][$key])) {
320-
foreach ($this->_values[$this->name][$key] as $idx => $val) {
321-
$this->_delete("[$key][$idx]");
322-
}
323-
}
324-
$this->_delete("[$key]");
325319
unset($this->_values[$this->name][$key]);
326-
return;
327-
}
328-
$names = explode('.', $key, 2);
329-
if (isset($this->_values[$this->name][$names[0]]) && is_array($this->_values[$this->name][$names[0]])) {
330-
$this->_values[$this->name][$names[0]] = Hash::remove($this->_values[$this->name][$names[0]], $names[1]);
320+
$this->_delete('[' . $key . ']');
321+
} else {
322+
$this->_values[$this->name] = Hash::remove((array)$this->_values[$this->name], $key);
323+
list($key) = explode('.', $key, 2);
324+
if (isset($this->_values[$this->name][$key])) {
325+
$value = $this->_values[$this->name][$key];
326+
$this->_write('[' . $key . ']', $value);
327+
}
331328
}
332-
$this->_delete('[' . implode('][', $names) . ']');
333329
}
334330

335331
/**
@@ -347,14 +343,7 @@ public function destroy() {
347343
}
348344

349345
foreach ($this->_values[$this->name] as $name => $value) {
350-
if (is_array($value)) {
351-
foreach ($value as $key => $val) {
352-
unset($this->_values[$this->name][$name][$key]);
353-
$this->_delete("[$name][$key]");
354-
}
355-
}
356-
unset($this->_values[$this->name][$name]);
357-
$this->_delete("[$name]");
346+
$this->delete($name);
358347
}
359348
}
360349

lib/Cake/Test/Case/Controller/Component/CookieComponentTest.php

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -807,6 +807,89 @@ public function testDeleteChildrenNotExist() {
807807
$this->assertNull($this->Cookie->delete('Not.Found'));
808808
}
809809

810+
/**
811+
* Test deleting deep child elements sends correct cookies.
812+
*
813+
* @return void
814+
*/
815+
public function testDeleteDeepChildren() {
816+
$_COOKIE = array(
817+
'CakeTestCookie' => array(
818+
'foo' => $this->_encrypt(array(
819+
'bar' => array(
820+
'baz' => 'value',
821+
),
822+
)),
823+
),
824+
);
825+
826+
$this->Cookie->delete('foo.bar.baz');
827+
828+
$cookies = $this->Controller->response->cookie();
829+
$expected = array(
830+
'CakeTestCookie[foo]' => array(
831+
'name' => 'CakeTestCookie[foo]',
832+
'value' => '{"bar":[]}',
833+
'path' => '/',
834+
'domain' => '',
835+
'secure' => false,
836+
'httpOnly' => false
837+
),
838+
);
839+
840+
$expires = Hash::combine($cookies, '{*}.name', '{*}.expire');
841+
$cookies = Hash::remove($cookies, '{*}.expire');
842+
$this->assertEquals($expected, $cookies);
843+
844+
$this->assertWithinMargin($expires['CakeTestCookie[foo]'], time() + 10, 2);
845+
}
846+
847+
/**
848+
* Test destroy works.
849+
*
850+
* @return void
851+
*/
852+
public function testDestroy() {
853+
$_COOKIE = array(
854+
'CakeTestCookie' => array(
855+
'foo' => $this->_encrypt(array(
856+
'bar' => array(
857+
'baz' => 'value',
858+
),
859+
)),
860+
'other' => 'value',
861+
),
862+
);
863+
864+
$this->Cookie->destroy();
865+
866+
$cookies = $this->Controller->response->cookie();
867+
$expected = array(
868+
'CakeTestCookie[foo]' => array(
869+
'name' => 'CakeTestCookie[foo]',
870+
'value' => '',
871+
'path' => '/',
872+
'domain' => '',
873+
'secure' => false,
874+
'httpOnly' => false
875+
),
876+
'CakeTestCookie[other]' => array(
877+
'name' => 'CakeTestCookie[other]',
878+
'value' => '',
879+
'path' => '/',
880+
'domain' => '',
881+
'secure' => false,
882+
'httpOnly' => false
883+
),
884+
);
885+
886+
$expires = Hash::combine($cookies, '{*}.name', '{*}.expire');
887+
$cookies = Hash::remove($cookies, '{*}.expire');
888+
$this->assertEquals($expected, $cookies);
889+
$this->assertWithinMargin($expires['CakeTestCookie[foo]'], time() - 42000, 2);
890+
$this->assertWithinMargin($expires['CakeTestCookie[other]'], time() - 42000, 2);
891+
}
892+
810893
/**
811894
* Helper method for generating old style encoded cookie values.
812895
*

0 commit comments

Comments
 (0)