Fixing issues in router where plugin => null would not always exit a plugin route. Test cases added.

This commit is contained in:
Mark Story 2009-12-08 21:36:25 -05:00
parent efa36abdf0
commit 4421fe6dc3
2 changed files with 63 additions and 20 deletions

View file

@ -1374,6 +1374,7 @@ class CakeRoute {
$diffUnfiltered = Set::diff($url, $defaults);
$diff = array();
foreach ($diffUnfiltered as $key => $var) {
if ($var === 0 || $var === '0' || !empty($var)) {
$diff[$key] = $var;
@ -1386,14 +1387,13 @@ class CakeRoute {
}
//remove defaults that are also keys. They can cause match failures
$count = count($this->keys);
while ($count--) {
unset($defaults[$this->keys[$count]]);
foreach ($this->keys as $key) {
unset($defaults[$key]);
}
$filteredDefaults = array_filter($defaults);
//if the difference between the url diff and defaults contains keys from defaults its not a match
if (array_intersect_key($filteredDefaults, $diff) !== array()) {
if (array_intersect_key($filteredDefaults, $diffUnfiltered) !== array()) {
return false;
}
@ -1405,6 +1405,7 @@ class CakeRoute {
}
$i++;
}
$passedArgsAndParams = array_diff_key($diff, $filteredDefaults, $keyNames);
list($named, $params) = Router::getNamedElements($passedArgsAndParams, $url['controller'], $url['action']);

View file

@ -770,7 +770,7 @@ class RouterTest extends CakeTestCase {
* @access public
* @return void
*/
function testPluginUrlGeneration() {
function testUrlGenerationPlugins() {
Router::setRequestInfo(array(
array(
'controller' => 'controller', 'action' => 'index', 'form' => array(),
@ -785,7 +785,6 @@ class RouterTest extends CakeTestCase {
$this->assertEqual(Router::url('read/1'), '/base/test/controller/read/1');
Router::reload();
Router::connect('/:lang/:plugin/:controller/*', array('action' => 'index'));
Router::setRequestInfo(array(
@ -795,7 +794,8 @@ class RouterTest extends CakeTestCase {
array(), 'form' => array(), 'url' =>
array('url' => 'en/shows/')),
array('plugin' => NULL, 'controller' => NULL, 'action' => NULL, 'base' => '',
'here' => '/en/shows/', 'webroot' => '/')));
'here' => '/en/shows/', 'webroot' => '/')
));
Router::parse('/en/shows/');
@ -807,6 +807,48 @@ class RouterTest extends CakeTestCase {
$this->assertEqual($result, $expected);
}
/**
* test that you can leave active plugin routes with plugin = null
*
* @return void
*/
function testCanLeavePlugin() {
Router::reload();
Router::connect(
'/admin/other/:controller/:action/*',
array(
'admin' => 1,
'plugin' => 'aliased',
'prefix' => 'admin'
)
);
Router::setRequestInfo(array(
array(
'pass' => array(),
'admin' => true,
'prefix' => 'admin',
'plugin' => 'this',
'action' => 'admin_index',
'controller' => 'interesting',
'url' => array('url' => 'admin/this/interesting/index'),
),
array(
'base' => '',
'here' => '/admin/this/interesting/index',
'webroot' => '/',
'passedArgs' => array(),
)
));
$result = Router::url(array('plugin' => null, 'controller' => 'posts', 'action' => 'index'));
$this->assertEqual($result, '/admin/posts');
$result = Router::url(array('controller' => 'posts', 'action' => 'index'));
$this->assertEqual($result, '/admin/this/posts');
$result = Router::url(array('plugin' => 'aliased', 'controller' => 'posts', 'action' => 'index'));
$this->assertEqual($result, '/admin/other/posts/index');
}
/**
* testUrlParsing method
*