commit de armazens
tests / ci (push) Canceled after 0s

This commit is contained in:
2026-11-04 09:40:26 +02:00
parent e753f7a0a4
commit 63d20b10fc
245 changed files with 10847 additions and 230 deletions
+109 -16
View File
@@ -4,28 +4,14 @@ use App\Enums\PurchasingRole;
use App\Models\AuditLog;
use App\Models\Department;
use App\Models\Supplier;
use App\Models\SupplierEvaluation;
use App\Models\SupplierImprovement;
use App\Models\User;
use App\Services\AdministrativeGeography;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;
use Inertia\Testing\AssertableInertia as Assert;
require_once __DIR__.'/../Support/ReceiptFixture.php';
function reportEvaluation(array $fixture, ?int $department = null, int $revision = 1, ?int $previous = null): SupplierEvaluation
{
return SupplierEvaluation::query()->create(['tenant_id' => $fixture['tenant']->id, 'supplier_id' => $fixture['order']->supplier_id, 'department_id' => $department ?? $fixture['r']->department_id,
'created_by' => $fixture['issuer']->id, 'operation_key' => (string) Str::uuid(), 'creation_hash' => str_repeat('b', 64), 'status' => 'approved', 'contributors' => [$fixture['issuer']->id],
'revision' => $revision, 'previous_id' => $previous, 'supplier_snapshot' => ['id' => $fixture['order']->supplier_id, 'name' => 'Fornecedor preservado QA', 'nuit' => '123456789'],
'payload' => ['title' => 'Avaliação QA', 'order_ids' => [], 'criteria' => [], 'period_start' => null, 'period_end' => null]]);
}
function reportMeasure(SupplierEvaluation $evaluation, User $responsible, array $extra = []): SupplierImprovement
{
return SupplierImprovement::query()->create([...['tenant_id' => $evaluation->tenant_id, 'supplier_evaluation_id' => $evaluation->id, 'created_by' => $responsible->id, 'responsible_id' => $responsible->id,
'operation_key' => (string) Str::uuid(), 'creation_hash' => str_repeat('c', 64), 'title' => 'Medida QA', 'description' => 'Objectivo fictício para o relatório.', 'due_on' => '2026-10-29', 'contributors' => [$responsible->id]], ...$extra]);
}
require_once __DIR__.'/../Support/SupplierImprovementFixture.php';
beforeEach(function () {
$this->f = receiptFixture();
@@ -35,6 +21,113 @@ beforeEach(function () {
$this->travelTo(now()->setDate(2026, 10, 30)->startOfDay());
});
function supplierReportReference(bool $otherProvince = false): array
{
$record = collect(app(AdministrativeGeography::class)->catalogue()['records'])->first(fn ($record) => $record['selectable'] && $record['level'] === 4 && ($otherProvince ? $record['codes'][0] !== '01' : $record['codes'][0] === '01'));
return ['country' => 'MZ', 'catalogue' => config('geography.source_sha256'), 'record_id' => $record['id']];
}
test('supplier territory report shares directory classification and preserves approved evaluation snapshots', function () {
$this->withoutVite();
config(['geography.enabled' => true]);
$supplier = Supplier::query()->findOrFail($this->evaluation->supplier_id);
$supplier->update(['geography_reference' => supplierReportReference()]);
$measure = reportMeasure($this->evaluation, $this->f['issuer']);
$before = [];
foreach (['suppliers', 'supplier_evaluations', 'supplier_improvements', 'audit_logs'] as $table) {
$before[$table] = DB::table($table)->orderBy('id')->get()->toArray();
}
$this->get($this->base.'?location=2')->assertInertia(fn (Assert $page) => $page->where('records.total', 1)->where('records.data.0.id', $measure->id)->where('records.data.0.supplier', 'Fornecedor preservado QA')->where('counts.open', 1)->where('overdue', 1)->where('geographyFilter.path.0.name', 'Niassa'));
$this->get('/fornecedores?location=2')->assertInertia(fn (Assert $page) => $page->has('suppliers.data', 1)->where('suppliers.data.0.id', $supplier->id));
expect($this->get($this->base.'.csv?location=2')->assertOk()->streamedContent())->toContain('Niassa', 'localizacao_actual_da_ficha', 'Fornecedor preservado QA');
foreach ($before as $table => $rows) {
expect(DB::table($table)->orderBy('id')->get()->toArray())->toEqual($rows);
}
});
test('supplier territory report uses current registration without rewriting the evaluation when geography changes', function () {
$this->withoutVite();
config(['geography.enabled' => true]);
$supplier = Supplier::query()->findOrFail($this->evaluation->supplier_id);
$supplier->update(['geography_reference' => supplierReportReference()]);
reportMeasure($this->evaluation, $this->f['issuer']);
$original = $this->evaluation->fresh()->getAttributes();
$this->get($this->base.'?location=2')->assertInertia(fn (Assert $page) => $page->where('records.total', 1));
$supplier->update(['geography_reference' => supplierReportReference(true)]);
$this->get($this->base.'?location=2')->assertInertia(fn (Assert $page) => $page->where('records.total', 0));
$this->get($this->base)->assertInertia(fn (Assert $page) => $page->where('records.total', 1));
expect($this->evaluation->refresh()->getAttributes())->toBe($original);
});
test('supplier territory report keeps historical revisions suspended suppliers and complete CSV across pages', function () {
$this->withoutVite();
config(['geography.enabled' => true]);
Supplier::query()->findOrFail($this->evaluation->supplier_id)->update(['geography_reference' => supplierReportReference(), 'status' => 'suspended']);
$revision = reportEvaluation($this->f, null, 2, $this->evaluation->id);
foreach (range(1, 26) as $index) {
reportMeasure($index === 26 ? $revision : $this->evaluation, $this->f['issuer'], ['title' => 'Território '.$index, 'status' => $index === 1 ? 'closed' : 'open']);
}
$this->get($this->base.'?location=2')->assertInertia(fn (Assert $page) => $page->where('records.total', 26)->has('records.data', 25)->where('counts.open', 25)->where('counts.closed', 1)->where('overdue', 25));
$response = $this->get($this->base.'?location=2&page=2')->assertInertia(fn (Assert $page) => $page->has('records.data', 1)->where('records.data.0.revision', 2));
expect($response->inertiaProps('records.prev_page_url'))->toContain('location=2');
$csv = $this->get($this->base.'.csv?location=2&page=2')->assertOk()->streamedContent();
$rows = array_map(fn ($line) => str_getcsv($line, ';', '"', ''), explode("\n", trim($csv)));
expect($rows)->toHaveCount(29)->and($rows[2])->toHaveCount(17)->and($rows[3][4])->toBe('Território 1')->and($rows[28][4])->toBe('Território 26');
});
test('supplier territory report does not infer location from address or obsolete and foreign country references', function () {
$this->withoutVite();
config(['geography.enabled' => true]);
$supplier = Supplier::query()->findOrFail($this->evaluation->supplier_id);
$supplier->update(['address' => 'Niassa / CIDADE DE LICHINGA / Urbana nº 1']);
reportMeasure($this->evaluation, $this->f['issuer']);
foreach ([null, [...supplierReportReference(), 'catalogue' => str_repeat('0', 64)], [...supplierReportReference(), 'country' => 'ZA']] as $reference) {
$supplier->update(['geography_reference' => $reference]);
$this->get($this->base.'?location=2')->assertInertia(fn (Assert $page) => $page->where('records.total', 0));
$this->get('/fornecedores?location=2')->assertInertia(fn (Assert $page) => $page->has('suppliers.data', 0));
}
$this->get($this->base)->assertInertia(fn (Assert $page) => $page->where('records.total', 1));
});
test('supplier territory report combines native filters and retains department and tenant boundaries', function () {
$this->withoutVite();
config(['geography.enabled' => true]);
Supplier::query()->findOrFail($this->evaluation->supplier_id)->update(['geography_reference' => supplierReportReference()]);
reportMeasure($this->evaluation, $this->f['issuer'], ['title' => 'Visível territorial', 'status' => 'returned']);
$department = Department::query()->create(['tenant_id' => $this->f['tenant']->id, 'name' => 'Outra área']);
reportMeasure(reportEvaluation($this->f, $department->id), $this->f['issuer'], ['title' => 'OCULTA-AREA']);
$foreign = receiptFixture();
Supplier::query()->findOrFail($foreign['order']->supplier_id)->update(['geography_reference' => supplierReportReference()]);
reportMeasure(reportEvaluation($foreign), $foreign['issuer'], ['title' => 'OCULTA-EMPRESA']);
$head = User::factory()->create(['tenant_id' => $this->f['tenant']->id, 'department_id' => $this->f['department']->id, 'purchasing_role' => PurchasingRole::DepartmentHead]);
$this->actingAs($head);
$query = '?location=2&status=returned&supplier_id='.$this->evaluation->supplier_id.'&responsible_id='.$this->f['issuer']->id.'&search=territorial&overdue=1';
$this->get($this->base.$query)->assertInertia(fn (Assert $page) => $page->where('records.total', 1)->where('counts.returned', 1));
$csv = $this->get($this->base.'.csv'.$query)->assertOk()->streamedContent();
expect($csv)->toContain('Visível territorial')->not->toContain('OCULTA-AREA', 'OCULTA-EMPRESA');
});
test('supplier territory report validates territory before export and preserves legacy reports when disabled', function () {
$this->withoutVite();
config(['geography.enabled' => true]);
reportMeasure($this->evaluation, $this->f['issuer']);
$this->getJson($this->base.'?location=999999')->assertUnprocessable();
$this->getJson($this->base.'.csv?location=999999')->assertUnprocessable();
$this->getJson($this->base.'?location[]=2')->assertUnprocessable();
config(['geography.enabled' => false]);
$this->getJson($this->base.'?location=2')->assertServiceUnavailable();
$this->getJson($this->base.'.csv?location=2')->assertServiceUnavailable();
$this->get($this->base)->assertInertia(fn (Assert $page) => $page->where('geographyEnabled', false)->where('records.total', 1));
});
test('supplier territory report revalidates a revoked role before reading geographic results', function () {
config(['geography.enabled' => true]);
$this->f['issuer']->forceFill(['purchasing_role' => PurchasingRole::Requester])->save();
$this->getJson($this->base.'?location=2')->assertForbidden();
$this->getJson($this->base.'.csv?location=2')->assertForbidden();
});
test('report paginates all authorized revisions with counts over the entire filtered set', function () {
$next = reportEvaluation($this->f, null, 2, $this->evaluation->id);
foreach (range(1, 27) as $i) {