Skip to content

Commit 8f8d9a7

Browse files
committed
fix(abstractitiltarget): itil category from template shall not take precedence
1 parent 599451a commit 8f8d9a7

File tree

3 files changed

+59
-5
lines changed

3 files changed

+59
-5
lines changed

inc/abstractitiltarget.class.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2284,8 +2284,6 @@ public function getDefaultData(PluginFormcreatorFormAnswer $formanswer): array {
22842284
$targetTemplateFk = $targetItemtype::getForeignKeyField();
22852285

22862286
$data = $targetItemtype::getDefaultValues();
2287-
// Determine category early, because it is used to determine the template
2288-
$data = $this->setTargetCategory($data, $formanswer);
22892287

22902288
$this->fields[$targetTemplateFk] = $this->getTargetTemplate($data);
22912289

@@ -2327,6 +2325,8 @@ public function getDefaultData(PluginFormcreatorFormAnswer $formanswer): array {
23272325

23282326
$data = array_merge($data, $predefined_fields);
23292327

2328+
$data = $this->setTargetCategory($data, $formanswer);
2329+
23302330
if (($data['requesttypes_id'] ?? 0) == 0) {
23312331
unset($data['requesttypes_id']);
23322332
}

inc/field/dropdownfield.class.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -720,6 +720,7 @@ public function parseObjectProperties(
720720
Plugin::loadLang(strtolower($plug['plugin']), "en_GB");
721721
}
722722

723+
/** @var CommonDBTM $item */
723724
$item = new $itemtype;
724725
$item->getFromDB($answer);
725726

tests/3-unit/PluginFormcreatorTargetTicket.php

Lines changed: 56 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -955,6 +955,59 @@ public function providerSetTargetCategory_noTemplate() {
955955
];
956956
}
957957

958+
public function providerSetTargetCategory_TargetOverridesTemplate() {
959+
// When the target ticket uses a ticket template and specifies a category
960+
$category1 = new ITILCategory();
961+
$category1Id = $category1->import([
962+
'name' => 'category 1',
963+
'entities_id' => 0,
964+
]);
965+
966+
$category2 = new ITILCategory();
967+
$category2Id = $category2->import([
968+
'name' => 'category 2',
969+
'entities_id' => 0,
970+
]);
971+
972+
$ticketTemplate = $this->getGlpiCoreItem(
973+
TicketTemplate::getType(), [
974+
'name' => 'template with predefined category to be overriden',
975+
]
976+
);
977+
$this->getGlpiCoreItem(TicketTemplatePredefinedField::getType(), [
978+
'tickettemplates_id' => $ticketTemplate->getID(),
979+
'num' => 7, // ITIL category
980+
'value' => $category1Id
981+
]);
982+
983+
$form = $this->getForm();
984+
985+
/** @var \PluginFormcreatorTargetTicket */
986+
$instance1 = $this->newTestedInstance();
987+
$instance1->add([
988+
'name' => 'target ticket',
989+
'target_name' => 'target ticket',
990+
'plugin_formcreator_forms_id' => $form->getID(),
991+
'tickettemplates_id' => $ticketTemplate->getID(),
992+
'category_rule' => $instance1::CATEGORY_RULE_SPECIFIC,
993+
'category_question' => $category2Id,
994+
]);
995+
996+
$formanswer = new PluginFormcreatorFormAnswer();
997+
$formanswer->add([
998+
'plugin_formcreator_forms_id' => $form->getID(),
999+
]);
1000+
$this->boolean($formanswer->isNewItem())->isFalse();
1001+
1002+
return [
1003+
[
1004+
'instance' => $instance1,
1005+
'formanswer' => $formanswer,
1006+
'expected' => $category2Id,
1007+
],
1008+
];
1009+
}
1010+
9581011
/**
9591012
* Test if a template with a predefined category is properly applied
9601013
*
@@ -1010,7 +1063,8 @@ public function providerSetTargetCategory() {
10101063
return array_merge(
10111064
$this->providerSetTargetCategory_nothing(),
10121065
$this->providerSetTargetCategory_noTemplate(),
1013-
$this->providerSetTargetCategory_FromTemplate()
1066+
$this->providerSetTargetCategory_FromTemplate(),
1067+
$this->providerSetTargetCategory_TargetOverridesTemplate()
10141068
);
10151069
}
10161070

@@ -1019,8 +1073,7 @@ public function providerSetTargetCategory() {
10191073
*/
10201074
public function testSetTargetCategory($instance, $formanswer, $expected) {
10211075
PluginFormcreatorFields::resetVisibilityCache();
1022-
$data = $this->callPrivateMethod($instance, 'getDefaultData', $formanswer);
1023-
$output = $this->callPrivateMethod($instance, 'setTargetCategory', $data, $formanswer);
1076+
$output = $this->callPrivateMethod($instance, 'getDefaultData', $formanswer);
10241077

10251078
$this->integer((int) $output['itilcategories_id'])->isEqualTo($expected);
10261079
}

0 commit comments

Comments
 (0)