Skip to content
Open
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
3 changes: 2 additions & 1 deletion inc/field/checkboxesfield.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,10 @@ public function serializeValue(PluginFormcreatorFormAnswer $formanswer): string
}

public function deserializeValue($value) {
$this->value = ($value !== null && $value !== '')
$decoded = ($value !== null && $value !== '')
? json_decode($value)
: [];
$this->value = is_array($decoded) ? $decoded : [];
}

public function getValueForDesign(): string {
Expand Down
2 changes: 1 addition & 1 deletion inc/field/ldapselectfield.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ public function lessThan($value): bool {
}

public function regex($value): bool {
return (preg_grep($value, $this->value)) ? true : false;
return preg_match($value, $this->value) ? true : false;
}

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

public function providerRegex() {
yield 'valid json value matches regex' => [
'value' => '["foo","bar"]',
'pattern' => '/foo/',
'expected' => true,
];
yield 'valid json value does not match regex' => [
'value' => '["foo","bar"]',
'pattern' => '/baz/',
'expected' => false,
];
yield 'corrupted json value does not throw and returns false' => [
'value' => 'not_valid_json',
'pattern' => '/foo/',
'expected' => false,
];
yield 'null-decoded json value does not throw and returns false' => [
'value' => 'null',
'pattern' => '/foo/',
'expected' => false,
];
}

/**
* @dataProvider providerRegex
*/
public function testRegex($value, $pattern, $expected) {
$instance = $this->newTestedInstance($this->getQuestion([
'fieldtype' => 'checkboxes',
'values' => implode('\r\n', ['foo', 'bar']),
'_parameters' => [
'checkboxes' => [
'range' => ['range_min' => '', 'range_max' => ''],
],
],
]));
$instance->deserializeValue($value);
$output = $instance->regex($pattern);
$this->boolean($output)->isEqualTo($expected);
}

public function providerGetValueForApi() {
return [
[
Expand Down
Loading