Files
crewli/api/tests/Unit/Models/UserProfileTest.php
bert.hausmans cd7a804024 test(forms): model tests, multi-tenancy, migration rollback (Phase 9)
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>
2026-04-17 16:44:47 +02:00

119 lines
3.7 KiB
PHP

<?php
declare(strict_types=1);
namespace Tests\Unit\Models;
use App\Enums\FormBuilder\FormSubmissionStatus;
use App\Models\FormBuilder\FormSchema;
use App\Models\FormBuilder\FormSubmission;
use App\Models\User;
use App\Models\UserProfile;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
final class UserProfileTest extends TestCase
{
use RefreshDatabase;
public function test_user_profile_belongs_to_user(): void
{
$user = User::factory()->create();
$profile = $user->profile;
$this->assertNotNull($profile);
$this->assertSame($user->id, $profile->user->id);
}
public function test_emergency_contact_fields_are_fillable(): void
{
$profile = UserProfile::factory()->create([
'emergency_contact_name' => 'Partner',
'emergency_contact_phone' => '+31611112222',
]);
$this->assertSame('Partner', $profile->emergency_contact_name);
$this->assertSame('+31611112222', $profile->emergency_contact_phone);
}
public function test_reliability_score_is_not_fillable(): void
{
$profile = (new UserProfile)->fill([
'user_id' => (string) \Illuminate\Support\Str::ulid(),
'reliability_score' => 4.99,
]);
$this->assertNull($profile->reliability_score);
}
public function test_is_ambassador_is_not_fillable(): void
{
$profile = (new UserProfile)->fill([
'user_id' => (string) \Illuminate\Support\Str::ulid(),
'is_ambassador' => true,
]);
$this->assertNull($profile->is_ambassador);
}
public function test_settings_is_cast_to_array(): void
{
$profile = UserProfile::factory()->create([
'settings' => ['ui.theme' => 'dark'],
]);
$this->assertIsArray($profile->fresh()->settings);
$this->assertSame('dark', $profile->fresh()->settings['ui.theme']);
}
public function test_last_submitted_at_returns_null_when_no_submissions(): void
{
$user = User::factory()->create();
$profile = $user->profile;
$this->assertNull($profile->last_submitted_at);
}
public function test_last_submitted_at_returns_max_submitted_at_from_user_subject_submissions(): void
{
$user = User::factory()->create();
$schema = FormSchema::factory()->create();
FormSubmission::factory()->create([
'form_schema_id' => $schema->id,
'subject_type' => 'user',
'subject_id' => $user->id,
'status' => FormSubmissionStatus::SUBMITTED,
'submitted_at' => now()->subDays(5),
]);
$latest = FormSubmission::factory()->create([
'form_schema_id' => $schema->id,
'subject_type' => 'user',
'subject_id' => $user->id,
'status' => FormSubmissionStatus::SUBMITTED,
'submitted_at' => now()->subDay(),
]);
// A draft and a test submission should be ignored.
FormSubmission::factory()->create([
'form_schema_id' => $schema->id,
'subject_type' => 'user',
'subject_id' => $user->id,
'status' => FormSubmissionStatus::DRAFT,
'submitted_at' => now(),
]);
FormSubmission::factory()->create([
'form_schema_id' => $schema->id,
'subject_type' => 'user',
'subject_id' => $user->id,
'status' => FormSubmissionStatus::SUBMITTED,
'submitted_at' => now(),
'is_test' => true,
]);
$this->assertSame(
$latest->submitted_at->format('Y-m-d H:i:s'),
$user->profile->last_submitted_at->format('Y-m-d H:i:s')
);
}
}