presseportale/tests/Feature/PressReleaseClassificationJobTest.php
Kevin Adametz a000238ca8 User Panel: Phase-8-Abschluss, Titelbild/Lizenzen/Zeitzonen und KI-Pruef-Pipeline
Phase 8 (Rest) + Umbauten vom 10./11.06.:
- Ein Titelbild pro PM (Cover 1280x580), SVG-Platzhalter-Set + Picker,
  PressReleaseCoverImage-Resolver
- Lizenz-/Rechteformular nach "Lizenztyp Bildupload" (7 Lizenztypen,
  Personen-/Sachrechte-Status, bedingte Pflichtfelder, Risikohinweise)
- Veroeffentlichungs-Box vereinfacht (Embargo aus der Form-UI entfernt),
  geplante Termine in Europe/Berlin (Speicherung UTC, DISPLAY_TIMEZONE)
- Quota-Stub (users.press_release_quota) + monatlicher Reset-Command
- Einreichungs-Modal einheitlich in Show/Create/Edit; Ghost-Buttons auf
  filled; PM-Editor-Layout responsive entkoppelt (.pr-editor-layout)

KI-Pruef-Pipeline (Phasen 1-5 des Entwicklungsplans):
- API-Haertung: status nicht mehr per API setzbar, eigene Submit-Route
  durch denselben Funnel (Blacklist, Quota, Status-Log)
- Klassifikation Rot/Gelb/Gruen asynchron (Queue classification,
  OpenAI-Treiber + deterministischer Fallback), ki_audits-Audit-Log
- Routing: Rot -> rejected + Mail, Gelb -> Review-Queue, Gruen ->
  Auto-Publish; Scheduler publiziert nur gruene faellige PMs
- Content-Score 0-100 -> Stufe (Standard/Geprueft/Hochwertig) inkl.
  Editor-Panel und Badges; Re-Klassifikation/-Score bei Aenderung
- Admin: KI-Badge + Filter, On-Demand-Pruefung mit Anbieter-Override

Suite: 442 passed, 4 skipped.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-06-12 08:30:13 +00:00

203 lines
7.3 KiB
PHP

<?php
use App\Enums\PressReleaseClassification;
use App\Enums\PressReleaseStatus;
use App\Jobs\ClassifyPressRelease;
use App\Mail\PressReleasePublished;
use App\Mail\PressReleaseRejected;
use App\Models\KiAudit;
use App\Models\PressRelease;
use App\Models\User;
use App\Services\PressRelease\Classification\ClassificationManager;
use App\Services\PressRelease\PressReleaseService;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Facades\Queue;
function fakeOpenAiClassification(string $classification, array $reasons = []): void
{
config()->set('scoring.classification.provider', 'openai');
config()->set('services.openai.api_key', 'test-key');
Http::fake([
'*' => Http::response([
'choices' => [
['message' => ['content' => json_encode([
'classification' => $classification,
'reasons' => $reasons,
])]],
],
], 200),
]);
}
test('classify job stores the openai classification and writes an audit', function () {
fakeOpenAiClassification('green');
$pressRelease = PressRelease::factory()->create(['title' => 'Sauber', 'text' => 'Inhalt']);
(new ClassifyPressRelease($pressRelease->id))->handle(
app(ClassificationManager::class),
app(PressReleaseService::class),
);
$fresh = $pressRelease->fresh();
expect($fresh->classification)->toBe(PressReleaseClassification::Green);
expect($fresh->classified_at)->not->toBeNull();
$audit = KiAudit::where('press_release_id', $pressRelease->id)->firstOrFail();
expect($audit->type)->toBe(KiAudit::TYPE_CLASSIFICATION);
expect($audit->provider)->toBe('openai');
expect($audit->result)->toBe('green');
});
test('classify job records a yellow classification with reasons', function () {
fakeOpenAiClassification('yellow', ['grenzwertige Werbesprache']);
$pressRelease = PressRelease::factory()->create();
(new ClassifyPressRelease($pressRelease->id))->handle(
app(ClassificationManager::class),
app(PressReleaseService::class),
);
$audit = KiAudit::where('press_release_id', $pressRelease->id)->firstOrFail();
expect($pressRelease->fresh()->classification)->toBe(PressReleaseClassification::Yellow);
expect($audit->reason)->toContain('Werbesprache');
});
test('classify job falls back to the deterministic driver when openai fails', function () {
config()->set('scoring.classification.provider', 'openai');
config()->set('services.openai.api_key', 'test-key');
config()->set('blacklist.words', ['penis']);
Http::fake(['*' => Http::response('error', 500)]);
// Sauberer Text -> deterministischer Fallback liefert green.
$pressRelease = PressRelease::factory()->create(['title' => 'Sauber', 'text' => 'Inhalt']);
(new ClassifyPressRelease($pressRelease->id))->handle(
app(ClassificationManager::class),
app(PressReleaseService::class),
);
$audit = KiAudit::where('press_release_id', $pressRelease->id)->firstOrFail();
expect($pressRelease->fresh()->classification)->toBe(PressReleaseClassification::Green);
expect($audit->provider)->toBe('deterministic');
});
test('deterministic driver classifies a banned word as red', function () {
config()->set('scoring.classification.provider', 'deterministic');
config()->set('blacklist.words', ['penis']);
$pressRelease = PressRelease::factory()->create(['title' => 'Titel penis', 'text' => 'Inhalt']);
(new ClassifyPressRelease($pressRelease->id))->handle(
app(ClassificationManager::class),
app(PressReleaseService::class),
);
$audit = KiAudit::where('press_release_id', $pressRelease->id)->firstOrFail();
expect($pressRelease->fresh()->classification)->toBe(PressReleaseClassification::Red);
expect($audit->provider)->toBe('deterministic');
});
test('classify job routes a red classification to rejected and notifies the author', function () {
Mail::fake();
config()->set('scoring.classification.provider', 'deterministic');
config()->set('blacklist.words', ['penis']);
$author = User::factory()->create();
$pressRelease = PressRelease::factory()->create([
'user_id' => $author->id,
'status' => PressReleaseStatus::Review->value,
'title' => 'Titel penis',
'text' => 'Inhalt',
]);
(new ClassifyPressRelease($pressRelease->id))->handle(
app(ClassificationManager::class),
app(PressReleaseService::class),
);
expect($pressRelease->fresh()->status)->toBe(PressReleaseStatus::Rejected);
Mail::assertQueued(PressReleaseRejected::class);
});
test('classify job auto-publishes a green classification without a schedule', function () {
Mail::fake();
config()->set('scoring.classification.provider', 'deterministic');
config()->set('blacklist.words', []);
$pressRelease = PressRelease::factory()->create([
'status' => PressReleaseStatus::Review->value,
'scheduled_at' => null,
'title' => 'Sauber',
'text' => 'Inhalt',
]);
(new ClassifyPressRelease($pressRelease->id))->handle(
app(ClassificationManager::class),
app(PressReleaseService::class),
);
expect($pressRelease->fresh()->status)->toBe(PressReleaseStatus::Published);
Mail::assertQueued(PressReleasePublished::class);
});
test('classify job leaves a green scheduled press release in review for the scheduler', function () {
config()->set('scoring.classification.provider', 'deterministic');
config()->set('blacklist.words', []);
$pressRelease = PressRelease::factory()->create([
'status' => PressReleaseStatus::Review->value,
'scheduled_at' => now()->addWeek(),
'title' => 'Sauber',
'text' => 'Inhalt',
]);
(new ClassifyPressRelease($pressRelease->id))->handle(
app(ClassificationManager::class),
app(PressReleaseService::class),
);
$fresh = $pressRelease->fresh();
expect($fresh->classification)->toBe(PressReleaseClassification::Green);
expect($fresh->status)->toBe(PressReleaseStatus::Review);
});
test('classify job keeps a yellow classification in the manual review queue', function () {
fakeOpenAiClassification('yellow', ['grenzwertig']);
$pressRelease = PressRelease::factory()->create([
'status' => PressReleaseStatus::Review->value,
'title' => 'Grenzfall',
'text' => 'Inhalt',
]);
(new ClassifyPressRelease($pressRelease->id))->handle(
app(ClassificationManager::class),
app(PressReleaseService::class),
);
expect($pressRelease->fresh()->status)->toBe(PressReleaseStatus::Review);
});
test('submitForReview dispatches the classification job onto the classification queue', function () {
Queue::fake();
config()->set('blacklist.words', []);
$user = User::factory()->create();
$pressRelease = PressRelease::factory()->create([
'user_id' => $user->id,
'status' => PressReleaseStatus::Draft->value,
'title' => 'Sauber',
'text' => 'Inhalt',
]);
app(PressReleaseService::class)->submitForReview($pressRelease);
Queue::assertPushedOn('classification', ClassifyPressRelease::class, function (ClassifyPressRelease $job) use ($pressRelease) {
return $job->pressReleaseId === $pressRelease->id;
});
});