Skip to content

Commit 9347ca2

Browse files
committed
Fix requestAction post simulation.
POST data should not trigger notice errors. Also fix issue where POST data could not be simulated with a string URL. Fixes #2248
1 parent a7fcb0a commit 9347ca2

File tree

2 files changed

+43
-8
lines changed

2 files changed

+43
-8
lines changed

lib/Cake/Core/Object.php

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,18 @@ public function toString() {
5555
* or tie plugins into a main application. requestAction can be used to return rendered views
5656
* or fetch the return value from controller actions.
5757
*
58-
* @param mixed $url String or array-based url.
59-
* @param array $extra if array includes the key "return" it sets the AutoRender to true.
58+
* Under the hood this method uses Router::reverse() to convert the $url parmeter into a string
59+
* URL. You should use URL formats that are compatible with Router::reverse()
60+
*
61+
* #### Passing POST and GET data
62+
*
63+
* POST and GET data can be simulated in requestAction. Use `$extra['url']` for
64+
* GET data. The `$extra['data']` parameter allows POST data simulation.
65+
*
66+
* @param mixed $url String or array-based url. Unlike other url arrays in CakePHP, this
67+
* url will not automatically handle passed and named arguments in the $url paramenter.
68+
* @param array $extra if array includes the key "return" it sets the AutoRender to true. Can
69+
* also be used to submit GET/POST data, and named/passed arguments.
6070
* @return mixed Boolean true or false on success/failure, or contents
6171
* of rendered action if 'return' is set in $extra.
6272
*/
@@ -74,16 +84,18 @@ public function requestAction($url, $extra = array()) {
7484
$extra['url'] = array();
7585
}
7686
$extra = array_merge(array('autoRender' => 0, 'return' => 1, 'bare' => 1, 'requested' => 1), $extra);
87+
$data = isset($extra['data']) ? $extra['data'] : null;
88+
unset($extra['data']);
7789

7890
if (is_string($url)) {
7991
$request = new CakeRequest($url);
8092
} elseif (is_array($url)) {
8193
$params = $url + array('pass' => array(), 'named' => array(), 'base' => false);
8294
$params = array_merge($params, $extra);
8395
$request = new CakeRequest(Router::reverse($params), false);
84-
if (isset($params['data'])) {
85-
$request->data = $params['data'];
86-
}
96+
}
97+
if (isset($data)) {
98+
$request->data = $data;
8799
}
88100

89101
$dispatcher = new Dispatcher();

lib/Cake/Test/Case/Core/ObjectTest.php

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ public function paginate_request_action() {
115115
* @return array
116116
*/
117117
public function post_pass() {
118-
return $this->data;
118+
return $this->request->data;
119119
}
120120

121121
/**
@@ -609,11 +609,12 @@ public function testRequestActionParamParseAndPass() {
609609
}
610610

611611
/**
612-
* test requestAction and POST parameter passing, and not passing when url is an array.
612+
* test that requestAction does not fish data out of the POST
613+
* superglobal.
613614
*
614615
* @return void
615616
*/
616-
public function testRequestActionPostPassing() {
617+
public function testRequestActionNoPostPassing() {
617618
$_tmp = $_POST;
618619

619620
$_POST = array('data' => array(
@@ -636,4 +637,26 @@ public function testRequestActionPostPassing() {
636637

637638
$_POST = $_tmp;
638639
}
640+
641+
/**
642+
* Test requestAction with post data.
643+
*
644+
* @return void
645+
*/
646+
public function testRequestActionPostWithData() {
647+
$data = array(
648+
'Post' => array('id' => 2)
649+
);
650+
$result = $this->object->requestAction(
651+
array('controller' => 'request_action', 'action' => 'post_pass'),
652+
array('data' => $data)
653+
);
654+
$this->assertEquals($data, $result);
655+
656+
$result = $this->object->requestAction(
657+
'/request_action/post_pass',
658+
array('data' => $data)
659+
);
660+
$this->assertEquals($data, $result);
661+
}
639662
}

0 commit comments

Comments
 (0)