Loheide-eu-mitarbeiterplatt.../tests/Feature/HomepageBuilderTest.php
Friederich Loheide 2c96fcce1c
Some checks failed
CI / PHP 8.3 / mariadb:10.6 (pull_request) Successful in 5m21s
CI / PHP 8.4 / mariadb:10.6 (pull_request) Successful in 4m55s
CI / PHP 8.3 / mariadb:11.4 (pull_request) Successful in 4m40s
CI / PHP 8.4 / mariadb:11.4 (pull_request) Successful in 4m42s
CI / PHP 8.3 / mysql:8.0 (pull_request) Failing after 7m25s
CI / PHP 8.4 / mysql:8.0 (pull_request) Failing after 7m57s
CI / PHP 8.3 / mysql:8.4 (pull_request) Failing after 6m16s
CI / PHP 8.4 / mysql:8.4 (pull_request) Failing after 5m34s
CI / Build-Artefakte aktuell (pull_request) Successful in 1m14s
Startseite frei gestalten, Neuigkeiten-Block mit Kategorien-Filter (0.26.0)
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-09-27 09:38:18 +00:00

127 lines
6.8 KiB
PHP

<?php
namespace Tests\Feature;
use App\Dashboard\Homepage;
use App\Filament\Pages\DashboardSettings;
use App\Models\Role;
use App\Models\User;
use App\Pagebuilder\Blocks;
use App\Support\Settings;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Inertia\Testing\AssertableInertia as Assert;
use Inhaus\Plugins\News\Models\NewsCategory;
use Inhaus\Plugins\News\Models\NewsPost;
use Livewire\Livewire;
use Tests\TestCase;
class HomepageBuilderTest extends TestCase
{
use RefreshDatabase;
private User $admin;
protected function setUp(): void
{
parent::setUp();
$this->admin = User::factory()->admin()->create(['first_name' => 'Ada']);
}
/** Zwei Neuigkeiten-Bereiche nebeneinander: links nur „Wichtig zu wissen“, rechts alle anderen */
private function twoNewsColumns(): array
{
$news = fn (string $title, string $filter) => ['type' => 'news.latest', 'data' => ['title' => $title, 'filter' => $filter, 'categories' => ['wichtig'], 'count' => 3, 'layout' => 'large', 'more_label' => 'Alle Beiträge']];
return ['blocks' => [
['type' => 'columns', 'data' => ['layout' => '1-1'], 'items' => [
['blocks' => [$news('Wichtig zu wissen', 'include')]],
['blocks' => [$news('Aktuelles', 'exclude')]],
]],
['type' => 'dashboard.widget', 'data' => ['widget' => 'core.greeting']],
]];
}
public function test_editor_starts_from_the_current_tiles(): void
{
$this->actingAs($this->admin)->get('/startseite/gestalten')->assertOk()->assertInertia(fn (Assert $page) => $page
->component('HomepageEditor')
->where('state.mode', 'widgets')
->where('blockTypes', fn ($types) => collect($types)->contains('key', 'dashboard.widget'))
->where('content.blocks', fn ($blocks) => collect($blocks)->contains('type', 'columns')
&& collect(collect($blocks)->firstWhere('type', 'columns')['items'][1]['blocks'])->contains(fn ($b) => $b['data']['widget'] === 'core.greeting')));
// Kachel-Blöcke gibt es nur auf der Startseite, nicht im Seiteneditor
$this->assertFalse(collect(app(Blocks::class)->forEditor())->contains('key', 'dashboard.widget'));
$this->actingAs(User::factory()->create())->get('/startseite/gestalten')->assertForbidden();
}
public function test_draft_preview_publish_and_news_filters(): void
{
$important = NewsCategory::factory()->create(['name' => 'Wichtig zu wissen', 'slug' => 'wichtig']);
$company = NewsCategory::factory()->create(['name' => 'Unternehmen', 'slug' => 'unternehmen']);
NewsPost::factory()->create(['title' => 'Brandschutzübung', 'category_id' => $important->id, 'published_at' => now()->subHour()]);
NewsPost::factory()->create(['title' => 'Sommerfest', 'category_id' => $company->id, 'published_at' => now()->subHours(2)]);
$employee = User::factory()->create();
$this->actingAs($this->admin)->putJson('/startseite/gestalten/entwurf', ['content' => $this->twoNewsColumns()])->assertOk()->assertJsonPath('state.has_draft', true);
// Entwurf: nur in der Vorschau, die Startseite zeigt weiter Kacheln
$this->actingAs($employee)->get('/')->assertInertia(fn (Assert $page) => $page->has('widgets')->missing('content'));
$this->actingAs($employee)->get('/?entwurf=1')->assertInertia(fn (Assert $page) => $page->missing('content'));
$this->actingAs($this->admin)->get('/?entwurf=1')->assertInertia(fn (Assert $page) => $page->where('preview', true)->has('content.blocks'));
$this->postJson('/startseite/gestalten/veroeffentlichen', ['activate' => true])->assertOk()->assertJsonPath('state.mode', 'builder');
$this->actingAs($employee)->get('/')->assertInertia(fn (Assert $page) => $page
->where('content.blocks.0.items.0.blocks.0.props.posts.0.title', 'Brandschutzübung')
->where('content.blocks.0.items.0.blocks.0.props.more_url', '/neuigkeiten?kategorie=wichtig')
->where('content.blocks.0.items.1.blocks.0.props.posts', fn ($posts) => collect($posts)->pluck('title')->all() === ['Sommerfest'])
->where('content.blocks.0.items.1.blocks.0.props.more_url', '/neuigkeiten?ohne=wichtig')
->where('content.blocks.1.props.component', 'core::Greeting'));
// „Alle Beiträge“ rechts: alles außer „Wichtig zu wissen“
$this->get('/neuigkeiten?ohne=wichtig')->assertInertia(fn (Assert $page) => $page
->where('filter', 'Ohne: Wichtig zu wissen')
->where('posts.data', fn ($posts) => collect($posts)->pluck('title')->all() === ['Sommerfest']));
}
public function test_blocks_can_target_roles_and_versions_can_be_restored(): void
{
$pflege = Role::query()->create(['name' => 'Pflege']);
$homepage = app(Homepage::class);
$homepage->saveDraft(['blocks' => [
['type' => 'heading', 'data' => ['text' => 'Für alle']],
['type' => 'heading', 'data' => ['text' => 'Nur Pflege'], 'visibility' => ['role' => [(string) $pflege->id]]],
]], $this->admin);
$homepage->publish($this->admin);
$homepage->setMode('builder');
$this->actingAs(User::factory()->create())->get('/')->assertInertia(fn (Assert $page) => $page->where('content.blocks', fn ($b) => count($b) === 1));
$this->actingAs(User::factory()->withRoles([$pflege])->create())->get('/')->assertInertia(fn (Assert $page) => $page->where('content.blocks', fn ($b) => count($b) === 2));
$homepage->saveDraft(['blocks' => []], $this->admin);
$homepage->publish($this->admin);
$first = $homepage->revisions()[1]['id'];
$this->actingAs($this->admin)->postJson("/startseite/gestalten/versionen/{$first}/wiederherstellen")->assertOk()->assertJsonCount(2, 'content.blocks');
}
public function test_admin_switches_mode_without_losing_the_tile_layout(): void
{
app(Settings::class)->set('dashboard.layout', ['core.greeting' => ['sort' => 0, 'visible' => false]]);
app(Homepage::class)->saveDraft(['blocks' => [['type' => 'heading', 'data' => ['text' => 'Hallo']]]], $this->admin);
app(Homepage::class)->publish($this->admin);
$this->actingAs($this->admin);
Livewire::test(DashboardSettings::class)->set('data.mode', 'builder')->call('save');
$this->assertSame('builder', app(Homepage::class)->mode());
$this->assertFalse(app(Settings::class)->get('dashboard.layout')['core.greeting']['visible']);
$this->get('/')->assertInertia(fn (Assert $page) => $page->where('content.blocks.0.data.text', 'Hallo'));
Livewire::test(DashboardSettings::class)->set('data.mode', 'widgets')->call('save');
$this->get('/')->assertInertia(fn (Assert $page) => $page->has('widgets')->missing('content'));
}
}