Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Most of RouterRoute::match() working now. Failing tests for params wi…

…th patterns included.
  • Loading branch information...
commit 5999264f1040c410a7dee0f431c25b2ed1e896dc 1 parent de0b90d
@markstory markstory authored
Showing with 53 additions and 25 deletions.
  1. +21 −21 cake/libs/router.php
  2. +32 −4 cake/tests/cases/libs/router.test.php
View
42 cake/libs/router.php
@@ -1354,33 +1354,23 @@ function match($url) {
if (!$this->compiled()) {
$this->compile();
}
-/* $url += array('controller' => null, 'plugin' => null);
-
- $defaults = $this->defaults;
- if (isset($defaults['prefix'])) {
- $prefix = $defaults['prefix'];
- unset($defaults['prefix']);
- }
-
- $diff = Set::diff($url, $defaults);
-
- $url += array('controller' => null, 'plugin' => null);
-
+ $url += $this->defaults;
+
+ //check that all the key names are in the url
$keyNames = array_flip($this->keys);
- if (array_keys(array_intersect_key($url, $keyNames)) != $this->keys) {
+ if (array_intersect_key($keyNames, $url) != $keyNames) {
return false;
}
+ $diff = Set::diff($url, $this->defaults);
- //if the default keys aren't the same its not a match.
- if (array_intersect_key($url, $this->defaults) != $this->defaults) {
+ //if a not a greedy route, no extra params are allowed.
+ if (!$this->_greedy && array_keys($diff) != $this->keys) {
return false;
}
- //if this route is not greedy, make sure there are no more params
- if (!$this->_greedy) {
- if (array_diff_key($url, array_merge($this->defaults, $keyNames)) !== array()) {
- 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()) {
+ return false;
}
//check that required passed parameters are the same.
@@ -1391,6 +1381,9 @@ function match($url) {
}
$i++;
}
+ $passedArgsAndParams = array_diff_key($diff, $this->defaults, $keyNames);
+ list($named, $params) = Router::getNamedElements($passedArgsAndParams, $url['controller'], $url['action']);
+
//remove any pass params, they have numeric indexes
$pass = array();
@@ -1400,10 +1393,11 @@ function match($url) {
unset($url[$i]);
$i++;
}
-
return $this->_writeUrl(array_merge($url, compact('pass', 'named', 'prefix')));
+
//*/
+/*
$defaults = $this->defaults;
if (isset($defaults['prefix'])) {
@@ -1432,7 +1426,12 @@ function match($url) {
unset($params[$key]);
}
}
+ debug($params);
list($named, $params) = Router::getNamedElements($params);
+ debug($named);
+ debug($params);
+ debug($this);
+ echo '-----------<br>';
if (!strpos($this->template, '*') && (!empty($pass) || !empty($named))) {
return false;
@@ -1548,6 +1547,7 @@ function _writeUrl($params) {
if (strpos($this->template, '*')) {
$out = str_replace('*', $params['pass'], $out);
}
+ $out = str_replace('//', '/', $out);
return $out;
}
}
View
36 cake/tests/cases/libs/router.test.php
@@ -1945,11 +1945,11 @@ function testRouterConnectDefaults() {
$result = Router::parse('/posts/edit/5');
$this->assertFalse(isset($result['controller']));
$this->assertFalse(isset($result['action']));
-
}
}
-//SimpleTest::ignore('RouterTest');
+SimpleTest::ignore('RouterTest');
+// SimpleTest::ignore('RouterRouteTestCase');
/**
* Test case for RouterRoute
*
@@ -2149,10 +2149,16 @@ function testComplexRouteCompilingAndParsing() {
* @return void
**/
function testMatchBasic() {
- $route = new RouterRoute('/:controller/:action/:id');
- $result = $route->match(array('controller' => 'posts', 'action' => 'view'));
+ $route = new RouterRoute('/:controller/:action/:id', array('plugin' => null));
+ $result = $route->match(array('controller' => 'posts', 'action' => 'view', 'plugin' => null));
+ $this->assertFalse($result);
+
+ $result = $route->match(array('plugin' => null, 'controller' => 'posts', 'action' => 'view', 0));
$this->assertFalse($result);
+ $result = $route->match(array('plugin' => null, 'controller' => 'posts', 'action' => 'view', 'id' => 1));
+ $this->assertEqual($result, '/posts/view/1');
+
$route =& new RouterRoute('/', array('controller' => 'pages', 'action' => 'display', 'home'));
$result = $route->match(array('controller' => 'pages', 'action' => 'display', 'home'));
$this->assertEqual($result, '/');
@@ -2173,6 +2179,9 @@ function testMatchBasic() {
$result = $route->match(array('controller' => 'posts', 'action' => 'view'));
$this->assertEqual($result, '/blog/view');
+ $result = $route->match(array('controller' => 'nodes', 'action' => 'view'));
+ $this->assertFalse($result);
+
$result = $route->match(array('controller' => 'posts', 'action' => 'view', 1));
$this->assertFalse($result);
@@ -2205,6 +2214,25 @@ function testMatchBasic() {
}
/**
+ * test match() with greedy routes, named parameters and passed args.
+ *
+ * @return void
+ */
+ function testMatchWithNamedParameters() {
+ Router::connectNamed(true);
+
+ $route = new RouterRoute('/:controller/:action/*', array('plugin' => null));
+ $result = $route->match(array('controller' => 'posts', 'action' => 'index', 'plugin' => null, 'page' => 1));
+ $this->assertEqual($result, '/posts/index/page:1');
+
+ $result = $route->match(array('controller' => 'posts', 'action' => 'view', 'plugin' => null, 5));
+ $this->assertEqual($result, '/posts/view/5');
+
+ $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');
+ }
+
+/**
* test that match with patterns works.
*
* @return void
Please sign in to comment.
Something went wrong with that request. Please try again.