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 locale in another domain #872

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
5 changes: 4 additions & 1 deletion src/Extension/FluentSiteTreeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
7 changes: 6 additions & 1 deletion src/Model/Locale.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
19 changes: 16 additions & 3 deletions tests/php/Extension/FluentSiteTreeExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
});
Expand Down Expand Up @@ -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());
}
);
}
Expand Down Expand Up @@ -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;
}
}
Loading