diff --git a/inc/abstracttarget.class.php b/inc/abstracttarget.class.php index 1309b7355..f1b7e2777 100644 --- a/inc/abstracttarget.class.php +++ b/inc/abstracttarget.class.php @@ -2473,4 +2473,30 @@ public function getDefaultData(PluginFormcreatorFormAnswer $formanswer): array { $data = array_merge($data, $predefined_fields); return $data; } + + /** + * get all target problems for a form + * + * @param int $formId + * @return array + */ + public function getTargetsForForm($formId) { + global $DB; + + $targets = []; + $rows = $DB->request([ + 'SELECT' => ['id'], + 'FROM' => static::getTable(), + 'WHERE' => [ + 'plugin_formcreator_forms_id' => $formId + ], + ]); + foreach ($rows as $row) { + $target = new static(); + $target->getFromDB($row['id']); + $targets[$row['id']] = $target; + } + + return $targets; + } } diff --git a/inc/form.class.php b/inc/form.class.php index 6cbe9ca06..87069404f 100644 --- a/inc/form.class.php +++ b/inc/form.class.php @@ -2238,7 +2238,8 @@ public function getFields() : array { public static function getTargetTypes() : array { return [ PluginFormcreatorTargetTicket::class, - PluginFormcreatorTargetChange::class + PluginFormcreatorTargetChange::class, + PluginFormcreatorTargetProblem::class, ]; } diff --git a/inc/targetchange.class.php b/inc/targetchange.class.php index 8009bf610..d6484cdcd 100644 --- a/inc/targetchange.class.php +++ b/inc/targetchange.class.php @@ -901,30 +901,4 @@ public function save(PluginFormcreatorFormAnswer $formanswer) { return $change; } - - /** - * get all target changes for a form - * - * @param int $formId - * @return array - */ - public function getTargetsForForm($formId) { - global $DB; - - $targets = []; - $rows = $DB->request([ - 'SELECT' => ['id'], - 'FROM' => self::getTable(), - 'WHERE' => [ - 'plugin_formcreator_forms_id' => $formId - ], - ]); - foreach ($rows as $row) { - $target = new self(); - $target->getFromDB($row['id']); - $targets[$row['id']] = $target; - } - - return $targets; - } } diff --git a/inc/targetticket.class.php b/inc/targetticket.class.php index 0e9e68b1a..ae889f07e 100644 --- a/inc/targetticket.class.php +++ b/inc/targetticket.class.php @@ -1543,30 +1543,4 @@ private function saveAssociatedItems($input) { unset($input['items_id']); return $input; } - - /** - * get all target tickets for a form - * - * @param int $formId - * @return array - */ - public function getTargetsForForm($formId) { - global $DB; - - $targets = []; - $rows = $DB->request([ - 'SELECT' => ['id'], - 'FROM' => self::getTable(), - 'WHERE' => [ - 'plugin_formcreator_forms_id' => $formId - ], - ]); - foreach ($rows as $row) { - $target = new self(); - $target->getFromDB($row['id']); - $targets[$row['id']] = $target; - } - - return $targets; - } } diff --git a/install/mysql/plugin_formcreator_empty.sql b/install/mysql/plugin_formcreator_empty.sql index e2941e609..68fa23c42 100644 --- a/install/mysql/plugin_formcreator_empty.sql +++ b/install/mysql/plugin_formcreator_empty.sql @@ -217,6 +217,31 @@ CREATE TABLE IF NOT EXISTS `glpi_plugin_formcreator_targettickets` ( INDEX `tickettemplates_id` (`tickettemplates_id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; +CREATE TABLE IF NOT EXISTS `glpi_plugin_formcreator_targetproblems` ( + `id` int(11) NOT NULL AUTO_INCREMENT, + `name` varchar(255) NOT NULL DEFAULT '', + `plugin_formcreator_forms_id` int(11) NOT NULL DEFAULT '0', + `target_name` varchar(255) NOT NULL DEFAULT '', + `problemtemplates_id` int(11) NOT NULL DEFAULT '0', + `content` longtext, + `impactcontent` longtext, + `causecontent` longtext, + `symptomcontent` longtext, + `urgency_rule` int(11) NOT NULL DEFAULT '1', + `urgency_question` int(11) NOT NULL DEFAULT '0', + `destination_entity` int(11) NOT NULL DEFAULT '1', + `destination_entity_value` int(11) DEFAULT NULL, + `tag_type` int(11) NOT NULL DEFAULT '1', + `tag_questions` varchar(255) NOT NULL, + `tag_specifics` varchar(255) NOT NULL, + `category_rule` int(11) NOT NULL DEFAULT '1', + `category_question` int(11) NOT NULL DEFAULT '0', + `show_rule` int(11) NOT NULL DEFAULT '1', + `uuid` varchar(255) DEFAULT NULL, + PRIMARY KEY (`id`), + INDEX `problemtemplates_id` (`problemtemplates_id`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; + CREATE TABLE IF NOT EXISTS `glpi_plugin_formcreator_targets_actors` ( `id` int(11) NOT NULL AUTO_INCREMENT, `itemtype` varchar(255) DEFAULT NULL, diff --git a/tests/2-integration/PluginFormcreatorIssue.php b/tests/2-integration/PluginFormcreatorIssue.php index fe9ab7c5e..6c2ebc105 100644 --- a/tests/2-integration/PluginFormcreatorIssue.php +++ b/tests/2-integration/PluginFormcreatorIssue.php @@ -33,8 +33,24 @@ class PluginFormcreatorIssue extends CommonTestCase { + public function beforeTestMethod($method) { + switch ($method) { + case 'testAddTicket': + case 'testUpdateTicket': + $this->login('post-only', 'postonly'); + break; + + case 'testDeleteTicket': + $this->login('glpi', 'glpi'); + break; + } + } + public function testAddTicket() { - $this->login('post-only', 'postonly'); + global $CFG_GLPI; + + $CFG_GLPI['use_notifications'] = '0'; + // Create a form with a target ticket $form = $this->getForm(); $this->getTargetTicket([ @@ -43,7 +59,10 @@ public function testAddTicket() { // answer the form $formAnswer = new \PluginFormcreatorFormAnswer(); - $formAnswer->add([\PluginFormcreatorForm::getForeignKeyField() => $form->getID()]); + $formAnswer->add([ + \PluginFormcreatorForm::getForeignKeyField() => $form->getID() + ]); + // Get the generated ticket $ticket = array_pop($formAnswer->targetList); $this->object($ticket); @@ -63,7 +82,10 @@ public function testAddTicket() { } public function testUpdateTicket() { - $this->login('post-only', 'postonly'); + global $CFG_GLPI; + + $CFG_GLPI['use_notifications'] = '0'; + // Create a form with a target ticket $form = $this->getForm(); $this->getTargetTicket([ @@ -104,7 +126,10 @@ public function testUpdateTicket() { } public function testDeleteTicket() { - $this->login('glpi', 'glpi'); + global $CFG_GLPI; + + $CFG_GLPI['use_notifications'] = '0'; + $form = $this->getForm(); $this->getTargetTicket([ \PluginFormcreatorForm::getForeignKeyField() => $form->getID(), diff --git a/tests/2-integration/PluginFormcreatorTargetChange.php b/tests/2-integration/PluginFormcreatorTargetChange.php index 9f700a4b7..b74887d64 100644 --- a/tests/2-integration/PluginFormcreatorTargetChange.php +++ b/tests/2-integration/PluginFormcreatorTargetChange.php @@ -44,30 +44,30 @@ public function testTargetChangeActors() { // Create a form with a target change $form = $this->getForm(); - $targetChange = new \PluginFormcreatorTargetChange(); - $targetChange->add([ + $instance = new \PluginFormcreatorTargetChange(); + $instance->add([ 'name' => 'a target', 'plugin_formcreator_forms_id' => $form->getID() ]); - $this->boolean($targetChange->isNewItem())->isFalse(); + $this->boolean($instance->isNewItem())->isFalse(); $requesterActor = new \PluginFormcreatorTarget_Actor(); $observerActor = new \PluginFormcreatorTarget_Actor(); - $targetChangeId = $targetChange->getID(); + $instanceId = $instance->getID(); // find the actors created by default $requesterActor->getFromDBByCrit([ 'AND' => [ - 'itemtype' => $targetChange->getType(), - 'items_id' => $targetChangeId, + 'itemtype' => $instance->getType(), + 'items_id' => $instanceId, 'actor_role' => \PluginFormcreatorTarget_Actor::ACTOR_ROLE_REQUESTER, 'actor_type' => \PluginFormcreatorTarget_Actor::ACTOR_TYPE_AUTHOR, ] ]); $observerActor->getFromDBByCrit([ 'AND' => [ - 'itemtype' => $targetChange->getType(), - 'items_id' => $targetChangeId, + 'itemtype' => $instance->getType(), + 'items_id' => $instanceId, 'actor_role' => \PluginFormcreatorTarget_Actor::ACTOR_ROLE_OBSERVER, 'actor_type' => \PluginFormcreatorTarget_Actor::ACTOR_TYPE_VALIDATOR ] diff --git a/tests/2-integration/PluginFormcreatorTargetTicket.php b/tests/2-integration/PluginFormcreatorTargetTicket.php index ef785d00b..01c927a80 100644 --- a/tests/2-integration/PluginFormcreatorTargetTicket.php +++ b/tests/2-integration/PluginFormcreatorTargetTicket.php @@ -46,31 +46,31 @@ public function testTargetTicketActors() { // Create a form with a target ticket $form = $this->getForm(); - $targetTicket = new \PluginFormcreatorTargetTicket(); - $targetTicket->add([ + $instance = new \PluginFormcreatorTargetTicket(); + $instance->add([ 'name' => 'a target', 'plugin_formcreator_forms_id' => $form->getID() ]); - $targetTicket->getFromDB($targetTicket->getID()); - $this->boolean($targetTicket->isNewItem())->isFalse(); + $instance->getFromDB($instance->getID()); + $this->boolean($instance->isNewItem())->isFalse(); // find the actors created by default $requesterActor = new \PluginFormcreatorTarget_Actor(); $observerActor = new \PluginFormcreatorTarget_Actor(); - $targetTicketId = $targetTicket->getID(); + $instanceId = $instance->getID(); $requesterActor->getFromDBByCrit([ 'AND' => [ - 'itemtype' => $targetTicket->getType(), - 'items_id' => $targetTicketId, + 'itemtype' => $instance->getType(), + 'items_id' => $instanceId, 'actor_role' => \PluginFormcreatorTarget_Actor::ACTOR_ROLE_REQUESTER, 'actor_type' => \PluginFormcreatorTarget_Actor::ACTOR_TYPE_AUTHOR, ] ]); $observerActor->getFromDBByCrit([ 'AND' => [ - 'itemtype' => $targetTicket->getType(), - 'items_id' => $targetTicketId, + 'itemtype' => $instance->getType(), + 'items_id' => $instanceId, 'actor_role' => \PluginFormcreatorTarget_Actor::ACTOR_ROLE_OBSERVER, 'actor_type' => \PluginFormcreatorTarget_Actor::ACTOR_TYPE_VALIDATOR ] diff --git a/tests/src/CommonTestCase.php b/tests/src/CommonTestCase.php index f6eec882b..9af86d87e 100644 --- a/tests/src/CommonTestCase.php +++ b/tests/src/CommonTestCase.php @@ -294,6 +294,22 @@ protected function getFormAnswer(array $input): ?\PluginFormcreatorFormAnswer { return $formAnswer; } + protected function getTargetProblem($input = []) { + if (!isset($input['name'])) { + $input['name'] = $this->getUniqueString(); + } + + $formFk = \PluginFormcreatorForm::getForeignKeyField(); + if (!isset($input[$formFk])) { + $input[$formFk] = $this->getForm()->getID(); + } + + $targetProblem = new \PluginFormcreatorTargetProblem(); + $targetProblem->add($input); + + return $targetProblem; + } + /** * Tests the session has a specific message * this may be replaced by a custom asserter for atoum