Files
crewli/api/tests/Feature/FormBuilder/ValidationRules/InsertFromLibraryCopiesRulesTest.php

71 lines
2.6 KiB
PHP

<?php
declare(strict_types=1);
namespace Tests\Feature\FormBuilder\ValidationRules;
use App\Enums\FormBuilder\FormFieldValidationRuleType;
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\Services\FormBuilder\FormFieldService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
/**
* FormFieldService::insertFromLibrary must row-copy validation rules from
* the library entry to the new field (addendum Q3 row-copy mandate, paired
* with the existing bindings row-copy from WS-5a).
*/
final class InsertFromLibraryCopiesRulesTest extends TestCase
{
use RefreshDatabase;
public function test_library_rules_are_copied_to_new_field(): void
{
$org = Organisation::factory()->create();
$library = FormFieldLibrary::factory()->create([
'organisation_id' => $org->id,
'slug' => 'kenteken',
]);
FormFieldValidationRule::factory()->forLibrary($library)
->ofType(FormFieldValidationRuleType::Regex, ['pattern' => '/^[A-Z]{3}$/'])->create();
FormFieldValidationRule::factory()->forLibrary($library)
->ofType(FormFieldValidationRuleType::MaxLength, ['value' => 8])->create();
$schema = FormSchema::factory()->create(['organisation_id' => $org->id]);
/** @var FormField $field */
$field = app(FormFieldService::class)->insertFromLibrary($schema, $library);
$rules = FormFieldValidationRule::query()
->where('owner_type', 'form_field')
->where('owner_id', $field->id)
->get()
->keyBy(fn ($r) => $r->rule_type->value);
$this->assertCount(2, $rules);
$this->assertSame('/^[A-Z]{3}$/', $rules['regex']->parameters['pattern']);
$this->assertSame(8, $rules['max_length']->parameters['value']);
}
public function test_library_without_rules_produces_field_without_rules(): void
{
$org = Organisation::factory()->create();
$library = FormFieldLibrary::factory()->create(['organisation_id' => $org->id]);
$schema = FormSchema::factory()->create(['organisation_id' => $org->id]);
/** @var FormField $field */
$field = app(FormFieldService::class)->insertFromLibrary($schema, $library);
$count = FormFieldValidationRule::query()
->where('owner_type', 'form_field')
->where('owner_id', $field->id)
->count();
$this->assertSame(0, $count);
}
}