Skip to content

Commit

Permalink
Adding PluginShortRoute, and a few test cases.
Browse files Browse the repository at this point in the history
  • Loading branch information
markstory committed Apr 3, 2010
1 parent 558a9db commit 874c511
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 1 deletion.
39 changes: 39 additions & 0 deletions cake/libs/router.php
Expand Up @@ -1411,6 +1411,7 @@ function parse($url) {
$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;
Expand Down Expand Up @@ -1576,4 +1577,42 @@ function _writeUrl($params) {
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;
}
}
?>
69 changes: 68 additions & 1 deletion cake/tests/cases/libs/router.test.php
Expand Up @@ -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');
Expand Down Expand Up @@ -2436,5 +2436,72 @@ function testParse() {
$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.');
}
}
?>

0 comments on commit 874c511

Please sign in to comment.