pp-planer/tests/Feature/SongPdfTest.php
Thorsten Bus bdbf0c65e3 refactor(php): rename SongGroup references throughout controllers/services/tests
Replace all SongGroup/SongArrangementGroup model usages with Label/SongArrangementLabel
after the schema migration to global labels. Updates 12 app files and 11 test files:

- SongService: createDefaultGroups now finds-or-creates global Labels by name
- ArrangementController: validates label_id (labels are global, no song-ownership)
- ProImportService: imports groups as Labels (firstOrCreate by name); does not
  overwrite existing label colors per spec
- ProExportService/SongPdfController/TranslationService/etc: traverse via
  arrangements -> arrangementLabels -> label -> songSlides chain
- All test factories and assertions adapted to label-based schema
2026-05-03 22:55:02 +02:00

357 lines
10 KiB
PHP

<?php
use App\Models\Label;
use App\Models\Song;
use App\Models\SongArrangement;
use App\Models\SongArrangementLabel;
use App\Models\SongSlide;
use App\Models\User;
beforeEach(function () {
$this->user = User::factory()->create();
});
test('song pdf download returns pdf with correct content type', function () {
$song = Song::factory()->create(['title' => 'Amazing Grace']);
$arrangement = SongArrangement::factory()->create([
'song_id' => $song->id,
'name' => 'Normal',
]);
$label = Label::factory()->create([
'name' => 'Verse 1',
'color' => '#3B82F6',
]);
SongSlide::factory()->create([
'label_id' => $label->id,
'order' => 1,
'text_content' => 'Amazing grace how sweet the sound',
]);
SongArrangementLabel::factory()->create([
'song_arrangement_id' => $arrangement->id,
'label_id' => $label->id,
'order' => 1,
]);
$response = $this->actingAs($this->user)
->get(route('songs.pdf', [$song, $arrangement]));
$response->assertOk();
$response->assertHeader('Content-Type', 'application/pdf');
});
test('song pdf contains song title in filename', function () {
$song = Song::factory()->create(['title' => 'Amazing Grace']);
$arrangement = SongArrangement::factory()->create([
'song_id' => $song->id,
'name' => 'Normal',
]);
$response = $this->actingAs($this->user)
->get(route('songs.pdf', [$song, $arrangement]));
$response->assertOk();
$contentDisposition = $response->headers->get('Content-Disposition');
expect($contentDisposition)->toContain('amazing-grace');
expect($contentDisposition)->toContain('normal');
});
test('song pdf includes arrangement groups in order', function () {
$song = Song::factory()->create(['title' => 'Großer Gott']);
$arrangement = SongArrangement::factory()->create([
'song_id' => $song->id,
'name' => 'Normal',
]);
$verse = Label::factory()->create([
'name' => 'Strophe 1',
'color' => '#3B82F6',
]);
$chorus = Label::factory()->create([
'name' => 'Refrain',
'color' => '#10B981',
]);
SongSlide::factory()->create([
'label_id' => $verse->id,
'order' => 1,
'text_content' => 'Großer Gott wir loben dich',
]);
SongSlide::factory()->create([
'label_id' => $chorus->id,
'order' => 1,
'text_content' => 'Heilig heilig heilig',
]);
SongArrangementLabel::factory()->create([
'song_arrangement_id' => $arrangement->id,
'label_id' => $verse->id,
'order' => 1,
]);
SongArrangementLabel::factory()->create([
'song_arrangement_id' => $arrangement->id,
'label_id' => $chorus->id,
'order' => 2,
]);
$response = $this->actingAs($this->user)
->get(route('songs.pdf', [$song, $arrangement]));
$response->assertOk();
$response->assertHeader('Content-Type', 'application/pdf');
});
test('song pdf includes translated text when present', function () {
$song = Song::factory()->create([
'title' => 'Amazing Grace',
'has_translation' => true,
]);
$arrangement = SongArrangement::factory()->create([
'song_id' => $song->id,
'name' => 'Normal',
]);
$label = Label::factory()->create([
'name' => 'Verse 1',
]);
SongSlide::factory()->create([
'label_id' => $label->id,
'order' => 1,
'text_content' => 'Amazing grace how sweet the sound',
'text_content_translated' => 'Erstaunliche Gnade wie süß der Klang',
]);
SongArrangementLabel::factory()->create([
'song_arrangement_id' => $arrangement->id,
'label_id' => $label->id,
'order' => 1,
]);
$response = $this->actingAs($this->user)
->get(route('songs.pdf', [$song, $arrangement]));
$response->assertOk();
$response->assertHeader('Content-Type', 'application/pdf');
});
test('song pdf includes copyright footer', function () {
$song = Song::factory()->create([
'title' => 'Amazing Grace',
'copyright_text' => 'John Newton, 1779',
'ccli_id' => '22025',
]);
$arrangement = SongArrangement::factory()->create([
'song_id' => $song->id,
'name' => 'Normal',
]);
$response = $this->actingAs($this->user)
->get(route('songs.pdf', [$song, $arrangement]));
$response->assertOk();
$response->assertHeader('Content-Type', 'application/pdf');
});
test('song pdf returns 404 when arrangement does not belong to song', function () {
$song = Song::factory()->create();
$otherSong = Song::factory()->create();
$arrangement = SongArrangement::factory()->create([
'song_id' => $otherSong->id,
]);
$response = $this->actingAs($this->user)
->get(route('songs.pdf', [$song, $arrangement]));
$response->assertNotFound();
});
test('song pdf requires authentication', function () {
$song = Song::factory()->create();
$arrangement = SongArrangement::factory()->create([
'song_id' => $song->id,
]);
$response = $this->get(route('songs.pdf', [$song, $arrangement]));
$response->assertRedirect(route('login'));
});
test('song pdf handles german umlauts correctly', function () {
$song = Song::factory()->create([
'title' => 'Großer Gott wir loben dich',
'copyright_text' => 'Überliefert',
]);
$arrangement = SongArrangement::factory()->create([
'song_id' => $song->id,
'name' => 'Übung',
]);
$label = Label::factory()->create([
'name' => 'Strophe 1',
]);
SongSlide::factory()->create([
'label_id' => $label->id,
'order' => 1,
'text_content' => 'Großer Gott wir loben dich',
]);
SongArrangementLabel::factory()->create([
'song_arrangement_id' => $arrangement->id,
'label_id' => $label->id,
'order' => 1,
]);
$response = $this->actingAs($this->user)
->get(route('songs.pdf', [$song, $arrangement]));
$response->assertOk();
$response->assertHeader('Content-Type', 'application/pdf');
$contentDisposition = $response->headers->get('Content-Disposition');
expect($contentDisposition)->toContain('.pdf');
});
test('song pdf works with empty arrangement (no groups)', function () {
$song = Song::factory()->create(['title' => 'Empty Song']);
$arrangement = SongArrangement::factory()->create([
'song_id' => $song->id,
'name' => 'Leer',
]);
$response = $this->actingAs($this->user)
->get(route('songs.pdf', [$song, $arrangement]));
$response->assertOk();
$response->assertHeader('Content-Type', 'application/pdf');
});
test('song preview returns json with groups in arrangement order', function () {
$song = Song::factory()->create([
'title' => 'Testlied',
'copyright_text' => 'Test Copyright',
'ccli_id' => '123456',
]);
$verse = Label::factory()->create([
'name' => 'Strophe 1',
'color' => '#3b82f6',
]);
$chorus = Label::factory()->create([
'name' => 'Refrain',
'color' => '#ef4444',
]);
SongSlide::factory()->create([
'label_id' => $verse->id,
'order' => 1,
'text_content' => 'Strophe Text',
]);
SongSlide::factory()->create([
'label_id' => $chorus->id,
'order' => 1,
'text_content' => 'Refrain Text',
]);
$arrangement = SongArrangement::factory()->create([
'song_id' => $song->id,
'name' => 'Normal',
]);
SongArrangementLabel::factory()->create([
'song_arrangement_id' => $arrangement->id,
'label_id' => $chorus->id,
'order' => 1,
]);
SongArrangementLabel::factory()->create([
'song_arrangement_id' => $arrangement->id,
'label_id' => $verse->id,
'order' => 2,
]);
$response = $this->actingAs($this->user)->getJson(route('songs.preview', [$song, $arrangement]));
$response->assertOk();
$response->assertJsonStructure([
'song' => ['id', 'title', 'copyright_text', 'ccli_id'],
'arrangement' => ['id', 'name'],
'groups' => [
'*' => [
'name',
'color',
'slides' => [
'*' => ['text_content', 'text_content_translated'],
],
],
],
]);
$data = $response->json();
expect($data['groups'][0]['name'])->toBe('Refrain');
expect($data['groups'][1]['name'])->toBe('Strophe 1');
expect($data['groups'][0]['slides'][0]['text_content'])->toBe('Refrain Text');
});
test('song preview includes translation text when slides have translations', function () {
$song = Song::factory()->create(['title' => 'Lied mit Übersetzung']);
$label = Label::factory()->create([
'name' => 'Verse',
]);
SongSlide::factory()->create([
'label_id' => $label->id,
'order' => 1,
'text_content' => 'Original Text',
'text_content_translated' => 'Translated Text',
]);
$arrangement = SongArrangement::factory()->create([
'song_id' => $song->id,
]);
SongArrangementLabel::factory()->create([
'song_arrangement_id' => $arrangement->id,
'label_id' => $label->id,
'order' => 1,
]);
$response = $this->actingAs($this->user)->getJson(route('songs.preview', [$song, $arrangement]));
$response->assertOk();
$data = $response->json();
expect($data['groups'][0]['slides'][0]['text_content'])->toBe('Original Text');
expect($data['groups'][0]['slides'][0]['text_content_translated'])->toBe('Translated Text');
});
test('song preview returns 404 when arrangement does not belong to song', function () {
$song = Song::factory()->create();
$otherSong = Song::factory()->create();
$arrangement = SongArrangement::factory()->create(['song_id' => $otherSong->id]);
$response = $this->actingAs($this->user)->getJson(route('songs.preview', [$song, $arrangement]));
$response->assertNotFound();
});
test('song preview requires authentication', function () {
$song = Song::factory()->create();
$arrangement = SongArrangement::factory()->create(['song_id' => $song->id]);
$response = $this->getJson(route('songs.preview', [$song, $arrangement]));
$response->assertUnauthorized();
});