Skip to content

Commit

Permalink
fix(glpiselect): show user firstname and lastname
Browse files Browse the repository at this point in the history
Signed-off-by: Thierry Bugier <tbugier@teclib.com>
  • Loading branch information
btry committed Mar 13, 2019
1 parent 63c8670 commit 575dbf8
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 6 deletions.
12 changes: 8 additions & 4 deletions inc/fields/dropdownfield.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -206,15 +206,19 @@ public function getValueForDesign() {
public function getValueForTargetText($richText) {
$DbUtil = new DbUtils();
$decodedValues = json_decode($this->fields['values'], JSON_OBJECT_AS_ARRAY);
if (!isset($decodedValues['itemtype'])) {
$value = Dropdown::getDropdownName($DbUtil->getTableForItemType($this->fields['values']), $this->value);
$itemtype = $this->fields['values'];
if (isset($decodedValues['itemtype'])) {
$itemtype = $decodedValues['itemtype'];
}
if ($itemtype == User::class) {
$value = (new DBUtils())->getUserName($this->value);
} else {
$value = Dropdown::getDropdownName($DbUtil->getTableForItemType($decodedValues['itemtype']), $this->value);
$value = Dropdown::getDropdownName($DbUtil->getTableForItemType($itemtype), $this->value);
}

return Toolbox::addslashes_deep($value);
}


public function getDocumentsForTarget() {
return [];
}
Expand Down
45 changes: 43 additions & 2 deletions tests/suite-unit/PluginFormcreatorGlpiselectField.php
Original file line number Diff line number Diff line change
Expand Up @@ -249,12 +249,12 @@ public function providerGetAnswer() {
return $dataset;
}

public function ptroviderIsValid() {
public function providerIsValid() {
return $this->providerGetAnswer();
}

/**
* @dataProvider ptroviderIsValid
* @dataProvider providerIsValid
*/
public function testIsValid($fields, $data, $expectedValue, $expectedValidity) {
$instance = $this->newTestedInstance($fields, $data);
Expand All @@ -275,4 +275,45 @@ public function testIsPrerequisites() {
$output = $instance->isPrerequisites();
$this->boolean($output)->isEqualTo(true);
}

public function testGetValueForTargetText() {
$computer = new \Computer();
$computer->add([
'name' => 'computer foo',
'entities_id' => 0,
]);

// Create a question glpi Object / computer
$question = $this->getQuestion([
'fieldtype' => 'glpiselect',
'values' => \Computer::class,
]);
$instance = $this->newTestedInstance($question->fields);
$instance->deserializeValue($computer->getID());

// test for the target text
$output = $instance->getValueForTargetText(true);
$this->string($output)->isEqualTo('computer foo');

// Create a user with first and last name
$user = new \User();
$user->add([
'name' => 'foobar' . $this->getUniqueString(),
'firstname' => 'foo',
'realname' => 'bar',
]);
$this->boolean($user->isNewItem())->isFalse();

// Create a question glpi Object / User
$question = $this->getQuestion([
'fieldtype' => 'glpiselect',
'values' => \User::class,
]);
$instance = $this->newTestedInstance($question->fields);
$instance->deserializeValue($user->getID());

// test the text for target
$output = $instance->getValueForTargetText(true);
$this->string($output)->isEqualTo('bar foo');
}
}

0 comments on commit 575dbf8

Please sign in to comment.