@@ -1525,4 +1525,77 @@ public function testHistoryButtonEscalationWithMandatoryAssignedGroupField()
15251525 $ ticket ->getFromDB ($ ticket ->getID ());
15261526 $ this ->assertEquals ($ category ->getID (), $ ticket ->fields ['itilcategories_id ' ]);
15271527 }
1528+
1529+ public function testRuleCreatesSingleTaskOnCategoryAssign ()
1530+ {
1531+ $ this ->login ();
1532+
1533+ // Create a task template that will be appended by the rule
1534+ $ task_template = $ this ->createItem (\TaskTemplate::class, [
1535+ 'name ' => 'Rule created task template ' ,
1536+ 'content ' => 'Task created by rule ' ,
1537+ 'is_recursive ' => 1 ,
1538+ ]);
1539+ $ this ->assertGreaterThan (0 , $ task_template ->getID ());
1540+
1541+ // Create an ITIL category that will trigger the rule
1542+ $ category = $ this ->createItem (\ITILCategory::class, [
1543+ 'name ' => 'Category that triggers task rule ' ,
1544+ 'entities_id ' => 0 ,
1545+ 'is_recursive ' => 1 ,
1546+ ]);
1547+ $ this ->assertGreaterThan (0 , $ category ->getID ());
1548+
1549+ // Create a RuleTicket that appends the task template when the category is set
1550+ $ rule = $ this ->createItem (\Rule::class, [
1551+ 'name ' => 'Create task on category assign ' ,
1552+ 'sub_type ' => 'RuleTicket ' ,
1553+ 'match ' => 'AND ' ,
1554+ 'is_active ' => 1 ,
1555+ // Trigger on update (could be ONADD | ONUPDATE but update is enough for this test)
1556+ 'condition ' => \RuleTicket::ONUPDATE ,
1557+ 'is_recursive ' => 1 ,
1558+ ]);
1559+ $ this ->assertGreaterThan (0 , $ rule ->getID ());
1560+
1561+ // Add action to append task template
1562+ $ this ->createItem (\RuleAction::class, [
1563+ 'rules_id ' => $ rule ->getID (),
1564+ 'action_type ' => 'append ' ,
1565+ 'field ' => 'task_template ' ,
1566+ 'value ' => $ task_template ->getID (),
1567+ ]);
1568+
1569+ // Add criteria: ticket category must be the created category
1570+ $ this ->createItem (\RuleCriteria::class, [
1571+ 'rules_id ' => $ rule ->getID (),
1572+ 'criteria ' => 'itilcategories_id ' ,
1573+ 'condition ' => \Rule::PATTERN_IS ,
1574+ 'pattern ' => $ category ->getID (),
1575+ ]);
1576+
1577+ // Reset rule cache for ticket rules
1578+ \SingletonRuleList::getInstance ("RuleTicket " , 0 )->load = 0 ;
1579+ \SingletonRuleList::getInstance ("RuleTicket " , 0 )->list = [];
1580+
1581+ // Create a ticket without category
1582+ $ ticket = $ this ->createItem (\Ticket::class, [
1583+ 'name ' => 'Ticket for rule task creation ' ,
1584+ 'content ' => 'Content ' ,
1585+ ]);
1586+ $ this ->assertGreaterThan (0 , $ ticket ->getID ());
1587+
1588+ // Ensure there is no task before assigning the category
1589+ $ ticket_task = new \TicketTask ();
1590+ $ this ->assertEquals (0 , count ($ ticket_task ->find (['tickets_id ' => $ ticket ->getID ()])));
1591+
1592+ // Update the ticket to set the category - this should trigger the rule
1593+ $ this ->updateItem (\Ticket::class, $ ticket ->getID (), [
1594+ 'itilcategories_id ' => $ category ->getID (),
1595+ ]);
1596+
1597+ // Verify that exactly one task was created for the ticket
1598+ $ tasks = $ ticket_task ->find (['tickets_id ' => $ ticket ->getID ()]);
1599+ $ this ->assertEquals (1 , count ($ tasks ), 'Exactly one task should be created when assigning the category ' );
1600+ }
15281601}
0 commit comments