Skip to content

Commit

Permalink
fix(checkboxesfield, radiosfield): checkboxes and radios backslashes (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
btry committed Oct 24, 2022
1 parent 0a847af commit 47da0ea
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 32 deletions.
1 change: 0 additions & 1 deletion inc/field/checkboxesfield.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,6 @@ public function isValidValue($value): bool {
return true;
}

$value = Toolbox::stripslashes_deep($value);
foreach ($value as $item) {
if (trim($item) == '') {
return false;
Expand Down
1 change: 0 additions & 1 deletion inc/field/radiosfield.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,6 @@ public function isValidValue($value): bool {
if ($value == '') {
return true;
}
$value = Toolbox::stripslashes_deep($value);
$value = trim($value);
return in_array($value, $this->getAvailableValues());
}
Expand Down
77 changes: 47 additions & 30 deletions tests/3-unit/GlpiPlugin/Formcreator/Field/CheckboxesField.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,37 +95,54 @@ public function providerIsValidValue() {
]
],
]));
return [
[
'instance' => $instance,
'value' => '',
'expected' => true,
],
[
'instance' => $instance,
'value' => [],
'expected' => true,
],
[
'instance' => $instance,
'value' => ['1'],
'expected' => true,
],
[
'instance' => $instance,
'value' => ['1', '4'],
'expected' => true,
],
[
'instance' => $instance,
'value' => ['1', '9'],
'expected' => false,
],
[
'instance' => $instance,
'value' => ['9'],
'expected' => false,
yield [
'instance' => $instance,
'value' => '',
'expected' => true,
];
yield [
'instance' => $instance,
'value' => [],
'expected' => true,
];
yield [
'instance' => $instance,
'value' => ['1'],
'expected' => true,
];
yield [
'instance' => $instance,
'value' => ['1', '4'],
'expected' => true,
];
yield [
'instance' => $instance,
'value' => ['1', '9'],
'expected' => false,
];
yield [
'instance' => $instance,
'value' => ['9'],
'expected' => false,
];

// values are escaped by GLPI, then backslashes are doubled
$instance = $this->newTestedInstance($this->getQuestion([
'fieldtype' => 'checkboxes',
'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,
];
}

Expand Down
49 changes: 49 additions & 0 deletions tests/3-unit/GlpiPlugin/Formcreator/Field/RadiosField.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,55 @@ public function testCanRequire() {
$this->boolean($output)->isTrue();
}

public function providerIsValidValue() {
$instance = $this->newTestedInstance($this->getQuestion([
'fieldtype' => 'radios',
'values' => implode('\r\n', ['1', '2', '3', '4']),
]));
yield [
'instance' => $instance,
'value' => '',
'expected' => true,
];
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' => 'radios',
'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() {
$question = $this->getQuestion([
'fieldtype' => 'radios',
Expand Down

0 comments on commit 47da0ea

Please sign in to comment.