From 762385ded80d8c59a62ce96b4348762baea0ab4c Mon Sep 17 00:00:00 2001 From: Guy Sartorelli Date: Tue, 13 Aug 2024 15:50:52 +1200 Subject: [PATCH] FIX Correct trailing slash for locale in another domain --- src/Extension/FluentSiteTreeExtension.php | 5 ++++- .../Extension/FluentSiteTreeExtensionTest.php | 19 ++++++++++++++++--- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/src/Extension/FluentSiteTreeExtension.php b/src/Extension/FluentSiteTreeExtension.php index 0eedf5dc..3f09f2f0 100644 --- a/src/Extension/FluentSiteTreeExtension.php +++ b/src/Extension/FluentSiteTreeExtension.php @@ -153,7 +153,10 @@ public function updateLink(&$link, &$action, &$relativeLink) return; } - // Prefix with domain + // Prefix with domain, making sure trailing slash is normalised correctly. + if ($link === '/' || $link === '') { + $link = Controller::config()->get('add_trailing_slash') ? '/' : ''; + } $link = Controller::join_links($domain->Link(), $link); } diff --git a/tests/php/Extension/FluentSiteTreeExtensionTest.php b/tests/php/Extension/FluentSiteTreeExtensionTest.php index aa0e1d97..b166f31f 100644 --- a/tests/php/Extension/FluentSiteTreeExtensionTest.php +++ b/tests/php/Extension/FluentSiteTreeExtensionTest.php @@ -75,8 +75,8 @@ public function testGetLocaleInformation() $this->assertEquals('English (New Zealand)', $result->getTitle()); $this->assertEquals('English', $result->getLanguageNative()); $this->assertEquals('en', $result->getLanguage()); - $this->assertEquals(Controller::normaliseTrailingSlash('/newzealand/a-page/'), $result->getLink()); - $this->assertEquals(Controller::normaliseTrailingSlash('http://mocked/newzealand/a-page/'), $result->getAbsoluteLink()); + $this->assertEquals($this->normaliseTrailingSlash('/newzealand/a-page/'), $result->getLink()); + $this->assertEquals($this->normaliseTrailingSlash('http://mocked/newzealand/a-page/'), $result->getAbsoluteLink()); $this->assertEquals('link', $result->getLinkingMode()); $this->assertEquals('newzealand', $result->getURLSegment()); }); @@ -180,7 +180,7 @@ function (FluentState $newState) use ($domain, $locale, $prefixDisabled, $pageNa /** @var Page|FluentSiteTreeExtension $page */ $page = $this->objFromFixture(Page::class, $pageName); - $this->assertEquals(Controller::normaliseTrailingSlash($url), $page->Link()); + $this->assertEquals($this->normaliseTrailingSlash($url), $page->Link()); } ); } @@ -562,4 +562,17 @@ public function localeFallbackProvider(): array ], ]; } + + /** + * Normalises a test URL's trailing slash, but ignores complexities + * such as whether the domain host in the UR matches Director::host() + */ + private function normaliseTrailingSlash(string $testURL): string + { + if ($testURL === '/' || $testURL === '') { + return '/'; + } + $slash = Controller::config()->get('add_trailing_slash') ? '/' : ''; + return (rtrim($testURL, '/')) . $slash; + } }