From 904828036d58cd52f74e25ff5cd8a8f8541fea08 Mon Sep 17 00:00:00 2001 From: mbressy Date: Thu, 17 Sep 2026 17:14:22 +0000 Subject: [PATCH 1/3] fix default checks after migration --- .../AbstractQuestionTypeSelectable.php | 13 +++++- .../AbstractQuestionTypeSelectableTest.php | 42 +++++++++++++++++++ 2 files changed, 53 insertions(+), 2 deletions(-) diff --git a/src/Glpi/Form/QuestionType/AbstractQuestionTypeSelectable.php b/src/Glpi/Form/QuestionType/AbstractQuestionTypeSelectable.php index d03776c47aa1..f4c280efe24a 100644 --- a/src/Glpi/Form/QuestionType/AbstractQuestionTypeSelectable.php +++ b/src/Glpi/Form/QuestionType/AbstractQuestionTypeSelectable.php @@ -257,8 +257,17 @@ public function convertDefaultValue(array $rawData): array $default_values = [$rawData['default_values']]; } - // Return the indexes of the default values - return array_map(fn($value) => array_search($value, $options) + 1, $default_values); + // Skip unmatched values: `array_search` returning false would + // otherwise wrongly resolve to the first option's index (false + 1). + $indexes = []; + foreach ($default_values as $value) { + $index = array_search($value, $options); + if ($index !== false) { + $indexes[] = (int) $index + 1; + } + } + + return $indexes; } #[Override] diff --git a/tests/src/Form/QuestionType/AbstractQuestionTypeSelectableTest.php b/tests/src/Form/QuestionType/AbstractQuestionTypeSelectableTest.php index 884499189e25..af408b4dbd95 100644 --- a/tests/src/Form/QuestionType/AbstractQuestionTypeSelectableTest.php +++ b/tests/src/Form/QuestionType/AbstractQuestionTypeSelectableTest.php @@ -77,4 +77,46 @@ public function testFormatPredefinedValue(string $value, ?string $expected): voi { $this->assertSame($expected, $this->getQuestionType()->formatPredefinedValue($value)); } + + public static function convertDefaultValueProvider(): iterable + { + $values = json_encode(['Option 1', 'Option 2', 'Option 3']); + + yield 'matching default value' => [ + 'raw_data' => ['default_values' => 'Option 2', 'values' => $values], + 'expected' => [2], + ]; + + yield 'matching json array of default values' => [ + 'raw_data' => ['default_values' => json_encode(['Option 1', 'Option 3']), 'values' => $values], + 'expected' => [1, 3], + ]; + + yield 'empty default value' => [ + 'raw_data' => ['default_values' => '', 'values' => $values], + 'expected' => [], + ]; + + yield 'no default value key' => [ + 'raw_data' => ['values' => $values], + 'expected' => [], + ]; + + // Must not fall back to the first option (false + 1 == 1). + yield 'default value with no matching option' => [ + 'raw_data' => ['default_values' => 'Not an option', 'values' => $values], + 'expected' => [], + ]; + + yield 'mix of matching and non matching default values' => [ + 'raw_data' => ['default_values' => json_encode(['Option 3', 'Not an option']), 'values' => $values], + 'expected' => [3], + ]; + } + + #[DataProvider('convertDefaultValueProvider')] + public function testConvertDefaultValue(array $raw_data, array $expected): void + { + $this->assertSame($expected, $this->getQuestionType()->convertDefaultValue($raw_data)); + } } From 8072f9f41542222ee2c091b5f684f5ac79fb90fd Mon Sep 17 00:00:00 2001 From: mbressy Date: Thu, 17 Sep 2026 17:59:51 +0000 Subject: [PATCH 2/3] remove stale phpstan baseline entry --- .phpstan-baseline.php | 6 ------ 1 file changed, 6 deletions(-) diff --git a/.phpstan-baseline.php b/.phpstan-baseline.php index 95d0a43c94a1..92cb5b3eb1f8 100644 --- a/.phpstan-baseline.php +++ b/.phpstan-baseline.php @@ -8455,12 +8455,6 @@ 'count' => 1, 'path' => __DIR__ . '/src/Glpi/Form/QuestionType/AbstractQuestionTypeActors.php', ]; -$ignoreErrors[] = [ - 'message' => '#^Binary operation "\\+" between int\\|string\\|false and 1 results in an error\\.$#', - 'identifier' => 'binaryOp.invalid', - 'count' => 1, - 'path' => __DIR__ . '/src/Glpi/Form/QuestionType/AbstractQuestionTypeSelectable.php', -]; $ignoreErrors[] = [ 'message' => '#^Cannot access property \\$fields on Group\\|false\\.$#', 'identifier' => 'property.nonObject', From cb3cbd9f71b8f93811ed74abbaf25f832bef2fa7 Mon Sep 17 00:00:00 2001 From: mbressy Date: Mon, 21 Sep 2026 14:10:54 +0000 Subject: [PATCH 3/3] dropdown default value index resolution --- .../Form/QuestionType/AbstractQuestionTypeSelectable.php | 6 +++--- src/Glpi/Form/QuestionType/QuestionTypeDropdown.php | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Glpi/Form/QuestionType/AbstractQuestionTypeSelectable.php b/src/Glpi/Form/QuestionType/AbstractQuestionTypeSelectable.php index f4c280efe24a..f66f62056113 100644 --- a/src/Glpi/Form/QuestionType/AbstractQuestionTypeSelectable.php +++ b/src/Glpi/Form/QuestionType/AbstractQuestionTypeSelectable.php @@ -238,8 +238,8 @@ public function convertDefaultValue(array $rawData): array return []; } - $options = json_decode($rawData['values']); - if (empty($options)) { + $options = $this->convertExtraData($rawData)[QuestionTypeSelectableExtraDataConfig::OPTIONS] ?? []; + if (!is_array($options) || $options === []) { return []; } @@ -263,7 +263,7 @@ public function convertDefaultValue(array $rawData): array foreach ($default_values as $value) { $index = array_search($value, $options); if ($index !== false) { - $indexes[] = (int) $index + 1; + $indexes[] = (int) $index; } } diff --git a/src/Glpi/Form/QuestionType/QuestionTypeDropdown.php b/src/Glpi/Form/QuestionType/QuestionTypeDropdown.php index c64a72528324..7eb860a90250 100644 --- a/src/Glpi/Form/QuestionType/QuestionTypeDropdown.php +++ b/src/Glpi/Form/QuestionType/QuestionTypeDropdown.php @@ -77,7 +77,7 @@ public function convertExtraData(array $rawData): array $config = new QuestionTypeDropdownExtraDataConfig( options: $options, - is_multiple_dropdown: $rawData['fieldtype'] === 'multiselect' + is_multiple_dropdown: ($rawData['fieldtype'] ?? null) === 'multiselect' ); return $config->jsonSerialize(); }