Make CakeRoute::persistParams() more tolerant

Subclasses and instances may not always define persist options.
CakeRoute should be accepting of these differences.

Fixes #3957
This commit is contained in:
mark_story 2013-08-02 13:28:58 -04:00
parent 50b192eb1b
commit 8209a298f7
2 changed files with 37 additions and 0 deletions

View file

@ -378,6 +378,9 @@ class CakeRoute {
* @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];

View file

@ -484,6 +484,40 @@ class CakeRouteTest extends CakeTestCase {
$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.
*