@@ -45,6 +45,7 @@ public function beforeTestMethod($method) {
4545 case 'testDeleteLinkedTickets ' :
4646 case 'testSetTargetAssociatedItem ' :
4747 case 'testSetTargetLocation ' :
48+ case 'testSetRequestSource ' :
4849 $ this ->boolean ($ this ->login ('glpi ' , 'glpi ' ))->isTrue ();
4950 break ;
5051 }
@@ -77,6 +78,23 @@ public function testGetTypeName($number, $expected) {
7778 $ this ->string ($ output )->isEqualTo ($ expected );
7879 }
7980
81+ public function testGetEnumRequestTypeRule (): void {
82+ $ output = \PluginFormcreatorTargetTicket::getEnumRequestTypeRule ();
83+ $ this ->array ($ output )->isEqualTo ([
84+ \PluginFormcreatorTargetTicket::REQUESTTYPE_NONE => 'Default or from a template ' ,
85+ \PluginFormcreatorTargetTicket::REQUESTTYPE_SPECIFIC => "Specific type " ,
86+ \PluginFormcreatorTargetTicket::REQUESTTYPE_ANSWER => "Equals to the answer to the question " ,
87+ ]);
88+ }
89+
90+ public function testGetEnumRequestSourceRule (): void {
91+ $ output = \PluginFormcreatorTargetTicket::getEnumRequestSourceRule ();
92+ $ this ->array ($ output )->isEqualTo ([
93+ \PluginFormcreatorTargetTicket::REQUESTTYPE_NONE => 'Source from template or user default or GLPI default ' ,
94+ \PluginFormcreatorTargetTicket::REQUESTTYPE_SPECIFIC => "Formcreator " ,
95+ ]);
96+ }
97+
8098 public function testGetEnumDestinationEntity () {
8199 $ output = \PluginFormcreatorTargetTicket::getEnumDestinationEntity ();
82100 $ this ->array ($ output )->isEqualTo ([
@@ -638,6 +656,8 @@ public function testExport() {
638656 $ fieldsWithoutID = [
639657 'name ' ,
640658 'target_name ' ,
659+ 'source_rule ' ,
660+ 'source_question ' ,
641661 'type_rule ' ,
642662 'type_question ' ,
643663 'content ' ,
@@ -712,6 +732,8 @@ public function testImport() {
712732 'category_question ' => '0 ' ,
713733 'associate_rule ' => \PluginFormcreatorTargetTicket::ASSOCIATE_RULE_NONE ,
714734 'associate_question ' => '0 ' ,
735+ 'source_rule ' => 0 ,
736+ 'source_question ' => 0 ,
715737 'type_rule ' => 1 ,
716738 'type_question ' => 0 ,
717739 'uuid ' => $ uuid ,
@@ -1266,6 +1288,7 @@ public function providerPrepareInputForAdd() {
12661288 $ formFk = \PluginFormcreatorForm::getForeignKeyField ();
12671289 $ form = $ this ->getForm ();
12681290 $ name = $ this ->getUniqueString ();
1291+ $ sourceId = \PluginFormcreatorCommon::getFormcreatorRequestTypeId ();
12691292 return [
12701293 'name is mandatory ' => [
12711294 'input ' => [
@@ -1287,6 +1310,8 @@ public function providerPrepareInputForAdd() {
12871310 'type_rule ' => \PluginFormcreatorTargetTicket::REQUESTTYPE_SPECIFIC ,
12881311 'type_question ' => \Ticket::INCIDENT_TYPE ,
12891312 'show_rule ' => \PluginFormcreatorCondition::SHOW_RULE_ALWAYS ,
1313+ 'source_rule ' => \PluginFormcreatorTargetTicket::REQUESTSOURCE_SPECIFIC ,
1314+ 'source_question ' => $ sourceId ,
12901315 ],
12911316 'message ' => null ,
12921317 ],
@@ -1296,6 +1321,7 @@ public function providerPrepareInputForAdd() {
12961321 'name ' => $ name ,
12971322 'type_rule ' => \PluginFormcreatorTargetTicket::REQUESTTYPE_SPECIFIC ,
12981323 'type_question ' => \Ticket::DEMAND_TYPE ,
1324+ 'source_rule ' => \PluginFormcreatorTargetTicket::REQUESTSOURCE_NONE ,
12991325 ],
13001326 'expected ' => [
13011327 $ formFk => $ form ->getID (),
@@ -1305,6 +1331,8 @@ public function providerPrepareInputForAdd() {
13051331 'type_rule ' => \PluginFormcreatorTargetTicket::REQUESTTYPE_SPECIFIC ,
13061332 'type_question ' => \Ticket::DEMAND_TYPE ,
13071333 'show_rule ' => \PluginFormcreatorCondition::SHOW_RULE_ALWAYS ,
1334+ 'source_rule ' => \PluginFormcreatorTargetTicket::REQUESTSOURCE_NONE ,
1335+ 'source_question ' => 0 ,
13081336 ],
13091337 'message ' => null ,
13101338 ],
@@ -1437,4 +1465,78 @@ public function testSetTargetLocation($instance, $formanswer, $expected) {
14371465 $ this ->array ($ output )->notHasKey ('locations_id ' );
14381466 }
14391467 }
1468+
1469+ public function providerSetRequestSource_none (): array {
1470+ $ form = $ this ->getForm ();
1471+ $ formanswer = new \PluginFormcreatorFormanswer ();
1472+ $ formanswer ->add ([
1473+ 'plugin_formcreator_forms_id ' => $ form ->getID (),
1474+ ]);
1475+ $ this ->boolean ($ formanswer ->isNewItem ())->isFalse ();
1476+ $ targetTicket = new \PluginFormcreatorTargetTicket ();
1477+ $ targetTicket ->add ([
1478+ 'name ' => 'target ticket ' ,
1479+ 'target_name ' => 'target ticket ' ,
1480+ 'plugin_formcreator_forms_id ' => $ form ->getID (),
1481+ 'source_rule ' => \PluginFormcreatorTargetTicket::REQUESTSOURCE_NONE ,
1482+ ]);
1483+ $ this ->boolean ($ targetTicket ->isNewItem ())->isFalse ();
1484+
1485+ return [
1486+ [
1487+ 'instance ' => $ targetTicket ,
1488+ 'formanswer ' => $ formanswer ,
1489+ 'expected ' => 0
1490+ ],
1491+ ];
1492+ }
1493+
1494+ public function providerSetRequestSource_specific (): array {
1495+ $ form = $ this ->getForm ();
1496+ $ formanswer = new \PluginFormcreatorFormanswer ();
1497+ $ formanswer ->add ([
1498+ 'plugin_formcreator_forms_id ' => $ form ->getID (),
1499+ ]);
1500+ $ this ->boolean ($ formanswer ->isNewItem ())->isFalse ();
1501+ $ targetTicket = new \PluginFormcreatorTargetTicket ();
1502+ $ targetTicket ->add ([
1503+ 'name ' => 'target ticket ' ,
1504+ 'target_name ' => 'target ticket ' ,
1505+ 'plugin_formcreator_forms_id ' => $ form ->getID (),
1506+ 'source_rule ' => \PluginFormcreatorTargetTicket::REQUESTSOURCE_SPECIFIC ,
1507+ 'source_question ' => \PluginFormcreatorCommon::getFormcreatorRequestTypeId (),
1508+ ]);
1509+ $ this ->boolean ($ targetTicket ->isNewItem ())->isFalse ();
1510+
1511+ return [
1512+ [
1513+ 'instance ' => $ targetTicket ,
1514+ 'formanswer ' => $ formanswer ,
1515+ 'expected ' => 0
1516+ ],
1517+ ];
1518+ }
1519+
1520+ public function providerSetRequestSource (): array {
1521+ return array_merge (
1522+ $ this ->providerSetRequestSource_none (),
1523+ $ this ->providerSetRequestSource_specific ()
1524+ );
1525+ }
1526+
1527+ /**
1528+ * @dataProvider providerSetRequestSource
1529+ */
1530+ public function testSetRequestSource ($ instance , $ formanswer , $ expected ): void {
1531+ // Substitute a dummy class to access protected / private methods
1532+ $ dummyItemtype = 'GlpiPlugin\Formcreator\Tests \\' . $ this ->getTestedClassName () . 'Dummy ' ;
1533+ $ dummyInstance = new $ dummyItemtype ();
1534+ /**@var \GlpiPlugin\Formcreator\Tests\PluginFormcreatorTargetTicketDummy */
1535+ $ instance ->getFromDB ($ instance ->getID ());
1536+ $ dummyInstance ->fields = $ instance ->fields ;
1537+
1538+ $ data = $ dummyInstance ->publicGetDefaultData ($ formanswer );
1539+ $ output = $ dummyInstance ->publicSetTargetCategory ($ data , $ formanswer );
1540+ $ this ->integer ((int ) $ output ['itilcategories_id ' ])->isEqualTo ($ expected );
1541+ }
14401542}
0 commit comments