A free open source IT asset/license management system
Fixed #19708 - better handle EOL date on searchable trait
snipe4337d3357e69
diff --git a/app/Models/Traits/Searchable.php b/app/Models/Traits/Searchable.phpindex 44feae79b9..71aa349fb2 100644--- a/app/Models/Traits/Searchable.php+++ b/app/Models/Traits/Searchable.php@@ -8,6 +8,7 @@ use App\Models\Location;use App\Models\User;use Illuminate\Database\Eloquent\Builder;use Illuminate\Support\Facades\DB;+use Illuminate\Support\Facades\Schema;/*** This trait allows for cleaner searching of models,@@ -701,19 +702,24 @@ trait Searchableif ($dbColumn !== null) {$column = $table.'.'.$dbColumn;+ $isText = $this->isTextTypeColumn($table, $dbColumn);$method = $boolean === 'or' ? 'orWhere' : 'where';- $query->{$method}(function (Builder $subQuery) use ($column, $isNull): void {+ $query->{$method}(function (Builder $subQuery) use ($column, $isNull, $isText): void {if ($isNull) {- $subQuery->whereNull($column)- ->orWhere($column, '=', '');+ $subQuery->whereNull($column);+ if ($isText) {+ $subQuery->orWhere($column, '=', '');+ }return;}- $subQuery->whereNotNull($column)- ->where($column, '!=', '');+ $subQuery->whereNotNull($column);+ if ($isText) {+ $subQuery->where($column, '!=', '');+ }});return $query;@@ -723,18 +729,23 @@ trait Searchable// Direct attribute column.if (in_array($filterKey, $searchableAttributes, true)) {$column = $table.'.'.$filterKey;+ $isText = $this->isTextTypeColumn($table, $filterKey);$method = $boolean === 'or' ? 'orWhere' : 'where';- $query->{$method}(function (Builder $subQuery) use ($column, $isNull): void {+ $query->{$method}(function (Builder $subQuery) use ($column, $isNull, $isText): void {if ($isNull) {- $subQuery->whereNull($column)- ->orWhere($column, '=', '');+ $subQuery->whereNull($column);+ if ($isText) {+ $subQuery->orWhere($column, '=', '');+ }return;}- $subQuery->whereNotNull($column)- ->where($column, '!=', '');+ $subQuery->whereNotNull($column);+ if ($isText) {+ $subQuery->where($column, '!=', '');+ }});return $query;@@ -1232,6 +1243,36 @@ trait Searchablereturn $map[$filterKey] ?? null;}+ /**+ * Whether the given column is a text-shaped type (varchar, text, etc.).+ *+ * Used by applyNullFilter to decide whether the empty-string ('') check+ * makes sense alongside IS NULL / IS NOT NULL. On DATE / DATETIME / numeric+ * columns, comparing to '' in strict MySQL modes yields UNKNOWN and+ * silently excludes every row (the exact bug in GH #19708 for+ * asset_eol_date). Skipping the empty-string check for those columns+ * fixes it without changing behavior on text columns.+ *+ * Answers are cached per table.column for the request.+ */+ private function isTextTypeColumn(string $table, string $column): bool+ {+ static $cache = [];+ $key = $table.'.'.$column;++ if (array_key_exists($key, $cache)) {+ return $cache[$key];+ }++ try {+ $type = strtolower(Schema::getColumnType($table, $column));+ } catch (\Throwable) {+ return $cache[$key] = true;+ }++ return $cache[$key] = str_contains($type, 'char') || str_contains($type, 'text');+ }+/*** Build a lookup map for custom field filter resolution.*diff --git a/tests/Feature/Search/SearchableTraitTest.php b/tests/Feature/Search/SearchableTraitTest.phpindex 16eda3ab86..98148599dc 100644--- a/tests/Feature/Search/SearchableTraitTest.php+++ b/tests/Feature/Search/SearchableTraitTest.php@@ -845,6 +845,42 @@ class SearchableTraitTest extends TestCase$this->assertNotContains((int) $blank->id, $returnedIds);}+ /**+ * Regression coverage for GH #19708: `is:not_null` on a DATE column+ * previously returned zero rows because the applyNullFilter added a+ * `!= ''` check that MySQL evaluates as UNKNOWN against DATE columns+ * in strict mode, excluding every row.+ */+ public function test_is_null_filter_on_date_column()+ {+ // AssetFactory's afterMaking hook overwrites asset_eol_date, so set it after create.+ $withEol = Asset::factory()->create();+ $withEol->asset_eol_date = now()->addYear()->format('Y-m-d');+ $withEol->save();++ $withoutEol = Asset::factory()->create();+ $withoutEol->asset_eol_date = null;+ $withoutEol->save();++ $superuser = User::factory()->viewAssets()->create();++ $notNull = $this->actingAsForApi($superuser)+ ->getJson(route('api.assets.index', ['filter' => json_encode(['asset_eol_date' => 'is:not_null'])]))+ ->assertOk();++ $notNullIds = collect($notNull->json('rows'))->pluck('id')->map(fn ($id) => (int) $id)->all();+ $this->assertContains((int) $withEol->id, $notNullIds);+ $this->assertNotContains((int) $withoutEol->id, $notNullIds);++ $isNull = $this->actingAsForApi($superuser)+ ->getJson(route('api.assets.index', ['filter' => json_encode(['asset_eol_date' => 'is:null'])]))+ ->assertOk();++ $isNullIds = collect($isNull->json('rows'))->pluck('id')->map(fn ($id) => (int) $id)->all();+ $this->assertContains((int) $withoutEol->id, $isNullIds);+ $this->assertNotContains((int) $withEol->id, $isNullIds);+ }+/*** "is:not_null" on the User virtual "name" column should match users where* at least one constituent column (first_name, last_name) is not null.