@@ -0,0 +1,114 @@
|
||||
<?php
|
||||
|
||||
use App\Enums\PurchasingRole;
|
||||
use App\Models\CriticalityLevel;
|
||||
use App\Models\Department;
|
||||
use App\Models\Equipment;
|
||||
use App\Models\GoodsReceipt;
|
||||
use App\Models\Invoice;
|
||||
use App\Models\Supplier;
|
||||
use App\Models\User;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Str;
|
||||
use Inertia\Testing\AssertableInertia as Assert;
|
||||
|
||||
require_once __DIR__.'/../Support/ReceiptFixture.php';
|
||||
|
||||
beforeEach(function () {
|
||||
// The new page is built privately; HTTP scope tests must not require publishing it to the pilot.
|
||||
$this->withoutVite();
|
||||
$this->f = receiptFixture();
|
||||
$this->actingAs($this->f['issuer']);
|
||||
});
|
||||
|
||||
test('process search combines native modules without exposing financial or contact data or writing records', function () {
|
||||
$this->f['r']->update(['number' => 'QA-NEED-001', 'justification' => 'Necessidade sintética de ensaio global.']);
|
||||
$supplier = Supplier::query()->findOrFail($this->f['order']->supplier_id);
|
||||
$supplier->update(['name' => 'QA Supplier', 'email' => 'private@example.invalid']);
|
||||
$before = [];
|
||||
foreach (['audit_logs', 'requisitions', 'purchase_orders', 'stock_movements'] as $table) {
|
||||
$before[$table] = DB::table($table)->count();
|
||||
}
|
||||
$response = $this->get('/compras/pesquisa?q=qa')->assertOk()->assertHeader('Cache-Control', 'max-age=0, no-store, private');
|
||||
$response->assertInertia(fn (Assert $page) => $page->component('search/procurement')->where('term', 'qa')->has('groups', 7)
|
||||
->where('groups.0.records.total', 1)->where('groups.0.records.data.0.title', 'QA-NEED-001')
|
||||
->where('groups.2.records.total', 1)->missing('groups.2.records.data.0.email')->missing('groups.2.records.data.0.nuit'));
|
||||
foreach ($before as $table => $count) {
|
||||
expect(DB::table($table)->count())->toBe($count);
|
||||
}
|
||||
$this->get('/compras/pesquisa?q=global')->assertInertia(fn (Assert $page) => $page->where('groups.0.records.total', 1));
|
||||
$this->get('/compras/pesquisa?q='.urlencode($this->f['order']->number))->assertInertia(fn (Assert $page) => $page->where('groups.1.records.total', 1));
|
||||
});
|
||||
|
||||
test('search scopes results to the current company department and native role', function () {
|
||||
$own = $this->f;
|
||||
$other = receiptFixture();
|
||||
$other['r']->update(['number' => 'SECRET-OTHER-COMPANY']);
|
||||
$this->actingAs($own['issuer']);
|
||||
$department = Department::query()->create(['tenant_id' => $own['tenant']->id, 'name' => 'Outro departamento sintético']);
|
||||
$copy = $own['r']->replicate();
|
||||
$copy->forceFill(['number' => 'SECRET-OTHER-DEPARTMENT', 'department_id' => $department->id])->save();
|
||||
$head = User::factory()->create(['tenant_id' => $own['tenant']->id, 'department_id' => $own['department']->id, 'purchasing_role' => PurchasingRole::DepartmentHead]);
|
||||
$this->actingAs($head)->get('/compras/pesquisa?q=SECRET')->assertInertia(fn (Assert $page) => $page->where('groups.0.records.total', 0));
|
||||
$requester = User::factory()->create(['tenant_id' => $own['tenant']->id, 'department_id' => $own['department']->id, 'purchasing_role' => PurchasingRole::Requester]);
|
||||
$this->actingAs($requester)->get('/compras/pesquisa?q=TEST')->assertInertia(fn (Assert $page) => $page->has('groups', 1)->where('groups.0.records.total', 0));
|
||||
});
|
||||
|
||||
test('search paginates each module and treats wildcards as text without listing the database on empty terms', function () {
|
||||
foreach (range(1, 17) as $index) {
|
||||
$copy = $this->f['r']->replicate();
|
||||
$copy->forceFill(['number' => 'PAGE-SEARCH-'.$index])->save();
|
||||
}
|
||||
$this->get('/compras/pesquisa?q=PAGE-SEARCH')->assertInertia(fn (Assert $page) => $page->where('groups.0.records.total', 17)->has('groups.0.records.data', 15));
|
||||
$this->get('/compras/pesquisa?q=PAGE-SEARCH&requests_page=2')->assertInertia(fn (Assert $page) => $page->where('groups.0.records.current_page', 2)->has('groups.0.records.data', 2));
|
||||
foreach (['', 'x', ' '] as $term) {
|
||||
$this->get('/compras/pesquisa?q='.urlencode($term))->assertInertia(fn (Assert $page) => $page->where('groups', []));
|
||||
}
|
||||
$this->get('/compras/pesquisa?q=%25_')->assertInertia(fn (Assert $page) => $page->where('groups.0.records.total', 0)->where('groups.2.records.total', 0));
|
||||
$this->getJson('/compras/pesquisa?q='.str_repeat('x', 101))->assertUnprocessable();
|
||||
$this->getJson('/compras/pesquisa?q=QA&requests_page=0')->assertUnprocessable();
|
||||
});
|
||||
|
||||
test('search covers stock warehouses equipment and receipt references with native permissions', function () {
|
||||
$f = $this->f;
|
||||
$level = CriticalityLevel::query()->create(['tenant_id' => $f['tenant']->id, 'created_by' => $f['issuer']->id, 'code' => 'SEARCH', 'name' => 'QA', 'criteria' => 'Synthetic test', 'is_critical' => false, 'created_at' => now()]);
|
||||
$asset = Equipment::query()->create(['tenant_id' => $f['tenant']->id, 'created_by' => $f['issuer']->id, 'department_id' => $f['department']->id, 'criticality_level_id' => $level->id, 'code' => 'FIND-EQ', 'description' => 'Synthetic search equipment', 'area' => 'QA', 'status' => 'active']);
|
||||
$receipt = GoodsReceipt::query()->create(['tenant_id' => $f['tenant']->id, 'purchase_order_id' => $f['order']->id, 'received_by' => $f['receiver']->id, 'warehouse_id' => $f['warehouse']->id,
|
||||
'idempotency_key' => (string) Str::uuid(), 'payload_hash' => str_repeat('a', 64), 'delivery_note' => 'FIND-GR', 'received_on' => today(), 'status' => 'confirmed', 'snapshot' => ['synthetic' => true], 'confirmed_at' => now()]);
|
||||
$f['stock']->update(['name' => 'FIND-STOCK']);
|
||||
$f['warehouse']->update(['name' => 'FIND-WAREHOUSE']);
|
||||
$this->get('/compras/pesquisa?q=FIND')->assertInertia(fn (Assert $page) => $page
|
||||
->where('groups.3.records.total', 1)->where('groups.3.records.data.0.href', '/equipamentos/'.$asset->id)
|
||||
->where('groups.4.records.total', 1)->where('groups.5.records.total', 1)
|
||||
->where('groups.6.records.total', 1)->where('groups.6.records.data.0.href', '/recepcoes/'.$receipt->id));
|
||||
$head = User::factory()->create(['tenant_id' => $f['tenant']->id, 'department_id' => $f['department']->id, 'purchasing_role' => PurchasingRole::DepartmentHead]);
|
||||
$otherDepartment = Department::query()->create(['tenant_id' => $f['tenant']->id, 'name' => 'Other QA department']);
|
||||
$asset->update(['department_id' => $otherDepartment->id]);
|
||||
$this->actingAs($head)->get('/compras/pesquisa?q=FIND')->assertInertia(fn (Assert $page) => $page->has('groups', 5)->where('groups.3.records.total', 0)->where('groups.4.key', 'receipts')->where('groups.4.records.total', 1));
|
||||
$this->actingAs($f['receiver'])->get('/compras/pesquisa?q=FIND')->assertInertia(fn (Assert $page) => $page->has('groups', 5)->where('groups.1.key', 'equipment')->where('groups.4.records.total', 1));
|
||||
});
|
||||
|
||||
test('search reloads revoked access instead of trusting the authenticated model and requires login', function () {
|
||||
DB::table('users')->where('id', $this->f['issuer']->id)->update(['purchasing_role' => PurchasingRole::Configured->value]);
|
||||
$this->get('/compras/pesquisa?q=TEST')->assertForbidden();
|
||||
auth()->logout();
|
||||
$this->get('/compras/pesquisa?q=TEST')->assertRedirect('/login');
|
||||
});
|
||||
|
||||
test('optional finance search uses native invoice scope and returns references without private snapshots or money', function () {
|
||||
config(['procurement_search.finance_metadata_enabled' => true]);
|
||||
$f = $this->f;
|
||||
$create = fn ($fixture) => Invoice::query()->create(['tenant_id' => $fixture['tenant']->id, 'purchase_order_id' => $fixture['order']->id, 'supplier_id' => $fixture['order']->supplier_id,
|
||||
'created_by' => $fixture['issuer']->id, 'number' => 'QA-SEARCH-INVOICE', 'issued_on' => today(), 'status' => 'pending', 'subtotal_minor' => 10000, 'tax_minor' => 0, 'total_minor' => 10000,
|
||||
'snapshot' => ['synthetic' => true, 'private_note' => 'Never return in search']]);
|
||||
$ownInvoice = $create($f);
|
||||
$create(receiptFixture());
|
||||
$finance = User::factory()->create(['tenant_id' => $f['tenant']->id, 'department_id' => $f['department']->id, 'purchasing_role' => PurchasingRole::Finance]);
|
||||
$this->actingAs($finance)->get('/compras/pesquisa?q=QA-SEARCH-INVOICE')->assertInertia(fn (Assert $page) => $page->has('groups', 8)
|
||||
->where('groups.7.key', 'invoices')->where('groups.7.records.total', 1)->where('groups.7.records.data.0.href', '/facturas?invoice_id='.$ownInvoice->id)
|
||||
->missing('groups.7.records.data.0.snapshot')->missing('groups.7.records.data.0.total_minor'));
|
||||
$this->get('/facturas?invoice_id='.$ownInvoice->id)->assertOk()->assertInertia(fn (Assert $page) => $page->where('focusInvoiceId', $ownInvoice->id));
|
||||
$this->actingAs($f['issuer'])->get('/compras/pesquisa?q=QA-SEARCH-INVOICE')->assertInertia(fn (Assert $page) => $page->has('groups', 7));
|
||||
config(['procurement_search.finance_metadata_enabled' => false]);
|
||||
$this->actingAs($finance)->get('/compras/pesquisa?q=QA-SEARCH-INVOICE')->assertInertia(fn (Assert $page) => $page->has('groups', 7));
|
||||
});
|
||||
Reference in New Issue
Block a user