recht: Meldeverfahren v2 Phasen 1-4 — Re-Sync-Sperre, Anonymisierung, Magic-Link-Intake, Self-Service-Korrektur

Phase 1: sync_locked_at auf press_releases (Importer überspringt gesperrte
PMs auch bei --force), LegalRequest um Verifikation/Ausführung erweitert,
verschlüsselte Vorher-Snapshots mit Löschfrist (snapshots:purge, täglich).

Phase 2: AnonymizationService (Unicode-Wortgrenzen-Schwärzung, Pflicht-Diff,
transaktionale Ausführung mit Snapshot, Kontakt-Detach, Bild-Löschung,
Sitemap-Invalidierung, Verlauf ohne Klartext-Altwerte); Admin-Queue kann
DSGVO-Anfragen anonymisieren oder depublizieren, inkl. Snapshot-Restore.

Phase 3: Deflektions-Trichter „PM melden/ändern" (melden/pm) mit Kosten-
Tabelle vorab; Magic-Link geht nur an die hinterlegte Pressekontaktadresse
(neuer purpose press_release_report), atomare Verifikation, Anliegen-Auswahl
erzeugt verifizierte Queue-Anfragen; toter-Kanal-Fallback; Owner-Einstieg.

Phase 4: Panel-Abschnitt „Änderung & Korrektur" — Levenshtein-Gate (5 %,
Mini-Edit-Ausnahme ≤ 2 Zeichen) stuft klein/groß ein; kleine Korrektur
kostenlos sofort live (Snapshot, Sperre, Verlauf, Re-Score), größere als
Change-Anfrage (Interim bis 10-€-Checkout, Phase 7).

Entwicklungsplan mit Entscheidungen E1-E8 und offenen Punkten in
docs/Decision-Update; 41 Pest-Tests (tests/Feature/Legal) grün.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
Kevin Adametz 2026-07-22 15:56:34 +00:00
parent 58c0cc3f23
commit b4b2e013d6
42 changed files with 3868 additions and 1 deletions

View file

@ -0,0 +1,165 @@
<?php
use App\Enums\LegalRequestStatus;
use App\Enums\LegalRequestType;
use App\Enums\PressReleaseStatus;
use App\Models\LegalRequest;
use App\Models\PressRelease;
use App\Models\User;
use App\Services\PressRelease\AnonymizationService;
use Database\Seeders\RolesAndPermissionsSeeder;
use Livewire\Volt\Volt;
use Tests\TestCase;
beforeEach(function () {
/** @var TestCase $this */
$this->seed(RolesAndPermissionsSeeder::class);
});
function adminUserForLegalQueue(): User
{
$admin = User::factory()->create(['is_active' => true]);
$admin->assignRole('admin');
return $admin;
}
test('an admin can preview and execute an anonymization from the queue', function () {
/** @var TestCase $this */
$this->actingAs(adminUserForLegalQueue());
$pressRelease = PressRelease::factory()->published()->create([
'title' => 'Matvey Teplitsky im Vorstand',
]);
$request = LegalRequest::factory()
->type(LegalRequestType::Dsgvo)
->create(['press_release_id' => $pressRelease->id]);
Volt::test('admin.legal-requests.show', ['id' => $request->id])
->set('redactionTerms', "Matvey Teplitsky\nTeplitsky")
->set('redactionReplacement', 'der Sprecher')
->call('previewAnonymization')
->assertHasNoErrors()
->call('executeAnonymization')
->assertHasNoErrors();
$pressRelease->refresh();
$request->refresh();
expect($pressRelease->title)->toBe('der Sprecher im Vorstand')
->and($pressRelease->isSyncLocked())->toBeTrue()
->and($request->status)->toBe(LegalRequestStatus::Resolved)
->and($request->executed_at)->not->toBeNull();
});
test('the panel prefills the replacement and shows the full content for copying terms', function () {
/** @var TestCase $this */
$this->actingAs(adminUserForLegalQueue());
$pressRelease = PressRelease::factory()->published()->create([
'title' => 'Matvey Teplitsky im Vorstand',
'text' => '<p>Ansprechpartner ist Matvey Teplitsky, Telefon 0123.</p>',
]);
$request = LegalRequest::factory()
->type(LegalRequestType::Dsgvo)
->create(['press_release_id' => $pressRelease->id]);
Volt::test('admin.legal-requests.show', ['id' => $request->id])
->assertSet('redactionReplacement', AnonymizationService::DEFAULT_REPLACEMENT)
->assertSee('Ansprechpartner ist Matvey Teplitsky')
->set('redactionTerms', 'Matvey Teplitsky')
->call('previewAnonymization')
->assertHasNoErrors()
->assertSee('Ansprechpartner ist [entfernt]')
->assertSee('Vorschau nach Schwärzung');
expect($pressRelease->fresh()->title)->toBe('Matvey Teplitsky im Vorstand');
});
test('execute without a prior preview is refused', function () {
/** @var TestCase $this */
$this->actingAs(adminUserForLegalQueue());
$pressRelease = PressRelease::factory()->published()->create(['title' => 'Original']);
$request = LegalRequest::factory()
->type(LegalRequestType::Dsgvo)
->create(['press_release_id' => $pressRelease->id]);
Volt::test('admin.legal-requests.show', ['id' => $request->id])
->set('redactionTerms', 'Original')
->call('executeAnonymization')
->assertHasErrors('redactionTerms');
expect($pressRelease->fresh()->title)->toBe('Original')
->and($request->fresh()->executed_at)->toBeNull();
});
test('an admin can depublish instead of anonymizing', function () {
/** @var TestCase $this */
$this->actingAs(adminUserForLegalQueue());
$pressRelease = PressRelease::factory()->published()->create();
$request = LegalRequest::factory()
->type(LegalRequestType::Dsgvo)
->create(['press_release_id' => $pressRelease->id]);
Volt::test('admin.legal-requests.show', ['id' => $request->id])
->call('depublishRelease')
->assertHasNoErrors();
$pressRelease->refresh();
$request->refresh();
expect($pressRelease->status)->toBe(PressReleaseStatus::Archived)
->and($pressRelease->depublished_at)->not->toBeNull()
->and($pressRelease->isSyncLocked())->toBeTrue()
->and($request->status)->toBe(LegalRequestStatus::Resolved)
->and($request->executed_at)->not->toBeNull();
});
test('an executed anonymization can be rolled back from the snapshot', function () {
/** @var TestCase $this */
$this->actingAs(adminUserForLegalQueue());
$pressRelease = PressRelease::factory()->published()->create([
'title' => 'Matvey Teplitsky im Vorstand',
]);
$request = LegalRequest::factory()
->type(LegalRequestType::Dsgvo)
->create(['press_release_id' => $pressRelease->id]);
Volt::test('admin.legal-requests.show', ['id' => $request->id])
->set('redactionTerms', 'Matvey Teplitsky')
->call('previewAnonymization')
->call('executeAnonymization')
->assertHasNoErrors()
->call('restoreSnapshot')
->assertHasNoErrors();
$pressRelease->refresh();
$request->refresh();
expect($pressRelease->title)->toBe('Matvey Teplitsky im Vorstand')
->and($pressRelease->isSyncLocked())->toBeTrue()
->and($request->status)->toBe(LegalRequestStatus::InProgress)
->and($request->executed_at)->toBeNull();
});
test('anonymization actions are forbidden without the compliance permission', function () {
/** @var TestCase $this */
$customer = User::factory()->create(['is_active' => true]);
$customer->assignRole('customer');
$this->actingAs($customer);
$pressRelease = PressRelease::factory()->published()->create(['title' => 'Original']);
$request = LegalRequest::factory()
->type(LegalRequestType::Dsgvo)
->create(['press_release_id' => $pressRelease->id]);
Volt::test('admin.legal-requests.show', ['id' => $request->id])
->set('redactionTerms', 'Original')
->call('previewAnonymization')
->assertForbidden();
expect($pressRelease->fresh()->title)->toBe('Original');
});

View file

@ -0,0 +1,130 @@
<?php
use App\Enums\ActivityType;
use App\Enums\LegalRequestType;
use App\Models\ActivityEvent;
use App\Models\Contact;
use App\Models\LegalRequest;
use App\Models\PressRelease;
use App\Models\PressReleaseImage;
use App\Models\PressReleaseRedactionSnapshot;
use App\Services\PressRelease\AnonymizationService;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Storage;
use Tests\TestCase;
test('redaction replaces terms only at word boundaries, longest variant first', function () {
/** @var TestCase $this */
$pressRelease = PressRelease::factory()->published()->create([
'title' => 'Max Mustermann übernimmt die Leitung',
'text' => '<p>Max ist maximal motiviert. Herr Mustermann bleibt. Mustermanns Rolle ändert sich.</p>',
]);
app(AnonymizationService::class)->execute(
$pressRelease,
['Max Mustermann', 'Max', 'Mustermann'],
'[entfernt]',
detachContacts: false,
);
$pressRelease->refresh();
expect($pressRelease->title)->toBe('[entfernt] übernimmt die Leitung')
->and($pressRelease->text)->toContain('maximal') // "Max" zerlegt "maximal" nicht
->and($pressRelease->text)->toContain('Mustermanns') // Genitiv nur über explizite Variante
->and($pressRelease->text)->toContain('[entfernt] ist maximal')
->and($pressRelease->text)->toContain('Herr [entfernt] bleibt')
->and($pressRelease->isSyncLocked())->toBeTrue();
});
test('preview reports hits per field without changing the press release', function () {
/** @var TestCase $this */
$pressRelease = PressRelease::factory()->published()->create([
'title' => 'Mariana Gleue informiert',
'text' => '<p>Kontakt: Mariana Gleue, Telefon 0123.</p>',
]);
$preview = app(AnonymizationService::class)->preview($pressRelease, ['Mariana Gleue']);
expect($preview['total_hits'])->toBe(2)
->and($preview['fields']['title']['hits'])->toBe(1)
->and($preview['fields']['text']['after'])->toContain('[entfernt]')
->and($pressRelease->fresh()->title)->toBe('Mariana Gleue informiert')
->and($pressRelease->fresh()->isSyncLocked())->toBeFalse();
});
test('execute snapshots encrypted, detaches contacts and logs without cleartext', function () {
/** @var TestCase $this */
$pressRelease = PressRelease::factory()->published()->create([
'title' => 'Matvey Teplitsky spricht für die Firma',
]);
$contact = Contact::factory()->create();
$pressRelease->contacts()->attach($contact->id);
$legalRequest = LegalRequest::factory()
->type(LegalRequestType::Dsgvo)
->create(['press_release_id' => $pressRelease->id]);
$snapshot = app(AnonymizationService::class)->execute(
$pressRelease,
['Matvey Teplitsky'],
'der Sprecher',
legalRequest: $legalRequest,
);
$pressRelease->refresh();
expect($pressRelease->title)->toBe('der Sprecher spricht für die Firma')
->and($pressRelease->contacts()->count())->toBe(0)
->and($snapshot->legal_request_id)->toBe($legalRequest->id)
->and($snapshot->encrypted_before['fields']['title'])->toContain('Matvey Teplitsky')
->and($snapshot->encrypted_before['contact_ids'])->toBe([$contact->id]);
// Klartext weder im DB-Rohwert des Snapshots noch im Verlaufs-Eintrag.
$raw = DB::table('press_release_redaction_snapshots')->where('id', $snapshot->id)->value('encrypted_before');
expect($raw)->not->toContain('Teplitsky');
$event = ActivityEvent::query()->where('type', ActivityType::Anonymization->value)->sole();
expect($event->description)->not->toContain('Teplitsky')
->and($event->meta['snapshot_id'])->toBe($snapshot->id);
});
test('selected images are soft-deleted and their public files removed', function () {
/** @var TestCase $this */
Storage::fake('public');
Storage::disk('public')->put('pr/foto.jpg', 'bild');
Storage::disk('public')->put('pr/foto-thumb.jpg', 'thumb');
$pressRelease = PressRelease::factory()->published()->create();
$image = PressReleaseImage::query()->create([
'press_release_id' => $pressRelease->id,
'disk' => 'public',
'path' => 'pr/foto.jpg',
'variants' => ['thumb' => 'pr/foto-thumb.jpg'],
'title' => 'Pressefoto mit Klarname',
]);
app(AnonymizationService::class)->execute(
$pressRelease,
[],
detachContacts: false,
removeImageIds: [$image->id],
);
expect(PressReleaseImage::query()->find($image->id))->toBeNull()
->and(PressReleaseImage::withTrashed()->find($image->id))->not->toBeNull();
Storage::disk('public')->assertMissing('pr/foto.jpg');
Storage::disk('public')->assertMissing('pr/foto-thumb.jpg');
});
test('the purge command removes only snapshots past their retention date', function () {
/** @var TestCase $this */
$due = PressReleaseRedactionSnapshot::factory()->dueForPurge()->create();
$kept = PressReleaseRedactionSnapshot::factory()->create();
$this->artisan('snapshots:purge')->assertSuccessful();
expect(PressReleaseRedactionSnapshot::query()->find($due->id))->toBeNull()
->and(PressReleaseRedactionSnapshot::query()->find($kept->id))->not->toBeNull();
});

View file

@ -0,0 +1,149 @@
<?php
use App\Enums\ActivityType;
use App\Enums\LegalRequestStatus;
use App\Enums\LegalRequestType;
use App\Enums\LegalRequestVerificationMethod;
use App\Models\ActivityEvent;
use App\Models\CreditTransaction;
use App\Models\PressRelease;
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('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('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);
});

View file

@ -0,0 +1,95 @@
<?php
use App\Enums\LegalRequestStatus;
use App\Enums\LegalRequestType;
use App\Models\LegalRequest;
use App\Models\PressRelease;
use App\Models\User;
use Database\Seeders\RolesAndPermissionsSeeder;
use Illuminate\Support\Facades\Queue;
use Livewire\Volt\Volt;
use Tests\TestCase;
beforeEach(function () {
/** @var TestCase $this */
$this->seed(RolesAndPermissionsSeeder::class);
});
function correctionPanelOwner(): User
{
$owner = User::factory()->create(['is_active' => true]);
$owner->assignRole('customer');
return $owner;
}
test('an owner can apply a minor correction from the panel and it goes live instantly', function () {
/** @var TestCase $this */
Queue::fake();
$owner = correctionPanelOwner();
$this->actingAs($owner);
$pressRelease = PressRelease::factory()->published()->create([
'user_id' => $owner->id,
'title' => 'Betrieb genomen',
]);
Volt::test('customer.press-releases.show', ['id' => $pressRelease->id])
->call('startCorrection')
->set('correctionValues.title', 'Betrieb genommen')
->call('previewCorrection')
->assertHasNoErrors()
->call('applyCorrection')
->assertHasNoErrors();
$pressRelease->refresh();
expect($pressRelease->title)->toBe('Betrieb genommen')
->and($pressRelease->isSyncLocked())->toBeTrue()
->and(LegalRequest::query()->where('type', LegalRequestType::Correction->value)->count())->toBe(1);
});
test('a major change from the panel becomes an open paid change request instead', function () {
/** @var TestCase $this */
$owner = correctionPanelOwner();
$this->actingAs($owner);
$pressRelease = PressRelease::factory()->published()->create([
'user_id' => $owner->id,
'title' => 'Kurzer Titel der Mitteilung',
]);
Volt::test('customer.press-releases.show', ['id' => $pressRelease->id])
->call('startCorrection')
->set('correctionValues.title', 'Ein komplett anderer, deutlich längerer und inhaltlich neuer Titel')
->call('previewCorrection')
->assertHasNoErrors()
->call('submitPaidChangeRequest')
->assertHasNoErrors();
$request = LegalRequest::query()->where('type', LegalRequestType::Change->value)->sole();
expect($request->status)->toBe(LegalRequestStatus::Open)
->and($request->payload['new_values']['title'])->toContain('komplett anderer')
->and($pressRelease->fresh()->title)->toBe('Kurzer Titel der Mitteilung');
});
test('apply without preview is refused', function () {
/** @var TestCase $this */
$owner = correctionPanelOwner();
$this->actingAs($owner);
$pressRelease = PressRelease::factory()->published()->create([
'user_id' => $owner->id,
'title' => 'Original',
]);
Volt::test('customer.press-releases.show', ['id' => $pressRelease->id])
->call('startCorrection')
->set('correctionValues.title', 'Origina')
->call('applyCorrection')
->assertHasErrors('correction');
expect($pressRelease->fresh()->title)->toBe('Original');
});

View file

@ -0,0 +1,190 @@
<?php
use App\Enums\LegalRequestStatus;
use App\Enums\LegalRequestType;
use App\Enums\LegalRequestVerificationMethod;
use App\Mail\LegalRequestReceived;
use App\Mail\PressReleaseReportLink;
use App\Models\Contact;
use App\Models\LegalRequest;
use App\Models\MagicLink;
use App\Models\PressRelease;
use App\Models\User;
use App\Services\Auth\MagicLinkGenerator;
use Database\Seeders\RolesAndPermissionsSeeder;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Facades\RateLimiter;
use Tests\TestCase;
beforeEach(function () {
RateLimiter::clear('pm-report|127.0.0.1');
});
function publishedReleaseWithContact(string $contactEmail = 'presse@firma.example'): PressRelease
{
$pressRelease = PressRelease::factory()->published()->create();
$contact = Contact::factory()->create(['email' => $contactEmail]);
$pressRelease->contacts()->attach($contact->id);
return $pressRelease;
}
function detailUrlFor(PressRelease $pressRelease): string
{
return 'https://presseecho.test/de/press-release/'.$pressRelease->slug;
}
test('the intake sends a magic link to the stored press contact, never to a form address', function () {
/** @var TestCase $this */
Mail::fake();
$pressRelease = publishedReleaseWithContact('presse@firma.example');
$response = $this->post('/de/melden/pm', [
'reported_url' => detailUrlFor($pressRelease),
]);
$response->assertRedirect();
$response->assertSessionHas('pm-report-status', fn (string $status) => str_contains($status, 'p***@firma.example'));
Mail::assertQueued(PressReleaseReportLink::class, fn (PressReleaseReportLink $mail) => $mail->hasTo('presse@firma.example'));
$link = MagicLink::query()->where('purpose', 'press_release_report')->sole();
expect($link->payload['press_release_id'])->toBe($pressRelease->id)
->and($link->payload['contact_email'])->toBe('presse@firma.example');
});
test('an unknown url is rejected with a validation error', function () {
/** @var TestCase $this */
Mail::fake();
$this->post('/de/melden/pm', ['reported_url' => 'https://presseecho.test/de/press-release/gibt-es-nicht'])
->assertSessionHasErrors('reported_url');
Mail::assertNothingQueued();
});
test('a release without any contact email points to the manual fallback', function () {
/** @var TestCase $this */
Mail::fake();
$pressRelease = PressRelease::factory()->published()->create();
$pressRelease->company()->withoutGlobalScopes()->update(['email' => null]);
$this->post('/de/melden/pm', ['reported_url' => detailUrlFor($pressRelease)])
->assertSessionHas('pm-report-manual', true);
Mail::assertNothingQueued();
});
test('link requests per release are rate limited', function () {
/** @var TestCase $this */
Mail::fake();
config()->set('legal.report_links_per_day', 1);
$pressRelease = publishedReleaseWithContact();
$this->post('/de/melden/pm', ['reported_url' => detailUrlFor($pressRelease)])
->assertSessionHas('pm-report-status');
$this->post('/de/melden/pm', ['reported_url' => detailUrlFor($pressRelease)])
->assertSessionHasErrors('reported_url');
Mail::assertQueuedCount(1);
});
test('the magic link verifies once and opens the concern selection', function () {
/** @var TestCase $this */
$pressRelease = publishedReleaseWithContact();
$link = app(MagicLinkGenerator::class)->createReportLink($pressRelease, 'presse@firma.example');
$this->get('/de/melden/pm/verifizieren/'.$link['plain_token'])
->assertRedirect('/de/melden/pm/anliegen');
$this->get('/de/melden/pm/anliegen')
->assertOk()
->assertSee($pressRelease->title)
->assertSee('p***@firma.example');
// Zweiter Klick: Link ist verbraucht.
$this->get('/de/melden/pm/verifizieren/'.$link['plain_token'])
->assertRedirect('/de/melden/pm')
->assertSessionHas('pm-report-error');
});
test('a verified visitor can submit a concern which lands verified in the queue', function () {
/** @var TestCase $this */
Mail::fake();
$pressRelease = publishedReleaseWithContact();
$link = app(MagicLinkGenerator::class)->createReportLink($pressRelease, 'presse@firma.example');
$this->get('/de/melden/pm/verifizieren/'.$link['plain_token']);
$this->post('/de/melden/pm/anliegen', [
'concern' => 'dsgvo',
'message' => 'Bitte meinen Namen aus dem Pressekontakt entfernen.',
])->assertSessionHas('pm-report-status');
$request = LegalRequest::query()->sole();
expect($request->type)->toBe(LegalRequestType::Dsgvo)
->and($request->press_release_id)->toBe($pressRelease->id)
->and($request->requester_email)->toBe('presse@firma.example')
->and($request->verified_at)->not->toBeNull()
->and($request->verification_method)->toBe(LegalRequestVerificationMethod::MagicLink)
->and($request->status)->toBe(LegalRequestStatus::Open);
Mail::assertQueued(LegalRequestReceived::class, fn (LegalRequestReceived $mail) => $mail->hasTo('presse@firma.example'));
});
test('a second open concern of the same type is blocked', function () {
/** @var TestCase $this */
Mail::fake();
$pressRelease = publishedReleaseWithContact();
$link = app(MagicLinkGenerator::class)->createReportLink($pressRelease, 'presse@firma.example');
$this->get('/de/melden/pm/verifizieren/'.$link['plain_token']);
LegalRequest::factory()->type(LegalRequestType::Correction)->create([
'press_release_id' => $pressRelease->id,
'status' => LegalRequestStatus::Open,
]);
$this->post('/de/melden/pm/anliegen', [
'concern' => 'correction',
'message' => 'Bitte Tippfehler im Titel korrigieren.',
])->assertSessionHasErrors('concern');
});
test('the concern page requires a verified session', function () {
/** @var TestCase $this */
$this->get('/de/melden/pm/anliegen')
->assertRedirect('/de/melden/pm')
->assertSessionHas('pm-report-error');
});
test('a logged-in owner skips verification and lands in the concern selection', function () {
/** @var TestCase $this */
$this->seed(RolesAndPermissionsSeeder::class);
$owner = User::factory()->create(['is_active' => true]);
$owner->assignRole('customer');
$pressRelease = PressRelease::factory()->published()->create(['user_id' => $owner->id]);
$this->actingAs($owner)
->post('/admin/me/press-releases/'.$pressRelease->id.'/melden')
->assertRedirect('/de/melden/pm/anliegen');
$this->get('/de/melden/pm/anliegen')->assertOk()->assertSee($pressRelease->title);
});
test('a foreign user cannot start the report flow for someone elses release', function () {
/** @var TestCase $this */
$this->seed(RolesAndPermissionsSeeder::class);
$stranger = User::factory()->create(['is_active' => true]);
$stranger->assignRole('customer');
$pressRelease = PressRelease::factory()->published()->create();
$this->actingAs($stranger)
->post('/admin/me/press-releases/'.$pressRelease->id.'/melden')
->assertForbidden();
});

View file

@ -0,0 +1,200 @@
<?php
use App\Enums\ActivityType;
use App\Models\ActivityEvent;
use App\Models\Category;
use App\Models\LegacyImportMap;
use App\Models\PressRelease;
use App\Models\PressReleaseRedactionSnapshot;
use App\Models\User;
use App\Services\Import\ImportContext;
use App\Services\Import\PressReleaseImporter;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
use Tests\TestCase;
/**
* Minimales Legacy-Schema + Import-Map (Kategorie/User), damit der Importer
* eine PM auflösen kann — analog LegacyPressReleaseDateImportTest, eigener
* Helper wegen globalem Pest-Funktionsnamensraum.
*/
function seedSyncLockLegacySchema(): void
{
Config::set('database.connections.mysql_presseecho', [
'driver' => 'sqlite',
'database' => ':memory:',
'prefix' => '',
'foreign_key_constraints' => false,
]);
DB::purge('mysql_presseecho');
Schema::connection('mysql_presseecho')->create('press_release', function (Blueprint $table): void {
$table->integer('id')->primary();
$table->string('title')->nullable();
$table->string('language')->nullable();
$table->text('text')->nullable();
$table->string('backlink_url')->nullable();
$table->integer('category_id')->nullable();
$table->string('keywords')->nullable();
$table->integer('user_id')->nullable();
$table->integer('company_id')->nullable();
$table->string('status')->nullable();
$table->integer('hits')->nullable();
$table->integer('teaser_begin')->nullable();
$table->integer('teaser_end')->nullable();
$table->boolean('no_export')->nullable();
$table->dateTime('created_at')->nullable();
$table->dateTime('updated_at')->nullable();
$table->string('slug')->nullable();
});
Schema::connection('mysql_presseecho')->create('press_release_image', function (Blueprint $table): void {
$table->integer('id')->primary();
$table->integer('press_release_id');
});
Schema::connection('mysql_presseecho')->create('press_release_contact', function (Blueprint $table): void {
$table->integer('press_release_id');
$table->integer('contact_id');
});
$category = Category::factory()->withTranslations()->create();
LegacyImportMap::query()->create([
'legacy_portal' => 'presseecho',
'legacy_table' => 'category',
'legacy_id' => 500,
'target_table' => 'categories',
'target_id' => $category->id,
]);
$author = User::factory()->create();
LegacyImportMap::query()->create([
'legacy_portal' => 'presseecho',
'legacy_table' => 'sf_guard_user',
'legacy_id' => 700,
'target_table' => 'users',
'target_id' => $author->id,
]);
}
test('lockSync stamps the press release, logs the event and is idempotent', function () {
/** @var TestCase $this */
$pressRelease = PressRelease::factory()->published()->create();
$pressRelease->lockSync('DSGVO-Anonymisierung M-1');
expect($pressRelease->isSyncLocked())->toBeTrue()
->and($pressRelease->sync_locked_at)->not->toBeNull()
->and($pressRelease->sync_lock_reason)->toBe('DSGVO-Anonymisierung M-1');
$firstLockedAt = $pressRelease->sync_locked_at;
$this->travel(1)->hours();
$pressRelease->lockSync('Zweiter Versuch');
$this->travelBack();
$pressRelease->refresh();
expect($pressRelease->sync_locked_at->equalTo($firstLockedAt))->toBeTrue()
->and($pressRelease->sync_lock_reason)->toBe('DSGVO-Anonymisierung M-1');
$event = ActivityEvent::query()->where('type', ActivityType::SyncLock->value)->sole();
expect($event->subject_id)->toBe($pressRelease->id)
->and($event->description)->toContain('DSGVO-Anonymisierung M-1');
});
test('the importer skips a sync-locked press release even with force', function () {
/** @var TestCase $this */
seedSyncLockLegacySchema();
DB::connection('mysql_presseecho')->table('press_release')->insert([
'id' => 42,
'title' => 'Originaltitel mit Klarname',
'language' => 'de',
'text' => 'Text mit Klarname',
'category_id' => 500,
'user_id' => 700,
'status' => 'published',
'no_export' => false,
'created_at' => '2019-05-01 08:00:00',
'updated_at' => '2019-05-01 08:00:00',
'slug' => 'original',
]);
// Erstimport, dann Mutation über das Meldeverfahren (Schwärzung) + Sperre.
app(PressReleaseImporter::class)->run(new ImportContext('presseecho', false, true));
$pressRelease = PressRelease::withoutGlobalScopes()
->where('legacy_portal', 'presseecho')->where('legacy_id', 42)->firstOrFail();
$pressRelease->forceFill(['title' => 'Geschwärzter Titel'])->save();
$pressRelease->lockSync('DSGVO-Anonymisierung');
// Force-Re-Import darf die Schwärzung nicht überschreiben.
$result = app(PressReleaseImporter::class)->run(new ImportContext('presseecho', false, true));
$pressRelease->refresh();
expect($pressRelease->title)->toBe('Geschwärzter Titel')
->and($result->skipped())->toBe(1)
->and($result->updated())->toBe(0);
});
test('an unlocked press release is still updated by a force re-import', function () {
/** @var TestCase $this */
seedSyncLockLegacySchema();
DB::connection('mysql_presseecho')->table('press_release')->insert([
'id' => 43,
'title' => 'Unveränderte Meldung',
'language' => 'de',
'text' => 'Inhalt',
'category_id' => 500,
'user_id' => 700,
'status' => 'published',
'no_export' => false,
'created_at' => '2019-05-01 08:00:00',
'updated_at' => '2019-05-01 08:00:00',
'slug' => 'unveraendert',
]);
app(PressReleaseImporter::class)->run(new ImportContext('presseecho', false, true));
$result = app(PressReleaseImporter::class)->run(new ImportContext('presseecho', false, true));
expect($result->updated())->toBe(1)
->and($result->skipped())->toBe(0);
});
test('a redaction snapshot encrypts the previous values and gets a purge date from config', function () {
/** @var TestCase $this */
config()->set('legal.snapshot_retention_days', 30);
$pressRelease = PressRelease::factory()->published()->create();
$snapshot = PressReleaseRedactionSnapshot::capture($pressRelease, [
'title' => 'Klarname Max Mustermann',
]);
// Klartext darf nie unverschlüsselt in der Datenbank liegen.
$raw = DB::table('press_release_redaction_snapshots')
->where('id', $snapshot->id)
->value('encrypted_before');
expect($raw)->not->toContain('Max Mustermann')
->and($snapshot->encrypted_before['title'])->toBe('Klarname Max Mustermann')
->and($snapshot->purge_after->toDateString())->toBe(now()->addDays(30)->toDateString());
});
test('the due-for-purge scope only matches snapshots past their purge date', function () {
/** @var TestCase $this */
$due = PressReleaseRedactionSnapshot::factory()->dueForPurge()->create();
PressReleaseRedactionSnapshot::factory()->create();
expect(PressReleaseRedactionSnapshot::query()->dueForPurge()->pluck('id')->all())
->toBe([$due->id]);
});