Skip to content

Commit

Permalink
Elements should switch to prefixed template path if available
Browse files Browse the repository at this point in the history
  • Loading branch information
HavokInspiration committed Mar 31, 2015
1 parent b299e2a commit 4f577c0
Show file tree
Hide file tree
Showing 14 changed files with 121 additions and 17 deletions.
47 changes: 34 additions & 13 deletions src/View/View.php
Expand Up @@ -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) {
Expand All @@ -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
*
Expand Down
14 changes: 14 additions & 0 deletions tests/TestCase/View/Helper/FlashHelperTest.php
Expand Up @@ -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);
}
}
61 changes: 61 additions & 0 deletions tests/TestCase/View/ViewTest.php
Expand Up @@ -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
*
Expand Down Expand Up @@ -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 = <<<TEXT
Parent View.
View content.
Parent Element.
Prefix Element content.
TEXT;
$this->assertEquals($expected, $content);
}
Expand Down Expand Up @@ -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 = <<<TEXT
Parent View.
this is the test prefix elementThe view
TEXT;
$this->assertEquals($expected, $result);
}
Expand Down
@@ -0,0 +1 @@
this is the plugin prefixed element using params[plugin]
@@ -0,0 +1 @@
this is the test set using View::$plugin plugin prefixed element
@@ -1 +1 @@
<?= 'this is the plugin element'; ?>
this is the plugin element
@@ -1 +1 @@
<?= 'this is the plugin element using params[plugin]'; ?>
this is the plugin element using params[plugin]
@@ -1 +1 @@
<?= 'this is the test set using View::$plugin plugin element'; ?>
this is the test set using View::$plugin plugin element
@@ -0,0 +1 @@
flash element from Admin prefix folder
@@ -0,0 +1,2 @@
<?php $this->extend('parent_element'); ?>
Prefix Element content.
@@ -0,0 +1 @@
this is a prefixed test element
@@ -0,0 +1 @@
this is the test prefix element
2 changes: 1 addition & 1 deletion tests/test_app/TestApp/Template/Element/test_element.ctp
@@ -1 +1 @@
<?= 'this is the test element'; ?>
this is the test element
@@ -0,0 +1 @@
this is a nested prefixed test element

0 comments on commit 4f577c0

Please sign in to comment.