presseportale/tests/Feature/Web/PublishedReleaseScopeTest.php
Kevin Adametz 987a5a2c42 Optimierung 4.2: Published-Release-Query ueber PressRelease-Scopes zentralisiert
Vier komponierbare Scopes auf PressRelease:
- published()         Status freigegeben + published_at gesetzt (ohne Zeitschranke,
                      damit Statistik-Fenster frei einschraenken koennen)
- visibleInArchive()  published() + published_at <= jetzt (oeffentlich sichtbar)
- forPortalValues()   whereIn portal
- forLanguage()       where language

~19 duplizierte Query-Bloecke in routes/web.php (Home, Kategorie, Detail,
Veroeffentlichen, Suche, Newsrooms, Newsroom-Detail, Legacy-Redirect) nutzen
jetzt diese Scopes. Jedes withoutGlobalScope(PortalScope) bleibt an seiner
Stelle erhalten; die Klausel-Reihenfolge ist fuers Ergebnis irrelevant (reine
AND-Verknuepfung), daher verhaltensgleich.

Test: Web/PublishedReleaseScopeTest; die 89 bestehenden Web/-Tests bleiben gruen.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-19 11:35:55 +00:00

45 lines
2.3 KiB
PHP

<?php
use App\Enums\Portal;
use App\Enums\PressReleaseStatus;
use App\Models\PressRelease;
test('published includes published releases with a date but excludes drafts, reviews and dateless', function () {
$live = PressRelease::factory()->published()->create(['portal' => Portal::Presseecho->value]);
$future = PressRelease::factory()->published()->create(['portal' => Portal::Presseecho->value, 'published_at' => now()->addDays(3)]);
$draft = PressRelease::factory()->create(['status' => PressReleaseStatus::Draft->value, 'portal' => Portal::Presseecho->value]);
$review = PressRelease::factory()->inReview()->create(['portal' => Portal::Presseecho->value]);
$dateless = PressRelease::factory()->create(['status' => PressReleaseStatus::Published->value, 'published_at' => null, 'portal' => Portal::Presseecho->value]);
$ids = PressRelease::withoutGlobalScopes()->published()->pluck('id');
expect($ids)->toContain($live->id)
->toContain($future->id)
->not->toContain($draft->id)
->not->toContain($review->id)
->not->toContain($dateless->id);
});
test('visibleInArchive excludes future-dated releases', function () {
$live = PressRelease::factory()->published()->create(['portal' => Portal::Presseecho->value]);
$future = PressRelease::factory()->published()->create(['portal' => Portal::Presseecho->value, 'published_at' => now()->addDay()]);
$ids = PressRelease::withoutGlobalScopes()->visibleInArchive()->pluck('id');
expect($ids)->toContain($live->id)->not->toContain($future->id);
});
test('forPortalValues and forLanguage filter as expected', function () {
$match = PressRelease::factory()->published()->create(['portal' => Portal::Presseecho->value, 'language' => 'de']);
$otherPortal = PressRelease::factory()->published()->create(['portal' => Portal::Businessportal24->value, 'language' => 'de']);
$otherLang = PressRelease::factory()->published()->create(['portal' => Portal::Presseecho->value, 'language' => 'en']);
$ids = PressRelease::withoutGlobalScopes()
->forPortalValues([Portal::Presseecho->value, Portal::Both->value])
->forLanguage('de')
->pluck('id');
expect($ids)->toContain($match->id)
->not->toContain($otherPortal->id)
->not->toContain($otherLang->id);
});