A free open source IT asset/license management system
Added test for weird url search result
snipeeb11108c1de8
diff --git a/app/Models/Traits/Searchable.php b/app/Models/Traits/Searchable.phpindex 026a569cae..ff9e95961e 100644--- a/app/Models/Traits/Searchable.php+++ b/app/Models/Traits/Searchable.php@@ -54,6 +54,27 @@ trait Searchable*/public function scopeTextSearch($query, $search){+ // Array-form input (?filter[field]=value, which Laravel parses+ // to ['field' => 'value']) is already structured. Routing it+ // through implode-and-reparse would discard the keys. Promote+ // to structured filters directly when any key is a string.+ // Numeric-only arrays fall through to the legacy implode path+ // so stray ->TextSearch(['a','b']) callers keep their+ // free-text behavior.+ if (is_array($search) && count(array_filter(array_keys($search), 'is_string')) > 0) {+ $filters = array_filter(+ $search,+ fn ($v, $k) => is_string($k) && is_scalar($v) && trim((string) $v) !== '',+ ARRAY_FILTER_USE_BOTH+ );++ if (empty($filters)) {+ return $query;+ }++ return $this->applySearchFilters($query, $filters, $this->resolveStructuredFilterOperator());+ }+$preparedSearch = $this->prepareSearchInput(is_array($search) ? implode(' ', $search) : (string) $search);$terms = $preparedSearch['terms'];$filters = $preparedSearch['filters'];diff --git a/tests/Feature/Search/SearchableTraitTest.php b/tests/Feature/Search/SearchableTraitTest.phpindex 71969bf147..82b3aae429 100644--- a/tests/Feature/Search/SearchableTraitTest.php+++ b/tests/Feature/Search/SearchableTraitTest.php@@ -1413,4 +1413,105 @@ class SearchableTraitTest extends TestCase->assertOk()->assertJson(fn (AssertableJson $json) => $json->has('rows', 1)->etc());}++ /**+ * Regression: ?filter[field]=value URL-array form must apply the+ * structured filter instead of degrading to free-text. Before the+ * fix in scopeTextSearch, the array was imploded to the value+ * only (keys discarded), so the field constraint was lost and the+ * value matched any searchable attribute.+ */+ public function test_array_form_filter_applies_as_structured_filter()+ {+ Asset::factory()->create(['name' => 'MacBook Pro 15"', 'serial' => 'SN-UNIQUE-NAME']);+ Asset::factory()->create(['name' => 'MacBook Pro 16"', 'serial' => 'SN-OTHER']);++ $this->actingAsForApi(User::factory()->viewAssets()->create())+ ->getJson(route('api.assets.index', [+ 'filter' => ['serial' => 'SN-UNIQUE-NAME'],+ ]))+ ->assertOk()+ ->assertJson(fn (AssertableJson $json) => $json->has('rows', 1)->etc());+ }++ /**+ * Array-form filter with multiple fields defaults to AND semantics.+ */+ public function test_array_form_filter_combines_fields_with_and_by_default()+ {+ Asset::factory()->create(['name' => 'MacBook', 'serial' => 'SN-MATCH']);+ Asset::factory()->create(['name' => 'MacBook', 'serial' => 'SN-OTHER']);+ Asset::factory()->create(['name' => 'Dell', 'serial' => 'SN-MATCH']);++ $this->actingAsForApi(User::factory()->viewAssets()->create())+ ->getJson(route('api.assets.index', [+ 'filter' => [+ 'name' => 'MacBook',+ 'serial' => 'SN-MATCH',+ ],+ ]))+ ->assertOk()+ ->assertJson(fn (AssertableJson $json) => $json->has('rows', 1)->etc());+ }++ /**+ * Array-form filter honors ?filter_operator=or.+ */+ public function test_array_form_filter_honors_or_operator()+ {+ Asset::factory()->create(['name' => 'OnlyNameMatch-Alpha']);+ Asset::factory()->create(['name' => 'OtherAsset', 'serial' => 'OnlySerialMatch-Beta']);+ Asset::factory()->create(['name' => 'Unrelated', 'serial' => 'SomethingElse']);++ $this->actingAsForApi(User::factory()->viewAssets()->create())+ ->getJson(route('api.assets.index', [+ 'filter' => [+ 'name' => 'OnlyNameMatch-Alpha',+ 'serial' => 'OnlySerialMatch-Beta',+ ],+ 'filter_operator' => 'or',+ ]))+ ->assertOk()+ ->assertJson(fn (AssertableJson $json) => $json->has('rows', 2)->etc());+ }++ /**+ * Empty values in the array-form filter are dropped (so clearing an+ * input in the UI does not emit a noisy no-op filter that+ * accidentally narrows the result set).+ */+ public function test_array_form_filter_ignores_empty_values()+ {+ Asset::factory()->create(['name' => 'KeeperAsset']);+ Asset::factory()->create(['name' => 'OtherAsset']);++ $this->actingAsForApi(User::factory()->viewAssets()->create())+ ->getJson(route('api.assets.index', [+ 'filter' => [+ 'name' => 'KeeperAsset',+ 'serial' => '',+ ],+ ]))+ ->assertOk()+ ->assertJson(fn (AssertableJson $json) => $json->has('rows', 1)->etc());+ }++ /**+ * Array-form filter also works on relation keys, not just direct+ * attributes, confirming the full structured-filter pipeline+ * engages, not just the attribute path.+ */+ public function test_array_form_filter_resolves_relation_keys()+ {+ $supplier = Supplier::factory()->create(['name' => 'ArrayFormSupplier-'.now()->timestamp]);+ Asset::factory()->create(['name' => 'A', 'supplier_id' => $supplier->id]);+ Asset::factory()->create(['name' => 'B']);++ $this->actingAsForApi(User::factory()->viewAssets()->create())+ ->getJson(route('api.assets.index', [+ 'filter' => ['supplier' => 'ArrayFormSupplier'],+ ]))+ ->assertOk()+ ->assertJson(fn (AssertableJson $json) => $json->has('rows', 1)->etc());+ }}