Skip to content

Commit

Permalink
FIX Correct trailing slash for locale in another domain
Browse files Browse the repository at this point in the history
  • Loading branch information
GuySartorelli committed Aug 13, 2024
1 parent 41015aa commit 7234e98
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 5 deletions.
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;
}
}

0 comments on commit 7234e98

Please sign in to comment.