Duplicate tags when moving and duplicating tasks to another project

This commit is contained in:
Libin Pan 2021-06-07 19:36:08 -07:00 committed by GitHub
parent 66d9245f33
commit c6ae9f3f24
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 28 additions and 12 deletions

View File

@ -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);
}
}
}

View File

@ -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();
}
/**

View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -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']);
}
}