diff --git a/CHANGELOG.md b/CHANGELOG.md index d63779fa..c7483320 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/). - Fix item creation failing when a Yes/No field has a non boolean default value - Fix search on number fields: `is` / `is not` ignoring values from 1000, `<` / `>` comparing values as text, and `is not` excluding items without value - Fix GenericObject migration error message to name the required 3.0.x version +- Fix sort on number fields: values were sorted as text (`1000`, `12345`, `1500`, `999`) ## [1.25.1] - 2026-10-28 diff --git a/hook.php b/hook.php index 140a8078..93936325 100644 --- a/hook.php +++ b/hook.php @@ -359,6 +359,23 @@ function plugin_fields_addWhere($link, $nott, $itemtype, $ID, $val, $searchtype) }; } +function plugin_fields_addOrderBy($itemtype, $ID, $order, $key) +{ + /** @var DBmysql $DB */ + global $DB; + + $searchopt = Search::getOptions($itemtype)[$ID] ?? []; + if (($searchopt['pfields_type'] ?? null) !== 'number') { + return null; + } + + // Number values are stored in a VARCHAR column: sort them as DOUBLE (`+ 0`), whose range covers any stored value unlike DECIMAL. + // Empty values become NULL, sorted first in ascending order like core does. + $column = $DB->quoteName($searchopt['table'] . '_' . $searchopt['field']) . '.' . $DB->quoteName($searchopt['field']); + + return sprintf("NULLIF(%s, '') + 0 %s", $column, $order); +} + function plugin_item_transfer_fields(array $options): void { $itemtype = $options['type'] ?? null; diff --git a/tests/Units/HookAddWhereTest.php b/tests/Units/HookAddWhereTest.php index 628640a0..d7dcff0a 100644 --- a/tests/Units/HookAddWhereTest.php +++ b/tests/Units/HookAddWhereTest.php @@ -182,6 +182,77 @@ public function testNumberFieldSearch(string $searchtype, string $value, bool $n * @return list Sorted names (without prefix) of the found items */ private function searchNumberField(string $searchtype, string $value, bool $not): array + { + $found = $this->searchNumberFieldItems(static fn(int $so_id): array => [ + 'criteria' => [ + ['link' => $not ? 'AND NOT' : 'AND', 'field' => $so_id, 'searchtype' => $searchtype, 'value' => $value], + ], + ]); + sort($found); + + return $found; + } + + public static function numberSortProvider(): iterable + { + // Items without value are NULL, sorted first in ascending order + $asc = [null, null, -5.0, 0.0, 12.5, 999.0, 1000.0, 1000.0, 1500.0, 12345.0, 1e40]; + yield 'ASC' => ['ASC', $asc]; + yield 'DESC' => ['DESC', array_reverse($asc)]; + } + + /** + * Number values are stored as VARCHAR and used to be sorted as strings (1000, 12345, 1500, 999). + */ + #[DataProvider('numberSortProvider')] + public function testNumberFieldSort(string $order, array $expected): void + { + $found = $this->searchNumberFieldItems(static fn(int $so_id): array => [ + 'sort' => [$so_id], + 'order' => [$order], + ]); + + // Compare values, not names: the order of equal values (1000 and 1000.0, empty values) is not defined + $sorted = array_map( + static fn(string $name): ?float => in_array($name, ['cleared', 'unsaved'], true) ? null : (float) $name, + $found, + ); + $this->assertSame($expected, $sorted); + } + + /** + * Search computers among items storing a predefined set of values in a number field. + * + * @param callable(int): array $params Search parameters to add, given the search option id of the number field + * + * @return list Names (without prefix) of the found items, in result order + */ + private function searchNumberFieldItems(callable $params): array + { + [$so_id, $prefix] = $this->createNumberFieldItems(); + $params = $params($so_id); + $data = Search::getDatas(Computer::class, [ + 'is_deleted' => 0, + 'start' => 0, + 'list_limit' => 100, + 'criteria' => [ + ['field' => 1, 'searchtype' => 'contains', 'value' => '^' . $prefix], + ...($params['criteria'] ?? []), + ], + ] + $params, [1]); + + return array_map( + static fn(array $row): string => substr((string) $row['raw']['ITEM_Computer_1'], strlen($prefix)), + $data['data']['rows'], + ); + } + + /** + * Create a number field and computers storing a predefined set of values in it. + * + * @return array{int, string} Search option id of the field, prefix of the created computers names + */ + private function createNumberFieldItems(): array { $this->login(); @@ -220,23 +291,6 @@ private function searchNumberField(string $searchtype, string $value, bool $not) // 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; + return [PluginFieldsField::SEARCH_OPTION_STARTING_INDEX + $field->getID(), $prefix]; } }