Skip to content

Commit 831cfa9

Browse files
authored
Fix missing default category for documents added to Tickets via follow-up, task, or solution (#22161)
1 parent 7c4d7f4 commit 831cfa9

File tree

2 files changed

+374
-4
lines changed

2 files changed

+374
-4
lines changed

phpunit/functional/DocumentTest.php

Lines changed: 364 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1345,4 +1345,368 @@ public function testDefaultDocumentCategoryForTicket()
13451345
$this->assertTrue($document->getFromDB(current($data)['documents_id']));
13461346
$this->assertEquals($documentCategory_id, $document->fields['documentcategories_id']);
13471347
}
1348+
1349+
public function testDefaultDocumentCategoryForTicketWithChangeTask()
1350+
{
1351+
global $CFG_GLPI;
1352+
$documentCategory = new DocumentCategory();
1353+
1354+
/////////////////////////////////////////////////////////////////////////////////////////
1355+
// Update config to have default category for document uploaded during ticket creation //
1356+
/////////////////////////////////////////////////////////////////////////////////////////
1357+
$documentCategory_id = $documentCategory->add([
1358+
'name' => 'Default Category',
1359+
]);
1360+
$this->assertGreaterThan(0, $documentCategory_id);
1361+
$CFG_GLPI['documentcategories_id_forticket'] = $documentCategory_id;
1362+
1363+
$input = [
1364+
'name' => 'Ticket 1',
1365+
'content' => 'testDefaultDocumentCategoryFromDocumentForm',
1366+
'entities_id' => 0,
1367+
];
1368+
$ticket_id = $this->createItem(\Ticket::class, $input)->getID();
1369+
1370+
$this->assertGreaterThan(0, $ticket_id);
1371+
1372+
$ticketTask = new \TicketTask();
1373+
$filename = 'wdgrgserh5515rgg.222222' . 'foo.txt';
1374+
1375+
//ajouter un ticket tast avec un document et vérifier si ce document a bien la catégorie par defaut
1376+
$input2 = [
1377+
'tickets_id' => $ticket_id,
1378+
'content' => 'test ticket task with document',
1379+
'_filename' => [
1380+
$filename,
1381+
],
1382+
'_tag_filename' => [ '564grgt4-684vfv8-fvs8b81.0000',
1383+
],
1384+
'_prefix_filename' => [
1385+
'wdgrgserh5515rgg.222222',
1386+
],
1387+
];
1388+
1389+
copy(FIXTURE_DIR . '/uploads/foo.txt', GLPI_TMP_DIR . '/' . $filename);
1390+
$ticketTask_id = $ticketTask->add($input2);
1391+
$this->assertGreaterThan(0, $ticketTask_id);
1392+
1393+
$document = new \Document();
1394+
$document_item = new \Document_Item();
1395+
1396+
$data = $document_item->find([
1397+
'itemtype' => \TicketTask::class,
1398+
'items_id' => $ticketTask_id,
1399+
]);
1400+
$this->assertCount(1, $data);
1401+
1402+
$this->assertTrue($document->getFromDB(current($data)['documents_id']));
1403+
$this->assertEquals($documentCategory_id, $document->fields['documentcategories_id']);
1404+
1405+
}
1406+
1407+
public function testDefaultDocumentCategoryForTicketWithITILFollowup()
1408+
{
1409+
$this->login();
1410+
global $CFG_GLPI;
1411+
$documentCategory = new DocumentCategory();
1412+
1413+
/////////////////////////////////////////////////////////////////////////////////////////
1414+
// Update config to have default category for document uploaded during ticket creation //
1415+
/////////////////////////////////////////////////////////////////////////////////////////
1416+
$documentCategory_id = $documentCategory->add([
1417+
'name' => 'Default Category',
1418+
]);
1419+
1420+
$CFG_GLPI['documentcategories_id_forticket'] = $documentCategory_id;
1421+
1422+
$ticket = new \Ticket();
1423+
$tickets_id = $ticket->add([
1424+
'name' => 'Ticket for ITIL followup',
1425+
'content' => 'Ticket content for followup',
1426+
'entities_id' => 0,
1427+
]);
1428+
$this->assertGreaterThan(0, $tickets_id);
1429+
1430+
$itilFollowup = new \ITILFollowup();
1431+
$filename = 'itilfollowup_doc.999999' . 'foo.txt';
1432+
$inputFollowup = [
1433+
'items_id' => $tickets_id,
1434+
'itemtype' => \Ticket::class,
1435+
'content' => 'Followup with document',
1436+
'_filename' => [$filename],
1437+
'_tag_filename' => ['tag-itilfollowup-999999'],
1438+
'_prefix_filename' => ['itilfollowup_doc.999999'],
1439+
];
1440+
1441+
copy(FIXTURE_DIR . '/uploads/foo.txt', GLPI_TMP_DIR . '/' . $filename);
1442+
$itilFollowups_id = $itilFollowup->add($inputFollowup);
1443+
$this->assertGreaterThan(0, $itilFollowups_id);
1444+
1445+
$document = new \Document();
1446+
$document_item = new \Document_Item();
1447+
1448+
$data = $document_item->find([
1449+
'itemtype' => \ITILFollowup::class,
1450+
'items_id' => $itilFollowups_id,
1451+
]);
1452+
$this->assertCount(1, $data);
1453+
1454+
$this->assertTrue($document->getFromDB(current($data)['documents_id']));
1455+
$this->assertEquals($documentCategory_id, $document->fields['documentcategories_id']);
1456+
1457+
}
1458+
1459+
public function testDefaultDocumentCategoryForTicketWithITILSolution()
1460+
{
1461+
$this->login();
1462+
global $CFG_GLPI;
1463+
$documentCategory = new DocumentCategory();
1464+
1465+
/////////////////////////////////////////////////////////////////////////////////////////
1466+
// Update config to have default category for document uploaded during ticket creation //
1467+
/////////////////////////////////////////////////////////////////////////////////////////
1468+
$documentCategory_id = $documentCategory->add([
1469+
'name' => 'Default Category',
1470+
]);
1471+
1472+
$CFG_GLPI['documentcategories_id_forticket'] = $documentCategory_id;
1473+
1474+
$ticket = new \Ticket();
1475+
$tickets_id = $ticket->add([
1476+
'name' => 'Ticket for ITIL solution',
1477+
'content' => 'Ticket content for solution',
1478+
'entities_id' => 0,
1479+
]);
1480+
$this->assertGreaterThan(0, $tickets_id);
1481+
1482+
$itilSolution = new \ITILSolution();
1483+
$filename = 'itilsolution_doc.888888' . 'foo.txt';
1484+
$inputSolution = [
1485+
'items_id' => $tickets_id,
1486+
'itemtype' => \Ticket::class,
1487+
'content' => 'Solution with document',
1488+
'_filename' => [$filename],
1489+
'_tag_filename' => ['tag-itilsolution-888888'],
1490+
'_prefix_filename' => ['itilsolution_doc.888888'],
1491+
];
1492+
copy(FIXTURE_DIR . '/uploads/foo.txt', GLPI_TMP_DIR . '/' . $filename);
1493+
$itilSolutions_id = $itilSolution->add($inputSolution);
1494+
$this->assertGreaterThan(0, $itilSolutions_id);
1495+
1496+
$document = new \Document();
1497+
$document_item = new \Document_Item();
1498+
1499+
$data = $document_item->find([
1500+
'itemtype' => \ITILSolution::class,
1501+
'items_id' => $itilSolutions_id,
1502+
]);
1503+
$this->assertCount(1, $data);
1504+
$this->assertTrue($document->getFromDB(current($data)['documents_id']));
1505+
$this->assertEquals($documentCategory_id, $document->fields['documentcategories_id']);
1506+
1507+
}
1508+
1509+
public function testDefaultDocumentCategoryForChange()
1510+
{
1511+
$this->login();
1512+
1513+
$document = new \Document();
1514+
$document_item = new \Document_Item();
1515+
1516+
///////////////////////////////////////////////////////////////////////
1517+
// Create Change with document, check document has no category //
1518+
///////////////////////////////////////////////////////////////////////
1519+
$change = new \Change();
1520+
$changes_id = $change->add([
1521+
'name' => "test new change",
1522+
'content' => "test new change",
1523+
]);
1524+
1525+
$this->assertGreaterThan(0, $changes_id);
1526+
1527+
// ajouter un document à un change
1528+
$filename = 'wdgrgserh5515rgg.222222' . 'foo.txt';
1529+
$input = [
1530+
'name' => 'Change 1 Document',
1531+
'content' => 'testUploadDocumentWithoutCategory',
1532+
'_filename' => [
1533+
$filename,
1534+
],
1535+
'_tag_filename' => [ '564grgt4-684vfv8-fvs8b81.0000',
1536+
],
1537+
'_prefix_filename' => [
1538+
'wdgrgserh5515rgg.222222',
1539+
],
1540+
'itemtype' => \Change::class,
1541+
'items_id' => $changes_id,
1542+
];
1543+
copy(FIXTURE_DIR . '/uploads/foo.txt', GLPI_TMP_DIR . '/' . $filename);
1544+
$doc_id = $document->add($input);
1545+
$this->assertGreaterThan(0, $doc_id);
1546+
$data = $document_item->find([
1547+
'itemtype' => \Change::class,
1548+
'items_id' => $changes_id,
1549+
]);
1550+
$this->assertCount(1, $data);
1551+
$this->assertTrue($document->getFromDB(current($data)['documents_id']));
1552+
$this->assertEquals(0, $document->fields['documentcategories_id']);
1553+
1554+
}
1555+
1556+
public function testDefaultDocumentCategoryForChangeWithChangeTask()
1557+
{
1558+
$this->login();
1559+
1560+
$change = new \Change();
1561+
$changes_id = $change->add([
1562+
'name' => "test new change",
1563+
'content' => "test new change",
1564+
]);
1565+
$this->assertGreaterThan(0, $changes_id);
1566+
1567+
// add a change task to the change
1568+
$changeTask = new \ChangeTask();
1569+
$changeTasks_id = $changeTask->add([
1570+
'changes_id' => $changes_id,
1571+
'name' => "test change task",
1572+
'content' => "test change task",
1573+
]);
1574+
$this->assertGreaterThan(0, $changeTasks_id);
1575+
$document = new \Document();
1576+
$document_item = new \Document_Item();
1577+
$filename = 'wdgrgserh5515rgg.222222' . 'foo.txt';
1578+
$input = [
1579+
'name' => 'ChangeTask Document',
1580+
'content' => 'testUploadDocumentWithoutCategory',
1581+
'_filename' => [
1582+
$filename,
1583+
],
1584+
'_tag_filename' => [ '564grgt4-684vfv8-fvs8b81.0000',
1585+
],
1586+
'_prefix_filename' => [
1587+
'wdgrgserh5515rgg.222222',
1588+
],
1589+
'itemtype' => \ChangeTask::class,
1590+
'items_id' => $changeTasks_id,
1591+
];
1592+
copy(FIXTURE_DIR . '/uploads/foo.txt', GLPI_TMP_DIR . '/' . $filename);
1593+
$doc_id = $document->add($input);
1594+
$this->assertGreaterThan(0, $doc_id);
1595+
$data = $document_item->find([
1596+
'itemtype' => \ChangeTask::class,
1597+
'items_id' => $changeTasks_id,
1598+
]);
1599+
$this->assertCount(1, $data);
1600+
$this->assertTrue($document->getFromDB(current($data)['documents_id']));
1601+
$this->assertEquals(0, $document->fields['documentcategories_id']);
1602+
}
1603+
1604+
public function testDefaultDocumentCategoryForChangeWithITILFollowup()
1605+
{
1606+
1607+
$this->login();
1608+
global $CFG_GLPI;
1609+
$documentCategory = new DocumentCategory();
1610+
1611+
/////////////////////////////////////////////////////////////////////////////////////////
1612+
// Update config to have default category for document uploaded during ticket creation //
1613+
/////////////////////////////////////////////////////////////////////////////////////////
1614+
$documentCategory_id = $documentCategory->add([
1615+
'name' => 'Default Category',
1616+
]);
1617+
1618+
$change = new \Change();
1619+
$changes_id = $change->add([
1620+
'name' => "test new change",
1621+
'content' => "test new change",
1622+
]);
1623+
$this->assertGreaterThan(0, $changes_id);
1624+
1625+
// add an itil followup to the change
1626+
$itilFollowup = new \ITILFollowup();
1627+
$itilFollowups_id = $itilFollowup->add([
1628+
'items_id' => $changes_id,
1629+
'itemtype' => \Change::class,
1630+
'content' => "test itil followup",
1631+
]);
1632+
$this->assertGreaterThan(0, $itilFollowups_id);
1633+
$document = new \Document();
1634+
$document_item = new \Document_Item();
1635+
$filename = 'wdgrgserh5515rgg.222222' . 'foo.txt';
1636+
$input = [
1637+
'name' => 'ITILFollowup Document',
1638+
'content' => 'testUploadDocumentWithoutCategory',
1639+
'_filename' => [
1640+
$filename,
1641+
],
1642+
'_tag_filename' => ['564grgt4-684vfv8-fvs8b81.0000',
1643+
],
1644+
'_prefix_filename' => [
1645+
'wdgrgserh5515rgg.222222',
1646+
],
1647+
'itemtype' => \ITILFollowup::class,
1648+
'items_id' => $itilFollowups_id,
1649+
];
1650+
copy(FIXTURE_DIR . '/uploads/foo.txt', GLPI_TMP_DIR . '/' . $filename);
1651+
$doc_id = $document->add($input);
1652+
$this->assertGreaterThan(0, $doc_id);
1653+
$data = $document_item->find([
1654+
'itemtype' => \ITILFollowup::class,
1655+
'items_id' => $itilFollowups_id,
1656+
]);
1657+
$this->assertCount(1, $data);
1658+
$this->assertTrue($document->getFromDB(current($data)['documents_id']));
1659+
$this->assertEquals(0, $document->fields['documentcategories_id']);
1660+
}
1661+
1662+
public function testDefaultDocumentCategoryForChangeWithITILSolution()
1663+
{
1664+
$this->login();
1665+
global $CFG_GLPI;
1666+
$documentCategory = new DocumentCategory();
1667+
1668+
/////////////////////////////////////////////////////////////////////////////////////////
1669+
// Update config to have default category for document uploaded during ticket creation //
1670+
/////////////////////////////////////////////////////////////////////////////////////////
1671+
$documentCategory_id = $documentCategory->add([
1672+
'name' => 'Default Category',
1673+
]);
1674+
1675+
$CFG_GLPI['documentcategories_id_forticket'] = $documentCategory_id;
1676+
1677+
$change = new \Change();
1678+
$changes_id = $change->add([
1679+
'name' => "test new change",
1680+
'content' => "test new change",
1681+
]);
1682+
$this->assertGreaterThan(0, $changes_id);
1683+
1684+
$itilSolution = new \ITILSolution();
1685+
$filename = 'itilsolution_doc.888888' . 'foo.txt';
1686+
$inputSolution = [
1687+
'items_id' => $changes_id,
1688+
'itemtype' => \Change::class,
1689+
'content' => 'Solution with document',
1690+
'_filename' => [$filename],
1691+
'_tag_filename' => ['tag-itilsolution-888888'],
1692+
'_prefix_filename' => ['itilsolution_doc.888888'],
1693+
];
1694+
copy(FIXTURE_DIR . '/uploads/foo.txt', GLPI_TMP_DIR . '/' . $filename);
1695+
$itilSolutions_id = $itilSolution->add($inputSolution);
1696+
$this->assertGreaterThan(0, $itilSolutions_id);
1697+
1698+
$document = new \Document();
1699+
$document_item = new \Document_item();
1700+
1701+
$data = $document_item->find([
1702+
'itemtype' => \ITILSolution::class,
1703+
'items_id' => $itilSolutions_id,
1704+
]);
1705+
1706+
$this->assertCount(1, $data);
1707+
$this->assertTrue($document->getFromDB(current($data)['documents_id']));
1708+
$this->assertEquals(0, $document->fields['documentcategories_id']);
1709+
1710+
}
1711+
13481712
}

src/CommonDBTM.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5594,11 +5594,17 @@ public function addFiles(array $input, $options = [])
55945594
$doc->update($input2);
55955595
}
55965596
} else {
5597-
if ($this->getType() == 'Ticket') {
5597+
5598+
if ($this instanceof Ticket || (isset($input['_job']) && $input['_job'] instanceof Ticket)) {
55985599
//TRANS: Default document to files attached to tickets : %d is the ticket id
5599-
$input2["name"] = addslashes(sprintf(__('Document Ticket %d'), $this->getID()));
5600-
$input2["tickets_id"] = $this->getID();
5601-
$input2['itemtype'] = Ticket::class;
5600+
if (isset($input['_job']) && $input['_job'] instanceof Ticket) {
5601+
$ticket_id = $input['_job']->getID();
5602+
} else {
5603+
$ticket_id = $this->getID();
5604+
}
5605+
$input2["name"] = addslashes(sprintf(__('Document Ticket %d'), $ticket_id));
5606+
$input2["tickets_id"] = $ticket_id;
5607+
$input2['itemtype'] = Ticket::class;
56025608
}
56035609

56045610
if (isset($input['_tag'][$key])) {

0 commit comments

Comments
 (0)