diff options
-rw-r--r-- | app/Model/Category.php | 14 | ||||
-rw-r--r-- | tests/units/ProjectDuplicateTest.php | 84 | ||||
-rw-r--r-- | tests/units/ProjectTest.php | 32 |
3 files changed, 91 insertions, 39 deletions
diff --git a/app/Model/Category.php b/app/Model/Category.php index 65fd0c56..54a0f552 100644 --- a/app/Model/Category.php +++ b/app/Model/Category.php @@ -165,26 +165,26 @@ class Category extends Base } /** - * Duplicate categories from a project to another one + * Duplicate categories from a project to another one, must be executed inside a transaction * * @author Antonio Rabelo - * @param integer $project_from Project Template - * @return integer $project_to Project that receives the copy + * @param integer $src_project_id Source project id + * @return integer $dst_project_id Destination project id * @return boolean */ - public function duplicate($project_from, $project_to) + public function duplicate($src_project_id, $dst_project_id) { $categories = $this->db->table(self::TABLE) ->columns('name') - ->eq('project_id', $project_from) + ->eq('project_id', $src_project_id) ->asc('name') ->findAll(); foreach ($categories as $category) { - $category['project_id'] = $project_to; + $category['project_id'] = $dst_project_id; - if (! $this->category->create($category)) { + if (! $this->db->table(self::TABLE)->save($category)) { return false; } } diff --git a/tests/units/ProjectDuplicateTest.php b/tests/units/ProjectDuplicateTest.php new file mode 100644 index 00000000..973a0c32 --- /dev/null +++ b/tests/units/ProjectDuplicateTest.php @@ -0,0 +1,84 @@ +<?php + +require_once __DIR__.'/Base.php'; + +use Model\Project; +use Model\Category; +use Model\ProjectPermission; +use Model\User; +use Model\Task; +use Model\TaskCreation; +use Model\Acl; +use Model\Board; + +class ProjectDuplicationTest extends Base +{ + public function testClonePublicProject() + { + $p = new Project($this->container); + + $this->assertEquals(1, $p->create(array('name' => 'Public'))); + $this->assertEquals(2, $p->duplicate(1)); + + $project = $p->getById(2); + $this->assertNotEmpty($project); + $this->assertEquals('Public (Clone)', $project['name']); + $this->assertEquals(0, $project['is_private']); + $this->assertEquals(0, $project['is_public']); + $this->assertEmpty($project['token']); + } + + public function testClonePrivateProject() + { + $p = new Project($this->container); + + $this->assertEquals(1, $p->create(array('name' => 'Private', 'is_private' => 1), 1, true)); + $this->assertEquals(2, $p->duplicate(1)); + + $project = $p->getById(2); + $this->assertNotEmpty($project); + $this->assertEquals('Private (Clone)', $project['name']); + $this->assertEquals(1, $project['is_private']); + $this->assertEquals(0, $project['is_public']); + $this->assertEmpty($project['token']); + + $pp = new ProjectPermission($this->container); + + $this->assertEquals(array(1 => 'admin'), $pp->getMembers(1)); + $this->assertEquals(array(1 => 'admin'), $pp->getMembers(2)); + } + + public function testCloneProjectWithCategories() + { + $p = new Project($this->container); + $c = new Category($this->container); + + $this->assertEquals(1, $p->create(array('name' => 'P1'))); + + $this->assertEquals(1, $c->create(array('name' => 'C1', 'project_id' => 1))); + $this->assertEquals(2, $c->create(array('name' => 'C2', 'project_id' => 1))); + $this->assertEquals(3, $c->create(array('name' => 'C3', 'project_id' => 1))); + + $this->assertEquals(2, $p->duplicate(1)); + + $project = $p->getById(2); + $this->assertNotEmpty($project); + $this->assertEquals('P1 (Clone)', $project['name']); + + $categories = $c->getAll(2); + $this->assertNotempty($categories); + $this->assertEquals(3, count($categories)); + + $this->assertEquals(4, $categories[0]['id']); + $this->assertEquals('C1', $categories[0]['name']); + + $this->assertEquals(5, $categories[1]['id']); + $this->assertEquals('C2', $categories[1]['name']); + + $this->assertEquals(6, $categories[2]['id']); + $this->assertEquals('C3', $categories[2]['name']); + } + + // TODO: test users + // TODO: test actions +} diff --git a/tests/units/ProjectTest.php b/tests/units/ProjectTest.php index aab8398d..5fff8ad6 100644 --- a/tests/units/ProjectTest.php +++ b/tests/units/ProjectTest.php @@ -139,36 +139,4 @@ class ProjectTest extends Base $this->assertFalse($p->disablePublicAccess(123)); } - - public function testDuplicate() - { - $p = new Project($this->container); - - // Clone public project - $this->assertEquals(1, $p->create(array('name' => 'Public'))); - $this->assertEquals(2, $p->duplicate(1)); - - $project = $p->getById(2); - $this->assertNotEmpty($project); - $this->assertEquals('Public (Clone)', $project['name']); - $this->assertEquals(0, $project['is_private']); - $this->assertEquals(0, $project['is_public']); - $this->assertEmpty($project['token']); - - // Clone private project - $this->assertEquals(3, $p->create(array('name' => 'Private', 'is_private' => 1), 1, true)); - $this->assertEquals(4, $p->duplicate(3)); - - $project = $p->getById(4); - $this->assertNotEmpty($project); - $this->assertEquals('Private (Clone)', $project['name']); - $this->assertEquals(1, $project['is_private']); - $this->assertEquals(0, $project['is_public']); - $this->assertEmpty($project['token']); - - $pp = new ProjectPermission($this->container); - - $this->assertEquals(array(1 => 'admin'), $pp->getMembers(3)); - $this->assertEquals(array(1 => 'admin'), $pp->getMembers(4)); - } } |