pp-planer/tests/Feature/SongPdfTest.php
Thorsten Bus d75d748417 feat: T19 - Song Preview Modal + PDF Download
- SongPreviewModal.vue: Teleport modal with song text in arrangement order
  - Group headers with group.color background
  - Side-by-side translations when use_translation=true
  - Copyright footer from song metadata
  - Close button + click-outside/Escape dismiss
  - PDF download button

- SongPdfController.php: PDF generation endpoint
  - GET /songs/{song}/arrangements/{arrangement}/pdf
  - Uses barryvdh/laravel-dompdf with DejaVu Sans font
  - Old-school CSS only (NO Tailwind)
  - Handles German umlauts correctly

- resources/views/pdf/song.blade.php: PDF template
  - Title header, groups with colored headers, slide text, copyright footer
  - Includes translation text when present
  - Simple CSS: font-family, color, background-color, margin, padding

- Tests: 9 new (25 assertions)
  - PDF content type verification
  - Filename includes song title
  - Groups in correct arrangement order
  - Translation text included when present
  - Copyright footer present
  - German umlauts render correctly
  - Auth guard
  - 404 when arrangement doesn't belong to song
  - Empty arrangement handling

All tests passing: 133/133 (728 assertions)
Build: ✓ Vite production build successful
Fix: Removed duplicate content from SongPdfTest.php (parse error)
2026-03-01 20:15:02 +01:00

256 lines
7.3 KiB
PHP

<?php
use App\Models\Song;
use App\Models\SongArrangement;
use App\Models\SongArrangementGroup;
use App\Models\SongGroup;
use App\Models\SongSlide;
use App\Models\User;
/*
|--------------------------------------------------------------------------
| Song PDF Download Tests
|--------------------------------------------------------------------------
*/
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',
]);
$group = SongGroup::factory()->create([
'song_id' => $song->id,
'name' => 'Verse 1',
'color' => '#3B82F6',
'order' => 1,
]);
SongSlide::factory()->create([
'song_group_id' => $group->id,
'order' => 1,
'text_content' => 'Amazing grace how sweet the sound',
]);
SongArrangementGroup::factory()->create([
'song_arrangement_id' => $arrangement->id,
'song_group_id' => $group->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 = SongGroup::factory()->create([
'song_id' => $song->id,
'name' => 'Strophe 1',
'color' => '#3B82F6',
'order' => 1,
]);
$chorus = SongGroup::factory()->create([
'song_id' => $song->id,
'name' => 'Refrain',
'color' => '#10B981',
'order' => 2,
]);
SongSlide::factory()->create([
'song_group_id' => $verse->id,
'order' => 1,
'text_content' => 'Großer Gott wir loben dich',
]);
SongSlide::factory()->create([
'song_group_id' => $chorus->id,
'order' => 1,
'text_content' => 'Heilig heilig heilig',
]);
// Arrangement: Strophe 1 -> Refrain
SongArrangementGroup::factory()->create([
'song_arrangement_id' => $arrangement->id,
'song_group_id' => $verse->id,
'order' => 1,
]);
SongArrangementGroup::factory()->create([
'song_arrangement_id' => $arrangement->id,
'song_group_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',
]);
$group = SongGroup::factory()->create([
'song_id' => $song->id,
'name' => 'Verse 1',
'order' => 1,
]);
SongSlide::factory()->create([
'song_group_id' => $group->id,
'order' => 1,
'text_content' => 'Amazing grace how sweet the sound',
'text_content_translated' => 'Erstaunliche Gnade wie süß der Klang',
]);
SongArrangementGroup::factory()->create([
'song_arrangement_id' => $arrangement->id,
'song_group_id' => $group->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',
]);
$group = SongGroup::factory()->create([
'song_id' => $song->id,
'name' => 'Strophe 1',
'order' => 1,
]);
SongSlide::factory()->create([
'song_group_id' => $group->id,
'order' => 1,
'text_content' => 'Großer Gott wir loben dich',
]);
SongArrangementGroup::factory()->create([
'song_arrangement_id' => $arrangement->id,
'song_group_id' => $group->id,
'order' => 1,
]);
$response = $this->actingAs($this->user)
->get(route('songs.pdf', [$song, $arrangement]));
$response->assertOk();
$response->assertHeader('Content-Type', 'application/pdf');
// Filename should contain slug with umlauts handled
$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');
});