Skip to content

Commit

Permalink
Fixing tests + adding tests for passed args order preservation when p…
Browse files Browse the repository at this point in the history
…assed args are part of default route parameters.
  • Loading branch information
markstory committed Nov 30, 2009
1 parent e41c408 commit 11dd789
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 11 deletions.
29 changes: 20 additions & 9 deletions cake/libs/router.php
Expand Up @@ -242,7 +242,7 @@ function getNamedExpressions() {
* {{{
* Router::connect(
* '/:lang/:controller/:action/:id',
* array('controller' => 'testing4'),
* array(),
* array('id' => '[0-9]+', 'lang' => '[a-z]{3}')
* );
* }}}
Expand Down Expand Up @@ -1354,45 +1354,56 @@ function match($url) {
if (!$this->compiled()) {
$this->compile();
}
$url += $this->defaults;

$defaults = $this->defaults;
$url += $defaults;

if (isset($defaults['prefix'])) {
$prefix = $defaults['prefix'];
unset($defaults['prefix']);
}

//check that all the key names are in the url
$keyNames = array_flip($this->keys);
if (array_intersect_key($keyNames, $url) != $keyNames) {
return false;
}
$diff = Set::diff($url, $this->defaults);
$diff = Set::diff($url, $defaults);

//if a not a greedy route, no extra params are allowed.
if (!$this->_greedy && array_keys($diff) != $this->keys) {
return false;
}

//if the difference between the url and defaults contains keys from defaults its not a match
if (array_intersect_key($diff, $this->defaults) !== array()) {
if (array_intersect_key(array_filter($defaults), $diff) !== array()) {
return false;
}

//check that required passed parameters are the same.
$i = 0;
while (isset($this->defaults[$i])) {
if (isset($url[$i]) && $this->defaults[$i] !== $url[$i]) {
while (isset($defaults[$i])) {
if (isset($url[$i]) && $defaults[$i] !== $url[$i]) {
return false;
}
$i++;
}
$passedArgsAndParams = array_diff_key($diff, $this->defaults, $keyNames);
$passedArgsAndParams = array_diff_key($diff, $defaults, $keyNames);
list($named, $params) = Router::getNamedElements($passedArgsAndParams, $url['controller'], $url['action']);

//remove any pass params, they have numeric indexes
//remove any pass params, they have numeric indexes, skip any params that are in the defaults
$pass = array();
$i = 0;
while (isset($url[$i])) {
if (!isset($diff[$i])) {
$i++;
continue;
}
$pass[] = $url[$i];
unset($url[$i]);
$i++;
}


//check patterns for routed params
if (!empty($this->params)) {
foreach ($this->params as $key => $pattern) {
Expand Down
12 changes: 10 additions & 2 deletions cake/tests/cases/libs/router.test.php
Expand Up @@ -2181,7 +2181,7 @@ function testMatchBasic() {

$result = $route->match(array('controller' => 'nodes', 'action' => 'view'));
$this->assertFalse($result);

$result = $route->match(array('controller' => 'posts', 'action' => 'view', 1));
$this->assertFalse($result);

Expand Down Expand Up @@ -2218,7 +2218,7 @@ function testMatchBasic() {
*
* @return void
*/
function testMatchWithNamedParameters() {
function testMatchWithNamedParametersAndPassedArgs() {
Router::connectNamed(true);

$route = new RouterRoute('/:controller/:action/*', array('plugin' => null));
Expand All @@ -2230,6 +2230,14 @@ function testMatchWithNamedParameters() {

$result = $route->match(array('controller' => 'posts', 'action' => 'view', 'plugin' => null, 5, 'page' => 1, 'limit' => 20, 'order' => 'title'));
$this->assertEqual($result, '/posts/view/5/page:1/limit:20/order:title');


$route =& new RouterRoute('/test2/*', array('controller' => 'pages', 'action' => 'display', 2));
$result = $route->match(array('controller' => 'pages', 'action' => 'display', 1));
$this->assertFalse($result);

$result = $route->match(array('controller' => 'pages', 'action' => 'display', 2, 'something'));
$this->assertEqual($result, '/test2/something');
}

/**
Expand Down

0 comments on commit 11dd789

Please sign in to comment.