Skip to content

Commit

Permalink
fix(formanswer): status update on ticket change
Browse files Browse the repository at this point in the history
  • Loading branch information
btry committed Sep 15, 2023
1 parent 6af1766 commit b4e8948
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 86 deletions.
1 change: 0 additions & 1 deletion hook.php
Original file line number Diff line number Diff line change
Expand Up @@ -489,7 +489,6 @@ function plugin_formcreator_hook_update_ticket(CommonDBTM $item) {
'requester_id' => $requester,
'comment' => addslashes($item->fields['content']),
]);
return;
}

// No issue linked to the ticket,
Expand Down
17 changes: 2 additions & 15 deletions tests/3-unit/PluginFormcreatorFormAnswer.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ public function beforeTestMethod($method) {
case 'testGetTargets':
case 'testGetGeneratedTargets':
case 'testGetAggregatedStatus':
case 'testStatus':
$this->login('glpi', 'glpi');
}
}
Expand Down Expand Up @@ -613,20 +614,6 @@ public function testGetTargets() {
]);
}

public function testUpdateStatus() {
global $CFG_GLPI;

$CFG_GLPI['use_notifications'] = 0;

// Prepare test context
$form = $this->getForm();

$formAnswer = $this->newTestedInstance();
$formAnswer->add([
'plugin_formcreator_forms_id' => $form->getID(),
]);
}

public function testGetGeneratedTargets() {
$form = $this->getForm();
$targets = [];
Expand Down Expand Up @@ -720,7 +707,7 @@ public function testGetAggregatedStatus() {
'status' => CommonITILObject::INCOMING,
]);
$output = $instance->getAggregatedStatus();
$this->integer($output)->isEqualTo(CommonITILObject::ASSIGNED);
$this->integer($output)->isEqualTo(CommonITILObject::INCOMING);

$tickets[0]->update([
'id' => $tickets[0]->getID(),
Expand Down
61 changes: 14 additions & 47 deletions tests/3-unit/PluginFormcreatorIssue.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@
namespace tests\units;
use GlpiPlugin\Formcreator\Tests\CommonTestCase;
use PluginFormcreatorFormAnswer;
use PluginFormcreatorTargetTicket;
use PluginFormcreatorTarget_Actor;
use CommonITILActor;
use RuleAction;
use User;
use Rule;
Expand Down Expand Up @@ -153,7 +156,7 @@ public function providerGetsyncIssuesRequest_simpleFormanswers() {
public function providerGetSyncIssuesRequest_formAnswerWithOneTicket() {
// case 1
$form = $this->getForm();
$targetTicket1 = new \PluginFormcreatorTargetTicket();
$targetTicket1 = new PluginFormcreatorTargetTicket();
$targetTicket1->add([
'plugin_formcreator_forms_id' => $form->getID(),
'name' => 'foo',
Expand Down Expand Up @@ -207,7 +210,7 @@ public function providerGetSyncIssuesRequest_formAnswerWithOneTicket() {
$this->boolean($formAnswer->isNewItem())->isFalse();
$formAnswer->getFromDB($formAnswer->getID());
$ticket2 = array_shift($formAnswer->targetList);
$this->object($ticket)->isInstanceOf(Ticket::getType());
$this->object($ticket2)->isInstanceOf(Ticket::getType());

return [
'formAnswerWithOneTicket' => [
Expand Down Expand Up @@ -241,13 +244,13 @@ public function providerGetSyncIssuesRequest_formAnswerWithOneTicket() {

public function providerGetSyncIssuesRequest_formAnswerWithSeveralTickets() {
$form = $this->getForm();
$targetTicket1 = new \PluginFormcreatorTargetTicket();
$targetTicket1 = new PluginFormcreatorTargetTicket();
$targetTicket1->add([
'plugin_formcreator_forms_id' => $form->getID(),
'name' => 'foo',
]);
$this->boolean($targetTicket1->isNewItem())->isFalse();
$targetTicket2 = new \PluginFormcreatorTargetTicket();
$targetTicket2 = new PluginFormcreatorTargetTicket();
$targetTicket2->add([
'plugin_formcreator_forms_id' => $form->getID(),
'name' => 'bar',
Expand Down Expand Up @@ -277,42 +280,6 @@ public function providerGetSyncIssuesRequest_formAnswerWithSeveralTickets() {
];
}

public function providerGetSyncIssuesRequest_formAnswerWithOneTickets() {
$form = $this->getForm();
$targetTicket1 = new \PluginFormcreatorTargetTicket();
$targetTicket1->add([
'plugin_formcreator_forms_id' => $form->getID(),
'name' => 'foo',
]);
$this->boolean($targetTicket1->isNewItem())->isFalse();

$formAnswer = new PluginFormcreatorFormAnswer();
$formAnswer->add([
'plugin_formcreator_forms_id' => $form->getID(),
]);
$this->boolean($formAnswer->isNewItem())->isFalse();
$formAnswer->getFromDB($formAnswer->getID());

/** @var Ticket */
$ticket = array_pop($formAnswer->targetList);
$this->object($ticket)->isInstanceOf(Ticket::class);
return [
'formAnswerWithOneTickets' => [
'item' => $formAnswer,
'expected' => [
'itemtype' => PluginFormcreatorFormAnswer::getType(),
'items_id' => $ticket->getID(),
'display_id' => 't_' . $ticket->getID(),
'name' => $formAnswer->fields['name'],
'status' => $formAnswer->fields['status'],
'requester_id' => $formAnswer->fields['requester_id'],
'date_creation' => $formAnswer->fields['request_date'],
'date_mod' => $formAnswer->fields['request_date'],
],
],
];
}

public function providerGetSyncIssuesRequest_formanswerUnderValidation() {
$form = $this->getForm([
'validation_required' => PluginFormcreatorForm::VALIDATION_USER,
Expand Down Expand Up @@ -466,29 +433,29 @@ public function providerGetsyncIssuesRequest_validatedTicket() {

public function providerGetSyncIssuesRequest_FormAnswerWithSeveralRequesters() {
$form = $this->getForm();
$targetTicket1 = new \PluginFormcreatorTargetTicket();
$targetTicket1 = new PluginFormcreatorTargetTicket();
$targetTicket1->add([
'plugin_formcreator_forms_id' => $form->getID(),
'name' => 'foo',
]);
$this->boolean($targetTicket1->isNewItem())->isFalse();

$actor1 = new \PluginFormcreatorTarget_Actor();
$actor1 = new PluginFormcreatorTarget_Actor();
$actor1->add([
'itemtype' => $targetTicket1->getType(),
'items_id' => $targetTicket1->getID(),
'actor_role' => \PluginFormcreatorTarget_Actor::ACTOR_TYPE_PERSON,
'actor_type' => \CommonITILActor::REQUESTER,
'actor_role' => PluginFormcreatorTarget_Actor::ACTOR_TYPE_PERSON,
'actor_type' => CommonITILActor::REQUESTER,
'actor_value' => 3,
'use_notification' => '1',
]);
$this->boolean($actor1->isNewItem())->isFalse();
$actor2 = new \PluginFormcreatorTarget_Actor();
$actor2 = new PluginFormcreatorTarget_Actor();
$actor2->add([
'itemtype' => $targetTicket1->getType(),
'items_id' => $targetTicket1->getID(),
'actor_role' => \PluginFormcreatorTarget_Actor::ACTOR_TYPE_PERSON,
'actor_type' => \CommonITILActor::REQUESTER,
'actor_role' => PluginFormcreatorTarget_Actor::ACTOR_TYPE_PERSON,
'actor_type' => CommonITILActor::REQUESTER,
'actor_value' => 5,
'use_notification' => '1',
]);
Expand Down
59 changes: 36 additions & 23 deletions tests/src/CommonTestCase.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,26 @@
<?php

namespace GlpiPlugin\Formcreator\Tests;
use CommonDBTM;
use Entity;
use Session;
use Html;
use DB;
use Auth;
use atoum;
use Profile;
use Ticket;
use Toolbox;
use User;
use ReflectionMethod;
use PluginFormcreatorCondition;
use PluginFormcreatorQuestion;
use PluginFormcreatorSection;
use PluginFormcreatorForm;
use PluginFormcreatorFormAnswer;
use PluginFormcreatorTargetProblem;
use PluginFormcreatorTargetTicket;
use PluginFormcreatorTargetChange;

abstract class CommonTestCase extends atoum
{
Expand Down Expand Up @@ -82,11 +94,11 @@ protected function disableDebug() {
if (isset($_SESSION['glpi_use_mode'])) {
$this->debugMode = $_SESSION['glpi_use_mode'];
}
\Toolbox::setDebugMode(Session::NORMAL_MODE);
Toolbox::setDebugMode(Session::NORMAL_MODE);
}

protected function restoreDebug() {
\Toolbox::setDebugMode($this->debugMode);
Toolbox::setDebugMode($this->debugMode);
}

public function afterTestMethod($method) {
Expand Down Expand Up @@ -140,16 +152,16 @@ protected function getUniqueEmail() {
* @param string $password
* @param string $profileName
* @param integer $entityId
* @return \User
* @return User
*/
protected function getUser($name, $password = 'p@ssw0rd', $profileName = 'Super-Admin', $entityId = 0) {
$profile = new \Profile();
$profile = new Profile();
$profile->getFromDBByRequest([
'name' => $profileName
]);
$this->boolean($profile->isNewItem())->isFalse('Profile not found to create a user');

$user = new \User();
$user = new User();
$user->add([
'name' => $name,
'password' => $password,
Expand Down Expand Up @@ -196,7 +208,7 @@ protected function getForm($input = []) {
if (!isset($input['is_active'])) {
$input['is_active'] = 1;
}
$form = new \PluginFormcreatorForm();
$form = new PluginFormcreatorForm();
$form->add($input);
$this->boolean($form->isNewItem())->isFalse();
$form->getFromDB($form->getID());
Expand All @@ -205,7 +217,7 @@ protected function getForm($input = []) {
}

protected function getSection($input = [], $formInput = []) {
$formFk = \PluginFormcreatorForm::getForeignKeyField();
$formFk = PluginFormcreatorForm::getForeignKeyField();
if (!isset($input[$formFk])) {
$formId = $this->getForm($formInput)->getID();
$input[$formFk] = $formId;
Expand Down Expand Up @@ -238,7 +250,7 @@ protected function getQuestion($input = [], $sectionInput = [], $formInput = [])
'row' => '0',
'col' => '0',
'width' => '4',
'show_rule' => \PluginFormcreatorCondition::SHOW_RULE_ALWAYS,
'show_rule' => PluginFormcreatorCondition::SHOW_RULE_ALWAYS,
'_parameters' => [],
];
$input = array_merge($defaultInput, $input);
Expand All @@ -255,7 +267,7 @@ protected function getQuestion($input = [], $sectionInput = [], $formInput = [])
];
$input['_parameters'] = array_merge($defaultParams, $input['_parameters']);

$question = new \PluginFormcreatorQuestion();
$question = new PluginFormcreatorQuestion();
$question->add($input);
$this->boolean($question->isNewItem())->isFalse(json_encode($_SESSION['MESSAGE_AFTER_REDIRECT'], JSON_PRETTY_PRINT));
$question->getFromDB($question->getID());
Expand All @@ -268,12 +280,12 @@ protected function getTargetTicket($input = []) {
$input['name'] = $this->getUniqueString();
}

$formFk = \PluginFormcreatorForm::getForeignKeyField();
$formFk = PluginFormcreatorForm::getForeignKeyField();
if (!isset($input[$formFk])) {
$input[$formFk] = $this->getForm()->getID();
}

$targetTicket = new \PluginFormcreatorTargetTicket();
$targetTicket = new PluginFormcreatorTargetTicket();
$targetTicket->add($input);
$this->boolean($targetTicket->isNewItem())->isFalse();
$targetTicket->getFromDB($targetTicket->getID());
Expand All @@ -286,23 +298,24 @@ protected function getTargetChange($input = []) {
$input['name'] = $this->getUniqueString();
}

$formFk = \PluginFormcreatorForm::getForeignKeyField();
$formFk = PluginFormcreatorForm::getForeignKeyField();
if (!isset($input[$formFk])) {
$input[$formFk] = $this->getForm()->getID();
}

$targetChange = new \PluginFormcreatorTargetChange();
$targetChange = new PluginFormcreatorTargetChange();
$targetChange->add($input);
$this->boolean($targetChange->isNewItem())->isFalse();
$targetChange->getFromDB($targetChange->getID());

return $targetChange;
}

protected function getFormAnswer(array $input): ?\PluginFormcreatorFormAnswer {
$formAnswer = new \PluginFormcreatorFormAnswer();
protected function getFormAnswer(array $input): ?PluginFormcreatorFormAnswer {
$formAnswer = new PluginFormcreatorFormAnswer();
$formAnswer->add($input);
$this->boolean($formAnswer->isNewItem())->isFalse();
$formAnswer->getFromDB($formAnswer->getID());

return $formAnswer;
}
Expand All @@ -312,12 +325,12 @@ protected function getTargetProblem($input = []) {
$input['name'] = $this->getUniqueString();
}

$formFk = \PluginFormcreatorForm::getForeignKeyField();
$formFk = PluginFormcreatorForm::getForeignKeyField();
if (!isset($input[$formFk])) {
$input[$formFk] = $this->getForm()->getID();
}

$targetProblem = new \PluginFormcreatorTargetProblem();
$targetProblem = new PluginFormcreatorTargetProblem();
$targetProblem->add($input);
$this->boolean($targetProblem->isNewItem())->isFalse();
$targetProblem->getFromDB($targetProblem->getID());
Expand Down Expand Up @@ -373,10 +386,10 @@ protected function getSessionMessage() {
*
* @param string $itemtype itemtype to create
* @param array $input
* @return \CommonDBTM
* @return CommonDBTM
*/
protected function getGlpiCoreItem(string $itemtype, array $input = []): \CommonDBTM {
/** @var \CommonDBTM */
protected function getGlpiCoreItem(string $itemtype, array $input = []): CommonDBTM {
/** @var CommonDBTM */
$item = new $itemtype();

$this->handleDeprecations($itemtype, $input);
Expand All @@ -387,8 +400,8 @@ protected function getGlpiCoreItem(string $itemtype, array $input = []): \Common
if (Session::getLoginUserID(true)) {
$entity = Session::getActiveEntity();
}
if (!isset($input[\Entity::getForeignKeyField()])) {
$input[\Entity::getForeignKeyField()] = $entity;
if (!isset($input[Entity::getForeignKeyField()])) {
$input[Entity::getForeignKeyField()] = $entity;
}
}

Expand Down Expand Up @@ -458,7 +471,7 @@ private function handleDeprecations($itemtype, &$input): void {
* @return mixed
*/
protected function callPrivateMethod($instance, string $methodName, ...$args) {
$method = new \ReflectionMethod($instance, $methodName);
$method = new ReflectionMethod($instance, $methodName);
$method->setAccessible(true);

return $method->invoke($instance, ...$args);
Expand Down

0 comments on commit b4e8948

Please sign in to comment.