diff --git a/modules/system/traits/AssetMaker.php b/modules/system/traits/AssetMaker.php index dc5d9aa26..423bf6e76 100644 --- a/modules/system/traits/AssetMaker.php +++ b/modules/system/traits/AssetMaker.php @@ -2,6 +2,7 @@ use Url; use Html; +use File; use System\Models\Parameter; use System\Models\PluginVersion; use System\Classes\CombineAssets; @@ -112,7 +113,7 @@ trait AssetMaker public function addJs($name, $attributes = []) { if (is_array($name)) { - $name = $this->combineAssets($name); + $name = $this->combineAssets($name, $this->getLocalPath($this->assetPath)); } $jsPath = $this->getAssetPath($name); @@ -142,7 +143,7 @@ trait AssetMaker public function addCss($name, $attributes = []) { if (is_array($name)) { - $name = $this->combineAssets($name); + $name = $this->combineAssets($name, $this->getLocalPath($this->assetPath)); } $cssPath = $this->getAssetPath($name); @@ -324,4 +325,13 @@ trait AssetMaker } } + + protected function getLocalPath(string $relativePath) + { + $relativePath = File::symbolizePath($relativePath); + if (!starts_with($relativePath, [base_path()])) { + $relativePath = base_path($relativePath); + } + return $relativePath; + } } diff --git a/tests/unit/system/traits/AssetMakerTest.php b/tests/unit/system/traits/AssetMakerTest.php new file mode 100644 index 000000000..2d247dcf3 --- /dev/null +++ b/tests/unit/system/traits/AssetMakerTest.php @@ -0,0 +1,59 @@ +createApplication(); + $this->stub = new AssetMakerStub(); + } + + // + // Helpers + // + + protected static function callProtectedMethod($object, $name, $params = []) + { + $className = get_class($object); + $class = new ReflectionClass($className); + $method = $class->getMethod($name); + $method->setAccessible(true); + return $method->invokeArgs($object, $params); + } + + // + // Tests + // + + public function testGetLocalPath() + { + $basePath = base_path(); + + // Default assetPath + $assetPath = $this->stub->guessViewPath('/assets', true); + $resolvedPath = $this->callProtectedMethod($this->stub, 'getLocalPath', [$assetPath]); + $this->assertEquals(realpath($basePath.$assetPath), realpath($resolvedPath)); + + // Paths with symbols + $resolvedPath = $this->callProtectedMethod($this->stub, 'getLocalPath', ['~/themes/demo/']); + $this->assertEquals(realpath($basePath.'/themes/demo/'), realpath($resolvedPath)); + + $resolvedPath = $this->callProtectedMethod($this->stub, 'getLocalPath', ['~/plugins/demo/']); + $this->assertEquals(realpath($basePath.'/plugins/demo/'), realpath($resolvedPath)); + + $resolvedPath = $this->callProtectedMethod($this->stub, 'getLocalPath', ['$/demo/']); + $this->assertEquals(realpath($basePath.'/plugins/demo/'), realpath($resolvedPath)); + + // Absolute Path + $resolvedPath = $this->callProtectedMethod($this->stub, 'getLocalPath', [$basePath.'/some/wild/absolute/path/']); + $this->assertEquals(realpath($basePath.'/some/wild/absolute/path/'), realpath($resolvedPath)); + } +}