From ff1f86d08469a2a4270892a8546ff1a2a4a2802e Mon Sep 17 00:00:00 2001 From: Guy Sartorelli Date: Tue, 13 Aug 2024 16:30:35 +1200 Subject: [PATCH] FIX Correct trailing slash for subsite in another domain --- src/Model/SubsiteDomain.php | 4 +++- tests/php/SubsiteTest.php | 18 ++++++++++++++++-- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/src/Model/SubsiteDomain.php b/src/Model/SubsiteDomain.php index 8545f546..d4e87cd4 100644 --- a/src/Model/SubsiteDomain.php +++ b/src/Model/SubsiteDomain.php @@ -217,9 +217,11 @@ public function getAbsoluteLink() */ public function absoluteBaseURL() { - return Controller::join_links( + $slash = Controller::config()->get('add_trailing_slash') ? '/' : ''; + $baseURL = Controller::join_links( $this->getAbsoluteLink(), Director::baseURL() ); + return (rtrim($baseURL, '/')) . $slash; } } diff --git a/tests/php/SubsiteTest.php b/tests/php/SubsiteTest.php index d0e4dd56..74ec6cbe 100644 --- a/tests/php/SubsiteTest.php +++ b/tests/php/SubsiteTest.php @@ -4,6 +4,7 @@ use Page; use SilverStripe\CMS\Model\SiteTree; +use SilverStripe\Control\Controller; use SilverStripe\Control\Director; use SilverStripe\Core\Config\Config; use SilverStripe\ORM\DataObject; @@ -304,7 +305,7 @@ public function testDefaultDomain() // If there is a Director::baseURL() value this will also be included $this->assertEquals( - 'http://' . $_SERVER['HTTP_HOST'], + $this->normaliseTrailingSlash('http://' . $_SERVER['HTTP_HOST']), singleton(Subsite::class)->absoluteBaseURL() ); } @@ -324,7 +325,7 @@ public function testDomainProtocol($class, $identifier, $currentIsSecure, $expec $model = $this->objFromFixture($class, $identifier); $protocol = $currentIsSecure ? 'https' : 'http'; Config::modify()->set(Director::class, 'alternate_base_url', $protocol . '://www.mysite.com'); - $this->assertSame($expected, $model->absoluteBaseURL()); + $this->assertSame($this->normaliseTrailingSlash($expected), $model->absoluteBaseURL()); } public function domainProtocolProvider() @@ -502,4 +503,17 @@ public function testDefaultPageCreatedWhenCreatingSubsite() $pages = SiteTree::get(); $this->assertGreaterThanOrEqual(1, $pages->count()); } + + /** + * 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; + } }