S2c D2, D3, D4, D8 — the meat of the public API rewrite.
Draft / save / submit split (D4):
- POST /public/forms/{public_token}/submissions
Creates a draft. idempotency_key is now REQUIRED; second POST with
the same key returns the existing draft (HTTP 200 vs 201 for fresh).
UniqueConstraintViolationException caught for race-safe replay.
- PUT /public/forms/{public_token}/submissions/{submission_id}
Auto-save. Partial updates only — each PUT writes just the
slugs in the body. Status stays 'draft'; auto_save_count++.
- POST /public/forms/{public_token}/submissions/{submission_id}/submit
Final submission. Merges body values with already-saved values,
runs strict rule set against the merged map, then calls
FormSubmissionService::submit which fires the lifecycle events
(tag sync, identity match). Rate-limited per IP per token per hour.
Access rules: submission must belong to the resolved schema; status
must be 'draft' (409 SUBMISSION_ALREADY_SUBMITTED otherwise); schema
still accepting submissions.
Sub-endpoints (D2, D3):
- GET /public/forms/{public_token}/time-slots
Volunteer-only, festival-aware (parent + children). Reads straight
from TimeSlot model — no org-coupled service to extract from. Out:
{id, name, date, start_time, end_time, duration_hours, event_id,
event_name}.
- GET /public/forms/{public_token}/sections
show_in_registration=true, type=standard, deduplicated by name
across festival children.
Dynamic per-field validation (D8):
- FormFieldRuleBuilder builds Laravel rule arrays from form_fields.
strict() enforces is_required + in:options + type rules (email,
url, numeric, date, boolean, phone regex); relaxed() is the
auto-save variant that drops required-ness.
- StartPublicDraftRequest (required idempotency_key),
SavePublicDraftRequest (relaxed rules, values optional),
SubmitPublicSubmissionRequest (relaxed rules at body level — the
controller merges the body with saved values and runs the strict
validator on the full map so submit with an empty body still
passes when everything was auto-saved).
- FormValueService backs the request layer up with deeper enforcement
of validation_rules JSON (min/max/regex) + is_unique. Throws
FieldValidationException (422) which renders via the D6 envelope.
PublicFormTokenResolver centralises the grace-window logic; every
public endpoint resolves through it so the standardised exceptions
bubble uniformly.
Routes: 6 total under /public/forms/ (up from 2). Tests:
PublicFormApiTest's existing submit test retrofitted to the three-step
flow; 857 tests still green.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
126 lines
4.1 KiB
PHP
126 lines
4.1 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Tests\Feature\FormBuilder;
|
|
|
|
use App\Enums\FormBuilder\FormFieldType;
|
|
use App\Enums\FormBuilder\FormPurpose;
|
|
use App\Models\FormBuilder\FormField;
|
|
use App\Models\FormBuilder\FormSchema;
|
|
use App\Models\Organisation;
|
|
use Database\Seeders\RoleSeeder;
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
|
use Illuminate\Support\Facades\Config;
|
|
use Illuminate\Support\Str;
|
|
use Tests\TestCase;
|
|
|
|
final class PublicFormApiTest extends TestCase
|
|
{
|
|
use RefreshDatabase;
|
|
|
|
private Organisation $org;
|
|
|
|
private FormSchema $schema;
|
|
|
|
protected function setUp(): void
|
|
{
|
|
parent::setUp();
|
|
$this->seed(RoleSeeder::class);
|
|
$this->org = Organisation::factory()->create();
|
|
$this->schema = FormSchema::factory()->create([
|
|
'organisation_id' => $this->org->id,
|
|
'purpose' => FormPurpose::PUBLIC_RSVP,
|
|
'is_published' => true,
|
|
'public_token' => (string) Str::ulid(),
|
|
]);
|
|
|
|
FormField::factory()->create([
|
|
'form_schema_id' => $this->schema->id,
|
|
'field_type' => FormFieldType::TEXT->value,
|
|
'slug' => 'name',
|
|
'label' => 'Naam',
|
|
'is_portal_visible' => true,
|
|
'is_admin_only' => false,
|
|
]);
|
|
FormField::factory()->create([
|
|
'form_schema_id' => $this->schema->id,
|
|
'field_type' => FormFieldType::TEXTAREA->value,
|
|
'slug' => 'secret_admin_notes',
|
|
'label' => 'Admin notes',
|
|
'is_portal_visible' => false,
|
|
'is_admin_only' => true,
|
|
]);
|
|
}
|
|
|
|
public function test_show_returns_schema_without_hidden_fields(): void
|
|
{
|
|
$response = $this->getJson("/api/v1/public/forms/{$this->schema->public_token}");
|
|
$response->assertOk();
|
|
|
|
$slugs = collect($response->json('data.fields'))->pluck('slug')->all();
|
|
$this->assertContains('name', $slugs);
|
|
$this->assertNotContains('secret_admin_notes', $slugs);
|
|
}
|
|
|
|
public function test_show_unknown_token_returns_404(): void
|
|
{
|
|
$this->getJson('/api/v1/public/forms/'.Str::ulid())->assertStatus(404);
|
|
}
|
|
|
|
public function test_submit_creates_submission(): void
|
|
{
|
|
// PUBLIC_RSVP does not require captcha by default
|
|
Config::set('form_builder.captcha.required_for_purposes', []);
|
|
|
|
// S2c D4 three-step flow: create draft, save values, submit.
|
|
$create = $this->postJson(
|
|
"/api/v1/public/forms/{$this->schema->public_token}/submissions",
|
|
[
|
|
'idempotency_key' => 'public-rsvp-test-001',
|
|
'public_submitter_name' => 'Bart',
|
|
'public_submitter_email' => 'bart@example.nl',
|
|
],
|
|
);
|
|
$create->assertCreated();
|
|
$this->assertSame('draft', $create->json('data.status'));
|
|
$submissionId = $create->json('data.id');
|
|
|
|
$this->putJson(
|
|
"/api/v1/public/forms/{$this->schema->public_token}/submissions/{$submissionId}",
|
|
['values' => ['name' => 'Bart']],
|
|
)->assertOk();
|
|
|
|
$this->postJson(
|
|
"/api/v1/public/forms/{$this->schema->public_token}/submissions/{$submissionId}/submit",
|
|
[],
|
|
)
|
|
->assertCreated()
|
|
->assertJsonPath('data.status', 'submitted');
|
|
}
|
|
|
|
public function test_submit_with_expired_previous_token_returns_410(): void
|
|
{
|
|
$previousToken = (string) Str::ulid();
|
|
$this->schema->update([
|
|
'public_token_previous' => $previousToken,
|
|
'public_token_rotated_at' => now()->subDays(8),
|
|
]);
|
|
|
|
$this->getJson("/api/v1/public/forms/{$previousToken}")->assertStatus(410);
|
|
}
|
|
|
|
public function test_submit_within_grace_window_still_works(): void
|
|
{
|
|
Config::set('form_builder.captcha.required_for_purposes', []);
|
|
|
|
$previousToken = (string) Str::ulid();
|
|
$this->schema->update([
|
|
'public_token_previous' => $previousToken,
|
|
'public_token_rotated_at' => now()->subDays(2),
|
|
]);
|
|
|
|
$this->getJson("/api/v1/public/forms/{$previousToken}")->assertOk();
|
|
}
|
|
}
|