Skip to content

Commit

Permalink
Showing 3 changed files with 59 additions and 6 deletions.
17 changes: 11 additions & 6 deletions inc/targetticket.class.php
Original file line number Diff line number Diff line change
@@ -816,6 +816,8 @@ protected function showAssociateSettings($rand) {
}

protected function setTargetAssociatedItem($data, $formanswer) {
global $DB;

switch ($this->fields['associate_rule']) {
case self::ASSOCIATE_RULE_ANSWER:
// find the itemtype of the associated item
@@ -825,12 +827,15 @@ protected function setTargetAssociatedItem($data, $formanswer) {
$itemtype = $question->fields['values'];

// find the id of the associated item
$answer = new PluginFormcreatorAnswer();
$formAnswerId = $formanswer->fields['id'];
$found = $answer->find("`plugin_formcreator_forms_answers_id` = '$formAnswerId'
AND `plugin_formcreator_questions_id` = '$associateQuestion'");
$associate = array_shift($found);
$itemId = $associate['answer'];
$item = $DB->request([
'SELECT' => ['answer'],
'FROM' => PluginFormcreatorAnswer::getTable(),
'WHERE' => [
'plugin_formcreator_formanswers_id' => $formanswer->fields['id'],
'plugin_formcreator_questions_id' => $associateQuestion
]
])->next();
$itemId = $item['answer'];

// associate the item if it exists
if (!class_exists($itemtype)) {
4 changes: 4 additions & 0 deletions tests/src/PluginFormcreatorTargetTicketDummy.php
Original file line number Diff line number Diff line change
@@ -80,4 +80,8 @@ public function publicGetTargetItemtypeName() {
public function publicSetTargetCategory($data, $formanswer) {
return $this->setTargetCategory($data, $formanswer);
}

public function publicSetTargetAssociatedItem($data, $formanswer) {
return $this->setTargetAssociatedItem($data, $formanswer);
}
}
44 changes: 44 additions & 0 deletions tests/suite-unit/PluginFormcreatorTargetTicket.php
Original file line number Diff line number Diff line change
@@ -741,4 +741,48 @@ public function testSetTargetCategory() {
// $output = $instance->publicSetTargetCategory($data, $formAnswer);
// $this->integer((int) $output['itilcategories_id'])->isEqualTo($expected['itilcategories_id']);
}

public function testSetTargetAssociatedItem() {
$instance = new PluginFormcreatorTargetTicketDummy();
$question = $this->getQuestion([
'fieldtype' => 'glpiselect',
'values' => \Computer::class,
]);
$form = new \PluginFormcreatorForm();
$form->getByQuestionId($question->getID());

$computer = new \Computer();
$computer->add([
'name' => $this->getUniqueString(),
'entities_id' => '0',
]);
$this->boolean($computer->isNewItem())->isFalse();
$formAnswer = new \PluginFormcreatorFormAnswer;
$formAnswer->add([
\PluginFormcreatorForm::getForeignKeyField() => $form->getID(),
'name' => $form->fields['name'],
'requester_d' => 2, // glpi user id
'status' => '101',
]);
$this->boolean($formAnswer->isNewItem())->isFalse();
$answer = new \PluginFormcreatorAnswer();
$answer->add([
\PluginFormcreatorFormAnswer::getForeignKeyField() => $formAnswer->getID(),
\PluginFormcreatorQuestion::getForeignKeyField() => $question->getID(),
'answer' => $computer->getID(),
]);
$this->boolean($answer->isNewItem())->isFalse();
$instance->add([
'name' => '',
'target_name' => '',
\PluginFormcreatorForm::getForeignKeyField() => $form->getID(),
'content' => '##FULLFORM',
'associate_rule' => \PluginFormcreatorTargetTicket::ASSOCIATE_RULE_ANSWER,
'associate_question' => $question->getID(),
]);
$this->boolean($instance->isNewItem())->isFalse();
$output = $instance->publicSetTargetAssociatedItem([], $formAnswer);
$this->array($output['items_id']['Computer'])->hasSize(1);
$this->integer((int) $output['items_id']['Computer'][$computer->getID()])->isEqualTo($computer->getID());
}
}

0 comments on commit 001976f

Please sign in to comment.