Browse Source

Fix prefixed controllers not working with testAction().

The generated controller class needs to be aware that it is in a prefix
and adjust the classname + viewPath accordingly.

Refs #3636
mark_story 11 years ago
parent
commit
7395d5d414

+ 18 - 11
src/TestSuite/ControllerTestCase.php

@@ -283,9 +283,16 @@ abstract class ControllerTestCase extends TestCase {
 		if ($this->_dirtyController) {
 			$this->controller = null;
 		}
-		$plugin = empty($request->params['plugin']) ? '' : Inflector::camelize($request->params['plugin']) . '.';
 		if ($this->controller === null && $this->autoMock) {
-			$this->generate($plugin . Inflector::camelize($request->params['controller']));
+			$plugin = '';
+			if (!empty($request->params['plugin'])) {
+				$plugin = Inflector::camelize($request->params['plugin']) . '.';
+			}
+			$controllerName = Inflector::camelize($request->params['controller']);
+			if (!empty($request->params['prefix'])) {
+				$controllerName = Inflector::camelize($request->params['prefix']) . '/' . $controllerName;
+			}
+			$this->generate($plugin . $controllerName, [], $request);
 		}
 		$params = array();
 		if ($options['return'] === 'result') {
@@ -313,9 +320,7 @@ abstract class ControllerTestCase extends TestCase {
 	}
 
 /**
- * Generates a mocked controller and mocks any classes passed to `$mocks`. By
- * default, `stop()` is stubbed as is sending the response headers, so to not
- * interfere with testing.
+ * Generates a mocked controller and mocks any classes passed to `$mocks`.
  *
  * ### Mocks:
  *
@@ -329,11 +334,13 @@ abstract class ControllerTestCase extends TestCase {
  *
  * @param string $controller Controller name
  * @param array $mocks List of classes and methods to mock
+ * @param \Cake\Network\Request $request A request object to build the controller with.
+ *   This parameter is required when mocking prefixed controllers.
  * @return \Cake\Controller\Controller Mocked controller
  * @throws \Cake\Controller\Error\MissingControllerException When controllers could not be created.
  * @throws \Cake\Controller\Error\MissingComponentException When components could not be created.
  */
-	public function generate($controller, array $mocks = array()) {
+	public function generate($controller, array $mocks = array(), $request = null) {
 		$className = App::className($controller, 'Controller', 'Controller');
 		if (!$className) {
 			list($plugin, $controller) = pluginSplit($controller);
@@ -343,15 +350,15 @@ abstract class ControllerTestCase extends TestCase {
 			));
 		}
 
-		$mocks = array_merge(array(
+		$mocks += [
 			'methods' => null,
-			'models' => array(),
-			'components' => array()
-		), $mocks);
+			'models' => [],
+			'components' => []
+		];
 		list(, $controllerName) = namespaceSplit($className);
 		$name = substr($controllerName, 0, -10);
 
-		$request = $this->getMock('Cake\Network\Request');
+		$request = $request ?: $this->getMock('Cake\Network\Request');
 		$response = $this->getMock('Cake\Network\Response', array('_sendHeader', 'stop'));
 		$controller = $this->getMock(
 			$className,

+ 18 - 0
tests/TestCase/TestSuite/ControllerTestCaseTest.php

@@ -199,6 +199,24 @@ class ControllerTestCaseTest extends TestCase {
 	}
 
 /**
+ * Test testAction() with prefix routes.
+ *
+ * @return void
+ */
+	public function testActionWithPrefix() {
+		Configure::write('Routing.prefixes', ['admin']);
+		Plugin::load('TestPlugin');
+
+		$result = $this->Case->testAction('/admin/posts/index', ['return' => 'view']);
+		$expected = '<h1>Admin Post Index</h1>';
+		$this->assertContains($expected, $result);
+
+		$result = $this->Case->testAction('/admin/test_plugin/comments/index', ['return' => 'view']);
+		$expected = '<h1>TestPlugin Admin Comments</h1>';
+		$this->assertContains($expected, $result);
+	}
+
+/**
  * Make sure testAction() can hit plugin controllers.
  *
  * @return void

+ 1 - 0
tests/test_app/Plugin/TestPlugin/Template/Admin/Comments/index.ctp

@@ -0,0 +1 @@
+<h1>TestPlugin Admin Comments</h1>

+ 1 - 0
tests/test_app/TestApp/Template/Admin/Posts/index.ctp

@@ -0,0 +1 @@
+<h1>Admin Post Index</h1>