Skip to content

Commit d032fb8

Browse files
committed
categorie ticket in follow, task and solution + tests
1 parent bec73d3 commit d032fb8

File tree

2 files changed

+365
-1
lines changed

2 files changed

+365
-1
lines changed

phpunit/functional/DocumentTest.php

Lines changed: 364 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1241,4 +1241,368 @@ public function testDefaultDocumentCategoryForTicket()
12411241
$this->assertTrue($document->getFromDB(current($data)['documents_id']));
12421242
$this->assertEquals($documentCategory_id, $document->fields['documentcategories_id']);
12431243
}
1244+
1245+
public function testDefaultDocumentCategoryForTicketWithChangeTask()
1246+
{
1247+
global $CFG_GLPI;
1248+
$documentCategory = new DocumentCategory();
1249+
1250+
/////////////////////////////////////////////////////////////////////////////////////////
1251+
// Update config to have default category for document uploaded during ticket creation //
1252+
/////////////////////////////////////////////////////////////////////////////////////////
1253+
$documentCategory_id = $documentCategory->add([
1254+
'name' => 'Default Category',
1255+
]);
1256+
$this->assertGreaterThan(0, $documentCategory_id);
1257+
$CFG_GLPI['documentcategories_id_forticket'] = $documentCategory_id;
1258+
1259+
$input = [
1260+
'name' => 'Ticket 1',
1261+
'content' => 'testDefaultDocumentCategoryFromDocumentForm',
1262+
'entities_id' => 0,
1263+
];
1264+
$ticket_id = $this->createItem(\Ticket::class, $input)->getID();
1265+
1266+
$this->assertGreaterThan(0, $ticket_id);
1267+
1268+
$ticketTask = new \TicketTask();
1269+
$filename = 'wdgrgserh5515rgg.222222' . 'foo.txt';
1270+
1271+
//ajouter un ticket tast avec un document et vérifier si ce document a bien la catégorie par defaut
1272+
$input2 = [
1273+
'tickets_id' => $ticket_id,
1274+
'content' => 'test ticket task with document',
1275+
'_filename' => [
1276+
$filename,
1277+
],
1278+
'_tag_filename' => [ '564grgt4-684vfv8-fvs8b81.0000',
1279+
],
1280+
'_prefix_filename' => [
1281+
'wdgrgserh5515rgg.222222',
1282+
],
1283+
];
1284+
1285+
copy(FIXTURE_DIR . '/uploads/foo.txt', GLPI_TMP_DIR . '/' . $filename);
1286+
$ticketTask_id = $ticketTask->add($input2);
1287+
$this->assertGreaterThan(0, $ticketTask_id);
1288+
1289+
$document = new \Document();
1290+
$document_item = new \Document_Item();
1291+
1292+
$data = $document_item->find([
1293+
'itemtype' => \TicketTask::class,
1294+
'items_id' => $ticketTask_id,
1295+
]);
1296+
$this->assertCount(1, $data);
1297+
1298+
$this->assertTrue($document->getFromDB(current($data)['documents_id']));
1299+
$this->assertEquals($documentCategory_id, $document->fields['documentcategories_id']);
1300+
1301+
}
1302+
1303+
public function testDefaultDocumentCategoryForTicketWithITILFollowup()
1304+
{
1305+
$this->login();
1306+
global $CFG_GLPI;
1307+
$documentCategory = new DocumentCategory();
1308+
1309+
/////////////////////////////////////////////////////////////////////////////////////////
1310+
// Update config to have default category for document uploaded during ticket creation //
1311+
/////////////////////////////////////////////////////////////////////////////////////////
1312+
$documentCategory_id = $documentCategory->add([
1313+
'name' => 'Default Category',
1314+
]);
1315+
1316+
$CFG_GLPI['documentcategories_id_forticket'] = $documentCategory_id;
1317+
1318+
$ticket = new \Ticket();
1319+
$tickets_id = $ticket->add([
1320+
'name' => 'Ticket for ITIL followup',
1321+
'content' => 'Ticket content for followup',
1322+
'entities_id' => 0,
1323+
]);
1324+
$this->assertGreaterThan(0, $tickets_id);
1325+
1326+
$itilFollowup = new \ITILFollowup();
1327+
$filename = 'itilfollowup_doc.999999' . 'foo.txt';
1328+
$inputFollowup = [
1329+
'items_id' => $tickets_id,
1330+
'itemtype' => \Ticket::class,
1331+
'content' => 'Followup with document',
1332+
'_filename' => [$filename],
1333+
'_tag_filename' => ['tag-itilfollowup-999999'],
1334+
'_prefix_filename' => ['itilfollowup_doc.999999'],
1335+
];
1336+
1337+
copy(FIXTURE_DIR . '/uploads/foo.txt', GLPI_TMP_DIR . '/' . $filename);
1338+
$itilFollowups_id = $itilFollowup->add($inputFollowup);
1339+
$this->assertGreaterThan(0, $itilFollowups_id);
1340+
1341+
$document = new \Document();
1342+
$document_item = new \Document_Item();
1343+
1344+
$data = $document_item->find([
1345+
'itemtype' => \ITILFollowup::class,
1346+
'items_id' => $itilFollowups_id,
1347+
]);
1348+
$this->assertCount(1, $data);
1349+
1350+
$this->assertTrue($document->getFromDB(current($data)['documents_id']));
1351+
$this->assertEquals($documentCategory_id, $document->fields['documentcategories_id']);
1352+
1353+
}
1354+
1355+
public function testDefaultDocumentCategoryForTicketWithITILSolution()
1356+
{
1357+
$this->login();
1358+
global $CFG_GLPI;
1359+
$documentCategory = new DocumentCategory();
1360+
1361+
/////////////////////////////////////////////////////////////////////////////////////////
1362+
// Update config to have default category for document uploaded during ticket creation //
1363+
/////////////////////////////////////////////////////////////////////////////////////////
1364+
$documentCategory_id = $documentCategory->add([
1365+
'name' => 'Default Category',
1366+
]);
1367+
1368+
$CFG_GLPI['documentcategories_id_forticket'] = $documentCategory_id;
1369+
1370+
$ticket = new \Ticket();
1371+
$tickets_id = $ticket->add([
1372+
'name' => 'Ticket for ITIL solution',
1373+
'content' => 'Ticket content for solution',
1374+
'entities_id' => 0,
1375+
]);
1376+
$this->assertGreaterThan(0, $tickets_id);
1377+
1378+
$itilSolution = new \ITILSolution();
1379+
$filename = 'itilsolution_doc.888888' . 'foo.txt';
1380+
$inputSolution = [
1381+
'items_id' => $tickets_id,
1382+
'itemtype' => \Ticket::class,
1383+
'content' => 'Solution with document',
1384+
'_filename' => [$filename],
1385+
'_tag_filename' => ['tag-itilsolution-888888'],
1386+
'_prefix_filename' => ['itilsolution_doc.888888'],
1387+
];
1388+
copy(FIXTURE_DIR . '/uploads/foo.txt', GLPI_TMP_DIR . '/' . $filename);
1389+
$itilSolutions_id = $itilSolution->add($inputSolution);
1390+
$this->assertGreaterThan(0, $itilSolutions_id);
1391+
1392+
$document = new \Document();
1393+
$document_item = new \Document_Item();
1394+
1395+
$data = $document_item->find([
1396+
'itemtype' => \ITILSolution::class,
1397+
'items_id' => $itilSolutions_id,
1398+
]);
1399+
$this->assertCount(1, $data);
1400+
$this->assertTrue($document->getFromDB(current($data)['documents_id']));
1401+
$this->assertEquals($documentCategory_id, $document->fields['documentcategories_id']);
1402+
1403+
}
1404+
1405+
public function testDefaultDocumentCategoryForChange()
1406+
{
1407+
$this->login();
1408+
1409+
$document = new \Document();
1410+
$document_item = new \Document_Item();
1411+
1412+
///////////////////////////////////////////////////////////////////////
1413+
// Create Change with document, check document has no category //
1414+
///////////////////////////////////////////////////////////////////////
1415+
$change = new \Change();
1416+
$changes_id = $change->add([
1417+
'name' => "test new change",
1418+
'content' => "test new change",
1419+
]);
1420+
1421+
$this->assertGreaterThan(0, $changes_id);
1422+
1423+
// ajouter un document à un change
1424+
$filename = 'wdgrgserh5515rgg.222222' . 'foo.txt';
1425+
$input = [
1426+
'name' => 'Change 1 Document',
1427+
'content' => 'testUploadDocumentWithoutCategory',
1428+
'_filename' => [
1429+
$filename,
1430+
],
1431+
'_tag_filename' => [ '564grgt4-684vfv8-fvs8b81.0000',
1432+
],
1433+
'_prefix_filename' => [
1434+
'wdgrgserh5515rgg.222222',
1435+
],
1436+
'itemtype' => \Change::class,
1437+
'items_id' => $changes_id,
1438+
];
1439+
copy(FIXTURE_DIR . '/uploads/foo.txt', GLPI_TMP_DIR . '/' . $filename);
1440+
$doc_id = $document->add($input);
1441+
$this->assertGreaterThan(0, $doc_id);
1442+
$data = $document_item->find([
1443+
'itemtype' => \Change::class,
1444+
'items_id' => $changes_id,
1445+
]);
1446+
$this->assertCount(1, $data);
1447+
$this->assertTrue($document->getFromDB(current($data)['documents_id']));
1448+
$this->assertEquals(0, $document->fields['documentcategories_id']);
1449+
1450+
}
1451+
1452+
public function testDefaultDocumentCategoryForChangeWithChangeTask()
1453+
{
1454+
$this->login();
1455+
1456+
$change = new \Change();
1457+
$changes_id = $change->add([
1458+
'name' => "test new change",
1459+
'content' => "test new change",
1460+
]);
1461+
$this->assertGreaterThan(0, $changes_id);
1462+
1463+
// add a change task to the change
1464+
$changeTask = new \ChangeTask();
1465+
$changeTasks_id = $changeTask->add([
1466+
'changes_id' => $changes_id,
1467+
'name' => "test change task",
1468+
'content' => "test change task",
1469+
]);
1470+
$this->assertGreaterThan(0, $changeTasks_id);
1471+
$document = new \Document();
1472+
$document_item = new \Document_Item();
1473+
$filename = 'wdgrgserh5515rgg.222222' . 'foo.txt';
1474+
$input = [
1475+
'name' => 'ChangeTask Document',
1476+
'content' => 'testUploadDocumentWithoutCategory',
1477+
'_filename' => [
1478+
$filename,
1479+
],
1480+
'_tag_filename' => [ '564grgt4-684vfv8-fvs8b81.0000',
1481+
],
1482+
'_prefix_filename' => [
1483+
'wdgrgserh5515rgg.222222',
1484+
],
1485+
'itemtype' => \ChangeTask::class,
1486+
'items_id' => $changeTasks_id,
1487+
];
1488+
copy(FIXTURE_DIR . '/uploads/foo.txt', GLPI_TMP_DIR . '/' . $filename);
1489+
$doc_id = $document->add($input);
1490+
$this->assertGreaterThan(0, $doc_id);
1491+
$data = $document_item->find([
1492+
'itemtype' => \ChangeTask::class,
1493+
'items_id' => $changeTasks_id,
1494+
]);
1495+
$this->assertCount(1, $data);
1496+
$this->assertTrue($document->getFromDB(current($data)['documents_id']));
1497+
$this->assertEquals(0, $document->fields['documentcategories_id']);
1498+
}
1499+
1500+
public function testDefaultDocumentCategoryForChangeWithITILFollowup()
1501+
{
1502+
1503+
$this->login();
1504+
global $CFG_GLPI;
1505+
$documentCategory = new DocumentCategory();
1506+
1507+
/////////////////////////////////////////////////////////////////////////////////////////
1508+
// Update config to have default category for document uploaded during ticket creation //
1509+
/////////////////////////////////////////////////////////////////////////////////////////
1510+
$documentCategory_id = $documentCategory->add([
1511+
'name' => 'Default Category',
1512+
]);
1513+
1514+
$change = new \Change();
1515+
$changes_id = $change->add([
1516+
'name' => "test new change",
1517+
'content' => "test new change",
1518+
]);
1519+
$this->assertGreaterThan(0, $changes_id);
1520+
1521+
// add an itil followup to the change
1522+
$itilFollowup = new \ITILFollowup();
1523+
$itilFollowups_id = $itilFollowup->add([
1524+
'items_id' => $changes_id,
1525+
'itemtype' => \Change::class,
1526+
'content' => "test itil followup",
1527+
]);
1528+
$this->assertGreaterThan(0, $itilFollowups_id);
1529+
$document = new \Document();
1530+
$document_item = new \Document_Item();
1531+
$filename = 'wdgrgserh5515rgg.222222' . 'foo.txt';
1532+
$input = [
1533+
'name' => 'ITILFollowup Document',
1534+
'content' => 'testUploadDocumentWithoutCategory',
1535+
'_filename' => [
1536+
$filename,
1537+
],
1538+
'_tag_filename' => ['564grgt4-684vfv8-fvs8b81.0000',
1539+
],
1540+
'_prefix_filename' => [
1541+
'wdgrgserh5515rgg.222222',
1542+
],
1543+
'itemtype' => \ITILFollowup::class,
1544+
'items_id' => $itilFollowups_id,
1545+
];
1546+
copy(FIXTURE_DIR . '/uploads/foo.txt', GLPI_TMP_DIR . '/' . $filename);
1547+
$doc_id = $document->add($input);
1548+
$this->assertGreaterThan(0, $doc_id);
1549+
$data = $document_item->find([
1550+
'itemtype' => \ITILFollowup::class,
1551+
'items_id' => $itilFollowups_id,
1552+
]);
1553+
$this->assertCount(1, $data);
1554+
$this->assertTrue($document->getFromDB(current($data)['documents_id']));
1555+
$this->assertEquals(0, $document->fields['documentcategories_id']);
1556+
}
1557+
1558+
public function testDefaultDocumentCategoryForChangeWithITILSolution()
1559+
{
1560+
$this->login();
1561+
global $CFG_GLPI;
1562+
$documentCategory = new DocumentCategory();
1563+
1564+
/////////////////////////////////////////////////////////////////////////////////////////
1565+
// Update config to have default category for document uploaded during ticket creation //
1566+
/////////////////////////////////////////////////////////////////////////////////////////
1567+
$documentCategory_id = $documentCategory->add([
1568+
'name' => 'Default Category',
1569+
]);
1570+
1571+
$CFG_GLPI['documentcategories_id_forticket'] = $documentCategory_id;
1572+
1573+
$change = new \Change();
1574+
$changes_id = $change->add([
1575+
'name' => "test new change",
1576+
'content' => "test new change",
1577+
]);
1578+
$this->assertGreaterThan(0, $changes_id);
1579+
1580+
$itilSolution = new \ITILSolution();
1581+
$filename = 'itilsolution_doc.888888' . 'foo.txt';
1582+
$inputSolution = [
1583+
'items_id' => $changes_id,
1584+
'itemtype' => \Change::class,
1585+
'content' => 'Solution with document',
1586+
'_filename' => [$filename],
1587+
'_tag_filename' => ['tag-itilsolution-888888'],
1588+
'_prefix_filename' => ['itilsolution_doc.888888'],
1589+
];
1590+
copy(FIXTURE_DIR . '/uploads/foo.txt', GLPI_TMP_DIR . '/' .$filename);
1591+
$itilSolutions_id = $itilSolution->add($inputSolution);
1592+
$this->assertGreaterThan(0, $itilSolutions_id);
1593+
1594+
$document = new \Document();
1595+
$document_item = new \Document_item();
1596+
1597+
$data = $document_item->find([
1598+
'itemtype' => \ITILSolution::class,
1599+
'items_id' => $itilSolutions_id,
1600+
]);
1601+
1602+
$this->assertCount(1, $data);
1603+
$this->assertTrue($document->getFromDB(current($data)['documents_id']));
1604+
$this->assertEquals(0, $document->fields['documentcategories_id']);
1605+
1606+
}
1607+
12441608
}

src/CommonDBTM.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5594,7 +5594,7 @@ public function addFiles(array $input, $options = [])
55945594
$doc->update($input2);
55955595
}
55965596
} else {
5597-
if ($this->getType() == 'Ticket') {
5597+
if ($this->getType() == 'Ticket' || (isset($input['_job']) && $input['_job'] instanceof Ticket)) {
55985598
//TRANS: Default document to files attached to tickets : %d is the ticket id
55995599
$input2["name"] = addslashes(sprintf(__('Document Ticket %d'), $this->getID()));
56005600
$input2["tickets_id"] = $this->getID();

0 commit comments

Comments
 (0)