Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Fix use of data()
Fix a type error in getData() when request->data is invalid.
  • Loading branch information
markstory committed Feb 3, 2017
1 parent 46fc1d1 commit dec61b9
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/Controller/Component/SecurityComponent.php
Expand Up @@ -265,7 +265,7 @@ protected function _authRequired(Controller $controller)
$requireAuth = $this->_config['requireAuth'];

if (in_array($request->getParam('action'), $requireAuth) || $requireAuth == ['*']) {
if (!isset($request->data['_Token'])) {
if ($request->getData('_Token') === null) {
throw new AuthSecurityException('\'_Token\' was not found in request data.');
}

Expand Down
3 changes: 3 additions & 0 deletions src/Http/ServerRequest.php
Expand Up @@ -1489,6 +1489,9 @@ public function getData($name = null, $default = null)
if ($name === null) {
return $this->data;
}
if (!is_array($this->data) && $name) {
return $default;
}

return Hash::get($this->data, $name, $default);
}
Expand Down
15 changes: 14 additions & 1 deletion tests/TestCase/Network/RequestTest.php
Expand Up @@ -2520,7 +2520,7 @@ public function testReadingParams()
*
* @return void
*/
public function testDataReading()
public function testGetData()
{
$post = [
'Model' => [
Expand All @@ -2541,6 +2541,19 @@ public function testDataReading()
$this->assertSame('default', $request->getData('Model.imaginary', 'default'));
}

/**
* Test that getData() doesn't fail on scalar data.
*
* @return void
*/
public function testGetDataOnStringData()
{
$post = 'strange, but could happen';
$request = new Request(compact('post'));
$this->assertNull($request->getData('Model'));
$this->assertNull($request->getData('Model.field'));
}

/**
* Test writing with data()
*
Expand Down

0 comments on commit dec61b9

Please sign in to comment.