Skip to content

Commit 001976f

Browse files
committed
fix(targetticket): associate item to ticket
1 parent c2e360e commit 001976f

File tree

3 files changed

+59
-6
lines changed

3 files changed

+59
-6
lines changed

inc/targetticket.class.php

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -816,6 +816,8 @@ protected function showAssociateSettings($rand) {
816816
}
817817

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

827829
// find the id of the associated item
828-
$answer = new PluginFormcreatorAnswer();
829-
$formAnswerId = $formanswer->fields['id'];
830-
$found = $answer->find("`plugin_formcreator_forms_answers_id` = '$formAnswerId'
831-
AND `plugin_formcreator_questions_id` = '$associateQuestion'");
832-
$associate = array_shift($found);
833-
$itemId = $associate['answer'];
830+
$item = $DB->request([
831+
'SELECT' => ['answer'],
832+
'FROM' => PluginFormcreatorAnswer::getTable(),
833+
'WHERE' => [
834+
'plugin_formcreator_formanswers_id' => $formanswer->fields['id'],
835+
'plugin_formcreator_questions_id' => $associateQuestion
836+
]
837+
])->next();
838+
$itemId = $item['answer'];
834839

835840
// associate the item if it exists
836841
if (!class_exists($itemtype)) {

tests/src/PluginFormcreatorTargetTicketDummy.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,4 +80,8 @@ public function publicGetTargetItemtypeName() {
8080
public function publicSetTargetCategory($data, $formanswer) {
8181
return $this->setTargetCategory($data, $formanswer);
8282
}
83+
84+
public function publicSetTargetAssociatedItem($data, $formanswer) {
85+
return $this->setTargetAssociatedItem($data, $formanswer);
86+
}
8387
}

tests/suite-unit/PluginFormcreatorTargetTicket.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -741,4 +741,48 @@ public function testSetTargetCategory() {
741741
// $output = $instance->publicSetTargetCategory($data, $formAnswer);
742742
// $this->integer((int) $output['itilcategories_id'])->isEqualTo($expected['itilcategories_id']);
743743
}
744+
745+
public function testSetTargetAssociatedItem() {
746+
$instance = new PluginFormcreatorTargetTicketDummy();
747+
$question = $this->getQuestion([
748+
'fieldtype' => 'glpiselect',
749+
'values' => \Computer::class,
750+
]);
751+
$form = new \PluginFormcreatorForm();
752+
$form->getByQuestionId($question->getID());
753+
754+
$computer = new \Computer();
755+
$computer->add([
756+
'name' => $this->getUniqueString(),
757+
'entities_id' => '0',
758+
]);
759+
$this->boolean($computer->isNewItem())->isFalse();
760+
$formAnswer = new \PluginFormcreatorFormAnswer;
761+
$formAnswer->add([
762+
\PluginFormcreatorForm::getForeignKeyField() => $form->getID(),
763+
'name' => $form->fields['name'],
764+
'requester_d' => 2, // glpi user id
765+
'status' => '101',
766+
]);
767+
$this->boolean($formAnswer->isNewItem())->isFalse();
768+
$answer = new \PluginFormcreatorAnswer();
769+
$answer->add([
770+
\PluginFormcreatorFormAnswer::getForeignKeyField() => $formAnswer->getID(),
771+
\PluginFormcreatorQuestion::getForeignKeyField() => $question->getID(),
772+
'answer' => $computer->getID(),
773+
]);
774+
$this->boolean($answer->isNewItem())->isFalse();
775+
$instance->add([
776+
'name' => '',
777+
'target_name' => '',
778+
\PluginFormcreatorForm::getForeignKeyField() => $form->getID(),
779+
'content' => '##FULLFORM',
780+
'associate_rule' => \PluginFormcreatorTargetTicket::ASSOCIATE_RULE_ANSWER,
781+
'associate_question' => $question->getID(),
782+
]);
783+
$this->boolean($instance->isNewItem())->isFalse();
784+
$output = $instance->publicSetTargetAssociatedItem([], $formAnswer);
785+
$this->array($output['items_id']['Computer'])->hasSize(1);
786+
$this->integer((int) $output['items_id']['Computer'][$computer->getID()])->isEqualTo($computer->getID());
787+
}
744788
}

0 commit comments

Comments
 (0)