Skip to content

Commit

Permalink
Fixing persistent param route exiting.
Browse files Browse the repository at this point in the history
Adding test for exiting persist param routes.
  • Loading branch information
markstory committed Nov 26, 2009
1 parent 35fac16 commit 52264cb
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
10 changes: 8 additions & 2 deletions cake/libs/router.php
Expand Up @@ -844,7 +844,13 @@ function url($url = null, $full = false) {
$originalUrl = $url;

if (isset($route->params['persist'], $_this->__params[0])) {
$url = array_merge(array_intersect_key($params, Set::combine($route->params['persist'], '/')), $url);
foreach ($route->params['persist'] as $persistKey) {
if (array_key_exists($persistKey, $_url)) {
$url[$persistKey] = $_url[$persistKey];
} elseif (array_key_exists($persistKey, $params)) {
$url[$persistKey] = $params[$persistKey];
}
}
}
if ($match = $route->match($url)) {
$output = trim($match, '/');
Expand Down Expand Up @@ -897,7 +903,7 @@ function url($url = null, $full = false) {
break;
}
}
$output = implode('/', $urlOut) . '/';
$output = implode('/', $urlOut);
}

if (!empty($args)) {
Expand Down
24 changes: 22 additions & 2 deletions cake/tests/cases/libs/router.test.php
Expand Up @@ -947,13 +947,29 @@ function testUrlParsing() {
$result = Router::parse('/posts/view/foo:bar/routing:fun/answer:42');
$expected = array('pass' => array(), 'named' => array('foo' => 'bar', 'routing' => 'fun', 'answer' => '42'), 'plugin' => null, 'controller' => 'posts', 'action' => 'view');
$this->assertEqual($result, $expected);
}

/**
* test that the persist key works.
*
* @return void
*/
function testPersistentParameters() {
Router::reload();
Router::connect('/:lang/:color/posts/view/*', array('controller' => 'posts', 'action' => 'view'), array('persist' => array('lang', 'color')));
Router::connect('/:lang/:color/posts/index', array('controller' => 'posts', 'action' => 'index'), array('persist' => array('lang')));
Router::connect(
'/:lang/:color/posts/view/*',
array('controller' => 'posts', 'action' => 'view'),
array('persist' => array('lang', 'color')
));
Router::connect(
'/:lang/:color/posts/index',
array('controller' => 'posts', 'action' => 'index'),
array('persist' => array('lang')
));
Router::connect('/:lang/:color/posts/edit/*', array('controller' => 'posts', 'action' => 'index'));
Router::connect('/about', array('controller' => 'pages', 'action' => 'view', 'about'));
Router::parse('/en/red/posts/view/5');

Router::setRequestInfo(array(
array('controller' => 'posts', 'action' => 'view', 'lang' => 'en', 'color' => 'red', 'form' => array(), 'url' => array(), 'plugin' => null),
array('base' => '/', 'here' => '/en/red/posts/view/5', 'webroot' => '/', 'passedArgs' => array(), 'argSeparator' => ':', 'namedArgs' => array())
Expand All @@ -966,6 +982,10 @@ function testUrlParsing() {
$result = Router::url(array('controller' => 'posts', 'action' => 'index', 'color' => 'blue'));
$this->assertEqual($result, $expected);

$expected = '/posts/edit/6';
$result = Router::url(array('controller' => 'posts', 'action' => 'edit', 6, 'color' => null, 'lang' => null));
$this->assertEqual($result, $expected);

$expected = '/posts';
$result = Router::url(array('controller' => 'posts', 'action' => 'index'));
$this->assertEqual($result, $expected);
Expand Down

0 comments on commit 52264cb

Please sign in to comment.