Skip to content

Commit

Permalink
Preview tokens: Use homepage for site token
Browse files Browse the repository at this point in the history
Site does not have a preview by itself, so Kirby will render the homepage and check its token instead.
  • Loading branch information
lukasbestle committed Dec 2, 2024
1 parent e79cc36 commit 4acdd2b
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 7 deletions.
21 changes: 15 additions & 6 deletions src/Content/Version.php
Original file line number Diff line number Diff line change
Expand Up @@ -398,15 +398,24 @@ protected function prepareFieldsForContent(
*/
public function previewToken(): string
{
$id = match (true) {
$this->model instanceof Site => '',
$this->model instanceof Page => $this->model->id() . $this->model->template(),
default => throw new LogicException('Invalid model type')
};
if ($this->model instanceof Site) {
// the site itself does not render; its preview is the home page
$homePage = $this->model->homePage();

if ($homePage === null) {
throw new NotFoundException('The home page does not exist');
}

return $homePage->version($this->id)->previewToken();
}

if (($this->model instanceof Page) === false) {
throw new LogicException('Invalid model type');
}

return $this->model->kirby()->contentToken(
$this->model,
$id
$this->model->id() . $this->model->template()
);
}

Expand Down
8 changes: 8 additions & 0 deletions tests/Content/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,10 @@ public function setUpMultiLanguage(
parent::setUpMultiLanguage(
site: $site ?? [
'children' => [
[
'slug' => 'home',
'template' => 'default'
],
[
'slug' => 'a-page',
'template' => 'article',
Expand All @@ -109,6 +113,10 @@ public function setUpSingleLanguage(
parent::setUpSingleLanguage(
site: $site ?? [
'children' => [
[
'slug' => 'home',
'template' => 'default'
],
[
'slug' => 'a-page',
'template' => 'article',
Expand Down
25 changes: 24 additions & 1 deletion tests/Content/VersionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Kirby\Content;

use Kirby\Cms\App;
use Kirby\Cms\File;
use Kirby\Cms\Page;
use Kirby\Cms\Site;
Expand Down Expand Up @@ -785,7 +786,7 @@ public function testPreviewToken()
model: $this->app->site(),
id: VersionId::latest()
);
$this->assertSame(hash_hmac('sha1', '', static::TMP . '/content/'), $version->previewToken());
$this->assertSame(hash_hmac('sha1', 'home' . 'default', static::TMP . '/content/home'), $version->previewToken());

// page
$version = new Version(
Expand Down Expand Up @@ -813,6 +814,28 @@ public function testPreviewTokenInvalidModel()
$version->previewToken();
}

/**
* @covers ::previewToken
*/
public function testPreviewTokenMissingHomePage()
{
$this->expectException(NotFoundException::class);
$this->expectExceptionMessage('The home page does not exist');

$app = new App([
'roots' => [
'index' => static::TMP
]
]);

$version = new Version(
model: $app->site(),
id: VersionId::latest()
);

$version->previewToken();
}

/**
* @covers ::publish
*/
Expand Down

0 comments on commit 4acdd2b

Please sign in to comment.