From b896127099089c1394c3df66d5112281aefa554e Mon Sep 17 00:00:00 2001 From: Mark Story Date: Wed, 6 May 2015 22:06:39 -0400 Subject: [PATCH] Fix incorrect handling of request data. Request data that is invalid should not be parsed into null. Instead it should always be cast as an array. This prevents other parts of the framework from failing. For example patchEntity() will emit errors when passed non-array data. --- .../Component/RequestHandlerComponent.php | 4 +-- .../Component/RequestHandlerComponentTest.php | 33 +++++++++++++++++++ 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/src/Controller/Component/RequestHandlerComponent.php b/src/Controller/Component/RequestHandlerComponent.php index db63ecc811c..0f051db6c30 100644 --- a/src/Controller/Component/RequestHandlerComponent.php +++ b/src/Controller/Component/RequestHandlerComponent.php @@ -240,8 +240,8 @@ public function startup(Event $event) foreach ($this->_inputTypeMap as $type => $handler) { if ($this->requestedWith($type)) { - $input = call_user_func_array([$controller->request, 'input'], $handler); - $request->data = $input; + $input = call_user_func_array([$request, 'input'], $handler); + $request->data = (array)$input; } } } diff --git a/tests/TestCase/Controller/Component/RequestHandlerComponentTest.php b/tests/TestCase/Controller/Component/RequestHandlerComponentTest.php index aa98e5a4c6d..d96ed8ccdc9 100644 --- a/tests/TestCase/Controller/Component/RequestHandlerComponentTest.php +++ b/tests/TestCase/Controller/Component/RequestHandlerComponentTest.php @@ -473,6 +473,39 @@ public function testStartupCallbackCharset() $this->assertFalse(is_object($this->Controller->request->data)); } + /** + * Test that processing data results in an array. + * + * @return void + * @triggers Controller.startup $this->Controller + */ + public function testStartupProcessData() + { + $this->Controller->request = $this->getMock('Cake\Network\Request', ['_readInput']); + $this->Controller->request->expects($this->at(0)) + ->method('_readInput') + ->will($this->returnValue('')); + $this->Controller->request->expects($this->at(1)) + ->method('_readInput') + ->will($this->returnValue('"invalid"')); + $this->Controller->request->expects($this->at(2)) + ->method('_readInput') + ->will($this->returnValue('{"valid":true}')); + + $this->Controller->request->env('REQUEST_METHOD', 'POST'); + $this->Controller->request->env('CONTENT_TYPE', 'application/json'); + + $event = new Event('Controller.startup', $this->Controller); + $this->RequestHandler->startup($event); + $this->assertEquals([], $this->Controller->request->data); + + $this->RequestHandler->startup($event); + $this->assertEquals(['invalid'], $this->Controller->request->data); + + $this->RequestHandler->startup($event); + $this->assertEquals(['valid' => true], $this->Controller->request->data); + } + /** * Test mapping a new type and having startup process it. *