Files
Investidor-V2/tests/Feature/DeskServiceSchemaPrefillTest.php
2026-11-02 20:31:15 +02:00

68 lines
4.3 KiB
PHP

<?php
namespace Tests\Feature;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;
use Tests\TestCase;
final class DeskServiceSchemaPrefillTest extends TestCase
{
use RefreshDatabase;
public function test_new_requests_reuse_only_fields_and_default_options_present_in_their_own_definition(): void
{
$this->withoutVite();
$manager = User::factory()->create();
$investor = User::factory()->create();
config(['portal.managers' => [$manager->email]]);
$url = '/management/balcao/servicos/project-change';
$definition = config('virtual-desk.services.project-change');
$publish = fn (int $base, array $schema) => $this->actingAs($manager)->putJson($url, [
'base_version' => $base, 'definition' => $schema, 'confirm' => true,
'reason' => 'Synthetic QA schema configuration, without actual applicants.',
])->assertCreated()->json('id');
$v1 = $publish(0, $definition);
$case = $this->actingAs($investor)->postJson('/balcao-virtual/dossiers', [
'title' => 'Synthetic project title', 'client_key' => (string) Str::uuid(), 'reuse_consent' => true,
])->assertCreated()->json('id');
$create = fn (string $version) => $this->actingAs($investor)->postJson('/balcao-virtual/dossiers/'.$case.'/pedidos', [
'service_version_id' => $version, 'client_key' => (string) Str::uuid(),
])->assertCreated()->json('id');
$old = $create($v1);
$oldPayload = DB::table('desk_requests')->where('id', $old)->value('payload');
$this->getJson('/balcao-virtual/pedidos/'.$old)->assertOk()->assertJsonPath('request.payload.title', 'Synthetic project title')
->assertJsonPath('request.payload.regime', 'unknown');
unset($definition['fields']['title'], $definition['fields']['description'], $definition['fields']['regime']);
$v2 = $publish(1, $definition);
$next = $create($v2);
$requestUrl = '/balcao-virtual/pedidos/'.$next;
$view = $this->getJson($requestUrl)->assertOk()->assertJsonMissingPath('request.payload.title')
->assertJsonMissingPath('request.payload.description')->assertJsonMissingPath('request.payload.regime')->json();
$answers = [...$view['request']['payload'], 'original_reference' => 'SYNTHETIC-REFERENCE', 'change_reason' => 'Synthetic reason'];
$this->putJson($requestUrl, ['version' => 1, 'payload' => [...$answers, 'unconfigured' => 'not allowed']])
->assertUnprocessable()->assertJsonValidationErrors('payload');
$this->putJson($requestUrl, ['version' => 1, 'payload' => $answers])->assertOk()->assertJsonPath('status', 'draft');
$this->putJson($requestUrl, ['version' => 2, 'payload' => $answers, 'submit' => true, 'consent' => true])
->assertOk()->assertJsonPath('status', 'submitted');
$this->assertSame($oldPayload, DB::table('desk_requests')->where('id', $old)->value('payload'));
$this->getJson('/balcao-virtual/pedidos/'.$old)->assertOk()->assertJsonPath('request.payload.title', 'Synthetic project title')
->assertJsonPath('request.payload.regime', 'unknown')->assertJsonPath('definition.fields.title.required', true);
$definition['fields']['regime'] = ['label' => 'Synthetic configured choice', 'type' => 'select', 'required' => true,
'options' => ['configured' => 'Synthetic configured option']];
$v3 = $publish(2, $definition);
$configured = $create($v3);
$configuredUrl = '/balcao-virtual/pedidos/'.$configured;
$view = $this->getJson($configuredUrl)->assertOk()->assertJsonMissingPath('request.payload.regime')->json();
$answers = [...$view['request']['payload'], 'original_reference' => 'SYNTHETIC-REFERENCE', 'change_reason' => 'Synthetic reason'];
$this->putJson($configuredUrl, ['version' => 1, 'payload' => $answers, 'submit' => true, 'consent' => true])
->assertUnprocessable()->assertJsonValidationErrors('payload.regime');
$this->putJson($configuredUrl, ['version' => 1, 'payload' => [...$answers, 'regime' => 'configured'], 'submit' => true, 'consent' => true])
->assertOk()->assertJsonPath('status', 'submitted');
$this->assertSame(3, DB::table('desk_service_versions')->count());
}
}