Permalink
Browse files

Adding tests for dispatching plugin shortcut urls.

Refactoring Dispatcher::__getController, Dispatcher::_restructureParams to fix compatiblity between router and Dispatcher.
  • Loading branch information...
1 parent 7ceb50b commit 5c6d59ea13a0e1f6f96a2e2e57884bb8151bb074 @markstory markstory committed Dec 16, 2009
Showing with 62 additions and 37 deletions.
  1. +9 −32 cake/dispatcher.php
  2. +53 −5 cake/tests/cases/dispatcher.test.php
View
@@ -62,14 +62,6 @@ class Dispatcher extends Object {
var $here = false;
/**
- * Plugin being served (if any)
- *
- * @var string
- * @access public
- */
- var $plugin = null;
-
-/**
* the params for this request
*
* @var string
@@ -159,7 +151,7 @@ function dispatch($url = null, $additionalParams = array()) {
$controller->base = $this->base;
$controller->here = $this->here;
$controller->webroot = $this->webroot;
- $controller->plugin = $this->plugin;
+ $controller->plugin = isset($this->params['plugin']) ? $this->params['plugin'] : null;
$controller->params =& $this->params;
$controller->action =& $this->params['action'];
$controller->passedArgs = array_merge($this->params['pass'], $this->params['named']);
@@ -399,16 +391,8 @@ function _restructureParams($params, $reverse = false) {
'pass' => array_merge($pass, $params['pass']),
'named' => array_merge($named, $params['named'])
));
- $this->plugin = $params['plugin'];
} else {
$params['plugin'] = $params['controller'];
- $params['controller'] = $params['action'];
- if (isset($params['pass'][0])) {
- $params['action'] = $params['pass'][0];
- array_shift($params['pass']);
- } else {
- $params['action'] = null;
- }
}
return $params;
}
@@ -420,19 +404,15 @@ function _restructureParams($params, $reverse = false) {
* @return mixed name of controller if not loaded, or object if loaded
* @access private
*/
- function &__getController($params = null) {
- if (!is_array($params)) {
- $original = $params = $this->params;
- }
+ function &__getController() {
+ $original = $params = $this->params;
+
$controller = false;
$ctrlClass = $this->__loadController($params);
if (!$ctrlClass) {
if (!isset($params['plugin'])) {
$params = $this->_restructureParams($params);
} else {
- if (empty($original['pass']) && $original['action'] == 'index') {
- $params['action'] = null;
- }
$params = $this->_restructureParams($params, true);
}
$ctrlClass = $this->__loadController($params);
@@ -441,15 +421,13 @@ function &__getController($params = null) {
return $controller;
}
} else {
- $params = $this->params;
+ if (!isset($params['plugin'])) {
+ $params = $this->_restructureParams($params);
+ }
}
$name = $ctrlClass;
- $ctrlClass = $ctrlClass . 'Controller';
+ $ctrlClass .= 'Controller';
if (class_exists($ctrlClass)) {
- if (strtolower(get_parent_class($ctrlClass)) === strtolower($name . 'AppController') && empty($params['plugin'])) {
- $params = $this->_restructureParams($params);
- $params = $this->_restructureParams($params, true);
- }
$this->params = $params;
$controller =& new $ctrlClass();
}
@@ -466,10 +444,9 @@ function &__getController($params = null) {
function __loadController($params) {
$pluginName = $pluginPath = $controller = null;
if (!empty($params['plugin'])) {
- $this->plugin = $params['plugin'];
$pluginName = Inflector::camelize($params['plugin']);
$pluginPath = $pluginName . '.';
- $this->params['controller'] = $this->plugin;
+ $this->params['controller'] = $params['plugin'];
$controller = $pluginName;
}
if (!empty($params['controller'])) {
@@ -1599,11 +1599,18 @@ function testAutomaticPluginDispatchWithShortAccess() {
$_SERVER['PHP_SELF'] = '/cake/repo/branches/1.2.x.x/index.php';
Router::reload();
- Router::connect('/my_plugin/:controller/:action/*', array('plugin'=>'my_plugin'));
+ Router::connect('/my_plugin/:controller/:action/*', array('plugin' => 'my_plugin'));
$Dispatcher =& new TestDispatcher();
$Dispatcher->base = false;
+ $url = 'my_plugin/';
+ $controller = $Dispatcher->dispatch($url, array('return' => 1));
+ $this->assertEqual($controller->params['controller'], 'my_plugin');
+ $this->assertEqual($controller->params['plugin'], 'my_plugin');
+ $this->assertEqual($controller->params['action'], 'index');
+ $this->assertFalse(isset($controller->params['pass'][0]));
+
$url = 'my_plugin/my_plugin/add';
$controller = $Dispatcher->dispatch($url, array('return' => 1));
$this->assertFalse(isset($controller->params['pass'][0]));
@@ -1620,22 +1627,55 @@ function testAutomaticPluginDispatchWithShortAccess() {
$url = 'my_plugin/add';
$controller = $Dispatcher->dispatch($url, array('return' => 1));
-
$this->assertFalse(isset($controller->params['pass'][0]));
$Dispatcher =& new TestDispatcher();
$Dispatcher->base = false;
$url = 'my_plugin/add/0';
$controller = $Dispatcher->dispatch($url, array('return' => 1));
- $this->assertIdentical('0',$controller->params['pass'][0]);
+ $this->assertEqual($controller->params['controller'], 'my_plugin');
+ $this->assertEqual($controller->params['plugin'], 'my_plugin');
+ $this->assertEqual($controller->params['action'], 'add');
+ $this->assertIdentical('0', $controller->params['pass'][0]);
$Dispatcher =& new TestDispatcher();
$Dispatcher->base = false;
$url = 'my_plugin/add/1';
$controller = $Dispatcher->dispatch($url, array('return' => 1));
- $this->assertIdentical('1',$controller->params['pass'][0]);
+
+ $this->assertEqual($controller->params['controller'], 'my_plugin');
+ $this->assertEqual($controller->params['plugin'], 'my_plugin');
+ $this->assertEqual($controller->params['action'], 'add');
+ $this->assertIdentical('1', $controller->params['pass'][0]);
+ }
+
+/**
+ * test plugin shortcut urls with controllers that need to be loaded,
+ * the above test uses a controller that has already been included.
+ *
+ * @return void
+ */
+ function testPluginShortCutUrlsWithControllerThatNeedsToBeLoaded() {
+ $loaded = class_exists('TestPluginController', false);
+ if ($this->skipIf($loaded, 'TestPluginController already loaded, this test will always pass, skipping %s')) {
+ return true;
+ }
+ Router::reload();
+ App::build(array(
+ 'plugins' => array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'plugins' . DS)
+ ), true);
+ $Dispatcher =& new TestDispatcher();
+ $Dispatcher->base = false;
+
+ $url = 'test_plugin/';
+ $controller = $Dispatcher->dispatch($url, array('return' => 1));
+ $this->assertEqual($controller->params['controller'], 'test_plugin');
+ $this->assertEqual($controller->params['plugin'], 'test_plugin');
+ $this->assertEqual($controller->params['action'], 'index');
+ $this->assertFalse(isset($controller->params['pass'][0]));
+ App::build();
}
/**
@@ -1711,7 +1751,7 @@ function testPrefixProtection() {
}
/**
- * undocumented function
+ * Test dispatching into the TestPlugin in the test_app
*
* @return void
* @access public
@@ -1721,13 +1761,21 @@ function testTestPluginDispatch() {
App::build(array(
'plugins' => array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'plugins' . DS)
));
+ App::objects('plugin', null, false);
+ Router::reload();
+ Router::parse('/');
+
$url = '/test_plugin/tests/index';
$result = $Dispatcher->dispatch($url, array('return' => 1));
$this->assertTrue(class_exists('TestsController'));
$this->assertTrue(class_exists('TestPluginAppController'));
$this->assertTrue(class_exists('OtherComponentComponent'));
$this->assertTrue(class_exists('PluginsComponentComponent'));
+ $this->assertEqual($result->params['controller'], 'tests');
+ $this->assertEqual($result->params['plugin'], 'test_plugin');
+ $this->assertEqual($result->params['action'], 'index');
+
App::build();
}

0 comments on commit 5c6d59e

Please sign in to comment.