From 8fa0c95f5ab0d95b74d64d315c0488124e9cad63 Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Mon, 1 Feb 2021 23:18:19 +0100 Subject: [PATCH] Ensured base path is honored when stringifying short URLs with a custom domain --- module/Core/config/dependencies.config.php | 2 +- .../ShortUrl/Helper/ShortUrlStringifier.php | 8 ++++++-- .../Helper/ShortUrlStringifierTest.php | 20 +++++++++++++++++-- 3 files changed, 25 insertions(+), 5 deletions(-) diff --git a/module/Core/config/dependencies.config.php b/module/Core/config/dependencies.config.php index 7a5950bc..48166aeb 100644 --- a/module/Core/config/dependencies.config.php +++ b/module/Core/config/dependencies.config.php @@ -121,7 +121,7 @@ return [ ], ShortUrl\Resolver\PersistenceShortUrlRelationResolver::class => ['em'], - ShortUrl\Helper\ShortUrlStringifier::class => ['config.url_shortener.domain'], + ShortUrl\Helper\ShortUrlStringifier::class => ['config.url_shortener.domain', 'config.router.base_path'], ShortUrl\Transformer\ShortUrlDataTransformer::class => [ShortUrl\Helper\ShortUrlStringifier::class], Mercure\MercureUpdatesGenerator::class => [ShortUrl\Transformer\ShortUrlDataTransformer::class], diff --git a/module/Core/src/ShortUrl/Helper/ShortUrlStringifier.php b/module/Core/src/ShortUrl/Helper/ShortUrlStringifier.php index d5edba52..4d34e26b 100644 --- a/module/Core/src/ShortUrl/Helper/ShortUrlStringifier.php +++ b/module/Core/src/ShortUrl/Helper/ShortUrlStringifier.php @@ -7,13 +7,17 @@ namespace Shlinkio\Shlink\Core\ShortUrl\Helper; use Laminas\Diactoros\Uri; use Shlinkio\Shlink\Core\Entity\ShortUrl; +use function sprintf; + class ShortUrlStringifier implements ShortUrlStringifierInterface { private array $domainConfig; + private string $basePath; - public function __construct(array $domainConfig) + public function __construct(array $domainConfig, string $basePath = '') { $this->domainConfig = $domainConfig; + $this->basePath = $basePath; } public function stringify(ShortUrl $shortUrl): string @@ -31,6 +35,6 @@ class ShortUrlStringifier implements ShortUrlStringifierInterface return $this->domainConfig['hostname'] ?? ''; } - return $domain->getAuthority(); + return sprintf('%s%s', $domain->getAuthority(), $this->basePath); } } diff --git a/module/Core/test/ShortUrl/Helper/ShortUrlStringifierTest.php b/module/Core/test/ShortUrl/Helper/ShortUrlStringifierTest.php index 80cff5ed..483fd57d 100644 --- a/module/Core/test/ShortUrl/Helper/ShortUrlStringifierTest.php +++ b/module/Core/test/ShortUrl/Helper/ShortUrlStringifierTest.php @@ -17,10 +17,11 @@ class ShortUrlStringifierTest extends TestCase */ public function generatesExpectedOutputBasedOnConfigAndShortUrl( array $config, + string $basePath, ShortUrl $shortUrl, string $expected ): void { - $stringifier = new ShortUrlStringifier($config); + $stringifier = new ShortUrlStringifier($config, $basePath); self::assertEquals($expected, $stringifier->stringify($shortUrl)); } @@ -35,21 +36,36 @@ class ShortUrlStringifierTest extends TestCase ]), ); - yield 'no config' => [[], $shortUrlWithShortCode('foo'), 'http:/foo']; + yield 'no config' => [[], '', $shortUrlWithShortCode('foo'), 'http:/foo']; yield 'hostname in config' => [ ['hostname' => 'example.com'], + '', $shortUrlWithShortCode('bar'), 'http://example.com/bar', ]; + yield 'hostname with base path in config' => [ + ['hostname' => 'example.com/foo/bar'], + '', + $shortUrlWithShortCode('abc'), + 'http://example.com/foo/bar/abc', + ]; yield 'full config' => [ ['schema' => 'https', 'hostname' => 'foo.com'], + '', $shortUrlWithShortCode('baz'), 'https://foo.com/baz', ]; yield 'custom domain' => [ ['schema' => 'https', 'hostname' => 'foo.com'], + '', $shortUrlWithShortCode('baz', 'mydom.es'), 'https://mydom.es/baz', ]; + yield 'custom domain with base path' => [ + ['schema' => 'https', 'hostname' => 'foo.com'], + '/foo/bar', + $shortUrlWithShortCode('baz', 'mydom.es'), + 'https://mydom.es/foo/bar/baz', + ]; } }