Files
compras/tests/Feature/OrderDocumentNavigationTest.php
T
nala 448948a487
tests / ci (push) Waiting to run
commit compras1 2911
2026-11-29 05:28:30 +02:00

65 lines
4.0 KiB
PHP

<?php
use App\Enums\PurchasingRole;
use App\Models\User;
use App\Services\ReceiptWorkflow;
use Illuminate\Support\Facades\DB;
use Inertia\Testing\AssertableInertia as Assert;
require_once __DIR__.'/../Support/ReceiptFixture.php';
test('order and receipt consultations link the exact authorised financial context without business effects', function () {
$f = receiptFixture('1.000');
$finance = User::factory()->create(['tenant_id' => $f['tenant']->id, 'department_id' => $f['department']->id, 'purchasing_role' => PurchasingRole::Finance]);
$receipt = app(ReceiptWorkflow::class)->confirm($f['receiver'], $f['order'], receiptPayload($f));
$before = [];
foreach (['purchase_orders', 'purchase_order_items', 'invoices', 'external_payments', 'stock_movements', 'budget_entries', 'audit_logs'] as $table) {
$before[$table] = DB::table($table)->orderBy('id')->get()->toJson();
}
$href = '/facturas?purchase_order_id='.$f['order']->id.'#invoice-documents';
$this->actingAs($finance)->get('/ordens/'.$f['order']->id)->assertOk()
->assertViewHas('page', fn ($page) => $page['encryptHistory'] === true)->assertInertia(fn (Assert $p) => $p
->component('purchases/show')->where('financialDocumentsHref', $href)
->has('relatedProcesses', 1)->where('relatedProcesses.0.id', $f['r']->id)
->where('relatedProcesses.0.href', '/requisicoes/'.$f['r']->id.'/processo'));
$this->get('/ordens/'.$f['order']->id.'/recepcoes')->assertOk()->assertInertia(fn (Assert $p) => $p
->component('receipts/order')->where('financialDocumentsHref', $href));
$this->get('/recepcoes/'.$receipt->id)->assertOk()->assertInertia(fn (Assert $p) => $p
->component('receipts/show')->where('financialDocumentsHref', $href));
$this->get($href)->assertOk()->assertInertia(fn (Assert $p) => $p->where('focusOrderId', $f['order']->id));
foreach ($before as $table => $rows) {
expect(DB::table($table)->orderBy('id')->get()->toJson())->toBe($rows);
}
});
test('context links do not grant procurement warehouse or foreign identities access to financial documents', function () {
$f = receiptFixture();
$receipt = app(ReceiptWorkflow::class)->confirm($f['receiver'], $f['order'], receiptPayload($f));
$this->actingAs($f['issuer'])->get('/ordens/'.$f['order']->id)->assertOk()->assertInertia(fn (Assert $p) => $p
->where('financialDocumentsHref', null)->has('relatedProcesses', 1));
$this->get('/facturas?purchase_order_id='.$f['order']->id)->assertForbidden();
$this->actingAs($f['receiver'])->get('/ordens/'.$f['order']->id.'/recepcoes')->assertOk()->assertInertia(fn (Assert $p) => $p
->where('financialDocumentsHref', null));
$this->get('/recepcoes/'.$receipt->id)->assertOk()->assertInertia(fn (Assert $p) => $p
->where('financialDocumentsHref', null));
$this->get('/facturas?purchase_order_id='.$f['order']->id)->assertForbidden();
$foreign = receiptFixture();
$other = User::factory()->create(['tenant_id' => $foreign['tenant']->id, 'purchasing_role' => PurchasingRole::Finance]);
$this->actingAs($other)->get('/ordens/'.$f['order']->id)->assertNotFound();
$this->get('/recepcoes/'.$receipt->id)->assertNotFound();
$this->getJson('/facturas?purchase_order_id='.$f['order']->id)->assertUnprocessable();
});
test('print navigation leaves Inertia only after checking the current order access', function () {
$f = receiptFixture();
$page = $this->actingAs($f['issuer'])->get('/ordens/'.$f['order']->id)->assertOk()->viewData('page');
$headers = ['X-Inertia' => 'true'];
if ($page['version'] !== null) {
$headers['X-Inertia-Version'] = $page['version'];
}
$route = '/ordens/'.$f['order']->id.'/imprimir';
$this->get($route, $headers)->assertStatus(409)->assertHeader('X-Inertia-Location', $route);
$this->get($route)->assertOk()->assertSee($f['order']->number);
$this->actingAs($f['receiver'])->get($route, $headers)->assertForbidden()->assertHeaderMissing('X-Inertia-Location');
});