Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Implemented plugin short i18n routes - closes #1

  • Loading branch information...
commit 820a9cc14e11a6ec27c081ba2c43ae4f2bb1e05b 1 parent acd3d39
Pierre Martin authored
Showing with 152 additions and 16 deletions.
  1. +54 −1 libs/i18n_route.php
  2. +98 −15 tests/cases/libs/i18n_route.test.php
55 libs/i18n_route.php
View
@@ -108,7 +108,6 @@ public function parse($url) {
* Connects the default, built-in routes, including prefix and plugin routes with the i18n custom Route
* Code mostly duplicated from Router::__connectDefaultRoutes
*
- * @TODO Add Short route for plugins
* @see Router::__connectDefaultRoutes
* @param array $pluginExceptions Plugins ommited from the lang default routing
* @return void
@@ -127,13 +126,16 @@ public static function connectDefaultRoutes($pluginExceptions = array()) {
$pluginPattern = implode('|', $plugins);
$match = array('plugin' => $pluginPattern) + $options;
+ $shortParams = array('routeClass' => 'PluginShortI18nRoute', 'plugin' => $pluginPattern);
foreach ($prefixes as $prefix) {
$params = array('prefix' => $prefix, $prefix => true);
$indexParams = $params + array('action' => 'index');
+ Router::connect("/{$prefix}/:plugin", $indexParams, $shortParams);
Router::connect("/{$prefix}/:plugin/:controller", $indexParams, $match);
Router::connect("/{$prefix}/:plugin/:controller/:action/*", $params, $match);
}
+ Router::connect('/:plugin', array('action' => 'index'), $shortParams);
Router::connect('/:plugin/:controller', array('action' => 'index'), $match);
Router::connect('/:plugin/:controller/:action/*', array(), $match);
}
@@ -192,3 +194,54 @@ public static function reload() {
Router::reload();
}
}
+
+/**
+ * Plugin short route, that copies the plugin param to the controller parameters
+ * It is used for supporting /:plugin routes.
+ *
+ * @package i18n
+ * @subpackage i18n.libs
+ * @see PluginShortRoute
+ */
+class PluginShortI18nRoute extends I18nRoute {
+/**
+* Class name - Workaround to be able to extend this class without breaking exixtent features
+*
+* @var string
+*/
+ public $name = __CLASS__;
+
+/**
+ * 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
+ */
+ public 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.
+ */
+ public 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;
+ }
+
+}
113 tests/cases/libs/i18n_route.test.php
View
@@ -11,10 +11,6 @@
App::import('Lib', array('I18n.I18nRoute'));
-if (!defined('FULL_BASE_URL')) {
- define('FULL_BASE_URL', 'http://cakephp.org');
-}
-
/**
* Test case for i18nroute
*
@@ -238,7 +234,6 @@ public function testParsing() {
);
$this->assertEqual($result, $expected);
}
-
/**
* Test connecting the default routes with i18n
@@ -257,12 +252,11 @@ public function testConnectDefaultRoutes() {
Router::reload();
I18nRoute::connectDefaultRoutes();
- $plugins = App::objects('plugin');
- $plugin = Inflector::underscore($plugins[0]);
- $result = Router::url(array('plugin' => $plugin, 'controller' => 'js_file', 'action' => 'index'));
+
+ $result = Router::url(array('plugin' => 'plugin_js', 'controller' => 'js_file', 'action' => 'index'));
$this->assertEqual($result, '/spa/plugin_js/js_file');
- $result = Router::url(array('plugin' => $plugin, 'controller' => 'js_file', 'action' => 'index', 'admin' => true));
+ $result = Router::url(array('plugin' => 'plugin_js', 'controller' => 'js_file', 'action' => 'index', 'admin' => true));
$this->assertEqual($result, '/spa/admin/plugin_js/js_file');
@@ -286,7 +280,8 @@ public function testConnectDefaultRoutes() {
unset($expected['admin'], $expected['prefix']);
$this->assertEqual($result, $expected);
- /* TODO Implement short plugin routes
+
+ // Short plugin syntax
$result = Router::url(array('plugin' => 'test_plugin', 'controller' => 'test_plugin', 'action' => 'index'));
$this->assertEqual($result, '/spa/test_plugin');
@@ -295,14 +290,12 @@ public function testConnectDefaultRoutes() {
'plugin' => 'test_plugin', 'controller' => 'test_plugin', 'action' => 'index',
'named' => array(), 'pass' => array(), 'lang' => $this->__defaultLang
);
- $this->assertEqual($result, $expected, 'Plugin shortcut route broken. %s');*/
- $result = Router::url(array('plugin' => 'test_plugin', 'controller' => 'test_plugin', 'action' => 'index'));
- $this->assertEqual($result, '/spa/test_plugin/test_plugin');
+ $this->assertEqual($result, $expected, 'Plugin shortcut route broken. %s');
- $result = Router::parse('/test_plugin/test_plugin');
+ $result = Router::parse('/spa/test_plugin');
$expected = array(
'plugin' => 'test_plugin', 'controller' => 'test_plugin', 'action' => 'index',
- 'named' => array(), 'pass' => array(), 'lang' => $this->__defaultLang
+ 'named' => array(), 'pass' => array(), 'lang' => 'spa'
);
$this->assertEqual($result, $expected, 'Plugin shortcut route broken. %s');
}
@@ -334,4 +327,94 @@ public function testPromoteLangRoutes() {
$this->assertIdentical($Router->routes[5], $beforePromotionRoutes[4]);
$this->assertIdentical($Router->routes[6], $beforePromotionRoutes[6]);
}
+
}
+
+/**
+ * Test case for PluginShortI18nRoute
+ *
+ * @package i18n
+ * @author i18n.test.cases.libs
+ */
+class PluginShortI18nRouteTestCase extends CakeTestCase {
+/**
+ * Default language of the application
+ *
+ * @var string
+ */
+ private $__defaultLang = 'eng';
+
+/**
+ * startTest method
+ *
+ * @return void
+ */
+ public function startTest() {
+ $this->_routing = Configure::read('Routing');
+ $this->_config = Configure::read('Config');
+ Configure::write('Config.language', 'spa');
+ Configure::write('Config.languages', array('eng', 'fre', 'spa'));
+ Configure::write('Routing', array('admin' => null, 'prefixes' => array()));
+
+ if (defined('DEFAULT_LANGUAGE')) {
+ $this->__defaultLang = DEFAULT_LANGUAGE;
+ } else {
+ define('DEFAULT_LANGUAGE', $this->__defaultLang);
+ }
+
+ PluginShortI18nRoute::reload();
+ }
+
+/**
+ * end the test and reset the environment
+ *
+ * @return void
+ * @access public
+ */
+ public function endTest() {
+ Configure::write('Routing', $this->_routing);
+ Configure::write('Config', $this->_config);
+ }
+
+/**
+ * test the parsing of routes.
+ *
+ * @return void
+ */
+ public function testParsing() {
+ Router::defaults(false);
+ Router::connect('/:plugin', array('action' => 'index'), array('routeClass' => 'PluginShortI18nRoute', 'plugin' => 'foo|bar'));
+ // This call is needed to work since the "default language" route is created from the constructor
+ PluginShortI18nRoute::promoteLangRoutes();
+
+ $result = Router::parse('/foo');
+ $this->assertEqual($result['plugin'], 'foo');
+ $this->assertEqual($result['controller'], 'foo');
+ $this->assertEqual($result['action'], 'index');
+ $this->assertEqual($result['lang'], $this->__defaultLang);
+
+ $result = Router::parse('/spa/foo');
+ $this->assertEqual($result['plugin'], 'foo');
+ $this->assertEqual($result['controller'], 'foo');
+ $this->assertEqual($result['action'], 'index');
+ $this->assertEqual($result['lang'], 'spa');
+
+ $result = Router::parse('/wrong');
+ $this->assertTrue(empty($result['plugin']), 'Wrong plugin name matched %s');
+ }
+
+/**
+ * test the reverse routing of the plugin shortcut urls.
+ *
+ * @return void
+ */
+ function testMatch() {
+ $route = new PluginShortI18nRoute('/: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, '/spa/foo');
+ }
+}
Please sign in to comment.
Something went wrong with that request. Please try again.