Skip to content

Commit

Permalink
FIX Correct trailing slash for subsite in another domain (#590)
Browse files Browse the repository at this point in the history
  • Loading branch information
GuySartorelli authored Aug 13, 2024
1 parent f78ad58 commit 6536e8f
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
4 changes: 3 additions & 1 deletion src/Model/SubsiteDomain.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
18 changes: 16 additions & 2 deletions tests/php/SubsiteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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()
);
}
Expand All @@ -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()
Expand Down Expand Up @@ -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;
}
}

0 comments on commit 6536e8f

Please sign in to comment.