107 lines
3.8 KiB
PHP
107 lines
3.8 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Tests\Feature\FormBuilder\Bindings;
|
|
|
|
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;
|
|
|
|
/**
|
|
* The pre-publish check (`FormSchemaService::publish()`,
|
|
* `PurposeRequirementsNotMetException`, ARCH §17.3) now reads from
|
|
* `form_field_bindings`. External contract (purposeSlug +
|
|
* missingBindings[]) unchanged.
|
|
*/
|
|
final class PublishChecksRelationalBindingsTest 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);
|
|
}
|
|
|
|
public function test_publish_succeeds_when_all_required_bindings_are_in_relational_table(): void
|
|
{
|
|
$schema = $this->service->create(
|
|
$this->org,
|
|
['name' => 'ER', 'purpose' => FormPurpose::EVENT_REGISTRATION->value],
|
|
$this->actor,
|
|
);
|
|
|
|
FormField::factory()->withEntityBinding('person', 'email')->create(['form_schema_id' => $schema->id]);
|
|
FormField::factory()->withEntityBinding('person', 'first_name')->create(['form_schema_id' => $schema->id]);
|
|
FormField::factory()->withEntityBinding('person', 'last_name')->create(['form_schema_id' => $schema->id]);
|
|
|
|
$published = $this->service->publish($schema->fresh(), $this->actor);
|
|
|
|
$this->assertTrue((bool) $published->is_published);
|
|
}
|
|
|
|
public function test_publish_fails_when_required_binding_missing_reports_exact_paths(): void
|
|
{
|
|
$schema = $this->service->create(
|
|
$this->org,
|
|
['name' => 'ER-partial', 'purpose' => FormPurpose::EVENT_REGISTRATION->value],
|
|
$this->actor,
|
|
);
|
|
FormField::factory()->withEntityBinding('person', 'email')->create(['form_schema_id' => $schema->id]);
|
|
|
|
try {
|
|
$this->service->publish($schema->fresh(), $this->actor);
|
|
$this->fail('Expected PurposeRequirementsNotMetException');
|
|
} catch (PurposeRequirementsNotMetException $e) {
|
|
$this->assertSame('event_registration', $e->purposeSlug);
|
|
$this->assertSame(['person.first_name', 'person.last_name'], $e->missingBindings);
|
|
}
|
|
}
|
|
|
|
public function test_publish_ignores_bindings_belonging_to_other_schemas(): void
|
|
{
|
|
$schemaA = $this->service->create(
|
|
$this->org,
|
|
['name' => 'A', 'purpose' => FormPurpose::SUPPLIER_INTAKE->value],
|
|
$this->actor,
|
|
);
|
|
$schemaB = $this->service->create(
|
|
$this->org,
|
|
['name' => 'B', 'purpose' => FormPurpose::SUPPLIER_INTAKE->value],
|
|
$this->actor,
|
|
);
|
|
|
|
FormField::factory()->withEntityBinding('company', 'contact_email')->create(['form_schema_id' => $schemaB->id]);
|
|
|
|
try {
|
|
$this->service->publish($schemaA->fresh(), $this->actor);
|
|
$this->fail('Expected PurposeRequirementsNotMetException');
|
|
} catch (PurposeRequirementsNotMetException $e) {
|
|
$this->assertSame('supplier_intake', $e->purposeSlug);
|
|
$this->assertSame(['company.name'], $e->missingBindings);
|
|
}
|
|
}
|
|
}
|