UserProfileTest: belongs-to user, fillable/non-fillable boundaries, settings cast, lastSubmittedAt accessor (null + max from user-subject submissions only, ignoring drafts and is_test rows). FormSchemaTest: ULID PK, OrganisationScope filtering, polymorphic owner resolution to Event, purpose enum cast, hasMany fields/submissions, and logSchemaChange() actually creates an activity-log entry. FormFieldTest: belongs-to schema, field_type stored as string (not DB enum), binding/translations array casts, hasMany values, soft-delete preserves historical values, logFieldChange() creates an entry. FormSubmissionTest: belongs-to schema, polymorphic subject resolution, status enum cast, schema_snapshot array cast, hasMany values. FormValueTest: belongs-to submission/field, value array cast, hasMany options pivot rebuilt by observer, unique-pair DB constraint enforced. MultiTenancyTest: OrganisationScope correctly filters FormSchema / FormTemplate / FormFieldLibrary by route-resolved organisation. Pins the FormSchemaWebhook un-scoped behaviour explicitly so a future scope addition is an intentional decision, not an accident. MigrationRollbackTest (group 'slow'): full migrate:fresh → rollback 14 S1 steps → assert all 13 form-builder tables dropped + legacy tables intentionally retained → re-migrate and assert table list matches snapshot. Plus a separate test exercising the populate-user-profiles migration's down(). Supporting tweaks: - UserProfile::lastSubmittedAt accessor now returns Carbon|null instead of a raw timestamp string — testable, and matches Eloquent convention. - UserProfileFactory cooperates with UserObserver via newModel override (updates the auto-created row instead of inserting a duplicate). - AppServiceProvider morph map extended with all 12 form-builder model keys so logSchemaChange/logFieldChange resolve under enforceMorphMap. Suite: 945 passed (was 911), 2671 assertions. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
71 lines
2.4 KiB
PHP
71 lines
2.4 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Tests\Unit\Models\FormBuilder;
|
|
|
|
use App\Enums\FormBuilder\FormSubmissionStatus;
|
|
use App\Models\FormBuilder\FormField;
|
|
use App\Models\FormBuilder\FormSchema;
|
|
use App\Models\FormBuilder\FormSubmission;
|
|
use App\Models\FormBuilder\FormValue;
|
|
use App\Models\Person;
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
|
use Tests\TestCase;
|
|
|
|
final class FormSubmissionTest extends TestCase
|
|
{
|
|
use RefreshDatabase;
|
|
|
|
public function test_form_submission_belongs_to_schema(): void
|
|
{
|
|
$schema = FormSchema::factory()->create();
|
|
$submission = FormSubmission::factory()->create(['form_schema_id' => $schema->id]);
|
|
|
|
$this->assertSame($schema->id, $submission->schema->id);
|
|
}
|
|
|
|
public function test_form_submission_morphs_to_subject(): void
|
|
{
|
|
$person = Person::factory()->create();
|
|
$submission = FormSubmission::factory()->create([
|
|
'subject_type' => 'person',
|
|
'subject_id' => $person->id,
|
|
]);
|
|
|
|
$this->assertInstanceOf(Person::class, $submission->subject);
|
|
$this->assertSame($person->id, $submission->subject->id);
|
|
}
|
|
|
|
public function test_form_submission_casts_status_to_enum(): void
|
|
{
|
|
$submission = FormSubmission::factory()->submitted()->create();
|
|
$this->assertSame(FormSubmissionStatus::SUBMITTED, $submission->fresh()->status);
|
|
}
|
|
|
|
public function test_form_submission_casts_schema_snapshot_to_array(): void
|
|
{
|
|
$snapshot = ['schema_version' => 2, 'fields' => [['slug' => 'shirtmaat']]];
|
|
$submission = FormSubmission::factory()->create(['schema_snapshot' => $snapshot]);
|
|
|
|
$this->assertIsArray($submission->fresh()->schema_snapshot);
|
|
$this->assertSame(2, $submission->fresh()->schema_snapshot['schema_version']);
|
|
}
|
|
|
|
public function test_form_submission_has_many_values(): void
|
|
{
|
|
$schema = FormSchema::factory()->create();
|
|
$fields = FormField::factory()->count(3)->for($schema, 'schema')->create();
|
|
$submission = FormSubmission::factory()->create(['form_schema_id' => $schema->id]);
|
|
foreach ($fields as $field) {
|
|
FormValue::create([
|
|
'form_submission_id' => $submission->id,
|
|
'form_field_id' => $field->id,
|
|
'value' => ['value' => 'x'],
|
|
]);
|
|
}
|
|
|
|
$this->assertCount(3, $submission->fresh()->values);
|
|
}
|
|
}
|