Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Implemented plugin short i18n routes - closes #1

  • Loading branch information...
commit 820a9cc14e11a6ec27c081ba2c43ae4f2bb1e05b 1 parent acd3d39
Pierre Martin authored

Showing 2 changed files with 152 additions and 16 deletions. Show diff stats Hide diff stats

  1. +54 1 libs/i18n_route.php
  2. +98 15 tests/cases/libs/i18n_route.test.php
55 libs/i18n_route.php
@@ -108,7 +108,6 @@ public function parse($url) {
108 108 * Connects the default, built-in routes, including prefix and plugin routes with the i18n custom Route
109 109 * Code mostly duplicated from Router::__connectDefaultRoutes
110 110 *
111   - * @TODO Add Short route for plugins
112 111 * @see Router::__connectDefaultRoutes
113 112 * @param array $pluginExceptions Plugins ommited from the lang default routing
114 113 * @return void
@@ -127,13 +126,16 @@ public static function connectDefaultRoutes($pluginExceptions = array()) {
127 126
128 127 $pluginPattern = implode('|', $plugins);
129 128 $match = array('plugin' => $pluginPattern) + $options;
  129 + $shortParams = array('routeClass' => 'PluginShortI18nRoute', 'plugin' => $pluginPattern);
130 130
131 131 foreach ($prefixes as $prefix) {
132 132 $params = array('prefix' => $prefix, $prefix => true);
133 133 $indexParams = $params + array('action' => 'index');
  134 + Router::connect("/{$prefix}/:plugin", $indexParams, $shortParams);
134 135 Router::connect("/{$prefix}/:plugin/:controller", $indexParams, $match);
135 136 Router::connect("/{$prefix}/:plugin/:controller/:action/*", $params, $match);
136 137 }
  138 + Router::connect('/:plugin', array('action' => 'index'), $shortParams);
137 139 Router::connect('/:plugin/:controller', array('action' => 'index'), $match);
138 140 Router::connect('/:plugin/:controller/:action/*', array(), $match);
139 141 }
@@ -192,3 +194,54 @@ public static function reload() {
192 194 Router::reload();
193 195 }
194 196 }
  197 +
  198 +/**
  199 + * Plugin short route, that copies the plugin param to the controller parameters
  200 + * It is used for supporting /:plugin routes.
  201 + *
  202 + * @package i18n
  203 + * @subpackage i18n.libs
  204 + * @see PluginShortRoute
  205 + */
  206 +class PluginShortI18nRoute extends I18nRoute {
  207 +/**
  208 +* Class name - Workaround to be able to extend this class without breaking exixtent features
  209 +*
  210 +* @var string
  211 +*/
  212 + public $name = __CLASS__;
  213 +
  214 +/**
  215 + * Parses a string url into an array. If a plugin key is found, it will be copied to the
  216 + * controller parameter
  217 + *
  218 + * @param string $url The url to parse
  219 + * @return mixed false on failure, or an array of request parameters
  220 + */
  221 + public function parse($url) {
  222 + $params = parent::parse($url);
  223 + if (!$params) {
  224 + return false;
  225 + }
  226 + $params['controller'] = $params['plugin'];
  227 + return $params;
  228 + }
  229 +
  230 +/**
  231 + * Reverse route plugin shortcut urls. If the plugin and controller
  232 + * are not the same the match is an auto fail.
  233 + *
  234 + * @param array $url Array of parameters to convert to a string.
  235 + * @return mixed either false or a string url.
  236 + */
  237 + public function match($url) {
  238 + if (isset($url['controller']) && isset($url['plugin']) && $url['plugin'] != $url['controller']) {
  239 + return false;
  240 + }
  241 + $this->defaults['controller'] = $url['controller'];
  242 + $result = parent::match($url);
  243 + unset($this->defaults['controller']);
  244 + return $result;
  245 + }
  246 +
  247 +}
113 tests/cases/libs/i18n_route.test.php
@@ -11,10 +11,6 @@
11 11
12 12 App::import('Lib', array('I18n.I18nRoute'));
13 13
14   -if (!defined('FULL_BASE_URL')) {
15   - define('FULL_BASE_URL', 'http://cakephp.org');
16   -}
17   -
18 14 /**
19 15 * Test case for i18nroute
20 16 *
@@ -238,7 +234,6 @@ public function testParsing() {
238 234 );
239 235 $this->assertEqual($result, $expected);
240 236 }
241   -
242 237
243 238 /**
244 239 * Test connecting the default routes with i18n
@@ -257,12 +252,11 @@ public function testConnectDefaultRoutes() {
257 252 Router::reload();
258 253 I18nRoute::connectDefaultRoutes();
259 254
260   - $plugins = App::objects('plugin');
261   - $plugin = Inflector::underscore($plugins[0]);
262   - $result = Router::url(array('plugin' => $plugin, 'controller' => 'js_file', 'action' => 'index'));
  255 +
  256 + $result = Router::url(array('plugin' => 'plugin_js', 'controller' => 'js_file', 'action' => 'index'));
263 257 $this->assertEqual($result, '/spa/plugin_js/js_file');
264 258
265   - $result = Router::url(array('plugin' => $plugin, 'controller' => 'js_file', 'action' => 'index', 'admin' => true));
  259 + $result = Router::url(array('plugin' => 'plugin_js', 'controller' => 'js_file', 'action' => 'index', 'admin' => true));
266 260 $this->assertEqual($result, '/spa/admin/plugin_js/js_file');
267 261
268 262
@@ -286,7 +280,8 @@ public function testConnectDefaultRoutes() {
286 280 unset($expected['admin'], $expected['prefix']);
287 281 $this->assertEqual($result, $expected);
288 282
289   - /* TODO Implement short plugin routes
  283 +
  284 + // Short plugin syntax
290 285 $result = Router::url(array('plugin' => 'test_plugin', 'controller' => 'test_plugin', 'action' => 'index'));
291 286 $this->assertEqual($result, '/spa/test_plugin');
292 287
@@ -295,14 +290,12 @@ public function testConnectDefaultRoutes() {
295 290 'plugin' => 'test_plugin', 'controller' => 'test_plugin', 'action' => 'index',
296 291 'named' => array(), 'pass' => array(), 'lang' => $this->__defaultLang
297 292 );
298   - $this->assertEqual($result, $expected, 'Plugin shortcut route broken. %s');*/
299   - $result = Router::url(array('plugin' => 'test_plugin', 'controller' => 'test_plugin', 'action' => 'index'));
300   - $this->assertEqual($result, '/spa/test_plugin/test_plugin');
  293 + $this->assertEqual($result, $expected, 'Plugin shortcut route broken. %s');
301 294
302   - $result = Router::parse('/test_plugin/test_plugin');
  295 + $result = Router::parse('/spa/test_plugin');
303 296 $expected = array(
304 297 'plugin' => 'test_plugin', 'controller' => 'test_plugin', 'action' => 'index',
305   - 'named' => array(), 'pass' => array(), 'lang' => $this->__defaultLang
  298 + 'named' => array(), 'pass' => array(), 'lang' => 'spa'
306 299 );
307 300 $this->assertEqual($result, $expected, 'Plugin shortcut route broken. %s');
308 301 }
@@ -334,4 +327,94 @@ public function testPromoteLangRoutes() {
334 327 $this->assertIdentical($Router->routes[5], $beforePromotionRoutes[4]);
335 328 $this->assertIdentical($Router->routes[6], $beforePromotionRoutes[6]);
336 329 }
  330 +
337 331 }
  332 +
  333 +/**
  334 + * Test case for PluginShortI18nRoute
  335 + *
  336 + * @package i18n
  337 + * @author i18n.test.cases.libs
  338 + */
  339 +class PluginShortI18nRouteTestCase extends CakeTestCase {
  340 +/**
  341 + * Default language of the application
  342 + *
  343 + * @var string
  344 + */
  345 + private $__defaultLang = 'eng';
  346 +
  347 +/**
  348 + * startTest method
  349 + *
  350 + * @return void
  351 + */
  352 + public function startTest() {
  353 + $this->_routing = Configure::read('Routing');
  354 + $this->_config = Configure::read('Config');
  355 + Configure::write('Config.language', 'spa');
  356 + Configure::write('Config.languages', array('eng', 'fre', 'spa'));
  357 + Configure::write('Routing', array('admin' => null, 'prefixes' => array()));
  358 +
  359 + if (defined('DEFAULT_LANGUAGE')) {
  360 + $this->__defaultLang = DEFAULT_LANGUAGE;
  361 + } else {
  362 + define('DEFAULT_LANGUAGE', $this->__defaultLang);
  363 + }
  364 +
  365 + PluginShortI18nRoute::reload();
  366 + }
  367 +
  368 +/**
  369 + * end the test and reset the environment
  370 + *
  371 + * @return void
  372 + * @access public
  373 + */
  374 + public function endTest() {
  375 + Configure::write('Routing', $this->_routing);
  376 + Configure::write('Config', $this->_config);
  377 + }
  378 +
  379 +/**
  380 + * test the parsing of routes.
  381 + *
  382 + * @return void
  383 + */
  384 + public function testParsing() {
  385 + Router::defaults(false);
  386 + Router::connect('/:plugin', array('action' => 'index'), array('routeClass' => 'PluginShortI18nRoute', 'plugin' => 'foo|bar'));
  387 + // This call is needed to work since the "default language" route is created from the constructor
  388 + PluginShortI18nRoute::promoteLangRoutes();
  389 +
  390 + $result = Router::parse('/foo');
  391 + $this->assertEqual($result['plugin'], 'foo');
  392 + $this->assertEqual($result['controller'], 'foo');
  393 + $this->assertEqual($result['action'], 'index');
  394 + $this->assertEqual($result['lang'], $this->__defaultLang);
  395 +
  396 + $result = Router::parse('/spa/foo');
  397 + $this->assertEqual($result['plugin'], 'foo');
  398 + $this->assertEqual($result['controller'], 'foo');
  399 + $this->assertEqual($result['action'], 'index');
  400 + $this->assertEqual($result['lang'], 'spa');
  401 +
  402 + $result = Router::parse('/wrong');
  403 + $this->assertTrue(empty($result['plugin']), 'Wrong plugin name matched %s');
  404 + }
  405 +
  406 +/**
  407 + * test the reverse routing of the plugin shortcut urls.
  408 + *
  409 + * @return void
  410 + */
  411 + function testMatch() {
  412 + $route = new PluginShortI18nRoute('/:plugin', array('action' => 'index'), array('plugin' => 'foo|bar'));
  413 +
  414 + $result = $route->match(array('plugin' => 'foo', 'controller' => 'posts', 'action' => 'index'));
  415 + $this->assertFalse($result, 'plugin controller mismatch was converted. %s');
  416 +
  417 + $result = $route->match(array('plugin' => 'foo', 'controller' => 'foo', 'action' => 'index'));
  418 + $this->assertEqual($result, '/spa/foo');
  419 + }
  420 +}

0 comments on commit 820a9cc

Please sign in to comment.
Something went wrong with that request. Please try again.