Skip to content

Commit 4a142bd

Browse files
committed
feat(targetticket): support location from template
1 parent d0d4bef commit 4a142bd

File tree

1 file changed

+200
-25
lines changed

1 file changed

+200
-25
lines changed

tests/3-unit/PluginFormcreatorTargetTicket.php

Lines changed: 200 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,11 @@ public function beforeTestMethod($method) {
4040
switch ($method) {
4141
case 'testSetTargetEntity':
4242
case 'testSetTargetCategory':
43+
case 'testSetTargetLocation':
4344
case 'testSetTargetType':
4445
case 'testPrepareTemplate':
4546
case 'testDeleteLinkedTickets':
4647
case 'testSetTargetAssociatedItem':
47-
case 'testSetTargetLocation':
4848
case 'testSetRequestSource':
4949
$this->boolean($this->login('glpi', 'glpi'))->isTrue();
5050
break;
@@ -1442,30 +1442,6 @@ public function providerSetTargetLocation_LastItem() {
14421442
];
14431443
}
14441444

1445-
public function providerSetTargetLocation() {
1446-
global $CFG_GLPI;
1447-
1448-
// Disable notification to avoid output to console
1449-
$CFG_GLPI['use_notifications'] = '0';
1450-
1451-
return array_merge(
1452-
$this->providerSetTargetLocation_NotSet(),
1453-
$this->providerSetTargetLocation_LastItem(),
1454-
);
1455-
}
1456-
1457-
/**
1458-
* @dataProvider providerSetTargetLocation
1459-
*/
1460-
public function testSetTargetLocation($instance, $formanswer, $expected) {
1461-
$output = $instance->publicSetTargetLocation([], $formanswer);
1462-
if ($expected !== null) {
1463-
$this->integer((int) $output['locations_id'])->isIdenticalTo($expected);
1464-
} else {
1465-
$this->array($output)->notHasKey('locations_id');
1466-
}
1467-
}
1468-
14691445
public function providerSetRequestSource_none(): array {
14701446
$form = $this->getForm();
14711447
$formanswer = new \PluginFormcreatorFormanswer();
@@ -1539,4 +1515,203 @@ public function testSetRequestSource($instance, $formanswer, $expected): void {
15391515
$output = $dummyInstance->publicSetTargetCategory($data, $formanswer);
15401516
$this->integer((int) $output['itilcategories_id'])->isEqualTo($expected);
15411517
}
1518+
1519+
public function providerSetTargetLocation_nothing() {
1520+
$form = $this->getForm();
1521+
$formanswer = new \PluginFormcreatorFormanswer();
1522+
$formanswer->add([
1523+
'plugin_formcreator_forms_id' => $form->getID(),
1524+
]);
1525+
$this->boolean($formanswer->isNewItem())->isFalse();
1526+
$targetTicket = new \PluginFormcreatorTargetTicket();
1527+
$targetTicket->add([
1528+
'name' => 'target ticket no location',
1529+
'target_name' => 'target ticket',
1530+
'plugin_formcreator_forms_id' => $form->getID(),
1531+
'location_rule' => \PluginFormcreatorTargetTicket::LOCATION_RULE_NONE,
1532+
]);
1533+
$this->boolean($targetTicket->isNewItem())->isFalse();
1534+
1535+
return [
1536+
[
1537+
'instance' => $targetTicket,
1538+
'formanswer' => $formanswer,
1539+
'expected' => 0
1540+
],
1541+
];
1542+
}
1543+
1544+
public function providerSetTargetLocation_noTemplate() {
1545+
$location1 = new \Location();
1546+
$location1Id = $location1->import([
1547+
'name' => 'location 1',
1548+
'entities_id' => 0,
1549+
]);
1550+
$location2 = new \Location();
1551+
$location2Id = $location2->import([
1552+
'name' => 'location 2',
1553+
'entities_id' => 0,
1554+
]);
1555+
1556+
$question1 = $this->getQuestion([
1557+
'name' => 'request type',
1558+
'fieldtype' => 'requesttype',
1559+
]);
1560+
$this->boolean($question1->isNewItem())->isFalse();
1561+
$section = new \PluginFormcreatorSection();
1562+
$section->getFromDB($question1->fields['plugin_formcreator_sections_id']);
1563+
$this->boolean($section->isNewItem())->isFalse();
1564+
$question2 = $this->getQuestion([
1565+
'plugin_formcreator_sections_id' => $section->getID(),
1566+
'name' => 'location',
1567+
'fieldtype' => 'dropdown',
1568+
'itemtype' => \Location::class,
1569+
'show_rule' => \PluginFormcreatorCondition::SHOW_RULE_HIDDEN,
1570+
'_conditions' => [
1571+
'show_logic' => [\PluginFormcreatorCondition::SHOW_LOGIC_AND],
1572+
'plugin_formcreator_questions_id' => [$question1->getID()],
1573+
'show_condition' => [\PluginFormcreatorCondition::SHOW_CONDITION_EQ],
1574+
'show_value' => ['Incident'],
1575+
]
1576+
]);
1577+
$question3 = $this->getQuestion([
1578+
'plugin_formcreator_sections_id' => $section->getID(),
1579+
'name' => 'other location',
1580+
'fieldtype' => 'dropdown',
1581+
'itemtype' => \Location::class,
1582+
'show_rule' => \PluginFormcreatorCondition::SHOW_RULE_HIDDEN,
1583+
'_conditions' => [
1584+
'show_logic' => [\PluginFormcreatorCondition::SHOW_LOGIC_AND],
1585+
'plugin_formcreator_questions_id' => [$question1->getID()],
1586+
'show_condition' => [\PluginFormcreatorCondition::SHOW_CONDITION_EQ],
1587+
'show_value' => ['Request'],
1588+
]
1589+
]);
1590+
1591+
$formanswer1 = new \PluginFormcreatorFormAnswer();
1592+
$formanswer1->add([
1593+
'plugin_formcreator_forms_id' => $section->fields['plugin_formcreator_forms_id'],
1594+
'formcreator_field_' . $question1->getID() => (string) \Ticket::INCIDENT_TYPE,
1595+
'formcreator_field_' . $question2->getID() => (string) $location1Id,
1596+
'formcreator_field_' . $question3->getID() => (string) $location2Id,
1597+
]);
1598+
1599+
$formanswer2 = new \PluginFormcreatorFormAnswer();
1600+
$formanswer2->add([
1601+
'plugin_formcreator_forms_id' => $section->fields['plugin_formcreator_forms_id'],
1602+
'formcreator_field_' . $question1->getID() => (string) \Ticket::DEMAND_TYPE,
1603+
'formcreator_field_' . $question2->getID() => (string) $location1Id,
1604+
'formcreator_field_' . $question3->getID() => (string) $location2Id,
1605+
]);
1606+
1607+
$formanswer3 = new \PluginFormcreatorFormAnswer();
1608+
$formanswer3->add([
1609+
'plugin_formcreator_forms_id' => $section->fields['plugin_formcreator_forms_id'],
1610+
'formcreator_field_' . $question1->getID() => (string) \Ticket::INCIDENT_TYPE,
1611+
'formcreator_field_' . $question2->getID() => (string) $location1Id,
1612+
'formcreator_field_' . $question3->getID() => (string) 0,
1613+
]);
1614+
1615+
$instance1 = $this->newTestedInstance();
1616+
$instance1->add([
1617+
'name' => 'target ticket no template',
1618+
'target_name' => 'target ticket',
1619+
'plugin_formcreator_forms_id' => $formanswer1->getForm()->getID(),
1620+
'location_rule' => \PluginFormcreatorTargetTicket::LOCATION_RULE_LAST_ANSWER,
1621+
]);
1622+
1623+
return [
1624+
// Check visibility is taken into account
1625+
'visibility taken into account' => [
1626+
'instance' => $instance1,
1627+
'formanswer' => $formanswer1,
1628+
'expected' => $location1Id,
1629+
],
1630+
// Check location dropdown is ignored
1631+
'1st ticket location question is ignored' => [
1632+
'instance' => $instance1,
1633+
'formanswer' => $formanswer2,
1634+
'expected' => $location2Id,
1635+
],
1636+
// Check zero value is ignored
1637+
'zero value is ignored' => [
1638+
'instance' => $instance1,
1639+
'formanswer' => $formanswer3,
1640+
'expected' => $location1Id,
1641+
]
1642+
];
1643+
}
1644+
1645+
public function providerSetTargetLocation_FromTemplate() {
1646+
// When the target ticket uses a ticket template and does not specify a location
1647+
$location1 = new \Location();
1648+
$location1Id = $location1->import([
1649+
'name' => 'location 1',
1650+
'entities_id' => 0,
1651+
]);
1652+
1653+
$ticketTemplate = $this->getGlpiCoreItem(
1654+
\TicketTemplate::getType(), [
1655+
'name' => 'template with predefined location',
1656+
]
1657+
);
1658+
$this->getGlpiCoreItem(\TicketTemplatePredefinedField::getType(), [
1659+
'tickettemplates_id' => $ticketTemplate->getID(),
1660+
'num' => 83, // Location
1661+
'value' => $location1Id
1662+
]);
1663+
1664+
$form = $this->getForm();
1665+
1666+
$formanswer1 = new \PluginFormcreatorFormAnswer();
1667+
$formanswer1->add([
1668+
'plugin_formcreator_forms_id' => $form->getID(),
1669+
]);
1670+
$this->boolean($formanswer1->isNewItem())->isFalse();
1671+
1672+
$instance1 = $this->newTestedInstance();
1673+
$instance1->add([
1674+
'name' => 'target ticket with template',
1675+
'target_name' => 'target ticket',
1676+
'plugin_formcreator_forms_id' => $form->getID(),
1677+
'tickettemplates_id' => $ticketTemplate->getID(),
1678+
'location_rule' => \PluginFormcreatorTargetTicket::LOCATION_RULE_NONE,
1679+
]);
1680+
$this->boolean($instance1->isNewItem())->isFalse();
1681+
1682+
return [
1683+
[
1684+
'instance' => $instance1,
1685+
'formanswer' => $formanswer1,
1686+
'expected' => $location1Id,
1687+
],
1688+
];
1689+
}
1690+
1691+
public function providerSetTargetLocation() {
1692+
return array_merge(
1693+
$this->providerSetTargetLocation_nothing(),
1694+
$this->providerSetTargetLocation_noTemplate(),
1695+
$this->providerSetTargetLocation_FromTemplate(),
1696+
);
1697+
}
1698+
1699+
/**
1700+
* @dataProvider providerSetTargetLocation
1701+
*
1702+
*/
1703+
public function testSetTargetLocation($instance, $formanswer, $expected) {
1704+
// Substitute a dummy class to access protected / private methods
1705+
$dummyItemtype = 'GlpiPlugin\Formcreator\Tests\\' . $this->getTestedClassName() . 'Dummy';
1706+
$dummyInstance = new $dummyItemtype();
1707+
/**@var \GlpiPlugin\Formcreator\Tests\PluginFormcreatorTargetTicketDummy */
1708+
$instance->getFromDB($instance->getID());
1709+
$dummyInstance->fields = $instance->fields;
1710+
1711+
\PluginFormcreatorFields::resetVisibilityCache();
1712+
$data = $dummyInstance->publicGetDefaultData($formanswer);
1713+
$output = $dummyInstance->publicSetTargetLocation($data, $formanswer);
1714+
1715+
$this->integer((int) $output['locations_id'])->isEqualTo($expected);
1716+
}
15421717
}

0 commit comments

Comments
 (0)