presseportale/tests/Feature/PressReleaseSchedulingTest.php
Kevin Adametz 72432fdb82 web/api/ki: Soft Deletes wieder wirksam, Caches bei Firmenwechsel, KI-Ergebnis an Inhalt gebunden (Audit Block 3+4, Deploy D)
Block 3 — Sichtbarkeit, Löschung, Cache, Eingaben

F04: withoutGlobalScopes() ohne Argument entfernte neben dem PortalScope
auch den Soft-Delete-Filter. Öffentliche, Kunden-, API-, Vorschau- und
Medienpfade sowie die PressRelease-Helfer schreiben jetzt
withoutGlobalScope(PortalScope::class). Zwei Stellen, die der Audit nicht
kannte: die öffentliche Meldungsseite zeigte gelöschte Firmenkontakte mit
Name, Mail und Telefon; das Pressefach listete gelöschte Kontakte weiter
und ließ sie bearbeiten. Ein Architekturtest friert die 39 bewusst
verbliebenen Stellen (Admin, Importer, Befehle, Slug-Eindeutigkeit) ein.
Der Vorschau-Test prüfte bisher forceDelete statt delete.

F22: LOWER(email) machte auf companies (110k Zeilen) bei jedem Login einen
Vollscan; die Kollation ist ohnehin case-insensitiv. EmailQuery vergleicht
auf MySQL direkt über den Index, auf SQLite (Tests) weiter per LOWER.
Neuer Index contacts_email_idx.

F13: Firmen-Observer verwirft die öffentlichen Seiten-Caches bei
Löschung, Wiederherstellung und is_active-Wechsel auf jedem Weg; die
Newsroom-Seite prüft Sichtbarkeit vor dem Cache-Treffer; Kontakt-
Änderungen im Pressefach flushen. Der Tombstone-Test wärmte den Cache
vorher nicht und traf den Fehler nie.

F12: Gemeldete URLs werden über Host, Ausgabe und Slug aufgelöst
(ReportedUrlResolver); fremde Hosts zählen nicht, bei Doppelveröffentlichung
gewinnt die Erstquelle.

F11: per_page 1–100, status als Enum, language nur de/en, Kategorie muss
aktiv sein und zum Ziel-Portal passen. Legacy-Aliasse behalten ihren
Vertrag.

F23: Rückgabewert von put() wird geprüft; Logo erst speichern, dann
Datenbank, dann altes Logo löschen. Fehler enden als Feldfehler, nicht
als verlorenes Logo.

Block 4 — KI-Prüfung und Scheduler

F07: Klassifikation und Score tragen die Prüfsumme des Inhalts bei
Einplanung. Weicht sie beim Speichern ab, wird das Ergebnis verworfen
(ki_audits.discarded_at) und ein neuer Lauf eingeplant. Der Scheduler
veröffentlicht keine Meldung, deren jüngste Klassifikation nicht zum
aktuellen Text passt, sondern plant eine Prüfung mit Routing ein.

F18: Kontingent an den Kalendermonat gebunden (users.press_release_quota_period).
Verbrauch aus einem früheren Monat zählt null, auch ohne Reset-Cron; der
Reset ist idempotent und lässt den laufenden Monat in Ruhe.

F20: Der Publikations-Scheduler liefert bei technischen Fehlern einen
Fehler-Exit-Code, damit der Failure-Hook greift.

F17: KI-Jobs mit $timeout = 120, retry_after 150; Runbook ergänzt.

F19: Aggregation der Aufrufe unter einem Cache-Lock für alle Einstiege.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
2026-09-18 10:40:48 +00:00

318 lines
11 KiB
PHP

<?php
use App\Console\Commands\PublishScheduledPressReleases;
use App\Enums\PressReleaseClassification;
use App\Enums\PressReleaseStatus;
use App\Jobs\ClassifyPressRelease;
use App\Models\KiAudit;
use App\Models\PressRelease;
use App\Models\PressReleaseStatusLog;
use App\Services\PressRelease\PressReleaseService;
use Database\Seeders\RolesAndPermissionsSeeder;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Facades\Queue;
use Tests\TestCase;
beforeEach(function (): void {
/** @var TestCase $this */
$this->seed(RolesAndPermissionsSeeder::class);
Mail::fake();
});
test('publish ohne scheduled_at und ohne embargo_at setzt published_at auf jetzt', function () {
/** @var TestCase $this */
Carbon::setTestNow('2026-06-01 10:00:00');
$pr = PressRelease::factory()->create([
'status' => PressReleaseStatus::Review->value,
'scheduled_at' => null,
'embargo_at' => null,
'published_at' => null,
]);
app(PressReleaseService::class)->publish($pr);
expect($pr->fresh()->published_at?->toDateTimeString())->toBe('2026-06-01 10:00:00');
});
test('publish mit scheduled_at in der Zukunft setzt published_at auf den Termin', function () {
/** @var TestCase $this */
Carbon::setTestNow('2026-06-01 10:00:00');
$pr = PressRelease::factory()->create([
'status' => PressReleaseStatus::Review->value,
'scheduled_at' => '2026-06-05 14:30:00',
'embargo_at' => null,
'published_at' => null,
]);
app(PressReleaseService::class)->publish($pr);
expect($pr->fresh()->published_at?->toDateTimeString())->toBe('2026-06-05 14:30:00');
});
test('publish mit embargo_at in der Zukunft verschiebt published_at auf das Embargo', function () {
/** @var TestCase $this */
Carbon::setTestNow('2026-06-01 10:00:00');
$pr = PressRelease::factory()->create([
'status' => PressReleaseStatus::Review->value,
'scheduled_at' => null,
'embargo_at' => '2026-06-10 08:00:00',
'published_at' => null,
]);
app(PressReleaseService::class)->publish($pr);
expect($pr->fresh()->published_at?->toDateTimeString())->toBe('2026-06-10 08:00:00');
});
test('publish mit scheduled_at und späterem embargo_at nimmt das Embargo', function () {
/** @var TestCase $this */
Carbon::setTestNow('2026-06-01 10:00:00');
$pr = PressRelease::factory()->create([
'status' => PressReleaseStatus::Review->value,
'scheduled_at' => '2026-06-05 14:30:00',
'embargo_at' => '2026-06-10 08:00:00',
'published_at' => null,
]);
app(PressReleaseService::class)->publish($pr);
expect($pr->fresh()->published_at?->toDateTimeString())->toBe('2026-06-10 08:00:00');
});
test('publish übernimmt bereits gesetztes published_at und überschreibt nicht', function () {
/** @var TestCase $this */
Carbon::setTestNow('2026-06-01 10:00:00');
$pr = PressRelease::factory()->create([
'status' => PressReleaseStatus::Review->value,
'scheduled_at' => '2026-06-05 14:30:00',
'embargo_at' => null,
'published_at' => '2025-12-01 00:00:00',
]);
app(PressReleaseService::class)->publish($pr);
expect($pr->fresh()->published_at?->toDateTimeString())->toBe('2025-12-01 00:00:00');
});
test('publish-Source landet als source im Status-Log', function () {
/** @var TestCase $this */
$pr = PressRelease::factory()->create([
'status' => PressReleaseStatus::Review->value,
'published_at' => null,
]);
app(PressReleaseService::class)->publish($pr, source: 'scheduler');
$log = PressReleaseStatusLog::query()->latest('id')->firstOrFail();
expect($log->source)->toBe('scheduler');
expect($log->to_status)->toBe(PressReleaseStatus::Published);
});
test('Command publisht fällige Review-PMs mit scheduled_at <= now', function () {
/** @var TestCase $this */
Carbon::setTestNow('2026-06-01 12:00:00');
$due = PressRelease::factory()->create([
'status' => PressReleaseStatus::Review->value,
'classification' => PressReleaseClassification::Green->value,
'scheduled_at' => '2026-06-01 11:55:00',
'published_at' => null,
]);
Artisan::call(PublishScheduledPressReleases::class);
$fresh = $due->fresh();
expect($fresh->status)->toBe(PressReleaseStatus::Published);
expect($fresh->published_at?->toDateTimeString())->toBe('2026-06-01 11:55:00');
});
test('Command publisht fällige gelbe PMs wie grüne (Direkt-Live)', function () {
/** @var TestCase $this */
Carbon::setTestNow('2026-06-01 12:00:00');
$yellow = PressRelease::factory()->create([
'status' => PressReleaseStatus::Review->value,
'classification' => PressReleaseClassification::Yellow->value,
'scheduled_at' => '2026-06-01 11:55:00',
'published_at' => null,
]);
Artisan::call(PublishScheduledPressReleases::class);
expect($yellow->fresh()->status)->toBe(PressReleaseStatus::Published);
});
test('Command ignoriert fällige unklassifizierte PMs (Fallback: manuelle Queue)', function () {
/** @var TestCase $this */
Carbon::setTestNow('2026-06-01 12:00:00');
$unclassified = PressRelease::factory()->create([
'status' => PressReleaseStatus::Review->value,
'classification' => null,
'scheduled_at' => '2026-06-01 11:55:00',
'published_at' => null,
]);
Artisan::call(PublishScheduledPressReleases::class);
expect($unclassified->fresh()->status)->toBe(PressReleaseStatus::Review);
});
test('Command ignoriert PMs mit scheduled_at in der Zukunft', function () {
/** @var TestCase $this */
Carbon::setTestNow('2026-06-01 12:00:00');
$future = PressRelease::factory()->create([
'status' => PressReleaseStatus::Review->value,
'scheduled_at' => '2026-06-01 12:30:00',
'published_at' => null,
]);
Artisan::call(PublishScheduledPressReleases::class);
expect($future->fresh()->status)->toBe(PressReleaseStatus::Review);
});
test('Command ignoriert PMs ohne scheduled_at', function () {
/** @var TestCase $this */
Carbon::setTestNow('2026-06-01 12:00:00');
$manual = PressRelease::factory()->create([
'status' => PressReleaseStatus::Review->value,
'scheduled_at' => null,
]);
Artisan::call(PublishScheduledPressReleases::class);
expect($manual->fresh()->status)->toBe(PressReleaseStatus::Review);
});
test('Command ignoriert gelöschte fällige Pressemitteilungen', function () {
Carbon::setTestNow('2026-06-01 12:00:00');
$deleted = PressRelease::factory()->create([
'status' => PressReleaseStatus::Review->value,
'classification' => PressReleaseClassification::Green->value,
'scheduled_at' => '2026-06-01 11:55:00',
'published_at' => null,
]);
$deleted->delete();
Artisan::call(PublishScheduledPressReleases::class);
expect(PressRelease::withTrashed()->findOrFail($deleted->id)->status)
->toBe(PressReleaseStatus::Review);
});
test('Command läuft mit dry-run ohne Statusänderung', function () {
/** @var TestCase $this */
Carbon::setTestNow('2026-06-01 12:00:00');
$due = PressRelease::factory()->create([
'status' => PressReleaseStatus::Review->value,
'classification' => PressReleaseClassification::Green->value,
'scheduled_at' => '2026-06-01 11:50:00',
'published_at' => null,
]);
Artisan::call(PublishScheduledPressReleases::class, ['--dry-run' => true]);
expect($due->fresh()->status)->toBe(PressReleaseStatus::Review);
});
test('Command publisht maximal --limit pro Lauf', function () {
/** @var TestCase $this */
Carbon::setTestNow('2026-06-01 12:00:00');
PressRelease::factory()->count(3)->state([
'status' => PressReleaseStatus::Review->value,
'classification' => PressReleaseClassification::Green->value,
'scheduled_at' => '2026-06-01 11:50:00',
'published_at' => null,
])->create();
Artisan::call(PublishScheduledPressReleases::class, ['--limit' => 2]);
$publishedCount = PressRelease::withoutGlobalScopes()
->where('status', PressReleaseStatus::Published->value)
->count();
expect($publishedCount)->toBe(2);
});
test('the scheduler reports failure when a due release cannot be published (Audit F20)', function () {
/** @var TestCase $this */
Carbon::setTestNow('2026-06-01 12:00:00');
PressRelease::factory()->create([
'status' => PressReleaseStatus::Review->value,
'classification' => PressReleaseClassification::Green->value,
'scheduled_at' => '2026-06-01 11:55:00',
'published_at' => null,
]);
$this->mock(PressReleaseService::class)
->shouldReceive('publish')
->once()
->andThrow(new RuntimeException('Datenbank nicht erreichbar'));
$this->artisan(PublishScheduledPressReleases::class)->assertFailed();
});
test('the scheduler re-checks a due release whose classification predates the last edit instead of publishing it (Audit F07)', function () {
/** @var TestCase $this */
Carbon::setTestNow('2026-06-01 12:00:00');
Queue::fake();
$stale = PressRelease::factory()->create([
'status' => PressReleaseStatus::Review->value,
'classification' => PressReleaseClassification::Green->value,
'scheduled_at' => '2026-06-01 11:55:00',
'published_at' => null,
]);
KiAudit::factory()->create([
'press_release_id' => $stale->id,
'type' => KiAudit::TYPE_CLASSIFICATION,
'result' => 'green',
'content_hash' => str_repeat('0', 64),
]);
$current = PressRelease::factory()->create([
'status' => PressReleaseStatus::Review->value,
'classification' => PressReleaseClassification::Green->value,
'scheduled_at' => '2026-06-01 11:55:00',
'published_at' => null,
]);
KiAudit::factory()->create([
'press_release_id' => $current->id,
'type' => KiAudit::TYPE_CLASSIFICATION,
'result' => 'green',
'content_hash' => $current->content_hash,
]);
// Ohne Audit (Altbestand vor der Hash-Bindung) wird wie bisher veröffentlicht.
$legacy = PressRelease::factory()->create([
'status' => PressReleaseStatus::Review->value,
'classification' => PressReleaseClassification::Green->value,
'scheduled_at' => '2026-06-01 11:55:00',
'published_at' => null,
]);
Artisan::call(PublishScheduledPressReleases::class);
expect($stale->fresh()->status)->toBe(PressReleaseStatus::Review)
->and($current->fresh()->status)->toBe(PressReleaseStatus::Published)
->and($legacy->fresh()->status)->toBe(PressReleaseStatus::Published);
Queue::assertPushed(ClassifyPressRelease::class, fn (ClassifyPressRelease $job) => $job->pressReleaseId === $stale->id
&& $job->route === true
&& $job->contentHash === $stale->fresh()->content_hash);
});