This commit is contained in:
2026-11-02 20:27:11 +02:00
parent b44378b899
commit badaada043
300 changed files with 17458 additions and 1056 deletions
+132 -1
View File
@@ -12,8 +12,11 @@ use App\Models\User;
use App\Services\ComparisonPolicy;
use App\Services\ProposalComparison;
use App\Services\RequisitionWorkflow;
use Illuminate\Support\Facades\DB;
use Inertia\Testing\AssertableInertia as Assert;
require_once __DIR__.'/../Support/SupplyFixture.php';
function supplierPayload(string $nuit = '123456789'): array
{
return ['name' => 'Fornecedor de teste', 'nuit' => $nuit, 'contact' => 'Contacto', 'email' => 'test@example.com', 'phone' => '123456789', 'address' => 'Maputo', 'category' => 'Material', 'beneficiaries' => 'Beneficiário teste', 'document_references' => 'Alvará e declaração verificados no processo 123.', 'diligence_notes' => 'Documentação conferida e sem impedimentos.', 'blocked' => false, 'next_review' => now()->addYear()->format('Y-m-d')];
@@ -149,7 +152,7 @@ test('configured comparison uses the preserved minimum and weights and retains i
$this->tenant->update(['comparison_policy' => ComparisonPolicy::DEFAULTS]);
expect(app(ProposalComparison::class)->inspect($this->r)['fingerprint'])->toBe($before['fingerprint']);
$this->get("/requisicoes/{$this->r->id}/cotacoes")->assertInertia(fn (Assert $page) => $page->where('comparisonPolicy.minimum_evaluators', 1)->where('ready', true));
$this->owner->update(['purchasing_role' => PurchasingRole::Finance]);
$this->owner->forceFill(['purchasing_role' => PurchasingRole::Finance])->save();
$this->actingAs($this->owner)->post("/cotacoes/{$quote->id}/avaliar", ['quality' => 8, 'history' => 5, 'payment' => 8, 'comment' => 'O solicitante continua impedido.', 'no_conflict' => true])->assertForbidden();
});
@@ -178,3 +181,131 @@ test('supplier quote and evaluation routes isolate companies', function () {
$this->get("/requisicoes/{$this->r->id}/cotacoes")->assertNotFound();
$this->post("/cotacoes/{$q->id}/avaliar", ['quality' => 8, 'history' => 5, 'payment' => 8, 'comment' => 'Tentativa de outro inquilino.', 'no_conflict' => true])->assertNotFound();
});
function quotationTaskRegister(object $test): Quotation
{
$test->actingAs($test->buyer)->post('/requisicoes/'.$test->r->id.'/cotacoes', $test->quote)->assertSessionHasNoErrors();
return Quotation::query()->sole();
}
function quotationTaskEvaluation(): array
{
return ['quality' => 8, 'history' => 5, 'payment' => 8, 'comment' => 'Avaliação sintética fundamentada.', 'no_conflict' => true];
}
test('quotation tasks link missing proposals and each independent evaluation to the existing forms', function () {
$path = '/requisicoes/'.$this->r->id.'/cotacoes';
$this->get('/requisicoes/tarefas?module=purchases')->assertInertia(fn (Assert $page) => $page->where('tasks.total', 1)->where('tasks.data.0.task', 'quotation_prepare'));
$this->get($path.'?prepare=1')->assertInertia(fn (Assert $page) => $page->where('prepare', true)->where('canCreate', true)->where('requisition.items.0.id', $this->item->id));
$quote = quotationTaskRegister($this);
$this->actingAs($this->finance)->get('/requisicoes/tarefas?module=purchases')->assertInertia(fn (Assert $page) => $page->where('tasks.total', 1)
->where('tasks.data.0.href', $path.'?quotation_id='.$quote->id.'#quotation-'.$quote->id)->missing('tasks.data.0.total_minor')->missing('tasks.data.0.evaluations'));
$this->get($path.'?quotation_id='.$quote->id)->assertHeader('Cache-Control', 'max-age=0, no-store, private')
->assertInertia(fn (Assert $page) => $page->where('focusQuotationId', $quote->id)->where('quotations.0.canEvaluate', true));
$this->post('/cotacoes/'.$quote->id.'/avaliar', quotationTaskEvaluation())->assertSessionHasNoErrors();
$this->get('/requisicoes/tarefas?module=purchases')->assertInertia(fn (Assert $page) => $page->where('tasks.total', 0));
$this->actingAs($this->buyer)->get('/requisicoes/tarefas?module=purchases')->assertInertia(fn (Assert $page) => $page->where('tasks.total', 2));
$this->post('/cotacoes/'.$quote->id.'/avaliar', quotationTaskEvaluation())->assertSessionHasNoErrors();
$this->get('/requisicoes/tarefas?module=purchases')->assertInertia(fn (Assert $page) => $page->where('tasks.total', 1)->where('tasks.data.0.task', 'quotation_prepare'));
});
test('quotation tasks use preserved comparison minima and hand the ready result to procurement', function () {
$policy = [...ComparisonPolicy::DEFAULTS, 'revision' => 1, 'configured' => true, 'minimum_proposals' => 1, 'minimum_evaluators' => 1];
$this->r->update(['comparison_policy' => $policy]);
$quote = quotationTaskRegister($this);
$this->actingAs($this->finance)->post('/cotacoes/'.$quote->id.'/avaliar', quotationTaskEvaluation())->assertSessionHasNoErrors();
$this->tenant->update(['comparison_policy' => [...$policy, 'revision' => 2, 'minimum_proposals' => 5, 'minimum_evaluators' => 5]]);
$this->actingAs($this->buyer)->get('/requisicoes/tarefas?module=purchases')->assertInertia(fn (Assert $page) => $page->where('tasks.total', 1)
->where('tasks.data.0.task', 'comparison_review')->where('tasks.data.0.href', '/requisicoes/'.$this->r->id.'/adjudicacao'));
$this->actingAs($this->finance)->get('/requisicoes/tarefas?module=purchases')->assertInertia(fn (Assert $page) => $page->where('tasks.total', 0));
expect(DB::table('purchase_awards')->count())->toBe(0)->and(DB::table('purchase_orders')->count())->toBe(0);
});
test('quotation tasks retain comparison impediments without asking for replacement data', function () {
$this->r->update(['comparison_policy' => [...ComparisonPolicy::DEFAULTS, 'revision' => 1, 'configured' => true, 'minimum_proposals' => 1]]);
$quote = quotationTaskRegister($this);
$quote->update(['valid_until' => today()->subDay()]);
$before = [DB::table('quotations')->get()->toArray(), DB::table('proposal_evaluations')->count(), DB::table('audit_logs')->count()];
$this->get('/requisicoes/tarefas?module=purchases')->assertInertia(fn (Assert $page) => $page->where('tasks.total', 1)
->where('tasks.data.0.task', 'quotation_check')->where('tasks.data.0.href', '/requisicoes/'.$this->r->id.'/cotacoes'));
$this->actingAs($this->finance)->get('/requisicoes/tarefas?module=purchases')->assertInertia(fn (Assert $page) => $page->where('tasks.total', 0));
$this->get('/requisicoes/'.$this->r->id.'/cotacoes?quotation_id='.$quote->id)->assertInertia(fn (Assert $page) => $page->where('quotations.0.canEvaluate', false));
expect([DB::table('quotations')->get()->toArray(), DB::table('proposal_evaluations')->count(), DB::table('audit_logs')->count()])->toEqual($before);
$this->post('/cotacoes/'.$quote->id.'/avaliar', quotationTaskEvaluation())->assertSessionHasErrors('evaluation');
});
test('quotation tasks respect requester independence department scope and quotation focus ownership', function () {
$quote = quotationTaskRegister($this);
$this->owner->forceFill(['purchasing_role' => PurchasingRole::Finance])->save();
$this->actingAs($this->owner)->get('/requisicoes/tarefas?module=purchases')->assertInertia(fn (Assert $page) => $page->where('tasks.total', 0));
$this->get('/requisicoes/'.$this->r->id.'/cotacoes')->assertOk()->assertInertia(fn (Assert $page) => $page->where('quotations.0.canEvaluate', false));
$department = Department::query()->create(['tenant_id' => $this->tenant->id, 'name' => 'Outra unidade fictícia']);
$head = User::factory()->create(['tenant_id' => $this->tenant->id, 'department_id' => $department->id, 'purchasing_role' => PurchasingRole::DepartmentHead]);
$this->actingAs($head)->get('/requisicoes/tarefas?module=purchases')->assertInertia(fn (Assert $page) => $page->where('tasks.total', 0));
$this->get('/requisicoes/'.$this->r->id.'/cotacoes')->assertNotFound();
$other = $this->r->replicate();
$other->number = 'REQ-OUTRA-ORIGEM';
$other->save();
$alien = $quote->replicate();
$alien->requisition_id = $other->id;
$alien->save();
$this->actingAs($this->finance)->getJson('/requisicoes/'.$this->r->id.'/cotacoes?quotation_id='.$alien->id)->assertUnprocessable();
});
test('quotation tasks never keep stale finance or procurement authority during a command', function () {
$quote = quotationTaskRegister($this);
$this->actingAs($this->finance);
DB::table('users')->where('id', $this->finance->id)->update(['purchasing_role' => 'warehouse']);
$this->post('/cotacoes/'.$quote->id.'/avaliar', quotationTaskEvaluation())->assertForbidden();
$this->actingAs($this->buyer);
DB::table('users')->where('id', $this->buyer->id)->update(['purchasing_role' => 'warehouse']);
$this->post('/requisicoes/'.$this->r->id.'/cotacoes', [...$this->quote, 'reference' => 'NOVA-TENTATIVA'])->assertForbidden();
expect(Quotation::query()->count())->toBe(1)->and(DB::table('proposal_evaluations')->count())->toBe(0);
});
test('quotation tasks exclude maintenance from preparation and evaluation', function (string $function) {
quotationTaskRegister($this);
$actor = $function === 'procurement' ? $this->buyer : $this->finance;
supplyGrant($actor, [$function]);
DB::table('organization_memberships')->where('user_id', $actor->id)->update(['maintenance' => true, 'expires_at' => now()->addHour()]);
$this->actingAs($actor)->get('/requisicoes/tarefas?module=purchases')->assertInertia(fn (Assert $page) => $page->where('tasks.total', 0));
$this->get('/requisicoes/'.$this->r->id.'/cotacoes')->assertInertia(fn (Assert $page) => $page->where('canCreate', false)->where('canEvaluate', false)->where('quotations.0.canEvaluate', false));
})->with(['procurement', 'finance']);
test('quotation tasks keep older market consultations visible through pagination and search', function () {
$this->r->forceFill(['created_at' => now()->subMonths(2)])->save();
for ($index = 1; $index < 30; $index++) {
$request = $this->r->replicate();
$request->number = 'REQ-NEW-'.$index;
$request->created_at = now()->subDay();
$request->save();
}
$this->get('/requisicoes/tarefas?module=purchases')->assertInertia(fn (Assert $page) => $page->where('tasks.total', 30)->has('tasks.data', 25)->where('tasks.data.0.reference', $this->r->number));
$this->get('/requisicoes/tarefas?module=purchases&page=2')->assertInertia(fn (Assert $page) => $page->where('tasks.total', 30)->has('tasks.data', 5));
$this->get('/requisicoes/tarefas?module=purchases&search=REQ-TEST')->assertInertia(fn (Assert $page) => $page->where('tasks.total', 1)->where('tasks.data.0.reference', $this->r->number));
});
test('quotation tasks and forms immediately respect a declared participation conflict', function () {
$quote = quotationTaskRegister($this);
$this->actingAs($this->finance)->get('/requisicoes/tarefas?module=purchases')->assertInertia(fn (Assert $page) => $page->where('tasks.total', 1));
$this->post('/conflitos', ['scope' => 'process_supplier', 'supplier_id' => $this->supplier->id, 'requisition_id' => $this->r->id,
'has_conflict' => true, 'known_since' => today()->toDateString(), 'reason' => 'Relação sintética que impede a participação.'])->assertSessionHasNoErrors();
$this->get('/requisicoes/tarefas?module=purchases')->assertInertia(fn (Assert $page) => $page->where('tasks.total', 0));
$this->get('/requisicoes/'.$this->r->id.'/cotacoes?quotation_id='.$quote->id)->assertInertia(fn (Assert $page) => $page->where('quotations.0.canEvaluate', false)->where('quotations.0.conflict_blocked', true));
$this->post('/cotacoes/'.$quote->id.'/avaliar', quotationTaskEvaluation())->assertSessionHasErrors('conflict');
expect(DB::table('proposal_evaluations')->count())->toBe(0);
});
test('quotation tasks recheck qualified evaluator authority without replacing the stored evaluation', function () {
$this->r->update(['comparison_policy' => [...ComparisonPolicy::DEFAULTS, 'revision' => 1, 'configured' => true, 'minimum_proposals' => 1, 'minimum_evaluators' => 1]]);
$quote = quotationTaskRegister($this);
$this->actingAs($this->finance)->post('/cotacoes/'.$quote->id.'/avaliar', quotationTaskEvaluation())->assertSessionHasNoErrors();
$history = DB::table('proposal_evaluations')->get()->toArray();
DB::table('users')->where('id', $this->finance->id)->update(['purchasing_role' => 'warehouse']);
$this->actingAs($this->buyer)->get('/requisicoes/tarefas?module=purchases')->assertInertia(fn (Assert $page) => $page->where('tasks.total', 1)->where('tasks.data.0.task', 'quotation_evaluate'));
expect(DB::table('proposal_evaluations')->get()->toArray())->toEqual($history);
$this->post('/cotacoes/'.$quote->id.'/avaliar', quotationTaskEvaluation())->assertSessionHasNoErrors();
expect(DB::table('proposal_evaluations')->count())->toBe(2);
$this->get('/requisicoes/tarefas?module=purchases')->assertInertia(fn (Assert $page) => $page->where('tasks.total', 1)->where('tasks.data.0.task', 'comparison_review'));
});