cms/tests/Unit/PageRendererTest.php

95 lines
2.9 KiB
PHP
Raw Normal View History

<?php
namespace Tests\Unit;
use Tests\TestCase;
use App\Support\PageRenderer;
use Illuminate\Support\Facades\View;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\File;
use Illuminate\Foundation\Testing\RefreshDatabase;
class PageRendererTest extends TestCase
{
use RefreshDatabase;
public function test_it_renders_basic_blocks(): void
{
$renderer = new PageRenderer();
$content = [
[
'type' => 'heading',
'data' => ['text' => 'Hello World', 'level' => 3]
],
[
'type' => 'paragraph',
'data' => ['text' => '<p>This is a paragraph.</p>']
]
];
$html = $renderer->render($content);
$this->assertStringContainsString('<h3>Hello World</h3>', $html);
$this->assertStringContainsString('<p>This is a paragraph.</p>', $html);
}
public function test_it_supports_theme_overrides(): void
{
// Setup theme blocks directory
$themesPath = base_path('themes');
if (! File::exists($themesPath)) {
File::makeDirectory($themesPath);
}
$themeBlocksPath = "{$themesPath}/test-theme/blocks";
if (! File::exists($themeBlocksPath)) {
File::makeDirectory($themeBlocksPath, 0755, true);
}
File::put("{$themeBlocksPath}/heading.blade.php", "<h1 class='theme-title'>{{ \$text }}</h1>");
// Add theme to view paths
View::addNamespace('themes.test-theme', $themesPath . '/test-theme');
\App\Models\Setting::set('active_theme', 'test-theme', 'cms');
$renderer = new PageRenderer();
$content = [
['type' => 'heading', 'data' => ['text' => 'Theme Heading']]
];
$html = $renderer->render($content);
$this->assertStringContainsString("<h1 class='theme-title'>Theme Heading</h1>", $html);
// Cleanup
File::deleteDirectory($themesPath . '/test-theme');
}
public function test_it_handles_missing_blocks_gracefully(): void
{
$renderer = new PageRenderer();
$content = [
['type' => 'unknown', 'data' => []]
];
$html = $renderer->render($content);
$this->assertStringContainsString('<!-- Block type [unknown] not found (resolved as [unknown]) -->', $html);
}
public function test_it_maps_aliases(): void
{
$renderer = new PageRenderer();
$content = [
['type' => 'text', 'data' => ['text' => 'Paragraph text']],
['type' => 'header', 'data' => ['text' => 'Header text']]
];
$html = $renderer->render($content);
// Alias 'text' maps to 'paragraph' which might use Icehouse template or fallback
$this->assertStringContainsString('Paragraph text', $html);
// Alias 'header' maps to 'heading' which should render core <h2>
$this->assertStringContainsString('<h2>Header text</h2>', $html);
}
}