commit melhorias compras6
tests / ci (push) Waiting to run

This commit is contained in:
2026-11-23 06:37:16 +02:00
parent db1615fac1
commit dc73540558
73 changed files with 7539 additions and 175 deletions
+101
View File
@@ -0,0 +1,101 @@
<?php
use App\Models\SupplyRecord;
use App\Services\ImportTariffCodes;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;
use Inertia\Testing\AssertableInertia as Assert;
use Symfony\Component\HttpKernel\Exception\HttpException;
require_once __DIR__.'/../Support/ReceiptFixture.php';
require_once __DIR__.'/../Support/SupplyFixture.php';
beforeEach(function () {
config(['import-tariffs.qa_enabled' => true]);
$this->withoutVite();
$this->f = receiptFixture();
$this->actingAs($this->f['issuer']);
$this->lineId = $this->f['r']->items()->sole()->id;
});
function tariffInput(string $action, array $data = []): array
{
return [...['operation_key' => (string) Str::uuid(), 'action' => $action, 'version' => 0,
'reason' => 'Synthetic classification; no legal rate or customs declaration.'], ...$data];
}
test('native tariff master reuse and buyer deviation preserve proposals versions approvals and financial facts', function () {
supplyGrant($this->f['issuer'], ['procurement', 'requester']);
$service = app(ImportTariffCodes::class);
$order = $this->f['order']->fresh()->getAttributes();
$stock = $this->f['stock']->fresh()->getAttributes();
$master = $service->save($this->f['issuer'], $this->f['r'], tariffInput('master', ['stock_item_id' => $this->f['stock']->id,
'code' => '1234.56', 'source_reference' => 'Synthetic source v1; not a legal classification']));
$proposalInput = tariffInput('propose', ['requisition_item_id' => $this->lineId, 'master_id' => $master->id,
'code' => $master->data['code'], 'source_reference' => $master->data['source_reference']]);
$proposal = $service->save($this->f['issuer'], $this->f['r'], $proposalInput);
expect($service->save($this->f['issuer'], $this->f['r'], $proposalInput)->id)->toBe($proposal->id);
$confirmation = $service->save($this->f['issuer'], $this->f['r'], tariffInput('confirm', ['version' => 1,
'requisition_item_id' => $this->lineId, 'code' => '1234.57', 'source_reference' => 'Synthetic buyer source v2',
'reason' => 'Synthetic buyer explains why the proposed code is changed.']));
expect($confirmation->state)->toBe('confirmed')->and($confirmation->data['deviation'])->toBeTrue()
->and($confirmation->data['proposed_code'])->toBe('1234.56')->and($confirmation->data['confirmed_code'])->toBe('1234.57')
->and($confirmation->snapshot['master']['version'])->toBe(1)->and($confirmation->snapshot['calculations_activated'])->toBeFalse()
->and($proposal->fresh()->state)->toBe('proposed')->and($this->f['order']->fresh()->getAttributes())->toBe($order)
->and($this->f['stock']->fresh()->getAttributes())->toBe($stock)->and(DB::table('budget_consumptions')->count())->toBe(0);
$service->save($this->f['issuer'], $this->f['r'], tariffInput('master', ['version' => 1, 'stock_item_id' => $this->f['stock']->id,
'code' => '1234.58', 'source_reference' => 'Synthetic later material source']));
expect($confirmation->fresh()->data['confirmed_code'])->toBe('1234.57');
expect(fn () => $service->save($this->f['issuer'], $this->f['r'], [...$proposalInput, 'operation_key' => (string) Str::uuid(), 'version' => 2]))->toThrow(HttpException::class);
expect(SupplyRecord::query()->where('kind', 'import_tariff')->count())->toBe(2);
});
test('unknown proposed code remains unknown and a requester cannot confirm or edit the material master', function () {
supplyGrant($this->f['issuer'], ['requester']);
$service = app(ImportTariffCodes::class);
$proposal = $service->save($this->f['issuer'], $this->f['r'], tariffInput('propose', ['requisition_item_id' => $this->lineId]));
expect($proposal->data['proposed_code'])->toBeNull()->and($proposal->state)->toBe('proposed');
expect(fn () => $service->save($this->f['issuer'], $this->f['r'], tariffInput('confirm', ['version' => 1,
'requisition_item_id' => $this->lineId, 'code' => '1234', 'source_reference' => 'Synthetic source'])))->toThrow(HttpException::class);
expect(fn () => $service->save($this->f['issuer'], $this->f['r'], tariffInput('master', ['stock_item_id' => $this->f['stock']->id,
'code' => '1234', 'source_reference' => 'Synthetic source'])))->toThrow(HttpException::class);
$workspace = $service->workspace($this->f['issuer'], $this->f['r']);
expect($workspace['canConfirm'])->toBeFalse()->and($workspace['masters']->total())->toBe(0)->and($workspace['stockItems']->total())->toBe(0);
});
test('tariff classification rejects stale versions reused operations foreign items and a disabled capability', function () {
supplyGrant($this->f['issuer'], ['procurement']);
$service = app(ImportTariffCodes::class);
$input = tariffInput('propose', ['requisition_item_id' => $this->lineId, 'code' => '1234', 'source_reference' => 'Synthetic source']);
$service->save($this->f['issuer'], $this->f['r'], $input);
expect(fn () => $service->save($this->f['issuer'], $this->f['r'], [...$input, 'code' => '5678']))->toThrow(HttpException::class);
expect(fn () => $service->save($this->f['issuer'], $this->f['r'], [...$input, 'operation_key' => (string) Str::uuid()]))->toThrow(HttpException::class);
$foreign = receiptFixture();
$foreignItemId = $foreign['r']->items()->sole()->id;
$this->actingAs($this->f['issuer']);
expect(fn () => $service->workspace($this->f['issuer'], $foreign['r']))->toThrow(ModelNotFoundException::class);
expect(fn () => $service->save($this->f['issuer'], $this->f['r'], tariffInput('propose', ['requisition_item_id' => $foreignItemId])))->toThrow(ModelNotFoundException::class);
config(['import-tariffs.qa_enabled' => false]);
expect(fn () => $service->workspace($this->f['issuer'], $this->f['r']))->toThrow(HttpException::class);
expect(SupplyRecord::query()->where('kind', 'import_tariff')->count())->toBe(1);
});
test('authenticated classification forms use private encrypted history and paginated native sources', function () {
supplyGrant($this->f['issuer'], ['procurement']);
$url = '/requisicoes/'.$this->f['r']->id.'/classificacao-pautal';
$this->post($url, tariffInput('master', ['stock_item_id' => $this->f['stock']->id, 'code' => '1234.56', 'source_reference' => 'Synthetic source']))->assertRedirect();
$this->get($url.'?search=1234')->assertOk()->assertHeader('Cache-Control', 'max-age=0, no-store, private')
->assertInertia(fn (Assert $page) => $page->component('supply/import-tariffs')->where('canConfirm', true)
->where('masters.total', 1)->where('masters.data.0.data.code', '1234.56')->where('history.total', 0));
$this->getJson($url.'?masters_page=0')->assertUnprocessable();
$this->postJson($url, tariffInput('master', ['stock_item_id' => $this->f['stock']->id]))->assertUnprocessable()->assertJsonValidationErrors(['code', 'source_reference']);
});
test('classification revalidates current company permissions rather than trusting the initial buyer identity', function () {
supplyGrant($this->f['issuer'], ['procurement']);
DB::table('organization_memberships')->where('user_id', $this->f['issuer']->id)->delete();
expect(fn () => app(ImportTariffCodes::class)->save($this->f['issuer'], $this->f['r'], tariffInput('master', [
'stock_item_id' => $this->f['stock']->id, 'code' => '1234', 'source_reference' => 'Synthetic source'])))->toThrow(HttpException::class);
expect(SupplyRecord::query()->whereIn('kind', ['stock_tariff', 'import_tariff'])->count())->toBe(0);
});