|
42 | 42 | use Ticket; |
43 | 43 | use Ticket_User; |
44 | 44 | use User; |
| 45 | +use TicketTask; |
45 | 46 |
|
46 | 47 | final class TicketTest extends EscaladeTestCase |
47 | 48 | { |
@@ -1257,4 +1258,79 @@ public function testHistoryButtonEscalationWithMandatoryTemplateFields() |
1257 | 1258 | $group1->delete(['id' => $group1_id], true); |
1258 | 1259 | $group2->delete(['id' => $group2_id], true); |
1259 | 1260 | } |
| 1261 | + |
| 1262 | + public function testRuleCreatesTaskWhenCategoryAssigned() |
| 1263 | + { |
| 1264 | + $this->initConfig(); |
| 1265 | + |
| 1266 | + // Create a task template that will be added by the rule |
| 1267 | + $task_template = $this->createItem('TaskTemplate', [ |
| 1268 | + 'name' => 'Rule task template', |
| 1269 | + 'content' => '<p>Task created by rule</p>', |
| 1270 | + 'entities_id' => 0, |
| 1271 | + 'is_recursive' => 1, |
| 1272 | + ]); |
| 1273 | + $task_template_id = $task_template->getID(); |
| 1274 | + |
| 1275 | + // Create the rule that appends a task template when category is set on update |
| 1276 | + $rule = $this->createItem('Rule', [ |
| 1277 | + 'name' => 'Create task on category assign', |
| 1278 | + 'sub_type' => 'RuleTicket', |
| 1279 | + 'match' => 'AND', |
| 1280 | + 'is_active' => 1, |
| 1281 | + 'condition' => \RuleCommonITILObject::ONUPDATE, |
| 1282 | + 'is_recursive' => 1, |
| 1283 | + 'entities_id' => 0, |
| 1284 | + ]); |
| 1285 | + $rule_id = $rule->getID(); |
| 1286 | + |
| 1287 | + $this->createItem('RuleAction', [ |
| 1288 | + 'rules_id' => $rule_id, |
| 1289 | + 'action_type' => 'append', |
| 1290 | + 'field' => 'task_template', |
| 1291 | + 'value' => $task_template_id, |
| 1292 | + ]); |
| 1293 | + |
| 1294 | + // Create a category that will trigger the rule when assigned |
| 1295 | + $category = $this->createItem('ITILCategory', [ |
| 1296 | + 'name' => 'Category triggering task', |
| 1297 | + 'entities_id' => 0, |
| 1298 | + 'is_recursive' => 1, |
| 1299 | + ]); |
| 1300 | + $category_id = $category->getID(); |
| 1301 | + |
| 1302 | + // Ensure the rule triggers only when the ticket category matches the created category |
| 1303 | + $this->createItem('RuleCriteria', [ |
| 1304 | + 'rules_id' => $rule_id, |
| 1305 | + 'criteria' => 'itilcategories_id', |
| 1306 | + 'condition' => \Rule::PATTERN_IS, |
| 1307 | + 'pattern' => $category_id, |
| 1308 | + ]); |
| 1309 | + |
| 1310 | + // Create a ticket without category |
| 1311 | + $ticket = $this->createItem('Ticket', [ |
| 1312 | + 'name' => 'Ticket for rule test', |
| 1313 | + 'content' => 'Content for rule test', |
| 1314 | + 'entities_id' => 0, |
| 1315 | + ]); |
| 1316 | + $ticket_id = $ticket->getID(); |
| 1317 | + |
| 1318 | + $tickettask = new TicketTask(); |
| 1319 | + $this->assertEquals(0, count($tickettask->find(['tickets_id' => $ticket_id]))); |
| 1320 | + |
| 1321 | + // Assign the category (update) - rule should fire and create a single task |
| 1322 | + $this->updateItem('Ticket', $ticket_id, [ |
| 1323 | + 'id' => $ticket_id, |
| 1324 | + 'itilcategories_id' => $category_id, |
| 1325 | + ]); |
| 1326 | + |
| 1327 | + $tasks = $tickettask->find(['tickets_id' => $ticket_id]); |
| 1328 | + $this->assertEquals(1, count($tasks)); |
| 1329 | + |
| 1330 | + // Clean up |
| 1331 | + $ticket->delete(['id' => $ticket_id], true); |
| 1332 | + $task_template->delete(['id' => $task_template_id], true); |
| 1333 | + $category->delete(['id' => $category_id], true); |
| 1334 | + $rule->delete(['id' => $rule_id], true); |
| 1335 | + } |
1260 | 1336 | } |
0 commit comments