76 lines
2.9 KiB
PHP
76 lines
2.9 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Tests\Feature\FormBuilder\ValidationRules;
|
|
|
|
use App\Enums\FormBuilder\FormFieldType;
|
|
use App\Enums\FormBuilder\FormFieldValidationRuleType;
|
|
use App\Http\Resources\FormBuilder\FormFieldLibraryResource;
|
|
use App\Http\Resources\FormBuilder\FormFieldResource;
|
|
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 Illuminate\Foundation\Testing\RefreshDatabase;
|
|
use Tests\TestCase;
|
|
|
|
/**
|
|
* API-resource contract: the `validation_rules` key in the resource output
|
|
* is now sourced from the relational table via
|
|
* `FormFieldValidationRuleService::toJsonShape()`. The shape is the
|
|
* canonical flat bag (new post-WS-5b key names). Parity means: same
|
|
* semantic rule → same bag entry.
|
|
*/
|
|
final class ValidationRulesResourceParityTest extends TestCase
|
|
{
|
|
use RefreshDatabase;
|
|
|
|
public function test_form_field_resource_emits_canonical_shape_from_relational_rules(): void
|
|
{
|
|
$org = Organisation::factory()->create();
|
|
$schema = FormSchema::factory()->create(['organisation_id' => $org->id]);
|
|
$field = FormField::factory()->create([
|
|
'form_schema_id' => $schema->id,
|
|
'field_type' => FormFieldType::TEXT->value,
|
|
]);
|
|
FormFieldValidationRule::factory()->forField($field)
|
|
->ofType(FormFieldValidationRuleType::MinLength, ['value' => 3])->create();
|
|
FormFieldValidationRule::factory()->forField($field)
|
|
->ofType(FormFieldValidationRuleType::MaxLength, ['value' => 40])->create();
|
|
|
|
$rendered = (new FormFieldResource($field->fresh()))->toArray(request());
|
|
|
|
$this->assertSame([
|
|
'min_length' => 3,
|
|
'max_length' => 40,
|
|
], $rendered['validation_rules']);
|
|
}
|
|
|
|
public function test_form_field_resource_emits_null_when_no_rules(): void
|
|
{
|
|
$org = Organisation::factory()->create();
|
|
$schema = FormSchema::factory()->create(['organisation_id' => $org->id]);
|
|
$field = FormField::factory()->create(['form_schema_id' => $schema->id]);
|
|
|
|
$rendered = (new FormFieldResource($field->fresh()))->toArray(request());
|
|
|
|
$this->assertNull($rendered['validation_rules']);
|
|
}
|
|
|
|
public function test_form_field_library_resource_emits_canonical_shape(): void
|
|
{
|
|
$org = Organisation::factory()->create();
|
|
$library = FormFieldLibrary::factory()->create(['organisation_id' => $org->id]);
|
|
FormFieldValidationRule::factory()->forLibrary($library)
|
|
->ofType(FormFieldValidationRuleType::AllowedMimeTypes, ['mime_types' => ['image/png', 'image/jpeg']])->create();
|
|
|
|
$rendered = (new FormFieldLibraryResource($library->fresh()))->toArray(request());
|
|
|
|
$this->assertSame([
|
|
'allowed_mime_types' => ['image/png', 'image/jpeg'],
|
|
], $rendered['validation_rules']);
|
|
}
|
|
}
|