Skip to content

Commit

Permalink
Fix merged ticket responses not being added to parent
Browse files Browse the repository at this point in the history
 ticket
  • Loading branch information
RomainLvr authored and trasher committed May 22, 2024
1 parent b5a28c6 commit 6614c78
Show file tree
Hide file tree
Showing 5 changed files with 184 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/CommonITILTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -728,9 +728,28 @@ public function post_addItem()
// Assign technician to main item from task
self::assignTechFromtask($this->input);

if ($this->input["_job"]->getType() == 'Ticket') {
self::addToMergedTickets();
}

parent::post_addItem();
}

private function addToMergedTickets(): void
{
$merged = Ticket::getMergedTickets($this->fields['tickets_id']);
foreach ($merged as $ticket_id) {
$input = $this->fields;
$input['tickets_id'] = $ticket_id;
$input['sourceitems_id'] = $this->fields['tickets_id'];
unset($input['id']);
$input['uuid'] = \Ramsey\Uuid\Uuid::uuid4();

$task = new static();
$task->add($input);
}
}

public function post_getEmpty()
{

Expand Down
15 changes: 15 additions & 0 deletions src/Document_Item.php
Original file line number Diff line number Diff line change
Expand Up @@ -214,9 +214,24 @@ public function post_addItem()

$ticket->update($input);
}

self::addToMergedTickets();

parent::post_addItem();
}

private function addToMergedTickets(): void
{
$merged = Ticket::getMergedTickets($this->fields['items_id']);
foreach ($merged as $ticket_id) {
$input = $this->input;
$input['items_id'] = $ticket_id;

$document = new self();
$document->add($input);
}
}

public function post_purgeItem()
{
if ($this->fields['itemtype'] === Ticket::class) {
Expand Down
15 changes: 15 additions & 0 deletions src/ITILFollowup.php
Original file line number Diff line number Diff line change
Expand Up @@ -308,9 +308,24 @@ public function post_addItem()
Log::HISTORY_ADD_SUBITEM
);

self::addToMergedTickets();

parent::post_addItem();
}

private function addToMergedTickets(): void
{
$merged = Ticket::getMergedTickets($this->fields['items_id']);
foreach ($merged as $ticket_id) {
$input = $this->input;
$input['items_id'] = $ticket_id;
$input['sourceitems_id'] = $this->fields['items_id'];

$followup = new self();
$followup->add($input);
}
}


public function post_deleteFromDB()
{
Expand Down
33 changes: 33 additions & 0 deletions src/Ticket.php
Original file line number Diff line number Diff line change
Expand Up @@ -6198,6 +6198,39 @@ public static function merge(int $merge_target_id, array $ticket_ids, array &$st
return true;
}

/**
* Get the list of tickets in which the ticket has been merged
*
* @param int $id The ID of the ticket
*
* @return array The list of tickets that have ticket with ID $id as son
*/
public static function getMergedTickets(int $id): array
{
/**
* @var \DBmysql $DB
*/
global $DB;

//look for merged tickets
$merged = [];
$iterator = $DB->request(
[
'FROM' => Ticket_Ticket::getTable(),
'SELECT' => ['tickets_id_2'],
'DISTINCT' => true,
'WHERE' => [
'tickets_id_1' => $id,
'link' => Ticket_Ticket::SON_OF
]
]
);
foreach ($iterator as $data) {
$merged[] = $data['tickets_id_2'];
}
return $merged;
}


/**
* Check profiles and detect where criteria from existing rights
Expand Down
102 changes: 102 additions & 0 deletions tests/functional/Ticket.php
Original file line number Diff line number Diff line change
Expand Up @@ -3841,6 +3841,108 @@ public function testMerge()
$this->integer((int)$supplier_count)->isEqualTo(3);
}

/**
* After a ticket has been merged (set as deleted), the responses from the child tickets should be copied to the parent ticket.
*/
public function testResponsesAfterMerge(): void
{
$this->login();
$_SESSION['glpiactiveprofile']['interface'] = '';
$this->setEntity('Root entity', true);

$ticket = new \Ticket();
$ticket1 = $ticket->add([
'name' => "Parent ticket",
'content' => "Parent ticket",
'entities_id' => 0,
'status' => \CommonITILObject::INCOMING,
]);
$ticket2 = $ticket->add([
'name' => "Child ticket",
'content' => "Child ticket",
'entities_id' => 0,
'status' => \CommonITILObject::INCOMING,
]);

$status = [];
$mergeparams = [
'linktypes' => [
'ITILFollowup',
'TicketTask',
'Document'
],
'link_type' => \Ticket_Ticket::SON_OF
];

\Ticket::merge($ticket1, [$ticket2], $status, $mergeparams);

$status_counts = array_count_values($status);
$failure_count = 0;
if (array_key_exists(1, $status_counts)) {
$failure_count += $status_counts[1];
}
if (array_key_exists(2, $status_counts)) {
$failure_count += $status_counts[2];
}

$this->integer((int)$failure_count)->isEqualTo(0);

// Add a followup to the child ticket
$followup = new \ITILFollowup();
$this->integer($followup->add([
'itemtype' => 'Ticket',
'items_id' => $ticket2,
'content' => 'Child ticket followup'
]))->isGreaterThan(0);

// Check that the followup was copied to the parent ticket
$this->array($followup->find([
'itemtype' => 'Ticket',
'items_id' => $ticket1,
'sourceitems_id' => $ticket2,
'content' => 'Child ticket followup'
]))->isNotEmpty();

// Add a task to the child ticket
$task = new \TicketTask();
$this->integer($task->add([
'tickets_id' => $ticket2,
'content' => 'Child ticket task'
]))->isGreaterThan(0);

// Check that the task was copied to the parent ticket
$this->array($task->find([
'tickets_id' => $ticket1,
'sourceitems_id' => $ticket2,
'content' => 'Child ticket task'
]))->isNotEmpty();

// Add a document to the child ticket
$document = new \Document();
$documents_id = $document->add([
'name' => 'Child ticket document',
'filename' => 'doc.xls',
'users_id' => '2', // user "glpi"
]);
$this->integer($documents_id)->isGreaterThan(0);

$document_item = new \Document_Item();
$this->integer($document_item->add([
'itemtype' => 'Ticket',
'items_id' => $ticket2,
'documents_id' => $documents_id,
'entities_id' => '0',
'is_recursive' => 0
]))->isGreaterThan(0);

// Check that the document was copied to the parent ticket
$this->array($document_item->find([
'itemtype' => 'Ticket',
'items_id' => $ticket1,
'documents_id' => $documents_id,
]))->isNotEmpty();
}

public function testKeepScreenshotsOnFormReload()
{
//login to get session
Expand Down

0 comments on commit 6614c78

Please sign in to comment.