Files
crewli/api/tests/Feature/FormBuilder/Purposes/PurposeSchemaLifecycleTest.php
bert.hausmans 55ba4f24c0 test(form-builder): cover purpose registry and morph-map alignment
- PurposeRegistryTest: all seven purposes load with expected shape;
  `get()` throws PurposeNotFoundException on unknown slug;
  `allSubjectTypes()` returns exactly [artist, company, person, user];
  `publicAccessibleSlugs()` is only `[event_registration]`.
- PurposeSchemaLifecycleTest: data-provider-driven create → publish
  for all seven purposes; negative tests for event_registration (three
  missing bindings) and supplier_intake (company.name missing); partial
  binding test reports only the missing subset.
- CustomPurposeEscapeRemovedTest: column gone, config file gone,
  FormPurpose::CUSTOM gone, store endpoint rejects `'custom'`, resource
  payload omits the field.
- SubjectTypeRegistryConsolidationTest: submission validation accepts
  registry subject types, rejects everything else including the legacy
  `event` alias that used to be allowed.
- MorphMapAlignmentTest: compile-time guard that every
  PurposeRegistry::allSubjectTypes() alias appears in the morph-map and
  in AppServiceProvider::PURPOSE_SUBJECT_FQCN.
- FormPurposeTest rewritten to cover the seven v1.0 cases and the
  registry-delegation helpers (now extends Tests\TestCase for the
  container).
- Public/listener tests swap the removed PUBLIC_RSVP / PUBLIC_COMPLAINT
  / FEEDBACK references for valid v1.0 purposes, preserving their
  negative-path assertions.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-24 14:36:09 +02:00

160 lines
5.5 KiB
PHP

<?php
declare(strict_types=1);
namespace Tests\Feature\FormBuilder\Purposes;
use App\Enums\FormBuilder\FormFieldType;
use App\Enums\FormBuilder\FormPurpose;
use App\Exceptions\FormBuilder\PurposeRequirementsNotMetException;
use App\Models\FormBuilder\FormField;
use App\Models\FormBuilder\FormSchema;
use App\Models\Organisation;
use App\Models\User;
use App\Services\FormBuilder\FormSchemaService;
use Database\Seeders\RoleSeeder;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
/**
* Smoke-tests for the seven v1.0 purposes. Each purpose must support
* create → publish end-to-end (with required bindings present). The two
* purposes that declare required bindings (`event_registration`,
* `supplier_intake`) also have negative tests that assert the pre-publish
* guard fires.
*/
final class PurposeSchemaLifecycleTest extends TestCase
{
use RefreshDatabase;
private Organisation $org;
private User $actor;
private FormSchemaService $service;
protected function setUp(): void
{
parent::setUp();
$this->seed(RoleSeeder::class);
$this->org = Organisation::factory()->create();
$this->actor = User::factory()->create();
$this->org->users()->attach($this->actor, ['role' => 'org_admin']);
$this->actor->assignRole('org_admin');
setPermissionsTeamId($this->org->id);
$this->service = $this->app->make(FormSchemaService::class);
}
/** @return iterable<string, array{FormPurpose}> */
public static function purposeProvider(): iterable
{
foreach (FormPurpose::cases() as $case) {
yield $case->value => [$case];
}
}
/** @dataProvider purposeProvider */
public function test_create_and_publish_succeeds_for_purpose(FormPurpose $purpose): void
{
$schema = $this->service->create(
$this->org,
[
'name' => 'Schema '.$purpose->value,
'purpose' => $purpose->value,
],
$this->actor,
);
$this->seedRequiredBindings($schema, $purpose);
$published = $this->service->publish($schema->fresh('fields'), $this->actor);
$this->assertTrue((bool) $published->is_published);
$this->assertSame($purpose->value, $published->purpose->value ?? $published->purpose);
}
public function test_event_registration_without_required_bindings_fails_publish(): void
{
$schema = $this->service->create(
$this->org,
['name' => 'ER', 'purpose' => FormPurpose::EVENT_REGISTRATION->value],
$this->actor,
);
try {
$this->service->publish($schema->fresh('fields'), $this->actor);
$this->fail('Expected PurposeRequirementsNotMetException');
} catch (PurposeRequirementsNotMetException $e) {
$this->assertSame('event_registration', $e->purposeSlug);
$this->assertSame(
['person.email', 'person.first_name', 'person.last_name'],
$e->missingBindings,
);
}
}
public function test_supplier_intake_without_company_name_binding_fails_publish(): void
{
$schema = $this->service->create(
$this->org,
['name' => 'SI', 'purpose' => FormPurpose::SUPPLIER_INTAKE->value],
$this->actor,
);
try {
$this->service->publish($schema->fresh('fields'), $this->actor);
$this->fail('Expected PurposeRequirementsNotMetException');
} catch (PurposeRequirementsNotMetException $e) {
$this->assertSame('supplier_intake', $e->purposeSlug);
$this->assertSame(['company.name'], $e->missingBindings);
}
}
public function test_event_registration_partial_bindings_reports_only_missing(): void
{
$schema = $this->service->create(
$this->org,
['name' => 'ER-partial', 'purpose' => FormPurpose::EVENT_REGISTRATION->value],
$this->actor,
);
$this->addBindingField($schema, 'person', 'email', 'email');
try {
$this->service->publish($schema->fresh('fields'), $this->actor);
$this->fail('Expected PurposeRequirementsNotMetException');
} catch (PurposeRequirementsNotMetException $e) {
$this->assertSame(
['person.first_name', 'person.last_name'],
$e->missingBindings,
);
}
}
private function seedRequiredBindings(FormSchema $schema, FormPurpose $purpose): void
{
match ($purpose) {
FormPurpose::EVENT_REGISTRATION => [
$this->addBindingField($schema, 'person', 'email', 'email'),
$this->addBindingField($schema, 'person', 'first_name', 'first_name'),
$this->addBindingField($schema, 'person', 'last_name', 'last_name'),
],
FormPurpose::SUPPLIER_INTAKE => [
$this->addBindingField($schema, 'company', 'name', 'company_name'),
],
default => null,
};
}
private function addBindingField(FormSchema $schema, string $entity, string $column, string $slug): FormField
{
return FormField::factory()->create([
'form_schema_id' => $schema->id,
'field_type' => FormFieldType::TEXT,
'slug' => $slug,
'label' => ucfirst($slug),
'binding' => ['mode' => 'entity_owned', 'entity' => $entity, 'column' => $column],
]);
}
}