Files
crewli/api/tests/Feature/FormBuilder/Purposes/EventRegistrationGuardsIntegrationTest.php
bert.hausmans d2059e3cff 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>
2026-04-27 23:47:32 +02:00

136 lines
4.8 KiB
PHP

<?php
declare(strict_types=1);
namespace Tests\Feature\FormBuilder\Purposes;
use App\Enums\FormBuilder\FormFieldType;
use App\Enums\FormBuilder\FormPurpose;
use App\FormBuilder\Purposes\Guards\EventRegistrationGuards;
use App\FormBuilder\Purposes\PurposeRegistry;
use App\Models\FormBuilder\FormField;
use App\Models\FormBuilder\FormFieldBinding;
use App\Models\FormBuilder\FormSchema;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
final class EventRegistrationGuardsIntegrationTest extends TestCase
{
use RefreshDatabase;
public function test_valid_schema_passes_all_guards(): void
{
$schema = $this->buildValidSchema();
$provider = $this->app->make(EventRegistrationGuards::class);
foreach ($provider->publishGuards() as $guard) {
$result = $guard->evaluate($schema);
$this->assertTrue(
$result->passed,
"Guard {$guard->code()} unexpectedly failed: {$result->messageKey}",
);
}
}
public function test_missing_identity_key_flag_fails_specific_guard(): void
{
$schema = $this->buildValidSchema();
// Mutation: clear is_identity_key on the email binding.
FormFieldBinding::query()
->withoutGlobalScopes()
->whereIn('owner_id', $schema->fields->pluck('id'))
->where('target_attribute', 'email')
->update(['is_identity_key' => false]);
$schema->load('fields.bindings');
$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_identity_key_binding:person:email',
$failedCodes,
'Expected the identity-key flag-check guard to fail.',
);
}
public function test_registry_resolves_event_registration_to_this_provider(): void
{
$registry = $this->app->make(PurposeRegistry::class);
$provider = $registry->guardProviderFor('event_registration');
$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,
'field_type' => FormFieldType::EMAIL->value,
]);
FormFieldBinding::factory()->forField($emailField)->entityOwned('person', 'email')
->create(['is_identity_key' => true, 'trust_level' => 80]);
$firstNameField = FormField::factory()->create([
'form_schema_id' => $schema->id,
'field_type' => FormFieldType::TEXT->value,
]);
FormFieldBinding::factory()->forField($firstNameField)->entityOwned('person', 'first_name')
->create(['is_identity_key' => false, 'trust_level' => 60]);
$lastNameField = FormField::factory()->create([
'form_schema_id' => $schema->id,
'field_type' => FormFieldType::TEXT->value,
]);
FormFieldBinding::factory()->forField($lastNameField)->entityOwned('person', 'last_name')
->create(['is_identity_key' => false, 'trust_level' => 50]);
$schema->load(['fields.bindings', 'fields.configs', 'sections']);
return $schema;
}
}