makePage([ 'start_date' => now()->addDay(), 'end_date' => now()->addMonth(), ]); $response = $this->get(route('public.page', ['publicPage' => $page->slug])); $response->assertOk(); $response->assertSee($page->heading, escape: false); } public function test_show_returns_not_found_when_page_is_inactive(): void { $page = $this->makePage([ 'is_active' => false, 'start_date' => now()->subHour(), 'end_date' => now()->addMonth(), ]); $response = $this->get(route('public.page', ['publicPage' => $page->slug])); $response->assertNotFound(); } public function test_subscribe_returns_json_success_and_creates_subscriber(): void { $page = $this->makePage([ 'thank_you_message' => 'Custom thanks', 'start_date' => now()->subHour(), 'end_date' => now()->addMonth(), 'phone_enabled' => false, ]); $response = $this->postJson(route('public.subscribe', ['publicPage' => $page->slug]), [ 'first_name' => 'Ada', 'last_name' => 'Lovelace', 'email' => 'ada@example.com', ]); $response->assertOk(); $response->assertJson([ 'success' => true, 'message' => 'Custom thanks', ]); $this->assertDatabaseHas('subscribers', [ 'preregistration_page_id' => $page->id, 'email' => 'ada@example.com', ]); } public function test_subscribe_returns_friendly_message_for_duplicate_email(): void { $page = $this->makePage([ 'start_date' => now()->subHour(), 'end_date' => now()->addMonth(), ]); Subscriber::query()->create([ 'preregistration_page_id' => $page->id, 'first_name' => 'A', 'last_name' => 'B', 'email' => 'dup@example.com', ]); $response = $this->postJson(route('public.subscribe', ['publicPage' => $page->slug]), [ 'first_name' => 'C', 'last_name' => 'D', 'email' => 'dup@example.com', ]); $response->assertStatus(422); $response->assertJson([ 'success' => false, 'message' => 'You are already registered for this event.', ]); } public function test_subscribe_forbidden_outside_registration_window(): void { $page = $this->makePage([ 'start_date' => now()->addDay(), 'end_date' => now()->addMonth(), ]); $response = $this->postJson(route('public.subscribe', ['publicPage' => $page->slug]), [ 'first_name' => 'A', 'last_name' => 'B', 'email' => 'a@example.com', ]); $response->assertForbidden(); } /** * @param array $overrides */ private function makePage(array $overrides = []): PreregistrationPage { $user = User::factory()->create(['role' => 'user']); return PreregistrationPage::query()->create(array_merge([ 'slug' => (string) Str::uuid(), 'user_id' => $user->id, 'title' => 'Test page', 'heading' => 'Join us', 'intro_text' => null, 'thank_you_message' => null, 'expired_message' => null, 'ticketshop_url' => null, 'start_date' => now()->subHour(), 'end_date' => now()->addMonth(), 'phone_enabled' => false, 'is_active' => true, ], $overrides)); } }