web: Entfernung mit 410 Gone als Alternative zur Löschung

Rechtlich oder redaktionell veranlasste Entfernungen brauchten bisher die
Löschung — danach antwortet die URL mit 404, als hätte es die Meldung nie
gegeben. Newsroom und Pressemitteilung lassen sich jetzt stattdessen mit
gone_at markieren: Der Inhalt verschwindet aus allen öffentlichen Listen, die
bestehende URL liefert aber einen Tombstone mit Status 410. Die vollständige
Löschung bleibt unverändert daneben stehen und erzeugt weiterhin 404.

Umkehrbar heißt hier wirklich umkehrbar. markGone() überschreibt zwei Felder —
companies.is_active (damit der Newsroom ohne Sonderbehandlung aus
Newsroom-Liste und Sitemap fällt, die beide darauf filtern) und
press_releases.no_export. Beide Vorzustände werden gesichert und beim Aufheben
zurückgestellt, sonst stünde eine schon vorher deaktivierte Firma danach
öffentlich in der Übersicht und eine Mitteilung dauerhaft als "kein Export" da,
ohne dass das je jemand gewählt hätte.

Der Auslieferungs-Cache kennt keine Tags, und die Keys der Listenseiten sind
über Portal, Sprache, Seite und Filter verstreut. Eine Entfernung erhöht
deshalb eine Generations-Nummer im Cache-Key und verwirft damit alle
Seiten-Caches auf einen Schlag — grob, aber Entfernungen sind selten und dürfen
nicht bis zu einer Stunde nachhängen. Der Zähler liegt bewusst außerhalb des
Namensraums, den er invalidiert, und wird zwischen zwei Queue-Jobs neu
gelesen: Ein Worker lebt über beliebig viele Jobs und arbeitete sonst dauerhaft
mit dem Stand seines ersten Zugriffs.

Beim Newsroom läuft die Tombstone-Prüfung bewusst VOR dem Seiten-Cache — eine
Entfernung muss sofort greifen, auch wenn für den Slug noch ein Stale-Eintrag
liegt. Kaskadiert man die Entfernung auf die Meldungen einer Firma, wird der
Cache einmal am Ende verworfen statt einmal je Meldung; sonst zwänge eine
Bestandsfirma mit dreistellig vielen Meldungen die gesamte öffentliche Seite
ebenso oft in den Neuaufbau.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Kevin Adametz 2026-09-01 11:06:35 +00:00
parent d14cb84107
commit 2024df9d28
24 changed files with 1150 additions and 57 deletions

View file

@ -195,7 +195,7 @@ test('booking a category boost invalidates the category page but keeps the homep
// Branchenseite sofort aktualisiert, Startseiten-Cache bleibt stehen
// (der Kategorie-Boost berührt die Startseite nicht).
expect(Cache::has('web.home.presseecho.de'))->toBeTrue();
expect(Cache::has(webPageCacheKey('web.home.presseecho.de')))->toBeTrue();
$this->get('https://presseecho.test/de/category/energie-klima')
->assertSuccessful()
->assertSeeText('Top-Platzierung');

View file

@ -32,9 +32,9 @@ test('category navigation caches per portal, language and limit separately', fun
CategoryNavigation::items(5);
expect(Cache::has('web.category-nav.presseecho.de.5'))->toBeTrue()
->and(Cache::has('web.category-nav.presseecho.de.all'))->toBeFalse();
expect(Cache::has(webPageCacheKey('web.category-nav.presseecho.de.5')))->toBeTrue()
->and(Cache::has(webPageCacheKey('web.category-nav.presseecho.de.all')))->toBeFalse();
CategoryNavigation::items(null);
expect(Cache::has('web.category-nav.presseecho.de.all'))->toBeTrue();
expect(Cache::has(webPageCacheKey('web.category-nav.presseecho.de.all')))->toBeTrue();
});

View file

@ -0,0 +1,359 @@
<?php
use App\Enums\Portal;
use App\Enums\PressReleaseStatus;
use App\Models\Company;
use App\Models\PressRelease;
use App\Models\User;
use App\Services\Company\CompanyRemovalService;
use App\Services\PressRelease\PressReleaseService;
use App\Support\WebPageCache;
use Database\Seeders\RolesAndPermissionsSeeder;
use Livewire\Volt\Volt as LivewireVolt;
use Tests\TestCase;
/**
* Entfernung mit 410 Gone (Newsroom und Pressemitteilung) als Alternative zur
* vollständigen Löschung, die weiterhin 404 liefert.
*/
beforeEach(function (): void {
/** @var TestCase $this */
$this->seed(RolesAndPermissionsSeeder::class);
});
function goneTestCompany(): Company
{
return Company::factory()->create([
'portal' => Portal::Businessportal24,
'is_active' => true,
]);
}
function goneTestRelease(?Company $company = null): PressRelease
{
return PressRelease::factory()
->published()
->forPortal(Portal::Businessportal24)
->create([
'company_id' => $company?->getKey(),
'published_at' => now()->subDay(),
]);
}
function goneTestAdmin(): User
{
$admin = User::factory()->create(['is_active' => true]);
$admin->assignRole('admin');
return $admin;
}
test('a press release marked gone answers 410 with a tombstone instead of 404', function () {
/** @var TestCase $this */
$release = goneTestRelease();
$url = 'https://businessportal24.test/de/press-release/'.$release->slug;
$this->get($url)->assertOk()->assertSeeText($release->title);
app(PressReleaseService::class)->markGone($release);
$this->get($url)
->assertStatus(410)
->assertSeeText('Diese Pressemitteilung wurde entfernt')
->assertDontSeeText($release->title)
// Eigener Wortlaut: keine Depublikation "auf Wunsch des Absenders".
->assertDontSeeText('auf Wunsch des Absenders');
});
test('marking a press release gone archives it, blocks export and locks the re-sync', function () {
/** @var TestCase $this */
$release = goneTestRelease();
app(PressReleaseService::class)->markGone($release);
$release->refresh();
expect($release->gone_at)->not->toBeNull()
->and($release->status)->toBe(PressReleaseStatus::Archived)
->and($release->no_export)->toBeTrue()
// Ohne Re-Sync-Sperre würde der nächste Importer-Lauf die Entfernung
// wieder überschreiben.
->and($release->isSyncLocked())->toBeTrue()
->and($release->deleted_at)->toBeNull()
->and($release->statusLogs()->where('source', 'gone')->count())->toBe(1);
});
test('a fully deleted press release still answers 404', function () {
/** @var TestCase $this */
$release = goneTestRelease();
$url = 'https://businessportal24.test/de/press-release/'.$release->slug;
$release->delete();
$this->get($url)->assertNotFound();
});
test('lifting the gone marker makes the press release publishable again', function () {
/** @var TestCase $this */
$release = goneTestRelease();
$service = app(PressReleaseService::class);
$service->markGone($release);
$service->restoreFromGone($release->fresh());
$release->refresh();
expect($release->gone_at)->toBeNull()
// Der Status bleibt bewusst archiviert — die Freigabe ist ein
// eigener redaktioneller Schritt.
->and($release->status)->toBe(PressReleaseStatus::Archived);
$this->get('https://businessportal24.test/de/press-release/'.$release->slug)->assertNotFound();
});
test('a newsroom marked gone answers 410 with a tombstone instead of 404', function () {
/** @var TestCase $this */
$company = goneTestCompany();
goneTestRelease($company);
$url = 'https://businessportal24.test/de/newsroom/'.$company->slug;
$this->get($url)->assertOk();
app(CompanyRemovalService::class)->markGone($company);
$this->get($url)
->assertStatus(410)
->assertSeeText('Dieser Newsroom wurde entfernt')
->assertDontSeeText($company->name);
});
test('the newsroom tombstone beats a warm page cache', function () {
/** @var TestCase $this */
$company = goneTestCompany();
goneTestRelease($company);
$url = 'https://businessportal24.test/de/newsroom/'.$company->slug;
// Erst aufwärmen, damit für den Slug ein Seiten-Cache existiert …
$this->get($url)->assertOk();
// … dann den Marker OHNE Cache-Invalidierung setzen. Damit prüft der Test
// die Absicherung in NewsroomDetailPageData (Gone-Prüfung vor dem Cache)
// und nicht bloß das Verwerfen der Caches im Service.
$company->forceFill(['gone_at' => now(), 'is_active' => false])->saveQuietly();
$this->get($url)->assertStatus(410);
});
test('removing content drops the cached public listings immediately', function () {
/** @var TestCase $this */
$release = goneTestRelease();
// Startseiten-/Ticker-Caches aufwärmen …
$this->get('https://businessportal24.test/de')->assertOk();
app(PressReleaseService::class)->markGone($release);
// … die entfernte Meldung darf danach in keiner Liste mehr auftauchen,
// auch nicht aus dem Stale-Fenster (bis zu einer Stunde).
$this->get('https://businessportal24.test/de')
->assertOk()
->assertDontSeeText($release->title);
});
test('a fully deleted newsroom still answers 404', function () {
/** @var TestCase $this */
$company = goneTestCompany();
goneTestRelease($company);
$url = 'https://businessportal24.test/de/newsroom/'.$company->slug;
$company->delete();
$this->get($url)->assertNotFound();
});
test('removing a newsroom leaves its press releases alone unless asked to cascade', function () {
/** @var TestCase $this */
$company = goneTestCompany();
$release = goneTestRelease($company);
app(CompanyRemovalService::class)->markGone($company);
expect($release->fresh()->gone_at)->toBeNull();
$this->get('https://businessportal24.test/de/press-release/'.$release->slug)->assertOk();
});
test('removing a newsroom can cascade the 410 to its press releases', function () {
/** @var TestCase $this */
$company = goneTestCompany();
$release = goneTestRelease($company);
$affected = app(CompanyRemovalService::class)->markGone($company, includePressReleases: true);
expect($affected)->toBe(1)
->and($release->fresh()->gone_at)->not->toBeNull();
$this->get('https://businessportal24.test/de/press-release/'.$release->slug)->assertStatus(410);
});
test('cascading a newsroom removal flushes the page cache once, not per release', function () {
/** @var TestCase $this */
$company = goneTestCompany();
goneTestRelease($company);
goneTestRelease($company);
goneTestRelease($company);
$before = WebPageCache::generation();
$affected = app(CompanyRemovalService::class)->markGone($company, includePressReleases: true);
// Ein Flush verwirft ALLE `web.`-Seiten-Caches auf einmal. Einer pro
// Mitteilung wäre ein selbst ausgelöster Cache-Stampede: Eine Bestandsfirma
// mit dreistellig vielen Meldungen zwänge die gesamte öffentliche Seite
// ebenso oft in den Neuaufbau.
expect($affected)->toBe(3)
->and(WebPageCache::generation())->toBe($before + 1);
});
test('lifting a newsroom removal keeps individually removed press releases gone', function () {
/** @var TestCase $this */
$company = goneTestCompany();
$release = goneTestRelease($company);
$removal = app(CompanyRemovalService::class);
$removal->markGone($company, includePressReleases: true);
$removal->restoreFromGone($company->fresh());
$company->refresh();
expect($company->gone_at)->toBeNull()
->and($company->is_active)->toBeTrue()
// Eine einmal entfernte PM kommt nicht automatisch zurück — sonst
// könnte eine rechtlich veranlasste Einzel-Entfernung versehentlich
// mit aufgehoben werden.
->and($release->fresh()->gone_at)->not->toBeNull();
});
test('lifting a newsroom removal leaves an already inactive newsroom inactive', function () {
/** @var TestCase $this */
$company = Company::factory()->create([
'portal' => Portal::Businessportal24,
'is_active' => false,
]);
$removal = app(CompanyRemovalService::class);
$removal->markGone($company);
$removal->restoreFromGone($company->fresh());
$company->refresh();
// Die Oberfläche verspricht Umkehrbarkeit. Eine Firma, die schon vor der
// Entfernung deaktiviert war, darf durch das Aufheben nicht öffentlich
// werden — sie stünde sonst plötzlich in Newsroom-Übersicht und Sitemap.
expect($company->gone_at)->toBeNull()
->and($company->is_active)->toBeFalse()
->and($company->is_active_before_gone)->toBeNull();
});
test('lifting a press release removal restores the export flag it overwrote', function () {
/** @var TestCase $this */
$release = goneTestRelease();
$release->update(['no_export' => false]);
$service = app(PressReleaseService::class);
$service->markGone($release);
expect($release->fresh()->no_export)->toBeTrue();
$service->restoreFromGone($release->fresh());
// `no_export` gehört zur Entfernung, nicht zur Mitteilung — sonst stünde
// sie in Kunden- und Admin-Ansicht dauerhaft als „kein Export“ da, ohne
// dass das je jemand gewählt hätte.
expect($release->fresh()->no_export)->toBeFalse()
->and($release->fresh()->no_export_before_gone)->toBeNull();
});
test('lifting a press release removal keeps an export block that predates it', function () {
/** @var TestCase $this */
$release = goneTestRelease();
$release->update(['no_export' => true]);
$service = app(PressReleaseService::class);
$service->markGone($release);
$service->restoreFromGone($release->fresh());
// Umgekehrt gilt dasselbe: Eine schon vorher gesetzte Sperre bleibt.
expect($release->fresh()->no_export)->toBeTrue();
});
test('a press release marked gone disappears from listings and the sitemap', function () {
/** @var TestCase $this */
$company = goneTestCompany();
$release = goneTestRelease($company);
$this->get('https://businessportal24.test/sitemap.xml')->assertSee($release->slug, false);
app(PressReleaseService::class)->markGone($release);
$this->get('https://businessportal24.test/sitemap.xml')->assertDontSee($release->slug, false);
$this->get('https://businessportal24.test/de/newsrooms')->assertDontSeeText($company->name);
});
test('admin can remove a press release with 410 from the edit page', function () {
/** @var TestCase $this */
$release = goneTestRelease();
$this->actingAs(goneTestAdmin());
LivewireVolt::test('admin.press-releases.edit', ['id' => $release->id])
->call('markGone')
->assertRedirect(route('admin.press-releases.index'));
expect($release->fresh()->gone_at)->not->toBeNull();
});
test('admin can remove a newsroom with 410 from the edit page', function () {
/** @var TestCase $this */
$company = goneTestCompany();
$release = goneTestRelease($company);
$this->actingAs(goneTestAdmin());
LivewireVolt::test('admin.companies.edit', ['id' => $company->id])
->set('goneIncludesPressReleases', true)
->call('markGone')
->assertRedirect(route('admin.companies.index'));
$company->refresh();
expect($company->gone_at)->not->toBeNull()
->and($company->is_active)->toBeFalse()
->and($release->fresh()->gone_at)->not->toBeNull();
});
test('admin can lift a press release removal from the edit page', function () {
/** @var TestCase $this */
$release = goneTestRelease();
app(PressReleaseService::class)->markGone($release);
$this->actingAs(goneTestAdmin());
LivewireVolt::test('admin.press-releases.edit', ['id' => $release->id])
->assertSee('410-Entfernung aufheben')
->call('restoreGone')
->assertRedirect(route('admin.press-releases.edit', $release->id));
expect($release->fresh()->gone_at)->toBeNull();
});
test('admin can lift a newsroom removal from the edit page', function () {
/** @var TestCase $this */
$company = goneTestCompany();
app(CompanyRemovalService::class)->markGone($company);
$this->actingAs(goneTestAdmin());
LivewireVolt::test('admin.companies.edit', ['id' => $company->id])
->assertSee('410-Entfernung aufheben')
->call('restoreGone')
->assertRedirect(route('admin.companies.edit', $company->id));
$company->refresh();
expect($company->gone_at)->toBeNull()
->and($company->is_active)->toBeTrue();
});

View file

@ -41,7 +41,7 @@ test('homepage data is cached per portal and language', function () {
->assertSuccessful()
->assertSeeText('Erste Meldung im Cache');
expect(Cache::has('web.home.presseecho.de'))->toBeTrue();
expect(Cache::has(webPageCacheKey('web.home.presseecho.de')))->toBeTrue();
PressRelease::factory()
->published()
@ -119,23 +119,23 @@ test('the press release feed caches its front pages but not deep pages or junk t
->assertSuccessful()
->assertSeeText('Feed-Meldung eins');
expect(Cache::has('web.feed.presseecho.de.all.page.1'))->toBeTrue();
expect(Cache::has(webPageCacheKey('web.feed.presseecho.de.all.page.1')))->toBeTrue();
// Tiefe Seiten bleiben ungecacht (Schutz vor unbegrenztem Key-Raum).
$this->get('https://presseecho.test/de?page=25')->assertSuccessful();
expect(Cache::has('web.feed.presseecho.de.all.page.25'))->toBeFalse();
expect(Cache::has(webPageCacheKey('web.feed.presseecho.de.all.page.25')))->toBeFalse();
// Unbekannte timeframe-Werte aus der URL fallen auf "all" zurück und
// erzeugen keinen eigenen (poisonbaren) Cache-Key.
$this->get('https://presseecho.test/de?timeframe=boesewert')->assertSuccessful();
expect(Cache::has('web.feed.presseecho.de.boesewert.page.1'))->toBeFalse();
expect(Cache::has(webPageCacheKey('web.feed.presseecho.de.boesewert.page.1')))->toBeFalse();
// Der Cache hält nur items + total — der Paginator (inkl. Host-URLs der
// Pagination-Links) wird pro Request neu gebaut, damit der gecachte Feed
// nicht den Host des Cache-Erzeugers an andere Besucher ausliefert.
expect(Cache::get('web.feed.presseecho.de.all.page.1'))
expect(Cache::get(webPageCacheKey('web.feed.presseecho.de.all.page.1')))
->toBeArray()
->toHaveKeys(['items', 'total']);
});
@ -156,18 +156,18 @@ test('category page data is cached per portal, language and slug but 404s are no
$this->get('https://businessportal24.test/de/category/mobilitaet')->assertSuccessful();
expect(Cache::has('web.category.businessportal24.de.mobilitaet'))->toBeTrue();
expect(Cache::has(webPageCacheKey('web.category.businessportal24.de.mobilitaet')))->toBeTrue();
$this->get('https://businessportal24.test/de/category/gibt-es-nicht')->assertNotFound();
expect(Cache::has('web.category.businessportal24.de.gibt-es-nicht'))->toBeFalse();
expect(Cache::has(webPageCacheKey('web.category.businessportal24.de.gibt-es-nicht')))->toBeFalse();
});
test('search sidebar data is cached per portal and language', function () {
/** @var TestCase $this */
$this->get('https://businessportal24.test/de/suche')->assertSuccessful();
expect(Cache::has('web.search.most-read.businessportal24-both.de'))->toBeTrue();
expect(Cache::has(webPageCacheKey('web.search.most-read.businessportal24-both.de')))->toBeTrue();
});
test('newsroom detail data is cached per portal language slug and page', function () {
@ -188,7 +188,7 @@ test('newsroom detail data is cached per portal language slug and page', functio
$this->get('https://businessportal24.test/de/newsroom/cache-newsroom?page=2')
->assertSuccessful();
expect(Cache::has('web.newsroom.businessportal24-both.de.cache-newsroom.page.2'))->toBeTrue();
expect(Cache::has(webPageCacheKey('web.newsroom.businessportal24-both.de.cache-newsroom.page.2')))->toBeTrue();
});
test('newsroom detail cache key aligns with the paginator so an invalid page cannot poison another page', function () {
@ -211,8 +211,8 @@ test('newsroom detail cache key aligns with the paginator so an invalid page can
$this->get('https://businessportal24.test/de/newsroom/poison-newsroom?page=2.5')
->assertSuccessful();
expect(Cache::has('web.newsroom.businessportal24-both.de.poison-newsroom.page.1'))->toBeTrue();
expect(Cache::has('web.newsroom.businessportal24-both.de.poison-newsroom.page.2'))->toBeFalse();
expect(Cache::has(webPageCacheKey('web.newsroom.businessportal24-both.de.poison-newsroom.page.1')))->toBeTrue();
expect(Cache::has(webPageCacheKey('web.newsroom.businessportal24-both.de.poison-newsroom.page.2')))->toBeFalse();
});
test('newsrooms unfiltered index is cached but filtered searches are not', function () {
@ -232,7 +232,7 @@ test('newsrooms unfiltered index is cached but filtered searches are not', funct
->assertSuccessful()
->assertSeeText('SolarTec Bestand');
expect(Cache::has('web.newsrooms.businessportal24,both.de.page.1'))->toBeTrue();
expect(Cache::has(webPageCacheKey('web.newsrooms.businessportal24,both.de.page.1')))->toBeTrue();
// Gefilterte Suche wird NICHT gecacht (Schutz vor unbegrenztem Key-Raum):
// eine nach dem ersten Aufruf angelegte Firma erscheint sofort.
@ -259,7 +259,7 @@ test('deep newsrooms pages are not cached', function () {
/** @var TestCase $this */
$this->get('https://businessportal24.test/de/newsrooms?page=25')->assertSuccessful();
expect(Cache::has('web.newsrooms.businessportal24,both.de.page.25'))->toBeFalse();
expect(Cache::has(webPageCacheKey('web.newsrooms.businessportal24,both.de.page.25')))->toBeFalse();
});
test('newsrooms cache does not retain unrelated query parameters in pagination links', function () {

View file

@ -3,6 +3,7 @@
use App\Models\Plan;
use App\Models\User;
use App\Services\CurrentPortalContext;
use App\Support\WebPageCache;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
@ -74,3 +75,14 @@ function subscribeUserToPlan(User $user, Plan $plan, string $interval = 'monthly
'quantity' => 1,
]);
}
/**
* Seiten-Cache-Keys tragen seit der Entfernung mit 410 eine Generations-Nummer
* (siehe WebPageCache::key): Ein Flush erhöht sie und verwirft damit alle
* `web.`-Seiten-Caches auf einmal. Die Tests prüfen weiterhin die fachliche
* Key-Struktur, nur eben generationsunabhängig.
*/
function webPageCacheKey(string $key): string
{
return 'web.g'.WebPageCache::generation().'.'.substr($key, strlen('web.'));
}