Skip to content

Commit

Permalink
Fix duplicate subdirectories for file type views.
Browse files Browse the repository at this point in the history
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 115a354
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
10 changes: 7 additions & 3 deletions src/View/View.php
Expand Up @@ -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;
Expand Down
25 changes: 25 additions & 0 deletions tests/TestCase/View/ViewTest.php
Expand Up @@ -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
*
Expand Down

0 comments on commit 115a354

Please sign in to comment.