From c6ae9f3f24409a32ce05d8278a346cd130ea9d54 Mon Sep 17 00:00:00 2001 From: Libin Pan Date: Mon, 7 Jun 2021 19:36:08 -0700 Subject: [PATCH] Duplicate tags when moving and duplicating tasks to another project --- app/Model/TagDuplicationModel.php | 21 +++++++++++++------ app/Model/TaskTagModel.php | 4 ++-- .../Model/TaskProjectDuplicationModelTest.php | 3 ++- .../units/Model/TaskProjectMoveModelTest.php | 3 ++- tests/units/Model/TaskTagModelTest.php | 9 ++++++-- 5 files changed, 28 insertions(+), 12 deletions(-) diff --git a/app/Model/TagDuplicationModel.php b/app/Model/TagDuplicationModel.php index ac54f5d02..ebca395b2 100644 --- a/app/Model/TagDuplicationModel.php +++ b/app/Model/TagDuplicationModel.php @@ -47,9 +47,11 @@ class TagDuplicationModel extends Base foreach ($tags as $tag) { $tag_id = $this->tagModel->getIdByName($dst_project_id, $tag['name']); - if ($tag_id) { - $this->taskTagModel->associateTag($dst_task_id, $tag_id); + if (empty($tag_id)) { + $tag_id = $this->tagModel->create($dst_project_id, $tag['name'], $tag['color_id']); } + + $this->taskTagModel->associateTag($dst_task_id, $tag_id); } } @@ -70,7 +72,7 @@ class TagDuplicationModel extends Base } /** - * Remove tags that are not available in destination project + * Sync tags that are not available in destination project * * @access public * @param integer $task_id @@ -78,10 +80,17 @@ class TagDuplicationModel extends Base */ public function syncTaskTagsToAnotherProject($task_id, $dst_project_id) { - $tag_ids = $this->taskTagModel->getTagIdsByTaskNotAvailableInProject($task_id, $dst_project_id); + $tags = $this->taskTagModel->getTagsByTaskNotAvailableInProject($task_id, $dst_project_id); - foreach ($tag_ids as $tag_id) { - $this->taskTagModel->dissociateTag($task_id, $tag_id); + foreach ($tags as $tag) { + $tag_id = $this->tagModel->getIdByName($dst_project_id, $tag['name']); + + if (empty($tag_id)) { + $tag_id = $this->tagModel->create($dst_project_id, $tag['name'], $tag['color_id']); + } + + $this->taskTagModel->dissociateTag($task_id, $tag['id']); + $this->taskTagModel->associateTag($task_id, $tag_id); } } } diff --git a/app/Model/TaskTagModel.php b/app/Model/TaskTagModel.php index 14d2db484..94c6f0785 100644 --- a/app/Model/TaskTagModel.php +++ b/app/Model/TaskTagModel.php @@ -27,13 +27,13 @@ class TaskTagModel extends Base * @param integer $project_id * @return array */ - public function getTagIdsByTaskNotAvailableInProject($task_id, $project_id) + public function getTagsByTaskNotAvailableInProject($task_id, $project_id) { return $this->db->table(TagModel::TABLE) ->eq(self::TABLE.'.task_id', $task_id) ->notIn(TagModel::TABLE.'.project_id', array(0, $project_id)) ->join(self::TABLE, 'tag_id', 'id') - ->findAllByColumn(TagModel::TABLE.'.id'); + ->findAll(); } /** diff --git a/tests/units/Model/TaskProjectDuplicationModelTest.php b/tests/units/Model/TaskProjectDuplicationModelTest.php index 68e37cd57..5ca91718a 100644 --- a/tests/units/Model/TaskProjectDuplicationModelTest.php +++ b/tests/units/Model/TaskProjectDuplicationModelTest.php @@ -370,8 +370,9 @@ class TaskProjectDuplicationModelTest extends Base // Check tags $tags = $taskTagModel->getList(2); - $this->assertCount(2, $tags); + $this->assertCount(3, $tags); $this->assertArrayHasKey(3, $tags); $this->assertArrayHasKey(6, $tags); + $this->assertArrayHasKey(7, $tags); } } diff --git a/tests/units/Model/TaskProjectMoveModelTest.php b/tests/units/Model/TaskProjectMoveModelTest.php index 28155b8fe..9782cd100 100644 --- a/tests/units/Model/TaskProjectMoveModelTest.php +++ b/tests/units/Model/TaskProjectMoveModelTest.php @@ -270,8 +270,9 @@ class TaskProjectMoveModelTest extends Base // Check tags $tags = $taskTagModel->getList(1); - $this->assertCount(2, $tags); + $this->assertCount(3, $tags); $this->assertArrayHasKey(5, $tags); $this->assertArrayHasKey(6, $tags); + $this->assertArrayHasKey(7, $tags); } } diff --git a/tests/units/Model/TaskTagModelTest.php b/tests/units/Model/TaskTagModelTest.php index f83bb05a1..0334e7392 100644 --- a/tests/units/Model/TaskTagModelTest.php +++ b/tests/units/Model/TaskTagModelTest.php @@ -129,7 +129,7 @@ class TaskTagModelTest extends Base $this->assertEquals(array(), $tags); } - public function testGetTagIdNotAvailableInDestinationProject() + public function testGetTagsNotAvailableInDestinationProject() { $projectModel = new ProjectModel($this->container); $taskCreationModel = new TaskCreationModel($this->container); @@ -147,6 +147,11 @@ class TaskTagModelTest extends Base $this->assertEquals(5, $tagModel->create(1, 'T3')); $this->assertTrue($taskTagModel->save(1, 1, array('T0', 'T2', 'T3'))); - $this->assertEquals(array(4, 5), $taskTagModel->getTagIdsByTaskNotAvailableInProject(1, 2)); + $tags = $taskTagModel->getTagsByTaskNotAvailableInProject(1, 2); + $this->assertCount(2, $tags); + $this->assertEquals('T2', $tags[0]['name']); + $this->assertEquals(1, $tags[0]['project_id']); + $this->assertEquals('T3', $tags[1]['name']); + $this->assertEquals(1, $tags[1]['project_id']); } }