@@ -0,0 +1,183 @@
|
||||
<?php
|
||||
|
||||
use App\Enums\PurchasingRole;
|
||||
use App\Models\SupplyRecord;
|
||||
use App\Models\User;
|
||||
use App\Services\ImportEvidence;
|
||||
use App\Services\ImportLegalRules;
|
||||
use App\Services\ImportTariffCodes;
|
||||
use Illuminate\Database\Eloquent\ModelNotFoundException;
|
||||
use Illuminate\Http\UploadedFile;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Illuminate\Support\Str;
|
||||
use Symfony\Component\HttpKernel\Exception\HttpException;
|
||||
|
||||
require_once __DIR__.'/../Support/ReceiptFixture.php';
|
||||
require_once __DIR__.'/../Support/SupplyFixture.php';
|
||||
|
||||
function legalRuleInput(array $overrides = []): array
|
||||
{
|
||||
return [...['operation_key' => (string) Str::uuid(), 'record_key' => null, 'version' => 0, 'action' => 'save',
|
||||
'title' => 'Synthetic tariff rule', 'official_reference' => 'Synthetic official edition QA only',
|
||||
'official_url' => 'https://example.test/official-qa.pdf', 'citation' => 'Synthetic article 1, page 2',
|
||||
'valid_from' => '2026-01-01', 'valid_until' => '2026-12-31', 'match_code' => '1234', 'suggested_code' => '1234.56',
|
||||
'origin_country' => 'Synthetic A', 'sector' => 'Synthetic sector', 'obligations' => 'Synthetic duty conditions; no real tax activated',
|
||||
'document_types' => ['licence'], 'reason' => 'Synthetic legal configuration; not an approved real legal rule'], ...$overrides];
|
||||
}
|
||||
|
||||
function legalRuleFile(): UploadedFile
|
||||
{
|
||||
return UploadedFile::fake()->createWithContent('source.pdf', "%PDF-1.4\nSynthetic official source for QA only\n%%EOF");
|
||||
}
|
||||
|
||||
function publishLegalRule(User $actor, SupplyRecord $draft): SupplyRecord
|
||||
{
|
||||
return app(ImportLegalRules::class)->save($actor, ['operation_key' => (string) Str::uuid(), 'record_key' => $draft->record_key,
|
||||
'version' => $draft->version, 'action' => 'publish', 'reason' => 'Synthetic customs review and publication of the prepared rule']);
|
||||
}
|
||||
|
||||
beforeEach(function () {
|
||||
config(['import-tariffs.qa_enabled' => true, 'import-legal.qa_enabled' => true]);
|
||||
Storage::fake('local');
|
||||
$this->withoutVite();
|
||||
$this->f = receiptFixture();
|
||||
supplyGrant($this->f['issuer'], ['procurement', 'import_customs']);
|
||||
$this->actingAs($this->f['issuer']);
|
||||
});
|
||||
|
||||
test('legal publication preserves sources versions periods scope and previous publication during a draft revision', function () {
|
||||
$service = app(ImportLegalRules::class);
|
||||
$before = [DB::table('budget_entries')->get()->toJson(), DB::table('invoices')->get()->toJson(), DB::table('stock_movements')->get()->toJson()];
|
||||
$input = legalRuleInput();
|
||||
$file = legalRuleFile();
|
||||
$draft = $service->save($this->f['issuer'], $input, $file);
|
||||
expect($service->save($this->f['issuer'], $input, $file)->id)->toBe($draft->id);
|
||||
$find = fn ($origin = 'Synthetic A', $sector = 'Synthetic sector', $date = '2026-11-27') => $service->suggestions($this->f['issuer'], $this->f['r'], '1234', $origin, $sector, $date);
|
||||
expect($find())->toBe([]);
|
||||
$published = publishLegalRule($this->f['issuer'], $draft);
|
||||
expect($find()[0]['id'])->toBe($published->id)->and($find(''))->toBe([])->and($find('Synthetic A', ''))->toBe([])
|
||||
->and($find('Synthetic A', 'Synthetic sector', '2027-01-01'))->toBe([])
|
||||
->and($find('Synthetic A', 'Synthetic sector', '2026-01-01'))->toHaveCount(1)
|
||||
->and($find('Synthetic A', 'Synthetic sector', '2026-12-31'))->toHaveCount(1);
|
||||
$frozen = $service->freeze($published->id, '1234', 'Synthetic A', 'Synthetic sector', '2026-11-27');
|
||||
$next = $service->save($this->f['issuer'], legalRuleInput(['record_key' => $published->record_key, 'version' => 2, 'suggested_code' => '1234.57']));
|
||||
expect($find()[0]['id'])->toBe($published->id)->and($service->changed($frozen))->toBeFalse();
|
||||
$new = publishLegalRule($this->f['issuer'], $next);
|
||||
expect($service->changed($frozen))->toBeTrue()->and($frozen['data']['suggested_code'])->toBe('1234.56')
|
||||
->and($published->fresh()->data['suggested_code'])->toBe('1234.56');
|
||||
$service->save($this->f['issuer'], ['operation_key' => (string) Str::uuid(), 'record_key' => $new->record_key, 'version' => 4,
|
||||
'action' => 'withdraw', 'reason' => 'Synthetic withdrawal without deleting original sources or applied decisions']);
|
||||
expect($find())->toBe([])->and(Storage::disk('local')->allFiles())->toHaveCount(1)
|
||||
->and([DB::table('budget_entries')->get()->toJson(), DB::table('invoices')->get()->toJson(), DB::table('stock_movements')->get()->toJson()])->toBe($before);
|
||||
});
|
||||
|
||||
test('buyer proposal freezes legal context and confirmation permits a reasoned deviation while changes require a current source', function () {
|
||||
$service = app(ImportLegalRules::class);
|
||||
$published = publishLegalRule($this->f['issuer'], $service->save($this->f['issuer'], legalRuleInput(), legalRuleFile()));
|
||||
$tariffs = app(ImportTariffCodes::class);
|
||||
$input = ['operation_key' => (string) Str::uuid(), 'action' => 'propose', 'version' => 0, 'requisition_item_id' => $this->f['r']->items()->sole()->id,
|
||||
'code' => '1234.56', 'source_reference' => $published->data['official_reference'].' — '.$published->data['citation'], 'legal_rule_id' => $published->id,
|
||||
'legal_match_code' => '1234', 'legal_origin' => 'Synthetic A', 'legal_sector' => 'Synthetic sector', 'legal_date' => '2026-11-27',
|
||||
'reason' => 'Synthetic buyer selection of a documented suggestion'];
|
||||
$proposal = $tariffs->save($this->f['issuer'], $this->f['r'], $input);
|
||||
$confirmed = $tariffs->save($this->f['issuer'], $this->f['r'], ['operation_key' => (string) Str::uuid(), 'action' => 'confirm', 'version' => 1,
|
||||
'requisition_item_id' => $input['requisition_item_id'], 'code' => '1234.59', 'source_reference' => 'Synthetic technical classification reference',
|
||||
'reason' => 'Synthetic buyer explains the documented deviation from the suggested code']);
|
||||
expect($confirmed->data['deviation'])->toBeTrue()->and($confirmed->snapshot['legal_rule']['id'])->toBe($published->id)
|
||||
->and($confirmed->snapshot['calculations_activated'])->toBeFalse()->and($proposal->fresh()->state)->toBe('proposed');
|
||||
expect($tariffs->freezeForItems($this->f['issuer'], [$input['requisition_item_id']])[$input['requisition_item_id']]['legal_rule'])->toBe($confirmed->snapshot['legal_rule']);
|
||||
$tariffs->save($this->f['issuer'], $this->f['r'], [...$input, 'operation_key' => (string) Str::uuid(), 'version' => 2]);
|
||||
$draft = $service->save($this->f['issuer'], legalRuleInput(['record_key' => $published->record_key, 'version' => 2]));
|
||||
publishLegalRule($this->f['issuer'], $draft);
|
||||
expect(fn () => $tariffs->save($this->f['issuer'], $this->f['r'], ['operation_key' => (string) Str::uuid(), 'action' => 'confirm', 'version' => 3,
|
||||
'requisition_item_id' => $input['requisition_item_id'], 'code' => '1234.56', 'source_reference' => 'Synthetic source', 'reason' => 'Synthetic obsolete-source confirmation must fail']))->toThrow(HttpException::class);
|
||||
expect($tariffs->workspace($this->f['issuer'], $this->f['r'])['latest']->first()->legal_changed)->toBeTrue();
|
||||
});
|
||||
|
||||
test('legal management rejects stale operations foreign access revoked permissions and missing or altered original bytes', function () {
|
||||
$service = app(ImportLegalRules::class);
|
||||
$input = legalRuleInput();
|
||||
$draft = $service->save($this->f['issuer'], $input, legalRuleFile());
|
||||
expect(fn () => $service->save($this->f['issuer'], [...$input, 'title' => 'Changed title'], legalRuleFile()))->toThrow(HttpException::class);
|
||||
expect(fn () => $service->save($this->f['issuer'], legalRuleInput(['record_key' => $draft->record_key])))->toThrow(HttpException::class);
|
||||
$viewer = User::factory()->create(['tenant_id' => $this->f['tenant']->id, 'purchasing_role' => PurchasingRole::Procurement]);
|
||||
supplyGrant($viewer, ['procurement']);
|
||||
$this->actingAs($viewer);
|
||||
expect(fn () => publishLegalRule($viewer, $draft))->toThrow(HttpException::class);
|
||||
$foreign = receiptFixture();
|
||||
supplyGrant($foreign['issuer'], ['procurement', 'import_customs']);
|
||||
$this->actingAs($foreign['issuer']);
|
||||
expect(fn () => $service->source($foreign['issuer'], $foreign['r'], $draft->snapshot['source_id']))->toThrow(ModelNotFoundException::class);
|
||||
$this->actingAs($this->f['issuer']);
|
||||
$source = $service->source($this->f['issuer'], $this->f['r'], $draft->snapshot['source_id']);
|
||||
Storage::disk('local')->put($source->snapshot['evidence']['path'], 'altered');
|
||||
expect(fn () => publishLegalRule($this->f['issuer'], $draft))->toThrow(HttpException::class);
|
||||
DB::table('organization_memberships')->where('user_id', $this->f['issuer']->id)->delete();
|
||||
expect(fn () => $service->workspace($this->f['issuer'], $this->f['r']))->toThrow(HttpException::class);
|
||||
});
|
||||
|
||||
test('native legal forms keep sources private and allow source download and explicit human publication only', function () {
|
||||
$url = '/requisicoes/'.$this->f['r']->id.'/base-legal';
|
||||
$this->post($url, [...legalRuleInput(), 'file' => legalRuleFile()])->assertRedirect();
|
||||
$draft = SupplyRecord::query()->where('kind', 'import_legal_rule')->sole();
|
||||
$this->get($url)->assertOk()->assertHeader('Cache-Control', 'max-age=0, no-store, private');
|
||||
$source = app(ImportLegalRules::class)->source($this->f['issuer'], $this->f['r'], $draft->snapshot['source_id']);
|
||||
$download = $this->get($url.'/fontes/'.$source->id)->assertOk()->assertHeader('X-Content-Type-Options', 'nosniff');
|
||||
expect($download->getContent())->toBe(app(ImportEvidence::class)->bytes($source));
|
||||
$this->getJson($url.'?rules_page=0')->assertUnprocessable();
|
||||
config(['import-legal.qa_enabled' => false]);
|
||||
$this->get($url)->assertStatus(503);
|
||||
});
|
||||
|
||||
test('missing source alerts an already applied rule and changed source metadata requires a new original', function () {
|
||||
$service = app(ImportLegalRules::class);
|
||||
$draft = $service->save($this->f['issuer'], legalRuleInput(), legalRuleFile());
|
||||
$published = publishLegalRule($this->f['issuer'], $draft);
|
||||
$frozen = $service->freeze($published->id, '1234', 'Synthetic A', 'Synthetic sector', '2026-11-27');
|
||||
expect(fn () => $service->save($this->f['issuer'], legalRuleInput(['record_key' => $published->record_key, 'version' => 2,
|
||||
'official_reference' => 'Different synthetic source without its original'])))->toThrow(HttpException::class);
|
||||
$source = $service->source($this->f['issuer'], $this->f['r'], $frozen['source_id']);
|
||||
Storage::disk('local')->delete($source->snapshot['evidence']['path']);
|
||||
expect($service->changed($frozen))->toBeTrue();
|
||||
expect(fn () => $service->freeze($published->id, '1234', 'Synthetic A', 'Synthetic sector', '2026-11-27'))->toThrow(HttpException::class);
|
||||
});
|
||||
|
||||
test('an official source larger than a transactional attachment remains consultable without truncation', function () {
|
||||
$bytes = "%PDF-1.4\n".str_repeat('Synthetic legal publication page bytes ', 70000)."\n%%EOF";
|
||||
$service = app(ImportLegalRules::class);
|
||||
$draft = $service->save($this->f['issuer'], legalRuleInput(), UploadedFile::fake()->createWithContent('large-source.pdf', $bytes));
|
||||
$source = $service->source($this->f['issuer'], $this->f['r'], $draft->snapshot['source_id']);
|
||||
expect(strlen($bytes))->toBeGreaterThan(2097152)->and(app(ImportEvidence::class)->bytes($source))->toBe($bytes);
|
||||
expect(publishLegalRule($this->f['issuer'], $draft)->snapshot['source_sha256'])->toBe(hash('sha256', $bytes));
|
||||
});
|
||||
|
||||
test('a shared official source is independently consultable and reused without inventing a code or publishing a rule', function () {
|
||||
$service = app(ImportLegalRules::class);
|
||||
$input = ['operation_key' => (string) Str::uuid(), 'version' => 0, 'action' => 'source',
|
||||
'official_reference' => 'Synthetic complete official journal; no rule or inferred rate',
|
||||
'reason' => 'Retain the synthetic official publication independently of its later use'];
|
||||
$source = $service->save($this->f['issuer'], $input, legalRuleFile());
|
||||
expect($service->save($this->f['issuer'], $input, legalRuleFile())->id)->toBe($source->id)
|
||||
->and($source->kind)->toBe('import_legal_source')->and($source->data['official_url'])->toBeNull()
|
||||
->and(SupplyRecord::query()->where('kind', 'import_legal_rule')->count())->toBe(0);
|
||||
$rule = $service->save($this->f['issuer'], legalRuleInput(['source_id' => $source->id,
|
||||
'official_reference' => $source->data['official_reference'], 'official_url' => null]));
|
||||
expect($rule->snapshot['source_id'])->toBe($source->id)->and(Storage::disk('local')->allFiles())->toHaveCount(1)
|
||||
->and($service->workspace($this->f['issuer'], $this->f['r'])['sources']->total())->toBe(1);
|
||||
$other = $service->save($this->f['issuer'], [...$input, 'operation_key' => (string) Str::uuid(), 'official_reference' => 'Another synthetic official source'], legalRuleFile());
|
||||
expect(fn () => $service->save($this->f['issuer'], ['operation_key' => (string) Str::uuid(), 'record_key' => $rule->record_key, 'version' => 1,
|
||||
'action' => 'publish', 'source_id' => $other->id, 'reason' => 'A publication decision cannot silently replace the prepared original']))->toThrow(HttpException::class);
|
||||
expect(publishLegalRule($this->f['issuer'], $rule)->snapshot['source_id'])->toBe($source->id);
|
||||
});
|
||||
|
||||
test('publishing a future period preserves the earlier applicable period without resurrecting an expired replacement', function () {
|
||||
$service = app(ImportLegalRules::class);
|
||||
$first = publishLegalRule($this->f['issuer'], $service->save($this->f['issuer'], legalRuleInput(), legalRuleFile()));
|
||||
$future = publishLegalRule($this->f['issuer'], $service->save($this->f['issuer'], legalRuleInput(['record_key' => $first->record_key, 'version' => 2,
|
||||
'valid_from' => '2027-01-01', 'valid_until' => '2027-06-30', 'suggested_code' => '1234.57'])));
|
||||
$find = fn ($date) => $service->suggestions($this->f['issuer'], $this->f['r'], '1234', 'Synthetic A', 'Synthetic sector', $date);
|
||||
expect($find('2026-11-27')[0]['id'])->toBe($first->id)->and($find('2027-01-01')[0]['id'])->toBe($future->id)
|
||||
->and($find('2027-07-01'))->toBe([])
|
||||
->and($service->freeze($first->id, '1234', 'Synthetic A', 'Synthetic sector', '2026-11-27')['id'])->toBe($first->id);
|
||||
});
|
||||
Reference in New Issue
Block a user