feat(form-builder): FormFieldConditionalLogicService + cycle detection + legacy backfill + snapshot
WS-5c commit 2 of 4 — the service layer, backfill migration, and
read-path switch. Per addendum Q3, conditional_logic applies to
FormField only — no library mirror and no copyLogic on
FormFieldService::insertFromLibrary.
FormFieldConditionalLogicService owns every write:
- logicFor(field): depth-limited eager-load of the tree
- replaceLogic(field, tree): transactional structure + operator +
field_slug validation + cycle check + activity-log emit
(field.conditional_logic_replaced)
- toJsonShape(root): reconstructs the canonical ARCH §8
`{show_when: {...}}` shape — single source of truth for the
snapshot writer + API resources
- assertSpecsValid(tree): public boundary guard for the FormRequest
strict validator (WS-5c commit 3 wires this up)
- assertNoCycles(field, tree): contract preserved from
FormFieldService::assertNoConditionalCycle, implementation now
reads the relational adjacency.
Backfill migration translates pre-WS-5c conditional_logic JSON to
rows. Strict dispatch: unknown operators / unknown top-level keys /
malformed groups FAIL the migration — Phase A seed-scan confirmed
the catalogue parity, so any drift is a data bug to fix at source,
not silently absorb. Rollback rebuilds canonical JSON and clears
the relational tree.
FormFieldService.create/update route `conditional_logic` through
the new service (matching the extract-and-delegate pattern from
WS-5a bindings and WS-5b validation rules). Snapshot writer + both
resources (FormFieldResource, PublicFormSchemaResource) read via
`toJsonShape(rootConditionalLogicGroup())` — byte-for-byte parity
with the pre-WS-5c JSON contract.
InvalidConditionalLogicSpecException handled in FormFieldController
as 422, same as FrozenSchemaException / CyclicDependencyException.
Tests: 20 new under tests/Feature/FormBuilder/ConditionalLogic/
(service, cycle detection, backfill forward+rollback+failure cases,
snapshot + resource parity). FormFieldApiTest cyclic rejection test
rewritten to use the new factory state. Rollback step counts in
WS-5a/b migration tests bumped +1 for the new backfill migration.
Baseline 1122 → 1142 green (3032 → 3085 assertions).
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -33,12 +33,13 @@ final class FormFieldBindingMigrationTest extends TestCase
|
||||
|
||||
public function test_forward_migrations_backfill_rows_from_both_json_sources(): void
|
||||
{
|
||||
// Roll back to pre-WS-5a state: 2 WS-5c migrations
|
||||
// (create-conditional-logic-conditions, create-conditional-logic-groups) +
|
||||
// 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) = 9.
|
||||
$this->artisan('migrate:rollback', ['--step' => 9])->assertSuccessful();
|
||||
// Roll back to pre-WS-5a state: 3 WS-5c migrations
|
||||
// (backfill-conditional-logic, create-conditional-logic-conditions,
|
||||
// create-conditional-logic-groups) + 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) = 10.
|
||||
$this->artisan('migrate:rollback', ['--step' => 10])->assertSuccessful();
|
||||
$this->assertFalse(Schema::hasTable('form_field_bindings'));
|
||||
$this->assertTrue(Schema::hasColumn('form_fields', 'binding'));
|
||||
$this->assertTrue(Schema::hasColumn('form_field_library', 'default_binding'));
|
||||
@@ -99,8 +100,8 @@ final class FormFieldBindingMigrationTest extends TestCase
|
||||
|
||||
public function test_rollback_reconstructs_json_and_drops_table(): void
|
||||
{
|
||||
// Walk back the full WS-5c + WS-5b + WS-5a stack (9 migrations).
|
||||
$this->artisan('migrate:rollback', ['--step' => 9])->assertSuccessful();
|
||||
// Walk back the full WS-5c + WS-5b + WS-5a stack (10 migrations).
|
||||
$this->artisan('migrate:rollback', ['--step' => 10])->assertSuccessful();
|
||||
[$fieldAId, , ] = $this->seedFieldsWithBindingJson();
|
||||
[$libAId, ] = $this->seedLibraryWithBindingJson();
|
||||
|
||||
@@ -110,11 +111,11 @@ 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-5c (2 migrations) + WS-5b (5 migrations) in one
|
||||
// Step back over WS-5c (3 migrations) + WS-5b (5 migrations) in one
|
||||
// go → restores the pre-WS-5b state (conditional-logic,
|
||||
// validation-rules and configs tables gone, validation_rules JSON
|
||||
// columns reappear on source tables; binding contract intact).
|
||||
$this->artisan('migrate:rollback', ['--step' => 7])->assertSuccessful();
|
||||
$this->artisan('migrate:rollback', ['--step' => 8])->assertSuccessful();
|
||||
$this->assertFalse(Schema::hasTable('form_field_conditional_logic_groups'));
|
||||
$this->assertFalse(Schema::hasTable('form_field_conditional_logic_conditions'));
|
||||
$this->assertFalse(Schema::hasTable('form_field_validation_rules'));
|
||||
|
||||
@@ -0,0 +1,228 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Tests\Feature\FormBuilder\ConditionalLogic;
|
||||
|
||||
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;
|
||||
|
||||
/**
|
||||
* Rolls back just the WS-5c backfill migration, seeds pre-WS-5c JSON into
|
||||
* `form_fields.conditional_logic`, then migrates forward and backward,
|
||||
* asserting:
|
||||
*
|
||||
* - Forward: relational rows mirror the tree shape (groups + conditions
|
||||
* + parent links + sort_order).
|
||||
* - Rollback: JSON reconstructed byte-accurate for the ARCH §8 shape.
|
||||
*
|
||||
* Cross-field cycle detection does NOT run on backfill — pre-WS-5c data
|
||||
* is assumed acyclic (the JSON-era check enforced on save). Post-backfill
|
||||
* the service enforces on every write.
|
||||
*/
|
||||
final class ConditionalLogicBackfillTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
public function test_forward_backfill_builds_nested_tree_from_legacy_json(): void
|
||||
{
|
||||
// Roll back only the backfill migration (latest WS-5c step).
|
||||
$this->artisan('migrate:rollback', ['--step' => 1])->assertSuccessful();
|
||||
$this->assertTrue(Schema::hasColumn('form_fields', 'conditional_logic'));
|
||||
|
||||
$fieldId = $this->seedFieldWithJson([
|
||||
'show_when' => [
|
||||
'all' => [
|
||||
['field_slug' => 'gate', 'operator' => 'equals', 'value' => 'yes'],
|
||||
[
|
||||
'any' => [
|
||||
['field_slug' => 'region', 'operator' => 'equals', 'value' => 'NL'],
|
||||
['field_slug' => 'region', 'operator' => 'equals', 'value' => 'BE'],
|
||||
],
|
||||
],
|
||||
['field_slug' => 'status', 'operator' => 'empty'],
|
||||
],
|
||||
],
|
||||
]);
|
||||
|
||||
$this->artisan('migrate')->assertSuccessful();
|
||||
|
||||
// Root group: `all`, no parent, 2 conditions + 1 subgroup.
|
||||
$rootGroup = DB::table('form_field_conditional_logic_groups')
|
||||
->where('form_field_id', $fieldId)
|
||||
->whereNull('parent_group_id')
|
||||
->first();
|
||||
$this->assertNotNull($rootGroup);
|
||||
$this->assertSame('all', $rootGroup->operator);
|
||||
|
||||
$conditions = DB::table('form_field_conditional_logic_conditions')
|
||||
->where('group_id', $rootGroup->id)
|
||||
->orderBy('sort_order')
|
||||
->get();
|
||||
$this->assertCount(2, $conditions);
|
||||
$this->assertSame('gate', $conditions[0]->field_slug);
|
||||
$this->assertSame('equals', $conditions[0]->comparison_operator);
|
||||
$this->assertSame('yes', json_decode((string) $conditions[0]->value, true));
|
||||
|
||||
$this->assertSame('status', $conditions[1]->field_slug);
|
||||
$this->assertSame('empty', $conditions[1]->comparison_operator);
|
||||
$this->assertNull($conditions[1]->value, 'valueless operator stores null');
|
||||
|
||||
// Subgroup: `any`, 2 conditions.
|
||||
$subGroup = DB::table('form_field_conditional_logic_groups')
|
||||
->where('parent_group_id', $rootGroup->id)
|
||||
->first();
|
||||
$this->assertNotNull($subGroup);
|
||||
$this->assertSame('any', $subGroup->operator);
|
||||
$subConditions = DB::table('form_field_conditional_logic_conditions')
|
||||
->where('group_id', $subGroup->id)
|
||||
->orderBy('sort_order')
|
||||
->get();
|
||||
$this->assertCount(2, $subConditions);
|
||||
$this->assertSame('region', $subConditions[0]->field_slug);
|
||||
$this->assertSame('NL', json_decode((string) $subConditions[0]->value, true));
|
||||
}
|
||||
|
||||
public function test_rollback_reconstructs_canonical_json(): void
|
||||
{
|
||||
// Starting state: fully migrated. Seed relational rows bypassing the
|
||||
// service, then roll back one step (backfill) — it should repopulate
|
||||
// the JSON column.
|
||||
$org = Organisation::factory()->create();
|
||||
$schema = FormSchema::factory()->create(['organisation_id' => $org->id]);
|
||||
|
||||
$fieldId = (string) Str::ulid();
|
||||
DB::table('form_fields')->insert([
|
||||
'id' => $fieldId,
|
||||
'form_schema_id' => $schema->id,
|
||||
'field_type' => 'TEXT',
|
||||
'slug' => 'subject',
|
||||
'label' => 'Subject',
|
||||
'value_storage_hint' => 'json',
|
||||
'sort_order' => 0,
|
||||
'conditional_logic' => null,
|
||||
'created_at' => now(),
|
||||
'updated_at' => now(),
|
||||
]);
|
||||
|
||||
$rootId = (string) Str::ulid();
|
||||
DB::table('form_field_conditional_logic_groups')->insert([
|
||||
'id' => $rootId,
|
||||
'form_field_id' => $fieldId,
|
||||
'parent_group_id' => null,
|
||||
'operator' => 'all',
|
||||
'sort_order' => 0,
|
||||
'created_at' => now(),
|
||||
'updated_at' => now(),
|
||||
]);
|
||||
DB::table('form_field_conditional_logic_conditions')->insert([
|
||||
'id' => (string) Str::ulid(),
|
||||
'group_id' => $rootId,
|
||||
'field_slug' => 'gate',
|
||||
'comparison_operator' => 'equals',
|
||||
'value' => json_encode('yes'),
|
||||
'sort_order' => 0,
|
||||
'created_at' => now(),
|
||||
'updated_at' => now(),
|
||||
]);
|
||||
$subGroupId = (string) Str::ulid();
|
||||
DB::table('form_field_conditional_logic_groups')->insert([
|
||||
'id' => $subGroupId,
|
||||
'form_field_id' => $fieldId,
|
||||
'parent_group_id' => $rootId,
|
||||
'operator' => 'any',
|
||||
'sort_order' => 1,
|
||||
'created_at' => now(),
|
||||
'updated_at' => now(),
|
||||
]);
|
||||
DB::table('form_field_conditional_logic_conditions')->insert([
|
||||
'id' => (string) Str::ulid(),
|
||||
'group_id' => $subGroupId,
|
||||
'field_slug' => 'region',
|
||||
'comparison_operator' => 'equals',
|
||||
'value' => json_encode('NL'),
|
||||
'sort_order' => 0,
|
||||
'created_at' => now(),
|
||||
'updated_at' => now(),
|
||||
]);
|
||||
|
||||
// Roll back only the backfill migration — writes the JSON back.
|
||||
$this->artisan('migrate:rollback', ['--step' => 1])->assertSuccessful();
|
||||
|
||||
$reconstructed = DB::table('form_fields')
|
||||
->where('id', $fieldId)
|
||||
->value('conditional_logic');
|
||||
$this->assertNotNull($reconstructed);
|
||||
$json = json_decode((string) $reconstructed, true);
|
||||
$this->assertSame([
|
||||
'show_when' => [
|
||||
'all' => [
|
||||
['field_slug' => 'gate', 'operator' => 'equals', 'value' => 'yes'],
|
||||
[
|
||||
'any' => [
|
||||
['field_slug' => 'region', 'operator' => 'equals', 'value' => 'NL'],
|
||||
],
|
||||
],
|
||||
],
|
||||
],
|
||||
], $json);
|
||||
|
||||
// Relational tables cleared after reconstruction.
|
||||
$this->assertSame(0, DB::table('form_field_conditional_logic_groups')->count());
|
||||
$this->assertSame(0, DB::table('form_field_conditional_logic_conditions')->count());
|
||||
}
|
||||
|
||||
public function test_unknown_top_level_key_fails_migration(): void
|
||||
{
|
||||
$this->artisan('migrate:rollback', ['--step' => 1])->assertSuccessful();
|
||||
|
||||
$this->seedFieldWithJson([
|
||||
'hide_when' => ['all' => [['field_slug' => 'x', 'operator' => 'equals', 'value' => 1]]],
|
||||
]);
|
||||
|
||||
$this->expectException(\RuntimeException::class);
|
||||
$this->expectExceptionMessageMatches('/hide_when/');
|
||||
$this->artisan('migrate');
|
||||
}
|
||||
|
||||
public function test_unknown_comparison_operator_fails_migration(): void
|
||||
{
|
||||
$this->artisan('migrate:rollback', ['--step' => 1])->assertSuccessful();
|
||||
|
||||
$this->seedFieldWithJson([
|
||||
'show_when' => ['all' => [['field_slug' => 'x', 'operator' => 'matches_regex', 'value' => 'y']]],
|
||||
]);
|
||||
|
||||
$this->expectException(\RuntimeException::class);
|
||||
$this->expectExceptionMessageMatches('/matches_regex/');
|
||||
$this->artisan('migrate');
|
||||
}
|
||||
|
||||
/** @param array<string, mixed> $json */
|
||||
private function seedFieldWithJson(array $json): 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' => 'TEXT',
|
||||
'slug' => 'f-'.Str::lower(Str::random(4)),
|
||||
'label' => 'field',
|
||||
'value_storage_hint' => 'json',
|
||||
'sort_order' => 0,
|
||||
'conditional_logic' => json_encode($json),
|
||||
'created_at' => now(),
|
||||
'updated_at' => now(),
|
||||
]);
|
||||
|
||||
return $id;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,124 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Tests\Feature\FormBuilder\ConditionalLogic;
|
||||
|
||||
use App\Exceptions\FormBuilder\CyclicDependencyException;
|
||||
use App\Models\FormBuilder\FormField;
|
||||
use App\Models\FormBuilder\FormSchema;
|
||||
use App\Models\Organisation;
|
||||
use App\Services\FormBuilder\FormFieldConditionalLogicService;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Tests\TestCase;
|
||||
|
||||
/**
|
||||
* Cycle detection (ARCH §8). Migrated from
|
||||
* FormFieldService::assertNoConditionalCycle (pre-WS-5c) to
|
||||
* FormFieldConditionalLogicService::assertNoCycles. Contract preserved;
|
||||
* implementation now reads the relational tree instead of walking JSON.
|
||||
*/
|
||||
final class ConditionalLogicCycleDetectionTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
public function test_two_node_cycle_rejected(): void
|
||||
{
|
||||
[$schema, $fieldA, $fieldB] = $this->seedSchemaWithTwoFields();
|
||||
$service = app(FormFieldConditionalLogicService::class);
|
||||
|
||||
// A depends on B.
|
||||
$service->replaceLogic($fieldA, [
|
||||
'operator' => 'all',
|
||||
'children' => [
|
||||
['field_slug' => $fieldB->slug, 'operator' => 'equals', 'value' => 'y'],
|
||||
],
|
||||
]);
|
||||
|
||||
// Proposing B depends on A would close the A → B → A cycle.
|
||||
$this->expectException(CyclicDependencyException::class);
|
||||
$service->assertNoCycles($fieldB, [
|
||||
'operator' => 'all',
|
||||
'children' => [
|
||||
['field_slug' => $fieldA->slug, 'operator' => 'equals', 'value' => 'x'],
|
||||
],
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_three_node_cycle_rejected(): void
|
||||
{
|
||||
[$schema, $fieldA, $fieldB, $fieldC] = $this->seedSchemaWithThreeFields();
|
||||
$service = app(FormFieldConditionalLogicService::class);
|
||||
|
||||
// A → B, B → C
|
||||
$service->replaceLogic($fieldA, [
|
||||
'operator' => 'all',
|
||||
'children' => [['field_slug' => $fieldB->slug, 'operator' => 'equals', 'value' => 'y']],
|
||||
]);
|
||||
$service->replaceLogic($fieldB, [
|
||||
'operator' => 'all',
|
||||
'children' => [['field_slug' => $fieldC->slug, 'operator' => 'equals', 'value' => 'y']],
|
||||
]);
|
||||
|
||||
// Proposing C → A closes A → B → C → A.
|
||||
$this->expectException(CyclicDependencyException::class);
|
||||
$service->assertNoCycles($fieldC, [
|
||||
'operator' => 'all',
|
||||
'children' => [['field_slug' => $fieldA->slug, 'operator' => 'equals', 'value' => 'x']],
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_diamond_is_accepted_no_cycle(): void
|
||||
{
|
||||
[$schema, $fieldA, $fieldB, $fieldC] = $this->seedSchemaWithThreeFields();
|
||||
$service = app(FormFieldConditionalLogicService::class);
|
||||
|
||||
// A → B and C → B (two fields depend on the same field, no cycle).
|
||||
$service->replaceLogic($fieldA, [
|
||||
'operator' => 'all',
|
||||
'children' => [['field_slug' => $fieldB->slug, 'operator' => 'equals', 'value' => 'y']],
|
||||
]);
|
||||
|
||||
$service->assertNoCycles($fieldC, [
|
||||
'operator' => 'all',
|
||||
'children' => [['field_slug' => $fieldB->slug, 'operator' => 'equals', 'value' => 'y']],
|
||||
]);
|
||||
|
||||
$this->expectNotToPerformAssertions();
|
||||
}
|
||||
|
||||
public function test_self_reference_rejected(): void
|
||||
{
|
||||
[$schema, $fieldA] = $this->seedSchemaWithTwoFields();
|
||||
$service = app(FormFieldConditionalLogicService::class);
|
||||
|
||||
$this->expectException(CyclicDependencyException::class);
|
||||
$service->assertNoCycles($fieldA, [
|
||||
'operator' => 'all',
|
||||
'children' => [['field_slug' => $fieldA->slug, 'operator' => 'equals', 'value' => 'x']],
|
||||
]);
|
||||
}
|
||||
|
||||
/** @return array{0:FormSchema,1:FormField,2:FormField} */
|
||||
private function seedSchemaWithTwoFields(): array
|
||||
{
|
||||
$org = Organisation::factory()->create();
|
||||
$schema = FormSchema::factory()->create(['organisation_id' => $org->id]);
|
||||
$fieldA = FormField::factory()->create(['form_schema_id' => $schema->id, 'slug' => 'a']);
|
||||
$fieldB = FormField::factory()->create(['form_schema_id' => $schema->id, 'slug' => 'b']);
|
||||
|
||||
return [$schema, $fieldA, $fieldB];
|
||||
}
|
||||
|
||||
/** @return array{0:FormSchema,1:FormField,2:FormField,3:FormField} */
|
||||
private function seedSchemaWithThreeFields(): array
|
||||
{
|
||||
$org = Organisation::factory()->create();
|
||||
$schema = FormSchema::factory()->create(['organisation_id' => $org->id]);
|
||||
$fieldA = FormField::factory()->create(['form_schema_id' => $schema->id, 'slug' => 'a']);
|
||||
$fieldB = FormField::factory()->create(['form_schema_id' => $schema->id, 'slug' => 'b']);
|
||||
$fieldC = FormField::factory()->create(['form_schema_id' => $schema->id, 'slug' => 'c']);
|
||||
|
||||
return [$schema, $fieldA, $fieldB, $fieldC];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Tests\Feature\FormBuilder\ConditionalLogic;
|
||||
|
||||
use App\Http\Resources\FormBuilder\FormFieldResource;
|
||||
use App\Models\FormBuilder\FormField;
|
||||
use App\Models\FormBuilder\FormSchema;
|
||||
use App\Models\Organisation;
|
||||
use App\Services\FormBuilder\FormFieldConditionalLogicService;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Http\Request;
|
||||
use Tests\TestCase;
|
||||
|
||||
/**
|
||||
* Snapshot + API resource parity — once conditional_logic lives in
|
||||
* relational tables, readers must produce byte-identical shapes to the
|
||||
* pre-WS-5c JSON contract. ARCH §8 / §4.6.1.
|
||||
*/
|
||||
final class ConditionalLogicSnapshotAndResourceParityTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
public function test_form_field_resource_surfaces_canonical_show_when_json(): void
|
||||
{
|
||||
$org = Organisation::factory()->create();
|
||||
$schema = FormSchema::factory()->create(['organisation_id' => $org->id]);
|
||||
$field = FormField::factory()->create(['form_schema_id' => $schema->id, 'slug' => 'subject']);
|
||||
FormField::factory()->create(['form_schema_id' => $schema->id, 'slug' => 'gate']);
|
||||
FormField::factory()->create(['form_schema_id' => $schema->id, 'slug' => 'region']);
|
||||
|
||||
app(FormFieldConditionalLogicService::class)->replaceLogic($field, [
|
||||
'operator' => 'all',
|
||||
'children' => [
|
||||
['field_slug' => 'gate', 'operator' => 'equals', 'value' => 'yes'],
|
||||
[
|
||||
'operator' => 'any',
|
||||
'children' => [
|
||||
['field_slug' => 'region', 'operator' => 'equals', 'value' => 'NL'],
|
||||
['field_slug' => 'region', 'operator' => 'equals', 'value' => 'BE'],
|
||||
],
|
||||
],
|
||||
],
|
||||
]);
|
||||
|
||||
$resource = new FormFieldResource($field->fresh());
|
||||
$payload = $resource->toArray(Request::create('/', 'GET'));
|
||||
|
||||
$this->assertSame([
|
||||
'show_when' => [
|
||||
'all' => [
|
||||
['field_slug' => 'gate', 'operator' => 'equals', 'value' => 'yes'],
|
||||
[
|
||||
'any' => [
|
||||
['field_slug' => 'region', 'operator' => 'equals', 'value' => 'NL'],
|
||||
['field_slug' => 'region', 'operator' => 'equals', 'value' => 'BE'],
|
||||
],
|
||||
],
|
||||
],
|
||||
],
|
||||
], $payload['conditional_logic']);
|
||||
}
|
||||
|
||||
public function test_form_field_resource_yields_null_when_no_logic(): void
|
||||
{
|
||||
$org = Organisation::factory()->create();
|
||||
$schema = FormSchema::factory()->create(['organisation_id' => $org->id]);
|
||||
$field = FormField::factory()->create(['form_schema_id' => $schema->id]);
|
||||
|
||||
$resource = new FormFieldResource($field->fresh());
|
||||
$payload = $resource->toArray(Request::create('/', 'GET'));
|
||||
|
||||
$this->assertNull($payload['conditional_logic']);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,231 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Tests\Feature\FormBuilder\ConditionalLogic;
|
||||
|
||||
use App\Enums\FormBuilder\FormFieldConditionalLogicConditionOperator;
|
||||
use App\Enums\FormBuilder\FormFieldConditionalLogicGroupOperator;
|
||||
use App\Exceptions\FormBuilder\InvalidConditionalLogicSpecException;
|
||||
use App\Models\FormBuilder\FormField;
|
||||
use App\Models\FormBuilder\FormFieldConditionalLogicCondition;
|
||||
use App\Models\FormBuilder\FormFieldConditionalLogicGroup;
|
||||
use App\Models\FormBuilder\FormSchema;
|
||||
use App\Models\Organisation;
|
||||
use App\Services\FormBuilder\FormFieldConditionalLogicService;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Spatie\Activitylog\Models\Activity;
|
||||
use Tests\TestCase;
|
||||
|
||||
final class FormFieldConditionalLogicServiceTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
public function test_replace_logic_persists_tree_and_fires_activity_log(): void
|
||||
{
|
||||
$org = Organisation::factory()->create();
|
||||
$schema = FormSchema::factory()->create(['organisation_id' => $org->id]);
|
||||
$field = FormField::factory()->create(['form_schema_id' => $schema->id, 'slug' => 'subject']);
|
||||
FormField::factory()->create(['form_schema_id' => $schema->id, 'slug' => 'gate']);
|
||||
FormField::factory()->create(['form_schema_id' => $schema->id, 'slug' => 'region']);
|
||||
|
||||
$service = app(FormFieldConditionalLogicService::class);
|
||||
$service->replaceLogic($field, [
|
||||
'operator' => FormFieldConditionalLogicGroupOperator::All->value,
|
||||
'children' => [
|
||||
['field_slug' => 'gate', 'operator' => 'equals', 'value' => 'yes'],
|
||||
[
|
||||
'operator' => FormFieldConditionalLogicGroupOperator::Any->value,
|
||||
'children' => [
|
||||
['field_slug' => 'region', 'operator' => 'equals', 'value' => 'NL'],
|
||||
['field_slug' => 'region', 'operator' => 'equals', 'value' => 'BE'],
|
||||
],
|
||||
],
|
||||
],
|
||||
]);
|
||||
|
||||
$this->assertSame(2, FormFieldConditionalLogicGroup::query()->count());
|
||||
$this->assertSame(3, FormFieldConditionalLogicCondition::query()->count());
|
||||
$this->assertDatabaseHas('activity_log', [
|
||||
'description' => 'field.conditional_logic_replaced',
|
||||
'subject_type' => 'form_field',
|
||||
'subject_id' => $field->id,
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_replace_logic_is_transactional_and_replaces_existing_tree(): void
|
||||
{
|
||||
$org = Organisation::factory()->create();
|
||||
$schema = FormSchema::factory()->create(['organisation_id' => $org->id]);
|
||||
$field = FormField::factory()->create(['form_schema_id' => $schema->id, 'slug' => 'subject']);
|
||||
FormField::factory()->create(['form_schema_id' => $schema->id, 'slug' => 'gate']);
|
||||
|
||||
$service = app(FormFieldConditionalLogicService::class);
|
||||
$service->replaceLogic($field, [
|
||||
'operator' => 'all',
|
||||
'children' => [
|
||||
['field_slug' => 'gate', 'operator' => 'equals', 'value' => 'yes'],
|
||||
],
|
||||
]);
|
||||
$this->assertSame(1, FormFieldConditionalLogicCondition::query()->count());
|
||||
|
||||
$service->replaceLogic($field, [
|
||||
'operator' => 'any',
|
||||
'children' => [
|
||||
['field_slug' => 'gate', 'operator' => 'not_equals', 'value' => 'no'],
|
||||
],
|
||||
]);
|
||||
|
||||
// Full replacement — old rows gone, new rows in place.
|
||||
$this->assertSame(1, FormFieldConditionalLogicGroup::query()->count());
|
||||
$this->assertSame(1, FormFieldConditionalLogicCondition::query()->count());
|
||||
$condition = FormFieldConditionalLogicCondition::query()->first();
|
||||
$this->assertSame('not_equals', $condition->comparison_operator->value);
|
||||
}
|
||||
|
||||
public function test_replace_logic_with_null_clears_tree(): void
|
||||
{
|
||||
$org = Organisation::factory()->create();
|
||||
$schema = FormSchema::factory()->create(['organisation_id' => $org->id]);
|
||||
$field = FormField::factory()->create(['form_schema_id' => $schema->id, 'slug' => 'subject']);
|
||||
FormField::factory()->create(['form_schema_id' => $schema->id, 'slug' => 'gate']);
|
||||
|
||||
$service = app(FormFieldConditionalLogicService::class);
|
||||
$service->replaceLogic($field, [
|
||||
'operator' => 'all',
|
||||
'children' => [
|
||||
['field_slug' => 'gate', 'operator' => 'empty'],
|
||||
],
|
||||
]);
|
||||
|
||||
$service->replaceLogic($field, null);
|
||||
|
||||
$this->assertSame(0, FormFieldConditionalLogicGroup::query()->count());
|
||||
$this->assertSame(0, FormFieldConditionalLogicCondition::query()->count());
|
||||
}
|
||||
|
||||
public function test_assert_specs_valid_rejects_unknown_operator(): void
|
||||
{
|
||||
$service = app(FormFieldConditionalLogicService::class);
|
||||
$this->expectException(InvalidConditionalLogicSpecException::class);
|
||||
$service->assertSpecsValid([
|
||||
'operator' => 'all',
|
||||
'children' => [
|
||||
['field_slug' => 'x', 'operator' => 'nonsense_op', 'value' => 1],
|
||||
],
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_assert_specs_valid_rejects_root_condition(): void
|
||||
{
|
||||
$service = app(FormFieldConditionalLogicService::class);
|
||||
$this->expectException(InvalidConditionalLogicSpecException::class);
|
||||
$service->assertSpecsValid([
|
||||
'field_slug' => 'x',
|
||||
'operator' => 'equals',
|
||||
'value' => 'y',
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_assert_specs_valid_rejects_empty_group(): void
|
||||
{
|
||||
$service = app(FormFieldConditionalLogicService::class);
|
||||
$this->expectException(InvalidConditionalLogicSpecException::class);
|
||||
$service->assertSpecsValid([
|
||||
'operator' => 'all',
|
||||
'children' => [],
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_replace_logic_rejects_unknown_field_slug(): void
|
||||
{
|
||||
$org = Organisation::factory()->create();
|
||||
$schema = FormSchema::factory()->create(['organisation_id' => $org->id]);
|
||||
$field = FormField::factory()->create(['form_schema_id' => $schema->id, 'slug' => 'subject']);
|
||||
|
||||
$service = app(FormFieldConditionalLogicService::class);
|
||||
$this->expectException(InvalidConditionalLogicSpecException::class);
|
||||
$this->expectExceptionMessageMatches('/ghost_slug/');
|
||||
$service->replaceLogic($field, [
|
||||
'operator' => 'all',
|
||||
'children' => [
|
||||
['field_slug' => 'ghost_slug', 'operator' => 'equals', 'value' => 'x'],
|
||||
],
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_to_json_shape_reconstructs_canonical_show_when(): void
|
||||
{
|
||||
$org = Organisation::factory()->create();
|
||||
$schema = FormSchema::factory()->create(['organisation_id' => $org->id]);
|
||||
$field = FormField::factory()->create(['form_schema_id' => $schema->id, 'slug' => 'subject']);
|
||||
FormField::factory()->create(['form_schema_id' => $schema->id, 'slug' => 'gate']);
|
||||
FormField::factory()->create(['form_schema_id' => $schema->id, 'slug' => 'region']);
|
||||
|
||||
$service = app(FormFieldConditionalLogicService::class);
|
||||
$service->replaceLogic($field, [
|
||||
'operator' => 'all',
|
||||
'children' => [
|
||||
['field_slug' => 'gate', 'operator' => 'equals', 'value' => 'yes'],
|
||||
[
|
||||
'operator' => 'any',
|
||||
'children' => [
|
||||
['field_slug' => 'region', 'operator' => 'equals', 'value' => 'NL'],
|
||||
['field_slug' => 'region', 'operator' => 'empty'],
|
||||
],
|
||||
],
|
||||
],
|
||||
]);
|
||||
|
||||
$shape = $service->toJsonShape($field->fresh()->rootConditionalLogicGroup());
|
||||
$this->assertNotNull($shape);
|
||||
$this->assertSame([
|
||||
'show_when' => [
|
||||
'all' => [
|
||||
['field_slug' => 'gate', 'operator' => 'equals', 'value' => 'yes'],
|
||||
[
|
||||
'any' => [
|
||||
['field_slug' => 'region', 'operator' => 'equals', 'value' => 'NL'],
|
||||
['field_slug' => 'region', 'operator' => 'empty'],
|
||||
],
|
||||
],
|
||||
],
|
||||
],
|
||||
], $shape);
|
||||
}
|
||||
|
||||
public function test_to_json_shape_returns_null_when_no_logic(): void
|
||||
{
|
||||
$service = app(FormFieldConditionalLogicService::class);
|
||||
$this->assertNull($service->toJsonShape(null));
|
||||
}
|
||||
|
||||
public function test_valueless_condition_stores_null_and_omits_value_in_json(): void
|
||||
{
|
||||
$org = Organisation::factory()->create();
|
||||
$schema = FormSchema::factory()->create(['organisation_id' => $org->id]);
|
||||
$field = FormField::factory()->create(['form_schema_id' => $schema->id, 'slug' => 'subject']);
|
||||
FormField::factory()->create(['form_schema_id' => $schema->id, 'slug' => 'gate']);
|
||||
|
||||
$service = app(FormFieldConditionalLogicService::class);
|
||||
$service->replaceLogic($field, [
|
||||
'operator' => 'all',
|
||||
'children' => [
|
||||
['field_slug' => 'gate', 'operator' => 'not_empty', 'value' => 'this_should_be_dropped'],
|
||||
],
|
||||
]);
|
||||
|
||||
$condition = FormFieldConditionalLogicCondition::query()->first();
|
||||
$this->assertNull($condition->value);
|
||||
$this->assertSame(FormFieldConditionalLogicConditionOperator::NotEmpty, $condition->comparison_operator);
|
||||
|
||||
$shape = $service->toJsonShape($field->fresh()->rootConditionalLogicGroup());
|
||||
$this->assertSame([
|
||||
'show_when' => [
|
||||
'all' => [
|
||||
['field_slug' => 'gate', 'operator' => 'not_empty'],
|
||||
],
|
||||
],
|
||||
], $shape);
|
||||
}
|
||||
}
|
||||
@@ -30,7 +30,7 @@ final class FormFieldConfigBackfillAndDropTest extends TestCase
|
||||
// Roll back 2 WS-5c migrations + 5 WS-5b migrations = 7, to get the
|
||||
// pre-WS-5b state where the JSON column still exists on form_fields
|
||||
// / form_field_library.
|
||||
$this->artisan('migrate:rollback', ['--step' => 7])->assertSuccessful();
|
||||
$this->artisan('migrate:rollback', ['--step' => 8])->assertSuccessful();
|
||||
$this->assertTrue(Schema::hasColumn('form_fields', 'validation_rules'));
|
||||
|
||||
$fieldId = $this->seedField([
|
||||
|
||||
@@ -110,14 +110,23 @@ final class FormFieldApiTest extends TestCase
|
||||
{
|
||||
Sanctum::actingAs($this->admin);
|
||||
|
||||
// field A depends on field B
|
||||
$fieldA = FormField::factory()->create([
|
||||
'form_schema_id' => $this->schema->id,
|
||||
'slug' => 'a',
|
||||
'conditional_logic' => ['show_when' => ['all' => [['field_slug' => 'b', 'operator' => 'equals', 'value' => true]]]],
|
||||
]);
|
||||
// field A depends on field B — relational rows via factory helper
|
||||
// (WS-5c commit 2 moved cycle detection to the relational-backed
|
||||
// `FormFieldConditionalLogicService::assertNoCycles`).
|
||||
$fieldA = FormField::factory()
|
||||
->withConditionalLogic([
|
||||
'operator' => 'all',
|
||||
'children' => [
|
||||
['field_slug' => 'b', 'operator' => 'equals', 'value' => true],
|
||||
],
|
||||
])
|
||||
->create([
|
||||
'form_schema_id' => $this->schema->id,
|
||||
'slug' => 'a',
|
||||
]);
|
||||
$fieldB = FormField::factory()->create(['form_schema_id' => $this->schema->id, 'slug' => 'b']);
|
||||
|
||||
// Updating fieldB to depend on fieldA would close the A → B → A loop.
|
||||
$response = $this->putJson(
|
||||
"/api/v1/organisations/{$this->org->id}/forms/schemas/{$this->schema->id}/fields/{$fieldB->id}",
|
||||
['conditional_logic' => ['show_when' => ['all' => [['field_slug' => 'a', 'operator' => 'equals', 'value' => true]]]]],
|
||||
|
||||
@@ -37,7 +37,7 @@ final class FormFieldValidationRuleBackfillTest extends TestCase
|
||||
// validation-rules-backfill + create-validation-rules) = 7.
|
||||
// Brings us to the pre-WS-5b state: validation_rules JSON column
|
||||
// present, no relational tables for WS-5b.
|
||||
$this->artisan('migrate:rollback', ['--step' => 7])->assertSuccessful();
|
||||
$this->artisan('migrate:rollback', ['--step' => 8])->assertSuccessful();
|
||||
$this->assertFalse(Schema::hasTable('form_field_validation_rules'));
|
||||
$this->assertTrue(Schema::hasColumn('form_fields', 'validation_rules'));
|
||||
|
||||
@@ -98,7 +98,7 @@ final class FormFieldValidationRuleBackfillTest extends TestCase
|
||||
// (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' => 7])->assertSuccessful();
|
||||
$this->artisan('migrate:rollback', ['--step' => 8])->assertSuccessful();
|
||||
|
||||
$fieldId = $this->seedFieldWithJson([
|
||||
'field_type' => 'TAG_PICKER',
|
||||
@@ -122,7 +122,7 @@ final class FormFieldValidationRuleBackfillTest extends TestCase
|
||||
// (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' => 7])->assertSuccessful();
|
||||
$this->artisan('migrate:rollback', ['--step' => 8])->assertSuccessful();
|
||||
|
||||
$fieldId = $this->seedFieldWithJson([
|
||||
'field_type' => 'TEXT',
|
||||
@@ -149,7 +149,7 @@ final class FormFieldValidationRuleBackfillTest extends TestCase
|
||||
// (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' => 7])->assertSuccessful();
|
||||
$this->artisan('migrate:rollback', ['--step' => 8])->assertSuccessful();
|
||||
|
||||
$this->seedFieldWithJson([
|
||||
'field_type' => 'TEXT',
|
||||
@@ -166,7 +166,7 @@ final class FormFieldValidationRuleBackfillTest extends TestCase
|
||||
// (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' => 7])->assertSuccessful();
|
||||
$this->artisan('migrate:rollback', ['--step' => 8])->assertSuccessful();
|
||||
|
||||
$this->seedFieldWithJson([
|
||||
'field_type' => 'BOOLEAN',
|
||||
@@ -185,7 +185,7 @@ final class FormFieldValidationRuleBackfillTest extends TestCase
|
||||
// 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' => 7])->assertSuccessful();
|
||||
$this->artisan('migrate:rollback', ['--step' => 8])->assertSuccessful();
|
||||
[$numberId] = $this->seedFields();
|
||||
|
||||
$this->artisan('migrate')->assertSuccessful();
|
||||
@@ -200,7 +200,7 @@ final class FormFieldValidationRuleBackfillTest extends TestCase
|
||||
|
||||
// Roll back WS-5b fully → column reappears and carries canonical JSON
|
||||
// reconstructed from the relational rows.
|
||||
$this->artisan('migrate:rollback', ['--step' => 7])->assertSuccessful();
|
||||
$this->artisan('migrate:rollback', ['--step' => 8])->assertSuccessful();
|
||||
$this->assertTrue(Schema::hasColumn('form_fields', 'validation_rules'));
|
||||
|
||||
$field = DB::table('form_fields')->where('id', $numberId)->first();
|
||||
|
||||
Reference in New Issue
Block a user