diff --git a/src/Service/UrlBundleService.php b/src/Service/UrlBundleService.php index f7a3977f..5da9f70a 100644 --- a/src/Service/UrlBundleService.php +++ b/src/Service/UrlBundleService.php @@ -2,6 +2,7 @@ namespace SilverStripe\StaticPublishQueue\Service; +use SilverStripe\Core\Config\Configurable; use SilverStripe\Core\Extensible; use SilverStripe\Core\Injector\Injectable; use SilverStripe\Core\Injector\Injector; @@ -18,15 +19,16 @@ class UrlBundleService implements UrlBundleInterface { use Extensible; use Injectable; + use Configurable; + + private static bool $strip_stage_param = true; protected array $urls = []; public function addUrls(array $urls): void { foreach ($urls as $url) { - $safeUrl = $this->stripStageParam($url); - - $this->urls[$safeUrl] = $safeUrl; + $this->urls[] = $url; } } @@ -103,6 +105,10 @@ protected function stripStageParam(string $url): string */ protected function formatUrl(string $url): ?string { + if ($this->config()->get('strip_stage_param')) { + $url = $this->stripStageParam($url); + } + // Use this extension point to reformat URLs, for example encode special characters $this->extend('updateFormatUrl', $url); diff --git a/tests/php/Service/UrlBundleServiceTest.php b/tests/php/Service/UrlBundleServiceTest.php index b1937ca7..7a0b981f 100644 --- a/tests/php/Service/UrlBundleServiceTest.php +++ b/tests/php/Service/UrlBundleServiceTest.php @@ -34,8 +34,8 @@ public function testJobsFromDataDefault(string $jobClass): void $job = array_shift($jobs); $this->assertEquals([ - 'http://some-locale/some-page/' => 0, - 'http://some-locale/some-other-page/' => 1, + 'http://some-locale/some-page/', + 'http://some-locale/some-other-page/', ], $job->URLsToProcess); @@ -144,6 +144,40 @@ public function chunkCases(): array ]; } + public function testGetUrls(): void + { + $urls = [ + 'http://www.test.com?stage=Stage', + 'https://www.test.com?test1=1&stage=Live&test2=2' + ]; + $expectedUrls = [ + 'http://www.test.com', + 'https://www.test.com?test1=1&test2=2', + ]; + + $urlService = UrlBundleService::create(); + $urlService->addUrls($urls); + $resultUrls = $urlService->getUrls(); + + $this->assertEqualsCanonicalizing($expectedUrls, $resultUrls); + } + + public function testGetUrlsDontStripStage(): void + { + UrlBundleService::config()->set('strip_stage_param', false); + + $urls = [ + 'http://www.test.com?stage=Stage', + 'https://www.test.com?test1=1&stage=Live&test2=2' + ]; + + $urlService = UrlBundleService::create(); + $urlService->addUrls($urls); + $resultUrls = $urlService->getUrls(); + + $this->assertEqualsCanonicalizing($urls, $resultUrls); + } + /** * @dataProvider provideStripStageParamUrls */