From eefb2e81fab0340369fec5b9478518ba83bccfae Mon Sep 17 00:00:00 2001 From: mark_story Date: Tue, 24 Apr 2012 20:48:23 -0400 Subject: [PATCH] Fix issue where prefixes mid action would be truncated. If a prefix occured mid action name it would be removed, corrupting the action name. Fixes #2831 --- lib/Cake/Routing/Route/CakeRoute.php | 7 +++++-- lib/Cake/Routing/Router.php | 5 +++-- lib/Cake/Test/Case/Routing/Route/CakeRouteTest.php | 10 ++++++++++ lib/Cake/Test/Case/Routing/RouterTest.php | 4 ++++ 4 files changed, 22 insertions(+), 4 deletions(-) diff --git a/lib/Cake/Routing/Route/CakeRoute.php b/lib/Cake/Routing/Route/CakeRoute.php index 2c2343b71a3..47c029e2b84 100644 --- a/lib/Cake/Routing/Route/CakeRoute.php +++ b/lib/Cake/Routing/Route/CakeRoute.php @@ -478,8 +478,11 @@ public function match($url) { * @return string Composed route string. */ protected function _writeUrl($params) { - if (isset($params['prefix'], $params['action'])) { - $params['action'] = str_replace($params['prefix'] . '_', '', $params['action']); + if (isset($params['prefix'])) { + $prefixed = $params['prefix'] . '_'; + } + if (isset($prefixed, $params['action']) && strpos($params['action'], $prefixed) === 0) { + $params['action'] = substr($params['action'], strlen($prefixed) * -1); unset($params['prefix']); } diff --git a/lib/Cake/Routing/Router.php b/lib/Cake/Routing/Router.php index 62cda92e7c9..1c8689410d4 100644 --- a/lib/Cake/Routing/Router.php +++ b/lib/Cake/Routing/Router.php @@ -893,8 +893,9 @@ protected static function _handleNoRoute($url) { list($args, $named) = array(Set::filter($args, true), Set::filter($named, true)); foreach (self::$_prefixes as $prefix) { - if (!empty($url[$prefix])) { - $url['action'] = str_replace($prefix . '_', '', $url['action']); + $prefixed = $prefix . '_'; + if (!empty($url[$prefix]) && strpos($url['action'], $prefixed) === 0) { + $url['action'] = substr($url['action'], strlen($prefixed) * -1); break; } } diff --git a/lib/Cake/Test/Case/Routing/Route/CakeRouteTest.php b/lib/Cake/Test/Case/Routing/Route/CakeRouteTest.php index 58e0644c4b8..a60f0453058 100644 --- a/lib/Cake/Test/Case/Routing/Route/CakeRouteTest.php +++ b/lib/Cake/Test/Case/Routing/Route/CakeRouteTest.php @@ -299,6 +299,16 @@ public function testMatchBasic() { $result = $route->match($url); $expected = '/admin/subscriptions/edit/1'; $this->assertEquals($expected, $result); + + $url = array( + 'controller' => 'subscribe', + 'admin' => true, + 'action' => 'edit_admin_e', + 1 + ); + $result = $route->match($url); + $expected = '/admin/subscriptions/edit_admin_e/1'; + $this->assertEquals($expected, $result); } /** diff --git a/lib/Cake/Test/Case/Routing/RouterTest.php b/lib/Cake/Test/Case/Routing/RouterTest.php index 55d4e5d02cc..283aa9cfaf3 100644 --- a/lib/Cake/Test/Case/Routing/RouterTest.php +++ b/lib/Cake/Test/Case/Routing/RouterTest.php @@ -1582,6 +1582,10 @@ public function testUrlGenerationWithAutoPrefixes() { $expected = '/protected/images/add'; $this->assertEquals($expected, $result); + $result = Router::url(array('controller' => 'images', 'action' => 'add_protected_test', 'protected' => true)); + $expected = '/protected/images/add_protected_test'; + $this->assertEquals($expected, $result); + $result = Router::url(array('action' => 'edit', 1)); $expected = '/images/edit/1'; $this->assertEquals($expected, $result);