Browse Source

Fix failing tests around prefixes.

There were commented out tests for prefixes + controller routes. Using
a prefix should create more specific route names that match earlier
preventing names like `users:_action` from matching.

* Fix up name generation for plugin/prefixes with 0 as a name.
* Remove bad Controller test for prefix.
* Remove duplicate route prefix test.
mark_story 11 years ago
parent
commit
12c5f5bb72

+ 19 - 14
src/Routing/Route/Route.php

@@ -227,22 +227,27 @@ class Route {
 			return $this->_name;
 		}
 		$name = '';
-		if (isset($this->defaults['plugin'])) {
-			$name = $this->defaults['plugin'] . '.';
-		}
-		if (strpos($this->template, ':plugin') !== false) {
-			$name = '_plugin.';
-		}
-		foreach (array('controller', 'action') as $key) {
-			if ($key === 'action') {
-				$name .= ':';
-			}
-			$var = ':' . $key;
-			if (strpos($this->template, $var) !== false) {
-				$name .= '_' . $key;
+		$keys = [
+			'prefix' => ':',
+			'plugin' => '.',
+			'controller' => ':',
+			'action' => ''
+		];
+		foreach ($keys as $key => $glue) {
+			$value = null;
+			if (strpos($this->template, ':' . $key) !== false) {
+				$value = '_' . $key;
 			} elseif (isset($this->defaults[$key])) {
-				$name .= $this->defaults[$key];
+				$value = $this->defaults[$key];
+			}
+
+			if ($value === null) {
+				continue;
+			}
+			if (is_bool($value)) {
+				$value = $value ? '1' : '0';
 			}
+			$name .= $value . $glue;
 		}
 		return $this->_name = strtolower($name);
 	}

+ 51 - 6
src/Routing/RouteCollection.php

@@ -120,31 +120,76 @@ class RouteCollection {
  */
 	protected function _getNames($url) {
 		$plugin = false;
-		if (isset($url['plugin'])) {
+		if (isset($url['plugin']) && $url['plugin'] !== false) {
 			$plugin = strtolower($url['plugin']);
 		}
+		$prefix = false;
+		if (isset($url['prefix']) && $url['prefix'] !== false) {
+			$prefix = strtolower($url['prefix']);
+		}
 		$controller = strtolower($url['controller']);
 		$action = strtolower($url['action']);
 
-		$fallbacks = [
+		$names = [
 			"${controller}:${action}",
 			"${controller}:_action",
 			"_controller:${action}",
 			"_controller:_action"
 		];
-		if ($plugin) {
-			$fallbacks = [
+
+		// No prefix, no plugin
+		if ($prefix === false && $plugin === false) {
+			return $names;
+		}
+
+		// Only a plugin
+		if ($prefix === false) {
+			return [
 				"${plugin}.${controller}:${action}",
 				"${plugin}.${controller}:_action",
 				"${plugin}._controller:${action}",
 				"${plugin}._controller:_action",
 				"_plugin.${controller}:${action}",
+				"_plugin.${controller}:_action",
 				"_plugin._controller:${action}",
 				"_plugin._controller:_action",
-				"_controller:_action"
 			];
 		}
-		return $fallbacks;
+
+		// Only a prefix
+		if ($plugin === false) {
+			return [
+				"${prefix}:${controller}:${action}",
+				"${prefix}:${controller}:_action",
+				"${prefix}:_controller:${action}",
+				"${prefix}:_controller:_action",
+				"_prefix:${controller}:${action}",
+				"_prefix:${controller}:_action",
+				"_prefix:_controller:${action}",
+				"_prefix:_controller:_action"
+			];
+		}
+
+		// Prefix and plugin has the most options
+		// as there are 4 factors.
+		return [
+			"${prefix}:${plugin}.${controller}:${action}",
+			"${prefix}:${plugin}.${controller}:_action",
+			"${prefix}:${plugin}._controller:${action}",
+			"${prefix}:${plugin}._controller:_action",
+			"${prefix}:_plugin.${controller}:${action}",
+			"${prefix}:_plugin.${controller}:_action",
+			"${prefix}:_plugin._controller:${action}",
+			"${prefix}:_plugin._controller:_action",
+			"_prefix:${plugin}.${controller}:${action}",
+			"_prefix:${plugin}.${controller}:_action",
+			"_prefix:${plugin}._controller:${action}",
+			"_prefix:${plugin}._controller:_action",
+			"_prefix:_plugin.${controller}:${action}",
+			"_prefix:_plugin.${controller}:_action",
+			"_prefix:_plugin._controller:${action}",
+			"_prefix:_plugin._controller:_action",
+		];
 	}
 
 /**

+ 0 - 20
tests/TestCase/Controller/ControllerTest.php

@@ -803,26 +803,6 @@ class ControllerTest extends TestCase {
 /**
  * test invoking controller methods.
  *
- * @expectedException \Cake\Controller\Error\PrivateActionException
- * @expectedExceptionMessage Private Action TestController::admin_add() is not directly accessible.
- * @return void
- */
-	public function testInvokeActionPrefixProtection() {
-		Configure::write('Routing.prefixes', array('admin'));
-		Router::reload();
-		Router::connect('/admin/:controller/:action/*', array('prefix' => 'admin'));
-
-		$url = new Request('test/admin_add/');
-		$url->addParams(array('controller' => 'test_controller', 'action' => 'admin_add'));
-		$response = $this->getMock('Cake\Network\Response');
-
-		$Controller = new TestController($url, $response);
-		$Controller->invokeAction();
-	}
-
-/**
- * test invoking controller methods.
- *
  * @return void
  */
 	public function testInvokeActionReturnValue() {

+ 31 - 1
tests/TestCase/Routing/Route/RouteTest.php

@@ -22,7 +22,6 @@ use Cake\TestSuite\TestCase;
 
 /**
  * Test case for Route
- *
  */
 class RouteTest extends TestCase {
 
@@ -881,6 +880,37 @@ class RouteTest extends TestCase {
 	}
 
 /**
+ * Test getName() with prefixes.
+ *
+ * @return void
+ */
+	public function testGetNamePrefix() {
+		$route = new Route(
+			'/admin/:controller/:action',
+			array('prefix' => 'admin')
+		);
+		$this->assertEquals('admin:_controller:_action', $route->getName());
+
+		$route = new Route(
+			'/:prefix/assets/:action',
+			array('controller' => 'assets')
+		);
+		$this->assertEquals('_prefix:assets:_action', $route->getName());
+
+		$route = new Route(
+			'/admin/assets/get',
+			array('prefix' => 'admin', 'plugin' => 'asset', 'controller' => 'assets', 'action' => 'get')
+		);
+		$this->assertEquals('admin:asset.assets:get', $route->getName());
+
+		$route = new Route(
+			'/:prefix/:plugin/:controller/:action/*',
+			[]
+		);
+		$this->assertEquals('_prefix:_plugin._controller:_action', $route->getName());
+	}
+
+/**
  * test that utf-8 patterns work for :section
  *
  * @return void

+ 2 - 2
tests/TestCase/Routing/RouteBuilderTest.php

@@ -280,10 +280,10 @@ class RouteBuilderTest extends TestCase {
  * @return void
  */
 	public function testFallbacks() {
-		$routes = new ScopedRouteCollection('/api', ['prefix' => 'api']);
+		$routes = new RouteBuilder($this->collection, '/api', ['prefix' => 'api']);
 		$routes->fallbacks();
 
-		$all = $routes->routes();
+		$all = $this->collection->routes();
 		$this->assertEquals('/api/:controller', $all[0]->template);
 		$this->assertEquals('/api/:controller/:action/*', $all[1]->template);
 	}

+ 0 - 4
tests/TestCase/Routing/RouterTest.php

@@ -2055,10 +2055,6 @@ class RouterTest extends TestCase {
 		$expected = '/company/users/login';
 		$this->assertEquals($expected, $result);
 
-		$result = Router::url(array('controller' => 'users', 'action' => 'login', 'prefix' => 'company'));
-		$expected = '/company/users/login';
-		$this->assertEquals($expected, $result);
-
 		$request = new Request();
 		Router::setRequestInfo(
 			$request->addParams(array(