From 4f577c0510fbf53a01e6c268686cc16f1867cd14 Mon Sep 17 00:00:00 2001 From: Yves P Date: Thu, 26 Mar 2015 22:01:30 +0100 Subject: [PATCH] Elements should switch to prefixed template path if available --- src/View/View.php | 47 ++++++++++---- .../TestCase/View/Helper/FlashHelperTest.php | 14 +++++ tests/TestCase/View/ViewTest.php | 61 +++++++++++++++++++ .../Template/Admin/Element/plugin_element.ctp | 1 + .../Admin/Element/test_plugin_element.ctp | 1 + .../Template/Element/Flash/plugin_element.ctp | 2 +- .../src/Template/Element/plugin_element.ctp | 2 +- .../Template/Element/test_plugin_element.ctp | 2 +- .../Template/Admin/Element/Flash/default.ctp | 1 + .../Admin/Element/extended_element.ctp | 2 + .../Template/Admin/Element/prefix_element.ctp | 1 + .../Template/Admin/Element/test_element.ctp | 1 + .../TestApp/Template/Element/test_element.ctp | 2 +- .../BarPrefix/Element/prefix_element.ctp | 1 + 14 files changed, 121 insertions(+), 17 deletions(-) create mode 100644 tests/test_app/Plugin/TestPlugin/src/Template/Admin/Element/plugin_element.ctp create mode 100644 tests/test_app/Plugin/TestPlugin/src/Template/Admin/Element/test_plugin_element.ctp create mode 100644 tests/test_app/TestApp/Template/Admin/Element/Flash/default.ctp create mode 100644 tests/test_app/TestApp/Template/Admin/Element/extended_element.ctp create mode 100644 tests/test_app/TestApp/Template/Admin/Element/prefix_element.ctp create mode 100644 tests/test_app/TestApp/Template/Admin/Element/test_element.ctp create mode 100644 tests/test_app/TestApp/Template/FooPrefix/BarPrefix/Element/prefix_element.ctp diff --git a/src/View/View.php b/src/View/View.php index 3fe04fd611a..5a0a92c6667 100644 --- a/src/View/View.php +++ b/src/View/View.php @@ -964,17 +964,7 @@ protected function _getLayoutFileName($name = null) } list($plugin, $name) = $this->pluginSplit($name); - $layoutPaths = ['Layout' . DS . $subDir]; - if (!empty($this->request->params['prefix'])) { - $prefixPath = array_map( - 'Cake\Utility\Inflector::camelize', - explode('/', $this->request->params['prefix']) - ); - array_unshift( - $layoutPaths, - implode('/', $prefixPath) . DS . $layoutPaths[0] - ); - } + $layoutPaths = $this->_getSubPaths('Layout' . DS . $subDir); foreach ($this->_paths($plugin) as $path) { foreach ($layoutPaths as $layoutPath) { @@ -1000,14 +990,45 @@ protected function _getElementFileName($name) list($plugin, $name) = $this->pluginSplit($name); $paths = $this->_paths($plugin); + $elementPaths = $this->_getSubPaths('Element'); + foreach ($paths as $path) { - if (file_exists($path . 'Element' . DS . $name . $this->_ext)) { - return $path . 'Element' . DS . $name . $this->_ext; + foreach ($elementPaths as $elementPath) { + if (file_exists($path . $elementPath . DS . $name . $this->_ext)) { + return $path . $elementPath . DS . $name . $this->_ext; + } } } return false; } + /** + * Find all sub templates path, based on $basePath + * If a prefix is defined in the current request, this method will prepend + * the prefixed template path to the $basePath. + * This is essentially used to find prefixed template paths for elements + * and layouts. + * + * @param string $basePath Base path on which to get the prefixed one. + * @return array Array with all the templates paths. + */ + protected function _getSubPaths($basePath) + { + $paths = [$basePath]; + if (!empty($this->request->params['prefix'])) { + $prefixPath = array_map( + 'Cake\Utility\Inflector::camelize', + explode('/', $this->request->params['prefix']) + ); + array_unshift( + $paths, + implode('/', $prefixPath) . DS . $basePath + ); + } + + return $paths; + } + /** * Return all possible paths to find view files in order * diff --git a/tests/TestCase/View/Helper/FlashHelperTest.php b/tests/TestCase/View/Helper/FlashHelperTest.php index a0105cd72b7..c5b38f4d306 100644 --- a/tests/TestCase/View/Helper/FlashHelperTest.php +++ b/tests/TestCase/View/Helper/FlashHelperTest.php @@ -169,4 +169,18 @@ public function testFlashWithTheme() $expected = 'flash element from TestTheme'; $this->assertContains($expected, $result); } + + /** + * test that when View prefix is set, flash element from that prefix + * is used if available. + * + * @return void + */ + public function testFlashWithPrefix() + { + $this->View->request->params['prefix'] = 'Admin'; + $result = $this->Flash->render('flash'); + $expected = 'flash element from Admin prefix folder'; + $this->assertContains($expected, $result); + } } diff --git a/tests/TestCase/View/ViewTest.php b/tests/TestCase/View/ViewTest.php index 53c12a4781b..96770bf52da 100644 --- a/tests/TestCase/View/ViewTest.php +++ b/tests/TestCase/View/ViewTest.php @@ -816,6 +816,29 @@ public function testElement() $this->assertEquals('this is the test set using View::$plugin plugin element', $result); } + /** + * Test element method with a prefix + * + * @return void + */ + public function testPrefixElement() + { + $this->View->request->params['prefix'] = 'Admin'; + $result = $this->View->element('prefix_element'); + $this->assertEquals('this is a prefixed test element', $result); + + $result = $this->View->element('TestPlugin.plugin_element'); + $this->assertEquals('this is the plugin prefixed element using params[plugin]', $result); + + $this->View->plugin = 'TestPlugin'; + $result = $this->View->element('test_plugin_element'); + $this->assertEquals('this is the test set using View::$plugin plugin prefixed element', $result); + + $this->View->request->params['prefix'] = 'FooPrefix/BarPrefix'; + $result = $this->View->element('prefix_element'); + $this->assertEquals('this is a nested prefixed test element', $result); + } + /** * Test elementInexistent method * @@ -1710,6 +1733,26 @@ public function testExtendElement() Parent Element. Element content. +TEXT; + $this->assertEquals($expected, $content); + } + + /** + * Test extend() in an element and a view. + * + * @return void + */ + public function testExtendPrefixElement() + { + $this->View->request->params['prefix'] = 'Admin'; + $this->View->layout = false; + $content = $this->View->render('extend_element'); + $expected = <<assertEquals($expected, $content); } @@ -1745,6 +1788,24 @@ public function testExtendWithElementBeforeExtend() Parent View. this is the test elementThe view +TEXT; + $this->assertEquals($expected, $result); + } + + /** + * Test extend() preceeded by an element() + * + * @return void + */ + public function testExtendWithPrefixElementBeforeExtend() + { + $this->View->request->params['prefix'] = 'Admin'; + $this->View->layout = false; + $result = $this->View->render('extend_with_element'); + $expected = <<assertEquals($expected, $result); } diff --git a/tests/test_app/Plugin/TestPlugin/src/Template/Admin/Element/plugin_element.ctp b/tests/test_app/Plugin/TestPlugin/src/Template/Admin/Element/plugin_element.ctp new file mode 100644 index 00000000000..f80cf170fca --- /dev/null +++ b/tests/test_app/Plugin/TestPlugin/src/Template/Admin/Element/plugin_element.ctp @@ -0,0 +1 @@ +this is the plugin prefixed element using params[plugin] \ No newline at end of file diff --git a/tests/test_app/Plugin/TestPlugin/src/Template/Admin/Element/test_plugin_element.ctp b/tests/test_app/Plugin/TestPlugin/src/Template/Admin/Element/test_plugin_element.ctp new file mode 100644 index 00000000000..ee29882ccfe --- /dev/null +++ b/tests/test_app/Plugin/TestPlugin/src/Template/Admin/Element/test_plugin_element.ctp @@ -0,0 +1 @@ +this is the test set using View::$plugin plugin prefixed element \ No newline at end of file diff --git a/tests/test_app/Plugin/TestPlugin/src/Template/Element/Flash/plugin_element.ctp b/tests/test_app/Plugin/TestPlugin/src/Template/Element/Flash/plugin_element.ctp index 38aacfe5784..b92c5bb081b 100644 --- a/tests/test_app/Plugin/TestPlugin/src/Template/Element/Flash/plugin_element.ctp +++ b/tests/test_app/Plugin/TestPlugin/src/Template/Element/Flash/plugin_element.ctp @@ -1 +1 @@ - \ No newline at end of file +this is the plugin element \ No newline at end of file diff --git a/tests/test_app/Plugin/TestPlugin/src/Template/Element/plugin_element.ctp b/tests/test_app/Plugin/TestPlugin/src/Template/Element/plugin_element.ctp index dbc9779e207..3fcb903476d 100644 --- a/tests/test_app/Plugin/TestPlugin/src/Template/Element/plugin_element.ctp +++ b/tests/test_app/Plugin/TestPlugin/src/Template/Element/plugin_element.ctp @@ -1 +1 @@ - \ No newline at end of file +this is the plugin element using params[plugin] \ No newline at end of file diff --git a/tests/test_app/Plugin/TestPlugin/src/Template/Element/test_plugin_element.ctp b/tests/test_app/Plugin/TestPlugin/src/Template/Element/test_plugin_element.ctp index 976c7634bea..99cc002d28c 100644 --- a/tests/test_app/Plugin/TestPlugin/src/Template/Element/test_plugin_element.ctp +++ b/tests/test_app/Plugin/TestPlugin/src/Template/Element/test_plugin_element.ctp @@ -1 +1 @@ - \ No newline at end of file +this is the test set using View::$plugin plugin element \ No newline at end of file diff --git a/tests/test_app/TestApp/Template/Admin/Element/Flash/default.ctp b/tests/test_app/TestApp/Template/Admin/Element/Flash/default.ctp new file mode 100644 index 00000000000..acc5c2a0486 --- /dev/null +++ b/tests/test_app/TestApp/Template/Admin/Element/Flash/default.ctp @@ -0,0 +1 @@ +flash element from Admin prefix folder diff --git a/tests/test_app/TestApp/Template/Admin/Element/extended_element.ctp b/tests/test_app/TestApp/Template/Admin/Element/extended_element.ctp new file mode 100644 index 00000000000..0a807df91a4 --- /dev/null +++ b/tests/test_app/TestApp/Template/Admin/Element/extended_element.ctp @@ -0,0 +1,2 @@ +extend('parent_element'); ?> +Prefix Element content. diff --git a/tests/test_app/TestApp/Template/Admin/Element/prefix_element.ctp b/tests/test_app/TestApp/Template/Admin/Element/prefix_element.ctp new file mode 100644 index 00000000000..032a1fbd5aa --- /dev/null +++ b/tests/test_app/TestApp/Template/Admin/Element/prefix_element.ctp @@ -0,0 +1 @@ +this is a prefixed test element \ No newline at end of file diff --git a/tests/test_app/TestApp/Template/Admin/Element/test_element.ctp b/tests/test_app/TestApp/Template/Admin/Element/test_element.ctp new file mode 100644 index 00000000000..6641f6ed9c2 --- /dev/null +++ b/tests/test_app/TestApp/Template/Admin/Element/test_element.ctp @@ -0,0 +1 @@ +this is the test prefix element \ No newline at end of file diff --git a/tests/test_app/TestApp/Template/Element/test_element.ctp b/tests/test_app/TestApp/Template/Element/test_element.ctp index f26f9d9b83a..91ad34a2e8d 100644 --- a/tests/test_app/TestApp/Template/Element/test_element.ctp +++ b/tests/test_app/TestApp/Template/Element/test_element.ctp @@ -1 +1 @@ - \ No newline at end of file +this is the test element \ No newline at end of file diff --git a/tests/test_app/TestApp/Template/FooPrefix/BarPrefix/Element/prefix_element.ctp b/tests/test_app/TestApp/Template/FooPrefix/BarPrefix/Element/prefix_element.ctp new file mode 100644 index 00000000000..c7824830872 --- /dev/null +++ b/tests/test_app/TestApp/Template/FooPrefix/BarPrefix/Element/prefix_element.ctp @@ -0,0 +1 @@ +this is a nested prefixed test element \ No newline at end of file