Skip to content

Commit

Permalink
Add test cov for new UrlBundleService config
Browse files Browse the repository at this point in the history
  • Loading branch information
chrispenny committed Apr 4, 2023
1 parent 76f9e1c commit e0a589f
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 5 deletions.
12 changes: 9 additions & 3 deletions src/Service/UrlBundleService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}
}

Expand Down Expand Up @@ -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);

Expand Down
38 changes: 36 additions & 2 deletions tests/php/Service/UrlBundleServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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
*/
Expand Down

0 comments on commit e0a589f

Please sign in to comment.