Skip to content

Commit

Permalink
fix(targetticket,targetchange): missing import of template settings
Browse files Browse the repository at this point in the history
Signed-off-by: Thierry Bugier <tbugier@teclib.com>
  • Loading branch information
btry committed Jul 19, 2021
1 parent f24c6ca commit 791b1a2
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 1 deletion.
2 changes: 1 addition & 1 deletion inc/abstracttarget.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ abstract protected function getItem_Item();
*
* @return string
*/
abstract protected function getTemplateItemtypeName(): string;
abstract protected function getTemplateItemtypeName(): string;

/**
* Get the class name of the target itemtype's template predefined field class
Expand Down
12 changes: 12 additions & 0 deletions inc/targetchange.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,18 @@ public static function import(PluginFormcreatorLinker $linker, array $input = []
);
}

// set template
$changeTemplateId = 0;
plugin_formcreator_getFromDBByField(
$changeTemplate = new ChangeTemplate(),
'name',
$input['_changetemplate']
);
if (!$changeTemplate->isNewItem() && $changeTemplate->canViewItem()) {
$changeTemplateId = $changeTemplate->getID();
}
$input['changetemplates_id'] = $changeTemplateId;

// Escape text fields
foreach (['name'] as $key) {
$input[$key] = $DB->escape($input[$key]);
Expand Down
12 changes: 12 additions & 0 deletions inc/targetticket.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -1111,6 +1111,18 @@ public static function import(PluginFormcreatorLinker $linker, array $input = []
);
}

// set template
$ticketTemplateId = 0;
plugin_formcreator_getFromDBByField(
$ticketTemplate = new TicketTemplate(),
'name',
$input['_tickettemplate']
);
if (!$ticketTemplate->isNewItem() && $ticketTemplate->canViewItem()) {
$ticketTemplateId = $ticketTemplate->getID();
}
$input['tickettemplates_id'] = $ticketTemplateId;

// Escape text fields
foreach (['name'] as $key) {
$input[$key] = $DB->escape($input[$key]);
Expand Down
25 changes: 25 additions & 0 deletions tests/3-unit/PluginFormcreatorTargetChange.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public function beforeTestMethod($method) {
parent::beforeTestMethod($method);
switch ($method) {
case 'testSetTargetEntity':
case 'testImport':
$this->boolean($this->login('glpi', 'glpi'))->isTrue();
break;
}
Expand Down Expand Up @@ -477,6 +478,7 @@ public function testImport() {
$input = [
'name' => $this->getUniqueString(),
'target_name' => $this->getUniqueString(),
'_changetemplate' => '',
'content' => $this->getUniqueString(),
'impactcontent' => $this->getUniqueString(),
'controlistcontent' => $this->getUniqueString(),
Expand Down Expand Up @@ -516,6 +518,29 @@ function() use($linker, $input, $form) {
$input['id'] = $targetChangeId;
$targetChangeId2 = \PluginFormcreatorTargetChange::import($linker, $input, $form->getID());
$this->integer((int) $targetChangeId)->isNotEqualTo($targetChangeId2);

$this->newTestedInstance()->delete([
'id' => $targetChangeId2,
]);

// Check successful link with template
$templateName = 'change template ' . $this->getUniqueString();
$changeTemplate = new \ChangeTemplate();
$changeTemplate->add([
'name' => $templateName,
'entities_id' => 0,
'is_recursive' => 1,
]);
$this->boolean($changeTemplate->isNewItem())->isFalse();
$input['_changetemplate'] = $templateName;

$linker = new \PluginFormcreatorLinker();
$targetChangeId3 = \PluginFormcreatorTargetChange::import($linker, $input, $form->getID());
$this->integer((int) $targetChangeId)->isNotEqualTo($targetChangeId3);
$targetChange = $this->newTestedInstance();
$targetChange->getFromDB($targetChangeId3);
$this->integer((int) $targetChange->fields['changetemplates_id'])
->isEqualTo($changeTemplate->getID());
}

public function testIsEntityAssign() {
Expand Down
28 changes: 28 additions & 0 deletions tests/3-unit/PluginFormcreatorTargetTicket.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public function beforeTestMethod($method) {
case 'testPrepareTemplate':
case 'testDeleteLinkedTickets':
case 'testSetTargetAssociatedItem':
case 'testImport':
$this->boolean($this->login('glpi', 'glpi'))->isTrue();
break;
}
Expand Down Expand Up @@ -698,6 +699,7 @@ public function testImport() {
$input = [
'name' => $this->getUniqueString(),
'target_name' => $this->getUniqueString(),
'_tickettemplate' => '',
'content' => $this->getUniqueString(),
'due_date_rule' => \PluginFormcreatorTargetTicket::DUE_DATE_RULE_NONE,
'due_date_question' => '0',
Expand All @@ -722,22 +724,48 @@ public function testImport() {
'uuid' => $uuid,
];

// Check successful import with UUID
$linker = new \PluginFormcreatorLinker();
$targetTicketId = \PluginFormcreatorTargetTicket::import($linker, $input, $form->getID());
$this->integer($targetTicketId)->isGreaterThan(0);

unset($input['uuid']);

// Check error if UUID and ID are mising
$this->exception(
function() use($linker, $input, $form) {
\PluginFormcreatorTargetTicket::import($linker, $input, $form->getID());
}
)->isInstanceOf(\GlpiPlugin\Formcreator\Exception\ImportFailureException::class)
->hasMessage('UUID or ID is mandatory for Target ticket'); // passes

// Check sucessful import with ID
$input['id'] = $targetTicketId;
$targetTicketId2 = \PluginFormcreatorTargetTicket::import($linker, $input, $form->getID());
$this->integer((int) $targetTicketId)->isNotEqualTo($targetTicketId2);

$this->newTestedInstance()->delete([
'id' => $targetTicketId2,
]);

// Check successful link with template
$templateName = 'ticket template ' . $this->getUniqueString();
$ticketTemplate = new \TicketTemplate();
$ticketTemplate->add([
'name' => $templateName,
'entities_id' => 0,
'is_recursive' => 1,
]);
$this->boolean($ticketTemplate->isNewItem())->isFalse();
$input['_tickettemplate'] = $templateName;

$linker = new \PluginFormcreatorLinker();
$targetTicketId3 = \PluginFormcreatorTargetTicket::import($linker, $input, $form->getID());
$this->integer((int) $targetTicketId)->isNotEqualTo($targetTicketId3);
$targetTicket = $this->newTestedInstance();
$targetTicket->getFromDB($targetTicketId3);
$this->integer((int) $targetTicket->fields['tickettemplates_id'])
->isEqualTo($ticketTemplate->getID());
}

public function providerSetTargetCategory_nothing() {
Expand Down

0 comments on commit 791b1a2

Please sign in to comment.