Adding PluginShortRoute, and a few test cases.

This commit is contained in:
Mark Story 2010-04-03 16:29:20 -04:00
parent 558a9db642
commit 874c511fe2
2 changed files with 107 additions and 1 deletions

View file

@ -1411,6 +1411,7 @@ class CakeRoute {
$route['pass'] = $route['named'] = array();
$route += $this->defaults;
//move numerically indexed elements from the defaults into pass.
foreach ($route as $key => $value) {
if (is_integer($key)) {
$route['pass'][] = $value;
@ -1576,4 +1577,42 @@ class CakeRoute {
return $out;
}
}
class PluginShortRoute extends CakeRoute {
var $_plugins = array();
/**
* Constructor Sets up the plugin sets.
*
* @return void
*/
function PluginShortRoute($template, $defaults = array(), $options = array()) {
parent::CakeRoute($template, $defaults, $options);
$plugins = App::objects('plugin');
foreach ($plugins as $plugin) {
$this->_plugins[Inflector::underscore($plugin)] = true;
}
}
/**
* Parses urls and creates the correct request parameter set.
* Plugin short cut routes have the same plugin and controller keys.
* If there is no controller available in the plugin with the same
* name as the plugin, this route cannot pass.
*
* @param string $url Url string to parse.
* @return mixed False on failure, or an array of request parameters
*/
function parse($url) {
$params = parent::parse($url);
if (!isset($params['plugin']) || (isset($params['plugin']) && !isset($this->_plugins[$params['plugin']]))) {
return false;
}
$pluginName = Inflector::camelize($params['plugin']);
$controllerName = $pluginName . '.' . $pluginName;
if (!App::import('Controller', $controllerName)) {
return false;
}
$params['controller'] = $params['plugin'];
return $params;
}
}
?>

View file

@ -17,7 +17,7 @@
* @since CakePHP(tm) v 1.2.0.4206
* @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License
*/
App::import('Core', array('Router', 'Debugger'));
App::import('Core', array('Router'));
if (!defined('FULL_BASE_URL')) {
define('FULL_BASE_URL', 'http://cakephp.org');
@ -2436,5 +2436,72 @@ class CakeRouteTestCase extends CakeTestCase {
$this->assertEqual($result['action'], 'index');
}
}
//SimpleTest::ignore('RouterTest');
//SimpleTest::ignore('CakeRouteTestCase');
/**
* Test case for PluginShortRoute
*
* @package cake.test.cases.libs
*/
class PluginShortRouteTestCase extends CakeTestCase {
/**
* startTest method
*
* @access public
* @return void
*/
function startTest() {
$this->_routing = Configure::read('Routing');
Configure::write('Routing', array('admin' => null, 'prefixes' => array()));
App::build(array(
'plugins' => array(
TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'plugins' . DS
)
));
App::objects('plugin', null, false);
Router::reload();
}
/**
* end the test and reset the environment
*
* @return void
**/
function endTest() {
Configure::write('Routing', $this->_routing);
App::build();
}
/**
* test the parsing for plugin short routes.
*
* @return void
*/
function testParsing() {
$route =& new PluginShortRoute('/:plugin', array('action' => 'index'));
$result = $route->parse('/non_existant_plugin');
$this->assertFalse($result, 'Route matched when it should fail.');
$result = $route->parse('/test_plugin');
$this->assertEqual($result['plugin'], 'test_plugin');
$this->assertEqual($result['controller'], 'test_plugin');
$this->assertEqual($result['action'], 'index');
$route =& new PluginShortRoute('/:plugin/:action/*');
$result = $route->parse('/test_plugin/add');
$this->assertEqual($result['plugin'], 'test_plugin');
$this->assertEqual($result['controller'], 'test_plugin');
$this->assertEqual($result['action'], 'add');
$result = $route->parse('/test_plugin/edit/1');
$this->assertEqual($result['plugin'], 'test_plugin');
$this->assertEqual($result['controller'], 'test_plugin');
$this->assertEqual($result['action'], 'edit');
$this->assertEqual($result['_args_'], '1', 'Passed args were wrong.');
}
}
?>