Skip to content

Commit

Permalink
fix(actors): rendering for answers view, and use JSON in DB
Browse files Browse the repository at this point in the history
in DB JSON is used in 2.6.4, then this version shall use it too

Signed-off-by: btry <tbugier@teclib.com>
  • Loading branch information
btry committed Oct 8, 2018
1 parent 5b3de79 commit 3f0c9b7
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 20 deletions.
19 changes: 16 additions & 3 deletions inc/fields/actorfield.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,20 @@ public function displayField($canEdit = true) {
pluginFormcreatorInitializeActor('$fieldName', '$rand', '$initialValue');
});");
} else {
echo empty($this->value) ? '' : implode('<br />', $this->value);
if (empty($this->value)) {
echo '';
} else {
foreach ($this->value as $item) {
if (filter_var($item, FILTER_VALIDATE_EMAIL) !== false) {
$value[] = $item;
} else {
$user = new User();
$user->getFromDB($item);
$value[] = $user->getRawName();
}
}
echo implode('<br>', $value);
}
}
}

Expand All @@ -73,13 +86,13 @@ public function serializeValue() {
return '';
}

return implode(',', $this->value);
return json_encode($this->value);
}

public function deserializeValue($value) {
$deserialized = [];
$serialized = ($value !== null && $value !== '')
? explode(',', $value)
? json_decode($value, JSON_OBJECT_AS_ARRAY)
: [];
foreach ($serialized as $item) {
$item = trim($item);
Expand Down
34 changes: 17 additions & 17 deletions tests/suite-unit/PluginFormcreatorActorField.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public function providerGetValue() {
'fieldtype' => 'actor',
'name' => 'question',
'required' => '0',
'default_values' => '',
'default_values' => json_encode([]),
'values' => '',
'order' => '1',
'show_rule' => 'always'
Expand All @@ -63,7 +63,7 @@ public function providerGetValue() {
'fieldtype' => 'actor',
'name' => 'question',
'required' => '0',
'default_values' => '',
'default_values' => json_encode([]),
'values' => 'glpi',
'order' => '1',
'show_rule' => 'always'
Expand All @@ -77,7 +77,7 @@ public function providerGetValue() {
'fieldtype' => 'actor',
'name' => 'question',
'required' => '0',
'default_values' => 'email@something.com',
'default_values' => json_encode(['email@something.com']),
'values' => '',
'order' => '1',
'show_rule' => 'always'
Expand All @@ -91,7 +91,7 @@ public function providerGetValue() {
'fieldtype' => 'actor',
'name' => 'question',
'required' => '0',
'default_values' => $userId . ',email@something.com',
'default_values' => json_encode([$userId, 'email@something.com']),
'values' => '',
'order' => '1',
'show_rule' => 'always'
Expand Down Expand Up @@ -128,23 +128,23 @@ public function providerSerializeValue() {
],
[
'value' => [],
'expected' => '',
'expected' => json_encode([]),
],
[
'value' => ['2'],
'expected' => '2',
'expected' => json_encode(['2']),
],
[
'value' => ['2', '5'],
'expected' => '2,5',
'expected' => json_encode(['2','5']),
],
[
'value' => ['2', '5', 'user@localhost.local'],
'expected' => '2,5,user@localhost.local',
'expected' => json_encode(['2','5','user@localhost.local']),
],
[
'value' => ['user@localhost.local'],
'expected' => 'user@localhost.local',
'expected' => json_encode(['user@localhost.local']),
],
];
}
Expand Down Expand Up @@ -175,19 +175,19 @@ public function providerDeserializeValue() {
'expected' => [],
],
[
'value' => "$glpiId",
'value' => json_encode(["$glpiId"]),
'expected' => [$glpiId],
],
[
'value' => "$glpiId,$normalId",
'value' => json_encode(["$glpiId","$normalId"]),
'expected' => [$glpiId, $normalId],
],
[
'value' => "$glpiId,$normalId,user@localhost.local",
'value' => json_encode(["$glpiId","$normalId","user@localhost.local"]),
'expected' => [$glpiId, $normalId, 'user@localhost.local'],
],
[
'value' => "user@localhost.local",
'value' => json_encode(["user@localhost.local"]),
'expected' => ['user@localhost.local'],
],
];
Expand Down Expand Up @@ -217,19 +217,19 @@ public function providerGetValueForDesign() {
'expected' => '',
],
[
'value' => "$glpiId",
'value' => json_encode(["$glpiId"]),
'expected' => "glpi",
],
[
'value' => "$glpiId,$normalId",
'value' => json_encode(["$glpiId", "$normalId"]),
'expected' => "glpi\r\nnormal",
],
[
'value' => "$glpiId, $normalId,user@localhost.local",
'value' => json_encode(["$glpiId", "$normalId", "user@localhost.local"]),
'expected' => "glpi\r\nnormal\r\nuser@localhost.local",
],
[
'value' => "user@localhost.local",
'value' => json_encode(["user@localhost.local"]),
'expected' => "user@localhost.local",
],
];
Expand Down

0 comments on commit 3f0c9b7

Please sign in to comment.