withoutVite(); $this->f = receiptFixture(); $this->actingAs($this->f['issuer']); }); test('global supply search returns only current native agreement and contract references with no private payload or writes', function () { config(['procurement_search.supply_metadata_enabled' => true, 'agreement_order_limits.qa_enabled' => true]); supplyGrant($this->f['issuer'], ['supply_prepare']); $planning = app(SupplyPlanning::class); $agreement = $planning->save($this->f['issuer'], 'agreement', supplyInput([ 'stock_item_id' => $this->f['stock']->id, 'warehouse_id' => $this->f['warehouse']->id, 'supplier_id' => $this->f['order']->supplier_id, 'habitual' => false, 'valid_from' => today()->toDateString(), 'valid_until' => today()->addYear()->toDateString(), 'origin' => 'local', 'currency' => 'MZN', 'price_minor' => 10000, 'lead_days' => 4, 'reference' => 'SEARCH-AGREEMENT-OLD'])); $current = $planning->save($this->f['issuer'], 'agreement', supplyInput([...$agreement->data, 'reference' => 'SEARCH-AGREEMENT-NEW'], 'save', $agreement)); $contract = $planning->save($this->f['issuer'], 'contract', supplyInput([ 'supplier_id' => $this->f['order']->supplier_id, 'reference' => 'SEARCH-CONTRACT', 'currency' => 'MZN', 'valid_from' => today()->toDateString(), 'valid_until' => today()->addYear()->toDateString(), 'periodicity_days' => 30, 'order_limit_amount' => '100.00', 'lead_days' => 3])); $supplierName = $contract->snapshot['supplier']['name']; Supplier::query()->findOrFail($this->f['order']->supplier_id)->update(['name' => 'Later supplier name, outside original reference']); $before = []; foreach (['supply_records', 'audit_logs', 'requisitions', 'purchase_orders', 'invoices', 'stock_movements'] as $table) { $before[$table] = DB::table($table)->orderBy('id')->get()->toJson(); } $this->get('/compras/pesquisa?q=SEARCH-')->assertOk()->assertViewHas('page', fn ($page) => $page['encryptHistory'] === true)->assertInertia(fn (Assert $page) => $page ->where('supplySearchAvailable', true)->has('groups', 3) ->where('groups.1.records.total', 1)->where('groups.1.records.data.0.id', $current->id) ->where('groups.1.records.data.0.title', 'SEARCH-AGREEMENT-NEW · v2') ->where('groups.1.records.data.0.href', '/compras/fornecimento?view=agreements&record_id='.$current->id.'#supply-record-'.$current->id) ->where('groups.2.records.data.0.id', $contract->id)->missing('groups.2.records.data.0.data') ->missing('groups.2.records.data.0.price_minor')->missing('groups.2.records.data.0.snapshot')); $this->get('/compras/pesquisa?q=SEARCH-AGREEMENT-OLD')->assertInertia(fn (Assert $page) => $page->where('groups.1.records.total', 0)); $this->get('/compras/pesquisa?q='.urlencode($supplierName))->assertInertia(fn (Assert $page) => $page ->where('groups.1.records.total', 1)->where('groups.2.records.total', 1)); foreach ($before as $table => $rows) { expect(DB::table($table)->orderBy('id')->get()->toJson())->toBe($rows); } $this->get('/compras/fornecimento?view=agreements&record_id='.$current->id)->assertOk(); $this->get('/compras/fornecimento?view=contracts&record_id='.$contract->id)->assertOk(); config(['procurement_search.supply_metadata_enabled' => false]); $this->get('/compras/pesquisa?q=SEARCH-')->assertInertia(fn (Assert $page) => $page->where('supplySearchAvailable', false)->has('groups', 1)); }); test('supply search preserves native isolation literal matching pagination and current access', function () { config(['procurement_search.supply_metadata_enabled' => true]); $own = $this->f; supplyGrant($own['issuer'], ['supply_read', 'supply_prepare']); $planning = app(SupplyPlanning::class); $input = ['stock_item_id' => $own['stock']->id, 'warehouse_id' => $own['warehouse']->id, 'supplier_id' => $own['order']->supplier_id, 'habitual' => false, 'valid_from' => today()->toDateString(), 'valid_until' => today()->addYear()->toDateString(), 'origin' => 'local', 'currency' => 'MZN', 'price_minor' => 10000, 'lead_days' => 4]; foreach (range(1, 17) as $index) { $planning->save($own['issuer'], 'agreement', supplyInput([...$input, 'reference' => 'SUPPLY-PAGE-'.$index])); } $literal = $planning->save($own['issuer'], 'agreement', supplyInput([...$input, 'reference' => 'LITERAL%_SUPPLY'])); $foreign = receiptFixture(); supplyGrant($foreign['issuer'], ['supply_prepare']); $this->actingAs($foreign['issuer']); $planning->save($foreign['issuer'], 'agreement', supplyInput([...$input, 'stock_item_id' => $foreign['stock']->id, 'warehouse_id' => $foreign['warehouse']->id, 'supplier_id' => $foreign['order']->supplier_id, 'reference' => 'SUPPLY-PAGE-FOREIGN'])); $this->actingAs($own['issuer'])->get('/compras/pesquisa?q=SUPPLY-PAGE&agreements_page=2')->assertOk() ->assertInertia(fn (Assert $page) => $page->has('groups', 2)->where('groups.1.records.total', 17) ->where('groups.1.records.current_page', 2)->has('groups.1.records.data', 2)); $this->get('/compras/pesquisa?q='.urlencode('%_'))->assertInertia(fn (Assert $page) => $page ->where('groups.1.records.total', 1)->where('groups.1.records.data.0.id', $literal->id)); $this->get('/compras/pesquisa?q=SUPPLY-PAGE&agreements_page=0')->assertSessionHasErrors('agreements_page'); $this->get('/compras/pesquisa?q=SUPPLY-PAGE&contracts_page=0')->assertSessionHasErrors('contracts_page'); $requester = User::factory()->create(['tenant_id' => $own['tenant']->id, 'department_id' => $own['department']->id, 'purchasing_role' => PurchasingRole::Configured]); supplyGrant($requester, ['requester']); $this->actingAs($requester)->get('/compras/pesquisa?q=SUPPLY-PAGE')->assertOk()->assertInertia(fn (Assert $page) => $page ->where('supplySearchAvailable', false)->has('groups', 1)); $this->actingAs($own['issuer']); DB::table('organization_memberships')->where('user_id', $own['issuer']->id)->update(['expires_at' => now()->subSecond()]); $this->get('/compras/pesquisa?q=SUPPLY-PAGE')->assertForbidden(); }); 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('invoiceSearchAvailable', true)->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->where('invoiceSearchAvailable', false)->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->where('invoiceSearchAvailable', false)->has('groups', 7)); });