feat(form-builder): add PublishGuard framework + 9 concrete guards (WS-6)

Per-purpose schema validation composes a PurposeGuardProvider returning
a list of guards. Errors collected (not first-fail) so the builder UI
surfaces every issue per save. ConditionalRequirement composes higher-
order without proliferating one-off classes.

RequiresIdentityKeyBinding checks the is_identity_key flag specifically;
the binding-existence check is handled additively by the existing
assertRequiredBindingsPresent in FormSchemaService.

SchemaHasLinkedEvent checks owner_type='event' + owner_id (FormSchema
uses polymorphic owner; there is no direct event_id column).

i18n messages live in lang/nl/form_builder_publish_guards.php.

Refs: RFC-WS-6.md §3 (Q13), §4 (V1, V3)

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-04-25 22:55:42 +02:00
parent c5b0210ae7
commit 81a8120f98
21 changed files with 1156 additions and 0 deletions

View File

@@ -0,0 +1,80 @@
<?php
declare(strict_types=1);
namespace Tests\Unit\FormBuilder\Publishing;
use App\Enums\FormBuilder\FormFieldBindingMergeStrategy;
use App\FormBuilder\Bindings\BindingTypeRegistry;
use App\FormBuilder\Publishing\AppendStrategyRequiresCollectionTarget;
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 AppendStrategyRequiresCollectionTargetTest extends TestCase
{
use RefreshDatabase;
public function test_passes_when_no_append_strategy_present(): void
{
$schema = FormSchema::factory()->create();
$field = FormField::factory()->create(['form_schema_id' => $schema->id]);
FormFieldBinding::factory()->forField($field)->entityOwned('person', 'email')->create([
'merge_strategy' => FormFieldBindingMergeStrategy::Overwrite->value,
]);
$schema->load('fields.bindings');
$result = $this->guard()->evaluate($schema);
$this->assertTrue($result->passed);
}
public function test_fails_with_scalar_target_reason(): void
{
$schema = FormSchema::factory()->create();
$field = FormField::factory()->create(['form_schema_id' => $schema->id]);
FormFieldBinding::factory()->forField($field)->entityOwned('person', 'email')->create([
'merge_strategy' => FormFieldBindingMergeStrategy::Append->value,
]);
$schema->load('fields.bindings');
$result = $this->guard()->evaluate($schema);
$this->assertFalse($result->passed);
$this->assertSame('scalar_target', $result->context['reason']);
}
public function test_passes_with_collection_target(): void
{
$schema = FormSchema::factory()->create();
$field = FormField::factory()->create(['form_schema_id' => $schema->id]);
FormFieldBinding::factory()->forField($field)->entityOwned('person', 'dietary_preferences')->create([
'merge_strategy' => FormFieldBindingMergeStrategy::Append->value,
]);
$schema->load('fields.bindings');
$result = $this->guard()->evaluate($schema);
$this->assertTrue($result->passed);
}
public function test_fails_with_unknown_target_reason(): void
{
$schema = FormSchema::factory()->create();
$field = FormField::factory()->create(['form_schema_id' => $schema->id]);
FormFieldBinding::factory()->forField($field)->entityOwned('person', 'unknown_attr')->create([
'merge_strategy' => FormFieldBindingMergeStrategy::Append->value,
]);
$schema->load('fields.bindings');
$result = $this->guard()->evaluate($schema);
$this->assertFalse($result->passed);
$this->assertSame('unknown_target', $result->context['reason']);
}
private function guard(): AppendStrategyRequiresCollectionTarget
{
return new AppendStrategyRequiresCollectionTarget(
$this->app->make(BindingTypeRegistry::class),
);
}
}

View File

@@ -0,0 +1,85 @@
<?php
declare(strict_types=1);
namespace Tests\Unit\FormBuilder\Publishing;
use App\FormBuilder\Publishing\ConditionalRequirement;
use App\FormBuilder\Publishing\PublishGuard;
use App\FormBuilder\Publishing\PublishGuardResult;
use App\Models\FormBuilder\FormSchema;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
final class ConditionalRequirementTest extends TestCase
{
use RefreshDatabase;
public function test_passes_when_predicate_false_regardless_of_sub_guard(): void
{
$schema = FormSchema::factory()->create();
$guard = new ConditionalRequirement(
predicate: fn (): bool => false,
subGuard: $this->alwaysFails(),
code: 'test_predicate_false',
);
$result = $guard->evaluate($schema);
$this->assertTrue($result->passed);
$this->assertSame('conditional:test_predicate_false', $result->guardCode);
}
public function test_passes_when_predicate_true_and_sub_passes(): void
{
$schema = FormSchema::factory()->create();
$guard = new ConditionalRequirement(
predicate: fn (): bool => true,
subGuard: $this->alwaysPasses(),
code: 'test_sub_passes',
);
$result = $guard->evaluate($schema);
$this->assertTrue($result->passed);
}
public function test_fails_when_predicate_true_and_sub_fails(): void
{
$schema = FormSchema::factory()->create();
$guard = new ConditionalRequirement(
predicate: fn (): bool => true,
subGuard: $this->alwaysFails(),
code: 'test_sub_fails',
);
$result = $guard->evaluate($schema);
$this->assertFalse($result->passed);
$this->assertSame('conditional:test_sub_fails', $result->guardCode);
$this->assertSame('inner_failure', $result->messageKey);
$this->assertSame('inner_guard', $result->context['delegated_to']);
}
private function alwaysPasses(): PublishGuard
{
return new class implements PublishGuard {
public function code(): string { return 'inner_guard'; }
public function evaluate(FormSchema $schema): PublishGuardResult
{
return PublishGuardResult::passed($this->code());
}
};
}
private function alwaysFails(): PublishGuard
{
return new class implements PublishGuard {
public function code(): string { return 'inner_guard'; }
public function evaluate(FormSchema $schema): PublishGuardResult
{
return PublishGuardResult::failed($this->code(), 'inner_failure');
}
};
}
}

View File

@@ -0,0 +1,70 @@
<?php
declare(strict_types=1);
namespace Tests\Unit\FormBuilder\Publishing;
use App\FormBuilder\Publishing\IdentityKeyBindingsOnlyInFirstSection;
use App\Models\FormBuilder\FormField;
use App\Models\FormBuilder\FormFieldBinding;
use App\Models\FormBuilder\FormSchema;
use App\Models\FormBuilder\FormSchemaSection;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
final class IdentityKeyBindingsOnlyInFirstSectionTest extends TestCase
{
use RefreshDatabase;
public function test_no_op_when_section_level_submit_is_false(): void
{
$schema = FormSchema::factory()->create(['section_level_submit' => false]);
FormSchemaSection::factory()->create(['form_schema_id' => $schema->id, 'sort_order' => 0]);
$section2 = FormSchemaSection::factory()->create(['form_schema_id' => $schema->id, 'sort_order' => 1]);
$field = FormField::factory()->create([
'form_schema_id' => $schema->id,
'form_schema_section_id' => $section2->id, // identity key in 2nd section
]);
FormFieldBinding::factory()->forField($field)->entityOwned('person', 'email')
->create(['is_identity_key' => true]);
$schema->load(['fields.bindings', 'sections']);
$result = (new IdentityKeyBindingsOnlyInFirstSection())->evaluate($schema);
$this->assertTrue($result->passed);
}
public function test_passes_when_identity_key_in_first_section(): void
{
$schema = FormSchema::factory()->create(['section_level_submit' => true]);
$section1 = FormSchemaSection::factory()->create(['form_schema_id' => $schema->id, 'sort_order' => 0]);
FormSchemaSection::factory()->create(['form_schema_id' => $schema->id, 'sort_order' => 1]);
$field = FormField::factory()->create([
'form_schema_id' => $schema->id,
'form_schema_section_id' => $section1->id,
]);
FormFieldBinding::factory()->forField($field)->entityOwned('artist', 'email')
->create(['is_identity_key' => true]);
$schema->load(['fields.bindings', 'sections']);
$result = (new IdentityKeyBindingsOnlyInFirstSection())->evaluate($schema);
$this->assertTrue($result->passed);
}
public function test_fails_when_identity_key_in_later_section(): void
{
$schema = FormSchema::factory()->create(['section_level_submit' => true]);
FormSchemaSection::factory()->create(['form_schema_id' => $schema->id, 'sort_order' => 0]);
$section2 = FormSchemaSection::factory()->create(['form_schema_id' => $schema->id, 'sort_order' => 1]);
$field = FormField::factory()->create([
'form_schema_id' => $schema->id,
'form_schema_section_id' => $section2->id,
]);
FormFieldBinding::factory()->forField($field)->entityOwned('artist', 'email')
->create(['is_identity_key' => true]);
$schema->load(['fields.bindings', 'sections']);
$result = (new IdentityKeyBindingsOnlyInFirstSection())->evaluate($schema);
$this->assertFalse($result->passed);
$this->assertSame((string) $field->id, $result->offendingFormFieldId);
}
}

View File

@@ -0,0 +1,70 @@
<?php
declare(strict_types=1);
namespace Tests\Unit\FormBuilder\Publishing;
use App\FormBuilder\Publishing\MaxOneIdentityKeyPerTargetEntity;
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 MaxOneIdentityKeyPerTargetEntityTest extends TestCase
{
use RefreshDatabase;
public function test_passes_with_zero_identity_keys(): void
{
$schema = FormSchema::factory()->create();
FormField::factory()->create(['form_schema_id' => $schema->id]);
$schema->load('fields.bindings');
$result = (new MaxOneIdentityKeyPerTargetEntity())->evaluate($schema);
$this->assertTrue($result->passed);
}
public function test_passes_with_one_identity_key_per_entity(): void
{
$schema = FormSchema::factory()->create();
$field = FormField::factory()->create(['form_schema_id' => $schema->id]);
FormFieldBinding::factory()->forField($field)->entityOwned('person', 'email')
->create(['is_identity_key' => true]);
$schema->load('fields.bindings');
$result = (new MaxOneIdentityKeyPerTargetEntity())->evaluate($schema);
$this->assertTrue($result->passed);
}
public function test_fails_with_two_identity_keys_same_entity(): void
{
$schema = FormSchema::factory()->create();
$f1 = FormField::factory()->create(['form_schema_id' => $schema->id]);
$f2 = FormField::factory()->create(['form_schema_id' => $schema->id]);
FormFieldBinding::factory()->forField($f1)->entityOwned('person', 'email')
->create(['is_identity_key' => true]);
FormFieldBinding::factory()->forField($f2)->entityOwned('person', 'first_name')
->create(['is_identity_key' => true]);
$schema->load('fields.bindings');
$result = (new MaxOneIdentityKeyPerTargetEntity())->evaluate($schema);
$this->assertFalse($result->passed);
$this->assertSame('person', $result->context['entity']);
}
public function test_passes_with_one_identity_key_each_on_different_entities(): void
{
$schema = FormSchema::factory()->create();
$f1 = FormField::factory()->create(['form_schema_id' => $schema->id]);
$f2 = FormField::factory()->create(['form_schema_id' => $schema->id]);
FormFieldBinding::factory()->forField($f1)->entityOwned('person', 'email')
->create(['is_identity_key' => true]);
FormFieldBinding::factory()->forField($f2)->entityOwned('company', 'email')
->create(['is_identity_key' => true]);
$schema->load('fields.bindings');
$result = (new MaxOneIdentityKeyPerTargetEntity())->evaluate($schema);
$this->assertTrue($result->passed);
}
}

View File

@@ -0,0 +1,48 @@
<?php
declare(strict_types=1);
namespace Tests\Unit\FormBuilder\Publishing;
use App\FormBuilder\Publishing\NoAmbiguousTrustLevels;
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 NoAmbiguousTrustLevelsTest extends TestCase
{
use RefreshDatabase;
public function test_passes_with_unique_trust_levels(): void
{
$schema = FormSchema::factory()->create();
$f1 = FormField::factory()->create(['form_schema_id' => $schema->id]);
$f2 = FormField::factory()->create(['form_schema_id' => $schema->id]);
FormFieldBinding::factory()->forField($f1)->entityOwned('person', 'email')
->create(['trust_level' => 80]);
FormFieldBinding::factory()->forField($f2)->entityOwned('person', 'email')
->create(['trust_level' => 60]);
$schema->load('fields.bindings');
$result = (new NoAmbiguousTrustLevels())->evaluate($schema);
$this->assertTrue($result->passed);
}
public function test_fails_with_duplicate_trust_levels(): void
{
$schema = FormSchema::factory()->create();
$f1 = FormField::factory()->create(['form_schema_id' => $schema->id]);
$f2 = FormField::factory()->create(['form_schema_id' => $schema->id]);
FormFieldBinding::factory()->forField($f1)->entityOwned('person', 'email')
->create(['trust_level' => 50]);
FormFieldBinding::factory()->forField($f2)->entityOwned('person', 'email')
->create(['trust_level' => 50]);
$schema->load('fields.bindings');
$result = (new NoAmbiguousTrustLevels())->evaluate($schema);
$this->assertFalse($result->passed);
$this->assertSame('person.email', $result->context['target']);
}
}

View File

@@ -0,0 +1,50 @@
<?php
declare(strict_types=1);
namespace Tests\Unit\FormBuilder\Publishing;
use App\Enums\FormBuilder\FormFieldType;
use App\FormBuilder\Publishing\RequiresFieldType;
use App\Models\FormBuilder\FormField;
use App\Models\FormBuilder\FormSchema;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
final class RequiresFieldTypeTest extends TestCase
{
use RefreshDatabase;
public function test_passes_when_min_count_satisfied(): void
{
$schema = FormSchema::factory()->create();
FormField::factory()->create([
'form_schema_id' => $schema->id,
'field_type' => FormFieldType::EMAIL->value,
]);
$schema->load('fields');
$result = (new RequiresFieldType(FormFieldType::EMAIL, 1))->evaluate($schema);
$this->assertTrue($result->passed);
}
public function test_fails_when_no_fields_of_type(): void
{
$schema = FormSchema::factory()->create();
FormField::factory()->create([
'form_schema_id' => $schema->id,
'field_type' => FormFieldType::TEXT->value,
]);
$schema->load('fields');
$result = (new RequiresFieldType(FormFieldType::EMAIL, 1))->evaluate($schema);
$this->assertFalse($result->passed);
$this->assertSame(0, $result->context['actual_count']);
}
public function test_code_includes_type_value(): void
{
$guard = new RequiresFieldType(FormFieldType::EMAIL);
$this->assertSame('requires_field_type:EMAIL', $guard->code());
}
}

View File

@@ -0,0 +1,57 @@
<?php
declare(strict_types=1);
namespace Tests\Unit\FormBuilder\Publishing;
use App\FormBuilder\Publishing\RequiresIdentityKeyBinding;
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 RequiresIdentityKeyBindingTest extends TestCase
{
use RefreshDatabase;
public function test_code_format(): void
{
$guard = new RequiresIdentityKeyBinding('person', 'email');
$this->assertSame('requires_identity_key_binding:person:email', $guard->code());
}
public function test_passes_when_identity_key_binding_exists(): void
{
$schema = FormSchema::factory()->create();
$field = FormField::factory()->create(['form_schema_id' => $schema->id]);
FormFieldBinding::factory()
->forField($field)
->entityOwned('person', 'email')
->create(['is_identity_key' => true]);
$schema->load('fields.bindings');
$result = (new RequiresIdentityKeyBinding('person', 'email'))->evaluate($schema);
$this->assertTrue($result->passed);
}
public function test_fails_when_binding_present_without_identity_key_flag(): void
{
$schema = FormSchema::factory()->create();
$field = FormField::factory()->create(['form_schema_id' => $schema->id]);
FormFieldBinding::factory()
->forField($field)
->entityOwned('person', 'email')
->create(['is_identity_key' => false]);
$schema->load('fields.bindings');
$result = (new RequiresIdentityKeyBinding('person', 'email'))->evaluate($schema);
$this->assertFalse($result->passed);
$this->assertSame(
'form_builder_publish_guards.requires_identity_key_binding',
$result->messageKey,
);
}
}

View File

@@ -0,0 +1,39 @@
<?php
declare(strict_types=1);
namespace Tests\Unit\FormBuilder\Publishing;
use App\FormBuilder\Publishing\SchemaHasLinkedEvent;
use App\Models\Event;
use App\Models\FormBuilder\FormSchema;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
final class SchemaHasLinkedEventTest extends TestCase
{
use RefreshDatabase;
public function test_passes_when_owner_type_event_with_owner_id(): void
{
$event = Event::factory()->create();
$schema = FormSchema::factory()->create([
'owner_type' => 'event',
'owner_id' => $event->id,
]);
$result = (new SchemaHasLinkedEvent())->evaluate($schema);
$this->assertTrue($result->passed);
}
public function test_fails_when_owner_type_is_not_event(): void
{
$schema = FormSchema::factory()->create([
'owner_type' => 'organisation',
'owner_id' => null,
]);
$result = (new SchemaHasLinkedEvent())->evaluate($schema);
$this->assertFalse($result->passed);
}
}

View File

@@ -0,0 +1,65 @@
<?php
declare(strict_types=1);
namespace Tests\Unit\FormBuilder\Publishing;
use App\Enums\FormBuilder\FormFieldConfigType;
use App\Enums\FormBuilder\FormFieldType;
use App\FormBuilder\Publishing\TagCategoriesConfiguredOnAllPickers;
use App\Models\FormBuilder\FormField;
use App\Models\FormBuilder\FormFieldConfig;
use App\Models\FormBuilder\FormSchema;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
final class TagCategoriesConfiguredOnAllPickersTest extends TestCase
{
use RefreshDatabase;
public function test_passes_when_no_tag_pickers(): void
{
$schema = FormSchema::factory()->create();
FormField::factory()->create([
'form_schema_id' => $schema->id,
'field_type' => FormFieldType::TEXT->value,
]);
$schema->load('fields.configs');
$result = (new TagCategoriesConfiguredOnAllPickers())->evaluate($schema);
$this->assertTrue($result->passed);
}
public function test_passes_when_tag_picker_has_categories_config(): void
{
$schema = FormSchema::factory()->create();
$field = FormField::factory()->create([
'form_schema_id' => $schema->id,
'field_type' => FormFieldType::TAG_PICKER->value,
]);
FormFieldConfig::query()->withoutGlobalScopes()->create([
'owner_type' => 'form_field',
'owner_id' => $field->id,
'config_type' => FormFieldConfigType::TagCategories->value,
'parameters' => ['categories' => ['Veiligheid', 'Horeca']],
]);
$schema->load('fields.configs');
$result = (new TagCategoriesConfiguredOnAllPickers())->evaluate($schema);
$this->assertTrue($result->passed);
}
public function test_fails_when_tag_picker_has_no_categories(): void
{
$schema = FormSchema::factory()->create();
$field = FormField::factory()->create([
'form_schema_id' => $schema->id,
'field_type' => FormFieldType::TAG_PICKER->value,
]);
$schema->load('fields.configs');
$result = (new TagCategoriesConfiguredOnAllPickers())->evaluate($schema);
$this->assertFalse($result->passed);
$this->assertSame((string) $field->id, $result->offendingFormFieldId);
}
}