/tests/units/ProjectDuplicationTest.php

https://gitlab.com/x33n/kanboard · PHP · 359 lines · 263 code · 79 blank · 17 comment · 0 complexity · 36a3d7c241bdbc0008425c7aa4b3a0e8 MD5 · raw file

  1. <?php
  2. require_once __DIR__.'/Base.php';
  3. use Model\Action;
  4. use Model\Project;
  5. use Model\Category;
  6. use Model\ProjectPermission;
  7. use Model\ProjectDuplication;
  8. use Model\User;
  9. use Model\Swimlane;
  10. use Model\Task;
  11. use Model\TaskCreation;
  12. use Model\TaskFinder;
  13. class ProjectDuplicationTest extends Base
  14. {
  15. public function testProjectName()
  16. {
  17. $pd = new ProjectDuplication($this->container);
  18. $this->assertEquals('test (Clone)', $pd->getClonedProjectName('test'));
  19. $this->assertEquals(50, strlen($pd->getClonedProjectName(str_repeat('a', 50))));
  20. $this->assertEquals(str_repeat('a', 42).' (Clone)', $pd->getClonedProjectName(str_repeat('a', 50)));
  21. $this->assertEquals(50, strlen($pd->getClonedProjectName(str_repeat('a', 60))));
  22. $this->assertEquals(str_repeat('a', 42).' (Clone)', $pd->getClonedProjectName(str_repeat('a', 60)));
  23. }
  24. public function testCopyProjectWithLongName()
  25. {
  26. $p = new Project($this->container);
  27. $pd = new ProjectDuplication($this->container);
  28. $this->assertEquals(1, $p->create(array('name' => str_repeat('a', 50))));
  29. $this->assertEquals(2, $pd->duplicate(1));
  30. $project = $p->getById(2);
  31. $this->assertNotEmpty($project);
  32. $this->assertEquals(str_repeat('a', 42).' (Clone)', $project['name']);
  33. }
  34. public function testClonePublicProject()
  35. {
  36. $p = new Project($this->container);
  37. $pd = new ProjectDuplication($this->container);
  38. $this->assertEquals(1, $p->create(array('name' => 'Public')));
  39. $this->assertEquals(2, $pd->duplicate(1));
  40. $project = $p->getById(2);
  41. $this->assertNotEmpty($project);
  42. $this->assertEquals('Public (Clone)', $project['name']);
  43. $this->assertEquals(0, $project['is_private']);
  44. $this->assertEquals(0, $project['is_public']);
  45. $this->assertEmpty($project['token']);
  46. }
  47. public function testClonePrivateProject()
  48. {
  49. $p = new Project($this->container);
  50. $pd = new ProjectDuplication($this->container);
  51. $this->assertEquals(1, $p->create(array('name' => 'Private', 'is_private' => 1), 1, true));
  52. $this->assertEquals(2, $pd->duplicate(1));
  53. $project = $p->getById(2);
  54. $this->assertNotEmpty($project);
  55. $this->assertEquals('Private (Clone)', $project['name']);
  56. $this->assertEquals(1, $project['is_private']);
  57. $this->assertEquals(0, $project['is_public']);
  58. $this->assertEmpty($project['token']);
  59. $pp = new ProjectPermission($this->container);
  60. $this->assertEquals(array(1 => 'admin'), $pp->getMembers(1));
  61. $this->assertEquals(array(1 => 'admin'), $pp->getMembers(2));
  62. }
  63. public function testCloneProjectWithCategories()
  64. {
  65. $p = new Project($this->container);
  66. $c = new Category($this->container);
  67. $pd = new ProjectDuplication($this->container);
  68. $this->assertEquals(1, $p->create(array('name' => 'P1')));
  69. $this->assertEquals(1, $c->create(array('name' => 'C1', 'project_id' => 1)));
  70. $this->assertEquals(2, $c->create(array('name' => 'C2', 'project_id' => 1)));
  71. $this->assertEquals(3, $c->create(array('name' => 'C3', 'project_id' => 1)));
  72. $this->assertEquals(2, $pd->duplicate(1));
  73. $project = $p->getById(2);
  74. $this->assertNotEmpty($project);
  75. $this->assertEquals('P1 (Clone)', $project['name']);
  76. $categories = $c->getAll(2);
  77. $this->assertNotempty($categories);
  78. $this->assertEquals(3, count($categories));
  79. $this->assertEquals(4, $categories[0]['id']);
  80. $this->assertEquals('C1', $categories[0]['name']);
  81. $this->assertEquals(5, $categories[1]['id']);
  82. $this->assertEquals('C2', $categories[1]['name']);
  83. $this->assertEquals(6, $categories[2]['id']);
  84. $this->assertEquals('C3', $categories[2]['name']);
  85. }
  86. public function testCloneProjectWithUsers()
  87. {
  88. $p = new Project($this->container);
  89. $c = new Category($this->container);
  90. $pp = new ProjectPermission($this->container);
  91. $u = new User($this->container);
  92. $pd = new ProjectDuplication($this->container);
  93. $this->assertEquals(2, $u->create(array('username' => 'unittest1', 'password' => 'unittest')));
  94. $this->assertEquals(3, $u->create(array('username' => 'unittest2', 'password' => 'unittest')));
  95. $this->assertEquals(4, $u->create(array('username' => 'unittest3', 'password' => 'unittest')));
  96. $this->assertEquals(1, $p->create(array('name' => 'P1')));
  97. $this->assertTrue($pp->addMember(1, 2));
  98. $this->assertTrue($pp->addMember(1, 4));
  99. $this->assertTrue($pp->addManager(1, 3));
  100. $this->assertTrue($pp->isMember(1, 2));
  101. $this->assertTrue($pp->isMember(1, 3));
  102. $this->assertTrue($pp->isMember(1, 4));
  103. $this->assertFalse($pp->isManager(1, 2));
  104. $this->assertTrue($pp->isManager(1, 3));
  105. $this->assertFalse($pp->isManager(1, 4));
  106. $this->assertEquals(2, $pd->duplicate(1));
  107. $project = $p->getById(2);
  108. $this->assertNotEmpty($project);
  109. $this->assertEquals('P1 (Clone)', $project['name']);
  110. $this->assertEquals(3, count($pp->getMembers(2)));
  111. $this->assertTrue($pp->isMember(2, 2));
  112. $this->assertTrue($pp->isMember(2, 3));
  113. $this->assertTrue($pp->isMember(2, 4));
  114. $this->assertFalse($pp->isManager(2, 2));
  115. $this->assertTrue($pp->isManager(2, 3));
  116. $this->assertFalse($pp->isManager(2, 4));
  117. }
  118. public function testCloneProjectWithActionTaskAssignCurrentUser()
  119. {
  120. $p = new Project($this->container);
  121. $a = new Action($this->container);
  122. $pd = new ProjectDuplication($this->container);
  123. $this->assertEquals(1, $p->create(array('name' => 'P1')));
  124. $this->assertEquals(1, $a->create(array(
  125. 'project_id' => 1,
  126. 'event_name' => Task::EVENT_MOVE_COLUMN,
  127. 'action_name' => 'TaskAssignCurrentUser',
  128. 'params' => array('column_id' => 2),
  129. )));
  130. $this->assertEquals(2, $pd->duplicate(1));
  131. $actions = $a->getAllByProject(2);
  132. $this->assertNotEmpty($actions);
  133. $this->assertEquals('TaskAssignCurrentUser', $actions[0]['action_name']);
  134. $this->assertNotEmpty($actions[0]['params']);
  135. $this->assertEquals(6, $actions[0]['params'][0]['value']);
  136. }
  137. public function testCloneProjectWithActionTaskAssignColorCategory()
  138. {
  139. $p = new Project($this->container);
  140. $a = new Action($this->container);
  141. $c = new Category($this->container);
  142. $pd = new ProjectDuplication($this->container);
  143. $this->assertEquals(1, $p->create(array('name' => 'P1')));
  144. $this->assertEquals(1, $c->create(array('name' => 'C1', 'project_id' => 1)));
  145. $this->assertEquals(2, $c->create(array('name' => 'C2', 'project_id' => 1)));
  146. $this->assertEquals(3, $c->create(array('name' => 'C3', 'project_id' => 1)));
  147. $this->assertEquals(1, $a->create(array(
  148. 'project_id' => 1,
  149. 'event_name' => Task::EVENT_CREATE_UPDATE,
  150. 'action_name' => 'TaskAssignColorCategory',
  151. 'params' => array('color_id' => 'blue', 'category_id' => 2),
  152. )));
  153. $this->assertEquals(2, $pd->duplicate(1));
  154. $actions = $a->getAllByProject(2);
  155. $this->assertNotEmpty($actions);
  156. $this->assertEquals('TaskAssignColorCategory', $actions[0]['action_name']);
  157. $this->assertNotEmpty($actions[0]['params']);
  158. $this->assertEquals('blue', $actions[0]['params'][0]['value']);
  159. $this->assertEquals(5, $actions[0]['params'][1]['value']);
  160. }
  161. public function testCloneProjectWithSwimlanesAndTasks()
  162. {
  163. $p = new Project($this->container);
  164. $pd = new ProjectDuplication($this->container);
  165. $s = new Swimlane($this->container);
  166. $tc = new TaskCreation($this->container);
  167. $tf = new TaskFinder($this->container);
  168. $this->assertEquals(1, $p->create(array('name' => 'P1')));
  169. // create initial swimlanes
  170. $this->assertEquals(1, $s->create(1, 'S1'));
  171. $this->assertEquals(2, $s->create(1, 'S2'));
  172. $this->assertEquals(3, $s->create(1, 'S3'));
  173. $default_swimlane1 = $s->getDefault(1);
  174. $default_swimlane1['default_swimlane'] = 'New Default';
  175. $this->assertTrue($s->updateDefault($default_swimlane1));
  176. //create initial tasks
  177. $this->assertEquals(1, $tc->create(array('title' => 'T1', 'project_id' => 1, 'column_id' => 1, 'owner_id' => 1)));
  178. $this->assertEquals(2, $tc->create(array('title' => 'T2', 'project_id' => 1, 'column_id' => 2, 'owner_id' => 1)));
  179. $this->assertEquals(3, $tc->create(array('title' => 'T3', 'project_id' => 1, 'column_id' => 3, 'owner_id' => 1)));
  180. $this->assertNotFalse($pd->duplicate(1, array('category', 'action', 'swimlane', 'task')));
  181. $project = $p->getByName('P1 (Clone)');
  182. $this->assertNotFalse($project);
  183. $project_id = $project['id'];
  184. // Check if Swimlanes have been duplicated
  185. $swimlanes = $s->getAll($project_id);
  186. $this->assertCount(3, $swimlanes);
  187. $this->assertEquals(4, $swimlanes[0]['id']);
  188. $this->assertEquals('S1', $swimlanes[0]['name']);
  189. $this->assertEquals(5, $swimlanes[1]['id']);
  190. $this->assertEquals('S2', $swimlanes[1]['name']);
  191. $this->assertEquals(6, $swimlanes[2]['id']);
  192. $this->assertEquals('S3', $swimlanes[2]['name']);
  193. $new_default = $s->getDefault($project_id);
  194. $this->assertEquals('New Default', $new_default['default_swimlane']);
  195. // Check if Tasks have been duplicated
  196. $tasks = $tf->getAll($project_id);
  197. $this->assertCount(3, $tasks);
  198. // $this->assertEquals(4, $tasks[0]['id']);
  199. $this->assertEquals('T1', $tasks[0]['title']);
  200. // $this->assertEquals(5, $tasks[1]['id']);
  201. $this->assertEquals('T2', $tasks[1]['title']);
  202. // $this->assertEquals(6, $tasks[2]['id']);
  203. $this->assertEquals('T3', $tasks[2]['title']);
  204. $p->remove($project_id);
  205. $this->assertFalse($p->exists($project_id));
  206. $this->assertCount(0, $s->getAll($project_id));
  207. $this->assertCount(0, $tf->getAll($project_id));
  208. }
  209. public function testCloneProjectWithSwimlanes()
  210. {
  211. $p = new Project($this->container);
  212. $pd = new ProjectDuplication($this->container);
  213. $s = new Swimlane($this->container);
  214. $tc = new TaskCreation($this->container);
  215. $tf = new TaskFinder($this->container);
  216. $this->assertEquals(1, $p->create(array('name' => 'P1')));
  217. // create initial swimlanes
  218. $this->assertEquals(1, $s->create(1, 'S1'));
  219. $this->assertEquals(2, $s->create(1, 'S2'));
  220. $this->assertEquals(3, $s->create(1, 'S3'));
  221. $default_swimlane1 = $s->getDefault(1);
  222. $default_swimlane1['default_swimlane'] = 'New Default';
  223. $this->assertTrue($s->updateDefault($default_swimlane1));
  224. //create initial tasks
  225. $this->assertEquals(1, $tc->create(array('title' => 'T1', 'project_id' => 1, 'column_id' => 1, 'owner_id' => 1)));
  226. $this->assertEquals(2, $tc->create(array('title' => 'T2', 'project_id' => 1, 'column_id' => 2, 'owner_id' => 1)));
  227. $this->assertEquals(3, $tc->create(array('title' => 'T3', 'project_id' => 1, 'column_id' => 3, 'owner_id' => 1)));
  228. $this->assertNotFalse($pd->duplicate(1, array('category', 'action', 'swimlane')));
  229. $project = $p->getByName('P1 (Clone)');
  230. $this->assertNotFalse($project);
  231. $project_id = $project['id'];
  232. $swimlanes = $s->getAll($project_id);
  233. $this->assertCount(3, $swimlanes);
  234. $this->assertEquals(4, $swimlanes[0]['id']);
  235. $this->assertEquals('S1', $swimlanes[0]['name']);
  236. $this->assertEquals(5, $swimlanes[1]['id']);
  237. $this->assertEquals('S2', $swimlanes[1]['name']);
  238. $this->assertEquals(6, $swimlanes[2]['id']);
  239. $this->assertEquals('S3', $swimlanes[2]['name']);
  240. $new_default = $s->getDefault($project_id);
  241. $this->assertEquals('New Default', $new_default['default_swimlane']);
  242. // Check if Tasks have NOT been duplicated
  243. $this->assertCount(0, $tf->getAll($project_id));
  244. }
  245. public function testCloneProjectWithTasks()
  246. {
  247. $p = new Project($this->container);
  248. $pd = new ProjectDuplication($this->container);
  249. $s = new Swimlane($this->container);
  250. $tc = new TaskCreation($this->container);
  251. $tf = new TaskFinder($this->container);
  252. $this->assertEquals(1, $p->create(array('name' => 'P1')));
  253. // create initial swimlanes
  254. $this->assertEquals(1, $s->create(1, 'S1'));
  255. $this->assertEquals(2, $s->create(1, 'S2'));
  256. $this->assertEquals(3, $s->create(1, 'S3'));
  257. $default_swimlane1 = $s->getDefault(1);
  258. $default_swimlane1['default_swimlane'] = 'New Default';
  259. $this->assertTrue($s->updateDefault($default_swimlane1));
  260. //create initial tasks
  261. $this->assertEquals(1, $tc->create(array('title' => 'T1', 'project_id' => 1, 'column_id' => 1, 'owner_id' => 1)));
  262. $this->assertEquals(2, $tc->create(array('title' => 'T2', 'project_id' => 1, 'column_id' => 2, 'owner_id' => 1)));
  263. $this->assertEquals(3, $tc->create(array('title' => 'T3', 'project_id' => 1, 'column_id' => 3, 'owner_id' => 1)));
  264. $this->assertNotFalse($pd->duplicate(1, array('category', 'action', 'task')));
  265. $project = $p->getByName('P1 (Clone)');
  266. $this->assertNotFalse($project);
  267. $project_id = $project['id'];
  268. // Check if Swimlanes have NOT been duplicated
  269. $this->assertCount(0, $s->getAll($project_id));
  270. // Check if Tasks have been duplicated
  271. $tasks = $tf->getAll($project_id);
  272. $this->assertCount(3, $tasks);
  273. //$this->assertEquals(4, $tasks[0]['id']);
  274. $this->assertEquals('T1', $tasks[0]['title']);
  275. //$this->assertEquals(5, $tasks[1]['id']);
  276. $this->assertEquals('T2', $tasks[1]['title']);
  277. //$this->assertEquals(6, $tasks[2]['id']);
  278. $this->assertEquals('T3', $tasks[2]['title']);
  279. }
  280. }