presseportale/tests/Feature/VisibilityStatsTest.php
Kevin Adametz 3ef1b84600 customer: Sichtbarkeit belegen - Aufrufe im Verlauf, Newsroom-Uebersicht (Roadmap 1c)
- VisibilityStats: Firmen-Kennzahlen ueber alle Meldungen, Aufrufe je
  Meldung (30 Tage), lueckenlose Tagesreihe (Tage vor der Zaehlung sind
  null, nicht 0), Sitemap-seit und Google-Suchlink (Indexierung Variante B).
- Firmenseite: alle Meldungen der Firma statt nur eigene (Beansprucher
  sahen eine leere Liste), Aufrufe-Spalten, echtes Statistik-Panel.
- Meldungsseite: Panel Sichtbarkeit mit SVG-Tagesbalken 30/90 Tage.
- Claim aus der Veroeffentlichungsmail landet auf der Meldung
  (payload press_release_id), Mail-Hinweis erweitert, ein Element (E4).

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-09-16 11:53:09 +00:00

158 lines
7.5 KiB
PHP

<?php
use App\Enums\Portal;
use App\Enums\PressReleaseEventType;
use App\Models\Company;
use App\Models\PressRelease;
use App\Models\PressReleaseEvent;
use App\Models\User;
use App\Services\PressRelease\VisibilityStats;
use Livewire\Volt\Volt as LivewireVolt;
use Tests\TestCase;
/**
* Sichtbarkeit belegen (Roadmap 1c): Aufrufe je Firma und je Meldung als
* Rohzahlen (E2), sichtbar für jeden, der die Firma verwalten darf — auch
* für Meldungen, die ein Partner eingereicht hat.
*/
function claimant(): array
{
$customer = User::factory()->create(['is_active' => true]);
$company = Company::factory()->businessportal24()->create(['name' => 'Muster GmbH']);
$customer->companies()->attach($company->id, ['role' => 'member']);
test()->actingAs($customer);
return [$customer, $company];
}
function viewsOn(PressRelease $release, int $count, int $daysAgo = 0): void
{
// created_at ist nicht fillable — wie im Produktivpfad setzt es das
// Model; für die Zeitreihe wird es hier nachträglich verschoben.
foreach (range(1, $count) as $i) {
$event = PressReleaseEvent::query()->create(['press_release_id' => $release->id, 'type' => PressReleaseEventType::View, 'channel' => null]);
$event->forceFill(['created_at' => now()->subDays($daysAgo)->setTime(10, $i % 60)])->saveQuietly();
}
}
test('the company summary counts every release of the company, whoever submitted it', function () {
/** @var TestCase $this */
[, $company] = claimant();
$partner = User::factory()->create();
$old = PressRelease::factory()->published()->forPortal(Portal::Businessportal24)->create(['company_id' => $company->id, 'user_id' => $partner->id, 'hits' => 120, 'published_at' => now()->subMonths(2)]);
$new = PressRelease::factory()->published()->forPortal(Portal::Businessportal24)->create(['company_id' => $company->id, 'user_id' => $partner->id, 'hits' => 30, 'published_at' => now()->subDays(3)]);
PressRelease::factory()->forPortal(Portal::Businessportal24)->create(['company_id' => $company->id, 'user_id' => $partner->id, 'status' => 'draft', 'hits' => 0]);
viewsOn($old, 5, daysAgo: 40);
viewsOn($old, 2, daysAgo: 2);
viewsOn($new, 7, daysAgo: 1);
$summary = app(VisibilityStats::class)->companySummary($company, 30);
expect($summary['releases'])->toBe(3)
->and($summary['published'])->toBe(2)
->and($summary['views_total'])->toBe(150)
->and($summary['views_recent'])->toBe(9)
->and($summary['last_published_at']->isSameDay($new->published_at))->toBeTrue();
expect(app(VisibilityStats::class)->recentViewsFor([$old->id, $new->id], 30))->toBe([$old->id => 2, $new->id => 7]);
});
test('the company page lists partner-submitted releases with their views', function () {
/** @var TestCase $this */
// Vor 1c.3 filterte die Seite nach user_id — ein Beansprucher sah
// „Keine Pressemitteilungen", obwohl connektar 68 eingereicht hatte.
[, $company] = claimant();
$partner = User::factory()->create();
$release = PressRelease::factory()->published()->forPortal(Portal::Businessportal24)->create([
'company_id' => $company->id, 'user_id' => $partner->id, 'title' => 'Partnermeldung mit Reichweite', 'hits' => 4321, 'published_at' => now()->subDays(2),
]);
viewsOn($release, 3, daysAgo: 1);
LivewireVolt::test('customer.press-kits.show', ['id' => $company->id])
->assertSeeText('Partnermeldung mit Reichweite')
->assertSeeText('eingereicht über Partner')
->assertSeeText('4.321')
->assertSeeText('Aufrufe 30 Tage')
->assertDontSeeText('Keine Pressemitteilungen für diese Firma');
});
test('the daily series is gapless, marks days before measurement as unmeasured and respects retention', function () {
/** @var TestCase $this */
config()->set('press_releases.events.recorded_since', now()->subDays(10)->toDateString());
[, $company] = claimant();
$release = PressRelease::factory()->published()->forPortal(Portal::Businessportal24)->create(['company_id' => $company->id, 'published_at' => now()->subDays(20)]);
viewsOn($release, 4, daysAgo: 3);
viewsOn($release, 1, daysAgo: 0);
$series = app(VisibilityStats::class)->dailyViews($release, 14);
expect($series)->toHaveCount(14)
->and($series[13]['views'])->toBe(1)
->and($series[10]['views'])->toBe(4)
->and($series[12]['views'])->toBe(0)
// Tage vor Beginn der Aufzeichnung: keine Messung, nicht 0.
->and($series[0]['views'])->toBeNull()
->and($series[2]['views'])->toBeNull()
// Der Starttag der Aufzeichnung selbst zählt schon.
->and($series[3]['views'])->toBe(0);
config()->set('press_releases.events.retention_days', 5);
expect(app(VisibilityStats::class)->dailyViews($release, 400))->toHaveCount(5);
});
test('the release summary reports sitemap presence and a search link only for public releases', function () {
/** @var TestCase $this */
[, $company] = claimant();
$public = PressRelease::factory()->published()->forPortal(Portal::Businessportal24)->create(['company_id' => $company->id, 'published_at' => now()->subDay(), 'hits' => 9]);
$draft = PressRelease::factory()->forPortal(Portal::Businessportal24)->create(['company_id' => $company->id, 'status' => 'draft']);
PressReleaseEvent::query()->create(['press_release_id' => $public->id, 'type' => PressReleaseEventType::Share, 'channel' => 'linkedin']);
viewsOn($public, 2, daysAgo: 1);
$stats = app(VisibilityStats::class);
$summary = $stats->releaseSummary($public);
expect($summary['hits'])->toBe(9)
->and($summary['shares_total'])->toBe(1)
->and($summary['in_sitemap_since']->isSameDay($public->published_at))->toBeTrue()
->and($summary['search_url'])->toContain('site%3A')
->and($summary['search_url'])->toContain($public->slug)
->and($summary['first_view_at'])->not->toBeNull();
$draftSummary = $stats->releaseSummary($draft);
expect($draftSummary['in_sitemap_since'])->toBeNull()
->and($draftSummary['search_url'])->toBeNull();
});
test('the release page shows the visibility panel with daily bars and the search link', function () {
/** @var TestCase $this */
[$customer, $company] = claimant();
$partner = User::factory()->create();
$release = PressRelease::factory()->published()->forPortal(Portal::Businessportal24)->create([
'company_id' => $company->id, 'user_id' => $partner->id, 'hits' => 777, 'published_at' => now()->subDays(5),
]);
viewsOn($release, 6, daysAgo: 2);
LivewireVolt::test('customer.press-releases.show', ['id' => $release->id])
->assertSeeText('Sichtbarkeit')
->assertSeeText('777')
->assertSeeText('Aufrufe 30 Tage')
->assertSeeText('Bei Google prüfen')
->assertSeeText('In der Sitemap für Suchmaschinen seit '.now()->subDays(5)->format('d.m.Y'))
->assertSee('<svg', false)
->set('viewRange', 90)
->assertSeeText('Aufrufe 90 Tage');
});
test('a draft shows no visibility panel', function () {
/** @var TestCase $this */
[$customer, $company] = claimant();
$draft = PressRelease::factory()->forPortal(Portal::Businessportal24)->create(['company_id' => $company->id, 'user_id' => $customer->id, 'status' => 'draft', 'hits' => 0, 'published_at' => null]);
LivewireVolt::test('customer.press-releases.show', ['id' => $draft->id])
->assertDontSeeText('Bei Google prüfen')
->assertSeeText('noch nicht veröffentlicht');
});