Skip to content

Commit

Permalink
fix(selectfield): too many unescaping
Browse files Browse the repository at this point in the history
  • Loading branch information
btry committed Oct 24, 2022
1 parent f357d9c commit 706b70f
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
1 change: 0 additions & 1 deletion inc/field/selectfield.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,6 @@ public function isValidValue($value): bool {
if ($value == '0') {
return true;
}
$value = Toolbox::stripslashes_deep($value);
$value = trim($value);
return in_array($value, $this->getAvailableValues());
}
Expand Down
49 changes: 49 additions & 0 deletions tests/3-unit/GlpiPlugin/Formcreator/Field/SelectField.php
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,55 @@ public function testGetEmptyParameters() {
->isIdenticalTo([]);
}

public function providerIsValidValue() {
$instance = $this->newTestedInstance($this->getQuestion([
'fieldtype' => 'select',
'values' => implode('\r\n', ['1', '2', '3', '4']),
]));
yield [
'instance' => $instance,
'value' => '',
'expected' => false,
];
yield [
'instance' => $instance,
'value' => '1',
'expected' => true,
];
yield [
'instance' => $instance,
'value' => '9',
'expected' => false,
];

// values are escaped by GLPI, then backslashes are doubled
$instance = $this->newTestedInstance($this->getQuestion([
'fieldtype' => 'select',
'values' => implode('\r\n', ['X:\\\\path\\\\to\\\\file', 'nothing']),
'_parameters' => [
'checkboxes' => [
'range' => [
'range_min' => '',
'range_max' => '',
]
]
],
]));
yield [
'instance' => $instance,
'value' => 'X:\\path\\to\\file',
'expected' => true,
];
}

/**
* @dataProvider providerIsValidValue
*/
public function testIsValidValue($instance, $value, $expected) {
$output = $instance->isValidValue($value);
$this->boolean($output)->isEqualTo($expected);
}

public function providerSerializeValue() {
return [
[
Expand Down

0 comments on commit 706b70f

Please sign in to comment.