From d8ced2686781dc0cecf3727685ee06f75a8d59f2 Mon Sep 17 00:00:00 2001 From: Will Rossiter Date: Wed, 26 Jun 2024 06:31:39 +1200 Subject: [PATCH] fix: resolve regression with customBasePath (#22) --- .github/workflows/ci.yml | 17 ++++++++++++++++ .gitignore | 4 ++++ composer.json | 9 +++++++- phpcs.xml.dist | 10 +++++++++ phpunit.xml.dist | 13 ++++++++++++ src/SVGTemplate.php | 43 +++++++++++++++++++++++++++------------ tests/SVGTemplateTest.php | 41 +++++++++++++++++++++++++++++++++++++ tests/logo.svg | 1 + 8 files changed, 124 insertions(+), 14 deletions(-) create mode 100644 .github/workflows/ci.yml create mode 100644 .gitignore create mode 100644 phpcs.xml.dist create mode 100644 phpunit.xml.dist create mode 100644 tests/SVGTemplateTest.php create mode 100644 tests/logo.svg diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..f998e4c --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,17 @@ +name: CI + +on: + push: + pull_request: + workflow_dispatch: + +jobs: + ci: + name: CI + uses: silverstripe/gha-ci/.github/workflows/ci.yml@v1 + with: + dynamic_matrix: false + extra_jobs: | + - php: '8.1' + db: mysql80 + phpunit: true diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2748123 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +/public +/vendor +composer.lock +.phpunit.result.cache diff --git a/composer.json b/composer.json index fe0aedf..a6ceb3e 100644 --- a/composer.json +++ b/composer.json @@ -13,9 +13,16 @@ "require": { "silverstripe/framework": "^4.0 || ^5.0" }, + "require-dev": { + "phpunit/phpunit": "^9" + }, + "scripts": { + "test": "phpunit" + }, "autoload": { "psr-4": { - "StevieMayhew\\SilverStripeSVG\\": "src/" + "StevieMayhew\\SilverStripeSVG\\": "src/", + "StevieMayhew\\SilverStripeSVG\\Tests\\": "tests/" } }, "config": { diff --git a/phpcs.xml.dist b/phpcs.xml.dist new file mode 100644 index 0000000..d834f9a --- /dev/null +++ b/phpcs.xml.dist @@ -0,0 +1,10 @@ + + + CodeSniffer ruleset for SilverStripe coding conventions. + + + + + + + diff --git a/phpunit.xml.dist b/phpunit.xml.dist new file mode 100644 index 0000000..ac723ba --- /dev/null +++ b/phpunit.xml.dist @@ -0,0 +1,13 @@ + + + tests + + + + src + + + diff --git a/src/SVGTemplate.php b/src/SVGTemplate.php index 2886e0c..15e33de 100755 --- a/src/SVGTemplate.php +++ b/src/SVGTemplate.php @@ -62,12 +62,12 @@ class SVGTemplate extends ViewableData /** * @var string */ - private $custom_base_path; + private $customBasePath; /** * @var array */ - private $extra_classes = []; + private $extraClasses = []; /** * @var array @@ -89,9 +89,9 @@ public function __construct($name, $id = '') { $this->name = $name; $this->id = $id; - $this->extra_classes = $this->config()->get('default_extra_classes') ?: []; - $this->extra_classes[] = 'svg-' . $this->name; - $this->subfolders = array(); + $this->extraClasses = $this->config()->get('default_extra_classes') ?: []; + $this->extraClasses[] = 'svg-' . $this->name; + $this->subfolders = []; $this->out = new DOMDocument(); $this->out->formatOutput = true; } @@ -154,7 +154,8 @@ public function size($width, $height) */ public function customBasePath($path) { - $this->custom_base_path = trim($path, DIRECTORY_SEPARATOR); + $this->customBasePath = trim($path, DIRECTORY_SEPARATOR); + return $this; } @@ -164,7 +165,8 @@ public function customBasePath($path) */ public function extraClass($class) { - $this->extra_classes[] = $class; + $this->extraClasses[] = $class; + return $this; } @@ -175,13 +177,18 @@ public function extraClass($class) public function addSubfolder($folder) { $this->subfolders[] = trim($folder, DIRECTORY_SEPARATOR); + return $this; } public function isRemoteSvg(): bool { - if (strpos($this->name, 'https://') === 0 || strpos($this->name, 'http://') === 0 || strpos($this->name, 'data:image') === 0) { + if ((strpos($this->name, '://') !== false || strpos($this->name, '//') === 0)) { + return true; + } + + if (strpos($this->name, 'data:image') === 0) { return true; } @@ -240,8 +247,8 @@ private function process($filePath) $root->setAttribute('height', $this->height); } - if ($this->extra_classes) { - $root->setAttribute('class', implode(' ', $this->extra_classes)); + if ($this->extraClasses) { + $root->setAttribute('class', implode(' ', $this->extraClasses)); } foreach ($out->getElementsByTagName('svg') as $element) { @@ -269,18 +276,28 @@ public function forTemplate() return DBField::create_field('HTMLText', $this->process($this->name)); } + $path = $this->fullSvgPathForTemplate(); + + return DBField::create_field('HTMLText', $this->process($path)); + } + + + public function fullSvgPathForTemplate() + { + $basePath = $this->customBasePath ?? $this->config()->get('base_path'); $parts = [ BASE_PATH, - $this->config()->get('base_path') + $basePath ]; foreach ($this->subfolders as $subfolder) { $parts[] = $subfolder; } - $parts[] = (strpos($this->name, ".") === false) ? $this->name . '.' . $this->config()->get('extension') : $this->name; + $extension = $this->config()->get('extension'); + $parts[] = (strpos($this->name, ".") === false) ? $this->name . '.' . $extension : $this->name; $path = Controller::join_links(array_filter($parts)); - return DBField::create_field('HTMLText', $this->process($path)); + return $path; } } diff --git a/tests/SVGTemplateTest.php b/tests/SVGTemplateTest.php new file mode 100644 index 0000000..22f0265 --- /dev/null +++ b/tests/SVGTemplateTest.php @@ -0,0 +1,41 @@ +set('base_path', 'tests'); + } + + + public function testFullSvgPathForTemplate() + { + $svgTemplate = new SVGTemplate('logo.svg'); + $this->assertStringEndsWith('logo.svg', $svgTemplate->fullSvgPathForTemplate()); + + $svgTemplate = $svgTemplate->customBasePath('custom'); + $this->assertStringEndsWith('custom/logo.svg', $svgTemplate->fullSvgPathForTemplate()); + } + + + public function testIsRemoteSvg() + { + $svgTemplate = new SVGTemplate('logo.svg'); + $this->assertFalse($svgTemplate->isRemoteSvg()); + + $svgTemplate = new SVGTemplate('https://example.com/logo.svg'); + $this->assertTrue($svgTemplate->isRemoteSvg()); + + + $svgTemplate = new SVGTemplate('//example.com/logo.svg'); + $this->assertTrue($svgTemplate->isRemoteSvg()); + } +} diff --git a/tests/logo.svg b/tests/logo.svg new file mode 100644 index 0000000..447386f --- /dev/null +++ b/tests/logo.svg @@ -0,0 +1 @@ +RGB - Silverstripe logo - white \ No newline at end of file