Browse files

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.
  • Loading branch information...
1 parent d2b4e33 commit cba8871f164e514fd6b1b6b6f833bf190eb6c8b3 @markstory markstory committed Sep 30, 2009
Showing with 31 additions and 2 deletions.
  1. +3 −2 cake/libs/router.php
  2. +28 −0 cake/tests/cases/libs/router.test.php
View
5 cake/libs/router.php
@@ -846,8 +846,9 @@ function url($url = null, $full = false) {
}
}
+ $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 @@ function url($url = null, $full = false) {
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);
View
28 cake/tests/cases/libs/router.test.php
@@ -1540,6 +1540,34 @@ function testAutoPrefixRoutePersistence() {
}
/**
+ * 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
*
* @access public

0 comments on commit cba8871

Please sign in to comment.