Skip to content

Commit

Permalink
fix(issue): answers with multiple tickets
Browse files Browse the repository at this point in the history
Signed-off-by: Thierry Bugier <tbugier@teclib.com>
  • Loading branch information
btry committed Oct 14, 2021
1 parent 677e2b8 commit c77be76
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 15 deletions.
6 changes: 4 additions & 2 deletions hook.php
Original file line number Diff line number Diff line change
Expand Up @@ -333,11 +333,12 @@ function plugin_formcreator_hook_add_ticket(CommonDBTM $item) {

$validationStatus = PluginFormcreatorCommon::getTicketStatusForIssue($item);

$issueName = $item->fields['name'] != '' ? addslashes($item->fields['name']) : '(' . $item->getID() . ')';
$issue = new PluginFormcreatorIssue();
$issue->add([
'items_id' => $item->getID(),
'itemtype' => 'Ticket',
'name' => addslashes($item->fields['name']),
'name' => $issueName,
'status' => $validationStatus['status'],
'date_creation' => $item->fields['date'],
'date_mod' => $item->fields['date_mod'],
Expand All @@ -358,6 +359,7 @@ function plugin_formcreator_hook_update_ticket(CommonDBTM $item) {

$validationStatus = PluginFormcreatorCommon::getTicketStatusForIssue($item);

$issueName = $item->fields['name'] != '' ? addslashes($item->fields['name']) : '(' . $item->getID() . ')';
$issue = new PluginFormcreatorIssue();
$issue->getFromDBByCrit([
'AND' => [
Expand All @@ -370,7 +372,7 @@ function plugin_formcreator_hook_update_ticket(CommonDBTM $item) {
'items_id' => $id,
'display_id' => "t_$id",
'itemtype' => 'Ticket',
'name' => addslashes($item->fields['name']),
'name' => $issueName,
'status' => $validationStatus['status'],
'date_creation' => $item->fields['date'],
'date_mod' => $item->fields['date_mod'],
Expand Down
12 changes: 8 additions & 4 deletions inc/formanswer.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -1239,10 +1239,11 @@ private function createIssue() {
if ($rows->count() != 1) {
// There are several tickets for this form answer
// The issue must be created from this form answer
$issueName = $this->fields['name'] != '' ? addslashes($this->fields['name']) : '(' . $this->getID() . ')';
$issue->add([
'items_id' => $this->getID(),
'itemtype' => PluginFormcreatorFormAnswer::class,
'name' => addslashes($this->fields['name']),
'name' => $issueName,
'status' => $this->fields['status'],
'date_creation' => $this->fields['request_date'],
'date_mod' => $this->fields['request_date'],
Expand Down Expand Up @@ -1280,10 +1281,11 @@ private function createIssue() {
$validationStatus = PluginFormcreatorCommon::getTicketStatusForIssue($ticket);

$ticketUserRow = array_pop($ticketUserRow);
$issueName = $ticket->fields['name'] != '' ? addslashes($ticket->fields['name']) : '(' . $ticket->getID() . ')';
$issue->add([
'items_id' => $ticketId,
'itemtype' => Ticket::class,
'name' => addslashes($ticket->fields['name']),
'name' => $issueName,
'status' => $validationStatus['status'],
'date_creation' => $ticket->fields['date'],
'date_mod' => $ticket->fields['date_mod'],
Expand Down Expand Up @@ -1333,11 +1335,12 @@ private function updateIssue() {
'items_id' => $this->getID()
]
]);
$issueName = $this->fields['name'] != '' ? addslashes($this->fields['name']) : '(' . $this->getID() . ')';
$issue->update([
'id' => $issue->getID(),
'items_id' => $this->getID(),
'itemtype' => PluginFormcreatorFormAnswer::class,
'name' => addslashes($this->fields['name']),
'name' => $issueName,
'status' => $this->fields['status'],
'date_creation' => $this->fields['request_date'],
'date_mod' => $this->fields['request_date'],
Expand Down Expand Up @@ -1377,11 +1380,12 @@ private function updateIssue() {
'items_id' => $this->getID()
]
]);
$issueName = $ticket->fields['name'] != '' ? addslashes($ticket->fields['name']) : '(' . $ticket->getID() . ')';
$issue->update([
'id' => $issue->getID(),
'items_id' => $ticketId,
'itemtype' => Ticket::class,
'name' => addslashes($ticket->fields['name']),
'name' => $issueName,
'status' => $ticket->fields['status'],
'date_creation' => $ticket->fields['date'],
'date_mod' => $ticket->fields['date_mod'],
Expand Down
34 changes: 26 additions & 8 deletions inc/issue.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,12 @@ public static function getSyncIssuesRequest() : AbstractQuery {
// The columns status of the 2nd part of the UNNION statement
// must match the same logic as PluginFormcreatorCommon::getTicketStatusForIssue()
// @see PluginFormcreatorCommon::getTicketStatusForIssue()
$query1 = new QuerySubQuery([

// assistance requests having no or deveral tickets
$query1 = [
'SELECT' => [
new QueryExpression('NULL as `id`'),
$formTable => ['name as name'],
new QueryExpression("IF(`$formAnswerTable`.`name` = '', CONCAT('(', `$formAnswerTable`.`id`, ')'), `$formAnswerTable`.`name`) as `name`"),
new QueryExpression("CONCAT('f_', `$formAnswerTable`.`id`) as `display_id`"),
"$formAnswerTable.id as items_id",
new QueryExpression("'" . PluginFormcreatorFormAnswer::getType() . "' as `itemtype`"),
Expand Down Expand Up @@ -126,15 +128,16 @@ public static function getSyncIssuesRequest() : AbstractQuery {
],
'GROUPBY' => ["$formAnswerTable.id"],
'HAVING' => new QueryExpression("COUNT(`$itemTicketTable`.`$ticketFk`) != 1"),
]);
];

// tickets not generated by Formcreator
$ticketTable = Ticket::getTable();
$ticketValidationTable = TicketValidation::getTable();
$ticketUserTable = Ticket_User::getTable();
$query2 = new QuerySubquery([
$query2 = [
'SELECT' => [
new QueryExpression('NULL as `id`'),
"$ticketTable.name as name",
new QueryExpression("IF(`$ticketTable`.`name` = '', CONCAT('(', `$ticketTable`.`id`, ')'), `$ticketTable`.`name`) as `name`"),
new QueryExpression("CONCAT('t_', `$ticketTable`.`id`) as `display_id`"),
"$ticketTable.id as items_id",
new QueryExpression("'" . Ticket::getType() . "' as `itemtype`"),
Expand Down Expand Up @@ -204,10 +207,25 @@ public static function getSyncIssuesRequest() : AbstractQuery {
"$ticketTable.is_deleted" => 0,
],
'GROUPBY' => ["$ticketTable.id"],
'HAVING' => new QueryExpression("COUNT(`$itemTicketTable`.`items_id`) <= 1")
]);
'HAVING' => new QueryExpression("COUNT(`$itemTicketTable`.`items_id`) = 0")
];

// assistance requests having only one generated ticket (use query2)
$query3 = $query2;
// replace LEFT JOIN with INNER JOIN to exclude tickets not linked to a Formanswer object
$query3['INNER JOIN'][$itemTicketTable] = $query3['LEFT JOIN'][$itemTicketTable];
unset($query3['LEFT JOIN'][$itemTicketTable]);
// Only 1 relation to a Formanswer object
$query3['GROUPBY'] = ["$itemTicketTable.items_id"];
$query3['HAVING'] = new QueryExpression("COUNT(`$itemTicketTable`.`items_id`) = 1");

// Union of the 3 previous queries
$union = new QueryUnion([
new QuerySubquery($query1),
new QuerySubquery($query2),
new QuerySubquery($query3)
], true);

$union = new QueryUnion([$query1, $query2], true);
return $union;
}

Expand Down
81 changes: 80 additions & 1 deletion tests/3-unit/PluginFormcreatorIssue.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,26 @@ public function beforeTestMethod($method) {
public function providerGetsyncIssuesRequest_simpleTicket() {
$ticket = new \Ticket();
$ticket->add([
'name' => 'a ticket',
'name' => 'simple ticket',
'content' => 'foo',
'status' => \Ticket::INCOMING
]);
$this->boolean($ticket->isNewItem())->isFalse();
$ticket->getFromDB($ticket->getID());

$ticket2 = new \Ticket();
$ticket2->add([
'name' => '',
'content' => 'foo',
'status' => \Ticket::INCOMING
]);
$this->boolean($ticket2->isNewItem())->isFalse();
$ticket2->getFromDB($ticket2->getID());
$ticket2->update([
'id' => $ticket2->getID(),
'name' => '',
]);

return [
'simpleTicket' => [
'item' => $ticket,
Expand All @@ -70,6 +83,21 @@ public function providerGetsyncIssuesRequest_simpleTicket() {
'groups_id_validator' => '0',
],
],
'simpleTicket_without_name' => [
'item' => $ticket2,
'expected' => [
'itemtype' => \Ticket::getType(),
'items_id' => $ticket2->getID(),
'display_id' => 't_' . $ticket2->getID(),
'name' => '(' . $ticket2->getID() . ')',
'status' => $ticket2->fields['status'],
'requester_id' => $ticket2->fields['users_id_recipient'],
'date_creation' => $ticket2->fields['date'],
'date_mod' => $ticket2->fields['date_mod'],
'users_id_validator' => '0',
'groups_id_validator' => '0',
]
]
];
}

Expand All @@ -81,6 +109,7 @@ public function providerGetsyncIssuesRequest_simpleFormanswers() {
]);
$this->boolean($formAnswer->isNewItem())->isFalse();
$formAnswer->getFromDB($formAnswer->getID());

return [
'simpleFormanswers' => [
'item' => $formAnswer,
Expand Down Expand Up @@ -420,6 +449,56 @@ public function testGetSyncIssuesRequest($item, $expected) {
foreach ($expected as $key => $field) {
$this->variable($row[$key])->isEqualTo($field, "mismatch in field '$key'");
}

// Test there are no other rows matching the form answer or ticket
if ($item->getType() == \Ticket::class) {
$unwantedItems = $DB->request([
'SELECT' => ['items_id'],
'FROM' => \Item_Ticket::getTable(),
'WHERE' => [
'itemtype' => \PluginFormcreatorFormAnswer::getType(),
'tickets_id' => $item->getID(),
],
]);
if (count($unwantedItems) > 0) {
$unwantedWhere = [
'itemtype' => \PluginFormcreatorFormAnswer::getType(),
];
foreach ($unwantedItems as $row) {
$unwantedWhere['items_id'][] = $row['items_id'];
}
// WHERE itemtype = 'PluginFormcreatorFormAnswer' AND items_id IN ( <list of numbers> )
$result = $DB->request([
'FROM' => $request,
'WHERE' => $unwantedWhere,
]);
$this->integer(count($result))->isEqualTo(0);
}
}
if ($item->getType() == \PluginFormcreatorFormAnswer::class) {
$unwantedItems = $DB->request([
'SELECT' => ['tickets_id'],
'FROM' => \Item_Ticket::getTable(),
'WHERE' => [
'itemtype' => \PluginFormcreatorFormAnswer::getType(),
'items_id' => $item->getID(),
],
]);
if (count($unwantedItems) > 0) {
$unwantedWhere = [
'itemtype' => \Ticket::getType(),
];
foreach ($unwantedItems as $row) {
$unwantedWhere['items_id'][] = $row['tickets_id'];
}
// WHERE itemtype = 'Ticket' AND items_id IN ( <list of numbers> )
$result = $DB->request([
'FROM' => $request,
'WHERE' => $unwantedWhere,
]);
$this->integer(count($result))->isEqualTo(0);
}
}
}

public function testUpdateDateModOnNewFollowup() {
Expand Down

0 comments on commit c77be76

Please sign in to comment.