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=