From 2a432f91d2ec5b2206817d2a98afcf98f61142a9 Mon Sep 17 00:00:00 2001 From: Lainow Date: Tue, 6 Aug 2024 13:59:50 +0200 Subject: [PATCH] Update units tests --- inc/ticket.class.php | 2 +- tests/units/TaskMessageTest.php | 160 ++++++++++++++++++++++++++------ 2 files changed, 132 insertions(+), 30 deletions(-) diff --git a/inc/ticket.class.php b/inc/ticket.class.php index da4a4b5..06344be 100644 --- a/inc/ticket.class.php +++ b/inc/ticket.class.php @@ -573,7 +573,7 @@ public static function removeAssignUsers($item, $keep_users_id = false, $type = return true; } - $tickets_id = $item->input['id']; + $tickets_id = $item->input['id'] ?? $item->fields['id']; $where_keep = [ 'tickets_id' => $tickets_id, diff --git a/tests/units/TaskMessageTest.php b/tests/units/TaskMessageTest.php index c212d92..bc10ce7 100644 --- a/tests/units/TaskMessageTest.php +++ b/tests/units/TaskMessageTest.php @@ -34,6 +34,7 @@ use Group_Ticket; use Plugin; use PluginEscaladeConfig; +use Ticket_User; use TicketTask; final class TaskMessageTest extends EscaladeTestCase @@ -49,22 +50,6 @@ public function testPluginReactivated() } protected function dataTestEscalationTaskGroup(): array { - $ticket = new \Ticket(); - $ticket->add([ - 'name' => 'Escalation Test', - 'content' => '', - ]); - - $group_test = new \Group(); - $group_test->add([ - 'name' => 'Test Group', - ]); - - $group_test_2 = new \Group(); - $group_test_2->add([ - 'name' => 'Test Group 2', - ]); - return [ [ 'expected' => [ @@ -125,26 +110,143 @@ protected function dataTestEscalationTaskGroup(): array ]; } - /** - * @dataProvider dataTestEscalationTaskGroup - */ - public function testEscalationTaskGroup($expected, $inputs) + public function testGroupEscalation() { $ticket = new \Ticket(); - $ticket->getFromDB($expected['ticket_id']); + $ticket->add([ + 'name' => 'Escalation Test', + 'content' => '', + ]); + + $user_test = new \User(); + $user_test->add([ + 'name' => 'Escalation Technician', + ]); + + $group_test = new \Group(); + $group_test->add([ + 'name' => 'Escalation Group', + ]); + + // Update ticket with a technician + $this->assertTrue($ticket->update( + [ + 'id' => $ticket->getID(), + '_actors' => [ + 'assign' => [ + [ + 'items_id' => $user_test->getID(), + 'itemtype' => $user_test->getType() + ], + ], + ], + ] + )); + + // Check that the group linked to this ticket is "Test group 1" and that the technician has disappeared. + $ticket_user = new Ticket_User(); + $ticket_user->getFromDBByCrit(['tickets_id' => $ticket->getID()]); + $this->assertEquals($ticket_user->fields['users_id'], $user_test->getID()); + + // Update ticket with a group + $this->assertTrue($ticket->update( + [ + 'id' => $ticket->getID(), + '_actors' => [ + 'assign' => [ + [ + 'items_id' => $user_test->getID(), + 'itemtype' => $user_test->getType() + ], + [ + 'items_id' => $group_test->getID(), + 'itemtype' => 'Group' + ] + ], + ], + ] + )); - $this->assertTrue($ticket->update($inputs)); + // Check that the group linked to this ticket is "Test group 1" and that the technician has disappeared. + $ticket_user = new Ticket_User(); + $t_users = $ticket_user->find(['tickets_id' => $ticket->getID()]); + $this->assertEquals(count($t_users), 0); + // Check that the group linked to this ticket is "Test group 1" and that the technician has disappeared. $ticket_group = new Group_Ticket(); - $t_groups = $ticket_group->find(['tickets_id' => $expected['ticket_id']]); - $t_groups = end($t_groups); - $this->assertEquals($t_groups['groups_id'] ?? null, $expected['group_id']); + $ticket_group->getFromDBByCrit(['tickets_id' => $ticket->getID()]); + $this->assertEquals($ticket_group->fields['groups_id'], $group_test->getID()); +} + + public function testTaskGroupEscalation() + { + $ticket = new \Ticket(); + $ticket->add([ + 'name' => 'Task Group Escalation Test', + 'content' => '', + ]); + + $group_test = new \Group(); + $group_test->add([ + 'name' => 'Task Group 1', + ]); + + $group_test_2 = new \Group(); + $group_test_2->add([ + 'name' => 'Task Group 2', + ]); + // Update ticket with just one group + $this->assertTrue($ticket->update( + [ + 'id' => $ticket->getID(), + '_actors' => [ + 'assign' => [ + [ + 'items_id' => $group_test->getID(), + 'itemtype' => 'Group' + ], + ], + ], + ] + )); + + // Check the correct task content $ticket_task = new TicketTask(); - $t_tasks = $ticket_task->find(['tickets_id' => $expected['ticket_id']]); + $t_tasks = $ticket_task->find(['tickets_id' => $ticket->getID()]); $last_task = end($t_tasks); - if ($last_task) { - $this->assertStringContainsString($expected['group_name'], $last_task['content']); - } + $this->assertStringContainsString('Task Group 1', $last_task['content']); + + $this->assertTrue($ticket->update( + [ + 'id' => $ticket->getID(), + '_actors' => [ + 'assign' => [ + [ + 'items_id' => $group_test->getID(), + 'itemtype' => 'Group' + ], + [ + 'items_id' => $group_test_2->getID(), + 'itemtype' => 'Group' + ], + ], + ], + ] + )); + + // Check the correct order of tasks and content + $ticket_task = new TicketTask(); + $t_tasks = $ticket_task->find(['tickets_id' => $ticket->getID()]); + $first_task = reset($t_tasks); + $this->assertStringContainsString('Task Group 1', $first_task['content']); + $last_task = end($t_tasks); + $this->assertStringContainsString('Task Group 2', $last_task['content']); + + // Check that the group linked to this ticket is "Test group 2". + $ticket_group = new Group_Ticket(); + $t_groups = $ticket_group->find(['tickets_id' => $ticket->getID()]); + $t_groups = end($t_groups); + $this->assertEquals($t_groups['groups_id'], $group_test_2->getID()); } }