248 lines
7.5 KiB
PHP
248 lines
7.5 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Tests\Feature;
|
|
|
|
use App\Models\PreregistrationPage;
|
|
use App\Models\Subscriber;
|
|
use App\Models\User;
|
|
use App\Services\PreregistrationPageBlockWriter;
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
|
use Illuminate\Support\Str;
|
|
use Tests\TestCase;
|
|
|
|
class PublicPageTest extends TestCase
|
|
{
|
|
use RefreshDatabase;
|
|
|
|
public function test_show_returns_ok_for_active_public_page(): void
|
|
{
|
|
$page = $this->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();
|
|
}
|
|
|
|
public function test_subscribe_rejects_invalid_email(): void
|
|
{
|
|
$page = $this->makePage([
|
|
'start_date' => now()->subHour(),
|
|
'end_date' => now()->addMonth(),
|
|
]);
|
|
|
|
$response = $this->postJson(route('public.subscribe', ['publicPage' => $page->slug]), [
|
|
'first_name' => 'A',
|
|
'last_name' => 'B',
|
|
'email' => 'not-a-valid-email',
|
|
]);
|
|
|
|
$response->assertUnprocessable();
|
|
$response->assertJsonValidationErrors(['email']);
|
|
}
|
|
|
|
public function test_subscribe_accepts_empty_phone_when_phone_field_enabled(): void
|
|
{
|
|
$page = $this->makePage([
|
|
'start_date' => now()->subHour(),
|
|
'end_date' => now()->addMonth(),
|
|
'phone_enabled' => true,
|
|
]);
|
|
|
|
$response = $this->postJson(route('public.subscribe', ['publicPage' => $page->slug]), [
|
|
'first_name' => 'A',
|
|
'last_name' => 'B',
|
|
'email' => 'nophone@example.com',
|
|
'phone' => '',
|
|
]);
|
|
|
|
$response->assertOk();
|
|
$this->assertDatabaseHas('subscribers', [
|
|
'preregistration_page_id' => $page->id,
|
|
'email' => 'nophone@example.com',
|
|
'phone' => null,
|
|
]);
|
|
}
|
|
|
|
public function test_subscribe_rejects_phone_with_too_few_digits(): void
|
|
{
|
|
$page = $this->makePage([
|
|
'start_date' => now()->subHour(),
|
|
'end_date' => now()->addMonth(),
|
|
'phone_enabled' => true,
|
|
]);
|
|
|
|
$response = $this->postJson(route('public.subscribe', ['publicPage' => $page->slug]), [
|
|
'first_name' => 'A',
|
|
'last_name' => 'B',
|
|
'email' => 'a@example.com',
|
|
'phone' => '+31 12 3',
|
|
]);
|
|
|
|
$response->assertUnprocessable();
|
|
$response->assertJsonValidationErrors(['phone']);
|
|
}
|
|
|
|
public function test_subscribe_normalizes_phone_to_digits(): void
|
|
{
|
|
$page = $this->makePage([
|
|
'start_date' => now()->subHour(),
|
|
'end_date' => now()->addMonth(),
|
|
'phone_enabled' => true,
|
|
]);
|
|
|
|
$response = $this->postJson(route('public.subscribe', ['publicPage' => $page->slug]), [
|
|
'first_name' => 'A',
|
|
'last_name' => 'B',
|
|
'email' => 'phoneuser@example.com',
|
|
'phone' => '+31 6 1234 5678',
|
|
]);
|
|
|
|
$response->assertOk();
|
|
$this->assertDatabaseHas('subscribers', [
|
|
'preregistration_page_id' => $page->id,
|
|
'email' => 'phoneuser@example.com',
|
|
'phone' => '31612345678',
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* @param array<string, mixed> $overrides
|
|
*/
|
|
private function makePage(array $overrides = []): PreregistrationPage
|
|
{
|
|
$user = User::factory()->create(['role' => 'user']);
|
|
|
|
$page = 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,
|
|
'background_image' => null,
|
|
'logo_image' => null,
|
|
'is_active' => true,
|
|
], $overrides));
|
|
|
|
$writer = app(PreregistrationPageBlockWriter::class);
|
|
if (! $page->blocks()->exists()) {
|
|
$writer->seedDefaultBlocks($page);
|
|
}
|
|
|
|
$page->refresh();
|
|
$hero = $page->getHeroBlock();
|
|
if ($hero !== null) {
|
|
$c = $hero->content;
|
|
$c['headline'] = $page->heading;
|
|
$hero->update(['content' => $c]);
|
|
}
|
|
|
|
if (($overrides['phone_enabled'] ?? false) === true) {
|
|
$page->load('blocks');
|
|
$form = $page->getFormBlock();
|
|
if ($form !== null) {
|
|
$c = $form->content;
|
|
data_set($c, 'fields.phone.enabled', true);
|
|
$form->update(['content' => $c]);
|
|
}
|
|
}
|
|
|
|
$page->syncLegacyContentColumnsFromBlocks();
|
|
|
|
return $page->fresh(['blocks']);
|
|
}
|
|
}
|