From a4f069b0aadd84c44f2c5fedecb7801a6892c172 Mon Sep 17 00:00:00 2001 From: Ben Thomson Date: Fri, 24 Jun 2022 14:15:35 +0800 Subject: [PATCH 1/4] Handle local reference partial paths early in ViewMaker Matches the behavior of the ViewMaker in the 1.1 branch more closely by checking if the partial path is a local reference first, returning that path if it is. Only if this check fails will it then run through the view paths. Fixes https://github.com/wintercms/winter/issues/571 --- modules/system/traits/ViewMaker.php | 42 ++++++++++++++--------------- 1 file changed, 20 insertions(+), 22 deletions(-) diff --git a/modules/system/traits/ViewMaker.php b/modules/system/traits/ViewMaker.php index a9cc1f2e1f..dab434b063 100644 --- a/modules/system/traits/ViewMaker.php +++ b/modules/system/traits/ViewMaker.php @@ -186,33 +186,31 @@ public function getViewPath(string $fileName, string|array $viewPaths = null): s $fileName = substr($fileName, 0, strrpos($fileName, '.')); } + // Check if this is a local path reference first + $absolutePath = File::symbolizePath($fileName); + foreach ($allowedExtensions as $ext) { + $viewPath = $absolutePath . ".$ext"; + + if ( + File::isLocalPath($viewPath) + || ( + !Config::get('cms.restrictBaseDir', true) + && realpath($viewPath) !== false + ) + ) { + return $viewPath; + } + } - if (File::isPathSymbol($fileName)) { - // Handle path symbols - $absolutePath = File::symbolizePath($fileName); + // Next, check if this a path relative to the view paths + foreach ($viewPaths as $path) { + $absolutePath = File::symbolizePath($path); foreach ($allowedExtensions as $ext) { - $viewPath = $absolutePath . ".$ext"; - - if ( - File::isLocalPath($viewPath) - || ( - !Config::get('cms.restrictBaseDir', true) - && realpath($viewPath) !== false - ) - ) { + $viewPath = $absolutePath . DIRECTORY_SEPARATOR . $fileName . ".$ext"; + if (File::isFile($viewPath)) { return $viewPath; } } - } else { - foreach ($viewPaths as $path) { - $absolutePath = File::symbolizePath($path); - foreach ($allowedExtensions as $ext) { - $viewPath = $absolutePath . DIRECTORY_SEPARATOR . $fileName . ".$ext"; - if (File::isFile($viewPath)) { - return $viewPath; - } - } - } } return $input; From 94f364e9a83a054605dea5299a49a6ec6e2d2c4d Mon Sep 17 00:00:00 2001 From: Ben Thomson Date: Fri, 24 Jun 2022 14:16:47 +0800 Subject: [PATCH 2/4] Run tests with the base path as the current directory Fixes the ViewMaker tests by emulating the same behaviour as a web request and having the current directory be the base path of the site, not the directory of the current module or plugin being tested. --- modules/system/console/WinterTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/system/console/WinterTest.php b/modules/system/console/WinterTest.php index f66b15b21f..db8e108d7e 100644 --- a/modules/system/console/WinterTest.php +++ b/modules/system/console/WinterTest.php @@ -143,7 +143,7 @@ protected function execPhpUnit(string $config, array $args): int $process = new Process( array_merge([$this->phpUnitExec, '--configuration=' . $config], $args), - dirname($config), + base_path(), null, null ); From 7d7cf09ac106508d92b664a9c6614b3e5a4e3b03 Mon Sep 17 00:00:00 2001 From: Ben Thomson Date: Fri, 24 Jun 2022 21:35:34 +0800 Subject: [PATCH 3/4] Swap order of pathing - check view paths first, then relative --- modules/system/traits/ViewMaker.php | 24 ++++++++++++------------ plugins/winter/test | 1 + 2 files changed, 13 insertions(+), 12 deletions(-) create mode 160000 plugins/winter/test diff --git a/modules/system/traits/ViewMaker.php b/modules/system/traits/ViewMaker.php index dab434b063..8407e4e033 100644 --- a/modules/system/traits/ViewMaker.php +++ b/modules/system/traits/ViewMaker.php @@ -186,7 +186,18 @@ public function getViewPath(string $fileName, string|array $viewPaths = null): s $fileName = substr($fileName, 0, strrpos($fileName, '.')); } - // Check if this is a local path reference first + // Check if this a path relative to the view paths + foreach ($viewPaths as $path) { + $absolutePath = File::symbolizePath($path); + foreach ($allowedExtensions as $ext) { + $viewPath = $absolutePath . DIRECTORY_SEPARATOR . $fileName . ".$ext"; + if (File::isFile($viewPath)) { + return $viewPath; + } + } + } + + // Next, check if this is a local path reference $absolutePath = File::symbolizePath($fileName); foreach ($allowedExtensions as $ext) { $viewPath = $absolutePath . ".$ext"; @@ -202,17 +213,6 @@ public function getViewPath(string $fileName, string|array $viewPaths = null): s } } - // Next, check if this a path relative to the view paths - foreach ($viewPaths as $path) { - $absolutePath = File::symbolizePath($path); - foreach ($allowedExtensions as $ext) { - $viewPath = $absolutePath . DIRECTORY_SEPARATOR . $fileName . ".$ext"; - if (File::isFile($viewPath)) { - return $viewPath; - } - } - } - return $input; } diff --git a/plugins/winter/test b/plugins/winter/test new file mode 160000 index 0000000000..ea97d5c33a --- /dev/null +++ b/plugins/winter/test @@ -0,0 +1 @@ +Subproject commit ea97d5c33a15303cf797ec60ddfe0c192d0f9e4f From fbe28660274104af715a6dfdf6f742dd44731eb6 Mon Sep 17 00:00:00 2001 From: Ben Thomson Date: Sat, 25 Jun 2022 11:47:45 +0800 Subject: [PATCH 4/4] Remove submodule --- plugins/winter/test | 1 - 1 file changed, 1 deletion(-) delete mode 160000 plugins/winter/test diff --git a/plugins/winter/test b/plugins/winter/test deleted file mode 160000 index ea97d5c33a..0000000000 --- a/plugins/winter/test +++ /dev/null @@ -1 +0,0 @@ -Subproject commit ea97d5c33a15303cf797ec60ddfe0c192d0f9e4f