Skip to content
Merged
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
6 changes: 0 additions & 6 deletions .phpstan-baseline.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
17 changes: 13 additions & 4 deletions src/Glpi/Form/QuestionType/AbstractQuestionTypeSelectable.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 [];
}

Expand All @@ -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;
}
}

return $indexes;
}

#[Override]
Expand Down
2 changes: 1 addition & 1 deletion src/Glpi/Form/QuestionType/QuestionTypeDropdown.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
42 changes: 42 additions & 0 deletions tests/src/Form/QuestionType/AbstractQuestionTypeSelectableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}
Loading