From fbd70bf23cc16b12b10367d9eedc5574ab810e05 Mon Sep 17 00:00:00 2001 From: Mark Story Date: Sat, 1 May 2010 10:48:30 -0400 Subject: [PATCH] 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. --- cake/dispatcher.php | 18 +++++++++-------- cake/libs/router.php | 2 +- cake/tests/cases/dispatcher.test.php | 29 +++++++++++++++------------- 3 files changed, 27 insertions(+), 22 deletions(-) diff --git a/cake/dispatcher.php b/cake/dispatcher.php index 2a72cbcccea..6e54a0a00cb 100644 --- a/cake/dispatcher.php +++ b/cake/dispatcher.php @@ -90,19 +90,21 @@ public function __construct($url = null, $base = false) { * the form of Missing Controllers information. It does the same with Actions (methods of Controllers are called * 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 * @return boolean Success */ public function dispatch($url = null, $additionalParams = array()) { if (is_array($url)) { $url = $this->_extractParams($url, $additionalParams); + } + if ($url instanceof CakeRequest) { + $request = $url; } else { - if ($url) { - $_GET['url'] = $url; - } + $request = new CakeRequest($url); } - $request = $this->parseParams($url, $additionalParams); + $request = $this->parseParams($request, $additionalParams); $this->params = $request; if ($this->asset($request->url) || $this->cached($request->url)) { @@ -229,15 +231,15 @@ protected function _extractParams($url, $additionalParams = array()) { /** * 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. */ - public function parseParams($fromUrl, $additionalParams = array()) { + public function parseParams(CakeRequest $request, $additionalParams = array()) { $namedExpressions = Router::getNamedExpressions(); extract($namedExpressions); include CONFIGS . 'routes.php'; - $request = Router::parse(new CakeRequest()); + $request = Router::parse($request); if (!empty($additionalParams)) { $request->params = array_merge($request->params, $additionalParams); diff --git a/cake/libs/router.php b/cake/libs/router.php index c7201d140c4..3bc1e6b2459 100644 --- a/cake/libs/router.php +++ b/cake/libs/router.php @@ -486,7 +486,7 @@ public static function parse($request) { $out['url']['ext'] = $ext; } - $request->params = $out; + $request->params = array_merge($request->params, $out); return $request; } diff --git a/cake/tests/cases/dispatcher.test.php b/cake/tests/cases/dispatcher.test.php index 3bf06e87775..09d63ef885c 100644 --- a/cake/tests/cases/dispatcher.test.php +++ b/cake/tests/cases/dispatcher.test.php @@ -579,8 +579,8 @@ public function endTest() { * @return void */ public function testParseParamsWithoutZerosAndEmptyPost() { - $Dispatcher =& new Dispatcher(); - $test = $Dispatcher->parseParams("/testcontroller/testaction/params1/params2/params3"); + $Dispatcher = new Dispatcher(); + $test = $Dispatcher->parseParams(new CakeRequest("/testcontroller/testaction/params1/params2/params3")); $this->assertIdentical($test['controller'], 'testcontroller'); $this->assertIdentical($test['action'], 'testaction'); $this->assertIdentical($test['pass'][0], 'params1'); @@ -596,9 +596,10 @@ public function testParseParamsWithoutZerosAndEmptyPost() { */ public function testParseParamsReturnsPostedData() { $_POST['testdata'] = "My Posted Content"; - $Dispatcher =& new Dispatcher(); - $test = $Dispatcher->parseParams("/"); - $this->assertTrue($test['form'], "Parsed URL not returning post data"); + $Dispatcher = new Dispatcher(); + + $test = $Dispatcher->parseParams(new CakeRequest("/")); + $this->assertTrue(isset($test['form']), "Parsed URL not returning post data"); $this->assertIdentical($test['form']['testdata'], "My Posted Content"); } @@ -608,8 +609,8 @@ public function testParseParamsReturnsPostedData() { * @return void */ public function testParseParamsWithSingleZero() { - $Dispatcher =& new Dispatcher(); - $test = $Dispatcher->parseParams("/testcontroller/testaction/1/0/23"); + $Dispatcher = new Dispatcher(); + $test = $Dispatcher->parseParams(new CakeRequest("/testcontroller/testaction/1/0/23")); $this->assertIdentical($test['controller'], 'testcontroller'); $this->assertIdentical($test['action'], 'testaction'); $this->assertIdentical($test['pass'][0], '1'); @@ -623,8 +624,8 @@ public function testParseParamsWithSingleZero() { * @return void */ public function testParseParamsWithManySingleZeros() { - $Dispatcher =& new Dispatcher(); - $test = $Dispatcher->parseParams("/testcontroller/testaction/0/0/0/0/0/0"); + $Dispatcher = new Dispatcher(); + $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'][1]); $this->assertPattern('/\\A(?:0)\\z/', $test['pass'][2]); @@ -639,8 +640,9 @@ public function testParseParamsWithManySingleZeros() { * @return void */ public function testParseParamsWithManyZerosInEachSectionOfUrl() { - $Dispatcher =& new Dispatcher(); - $test = $Dispatcher->parseParams("/testcontroller/testaction/000/0000/00000/000000/000000/0000000"); + $Dispatcher = new Dispatcher(); + $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(?:0000)\\z/', $test['pass'][1]); $this->assertPattern('/\\A(?:00000)\\z/', $test['pass'][2]); @@ -655,8 +657,9 @@ public function testParseParamsWithManyZerosInEachSectionOfUrl() { * @return void */ public function testParseParamsWithMixedOneToManyZerosInEachSectionOfUrl() { - $Dispatcher =& new Dispatcher(); - $test = $Dispatcher->parseParams("/testcontroller/testaction/01/0403/04010/000002/000030/0000400"); + $Dispatcher = new Dispatcher(); + $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(?:0403)\\z/', $test['pass'][1]); $this->assertPattern('/\\A(?:04010)\\z/', $test['pass'][2]);