create(); $company = Company::factory()->create(); $user->companies()->attach($company->id, ['role' => 'member']); $authored = PressRelease::factory()->create(['user_id' => $user->id, 'company_id' => null]); $viaCompany = PressRelease::factory()->create(['company_id' => $company->id]); // erfüllt beide Zweige – darf nur einmal gezählt werden $both = PressRelease::factory()->create(['user_id' => $user->id, 'company_id' => $company->id]); $page = PressRelease::accessiblePageFor($user); expect($page->total())->toBe(3) ->and($page->pluck('id')->sort()->values()->all()) ->toBe(collect([$authored->id, $viaCompany->id, $both->id])->sort()->values()->all()); }); test('excludes releases the user cannot access', function () { $user = User::factory()->create(); $stranger = PressRelease::factory()->create(); $page = PressRelease::accessiblePageFor($user); expect($page->total())->toBe(0) ->and($page->contains($stranger))->toBeFalse(); }); test('applies the constrain closure to both union branches', function () { $user = User::factory()->create(); PressRelease::factory()->create(['user_id' => $user->id, 'status' => PressReleaseStatus::Published->value]); PressRelease::factory()->create(['user_id' => $user->id, 'status' => PressReleaseStatus::Draft->value]); $page = PressRelease::accessiblePageFor( $user, fn ($query) => $query->where('status', PressReleaseStatus::Published->value), ); expect($page->total())->toBe(1) ->and($page->first()->status)->toBe(PressReleaseStatus::Published); }); test('respects sort column and direction', function () { $user = User::factory()->create(); $a = PressRelease::factory()->create(['user_id' => $user->id, 'title' => 'Alpha']); $z = PressRelease::factory()->create(['user_id' => $user->id, 'title' => 'Zulu']); $asc = PressRelease::accessiblePageFor($user, null, 'title', 'asc'); $desc = PressRelease::accessiblePageFor($user, null, 'title', 'desc'); expect($asc->first()->id)->toBe($a->id) ->and($desc->first()->id)->toBe($z->id); }); test('paginates with the given page size', function () { $user = User::factory()->create(); PressRelease::factory()->count(7)->create(['user_id' => $user->id]); $page = PressRelease::accessiblePageFor($user, null, 'created_at', 'desc', 5); expect($page->total())->toBe(7) ->and($page->perPage())->toBe(5) ->and($page->count())->toBe(5); }); test('falls back to created_at for an invalid sort column', function () { $user = User::factory()->create(); PressRelease::factory()->create(['user_id' => $user->id]); $page = PressRelease::accessiblePageFor($user, null, 'title; drop table', 'sideways'); expect($page->total())->toBe(1); });