|
31 | 31 | namespace tests\units;
|
32 | 32 | use GlpiPlugin\Formcreator\Tests\CommonTestCase;
|
33 | 33 | use PluginFormcreatorForm_Validator;
|
| 34 | +use PluginFormcreatorIssue; |
34 | 35 | use Search;
|
35 | 36 | use TicketValidation;
|
| 37 | +use Ticket; |
36 | 38 |
|
37 | 39 | /**
|
38 | 40 | * The methods conflict when running in parallel
|
@@ -304,4 +306,172 @@ public function testGetMyLastAnswersAsValidator() {
|
304 | 306 | $this->boolean(in_array($id, $formAnswers))->isTrue();
|
305 | 307 | }
|
306 | 308 | }
|
| 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 | + } |
307 | 477 | }
|
0 commit comments