Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Implementing PluginShortRoute and some tests for its methods.

  • Loading branch information...
commit 131578cb4257e384ec473deacb0a8fd4a19e86b0 1 parent 07efd76
Mark Story markstory authored
Showing with 102 additions and 0 deletions.
  1. +42 −0 cake/libs/router.php
  2. +60 −0 cake/tests/cases/libs/router.test.php
42 cake/libs/router.php
View
@@ -1585,4 +1585,46 @@ function _writeUrl($params) {
return $out;
}
}
+
+/**
+ * Plugin short route, that copies the plugin param to the controller parameters
+ * It is used for supporting /:plugin routes.
+ *
+ * @package cake.libs
+ */
+class PluginShortRoute extends CakeRoute {
+/**
+ * Parses a string url into an array. If a plugin key is found, it will be copied to the
+ * controller parameter
+ *
+ * @param string $url The url to parse
+ * @return mixed false on failure, or an array of request parameters
+ */
+ function parse($url) {
+ $params = parent::parse($url);
+ if (!$params) {
+ return false;
+ }
+ $params['controller'] = $params['plugin'];
+ return $params;
+ }
+
+/**
+ * Reverse route plugin shortcut urls. If the plugin and controller
+ * are not the same the match is an auto fail.
+ *
+ * @param array $url Array of parameters to convert to a string.
+ * @return mixed either false or a string url.
+ */
+ function match($url) {
+ if (isset($url['controller']) && isset($url['plugin']) && $url['plugin'] != $url['controller']) {
+ return false;
+ }
+ $this->defaults['controller'] = $url['controller'];
+ $result = parent::match($url);
+ unset($this->defaults['controller']);
+ return $result;
+ }
+}
+
?>
60 cake/tests/cases/libs/router.test.php
View
@@ -2416,4 +2416,64 @@ function testParse() {
}
}
+/**
+ * test case for PluginShortRoute
+ *
+ * @package cake.tests.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()));
+ Router::reload();
+ }
+
+/**
+ * end the test and reset the environment
+ *
+ * @return void
+ **/
+ function endTest() {
+ Configure::write('Routing', $this->_routing);
+ }
+
+/**
+ * test the parsing of routes.
+ *
+ * @return void
+ */
+ function testParsing() {
+ $route =& new PluginShortRoute('/:plugin', array('action' => 'index'), array('plugin' => 'foo|bar'));
+
+ $result = $route->parse('/foo');
+ $this->assertEqual($result['plugin'], 'foo');
+ $this->assertEqual($result['controller'], 'foo');
+ $this->assertEqual($result['action'], 'index');
+
+ $result = $route->parse('/wrong');
+ $this->assertFalse($result, 'Wrong plugin name matched %s');
+ }
+
+/**
+ * test the reverse routing of the plugin shortcut urls.
+ *
+ * @return void
+ */
+ function testMatch() {
+ $route =& new PluginShortRoute('/:plugin', array('action' => 'index'), array('plugin' => 'foo|bar'));
+
+ $result = $route->match(array('plugin' => 'foo', 'controller' => 'posts', 'action' => 'index'));
+ $this->assertFalse($result, 'plugin controller mismatch was converted. %s');
+
+ $result = $route->match(array('plugin' => 'foo', 'controller' => 'foo', 'action' => 'index'));
+ $this->assertEqual($result, '/foo');
+ }
+}
+
?>
Please sign in to comment.
Something went wrong with that request. Please try again.