/tests/units/TaskCreationTest.php

https://gitlab.com/x33n/kanboard · PHP · 416 lines · 315 code · 96 blank · 5 comment · 0 complexity · a3f731782af1fa43a7d1e160219da49a MD5 · raw file

  1. <?php
  2. require_once __DIR__.'/Base.php';
  3. use Model\Config;
  4. use Model\Task;
  5. use Model\TaskCreation;
  6. use Model\TaskFinder;
  7. use Model\TaskStatus;
  8. use Model\Project;
  9. use Model\ProjectPermission;
  10. class TaskCreationTest extends Base
  11. {
  12. public function onCreate($event)
  13. {
  14. $this->assertInstanceOf('Event\TaskEvent', $event);
  15. $event_data = $event->getAll();
  16. $this->assertNotEmpty($event_data);
  17. $this->assertEquals(1, $event_data['task_id']);
  18. $this->assertEquals('test', $event_data['title']);
  19. }
  20. public function testNoProjectId()
  21. {
  22. $p = new Project($this->container);
  23. $tc = new TaskCreation($this->container);
  24. $tf = new TaskFinder($this->container);
  25. $this->container['dispatcher']->addListener(Task::EVENT_CREATE_UPDATE, function() {});
  26. $this->container['dispatcher']->addListener(Task::EVENT_CREATE, function() {});
  27. $this->assertEquals(1, $p->create(array('name' => 'test')));
  28. $this->assertEquals(0, $tc->create(array('title' => 'test', 'project_id' => 0)));
  29. $called = $this->container['dispatcher']->getCalledListeners();
  30. $this->assertArrayNotHasKey(Task::EVENT_CREATE_UPDATE.'.closure', $called);
  31. $this->assertArrayNotHasKey(Task::EVENT_CREATE.'.closure', $called);
  32. }
  33. public function testNoTitle()
  34. {
  35. $p = new Project($this->container);
  36. $tc = new TaskCreation($this->container);
  37. $tf = new TaskFinder($this->container);
  38. $this->container['dispatcher']->addListener(Task::EVENT_CREATE_UPDATE, function() {});
  39. $this->container['dispatcher']->addListener(Task::EVENT_CREATE, function() {});
  40. $this->assertEquals(1, $p->create(array('name' => 'test')));
  41. $this->assertEquals(1, $tc->create(array('project_id' => 1)));
  42. $called = $this->container['dispatcher']->getCalledListeners();
  43. $this->assertArrayHasKey(Task::EVENT_CREATE_UPDATE.'.closure', $called);
  44. $this->assertArrayHasKey(Task::EVENT_CREATE.'.closure', $called);
  45. $task = $tf->getById(1);
  46. $this->assertNotEmpty($task);
  47. $this->assertEquals(1, $task['id']);
  48. $this->assertEquals('Untitled', $task['title']);
  49. $this->assertEquals(1, $task['project_id']);
  50. }
  51. public function testMinimum()
  52. {
  53. $p = new Project($this->container);
  54. $tc = new TaskCreation($this->container);
  55. $tf = new TaskFinder($this->container);
  56. $this->container['dispatcher']->addListener(Task::EVENT_CREATE_UPDATE, function() {});
  57. $this->container['dispatcher']->addListener(Task::EVENT_CREATE, array($this, 'onCreate'));
  58. $this->assertEquals(1, $p->create(array('name' => 'test')));
  59. $this->assertEquals(1, $tc->create(array('project_id' => 1, 'title' => 'test')));
  60. $called = $this->container['dispatcher']->getCalledListeners();
  61. $this->assertArrayHasKey(Task::EVENT_CREATE_UPDATE.'.closure', $called);
  62. $this->assertArrayHasKey(Task::EVENT_CREATE.'.TaskCreationTest::onCreate', $called);
  63. $task = $tf->getById(1);
  64. $this->assertNotEmpty($task);
  65. $this->assertNotFalse($task);
  66. $this->assertEquals(1, $task['id']);
  67. $this->assertEquals('yellow', $task['color_id']);
  68. $this->assertEquals(1, $task['project_id']);
  69. $this->assertEquals(1, $task['column_id']);
  70. $this->assertEquals(0, $task['owner_id']);
  71. $this->assertEquals(0, $task['category_id']);
  72. $this->assertEquals(0, $task['creator_id']);
  73. $this->assertEquals('test', $task['title']);
  74. $this->assertEquals('', $task['description']);
  75. $this->assertEquals('', $task['reference']);
  76. $this->assertEquals(time(), $task['date_creation'], 'Wrong timestamp', 1);
  77. $this->assertEquals(time(), $task['date_modification'], 'Wrong timestamp', 1);
  78. $this->assertEquals(0, $task['date_due']);
  79. $this->assertEquals(0, $task['date_completed']);
  80. $this->assertEquals(0, $task['date_started']);
  81. $this->assertEquals(0, $task['time_estimated']);
  82. $this->assertEquals(0, $task['time_spent']);
  83. $this->assertEquals(1, $task['position']);
  84. $this->assertEquals(1, $task['is_active']);
  85. $this->assertEquals(0, $task['score']);
  86. }
  87. public function testColorId()
  88. {
  89. $p = new Project($this->container);
  90. $tc = new TaskCreation($this->container);
  91. $tf = new TaskFinder($this->container);
  92. $this->assertEquals(1, $p->create(array('name' => 'test')));
  93. $this->assertEquals(1, $tc->create(array('project_id' => 1, 'title' => 'test', 'color_id' => 'blue')));
  94. $task = $tf->getById(1);
  95. $this->assertNotEmpty($task);
  96. $this->assertNotFalse($task);
  97. $this->assertEquals(1, $task['id']);
  98. $this->assertEquals('blue', $task['color_id']);
  99. }
  100. public function testOwnerId()
  101. {
  102. $p = new Project($this->container);
  103. $tc = new TaskCreation($this->container);
  104. $tf = new TaskFinder($this->container);
  105. $this->assertEquals(1, $p->create(array('name' => 'test')));
  106. $this->assertEquals(1, $tc->create(array('project_id' => 1, 'title' => 'test', 'owner_id' => 1)));
  107. $task = $tf->getById(1);
  108. $this->assertNotEmpty($task);
  109. $this->assertNotFalse($task);
  110. $this->assertEquals(1, $task['id']);
  111. $this->assertEquals(1, $task['owner_id']);
  112. }
  113. public function testCategoryId()
  114. {
  115. $p = new Project($this->container);
  116. $tc = new TaskCreation($this->container);
  117. $tf = new TaskFinder($this->container);
  118. $this->assertEquals(1, $p->create(array('name' => 'test')));
  119. $this->assertEquals(1, $tc->create(array('project_id' => 1, 'title' => 'test', 'category_id' => 1)));
  120. $task = $tf->getById(1);
  121. $this->assertNotEmpty($task);
  122. $this->assertNotFalse($task);
  123. $this->assertEquals(1, $task['id']);
  124. $this->assertEquals(1, $task['category_id']);
  125. }
  126. public function testCreatorId()
  127. {
  128. $p = new Project($this->container);
  129. $tc = new TaskCreation($this->container);
  130. $tf = new TaskFinder($this->container);
  131. $this->assertEquals(1, $p->create(array('name' => 'test')));
  132. $this->assertEquals(1, $tc->create(array('project_id' => 1, 'title' => 'test', 'creator_id' => 1)));
  133. $task = $tf->getById(1);
  134. $this->assertNotEmpty($task);
  135. $this->assertNotFalse($task);
  136. $this->assertEquals(1, $task['id']);
  137. $this->assertEquals(1, $task['creator_id']);
  138. }
  139. public function testThatCreatorIsDefined()
  140. {
  141. $p = new Project($this->container);
  142. $tc = new TaskCreation($this->container);
  143. $tf = new TaskFinder($this->container);
  144. $_SESSION = array();
  145. $_SESSION['user']['id'] = 1;
  146. $this->assertEquals(1, $p->create(array('name' => 'test')));
  147. $this->assertEquals(1, $tc->create(array('project_id' => 1, 'title' => 'test')));
  148. $task = $tf->getById(1);
  149. $this->assertNotEmpty($task);
  150. $this->assertNotFalse($task);
  151. $this->assertEquals(1, $task['id']);
  152. $this->assertEquals(1, $task['creator_id']);
  153. $_SESSION = array();
  154. }
  155. public function testColumnId()
  156. {
  157. $p = new Project($this->container);
  158. $tc = new TaskCreation($this->container);
  159. $tf = new TaskFinder($this->container);
  160. $this->assertEquals(1, $p->create(array('name' => 'test')));
  161. $this->assertEquals(1, $tc->create(array('project_id' => 1, 'title' => 'test', 'column_id' => 2)));
  162. $task = $tf->getById(1);
  163. $this->assertNotEmpty($task);
  164. $this->assertNotFalse($task);
  165. $this->assertEquals(1, $task['id']);
  166. $this->assertEquals(2, $task['column_id']);
  167. $this->assertEquals(1, $task['position']);
  168. }
  169. public function testPosition()
  170. {
  171. $p = new Project($this->container);
  172. $tc = new TaskCreation($this->container);
  173. $tf = new TaskFinder($this->container);
  174. $this->assertEquals(1, $p->create(array('name' => 'test')));
  175. $this->assertEquals(1, $tc->create(array('project_id' => 1, 'title' => 'test', 'column_id' => 2)));
  176. $task = $tf->getById(1);
  177. $this->assertNotEmpty($task);
  178. $this->assertNotFalse($task);
  179. $this->assertEquals(1, $task['id']);
  180. $this->assertEquals(2, $task['column_id']);
  181. $this->assertEquals(1, $task['position']);
  182. $this->assertEquals(2, $tc->create(array('project_id' => 1, 'title' => 'test', 'column_id' => 2)));
  183. $task = $tf->getById(2);
  184. $this->assertNotEmpty($task);
  185. $this->assertNotFalse($task);
  186. $this->assertEquals(2, $task['id']);
  187. $this->assertEquals(2, $task['column_id']);
  188. $this->assertEquals(2, $task['position']);
  189. }
  190. public function testDescription()
  191. {
  192. $p = new Project($this->container);
  193. $tc = new TaskCreation($this->container);
  194. $tf = new TaskFinder($this->container);
  195. $this->assertEquals(1, $p->create(array('name' => 'test')));
  196. $this->assertEquals(1, $tc->create(array('project_id' => 1, 'title' => 'test', 'description' => 'test')));
  197. $task = $tf->getById(1);
  198. $this->assertNotEmpty($task);
  199. $this->assertNotFalse($task);
  200. $this->assertEquals(1, $task['id']);
  201. $this->assertEquals('test', $task['description']);
  202. }
  203. public function testReference()
  204. {
  205. $p = new Project($this->container);
  206. $tc = new TaskCreation($this->container);
  207. $tf = new TaskFinder($this->container);
  208. $this->assertEquals(1, $p->create(array('name' => 'test')));
  209. $this->assertEquals(1, $tc->create(array('project_id' => 1, 'title' => 'test', 'reference' => 'test')));
  210. $task = $tf->getById(1);
  211. $this->assertNotEmpty($task);
  212. $this->assertNotFalse($task);
  213. $this->assertEquals(1, $task['id']);
  214. $this->assertEquals('test', $task['reference']);
  215. }
  216. public function testDateDue()
  217. {
  218. $date = '2014-11-23';
  219. $timestamp = strtotime('+2days');
  220. $p = new Project($this->container);
  221. $tc = new TaskCreation($this->container);
  222. $tf = new TaskFinder($this->container);
  223. $this->assertEquals(1, $p->create(array('name' => 'test')));
  224. $this->assertEquals(1, $tc->create(array('project_id' => 1, 'title' => 'test', 'date_due' => $date)));
  225. $this->assertEquals(2, $tc->create(array('project_id' => 1, 'title' => 'test', 'date_due' => $timestamp)));
  226. $this->assertEquals(3, $tc->create(array('project_id' => 1, 'title' => 'test', 'date_due' => '')));
  227. $task = $tf->getById(1);
  228. $this->assertNotEmpty($task);
  229. $this->assertEquals(1, $task['id']);
  230. $this->assertEquals($date, date('Y-m-d', $task['date_due']));
  231. $task = $tf->getById(2);
  232. $this->assertNotEmpty($task);
  233. $this->assertEquals(2, $task['id']);
  234. $this->assertEquals($timestamp, $task['date_due']);
  235. $task = $tf->getById(3);
  236. $this->assertEquals(3, $task['id']);
  237. $this->assertEquals(0, $task['date_due']);
  238. }
  239. public function testDateStarted()
  240. {
  241. $p = new Project($this->container);
  242. $tc = new TaskCreation($this->container);
  243. $tf = new TaskFinder($this->container);
  244. $this->assertEquals(1, $p->create(array('name' => 'test')));
  245. // Set only a date
  246. $this->assertEquals(1, $tc->create(array('project_id' => 1, 'title' => 'test', 'date_started' => '2014-11-24')));
  247. $task = $tf->getById(1);
  248. $this->assertEquals('2014-11-24 '.date('H:i'), date('Y-m-d H:i', $task['date_started']));
  249. // Set a datetime
  250. $this->assertEquals(2, $tc->create(array('project_id' => 1, 'title' => 'test', 'date_started' => '2014-11-24 16:25')));
  251. $task = $tf->getById(2);
  252. $this->assertEquals('2014-11-24 16:25', date('Y-m-d H:i', $task['date_started']));
  253. // Set a datetime
  254. $this->assertEquals(3, $tc->create(array('project_id' => 1, 'title' => 'test', 'date_started' => '2014-11-24 6:25pm')));
  255. $task = $tf->getById(3);
  256. $this->assertEquals('2014-11-24 18:25', date('Y-m-d H:i', $task['date_started']));
  257. // Set a timestamp
  258. $this->assertEquals(4, $tc->create(array('project_id' => 1, 'title' => 'test', 'date_started' => time())));
  259. $task = $tf->getById(4);
  260. $this->assertEquals(time(), $task['date_started'], '', 1);
  261. // Set empty string
  262. $this->assertEquals(5, $tc->create(array('project_id' => 1, 'title' => 'test', 'date_started' => '')));
  263. $task = $tf->getById(5);
  264. $this->assertEquals(0, $task['date_started']);
  265. }
  266. public function testTime()
  267. {
  268. $p = new Project($this->container);
  269. $tc = new TaskCreation($this->container);
  270. $tf = new TaskFinder($this->container);
  271. $this->assertEquals(1, $p->create(array('name' => 'test')));
  272. $this->assertEquals(1, $tc->create(array('project_id' => 1, 'title' => 'test', 'time_estimated' => 1.5, 'time_spent' => 2.3)));
  273. $task = $tf->getById(1);
  274. $this->assertNotEmpty($task);
  275. $this->assertNotFalse($task);
  276. $this->assertEquals(1, $task['id']);
  277. $this->assertEquals(1.5, $task['time_estimated']);
  278. $this->assertEquals(2.3, $task['time_spent']);
  279. }
  280. public function testStripColumn()
  281. {
  282. $p = new Project($this->container);
  283. $tc = new TaskCreation($this->container);
  284. $tf = new TaskFinder($this->container);
  285. $this->assertEquals(1, $p->create(array('name' => 'test')));
  286. $this->assertEquals(1, $tc->create(array('project_id' => 1, 'title' => 'test', 'another_task' => '1')));
  287. $task = $tf->getById(1);
  288. $this->assertNotEmpty($task);
  289. $this->assertNotFalse($task);
  290. }
  291. public function testScore()
  292. {
  293. $p = new Project($this->container);
  294. $tc = new TaskCreation($this->container);
  295. $tf = new TaskFinder($this->container);
  296. $this->assertEquals(1, $p->create(array('name' => 'test')));
  297. $this->assertEquals(1, $tc->create(array('project_id' => 1, 'title' => 'test', 'score' => '3')));
  298. $task = $tf->getById(1);
  299. $this->assertNotEmpty($task);
  300. $this->assertNotFalse($task);
  301. $this->assertEquals(3, $task['score']);
  302. }
  303. public function testDefaultColor()
  304. {
  305. $p = new Project($this->container);
  306. $tc = new TaskCreation($this->container);
  307. $tf = new TaskFinder($this->container);
  308. $c = new Config($this->container);
  309. $this->assertEquals(1, $p->create(array('name' => 'test')));
  310. $this->assertEquals(1, $tc->create(array('project_id' => 1, 'title' => 'test1')));
  311. $task = $tf->getById(1);
  312. $this->assertNotEmpty($task);
  313. $this->assertEquals('yellow', $task['color_id']);
  314. $this->assertTrue($c->save(array('default_color' => 'orange')));
  315. $this->assertEquals(2, $tc->create(array('project_id' => 1, 'title' => 'test2')));
  316. $task = $tf->getById(2);
  317. $this->assertNotEmpty($task);
  318. $this->assertEquals('orange', $task['color_id']);
  319. }
  320. }