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>
91 lines
3.0 KiB
PHP
91 lines
3.0 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Tests\Unit\Models\FormBuilder;
|
|
|
|
use App\Enums\FormBuilder\FormPurpose;
|
|
use App\Models\Event;
|
|
use App\Models\FormBuilder\FormField;
|
|
use App\Models\FormBuilder\FormSchema;
|
|
use App\Models\FormBuilder\FormSubmission;
|
|
use App\Models\Organisation;
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
|
use Spatie\Activitylog\Models\Activity;
|
|
use Tests\TestCase;
|
|
|
|
final class FormSchemaTest extends TestCase
|
|
{
|
|
use RefreshDatabase;
|
|
|
|
public function test_form_schema_uses_ulids(): void
|
|
{
|
|
$schema = FormSchema::factory()->create();
|
|
$this->assertSame(26, mb_strlen($schema->id));
|
|
}
|
|
|
|
public function test_form_schema_is_org_scoped(): void
|
|
{
|
|
$orgA = Organisation::factory()->create();
|
|
$orgB = Organisation::factory()->create();
|
|
|
|
FormSchema::factory()->count(2)->create(['organisation_id' => $orgA->id]);
|
|
FormSchema::factory()->count(3)->create(['organisation_id' => $orgB->id]);
|
|
|
|
// Without context: scope is a no-op (CLI tests have no route context)
|
|
$this->assertSame(5, FormSchema::query()->count());
|
|
|
|
// Manually-applied scope filters by org
|
|
$orgAOnly = FormSchema::query()
|
|
->where('organisation_id', $orgA->id)
|
|
->count();
|
|
$this->assertSame(2, $orgAOnly);
|
|
}
|
|
|
|
public function test_form_schema_morphs_to_owner(): void
|
|
{
|
|
$event = Event::factory()->create();
|
|
$schema = FormSchema::factory()->create([
|
|
'organisation_id' => $event->organisation_id,
|
|
'owner_type' => 'event',
|
|
'owner_id' => $event->id,
|
|
]);
|
|
|
|
$this->assertInstanceOf(Event::class, $schema->owner);
|
|
$this->assertSame($event->id, $schema->owner->id);
|
|
}
|
|
|
|
public function test_form_schema_casts_purpose_to_enum(): void
|
|
{
|
|
$schema = FormSchema::factory()->forPurpose(FormPurpose::INCIDENT_REPORT)->create();
|
|
$this->assertSame(FormPurpose::INCIDENT_REPORT, $schema->fresh()->purpose);
|
|
}
|
|
|
|
public function test_form_schema_has_many_fields_and_submissions(): void
|
|
{
|
|
$schema = FormSchema::factory()->create();
|
|
FormField::factory()->count(3)->for($schema, 'schema')->create();
|
|
FormSubmission::factory()->count(2)->create(['form_schema_id' => $schema->id]);
|
|
|
|
$this->assertCount(3, $schema->fresh()->fields);
|
|
$this->assertCount(2, $schema->fresh()->submissions);
|
|
}
|
|
|
|
public function test_log_schema_change_creates_activity_entry(): void
|
|
{
|
|
Activity::query()->delete();
|
|
$schema = FormSchema::factory()->create();
|
|
|
|
$schema->logSchemaChange('schema.published', ['by' => 'admin']);
|
|
|
|
$entry = Activity::query()
|
|
->where('subject_type', $schema->getMorphClass())
|
|
->where('subject_id', $schema->id)
|
|
->first();
|
|
|
|
$this->assertNotNull($entry);
|
|
$this->assertSame('schema.published', $entry->description);
|
|
$this->assertSame('admin', $entry->properties->get('by'));
|
|
}
|
|
}
|