From b7866180029aa49584c2b102bb616ed52228832c Mon Sep 17 00:00:00 2001 From: Rom1-B <8530352+Rom1-B@users.noreply.github.com> Date: Wed, 18 Nov 2026 11:08:14 +0100 Subject: [PATCH] Fix: compare number field values numerically in search --- CHANGELOG.md | 1 + hook.php | 46 ++++++++----- tests/Units/HookAddWhereTest.php | 113 +++++++++++++++++++++++++++++++ 3 files changed, 144 insertions(+), 16 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4b19cb7d..098da5a4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/). - Fix search criterion value on multiple dropdown fields being reset after a search, which emptied the criterion when sorting or paging results - Fix item clone failing when a mandatory GLPI item field is in an "Insert in form" block - Fix `Some mandatory fields are empty` error displayed when creating an item with an assigned technician +- Fix search on number fields: `is` / `is not` ignoring values from 1000, `<` / `>` comparing values as text, and `is not` excluding items without value ## [1.24.7] - 2026-10-28 diff --git a/hook.php b/hook.php index bc3a3526..0723aecc 100644 --- a/hook.php +++ b/hook.php @@ -292,24 +292,38 @@ function plugin_fields_addWhere($link, $nott, $itemtype, $ID, $val, $searchtype) } if ($field_field->fields['type'] === 'number') { - // if 'number' field with searchtype 'equals' or 'notequals' - // update WHERE clause with `$table_$field.$field` because without `$table_$field.id` is used - if ($searchtype == 'equals' || $searchtype == 'notequals') { - $operator = ($searchtype == 'equals') ? '=' : '!='; - if ($nott) { - $link .= ' NOT '; + // Number values are stored in a VARCHAR column: compare them numerically. + // The predicate is never NULL, so its negation also matches items without value. + $column = $DB->quoteName($table . '_' . $field) . '.' . $DB->quoteName($field); + $number_predicate = static function (string $operator, string $value) use ($DB, $column): string { + $value = str_replace(',', '.', trim($value)); + if (!is_numeric($value) || abs((float) $value) >= 1e35) { + // No number can match (DECIMAL(65,30) range), and casting it would raise a SQL warning + return '(1 = 0)'; } - return $link . ' CAST(' . $DB->quoteName($table . '_' . $field) . '.' . $DB->quoteName($field) . ' AS DECIMAL(10,7))' . $operator . ' ' . $DB->quoteValue($val); - } else { - // if 'number' field with <= or >= or < or > search - // update WHERE clause with the correct operator - $val = html_entity_decode((string) $val); - if (preg_match('/(<=|>=|>|<)/', $val, $matches)) { - $operator = $matches[1]; - $val = trim(str_replace($operator, '', $val)); - return $link . $DB->quoteName($table . '_' . $field) . '.' . $DB->quoteName($field) . $operator . ' ' . $DB->quoteValue($val); - } + // Like core, items without value match an equality search on 0 + $format = $operator === '=' && (float) $value == 0 + ? '(%1$s IS NULL OR %1$s = \'\' OR CAST(%1$s AS DECIMAL(65,30)) %2$s CAST(%3$s AS DECIMAL(65,30)))' + : '(%1$s IS NOT NULL AND %1$s <> \'\' AND CAST(%1$s AS DECIMAL(65,30)) %2$s CAST(%3$s AS DECIMAL(65,30)))'; + + return sprintf( + $format, + $column, + $operator, + $DB->quoteValue($value), + ); + }; + + if ($searchtype == 'equals' || $searchtype == 'notequals') { + $negate = (bool) $nott !== ($searchtype === 'notequals'); + return $link . ($negate ? ' NOT ' : ' ') . $number_predicate('=', (string) $val); + } + + // 'contains' search with a <=, >=, < or > prefix + $val = html_entity_decode((string) $val); + if (preg_match('/^\s*(<=|>=|>|<)(.*)$/', $val, $matches)) { + return $link . ($nott ? ' NOT ' : ' ') . $number_predicate($matches[1], $matches[2]); } } diff --git a/tests/Units/HookAddWhereTest.php b/tests/Units/HookAddWhereTest.php index cf5709b8..8622a6e3 100644 --- a/tests/Units/HookAddWhereTest.php +++ b/tests/Units/HookAddWhereTest.php @@ -30,6 +30,7 @@ namespace GlpiPlugin\Field\Tests\Units; +use PHPUnit\Framework\Attributes\DataProvider; use Computer; use Glpi\Tests\DbTestCase; use Glpi\Tests\GLPITestCase; @@ -125,4 +126,116 @@ public function testAddWhereDoesNotThrowOnDuplicateFieldName(): void $this->assertNotFalse($result); } + + public static function numberSearchProvider(): iterable + { + $all = ['999', '1000', '1000.0', '1500', '12345', '12.50', '-5', '0', 'cleared', 'unsaved']; + $without = static fn(string ...$excluded): array => array_values(array_diff($all, $excluded)); + + yield 'is 999' => ['equals', '999', false, ['999']]; + yield 'is 1000' => ['equals', '1000', false, ['1000', '1000.0']]; + yield 'is 12345' => ['equals', '12345', false, ['12345']]; + yield 'is 12.5' => ['equals', '12.5', false, ['12.50']]; + yield 'is 12,5' => ['equals', '12,5', false, ['12.50']]; + yield 'is -5' => ['equals', '-5', false, ['-5']]; + yield 'is 1e3' => ['equals', '1e3', false, ['1000', '1000.0']]; + yield 'is 1e999' => ['equals', '1e999', false, []]; + // Like core, items without value match an equality search on 0 + yield 'is 0' => ['equals', '0', false, ['0', 'cleared', 'unsaved']]; + yield 'is not 0' => ['notequals', '0', false, $without('0', 'cleared', 'unsaved')]; + yield 'is not 1000' => ['notequals', '1000', false, $without('1000', '1000.0')]; + yield 'is not 999' => ['notequals', '999', false, $without('999')]; + yield 'AND NOT is 1000' => ['equals', '1000', true, $without('1000', '1000.0')]; + yield 'AND NOT is not 1000' => ['notequals', '1000', true, ['1000', '1000.0']]; + yield 'contains > 900' => ['contains', '>900', false, ['999', '1000', '1000.0', '1500', '12345']]; + yield 'contains > 1000' => ['contains', '>1000', false, ['1500', '12345']]; + yield 'contains >= 1500' => ['contains', '>= 1500', false, ['1500', '12345']]; + yield 'contains < 1000' => ['contains', '<1000', false, ['999', '12.50', '-5', '0']]; + yield 'contains <= 1000' => ['contains', '<=1000', false, ['999', '1000', '1000.0', '12.50', '-5', '0']]; + yield 'contains < 2000' => ['contains', '<2000', false, ['999', '1000', '1000.0', '1500', '12.50', '-5', '0']]; + yield 'contains < 2000' => ['contains', '<2000', false, ['999', '1000', '1000.0', '1500', '12.50', '-5', '0']]; + yield 'contains < 0' => ['contains', '<0', false, ['-5']]; + yield 'not contains < 2000' => ['notcontains', '<2000', false, ['12345', 'cleared', 'unsaved']]; + yield 'contains 100 (no operator)' => ['contains', '100', false, ['1000', '1000.0']]; + // Casting a non numeric value would raise a SQL warning, which fails the test + yield 'is abc' => ['equals', 'abc', false, []]; + yield 'is not abc' => ['notequals', 'abc', false, $all]; + yield 'contains < abc' => ['contains', ' compared them as strings. + */ + #[DataProvider('numberSearchProvider')] + public function testNumberFieldSearch(string $searchtype, string $value, bool $not, array $expected): void + { + $found = $this->searchNumberField($searchtype, $value, $not); + sort($expected); + $this->assertSame($expected, $found); + } + + /** + * Search computers on a number field, among items storing a predefined set of values. + * + * @return list Sorted names (without prefix) of the found items + */ + private function searchNumberField(string $searchtype, string $value, bool $not): array + { + $this->login(); + + $container = $this->createFieldContainer([ + 'label' => 'Number search ' . $this->getUniqueString(), + 'type' => 'tab', + 'itemtypes' => [Computer::class], + 'is_active' => 1, + 'entities_id' => 0, + 'is_recursive' => 1, + ]); + $field = $this->createField([ + 'label' => 'Number', + 'type' => 'number', + PluginFieldsContainer::getForeignKeyField() => $container->getID(), + 'ranking' => 1, + 'is_active' => 1, + 'is_readonly' => 0, + ]); + + $entities_id = $this->getTestRootEntity(true); + $prefix = 'Number search ' . $this->getUniqueString() . ' '; + $container_class = PluginFieldsContainer::getClassname(Computer::class, $container->fields['name']); + $stored_values = ['999', '1000', '1000.0', '1500', '12345', '12.50', '-5', '0', 'cleared' => '']; + foreach ($stored_values as $name => $stored_value) { + $name = is_int($name) ? $stored_value : $name; + $computer = $this->createItem(Computer::class, ['name' => $prefix . $name, 'entities_id' => $entities_id]); + $this->createItem($container_class, [ + 'items_id' => $computer->getID(), + 'itemtype' => Computer::class, + 'plugin_fields_containers_id' => $container->getID(), + $field->fields['name'] => $stored_value, + ]); + } + + // Item for which the container was never saved: no row, NULL through the LEFT JOIN + $this->createItem(Computer::class, ['name' => $prefix . 'unsaved', 'entities_id' => $entities_id]); + + $so_id = PluginFieldsField::SEARCH_OPTION_STARTING_INDEX + $field->getID(); + $data = Search::getDatas(Computer::class, [ + 'is_deleted' => 0, + 'start' => 0, + 'list_limit' => 100, + 'criteria' => [ + ['field' => 1, 'searchtype' => 'contains', 'value' => '^' . $prefix], + ['link' => $not ? 'AND NOT' : 'AND', 'field' => $so_id, 'searchtype' => $searchtype, 'value' => $value], + ], + ], [1]); + + $found = array_map( + static fn(array $row): string => substr((string) $row['raw']['ITEM_Computer_1'], strlen($prefix)), + $data['data']['rows'], + ); + sort($found); + + return $found; + } }