Skip to content

Commit

Permalink
Making Dispatcher::parseParams require a CakeRequest object instead o…
Browse files Browse the repository at this point in the history
…f as string url. Updating tests in the Dispatcher, and fixing an issue where POST params would be wiped out by Router.
  • Loading branch information
markstory committed May 1, 2010
1 parent ffd05ff commit fbd70bf
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 22 deletions.
18 changes: 10 additions & 8 deletions cake/dispatcher.php
Expand Up @@ -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)) {
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion cake/libs/router.php
Expand Up @@ -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;
}
Expand Down
29 changes: 16 additions & 13 deletions cake/tests/cases/dispatcher.test.php
Expand Up @@ -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');
Expand All @@ -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");
}

Expand All @@ -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');
Expand All @@ -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]);
Expand All @@ -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]);
Expand All @@ -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]);
Expand Down

0 comments on commit fbd70bf

Please sign in to comment.