Skip to content
Permalink
Browse files

Fix duplicate subdirectories for file type views.

When a template path ends with the current subDir property we shouldn't
re-apply that subDir as it will result in bad paths.

Refs #11316
  • Loading branch information...
markstory committed Nov 7, 2017
1 parent e6e3179 commit 115a3547ab0c9120e3d2c6a3a2e6d2c3573ddc67
Showing with 32 additions and 3 deletions.
  1. +7 −3 src/View/View.php
  2. +25 −0 tests/TestCase/View/ViewTest.php
@@ -1243,12 +1243,16 @@ protected function _getViewFileName($name = null)
{
$templatePath = $subDir = '';
if ($this->subDir !== null) {
$subDir = $this->subDir . DIRECTORY_SEPARATOR;
}
if ($this->templatePath) {
$templatePath = $this->templatePath . DIRECTORY_SEPARATOR;
}
if (strlen($this->subDir)) {
$subDir = $this->subDir . DIRECTORY_SEPARATOR;
// Check if templatePath already terminates with subDir
if (strrpos($templatePath, $subDir) == strlen($templatePath) - strlen($subDir)) {
$subDir = '';
}
}
if ($name === null) {
$name = $this->template;
@@ -650,6 +650,31 @@ public function testGetViewFileNameDirectoryTraversal()
$view->getViewFileName('../../../../bootstrap');
}
/**
* Test getViewFileName doesn't re-apply existing subdirectories
*
* @return void
*/
public function testGetViewFileNameSubDir()
{
$viewOptions = [
'plugin' => null,
'name' => 'Posts',
'viewPath' => 'Posts/json',
'layoutPath' => 'json',
];
$view = new TestView(null, null, null, $viewOptions);
$expected = TEST_APP . 'TestApp' . DS . 'Template' . DS . 'Posts' . DS . 'json' . DS . 'index.ctp';
$result = $view->getViewFileName('index');
$this->assertPathEquals($expected, $result);
$view->subDir = 'json';
$result = $view->getViewFileName('index');
$expected = TEST_APP . 'TestApp' . DS . 'Template' . DS . 'Posts' . DS . 'json' . DS . 'index.ctp';
$this->assertPathEquals($expected, $result);
}
/**
* Test getting layout filenames
*

0 comments on commit 115a354

Please sign in to comment.
You can’t perform that action at this time.