Files
crewli/api/tests/Feature/Api/V1/Public/FormBuilder/IdentityMatchOnSubmitTest.php
bert.hausmans 1afe11609a test(form-builder): WS-6 v1.3-delta D2 tests
~30 new tests + 6 modified covering D2 deliverables.

NEW test files:
- FormSubmissionSubmittedListenerOrderTest: rewritten — flips
  identity-match assertion from sync to ShouldQueue + adds AST-level
  structural guard that every queued listener has the
  apply_status=COMPLETED gate as an early statement
  (form-builder.queued-listener.skipped_apply_failed log line + ApplyStatus::COMPLETED check).
- TriggerPersonIdentityMatchOnFormSubmitTest: rewritten — drops
  failsafe-pad assertions; adds gate-skip tests (null/PENDING/PARTIAL/FAILED);
  invariant-violation throw test; broadcast-dispatch test.
- ApplyBindingsOnFormSubmitTest: extended — initial
  identity_match_status='pending' write, apply_completed_at on both
  paths, classifier-derived failure_response_code per exception subclass,
  unknown_error fallback, deadline wrapper invocation captured by
  test double, outer-transaction failure record.
- SyncTagPickerSelectionsOnSubmitGateTest (NEW): canonical skip-log
  assertion for null/PENDING/PARTIAL/FAILED apply_status; no-skip-log
  assertion for COMPLETED. Uses Log::spy because FormTagSyncService
  is final and can't be Mockery-mocked.
- FormBindingApplicatorDeadlineTest (NEW): withDeadline returns clone;
  no-deadline path; generous-deadline path; timeout exception thrown
  with correct submissionId + reasonCode (temporary_error inherited
  via FormBindingInfraException). Uses incident_report purpose for
  anonymous-allowed branch to avoid PersonProvisioner constraints.
- RetryServiceFailureClassifierTest (NEW): per-subclass
  failure_response_code mapping in recordFailure; apply_completed_at
  symmetry-fix coverage.
- SubmissionChannelAuthTest (NEW): submitter authorised, other user
  denied, missing submission denied, org admin currently denied
  (locks v1 contract per BACKLOG TECH-CHANNEL-AUTH-ORG-ADMIN).
- FormSubmissionResourceIdentityMatchTest: extended — DataProvider
  iterates over all six non-person purposes asserting
  identity_match=null per RFC §Q2 v1.3 contract.

MODIFIED to fit v1.3 layout:
- IdentityMatchOnSubmitTest: rewritten — directly invokes the listener
  with apply_status=COMPLETED pre-set, mirroring ApplyBindings'
  happy-path output (the test fixtures lack an identity-key binding
  so going through full event dispatch fails at PersonProvisioner).
  Drops the failsafe-pad assertion in test_public_submission_marked_pending;
  replaces with v1.3 contract: subject_type=null leaves
  identity_match_status untouched.
- TagPickerSyncListenerTest: same fix — sets apply_status=COMPLETED
  on the submission and invokes the listener directly.

Full suite: 1621 passing (4281 assertions). Larastan: 0 errors.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-08 03:20:27 +02:00

205 lines
7.2 KiB
PHP

<?php
declare(strict_types=1);
namespace Tests\Feature\Api\V1\Public\FormBuilder;
use App\Enums\FormBuilder\ApplyStatus;
use App\Enums\FormBuilder\FormFieldType;
use App\Enums\FormBuilder\FormPurpose;
use App\Events\FormBuilder\FormSubmissionSubmitted;
use App\Listeners\FormBuilder\TriggerPersonIdentityMatchOnFormSubmit;
use App\Models\CrowdType;
use App\Models\Event;
use App\Models\FormBuilder\FormField;
use App\Models\FormBuilder\FormSchema;
use App\Models\FormBuilder\FormSubmission;
use App\Models\Organisation;
use App\Models\Person;
use App\Models\User;
use Database\Seeders\RoleSeeder;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
/**
* ARCH §31.1 — TriggerPersonIdentityMatchOnFormSubmit contract.
* Verifies: (a) only fires for event_registration, (b) matched/pending/
* none states written correctly, (c) listener failures never rethrow.
*
* D2 update: post-RFC-WS-6 v1.3, the listener is queued + gated on
* apply_status=COMPLETED. These tests bypass the full submit pipeline
* (no real bindings configured) and directly invoke the listener with
* apply_status=COMPLETED pre-set on the submission, mirroring the
* gate-passing happy-path output of ApplyBindingsOnFormSubmit.
*/
final class IdentityMatchOnSubmitTest extends TestCase
{
use RefreshDatabase;
private Organisation $org;
private Event $event;
private CrowdType $crowdType;
private FormSchema $schema;
protected function setUp(): void
{
parent::setUp();
$this->seed(RoleSeeder::class);
$this->org = Organisation::factory()->create();
$this->event = Event::factory()->create(['organisation_id' => $this->org->id]);
$this->crowdType = CrowdType::factory()->systemType('VOLUNTEER')->create([
'organisation_id' => $this->org->id,
]);
$this->schema = FormSchema::factory()->create([
'organisation_id' => $this->org->id,
'purpose' => FormPurpose::EVENT_REGISTRATION,
'owner_type' => 'event',
'owner_id' => $this->event->id,
]);
FormField::factory()->create([
'form_schema_id' => $this->schema->id,
'field_type' => FormFieldType::TEXT->value,
'slug' => 'naam',
'is_portal_visible' => true,
]);
}
/**
* Build a submission with apply_status=COMPLETED pre-set so the v1.3
* gate passes. Then invoke the listener directly — bypasses
* ApplyBindings (which would fail without a properly-configured
* schema with identity-key binding + default_crowd_type_id).
*
* @param array<string, mixed> $overrides
*/
private function submitAndDispatch(array $overrides): FormSubmission
{
$submission = FormSubmission::factory()->create(array_merge([
'form_schema_id' => $this->schema->id,
'status' => 'submitted',
'submitted_at' => now(),
], $overrides));
$submission->apply_status = ApplyStatus::COMPLETED;
$submission->save();
$listener = $this->app->make(TriggerPersonIdentityMatchOnFormSubmit::class);
$listener->handle(new FormSubmissionSubmitted($submission->fresh()));
return $submission->fresh();
}
public function test_event_registration_triggers_matched_when_person_has_user(): void
{
$user = User::factory()->create();
$person = Person::factory()->create([
'event_id' => $this->event->id,
'crowd_type_id' => $this->crowdType->id,
'user_id' => $user->id,
]);
$submission = $this->submitAndDispatch([
'subject_type' => 'person',
'subject_id' => $person->id,
]);
$this->assertSame('matched', $submission->identity_match_status);
}
public function test_event_registration_triggers_none_when_person_unlinked_no_match(): void
{
$person = Person::factory()->create([
'event_id' => $this->event->id,
'crowd_type_id' => $this->crowdType->id,
'user_id' => null,
'email' => 'nobody@nowhere.test',
'first_name' => 'Xyz',
'last_name' => 'NoMatch',
]);
$submission = $this->submitAndDispatch([
'subject_type' => 'person',
'subject_id' => $person->id,
]);
$this->assertSame('none', $submission->identity_match_status);
}
public function test_event_registration_triggers_pending_when_matcher_finds_candidate(): void
{
// Pre-seed a User with a specific email, then a Person in the same
// org with the matching email → detectMatches should create a
// PersonIdentityMatch with status=pending.
$user = User::factory()->create(['email' => 'match@example.test']);
$this->org->users()->attach($user, ['role' => 'org_member']);
$person = Person::factory()->create([
'event_id' => $this->event->id,
'crowd_type_id' => $this->crowdType->id,
'user_id' => null,
'email' => 'match@example.test',
'first_name' => 'Anne',
'last_name' => 'Match',
]);
$submission = $this->submitAndDispatch([
'subject_type' => 'person',
'subject_id' => $person->id,
]);
$this->assertSame('pending', $submission->identity_match_status);
}
public function test_non_event_registration_purpose_does_not_trigger(): void
{
$otherSchema = FormSchema::factory()->create([
'organisation_id' => $this->org->id,
'purpose' => FormPurpose::INCIDENT_REPORT,
]);
$submission = FormSubmission::factory()->create([
'form_schema_id' => $otherSchema->id,
'subject_type' => null,
'subject_id' => null,
'status' => 'submitted',
'submitted_at' => now(),
]);
$submission->apply_status = ApplyStatus::COMPLETED;
$submission->save();
$listener = $this->app->make(TriggerPersonIdentityMatchOnFormSubmit::class);
$listener->handle(new FormSubmissionSubmitted($submission->fresh()));
$this->assertNull($submission->fresh()->identity_match_status);
}
/**
* v1.3 contract change: the failsafe-pad ("subject_id null → write
* 'pending' + log warning") is gone. With apply_status=COMPLETED but
* subject_type='person' AND subject_id=null, the listener throws
* IdentityMatchInvariantViolation per RFC §Q2. With subject_type=null
* (this test's scenario), the listener simply returns at the
* subject-type check without writing anything.
*/
public function test_public_submission_with_null_subject_does_not_write_status(): void
{
$submission = FormSubmission::factory()->create([
'form_schema_id' => $this->schema->id,
'subject_type' => null,
'subject_id' => null,
'status' => 'submitted',
'submitted_at' => now(),
]);
$submission->apply_status = ApplyStatus::COMPLETED;
$submission->save();
$listener = $this->app->make(TriggerPersonIdentityMatchOnFormSubmit::class);
$listener->handle(new FormSubmissionSubmitted($submission->fresh()));
$this->assertNull($submission->fresh()->identity_match_status);
}
}