diff --git a/lib/Cake/Controller/Component/CookieComponent.php b/lib/Cake/Controller/Component/CookieComponent.php index 4332dac81..5aeb5a365 100644 --- a/lib/Cake/Controller/Component/CookieComponent.php +++ b/lib/Cake/Controller/Component/CookieComponent.php @@ -229,27 +229,14 @@ class CookieComponent extends Component { } foreach ($key as $name => $value) { - $names = array($name); if (strpos($name, '.') !== false) { - $names = explode('.', $name, 2); - } - $firstName = $names[0]; - $isMultiValue = (is_array($value) || count($names) > 1); - - if (!isset($this->_values[$this->name][$firstName]) && $isMultiValue) { - $this->_values[$this->name][$firstName] = array(); - } - - if (count($names) > 1) { - $this->_values[$this->name][$firstName] = Hash::insert( - $this->_values[$this->name][$firstName], - $names[1], - $value - ); + $this->_values[$this->name] = Hash::insert($this->_values[$this->name], $name, $value); + list($name) = explode('.', $name, 2); + $value = $this->_values[$this->name][$name]; } else { - $this->_values[$this->name][$firstName] = $value; + $this->_values[$this->name][$name] = $value; } - $this->_write('[' . $firstName . ']', $this->_values[$this->name][$firstName]); + $this->_write('[' . $name . ']', $value); } $this->_encrypted = true; } diff --git a/lib/Cake/Test/Case/Controller/Component/CookieComponentTest.php b/lib/Cake/Test/Case/Controller/Component/CookieComponentTest.php index c3fddddb3..ea2f4998b 100644 --- a/lib/Cake/Test/Case/Controller/Component/CookieComponentTest.php +++ b/lib/Cake/Test/Case/Controller/Component/CookieComponentTest.php @@ -429,6 +429,24 @@ class CookieComponentTest extends CakeTestCase { $this->assertEquals($expected, $result); } +/** + * Test that replacing scalar with array works. + * + * @return void + */ + public function testReplaceScalarWithArray() { + $this->Cookie->write('foo', 1); + $this->Cookie->write('foo.bar', 2); + + $data = $this->Cookie->read(); + $expected = array( + 'foo' => array( + 'bar' => 2 + ) + ); + $this->assertEquals($expected, $data); + } + /** * testReadingCookieValue *