Files
crewli/api/tests/Unit/Observers/FormBuilder/FormValueObserverTest.php
bert.hausmans 85815ccb16 feat(forms): add Eloquent models, observer, events, activity-log helpers
Phase 4 of S1.

Models (app/Models/FormBuilder/): FormSchema, FormSchemaSection, FormField,
FormSubmission, FormValue, FormValueOption, FormTemplate, FormFieldLibrary,
FormSchemaWebhook, FormWebhookDelivery, FormSubmissionSectionStatus,
FormSubmissionDelegation. Plus UserProfile at app/Models/ (user-universal).

OrganisationScope applied on: FormSchema, FormTemplate, FormFieldLibrary.
FormSchemaWebhook documents inherited-scope discipline (OrganisationScope's
strategies — organisation_id/event_id/festival_section_id — don't cover
form_schema_id; direct queries would leak across orgs, so must go via
$schema->webhooks()).

User::profile()/getOrCreateProfile(), Event::formSchemas() (morphMany),
Person::formSubmissions() (morphMany).

Morph map enforced in AppServiceProvider with 28 keys covering every model
that appears as activitylog subject/causer. Also updated
OrganisationDashboardService (and its test) to query activitylog via
getMorphClass() instead of FQCN.

Activity log strategy: nuanced explicit calls (logSchemaChange on FormSchema,
logFieldChange on FormField) — no LogsActivity trait. Suppression for bulk
fixtures via App\Support\ActivityLog::suppressed(fn() => ...) which flips
config('activitylog.enabled') around a callback. Both our explicit calls
and spatie's trait on Organisation respect the flag via ActivityLogger::log().

FormValueObserver (app/Observers/FormBuilder/) populates value_indexed/
value_number/value_date/value_bool on save per field.value_storage_hint,
rebuilds form_value_options pivot on multi-value filterable fields, cleans
up on delete. Memoised field cache avoids N+1. Registered in AppServiceProvider.

9 lightweight event classes (app/Events/FormBuilder/) as SerializesModels
containers — submission lifecycle signatures lock in for S2 services, no
listeners yet.

Factories for all models with Dutch fake data (fake('nl_NL')). FormSchema
factory uses defaultSubmissionMode(); FormField factory uses
recommendedValueStorageHint().

Tests: 9 new observer tests (all pass); full suite 910/910 (up from 901).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-17 12:35:41 +02:00

205 lines
6.7 KiB
PHP

<?php
declare(strict_types=1);
namespace Tests\Unit\Observers\FormBuilder;
use App\Enums\FormBuilder\FormFieldType;
use App\Enums\FormBuilder\FormValueStorageHint;
use App\Models\FormBuilder\FormField;
use App\Models\FormBuilder\FormSchema;
use App\Models\FormBuilder\FormSubmission;
use App\Models\FormBuilder\FormValue;
use App\Models\FormBuilder\FormValueOption;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
final class FormValueObserverTest extends TestCase
{
use RefreshDatabase;
private FormSchema $schema;
private FormSubmission $submission;
protected function setUp(): void
{
parent::setUp();
$this->schema = FormSchema::factory()->create();
$this->submission = FormSubmission::factory()->create([
'form_schema_id' => $this->schema->id,
]);
}
public function test_string_hint_populates_value_indexed(): void
{
$field = FormField::factory()->for($this->schema, 'schema')->create([
'field_type' => FormFieldType::TEXT->value,
'value_storage_hint' => FormValueStorageHint::STRING,
]);
$value = FormValue::create([
'form_submission_id' => $this->submission->id,
'form_field_id' => $field->id,
'value' => ['value' => 'Bert Hausmans'],
]);
$this->assertSame('Bert Hausmans', $value->fresh()->value_indexed);
$this->assertNull($value->value_number);
$this->assertNull($value->value_date);
$this->assertNull($value->value_bool);
}
public function test_number_hint_populates_value_number(): void
{
$field = FormField::factory()->for($this->schema, 'schema')->create([
'field_type' => FormFieldType::NUMBER->value,
'value_storage_hint' => FormValueStorageHint::NUMBER,
]);
$value = FormValue::create([
'form_submission_id' => $this->submission->id,
'form_field_id' => $field->id,
'value' => ['value' => 42.5],
]);
$this->assertEqualsWithDelta(42.5, (float) $value->fresh()->value_number, 0.0001);
$this->assertNull($value->fresh()->value_indexed);
}
public function test_date_hint_populates_value_date(): void
{
$field = FormField::factory()->for($this->schema, 'schema')->create([
'field_type' => FormFieldType::DATE->value,
'value_storage_hint' => FormValueStorageHint::DATE,
]);
$value = FormValue::create([
'form_submission_id' => $this->submission->id,
'form_field_id' => $field->id,
'value' => ['value' => '2026-07-04'],
]);
$this->assertSame('2026-07-04', $value->fresh()->value_date->format('Y-m-d'));
}
public function test_bool_hint_populates_value_bool(): void
{
$field = FormField::factory()->for($this->schema, 'schema')->create([
'field_type' => FormFieldType::BOOLEAN->value,
'value_storage_hint' => FormValueStorageHint::BOOL,
]);
$value = FormValue::create([
'form_submission_id' => $this->submission->id,
'form_field_id' => $field->id,
'value' => ['value' => true],
]);
$this->assertTrue($value->fresh()->value_bool);
}
public function test_json_hint_leaves_typed_columns_null(): void
{
$field = FormField::factory()->for($this->schema, 'schema')->create([
'field_type' => FormFieldType::TABLE_ROWS->value,
'value_storage_hint' => FormValueStorageHint::JSON,
]);
$value = FormValue::create([
'form_submission_id' => $this->submission->id,
'form_field_id' => $field->id,
'value' => [['col_a' => 'x', 'col_b' => 'y']],
]);
$fresh = $value->fresh();
$this->assertNull($fresh->value_indexed);
$this->assertNull($fresh->value_number);
$this->assertNull($fresh->value_date);
$this->assertNull($fresh->value_bool);
}
public function test_filterable_multiselect_rebuilds_pivot(): void
{
$field = FormField::factory()->for($this->schema, 'schema')->create([
'field_type' => FormFieldType::MULTISELECT->value,
'value_storage_hint' => FormValueStorageHint::JSON,
'is_filterable' => true,
]);
$value = FormValue::create([
'form_submission_id' => $this->submission->id,
'form_field_id' => $field->id,
'value' => ['XS', 'M', 'XXL'],
]);
$options = FormValueOption::where('form_value_id', $value->id)
->pluck('option_value')
->sort()
->values()
->all();
$this->assertSame(['M', 'XS', 'XXL'], $options);
}
public function test_resave_rebuilds_pivot_idempotently(): void
{
$field = FormField::factory()->for($this->schema, 'schema')->create([
'field_type' => FormFieldType::MULTISELECT->value,
'is_filterable' => true,
]);
$value = FormValue::create([
'form_submission_id' => $this->submission->id,
'form_field_id' => $field->id,
'value' => ['A', 'B'],
]);
$this->assertSame(2, FormValueOption::where('form_value_id', $value->id)->count());
// Resave with different options.
$value->value = ['C'];
$value->save();
$options = FormValueOption::where('form_value_id', $value->id)
->pluck('option_value')
->all();
$this->assertSame(['C'], $options);
}
public function test_non_filterable_field_does_not_populate_pivot(): void
{
$field = FormField::factory()->for($this->schema, 'schema')->create([
'field_type' => FormFieldType::MULTISELECT->value,
'is_filterable' => false,
]);
$value = FormValue::create([
'form_submission_id' => $this->submission->id,
'form_field_id' => $field->id,
'value' => ['A', 'B'],
]);
$this->assertSame(0, FormValueOption::where('form_value_id', $value->id)->count());
}
public function test_single_value_filterable_populates_value_indexed_even_when_hint_is_json(): void
{
$field = FormField::factory()->for($this->schema, 'schema')->create([
'field_type' => FormFieldType::SELECT->value,
'value_storage_hint' => FormValueStorageHint::JSON,
'is_filterable' => true,
]);
$value = FormValue::create([
'form_submission_id' => $this->submission->id,
'form_field_id' => $field->id,
'value' => ['value' => 'beta'],
]);
$this->assertSame('beta', $value->fresh()->value_indexed);
}
}