Skip to content

Commit

Permalink
Adding tests for dispatching plugin shortcut urls.
Browse files Browse the repository at this point in the history
Refactoring Dispatcher::__getController, Dispatcher::_restructureParams to fix compatiblity between router and Dispatcher.
  • Loading branch information
markstory committed Dec 16, 2009
1 parent 7ceb50b commit 5c6d59e
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 37 deletions.
41 changes: 9 additions & 32 deletions cake/dispatcher.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -61,14 +61,6 @@ class Dispatcher extends Object {
*/ */
var $here = false; var $here = false;


/**
* Plugin being served (if any)
*
* @var string
* @access public
*/
var $plugin = null;

/** /**
* the params for this request * the params for this request
* *
Expand Down Expand Up @@ -159,7 +151,7 @@ function dispatch($url = null, $additionalParams = array()) {
$controller->base = $this->base; $controller->base = $this->base;
$controller->here = $this->here; $controller->here = $this->here;
$controller->webroot = $this->webroot; $controller->webroot = $this->webroot;
$controller->plugin = $this->plugin; $controller->plugin = isset($this->params['plugin']) ? $this->params['plugin'] : null;
$controller->params =& $this->params; $controller->params =& $this->params;
$controller->action =& $this->params['action']; $controller->action =& $this->params['action'];
$controller->passedArgs = array_merge($this->params['pass'], $this->params['named']); $controller->passedArgs = array_merge($this->params['pass'], $this->params['named']);
Expand Down Expand Up @@ -399,16 +391,8 @@ function _restructureParams($params, $reverse = false) {
'pass' => array_merge($pass, $params['pass']), 'pass' => array_merge($pass, $params['pass']),
'named' => array_merge($named, $params['named']) 'named' => array_merge($named, $params['named'])
)); ));
$this->plugin = $params['plugin'];
} else { } else {
$params['plugin'] = $params['controller']; $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; return $params;
} }
Expand All @@ -420,19 +404,15 @@ function _restructureParams($params, $reverse = false) {
* @return mixed name of controller if not loaded, or object if loaded * @return mixed name of controller if not loaded, or object if loaded
* @access private * @access private
*/ */
function &__getController($params = null) { function &__getController() {
if (!is_array($params)) { $original = $params = $this->params;
$original = $params = $this->params;
}
$controller = false; $controller = false;
$ctrlClass = $this->__loadController($params); $ctrlClass = $this->__loadController($params);
if (!$ctrlClass) { if (!$ctrlClass) {
if (!isset($params['plugin'])) { if (!isset($params['plugin'])) {
$params = $this->_restructureParams($params); $params = $this->_restructureParams($params);
} else { } else {
if (empty($original['pass']) && $original['action'] == 'index') {
$params['action'] = null;
}
$params = $this->_restructureParams($params, true); $params = $this->_restructureParams($params, true);
} }
$ctrlClass = $this->__loadController($params); $ctrlClass = $this->__loadController($params);
Expand All @@ -441,15 +421,13 @@ function &__getController($params = null) {
return $controller; return $controller;
} }
} else { } else {
$params = $this->params; if (!isset($params['plugin'])) {
$params = $this->_restructureParams($params);
}
} }
$name = $ctrlClass; $name = $ctrlClass;
$ctrlClass = $ctrlClass . 'Controller'; $ctrlClass .= 'Controller';
if (class_exists($ctrlClass)) { 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; $this->params = $params;
$controller =& new $ctrlClass(); $controller =& new $ctrlClass();
} }
Expand All @@ -466,10 +444,9 @@ function &__getController($params = null) {
function __loadController($params) { function __loadController($params) {
$pluginName = $pluginPath = $controller = null; $pluginName = $pluginPath = $controller = null;
if (!empty($params['plugin'])) { if (!empty($params['plugin'])) {
$this->plugin = $params['plugin'];
$pluginName = Inflector::camelize($params['plugin']); $pluginName = Inflector::camelize($params['plugin']);
$pluginPath = $pluginName . '.'; $pluginPath = $pluginName . '.';
$this->params['controller'] = $this->plugin; $this->params['controller'] = $params['plugin'];
$controller = $pluginName; $controller = $pluginName;
} }
if (!empty($params['controller'])) { if (!empty($params['controller'])) {
Expand Down
58 changes: 53 additions & 5 deletions cake/tests/cases/dispatcher.test.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -1599,11 +1599,18 @@ function testAutomaticPluginDispatchWithShortAccess() {
$_SERVER['PHP_SELF'] = '/cake/repo/branches/1.2.x.x/index.php'; $_SERVER['PHP_SELF'] = '/cake/repo/branches/1.2.x.x/index.php';


Router::reload(); 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 =& new TestDispatcher();
$Dispatcher->base = false; $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'; $url = 'my_plugin/my_plugin/add';
$controller = $Dispatcher->dispatch($url, array('return' => 1)); $controller = $Dispatcher->dispatch($url, array('return' => 1));
$this->assertFalse(isset($controller->params['pass'][0])); $this->assertFalse(isset($controller->params['pass'][0]));
Expand All @@ -1620,22 +1627,55 @@ function testAutomaticPluginDispatchWithShortAccess() {


$url = 'my_plugin/add'; $url = 'my_plugin/add';
$controller = $Dispatcher->dispatch($url, array('return' => 1)); $controller = $Dispatcher->dispatch($url, array('return' => 1));

$this->assertFalse(isset($controller->params['pass'][0])); $this->assertFalse(isset($controller->params['pass'][0]));


$Dispatcher =& new TestDispatcher(); $Dispatcher =& new TestDispatcher();
$Dispatcher->base = false; $Dispatcher->base = false;


$url = 'my_plugin/add/0'; $url = 'my_plugin/add/0';
$controller = $Dispatcher->dispatch($url, array('return' => 1)); $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 =& new TestDispatcher();
$Dispatcher->base = false; $Dispatcher->base = false;


$url = 'my_plugin/add/1'; $url = 'my_plugin/add/1';
$controller = $Dispatcher->dispatch($url, array('return' => 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();
} }


/** /**
Expand Down Expand Up @@ -1711,7 +1751,7 @@ function testPrefixProtection() {
} }


/** /**
* undocumented function * Test dispatching into the TestPlugin in the test_app
* *
* @return void * @return void
* @access public * @access public
Expand All @@ -1721,13 +1761,21 @@ function testTestPluginDispatch() {
App::build(array( App::build(array(
'plugins' => array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'plugins' . DS) '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'; $url = '/test_plugin/tests/index';
$result = $Dispatcher->dispatch($url, array('return' => 1)); $result = $Dispatcher->dispatch($url, array('return' => 1));
$this->assertTrue(class_exists('TestsController')); $this->assertTrue(class_exists('TestsController'));
$this->assertTrue(class_exists('TestPluginAppController')); $this->assertTrue(class_exists('TestPluginAppController'));
$this->assertTrue(class_exists('OtherComponentComponent')); $this->assertTrue(class_exists('OtherComponentComponent'));
$this->assertTrue(class_exists('PluginsComponentComponent')); $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(); App::build();
} }


Expand Down

0 comments on commit 5c6d59e

Please sign in to comment.