diff --git a/lib/Cake/Controller/Component/CookieComponent.php b/lib/Cake/Controller/Component/CookieComponent.php index cea188349..3abb24ddc 100644 --- a/lib/Cake/Controller/Component/CookieComponent.php +++ b/lib/Cake/Controller/Component/CookieComponent.php @@ -483,7 +483,8 @@ class CookieComponent extends Component { * @return array Map of key and values */ protected function _explode($string) { - if ($string[0] === '{' || $string[0] === '[') { + $first = substr($string, 0, 1); + if ($first !== false && $first === '{' || $first === '[') { $ret = json_decode($string, true); return ($ret != null) ? $ret : $string; } diff --git a/lib/Cake/Test/Case/Controller/Component/CookieComponentTest.php b/lib/Cake/Test/Case/Controller/Component/CookieComponentTest.php index 56fe5b645..701973dab 100644 --- a/lib/Cake/Test/Case/Controller/Component/CookieComponentTest.php +++ b/lib/Cake/Test/Case/Controller/Component/CookieComponentTest.php @@ -471,6 +471,21 @@ class CookieComponentTest extends CakeTestCase { $this->assertEquals($expected, $result); } +/** + * Test reading empty values. + */ + public function testReadEmpty() { + $_COOKIE['CakeTestCookie'] = array( + 'JSON' => '{"name":"value"}', + 'Empty' => '', + 'String' => '{"somewhat:"broken"}' + ); + $this->assertEqual(array('name' => 'value'), $this->Cookie->read('JSON')); + $this->assertEqual('value', $this->Cookie->read('JSON.name')); + $this->assertEqual('', $this->Cookie->read('Empty')); + $this->assertEqual('{"somewhat:"broken"}', $this->Cookie->read('String')); + } + /** * test that no error is issued for non array data. * @@ -483,6 +498,7 @@ class CookieComponentTest extends CakeTestCase { $this->assertNull($this->Cookie->read('value')); } + /** * test that deleting a top level keys kills the child elements too. *