Making Dispatcher::parseParams require a CakeRequest object instead of as string url. Updating tests in the Dispatcher, and fixing an issue where POST params would be wiped out by Router.

This commit is contained in:
Mark Story 2010-05-01 10:48:30 -04:00
parent ffd05ffc3e
commit fbd70bf23c
3 changed files with 28 additions and 23 deletions

View file

@ -90,19 +90,21 @@ class Dispatcher extends Object {
* the form of Missing Controllers information. It does the same with Actions (methods of Controllers are called * the form of Missing Controllers information. It does the same with Actions (methods of Controllers are called
* Actions). * Actions).
* *
* @param string $url URL information to work on * @param mixed $url Either a string url or a CakeRequest object information to work on. If $url is a string
* It will be used to create the request object.
* @param array $additionalParams Settings array ("bare", "return") which is melded with the GET and POST params * @param array $additionalParams Settings array ("bare", "return") which is melded with the GET and POST params
* @return boolean Success * @return boolean Success
*/ */
public function dispatch($url = null, $additionalParams = array()) { public function dispatch($url = null, $additionalParams = array()) {
if (is_array($url)) { if (is_array($url)) {
$url = $this->_extractParams($url, $additionalParams); $url = $this->_extractParams($url, $additionalParams);
}
if ($url instanceof CakeRequest) {
$request = $url;
} else { } else {
if ($url) { $request = new CakeRequest($url);
$_GET['url'] = $url;
} }
} $request = $this->parseParams($request, $additionalParams);
$request = $this->parseParams($url, $additionalParams);
$this->params = $request; $this->params = $request;
if ($this->asset($request->url) || $this->cached($request->url)) { if ($this->asset($request->url) || $this->cached($request->url)) {
@ -229,15 +231,15 @@ class Dispatcher extends Object {
/** /**
* Returns array of GET and POST parameters. GET parameters are taken from given URL. * Returns array of GET and POST parameters. GET parameters are taken from given URL.
* *
* @param string $fromUrl URL to mine for parameter information. * @param CakeRequest $fromUrl CakeRequest object to mine for parameter information.
* @return array Parameters found in POST and GET. * @return array Parameters found in POST and GET.
*/ */
public function parseParams($fromUrl, $additionalParams = array()) { public function parseParams(CakeRequest $request, $additionalParams = array()) {
$namedExpressions = Router::getNamedExpressions(); $namedExpressions = Router::getNamedExpressions();
extract($namedExpressions); extract($namedExpressions);
include CONFIGS . 'routes.php'; include CONFIGS . 'routes.php';
$request = Router::parse(new CakeRequest()); $request = Router::parse($request);
if (!empty($additionalParams)) { if (!empty($additionalParams)) {
$request->params = array_merge($request->params, $additionalParams); $request->params = array_merge($request->params, $additionalParams);

View file

@ -486,7 +486,7 @@ class Router {
$out['url']['ext'] = $ext; $out['url']['ext'] = $ext;
} }
$request->params = $out; $request->params = array_merge($request->params, $out);
return $request; return $request;
} }

View file

@ -579,8 +579,8 @@ class DispatcherTest extends CakeTestCase {
* @return void * @return void
*/ */
public function testParseParamsWithoutZerosAndEmptyPost() { public function testParseParamsWithoutZerosAndEmptyPost() {
$Dispatcher =& new Dispatcher(); $Dispatcher = new Dispatcher();
$test = $Dispatcher->parseParams("/testcontroller/testaction/params1/params2/params3"); $test = $Dispatcher->parseParams(new CakeRequest("/testcontroller/testaction/params1/params2/params3"));
$this->assertIdentical($test['controller'], 'testcontroller'); $this->assertIdentical($test['controller'], 'testcontroller');
$this->assertIdentical($test['action'], 'testaction'); $this->assertIdentical($test['action'], 'testaction');
$this->assertIdentical($test['pass'][0], 'params1'); $this->assertIdentical($test['pass'][0], 'params1');
@ -596,9 +596,10 @@ class DispatcherTest extends CakeTestCase {
*/ */
public function testParseParamsReturnsPostedData() { public function testParseParamsReturnsPostedData() {
$_POST['testdata'] = "My Posted Content"; $_POST['testdata'] = "My Posted Content";
$Dispatcher =& new Dispatcher(); $Dispatcher = new Dispatcher();
$test = $Dispatcher->parseParams("/");
$this->assertTrue($test['form'], "Parsed URL not returning post data"); $test = $Dispatcher->parseParams(new CakeRequest("/"));
$this->assertTrue(isset($test['form']), "Parsed URL not returning post data");
$this->assertIdentical($test['form']['testdata'], "My Posted Content"); $this->assertIdentical($test['form']['testdata'], "My Posted Content");
} }
@ -608,8 +609,8 @@ class DispatcherTest extends CakeTestCase {
* @return void * @return void
*/ */
public function testParseParamsWithSingleZero() { public function testParseParamsWithSingleZero() {
$Dispatcher =& new Dispatcher(); $Dispatcher = new Dispatcher();
$test = $Dispatcher->parseParams("/testcontroller/testaction/1/0/23"); $test = $Dispatcher->parseParams(new CakeRequest("/testcontroller/testaction/1/0/23"));
$this->assertIdentical($test['controller'], 'testcontroller'); $this->assertIdentical($test['controller'], 'testcontroller');
$this->assertIdentical($test['action'], 'testaction'); $this->assertIdentical($test['action'], 'testaction');
$this->assertIdentical($test['pass'][0], '1'); $this->assertIdentical($test['pass'][0], '1');
@ -623,8 +624,8 @@ class DispatcherTest extends CakeTestCase {
* @return void * @return void
*/ */
public function testParseParamsWithManySingleZeros() { public function testParseParamsWithManySingleZeros() {
$Dispatcher =& new Dispatcher(); $Dispatcher = new Dispatcher();
$test = $Dispatcher->parseParams("/testcontroller/testaction/0/0/0/0/0/0"); $test = $Dispatcher->parseParams(new CakeRequest("/testcontroller/testaction/0/0/0/0/0/0"));
$this->assertPattern('/\\A(?:0)\\z/', $test['pass'][0]); $this->assertPattern('/\\A(?:0)\\z/', $test['pass'][0]);
$this->assertPattern('/\\A(?:0)\\z/', $test['pass'][1]); $this->assertPattern('/\\A(?:0)\\z/', $test['pass'][1]);
$this->assertPattern('/\\A(?:0)\\z/', $test['pass'][2]); $this->assertPattern('/\\A(?:0)\\z/', $test['pass'][2]);
@ -639,8 +640,9 @@ class DispatcherTest extends CakeTestCase {
* @return void * @return void
*/ */
public function testParseParamsWithManyZerosInEachSectionOfUrl() { public function testParseParamsWithManyZerosInEachSectionOfUrl() {
$Dispatcher =& new Dispatcher(); $Dispatcher = new Dispatcher();
$test = $Dispatcher->parseParams("/testcontroller/testaction/000/0000/00000/000000/000000/0000000"); $request = new CakeRequest("/testcontroller/testaction/000/0000/00000/000000/000000/0000000");
$test = $Dispatcher->parseParams($request);
$this->assertPattern('/\\A(?:000)\\z/', $test['pass'][0]); $this->assertPattern('/\\A(?:000)\\z/', $test['pass'][0]);
$this->assertPattern('/\\A(?:0000)\\z/', $test['pass'][1]); $this->assertPattern('/\\A(?:0000)\\z/', $test['pass'][1]);
$this->assertPattern('/\\A(?:00000)\\z/', $test['pass'][2]); $this->assertPattern('/\\A(?:00000)\\z/', $test['pass'][2]);
@ -655,8 +657,9 @@ class DispatcherTest extends CakeTestCase {
* @return void * @return void
*/ */
public function testParseParamsWithMixedOneToManyZerosInEachSectionOfUrl() { public function testParseParamsWithMixedOneToManyZerosInEachSectionOfUrl() {
$Dispatcher =& new Dispatcher(); $Dispatcher = new Dispatcher();
$test = $Dispatcher->parseParams("/testcontroller/testaction/01/0403/04010/000002/000030/0000400"); $request = new CakeRequest("/testcontroller/testaction/01/0403/04010/000002/000030/0000400");
$test = $Dispatcher->parseParams($request);
$this->assertPattern('/\\A(?:01)\\z/', $test['pass'][0]); $this->assertPattern('/\\A(?:01)\\z/', $test['pass'][0]);
$this->assertPattern('/\\A(?:0403)\\z/', $test['pass'][1]); $this->assertPattern('/\\A(?:0403)\\z/', $test['pass'][1]);
$this->assertPattern('/\\A(?:04010)\\z/', $test['pass'][2]); $this->assertPattern('/\\A(?:04010)\\z/', $test['pass'][2]);