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 for homepage preview URLs on proper domains #6856

Merged
merged 1 commit into from
Dec 12, 2024
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
10 changes: 7 additions & 3 deletions src/Content/Version.php
Original file line number Diff line number Diff line change
Expand Up @@ -427,9 +427,13 @@ protected function previewTokenFromUrl(string $url): string|null
{
$localPrefix = $this->model->kirby()->url('base') . '/';

// Todo: this is only a quick fix to get home page previews working again,
// we need to double-check if this is still correct
if (Str::startsWith($url, $localPrefix) === false && $url . '/' !== $localPrefix) {
// normalize homepage URLs to have a trailing slash
// to make the following logic work with those as well
if ($url . '/' === $localPrefix) {
$url .= '/';
}

if (Str::startsWith($url, $localPrefix) === false) {
return null;
}
afbora marked this conversation as resolved.
Show resolved Hide resolved

Expand Down
33 changes: 31 additions & 2 deletions tests/Content/VersionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -774,13 +774,34 @@ public function testMoveToVersion(): void
$this->assertSame($content, Data::read($fileENLatest));
}

public static function previewTokenIndexUrlProvider()
{
return [
['/'],
['/subfolder'],
['/subfolder/'],
['https://example.com'],
['https://example.com/'],
['https://example.com/subfolder'],
['https://example.com/subfolder/'],
];
}

/**
* @covers ::previewToken
* @covers ::previewTokenFromUrl
* @dataProvider previewTokenIndexUrlProvider
*/
public function testPreviewToken()
public function testPreviewToken(string $indexUrl)
{
$this->setUpSingleLanguage();

$this->app = $this->app->clone([
'urls' => [
'index' => $indexUrl
]
]);

// site
$version = new Version(
model: $this->app->site(),
Expand All @@ -789,7 +810,15 @@ public function testPreviewToken()
$expected = substr(hash_hmac('sha1', '{"uri":"","versionId":"latest"}', static::TMP . '/content'), 0, 10);
$this->assertSame($expected, $version->previewToken());

// page
// homepage
$version = new Version(
model: $this->app->site()->page('home'),
id: VersionId::latest()
);
$expected = substr(hash_hmac('sha1', '{"uri":"","versionId":"latest"}', static::TMP . '/content'), 0, 10);
$this->assertSame($expected, $version->previewToken());

// another page
$version = new Version(
model: $this->model,
id: VersionId::latest()
Expand Down