Files
compras/tests/Feature/SavedProcurementSearchesTest.php
T
nala db1615fac1
tests / ci (push) Waiting to run
commit melhorias compras5
2026-11-22 19:39:09 +02:00

126 lines
8.3 KiB
PHP

<?php
use App\Enums\PurchasingRole;
use App\Models\User;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
use Inertia\Testing\AssertableInertia as Assert;
require_once __DIR__.'/../Support/ReceiptFixture.php';
beforeEach(function () {
$this->withoutVite();
$this->f = receiptFixture();
$this->actingAs($this->f['issuer']);
$this->body = ['name' => 'Necessidades da área', 'term' => 'QA-NEED', 'q' => 'QA-NEED'];
});
test('saved criteria are private encrypted metadata and repeated creation preserves a single preference', function () {
foreach ([1, 2] as $attempt) {
$this->postJson('/compras/pesquisas-guardadas', $this->body + ['tenant_id' => 999, 'user_id' => 999])->assertRedirect();
}
$row = DB::table('procurement_saved_searches')->sole();
expect($row->tenant_id)->toBe($this->f['tenant']->id)->and($row->user_id)->toBe($this->f['issuer']->id)->and($row->version)->toBe(1);
$this->get('/compras/pesquisa?q=QA-NEED')->assertInertia(fn (Assert $page) => $page->has('savedSearches.active', 1)
->where('savedSearches.active.0.name', $this->body['name'])->missing('savedSearches.active.0.user_id')->missing('savedSearches.active.0.results'));
$this->get('/compras/pesquisa')->assertViewHas('page', fn (array $page) => $page['encryptHistory'] === true);
});
test('opening saved criteria reruns the current native query instead of caching results or granting access', function () {
$this->f['r']->update(['number' => 'QA-NEED-ONE']);
$this->postJson('/compras/pesquisas-guardadas', $this->body)->assertRedirect();
$href = '/compras/pesquisa?q=QA-NEED';
$this->get($href)->assertInertia(fn (Assert $page) => $page->where('groups.0.records.total', 1));
$this->f['r']->update(['number' => 'CHANGED-SOURCE']);
$this->get($href)->assertInertia(fn (Assert $page) => $page->where('groups.0.records.total', 0)->has('savedSearches.active', 1));
$other = receiptFixture();
$this->actingAs($other['issuer'])->get($href)->assertInertia(fn (Assert $page) => $page->where('groups.0.records.total', 0)->has('savedSearches.active', 0));
});
test('rename archive and restore preserve explicit versions criteria and all business records', function () {
$this->postJson('/compras/pesquisas-guardadas', $this->body)->assertRedirect();
$row = DB::table('procurement_saved_searches')->sole();
$before = [];
foreach (['requisitions', 'audit_logs', 'budget_entries', 'stock_movements'] as $table) {
$before[$table] = DB::table($table)->orderBy('id')->get()->toJson();
}
$path = '/compras/pesquisas-guardadas/'.$row->id;
$this->putJson($path, ['version' => 1, 'name' => 'Nome revisto', 'term' => 'Texto revisto', 'q' => 'QA'])->assertRedirect();
$this->postJson($path.'/arquivar', ['version' => 1])->assertConflict();
$this->postJson($path.'/arquivar', ['version' => 2, 'q' => 'QA'])->assertRedirect();
$this->get('/compras/pesquisa?q=QA')->assertInertia(fn (Assert $page) => $page->has('savedSearches.active', 0)
->where('savedSearches.archived.total', 1)->where('savedSearches.archived.data.0.term', 'Texto revisto'));
$this->postJson($path.'/restaurar', ['version' => 3])->assertRedirect();
expect(DB::table('procurement_saved_searches')->sole()->version)->toBe(4)
->and(DB::table('procurement_saved_searches')->sole()->archived_at)->toBeNull();
foreach ($before as $table => $snapshot) {
expect(DB::table($table)->orderBy('id')->get()->toJson())->toBe($snapshot);
}
});
test('another user or company cannot read or mutate guessed preference identifiers', function () {
$this->postJson('/compras/pesquisas-guardadas', $this->body)->assertRedirect();
$id = DB::table('procurement_saved_searches')->sole()->id;
$colleague = User::factory()->create(['tenant_id' => $this->f['tenant']->id, 'department_id' => $this->f['department']->id, 'purchasing_role' => PurchasingRole::Requester]);
$foreign = receiptFixture();
foreach ([$colleague, $foreign['issuer']] as $actor) {
$this->actingAs($actor)->get('/compras/pesquisa')->assertInertia(fn (Assert $page) => $page->has('savedSearches.active', 0));
$this->putJson('/compras/pesquisas-guardadas/'.$id, $this->body + ['version' => 1])->assertNotFound();
$this->postJson('/compras/pesquisas-guardadas/'.$id.'/arquivar', ['version' => 1])->assertNotFound();
}
expect(DB::table('procurement_saved_searches')->sole()->version)->toBe(1);
});
test('active names are unique per user and restoring a colliding archived name never overwrites either record', function () {
$this->postJson('/compras/pesquisas-guardadas', $this->body)->assertRedirect();
$id = DB::table('procurement_saved_searches')->sole()->id;
$this->postJson('/compras/pesquisas-guardadas', [...$this->body, 'term' => 'OTHER'])->assertUnprocessable();
$this->postJson('/compras/pesquisas-guardadas/'.$id.'/arquivar', ['version' => 1])->assertRedirect();
$this->postJson('/compras/pesquisas-guardadas', [...$this->body, 'term' => 'OTHER'])->assertRedirect();
$this->postJson('/compras/pesquisas-guardadas/'.$id.'/restaurar', ['version' => 2])->assertUnprocessable();
expect(DB::table('procurement_saved_searches')->whereNotNull('archived_at')->sole()->term)->toBe('QA-NEED')
->and(DB::table('procurement_saved_searches')->whereNull('archived_at')->sole()->term)->toBe('OTHER');
});
test('query links encode criteria as local text and invalid or unavailable storage never silently discards them', function () {
$term = 'https://example.invalid/?x=%_&q=<script>';
$this->postJson('/compras/pesquisas-guardadas', [...$this->body, 'term' => $term])->assertRedirect();
$response = $this->get('/compras/pesquisa')->assertOk();
$page = $response->viewData('page');
$href = $page['props']['savedSearches']['active'][0]['href'];
expect(str_starts_with($href, '/compras/pesquisa?'))->toBeTrue();
parse_str(parse_url($href, PHP_URL_QUERY), $query);
expect($query['q'])->toBe($term);
$this->postJson('/compras/pesquisas-guardadas', ['name' => ' ', 'term' => ' '])->assertUnprocessable();
Schema::drop('procurement_saved_searches');
$this->get('/compras/pesquisa')->assertInertia(fn (Assert $page) => $page->where('savedSearches', null));
$this->postJson('/compras/pesquisas-guardadas', $this->body)->assertServiceUnavailable();
});
test('revoked native access is checked again before preference changes or live search exposure', function () {
$this->postJson('/compras/pesquisas-guardadas', $this->body)->assertRedirect();
$id = DB::table('procurement_saved_searches')->sole()->id;
// This security field is deliberately not mass assignable on User.
DB::table('users')->where('tenant_id', $this->f['tenant']->id)->where('id', $this->f['issuer']->id)->update(['purchasing_role' => PurchasingRole::Configured->value]);
expect(User::query()->findOrFail($this->f['issuer']->id)->purchasing_role)->toBe(PurchasingRole::Configured);
$this->get('/compras/pesquisa')->assertForbidden();
$this->postJson('/compras/pesquisas-guardadas/'.$id.'/arquivar', ['version' => 1])->assertForbidden();
expect(DB::table('procurement_saved_searches')->sole()->archived_at)->toBeNull();
});
test('active capacity and archived pagination preserve older criteria instead of deleting or overwriting them', function () {
foreach (range(1, 50) as $number) {
DB::table('procurement_saved_searches')->insert(['tenant_id' => $this->f['tenant']->id, 'user_id' => $this->f['issuer']->id,
'name' => 'Sintética '.$number, 'term' => 'QA-'.$number, 'active_name_key' => hash('sha256', 'sintética '.$number),
'version' => 1, 'created_at' => now(), 'updated_at' => now()]);
}
$this->postJson('/compras/pesquisas-guardadas', $this->body)->assertUnprocessable();
foreach (DB::table('procurement_saved_searches')->orderBy('id')->limit(11)->get() as $row) {
$this->postJson('/compras/pesquisas-guardadas/'.$row->id.'/arquivar', ['version' => 1])->assertRedirect();
}
$this->postJson('/compras/pesquisas-guardadas', $this->body)->assertRedirect();
$this->get('/compras/pesquisa?archived_page=2')->assertInertia(fn (Assert $page) => $page->has('savedSearches.active', 40)
->where('savedSearches.archived.total', 11)->has('savedSearches.archived.data', 1));
expect(DB::table('procurement_saved_searches')->count())->toBe(51);
});