presseportale/tests/Feature/Legal/CorrectionServiceTest.php
Kevin Adametz e6eca9c85f
Some checks failed
linter / quality (push) Has been cancelled
tests / ci (push) Has been cancelled
recht: Meldeverfahren v2 Phasen 5+7 — DSGVO-Strukturformular, 10-€-Checkout mit KI-Vorprüfung, Triage & Panel-Modal
Phase 7 (Spur D, E2 revidiert): KI-Vorprüfung VOR der Zahlung im Modal —
rot lehnt ohne Zahlung ab, gelb/grün führt zum Stripe-Checkout
(SinglePurchaseType::Change, 10 € netto), nach Zahlungseingang übernimmt
der Webhook die geprüfte Fassung sofort (applyPaid: Snapshot, Sperre,
Verlauf). Nur einmal je PM; Obergrenze legal.change_max_distance_percent
(50 %) verhindert die verdeckte Selbst-Depublizierung.

Phase 5 (Spur B): strukturiertes Schwärzungsformular mit Pflicht-Vorschau
im verifizierten Web-Bereich UND als Modal im Kunden-Panel; Admin-Editor
wird aus payload.redaction vorbefüllt. Umsetzungsmail LegalRequestExecuted
an Antragsteller, Bestätigungsmail PressReleaseChangeApplied (Spur A/D,
neuer abbestellbarer Typ press_release_corrected).

Außerdem:
- Re-Sync-Sperre + Sitemap-Invalidierung fest in depublish() verankert
  (Lücke im bezahlten Webhook-Pfad geschlossen)
- Öffentliche „Rechtliches"-Seite als zweistufige Triage: primär der
  Portal-Anmeldelink, Formular nur Fallback mit Identifizierungs-Hinweis;
  Store auf report/dsgvo/personal_rights gehärtet
- Panel-Button „Ändern/Melden" öffnet Anliegen-Modal (4 Spuren) statt
  des Web-Formulars
- Pressekontakt-Fallback für Legacy-Bestände (30k PMs ohne Verknüpfung):
  Detailseite + pressContactEmail() nutzen PM-Kontakt → Firmen-Kontakt →
  Firmendaten
- Korrektur-Gate entity-fest (Editor-Normalisierung zählt nicht als
  Änderung), toEditorHtml() in Admin-Edit + Korrektur-Panel,
  Wochenlimit-Hinweis in Text und Bremsen-Meldung

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-23 13:02:57 +00:00

261 lines
10 KiB
PHP

<?php
use App\Enums\ActivityType;
use App\Enums\LegalRequestStatus;
use App\Enums\LegalRequestType;
use App\Enums\LegalRequestVerificationMethod;
use App\Enums\PressReleaseClassification;
use App\Models\ActivityEvent;
use App\Models\CreditTransaction;
use App\Models\LegalRequest;
use App\Models\PressRelease;
use App\Services\PressRelease\BlacklistService;
use App\Services\PressRelease\CorrectionRateLimitException;
use App\Services\PressRelease\CorrectionService;
use App\Services\PressRelease\CorrectionTooLargeException;
use Illuminate\Support\Facades\Queue;
use Tests\TestCase;
test('a typo fix in a short title classifies as minor', function () {
/** @var TestCase $this */
$pressRelease = PressRelease::factory()->published()->create([
'title' => 'Neue Photovoltaikanlage in Betrieb genomen',
]);
$classification = app(CorrectionService::class)->classify($pressRelease, [
'title' => 'Neue Photovoltaikanlage in Betrieb genommen',
]);
expect($classification['is_minor'])->toBeTrue()
->and($classification['fields']['title']['distance_percent'])->toBeLessThan(15);
});
test('a rewritten text classifies as major', function () {
/** @var TestCase $this */
$pressRelease = PressRelease::factory()->published()->create([
'text' => '<p>Die Firma eröffnet einen neuen Standort in Hamburg und schafft zwanzig Arbeitsplätze.</p>',
]);
$classification = app(CorrectionService::class)->classify($pressRelease, [
'text' => '<p>Völlig neuer Inhalt über ein ganz anderes Produkt mit komplett anderem Schwerpunkt und neuen Aussagen zur Marktlage.</p>',
]);
expect($classification['is_minor'])->toBeFalse()
->and($classification['fields']['text']['changed'])->toBeTrue();
});
test('markup-only changes count as no change', function () {
/** @var TestCase $this */
$pressRelease = PressRelease::factory()->published()->create([
'text' => '<p>Gleicher Inhalt in einem Absatz.</p>',
]);
$classification = app(CorrectionService::class)->classify($pressRelease, [
'text' => '<div>Gleicher Inhalt in einem Absatz.</div>',
]);
expect($classification['has_changes'])->toBeFalse();
});
test('editor normalization of entities and line breaks counts as no change', function () {
/** @var TestCase $this */
$pressRelease = PressRelease::factory()->published()->create([
'text' => '<p>Preis &amp; Leistung&nbsp;stimmen.<br />Zeile zwei mit &quot;Zitat&quot;.</p>',
]);
// So liefert der Rich-Text-Editor denselben Inhalt zurück: Entities
// dekodiert, <br> als eigener Absatz.
$classification = app(CorrectionService::class)->classify($pressRelease, [
'text' => "<p>Preis & Leistung\u{00A0}stimmen.</p><p>Zeile zwei mit \"Zitat\".</p>",
]);
expect($classification['has_changes'])->toBeFalse();
});
test('one added letter in an entity-laden text stays near zero distance', function () {
/** @var TestCase $this */
$words = implode(' ', array_fill(0, 60, 'Wort&nbsp;und &amp; mehr'));
$pressRelease = PressRelease::factory()->published()->create([
'text' => '<p>'.$words.' Betrieb genomen.</p>',
]);
$decoded = implode(' ', array_fill(0, 60, "Wort\u{00A0}und & mehr"));
$classification = app(CorrectionService::class)->classify($pressRelease, [
'text' => '<p>'.$decoded.' Betrieb genommen.</p>',
]);
expect($classification['is_minor'])->toBeTrue()
->and($classification['fields']['text']['distance_percent'])->toBeLessThan(1.0);
});
test('apply saves a minor correction immediately with snapshot, lock and log', function () {
/** @var TestCase $this */
Queue::fake();
$pressRelease = PressRelease::factory()->published()->create([
'title' => 'Betrieb genomen',
]);
$legalRequest = app(CorrectionService::class)->apply(
$pressRelease,
['title' => 'Betrieb genommen'],
'kunde@firma.example',
LegalRequestVerificationMethod::LoggedIn,
);
$pressRelease->refresh();
expect($pressRelease->title)->toBe('Betrieb genommen')
->and($pressRelease->isSyncLocked())->toBeTrue()
->and($legalRequest->type)->toBe(LegalRequestType::Correction)
->and($legalRequest->status)->toBe(LegalRequestStatus::Resolved)
->and($legalRequest->executed_at)->not->toBeNull()
->and($legalRequest->redactionSnapshots()->count())->toBe(1)
->and($legalRequest->redactionSnapshots()->first()->encrypted_before['fields']['title'])->toBe('Betrieb genomen');
ActivityEvent::query()->where('type', ActivityType::Correction->value)->sole();
});
test('apply refuses a major change', function () {
/** @var TestCase $this */
$pressRelease = PressRelease::factory()->published()->create([
'title' => 'Kurzer Titel der Mitteilung',
]);
app(CorrectionService::class)->apply(
$pressRelease,
['title' => 'Ein komplett anderer, deutlich längerer und inhaltlich neuer Titel'],
'kunde@firma.example',
LegalRequestVerificationMethod::LoggedIn,
);
})->throws(CorrectionTooLargeException::class);
test('the weekly correction brake blocks the third correction', function () {
/** @var TestCase $this */
Queue::fake();
config()->set('legal.corrections_per_week', 2);
$pressRelease = PressRelease::factory()->published()->create(['title' => 'Titel A1']);
$service = app(CorrectionService::class);
$service->apply($pressRelease->refresh(), ['title' => 'Titel A2'], 'k@f.example', LegalRequestVerificationMethod::LoggedIn);
$service->apply($pressRelease->refresh(), ['title' => 'Titel A3'], 'k@f.example', LegalRequestVerificationMethod::LoggedIn);
$service->apply($pressRelease->refresh(), ['title' => 'Titel A4'], 'k@f.example', LegalRequestVerificationMethod::LoggedIn);
})->throws(CorrectionRateLimitException::class);
test('classify flags changes above the paid distance limit', function () {
/** @var TestCase $this */
$pressRelease = PressRelease::factory()->published()->create([
'title' => 'Kurzer Titel der Mitteilung',
]);
$classification = app(CorrectionService::class)->classify($pressRelease, [
'title' => 'Ein voellig anderer, deutlich laengerer und inhaltlich komplett neuer Titel',
]);
expect($classification['exceeds_paid_limit'])->toBeTrue()
->and($classification['is_minor'])->toBeFalse();
});
test('applyPaid executes a paid change with snapshot, lock and resolved request', function () {
/** @var TestCase $this */
Queue::fake();
$pressRelease = PressRelease::factory()->published()->create([
'title' => 'Solaranlage in Hamburg eroeffnet und in Betrieb',
]);
$legalRequest = LegalRequest::factory()
->type(LegalRequestType::Change)
->create([
'press_release_id' => $pressRelease->id,
'payload' => ['new_values' => ['title' => 'Solaranlage in Muenchen eroeffnet und in Vollbetrieb']],
]);
$applied = app(CorrectionService::class)->applyPaid($pressRelease, $legalRequest);
$pressRelease->refresh();
$legalRequest->refresh();
expect($applied)->toBeTrue()
->and($pressRelease->title)->toBe('Solaranlage in Muenchen eroeffnet und in Vollbetrieb')
->and($pressRelease->isSyncLocked())->toBeTrue()
->and($legalRequest->status)->toBe(LegalRequestStatus::Resolved)
->and($legalRequest->executed_at)->not->toBeNull()
->and($legalRequest->redactionSnapshots()->count())->toBe(1);
});
test('applyPaid refuses a change above the paid distance limit', function () {
/** @var TestCase $this */
$pressRelease = PressRelease::factory()->published()->create([
'title' => 'Kurzer Titel der Mitteilung',
]);
$legalRequest = LegalRequest::factory()
->type(LegalRequestType::Change)
->create([
'press_release_id' => $pressRelease->id,
'payload' => ['new_values' => ['title' => 'Ein voellig anderer, deutlich laengerer und inhaltlich komplett neuer Titel']],
]);
app(CorrectionService::class)->applyPaid($pressRelease, $legalRequest);
})->throws(CorrectionTooLargeException::class);
test('the precheck classifies the proposed content instead of the stored one', function () {
/** @var TestCase $this */
config()->set('scoring.classification.provider', 'deterministic');
config()->set('blacklist.words', ['skandalwort']);
app()->forgetInstance(BlacklistService::class);
$pressRelease = PressRelease::factory()->published()->create([
'title' => 'Harmloser Titel der Mitteilung',
]);
$service = app(CorrectionService::class);
$red = $service->precheckProposedContent($pressRelease, ['title' => 'Titel mit Skandalwort darin']);
$green = $service->precheckProposedContent($pressRelease, ['title' => 'Harmlose neue Fassung des Titels']);
expect($red->classification)->toBe(PressReleaseClassification::Red)
->and($green->classification)->toBe(PressReleaseClassification::Green)
->and($pressRelease->fresh()->title)->toBe('Harmloser Titel der Mitteilung');
});
test('restoreSnapshot brings back the previous field values and keeps the lock', function () {
/** @var TestCase $this */
Queue::fake();
$pressRelease = PressRelease::factory()->published()->create(['title' => 'Original-Titel hier']);
$service = app(CorrectionService::class);
$legalRequest = $service->apply(
$pressRelease,
['title' => 'Original-Titel hie'],
'kunde@firma.example',
LegalRequestVerificationMethod::LoggedIn,
);
$service->restoreSnapshot($legalRequest->redactionSnapshots()->first());
$pressRelease->refresh();
expect($pressRelease->title)->toBe('Original-Titel hier')
->and($pressRelease->isSyncLocked())->toBeTrue();
});
test('a correction never touches credits', function () {
/** @var TestCase $this */
Queue::fake();
$pressRelease = PressRelease::factory()->published()->create(['title' => 'Titel mit Tippfehlerr']);
app(CorrectionService::class)->apply(
$pressRelease,
['title' => 'Titel mit Tippfehler'],
'kunde@firma.example',
LegalRequestVerificationMethod::LoggedIn,
);
expect(CreditTransaction::query()->count())->toBe(0);
});