Skip to content

Commit 3cf3e4e

Browse files
committed
fix: restoring ticket may create inconsistency in DB
1 parent bda43df commit 3cf3e4e

File tree

2 files changed

+180
-2
lines changed

2 files changed

+180
-2
lines changed

hook.php

+10-2
Original file line numberDiff line numberDiff line change
@@ -527,7 +527,8 @@ function plugin_formcreator_hook_delete_ticket(CommonDBTM $item) {
527527
}
528528
}
529529

530-
// Delete the issue
530+
// Delete the issue associated to the ticlet
531+
// (when a form generated one and only one ticket)
531532
// TODO: add is_deleted column to issue ?
532533
$issue = new PluginFormcreatorIssue();
533534
$issue->deleteByCriteria([
@@ -539,7 +540,14 @@ function plugin_formcreator_hook_delete_ticket(CommonDBTM $item) {
539540
function plugin_formcreator_hook_restore_ticket(CommonDBTM $item) {
540541
$formAnswer = new PluginFormcreatorFormAnswer();
541542
if ($formAnswer->getFromDbByTicket($item)) {
542-
$formAnswer->createIssue();
543+
$relations = (new Item_Ticket())->find([
544+
'itemtype' => $formAnswer->getType(),
545+
'items_id' => $formAnswer->getID(),
546+
]);
547+
if (count($relations) === 1) {
548+
// Recreate the issue when one and only one ticket has been created by the form
549+
$formAnswer->createIssue();
550+
}
543551
$minimalStatus = $formAnswer->getAggregatedStatus();
544552
if ($minimalStatus !== null) {
545553
$formAnswer->updateStatus($minimalStatus);

tests/2-integration/PluginFormcreatorFormAnswer.php

+170
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,10 @@
3131
namespace tests\units;
3232
use GlpiPlugin\Formcreator\Tests\CommonTestCase;
3333
use PluginFormcreatorForm_Validator;
34+
use PluginFormcreatorIssue;
3435
use Search;
3536
use TicketValidation;
37+
use Ticket;
3638

3739
/**
3840
* The methods conflict when running in parallel
@@ -304,4 +306,172 @@ public function testGetMyLastAnswersAsValidator() {
304306
$this->boolean(in_array($id, $formAnswers))->isTrue();
305307
}
306308
}
309+
310+
/**
311+
* Undocumented function
312+
*
313+
* @return void
314+
*/
315+
public function testDeleteTicket() {
316+
$form = $this->getForm();
317+
318+
$targetTicket1 = $this->getTargetTicket([
319+
$form::getForeignKeyField() => $form->getID(),
320+
]);
321+
$formAnswer = $this->newTestedInstance();
322+
$formAnswer->add([
323+
'plugin_formcreator_forms_id' => $form->getID(),
324+
]);
325+
$this->boolean($formAnswer->isNewItem())->isFalse();
326+
$this->array($formAnswer->targetList)->hasSize(1);
327+
/** @var Ticket */
328+
$ticket = $formAnswer->targetList[0];
329+
$issue = new PluginFormcreatorIssue();
330+
$issue->getFromDbByCrit([
331+
'itemtype' => $ticket::getType(),
332+
'items_id' => $ticket->getID(),
333+
]);
334+
$this->boolean($issue->isNewItem())->isFalse();
335+
336+
$ticket->delete([
337+
'id' => $ticket->getID(),
338+
]);
339+
340+
// Test the issue has been deleted
341+
$issue = new PluginFormcreatorIssue();
342+
$issue->getFromDbByCrit([
343+
'itemtype' => $ticket::getType(),
344+
'items_id' => $ticket->getID(),
345+
]);
346+
$this->boolean($issue->isNewItem())->isTrue();
347+
348+
// Add a 2nd ttarget ticket to the form
349+
$targetTicket2 = $this->getTargetTicket([
350+
$form::getForeignKeyField() => $form->getID(),
351+
]);
352+
353+
$formAnswer = $this->newTestedInstance();
354+
$formAnswer->add([
355+
'plugin_formcreator_forms_id' => $form->getID(),
356+
]);
357+
$this->boolean($formAnswer->isNewItem())->isFalse();
358+
$this->array($formAnswer->targetList)->hasSize(2);
359+
$ticket = $formAnswer->targetList[0];
360+
$issue = new PluginFormcreatorIssue();
361+
$issue->getFromDbByCrit([
362+
'itemtype' => $formAnswer::getType(),
363+
'items_id' => $formAnswer->getID(),
364+
]);
365+
$this->boolean($issue->isNewItem())->isFalse();
366+
367+
$ticket->delete([
368+
'id' => $ticket->getID(),
369+
]);
370+
371+
// Test the issue still exists
372+
$issue = new PluginFormcreatorIssue();
373+
$issue->getFromDbByCrit([
374+
'itemtype' => $formAnswer::getType(),
375+
'items_id' => $formAnswer->getID(),
376+
]);
377+
$this->boolean($issue->isNewItem())->isFalse();
378+
}
379+
380+
/**
381+
* Undocumented function
382+
*
383+
* @return void
384+
*/
385+
public function testRestoreTicket() {
386+
$form = $this->getForm();
387+
388+
$targetTicket = $this->getTargetTicket([
389+
$form::getForeignKeyField() => $form->getID(),
390+
]);
391+
$formAnswer = $this->newTestedInstance();
392+
$formAnswer->add([
393+
'plugin_formcreator_forms_id' => $form->getID(),
394+
]);
395+
$this->boolean($formAnswer->isNewItem())->isFalse();
396+
$this->array($formAnswer->targetList)->hasSize(1);
397+
/** @var Ticket */
398+
$ticket = $formAnswer->targetList[0];
399+
$issue = new PluginFormcreatorIssue();
400+
$issue->getFromDbByCrit([
401+
'itemtype' => $ticket::getType(),
402+
'items_id' => $ticket->getID(),
403+
]);
404+
$this->boolean($issue->isNewItem())->isFalse();
405+
406+
$ticket->delete([
407+
'id' => $ticket->getID(),
408+
]);
409+
410+
// Test the issue has been deleted
411+
$issue = new PluginFormcreatorIssue();
412+
$issue->getFromDbByCrit([
413+
'itemtype' => $ticket::getType(),
414+
'items_id' => $ticket->getID(),
415+
]);
416+
$this->boolean($issue->isNewItem())->isTrue();
417+
418+
// Restore the ticket (triggers plugin's hook)
419+
$ticket->restore([
420+
'id' => $ticket->getID(),
421+
]);
422+
423+
// Test the issue has been recreated
424+
$issue = new PluginFormcreatorIssue();
425+
$issue->getFromDbByCrit([
426+
'itemtype' => $ticket::getType(),
427+
'items_id' => $ticket->getID(),
428+
]);
429+
$this->boolean($issue->isNewItem())->isFalse();
430+
431+
// Add a 2nd ttarget ticket to the form
432+
$targetTicket2 = $this->getTargetTicket([
433+
$form::getForeignKeyField() => $form->getID(),
434+
]);
435+
436+
$formAnswer = $this->newTestedInstance();
437+
$formAnswer->add([
438+
'plugin_formcreator_forms_id' => $form->getID(),
439+
]);
440+
$this->boolean($formAnswer->isNewItem())->isFalse();
441+
$this->array($formAnswer->targetList)->hasSize(2);
442+
$ticket = $formAnswer->targetList[0];
443+
$issue = new PluginFormcreatorIssue();
444+
$issue->getFromDbByCrit([
445+
'itemtype' => $formAnswer::getType(),
446+
'items_id' => $formAnswer->getID(),
447+
]);
448+
$this->boolean($issue->isNewItem())->isFalse();
449+
450+
$ticket->delete([
451+
'id' => $ticket->getID(),
452+
]);
453+
454+
// Test the issue still exists
455+
$issue = new PluginFormcreatorIssue();
456+
$issue->getFromDbByCrit([
457+
'itemtype' => $formAnswer::getType(),
458+
'items_id' => $formAnswer->getID(),
459+
]);
460+
$this->boolean($issue->isNewItem())->isFalse();
461+
462+
// Restore the ticket (triggers plugin's hook)
463+
$ticket->restore([
464+
'id' => $ticket->getID(),
465+
]);
466+
467+
// Test there is still only 1 issue
468+
$issue = new PluginFormcreatorIssue();
469+
$issue->getFromDbByCrit([
470+
'itemtype' => $formAnswer::getType(),
471+
'items_id' => $formAnswer->getID(),
472+
]);
473+
// If no issue or several issues matches the previous request,
474+
// then the issue is not populated from DB
475+
$this->boolean($issue->isNewItem())->isFalse();
476+
}
307477
}

0 commit comments

Comments
 (0)