Files
compras/tests/Feature/ImportLegalReferenceDraftsTest.php
T
nala 060c119ba1
tests / ci (push) Waiting to run
commit compras2 2811
2026-11-28 15:45:52 +02:00

66 lines
3.5 KiB
PHP

<?php
use App\Models\SupplyRecord;
use App\Services\ImportLegalReferenceDrafts;
use App\Services\ImportLegalRules;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
require_once __DIR__.'/../Support/ReceiptFixture.php';
require_once __DIR__.'/../Support/SupplyFixture.php';
beforeEach(function () {
config(['import-tariffs.qa_enabled' => true, 'import-legal.qa_enabled' => true]);
Storage::fake('local');
$this->f = receiptFixture();
supplyGrant($this->f['issuer'], ['procurement', 'import_customs']);
$this->actingAs($this->f['issuer']);
});
test('a supplied official edition offers exact documentary drafts without publishing rules or changing financial facts', function () {
$path = 'C:/Mac/Home/Documents/TEMPO/Portal Tempo/Legislacao/novas/BR_248_I_SERIE_SUPLEMENTO_2025.pdf';
if (! is_file($path)) {
test()->markTestSkipped('The owner supplied original is required for this real-source integration.');
}
expect(hash_file('sha256', $path))->toBe(ImportLegalReferenceDrafts::EDITION_SHA256);
$before = [];
foreach (['budget_entries', 'stock_movements', 'purchase_orders', 'invoices'] as $table) {
$before[$table] = DB::table($table)->orderBy('id')->get()->toJson();
}
$service = app(ImportLegalRules::class);
$source = $service->save($this->f['issuer'], ['operation_key' => (string) Str::uuid(), 'action' => 'source', 'version' => 0,
'official_reference' => 'BR I Série 248, suplemento, 29/12/2025; Lei 8/2025',
'reason' => 'Actual supplied official original copied only to synthetic isolated QA; no rule publication.'],
new UploadedFile($path, basename($path), 'application/pdf', UPLOAD_ERR_OK, true));
$workspace = $service->workspace($this->f['issuer'], $this->f['r']);
$drafts = $workspace['referenceDrafts'];
expect($drafts)->toHaveCount(11)
->and(array_column($drafts, 'code'))->toContain('3506.91.20', '7604.10.10')
->and($drafts[0]['rate_percent'])->toBe('20')
->and($drafts[2]['rate_percent'])->toBe('7.5')
->and($drafts[0]['valid_from'])->toBe('2026-01-01')
->and($drafts[0]['citation'])->toContain('2180-(4)')
->and($drafts[0]['source_id'])->toBe($source->id)
->and($drafts[0]['charges'][0]['kind'])->toBe('duty')
->and($workspace['referenceDraftWarnings'])->toBe([])
->and(SupplyRecord::query()->where('kind', 'import_legal_rule')->count())->toBe(0)
->and(json_encode($workspace['sources'], JSON_THROW_ON_ERROR))->not->toContain('import-legal/');
foreach ($before as $table => $rows) {
expect(DB::table($table)->orderBy('id')->get()->toJson())->toBe($rows);
}
Storage::disk('local')->put($source->snapshot['evidence']['path'], 'ALTERED-SYNTHETIC-COPY');
$workspace = $service->workspace($this->f['issuer'], $this->f['r']);
expect($workspace['referenceDrafts'])->toBe([])
->and($workspace['referenceDraftWarnings'])->toHaveCount(1)
->and($workspace['referenceDraftWarnings'][0]['source_id'])->toBe($source->id)
->and(hash_file('sha256', $path))->toBe(ImportLegalReferenceDrafts::EDITION_SHA256);
});
test('an unknown edition never inherits parameters from its title or citation', function () {
$source = new SupplyRecord(['kind' => 'import_legal_source', 'data' => ['official_reference' => 'Lei 8/2025'],
'snapshot' => ['evidence' => ['sha256' => hash('sha256', 'different edition')]]]);
expect(app(ImportLegalReferenceDrafts::class)->forSource($source))->toBe([]);
});