feat(form-builder): form_field_configs relational table + non-validation key split + drop validation_rules JSON columns

This commit is contained in:
2026-04-24 22:42:35 +02:00
parent 9d2758a42c
commit d494478c08
31 changed files with 1233 additions and 60 deletions

View File

@@ -33,10 +33,11 @@ final class FormFieldBindingMigrationTest extends TestCase
public function test_forward_migrations_backfill_rows_from_both_json_sources(): void
{
// Roll back, newest first: backfill_form_field_validation_rules
// → create_form_field_validation_rules_table (both WS-5b commit 1+2)
// → drop_binding_json_columns → create_form_field_bindings.
$this->artisan('migrate:rollback', ['--step' => 4])->assertSuccessful();
// Roll back to pre-WS-5a state: 5 WS-5b migrations
// (drop-validation-cols, configs-backfill, create-configs,
// validation-rules-backfill, create-validation-rules) +
// 2 WS-5a migrations (drop-binding-cols, create-bindings) = 7.
$this->artisan('migrate:rollback', ['--step' => 7])->assertSuccessful();
$this->assertFalse(Schema::hasTable('form_field_bindings'));
$this->assertTrue(Schema::hasColumn('form_fields', 'binding'));
$this->assertTrue(Schema::hasColumn('form_field_library', 'default_binding'));
@@ -97,23 +98,24 @@ final class FormFieldBindingMigrationTest extends TestCase
public function test_rollback_reconstructs_json_and_drops_table(): void
{
// Walk back the full WS-5b + WS-5a stack: backfill (validation rules)
// → create (validation rules table) → drop (binding columns) →
// create (bindings table).
$this->artisan('migrate:rollback', ['--step' => 4])->assertSuccessful();
// Walk back the full WS-5b + WS-5a stack (7 migrations).
$this->artisan('migrate:rollback', ['--step' => 7])->assertSuccessful();
[$fieldAId, , ] = $this->seedFieldsWithBindingJson();
[$libAId, ] = $this->seedLibraryWithBindingJson();
$this->artisan('migrate')->assertSuccessful();
// Fully-forward state: columns gone, rows in form_field_bindings.
// Fully-forward state: binding columns gone, rows in form_field_bindings.
$this->assertFalse(Schema::hasColumn('form_fields', 'binding'));
$this->assertSame(5, DB::table('form_field_bindings')->count());
// Step back over the two WS-5b migrations → restores the pre-WS-5b
// state (validation-rules table gone; binding contract intact).
$this->artisan('migrate:rollback', ['--step' => 2])->assertSuccessful();
// Step back over all five WS-5b migrations in one go → restores the
// pre-WS-5b state (validation-rules and configs tables gone,
// validation_rules JSON columns reappear on source tables; binding
// contract intact).
$this->artisan('migrate:rollback', ['--step' => 5])->assertSuccessful();
$this->assertFalse(Schema::hasTable('form_field_validation_rules'));
$this->assertFalse(Schema::hasTable('form_field_configs'));
$this->assertTrue(Schema::hasTable('form_field_bindings'));
// Step back over drop_binding_json_columns → columns reappear empty.

View File

@@ -0,0 +1,112 @@
<?php
declare(strict_types=1);
namespace Tests\Feature\FormBuilder\Configs;
use App\Models\FormBuilder\FormSchema;
use App\Models\Organisation;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\Str;
use Tests\TestCase;
/**
* WS-5b commit 5 verifies the configs backfill + column drop pair.
*
* - `tag_categories` + `storage_disk` keys in the pre-WS-5b JSON bag
* become rows in `form_field_configs` (not in
* `form_field_validation_rules`).
* - After the full forward-migration, `form_fields.validation_rules`
* and `form_field_library.validation_rules` columns are gone.
*/
final class FormFieldConfigBackfillAndDropTest extends TestCase
{
use RefreshDatabase;
public function test_backfill_translates_tag_categories_and_storage_disk(): void
{
// Roll back 5 WS-5b migrations to get the pre-WS-5b state where
// the JSON column still exists on form_fields / form_field_library.
$this->artisan('migrate:rollback', ['--step' => 5])->assertSuccessful();
$this->assertTrue(Schema::hasColumn('form_fields', 'validation_rules'));
$fieldId = $this->seedField([
'field_type' => 'TAG_PICKER',
'validation_rules' => [
'tag_categories' => ['Veiligheid', 'Horeca'],
'storage_disk' => 's3',
],
]);
$this->artisan('migrate')->assertSuccessful();
$rows = DB::table('form_field_configs')
->where('owner_id', $fieldId)
->get()->keyBy('config_type');
$this->assertTrue($rows->has('tag_categories'));
$this->assertSame(
['Veiligheid', 'Horeca'],
json_decode((string) $rows['tag_categories']->parameters, true)['categories'],
);
$this->assertTrue($rows->has('storage_disk'));
$this->assertSame(
's3',
json_decode((string) $rows['storage_disk']->parameters, true)['disk'],
);
}
public function test_validation_rules_json_columns_are_dropped_after_migrations(): void
{
// Default state after RefreshDatabase: full migration applied.
$this->assertFalse(Schema::hasColumn('form_fields', 'validation_rules'));
$this->assertFalse(Schema::hasColumn('form_field_library', 'validation_rules'));
}
public function test_cascade_observer_cleans_up_configs_on_owner_delete(): void
{
// Integration-level: confirms the renamed cascade observer covers
// the configs table too.
$org = Organisation::factory()->create();
$schema = FormSchema::factory()->create(['organisation_id' => $org->id]);
$field = \App\Models\FormBuilder\FormField::factory()->create(['form_schema_id' => $schema->id]);
\App\Models\FormBuilder\FormFieldConfig::factory()->forField($field)->create();
$this->assertSame(1, \App\Models\FormBuilder\FormFieldConfig::query()
->withoutGlobalScopes()
->where('owner_id', $field->id)
->count());
$field->delete();
$this->assertSame(0, \App\Models\FormBuilder\FormFieldConfig::query()
->withoutGlobalScopes()
->where('owner_id', $field->id)
->count());
}
/** @param array<string, mixed> $attrs */
private function seedField(array $attrs): string
{
$org = Organisation::factory()->create();
$schema = FormSchema::factory()->create(['organisation_id' => $org->id]);
$id = (string) Str::ulid();
DB::table('form_fields')->insert([[
'id' => $id,
'form_schema_id' => $schema->id,
'field_type' => $attrs['field_type'],
'slug' => 'f-'.Str::lower(Str::random(4)),
'label' => 'field',
'validation_rules' => json_encode($attrs['validation_rules']),
'value_storage_hint' => 'json',
'sort_order' => 0,
'created_at' => now(),
'updated_at' => now(),
]]);
return $id;
}
}

View File

@@ -0,0 +1,184 @@
<?php
declare(strict_types=1);
namespace Tests\Feature\FormBuilder\Configs;
use App\Enums\FormBuilder\FormFieldConfigType;
use App\Exceptions\FormBuilder\UnknownValidationRuleTypeException;
use App\Models\FormBuilder\FormField;
use App\Models\FormBuilder\FormFieldConfig;
use App\Models\FormBuilder\FormFieldLibrary;
use App\Models\FormBuilder\FormSchema;
use App\Models\Organisation;
use App\Models\Scopes\FormFieldConfigScope;
use App\Services\FormBuilder\FormFieldConfigService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Routing\Route;
use Spatie\Activitylog\Models\Activity;
use Tests\TestCase;
/**
* Consolidated coverage for `form_field_configs` relation round-trip,
* scope isolation, cascade, and service-layer contract (replace, copy,
* toJsonShape). Mirrors the structure of the validation-rules test suite.
*/
final class FormFieldConfigServiceAndScopeTest extends TestCase
{
use RefreshDatabase;
public function test_field_morph_many_configs_and_owner_morphto_roundtrip(): void
{
$org = Organisation::factory()->create();
$schema = FormSchema::factory()->create(['organisation_id' => $org->id]);
$field = FormField::factory()->create(['form_schema_id' => $schema->id]);
FormFieldConfig::factory()->forField($field)
->ofType(FormFieldConfigType::TagCategories, ['categories' => ['Veiligheid']])->create();
FormFieldConfig::factory()->forField($field)
->ofType(FormFieldConfigType::StorageDisk, ['disk' => 'local'])->create();
$configs = $field->fresh()->configs;
$this->assertCount(2, $configs);
$first = $configs->firstWhere('config_type', FormFieldConfigType::TagCategories);
$this->assertSame(FormField::class, $first->fresh()->owner::class);
}
public function test_scope_isolates_configs_per_organisation_both_owner_types(): void
{
[$orgA, $fieldA, $libraryA] = $this->seedOrgWithConfigs();
[$orgB, $fieldB, $libraryB] = $this->seedOrgWithConfigs();
$this->withOrgRoute($orgA);
$ids = FormFieldConfig::query()->pluck('owner_id')->sort()->values()->all();
$expected = collect([$fieldA->id, $libraryA->id])->sort()->values()->all();
$this->assertSame($expected, $ids);
// Escape hatch.
$this->assertSame(
4,
FormFieldConfig::query()->withoutGlobalScope(FormFieldConfigScope::class)->count(),
);
$this->assertSame(2, FormFieldConfig::query()->count());
}
public function test_cascade_deletes_configs_on_owner_delete(): void
{
$org = Organisation::factory()->create();
$schema = FormSchema::factory()->create(['organisation_id' => $org->id]);
$field = FormField::factory()->create(['form_schema_id' => $schema->id]);
FormFieldConfig::factory()->forField($field)->create();
$this->assertSame(1, FormFieldConfig::query()->withoutGlobalScopes()
->where('owner_id', $field->id)->count());
$field->delete();
$this->assertSame(0, FormFieldConfig::query()->withoutGlobalScopes()
->where('owner_id', $field->id)->count());
}
public function test_replace_configs_enum_and_parameter_shape_enforced(): void
{
$org = Organisation::factory()->create();
$schema = FormSchema::factory()->create(['organisation_id' => $org->id]);
$field = FormField::factory()->create(['form_schema_id' => $schema->id]);
$service = app(FormFieldConfigService::class);
$this->expectException(UnknownValidationRuleTypeException::class);
$service->replaceConfigs($field, [
['config_type' => 'not_a_thing', 'parameters' => []],
]);
}
public function test_replace_configs_rejects_bad_tag_categories_shape(): void
{
$org = Organisation::factory()->create();
$schema = FormSchema::factory()->create(['organisation_id' => $org->id]);
$field = FormField::factory()->create(['form_schema_id' => $schema->id]);
$service = app(FormFieldConfigService::class);
$this->expectException(UnknownValidationRuleTypeException::class);
$service->replaceConfigs($field, [
['config_type' => 'tag_categories', 'parameters' => ['categories' => 'not-an-array']],
]);
}
public function test_replace_configs_emits_activity_log_on_field_only(): 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]);
$service = app(FormFieldConfigService::class);
$service->replaceConfigs($field, [
['config_type' => 'storage_disk', 'parameters' => ['disk' => 's3']],
]);
$service->replaceConfigs($library, [
['config_type' => 'storage_disk', 'parameters' => ['disk' => 'local']],
]);
$this->assertNotNull(Activity::query()
->where('subject_type', 'form_field')
->where('subject_id', $field->id)
->where('description', 'field.configs_replaced')
->first());
$this->assertNull(Activity::query()
->where('subject_type', 'form_field_library')
->where('description', 'field.configs_replaced')
->first());
}
public function test_copy_configs_clones_every_row(): void
{
$org = Organisation::factory()->create();
$library = FormFieldLibrary::factory()->create(['organisation_id' => $org->id]);
FormFieldConfig::factory()->forLibrary($library)
->ofType(FormFieldConfigType::TagCategories, ['categories' => ['Horeca']])->create();
FormFieldConfig::factory()->forLibrary($library)
->ofType(FormFieldConfigType::StorageDisk, ['disk' => 'local'])->create();
$schema = FormSchema::factory()->create(['organisation_id' => $org->id]);
$field = FormField::factory()->create(['form_schema_id' => $schema->id]);
app(FormFieldConfigService::class)->copyConfigs($library, $field);
$configs = FormFieldConfig::query()->where('owner_id', $field->id)->get();
$this->assertCount(2, $configs);
}
public function test_to_json_shape_nested_object_envelope(): void
{
$org = Organisation::factory()->create();
$schema = FormSchema::factory()->create(['organisation_id' => $org->id]);
$field = FormField::factory()->create(['form_schema_id' => $schema->id]);
FormFieldConfig::factory()->forField($field)
->ofType(FormFieldConfigType::TagCategories, ['categories' => ['Veiligheid']])->create();
FormFieldConfig::factory()->forField($field)
->ofType(FormFieldConfigType::StorageDisk, ['disk' => 's3'])->create();
$shape = app(FormFieldConfigService::class)->toJsonShape($field->fresh()->configs);
$this->assertSame(['categories' => ['Veiligheid']], $shape['tag_categories']);
$this->assertSame(['disk' => 's3'], $shape['storage_disk']);
}
/** @return array{0:Organisation,1:FormField,2:FormFieldLibrary} */
private function seedOrgWithConfigs(): 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]);
FormFieldConfig::factory()->forField($field)->create();
FormFieldConfig::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);
}
}

View File

@@ -34,7 +34,11 @@ final class FormFieldValidationRuleBackfillTest extends TestCase
// Roll back: backfill + create-table. Brings us to a state where
// form_fields.validation_rules exists but form_field_validation_rules
// table does not.
$this->artisan('migrate:rollback', ['--step' => 2])->assertSuccessful();
// Roll back all WS-5b migrations to reach the pre-WS-5b state
// (validation_rules JSON column present; no relational tables for
// WS-5b). Step count: drop-cols + configs-backfill + create-configs
// + validation-rules-backfill + create-validation-rules = 5.
$this->artisan('migrate:rollback', ['--step' => 5])->assertSuccessful();
$this->assertFalse(Schema::hasTable('form_field_validation_rules'));
$this->assertTrue(Schema::hasColumn('form_fields', 'validation_rules'));
@@ -91,7 +95,11 @@ final class FormFieldValidationRuleBackfillTest extends TestCase
public function test_tag_categories_and_storage_disk_skipped_for_commit_5(): void
{
$this->artisan('migrate:rollback', ['--step' => 2])->assertSuccessful();
// Roll back all WS-5b migrations to reach the pre-WS-5b state
// (validation_rules JSON column present; no relational tables for
// WS-5b). Step count: drop-cols + configs-backfill + create-configs
// + validation-rules-backfill + create-validation-rules = 5.
$this->artisan('migrate:rollback', ['--step' => 5])->assertSuccessful();
$fieldId = $this->seedFieldWithJson([
'field_type' => 'TAG_PICKER',
@@ -111,7 +119,11 @@ final class FormFieldValidationRuleBackfillTest extends TestCase
public function test_required_and_unique_skipped_with_warn(): void
{
$this->artisan('migrate:rollback', ['--step' => 2])->assertSuccessful();
// Roll back all WS-5b migrations to reach the pre-WS-5b state
// (validation_rules JSON column present; no relational tables for
// WS-5b). Step count: drop-cols + configs-backfill + create-configs
// + validation-rules-backfill + create-validation-rules = 5.
$this->artisan('migrate:rollback', ['--step' => 5])->assertSuccessful();
$fieldId = $this->seedFieldWithJson([
'field_type' => 'TEXT',
@@ -134,7 +146,11 @@ final class FormFieldValidationRuleBackfillTest extends TestCase
public function test_unknown_top_level_key_fails_migration(): void
{
$this->artisan('migrate:rollback', ['--step' => 2])->assertSuccessful();
// Roll back all WS-5b migrations to reach the pre-WS-5b state
// (validation_rules JSON column present; no relational tables for
// WS-5b). Step count: drop-cols + configs-backfill + create-configs
// + validation-rules-backfill + create-validation-rules = 5.
$this->artisan('migrate:rollback', ['--step' => 5])->assertSuccessful();
$this->seedFieldWithJson([
'field_type' => 'TEXT',
@@ -147,7 +163,11 @@ final class FormFieldValidationRuleBackfillTest extends TestCase
public function test_unmapped_field_type_for_min_max_fails_migration(): void
{
$this->artisan('migrate:rollback', ['--step' => 2])->assertSuccessful();
// Roll back all WS-5b migrations to reach the pre-WS-5b state
// (validation_rules JSON column present; no relational tables for
// WS-5b). Step count: drop-cols + configs-backfill + create-configs
// + validation-rules-backfill + create-validation-rules = 5.
$this->artisan('migrate:rollback', ['--step' => 5])->assertSuccessful();
$this->seedFieldWithJson([
'field_type' => 'BOOLEAN',
@@ -158,22 +178,34 @@ final class FormFieldValidationRuleBackfillTest extends TestCase
$this->artisan('migrate');
}
public function test_rollback_reconstructs_canonical_json_on_source_tables(): void
public function test_full_wsb_rollback_reconstructs_source_state(): void
{
$this->artisan('migrate:rollback', ['--step' => 2])->assertSuccessful();
// Architect contract: "the forward+back pair is safe when run as a
// unit; a partial 'rollback this migration but not its create-table
// sibling' state is not supported." This test exercises the
// full-back-then-full-forward cycle — rolling back all WS-5b
// migrations restores the pre-WS-5b state (columns present on
// source tables; validation rules relational table gone).
$this->artisan('migrate:rollback', ['--step' => 5])->assertSuccessful();
[$numberId] = $this->seedFields();
$this->artisan('migrate')->assertSuccessful();
// Post-migration: rows exist, column dropped.
$this->assertSame(
2,
DB::table('form_field_validation_rules')
->where('owner_id', $numberId)
->count(),
);
$this->assertFalse(Schema::hasColumn('form_fields', 'validation_rules'));
$this->artisan('migrate:rollback', ['--step' => 1])->assertSuccessful();
// Only the backfill rolled back — the create-table migration still
// applied, so rows remain accessible (until we step back once more).
$this->assertTrue(Schema::hasTable('form_field_validation_rules'));
// Roll back WS-5b fully → column reappears and carries canonical JSON
// reconstructed from the relational rows.
$this->artisan('migrate:rollback', ['--step' => 5])->assertSuccessful();
$this->assertTrue(Schema::hasColumn('form_fields', 'validation_rules'));
$field = DB::table('form_fields')->where('id', $numberId)->first();
$decoded = json_decode((string) $field->validation_rules, true);
// Rollback reconstructs using canonical keys — the legacy `min`/`max`
// are intentionally NOT resurrected (post-rename semantic).
$this->assertSame(16, $decoded['min_value']);
$this->assertSame(99, $decoded['max_value']);
}