From cba8871f164e514fd6b1b6b6f833bf190eb6c8b3 Mon Sep 17 00:00:00 2001 From: mark_story Date: Wed, 30 Sep 2009 10:19:43 -0400 Subject: [PATCH] Adding tests so that adding one prefix removes other prefixes. Making it so you can only have one prefix at a time in a route. Also simplifies prefix switching. --- cake/libs/router.php | 5 +++-- cake/tests/cases/libs/router.test.php | 28 +++++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/cake/libs/router.php b/cake/libs/router.php index 0b8438572..109241836 100644 --- a/cake/libs/router.php +++ b/cake/libs/router.php @@ -846,8 +846,9 @@ class Router { } } + $prefixExists = (array_intersect_key($url, array_flip($_this->__prefixes))); foreach ($_this->__prefixes as $prefix) { - if (!isset($url[$prefix]) && !empty($params[$prefix])) { + if (!isset($url[$prefix]) && !empty($params[$prefix]) && !$prefixExists) { $url[$prefix] = true; } elseif (isset($url[$prefix]) && !$url[$prefix]) { unset($url[$prefix]); @@ -926,7 +927,7 @@ class Router { if (isset($url['plugin']) && $url['plugin'] != $url['controller']) { array_unshift($urlOut, $url['plugin']); } - + foreach ($_this->__prefixes as $prefix) { if (isset($url[$prefix])) { array_unshift($urlOut, $prefix); diff --git a/cake/tests/cases/libs/router.test.php b/cake/tests/cases/libs/router.test.php index 5e4439d18..11991fc91 100644 --- a/cake/tests/cases/libs/router.test.php +++ b/cake/tests/cases/libs/router.test.php @@ -1539,6 +1539,34 @@ class RouterTest extends CakeTestCase { $this->assertEqual($result, $expected); } +/** + * test that setting a prefix override the current one + * + * @return void + */ + function testPrefixOverride() { + Configure::write('Routing.prefixes', array('protected', 'admin')); + Router::reload(); + Router::parse('/'); + + Router::setRequestInfo(array( + array('plugin' => null, 'controller' => 'images', 'action' => 'index', 'pass' => array(), 'prefix' => 'protected', 'protected' => true, 'form' => array(), 'url' => array('url' => 'protected/images/index')), + array('base' => '', 'here' => '/protected/images/index', 'webroot' => '/') + )); + + $result = Router::url(array('controller' => 'images', 'action' => 'add', 'admin' => true)); + $expected = '/admin/images/add'; + $this->assertEqual($result, $expected); + + Router::setRequestInfo(array( + array('plugin' => null, 'controller' => 'images', 'action' => 'index', 'pass' => array(), 'prefix' => 'admin', 'admin' => true, 'form' => array(), 'url' => array('url' => 'admin/images/index')), + array('base' => '', 'here' => '/admin/images/index', 'webroot' => '/') + )); + $result = Router::url(array('controller' => 'images', 'action' => 'add', 'protected' => true)); + $expected = '/protected/images/add'; + $this->assertEqual($result, $expected); + } + /** * testRemoveBase method *