Skip to content

Commit

Permalink
Expanding tests for passed arguments in route defaults working correc…
Browse files Browse the repository at this point in the history
…tly. Refactoring passed arguments in route default handling.
  • Loading branch information
markstory committed Nov 29, 2009
1 parent 889e367 commit f59b8da
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 27 deletions.
44 changes: 17 additions & 27 deletions cake/libs/router.php
Expand Up @@ -670,7 +670,7 @@ function promote($which = null) {
* Returns an URL pointing to a combination of controller and action. Param
* $url can be:
*
* - Empty - the method will find adress to actuall controller/action.
* - Empty - the method will find address to actuall controller/action.
* - '/' - the method will find base URL of application.
* - A combination of controller/action - the method will find url for it.
*
Expand All @@ -693,7 +693,7 @@ function url($url = null, $full = false) {
if (is_bool($full)) {
$escape = false;
} else {
extract(array_merge(array('escape' => false, 'full' => false), $full));
extract($full + array('escape' => false, 'full' => false));
}

if (!empty($self->__params)) {
Expand Down Expand Up @@ -751,17 +751,15 @@ function url($url = null, $full = false) {
unset($url[$prefix]);
}
}
$plugin = false;
if (array_key_exists('plugin', $url)) {
$plugin = $url['plugin'];
$params['plugin'] = $url['plugin'];
}

$_url = $url;
$url = array_merge(array('controller' => $params['controller'], 'plugin' => $params['plugin']), Set::filter($url, true));

if ($plugin !== false) {
$url['plugin'] = $plugin;
}
$url = array_merge(
array('controller' => $params['controller'], 'plugin' => $params['plugin']),
Set::filter($url, true)
);

if (isset($url['ext'])) {
$extension = '.' . $url['ext'];
Expand All @@ -771,8 +769,6 @@ function url($url = null, $full = false) {

for ($i = 0, $len = count($self->routes); $i < $len; $i++) {
$route =& $self->routes[$i];
$route->compile();

$originalUrl = $url;

if (isset($route->params['persist'], $self->__params[0])) {
Expand Down Expand Up @@ -841,7 +837,7 @@ function url($url = null, $full = false) {
if (!empty($args)) {
$args = implode('/', $args);
if ($output{strlen($output) - 1} != '/') {
$args = '/'. $args;
$args = '/' . $args;
}
$output .= $args;
}
Expand Down Expand Up @@ -944,8 +940,7 @@ function matchNamed($param, $val, $rule, $context = array()) {
if (!$actionMatches) {
return false;
}
$valueMatches = !isset($rule['match']) || preg_match(sprintf('/%s/', $rule['match']), $val);
return $valueMatches;
return (!isset($rule['match']) || preg_match('/' . $rule['match'] . '/', $val));
}

/**
Expand Down Expand Up @@ -1097,10 +1092,7 @@ function getArgs($args, $options = array()) {
$pass = $named = array();
$args = explode('/', $args);

$greedy = $self->named['greedy'];
if (isset($options['greedy'])) {
$greedy = $options['greedy'];
}
$greedy = isset($options['greedy']) ? $options['greedy'] : $self->named['greedy'];
$context = array();
if (isset($options['context'])) {
$context = $options['context'];
Expand Down Expand Up @@ -1363,19 +1355,17 @@ function match($url) {

$pass = array();
$params = Set::diff($url, $defaults);
$urlInv = array_combine(array_values($url), array_keys($url));

$i = 0;
while (isset($defaults[$i])) {
if (isset($urlInv[$defaults[$i]])) {
if (!in_array($defaults[$i], $url) && is_int($urlInv[$defaults[$i]])) {
if (isset($defaults[0])) {
$i = 0;
while (isset($defaults[$i])) {
if (isset($url[$i]) && $defaults[$i] == $url[$i]) {
unset($defaults[$i]);
} else {
return false;
}
unset($urlInv[$defaults[$i]], $defaults[$i]);
} else {
return false;
$i++;
}
$i++;
}

foreach ($params as $key => $value) {
Expand Down
5 changes: 5 additions & 0 deletions cake/tests/cases/libs/router.test.php
Expand Up @@ -1764,6 +1764,7 @@ function testUrlWritingWithPrefixes() {
* @return void
*/
function testPassedArgsOrder() {
Router::connect('/test-passed/*', array('controller' => 'pages', 'action' => 'display', 'home'));
Router::connect('/test2/*', array('controller' => 'pages', 'action' => 'display', 2));
Router::connect('/test/*', array('controller' => 'pages', 'action' => 'display', 1));
Router::parse('/');
Expand All @@ -1776,6 +1777,10 @@ function testPassedArgsOrder() {
$expected = '/test2/whatever';
$this->assertEqual($result, $expected);

$result = Router::url(array('controller' => 'pages', 'action' => 'display', 'home', 'whatever'));
$expected = '/test-passed/whatever';
$this->assertEqual($result, $expected);

Configure::write('Routing.prefixes', array('admin'));
Router::reload();

Expand Down

0 comments on commit f59b8da

Please sign in to comment.