Skip to content

Commit

Permalink
feat(PendingReason): restore previous status
Browse files Browse the repository at this point in the history
  • Loading branch information
stonebuzz authored and cedric-anne committed Sep 25, 2023
1 parent da9d858 commit 8f8851f
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 6 deletions.
44 changes: 44 additions & 0 deletions install/migrations/update_10.0.10_to_10.0.11/pendingreason.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

/**
* ---------------------------------------------------------------------
*
* GLPI - Gestionnaire Libre de Parc Informatique
*
* http://glpi-project.org
*
* @copyright 2015-2023 Teclib' and contributors.
* @copyright 2003-2014 by the INDEPNET Development Team.
* @licence https://www.gnu.org/licenses/gpl-3.0.html
*
* ---------------------------------------------------------------------
*
* LICENSE
*
* This file is part of GLPI.
*
* This program 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 3 of the License, or
* (at your option) any later version.
*
* This program 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 this program. If not, see <https://www.gnu.org/licenses/>.
*
* ---------------------------------------------------------------------
*/

/**
* @var DB $DB
* @var Migration $migration
*/

/* Add `previous_status` to glpi_pendingreasons_items */
if (!$DB->fieldExists('glpi_pendingreasons_items', 'previous_status')) {
$migration->addField('glpi_pendingreasons_items', 'previous_status', "int DEFAULT NULL");
}
1 change: 1 addition & 0 deletions install/mysql/glpi-empty.sql
Original file line number Diff line number Diff line change
Expand Up @@ -9098,6 +9098,7 @@ CREATE TABLE `glpi_pendingreasons_items` (
`followups_before_resolution` int NOT NULL DEFAULT '0',
`bump_count` int NOT NULL DEFAULT '0',
`last_bump_date` timestamp NULL DEFAULT NULL,
`previous_status` int DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `unicity` (`items_id`,`itemtype`),
KEY `pendingreasons_id` (`pendingreasons_id`),
Expand Down
1 change: 1 addition & 0 deletions src/Features/ParentStatus.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ public function updateParentStatus(CommonITILObject $parentitem, array $input):
'pendingreasons_id' => $input['pendingreasons_id'] ?? 0,
'followup_frequency' => $input['followup_frequency'] ?? 0,
'followups_before_resolution' => $input['followups_before_resolution'] ?? 0,
'previous_status' => $parentitem->fields['status'],
]);
PendingReason_Item::createForItem($this, [
'pendingreasons_id' => $input['pendingreasons_id'] ?? 0,
Expand Down
15 changes: 9 additions & 6 deletions src/PendingReason_Item.php
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,6 @@ public static function isLastTimelineItem(CommonDBTM $timeline_item): bool
*/
public static function handleTimelineEdits(CommonDBTM $timeline_item): array
{

if (self::getForItem($timeline_item)) {
// Event was already marked as pending

Expand Down Expand Up @@ -360,14 +359,17 @@ public static function handleTimelineEdits(CommonDBTM $timeline_item): array
}
}
} else if (!$timeline_item->input['pending'] ?? 1) {
// No longer pending, remove pending data
self::deleteForItem($timeline_item->input["_job"]);
self::deleteForItem($timeline_item);

// Change status of parent if needed
if ($timeline_item->input["_job"]->fields['status'] == CommonITILObject::WAITING) {
$timeline_item->input['_status'] = CommonITILObject::ASSIGNED;
// get previous stored status for parent
if ($parent_pending = self::getForItem($timeline_item->input["_job"])) {
$timeline_item->input['_status'] = $parent_pending->fields['previous_status'] ?? CommonITILObject::ASSIGNED;
}
}

// No longer pending, remove pending data
self::deleteForItem($timeline_item->input["_job"]);
self::deleteForItem($timeline_item);
}
} else {
// Not pending yet; did it change ?
Expand All @@ -380,6 +382,7 @@ public static function handleTimelineEdits(CommonDBTM $timeline_item): array
'pendingreasons_id' => $timeline_item->input['pendingreasons_id'],
'followup_frequency' => $timeline_item->input['followup_frequency'] ?? 0,
'followups_before_resolution' => $timeline_item->input['followups_before_resolution'] ?? 0,
'previous_status' => $timeline_item->input["_job"]->fields['status']
]);
self::createForItem($timeline_item, [
'pendingreasons_id' => $timeline_item->input['pendingreasons_id'],
Expand Down
68 changes: 68 additions & 0 deletions tests/functional/PendingReason.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -382,4 +382,72 @@ public function testAddPendingFollowupOnAlreadyPending(): void
$p_item = PendingReason_Item::getForItem($ticket);
$this->boolean($p_item)->isFalse();
}

/**
* Remove pending from timeline item should delete any linked
* PendingReason_Item objects and restore previous status
*/
public function testRemovePendingAndRevertStatus(): void
{
$this->login();
$entity = getItemByTypeName('Entity', '_test_root_entity', true);

// Create a pending reason and a ticket for our tests
$pending_reason = $this->createItem('PendingReason', [
'entities_id' => $entity,
'name' => 'Pending reason 1',
'followup_frequency' => 604800,
'followups_before_resolution' => 3,
]);

foreach (
[
Ticket::class => CommonITILObject::ASSIGNED,
Change::class => CommonITILObject::EVALUATION,
Problem::class => CommonITILObject::OBSERVED
] as $itemtype => $status
) {
$item = $this->createItem($itemtype, [
'name' => $itemtype,
'content' => "test " . $itemtype,
'status' => $status,
'_users_id_requester' => getItemByTypeName('User', 'post-only', true),
'_users_id_assign' => getItemByTypeName('User', TU_USER, true),
'entities_id' => $entity
]);

// Set the item as pending with a reason
$followup = $this->createItem('ITILFollowup', [
'itemtype' => $item->getType(),
'items_id' => $item->getID(),
'content' => 'Followup with pending reason',
'pending' => true,
'pendingreasons_id' => $pending_reason->getID(),
'followup_frequency' => 604800,
'followups_before_resolution' => 3,
], ['pending', 'pendingreasons_id', 'followup_frequency', 'followups_before_resolution']);

// Check that pending reason is applied to parent item
$p_item = PendingReason_Item::getForItem($item);
$this->integer($p_item->fields['pendingreasons_id'])->isEqualTo($pending_reason->getID());
$this->integer($p_item->fields['followup_frequency'])->isEqualTo(604800);
$this->integer($p_item->fields['followups_before_resolution'])->isEqualTo(3);
$this->integer($p_item->fields['previous_status'])->isEqualTo($status);

// Update followup and unset pending
$this->boolean($followup->update([
'id' => $followup->getID(),
'content' => $followup->fields['content'],
'pending' => false,
]))->isTrue();

// Check that pending reason no longer exist
$p_item = PendingReason_Item::getForItem($item);
$this->boolean($p_item)->isFalse();

// Reload / Check that original status is set
$item->getFromDB($item->getID());
$this->integer($item->fields['status'])->isEqualTo($status);
}
}
}

0 comments on commit 8f8851f

Please sign in to comment.