Skip to content

Commit 47da0ea

Browse files
committed
fix(checkboxesfield, radiosfield): checkboxes and radios backslashes (#3050)
1 parent 0a847af commit 47da0ea

File tree

4 files changed

+96
-32
lines changed

4 files changed

+96
-32
lines changed

inc/field/checkboxesfield.class.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,6 @@ public function isValidValue($value): bool {
187187
return true;
188188
}
189189

190-
$value = Toolbox::stripslashes_deep($value);
191190
foreach ($value as $item) {
192191
if (trim($item) == '') {
193192
return false;

inc/field/radiosfield.class.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,6 @@ public function isValidValue($value): bool {
205205
if ($value == '') {
206206
return true;
207207
}
208-
$value = Toolbox::stripslashes_deep($value);
209208
$value = trim($value);
210209
return in_array($value, $this->getAvailableValues());
211210
}

tests/3-unit/GlpiPlugin/Formcreator/Field/CheckboxesField.php

Lines changed: 47 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -95,37 +95,54 @@ public function providerIsValidValue() {
9595
]
9696
],
9797
]));
98-
return [
99-
[
100-
'instance' => $instance,
101-
'value' => '',
102-
'expected' => true,
103-
],
104-
[
105-
'instance' => $instance,
106-
'value' => [],
107-
'expected' => true,
108-
],
109-
[
110-
'instance' => $instance,
111-
'value' => ['1'],
112-
'expected' => true,
113-
],
114-
[
115-
'instance' => $instance,
116-
'value' => ['1', '4'],
117-
'expected' => true,
118-
],
119-
[
120-
'instance' => $instance,
121-
'value' => ['1', '9'],
122-
'expected' => false,
123-
],
124-
[
125-
'instance' => $instance,
126-
'value' => ['9'],
127-
'expected' => false,
98+
yield [
99+
'instance' => $instance,
100+
'value' => '',
101+
'expected' => true,
102+
];
103+
yield [
104+
'instance' => $instance,
105+
'value' => [],
106+
'expected' => true,
107+
];
108+
yield [
109+
'instance' => $instance,
110+
'value' => ['1'],
111+
'expected' => true,
112+
];
113+
yield [
114+
'instance' => $instance,
115+
'value' => ['1', '4'],
116+
'expected' => true,
117+
];
118+
yield [
119+
'instance' => $instance,
120+
'value' => ['1', '9'],
121+
'expected' => false,
122+
];
123+
yield [
124+
'instance' => $instance,
125+
'value' => ['9'],
126+
'expected' => false,
127+
];
128+
129+
// values are escaped by GLPI, then backslashes are doubled
130+
$instance = $this->newTestedInstance($this->getQuestion([
131+
'fieldtype' => 'checkboxes',
132+
'values' => implode('\r\n', ['X:\\\\path\\\\to\\\\file', 'nothing']),
133+
'_parameters' => [
134+
'checkboxes' => [
135+
'range' => [
136+
'range_min' => '',
137+
'range_max' => '',
138+
]
139+
]
128140
],
141+
]));
142+
yield [
143+
'instance' => $instance,
144+
'value' => ['X:\\path\\to\\file'],
145+
'expected' => true,
129146
];
130147
}
131148

tests/3-unit/GlpiPlugin/Formcreator/Field/RadiosField.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,55 @@ public function testCanRequire() {
100100
$this->boolean($output)->isTrue();
101101
}
102102

103+
public function providerIsValidValue() {
104+
$instance = $this->newTestedInstance($this->getQuestion([
105+
'fieldtype' => 'radios',
106+
'values' => implode('\r\n', ['1', '2', '3', '4']),
107+
]));
108+
yield [
109+
'instance' => $instance,
110+
'value' => '',
111+
'expected' => true,
112+
];
113+
yield [
114+
'instance' => $instance,
115+
'value' => '1',
116+
'expected' => true,
117+
];
118+
yield [
119+
'instance' => $instance,
120+
'value' => '9',
121+
'expected' => false,
122+
];
123+
124+
// values are escaped by GLPI, then backslashes are doubled
125+
$instance = $this->newTestedInstance($this->getQuestion([
126+
'fieldtype' => 'radios',
127+
'values' => implode('\r\n', ['X:\\\\path\\\\to\\\\file', 'nothing']),
128+
'_parameters' => [
129+
'checkboxes' => [
130+
'range' => [
131+
'range_min' => '',
132+
'range_max' => '',
133+
]
134+
]
135+
],
136+
]));
137+
yield [
138+
'instance' => $instance,
139+
'value' => 'X:\\path\\to\\file',
140+
'expected' => true,
141+
];
142+
}
143+
144+
/**
145+
* @dataProvider providerIsValidValue
146+
*/
147+
public function testIsValidValue($instance, $value, $expected) {
148+
$output = $instance->isValidValue($value);
149+
$this->boolean($output)->isEqualTo($expected);
150+
}
151+
103152
public function providerSerializeValue() {
104153
$question = $this->getQuestion([
105154
'fieldtype' => 'radios',

0 commit comments

Comments
 (0)