Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the group assigned on ticket creation to history #265

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
31 changes: 31 additions & 0 deletions hook.php
Original file line number Diff line number Diff line change
Expand Up @@ -500,6 +500,37 @@ function plugin_escalade_item_add_ticket($item)
if ($item instanceof Ticket) {
unset($_SESSION['plugin_escalade']['skip_hook_add_user']);
unset($_SESSION['plugin_escalade']['keep_users']);
// add first group in history
if ($_SESSION['plugins']['escalade']['config']['remove_group']) {
$groups_id = 0;
if (isset($item->input['_groups_id_assign']) && $item->input['_groups_id_assign']) {
$groups = $item->input['_groups_id_assign'];
if (is_array($groups)) {
$groups = array_values($groups);
$groups_id = $groups[count($groups) - 1];
} else {
$groups_id = $groups;
}
} else if (isset($item->input['_actors']) && $item->input['_actors']) {
if (isset($item->input['_actors']['assign'])) {
foreach ($item->input['_actors']['assign'] as $actor) {
if ($actor['itemtype'] == 'Group') {
$groups_id = $actor['items_id'];
}
}
}
}
if ($groups_id) {
$group_ticket = new Group_Ticket();
$group_ticket->input = [
'id' => $item->getID(),
'groups_id' => $groups_id,
'actortype' => CommonITILActor::ASSIGN,
'_disablenotif' => true
];
PluginEscaladeTicket::addHistoryOnAddGroup($group_ticket);
}
}
}
}

Expand Down
26 changes: 16 additions & 10 deletions inc/ticket.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,20 @@ public static function addHistoryOnAddGroup(CommonDBTM $item)
$previous_groups_id = 0;
$counter = 0;

if (count($group_ticket->fields) > 0) {
// check if group assignment is made during ticket creation
$in_add = false;
$backtraces = debug_backtrace();
foreach ($backtraces as $backtrace) {
if (
$backtrace['function'] == "add"
&& ($backtrace['object'] instanceof CommonITILObject)
) {
$in_add = true;
break;
}
}

if (count($group_ticket->fields) > 0 && !$in_add) {
$previous_groups_id = $group_ticket->fields['groups_id'];

$last_history_groups = PluginEscaladeHistory::getLastHistoryForTicketAndGroup($tickets_id, $groups_id, $previous_groups_id);
Expand All @@ -350,16 +363,9 @@ public static function addHistoryOnAddGroup(CommonDBTM $item)
'counter' => $counter
]);

// check if group assignment is made during ticket creation
// in this case, skip following steps as it cannot be considered as a group escalation
$backtraces = debug_backtrace();
foreach ($backtraces as $backtrace) {
if (
$backtrace['function'] == "add"
&& ($backtrace['object'] instanceof CommonITILObject)
) {
return;
}
if ($in_add) {
return;
}

//remove old user(s) (pass if user added by new ticket)
Expand Down
136 changes: 136 additions & 0 deletions tests/units/GroupHistoryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
<?php

/**
* -------------------------------------------------------------------------
* Escalade plugin for GLPI
* -------------------------------------------------------------------------
*
* LICENSE
*
* This file is part of Escalade.
*
* Escalade is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* Escalade is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Escalade. If not, see <http://www.gnu.org/licenses/>.
* -------------------------------------------------------------------------
* @copyright Copyright (C) 2015-2023 by Escalade plugin team.
* @license GPLv2 https://www.gnu.org/licenses/gpl-2.0.html
* @link https://github.com/pluginsGLPI/escalade
* -------------------------------------------------------------------------
*/

namespace GlpiPlugin\Escalade\Tests\units;

use GlpiPlugin\Escalade\Tests\EscaladeTestCase;
use CommonITILActor;
use PluginEscaladeConfig;

final class GroupHistoryTest extends EscaladeTestCase
{
public function testGroupHistory()
{
$this->login();

$config = new PluginEscaladeConfig();
$conf = $config->find();
$conf = reset($conf);
$config->getFromDB($conf['id']);
$this->assertGreaterThan(0, $conf['id']);

// Update escalade config
$this->assertTrue($config->update(['remove_group' => 1] + $conf));

PluginEscaladeConfig::loadInSession();

$group1 = new \Group();
$group1_id = $group1->add(['name' => 'Group_1']);
$this->assertGreaterThan(0, $group1_id);

$group2 = new \Group();
$group2_id = $group2->add(['name' => 'Group_2']);
$this->assertGreaterThan(0, $group2_id);

// Create ticket with a group assigned
$ticket = new \Ticket();
$t_id = $ticket->add([
'name' => 'Assign Group Escalation Test',
'content' => '',
'_actors' => [
'assign' => [
[
'items_id' => $group1->getID(),
'itemtype' => 'Group'
]
],
]
]);

// Check if the group was assigned to the ticket
$ticket_group = new \Group_Ticket();
$this->assertTrue($ticket_group->getFromDBByCrit([
'tickets_id' => $t_id,
'groups_id' => $group1->getID(),
'type' => CommonITILActor::ASSIGN
]));

// Check if a record was created with 0 as the previous groups
$history1 = new \PluginEscaladeHistory();
$this->assertTrue($history1->getFromDBByCrit([
'tickets_id' => $t_id,
'groups_id_previous' => 0,
'groups_id' => $group1->getID()
]));

// Update ticket with a new group added
$this->assertTrue($ticket->update(
[
'id' => $t_id,
'_actors' => [
'assign' => [
[
'items_id' => $group1->getID(),
'itemtype' => 'Group'
],
[
'items_id' => $group2->getID(),
'itemtype' => 'Group'
]
],
],
]
));

// Check if group2 was assigned to the ticket
$ticket_group2 = new \Group_Ticket();
$this->assertTrue($ticket_group2->getFromDBByCrit([
'tickets_id' => $t_id,
'groups_id' => $group2->getID(),
'type' => CommonITILActor::ASSIGN
]));

// Check if group1 was removed from the ticket
$ticket_group = new \Group_Ticket();
$this->assertFalse($ticket_group->getFromDBByCrit([
'tickets_id' => $t_id,
'groups_id' => $group1->getID(),
'type' => CommonITILActor::ASSIGN
]));

// Check if a record was created to reflect the changes
$history2 = new \PluginEscaladeHistory();
$this->assertTrue($history2->getFromDBByCrit([
'tickets_id' => $t_id,
'groups_id_previous' => $group1->getID(),
'groups_id' => $group2->getID()
]));
}
}
Loading