|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * --------------------------------------------------------------------- |
| 4 | + * Formcreator is a plugin which allows creation of custom forms of |
| 5 | + * easy access. |
| 6 | + * --------------------------------------------------------------------- |
| 7 | + * LICENSE |
| 8 | + * |
| 9 | + * This file is part of Formcreator. |
| 10 | + * |
| 11 | + * Formcreator is free software; you can redistribute it and/or modify |
| 12 | + * it under the terms of the GNU General Public License as published by |
| 13 | + * the Free Software Foundation; either version 2 of the License, or |
| 14 | + * (at your option) any later version. |
| 15 | + * |
| 16 | + * Formcreator is distributed in the hope that it will be useful, |
| 17 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | + * GNU General Public License for more details. |
| 20 | + * |
| 21 | + * You should have received a copy of the GNU General Public License |
| 22 | + * along with Formcreator. If not, see <http://www.gnu.org/licenses/>. |
| 23 | + * --------------------------------------------------------------------- |
| 24 | + * @copyright Copyright © 2011 - 2021 Teclib' |
| 25 | + * @license http://www.gnu.org/licenses/gpl.txt GPLv3+ |
| 26 | + * @link https://github.com/pluginsGLPI/formcreator/ |
| 27 | + * @link https://pluginsglpi.github.io/formcreator/ |
| 28 | + * @link http://plugins.glpi-project.org/#/plugin/formcreator |
| 29 | + * --------------------------------------------------------------------- |
| 30 | + */ |
| 31 | + |
| 32 | +namespace GlpiPlugin\Formcreator\Tests; |
| 33 | + |
| 34 | +use PluginFormcreatorFormanswer; |
| 35 | + |
| 36 | +abstract class AbstractItilTargetTestCase extends CommonTargetTestCase { |
| 37 | + public function beforeTestMethod($method) { |
| 38 | + parent::beforeTestMethod($method); |
| 39 | + switch ($method) { |
| 40 | + case 'testSetTargetPriority': |
| 41 | + $this->boolean($this->login('glpi', 'glpi'))->isTrue(); |
| 42 | + break; |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + public function setTargetPriorityProvider() { |
| 47 | + $form = $this->getForm(); |
| 48 | + $target = $this->newTestedInstance(); |
| 49 | + $target->add([ |
| 50 | + 'name' => $this->getUniqueString(), |
| 51 | + 'plugin_formcreator_forms_id' => $form->getID(), |
| 52 | + 'urgency_rule' => \PluginFormcreatorTargetTicket::URGENCY_RULE_NONE, |
| 53 | + ]); |
| 54 | + |
| 55 | + yield 'no urgency and no template' => [ |
| 56 | + 'formanswerData' => [ |
| 57 | + $form::getForeignKeyField() => $form->getID(), |
| 58 | + ], |
| 59 | + 'expected' => 3 |
| 60 | + ]; |
| 61 | + |
| 62 | + $testedClassName = $this->getTestedClassName(); |
| 63 | + $section = $this->getSection([ |
| 64 | + $form::getForeignKeyField() => $form->getID(), |
| 65 | + ]); |
| 66 | + $question = $this->getQuestion([ |
| 67 | + 'plugin_formcreator_sections_id' => $section->getID(), |
| 68 | + 'fieldtype' => 'urgency', |
| 69 | + ]); |
| 70 | + $target->update([ |
| 71 | + 'id' => $target->getID(), |
| 72 | + 'urgency_rule' => $testedClassName::URGENCY_RULE_SPECIFIC, |
| 73 | + '_urgency_specific' => $question->getID(), |
| 74 | + ]); |
| 75 | + |
| 76 | + yield 'urgency from question' => [ |
| 77 | + 'formanswerData' => [ |
| 78 | + $form::getForeignKeyField() => $form->getID(), |
| 79 | + 'formcreator_field_' . $question->getID() => '5', |
| 80 | + ], |
| 81 | + 'expected' => 4, // Urgency 5 and impact 3 gives priority 4 with default matrix |
| 82 | + ]; |
| 83 | + |
| 84 | + // Ugly, but GLPI itself does the same internally... |
| 85 | + $templateType = $target->getTargetItemtypeName() . 'Template'; |
| 86 | + $predefinedType = $target->getTargetItemtypeName() . 'TemplatePredefinedField'; |
| 87 | + |
| 88 | + $template = $this->getGlpiCoreItem($templateType, [ |
| 89 | + 'name' => $this->getUniqueString(), |
| 90 | + ]); |
| 91 | + $predefined = new $predefinedType(); |
| 92 | + $predefined->add([ |
| 93 | + $templateType::getForeignKeyField() => $template->getID(), |
| 94 | + 'num' => 11, // Impact search option ID, |
| 95 | + 'value' => '5', // Very high impact |
| 96 | + ]); |
| 97 | + $target->update([ |
| 98 | + 'id' => $target->getID(), |
| 99 | + 'urgency_rule' => $testedClassName::URGENCY_RULE_NONE, |
| 100 | + '_urgency_question' => '0', |
| 101 | + $templateType::getForeignKeyField() => $template->getID(), |
| 102 | + ]); |
| 103 | + |
| 104 | + yield 'impact from template' => [ |
| 105 | + 'formanswerData' => [ |
| 106 | + $form::getForeignKeyField() => $form->getID(), |
| 107 | + ], |
| 108 | + 'expected' => 4, // Urgency 3 and impact 5 gives priority 4 with default matrix |
| 109 | + ]; |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * @dataProvider setTargetPriorityProvider |
| 114 | + */ |
| 115 | + public function testSetTargetPriority($formanswerData, $expected) { |
| 116 | + $formanswer = new PluginFormcreatorFormanswer(); |
| 117 | + $formanswer->add($formanswerData); |
| 118 | + $_SESSION['MESSAGE_AFTER_REDIRECT'] = []; |
| 119 | + $this->boolean($formanswer->isNewItem())->isFalse(json_encode($_SESSION['MESSAGE_AFTER_REDIRECT'], JSON_PRETTY_PRINT)); |
| 120 | + $generatedTarget = $formanswer->targetList[0]; // Assume the target has been generated |
| 121 | + $generatedTarget->fields['priority'] = $expected; |
| 122 | + } |
| 123 | +} |
0 commit comments