@@ -44,6 +44,7 @@ public function beforeTestMethod($method) {
4444 case 'testPrepareTemplate ' :
4545 case 'testDeleteLinkedTickets ' :
4646 case 'testSetTargetAssociatedItem ' :
47+ case 'testSetTargetLocation ' :
4748 $ this ->boolean ($ this ->login ('glpi ' , 'glpi ' ))->isTrue ();
4849 break ;
4950 }
@@ -1331,4 +1332,114 @@ public function testPrepareInputForAdd($input, $expected, $message) {
13311332 $ this ->sessionHasMessage ($ message , ERROR );
13321333 }
13331334 }
1335+
1336+ public function providerSetTargetLocation_NotSet () {
1337+ // Prepare form
1338+
1339+ $ form1 = $ this ->getForm ();
1340+
1341+ $ instance1 = new PluginFormcreatorTargetTicketDummy ();
1342+ $ instance1 ->add ([
1343+ 'name ' => 'foo ' ,
1344+ 'target_name ' => '' ,
1345+ \PluginFormcreatorForm::getForeignKeyField () => $ form1 ->getID (),
1346+ 'content ' => '##FULLFORM ' ,
1347+ 'location_rule ' => \PluginFormcreatorTargetTicket::LOCATION_RULE_NONE ,
1348+ 'location_question ' => '0 ' ,
1349+ ]);
1350+ $ this ->boolean ($ instance1 ->isNewItem ())->isFalse ();
1351+ $ formAnswer1 = new \PluginFormcreatorFormAnswer ();
1352+ $ formAnswer1 ->add ([
1353+ 'plugin_formcreator_forms_id ' => $ form1 ->getID (),
1354+ ]);
1355+ $ this ->boolean ($ formAnswer1 ->isNewItem ())->isFalse ();
1356+
1357+ return [
1358+ [
1359+ 'instance ' => $ instance1 ,
1360+ 'formanswer ' => $ formAnswer1 ,
1361+ 'expected ' => null ,
1362+ ],
1363+ ];
1364+ }
1365+
1366+ public function providerSetTargetLocation_LastItem () {
1367+ // Prepare form
1368+ $ validItemtype = \Location::class;
1369+ $ invalidItemtype = \Monitor::getType ();
1370+
1371+ $ item1 = new $ validItemtype ();
1372+ $ item1 ->add ([
1373+ 'name ' => $ this ->getUniqueString (),
1374+ 'entities_id ' => \Session::getActiveEntity (),
1375+ ]);
1376+ $ this ->boolean ($ item1 ->isNewItem ())->isFalse ();
1377+ $ item2 = new $ validItemtype ();
1378+ $ item2 ->add ([
1379+ 'name ' => $ this ->getUniqueString (),
1380+ 'entities_id ' => \Session::getActiveEntity (),
1381+ ]);
1382+ $ this ->boolean ($ item2 ->isNewItem ())->isFalse ();
1383+
1384+ $ question1 = $ this ->getQuestion ([
1385+ 'fieldtype ' => 'dropdown ' ,
1386+ 'dropdown_values ' => $ validItemtype ,
1387+ ]);
1388+ $ form1 = \PluginFormcreatorForm::getByItem ($ question1 );
1389+ $ sectionId = $ question1 ->fields ['plugin_formcreator_sections_id ' ];
1390+ $ question2 = $ this ->getQuestion ([
1391+ 'plugin_formcreator_sections_id ' => $ sectionId ,
1392+ 'fieldtype ' => 'dropdown ' ,
1393+ 'dropdown_values ' => $ validItemtype
1394+ ]);
1395+ $ instance1 = new PluginFormcreatorTargetTicketDummy ();
1396+ $ instance1 ->add ([
1397+ 'name ' => 'foo ' ,
1398+ 'target_name ' => '' ,
1399+ \PluginFormcreatorForm::getForeignKeyField () => $ form1 ->getID (),
1400+ 'content ' => '##FULLFORM ' ,
1401+ 'location_rule ' => \PluginFormcreatorTargetTicket::LOCATION_RULE_LAST_ANSWER ,
1402+ 'location_question ' => '0 ' ,
1403+ ]);
1404+ $ this ->boolean ($ instance1 ->isNewItem ())->isFalse ();
1405+ $ formAnswer1 = new \PluginFormcreatorFormAnswer ();
1406+ $ formAnswer1 ->add ([
1407+ 'plugin_formcreator_forms_id ' => $ form1 ->getID (),
1408+ 'formcreator_field_ ' . $ question1 ->getID () => (string ) $ item1 ->getID (),
1409+ 'formcreator_field_ ' . $ question2 ->getID () => (string ) $ item2 ->getID (),
1410+ ]);
1411+ $ this ->boolean ($ formAnswer1 ->isNewItem ())->isFalse ();
1412+
1413+ return [
1414+ [
1415+ 'instance ' => $ instance1 ,
1416+ 'formanswer ' => $ formAnswer1 ,
1417+ 'expected ' => $ item2 ->getID (),
1418+ ],
1419+ ];
1420+ }
1421+
1422+ public function providerSetTargetLocation () {
1423+ global $ CFG_GLPI ;
1424+
1425+ // Disable notification to avoid output to console
1426+ $ CFG_GLPI ['use_notifications ' ] = '0 ' ;
1427+
1428+ return array_merge (
1429+ $ this ->providerSetTargetLocation_NotSet (),
1430+ $ this ->providerSetTargetLocation_LastItem (),
1431+ );
1432+ }
1433+
1434+ /**
1435+ * @dataProvider providerSetTargetLocation
1436+ */
1437+ public function testSetTargetLocation ($ instance , $ formanswer , $ expected ) {
1438+ $ output = $ instance ->publicSetTargetLocation ([], $ formanswer );
1439+ if ($ expected !== null ) {
1440+ $ this ->integer ((int ) $ output ['locations_id ' ])->isIdenticalTo ($ expected );
1441+ } else {
1442+ $ this ->array ($ output )->notHasKey ('locations_id ' );
1443+ }
1444+ }
13341445}
0 commit comments