Skip to content

Commit

Permalink
Extend page render unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lukasbestle committed Nov 29, 2024
1 parent f4c2846 commit ce1cc6e
Show file tree
Hide file tree
Showing 13 changed files with 151 additions and 2 deletions.
135 changes: 133 additions & 2 deletions tests/Cms/Pages/PageRenderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Kirby\Cms;

use Kirby\Cache\Value;
use Kirby\Exception\NotFoundException;
use Kirby\Filesystem\Dir;
use Kirby\TestCase;

Expand All @@ -18,19 +19,28 @@ public function setUp(): void
{
$this->app = new App([
'roots' => [
'index' => static::TMP,
'templates' => static::FIXTURES
'index' => static::TMP,
'controllers' => static::FIXTURES . '/controllers',
'templates' => static::FIXTURES . '/templates'
],
'site' => [
'children' => [
[
'slug' => 'default',
'template' => 'cache-default'
],
[
'slug' => 'data',
'template' => 'cache-data'
],
[
'slug' => 'expiry',
'template' => 'cache-expiry'
],
[
'slug' => 'metadata',
'template' => 'cache-metadata',
],
[
'slug' => 'disabled',
'template' => 'cache-disabled'
Expand All @@ -51,6 +61,18 @@ public function setUp(): void
'slug' => 'dynamic-auth-session',
'template' => 'cache-dynamic'
],
[
'slug' => 'representation',
'template' => 'representation'
],
[
'slug' => 'invalid',
'template' => 'invalid',
],
[
'slug' => 'controller',
'template' => 'controller',
],
[
'slug' => 'bar',
'template' => 'hook-bar',
Expand Down Expand Up @@ -120,6 +142,33 @@ public function testCacheCustomExpiry()
$this->assertSame((int)$time, $value->expires());
}

public function testCacheMetadata()
{
$cache = $this->app->cache('pages');
$page = $this->app->page('metadata');

$this->assertNull($cache->retrieve('metadata.html'));

$html1 = $page->render();
$this->assertStringStartsWith('This is a test:', $html1);
$this->assertSame(202, $this->app->response()->code());
$this->assertSame(['Cache-Control' => 'private'], $this->app->response()->headers());
$this->assertSame('text/plain', $this->app->response()->type());

// reset the Kirby Responder object
$this->setUp();
$this->assertNull($this->app->response()->code());
$this->assertSame([], $this->app->response()->headers());
$this->assertNull($this->app->response()->type());

// ensure the Responder object is restored from cache
$html2 = $this->app->page('metadata')->render();
$this->assertSame($html1, $html2);
$this->assertSame(202, $this->app->response()->code());
$this->assertSame(['Cache-Control' => 'private'], $this->app->response()->headers());
$this->assertSame('text/plain', $this->app->response()->type());
}

public function testCacheDisabled()
{
$cache = $this->app->cache('pages');
Expand All @@ -133,6 +182,7 @@ public function testCacheDisabled()
$this->assertNull($cache->retrieve('disabled.html'));

$html2 = $page->render();
$this->assertStringStartsWith('This is a test:', $html2);
$this->assertNotSame($html1, $html2);
}

Expand Down Expand Up @@ -202,6 +252,7 @@ public function testCacheDynamicActiveOnFirstRender(string $slug, array $dynamic
// reset the Kirby Responder object
$this->setUp();
$html2 = $page->render();
$this->assertStringStartsWith('This is a test:', $html2);
$this->assertNotSame($html1, $html2);
}

Expand Down Expand Up @@ -235,9 +286,89 @@ public function testCacheDynamicActiveOnSecondRender(string $slug, array $dynami
// reset the Kirby Responder object
$this->setUp();
$html2 = $page->render();
$this->assertStringStartsWith('This is a test:', $html2);
$this->assertNotSame($html1, $html2);
}

public function testCacheDataInitial()
{
$cache = $this->app->cache('pages');
$page = $this->app->page('data');

$this->assertNull($cache->retrieve('data.html'));

$html = $page->render(['test' => 'custom test']);
$this->assertStringStartsWith('This is a custom test:', $html);

$this->assertNull($cache->retrieve('data.html'));
}

public function testCacheDataPreCached()
{
$cache = $this->app->cache('pages');
$page = $this->app->page('data');

$this->assertNull($cache->retrieve('data.html'));

$html1 = $page->render();
$this->assertStringStartsWith('This is a test:', $html1);

$value = $cache->retrieve('data.html');
$this->assertInstanceOf(Value::class, $value);
$this->assertSame($html1, $value->value()['html']);
$this->assertNull($value->expires());

$html2 = $page->render(['test' => 'custom test']);
$this->assertStringStartsWith('This is a custom test:', $html2);

// cache still stores the non-custom result
$value = $cache->retrieve('data.html');
$this->assertInstanceOf(Value::class, $value);
$this->assertSame($html1, $value->value()['html']);
$this->assertNull($value->expires());
}

public function testRepresentationDefault()
{
$page = $this->app->page('representation');

$this->assertSame('<html>Some HTML: representation</html>', $page->render());
}

public function testRepresentationOverride()
{
$page = $this->app->page('representation');

$this->assertSame('<html>Some HTML: representation</html>', $page->render(contentType: 'html'));
$this->assertSame('{"some json": "representation"}', $page->render(contentType: 'json'));
}

public function testRepresentationMissing()
{
$this->expectException(NotFoundException::class);
$this->expectExceptionMessage('The content representation cannot be found');

$page = $this->app->page('representation');
$page->render(contentType: 'txt');
}

public function testTemplateMissing()
{
$this->expectException(NotFoundException::class);
$this->expectExceptionMessage('The default template does not exist');

$page = $this->app->page('invalid');
$page->render();
}

public function testController()
{
$page = $this->app->page('controller');

$this->assertSame('Data says TEST: controller and default!', $page->render());
$this->assertSame('Data says TEST: controller and custom!', $page->render(['test' => 'override', 'test2' => 'custom']));
}

public function testHookBefore()
{
$app = $this->app->clone([
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

return function ($page) {
return [
'test' => 'TEST: ' . $page->title()
];
};
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This is a <?= $test ?? 'test' ?>: <?= uniqid() ?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

$kirby->response()->code(202);
$kirby->response()->header('Cache-Control', 'private');
$kirby->response()->type('text/plain');

echo 'This is a test: ' . uniqid();
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Data says <?= $test ?> and <?= $test2 ?? 'default' ?>!
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"some json": "<?= $page->title() ?>"}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<html>Some HTML: <?= $page->title() ?></html>

0 comments on commit ce1cc6e

Please sign in to comment.