presseportale/tests/Feature/Web/PageDataCacheTest.php
Kevin Adametz fd8e050cb7 Backend-WIP: Kontakt-Queue, Page-Cache, Validierungs-Feedback, i18n
Gebuendelt, da ueber gemeinsame lang-/Service-Dateien verschraenkt:
- Kontakt-Nachrichten: contact_messages-Tabelle + Model/Enums/Factory,
  Admin-Queue (livewire/admin/contact-messages), ContactController
  persistiert das Anliegen zusaetzlich zur Brand-Mail
- Page-Data-Cache (Services/Web/*PageData) + web-archive-Indizes auf
  press_releases
- Validierungs-Feedback + Company-Portal-Restriction (Feature-Tests)
- i18n-Auslagerung lang/de|en (auth, passwords, validation, static)

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-07-09 15:38:11 +00:00

160 lines
5.5 KiB
PHP

<?php
use App\Enums\Portal;
use App\Models\Category;
use App\Models\Company;
use App\Models\PressRelease;
use Illuminate\Support\Facades\Cache;
use Livewire\Volt\Volt as LivewireVolt;
use Tests\TestCase;
function makeCategoryWithSlug(Portal $portal, string $name, string $slug, ?int $parentId = null): Category
{
$category = Category::factory()->create(['portal' => $portal, 'parent_id' => $parentId]);
$category->translations()->create([
'locale' => 'de',
'name' => $name,
'slug' => $slug,
]);
return $category;
}
test('homepage data is cached per portal and language', function () {
/** @var TestCase $this */
$category = makeCategoryWithSlug(Portal::Presseecho, 'Energie & Klima', 'energie-klima');
$company = Company::factory()->presseecho()->create();
PressRelease::factory()
->published()
->forPortal(Portal::Presseecho)
->for($category)
->for($company)
->create([
'title' => 'Erste Meldung im Cache',
'slug' => 'erste-meldung-im-cache',
'published_at' => now()->subHour(),
]);
$this->get('https://presseecho.test/de')
->assertSuccessful()
->assertSeeText('Erste Meldung im Cache');
expect(Cache::has('web.home.presseecho.de'))->toBeTrue();
PressRelease::factory()
->published()
->forPortal(Portal::Presseecho)
->for($category)
->for($company)
->create([
'title' => 'Neue Meldung nach Cache-Aufbau',
'slug' => 'neue-meldung-nach-cache-aufbau',
'published_at' => now()->subMinutes(5),
]);
// Innerhalb der TTL liefert HomePageData den gecachten Stand: Lead bleibt
// die alte Meldung. (Der Livewire-Feed ist bewusst ungecacht und zeigt
// neue Meldungen sofort.)
$cachedLead = $this->get('https://presseecho.test/de')
->assertSuccessful()
->viewData('leadRelease');
expect($cachedLead->title)->toBe('Erste Meldung im Cache');
Cache::flush();
$freshLead = $this->get('https://presseecho.test/de')
->assertSuccessful()
->viewData('leadRelease');
expect($freshLead->title)->toBe('Neue Meldung nach Cache-Aufbau');
});
test('category page data is cached per portal, language and slug but 404s are not cached', function () {
/** @var TestCase $this */
$category = makeCategoryWithSlug(Portal::Businessportal24, 'Mobilität', 'mobilitaet');
PressRelease::factory()
->published()
->forPortal(Portal::Businessportal24)
->for($category)
->for(Company::factory()->businessportal24())
->create([
'slug' => 'mobilitaets-meldung',
'published_at' => now()->subDay(),
]);
$this->get('https://businessportal24.test/de/category/mobilitaet')->assertSuccessful();
expect(Cache::has('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();
});
test('subcategory counts are aggregated correctly in a single grouped query', function () {
/** @var TestCase $this */
$parent = makeCategoryWithSlug(Portal::Both, 'Wirtschaft', 'wirtschaft');
$child = makeCategoryWithSlug(Portal::Both, 'Handel', 'handel', $parent->id);
$company = Company::factory()->businessportal24()->create();
// Stichtag (referenceNow) = jüngste Meldung: 2 im 7-Tage-Fenster, 1 älter (>14 Tage).
foreach ([1, 2, 20] as $daysAgo) {
PressRelease::factory()
->published()
->forPortal(Portal::Businessportal24)
->for($child)
->for($company)
->create([
'slug' => "handel-meldung-{$daysAgo}",
'published_at' => now()->subDays($daysAgo),
]);
}
$response = $this->get('https://businessportal24.test/de/category/wirtschaft')->assertSuccessful();
$subCategories = $response->viewData('subCategories');
expect($subCategories)->toHaveCount(1)
->and($subCategories[0]['name'])->toBe('Handel')
->and($subCategories[0]['total'])->toBe(3)
->and($subCategories[0]['recent'])->toBe(2)
->and($subCategories[0]['delta'])->toBe(2);
});
test('the press release feed today filter matches the reference day via range comparison', function () {
/** @var TestCase $this */
$category = makeCategoryWithSlug(Portal::Presseecho, 'Technologie', 'technologie');
$company = Company::factory()->presseecho()->create();
// Historisches Archiv: Stichtag = jüngste Meldung (vor 10 Tagen).
PressRelease::factory()
->published()
->forPortal(Portal::Presseecho)
->for($category)
->for($company)
->create([
'title' => 'Meldung am Stichtag',
'slug' => 'meldung-am-stichtag',
'published_at' => now()->subDays(10)->setTime(9, 0),
]);
PressRelease::factory()
->published()
->forPortal(Portal::Presseecho)
->for($category)
->for($company)
->create([
'title' => 'Meldung vom Vortag',
'slug' => 'meldung-vom-vortag',
'published_at' => now()->subDays(11)->setTime(9, 0),
]);
LivewireVolt::test('web.press-release-feed', ['portal' => Portal::Presseecho->value, 'language' => 'de'])
->call('setTimeframe', 'today')
->assertSee('Meldung am Stichtag')
->assertDontSee('Meldung vom Vortag');
});