/tests/units/SubtaskTimeTrackingTest.php

https://gitlab.com/x33n/kanboard · PHP · 314 lines · 238 code · 62 blank · 14 comment · 0 complexity · 29054dc662ea824232467e60618ae657 MD5 · raw file

  1. <?php
  2. require_once __DIR__.'/Base.php';
  3. use Model\TaskFinder;
  4. use Model\TaskCreation;
  5. use Model\Subtask;
  6. use Model\SubtaskTimeTracking;
  7. use Model\Project;
  8. use Model\Category;
  9. use Model\User;
  10. use Core\Session;
  11. class SubtaskTimeTrackingTest extends Base
  12. {
  13. public function testHasTimer()
  14. {
  15. $tc = new TaskCreation($this->container);
  16. $s = new Subtask($this->container);
  17. $st = new SubtaskTimeTracking($this->container);
  18. $p = new Project($this->container);
  19. $this->assertEquals(1, $p->create(array('name' => 'test1')));
  20. $this->assertEquals(1, $tc->create(array('title' => 'test 1', 'project_id' => 1, 'column_id' => 1, 'owner_id' => 1)));
  21. $this->assertEquals(1, $s->create(array('title' => 'subtask #2', 'task_id' => 1, 'user_id' => 1)));
  22. $this->assertFalse($st->hasTimer(1, 1));
  23. $this->assertTrue($st->logStartTime(1, 1));
  24. $this->assertTrue($st->hasTimer(1, 1));
  25. $this->assertFalse($st->logStartTime(1, 1));
  26. $this->assertTrue($st->logEndTime(1, 1));
  27. $this->assertFalse($st->hasTimer(1, 1));
  28. }
  29. public function testGetTimerStatus()
  30. {
  31. $tc = new TaskCreation($this->container);
  32. $s = new Subtask($this->container);
  33. $st = new SubtaskTimeTracking($this->container);
  34. $p = new Project($this->container);
  35. $ss = new Session;
  36. $ss['user'] = array('id' => 1);
  37. $this->assertEquals(1, $p->create(array('name' => 'test1')));
  38. $this->assertEquals(1, $tc->create(array('title' => 'test 1', 'project_id' => 1, 'column_id' => 1, 'owner_id' => 1)));
  39. $this->assertEquals(1, $s->create(array('title' => 'subtask #1', 'task_id' => 1, 'user_id' => 1)));
  40. // Nothing started
  41. $subtasks = $s->getAll(1);
  42. $this->assertNotEmpty($subtasks);
  43. $this->assertEquals(0, $subtasks[0]['timer_start_date']);
  44. $this->assertFalse($subtasks[0]['is_timer_started']);
  45. $subtask = $s->getbyId(1, true);
  46. $this->assertNotEmpty($subtask);
  47. $this->assertEquals(0, $subtask['timer_start_date']);
  48. $this->assertFalse($subtask['is_timer_started']);
  49. // Start the clock
  50. $this->assertTrue($st->logStartTime(1, 1));
  51. $subtasks = $s->getAll(1);
  52. $this->assertNotEmpty($subtasks);
  53. $this->assertEquals(time(), $subtasks[0]['timer_start_date'], '', 3);
  54. $this->assertTrue($subtasks[0]['is_timer_started']);
  55. $subtask = $s->getbyId(1, true);
  56. $this->assertNotEmpty($subtask);
  57. $this->assertEquals(time(), $subtask['timer_start_date'], '', 3);
  58. $this->assertTrue($subtask['is_timer_started']);
  59. // Stop the clock
  60. $this->assertTrue($st->logEndTime(1, 1));
  61. $subtasks = $s->getAll(1);
  62. $this->assertNotEmpty($subtasks);
  63. $this->assertEquals(0, $subtasks[0]['timer_start_date']);
  64. $this->assertFalse($subtasks[0]['is_timer_started']);
  65. $subtask = $s->getbyId(1, true);
  66. $this->assertNotEmpty($subtask);
  67. $this->assertEquals(0, $subtask['timer_start_date']);
  68. $this->assertFalse($subtask['is_timer_started']);
  69. }
  70. public function testLogStartTime()
  71. {
  72. $tc = new TaskCreation($this->container);
  73. $s = new Subtask($this->container);
  74. $st = new SubtaskTimeTracking($this->container);
  75. $p = new Project($this->container);
  76. $this->assertEquals(1, $p->create(array('name' => 'test1')));
  77. $this->assertEquals(1, $tc->create(array('title' => 'test 1', 'project_id' => 1, 'column_id' => 1, 'owner_id' => 1)));
  78. $this->assertEquals(1, $s->create(array('title' => 'subtask #2', 'task_id' => 1, 'user_id' => 1)));
  79. $this->assertTrue($st->logStartTime(1, 1));
  80. $timesheet = $st->getUserTimesheet(1);
  81. $this->assertNotEmpty($timesheet);
  82. $this->assertCount(1, $timesheet);
  83. $this->assertNotEmpty($timesheet[0]['start']);
  84. $this->assertEmpty($timesheet[0]['end']);
  85. $this->assertEquals(1, $timesheet[0]['user_id']);
  86. $this->assertEquals(1, $timesheet[0]['subtask_id']);
  87. }
  88. public function testLogStartEnd()
  89. {
  90. $tc = new TaskCreation($this->container);
  91. $s = new Subtask($this->container);
  92. $st = new SubtaskTimeTracking($this->container);
  93. $p = new Project($this->container);
  94. $this->assertEquals(1, $p->create(array('name' => 'test1')));
  95. $this->assertEquals(1, $tc->create(array('title' => 'test 1', 'project_id' => 1, 'column_id' => 1, 'owner_id' => 1)));
  96. $this->assertEquals(1, $s->create(array('title' => 'subtask #2', 'task_id' => 1, 'user_id' => 1)));
  97. // No start time
  98. $this->assertTrue($st->logEndTime(1, 1));
  99. $timesheet = $st->getUserTimesheet(1);
  100. $this->assertEmpty($timesheet);
  101. // Log start and end time
  102. $this->assertTrue($st->logStartTime(1, 1));
  103. sleep(1);
  104. $this->assertTrue($st->logEndTime(1, 1));
  105. $timesheet = $st->getUserTimesheet(1);
  106. $this->assertNotEmpty($timesheet);
  107. $this->assertCount(1, $timesheet);
  108. $this->assertNotEmpty($timesheet[0]['start']);
  109. $this->assertNotEmpty($timesheet[0]['end']);
  110. $this->assertEquals(1, $timesheet[0]['user_id']);
  111. $this->assertEquals(1, $timesheet[0]['subtask_id']);
  112. $this->assertNotEquals($timesheet[0]['start'], $timesheet[0]['end']);
  113. }
  114. public function testCalculateSubtaskTime()
  115. {
  116. $tc = new TaskCreation($this->container);
  117. $s = new Subtask($this->container);
  118. $st = new SubtaskTimeTracking($this->container);
  119. $p = new Project($this->container);
  120. $this->assertEquals(1, $p->create(array('name' => 'test1')));
  121. $this->assertEquals(1, $tc->create(array('title' => 'test 1', 'project_id' => 1, 'column_id' => 1, 'owner_id' => 1)));
  122. $this->assertEquals(1, $s->create(array('title' => 'subtask #2', 'task_id' => 1, 'time_spent' => 2.2, 'time_estimated' => 3.3)));
  123. $this->assertEquals(2, $s->create(array('title' => 'subtask #2', 'task_id' => 1, 'time_spent' => 1.1, 'time_estimated' => 4.4)));
  124. $time = $st->calculateSubtaskTime(1);
  125. $this->assertNotempty($time);
  126. $this->assertCount(2, $time);
  127. $this->assertEquals(3.3, $time['total_spent'], 'Total spent', 0.01);
  128. $this->assertEquals(7.7, $time['total_estimated'], 'Total estimated', 0.01);
  129. }
  130. public function testUpdateSubtaskTimeSpent()
  131. {
  132. $tc = new TaskCreation($this->container);
  133. $s = new Subtask($this->container);
  134. $st = new SubtaskTimeTracking($this->container);
  135. $p = new Project($this->container);
  136. $this->assertEquals(1, $p->create(array('name' => 'test1')));
  137. $this->assertEquals(1, $tc->create(array('title' => 'test 1', 'project_id' => 1, 'column_id' => 1, 'owner_id' => 1)));
  138. $this->assertEquals(1, $s->create(array('title' => 'subtask #2', 'task_id' => 1, 'time_spent' => 2.2)));
  139. $this->assertEquals(2, $s->create(array('title' => 'subtask #2', 'task_id' => 1)));
  140. $this->assertTrue($st->logStartTime(1, 1));
  141. $this->assertTrue($st->logStartTime(2, 1));
  142. // Fake start time
  143. $this->container['db']->table(SubtaskTimeTracking::TABLE)->update(array('start' => time() - 3600));
  144. $this->assertTrue($st->logEndTime(1, 1));
  145. $this->assertTrue($st->logEndTime(2, 1));
  146. $timesheet = $st->getUserTimesheet(1);
  147. $this->assertNotEmpty($timesheet);
  148. $this->assertCount(2, $timesheet);
  149. $this->assertEquals(3600, $timesheet[0]['end'] - $timesheet[0]['start'], 'Wrong timestamps', 1);
  150. $this->assertEquals(3600, $timesheet[1]['end'] - $timesheet[1]['start'], 'Wrong timestamps', 1);
  151. $time = $st->calculateSubtaskTime(1);
  152. $this->assertNotempty($time);
  153. $this->assertEquals(4.2, $time['total_spent'], 'Total spent', 0.01);
  154. $this->assertEquals(0, $time['total_estimated'], 'Total estimated', 0.01);
  155. $time = $st->calculateSubtaskTime(2);
  156. $this->assertNotempty($time);
  157. $this->assertEquals(0, $time['total_spent'], 'Total spent', 0.01);
  158. $this->assertEquals(0, $time['total_estimated'], 'Total estimated', 0.01);
  159. }
  160. public function testUpdateTaskTimeTracking()
  161. {
  162. $tf = new TaskFinder($this->container);
  163. $tc = new TaskCreation($this->container);
  164. $s = new Subtask($this->container);
  165. $st = new SubtaskTimeTracking($this->container);
  166. $p = new Project($this->container);
  167. $this->assertEquals(1, $p->create(array('name' => 'test1')));
  168. $this->assertEquals(1, $tc->create(array('title' => 'test 1', 'project_id' => 1)));
  169. $this->assertEquals(2, $tc->create(array('title' => 'test 2', 'project_id' => 1, 'time_estimated' => 1.5, 'time_spent' => 0.5)));
  170. $this->assertEquals(3, $tc->create(array('title' => 'test 2', 'project_id' => 1, 'time_estimated' => 4, 'time_spent' => 2)));
  171. $this->assertEquals(1, $s->create(array('title' => 'subtask #1', 'task_id' => 1, 'time_spent' => 2.2)));
  172. $this->assertEquals(2, $s->create(array('title' => 'subtask #2', 'task_id' => 1, 'time_estimated' => 1)));
  173. $this->assertEquals(3, $s->create(array('title' => 'subtask #3', 'task_id' => 2, 'time_spent' => 3.4)));
  174. $this->assertEquals(4, $s->create(array('title' => 'subtask #4', 'task_id' => 2, 'time_estimated' => 1.25)));
  175. $this->assertEquals(5, $s->create(array('title' => 'subtask #5', 'task_id' => 3, 'time_spent' => 8)));
  176. $st->updateTaskTimeTracking(1);
  177. $st->updateTaskTimeTracking(2);
  178. $st->updateTaskTimeTracking(3);
  179. $task = $tf->getById(1);
  180. $this->assertNotEmpty($task);
  181. $this->assertEquals(2.2, $task['time_spent'], 'Total spent', 0.01);
  182. $this->assertEquals(1, $task['time_estimated'], 'Total estimated', 0.01);
  183. $task = $tf->getById(2);
  184. $this->assertNotEmpty($task);
  185. $this->assertEquals(3.4, $task['time_spent'], 'Total spent', 0.01);
  186. $this->assertEquals(1.25, $task['time_estimated'], 'Total estimated', 0.01);
  187. $task = $tf->getById(3);
  188. $this->assertNotEmpty($task);
  189. $this->assertEquals(4, $task['time_estimated']);
  190. $this->assertEquals(8, $task['time_spent']);
  191. }
  192. public function testGetCalendarEvents()
  193. {
  194. $tf = new TaskFinder($this->container);
  195. $tc = new TaskCreation($this->container);
  196. $s = new Subtask($this->container);
  197. $st = new SubtaskTimeTracking($this->container);
  198. $p = new Project($this->container);
  199. $this->assertEquals(1, $p->create(array('name' => 'test1')));
  200. $this->assertEquals(2, $p->create(array('name' => 'test2')));
  201. $this->assertEquals(1, $tc->create(array('title' => 'test 1', 'project_id' => 1)));
  202. $this->assertEquals(2, $tc->create(array('title' => 'test 1', 'project_id' => 2)));
  203. $this->assertEquals(1, $s->create(array('title' => 'subtask #1', 'task_id' => 1)));
  204. $this->assertEquals(2, $s->create(array('title' => 'subtask #2', 'task_id' => 1)));
  205. $this->assertEquals(3, $s->create(array('title' => 'subtask #3', 'task_id' => 1)));
  206. $this->assertEquals(4, $s->create(array('title' => 'subtask #4', 'task_id' => 2)));
  207. $this->assertEquals(5, $s->create(array('title' => 'subtask #5', 'task_id' => 2)));
  208. $this->assertEquals(6, $s->create(array('title' => 'subtask #6', 'task_id' => 2)));
  209. $this->assertEquals(7, $s->create(array('title' => 'subtask #7', 'task_id' => 2)));
  210. $this->assertEquals(8, $s->create(array('title' => 'subtask #8', 'task_id' => 2)));
  211. // Slot start before and finish inside the calendar time range
  212. $this->container['db']->table(SubtaskTimeTracking::TABLE)->insert(array('user_id' => 1, 'subtask_id' => 1, 'start' => strtotime('-1 day'), 'end' => strtotime('+1 hour')));
  213. // Slot start inside time range and finish after the time range
  214. $this->container['db']->table(SubtaskTimeTracking::TABLE)->insert(array('user_id' => 1, 'subtask_id' => 2, 'start' => strtotime('+1 hour'), 'end' => strtotime('+2 days')));
  215. // Start before time range and finish inside time range
  216. $this->container['db']->table(SubtaskTimeTracking::TABLE)->insert(array('user_id' => 1, 'subtask_id' => 3, 'start' => strtotime('-1 day'), 'end' => strtotime('+1.5 days')));
  217. // Start and finish inside time range
  218. $this->container['db']->table(SubtaskTimeTracking::TABLE)->insert(array('user_id' => 1, 'subtask_id' => 4, 'start' => strtotime('+1 hour'), 'end' => strtotime('+2 hours')));
  219. // Start and finish after the time range
  220. $this->container['db']->table(SubtaskTimeTracking::TABLE)->insert(array('user_id' => 1, 'subtask_id' => 5, 'start' => strtotime('+2 days'), 'end' => strtotime('+3 days')));
  221. // Start and finish before the time range
  222. $this->container['db']->table(SubtaskTimeTracking::TABLE)->insert(array('user_id' => 1, 'subtask_id' => 6, 'start' => strtotime('-2 days'), 'end' => strtotime('-1 day')));
  223. // Start before time range and not finished
  224. $this->container['db']->table(SubtaskTimeTracking::TABLE)->insert(array('user_id' => 1, 'subtask_id' => 7, 'start' => strtotime('-1 day')));
  225. // Start inside time range and not finish
  226. $this->container['db']->table(SubtaskTimeTracking::TABLE)->insert(array('user_id' => 1, 'subtask_id' => 8, 'start' => strtotime('+3200 seconds')));
  227. $timesheet = $st->getUserTimesheet(1);
  228. $this->assertNotEmpty($timesheet);
  229. $this->assertCount(8, $timesheet);
  230. $events = $st->getUserCalendarEvents(1, date('Y-m-d'), date('Y-m-d', strtotime('+2 day')));
  231. $this->assertNotEmpty($events);
  232. $this->assertCount(6, $events);
  233. $this->assertEquals(1, $events[0]['subtask_id']);
  234. $this->assertEquals(2, $events[1]['subtask_id']);
  235. $this->assertEquals(3, $events[2]['subtask_id']);
  236. $this->assertEquals(4, $events[3]['subtask_id']);
  237. $this->assertEquals(7, $events[4]['subtask_id']);
  238. $this->assertEquals(8, $events[5]['subtask_id']);
  239. $events = $st->getProjectCalendarEvents(1, date('Y-m-d'), date('Y-m-d', strtotime('+2 days')));
  240. $this->assertNotEmpty($events);
  241. $this->assertCount(3, $events);
  242. $this->assertEquals(1, $events[0]['subtask_id']);
  243. $this->assertEquals(2, $events[1]['subtask_id']);
  244. $this->assertEquals(3, $events[2]['subtask_id']);
  245. $events = $st->getProjectCalendarEvents(2, date('Y-m-d'), date('Y-m-d', strtotime('+2 days')));
  246. $this->assertNotEmpty($events);
  247. $this->assertCount(3, $events);
  248. $this->assertEquals(4, $events[0]['subtask_id']);
  249. $this->assertEquals(7, $events[1]['subtask_id']);
  250. $this->assertEquals(8, $events[2]['subtask_id']);
  251. }
  252. }