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...
1 parent 50b192e commit 8209a298f7f1a827ced670f2374b180042b0cb6e @markstory markstory committed Aug 2, 2013
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];
@@ -485,6 +485,40 @@ public function testPersistParams() {
}
/**
+ * 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.
*
* @return void

3 comments on commit 8209a29

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

Owner

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.

Contributor

spiliot replied Aug 30, 2013

Indeed, I confused the two, thanks.

Please sign in to comment.