Permalink
Browse files

Fixing persistent param route exiting.

Adding test for exiting persist param routes.
  • Loading branch information...
markstory committed Nov 26, 2009
1 parent 35fac16 commit 52264cb61c0d45fa6b2cc6e69011715f4f1f0d95
Showing with 30 additions and 4 deletions.
  1. +8 −2 cake/libs/router.php
  2. +22 −2 cake/tests/cases/libs/router.test.php
View
@@ -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, '/');
@@ -897,7 +903,7 @@ function url($url = null, $full = false) {
break;
}
}
- $output = implode('/', $urlOut) . '/';
+ $output = implode('/', $urlOut);
}
if (!empty($args)) {
@@ -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())
@@ -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);

0 comments on commit 52264cb

Please sign in to comment.