From c72901c7216558260beb27fa6a6d5a859cad1540 Mon Sep 17 00:00:00 2001 From: Thierry Bugier Date: Tue, 9 May 2023 11:51:22 +0200 Subject: [PATCH] fix(targetticket): request source better unit tests, and ignore unset request source in user's preferences --- inc/abstractitiltarget.class.php | 5 +++ inc/field/filefield.class.php | 17 ++++----- .../3-unit/PluginFormcreatorTargetTicket.php | 38 ++++++++++++++----- 3 files changed, 42 insertions(+), 18 deletions(-) diff --git a/inc/abstractitiltarget.class.php b/inc/abstractitiltarget.class.php index 050d34d2e..a2f04da4f 100644 --- a/inc/abstractitiltarget.class.php +++ b/inc/abstractitiltarget.class.php @@ -2326,6 +2326,11 @@ public function getDefaultData(PluginFormcreatorFormAnswer $formanswer): array { } $data = array_merge($data, $predefined_fields); + + if (($data['requesttypes_id'] ?? 0) == 0) { + unset($data['requesttypes_id']); + } + return $data; } diff --git a/inc/field/filefield.class.php b/inc/field/filefield.class.php index 229ee2f3e..fc4459336 100644 --- a/inc/field/filefield.class.php +++ b/inc/field/filefield.class.php @@ -238,15 +238,14 @@ private function saveDocument($file, $prefix) { return; } - $doc = new Document(); - $file_data = []; - $file_data["name"] = Toolbox::addslashes_deep($form->getField('name') . ' - ' . $this->question->fields['name']); - $file_data["entities_id"] = isset($_SESSION['glpiactive_entity']) - ? $_SESSION['glpiactive_entity'] - : $form->getField('entities_id'); - $file_data["is_recursive"] = $form->getField('is_recursive'); - $file_data['_filename'] = [$file]; - $file_data['_prefix_filename'] = [$prefix]; + $file_data = [ + 'name' => Toolbox::addslashes_deep($form->fields['name'] . ' - ' . $this->question->fields['name']), + 'entities_id' => $_SESSION['glpiactive_entity'] ?? $form->getField('entities_id'), + 'is_recursive' => $form->getField('is_recursive'), + '_filename' => [$file], + '_prefix_filename' => [$prefix], + ]; + $doc = new Document(); if ($docID = $doc->add($file_data)) { return $docID; } diff --git a/tests/3-unit/PluginFormcreatorTargetTicket.php b/tests/3-unit/PluginFormcreatorTargetTicket.php index b5c83c1e0..352cde6a9 100644 --- a/tests/3-unit/PluginFormcreatorTargetTicket.php +++ b/tests/3-unit/PluginFormcreatorTargetTicket.php @@ -48,6 +48,7 @@ use PluginFormcreatorSection; use Profile; use Profile_User; +use RequestType; use Session; use Supplier_Ticket; use TaskCategory; @@ -77,6 +78,19 @@ public function beforeTestMethod($method) { } } + public function afterTestMethod($method) { + parent::beforeTestMethod($method); + switch ($method) { + case 'testRequestSource': + $requestType = new RequestType(); + $requestType->update([ + 'id' => 1, // Helpdesk + 'is_helpdesk_default' => 1, + ]); + break; + } + } + public function providerGetTypeName() { return [ [ @@ -1732,7 +1746,7 @@ public function providerRequestSource() { $testedClassName = $this->getTestedClassName(); $form = $this->getForm(); - yield [ + yield 'request source is Formcreator' =>[ 'instance' => $this->getTargetTicket([ PluginFormcreatorForm::getForeignKeyField() => $form->getID(), 'source_rule' => $testedClassName::REQUESTSOURCE_FORMCREATOR, @@ -1741,21 +1755,22 @@ public function providerRequestSource() { 'expected' => PluginFormcreatorCommon::getFormcreatorRequestTypeId() ]; + $email_request_source = 2; // e-mail, see table glpi_requesttypes $form = $this->getForm(); $user = $this->getGlpiCoreItem(User::class, [ 'name' => 'user' . $this->getUniqueString(), 'password' => 'password', 'password2' => 'password', - 'default_requesttypes_id' => 2, // e-mail, see table glpi_requesttypes + 'default_requesttypes_id' => $email_request_source, ]); $this->login($user->fields['name'], 'password'); - yield [ + yield 'request source is none; then set by user\'s preference' => [ 'instance' => $this->getTargetTicket([ PluginFormcreatorForm::getForeignKeyField() => $form->getID(), 'source_rule' => $testedClassName::REQUESTSOURCE_NONE ]), - 'expected' => 2 + 'expected' => $email_request_source, ]; $form = $this->getForm(); @@ -1766,13 +1781,18 @@ public function providerRequestSource() { 'default_requesttypes_id' => 0, // unset ]); $this->login($user->fields['name'], 'password'); + $requestType = new RequestType(); + $requestType->update([ + 'id' => 3, // Phone + 'is_helpdesk_default' => 1, + ]); - yield [ + yield 'request source is none; then set by GLPI default' => [ 'instance' => $this->getTargetTicket([ PluginFormcreatorForm::getForeignKeyField() => $form->getID(), 'source_rule' => $testedClassName::REQUESTSOURCE_NONE, ]), - 'expected' => 0 // Unset (see Setup > General > Default values) + 'expected' => 3 // Unset (see Setup > General > Default values) ]; $form = $this->getForm(); @@ -1784,16 +1804,16 @@ public function providerRequestSource() { $this->getGlpiCoreItem(TicketTemplatePredefinedField::getType(), [ 'tickettemplates_id' => $ticketTemplate->getID(), 'num' => 9, // RequestType - 'value' => 1, // Helpdesk + 'value' => 4, // Direct ]); - yield [ + yield 'request source is none; then set by target\'s template' => [ 'instance' => $this->getTargetTicket([ PluginFormcreatorForm::getForeignKeyField() => $form->getID(), 'source_rule' => $testedClassName::REQUESTSOURCE_NONE, 'tickettemplates_id' => $ticketTemplate->getID(), ]), - 'expected' => 1 // Helpdesk (see Setup > General > Default values) + 'expected' => 4 // Helpdesk (see Setup > General > Default values) ]; }