From 6536e8f524771190c0170b583bacd1602b5b3191 Mon Sep 17 00:00:00 2001 From: Guy Sartorelli <36352093+GuySartorelli@users.noreply.github.com> Date: Tue, 13 Aug 2024 17:03:52 +1200 Subject: [PATCH] FIX Correct trailing slash for subsite in another domain (#590) --- 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; + } }