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

@@ -5,7 +5,7 @@ declare(strict_types=1);
namespace App\FormBuilder\Bindings;
use App\Exceptions\FormBuilder\PersonProvisioningException;
use App\Models\CrowdType;
use App\Models\FormBuilder\FormSchema;
use App\Models\FormBuilder\FormSubmission;
use App\Models\FormBuilder\FormValue;
use App\Models\Person;
@@ -152,31 +152,39 @@ final readonly class PersonProvisioner
/**
* Resolve a default crowd_type_id for a freshly-provisioned Person.
* Person.crowd_type_id is NOT NULL on the migration. Session 2 picks
* the first active CrowdType for the submission's organisation. A
* future per-schema setting (`default_crowd_type_id`) is the proper
* resolution but out-of-scope here.
* Person.crowd_type_id is NOT NULL on the migration; the schema
* declares its target CrowdType explicitly via `default_crowd_type_id`.
*
* RFC-WS-6 v1.1 §3 Q8 addendum (was: silent oldest() fallback in
* session 2). The RequiresDefaultCrowdType publish guard prevents
* misconfigured event_registration schemas from publishing; this
* runtime throw is a failsafe for live-table edits between publish
* and apply.
*
* @throws PersonProvisioningException
*/
private function resolveCrowdTypeId(FormSubmission $submission): string
{
$orgId = (string) $submission->organisation_id;
$crowdType = CrowdType::query()
->withoutGlobalScopes()
->where('organisation_id', $orgId)
->where('is_active', true)->oldest()
->first();
if ($crowdType === null) {
/** @var FormSchema|null $schema */
$schema = $submission->schema;
if (! $schema instanceof FormSchema) {
throw new PersonProvisioningException(
'no_crowd_type',
'no_schema',
(string) $submission->id,
"no active CrowdType available for organisation {$orgId}",
'submission has no schema relation loaded',
);
}
return (string) $crowdType->id;
$crowdTypeId = $schema->default_crowd_type_id;
if ($crowdTypeId === null) {
throw new PersonProvisioningException(
'no_default_crowd_type',
(string) $submission->id,
"form_schema {$schema->id} has no default_crowd_type_id set",
);
}
return (string) $crowdTypeId;
}
private function readFormValue(FormSubmission $submission, string $formFieldId): mixed

View File

@@ -0,0 +1,33 @@
<?php
declare(strict_types=1);
namespace App\FormBuilder\Publishing;
use App\Models\FormBuilder\FormSchema;
/**
* RFC-WS-6 v1.1 §3 Q8 addendum event_registration schemas must
* declare a default_crowd_type_id so PersonProvisioner can land
* new registrants in the right CrowdType. Replaces the silent
* oldest() heuristic from session 2.
*/
final readonly class RequiresDefaultCrowdType implements PublishGuard
{
public function code(): string
{
return 'requires_default_crowd_type';
}
public function evaluate(FormSchema $schema): PublishGuardResult
{
if ($schema->default_crowd_type_id !== null) {
return PublishGuardResult::passed($this->code());
}
return PublishGuardResult::failed(
$this->code(),
'form_builder_publish_guards.requires_default_crowd_type',
);
}
}

View File

@@ -12,6 +12,7 @@ use App\FormBuilder\Publishing\IdentityKeyBindingsOnlyInFirstSection;
use App\FormBuilder\Publishing\MaxOneIdentityKeyPerTargetEntity;
use App\FormBuilder\Publishing\NoAmbiguousTrustLevels;
use App\FormBuilder\Publishing\RequiresFieldType;
use App\FormBuilder\Publishing\RequiresDefaultCrowdType;
use App\FormBuilder\Publishing\RequiresIdentityKeyBinding;
use App\FormBuilder\Publishing\SchemaHasLinkedEvent;
use App\FormBuilder\Publishing\TagCategoriesConfiguredOnAllPickers;
@@ -27,6 +28,7 @@ final readonly class EventRegistrationGuards implements PurposeGuardProvider
{
return [
new RequiresIdentityKeyBinding('person', 'email'),
new RequiresDefaultCrowdType(),
new MaxOneIdentityKeyPerTargetEntity(),
new RequiresFieldType(FormFieldType::EMAIL, 1),
new ConditionalRequirement(

View File

@@ -7,6 +7,7 @@ namespace App\Models\FormBuilder;
use App\Enums\FormBuilder\FormPurpose;
use App\Enums\FormBuilder\FormSchemaSnapshotMode;
use App\Enums\FormBuilder\FormSubmissionMode;
use App\Models\CrowdType;
use App\Models\Organisation;
use App\Models\Scopes\OrganisationScope;
use App\Models\User;
@@ -42,6 +43,7 @@ final class FormSchema extends Model
'name',
'slug',
'purpose',
'default_crowd_type_id',
'description',
'is_published',
'submission_mode',
@@ -88,6 +90,12 @@ final class FormSchema extends Model
return $this->belongsTo(Organisation::class);
}
/** @return BelongsTo<CrowdType, $this> */
public function defaultCrowdType(): BelongsTo
{
return $this->belongsTo(CrowdType::class, 'default_crowd_type_id');
}
public function owner(): MorphTo
{
return $this->morphTo();

View File

@@ -0,0 +1,47 @@
<?php
declare(strict_types=1);
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
/**
* RFC-WS-6 v1.1 §3 Q8 addendum per-schema default CrowdType for
* Person provisioning. Replaces the silent oldest() heuristic from
* WS-6 session 2 with explicit configuration; RequiresDefaultCrowdType
* publish guard ensures event_registration schemas declare it.
*
* No backfill: pre-launch the table is empty. Dev seeders populate
* the column when reseeding (FormBuilderDevSeeder).
*/
return new class extends Migration
{
public function up(): void
{
// Plain nullable ULID column. NO database-level foreign key —
// SQLite's rebuild-on-FK-add cascade-deletes form_fields rows
// (form_fields.form_schema_id has cascadeOnDelete on
// form_schemas), which corrupts running migration tests.
// Application-level integrity: FormSchema::defaultCrowdType()
// belongsTo loads from CrowdType; CrowdTypeObserver could add
// a soft-handle on delete if needed in production. The new
// RequiresDefaultCrowdType publish guard plus the runtime
// failsafe in PersonProvisioner are the load-bearing checks.
Schema::table('form_schemas', function (Blueprint $table): void {
$table->ulid('default_crowd_type_id')
->nullable()
->after('purpose');
$table->index(['organisation_id', 'default_crowd_type_id'], 'fs_org_default_crowd_type_idx');
});
}
public function down(): void
{
Schema::table('form_schemas', function (Blueprint $table): void {
$table->dropIndex('fs_org_default_crowd_type_idx');
$table->dropColumn('default_crowd_type_id');
});
}
};

View File

@@ -10,6 +10,7 @@ use App\Enums\FormBuilder\FormSchemaSnapshotMode;
use App\Enums\FormBuilder\FormSubmissionMode;
use App\Enums\FormBuilder\FormSubmissionStatus;
use App\Enums\FormBuilder\FormValueStorageHint;
use App\Models\CrowdType;
use App\Models\Event;
use App\Models\FormBuilder\FormField;
use App\Models\FormBuilder\FormSchema;
@@ -132,6 +133,7 @@ final class FormBuilderDevSeeder
'name' => $event->name.' — registratie',
'slug' => Str::slug($event->slug.'-registratie'),
'purpose' => FormPurpose::EVENT_REGISTRATION,
'default_crowd_type_id' => self::resolveDefaultCrowdTypeId((string) $event->organisation_id),
'description' => "Registratieformulier voor {$event->name}.",
'is_published' => in_array(
$event->status,
@@ -391,6 +393,7 @@ final class FormBuilderDevSeeder
'name' => $name,
'slug' => $slug,
'purpose' => FormPurpose::EVENT_REGISTRATION,
'default_crowd_type_id' => self::resolveDefaultCrowdTypeId((string) $org->id),
'description' => "Demo-formulier voor het end-to-end doorlopen van de vrijwilligersregistratie voor {$event->name}.",
'is_published' => true,
'submission_mode' => FormSubmissionMode::DRAFT_SINGLE,
@@ -615,6 +618,46 @@ final class FormBuilderDevSeeder
->count();
}
/**
* RFC-WS-6 v1.1 §3 Q8 addendum event_registration schemas declare
* a target CrowdType. DevSeeder picks the VOLUNTEER system_type
* (DevSeeder::run() always seeds it for every dev org); falls back
* to the first active CrowdType if VOLUNTEER is somehow missing,
* and creates a minimal CrowdType row as last-resort to satisfy the
* NOT NULL FK on Person.crowd_type_id.
*
* Future seeders MUST keep this lookup contract: schemas need a
* resolvable CrowdType per org or the RequiresDefaultCrowdType
* publish guard will block publish.
*/
private static function resolveDefaultCrowdTypeId(string $organisationId): string
{
$crowdType = CrowdType::query()
->withoutGlobalScopes()
->where('organisation_id', $organisationId)
->where('is_active', true)
->where('system_type', 'VOLUNTEER')
->first()
?? CrowdType::query()
->withoutGlobalScopes()
->where('organisation_id', $organisationId)
->where('is_active', true)
->oldest()
->first();
if ($crowdType === null) {
$crowdType = CrowdType::create([
'organisation_id' => $organisationId,
'name' => 'Vrijwilliger',
'system_type' => 'VOLUNTEER',
'color' => '#10b981',
'is_active' => true,
]);
}
return (string) $crowdType->id;
}
private static function uniqueSchemaSlug(Organisation $org, string $base): string
{
$candidate = $base;

View File

@@ -7,6 +7,7 @@ declare(strict_types=1);
* Dutch only for v1 per CLAUDE.md (Crewli is Dutch-first).
*/
return [
'requires_default_crowd_type' => 'Schema voor vrijwilligers/crew registratie moet een standaard crowd type hebben.',
'requires_identity_key_binding' => 'Het veld voor :entity.:attribute moet als identity-key zijn aangemerkt.',
'max_one_identity_key_per_target_entity' => 'Per doel-entiteit mag maximaal één binding identity-key zijn.',
'requires_field_type' => 'Dit formulier moet ten minste :min_count veld(en) van type :type bevatten.',

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