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.

This commit is contained in:
mark_story 2009-09-30 10:19:43 -04:00
parent d2b4e33e1b
commit cba8871f16
2 changed files with 31 additions and 2 deletions

View file

@ -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);

View file

@ -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
*