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