PM-Tracking: Ereignis-Log fuer Aufrufe + Shares (press_release_events)

- press_release_events (type view/share, channel, ohne Personenbezug):
  Model/Enums/Factory, Cascade-Delete an der PM
- Detailaufruf loggt View-Event zusaetzlich zum Legacy-hits-Zaehler
  (bleibt bewusst einfacher Page-Request-Zaehler, User-Vorgabe 10.07.)
- POST press-release/{slug}/share (throttle 30/min, nur archiv-sichtbare
  PMs des Portals) loggt Teilen-Klicks je Kanal (LinkedIn/X/E-Mail/Copy)
- Kundenpanel-PM-Seite: neue Kachel Geteilt mit Kanal-Aufschluesselung
- lang: web.detail.views (de/en)
- Tests: Web/PressReleaseTrackingTest (7)

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
Kevin Adametz 2026-07-10 09:56:42 +00:00
parent 60d0d9b103
commit 8f5970436b
14 changed files with 392 additions and 0 deletions

View file

@ -0,0 +1,142 @@
<?php
use App\Enums\Portal;
use App\Enums\PressReleaseEventType;
use App\Enums\PressReleaseShareChannel;
use App\Enums\PressReleaseStatus;
use App\Models\Category;
use App\Models\Company;
use App\Models\PressRelease;
use App\Models\PressReleaseEvent;
use App\Models\User;
use Database\Seeders\RolesAndPermissionsSeeder;
use Livewire\Volt\Volt as LivewireVolt;
use Tests\TestCase;
/**
* @return PressRelease im Archiv sichtbare PM auf businessportal24
*/
function trackingRelease(array $attributes = []): PressRelease
{
$category = Category::factory()->create(['portal' => Portal::Businessportal24]);
$company = Company::factory()->create(['portal' => Portal::Businessportal24, 'is_active' => true]);
return PressRelease::factory()
->published()
->forPortal(Portal::Businessportal24)
->for($category)
->for($company)
->create(array_merge(['slug' => 'tracking-pm', 'published_at' => now()->subDay()], $attributes));
}
test('release detail logs a view event alongside the hit counter', function () {
/** @var TestCase $this */
$release = trackingRelease(['hits' => 4]);
$this->get('https://businessportal24.test/de/press-release/'.$release->slug)->assertSuccessful();
expect(PressRelease::withoutGlobalScopes()->find($release->id)->hits)->toBe(5);
$event = PressReleaseEvent::query()->sole();
expect($event->press_release_id)->toBe($release->id)
->and($event->type)->toBe(PressReleaseEventType::View)
->and($event->channel)->toBeNull();
});
test('release detail shows the public view count', function () {
/** @var TestCase $this */
$release = trackingRelease(['hits' => 1233]);
// Anzeige inkl. des aktuellen Aufrufs (1234).
$this->get('https://businessportal24.test/de/press-release/'.$release->slug)
->assertSuccessful()
->assertSeeText('1.234 Aufrufe');
});
test('share endpoint logs a share event with its channel', function () {
/** @var TestCase $this */
$release = trackingRelease();
$this->postJson('https://businessportal24.test/de/press-release/'.$release->slug.'/share', [
'channel' => 'linkedin',
])->assertNoContent();
$event = PressReleaseEvent::query()->sole();
expect($event->press_release_id)->toBe($release->id)
->and($event->type)->toBe(PressReleaseEventType::Share)
->and($event->channel)->toBe(PressReleaseShareChannel::LinkedIn);
});
test('share endpoint rejects unknown channels', function () {
/** @var TestCase $this */
$release = trackingRelease();
$this->postJson('https://businessportal24.test/de/press-release/'.$release->slug.'/share', [
'channel' => 'myspace',
])->assertUnprocessable();
expect(PressReleaseEvent::query()->count())->toBe(0);
});
test('share endpoint returns 404 for drafts and unknown slugs', function () {
/** @var TestCase $this */
$category = Category::factory()->create(['portal' => Portal::Businessportal24]);
$company = Company::factory()->create(['portal' => Portal::Businessportal24, 'is_active' => true]);
$draft = PressRelease::factory()
->forPortal(Portal::Businessportal24)
->for($category)
->for($company)
->create(['status' => PressReleaseStatus::Draft, 'slug' => 'entwurf-pm']);
$this->postJson('https://businessportal24.test/de/press-release/'.$draft->slug.'/share', [
'channel' => 'x',
])->assertNotFound();
$this->postJson('https://businessportal24.test/de/press-release/gibt-es-nicht/share', [
'channel' => 'x',
])->assertNotFound();
expect(PressReleaseEvent::query()->count())->toBe(0);
});
test('customer panel shows view and share stats for the owner', function () {
/** @var TestCase $this */
$this->seed(RolesAndPermissionsSeeder::class);
$customer = User::factory()->create(['is_active' => true]);
$customer->assignRole('customer');
$company = Company::factory()->presseecho()->create();
$customer->companies()->attach($company->id, ['role' => 'owner']);
$release = PressRelease::factory()->published()->create([
'user_id' => $customer->id,
'company_id' => $company->id,
'category_id' => Category::factory()->create()->id,
'portal' => $company->portal->value,
'hits' => 42,
]);
PressReleaseEvent::factory()->share(PressReleaseShareChannel::LinkedIn)->for($release)->count(2)->create();
PressReleaseEvent::factory()->share(PressReleaseShareChannel::CopyLink)->for($release)->create();
$this->actingAs($customer);
LivewireVolt::test('customer.press-releases.show', ['id' => $release->id])
->assertSee('Aufrufe')
->assertSee('42')
->assertSee('Geteilt')
->assertSee('LinkedIn 2')
->assertSee('Link kopiert 1');
});
test('deleting a press release removes its tracked events', function () {
$release = trackingRelease();
PressReleaseEvent::factory()->for($release)->create();
PressReleaseEvent::factory()->share(PressReleaseShareChannel::X)->for($release)->create();
PressRelease::withoutGlobalScopes()->find($release->id)->forceDelete();
expect(PressReleaseEvent::query()->count())->toBe(0);
});