/tests/units/GithubWebhookTest.php

https://gitlab.com/x33n/kanboard · PHP · 459 lines · 349 code · 110 blank · 0 comment · 0 complexity · 02cc168e246afa7b61c5461f55f95aed MD5 · raw file

  1. <?php
  2. require_once __DIR__.'/Base.php';
  3. use Integration\GithubWebhook;
  4. use Model\TaskCreation;
  5. use Model\TaskFinder;
  6. use Model\Project;
  7. use Model\ProjectPermission;
  8. use Model\User;
  9. class GithubWebhookTest extends Base
  10. {
  11. public function testIssueOpened()
  12. {
  13. $this->container['dispatcher']->addListener(GithubWebhook::EVENT_ISSUE_OPENED, array($this, 'onIssueOpened'));
  14. $p = new Project($this->container);
  15. $this->assertEquals(1, $p->create(array('name' => 'foobar')));
  16. $g = new GithubWebhook($this->container);
  17. $g->setProjectId(1);
  18. $this->assertNotFalse($g->parsePayload(
  19. 'issues',
  20. json_decode(file_get_contents(__DIR__.'/fixtures/github_issue_opened.json'), true)
  21. ));
  22. }
  23. public function testIssueAssigned()
  24. {
  25. $this->container['dispatcher']->addListener(GithubWebhook::EVENT_ISSUE_ASSIGNEE_CHANGE, array($this, 'onIssueAssigned'));
  26. $p = new Project($this->container);
  27. $this->assertEquals(1, $p->create(array('name' => 'foobar')));
  28. $tc = new TaskCreation($this->container);
  29. $this->assertEquals(1, $tc->create(array('title' => 'boo', 'reference' => 3, 'project_id' => 1)));
  30. $u = new User($this->container);
  31. $this->assertEquals(2, $u->create(array('username' => 'fguillot')));
  32. $pp = new ProjectPermission($this->container);
  33. $this->assertTrue($pp->addMember(1, 2));
  34. $g = new GithubWebhook($this->container);
  35. $g->setProjectId(1);
  36. $this->assertNotFalse($g->parsePayload(
  37. 'issues',
  38. json_decode(file_get_contents(__DIR__.'/fixtures/github_issue_assigned.json'), true)
  39. ));
  40. }
  41. public function testIssueAssignedWithNoExistingTask()
  42. {
  43. $p = new Project($this->container);
  44. $this->assertEquals(1, $p->create(array('name' => 'foobar')));
  45. $g = new GithubWebhook($this->container);
  46. $g->setProjectId(1);
  47. $payload = json_decode(file_get_contents(__DIR__.'/fixtures/github_issue_assigned.json'), true);
  48. $this->assertFalse($g->handleIssueAssigned($payload['issue']));
  49. }
  50. public function testIssueAssignedWithNoExistingUser()
  51. {
  52. $p = new Project($this->container);
  53. $this->assertEquals(1, $p->create(array('name' => 'foobar')));
  54. $tc = new TaskCreation($this->container);
  55. $this->assertEquals(1, $tc->create(array('title' => 'boo', 'reference' => 3, 'project_id' => 1)));
  56. $g = new GithubWebhook($this->container);
  57. $g->setProjectId(1);
  58. $payload = json_decode(file_get_contents(__DIR__.'/fixtures/github_issue_assigned.json'), true);
  59. $this->assertFalse($g->handleIssueAssigned($payload['issue']));
  60. }
  61. public function testIssueAssignedWithNoMember()
  62. {
  63. $p = new Project($this->container);
  64. $this->assertEquals(1, $p->create(array('name' => 'foobar')));
  65. $tc = new TaskCreation($this->container);
  66. $this->assertEquals(1, $tc->create(array('title' => 'boo', 'reference' => 3, 'project_id' => 1)));
  67. $u = new User($this->container);
  68. $this->assertEquals(2, $u->create(array('username' => 'fguillot')));
  69. $g = new GithubWebhook($this->container);
  70. $g->setProjectId(1);
  71. $payload = json_decode(file_get_contents(__DIR__.'/fixtures/github_issue_assigned.json'), true);
  72. $this->assertFalse($g->handleIssueAssigned($payload['issue']));
  73. }
  74. public function testIssueAssignedWithMember()
  75. {
  76. $p = new Project($this->container);
  77. $this->assertEquals(1, $p->create(array('name' => 'foobar')));
  78. $tc = new TaskCreation($this->container);
  79. $this->assertEquals(1, $tc->create(array('title' => 'boo', 'reference' => 3, 'project_id' => 1)));
  80. $u = new User($this->container);
  81. $this->assertEquals(2, $u->create(array('username' => 'fguillot')));
  82. $pp = new ProjectPermission($this->container);
  83. $this->assertTrue($pp->addMember(1, 2));
  84. $g = new GithubWebhook($this->container);
  85. $g->setProjectId(1);
  86. $payload = json_decode(file_get_contents(__DIR__.'/fixtures/github_issue_assigned.json'), true);
  87. $this->assertTrue($g->handleIssueAssigned($payload['issue']));
  88. }
  89. public function testIssueUnassigned()
  90. {
  91. $this->container['dispatcher']->addListener(GithubWebhook::EVENT_ISSUE_ASSIGNEE_CHANGE, array($this, 'onIssueUnassigned'));
  92. $p = new Project($this->container);
  93. $this->assertEquals(1, $p->create(array('name' => 'foobar')));
  94. $tc = new TaskCreation($this->container);
  95. $this->assertEquals(1, $tc->create(array('title' => 'boo', 'reference' => 3, 'project_id' => 1)));
  96. $g = new GithubWebhook($this->container);
  97. $g->setProjectId(1);
  98. $this->assertNotFalse($g->parsePayload(
  99. 'issues',
  100. json_decode(file_get_contents(__DIR__.'/fixtures/github_issue_unassigned.json'), true)
  101. ));
  102. }
  103. public function testIssueClosed()
  104. {
  105. $this->container['dispatcher']->addListener(GithubWebhook::EVENT_ISSUE_CLOSED, array($this, 'onIssueClosed'));
  106. $p = new Project($this->container);
  107. $this->assertEquals(1, $p->create(array('name' => 'foobar')));
  108. $tc = new TaskCreation($this->container);
  109. $this->assertEquals(1, $tc->create(array('title' => 'boo', 'reference' => 3, 'project_id' => 1)));
  110. $g = new GithubWebhook($this->container);
  111. $g->setProjectId(1);
  112. $this->assertNotFalse($g->parsePayload(
  113. 'issues',
  114. json_decode(file_get_contents(__DIR__.'/fixtures/github_issue_closed.json'), true)
  115. ));
  116. }
  117. public function testIssueClosedWithTaskNotFound()
  118. {
  119. $p = new Project($this->container);
  120. $this->assertEquals(1, $p->create(array('name' => 'foobar')));
  121. $g = new GithubWebhook($this->container);
  122. $g->setProjectId(1);
  123. $payload = json_decode(file_get_contents(__DIR__.'/fixtures/github_issue_closed.json'), true);
  124. $this->assertFalse($g->handleIssueClosed($payload['issue']));
  125. }
  126. public function testIssueReopened()
  127. {
  128. $this->container['dispatcher']->addListener(GithubWebhook::EVENT_ISSUE_REOPENED, array($this, 'onIssueReopened'));
  129. $p = new Project($this->container);
  130. $this->assertEquals(1, $p->create(array('name' => 'foobar')));
  131. $tc = new TaskCreation($this->container);
  132. $this->assertEquals(1, $tc->create(array('title' => 'boo', 'reference' => 3, 'project_id' => 1)));
  133. $g = new GithubWebhook($this->container);
  134. $g->setProjectId(1);
  135. $this->assertNotFalse($g->parsePayload(
  136. 'issues',
  137. json_decode(file_get_contents(__DIR__.'/fixtures/github_issue_reopened.json'), true)
  138. ));
  139. }
  140. public function testIssueReopenedWithTaskNotFound()
  141. {
  142. $p = new Project($this->container);
  143. $this->assertEquals(1, $p->create(array('name' => 'foobar')));
  144. $g = new GithubWebhook($this->container);
  145. $g->setProjectId(1);
  146. $payload = json_decode(file_get_contents(__DIR__.'/fixtures/github_issue_reopened.json'), true);
  147. $this->assertFalse($g->handleIssueReopened($payload['issue']));
  148. }
  149. public function testIssueLabeled()
  150. {
  151. $this->container['dispatcher']->addListener(GithubWebhook::EVENT_ISSUE_LABEL_CHANGE, array($this, 'onIssueLabeled'));
  152. $p = new Project($this->container);
  153. $this->assertEquals(1, $p->create(array('name' => 'foobar')));
  154. $tc = new TaskCreation($this->container);
  155. $this->assertEquals(1, $tc->create(array('title' => 'boo', 'reference' => 3, 'project_id' => 1)));
  156. $g = new GithubWebhook($this->container);
  157. $g->setProjectId(1);
  158. $this->assertNotFalse($g->parsePayload(
  159. 'issues',
  160. json_decode(file_get_contents(__DIR__.'/fixtures/github_issue_labeled.json'), true)
  161. ));
  162. }
  163. public function testIssueLabeledWithTaskNotFound()
  164. {
  165. $p = new Project($this->container);
  166. $this->assertEquals(1, $p->create(array('name' => 'foobar')));
  167. $g = new GithubWebhook($this->container);
  168. $g->setProjectId(1);
  169. $payload = json_decode(file_get_contents(__DIR__.'/fixtures/github_issue_labeled.json'), true);
  170. $this->assertFalse($g->handleIssueLabeled($payload['issue'], $payload['label']));
  171. }
  172. public function testIssueUnLabeled()
  173. {
  174. $this->container['dispatcher']->addListener(GithubWebhook::EVENT_ISSUE_LABEL_CHANGE, array($this, 'onIssueUnlabeled'));
  175. $p = new Project($this->container);
  176. $this->assertEquals(1, $p->create(array('name' => 'foobar')));
  177. $tc = new TaskCreation($this->container);
  178. $this->assertEquals(1, $tc->create(array('title' => 'boo', 'reference' => 3, 'project_id' => 1)));
  179. $g = new GithubWebhook($this->container);
  180. $g->setProjectId(1);
  181. $this->assertNotFalse($g->parsePayload(
  182. 'issues',
  183. json_decode(file_get_contents(__DIR__.'/fixtures/github_issue_unlabeled.json'), true)
  184. ));
  185. }
  186. public function testIssueUnLabeledWithTaskNotFound()
  187. {
  188. $p = new Project($this->container);
  189. $this->assertEquals(1, $p->create(array('name' => 'foobar')));
  190. $g = new GithubWebhook($this->container);
  191. $g->setProjectId(1);
  192. $payload = json_decode(file_get_contents(__DIR__.'/fixtures/github_issue_unlabeled.json'), true);
  193. $this->assertFalse($g->handleIssueUnlabeled($payload['issue'], $payload['label']));
  194. }
  195. public function testCommentCreatedWithNoUser()
  196. {
  197. $this->container['dispatcher']->addListener(GithubWebhook::EVENT_ISSUE_COMMENT, array($this, 'onCommentCreatedWithNoUser'));
  198. $p = new Project($this->container);
  199. $this->assertEquals(1, $p->create(array('name' => 'foobar')));
  200. $tc = new TaskCreation($this->container);
  201. $this->assertEquals(1, $tc->create(array('title' => 'boo', 'reference' => 3, 'project_id' => 1)));
  202. $g = new GithubWebhook($this->container);
  203. $g->setProjectId(1);
  204. $this->assertNotFalse($g->parsePayload(
  205. 'issue_comment',
  206. json_decode(file_get_contents(__DIR__.'/fixtures/github_comment_created.json'), true)
  207. ));
  208. }
  209. public function testCommentCreatedWithNotMember()
  210. {
  211. $this->container['dispatcher']->addListener(GithubWebhook::EVENT_ISSUE_COMMENT, array($this, 'onCommentCreatedWithNotMember'));
  212. $p = new Project($this->container);
  213. $this->assertEquals(1, $p->create(array('name' => 'foobar')));
  214. $tc = new TaskCreation($this->container);
  215. $this->assertEquals(1, $tc->create(array('title' => 'boo', 'reference' => 3, 'project_id' => 1)));
  216. $u = new User($this->container);
  217. $this->assertEquals(2, $u->create(array('username' => 'fguillot')));
  218. $g = new GithubWebhook($this->container);
  219. $g->setProjectId(1);
  220. $this->assertNotFalse($g->parsePayload(
  221. 'issue_comment',
  222. json_decode(file_get_contents(__DIR__.'/fixtures/github_comment_created.json'), true)
  223. ));
  224. }
  225. public function testCommentCreatedWithUser()
  226. {
  227. $this->container['dispatcher']->addListener(GithubWebhook::EVENT_ISSUE_COMMENT, array($this, 'onCommentCreatedWithUser'));
  228. $p = new Project($this->container);
  229. $this->assertEquals(1, $p->create(array('name' => 'foobar')));
  230. $tc = new TaskCreation($this->container);
  231. $this->assertEquals(1, $tc->create(array('title' => 'boo', 'reference' => 3, 'project_id' => 1)));
  232. $u = new User($this->container);
  233. $this->assertEquals(2, $u->create(array('username' => 'fguillot')));
  234. $pp = new ProjectPermission($this->container);
  235. $this->assertTrue($pp->addMember(1, 2));
  236. $g = new GithubWebhook($this->container);
  237. $g->setProjectId(1);
  238. $this->assertNotFalse($g->parsePayload(
  239. 'issue_comment',
  240. json_decode(file_get_contents(__DIR__.'/fixtures/github_comment_created.json'), true)
  241. ));
  242. }
  243. public function testPush()
  244. {
  245. $this->container['dispatcher']->addListener(GithubWebhook::EVENT_COMMIT, array($this, 'onPush'));
  246. $p = new Project($this->container);
  247. $this->assertEquals(1, $p->create(array('name' => 'foobar')));
  248. $tc = new TaskCreation($this->container);
  249. $this->assertEquals(1, $tc->create(array('title' => 'boo', 'project_id' => 1)));
  250. $g = new GithubWebhook($this->container);
  251. $g->setProjectId(1);
  252. $this->assertNotFalse($g->parsePayload(
  253. 'push',
  254. json_decode(file_get_contents(__DIR__.'/fixtures/github_push.json'), true)
  255. ));
  256. }
  257. public function onIssueOpened($event)
  258. {
  259. $data = $event->getAll();
  260. $this->assertEquals(1, $data['project_id']);
  261. $this->assertEquals(3, $data['reference']);
  262. $this->assertEquals('Test Webhook', $data['title']);
  263. $this->assertEquals("plop\n\n[Github Issue](https://github.com/kanboardapp/webhook/issues/3)", $data['description']);
  264. }
  265. public function onIssueAssigned($event)
  266. {
  267. $data = $event->getAll();
  268. $this->assertEquals(1, $data['project_id']);
  269. $this->assertEquals(1, $data['task_id']);
  270. $this->assertEquals(3, $data['reference']);
  271. $this->assertEquals(2, $data['owner_id']);
  272. }
  273. public function onIssueUnassigned($event)
  274. {
  275. $data = $event->getAll();
  276. $this->assertEquals(1, $data['project_id']);
  277. $this->assertEquals(1, $data['task_id']);
  278. $this->assertEquals(3, $data['reference']);
  279. $this->assertEquals(0, $data['owner_id']);
  280. }
  281. public function onIssueClosed($event)
  282. {
  283. $data = $event->getAll();
  284. $this->assertEquals(1, $data['project_id']);
  285. $this->assertEquals(1, $data['task_id']);
  286. $this->assertEquals(3, $data['reference']);
  287. }
  288. public function onIssueReopened($event)
  289. {
  290. $data = $event->getAll();
  291. $this->assertEquals(1, $data['project_id']);
  292. $this->assertEquals(1, $data['task_id']);
  293. $this->assertEquals(3, $data['reference']);
  294. }
  295. public function onIssueLabeled($event)
  296. {
  297. $data = $event->getAll();
  298. $this->assertEquals(1, $data['project_id']);
  299. $this->assertEquals(1, $data['task_id']);
  300. $this->assertEquals(3, $data['reference']);
  301. $this->assertEquals('bug', $data['label']);
  302. }
  303. public function onIssueUnlabeled($event)
  304. {
  305. $data = $event->getAll();
  306. $this->assertEquals(1, $data['project_id']);
  307. $this->assertEquals(1, $data['task_id']);
  308. $this->assertEquals(3, $data['reference']);
  309. $this->assertEquals('bug', $data['label']);
  310. $this->assertEquals(0, $data['category_id']);
  311. }
  312. public function onCommentCreatedWithNoUser($event)
  313. {
  314. $data = $event->getAll();
  315. $this->assertEquals(1, $data['project_id']);
  316. $this->assertEquals(1, $data['task_id']);
  317. $this->assertEquals(0, $data['user_id']);
  318. $this->assertEquals(113834672, $data['reference']);
  319. $this->assertEquals("test\n\n[By @fguillot on Github](https://github.com/kanboardapp/webhook/issues/3#issuecomment-113834672)", $data['comment']);
  320. }
  321. public function onCommentCreatedWithNotMember($event)
  322. {
  323. $data = $event->getAll();
  324. $this->assertEquals(1, $data['project_id']);
  325. $this->assertEquals(1, $data['task_id']);
  326. $this->assertEquals(0, $data['user_id']);
  327. $this->assertEquals(113834672, $data['reference']);
  328. $this->assertEquals("test\n\n[By @fguillot on Github](https://github.com/kanboardapp/webhook/issues/3#issuecomment-113834672)", $data['comment']);
  329. }
  330. public function onCommentCreatedWithUser($event)
  331. {
  332. $data = $event->getAll();
  333. $this->assertEquals(1, $data['project_id']);
  334. $this->assertEquals(1, $data['task_id']);
  335. $this->assertEquals(2, $data['user_id']);
  336. $this->assertEquals(113834672, $data['reference']);
  337. $this->assertEquals("test\n\n[By @fguillot on Github](https://github.com/kanboardapp/webhook/issues/3#issuecomment-113834672)", $data['comment']);
  338. }
  339. public function onPush($event)
  340. {
  341. $data = $event->getAll();
  342. $this->assertEquals(1, $data['project_id']);
  343. $this->assertEquals(1, $data['task_id']);
  344. $this->assertEquals('boo', $data['title']);
  345. $this->assertEquals("Update README to fix #1\n\n[Commit made by @fguillot on Github](https://github.com/kanboardapp/webhook/commit/98dee3e49ee7aa66ffec1f761af93da5ffd711f6)", $data['commit_comment']);
  346. $this->assertEquals('Update README to fix #1', $data['commit_message']);
  347. $this->assertEquals('https://github.com/kanboardapp/webhook/commit/98dee3e49ee7aa66ffec1f761af93da5ffd711f6', $data['commit_url']);
  348. }
  349. }