Merge pull request #11283 from chinpei215/2.x-cookie-component-1

[2.x] Fix fatal error thrown when replacing scalar with array
This commit is contained in:
Mark Story 2017-10-06 16:45:38 -04:00 committed by GitHub
commit a71cad0420
2 changed files with 23 additions and 18 deletions

View file

@ -229,27 +229,14 @@ class CookieComponent extends Component {
} }
foreach ($key as $name => $value) { foreach ($key as $name => $value) {
$names = array($name);
if (strpos($name, '.') !== false) { if (strpos($name, '.') !== false) {
$names = explode('.', $name, 2); $this->_values[$this->name] = Hash::insert($this->_values[$this->name], $name, $value);
} list($name) = explode('.', $name, 2);
$firstName = $names[0]; $value = $this->_values[$this->name][$name];
$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
);
} else { } 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; $this->_encrypted = true;
} }

View file

@ -429,6 +429,24 @@ class CookieComponentTest extends CakeTestCase {
$this->assertEquals($expected, $result); $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 * testReadingCookieValue
* *