diff --git a/inc/abstractitiltarget.class.php b/inc/abstractitiltarget.class.php index a2f04da4f..86681fbd4 100644 --- a/inc/abstractitiltarget.class.php +++ b/inc/abstractitiltarget.class.php @@ -2284,8 +2284,6 @@ public function getDefaultData(PluginFormcreatorFormAnswer $formanswer): array { $targetTemplateFk = $targetItemtype::getForeignKeyField(); $data = $targetItemtype::getDefaultValues(); - // Determine category early, because it is used to determine the template - $data = $this->setTargetCategory($data, $formanswer); $this->fields[$targetTemplateFk] = $this->getTargetTemplate($data); @@ -2327,6 +2325,8 @@ public function getDefaultData(PluginFormcreatorFormAnswer $formanswer): array { $data = array_merge($data, $predefined_fields); + $data = $this->setTargetCategory($data, $formanswer); + if (($data['requesttypes_id'] ?? 0) == 0) { unset($data['requesttypes_id']); } diff --git a/inc/field/dropdownfield.class.php b/inc/field/dropdownfield.class.php index e04e49efd..d93229f3c 100644 --- a/inc/field/dropdownfield.class.php +++ b/inc/field/dropdownfield.class.php @@ -720,6 +720,7 @@ public function parseObjectProperties( Plugin::loadLang(strtolower($plug['plugin']), "en_GB"); } + /** @var CommonDBTM $item */ $item = new $itemtype; $item->getFromDB($answer); diff --git a/tests/3-unit/PluginFormcreatorTargetTicket.php b/tests/3-unit/PluginFormcreatorTargetTicket.php index 352cde6a9..479fe8d79 100644 --- a/tests/3-unit/PluginFormcreatorTargetTicket.php +++ b/tests/3-unit/PluginFormcreatorTargetTicket.php @@ -955,6 +955,59 @@ public function providerSetTargetCategory_noTemplate() { ]; } + public function providerSetTargetCategory_TargetOverridesTemplate() { + // When the target ticket uses a ticket template and specifies a category + $category1 = new ITILCategory(); + $category1Id = $category1->import([ + 'name' => 'category 1', + 'entities_id' => 0, + ]); + + $category2 = new ITILCategory(); + $category2Id = $category2->import([ + 'name' => 'category 2', + 'entities_id' => 0, + ]); + + $ticketTemplate = $this->getGlpiCoreItem( + TicketTemplate::getType(), [ + 'name' => 'template with predefined category to be overriden', + ] + ); + $this->getGlpiCoreItem(TicketTemplatePredefinedField::getType(), [ + 'tickettemplates_id' => $ticketTemplate->getID(), + 'num' => 7, // ITIL category + 'value' => $category1Id + ]); + + $form = $this->getForm(); + + /** @var \PluginFormcreatorTargetTicket */ + $instance1 = $this->newTestedInstance(); + $instance1->add([ + 'name' => 'target ticket', + 'target_name' => 'target ticket', + 'plugin_formcreator_forms_id' => $form->getID(), + 'tickettemplates_id' => $ticketTemplate->getID(), + 'category_rule' => $instance1::CATEGORY_RULE_SPECIFIC, + 'category_question' => $category2Id, + ]); + + $formanswer = new PluginFormcreatorFormAnswer(); + $formanswer->add([ + 'plugin_formcreator_forms_id' => $form->getID(), + ]); + $this->boolean($formanswer->isNewItem())->isFalse(); + + return [ + [ + 'instance' => $instance1, + 'formanswer' => $formanswer, + 'expected' => $category2Id, + ], + ]; + } + /** * Test if a template with a predefined category is properly applied * @@ -1010,7 +1063,8 @@ public function providerSetTargetCategory() { return array_merge( $this->providerSetTargetCategory_nothing(), $this->providerSetTargetCategory_noTemplate(), - $this->providerSetTargetCategory_FromTemplate() + $this->providerSetTargetCategory_FromTemplate(), + $this->providerSetTargetCategory_TargetOverridesTemplate() ); } @@ -1019,8 +1073,7 @@ public function providerSetTargetCategory() { */ public function testSetTargetCategory($instance, $formanswer, $expected) { PluginFormcreatorFields::resetVisibilityCache(); - $data = $this->callPrivateMethod($instance, 'getDefaultData', $formanswer); - $output = $this->callPrivateMethod($instance, 'setTargetCategory', $data, $formanswer); + $output = $this->callPrivateMethod($instance, 'getDefaultData', $formanswer); $this->integer((int) $output['itilcategories_id'])->isEqualTo($expected); }