f = qualificationFixture(); $this->url = '/requisicoes/'.$this->f['r']->id.'/relatorio.csv'; $this->actingAs($this->f['issuer']); }); function reviewCsvRows(string $content): array { $file = fopen('php://temp', 'w+'); fwrite($file, substr($content, 3)); rewind($file); $rows = []; while (($row = fgetcsv($file, null, ';', '"', '')) !== false) { $rows[] = $row; } fclose($file); return $rows; } test('review csv exports each frozen per order agreement once and distinguishes undefined caps from zero', function () { $report = app(ProcurementReviewReport::class)->build($this->f['issuer'], $this->f['r']->id); $binding = ['agreement_id' => 7, 'record_key' => 'synthetic', 'version' => 2, 'reference' => '=AGREEMENT-QA', 'basis' => 'per_order', 'currency' => 'MZN', 'unit' => 'un', 'includes_tax' => true, 'max_order_value_minor' => null, 'max_order_quantity_milli' => 0, 'checked_order_value_minor' => 12345, 'checked_order_quantity_milli' => '1000']; $report['agreement_limits'] = [$binding, $binding]; $rows = reviewCsvRows(app(ProcurementReviewCsv::class)->render($report)); expect(array_values(array_filter($rows, fn ($row) => $row === ['Acordo por ordem', '7', 'Referência', "'=AGREEMENT-QA"])))->toHaveCount(1); expect($rows)->toContain(['Acordo por ordem', '7', 'Limite por ordem (centavos)', 'Não definido']) ->toContain(['Acordo por ordem', '7', 'Limite por ordem (milésimos)', '0']) ->toContain(['Acordo por ordem', '7', 'Total conferido nesta ordem (centavos)', '12345']); $report['agreement_limits'] = 'invalid'; expect(fn () => app(ProcurementReviewCsv::class)->render($report))->toThrow(UnexpectedValueException::class); }); test('complete review csv keeps missing scores distinct from zero and makes no business writes', function () { $before = AuditLog::query()->count(); $response = $this->get($this->url)->assertOk() ->assertHeader('Content-Type', 'text/csv; charset=UTF-8') ->assertHeader('Cache-Control', 'max-age=0, no-store, private') ->assertHeader('X-Content-Type-Options', 'nosniff'); $rows = reviewCsvRows($response->getContent()); expect($rows)->toContain(['Secção', 'Registo', 'Campo', 'Valor']) ->toContain(['Proposta actual', (string) $this->f['quote']->id, 'Pontuação (centésimos de 10)', 'Não disponível']) ->toContain(['Comparação actual', '', 'Comparação completa', 'Não']) ->toContain(['Ordem', '', 'Registo disponível', 'Não']) ->toContain(['Execução financeira', '', 'Consulta autorizada', 'Não']) ->and(AuditLog::query()->count())->toBe($before); }); test('complete review csv includes all proposals beyond the page boundary and protects spreadsheet cells', function () { $quote = $this->f['quote']; $this->f['r']->update(['justification' => " \t=HYPERLINK(\"https://example.invalid\")\nlinha;seguinte"]); for ($i = 0; $i < 21; $i++) { $supplier = $quote->supplier->replicate(); $supplier->nuit = (string) (200000000 + $i); $supplier->save(); $copy = $quote->replicate(); $copy->supplier_id = $supplier->id; $copy->reference = 'PROPOSTA-ADICIONAL-'.$i; $copy->save(); } $response = $this->get($this->url)->assertOk(); $rows = collect(reviewCsvRows($response->getContent())); expect($rows->filter(fn ($row) => $row[0] === 'Proposta actual' && $row[2] === 'Referência'))->toHaveCount(22) ->and($rows->first(fn ($row) => $row[0] === 'Pedido' && $row[2] === 'Fundamentação')[3])->toBe("' \t=HYPERLINK(\"https://example.invalid\")\nlinha;seguinte"); }); test('complete review csv separates preserved documentary evidence from current proposals', function () { $workflow = app(DocumentaryQualificationWorkflow::class); $set = $workflow->define($this->f['issuer'], $this->f['r'], $this->f['definition']); $this->actingAs($this->f['analyst']); $workflow->assess($this->f['analyst'], $set, $this->f['assessment']); $this->f['quote']->update(['reference' => 'REFERENCIA-NOVA']); $rows = reviewCsvRows($this->get($this->url)->assertOk()->getContent()); expect($rows)->toContain(['Proposta actual', (string) $this->f['quote']->id, 'Referência', 'REFERENCIA-NOVA']) ->toContain(['Proposta na qualificação', (string) $this->f['quote']->id, 'Referência preservada', 'PROP-TEST']) ->toContain(['Qualificação documental', (string) $set->id, 'Base corresponde às propostas actuais', 'Não']); expect(collect($rows)->contains(fn ($row) => $row[0] === 'Resposta documental' && $row[2] === 'Resultado' && $row[3] === 'pending'))->toBeTrue(); }); test('complete review csv reapplies actor company department and guest access', function () { $this->actingAs($this->f['receiver'])->get($this->url)->assertForbidden(); $other = Tenant::query()->create(['name' => 'Outra empresa fictícia']); $this->actingAs(User::factory()->create(['tenant_id' => $other->id, 'purchasing_role' => PurchasingRole::Admin]))->get($this->url)->assertNotFound(); $department = Department::query()->create(['tenant_id' => $this->f['tenant']->id, 'name' => 'Outro']); $head = User::factory()->create(['tenant_id' => $this->f['tenant']->id, 'department_id' => $department->id, 'purchasing_role' => PurchasingRole::DepartmentHead]); $this->actingAs($head)->get($this->url)->assertNotFound(); $this->actingAs($this->f['analyst']); DB::table('users')->where('id', $this->f['analyst']->id)->update(['purchasing_role' => 'requester']); $this->get($this->url)->assertForbidden(); auth()->logout(); $this->get($this->url)->assertRedirect('/login'); }); test('complete review csv excludes financial contents when permission is absent and preserves exact authorised amounts', function () { $report = app(ProcurementReviewReport::class)->build($this->f['issuer'], $this->f['r']->id); // Synthetic projection tests the export boundary independently of the database builder. $report['fulfilment']['invoices'] = [['id' => 99, 'number' => 'FACTURA-RESERVADA', 'status' => 'registered', 'issued_on' => '2026-11-21', 'total_minor' => '99999999999999', 'currency' => 'MZN']]; $report['fulfilment']['payments'] = [['id' => 100, 'invoice_id' => 99, 'status' => 'confirmed', 'paid_on' => '2026-11-21', 'amount_minor' => '0', 'currency' => 'MZN']]; $csv = app(ProcurementReviewCsv::class); expect($csv->render($report))->not->toContain('FACTURA-RESERVADA')->not->toContain('99999999999999'); $report['fulfilment']['finance_visible'] = true; $rows = reviewCsvRows($csv->render($report)); expect($rows)->toContain(['Factura', '99', 'Total (centavos)', '99999999999999']) ->toContain(['Pagamento externo', '100', 'Montante (centavos)', '0']); });