Files
crewli/api/tests/Feature/FormBuilder/Configs/FormFieldConfigBackfillAndDropTest.php
bert.hausmans 192353f4bc feat(form-builder): admin UI completion — server filters, KPIs, resource expansion (WS-6 sessie 3c)
Closes the four production gaps that emerged from sessie 3b's admin UI.
What we ship here is final: no further rework planned before production.

Backend
- IndexFailuresRequest validates state/search/failed_at_from/failed_at_to/
  listener_class. orgIndex + platformIndex apply them via a single
  applyIndexFilters() helper. Search runs case-insensitive `LIKE` on
  exception_message; SQL wildcards in user input are escaped.
- New /kpis aggregate endpoint per scope (orgKpis, platformKpis) returns
  open / resolved_30d / dismissed_30d / total_submissions in O(1) COUNTs.
  Replaces sessie 3b's client-side bucketing of an oversized list.
- Resource expansion: organisation_name, form_schema_label,
  resolved_by_user_name, dismissed_by_user_name, exception_trace,
  retry_history[]. Eager-loading via indexEagerLoads()/detailEagerLoads()
  prevents N+1 (verified by query-count assertion in test).
- New 2026_04_28_181000 migration adds exception_trace (longtext nullable)
  to form_submission_action_failures. ApplyBindingsOnFormSubmit listener
  now captures $e->getTraceAsString() at failure time.
- New FormSubmissionActionFailureRetryAttemptResource exposes per-attempt
  data (timestamp, actor name, outcome, exception details) inside
  retry_history[]. Index payloads omit the field via whenLoaded() to keep
  list responses lean.

Frontend (apps/app)
- Types updated to mirror the expanded resource shape and the new KPI
  endpoint contract. FormFailuresKpis is now { open, resolved_30d,
  dismissed_30d, total_submissions } (server-aggregate).
- useFormFailures composable forwards all 5 server filters via
  buildIndexParams() (strips empty/whitespace). useFormFailuresKpis hits
  the dedicated /kpis endpoint per scope.
- FormFailuresTable replaces client-side bucketing with server-side
  filtering, adds listener_class + date-range filter inputs, and renames
  the 4th KPI tile to "Submissions" (was "Totaal").
- FormFailureDetail renders organisation_name + form_schema_label in the
  header, surfaces an expandable stack-trace card, names the resolved/
  dismissed actor in the timeline, and replaces the "v1 placeholder"
  retry-history card with a full per-attempt timeline.

ESLint config gap (apps/app)
- New .eslintrc.cjs adapted from the Vuexy reference, minus Vuexy-internal
  rules. `pnpm lint` now runs successfully (was previously broken — the
  package.json script referenced a missing config). The 80 baseline
  violations across the codebase are pre-existing and out of scope for
  this session.

Tests + gates
- 24 new backend tests across filter, kpis, and resource-shape suites.
  Backend: 1462 → 1486 passing, 0 → 0 failing. Larastan clean. Rector
  dry-run unchanged at 354 (pre-Task-1 baseline from f18b55b).
- 3 new vitest tests in apps/app (filter wiring, KPI endpoint, KPI tile
  values from /kpis). Vitest: 38 → 41 passing. tsc clean. Portal
  unchanged (113 vitest, tsc clean).
- 5 backfill rollback tests bumped --step counts +1 for the new migration.
- Ws6FoundationMigrationTest down/up chain now includes exception_trace
  before the parent table is restored.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-29 00:14:20 +02:00

114 lines
4.1 KiB
PHP

<?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 4 WS-5c migrations + 2 WS-6 migrations + 5 WS-5b
// migrations = 11, to get the pre-WS-5b state where the JSON column
// still exists on form_fields / form_field_library.
$this->artisan('migrate:rollback', ['--step' => 16])->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;
}
}