A free open source IT asset/license management system
Fixed custom field search nesting
snipe8c9a48b38ad6
diff --git a/app/Models/Traits/Searchable.php b/app/Models/Traits/Searchable.phpindex da74e66161..ec22765713 100644--- a/app/Models/Traits/Searchable.php+++ b/app/Models/Traits/Searchable.php@@ -456,23 +456,32 @@ trait Searchable}// Only pull unencrypted fields, since encrypted fields cannot be searched on- $customFields = CustomField::where('field_encrypted', 0)->get();- $firstConditionAdded = false;+ $customFields = CustomField::query()+ ->whereNotNull('db_column')+ ->where('field_encrypted', 0)+ ->get(['db_column']);- foreach ($customFields as $field) {- foreach ($terms as $term) {- if (! $firstConditionAdded) {- $query = $query->where($this->getTable().'.'.$field->db_column_name(), 'LIKE', '%'.$term.'%');- $firstConditionAdded = true;+ if ($customFields->isEmpty()) {+ return $query;+ }- continue;- }+ // Group custom-fields so all custom fields behave consistently as OR conditions.+ return $query->orWhere(function (Builder $customFieldQuery) use ($customFields, $terms): void {+ $firstConditionAdded = false;- $query = $query->orWhere($this->getTable().'.'.$field->db_column_name(), 'LIKE', '%'.$term.'%');- }- }+ foreach ($customFields as $field) {+ foreach ($terms as $term) {+ if (!$firstConditionAdded) {+ $customFieldQuery->where($this->getTable() . '.' . $field->db_column_name(), 'LIKE', '%' . $term . '%');+ $firstConditionAdded = true;- return $query;+ continue;+ }++ $customFieldQuery->orWhere($this->getTable() . '.' . $field->db_column_name(), 'LIKE', '%' . $term . '%');+ }+ }+ });}/**diff --git a/tests/Feature/Search/SearchableTraitTest.php b/tests/Feature/Search/SearchableTraitTest.phpindex d47d5ff979..f9d38481fc 100644--- a/tests/Feature/Search/SearchableTraitTest.php+++ b/tests/Feature/Search/SearchableTraitTest.php@@ -520,6 +520,54 @@ class SearchableTraitTest extends TestCase->assertJson(fn (AssertableJson $json) => $json->has('rows', 0)->etc());}+ /**+ * Test asset free-text search across multiple custom fields.+ */+ public function test_asset_free_text_search_matches_across_multiple_custom_fields()+ {+ $macFieldOne = CustomField::factory()->macAddress()->create([+ 'name' => 'MAC Address One '.now()->timestamp,+ ]);+ $macFieldTwo = CustomField::factory()->macAddress()->create([+ 'name' => 'MAC Address Two '.(now()->timestamp + 1),+ ]);++ $dbColumnOne = $macFieldOne->db_column_name();+ $dbColumnTwo = $macFieldTwo->db_column_name();++ $firstMatchingAsset = Asset::factory()->create([+ $dbColumnOne => 'AA:BB:CC:11:22:33',+ $dbColumnTwo => null,+ ]);+ $secondMatchingAsset = Asset::factory()->create([+ $dbColumnOne => null,+ $dbColumnTwo => 'AA:BB:CC:44:55:66',+ ]);+ Asset::factory()->create([+ $dbColumnOne => '10:20:30:40:50:60',+ $dbColumnTwo => '66:55:44:33:22:11',+ ]);++ $response = $this->actingAsForApi(User::factory()->viewAssets()->create())+ ->getJson(route('api.assets.index', ['search' => 'AA:BB:CC']))+ ->assertOk()+ ->assertJson(fn (AssertableJson $json) => $json->has('rows', 2)->etc());++ $returnedIds = collect($response->json('rows'))+ ->pluck('id')+ ->map(fn ($id) => (int) $id)+ ->sort()+ ->values()+ ->all();++ $expectedIds = collect([$firstMatchingAsset->id, $secondMatchingAsset->id])+ ->sort()+ ->values()+ ->all();++ $this->assertSame($expectedIds, $returnedIds);+ }+/*** Test filtering on a custom field using the raw db_column slug.*/