pp-planer/tests/Feature/SongMatchingTest.php
Thorsten Bus d915f8cfc2 feat: Wave 2 - Service list, Song CRUD, Slide upload, Arrangements, Song matching, Translation
T8: Service List Page
- ServiceController with index, finalize, reopen actions
- Services/Index.vue with status indicators (songs mapped/arranged, slides uploaded)
- German UI with finalize/reopen toggle buttons
- Status aggregation via SQL subqueries for efficiency
- Tests: 3 passing (46 assertions)

T9: Song CRUD Backend
- SongController with full REST API (index, store, show, update, destroy)
- SongService for default groups/arrangements creation
- SongRequest validation (title required, ccli_id unique)
- Search by title and CCLI ID
- last_used_in_service accessor via service_songs join
- Tests: 20 passing (85 assertions)

T10: Slide Upload Component
- SlideController with store, destroy, updateExpireDate
- SlideUploader.vue with vue3-dropzone drag-and-drop
- SlideGrid.vue with thumbnail grid and inline expire date editing
- Multi-format support: images (sync), PPT (async job), ZIP (extract)
- Type validation: information (global), moderation/sermon (service-specific)
- Tests: 15 passing (37 assertions)

T11: Arrangement Configurator
- ArrangementController with store, clone, update, destroy
- ArrangementConfigurator.vue with vue-draggable-plus
- Drag-and-drop arrangement editor with colored group pills
- Clone from default or existing arrangement
- Color picker for group customization
- Prevent deletion of last arrangement
- Tests: 4 passing (17 assertions)

T12: Song Matching Service
- SongMatchingService with autoMatch, manualAssign, requestCreation, unassign
- ServiceSongController API endpoints for song assignment
- Auto-match by CCLI ID during CTS sync
- Manual assignment with searchable song select
- Email request for missing songs (MissingSongRequest mailable)
- Tests: 14 passing (33 assertions)

T13: Translation Service
- TranslationService with fetchFromUrl, importTranslation, removeTranslation
- TranslationController API endpoints
- URL scraping (best-effort HTTP fetch with strip_tags)
- Line-count distribution algorithm (match original slide line counts)
- Mark song as translated, remove translation
- Tests: 18 passing (18 assertions)

All tests passing: 103/103 (488 assertions)
Build: ✓ Vite production build successful
German UI: All user-facing text in German with 'Du' form
2026-03-01 19:55:37 +01:00

251 lines
7.5 KiB
PHP

<?php
use App\Mail\MissingSongRequest;
use App\Models\Service;
use App\Models\ServiceSong;
use App\Models\Song;
use App\Models\User;
use App\Services\SongMatchingService;
use Illuminate\Support\Facades\Mail;
/*
|--------------------------------------------------------------------------
| SongMatchingService — Unit-level feature tests
|--------------------------------------------------------------------------
*/
test('autoMatch ordnet Song per CCLI-ID zu', function () {
$song = Song::factory()->create(['ccli_id' => '7115744']);
$serviceSong = ServiceSong::factory()->create([
'cts_ccli_id' => '7115744',
'song_id' => null,
'matched_at' => null,
]);
$service = app(SongMatchingService::class);
$result = $service->autoMatch($serviceSong);
expect($result)->toBeTrue();
$serviceSong->refresh();
expect($serviceSong->song_id)->toBe($song->id);
expect($serviceSong->matched_at)->not->toBeNull();
});
test('autoMatch gibt false zurück wenn kein CCLI-ID vorhanden', function () {
$serviceSong = ServiceSong::factory()->create([
'cts_ccli_id' => null,
'song_id' => null,
'matched_at' => null,
]);
$service = app(SongMatchingService::class);
$result = $service->autoMatch($serviceSong);
expect($result)->toBeFalse();
$serviceSong->refresh();
expect($serviceSong->song_id)->toBeNull();
});
test('autoMatch gibt false zurück wenn kein passender Song in DB', function () {
$serviceSong = ServiceSong::factory()->create([
'cts_ccli_id' => '9999999',
'song_id' => null,
'matched_at' => null,
]);
$service = app(SongMatchingService::class);
$result = $service->autoMatch($serviceSong);
expect($result)->toBeFalse();
$serviceSong->refresh();
expect($serviceSong->song_id)->toBeNull();
});
test('autoMatch überspringt bereits zugeordnete Songs', function () {
$existingSong = Song::factory()->create(['ccli_id' => '7115744']);
$serviceSong = ServiceSong::factory()->create([
'cts_ccli_id' => '7115744',
'song_id' => $existingSong->id,
'matched_at' => now(),
]);
$service = app(SongMatchingService::class);
$result = $service->autoMatch($serviceSong);
expect($result)->toBeFalse();
$serviceSong->refresh();
expect($serviceSong->song_id)->toBe($existingSong->id);
});
test('manualAssign ordnet Song manuell zu', function () {
$song = Song::factory()->create();
$serviceSong = ServiceSong::factory()->create([
'song_id' => null,
'matched_at' => null,
]);
$service = app(SongMatchingService::class);
$service->manualAssign($serviceSong, $song);
$serviceSong->refresh();
expect($serviceSong->song_id)->toBe($song->id);
expect($serviceSong->matched_at)->not->toBeNull();
});
test('manualAssign überschreibt bestehende Zuordnung', function () {
$oldSong = Song::factory()->create();
$newSong = Song::factory()->create();
$serviceSong = ServiceSong::factory()->create([
'song_id' => $oldSong->id,
'matched_at' => now()->subDay(),
]);
$service = app(SongMatchingService::class);
$service->manualAssign($serviceSong, $newSong);
$serviceSong->refresh();
expect($serviceSong->song_id)->toBe($newSong->id);
expect($serviceSong->matched_at)->not->toBeNull();
});
test('requestCreation sendet E-Mail und setzt request_sent_at', function () {
Mail::fake();
$serviceSong = ServiceSong::factory()->create([
'cts_song_name' => 'Großer Gott',
'cts_ccli_id' => '12345678',
'request_sent_at' => null,
]);
$service = app(SongMatchingService::class);
$service->requestCreation($serviceSong);
Mail::assertSent(MissingSongRequest::class, function (MissingSongRequest $mail) {
return $mail->songName === 'Großer Gott'
&& $mail->ccliId === '12345678';
});
$serviceSong->refresh();
expect($serviceSong->request_sent_at)->not->toBeNull();
});
test('unassign entfernt Zuordnung', function () {
$song = Song::factory()->create();
$serviceSong = ServiceSong::factory()->create([
'song_id' => $song->id,
'matched_at' => now(),
]);
$service = app(SongMatchingService::class);
$service->unassign($serviceSong);
$serviceSong->refresh();
expect($serviceSong->song_id)->toBeNull();
expect($serviceSong->matched_at)->toBeNull();
});
/*
|--------------------------------------------------------------------------
| ServiceSongController — API endpoint tests
|--------------------------------------------------------------------------
*/
test('POST /api/service-songs/{id}/assign ordnet Song zu', function () {
$user = User::factory()->create();
$song = Song::factory()->create();
$serviceSong = ServiceSong::factory()->create([
'song_id' => null,
'matched_at' => null,
]);
$response = $this->actingAs($user)
->postJson("/api/service-songs/{$serviceSong->id}/assign", [
'song_id' => $song->id,
]);
$response->assertOk()
->assertJson(['message' => 'Song erfolgreich zugeordnet']);
$serviceSong->refresh();
expect($serviceSong->song_id)->toBe($song->id);
});
test('POST /api/service-songs/{id}/assign validiert song_id', function () {
$user = User::factory()->create();
$serviceSong = ServiceSong::factory()->create([
'song_id' => null,
]);
$response = $this->actingAs($user)
->postJson("/api/service-songs/{$serviceSong->id}/assign", [
'song_id' => 99999,
]);
$response->assertUnprocessable();
});
test('POST /api/service-songs/{id}/request sendet Anfrage-E-Mail', function () {
Mail::fake();
$user = User::factory()->create();
$serviceSong = ServiceSong::factory()->create([
'cts_song_name' => 'Way Maker',
'cts_ccli_id' => '7115744',
'request_sent_at' => null,
]);
$response = $this->actingAs($user)
->postJson("/api/service-songs/{$serviceSong->id}/request");
$response->assertOk()
->assertJson(['message' => 'Anfrage wurde gesendet']);
Mail::assertSent(MissingSongRequest::class);
$serviceSong->refresh();
expect($serviceSong->request_sent_at)->not->toBeNull();
});
test('POST /api/service-songs/{id}/unassign entfernt Zuordnung', function () {
$user = User::factory()->create();
$song = Song::factory()->create();
$serviceSong = ServiceSong::factory()->create([
'song_id' => $song->id,
'matched_at' => now(),
]);
$response = $this->actingAs($user)
->postJson("/api/service-songs/{$serviceSong->id}/unassign");
$response->assertOk()
->assertJson(['message' => 'Zuordnung entfernt']);
$serviceSong->refresh();
expect($serviceSong->song_id)->toBeNull();
expect($serviceSong->matched_at)->toBeNull();
});
test('API Endpunkte erfordern Authentifizierung', function () {
$serviceSong = ServiceSong::factory()->create();
$this->postJson("/api/service-songs/{$serviceSong->id}/assign", ['song_id' => 1])
->assertUnauthorized();
$this->postJson("/api/service-songs/{$serviceSong->id}/request")
->assertUnauthorized();
$this->postJson("/api/service-songs/{$serviceSong->id}/unassign")
->assertUnauthorized();
});
test('API gibt 404 für nicht existierende ServiceSong', function () {
$user = User::factory()->create();
$song = Song::factory()->create();
$this->actingAs($user)
->postJson('/api/service-songs/99999/assign', ['song_id' => $song->id])
->assertNotFound();
});