Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
46 changes: 30 additions & 16 deletions hook.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
}
}

Expand Down
113 changes: 113 additions & 0 deletions tests/Units/HookAddWhereTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@

namespace GlpiPlugin\Field\Tests\Units;

use PHPUnit\Framework\Attributes\DataProvider;
use Computer;
use Glpi\Tests\DbTestCase;
use Glpi\Tests\GLPITestCase;
Expand Down Expand Up @@ -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 &lt; 2000' => ['contains', '&lt;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', '<abc', false, []];
}

/**
* Number values are stored as VARCHAR: "is" / "is not" used to cast them to DECIMAL(10,7),
* which overflows from 1000, and <, > 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<string> 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;
}
}
Loading