Permalink
Browse files

Fixing tests + adding tests for passed args order preservation when p…

…assed args are part of default route parameters.
  • Loading branch information...
1 parent e41c408 commit 11dd7898c4c6dfb307a5912d663cab0030e5e78d @markstory markstory committed Nov 30, 2009
Showing with 30 additions and 11 deletions.
  1. +20 −9 cake/libs/router.php
  2. +10 −2 cake/tests/cases/libs/router.test.php
View
@@ -242,7 +242,7 @@ function getNamedExpressions() {
* {{{
* Router::connect(
* '/:lang/:controller/:action/:id',
- * array('controller' => 'testing4'),
+ * array(),
* array('id' => '[0-9]+', 'lang' => '[a-z]{3}')
* );
* }}}
@@ -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) {
@@ -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);
@@ -2218,7 +2218,7 @@ function testMatchBasic() {
*
* @return void
*/
- function testMatchWithNamedParameters() {
+ function testMatchWithNamedParametersAndPassedArgs() {
Router::connectNamed(true);
$route = new RouterRoute('/:controller/:action/*', array('plugin' => null));
@@ -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');
}
/**

0 comments on commit 11dd789

Please sign in to comment.