Skip to content

Commit

Permalink
Fixing tests that should have never worked, and removing additional c…
Browse files Browse the repository at this point in the history
…alls to _restructureParams. Adding in prefixed plugin shortcuts as they were missing.
  • Loading branch information
markstory committed Apr 4, 2010
1 parent 2814ddc commit ed60939
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 17 deletions.
11 changes: 1 addition & 10 deletions cake/dispatcher.php
Expand Up @@ -398,23 +398,14 @@ function _restructureParams($params, $reverse = false) {
* @access private
*/
function &__getController() {
$original = $params = $this->params;

$controller = false;
$ctrlClass = $this->__loadController($params);
$ctrlClass = $this->__loadController($this->params);
if (!$ctrlClass) {
return $controller;
}
$name = $ctrlClass;
$ctrlClass .= 'Controller';
if (class_exists($ctrlClass)) {
if (
empty($params['plugin']) &&
strtolower(get_parent_class($ctrlClass)) === strtolower($name . 'AppController')
) {
$params = $this->_restructureParams($params);
}
$this->params = $params;
$controller =& new $ctrlClass();
}
return $controller;
Expand Down
7 changes: 5 additions & 2 deletions cake/libs/router.php
Expand Up @@ -560,15 +560,18 @@ function __connectDefaultRoutes() {
foreach ($plugins as $key => $value) {
$plugins[$key] = Inflector::underscore($value);
}
$match = array('plugin' => implode('|', $plugins));
$pluginPiped = implode('|', $plugins);
$match = array('plugin' => $pluginPiped);
$shortPlugin = array('plugin' => $pluginPiped, 'routeClass' => 'PluginShortRoute');

foreach ($this->__prefixes as $prefix) {
$params = array('prefix' => $prefix, $prefix => true);
$indexParams = $params + array('action' => 'index');
$this->connect("/{$prefix}/:plugin", $indexParams, $shortPlugin);
$this->connect("/{$prefix}/:plugin/:action/*", $params, $shortPlugin);
$this->connect("/{$prefix}/:plugin/:controller", $indexParams, $match);
$this->connect("/{$prefix}/:plugin/:controller/:action/*", $params, $match);
}
$shortPlugin = array_merge($match, array('routeClass' => 'PluginShortRoute'));
$this->connect('/:plugin', array('action' => 'index'), $shortPlugin);
$this->connect('/:plugin/:action/*', array(), $shortPlugin);
$this->connect('/:plugin/:controller', array('action' => 'index'), $match);
Expand Down
46 changes: 41 additions & 5 deletions cake/tests/cases/dispatcher.test.php
Expand Up @@ -340,6 +340,14 @@ class ArticlesTestController extends ArticlesTestAppController {
function admin_index() {
return true;
}
/**
* fake index method.
*
* @return void
*/
function index() {
return true;
}
}

/**
Expand Down Expand Up @@ -1423,7 +1431,10 @@ function testPluginDispatch() {

Router::reload();
$Dispatcher =& new TestDispatcher();
Router::connect('/my_plugin/:controller/*', array('plugin'=>'my_plugin', 'controller'=>'pages', 'action'=>'display'));
Router::connect(
'/my_plugin/:controller/*',
array('plugin' => 'my_plugin', 'controller' => 'pages', 'action' => 'display')
);

$Dispatcher->base = false;
$url = 'my_plugin/some_pages/home/param:value/param2:value2';
Expand Down Expand Up @@ -1472,7 +1483,10 @@ function testAutomaticPluginDispatch() {

Router::reload();
$Dispatcher =& new TestDispatcher();
Router::connect('/my_plugin/:controller/:action/*', array('plugin'=>'my_plugin', 'controller'=>'pages', 'action'=>'display'));
Router::connect(
'/my_plugin/:controller/:action/*',
array('plugin' => 'my_plugin', 'controller' => 'pages', 'action' => 'display')
);

$Dispatcher->base = false;

Expand Down Expand Up @@ -1508,6 +1522,13 @@ function testAutomaticPluginControllerDispatch() {
$_POST = array();
$_SERVER['PHP_SELF'] = '/cake/repo/branches/1.2.x.x/index.php';

$plugins = App::objects('plugin');
$plugins[] = 'MyPlugin';
$plugins[] = 'ArticlesTest';

$app = App::getInstance();
$app->__objects['plugin'] = $plugins;

Router::reload();
$Dispatcher =& new TestDispatcher();
$Dispatcher->base = false;
Expand All @@ -1526,7 +1547,7 @@ function testAutomaticPluginControllerDispatch() {
$Dispatcher =& new TestDispatcher();
$Dispatcher->base = false;

/* Simulates the Route for a real plugin, installed in APP/plugins */
// Simulates the Route for a real plugin, installed in APP/plugins
Router::connect('/my_plugin/:controller/:action/*', array('plugin' => 'my_plugin'));

$plugin = 'MyPlugin';
Expand All @@ -1550,13 +1571,20 @@ function testAutomaticPluginControllerDispatch() {

$url = 'admin/my_plugin/add/5/param:value/param2:value2';
$controller = $Dispatcher->dispatch($url, array('return' => 1));

$this->assertEqual($controller->params['plugin'], 'my_plugin');
$this->assertEqual($controller->params['controller'], 'my_plugin');
$this->assertEqual($controller->params['action'], 'admin_add');
$this->assertEqual($controller->params['pass'], array(5));
$this->assertEqual($controller->params['named'], array('param' => 'value', 'param2' => 'value2'));
$this->assertIdentical($controller->plugin, 'my_plugin');
$this->assertIdentical($controller->name, 'MyPlugin');
$this->assertIdentical($controller->action, 'admin_add');

$expected = array(0 => 5, 'param'=>'value', 'param2'=>'value2');
$this->assertEqual($controller->passedArgs, $expected);

Configure::write('Routing.prefixes', array('admin'));
Router::reload();

$Dispatcher =& new TestDispatcher();
Expand All @@ -1568,8 +1596,16 @@ function testAutomaticPluginControllerDispatch() {
$this->assertIdentical($controller->action, 'admin_index');

$expected = array(
'pass'=> array(), 'named' => array(), 'controller' => 'articles_test', 'plugin' => 'articles_test', 'action' => 'admin_index',
'prefix' => 'admin', 'admin' => true, 'form' => array(), 'url' => array('url' => 'admin/articles_test'), 'return' => 1
'pass'=> array(),
'named' => array(),
'controller' => 'articles_test',
'plugin' => 'articles_test',
'action' => 'admin_index',
'prefix' => 'admin',
'admin' => true,
'form' => array(),
'url' => array('url' => 'admin/articles_test'),
'return' => 1
);
$this->assertEqual($controller->params, $expected);
}
Expand Down

0 comments on commit ed60939

Please sign in to comment.