Browse Source

Merge pull request #4081 from cakephp/issue-4064

Fix incorrect order of view paths for themed plugins
José Lorenzo Rodríguez 11 years ago
parent
commit
c76fa51238
2 changed files with 37 additions and 19 deletions
  1. 8 18
      src/View/View.php
  2. 29 1
      tests/TestCase/View/ViewTest.php

+ 8 - 18
src/View/View.php

@@ -930,39 +930,29 @@ class View {
 				return $this->_pathsForPlugin[$plugin];
 			}
 		}
-		$paths = array();
 		$viewPaths = App::path('Template');
-		$corePaths = App::core('Template');
-
+		$pluginPaths = $themePaths = [];
 		if (!empty($plugin)) {
-			$count = count($viewPaths);
-			for ($i = 0; $i < $count; $i++) {
-				$paths[] = $viewPaths[$i] . 'Plugin' . DS . $plugin . DS;
+			for ($i = 0, $count = count($viewPaths); $i < $count; $i++) {
+				$pluginPaths[] = $viewPaths[$i] . 'Plugin' . DS . $plugin . DS;
 			}
-			$paths = array_merge($paths, App::path('Template', $plugin));
+			$pluginPaths = array_merge($pluginPaths, App::path('Template', $plugin));
 		}
 
-		$paths = array_unique(array_merge($paths, $viewPaths));
 		if (!empty($this->theme)) {
-			$theme = Inflector::camelize($this->theme);
-			$themePaths = App::path('Template', $theme);
+			$themePaths = App::path('Template', Inflector::camelize($this->theme));
 
 			if ($plugin) {
-				$count = count($viewPaths);
-				for ($i = 0; $i < $count; $i++) {
-					$themePaths[] = $themePaths[$i] . 'Plugin' . DS . $plugin . DS;
+				for ($i = 0, $count = count($viewPaths); $i < $count; $i++) {
+					array_unshift($themePaths, $themePaths[$i] . 'Plugin' . DS . $plugin . DS);
 				}
 			}
-
-			$paths = array_merge($themePaths, $paths);
 		}
-
-		$paths = array_merge($paths, $corePaths);
+		$paths = array_merge($themePaths, $pluginPaths, $viewPaths, App::core('Template'));
 
 		if ($plugin !== null) {
 			return $this->_pathsForPlugin[$plugin] = $paths;
 		}
-
 		return $this->_paths = $paths;
 	}
 

+ 29 - 1
tests/TestCase/View/ViewTest.php

@@ -422,7 +422,7 @@ class ViewTest extends TestCase {
  *
  * @return void
  */
-	public function testPluginPathGeneration() {
+	public function testPathPluginGeneration() {
 		$viewOptions = ['plugin' => 'TestPlugin',
 			'name' => 'TestPlugin',
 			'viewPath' => 'Tests',
@@ -446,6 +446,34 @@ class ViewTest extends TestCase {
 	}
 
 /**
+ * Test that themed plugin paths are generated correctly.
+ *
+ * @return void
+ */
+	public function testPathThemedPluginGeneration() {
+		$viewOptions = ['plugin' => 'TestPlugin',
+			'name' => 'TestPlugin',
+			'viewPath' => 'Tests',
+			'view' => 'index',
+			'theme' => 'TestTheme'
+		];
+
+		$View = new TestView(null, null, null, $viewOptions);
+		$paths = $View->paths('TestPlugin');
+		$pluginPath = Plugin::path('TestPlugin');
+		$themePath = Plugin::path('TestTheme');
+		$expected = array(
+			$themePath . 'src' . DS . 'Template' . DS . 'Plugin' . DS . 'TestPlugin' . DS,
+			$themePath . 'src' . DS . 'Template' . DS,
+			TEST_APP . 'TestApp' . DS . 'Template' . DS . 'Plugin' . DS . 'TestPlugin' . DS,
+			$pluginPath . 'src' . DS . 'Template' . DS,
+			TEST_APP . 'TestApp' . DS . 'Template' . DS,
+			CAKE . 'Template' . DS,
+		);
+		$this->assertPathEquals($expected, $paths);
+	}
+
+/**
  * Test that CamelCase'd plugins still find their view files.
  *
  * @return void