Loheide-eu-mitarbeiterplatt.../tests/Feature/Admin/AppearanceTest.php
Friederich Loheide c495dfa1cb
Some checks failed
CI / PHP 8.4 / mariadb:10.6 (pull_request) Successful in 3m29s
CI / PHP 8.3 / mariadb:10.6 (pull_request) Successful in 3m31s
CI / PHP 8.4 / mariadb:11.4 (pull_request) Successful in 3m56s
CI / PHP 8.3 / mariadb:11.4 (pull_request) Successful in 4m2s
CI / PHP 8.3 / mysql:8.0 (pull_request) Failing after 5m20s
CI / PHP 8.4 / mysql:8.0 (pull_request) Failing after 5m22s
CI / PHP 8.3 / mysql:8.4 (pull_request) Failing after 4m42s
CI / PHP 8.4 / mysql:8.4 (pull_request) Failing after 4m41s
CI / Build-Artefakte aktuell (pull_request) Successful in 1m18s
Backend: eigenes kompaktes Theme, Übersicht, Cluster mit Unterseiten, Plugin-Liste
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-09-26 20:50:17 +00:00

250 lines
12 KiB
PHP

<?php
namespace Tests\Feature\Admin;
use App\Filament\Pages\Appearance\AppearanceColors;
use App\Filament\Pages\Appearance\AppearanceIdentity;
use App\Models\User;
use App\Support\Branding;
use App\Support\ColorPalette;
use App\Support\Settings;
use App\Support\Theme\Contrast;
use App\Support\Theme\Icons;
use App\Support\Theme\SvgSanitizer;
use App\Support\Theme\Theme;
use App\Support\Theme\ThemeCompiler;
use App\Support\Theme\ThemeTransfer;
use Illuminate\Auth\Notifications\ResetPassword;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Storage;
use Inertia\Testing\AssertableInertia as Assert;
use Livewire\Livewire;
use Tests\TestCase;
class AppearanceTest extends TestCase
{
use RefreshDatabase;
protected function setUp(): void
{
parent::setUp();
Storage::fake('uploads');
}
private function refreshBranding(): void
{
app(Branding::class)->refresh();
}
public function test_changes_are_a_draft_until_published(): void
{
$admin = User::factory()->admin()->create();
$employee = User::factory()->create();
// Zwei Unterseiten, ein gemeinsamer Entwurf
Livewire::actingAs($admin)->test(AppearanceIdentity::class)
->fillForm(['portal_name' => 'Caritas-Netz'])
->call('save')
->assertHasNoFormErrors();
Livewire::actingAs($admin)->test(AppearanceColors::class)
->fillForm(['primary_color' => '#a3122a', 'header_style' => 'brand'])
->call('save')
->assertHasNoFormErrors()
->assertDispatched('theme-preview-reload');
// Andere sehen weiter den veröffentlichten Stand.
$this->refreshBranding();
$this->actingAs($employee)->get('/')->assertInertia(fn (Assert $p) => $p->where('branding.portal_name', 'Inhaus')->where('branding.preview', false));
// Die Person mit Vorschau sieht den Entwurf.
$this->refreshBranding();
$this->actingAs($admin)->withSession([Branding::PREVIEW_SESSION_KEY => true])->get('/')
->assertInertia(fn (Assert $p) => $p->where('branding.portal_name', 'Caritas-Netz')->where('branding.header_style', 'brand')->where('branding.preview', true))
->assertSee('--brand:#A3122A', false);
Livewire::actingAs($admin)->test(AppearanceIdentity::class)->callAction('publish');
$this->refreshBranding();
$published = app(Theme::class)->published();
$this->assertSame('Caritas-Netz', $published['portal_name']);
$this->assertFalse(app(Theme::class)->hasDraft());
Storage::disk('uploads')->assertExists($published['stylesheet']);
$this->assertStringContainsString('--brand:#A3122A', Storage::disk('uploads')->get($published['stylesheet']));
$this->actingAs($employee)->get('/')
->assertInertia(fn (Assert $p) => $p->where('branding.portal_name', 'Caritas-Netz')->where('branding.primary_color', '#A3122A'))
->assertSee(Storage::disk('uploads')->url($published['stylesheet']), false);
}
public function test_draft_can_be_discarded_and_reset(): void
{
$admin = User::factory()->admin()->create();
app(Theme::class)->saveDraft(['primary_color' => '#123456']);
Livewire::actingAs($admin)->test(AppearanceColors::class)
->assertFormSet(['primary_color' => '#123456'])
->callAction('discard')
->assertFormSet(['primary_color' => Theme::DEFAULTS['primary_color']]);
$this->assertFalse(app(Theme::class)->hasDraft());
Livewire::actingAs($admin)->test(AppearanceColors::class)->callAction('reset');
$this->assertTrue(app(Theme::class)->hasDraft());
}
public function test_validation(): void
{
Livewire::actingAs(User::factory()->admin()->create())->test(AppearanceColors::class)
->fillForm(['primary_color' => 'rot'])
->call('save')
->assertHasFormErrors(['primary_color']);
Livewire::actingAs(User::factory()->admin()->create())->test(AppearanceIdentity::class)
->fillForm(['portal_name' => ''])
->call('save')
->assertHasFormErrors(['portal_name']);
$this->actingAs(User::factory()->create())->get('/admin/erscheinungsbild')->assertForbidden();
$this->get('/erscheinungsbild/vorschau/anmeldung')->assertForbidden();
}
public function test_settings_from_phase_0_are_taken_over(): void
{
app(Settings::class)->setMany(['branding.portal_name' => 'Altname', 'branding.primary_color' => '#E20074']);
$this->assertSame('Altname', app(Theme::class)->published()['portal_name']);
$this->refreshBranding();
$this->actingAs(User::factory()->create())->get('/')->assertSee('--brand:#E20074', false);
}
public function test_light_brand_colors_get_readable_link_colors(): void
{
$css = app(ThemeCompiler::class)->css(Theme::normalize(['primary_color' => '#FFD400']));
$palette = ColorPalette::fromHex('#FFD400');
$shade = Contrast::textShadeOnLight('#FFD400');
$this->assertGreaterThan(600, $shade);
$this->assertStringContainsString('--brand:#FFD400', $css, 'Die Markenfarbe selbst bleibt exakt.');
$this->assertStringContainsString('--brand-text:'.$palette[$shade], $css);
$this->assertGreaterThanOrEqual(4.5, ColorPalette::contrast($palette[$shade], '#FFFFFF'));
$report = Contrast::report('#FFD400');
$this->assertSame('info', $report[1]['level']);
// Dunkle Markenfarbe: unverändert als Linkfarbe.
$this->assertSame(600, Contrast::textShadeOnLight('#0F4C5C'));
}
public function test_fonts_shape_and_custom_css_end_up_in_the_stylesheet(): void
{
$css = app(ThemeCompiler::class)->css(Theme::normalize([
'font_body' => 'source-sans-3', 'font_heading' => 'merriweather', 'radius' => 'none', 'density' => 'compact',
'custom_css' => '.x{color:red}</style><script>', 'custom_font' => 'theme/fonts/haus.woff2',
]));
$this->assertStringContainsString('--font-body:"Source Sans 3 Variable"', $css);
$this->assertStringContainsString('--font-heading:"Merriweather Variable", ui-serif', $css);
$this->assertStringContainsString('--radius:0rem', $css);
$this->assertStringContainsString('--spacing:0.225rem', $css);
$this->assertStringContainsString('@font-face{font-family:"Portal Eigene Schrift"', $css);
$this->assertStringNotContainsString('</style>', $css);
}
public function test_forced_dark_mode_and_hidden_theme_choice(): void
{
app(Theme::class)->saveDraft(['dark_mode' => 'dark']);
app(Theme::class)->publish(app(ThemeCompiler::class), app(Icons::class));
$this->refreshBranding();
$this->actingAs(User::factory()->create(['theme' => 'light']))->get('/')
->assertSee('class="dark"', false)
->assertInertia(fn (Assert $p) => $p->where('branding.color_mode', 'dark'));
}
public function test_app_icon_becomes_favicon_and_manifest_icons(): void
{
Storage::disk('uploads')->put('theme/icon.png', UploadedFile::fake()->image('icon.png', 600, 600)->getContent());
app(Theme::class)->saveDraft(['app_icon' => 'theme/icon.png', 'portal_name' => 'Musterstift Intranet']);
app(Theme::class)->publish(app(ThemeCompiler::class), app(Icons::class));
$this->refreshBranding();
$icons = app(Theme::class)->published()['icons'];
$this->assertSame([32, 180, 192, 512], array_keys($icons));
Storage::disk('uploads')->assertExists($icons[512]);
$manifest = $this->get('/manifest.webmanifest')->assertOk()->json();
$this->assertSame('Musterstift Intranet', $manifest['name']);
$this->assertSame(['192x192', '512x512'], array_column($manifest['icons'], 'sizes'));
$this->actingAs(User::factory()->create())->get('/')->assertSee(Storage::disk('uploads')->url($icons[32]), false);
}
public function test_without_icon_a_monogram_favicon_is_used(): void
{
$this->actingAs(User::factory()->create())->get('/')->assertSee('rel="icon" href="data:image/svg+xml;base64,', false);
$this->get('/manifest.webmanifest')->assertJsonPath('icons.0.type', 'image/svg+xml');
}
public function test_svg_logos_are_stripped_of_scripts(): void
{
$clean = SvgSanitizer::sanitize('<svg xmlns="http://www.w3.org/2000/svg" onload="alert(1)"><script>alert(2)</script><a href="javascript:alert(3)"><rect width="10" height="10"/></a></svg>');
$this->assertStringNotContainsString('script', $clean);
$this->assertStringNotContainsString('onload', $clean);
$this->assertStringNotContainsString('javascript', $clean);
$this->assertStringContainsString('<rect', $clean);
$this->assertNull(SvgSanitizer::sanitize('<html><body>kein svg</body></html>'));
Storage::disk('uploads')->put('theme/logo.svg', '<svg xmlns="http://www.w3.org/2000/svg"><script>x</script></svg>');
app(Theme::class)->saveDraft(['logo_light' => 'theme/logo.svg']);
$this->assertStringNotContainsString('<script', Storage::disk('uploads')->get('theme/logo.svg'));
}
public function test_export_and_import(): void
{
Storage::disk('uploads')->put('theme/logo.png', UploadedFile::fake()->image('logo.png', 200, 60)->getContent());
$export = app(ThemeTransfer::class)->export(Theme::normalize(['portal_name' => 'Testportal', 'primary_color' => '#335577', 'logo_light' => 'theme/logo.png', 'footer_links' => [['label' => 'Impressum', 'url' => '/seiten/impressum']]]));
$this->assertSame('inhaus-theme', $export['format']);
$this->assertStringStartsWith('data:image/png;base64,', $export['files']['logo_light']);
$imported = app(ThemeTransfer::class)->import(json_encode($export));
$this->assertSame('Testportal', $imported['portal_name']);
$this->assertSame('#335577', $imported['primary_color']);
$this->assertNotSame('theme/logo.png', $imported['logo_light']);
Storage::disk('uploads')->assertExists($imported['logo_light']);
$this->expectExceptionMessage('kein exportiertes Erscheinungsbild');
app(ThemeTransfer::class)->import('{"format":"etwas-anderes"}');
}
public function test_login_page_and_emails_use_the_branding(): void
{
app(Theme::class)->saveDraft(['portal_name' => 'Musterstift', 'primary_color' => '#7A1F5C', 'login_layout' => 'centered', 'login_title' => 'Willkommen im Musterstift', 'email_footer' => 'Musterstift gGmbH · Musterweg 1']);
app(Theme::class)->publish(app(ThemeCompiler::class), app(Icons::class));
$this->refreshBranding();
$this->get('/login')->assertInertia(fn (Assert $p) => $p->where('branding.login.layout', 'centered')->where('branding.login.title', 'Willkommen im Musterstift'));
$user = User::factory()->create();
$mail = (new ResetPassword('token'))->toMail($user)->render();
$this->assertStringContainsString('Musterstift', $mail);
$this->assertStringContainsString('#7A1F5C', $mail);
$this->assertStringContainsString('Musterstift gGmbH · Musterweg 1', $mail);
$this->assertStringNotContainsString('Laravel', $mail);
}
public function test_preview_pages(): void
{
$this->actingAs(User::factory()->admin()->create());
$this->get('/erscheinungsbild/vorschau/anmeldung')->assertOk()->assertInertia(fn (Assert $p) => $p->component('Auth/Login')->where('preview', true));
$this->get('/erscheinungsbild/vorschau/beispiel')->assertOk()->assertInertia(fn (Assert $p) => $p->component('Appearance/Sample')->has('content.blocks', 7));
$this->get('/?vorschau=entwurf')->assertOk();
$this->assertTrue(session(Branding::PREVIEW_SESSION_KEY));
$this->get('/?vorschau=aus');
$this->assertNull(session(Branding::PREVIEW_SESSION_KEY));
}
}