Skip to content

Commit

Permalink
Adding tests so that adding one prefix removes other prefixes. Making…
Browse files Browse the repository at this point in the history
… it so you can only have one prefix at a time in a route. Also simplifies prefix switching.
  • Loading branch information
markstory committed Sep 30, 2009
1 parent d2b4e33 commit cba8871
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
5 changes: 3 additions & 2 deletions cake/libs/router.php
Expand Up @@ -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]);
Expand Down Expand Up @@ -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);
Expand Down
28 changes: 28 additions & 0 deletions cake/tests/cases/libs/router.test.php
Expand Up @@ -1539,6 +1539,34 @@ function testAutoPrefixRoutePersistence() {
$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
*
Expand Down

0 comments on commit cba8871

Please sign in to comment.