diff --git a/phpunit/functional/DocumentTest.php b/phpunit/functional/DocumentTest.php index 531a1917fea..28738c2edad 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..7eaf4291304 100644 --- a/src/CommonDBTM.php +++ b/src/CommonDBTM.php @@ -5594,11 +5594,17 @@ public function addFiles(array $input, $options = []) $doc->update($input2); } } else { - if ($this->getType() == '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 - $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])) {