From 59a4c7f73bd32e6646cf32331546dd1f6e0c4ead Mon Sep 17 00:00:00 2001 From: mundry <1453314+mundry@users.noreply.github.com> Date: Sat, 25 Mar 2023 03:48:32 +0100 Subject: [PATCH] Duplicate attachments & external links during task duplication & importing --- app/Model/FileModel.php | 11 ++++++++--- app/Model/TaskProjectDuplicationModel.php | 18 ++++++++++++++++++ .../Action/TaskDuplicateAnotherProjectTest.php | 6 ++++++ .../Model/ProjectDuplicationModelTest.php | 18 ++++++++++++++++++ tests/units/Model/TaskFileModelTest.php | 2 +- .../Model/TaskProjectDuplicationModelTest.php | 11 +++++++++++ 6 files changed, 62 insertions(+), 4 deletions(-) diff --git a/app/Model/FileModel.php b/app/Model/FileModel.php index 1519cfafc..bb3afd722 100644 --- a/app/Model/FileModel.php +++ b/app/Model/FileModel.php @@ -199,10 +199,15 @@ abstract class FileModel extends Base $this->fireDestructionEvent($file_id); $file = $this->getById($file_id); - $this->objectStorage->remove($file['path']); - if ($file['is_image'] == 1) { - $this->objectStorage->remove($this->getThumbnailPath($file['path'])); + // Only remove files from disk attached to a single task. + $multiple_tasks_count = $this->db->table($this->getTable())->eq('path', $file['path'])->count(); + if ($multiple_tasks_count === 1) { + $this->objectStorage->remove($file['path']); + + if ($file['is_image'] == 1) { + $this->objectStorage->remove($this->getThumbnailPath($file['path'])); + } } return $this->db->table($this->getTable())->eq('id', $file['id'])->remove(); diff --git a/app/Model/TaskProjectDuplicationModel.php b/app/Model/TaskProjectDuplicationModel.php index 57ac0e90b..2a0bf1bc8 100644 --- a/app/Model/TaskProjectDuplicationModel.php +++ b/app/Model/TaskProjectDuplicationModel.php @@ -31,6 +31,24 @@ class TaskProjectDuplicationModel extends TaskDuplicationModel if ($new_task_id !== false) { $this->tagDuplicationModel->duplicateTaskTagsToAnotherProject($task_id, $new_task_id, $project_id); $this->taskLinkModel->create($new_task_id, $task_id, 4); + + $attachments = $this->taskFileModel->getAll($task_id); + $externalLinks = $this->taskExternalLinkModel->getAll($task_id); + + foreach ($attachments as $attachment) { + $this->taskFileModel->create($new_task_id, $attachment['name'], $attachment['path'], $attachment['size']); + } + + foreach ($externalLinks as $externalLink) { + $this->taskExternalLinkModel->create([ + 'task_id' => $new_task_id, + 'creator_id' => $externalLink['creator_id'], + 'dependency' => $externalLink['dependency'], + 'title' => $externalLink['title'], + 'link_type' => $externalLink['link_type'], + 'url' => $externalLink['url'], + ]); + } } $hook_values = [ 'source_task_id' => $task_id, 'destination_task_id' => $new_task_id]; diff --git a/tests/units/Action/TaskDuplicateAnotherProjectTest.php b/tests/units/Action/TaskDuplicateAnotherProjectTest.php index 5cd0c9778..676240ee0 100644 --- a/tests/units/Action/TaskDuplicateAnotherProjectTest.php +++ b/tests/units/Action/TaskDuplicateAnotherProjectTest.php @@ -13,6 +13,12 @@ class TaskDuplicateAnotherProjectTest extends Base { public function testSuccess() { + $this->container['externalLinkManager'] = $this + ->getMockBuilder('Kanboard\Core\ExternalLink\ExternalLinkManager') + ->setConstructorArgs(array($this->container)) + ->setMethods(['push']) + ->getMock(); + $projectModel = new ProjectModel($this->container); $taskCreationModel = new TaskCreationModel($this->container); $taskFinderModel = new TaskFinderModel($this->container); diff --git a/tests/units/Model/ProjectDuplicationModelTest.php b/tests/units/Model/ProjectDuplicationModelTest.php index c0d954d8e..859b1ec3a 100644 --- a/tests/units/Model/ProjectDuplicationModelTest.php +++ b/tests/units/Model/ProjectDuplicationModelTest.php @@ -498,6 +498,12 @@ class ProjectDuplicationModelTest extends Base public function testCloneProjectWithTasks() { + $this->container['externalLinkManager'] = $this + ->getMockBuilder('Kanboard\Core\ExternalLink\ExternalLinkManager') + ->setConstructorArgs(array($this->container)) + ->setMethods(['push']) + ->getMock(); + $projectModel = new ProjectModel($this->container); $projectDuplicationModel = new ProjectDuplicationModel($this->container); $taskCreationModel = new TaskCreationModel($this->container); @@ -522,6 +528,12 @@ class ProjectDuplicationModelTest extends Base public function testCloneProjectWithSwimlanesAndTasks() { + $this->container['externalLinkManager'] = $this + ->getMockBuilder('Kanboard\Core\ExternalLink\ExternalLinkManager') + ->setConstructorArgs(array($this->container)) + ->setMethods(['push']) + ->getMock(); + $projectModel = new ProjectModel($this->container); $projectDuplicationModel = new ProjectDuplicationModel($this->container); $swimlaneModel = new SwimlaneModel($this->container); @@ -572,6 +584,12 @@ class ProjectDuplicationModelTest extends Base public function testCloneProjectWithTags() { + $this->container['externalLinkManager'] = $this + ->getMockBuilder('Kanboard\Core\ExternalLink\ExternalLinkManager') + ->setConstructorArgs(array($this->container)) + ->setMethods(['push']) + ->getMock(); + $projectModel = new ProjectModel($this->container); $projectDuplicationModel = new ProjectDuplicationModel($this->container); $taskCreationModel = new TaskCreationModel($this->container); diff --git a/tests/units/Model/TaskFileModelTest.php b/tests/units/Model/TaskFileModelTest.php index 5cfc5c0da..4fa849025 100644 --- a/tests/units/Model/TaskFileModelTest.php +++ b/tests/units/Model/TaskFileModelTest.php @@ -435,7 +435,7 @@ class TaskFileModelTest extends Base $this->assertEquals(2, $fileModel->create(1, 'test', 'tmp/foo', 10)); $this->container['objectStorage'] - ->expects($this->exactly(2)) + ->expects($this->exactly(1)) ->method('remove') ->with('tmp/foo'); diff --git a/tests/units/Model/TaskProjectDuplicationModelTest.php b/tests/units/Model/TaskProjectDuplicationModelTest.php index 8c4a0d473..8043b2577 100644 --- a/tests/units/Model/TaskProjectDuplicationModelTest.php +++ b/tests/units/Model/TaskProjectDuplicationModelTest.php @@ -17,6 +17,17 @@ use Kanboard\Model\UserModel; class TaskProjectDuplicationModelTest extends Base { + protected function setUp(): void + { + parent::setUp(); + + $this->container['externalLinkManager'] = $this + ->getMockBuilder('Kanboard\Core\ExternalLink\ExternalLinkManager') + ->setConstructorArgs(array($this->container)) + ->setMethods(['push']) + ->getMock(); + } + public function testDuplicateAnotherProject() { $taskProjectDuplicationModel = new TaskProjectDuplicationModel($this->container);