Skip to content

Commit

Permalink
Make CakeRoute::persistParams() more tolerant
Browse files Browse the repository at this point in the history
Subclasses and instances may not always define persist options.
CakeRoute should be accepting of these differences.

Fixes #3957
  • Loading branch information
markstory committed Aug 2, 2013
1 parent 50b192e commit 8209a29
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
3 changes: 3 additions & 0 deletions lib/Cake/Routing/Route/CakeRoute.php
Expand Up @@ -378,6 +378,9 @@ protected function _matchNamed($val, $rule, $context) {
* @return array An array with persistent parameters applied.
*/
public function persistParams($url, $params) {
if (empty($this->options['persist']) || !is_array($this->options['persist'])) {
return $url;
}
foreach ($this->options['persist'] as $persistKey) {
if (array_key_exists($persistKey, $params) && !isset($url[$persistKey])) {
$url[$persistKey] = $params[$persistKey];
Expand Down
34 changes: 34 additions & 0 deletions lib/Cake/Test/Case/Routing/Route/CakeRouteTest.php
Expand Up @@ -484,6 +484,40 @@ public function testPersistParams() {
$this->assertEquals('red', $result['color']);
}

/**
* test persist with a non array value
*
* @return void
*/
public function testPersistParamsNonArray() {
$url = array('controller' => 'posts', 'action' => 'index');
$params = array('lang' => 'en', 'color' => 'blue');

$route = new CakeRoute(
'/:lang/:color/blog/:action',
array('controller' => 'posts')
// No persist options
);
$result = $route->persistParams($url, $params);
$this->assertEquals($url, $result);

$route = new CakeRoute(
'/:lang/:color/blog/:action',
array('controller' => 'posts'),
array('persist' => false)
);
$result = $route->persistParams($url, $params);
$this->assertEquals($url, $result);

$route = new CakeRoute(
'/:lang/:color/blog/:action',
array('controller' => 'posts'),
array('persist' => 'derp')
);
$result = $route->persistParams($url, $params);
$this->assertEquals($url, $result);
}

/**
* test the parse method of CakeRoute.
*
Expand Down

3 comments on commit 8209a29

@spiliot
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change means 'persist' => true won't work anymore?
http://book.cakephp.org/2.0/en/development/routing.html#redirect-routing

@markstory
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No it should continue to work as it did before. The existing test cases continue to pass. RedirectRoute implements persist differently from what I remember.

@spiliot
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed, I confused the two, thanks.

Please sign in to comment.