From f9e8a9a29ffee425358a6c8419f0e670ff1f1f90 Mon Sep 17 00:00:00 2001 From: Lainow Date: Wed, 18 Dec 2024 14:47:28 +0100 Subject: [PATCH 1/4] Remove user deletion when we add more than one user --- inc/ticket.class.php | 1 - 1 file changed, 1 deletion(-) diff --git a/inc/ticket.class.php b/inc/ticket.class.php index 6785dea7..64b05224 100644 --- a/inc/ticket.class.php +++ b/inc/ticket.class.php @@ -622,7 +622,6 @@ public static function item_add_user(Ticket_User $item, $type = CommonITILActor: $ticket->getFromDB($tickets_id); $groups_id = []; - self::removeAssignUsers($ticket, $users_id, $type); // == Add user groups on modification == //check this plugin config From 7178b1122288a92a35afacdbe2d60d0d1c3cd993 Mon Sep 17 00:00:00 2001 From: Lainow Date: Thu, 19 Dec 2024 11:05:29 +0100 Subject: [PATCH 2/4] Fix deletion of old users --- inc/ticket.class.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/inc/ticket.class.php b/inc/ticket.class.php index 64b05224..d65bdb91 100644 --- a/inc/ticket.class.php +++ b/inc/ticket.class.php @@ -108,6 +108,8 @@ function ($carry, $type) use ($item) { } } } + + self::removeAssignUsers($item); return $item; } } From 47eb3db4adc7173397fdcd7910fc3e228dc12785 Mon Sep 17 00:00:00 2001 From: Lainow Date: Thu, 19 Dec 2024 11:22:11 +0100 Subject: [PATCH 3/4] Add units tests --- CHANGELOG.md | 1 + tests/units/UserEscalationTest.php | 96 ++++++++++++++++++++++++++++++ 2 files changed, 97 insertions(+) create mode 100644 tests/units/UserEscalationTest.php diff --git a/CHANGELOG.md b/CHANGELOG.md index 968b5968..e06b2e79 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/). ### Fixed +- Fix accidental deletion of new users when adding to a ticket - Redirect users without ticket rights after escalation. ## [2.9.10] - 2024-11-27 diff --git a/tests/units/UserEscalationTest.php b/tests/units/UserEscalationTest.php new file mode 100644 index 00000000..56e0c76a --- /dev/null +++ b/tests/units/UserEscalationTest.php @@ -0,0 +1,96 @@ +. + * ------------------------------------------------------------------------- + * @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 PluginEscaladeConfig; + +final class UserEscalationTest extends EscaladeTestCase +{ + public function testTaskGroupEscalation() + { + $this->login(); + + $config = new PluginEscaladeConfig(); + $conf = reset($config->find()); + $config->getFromDB($conf['id']); + $this->assertGreaterThan(0, $conf['id']); + $this->assertTrue($config->update([ + 'remove_tech' => 1 + ] + $conf)); + + $user1 = $user2 = new \User(); + $user1->getFromDBbyName('glpi'); + $this->assertGreaterThan(0, $user1->getID()); + $user2->getFromDBbyName('tech'); + $this->assertGreaterThan(0, $user2->getID()); + + $ticket = new \Ticket(); + $t_id = $ticket->add([ + 'name' => 'Task User change Escalation Test', + 'content' => '', + '_actors' => [ + 'assign' => [ + [ + 'items_id' => $user1->getID(), + 'itemtype' => 'User' + ] + ], + ] + ]); + + $ticket_user = new \Ticket_User(); + $this->assertEquals(1, count($ticket_user->find(['tickets_id' => $t_id, 'type' => \CommonITILActor::ASSIGN]))); + + // Update ticket with just one group + $this->assertTrue($ticket->update( + [ + 'id' => $t_id, + '_actors' => [ + 'assign' => [ + [ + 'items_id' => $user1->getID(), + 'itemtype' => 'User' + ], + [ + 'items_id' => $user2->getID(), + 'itemtype' => 'User' + ] + ], + ], + ] + )); + + $ticket_user = new \Ticket_User(); + $this->assertEquals(1, count($ticket_user->find(['tickets_id' => $t_id, 'type' => \CommonITILActor::ASSIGN]))); + $this->assertEquals(1, count($ticket_user->find(['tickets_id' => $t_id, 'type' => \CommonITILActor::ASSIGN, 'users_id' => $user1->getID()]))); + } +} From 7f6c85a925663d7fc0e9e3d1f4f1327beb36d863 Mon Sep 17 00:00:00 2001 From: Lainow Date: Mon, 30 Dec 2024 09:48:14 +0100 Subject: [PATCH 4/4] Fix units tests --- inc/ticket.class.php | 2 +- tests/units/UserEscalationTest.php | 39 +++++++++++++++++++++++++++--- 2 files changed, 36 insertions(+), 5 deletions(-) diff --git a/inc/ticket.class.php b/inc/ticket.class.php index d65bdb91..ede3c024 100644 --- a/inc/ticket.class.php +++ b/inc/ticket.class.php @@ -29,6 +29,7 @@ */ use Glpi\Application\View\TemplateRenderer; +use Symfony\Component\VarDumper\VarDumper; if (!defined('GLPI_ROOT')) { die("Sorry. You can't access directly to this file"); @@ -589,7 +590,6 @@ public static function removeAssignUsers($item, $keep_users_id = false, $type = //delete user $ticket_user->delete(['id' => $id]); - if (isset($item->input['_actors'])) { foreach ($item->input['_actors'][$types[$type]] as $key => $actor) { if ( diff --git a/tests/units/UserEscalationTest.php b/tests/units/UserEscalationTest.php index 56e0c76a..dcc1d097 100644 --- a/tests/units/UserEscalationTest.php +++ b/tests/units/UserEscalationTest.php @@ -35,21 +35,25 @@ final class UserEscalationTest extends EscaladeTestCase { - public function testTaskGroupEscalation() + public function testUserEscalationRemoveTech() { $this->login(); $config = new PluginEscaladeConfig(); - $conf = reset($config->find()); + $conf = $config->find(); + $conf = reset($conf); $config->getFromDB($conf['id']); $this->assertGreaterThan(0, $conf['id']); $this->assertTrue($config->update([ 'remove_tech' => 1 ] + $conf)); - $user1 = $user2 = new \User(); + PluginEscaladeConfig::loadInSession(); + + $user1 = new \User(); $user1->getFromDBbyName('glpi'); $this->assertGreaterThan(0, $user1->getID()); + $user2 = new \User(); $user2->getFromDBbyName('tech'); $this->assertGreaterThan(0, $user2->getID()); @@ -91,6 +95,33 @@ public function testTaskGroupEscalation() $ticket_user = new \Ticket_User(); $this->assertEquals(1, count($ticket_user->find(['tickets_id' => $t_id, 'type' => \CommonITILActor::ASSIGN]))); - $this->assertEquals(1, count($ticket_user->find(['tickets_id' => $t_id, 'type' => \CommonITILActor::ASSIGN, 'users_id' => $user1->getID()]))); + $this->assertEquals(1, count($ticket_user->find(['tickets_id' => $t_id, 'type' => \CommonITILActor::ASSIGN, 'users_id' => $user2->getID()]))); + + $this->assertTrue($config->update([ + 'remove_tech' => 0 + ] + $conf)); + + PluginEscaladeConfig::loadInSession(); + + $this->assertTrue($ticket->update( + [ + 'id' => $t_id, + '_actors' => [ + 'assign' => [ + [ + 'items_id' => $user1->getID(), + 'itemtype' => 'User' + ], + [ + 'items_id' => $user2->getID(), + 'itemtype' => 'User' + ] + ], + ], + ] + )); + + $ticket_user2 = new \Ticket_User(); + $this->assertEquals(2, count($ticket_user2->find(['tickets_id' => $t_id, 'type' => \CommonITILActor::ASSIGN]))); } }