Reworking parameter munging specific to requestAction into requestAction. Updating tests cases for Object. As request->data is an array() not null. And leading / is trimmed off of [url][url].

This commit is contained in:
mark_story 2010-11-14 01:01:11 -05:00
parent 55cc3296ab
commit f02e0483ee
3 changed files with 23 additions and 35 deletions

View file

@ -87,19 +87,8 @@ class Dispatcher {
* are encountered.
*/
public function dispatch(CakeRequest $request, $additionalParams = array()) {
/* Should move to Object::requestAction()
if (is_array($url)) {
$url = $this->_extractParams($url, $additionalParams);
}
if ($url instanceof CakeRequest) {
$request = $url;
} else {
$request = new CakeRequest($url);
}
*/
$this->here = $request->here;
if ($this->asset($request->url) || $this->cached($request->url)) {
return;
}
@ -180,22 +169,6 @@ class Dispatcher {
$response->send();
}
/**
* Sets the params when $url is passed as an array to Object::requestAction();
* Merges the $url and $additionalParams and creates a string url.
*
* @param array $url Array or request parameters
* @param array $additionalParams Array of additional parameters.
* @return string $url The generated url string.
*/
protected function _extractParams($url, $additionalParams = array()) {
$defaults = array('pass' => array(), 'named' => array(), 'form' => array());
$params = array_merge($defaults, $url, $additionalParams);
$params += array('base' => false, 'url' => array());
return ltrim(Router::reverse($params), '/');
}
/**
* Returns array of GET and POST parameters. GET parameters are taken from given URL.
*

View file

@ -67,7 +67,7 @@ class Object {
return false;
}
if (!class_exists('dispatcher')) {
require CAKE . 'dispatcher.php';
require LIBS . 'dispatcher.php';
}
if (in_array('return', $extra, true)) {
$extra = array_merge($extra, array('return' => 0, 'autoRender' => 1));
@ -75,9 +75,21 @@ class Object {
if (is_array($url) && !isset($extra['url'])) {
$extra['url'] = array();
}
$params = array_merge(array('autoRender' => 0, 'return' => 1, 'bare' => 1, 'requested' => 1), $extra);
$dispatcher = new Dispatcher;
return $dispatcher->dispatch($url, $params);
$extra = array_merge(array('autoRender' => 0, 'return' => 1, 'bare' => 1, 'requested' => 1), $extra);
if (is_string($url)) {
$request = new CakeRequest($url);
} elseif (is_array($url)) {
$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'];
}
}
$dispatcher = new Dispatcher();
return $dispatcher->dispatch($request, $extra);
}
/**

View file

@ -716,7 +716,7 @@ class ObjectTest extends CakeTestCase {
$result = $this->object->requestAction('/request_action/normal_request_action');
$expected = 'Hello World';
$this->assertEqual($result, $expected);
App::build();
}
@ -824,7 +824,7 @@ class ObjectTest extends CakeTestCase {
function testRequestActionParamParseAndPass() {
$result = $this->object->requestAction('/request_action/params_pass');
$this->assertTrue(isset($result['url']['url']));
$this->assertEqual($result['url']['url'], '/request_action/params_pass');
$this->assertEqual($result['url']['url'], 'request_action/params_pass');
$this->assertEqual($result['controller'], 'request_action');
$this->assertEqual($result['action'], 'params_pass');
$this->assertEqual($result['form'], array());
@ -855,9 +855,12 @@ class ObjectTest extends CakeTestCase {
));
$result = $this->object->requestAction(array('controller' => 'request_action', 'action' => 'post_pass'));
$expected = null;
$this->assertEqual($expected, $result);
$this->assertEmpty($result);
$result = $this->object->requestAction(array('controller' => 'request_action', 'action' => 'post_pass'), array('data' => $_POST['data']));
$result = $this->object->requestAction(
array('controller' => 'request_action', 'action' => 'post_pass'),
array('data' => $_POST['data'])
);
$expected = $_POST['data'];
$this->assertEqual($expected, $result);