Skip to content

Commit

Permalink
Merge pull request #6856 from getkirby/v5/fix/preview-tokens-home
Browse files Browse the repository at this point in the history
Fix for homepage preview URLs on proper domains
  • Loading branch information
bastianallgeier authored Dec 12, 2024
2 parents ea9ecfa + f1d8072 commit e352c68
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 5 deletions.
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;
}

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

0 comments on commit e352c68

Please sign in to comment.