Skip to content

Commit

Permalink
fix(abstractitiltarget): itil category from template shall not take p…
Browse files Browse the repository at this point in the history
…recedence
  • Loading branch information
btry committed Oct 16, 2023
1 parent 599451a commit 8f8d9a7
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 5 deletions.
4 changes: 2 additions & 2 deletions inc/abstractitiltarget.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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']);
}
Expand Down
1 change: 1 addition & 0 deletions inc/field/dropdownfield.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -720,6 +720,7 @@ public function parseObjectProperties(
Plugin::loadLang(strtolower($plug['plugin']), "en_GB");
}

/** @var CommonDBTM $item */
$item = new $itemtype;
$item->getFromDB($answer);

Expand Down
59 changes: 56 additions & 3 deletions tests/3-unit/PluginFormcreatorTargetTicket.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down Expand Up @@ -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()
);
}

Expand All @@ -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);
}
Expand Down

0 comments on commit 8f8d9a7

Please sign in to comment.