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

@@ -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,
);
}
}