From 4cabe17612031ead7f605d7ab8d87c6e43881a85 Mon Sep 17 00:00:00 2001 From: Herafia Date: Mon, 1 Dec 2025 16:50:23 +0100 Subject: [PATCH 1/6] categorie ticket in follow, task and solution + tests --- phpunit/functional/DocumentTest.php | 364 ++++++++++++++++++++++++++++ src/CommonDBTM.php | 2 +- 2 files changed, 365 insertions(+), 1 deletion(-) diff --git a/phpunit/functional/DocumentTest.php b/phpunit/functional/DocumentTest.php index 531a1917fea..9bc7b6796b0 100644 --- a/phpunit/functional/DocumentTest.php +++ b/phpunit/functional/DocumentTest.php @@ -1345,4 +1345,368 @@ public function testDefaultDocumentCategoryForTicket() $this->assertTrue($document->getFromDB(current($data)['documents_id'])); $this->assertEquals($documentCategory_id, $document->fields['documentcategories_id']); } + + public function testDefaultDocumentCategoryForTicketWithChangeTask() + { + global $CFG_GLPI; + $documentCategory = new DocumentCategory(); + + ///////////////////////////////////////////////////////////////////////////////////////// + // Update config to have default category for document uploaded during ticket creation // + ///////////////////////////////////////////////////////////////////////////////////////// + $documentCategory_id = $documentCategory->add([ + 'name' => 'Default Category', + ]); + $this->assertGreaterThan(0, $documentCategory_id); + $CFG_GLPI['documentcategories_id_forticket'] = $documentCategory_id; + + $input = [ + 'name' => 'Ticket 1', + 'content' => 'testDefaultDocumentCategoryFromDocumentForm', + 'entities_id' => 0, + ]; + $ticket_id = $this->createItem(\Ticket::class, $input)->getID(); + + $this->assertGreaterThan(0, $ticket_id); + + $ticketTask = new \TicketTask(); + $filename = 'wdgrgserh5515rgg.222222' . 'foo.txt'; + + //ajouter un ticket tast avec un document et vérifier si ce document a bien la catégorie par defaut + $input2 = [ + 'tickets_id' => $ticket_id, + 'content' => 'test ticket task with document', + '_filename' => [ + $filename, + ], + '_tag_filename' => [ '564grgt4-684vfv8-fvs8b81.0000', + ], + '_prefix_filename' => [ + 'wdgrgserh5515rgg.222222', + ], + ]; + + copy(FIXTURE_DIR . '/uploads/foo.txt', GLPI_TMP_DIR . '/' . $filename); + $ticketTask_id = $ticketTask->add($input2); + $this->assertGreaterThan(0, $ticketTask_id); + + $document = new \Document(); + $document_item = new \Document_Item(); + + $data = $document_item->find([ + 'itemtype' => \TicketTask::class, + 'items_id' => $ticketTask_id, + ]); + $this->assertCount(1, $data); + + $this->assertTrue($document->getFromDB(current($data)['documents_id'])); + $this->assertEquals($documentCategory_id, $document->fields['documentcategories_id']); + + } + + public function testDefaultDocumentCategoryForTicketWithITILFollowup() + { + $this->login(); + global $CFG_GLPI; + $documentCategory = new DocumentCategory(); + + ///////////////////////////////////////////////////////////////////////////////////////// + // Update config to have default category for document uploaded during ticket creation // + ///////////////////////////////////////////////////////////////////////////////////////// + $documentCategory_id = $documentCategory->add([ + 'name' => 'Default Category', + ]); + + $CFG_GLPI['documentcategories_id_forticket'] = $documentCategory_id; + + $ticket = new \Ticket(); + $tickets_id = $ticket->add([ + 'name' => 'Ticket for ITIL followup', + 'content' => 'Ticket content for followup', + 'entities_id' => 0, + ]); + $this->assertGreaterThan(0, $tickets_id); + + $itilFollowup = new \ITILFollowup(); + $filename = 'itilfollowup_doc.999999' . 'foo.txt'; + $inputFollowup = [ + 'items_id' => $tickets_id, + 'itemtype' => \Ticket::class, + 'content' => 'Followup with document', + '_filename' => [$filename], + '_tag_filename' => ['tag-itilfollowup-999999'], + '_prefix_filename' => ['itilfollowup_doc.999999'], + ]; + + copy(FIXTURE_DIR . '/uploads/foo.txt', GLPI_TMP_DIR . '/' . $filename); + $itilFollowups_id = $itilFollowup->add($inputFollowup); + $this->assertGreaterThan(0, $itilFollowups_id); + + $document = new \Document(); + $document_item = new \Document_Item(); + + $data = $document_item->find([ + 'itemtype' => \ITILFollowup::class, + 'items_id' => $itilFollowups_id, + ]); + $this->assertCount(1, $data); + + $this->assertTrue($document->getFromDB(current($data)['documents_id'])); + $this->assertEquals($documentCategory_id, $document->fields['documentcategories_id']); + + } + + public function testDefaultDocumentCategoryForTicketWithITILSolution() + { + $this->login(); + global $CFG_GLPI; + $documentCategory = new DocumentCategory(); + + ///////////////////////////////////////////////////////////////////////////////////////// + // Update config to have default category for document uploaded during ticket creation // + ///////////////////////////////////////////////////////////////////////////////////////// + $documentCategory_id = $documentCategory->add([ + 'name' => 'Default Category', + ]); + + $CFG_GLPI['documentcategories_id_forticket'] = $documentCategory_id; + + $ticket = new \Ticket(); + $tickets_id = $ticket->add([ + 'name' => 'Ticket for ITIL solution', + 'content' => 'Ticket content for solution', + 'entities_id' => 0, + ]); + $this->assertGreaterThan(0, $tickets_id); + + $itilSolution = new \ITILSolution(); + $filename = 'itilsolution_doc.888888' . 'foo.txt'; + $inputSolution = [ + 'items_id' => $tickets_id, + 'itemtype' => \Ticket::class, + 'content' => 'Solution with document', + '_filename' => [$filename], + '_tag_filename' => ['tag-itilsolution-888888'], + '_prefix_filename' => ['itilsolution_doc.888888'], + ]; + copy(FIXTURE_DIR . '/uploads/foo.txt', GLPI_TMP_DIR . '/' . $filename); + $itilSolutions_id = $itilSolution->add($inputSolution); + $this->assertGreaterThan(0, $itilSolutions_id); + + $document = new \Document(); + $document_item = new \Document_Item(); + + $data = $document_item->find([ + 'itemtype' => \ITILSolution::class, + 'items_id' => $itilSolutions_id, + ]); + $this->assertCount(1, $data); + $this->assertTrue($document->getFromDB(current($data)['documents_id'])); + $this->assertEquals($documentCategory_id, $document->fields['documentcategories_id']); + + } + + public function testDefaultDocumentCategoryForChange() + { + $this->login(); + + $document = new \Document(); + $document_item = new \Document_Item(); + + /////////////////////////////////////////////////////////////////////// + // Create Change with document, check document has no category // + /////////////////////////////////////////////////////////////////////// + $change = new \Change(); + $changes_id = $change->add([ + 'name' => "test new change", + 'content' => "test new change", + ]); + + $this->assertGreaterThan(0, $changes_id); + + // ajouter un document à un change + $filename = 'wdgrgserh5515rgg.222222' . 'foo.txt'; + $input = [ + 'name' => 'Change 1 Document', + 'content' => 'testUploadDocumentWithoutCategory', + '_filename' => [ + $filename, + ], + '_tag_filename' => [ '564grgt4-684vfv8-fvs8b81.0000', + ], + '_prefix_filename' => [ + 'wdgrgserh5515rgg.222222', + ], + 'itemtype' => \Change::class, + 'items_id' => $changes_id, + ]; + copy(FIXTURE_DIR . '/uploads/foo.txt', GLPI_TMP_DIR . '/' . $filename); + $doc_id = $document->add($input); + $this->assertGreaterThan(0, $doc_id); + $data = $document_item->find([ + 'itemtype' => \Change::class, + 'items_id' => $changes_id, + ]); + $this->assertCount(1, $data); + $this->assertTrue($document->getFromDB(current($data)['documents_id'])); + $this->assertEquals(0, $document->fields['documentcategories_id']); + + } + + public function testDefaultDocumentCategoryForChangeWithChangeTask() + { + $this->login(); + + $change = new \Change(); + $changes_id = $change->add([ + 'name' => "test new change", + 'content' => "test new change", + ]); + $this->assertGreaterThan(0, $changes_id); + + // add a change task to the change + $changeTask = new \ChangeTask(); + $changeTasks_id = $changeTask->add([ + 'changes_id' => $changes_id, + 'name' => "test change task", + 'content' => "test change task", + ]); + $this->assertGreaterThan(0, $changeTasks_id); + $document = new \Document(); + $document_item = new \Document_Item(); + $filename = 'wdgrgserh5515rgg.222222' . 'foo.txt'; + $input = [ + 'name' => 'ChangeTask Document', + 'content' => 'testUploadDocumentWithoutCategory', + '_filename' => [ + $filename, + ], + '_tag_filename' => [ '564grgt4-684vfv8-fvs8b81.0000', + ], + '_prefix_filename' => [ + 'wdgrgserh5515rgg.222222', + ], + 'itemtype' => \ChangeTask::class, + 'items_id' => $changeTasks_id, + ]; + copy(FIXTURE_DIR . '/uploads/foo.txt', GLPI_TMP_DIR . '/' . $filename); + $doc_id = $document->add($input); + $this->assertGreaterThan(0, $doc_id); + $data = $document_item->find([ + 'itemtype' => \ChangeTask::class, + 'items_id' => $changeTasks_id, + ]); + $this->assertCount(1, $data); + $this->assertTrue($document->getFromDB(current($data)['documents_id'])); + $this->assertEquals(0, $document->fields['documentcategories_id']); + } + + public function testDefaultDocumentCategoryForChangeWithITILFollowup() + { + + $this->login(); + global $CFG_GLPI; + $documentCategory = new DocumentCategory(); + + ///////////////////////////////////////////////////////////////////////////////////////// + // Update config to have default category for document uploaded during ticket creation // + ///////////////////////////////////////////////////////////////////////////////////////// + $documentCategory_id = $documentCategory->add([ + 'name' => 'Default Category', + ]); + + $change = new \Change(); + $changes_id = $change->add([ + 'name' => "test new change", + 'content' => "test new change", + ]); + $this->assertGreaterThan(0, $changes_id); + + // add an itil followup to the change + $itilFollowup = new \ITILFollowup(); + $itilFollowups_id = $itilFollowup->add([ + 'items_id' => $changes_id, + 'itemtype' => \Change::class, + 'content' => "test itil followup", + ]); + $this->assertGreaterThan(0, $itilFollowups_id); + $document = new \Document(); + $document_item = new \Document_Item(); + $filename = 'wdgrgserh5515rgg.222222' . 'foo.txt'; + $input = [ + 'name' => 'ITILFollowup Document', + 'content' => 'testUploadDocumentWithoutCategory', + '_filename' => [ + $filename, + ], + '_tag_filename' => ['564grgt4-684vfv8-fvs8b81.0000', + ], + '_prefix_filename' => [ + 'wdgrgserh5515rgg.222222', + ], + 'itemtype' => \ITILFollowup::class, + 'items_id' => $itilFollowups_id, + ]; + copy(FIXTURE_DIR . '/uploads/foo.txt', GLPI_TMP_DIR . '/' . $filename); + $doc_id = $document->add($input); + $this->assertGreaterThan(0, $doc_id); + $data = $document_item->find([ + 'itemtype' => \ITILFollowup::class, + 'items_id' => $itilFollowups_id, + ]); + $this->assertCount(1, $data); + $this->assertTrue($document->getFromDB(current($data)['documents_id'])); + $this->assertEquals(0, $document->fields['documentcategories_id']); + } + + public function testDefaultDocumentCategoryForChangeWithITILSolution() + { + $this->login(); + global $CFG_GLPI; + $documentCategory = new DocumentCategory(); + + ///////////////////////////////////////////////////////////////////////////////////////// + // Update config to have default category for document uploaded during ticket creation // + ///////////////////////////////////////////////////////////////////////////////////////// + $documentCategory_id = $documentCategory->add([ + 'name' => 'Default Category', + ]); + + $CFG_GLPI['documentcategories_id_forticket'] = $documentCategory_id; + + $change = new \Change(); + $changes_id = $change->add([ + 'name' => "test new change", + 'content' => "test new change", + ]); + $this->assertGreaterThan(0, $changes_id); + + $itilSolution = new \ITILSolution(); + $filename = 'itilsolution_doc.888888' . 'foo.txt'; + $inputSolution = [ + 'items_id' => $changes_id, + 'itemtype' => \Change::class, + 'content' => 'Solution with document', + '_filename' => [$filename], + '_tag_filename' => ['tag-itilsolution-888888'], + '_prefix_filename' => ['itilsolution_doc.888888'], + ]; + copy(FIXTURE_DIR . '/uploads/foo.txt', GLPI_TMP_DIR . '/' .$filename); + $itilSolutions_id = $itilSolution->add($inputSolution); + $this->assertGreaterThan(0, $itilSolutions_id); + + $document = new \Document(); + $document_item = new \Document_item(); + + $data = $document_item->find([ + 'itemtype' => \ITILSolution::class, + 'items_id' => $itilSolutions_id, + ]); + + $this->assertCount(1, $data); + $this->assertTrue($document->getFromDB(current($data)['documents_id'])); + $this->assertEquals(0, $document->fields['documentcategories_id']); + + } + } diff --git a/src/CommonDBTM.php b/src/CommonDBTM.php index 947259409e0..5cad80159ec 100644 --- a/src/CommonDBTM.php +++ b/src/CommonDBTM.php @@ -5594,7 +5594,7 @@ public function addFiles(array $input, $options = []) $doc->update($input2); } } else { - if ($this->getType() == 'Ticket') { + if ($this->getType() == 'Ticket' || (isset($input['_job']) && $input['_job'] instanceof Ticket)) { //TRANS: Default document to files attached to tickets : %d is the ticket id $input2["name"] = addslashes(sprintf(__('Document Ticket %d'), $this->getID())); $input2["tickets_id"] = $this->getID(); From 9d758f3aa3209d4ae5628c10e72de5d6bbff3b98 Mon Sep 17 00:00:00 2001 From: Herafia Date: Thu, 4 Dec 2025 09:33:56 +0100 Subject: [PATCH 2/6] get id ticket in _job when is possible --- src/CommonDBTM.php | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/CommonDBTM.php b/src/CommonDBTM.php index 5cad80159ec..e2c813fa968 100644 --- a/src/CommonDBTM.php +++ b/src/CommonDBTM.php @@ -5596,9 +5596,14 @@ public function addFiles(array $input, $options = []) } else { if ($this->getType() == 'Ticket' || (isset($input['_job']) && $input['_job'] instanceof Ticket)) { //TRANS: Default document to files attached to tickets : %d is the ticket id - $input2["name"] = addslashes(sprintf(__('Document Ticket %d'), $this->getID())); - $input2["tickets_id"] = $this->getID(); - $input2['itemtype'] = Ticket::class; + if (isset($input['_job']) && $input['_job'] instanceof Ticket) { + $ticket_id = $input['_job']->getID(); + } else { + $ticket_id = $this->getID(); + } + $input2["name"] = addslashes(sprintf(__('Document Ticket %d'), $ticket_id)); + $input2["tickets_id"] = $ticket_id; + $input2['itemtype'] = Ticket::class; } if (isset($input['_tag'][$key])) { From 5847f9688b24073a242969173638a4a01424c9bf Mon Sep 17 00:00:00 2001 From: Herafia Date: Thu, 4 Dec 2025 10:13:14 +0100 Subject: [PATCH 3/6] lint --- phpunit/functional/DocumentTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/phpunit/functional/DocumentTest.php b/phpunit/functional/DocumentTest.php index 9bc7b6796b0..28738c2edad 100644 --- a/phpunit/functional/DocumentTest.php +++ b/phpunit/functional/DocumentTest.php @@ -1691,7 +1691,7 @@ public function testDefaultDocumentCategoryForChangeWithITILSolution() '_tag_filename' => ['tag-itilsolution-888888'], '_prefix_filename' => ['itilsolution_doc.888888'], ]; - copy(FIXTURE_DIR . '/uploads/foo.txt', GLPI_TMP_DIR . '/' .$filename); + copy(FIXTURE_DIR . '/uploads/foo.txt', GLPI_TMP_DIR . '/' . $filename); $itilSolutions_id = $itilSolution->add($inputSolution); $this->assertGreaterThan(0, $itilSolutions_id); From 742a7b4f4d1b4bd81a5b7b4962ed63f7b587a3ec Mon Sep 17 00:00:00 2001 From: Herafia Date: Thu, 4 Dec 2025 11:12:33 +0100 Subject: [PATCH 4/6] refracto --- src/CommonDBTM.php | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/src/CommonDBTM.php b/src/CommonDBTM.php index e2c813fa968..44229268611 100644 --- a/src/CommonDBTM.php +++ b/src/CommonDBTM.php @@ -5594,13 +5594,8 @@ public function addFiles(array $input, $options = []) $doc->update($input2); } } else { - if ($this->getType() == 'Ticket' || (isset($input['_job']) && $input['_job'] instanceof Ticket)) { - //TRANS: Default document to files attached to tickets : %d is the ticket id - if (isset($input['_job']) && $input['_job'] instanceof Ticket) { - $ticket_id = $input['_job']->getID(); - } else { - $ticket_id = $this->getID(); - } + if (($input['_job'] ?? $this) instanceof Ticket) { + $ticket_id = $input['_job']->getID() ?? $this->getID(); $input2["name"] = addslashes(sprintf(__('Document Ticket %d'), $ticket_id)); $input2["tickets_id"] = $ticket_id; $input2['itemtype'] = Ticket::class; From 859b380fea3b1d24fa7d047c2322cd5f5b038330 Mon Sep 17 00:00:00 2001 From: Herafia Date: Thu, 4 Dec 2025 16:50:20 +0100 Subject: [PATCH 5/6] refracto 2 --- src/CommonDBTM.php | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/CommonDBTM.php b/src/CommonDBTM.php index 44229268611..2bdcf958f64 100644 --- a/src/CommonDBTM.php +++ b/src/CommonDBTM.php @@ -5594,8 +5594,14 @@ public function addFiles(array $input, $options = []) $doc->update($input2); } } else { - if (($input['_job'] ?? $this) instanceof Ticket) { - $ticket_id = $input['_job']->getID() ?? $this->getID(); + + if ($this->getType() == 'Ticket' || (isset($input['_job']) && $input['_job'] instanceof Ticket)) { + //TRANS: Default document to files attached to tickets : %d is the ticket id + if (isset($input['_job']) && $input['_job'] instanceof Ticket) { + $ticket_id = $input['_job']->getID(); + } else { + $ticket_id = $this->getID(); + } $input2["name"] = addslashes(sprintf(__('Document Ticket %d'), $ticket_id)); $input2["tickets_id"] = $ticket_id; $input2['itemtype'] = Ticket::class; From 368cb914210a497c34d0cbe69330747b6d9de322 Mon Sep 17 00:00:00 2001 From: Laura <35998899+Herafia@users.noreply.github.com> Date: Fri, 5 Dec 2025 14:13:28 +0100 Subject: [PATCH 6/6] Update src/CommonDBTM.php Co-authored-by: Johan Cwiklinski --- src/CommonDBTM.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/CommonDBTM.php b/src/CommonDBTM.php index 2bdcf958f64..7eaf4291304 100644 --- a/src/CommonDBTM.php +++ b/src/CommonDBTM.php @@ -5595,7 +5595,7 @@ public function addFiles(array $input, $options = []) } } else { - if ($this->getType() == 'Ticket' || (isset($input['_job']) && $input['_job'] instanceof Ticket)) { + if ($this instanceof Ticket || (isset($input['_job']) && $input['_job'] instanceof Ticket)) { //TRANS: Default document to files attached to tickets : %d is the ticket id if (isset($input['_job']) && $input['_job'] instanceof Ticket) { $ticket_id = $input['_job']->getID();