From 7234e984a957a59d40b430afe9ece2409e54a8b2 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 ++++- src/Model/Locale.php | 7 ++++++- .../Extension/FluentSiteTreeExtensionTest.php | 19 ++++++++++++++++--- 3 files changed, 26 insertions(+), 5 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/src/Model/Locale.php b/src/Model/Locale.php index b7b5f1ef..d555f974 100644 --- a/src/Model/Locale.php +++ b/src/Model/Locale.php @@ -580,9 +580,14 @@ public function getBaseURL() if ($append) { // Append locale url segment - $base = Controller::join_links($base, $this->getURLSegment(), '/'); + $base = Controller::join_links($base, $this->getURLSegment()); } + // Normalise trailing slash + // Can't use Controller::normaliseTrailingSlash() because that doesn't + // take locale domains into account + $base = rtrim($base, '/') . (Controller::config()->get('add_trailing_slash') ? '/' : ''); + return $base; } 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; + } }