feat(form-builder): form_field_validation_rules table + polymorphic owner + scope + cascade
This commit is contained in:
@@ -33,8 +33,9 @@ final class FormFieldBindingMigrationTest extends TestCase
|
||||
|
||||
public function test_forward_migrations_backfill_rows_from_both_json_sources(): void
|
||||
{
|
||||
// Roll back: drop_binding_json_columns → create_form_field_bindings.
|
||||
$this->artisan('migrate:rollback', ['--step' => 2])->assertSuccessful();
|
||||
// Roll back: create_form_field_validation_rules_table (WS-5b commit 1)
|
||||
// → drop_binding_json_columns → create_form_field_bindings.
|
||||
$this->artisan('migrate:rollback', ['--step' => 3])->assertSuccessful();
|
||||
$this->assertFalse(Schema::hasTable('form_field_bindings'));
|
||||
$this->assertTrue(Schema::hasColumn('form_fields', 'binding'));
|
||||
$this->assertTrue(Schema::hasColumn('form_field_library', 'default_binding'));
|
||||
@@ -95,7 +96,9 @@ final class FormFieldBindingMigrationTest extends TestCase
|
||||
|
||||
public function test_rollback_reconstructs_json_and_drops_table(): void
|
||||
{
|
||||
$this->artisan('migrate:rollback', ['--step' => 2])->assertSuccessful();
|
||||
// Walk back: validation-rules table (WS-5b commit 1) →
|
||||
// drop_binding_json_columns → create_form_field_bindings.
|
||||
$this->artisan('migrate:rollback', ['--step' => 3])->assertSuccessful();
|
||||
[$fieldAId, , ] = $this->seedFieldsWithBindingJson();
|
||||
[$libAId, ] = $this->seedLibraryWithBindingJson();
|
||||
|
||||
@@ -105,6 +108,12 @@ final class FormFieldBindingMigrationTest extends TestCase
|
||||
$this->assertFalse(Schema::hasColumn('form_fields', 'binding'));
|
||||
$this->assertSame(5, DB::table('form_field_bindings')->count());
|
||||
|
||||
// Step back over WS-5b validation-rules table → irrelevant to the
|
||||
// binding contract, but restores the pre-WS-5b state.
|
||||
$this->artisan('migrate:rollback', ['--step' => 1])->assertSuccessful();
|
||||
$this->assertFalse(Schema::hasTable('form_field_validation_rules'));
|
||||
$this->assertTrue(Schema::hasTable('form_field_bindings'));
|
||||
|
||||
// Step back over drop_binding_json_columns → columns reappear empty.
|
||||
$this->artisan('migrate:rollback', ['--step' => 1])->assertSuccessful();
|
||||
$this->assertTrue(Schema::hasColumn('form_fields', 'binding'));
|
||||
|
||||
@@ -0,0 +1,124 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Tests\Feature\FormBuilder\ValidationRules;
|
||||
|
||||
use App\Models\FormBuilder\FormField;
|
||||
use App\Models\FormBuilder\FormFieldBinding;
|
||||
use App\Models\FormBuilder\FormFieldLibrary;
|
||||
use App\Models\FormBuilder\FormFieldValidationRule;
|
||||
use App\Models\FormBuilder\FormSchema;
|
||||
use App\Models\Organisation;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Tests\TestCase;
|
||||
|
||||
/**
|
||||
* Asserts that the renamed `FormFieldChildTablesCascadeObserver` cleans up
|
||||
* validation-rule rows alongside binding rows when the owner is deleted.
|
||||
*/
|
||||
final class FormFieldValidationRuleCascadeTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
public function test_soft_delete_of_field_cascades_validation_rules(): void
|
||||
{
|
||||
$org = Organisation::factory()->create();
|
||||
$schema = FormSchema::factory()->create(['organisation_id' => $org->id]);
|
||||
$field = FormField::factory()->create(['form_schema_id' => $schema->id]);
|
||||
|
||||
FormFieldValidationRule::factory()->forField($field)->create();
|
||||
FormFieldValidationRule::factory()
|
||||
->forField($field)
|
||||
->ofType(\App\Enums\FormBuilder\FormFieldValidationRuleType::MaxLength, ['value' => 10])
|
||||
->create();
|
||||
|
||||
$this->assertSame(2, FormFieldValidationRule::query()
|
||||
->withoutGlobalScopes()
|
||||
->where('owner_type', 'form_field')
|
||||
->where('owner_id', $field->id)
|
||||
->count(),
|
||||
);
|
||||
|
||||
$field->delete(); // soft delete on FormField
|
||||
|
||||
$this->assertSame(0, FormFieldValidationRule::query()
|
||||
->withoutGlobalScopes()
|
||||
->where('owner_type', 'form_field')
|
||||
->where('owner_id', $field->id)
|
||||
->count(),
|
||||
);
|
||||
}
|
||||
|
||||
public function test_delete_of_library_entry_cascades_validation_rules(): void
|
||||
{
|
||||
$org = Organisation::factory()->create();
|
||||
$library = FormFieldLibrary::factory()->create(['organisation_id' => $org->id]);
|
||||
|
||||
FormFieldValidationRule::factory()->forLibrary($library)->create();
|
||||
|
||||
$this->assertSame(1, FormFieldValidationRule::query()
|
||||
->withoutGlobalScopes()
|
||||
->where('owner_type', 'form_field_library')
|
||||
->where('owner_id', $library->id)
|
||||
->count(),
|
||||
);
|
||||
|
||||
$library->delete();
|
||||
|
||||
$this->assertSame(0, FormFieldValidationRule::query()
|
||||
->withoutGlobalScopes()
|
||||
->where('owner_type', 'form_field_library')
|
||||
->where('owner_id', $library->id)
|
||||
->count(),
|
||||
);
|
||||
}
|
||||
|
||||
public function test_deleting_one_field_does_not_cascade_others(): void
|
||||
{
|
||||
$org = Organisation::factory()->create();
|
||||
$schema = FormSchema::factory()->create(['organisation_id' => $org->id]);
|
||||
$a = FormField::factory()->create(['form_schema_id' => $schema->id]);
|
||||
$b = FormField::factory()->create(['form_schema_id' => $schema->id]);
|
||||
|
||||
FormFieldValidationRule::factory()->forField($a)->create();
|
||||
FormFieldValidationRule::factory()->forField($b)->create();
|
||||
|
||||
$a->delete();
|
||||
|
||||
$this->assertSame(1, FormFieldValidationRule::query()
|
||||
->withoutGlobalScopes()
|
||||
->where('owner_type', 'form_field')
|
||||
->where('owner_id', $b->id)
|
||||
->count(),
|
||||
);
|
||||
}
|
||||
|
||||
public function test_cascade_observer_also_cleans_up_bindings_on_same_owner(): void
|
||||
{
|
||||
// Regression guard after renaming the observer: the combined
|
||||
// observer must still clean up binding rows (WS-5a responsibility)
|
||||
// not only validation rules (WS-5b addition).
|
||||
$org = Organisation::factory()->create();
|
||||
$schema = FormSchema::factory()->create(['organisation_id' => $org->id]);
|
||||
$field = FormField::factory()->create(['form_schema_id' => $schema->id]);
|
||||
|
||||
FormFieldBinding::factory()->forField($field)->entityOwned('person', 'email')->create();
|
||||
FormFieldValidationRule::factory()->forField($field)->create();
|
||||
|
||||
$field->delete();
|
||||
|
||||
$this->assertSame(0, FormFieldBinding::query()
|
||||
->withoutGlobalScopes()
|
||||
->where('owner_type', 'form_field')
|
||||
->where('owner_id', $field->id)
|
||||
->count(),
|
||||
);
|
||||
$this->assertSame(0, FormFieldValidationRule::query()
|
||||
->withoutGlobalScopes()
|
||||
->where('owner_type', 'form_field')
|
||||
->where('owner_id', $field->id)
|
||||
->count(),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,111 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Tests\Feature\FormBuilder\ValidationRules;
|
||||
|
||||
use App\Enums\FormBuilder\FormFieldValidationRuleType;
|
||||
use App\Models\FormBuilder\FormField;
|
||||
use App\Models\FormBuilder\FormFieldLibrary;
|
||||
use App\Models\FormBuilder\FormFieldValidationRule;
|
||||
use App\Models\FormBuilder\FormSchema;
|
||||
use App\Models\Organisation;
|
||||
use Illuminate\Database\Eloquent\Relations\Relation;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Tests\TestCase;
|
||||
|
||||
final class FormFieldValidationRuleRelationTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
public function test_field_morph_many_validation_rules_loads_all(): void
|
||||
{
|
||||
$org = Organisation::factory()->create();
|
||||
$schema = FormSchema::factory()->create(['organisation_id' => $org->id]);
|
||||
$field = FormField::factory()->create(['form_schema_id' => $schema->id]);
|
||||
|
||||
FormFieldValidationRule::factory()
|
||||
->forField($field)
|
||||
->ofType(FormFieldValidationRuleType::MinLength, ['value' => 2])
|
||||
->create();
|
||||
FormFieldValidationRule::factory()
|
||||
->forField($field)
|
||||
->ofType(FormFieldValidationRuleType::MaxLength, ['value' => 40])
|
||||
->create();
|
||||
|
||||
$rules = $field->fresh()->validationRules;
|
||||
$this->assertCount(2, $rules);
|
||||
$types = $rules->pluck('rule_type.value')->sort()->values()->all();
|
||||
$this->assertSame(['max_length', 'min_length'], $types);
|
||||
}
|
||||
|
||||
public function test_library_morph_many_validation_rules_loads_all(): void
|
||||
{
|
||||
$org = Organisation::factory()->create();
|
||||
$library = FormFieldLibrary::factory()->create(['organisation_id' => $org->id]);
|
||||
|
||||
FormFieldValidationRule::factory()
|
||||
->forLibrary($library)
|
||||
->ofType(FormFieldValidationRuleType::Regex, ['pattern' => '/^[0-9]+$/'])
|
||||
->create();
|
||||
|
||||
$rules = $library->fresh()->validationRules;
|
||||
$this->assertCount(1, $rules);
|
||||
$this->assertSame(FormFieldValidationRuleType::Regex, $rules->first()->rule_type);
|
||||
$this->assertSame(['pattern' => '/^[0-9]+$/'], $rules->first()->parameters);
|
||||
}
|
||||
|
||||
public function test_owner_morphto_returns_correct_concrete_model(): void
|
||||
{
|
||||
$org = Organisation::factory()->create();
|
||||
$schema = FormSchema::factory()->create(['organisation_id' => $org->id]);
|
||||
$field = FormField::factory()->create(['form_schema_id' => $schema->id]);
|
||||
$library = FormFieldLibrary::factory()->create(['organisation_id' => $org->id]);
|
||||
|
||||
$fieldRule = FormFieldValidationRule::factory()->forField($field)->create();
|
||||
$libraryRule = FormFieldValidationRule::factory()->forLibrary($library)->create();
|
||||
|
||||
$this->assertInstanceOf(FormField::class, $fieldRule->fresh()->owner);
|
||||
$this->assertSame($field->id, $fieldRule->fresh()->owner->id);
|
||||
|
||||
$this->assertInstanceOf(FormFieldLibrary::class, $libraryRule->fresh()->owner);
|
||||
$this->assertSame($library->id, $libraryRule->fresh()->owner->id);
|
||||
}
|
||||
|
||||
public function test_morph_aliases_form_field_and_form_field_library_are_registered(): void
|
||||
{
|
||||
// Morph-map alignment guard — both aliases need to resolve for the
|
||||
// polymorphic `owner` relation on the validation-rule rows. Reuses
|
||||
// the aliases WS-5a registered for bindings; this test protects
|
||||
// against an accidental rename in AppServiceProvider.
|
||||
$morphMap = Relation::morphMap();
|
||||
$this->assertArrayHasKey('form_field', $morphMap);
|
||||
$this->assertSame(FormField::class, $morphMap['form_field']);
|
||||
$this->assertArrayHasKey('form_field_library', $morphMap);
|
||||
$this->assertSame(FormFieldLibrary::class, $morphMap['form_field_library']);
|
||||
}
|
||||
|
||||
public function test_parameters_roundtrip_through_json_cast(): void
|
||||
{
|
||||
$org = Organisation::factory()->create();
|
||||
$schema = FormSchema::factory()->create(['organisation_id' => $org->id]);
|
||||
$field = FormField::factory()->create(['form_schema_id' => $schema->id]);
|
||||
|
||||
$rule = FormFieldValidationRule::factory()
|
||||
->forField($field)
|
||||
->ofType(FormFieldValidationRuleType::AllowedMimeTypes, [
|
||||
'mime_types' => ['image/jpeg', 'image/png'],
|
||||
])
|
||||
->create();
|
||||
|
||||
$fresh = $rule->fresh();
|
||||
$this->assertSame(
|
||||
['mime_types' => ['image/jpeg', 'image/png']],
|
||||
$fresh->parameters,
|
||||
);
|
||||
$this->assertSame(
|
||||
FormFieldValidationRuleType::AllowedMimeTypes,
|
||||
$fresh->rule_type,
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Tests\Feature\FormBuilder\ValidationRules;
|
||||
|
||||
use App\Models\FormBuilder\FormField;
|
||||
use App\Models\FormBuilder\FormFieldLibrary;
|
||||
use App\Models\FormBuilder\FormFieldValidationRule;
|
||||
use App\Models\FormBuilder\FormSchema;
|
||||
use App\Models\Organisation;
|
||||
use App\Models\Scopes\FormFieldValidationRuleScope;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Routing\Route;
|
||||
use Tests\TestCase;
|
||||
|
||||
/**
|
||||
* Asserts `FormFieldValidationRuleScope` isolates rules per organisation
|
||||
* across both owner types (form_field and form_field_library) and that
|
||||
* the `withoutGlobalScope` escape hatch exposes cross-org rows. Sibling
|
||||
* coverage to `FormFieldBindingScopeTest`.
|
||||
*/
|
||||
final class FormFieldValidationRuleScopeTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
public function test_scope_isolates_rules_per_organisation_for_both_owner_types(): void
|
||||
{
|
||||
[$orgA, $fieldA, $libraryA] = $this->seedOrgWithRules();
|
||||
[$orgB, $fieldB, $libraryB] = $this->seedOrgWithRules();
|
||||
|
||||
$this->withOrgRoute($orgA);
|
||||
$ownerIdsA = FormFieldValidationRule::query()->pluck('owner_id')->sort()->values()->all();
|
||||
$expectedA = collect([$fieldA->id, $libraryA->id])->sort()->values()->all();
|
||||
$this->assertSame($expectedA, $ownerIdsA);
|
||||
|
||||
$this->withOrgRoute($orgB);
|
||||
$ownerIdsB = FormFieldValidationRule::query()->pluck('owner_id')->sort()->values()->all();
|
||||
$expectedB = collect([$fieldB->id, $libraryB->id])->sort()->values()->all();
|
||||
$this->assertSame($expectedB, $ownerIdsB);
|
||||
}
|
||||
|
||||
public function test_without_global_scope_exposes_cross_org(): void
|
||||
{
|
||||
[$orgA, , ] = $this->seedOrgWithRules();
|
||||
$this->seedOrgWithRules();
|
||||
|
||||
$this->withOrgRoute($orgA);
|
||||
|
||||
$this->assertSame(
|
||||
4,
|
||||
FormFieldValidationRule::query()
|
||||
->withoutGlobalScope(FormFieldValidationRuleScope::class)
|
||||
->count(),
|
||||
);
|
||||
$this->assertSame(2, FormFieldValidationRule::query()->count());
|
||||
}
|
||||
|
||||
/** @return array{0:Organisation,1:FormField,2:FormFieldLibrary} */
|
||||
private function seedOrgWithRules(): array
|
||||
{
|
||||
$org = Organisation::factory()->create();
|
||||
$schema = FormSchema::factory()->create(['organisation_id' => $org->id]);
|
||||
$field = FormField::factory()->create(['form_schema_id' => $schema->id]);
|
||||
$library = FormFieldLibrary::factory()->create(['organisation_id' => $org->id]);
|
||||
|
||||
FormFieldValidationRule::factory()->forField($field)->create();
|
||||
FormFieldValidationRule::factory()->forLibrary($library)->create();
|
||||
|
||||
return [$org, $field, $library];
|
||||
}
|
||||
|
||||
private function withOrgRoute(Organisation $org): void
|
||||
{
|
||||
$route = new Route(['GET'], '/_test', static fn () => null);
|
||||
$route->bind(request());
|
||||
$route->setParameter('organisation', $org);
|
||||
request()->setRouteResolver(static fn () => $route);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user