feat(form-builder): form_field_validation_rules table + polymorphic owner + scope + cascade
This commit is contained in:
@@ -0,0 +1,80 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Tests\Feature\FormBuilder\ValidationRules;
|
||||
|
||||
use App\Models\FormBuilder\FormField;
|
||||
use App\Models\FormBuilder\FormFieldLibrary;
|
||||
use App\Models\FormBuilder\FormFieldValidationRule;
|
||||
use App\Models\FormBuilder\FormSchema;
|
||||
use App\Models\Organisation;
|
||||
use App\Models\Scopes\FormFieldValidationRuleScope;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Routing\Route;
|
||||
use Tests\TestCase;
|
||||
|
||||
/**
|
||||
* Asserts `FormFieldValidationRuleScope` isolates rules per organisation
|
||||
* across both owner types (form_field and form_field_library) and that
|
||||
* the `withoutGlobalScope` escape hatch exposes cross-org rows. Sibling
|
||||
* coverage to `FormFieldBindingScopeTest`.
|
||||
*/
|
||||
final class FormFieldValidationRuleScopeTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
public function test_scope_isolates_rules_per_organisation_for_both_owner_types(): void
|
||||
{
|
||||
[$orgA, $fieldA, $libraryA] = $this->seedOrgWithRules();
|
||||
[$orgB, $fieldB, $libraryB] = $this->seedOrgWithRules();
|
||||
|
||||
$this->withOrgRoute($orgA);
|
||||
$ownerIdsA = FormFieldValidationRule::query()->pluck('owner_id')->sort()->values()->all();
|
||||
$expectedA = collect([$fieldA->id, $libraryA->id])->sort()->values()->all();
|
||||
$this->assertSame($expectedA, $ownerIdsA);
|
||||
|
||||
$this->withOrgRoute($orgB);
|
||||
$ownerIdsB = FormFieldValidationRule::query()->pluck('owner_id')->sort()->values()->all();
|
||||
$expectedB = collect([$fieldB->id, $libraryB->id])->sort()->values()->all();
|
||||
$this->assertSame($expectedB, $ownerIdsB);
|
||||
}
|
||||
|
||||
public function test_without_global_scope_exposes_cross_org(): void
|
||||
{
|
||||
[$orgA, , ] = $this->seedOrgWithRules();
|
||||
$this->seedOrgWithRules();
|
||||
|
||||
$this->withOrgRoute($orgA);
|
||||
|
||||
$this->assertSame(
|
||||
4,
|
||||
FormFieldValidationRule::query()
|
||||
->withoutGlobalScope(FormFieldValidationRuleScope::class)
|
||||
->count(),
|
||||
);
|
||||
$this->assertSame(2, FormFieldValidationRule::query()->count());
|
||||
}
|
||||
|
||||
/** @return array{0:Organisation,1:FormField,2:FormFieldLibrary} */
|
||||
private function seedOrgWithRules(): array
|
||||
{
|
||||
$org = Organisation::factory()->create();
|
||||
$schema = FormSchema::factory()->create(['organisation_id' => $org->id]);
|
||||
$field = FormField::factory()->create(['form_schema_id' => $schema->id]);
|
||||
$library = FormFieldLibrary::factory()->create(['organisation_id' => $org->id]);
|
||||
|
||||
FormFieldValidationRule::factory()->forField($field)->create();
|
||||
FormFieldValidationRule::factory()->forLibrary($library)->create();
|
||||
|
||||
return [$org, $field, $library];
|
||||
}
|
||||
|
||||
private function withOrgRoute(Organisation $org): void
|
||||
{
|
||||
$route = new Route(['GET'], '/_test', static fn () => null);
|
||||
$route->bind(request());
|
||||
$route->setParameter('organisation', $org);
|
||||
request()->setRouteResolver(static fn () => $route);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user