Adds PurposeGuardProvider as a parallel interface to PurposeDefinition (value object stays untouched). Seven concrete providers, one per v1.0 purpose, each declaring its publish-guard list. Registry resolves and caches providers via guards_class config key. Universal guards (MaxOneIdentityKeyPerTargetEntity, AppendStrategyRequiresCollectionTarget, NoAmbiguousTrustLevels, IdentityKeyBindingsOnlyInFirstSection) wire into every purpose. The section guard is a cheap no-op when section_level_submit=false. ArtistAdvanceGuards omits RequiresIdentityKeyBinding because the artist subject is resolved via portal token, not form data. Same reasoning for supplier_intake (production_request) and the auth-based purposes. Includes a cross-cutting BindingTypeRegistryConsistencyTest that verifies tasks 5/7/8 do not contradict each other (registry ↔ guards ↔ purpose required_bindings). Refs: RFC-WS-6.md §3 (Q9, Q13) Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
67 lines
2.2 KiB
PHP
67 lines
2.2 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Tests\Feature\FormBuilder\Purposes;
|
|
|
|
use App\Enums\FormBuilder\FormPurpose;
|
|
use App\FormBuilder\Purposes\Guards\SupplierIntakeGuards;
|
|
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 SupplierIntakeGuardsIntegrationTest extends TestCase
|
|
{
|
|
use RefreshDatabase;
|
|
|
|
public function test_valid_schema_passes_all_guards(): void
|
|
{
|
|
$schema = $this->buildValidSchema();
|
|
$provider = $this->app->make(SupplierIntakeGuards::class);
|
|
|
|
foreach ($provider->publishGuards() as $guard) {
|
|
$result = $guard->evaluate($schema);
|
|
$this->assertTrue(
|
|
$result->passed,
|
|
"Guard {$guard->code()} failed: {$result->messageKey}",
|
|
);
|
|
}
|
|
}
|
|
|
|
public function test_two_identity_keys_on_company_fails(): void
|
|
{
|
|
$schema = $this->buildValidSchema();
|
|
$extraField = FormField::factory()->create(['form_schema_id' => $schema->id]);
|
|
FormFieldBinding::factory()->forField($extraField)->entityOwned('company', 'kvk_number')
|
|
->create(['is_identity_key' => true, 'trust_level' => 60]);
|
|
$schema->load('fields.bindings');
|
|
|
|
$provider = $this->app->make(SupplierIntakeGuards::class);
|
|
|
|
$failedCodes = [];
|
|
foreach ($provider->publishGuards() as $guard) {
|
|
$result = $guard->evaluate($schema);
|
|
if (! $result->passed) {
|
|
$failedCodes[] = $guard->code();
|
|
}
|
|
}
|
|
|
|
$this->assertContains('max_one_identity_key_per_target_entity', $failedCodes);
|
|
}
|
|
|
|
private function buildValidSchema(): FormSchema
|
|
{
|
|
$schema = FormSchema::factory()->create([
|
|
'purpose' => FormPurpose::SUPPLIER_INTAKE->value,
|
|
]);
|
|
$field = FormField::factory()->create(['form_schema_id' => $schema->id]);
|
|
FormFieldBinding::factory()->forField($field)->entityOwned('company', 'name')
|
|
->create(['is_identity_key' => true, 'trust_level' => 80]);
|
|
$schema->load(['fields.bindings', 'sections']);
|
|
|
|
return $schema;
|
|
}
|
|
}
|