From aeb7546086ca9ec8f1e36c82d397a6b5753a779e Mon Sep 17 00:00:00 2001 From: Thierry Bugier Date: Fri, 15 Sep 2023 14:15:38 +0200 Subject: [PATCH] fix(formanswer): status update on ticket change --- hook.php | 2 +- tests/3-unit/PluginFormcreatorFormAnswer.php | 21 ++----- tests/3-unit/PluginFormcreatorIssue.php | 61 +++++--------------- tests/src/CommonTestCase.php | 59 +++++++++++-------- 4 files changed, 57 insertions(+), 86 deletions(-) diff --git a/hook.php b/hook.php index 6dce595e0..b2b418e1b 100644 --- a/hook.php +++ b/hook.php @@ -489,7 +489,7 @@ function plugin_formcreator_hook_update_ticket(CommonDBTM $item) { 'requester_id' => $requester, 'comment' => addslashes($item->fields['content']), ]); - return; + // return; } // No issue linked to the ticket, diff --git a/tests/3-unit/PluginFormcreatorFormAnswer.php b/tests/3-unit/PluginFormcreatorFormAnswer.php index 6b01aa6c2..0c06d7cd4 100644 --- a/tests/3-unit/PluginFormcreatorFormAnswer.php +++ b/tests/3-unit/PluginFormcreatorFormAnswer.php @@ -49,6 +49,10 @@ use PluginFormcreatorTargetChange; use PluginFormcreatorTargetProblem; use Problem; +use Rule; +use RuleCriteria; +use RuleTicket; +use RuleAction; use Session; use PluginFormcreatorForm_Validator; use Ticket; @@ -69,6 +73,7 @@ public function beforeTestMethod($method) { case 'testGetTargets': case 'testGetGeneratedTargets': case 'testGetAggregatedStatus': + case 'testStatus': $this->login('glpi', 'glpi'); } } @@ -613,20 +618,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 = []; @@ -720,7 +711,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(), diff --git a/tests/3-unit/PluginFormcreatorIssue.php b/tests/3-unit/PluginFormcreatorIssue.php index f0f0e455d..075657513 100644 --- a/tests/3-unit/PluginFormcreatorIssue.php +++ b/tests/3-unit/PluginFormcreatorIssue.php @@ -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; @@ -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', @@ -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' => [ @@ -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', @@ -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, @@ -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', ]); diff --git a/tests/src/CommonTestCase.php b/tests/src/CommonTestCase.php index e11c0b2ea..99e29486d 100644 --- a/tests/src/CommonTestCase.php +++ b/tests/src/CommonTestCase.php @@ -1,14 +1,26 @@ 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) { @@ -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, @@ -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()); @@ -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; @@ -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); @@ -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()); @@ -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()); @@ -286,12 +298,12 @@ 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()); @@ -299,10 +311,11 @@ protected function getTargetChange($input = []) { 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; } @@ -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()); @@ -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); @@ -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; } } @@ -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);