From 7e17da0ae8a81cfde96bef4e7d4be553c487e821 Mon Sep 17 00:00:00 2001 From: mark_story Date: Sat, 11 Feb 2012 11:32:44 -0500 Subject: [PATCH] Fix notice error when reading empty values. When reading empty values a notice error would be triggered. Slicing the first char off and comparing that solves this. Fixes #2537 --- .../Controller/Component/CookieComponent.php | 3 ++- .../Controller/Component/CookieComponentTest.php | 16 ++++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) 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. *