Files
laraundfelix/tests/Feature/CmsProseFormattingTest.php
T
LarsandClaude Sonnet 5.5 7f9534d289
CI Feature / test (push) Successful in 7m47s
Deploy Test / test (push) Successful in 8m9s
Deploy Test / deploy (push) Successful in 44s
Erkenne Text mit H1, H2 oder div am Anfang als HTML statt als Klartext
Ein Fließtext, dessen HTML mit <h2> begann, wurde als Klartext des Altbestands gelesen
und mit sichtbaren Tags ausgegeben (Website und Vorschau). Die Bereinigung bildet H1/H2
auf H3 ab, kam aber nie zum Zug. isHtml kennt nun h1, h2 und div am Textanfang.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-12-01 10:43:04 +01:00

184 lines
9.4 KiB
PHP

<?php
namespace Tests\Feature;
use App\Domain\Catalog\FormattedText;
use App\Domain\Cms\Page;
use App\Domain\Cms\PageService;
use App\Domain\Cms\Widgets\Types\ProseWidget;
use App\Domain\Cms\Widgets\WidgetRegistry;
use App\Filament\Support\RichField;
use Filament\Forms\Components\RichEditor;
use Filament\Forms\Components\RichEditor\StateCasts\RichEditorStateCast;
use Filament\Schemas\Concerns\InteractsWithSchemas;
use Filament\Schemas\Contracts\HasSchemas;
use Filament\Schemas\Schema;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Livewire\Component;
use Tests\TestCase;
/**
* Fließtext der Website: Schriftgröße, Schriftfarbe und Ausrichtung aus festen Werten. Gespeichert wird nur das Webprofil
* von FormattedText; Verwaltung und PDF kennen diese Auszeichnungen nicht.
*/
class CmsProseFormattingTest extends TestCase
{
use RefreshDatabase;
/** So schreibt der Editor (Filament/TipTap) die Auszeichnungen. */
private const EDITOR = '<p style="text-align: center">Zentriert mit <span class="color" data-color="gruen" style="--color: #3b7b09; --dark-color: #3b7b09">grün</span> und <span data-size="large" style="font-size: 1.25em">groß</span>.</p><h3 style="text-align: end">Rechts</h3><p>Normal</p>';
public function test_the_website_profile_keeps_alignment_size_and_color_in_the_form_the_editor_reads_again(): void
{
$html = FormattedText::canonical(self::EDITOR, true);
$this->assertSame('<p style="text-align: center">Zentriert mit <span class="color" data-color="gruen">grün</span> und <span data-size="large">groß</span>.</p><h3 style="text-align: end">Rechts</h3><p>Normal</p>', $html);
$this->assertSame($html, FormattedText::canonical($html, true), 'Die kanonische Form ist stabil, ein erneutes Öffnen erzeugt keine Änderung.');
}
public function test_the_catalog_profile_drops_all_three(): void
{
foreach ([FormattedText::canonical(self::EDITOR), FormattedText::canonical(FormattedText::canonical(self::EDITOR, true))] as $html) {
$this->assertStringNotContainsString('style', $html);
$this->assertStringNotContainsString('<span', $html);
$this->assertStringNotContainsString('data-', $html);
$this->assertStringContainsString('<h3>Rechts</h3>', $html);
}
$this->assertStringNotContainsString('text-align', json_encode(FormattedText::blocks(FormattedText::canonical(self::EDITOR, true))));
}
public function test_the_summary_in_the_editor_keeps_the_text_of_colored_and_sized_words(): void
{
$summary = app(WidgetRegistry::class)->get('prose')->summary(['data' => ['body' => self::EDITOR]]);
$this->assertSame('Zentriert mit grün und groß. Rechts Normal', $summary);
}
public function test_unknown_values_and_arbitrary_styles_are_removed(): void
{
$html = FormattedText::canonical(
'<p style="color: red; background: url(https://evil.example/x.png); text-align: justify">A <span class="color" data-color="pink" style="color:red">rosa</span> <span data-size="huge">riesig</span> <span style="font-size: 99px" class="x">frei</span> <span data-color="rot" data-size="xlarge" onclick="x()" style="color:blue">beides</span></p>',
true,
);
$this->assertSame('<p>A rosa riesig frei <span class="color" data-color="rot" data-size="xlarge">beides</span></p>', $html);
}
public function test_alignment_survives_in_list_items_table_cells_and_quotes(): void
{
$html = FormattedText::canonical(
'<ul><li><p style="text-align: center">Punkt</p></li></ul><table><tbody><tr><td><p style="text-align: end">Zelle</p></td></tr></tbody></table><blockquote><p style="text-align: center">Zitat</p></blockquote>',
true,
);
$this->assertStringContainsString('<li><p style="text-align: center">Punkt</p></li>', $html);
$this->assertStringContainsString('<td><p style="text-align: end">Zelle</p></td>', $html);
$this->assertStringContainsString('<blockquote><p style="text-align: center">Zitat</p></blockquote>', $html);
}
public function test_left_alignment_is_the_default_and_is_not_stored(): void
{
$this->assertSame('<p>Links</p><p>Links</p>', FormattedText::canonical('<p style="text-align: start">Links</p><p style="text-align: left">Links</p>', true));
$this->assertSame('<p style="text-align: end">Rechts</p>', FormattedText::canonical('<p style="text-align: right">Rechts</p>', true));
}
private function field(): RichEditor
{
$livewire = new class extends Component implements HasSchemas
{
use InteractsWithSchemas;
public function render(): string
{
return '';
}
};
$field = RichField::make('body', 'Text', website: true);
$field->container(Schema::make($livewire));
return $field;
}
public function test_every_value_of_the_editor_survives_the_real_state_round_trip(): void
{
$editor = $this->field();
$cast = new RichEditorStateCast($editor);
$stored = FormattedText::canonical(self::EDITOR, true);
$json = $cast->set($stored);
$back = FormattedText::canonical($cast->get($json), true);
$this->assertSame($stored, $back, 'Öffnen und Speichern im Editor ändert Ausrichtung, Größe und Farbe nicht.');
}
public function test_the_editor_offers_size_color_and_alignment_only_for_website_texts(): void
{
$website = $this->field();
$plainField = RichField::make('body', 'Text');
$plainField->container($website->getContainer());
$plain = array_merge(...$plainField->getToolbarButtons());
$buttons = array_merge(...array_map(fn (array $group): array => $group, $website->getToolbarButtons()));
foreach (['fontSize', 'textColor', 'alignStart', 'alignCenter', 'alignEnd'] as $tool) {
$this->assertNotContains($tool, $plain, "Die Verwaltung bietet $tool nicht.");
$this->assertContains($tool, $buttons, "Der Fließtext bietet $tool.");
}
$this->assertSame(array_keys(FormattedText::SITE_COLORS), array_keys($website->getTextColors()), 'Nur die festen Farben, in dieser Reihenfolge.');
$this->assertFalse($website->hasCustomTextColors(), 'Keine freie Farbe.');
$this->assertArrayHasKey('fontSize', $website->getTools());
}
public function test_the_page_shows_classes_instead_of_inline_styles(): void
{
$page = Page::query()->create(['path' => '/formatiert']);
$service = app(PageService::class);
$service->publish($page, $service->saveRevision($page, [['layout' => 'u4', 'style' => [], 'slots' => [['type' => 'prose', 'v' => 2, 'settings' => [], 'data' => ['body' => self::EDITOR]]]]], null, null, null));
$html = $this->get('/formatiert')->assertOk()->getContent();
$this->assertStringContainsString('<p class="ta-center">Zentriert mit <span class="color" data-color="gruen">grün</span> und <span data-size="large">groß</span>.</p>', $html);
$this->assertStringContainsString('<h3 class="ta-end">Rechts</h3>', $html);
$this->assertStringNotContainsString('text-align', $html);
$this->assertSame('<p class="ta-center">x</p>', ProseWidget::render('<p style="text-align: center">x</p>'));
}
public function test_a_text_that_starts_with_a_level_two_heading_is_shown_formatted_and_not_as_markup(): void
{
// Zugeliefert (Import, Einfügen aus einem anderen Text): beginnt mit h2, nicht mit einem Absatz.
$body = '<h2>Was Kinder üben</h2><p>Text mit <strong>fett</strong>.</p><h3>Nein sagen</h3><ul><li><p>Eigene Ideen</p></li><li><p>Pause nutzen</p></li></ul>';
$page = Page::query()->create(['path' => '/ueberschrift-zwei']);
$service = app(PageService::class);
$service->publish($page, $service->saveRevision($page, [['layout' => 'u4', 'style' => [], 'slots' => [['type' => 'prose', 'v' => 2, 'settings' => [], 'data' => ['body' => $body]]]]], null, null, null));
$html = $this->get('/ueberschrift-zwei')->assertOk()->getContent();
$this->assertStringContainsString('<h3>Was Kinder üben</h3><p>Text mit <strong>fett</strong>.</p><h3>Nein sagen</h3><ul><li><p>Eigene Ideen</p></li><li><p>Pause nutzen</p></li></ul>', $html);
$this->assertStringNotContainsString('&lt;h2&gt;', $html);
$this->assertStringNotContainsString('&lt;p&gt;', $html);
$this->assertStringContainsString('<h3>Was Kinder üben</h3>', ProseWidget::render($body));
}
public function test_the_stylesheet_knows_every_size_and_color(): void
{
$css = (string) file_get_contents(resource_path('css/widgets.css'));
foreach (array_keys(FormattedText::SITE_SIZES) as $size) {
$this->assertStringContainsString("span[data-size='$size']", $css);
}
foreach (array_keys(FormattedText::SITE_COLORS) as $color) {
$this->assertStringContainsString("span.color[data-color='$color']", $css);
$this->assertStringContainsString("--tc-$color:", $css);
}
$this->assertFileExists(public_path('js/rich-font-size.js'));
$script = (string) file_get_contents(public_path('js/rich-font-size.js'));
foreach (array_keys(FormattedText::SITE_SIZES) as $size) {
$this->assertStringContainsString($size.':', $script, 'Die JS-Erweiterung kennt jede Größe.');
}
}
}