f = receiptFixture(); $this->actingAs($this->f['issuer']); $this->partial = ['operation_key' => (string) Str::uuid(), 'priority' => 'normal', 'justification' => 'Ini', 'items' => [ ['description' => 'Artigo sintético em preparação', 'purchase_type' => 'consumable', 'quantity' => null, 'unit_price' => null, 'tax_rate' => '16.00', 'unexpected' => 'Do not persist unrelated data'], ]]; }); test('new and existing requisition form history is encrypted without creating a process on read', function () { $count = Requisition::query()->count(); $this->get('/requisicoes/criar')->assertOk()->assertViewHas('page', fn (array $page) => $page['encryptHistory'] === true); expect(Requisition::query()->count())->toBe($count); $this->postJson('/requisicoes', $this->partial)->assertRedirect(); $r = Requisition::query()->latest('id')->firstOrFail(); $this->get('/requisicoes/'.$r->id.'/editar')->assertOk()->assertViewHas('page', fn (array $page) => $page['encryptHistory'] === true); }); test('incomplete draft persists unknown values without invented amounts and cannot reserve or submit', function () { $entries = DB::table('budget_entries')->count(); $this->postJson('/requisicoes', $this->partial)->assertRedirect(); $r = Requisition::query()->latest('id')->firstOrFail(); expect($r->status)->toBe('draft')->and($r->total_minor)->toBeNull()->and($r->needed_by)->toBeNull() ->and($r->cost_center_id)->toBeNull()->and($r->draft_input['values']['items'][0]['unit_price'])->toBeNull() ->and($r->items()->count())->toBe(0)->and($r->steps()->count())->toBe(0); expect(array_key_exists('unexpected', $r->draft_input['values']['items'][0]))->toBeFalse(); $this->get('/requisicoes/'.$r->id.'/editar')->assertInertia(fn (Assert $page) => $page->where('incompleteDraftsAvailable', true)->where('requisition.draft_input.values.justification', 'Ini')); $this->get('/requisicoes/'.$r->id)->assertOk()->assertInertia(fn (Assert $page) => $page->where('requisition.total_minor', null)); $this->get('/requisicoes/'.$r->id.'/processo')->assertOk(); $this->get('/relatorios?search='.$r->number)->assertOk()->assertInertia(fn (Assert $page) => $page->where('records.data.0.total_minor', null)); $this->postJson('/requisicoes/'.$r->id.'/submeter')->assertUnprocessable(); expect($r->fresh()->status)->toBe('draft')->and(DB::table('budget_entries')->count())->toBe($entries); $this->postJson('/requisicoes', $this->partial)->assertRedirect(); expect(Requisition::query()->where('requester_id', $this->f['issuer']->id)->where('status', 'draft')->count())->toBe(1); }); test('completing the same draft restores exact calculations and clears preparation without duplicating the process', function () { $this->postJson('/requisicoes', $this->partial)->assertRedirect(); $r = Requisition::query()->latest('id')->firstOrFail(); $this->putJson('/requisicoes/'.$r->id, ['operation_key' => (string) Str::uuid(), 'process_version' => $r->process_version, 'cost_center_id' => $this->f['r']->cost_center_id, 'priority' => 'normal', 'needed_by' => today()->addWeek()->toDateString(), 'justification' => 'Preparação sintética agora completa.', 'items' => [['description' => 'Artigo sintético', 'category' => 'Material', 'purchase_type' => 'consumable', 'quantity' => '2.000', 'unit' => 'UN', 'unit_price' => '50.00', 'tax_rate' => '16.00']], ])->assertRedirect(); expect($r->fresh()->draft_input)->toBeNull()->and($r->fresh()->total_minor)->toBe(11600)->and($r->items()->count())->toBe(1)->and($r->fresh()->status)->toBe('draft'); }); test('partial preparation still rejects invalid supplied values and foreign cost centres', function () { foreach (['quantity' => '-1', 'unit_price' => '-1', 'tax_rate' => '101'] as $key => $value) { $body = $this->partial; $body['items'][0][$key] = $value; $this->postJson('/requisicoes', $body)->assertUnprocessable(); } $other = receiptFixture(); $this->actingAs($this->f['issuer']); $this->postJson('/requisicoes', [...$this->partial, 'cost_center_id' => $other['r']->cost_center_id])->assertUnprocessable(); }); test('rollback refuses to discard incomplete input and an empty draft can be saved', function () { $this->postJson('/requisicoes', ['items' => []])->assertRedirect(); $migration = require database_path('migrations/2026_11_22_000002_allow_incomplete_requisition_drafts.php'); expect(fn () => $migration->down())->toThrow(LogicException::class); expect(Requisition::query()->latest('id')->firstOrFail()->total_minor)->toBeNull(); }); test('migration preserves existing document values and legacy validation until explicitly installed', function () { $migration = require database_path('migrations/2026_11_22_000002_allow_incomplete_requisition_drafts.php'); $sqlite = DB::getDriverName() === 'sqlite'; if ($sqlite) { expect(fn () => $migration->down())->toThrow(LogicException::class); DB::getSchemaBuilder()->table('requisitions', fn ($table) => $table->dropColumn('draft_input')); } else { $migration->down(); } $columns = DB::getSchemaBuilder()->getColumnListing('requisitions'); $before = DB::table('requisitions')->select($columns)->orderBy('id')->get()->toArray(); try { $this->postJson('/requisicoes', $this->partial)->assertUnprocessable(); if ($sqlite) { DB::getSchemaBuilder()->table('requisitions', fn ($table) => $table->json('draft_input')->nullable()); } else { $migration->up(); } expect(DB::table('requisitions')->select($columns)->orderBy('id')->get()->toArray())->toEqual($before); } finally { if (! DB::getSchemaBuilder()->hasColumn('requisitions', 'draft_input')) { if ($sqlite) { DB::getSchemaBuilder()->table('requisitions', fn ($table) => $table->json('draft_input')->nullable()); } else { $migration->up(); } } } }); test('partial preparation remains author scoped and refuses stale concurrent edits', function () { $this->postJson('/requisicoes', $this->partial)->assertRedirect(); $r = Requisition::query()->latest('id')->firstOrFail(); $body = [...$this->partial, 'operation_key' => (string) Str::uuid(), 'process_version' => $r->process_version, 'justification' => 'Nova preparação parcial']; $this->putJson('/requisicoes/'.$r->id, $body)->assertRedirect(); $this->putJson('/requisicoes/'.$r->id, [...$body, 'operation_key' => (string) Str::uuid(), 'justification' => 'Edição concorrente'])->assertStatus(409); $other = User::factory()->create(['tenant_id' => $r->tenant_id, 'department_id' => $r->department_id, 'purchasing_role' => PurchasingRole::Requester]); $this->actingAs($other)->putJson('/requisicoes/'.$r->id, [...$body, 'process_version' => $r->fresh()->process_version])->assertForbidden(); expect($r->fresh()->draft_input['values']['justification'])->toBe('Nova preparação parcial'); }); test('PostgreSQL refuses a direct transition of incomplete preparation into approval', function () { if (DB::getDriverName() !== 'pgsql') { $this->markTestSkipped('Database constraint is PostgreSQL-specific; SQLite validates application behavior.'); } $this->postJson('/requisicoes', $this->partial)->assertRedirect(); $r = Requisition::query()->latest('id')->firstOrFail(); expect(fn () => DB::transaction(fn () => DB::table('requisitions')->where('id', $r->id)->update(['status' => 'pending'])))->toThrow(QueryException::class); expect($r->fresh()->status)->toBe('draft'); });