Files
laraundfelix/tests/Feature/RedaktionelleStammdatenTest.php
T
LarsandClaude Opus 5.5 8a97d9c334
CI Feature / test (push) Successful in 1m13s
Deploy Test / test (push) Successful in 1m7s
Deploy Test / deploy (push) Successful in 24s
Filtere im Aufbau-Picker nach der Filterregel, nenne betroffene Objekte und tausche beim Sortieren nur Nachbarn
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-20 21:39:25 +01:00

327 lines
17 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
namespace Tests\Feature;
use App\Domain\Catalog\CompositionKind;
use App\Domain\Catalog\ContentElement;
use App\Domain\Catalog\ContentService;
use App\Domain\Catalog\EditorialContentKind;
use App\Domain\Catalog\EditorialDimension;
use App\Domain\Catalog\EditorialDimensionValue;
use App\Domain\Catalog\EditorialVocabulary;
use App\Domain\Catalog\IdentityDimension;
use App\Domain\Catalog\IdentityDimensionValue;
use App\Domain\Catalog\PillarService;
use App\Domain\Cms\Widgets\Support\WebsiteImage;
use App\Filament\Pages\RedaktionelleStammdaten;
use App\Filament\Resources\ContentElements\Pages\ListContentElements;
use App\Filament\Resources\ContentElements\Pages\ViewContentElement;
use App\Filament\Resources\Exercises\Pages\EditExercise;
use App\Filament\Support\MediaLibrary;
use App\Models\User;
use Filament\Actions\Testing\TestAction;
use Filament\Facades\Filament;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Carbon;
use Livewire\Livewire;
use Tests\TestCase;
/**
* Redaktionelle Stammdaten (FACH-56 bis FACH-58): zentrale Pflege, Schutz von Identität und Zuordnungen, zulässige
* Kontexte und dieselben Regeln in Editor, Filter und Picker.
*/
class RedaktionelleStammdatenTest extends TestCase
{
use RefreshDatabase;
protected function setUp(): void
{
parent::setUp();
Filament::setCurrentPanel(Filament::getPanel('verwaltung'));
}
public function test_a_new_value_is_offered_in_the_matching_editor_and_saved_and_found(): void
{
$editor = $this->editor();
$film = app(ContentService::class)->createElement('film', 'film', 'Lara im Park');
Livewire::actingAs($editor, 'staff')
->test(RedaktionelleStammdaten::class)
->call('zeigeGruppe', 'zielgruppe')
->callAction('anlegen', ['label' => 'Großeltern'])
->assertHasNoActionErrors()
->call('zeigeGruppe', 'materialart')
->callAction('anlegen', ['label' => 'Rollenspielkarte', 'contexts' => ['medium']])
->assertHasNoActionErrors();
$wert = $this->value('zielgruppe', 'grosseltern');
$art = EditorialContentKind::query()->where('slug', 'rollenspielkarte')->firstOrFail();
$this->assertTrue($art->is_active);
$this->assertSame(['medium'], $art->contexts);
$this->assertArrayNotHasKey($art->id, app(EditorialVocabulary::class)->kindOptions('lizenzartikel'), 'Nur im gewählten Kontext angeboten.');
Livewire::actingAs($editor, 'staff')
->test(ViewContentElement::class, ['record' => $film->getKey()])
->call('openRedaktion', 'einordnung')
->assertSee('Großeltern')
->assertSee('Rollenspielkarte')
->set('kindId', $art->id)
->set('dimensionIds', [$wert->id])
->call('saveEditorial')
->assertSet('deskFeedbackError', false);
$this->assertSame($art->id, $film->fresh()->editorial_content_kind_id);
$this->assertTrue($this->assigned($film, $wert));
Livewire::withQueryParams(['einordnung' => (string) $wert->id])->actingAs($editor, 'staff')
->test(ListContentElements::class)->assertSee('Lara im Park');
Livewire::withQueryParams(['inhaltsart' => (string) $art->id])->actingAs($editor, 'staff')
->test(ListContentElements::class)->assertSee('Lara im Park');
}
public function test_renaming_keeps_identity_and_assignments_everywhere(): void
{
$film = app(ContentService::class)->createElement('film', 'film', 'Lara im Park');
$kinder = $this->value('zielgruppe', 'kinder');
app(IdentityDimension::class)->assign($film, $kinder);
$trainer = EditorialContentKind::query()->where('slug', 'traineranleitung')->firstOrFail();
Livewire::actingAs($this->editor(), 'staff')
->test(RedaktionelleStammdaten::class)
->call('zeigeGruppe', 'zielgruppe')
->callAction(TestAction::make('bearbeiten')->table($kinder), ['label' => 'Kinder (6–10 Jahre)'])
->assertHasNoActionErrors()
->call('zeigeGruppe', 'materialart')
->callAction(TestAction::make('bearbeiten')->table($trainer), ['label' => 'Anleitung für Trainer'])
->assertHasNoActionErrors();
$kinder->refresh();
$this->assertSame('kinder', $kinder->slug, 'Die Kennung bleibt.');
$this->assertSame('Kinder (6–10 Jahre)', $kinder->label);
$this->assertTrue($this->assigned($film, $kinder));
$this->assertContains('Altersbereich und Zielgruppe: Kinder (6–10 Jahre)', array_column(app(MediaLibrary::class)->classifications(), 'label'));
// Systemkennung: Vertraulichkeit hängt an der Kennung, nicht an der Bezeichnung.
$this->assertSame('traineranleitung', $trainer->fresh()->slug);
$anleitung = app(ContentService::class)->createElement('bild', 'bild', 'Ablauf intern');
$anleitung->forceFill(['editorial_content_kind_id' => $trainer->id])->save();
$this->assertFalse(WebsiteImage::isSelectable($anleitung->fresh()));
}
public function test_deactivating_keeps_assignments_visible_blocks_new_choice_and_survives_other_saves(): void
{
$editor = $this->editor();
$film = app(ContentService::class)->createElement('film', 'film', 'Lara im Park');
$anderes = app(ContentService::class)->createElement('film', 'film', 'Felix im Bus');
$schule = $this->value('einsatzbereich', 'schule');
$art = EditorialContentKind::query()->where('slug', 'unterrichtsfilm')->firstOrFail();
app(IdentityDimension::class)->assign($film, $schule);
$film->forceFill(['editorial_content_kind_id' => $art->id])->save();
$stammdaten = Livewire::actingAs($editor, 'staff')->test(RedaktionelleStammdaten::class)
->call('zeigeGruppe', 'einsatzbereich')
->callAction(TestAction::make('umschalten')->table($schule))
->call('zeigeGruppe', 'materialart')
->callAction(TestAction::make('umschalten')->table($art));
$this->assertFalse($schule->fresh()->is_active);
$this->assertFalse($art->fresh()->is_active);
// Bestehende Zuordnung sichtbar und erhalten, auch wenn andere Angaben gespeichert werden.
Livewire::actingAs($editor, 'staff')
->test(ViewContentElement::class, ['record' => $film->getKey()])
->call('openRedaktion', 'einordnung')
->assertSee('Schule (inaktiv)')
->assertSee('Unterrichtsfilm (inaktiv)')
->set('teaser', 'Neuer Teaser')
->call('saveEditorial')
->assertSet('deskFeedbackError', false);
$this->assertTrue($this->assigned($film, $schule), 'Die inaktive Zuordnung bleibt beim Speichern anderer Angaben.');
$this->assertSame($art->id, $film->fresh()->editorial_content_kind_id);
// Neue Auswahl ausgeschlossen: weder angeboten noch serverseitig angenommen.
Livewire::actingAs($editor, 'staff')
->test(ViewContentElement::class, ['record' => $anderes->getKey()])
->call('openRedaktion', 'einordnung')
->assertSee('Kita')
->assertDontSee('Schule (inaktiv)')
->assertDontSee('Unterrichtsfilm')
->set('dimensionIds', [$schule->id])
->set('kindId', $art->id)
->call('saveEditorial')
->assertSet('deskFeedbackError', true)
->assertSet('redaktionTab', 'einordnung');
$this->assertNull($anderes->fresh()->editorial_content_kind_id);
app(IdentityDimension::class)->replaceOffered($anderes, [$schule->id]);
$this->assertFalse($this->assigned($anderes, $schule));
// Filter findet die Zuordnung weiterhin; ein gewählter Filter bleibt.
$this->assertContains('Einsatzbereich: Schule (inaktiv)', array_column(app(MediaLibrary::class)->classifications(), 'label'));
Livewire::withQueryParams(['einordnung' => (string) $schule->id])->actingAs($editor, 'staff')
->test(ListContentElements::class)->assertSet('einordnung', (string) $schule->id)->assertSee('Lara im Park')->assertDontSee('Felix im Bus');
// Reaktivieren und Reihenfolge ändern.
$stammdaten->call('zeigeGruppe', 'einsatzbereich')
->callAction(TestAction::make('umschalten')->table($schule->fresh()));
$this->assertTrue($schule->fresh()->is_active);
$erster = $this->dimension('einsatzbereich')->values()->first();
$stammdaten->callAction(TestAction::make('runter')->table($erster));
$this->assertNotSame($erster->id, $this->dimension('einsatzbereich')->values()->first()->id);
$stammdaten->callAction(TestAction::make('hoch')->table($erster->fresh()));
$this->assertSame($erster->id, $this->dimension('einsatzbereich')->values()->first()->id);
}
public function test_object_kinds_are_not_offered_as_material_of_a_medium(): void
{
$editor = $this->editor();
$bild = app(ContentService::class)->createElement('bild', 'bild', 'Szenenbild');
$programm = EditorialContentKind::query()->where('slug', 'programm')->firstOrFail();
$modul = EditorialContentKind::query()->where('slug', 'modul')->firstOrFail();
$vokabular = app(EditorialVocabulary::class);
foreach ([$vokabular->kindOptions('medium'), $vokabular->kindFilterOptions('medium')] as $optionen) {
$this->assertArrayNotHasKey($programm->id, $optionen);
$this->assertArrayNotHasKey($modul->id, $optionen);
}
$this->assertArrayHasKey($programm->id, $vokabular->kindOptions('programm'));
$this->assertArrayHasKey($modul->id, $vokabular->kindOptions('modul'));
$seite = Livewire::actingAs($editor, 'staff')->test(ViewContentElement::class, ['record' => $bild->getKey()]);
$this->assertArrayNotHasKey($programm->id, $seite->instance()->einordnungArt()['options']);
$this->assertArrayNotHasKey($programm->id, $seite->instance()->pickerMaterialarten());
$seite->set('kindId', $programm->id)->call('saveEditorial')->assertSet('deskFeedbackError', true);
$this->assertNull($bild->fresh()->editorial_content_kind_id);
}
public function test_changing_contexts_of_a_used_kind_asks_and_keeps_assignments(): void
{
$film = app(ContentService::class)->createElement('film', 'film', 'Lara im Park');
$art = EditorialContentKind::query()->where('slug', 'interview')->firstOrFail();
$film->forceFill(['editorial_content_kind_id' => $art->id])->save();
$seite = Livewire::actingAs($this->editor(), 'staff')->test(RedaktionelleStammdaten::class);
$seite->callAction(TestAction::make('bearbeiten')->table($art), ['label' => 'Interview', 'contexts' => ['lizenzartikel']])
->assertHasActionErrors(['konflikt_bestaetigt']);
$this->assertContains('medium', $art->fresh()->contexts);
Livewire::actingAs($this->editor(), 'staff')->test(RedaktionelleStammdaten::class)
->callAction(TestAction::make('bearbeiten')->table($art), ['label' => 'Interview', 'contexts' => ['lizenzartikel'], 'konflikt_bestaetigt' => true])
->assertHasNoActionErrors();
$this->assertSame(['lizenzartikel'], $art->fresh()->contexts);
$this->assertSame($art->id, $film->fresh()->editorial_content_kind_id, 'Die Zuordnung bleibt.');
$this->assertSame('Interview (hier nicht vorgesehen)', app(EditorialVocabulary::class)->kindOptions('medium', $art->id)[$art->id]);
}
public function test_used_values_cannot_be_deleted_and_system_kinds_stay_fixed(): void
{
$film = app(ContentService::class)->createElement('film', 'film', 'Lara im Park');
$kinder = $this->value('zielgruppe', 'kinder');
app(IdentityDimension::class)->assign($film, $kinder);
$unbenutzt = $this->value('zielgruppe', 'eltern');
$programm = EditorialContentKind::query()->where('slug', 'programm')->firstOrFail();
Livewire::actingAs($this->editor(), 'staff')->test(RedaktionelleStammdaten::class)
->call('zeigeGruppe', 'zielgruppe')
->assertActionHidden(TestAction::make('loeschen')->table($kinder))
->callAction(TestAction::make('loeschen')->table($unbenutzt))
->call('zeigeGruppe', 'materialart')
->assertActionHidden(TestAction::make('umschalten')->table($programm))
->assertActionHidden(TestAction::make('loeschen')->table($programm));
$this->assertNotNull($kinder->fresh());
$this->assertNull($unbenutzt->fresh());
$this->assertTrue($programm->fresh()->is_active);
}
public function test_pillars_keep_their_own_management(): void
{
$editor = $this->editor();
app(PillarService::class)->create('Neue Säule');
$this->actingAs($editor, 'staff')->get('/verwaltung/saeulen')->assertOk()->assertSee('Neue Säule');
Livewire::actingAs($editor, 'staff')->test(RedaktionelleStammdaten::class)
->assertSee('Fachliche Säulen')
->assertDontSee('pfeiler-1');
$this->assertArrayNotHasKey(EditorialDimension::PILLAR_SLUG, (new RedaktionelleStammdaten)->gruppen());
}
public function test_filters_in_picker_and_member_library_follow_their_rules_for_inactive_values(): void
{
$film = app(ContentService::class)->createElement('film', 'film', 'Lara im Park');
$schule = $this->value('einsatzbereich', 'schule');
$kita = $this->value('einsatzbereich', 'kita');
app(IdentityDimension::class)->assign($film, $schule);
$schule->update(['is_active' => false]);
$kita->update(['is_active' => false]);
$modul = app(ContentService::class)->createComposition('Grenzen üben', CompositionKind::Uebung);
// Aufbau-Picker: verwendete inaktive Werte bleiben als Filter auffindbar, unbenutzte inaktive nicht.
$seite = Livewire::actingAs($this->editor(), 'staff')->test(EditExercise::class, ['record' => $modul->getKey()]);
$labels = array_merge(...array_map(fn (array $g): array => array_column($g['values'], 'label'), $seite->instance()->pickerDimensionGroups()));
$this->assertContains('Schule (inaktiv)', $labels);
$this->assertNotContains('Kita (inaktiv)', $labels);
// Mitgliederbibliothek: nur aktive Werte, ein gewählter inaktiver bleibt sichtbar.
$vokabular = app(EditorialVocabulary::class);
$ids = fn ($dimensionen): array => $dimensionen->flatMap(fn ($d) => $d->values->pluck('id'))->map(fn ($id): int => (int) $id)->all();
$this->assertNotContains($schule->id, $ids($vokabular->memberFilterDimensions()));
$this->assertContains($schule->id, $ids($vokabular->memberFilterDimensions([$schule->id])));
}
public function test_reordering_swaps_only_the_two_neighbours(): void
{
$werte = $this->dimension('zielgruppe')->values()->get();
[$erster, $zweiter, $dritter] = [$werte[0], $werte[1], $werte[2]];
$vorher = $dritter->updated_at?->toDateTimeString();
Carbon::setTestNow(now()->addMinute());
Livewire::actingAs($this->editor(), 'staff')->test(RedaktionelleStammdaten::class)
->call('zeigeGruppe', 'zielgruppe')
->callAction(TestAction::make('runter')->table($erster));
$this->assertSame([$zweiter->id, $erster->id], $this->dimension('zielgruppe')->values()->limit(2)->pluck('id')->all());
$this->assertSame($vorher, $dritter->fresh()->updated_at?->toDateTimeString(), 'Unbeteiligte Werte werden nicht geschrieben.');
Carbon::setTestNow();
}
public function test_staff_without_the_editor_role_cannot_change_master_data(): void
{
$katalog = User::factory()->katalogverwalter()->create();
$katalog->saveAppAuthenticationSecret('JBSWY3DPEHPK3PXP');
$kinder = $this->value('zielgruppe', 'kinder');
$this->actingAs($katalog, 'staff')->get('/verwaltung/stammdaten')->assertForbidden();
Livewire::actingAs($katalog, 'staff')->test(RedaktionelleStammdaten::class)->assertForbidden();
$this->assertTrue($kinder->fresh()->is_active);
}
private function assigned(ContentElement $element, EditorialDimensionValue $value): bool
{
return IdentityDimensionValue::query()
->where('subject_type', $element->getMorphClass())
->where('subject_id', $element->id)
->where('editorial_dimension_value_id', $value->id)
->exists();
}
private function dimension(string $slug): EditorialDimension
{
return EditorialDimension::query()->where('slug', $slug)->firstOrFail();
}
private function value(string $dimension, string $slug): EditorialDimensionValue
{
return EditorialDimensionValue::query()
->where('editorial_dimension_id', $this->dimension($dimension)->id)
->where('slug', $slug)
->firstOrFail();
}
private function editor(): User
{
$editor = User::factory()->redakteur()->create();
$editor->saveAppAuthenticationSecret('JBSWY3DPEHPK3PXP');
return $editor;
}
}