Skip to content
Permalink
Browse files

Make CakeRoute::persistParams() more tolerant

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 8209a298f7f1a827ced670f2374b180042b0cb6e
Showing with 37 additions and 0 deletions.
  1. +3 −0 lib/Cake/Routing/Route/CakeRoute.php
  2. +34 −0 lib/Cake/Test/Case/Routing/Route/CakeRouteTest.php
@@ -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];
@@ -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.
*

3 comments on commit 8209a29

@spiliot

This comment has been minimized.

Copy link
Contributor

spiliot replied Aug 30, 2013

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

@markstory

This comment has been minimized.

Copy link
Member Author

markstory replied Aug 30, 2013

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

This comment has been minimized.

Copy link
Contributor

spiliot replied Aug 30, 2013

Indeed, I confused the two, thanks.

Please sign in to comment.
You can’t perform that action at this time.