-
Notifications
You must be signed in to change notification settings - Fork 62
[Fix]: Data values not formatted when coming from fields plugin custom fields #655
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
jdurand-teclib
merged 8 commits into
main
from
fix/45898/dates-from-fields-not-formatted
Aug 26, 2026
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
019b145
fix(45898): data values not formatted when coming from fields plugin
jdurand-teclib 5de6a4f
chore(45898): changelog
jdurand-teclib bea4c95
fix(45898): float format detection by datainjection plugin
jdurand-teclib c7c86a0
fix(45898): incomplete regex that was not properly catching all forms…
jdurand-teclib 9c05d17
Update inc/commoninjectionlib.class.php
jdurand-teclib 9891bf4
fix(45898): more accurate usage of fieldype in checkType method
jdurand-teclib c9a98be
chore(45898): Add unit tests
jdurand-teclib 58c603f
Update tests/unit/CommonInjectionLibFloatDetectionTest.php
jdurand-teclib File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| <?php | ||
|
|
||
| /** | ||
| * ------------------------------------------------------------------------- | ||
| * DataInjection plugin for GLPI | ||
| * ------------------------------------------------------------------------- | ||
| * | ||
| * LICENSE | ||
| * | ||
| * This file is part of DataInjection. | ||
| * | ||
| * DataInjection is free software; you can redistribute it and/or modify | ||
| * it under the terms of the GNU General Public License as published by | ||
| * the Free Software Foundation; either version 2 of the License, or | ||
| * (at your option) any later version. | ||
| * | ||
| * DataInjection is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| * GNU General Public License for more details. | ||
| * | ||
| * You should have received a copy of the GNU General Public License | ||
| * along with DataInjection. If not, see <http://www.gnu.org/licenses/>. | ||
| * ------------------------------------------------------------------------- | ||
| * @copyright Copyright (C) 2007-2023 by DataInjection plugin team. | ||
| * @license GPLv2 https://www.gnu.org/licenses/gpl-2.0.html | ||
| * @link https://github.com/pluginsGLPI/datainjection | ||
| * ------------------------------------------------------------------------- | ||
| */ | ||
|
|
||
| namespace GlpiPlugin\Datainjection\Tests\Unit; | ||
|
|
||
| use Glpi\Tests\DbTestCase; | ||
| use PluginDatainjectionCommonInjectionLib; | ||
| use PluginDatainjectionComputerInjection; | ||
|
|
||
| require_once dirname(__DIR__, 2) . '/inc/injectioninterface.class.php'; | ||
| require_once dirname(__DIR__, 2) . '/inc/commoninjectionlib.class.php'; | ||
| require_once dirname(__DIR__, 2) . '/inc/computerinjection.class.php'; | ||
|
|
||
| /** | ||
| * Covers the float-detection regex used by reformatThirdPass() to derive | ||
| * $option['checktype'] when a search option (e.g. a custom field coming from | ||
| * a third-party plugin) only has a 'datatype' and no explicit 'checktype'. | ||
| * | ||
| * The regex under test is not hardcoded here: it is retrieved from | ||
| * PluginDatainjectionCommonInjectionLib::getFloatDetectionRegex(), the same | ||
| * shared source reformatThirdPass() itself calls, so this test can never | ||
| * drift out of sync with the actual pattern shipped in the code. | ||
| */ | ||
| final class CommonInjectionLibFloatDetectionTest extends DbTestCase | ||
| { | ||
| private static function getFloatDetectionRegex(): string | ||
| { | ||
| $lib = new PluginDatainjectionCommonInjectionLib(new PluginDatainjectionComputerInjection()); | ||
|
|
||
| return $lib->getFloatDetectionRegex(); | ||
| } | ||
|
|
||
| public static function floatDetectionProvider(): array | ||
| { | ||
| return [ | ||
| // Accepted formats | ||
| 'plain dot decimal' => ['1234.56', true], | ||
| 'plain comma decimal' => ['1234,56', true], | ||
| 'leading zero decimal' => ['0.5', true], | ||
| 'space-grouped thousands, dot decimal' => ['1 234.56', true], | ||
| 'space-grouped thousands, comma decimal' => ['1 234,56', true], | ||
| 'multiple space groups, comma decimal' => ['12 345 678,90', true], | ||
| 'comma-grouped thousands, dot decimal' => ['1,234.56', true], | ||
| 'multiple comma groups, dot decimal' => ['12,345,678.90', true], | ||
| 'basic comma decimal, no grouping' => ['12,34', true], | ||
|
|
||
| // Rejected formats (should fall back to $option['datatype']) | ||
| 'plain integer, no separator' => ['1234', false], | ||
| 'empty string' => ['', false], | ||
| 'non numeric text' => ['abc', false], | ||
| 'multiple dots (malformed)' => ['1234.56.78', false], | ||
| 'comma-grouped integer, no decimal part' => ['1,234,567', false], | ||
| 'space-grouped integer, no decimal part' => ['1 234', false], | ||
| 'leading dot, no integer part' => ['.56', false], | ||
| 'negative float (unsupported by regex)' => ['-1234.56', false], | ||
| 'malformed space grouping (2nd group not 3 digits)' => ['12 34.56', false], | ||
| 'malformed comma grouping (1st group over 3 digits)' => ['1234,567.89', false], | ||
| 'leading whitespace' => [' 1234.56', false], | ||
| 'trailing whitespace' => ['1234.56 ', false], | ||
| 'scientific notation (unsupported by regex)' => ['1.5e10', false], | ||
| ]; | ||
| } | ||
|
|
||
| /** | ||
| * @dataProvider floatDetectionProvider | ||
| */ | ||
| public function testFloatDetectionRegex(string $value, bool $expected_match): void | ||
| { | ||
| $regex = self::getFloatDetectionRegex(); | ||
|
|
||
| self::assertSame($expected_match, preg_match($regex, $value) !== 0); | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.