Skip to content

Commit

Permalink
Fix notice error when reading empty values.
Browse files Browse the repository at this point in the history
When reading empty values a notice error would be triggered.
Slicing the first char off and comparing that solves this.

Fixes #2537
  • Loading branch information
markstory committed Feb 11, 2012
1 parent 6f91417 commit 7e17da0
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/Cake/Controller/Component/CookieComponent.php
Expand Up @@ -483,7 +483,8 @@ protected function _implode(array $array) {
* @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;
}
Expand Down
16 changes: 16 additions & 0 deletions lib/Cake/Test/Case/Controller/Component/CookieComponentTest.php
Expand Up @@ -471,6 +471,21 @@ public function testReadLegacyCookieValue() {
$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.
*
Expand All @@ -483,6 +498,7 @@ public function testNoErrorOnNonArrayData() {
$this->assertNull($this->Cookie->read('value'));
}


/**
* test that deleting a top level keys kills the child elements too.
*
Expand Down

0 comments on commit 7e17da0

Please sign in to comment.