Skip to content

Commit

Permalink
fix(issue): update modificaitoin date when a followup is added to ticket
Browse files Browse the repository at this point in the history
Signed-off-by: Thierry Bugier <[email protected]>
  • Loading branch information
btry committed Mar 19, 2021
1 parent 3d28c6b commit 0d6597a
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 1 deletion.
31 changes: 31 additions & 0 deletions hook.php
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,37 @@ function plugin_formcreator_hook_update_ticketvalidation(CommonDBTM $item) {
$issue->update(['status' => $status['status']] + $issue->fields);
}

function plugin_formcreator_hook_update_itilFollowup($followup) {
$itemtype = $followup->fields['itemtype'];
if ($itemtype != Ticket::getType()) {
return;
}

$item = new Ticket();
if (!$item->getFromDB($followup->fields['items_id'])) {
return;
}

$validationStatus = PluginFormcreatorCommon::getTicketStatusForIssue($item);
$issue = new PluginFormcreatorIssue();
$issue->getFromDBByCrit([
'AND' => [
'sub_itemtype' => $itemtype,
'original_id' => $item->getID(),
]
]);
if ($issue->isNewItem()) {
return;
}
$issue->update([
'id' => $issue->getID(),
'sub_itemtype' => $itemtype,
'original_id' => $item->getID(),
'status' => $validationStatus['status'],
'date_mod' => $item->fields['date_mod'],
]);
}

function plugin_formcreator_dynamicReport($params) {
switch ($params['item_type']) {
case PluginFormcreatorFormAnswer::class;
Expand Down
3 changes: 2 additions & 1 deletion setup.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,8 @@ function plugin_init_formcreator() {

// hook to update issues when an operation occurs on a ticket
$PLUGIN_HOOKS['item_add']['formcreator'] = [
Ticket::class => 'plugin_formcreator_hook_add_ticket'
Ticket::class => 'plugin_formcreator_hook_add_ticket',
ITILFollowup::class => 'plugin_formcreator_hook_update_itilFollowup',
];
$PLUGIN_HOOKS['item_update']['formcreator'] = [
Ticket::class => 'plugin_formcreator_hook_update_ticket',
Expand Down
42 changes: 42 additions & 0 deletions tests/3-unit/PluginFormcreatorIssue.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public function beforeTestMethod($method) {
global $CFG_GLPI;
switch ($method) {
case 'testGetSyncIssuesRequest':
case 'testUpdateDateModOnNewFollowup':
$this->login('glpi', 'glpi');
$CFG_GLPI['use_notifications'] = 0;
break;
Expand Down Expand Up @@ -421,4 +422,45 @@ public function testGetSyncIssuesRequest($item, $expected) {
}
}

public function testUpdateDateModOnNewFollowup() {
$ticket = new \Ticket();
$ticket->add([
'name' => 'ticket',
'content' => 'foo',
]);
$this->boolean($ticket->isNewItem())->isFalse();
$creationDate = $ticket->fields['date_creation'];

$issue = new \PluginFormcreatorISsue();
$issue->getFromDBByCrit([
'sub_itemtype' => \Ticket::getType(),
'original_id' => $ticket->getID(),
]);
$this->boolean($issue->isNewItem())->isFalse();
$this->string($issue->fields['date_creation'])->isEqualTo($creationDate);
$this->string($issue->fields['date_mod'])->isEqualTo($creationDate);

sleep(2); // 2 seconds sleep to change the current datetime
$this->login('glpi', 'glpi'); // Needed to update the current datetime in session
$followup = new \ITILFollowup();
$followup->add([
'itemtype' => \Ticket::getType(),
'items_id' => $ticket->getID(),
'content' => 'bar'
]);
$this->boolean($followup->isNewItem())->isFalse();
$ticket = new \Ticket();
$ticket->getFromDB($issue->fields['original_id']);
$this->boolean($ticket->isNewItem())->isFalse();
$this->string($ticket->fields['date_mod'])->isNotEqualTo($creationDate);
$modDate = $ticket->fields['date_mod'];

$issue = new \PluginFormcreatorISsue();
$issue->getFromDBByCrit([
'sub_itemtype' => \Ticket::getType(),
'original_id' => $ticket->getID(),
]);
$this->string($issue->fields['date_creation'])->isEqualTo($creationDate);
$this->string($issue->fields['date_mod'])->isEqualTo($modDate);
}
}

0 comments on commit 0d6597a

Please sign in to comment.