feat(form-builder): per-schema default_crowd_type_id replaces silent oldest() heuristic (WS-6)

Session 2's PersonProvisioner picked CrowdType::oldest() for the org —
silently wrong for multi-crowd_type orgs (Volunteer + Crew + Press are
three distinct crowd_types in one org). Schemas now declare their
target crowd_type explicitly via form_schemas.default_crowd_type_id.
RequiresDefaultCrowdType publish guard prevents misconfigured
event_registration schemas from publishing.

PersonProvisioner: oldest() fallback removed entirely. Misconfiguration
throws no_default_crowd_type at runtime; publish guard prevents it at
config time.

Migration uses a plain ulid() column without DB-level FK because
SQLite's table-rebuild on ALTER ADD FOREIGN KEY cascade-deletes
form_fields rows (form_fields.form_schema_id has cascadeOnDelete on
form_schemas). Application-level integrity via FormSchema::defaultCrowdType()
belongsTo + the publish guard + the runtime failsafe — three load-bearing
checks, none of which require the DB-level constraint.

Three pre-existing migration backfill tests bumped step counts +1 to
account for the new migration sitting between WS-5c and WS-5d:
FormFieldBindingMigrationTest (16→17, 14→15), FormFieldConfigBackfillAndDropTest
(11→12), FormFieldValidationRuleBackfillTest (14→15),
ConditionalLogicBackfillTest (5→6).

Six event_registration test fixtures updated to set default_crowd_type_id
to satisfy the new publish guard.

FormBuilderDevSeeder.resolveDefaultCrowdTypeId() — VOLUNTEER → first-active
→ create-as-needed fallback chain; documented contract for future seeders.

SCHEMA.md updated to v2.7.
Refs: RFC-WS-6.md v1.1 §3 Q8 addendum (Task 4 of this session)

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-04-27 23:47:32 +02:00
parent 1fdd254a8a
commit d2059e3cff
20 changed files with 311 additions and 36 deletions

View File

@@ -80,7 +80,7 @@ final class FormBindingApplicatorIntegrationTest extends TestCase
private function makeEventRegistrationSubmission(): FormSubmission
{
$event = Event::factory()->create();
CrowdType::factory()->create([
$crowdType = CrowdType::factory()->create([
'organisation_id' => $event->organisation_id,
'is_active' => true,
]);
@@ -88,6 +88,7 @@ final class FormBindingApplicatorIntegrationTest extends TestCase
$schema = FormSchema::factory()->create([
'organisation_id' => $event->organisation_id,
'purpose' => FormPurpose::EVENT_REGISTRATION->value,
'default_crowd_type_id' => $crowdType->id,
]);
$emailField = FormField::factory()->create([

View File

@@ -42,7 +42,7 @@ final class FormFieldBindingMigrationTest extends TestCase
// validation-rules-backfill, create-validation-rules) +
// 2 WS-6 migrations (action-failures, apply-status) +
// 2 WS-5a migrations (drop-binding-cols, create-bindings) = 16.
$this->artisan('migrate:rollback', ['--step' => 16])->assertSuccessful();
$this->artisan('migrate:rollback', ['--step' => 17])->assertSuccessful();
$this->assertFalse(Schema::hasTable('form_field_bindings'));
$this->assertTrue(Schema::hasColumn('form_fields', 'binding'));
$this->assertTrue(Schema::hasColumn('form_field_library', 'default_binding'));
@@ -104,7 +104,7 @@ final class FormFieldBindingMigrationTest extends TestCase
public function test_rollback_reconstructs_json_and_drops_table(): void
{
// Walk back the full WS-5d + WS-5c + WS-6 + WS-5b + WS-5a stack (16 migrations).
$this->artisan('migrate:rollback', ['--step' => 16])->assertSuccessful();
$this->artisan('migrate:rollback', ['--step' => 17])->assertSuccessful();
[$fieldAId, , ] = $this->seedFieldsWithBindingJson();
[$libAId, ] = $this->seedLibraryWithBindingJson();
@@ -119,7 +119,7 @@ final class FormFieldBindingMigrationTest extends TestCase
// the pre-WS-5b state (conditional-logic, validation-rules, configs
// and options tables gone, validation_rules + options JSON columns
// reappear on source tables; binding contract intact).
$this->artisan('migrate:rollback', ['--step' => 14])->assertSuccessful();
$this->artisan('migrate:rollback', ['--step' => 15])->assertSuccessful();
$this->assertFalse(Schema::hasTable('form_field_options'));
$this->assertFalse(Schema::hasTable('form_field_conditional_logic_groups'));
$this->assertFalse(Schema::hasTable('form_field_conditional_logic_conditions'));

View File

@@ -54,6 +54,13 @@ final class PublishChecksRelationalBindingsTest extends TestCase
['name' => 'ER', 'purpose' => FormPurpose::EVENT_REGISTRATION->value],
$this->actor,
);
// RFC v1.1 §3 Q8 addendum: event_registration schemas need a
// default_crowd_type_id (RequiresDefaultCrowdType publish guard).
$crowdType = \App\Models\CrowdType::factory()->create([
'organisation_id' => $this->org->id,
]);
$schema->default_crowd_type_id = $crowdType->id;
$schema->save();
// WS-6 publish guards require: EMAIL field type, identity_key flag
// on person.email, unique trust levels per (entity, attribute).

View File

@@ -35,7 +35,7 @@ final class ConditionalLogicBackfillTest extends TestCase
// create-options + WS-5c drop-cl-col + WS-5c backfill-cl
// migrations to land in the conditional-logic JSON-era state with
// no relational form_field_options table yet.
$this->artisan('migrate:rollback', ['--step' => 5])->assertSuccessful();
$this->artisan('migrate:rollback', ['--step' => 6])->assertSuccessful();
$this->assertTrue(Schema::hasColumn('form_fields', 'conditional_logic'));
$fieldId = $this->seedFieldWithJson([
@@ -156,7 +156,7 @@ final class ConditionalLogicBackfillTest extends TestCase
]);
// Roll back only the backfill migration — writes the JSON back.
$this->artisan('migrate:rollback', ['--step' => 5])->assertSuccessful();
$this->artisan('migrate:rollback', ['--step' => 6])->assertSuccessful();
$reconstructed = DB::table('form_fields')
->where('id', $fieldId)
@@ -183,7 +183,7 @@ final class ConditionalLogicBackfillTest extends TestCase
public function test_unknown_top_level_key_fails_migration(): void
{
$this->artisan('migrate:rollback', ['--step' => 5])->assertSuccessful();
$this->artisan('migrate:rollback', ['--step' => 6])->assertSuccessful();
$this->seedFieldWithJson([
'hide_when' => ['all' => [['field_slug' => 'x', 'operator' => 'equals', 'value' => 1]]],
@@ -196,7 +196,7 @@ final class ConditionalLogicBackfillTest extends TestCase
public function test_unknown_comparison_operator_fails_migration(): void
{
$this->artisan('migrate:rollback', ['--step' => 5])->assertSuccessful();
$this->artisan('migrate:rollback', ['--step' => 6])->assertSuccessful();
$this->seedFieldWithJson([
'show_when' => ['all' => [['field_slug' => 'x', 'operator' => 'matches_regex', 'value' => 'y']]],

View File

@@ -30,7 +30,7 @@ final class FormFieldConfigBackfillAndDropTest extends TestCase
// Roll back 4 WS-5c migrations + 2 WS-6 migrations + 5 WS-5b
// migrations = 11, to get the pre-WS-5b state where the JSON column
// still exists on form_fields / form_field_library.
$this->artisan('migrate:rollback', ['--step' => 11])->assertSuccessful();
$this->artisan('migrate:rollback', ['--step' => 12])->assertSuccessful();
$this->assertTrue(Schema::hasColumn('form_fields', 'validation_rules'));
$fieldId = $this->seedField([

View File

@@ -124,6 +124,11 @@ final class FormSchemaServicePublishGuardsTest extends TestCase
'section_level_submit' => false,
'is_published' => false,
]);
$crowdType = \App\Models\CrowdType::factory()->create([
'organisation_id' => $schema->organisation_id,
]);
$schema->default_crowd_type_id = $crowdType->id;
$schema->save();
$emailField = FormField::factory()->create([
'form_schema_id' => $schema->id,

View File

@@ -94,7 +94,7 @@ final class ApplyBindingsOnFormSubmitTest extends TestCase
private function makeSubmission(): FormSubmission
{
$event = Event::factory()->create();
CrowdType::factory()->create([
$crowdType = CrowdType::factory()->create([
'organisation_id' => $event->organisation_id,
'is_active' => true,
]);
@@ -102,6 +102,7 @@ final class ApplyBindingsOnFormSubmitTest extends TestCase
$schema = FormSchema::factory()->create([
'organisation_id' => $event->organisation_id,
'purpose' => FormPurpose::EVENT_REGISTRATION->value,
'default_crowd_type_id' => $crowdType->id,
]);
$emailField = FormField::factory()->create([

View File

@@ -67,12 +67,45 @@ final class EventRegistrationGuardsIntegrationTest extends TestCase
$this->assertInstanceOf(EventRegistrationGuards::class, $provider);
}
public function test_requires_default_crowd_type_is_in_guard_list(): void
{
$provider = $this->app->make(EventRegistrationGuards::class);
$codes = array_map(
static fn (\App\FormBuilder\Publishing\PublishGuard $g): string => $g->code(),
$provider->publishGuards(),
);
$this->assertContains('requires_default_crowd_type', $codes);
}
public function test_missing_default_crowd_type_fails_specific_guard(): void
{
$schema = $this->buildValidSchema();
$schema->default_crowd_type_id = null;
$schema->save();
$provider = $this->app->make(EventRegistrationGuards::class);
$failedCodes = [];
foreach ($provider->publishGuards() as $guard) {
$result = $guard->evaluate($schema);
if (! $result->passed) {
$failedCodes[] = $guard->code();
}
}
$this->assertContains('requires_default_crowd_type', $failedCodes);
}
private function buildValidSchema(): FormSchema
{
$schema = FormSchema::factory()->create([
'purpose' => FormPurpose::EVENT_REGISTRATION->value,
'section_level_submit' => false,
]);
$crowdType = \App\Models\CrowdType::factory()->create([
'organisation_id' => $schema->organisation_id,
]);
$schema->default_crowd_type_id = $crowdType->id;
$schema->save();
$emailField = FormField::factory()->create([
'form_schema_id' => $schema->id,

View File

@@ -133,6 +133,16 @@ final class PurposeSchemaLifecycleTest extends TestCase
private function seedRequiredBindings(FormSchema $schema, FormPurpose $purpose): void
{
if ($purpose === FormPurpose::EVENT_REGISTRATION) {
// RFC v1.1 §3 Q8 addendum: event_registration needs
// default_crowd_type_id (RequiresDefaultCrowdType guard).
$crowdType = \App\Models\CrowdType::factory()->create([
'organisation_id' => $schema->organisation_id,
]);
$schema->default_crowd_type_id = $crowdType->id;
$schema->save();
}
match ($purpose) {
FormPurpose::EVENT_REGISTRATION => [
// WS-6 publish guards require: identity_key flag on email,

View File

@@ -40,7 +40,7 @@ final class FormFieldValidationRuleBackfillTest extends TestCase
// validation-rules-backfill + create-validation-rules) = 14.
// Brings us to the pre-WS-5b state: validation_rules JSON column
// present, no relational tables for WS-5b/c/d.
$this->artisan('migrate:rollback', ['--step' => 14])->assertSuccessful();
$this->artisan('migrate:rollback', ['--step' => 15])->assertSuccessful();
$this->assertFalse(Schema::hasTable('form_field_validation_rules'));
$this->assertTrue(Schema::hasColumn('form_fields', 'validation_rules'));
@@ -101,7 +101,7 @@ final class FormFieldValidationRuleBackfillTest extends TestCase
// (validation_rules JSON column present; no relational tables for
// WS-5b). Step count: drop-cols + configs-backfill + create-configs
// + validation-rules-backfill + create-validation-rules = 5.
$this->artisan('migrate:rollback', ['--step' => 14])->assertSuccessful();
$this->artisan('migrate:rollback', ['--step' => 15])->assertSuccessful();
$fieldId = $this->seedFieldWithJson([
'field_type' => 'TAG_PICKER',
@@ -125,7 +125,7 @@ final class FormFieldValidationRuleBackfillTest extends TestCase
// (validation_rules JSON column present; no relational tables for
// WS-5b). Step count: drop-cols + configs-backfill + create-configs
// + validation-rules-backfill + create-validation-rules = 5.
$this->artisan('migrate:rollback', ['--step' => 14])->assertSuccessful();
$this->artisan('migrate:rollback', ['--step' => 15])->assertSuccessful();
$fieldId = $this->seedFieldWithJson([
'field_type' => 'TEXT',
@@ -152,7 +152,7 @@ final class FormFieldValidationRuleBackfillTest extends TestCase
// (validation_rules JSON column present; no relational tables for
// WS-5b). Step count: drop-cols + configs-backfill + create-configs
// + validation-rules-backfill + create-validation-rules = 5.
$this->artisan('migrate:rollback', ['--step' => 14])->assertSuccessful();
$this->artisan('migrate:rollback', ['--step' => 15])->assertSuccessful();
$this->seedFieldWithJson([
'field_type' => 'TEXT',
@@ -169,7 +169,7 @@ final class FormFieldValidationRuleBackfillTest extends TestCase
// (validation_rules JSON column present; no relational tables for
// WS-5b). Step count: drop-cols + configs-backfill + create-configs
// + validation-rules-backfill + create-validation-rules = 5.
$this->artisan('migrate:rollback', ['--step' => 14])->assertSuccessful();
$this->artisan('migrate:rollback', ['--step' => 15])->assertSuccessful();
$this->seedFieldWithJson([
'field_type' => 'BOOLEAN',
@@ -188,7 +188,7 @@ final class FormFieldValidationRuleBackfillTest extends TestCase
// full-back-then-full-forward cycle — rolling back all WS-5b
// migrations restores the pre-WS-5b state (columns present on
// source tables; validation rules relational table gone).
$this->artisan('migrate:rollback', ['--step' => 14])->assertSuccessful();
$this->artisan('migrate:rollback', ['--step' => 15])->assertSuccessful();
[$numberId] = $this->seedFields();
$this->artisan('migrate')->assertSuccessful();
@@ -203,7 +203,7 @@ final class FormFieldValidationRuleBackfillTest extends TestCase
// Roll back WS-5b fully → column reappears and carries canonical JSON
// reconstructed from the relational rows.
$this->artisan('migrate:rollback', ['--step' => 14])->assertSuccessful();
$this->artisan('migrate:rollback', ['--step' => 15])->assertSuccessful();
$this->assertTrue(Schema::hasColumn('form_fields', 'validation_rules'));
$field = DB::table('form_fields')->where('id', $numberId)->first();

View File

@@ -123,6 +123,26 @@ final class PersonProvisionerTest extends TestCase
});
}
public function test_throws_when_schema_has_no_default_crowd_type(): void
{
$submission = $this->makeSubmissionWithEmail('jan@example.nl');
// Clear the field that the helper set up to satisfy the new contract.
/** @var FormSchema $schema */
$schema = $submission->schema;
$schema->default_crowd_type_id = null;
$schema->save();
$submission = $submission->fresh(['schema']);
DB::transaction(function () use ($submission): void {
try {
$this->provisioner()->provisionFromSubmission($submission);
$this->fail('Expected PersonProvisioningException');
} catch (PersonProvisioningException $e) {
$this->assertSame('no_default_crowd_type', $e->reasonCode);
}
});
}
public function test_throws_when_identity_key_form_value_absent(): void
{
// Schema has the binding, but no form_value row was written
@@ -162,8 +182,12 @@ final class PersonProvisionerTest extends TestCase
// PersonProvisioner needs an active CrowdType in the org to set
// crowd_type_id on a freshly-provisioned Person (NOT NULL column).
if (! CrowdType::query()->where('organisation_id', $organisation->id)->where('is_active', true)->exists()) {
CrowdType::factory()->create([
$crowdType = CrowdType::query()
->where('organisation_id', $organisation->id)
->where('is_active', true)
->first();
if ($crowdType === null) {
$crowdType = CrowdType::factory()->create([
'organisation_id' => $organisation->id,
'is_active' => true,
]);
@@ -171,6 +195,7 @@ final class PersonProvisionerTest extends TestCase
$schema = FormSchema::factory()->create([
'organisation_id' => $organisation->id,
'default_crowd_type_id' => $crowdType->id,
]);
$emailField = FormField::factory()->create([

View File

@@ -0,0 +1,44 @@
<?php
declare(strict_types=1);
namespace Tests\Unit\FormBuilder\Publishing;
use App\FormBuilder\Publishing\RequiresDefaultCrowdType;
use App\Models\CrowdType;
use App\Models\FormBuilder\FormSchema;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
final class RequiresDefaultCrowdTypeTest extends TestCase
{
use RefreshDatabase;
public function test_passes_when_default_crowd_type_id_set(): void
{
$schema = FormSchema::factory()->create();
$crowdType = CrowdType::factory()->create([
'organisation_id' => $schema->organisation_id,
]);
$schema->default_crowd_type_id = $crowdType->id;
$schema->save();
$result = (new RequiresDefaultCrowdType())->evaluate($schema->fresh());
$this->assertTrue($result->passed);
$this->assertSame('requires_default_crowd_type', $result->guardCode);
}
public function test_fails_when_default_crowd_type_id_null(): void
{
$schema = FormSchema::factory()->create();
$this->assertNull($schema->default_crowd_type_id);
$result = (new RequiresDefaultCrowdType())->evaluate($schema);
$this->assertFalse($result->passed);
$this->assertSame('requires_default_crowd_type', $result->guardCode);
$this->assertSame(
'form_builder_publish_guards.requires_default_crowd_type',
$result->messageKey,
);
}
}