diff --git a/lib/Cake/Core/Object.php b/lib/Cake/Core/Object.php index b450d19615a..43b80bf5746 100644 --- a/lib/Cake/Core/Object.php +++ b/lib/Cake/Core/Object.php @@ -55,8 +55,18 @@ public function toString() { * or tie plugins into a main application. requestAction can be used to return rendered views * or fetch the return value from controller actions. * - * @param mixed $url String or array-based url. - * @param array $extra if array includes the key "return" it sets the AutoRender to true. + * Under the hood this method uses Router::reverse() to convert the $url parmeter into a string + * URL. You should use URL formats that are compatible with Router::reverse() + * + * #### Passing POST and GET data + * + * POST and GET data can be simulated in requestAction. Use `$extra['url']` for + * GET data. The `$extra['data']` parameter allows POST data simulation. + * + * @param mixed $url String or array-based url. Unlike other url arrays in CakePHP, this + * url will not automatically handle passed and named arguments in the $url paramenter. + * @param array $extra if array includes the key "return" it sets the AutoRender to true. Can + * also be used to submit GET/POST data, and named/passed arguments. * @return mixed Boolean true or false on success/failure, or contents * of rendered action if 'return' is set in $extra. */ @@ -74,6 +84,8 @@ public function requestAction($url, $extra = array()) { $extra['url'] = array(); } $extra = array_merge(array('autoRender' => 0, 'return' => 1, 'bare' => 1, 'requested' => 1), $extra); + $data = isset($extra['data']) ? $extra['data'] : null; + unset($extra['data']); if (is_string($url)) { $request = new CakeRequest($url); @@ -81,9 +93,9 @@ public function requestAction($url, $extra = array()) { $params = $url + array('pass' => array(), 'named' => array(), 'base' => false); $params = array_merge($params, $extra); $request = new CakeRequest(Router::reverse($params), false); - if (isset($params['data'])) { - $request->data = $params['data']; - } + } + if (isset($data)) { + $request->data = $data; } $dispatcher = new Dispatcher(); diff --git a/lib/Cake/Test/Case/Core/ObjectTest.php b/lib/Cake/Test/Case/Core/ObjectTest.php index 51ee67bcee5..b02d523bef7 100644 --- a/lib/Cake/Test/Case/Core/ObjectTest.php +++ b/lib/Cake/Test/Case/Core/ObjectTest.php @@ -115,7 +115,7 @@ public function paginate_request_action() { * @return array */ public function post_pass() { - return $this->data; + return $this->request->data; } /** @@ -609,11 +609,12 @@ public function testRequestActionParamParseAndPass() { } /** - * test requestAction and POST parameter passing, and not passing when url is an array. + * test that requestAction does not fish data out of the POST + * superglobal. * * @return void */ - public function testRequestActionPostPassing() { + public function testRequestActionNoPostPassing() { $_tmp = $_POST; $_POST = array('data' => array( @@ -636,4 +637,26 @@ public function testRequestActionPostPassing() { $_POST = $_tmp; } + +/** + * Test requestAction with post data. + * + * @return void + */ + public function testRequestActionPostWithData() { + $data = array( + 'Post' => array('id' => 2) + ); + $result = $this->object->requestAction( + array('controller' => 'request_action', 'action' => 'post_pass'), + array('data' => $data) + ); + $this->assertEquals($data, $result); + + $result = $this->object->requestAction( + '/request_action/post_pass', + array('data' => $data) + ); + $this->assertEquals($data, $result); + } }