Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FIX Correct trailing slash for subsite in another domain #590

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
}
Loading