From 7fbbb967bbaa0911644d7583284a5a9679c3f70f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Langlois=20Ga=C3=ABtan?= <64356364+MyvTsv@users.noreply.github.com> Date: Tue, 26 Nov 2024 15:31:03 +0100 Subject: [PATCH] fix(SO): Add a search option to retrieve the latest follow-up, tasks, and solution --- phpunit/functional/ChangeTest.php | 113 +++++++++++++++++++++++++++++ phpunit/functional/ProblemTest.php | 113 +++++++++++++++++++++++++++++ phpunit/functional/TicketTest.php | 113 +++++++++++++++++++++++++++++ src/CommonITILObject.php | 15 ++++ src/CommonITILTask.php | 18 +++++ src/ITILFollowup.php | 20 +++++ 6 files changed, 392 insertions(+) diff --git a/phpunit/functional/ChangeTest.php b/phpunit/functional/ChangeTest.php index 1d56f95ba51..dd5afd9980f 100644 --- a/phpunit/functional/ChangeTest.php +++ b/phpunit/functional/ChangeTest.php @@ -318,4 +318,117 @@ public function testStatusWhenSolutionIsRefused() $item = $change->getById($changes_id); $this->assertSame(\CommonITILObject::INCOMING, $item->fields['status']); } + + public function testSearchOptions() + { + $this->login(); + + $last_followup_date = '2016-01-01 00:00:00'; + $last_task_date = '2017-01-01 00:00:00'; + $last_solution_date = '2018-01-01 00:00:00'; + + $change = new \Change(); + $change_id = $change->add( + [ + 'name' => 'ticket title', + 'content' => 'a description', + 'entities_id' => getItemByTypeName('Entity', '_test_root_entity', true), + ] + ); + + $followup = new \ITILFollowup(); + $followup->add([ + 'itemtype' => $change::getType(), + 'items_id' => $change_id, + 'content' => 'followup content', + 'date' => '2015-01-01 00:00:00', + ]); + + $followup->add([ + 'itemtype' => $change::getType(), + 'items_id' => $change_id, + 'content' => 'followup content', + 'date' => '2015-02-01 00:00:00', + ]); + + $task = new \ChangeTask(); + $this->assertGreaterThan( + 0, + (int)$task->add([ + 'changes_id' => $change_id, + 'content' => 'A simple Task', + 'date' => '2015-01-01 00:00:00', + ]) + ); + + $this->assertGreaterThan( + 0, + (int)$task->add([ + 'changes_id' => $change_id, + 'content' => 'A simple Task', + 'date' => $last_task_date, + ]) + ); + + $this->assertGreaterThan( + 0, + (int)$task->add([ + 'changes_id' => $change_id, + 'content' => 'A simple Task', + 'date' => '2016-01-01 00:00:00', + ]) + ); + + $solution = new \ITILSolution(); + $this->assertGreaterThan( + 0, + (int)$solution->add([ + 'itemtype' => $change::getType(), + 'items_id' => $change_id, + 'content' => 'solution content', + 'date_creation' => '2017-01-01 00:00:00', + 'status' => 2, + ]) + ); + + $this->assertGreaterThan( + 0, + (int)$followup->add([ + 'itemtype' => $change::getType(), + 'items_id' => $change_id, + 'add_reopen' => '1', + 'content' => 'This is required', + 'date' => $last_followup_date, + ]) + ); + + $this->assertGreaterThan( + 0, + (int)$solution->add([ + 'itemtype' => $change::getType(), + 'items_id' => $change_id, + 'content' => 'solution content', + 'date_creation' => $last_solution_date, + ]) + ); + + $criteria = [ + [ + 'link' => 'AND', + 'field' => 2, + 'searchtype' => 'contains', + 'value' => $change_id, + ] + ]; + $data = \Search::getDatas($change->getType(), ["criteria" => $criteria], [72,73,74]); + $this->assertSame(1, $data['data']['totalcount']); + $change_with_so = $data['data']['rows'][0]['raw']; + $this->assertEquals($change_id, $change_with_so['id']); + $this->assertTrue(array_key_exists('ITEM_Change_72', $change_with_so)); + $this->assertEquals($last_followup_date, $change_with_so['ITEM_Change_72']); + $this->assertTrue(array_key_exists('ITEM_Change_73', $change_with_so)); + $this->assertEquals($last_task_date, $change_with_so['ITEM_Change_73']); + $this->assertTrue(array_key_exists('ITEM_Change_74', $change_with_so)); + $this->assertEquals($last_solution_date, $change_with_so['ITEM_Change_74']); + } } diff --git a/phpunit/functional/ProblemTest.php b/phpunit/functional/ProblemTest.php index 95ab1cf94dc..1a4623ec829 100644 --- a/phpunit/functional/ProblemTest.php +++ b/phpunit/functional/ProblemTest.php @@ -194,4 +194,117 @@ public function testGetTeamRoleName(): void $this->assertNotEmpty(\Problem::getTeamRoleName($role)); } } + + public function testSearchOptions() + { + $this->login(); + + $last_followup_date = '2016-01-01 00:00:00'; + $last_task_date = '2017-01-01 00:00:00'; + $last_solution_date = '2018-01-01 00:00:00'; + + $problem = new \Problem(); + $problem_id = $problem->add( + [ + 'name' => 'ticket title', + 'content' => 'a description', + 'entities_id' => getItemByTypeName('Entity', '_test_root_entity', true), + ] + ); + + $followup = new \ITILFollowup(); + $followup->add([ + 'itemtype' => $problem::getType(), + 'items_id' => $problem_id, + 'content' => 'followup content', + 'date' => '2015-01-01 00:00:00', + ]); + + $followup->add([ + 'itemtype' => $problem::getType(), + 'items_id' => $problem_id, + 'content' => 'followup content', + 'date' => '2015-02-01 00:00:00', + ]); + + $task = new \ProblemTask(); + $this->assertGreaterThan( + 0, + (int)$task->add([ + 'problems_id' => $problem_id, + 'content' => 'A simple Task', + 'date' => '2015-01-01 00:00:00', + ]) + ); + + $this->assertGreaterThan( + 0, + (int)$task->add([ + 'problems_id' => $problem_id, + 'content' => 'A simple Task', + 'date' => $last_task_date, + ]) + ); + + $this->assertGreaterThan( + 0, + (int)$task->add([ + 'problems_id' => $problem_id, + 'content' => 'A simple Task', + 'date' => '2016-01-01 00:00:00', + ]) + ); + + $solution = new \ITILSolution(); + $this->assertGreaterThan( + 0, + (int)$solution->add([ + 'itemtype' => $problem::getType(), + 'items_id' => $problem_id, + 'content' => 'solution content', + 'date_creation' => '2017-01-01 00:00:00', + 'status' => 2, + ]) + ); + + $this->assertGreaterThan( + 0, + (int)$followup->add([ + 'itemtype' => $problem::getType(), + 'items_id' => $problem_id, + 'add_reopen' => '1', + 'content' => 'This is required', + 'date' => $last_followup_date, + ]) + ); + + $this->assertGreaterThan( + 0, + (int)$solution->add([ + 'itemtype' => $problem::getType(), + 'items_id' => $problem_id, + 'content' => 'solution content', + 'date_creation' => $last_solution_date, + ]) + ); + + $criteria = [ + [ + 'link' => 'AND', + 'field' => 2, + 'searchtype' => 'contains', + 'value' => $problem_id, + ] + ]; + $data = \Search::getDatas($problem->getType(), ["criteria" => $criteria], [72,73,74]); + $this->assertSame(1, $data['data']['totalcount']); + $problem_with_so = $data['data']['rows'][0]['raw']; + $this->assertEquals($problem_id, $problem_with_so['id']); + $this->assertTrue(array_key_exists('ITEM_Problem_72', $problem_with_so)); + $this->assertEquals($last_followup_date, $problem_with_so['ITEM_Problem_72']); + $this->assertTrue(array_key_exists('ITEM_Problem_73', $problem_with_so)); + $this->assertEquals($last_task_date, $problem_with_so['ITEM_Problem_73']); + $this->assertTrue(array_key_exists('ITEM_Problem_74', $problem_with_so)); + $this->assertEquals($last_solution_date, $problem_with_so['ITEM_Problem_74']); + } } diff --git a/phpunit/functional/TicketTest.php b/phpunit/functional/TicketTest.php index bf189161218..a72dfabeb0e 100644 --- a/phpunit/functional/TicketTest.php +++ b/phpunit/functional/TicketTest.php @@ -542,6 +542,119 @@ public function testCreateTicketWithActors(array $actors_input, array $expected_ $this->checkActors($ticket, $expected_actors); } + public function testSearchOptions() + { + $this->login(); + + $last_followup_date = '2016-01-01 00:00:00'; + $last_task_date = '2017-01-01 00:00:00'; + $last_solution_date = '2018-01-01 00:00:00'; + + $ticket = new \Ticket(); + $ticket_id = $ticket->add( + [ + 'name' => 'ticket title', + 'content' => 'a description', + 'entities_id' => getItemByTypeName('Entity', '_test_root_entity', true), + ] + ); + + $followup = new \ITILFollowup(); + $followup->add([ + 'itemtype' => $ticket::getType(), + 'items_id' => $ticket_id, + 'content' => 'followup content', + 'date' => '2015-01-01 00:00:00', + ]); + + $followup->add([ + 'itemtype' => $ticket::getType(), + 'items_id' => $ticket_id, + 'content' => 'followup content', + 'date' => '2015-02-01 00:00:00', + ]); + + $task = new \TicketTask(); + $this->assertGreaterThan( + 0, + (int)$task->add([ + 'tickets_id' => $ticket_id, + 'content' => 'A simple Task', + 'date' => '2015-01-01 00:00:00', + ]) + ); + + $this->assertGreaterThan( + 0, + (int)$task->add([ + 'tickets_id' => $ticket_id, + 'content' => 'A simple Task', + 'date' => $last_task_date, + ]) + ); + + $this->assertGreaterThan( + 0, + (int)$task->add([ + 'tickets_id' => $ticket_id, + 'content' => 'A simple Task', + 'date' => '2016-01-01 00:00:00', + ]) + ); + + $solution = new \ITILSolution(); + $this->assertGreaterThan( + 0, + (int)$solution->add([ + 'itemtype' => $ticket::getType(), + 'items_id' => $ticket_id, + 'content' => 'solution content', + 'date_creation' => '2017-01-01 00:00:00', + 'status' => 2, + ]) + ); + + $this->assertGreaterThan( + 0, + (int)$followup->add([ + 'itemtype' => $ticket::getType(), + 'items_id' => $ticket_id, + 'add_reopen' => '1', + 'content' => 'This is required', + 'date' => $last_followup_date, + ]) + ); + + $this->assertGreaterThan( + 0, + (int)$solution->add([ + 'itemtype' => $ticket::getType(), + 'items_id' => $ticket_id, + 'content' => 'solution content', + 'date_creation' => $last_solution_date, + ]) + ); + + $criteria = [ + [ + 'link' => 'AND', + 'field' => 2, + 'searchtype' => 'contains', + 'value' => $ticket_id, + ] + ]; + $data = \Search::getDatas($ticket->getType(), ["criteria" => $criteria], [72,73,74]); + $this->assertSame(1, $data['data']['totalcount']); + $ticket_with_so = $data['data']['rows'][0]['raw']; + $this->assertEquals($ticket_id, $ticket_with_so['id']); + $this->assertTrue(array_key_exists('ITEM_Ticket_72', $ticket_with_so)); + $this->assertEquals($last_followup_date, $ticket_with_so['ITEM_Ticket_72']); + $this->assertTrue(array_key_exists('ITEM_Ticket_73', $ticket_with_so)); + $this->assertEquals($last_task_date, $ticket_with_so['ITEM_Ticket_73']); + $this->assertTrue(array_key_exists('ITEM_Ticket_74', $ticket_with_so)); + $this->assertEquals($last_solution_date, $ticket_with_so['ITEM_Ticket_74']); + } + public static function updateActorsProvider(): iterable { diff --git a/src/CommonITILObject.php b/src/CommonITILObject.php index 04750249a5f..c4495f8039a 100644 --- a/src/CommonITILObject.php +++ b/src/CommonITILObject.php @@ -4365,6 +4365,21 @@ public function getSearchOptionsSolution() ] ]; + $tab[] = [ + 'id' => '74', + 'table' => ITILSolution::getTable(), + 'field' => 'date_creation', + 'name' => _n('Latest date', 'Latest dates', 1), + 'datatype' => 'datetime', + 'massiveaction' => false, + 'forcegroupby' => true, + 'joinparams' => [ + 'jointype' => 'itemtype_item', + ], + 'computation' => 'MAX( ' . $DB->quoteName('TABLE.date_creation') . ')', + 'nometa' => true // cannot GROUP_CONCAT a MAX + ]; + return $tab; } diff --git a/src/CommonITILTask.php b/src/CommonITILTask.php index fbd143c2517..6253161c761 100644 --- a/src/CommonITILTask.php +++ b/src/CommonITILTask.php @@ -815,6 +815,8 @@ public function rawSearchOptions() **/ public static function rawSearchOptionsToAdd($itemtype = null) { + /** @var \DBmysql $DB */ + global $DB; $task = new static(); $tab = []; @@ -993,6 +995,22 @@ public static function rawSearchOptionsToAdd($itemtype = null) ] ]; + $tab[] = [ + 'id' => '73', + 'table' => static::getTable(), + 'field' => 'date', + 'name' => _n('Latest date', 'Latest dates', 1), + 'datatype' => 'datetime', + 'massiveaction' => false, + 'forcegroupby' => true, + 'joinparams' => [ + 'jointype' => 'child', + 'condition' => $task_condition + ], + 'computation' => 'MAX( ' . $DB->quoteName('TABLE.date') . ')', + 'nometa' => true // cannot GROUP_CONCAT a MAX + ]; + $tab[] = [ 'id' => '33', 'table' => static::getTable(), diff --git a/src/ITILFollowup.php b/src/ITILFollowup.php index e50eef57a94..53395e72884 100644 --- a/src/ITILFollowup.php +++ b/src/ITILFollowup.php @@ -665,6 +665,8 @@ public function rawSearchOptions() public static function rawSearchOptionsToAdd($itemtype = null) { + /** @var \DBmysql $DB */ + global $DB; $tab = []; @@ -713,6 +715,24 @@ public static function rawSearchOptionsToAdd($itemtype = null) ] ]; + $tab[] = [ + 'id' => '72', + 'table' => static::getTable(), + 'field' => 'date', + 'name' => _n('Latest date', 'Latest dates', 1), + 'datatype' => 'datetime', + 'massiveaction' => false, + 'forcegroupby' => true, + 'usehaving' => true, + 'joinparams' => [ + 'jointype' => 'itemtype_item', + 'condition' => $followup_condition + ], + 'computation' => 'MAX( ' . $DB->quoteName('TABLE.date') . ')', + 'nometa' => true // cannot GROUP_CONCAT a MAX + ]; + + $tab[] = [ 'id' => '27', 'table' => static::getTable(),