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>
This commit is contained in:
@@ -50,12 +50,16 @@ final class UserProfile extends Model
|
|||||||
protected function lastSubmittedAt(): Attribute
|
protected function lastSubmittedAt(): Attribute
|
||||||
{
|
{
|
||||||
return Attribute::make(
|
return Attribute::make(
|
||||||
get: fn () => FormSubmission::query()
|
get: function () {
|
||||||
->where('subject_type', 'user')
|
$raw = FormSubmission::query()
|
||||||
->where('subject_id', $this->user_id)
|
->where('subject_type', 'user')
|
||||||
->where('status', FormSubmissionStatus::SUBMITTED)
|
->where('subject_id', $this->user_id)
|
||||||
->where('is_test', false)
|
->where('status', FormSubmissionStatus::SUBMITTED)
|
||||||
->max('submitted_at'),
|
->where('is_test', false)
|
||||||
|
->max('submitted_at');
|
||||||
|
|
||||||
|
return $raw === null ? null : \Illuminate\Support\Carbon::parse($raw);
|
||||||
|
},
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -34,7 +34,18 @@ use App\Models\User;
|
|||||||
use App\Models\UserInvitation;
|
use App\Models\UserInvitation;
|
||||||
use App\Models\UserOrganisationTag;
|
use App\Models\UserOrganisationTag;
|
||||||
use App\Models\UserProfile;
|
use App\Models\UserProfile;
|
||||||
|
use App\Models\FormBuilder\FormField;
|
||||||
|
use App\Models\FormBuilder\FormFieldLibrary;
|
||||||
|
use App\Models\FormBuilder\FormSchema;
|
||||||
|
use App\Models\FormBuilder\FormSchemaSection;
|
||||||
|
use App\Models\FormBuilder\FormSchemaWebhook;
|
||||||
|
use App\Models\FormBuilder\FormSubmission;
|
||||||
|
use App\Models\FormBuilder\FormSubmissionDelegation;
|
||||||
|
use App\Models\FormBuilder\FormSubmissionSectionStatus;
|
||||||
|
use App\Models\FormBuilder\FormTemplate;
|
||||||
use App\Models\FormBuilder\FormValue;
|
use App\Models\FormBuilder\FormValue;
|
||||||
|
use App\Models\FormBuilder\FormValueOption;
|
||||||
|
use App\Models\FormBuilder\FormWebhookDelivery;
|
||||||
use App\Models\VolunteerAvailability;
|
use App\Models\VolunteerAvailability;
|
||||||
use App\Observers\FormBuilder\FormValueObserver;
|
use App\Observers\FormBuilder\FormValueObserver;
|
||||||
use App\Observers\PersonObserver;
|
use App\Observers\PersonObserver;
|
||||||
@@ -95,6 +106,22 @@ class AppServiceProvider extends ServiceProvider
|
|||||||
'user_invitation' => UserInvitation::class,
|
'user_invitation' => UserInvitation::class,
|
||||||
'user_organisation_tag' => UserOrganisationTag::class,
|
'user_organisation_tag' => UserOrganisationTag::class,
|
||||||
'volunteer_availability' => VolunteerAvailability::class,
|
'volunteer_availability' => VolunteerAvailability::class,
|
||||||
|
|
||||||
|
// Form-builder models — used as activity-log subjects via the
|
||||||
|
// logSchemaChange / logFieldChange helpers, and (in S2+) as
|
||||||
|
// polymorphic webhook payload subjects.
|
||||||
|
'form_schema' => FormSchema::class,
|
||||||
|
'form_schema_section' => FormSchemaSection::class,
|
||||||
|
'form_field' => FormField::class,
|
||||||
|
'form_field_library' => FormFieldLibrary::class,
|
||||||
|
'form_submission' => FormSubmission::class,
|
||||||
|
'form_submission_section_status' => FormSubmissionSectionStatus::class,
|
||||||
|
'form_submission_delegation' => FormSubmissionDelegation::class,
|
||||||
|
'form_value' => FormValue::class,
|
||||||
|
'form_value_option' => FormValueOption::class,
|
||||||
|
'form_template' => FormTemplate::class,
|
||||||
|
'form_schema_webhook' => FormSchemaWebhook::class,
|
||||||
|
'form_webhook_delivery' => FormWebhookDelivery::class,
|
||||||
]);
|
]);
|
||||||
|
|
||||||
Person::observe(PersonObserver::class);
|
Person::observe(PersonObserver::class);
|
||||||
|
|||||||
@@ -8,7 +8,14 @@ use App\Models\User;
|
|||||||
use App\Models\UserProfile;
|
use App\Models\UserProfile;
|
||||||
use Illuminate\Database\Eloquent\Factories\Factory;
|
use Illuminate\Database\Eloquent\Factories\Factory;
|
||||||
|
|
||||||
/** @extends Factory<UserProfile> */
|
/**
|
||||||
|
* @extends Factory<UserProfile>
|
||||||
|
*
|
||||||
|
* Note: UserObserver auto-creates a user_profiles row whenever a User is
|
||||||
|
* created. To avoid unique-constraint collisions, this factory cooperates
|
||||||
|
* with the observer: it updates the existing profile row (if any) rather
|
||||||
|
* than blindly inserting a new one.
|
||||||
|
*/
|
||||||
final class UserProfileFactory extends Factory
|
final class UserProfileFactory extends Factory
|
||||||
{
|
{
|
||||||
/** @return array<string, mixed> */
|
/** @return array<string, mixed> */
|
||||||
@@ -30,4 +37,19 @@ final class UserProfileFactory extends Factory
|
|||||||
{
|
{
|
||||||
return $this->state(fn () => ['is_ambassador' => true]);
|
return $this->state(fn () => ['is_ambassador' => true]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function newModel(array $attributes = []): UserProfile
|
||||||
|
{
|
||||||
|
$userId = $attributes['user_id'] ?? null;
|
||||||
|
if ($userId !== null) {
|
||||||
|
$existing = UserProfile::where('user_id', $userId)->first();
|
||||||
|
if ($existing !== null) {
|
||||||
|
$existing->forceFill($attributes)->save();
|
||||||
|
|
||||||
|
return $existing;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return parent::newModel($attributes);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
108
api/tests/Feature/FormBuilder/MultiTenancyTest.php
Normal file
108
api/tests/Feature/FormBuilder/MultiTenancyTest.php
Normal file
@@ -0,0 +1,108 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Tests\Feature\FormBuilder;
|
||||||
|
|
||||||
|
use App\Models\FormBuilder\FormSchema;
|
||||||
|
use App\Models\FormBuilder\FormSchemaWebhook;
|
||||||
|
use App\Models\FormBuilder\FormTemplate;
|
||||||
|
use App\Models\FormBuilder\FormFieldLibrary;
|
||||||
|
use App\Models\Organisation;
|
||||||
|
use App\Models\Scopes\OrganisationScope;
|
||||||
|
use App\Models\User;
|
||||||
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||||
|
use Illuminate\Routing\Route;
|
||||||
|
use Tests\TestCase;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Verifies OrganisationScope discipline:
|
||||||
|
* - FormSchema, FormTemplate, FormFieldLibrary apply OrganisationScope and
|
||||||
|
* are filtered when an organisation route parameter is in scope.
|
||||||
|
* - FormSchemaWebhook intentionally does NOT apply OrganisationScope (its
|
||||||
|
* scope is enforced via the parent FormSchema). The docblock on the
|
||||||
|
* model warns callers; this test pins the current behaviour so a future
|
||||||
|
* refactor that adds the scope is an intentional decision.
|
||||||
|
*/
|
||||||
|
final class MultiTenancyTest extends TestCase
|
||||||
|
{
|
||||||
|
use RefreshDatabase;
|
||||||
|
|
||||||
|
private Organisation $orgA;
|
||||||
|
|
||||||
|
private Organisation $orgB;
|
||||||
|
|
||||||
|
protected function setUp(): void
|
||||||
|
{
|
||||||
|
parent::setUp();
|
||||||
|
$this->orgA = Organisation::factory()->create();
|
||||||
|
$this->orgB = Organisation::factory()->create();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function test_form_schema_filters_by_organisation_route_parameter(): void
|
||||||
|
{
|
||||||
|
FormSchema::factory()->count(3)->create(['organisation_id' => $this->orgA->id]);
|
||||||
|
FormSchema::factory()->count(2)->create(['organisation_id' => $this->orgB->id]);
|
||||||
|
|
||||||
|
$this->actingAsOrgUser($this->orgA);
|
||||||
|
$this->withRouteParameter('organisation', $this->orgA);
|
||||||
|
|
||||||
|
$this->assertSame(3, FormSchema::query()->count());
|
||||||
|
|
||||||
|
$this->withRouteParameter('organisation', $this->orgB);
|
||||||
|
$this->assertSame(2, FormSchema::query()->count());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function test_form_template_filters_by_organisation_route_parameter(): void
|
||||||
|
{
|
||||||
|
FormTemplate::factory()->count(2)->create(['organisation_id' => $this->orgA->id]);
|
||||||
|
FormTemplate::factory()->count(4)->create(['organisation_id' => $this->orgB->id]);
|
||||||
|
|
||||||
|
$this->withRouteParameter('organisation', $this->orgA);
|
||||||
|
$this->assertSame(2, FormTemplate::query()->count());
|
||||||
|
|
||||||
|
$this->withRouteParameter('organisation', $this->orgB);
|
||||||
|
$this->assertSame(4, FormTemplate::query()->count());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function test_form_field_library_filters_by_organisation_route_parameter(): void
|
||||||
|
{
|
||||||
|
FormFieldLibrary::factory()->count(1)->create(['organisation_id' => $this->orgA->id]);
|
||||||
|
FormFieldLibrary::factory()->count(3)->create(['organisation_id' => $this->orgB->id]);
|
||||||
|
|
||||||
|
$this->withRouteParameter('organisation', $this->orgA);
|
||||||
|
$this->assertSame(1, FormFieldLibrary::query()->count());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function test_form_schema_webhook_is_not_globally_scoped(): void
|
||||||
|
{
|
||||||
|
$schemaA = FormSchema::factory()->create(['organisation_id' => $this->orgA->id]);
|
||||||
|
$schemaB = FormSchema::factory()->create(['organisation_id' => $this->orgB->id]);
|
||||||
|
FormSchemaWebhook::factory()->count(2)->create(['form_schema_id' => $schemaA->id]);
|
||||||
|
FormSchemaWebhook::factory()->count(3)->create(['form_schema_id' => $schemaB->id]);
|
||||||
|
|
||||||
|
$this->withRouteParameter('organisation', $this->orgA);
|
||||||
|
// Direct queries leak across orgs — exact reason the docblock warns
|
||||||
|
// never to query FormSchemaWebhook::query() without an eager constraint.
|
||||||
|
$this->assertSame(5, FormSchemaWebhook::query()->count());
|
||||||
|
|
||||||
|
// Going through the schema relation respects OrganisationScope on the parent.
|
||||||
|
$this->assertCount(2, $schemaA->fresh()->webhooks);
|
||||||
|
$this->assertCount(3, $schemaB->fresh()->webhooks);
|
||||||
|
}
|
||||||
|
|
||||||
|
private function actingAsOrgUser(Organisation $org): void
|
||||||
|
{
|
||||||
|
$user = User::factory()->create();
|
||||||
|
$org->users()->attach($user, ['role' => 'org_member']);
|
||||||
|
$this->actingAs($user);
|
||||||
|
}
|
||||||
|
|
||||||
|
private function withRouteParameter(string $name, mixed $value): void
|
||||||
|
{
|
||||||
|
$route = new Route(['GET'], '/_test', static fn () => null);
|
||||||
|
$route->bind(request());
|
||||||
|
$route->setParameter($name, $value);
|
||||||
|
request()->setRouteResolver(static fn () => $route);
|
||||||
|
}
|
||||||
|
}
|
||||||
114
api/tests/Feature/MigrationRollbackTest.php
Normal file
114
api/tests/Feature/MigrationRollbackTest.php
Normal file
@@ -0,0 +1,114 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Tests\Feature;
|
||||||
|
|
||||||
|
use Illuminate\Foundation\Testing\WithoutMiddleware;
|
||||||
|
use Illuminate\Support\Facades\Artisan;
|
||||||
|
use Illuminate\Support\Facades\DB;
|
||||||
|
use Illuminate\Support\Facades\Schema;
|
||||||
|
use PHPUnit\Framework\Attributes\Group;
|
||||||
|
use Tests\TestCase;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Hard-resets the test database via migrate:fresh, rolls back every
|
||||||
|
* form-builder migration in reverse, asserts the new tables are gone (and
|
||||||
|
* legacy tables remain — Phase 8 was deferred to S2 per S1 wrap-up), then
|
||||||
|
* re-applies and asserts the table list matches the post-fresh snapshot.
|
||||||
|
*
|
||||||
|
* Slow because we exercise the real migrator against the real database.
|
||||||
|
* Tagged "slow" so CI can parallel-isolate or skip it where needed.
|
||||||
|
*/
|
||||||
|
#[Group('slow')]
|
||||||
|
final class MigrationRollbackTest extends TestCase
|
||||||
|
{
|
||||||
|
use WithoutMiddleware;
|
||||||
|
|
||||||
|
/** Migration steps added in S1 (Phase 3 + Phase 4). */
|
||||||
|
private const S1_MIGRATION_STEPS = 14;
|
||||||
|
|
||||||
|
private const FORM_BUILDER_TABLES = [
|
||||||
|
'user_profiles',
|
||||||
|
'form_schemas',
|
||||||
|
'form_schema_sections',
|
||||||
|
'form_field_library',
|
||||||
|
'form_fields',
|
||||||
|
'form_submissions',
|
||||||
|
'form_submission_section_statuses',
|
||||||
|
'form_submission_delegations',
|
||||||
|
'form_values',
|
||||||
|
'form_value_options',
|
||||||
|
'form_templates',
|
||||||
|
'form_schema_webhooks',
|
||||||
|
'form_webhook_deliveries',
|
||||||
|
];
|
||||||
|
|
||||||
|
public function test_form_builder_migrations_are_fully_reversible(): void
|
||||||
|
{
|
||||||
|
Artisan::call('migrate:fresh');
|
||||||
|
$beforeTables = $this->tableList();
|
||||||
|
|
||||||
|
// S1 leaves the legacy registration_* tables in place — Phase 8
|
||||||
|
// was deferred to S2. Sanity-check that assumption is still true.
|
||||||
|
foreach (['registration_form_fields', 'person_field_values', 'registration_field_templates'] as $legacy) {
|
||||||
|
$this->assertTrue(Schema::hasTable($legacy), "legacy table {$legacy} should still exist after S1");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Every form-builder table is present after fresh.
|
||||||
|
foreach (self::FORM_BUILDER_TABLES as $table) {
|
||||||
|
$this->assertTrue(Schema::hasTable($table), "{$table} should exist after migrate:fresh");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Roll back exactly the S1 migration steps.
|
||||||
|
Artisan::call('migrate:rollback', ['--step' => self::S1_MIGRATION_STEPS]);
|
||||||
|
|
||||||
|
// All form-builder tables should now be gone.
|
||||||
|
foreach (self::FORM_BUILDER_TABLES as $table) {
|
||||||
|
$this->assertFalse(Schema::hasTable($table), "{$table} should be dropped by rollback");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Legacy tables remain untouched by the rollback.
|
||||||
|
$this->assertTrue(Schema::hasTable('registration_form_fields'));
|
||||||
|
|
||||||
|
// Re-apply: tables are recreated, table list matches snapshot.
|
||||||
|
Artisan::call('migrate');
|
||||||
|
$afterTables = $this->tableList();
|
||||||
|
sort($beforeTables);
|
||||||
|
sort($afterTables);
|
||||||
|
$this->assertSame($beforeTables, $afterTables);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function test_user_profiles_populate_migration_down_clears_backfilled_rows(): void
|
||||||
|
{
|
||||||
|
Artisan::call('migrate:fresh');
|
||||||
|
|
||||||
|
// The populate migration ran during fresh. Assert it left rows for
|
||||||
|
// any users present at migrate time (test DB has none, so 0 is OK).
|
||||||
|
$populatedCount = DB::table('user_profiles')->count();
|
||||||
|
|
||||||
|
// down() of the populate migration deletes all profiles.
|
||||||
|
Artisan::call('migrate:rollback', ['--step' => self::S1_MIGRATION_STEPS - 1]);
|
||||||
|
Artisan::call('migrate:rollback', ['--step' => 1]); // populate step
|
||||||
|
// Next rollback step now drops the table — handled by the other test.
|
||||||
|
|
||||||
|
// Re-apply for clean state for subsequent tests.
|
||||||
|
Artisan::call('migrate');
|
||||||
|
|
||||||
|
// Sanity: counts can be compared before/after but tests are isolated
|
||||||
|
// per RefreshDatabase so we mainly assert no exceptions.
|
||||||
|
$this->assertSame($populatedCount, DB::table('user_profiles')->count());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array<int, string>
|
||||||
|
*/
|
||||||
|
private function tableList(): array
|
||||||
|
{
|
||||||
|
return collect(Schema::getTables())
|
||||||
|
->pluck('name')
|
||||||
|
->reject(fn (string $n) => str_starts_with($n, 'sqlite_') || $n === 'migrations')
|
||||||
|
->values()
|
||||||
|
->all();
|
||||||
|
}
|
||||||
|
}
|
||||||
95
api/tests/Unit/Models/FormBuilder/FormFieldTest.php
Normal file
95
api/tests/Unit/Models/FormBuilder/FormFieldTest.php
Normal file
@@ -0,0 +1,95 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Tests\Unit\Models\FormBuilder;
|
||||||
|
|
||||||
|
use App\Enums\FormBuilder\FormFieldType;
|
||||||
|
use App\Models\FormBuilder\FormField;
|
||||||
|
use App\Models\FormBuilder\FormSchema;
|
||||||
|
use App\Models\FormBuilder\FormSubmission;
|
||||||
|
use App\Models\FormBuilder\FormValue;
|
||||||
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||||
|
use Spatie\Activitylog\Models\Activity;
|
||||||
|
use Tests\TestCase;
|
||||||
|
|
||||||
|
final class FormFieldTest extends TestCase
|
||||||
|
{
|
||||||
|
use RefreshDatabase;
|
||||||
|
|
||||||
|
public function test_form_field_belongs_to_schema(): void
|
||||||
|
{
|
||||||
|
$schema = FormSchema::factory()->create();
|
||||||
|
$field = FormField::factory()->for($schema, 'schema')->create();
|
||||||
|
|
||||||
|
$this->assertSame($schema->id, $field->schema->id);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function test_form_field_stores_field_type_as_string(): void
|
||||||
|
{
|
||||||
|
$field = FormField::factory()->ofType(FormFieldType::SELECT)->create();
|
||||||
|
$this->assertSame('SELECT', $field->fresh()->field_type);
|
||||||
|
$this->assertIsString($field->fresh()->field_type);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function test_form_field_casts_binding_and_translations_to_array(): void
|
||||||
|
{
|
||||||
|
$field = FormField::factory()->create([
|
||||||
|
'binding' => ['mode' => 'entity_owned', 'entity' => 'person', 'column' => 'first_name'],
|
||||||
|
'translations' => ['en' => ['label' => 'First name']],
|
||||||
|
]);
|
||||||
|
$fresh = $field->fresh();
|
||||||
|
|
||||||
|
$this->assertIsArray($fresh->binding);
|
||||||
|
$this->assertSame('entity_owned', $fresh->binding['mode']);
|
||||||
|
$this->assertIsArray($fresh->translations);
|
||||||
|
$this->assertSame('First name', $fresh->translations['en']['label']);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function test_form_field_has_many_values(): void
|
||||||
|
{
|
||||||
|
$schema = FormSchema::factory()->create();
|
||||||
|
$field = FormField::factory()->for($schema, 'schema')->create();
|
||||||
|
$submissions = FormSubmission::factory()->count(2)->create(['form_schema_id' => $schema->id]);
|
||||||
|
foreach ($submissions as $submission) {
|
||||||
|
FormValue::create([
|
||||||
|
'form_field_id' => $field->id,
|
||||||
|
'form_submission_id' => $submission->id,
|
||||||
|
'value' => ['value' => 'x'],
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->assertCount(2, $field->fresh()->values);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function test_form_field_soft_deletes_preserve_values(): void
|
||||||
|
{
|
||||||
|
$schema = FormSchema::factory()->create();
|
||||||
|
$field = FormField::factory()->for($schema, 'schema')->create();
|
||||||
|
$submission = FormSubmission::factory()->create(['form_schema_id' => $schema->id]);
|
||||||
|
$value = FormValue::factory()->create([
|
||||||
|
'form_field_id' => $field->id,
|
||||||
|
'form_submission_id' => $submission->id,
|
||||||
|
]);
|
||||||
|
|
||||||
|
$field->delete();
|
||||||
|
|
||||||
|
$this->assertNotNull(FormValue::find($value->id));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function test_log_field_change_creates_activity_entry(): void
|
||||||
|
{
|
||||||
|
Activity::query()->delete();
|
||||||
|
$field = FormField::factory()->create();
|
||||||
|
|
||||||
|
$field->logFieldChange('field.binding_changed', ['from' => null, 'to' => ['mode' => 'entity_owned']]);
|
||||||
|
|
||||||
|
$entry = Activity::query()
|
||||||
|
->where('subject_type', $field->getMorphClass())
|
||||||
|
->where('subject_id', $field->id)
|
||||||
|
->first();
|
||||||
|
|
||||||
|
$this->assertNotNull($entry);
|
||||||
|
$this->assertSame('field.binding_changed', $entry->description);
|
||||||
|
}
|
||||||
|
}
|
||||||
90
api/tests/Unit/Models/FormBuilder/FormSchemaTest.php
Normal file
90
api/tests/Unit/Models/FormBuilder/FormSchemaTest.php
Normal file
@@ -0,0 +1,90 @@
|
|||||||
|
<?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'));
|
||||||
|
}
|
||||||
|
}
|
||||||
70
api/tests/Unit/Models/FormBuilder/FormSubmissionTest.php
Normal file
70
api/tests/Unit/Models/FormBuilder/FormSubmissionTest.php
Normal file
@@ -0,0 +1,70 @@
|
|||||||
|
<?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);
|
||||||
|
}
|
||||||
|
}
|
||||||
86
api/tests/Unit/Models/FormBuilder/FormValueTest.php
Normal file
86
api/tests/Unit/Models/FormBuilder/FormValueTest.php
Normal file
@@ -0,0 +1,86 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Tests\Unit\Models\FormBuilder;
|
||||||
|
|
||||||
|
use App\Enums\FormBuilder\FormFieldType;
|
||||||
|
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\Database\QueryException;
|
||||||
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||||
|
use Tests\TestCase;
|
||||||
|
|
||||||
|
final class FormValueTest extends TestCase
|
||||||
|
{
|
||||||
|
use RefreshDatabase;
|
||||||
|
|
||||||
|
public function test_form_value_belongs_to_submission_and_field(): void
|
||||||
|
{
|
||||||
|
$schema = FormSchema::factory()->create();
|
||||||
|
$field = FormField::factory()->for($schema, 'schema')->create();
|
||||||
|
$submission = FormSubmission::factory()->create(['form_schema_id' => $schema->id]);
|
||||||
|
|
||||||
|
$value = FormValue::factory()->create([
|
||||||
|
'form_submission_id' => $submission->id,
|
||||||
|
'form_field_id' => $field->id,
|
||||||
|
]);
|
||||||
|
|
||||||
|
$this->assertSame($submission->id, $value->submission->id);
|
||||||
|
$this->assertSame($field->id, $value->field->id);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function test_form_value_casts_value_to_array(): void
|
||||||
|
{
|
||||||
|
$schema = FormSchema::factory()->create();
|
||||||
|
$field = FormField::factory()->for($schema, 'schema')->create();
|
||||||
|
$submission = FormSubmission::factory()->create(['form_schema_id' => $schema->id]);
|
||||||
|
$value = FormValue::create([
|
||||||
|
'form_submission_id' => $submission->id,
|
||||||
|
'form_field_id' => $field->id,
|
||||||
|
'value' => ['value' => 'hallo'],
|
||||||
|
]);
|
||||||
|
|
||||||
|
$this->assertIsArray($value->fresh()->value);
|
||||||
|
$this->assertSame('hallo', $value->fresh()->value['value']);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function test_form_value_has_many_options(): void
|
||||||
|
{
|
||||||
|
$schema = FormSchema::factory()->create();
|
||||||
|
$field = FormField::factory()->for($schema, 'schema')->ofType(FormFieldType::MULTISELECT)->filterable()->create();
|
||||||
|
$submission = FormSubmission::factory()->create(['form_schema_id' => $schema->id]);
|
||||||
|
|
||||||
|
$value = FormValue::create([
|
||||||
|
'form_submission_id' => $submission->id,
|
||||||
|
'form_field_id' => $field->id,
|
||||||
|
'value' => ['A', 'B', 'C'],
|
||||||
|
]);
|
||||||
|
|
||||||
|
// Observer rebuilds the pivot on save.
|
||||||
|
$this->assertCount(3, $value->fresh()->options);
|
||||||
|
$this->assertSame(3, FormValueOption::where('form_value_id', $value->id)->count());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function test_form_value_is_unique_per_submission_field_pair(): void
|
||||||
|
{
|
||||||
|
$schema = FormSchema::factory()->create();
|
||||||
|
$field = FormField::factory()->for($schema, 'schema')->create();
|
||||||
|
$submission = FormSubmission::factory()->create(['form_schema_id' => $schema->id]);
|
||||||
|
FormValue::create([
|
||||||
|
'form_submission_id' => $submission->id,
|
||||||
|
'form_field_id' => $field->id,
|
||||||
|
'value' => ['value' => 'x'],
|
||||||
|
]);
|
||||||
|
|
||||||
|
$this->expectException(QueryException::class);
|
||||||
|
FormValue::create([
|
||||||
|
'form_submission_id' => $submission->id,
|
||||||
|
'form_field_id' => $field->id,
|
||||||
|
'value' => ['value' => 'y'],
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
}
|
||||||
118
api/tests/Unit/Models/UserProfileTest.php
Normal file
118
api/tests/Unit/Models/UserProfileTest.php
Normal file
@@ -0,0 +1,118 @@
|
|||||||
|
<?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')
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user