/tests/units/BitbucketWebhookTest.php

https://gitlab.com/x33n/kanboard · PHP · 398 lines · 301 code · 94 blank · 3 comment · 0 complexity · 7c9bba4f93af42a56d5880ecab3c5740 MD5 · raw file

  1. <?php
  2. require_once __DIR__.'/Base.php';
  3. use Integration\BitbucketWebhook;
  4. use Model\TaskCreation;
  5. use Model\TaskFinder;
  6. use Model\Project;
  7. use Model\ProjectPermission;
  8. use Model\User;
  9. class BitbucketWebhookTest extends Base
  10. {
  11. public function testHandlePush()
  12. {
  13. $this->container['dispatcher']->addListener(BitbucketWebhook::EVENT_COMMIT, array($this, 'onCommit'));
  14. $tc = new TaskCreation($this->container);
  15. $p = new Project($this->container);
  16. $bw = new BitbucketWebhook($this->container);
  17. $payload = json_decode(file_get_contents(__DIR__.'/fixtures/bitbucket_push.json'), true);
  18. $this->assertEquals(1, $p->create(array('name' => 'test')));
  19. $bw->setProjectId(1);
  20. // No task
  21. $this->assertFalse($bw->handlePush($payload));
  22. // Create task with the wrong id
  23. $this->assertEquals(1, $tc->create(array('title' => 'test1', 'project_id' => 1)));
  24. $this->assertFalse($bw->handlePush($payload));
  25. // Create task with the right id
  26. $this->assertEquals(2, $tc->create(array('title' => 'test2', 'project_id' => 1)));
  27. $this->assertTrue($bw->handlePush($payload));
  28. $called = $this->container['dispatcher']->getCalledListeners();
  29. $this->assertArrayHasKey(BitbucketWebhook::EVENT_COMMIT.'.BitbucketWebhookTest::onCommit', $called);
  30. }
  31. public function testIssueOpened()
  32. {
  33. $this->container['dispatcher']->addListener(BitbucketWebhook::EVENT_ISSUE_OPENED, array($this, 'onIssueOpened'));
  34. $p = new Project($this->container);
  35. $this->assertEquals(1, $p->create(array('name' => 'foobar')));
  36. $bw = new BitbucketWebhook($this->container);
  37. $bw->setProjectId(1);
  38. $this->assertNotFalse($bw->parsePayload(
  39. 'issue:created',
  40. json_decode(file_get_contents(__DIR__.'/fixtures/bitbucket_issue_opened.json'), true)
  41. ));
  42. }
  43. public function testCommentCreatedWithNoUser()
  44. {
  45. $this->container['dispatcher']->addListener(BitbucketWebhook::EVENT_ISSUE_COMMENT, array($this, 'onCommentCreatedWithNoUser'));
  46. $p = new Project($this->container);
  47. $this->assertEquals(1, $p->create(array('name' => 'foobar')));
  48. $tc = new TaskCreation($this->container);
  49. $this->assertEquals(1, $tc->create(array('title' => 'boo', 'reference' => 1, 'project_id' => 1)));
  50. $g = new BitbucketWebhook($this->container);
  51. $g->setProjectId(1);
  52. $this->assertNotFalse($g->parsePayload(
  53. 'issue:comment_created',
  54. json_decode(file_get_contents(__DIR__.'/fixtures/bitbucket_comment_created.json'), true)
  55. ));
  56. }
  57. public function testCommentCreatedWithNotMember()
  58. {
  59. $this->container['dispatcher']->addListener(BitbucketWebhook::EVENT_ISSUE_COMMENT, array($this, 'onCommentCreatedWithNotMember'));
  60. $p = new Project($this->container);
  61. $this->assertEquals(1, $p->create(array('name' => 'foobar')));
  62. $tc = new TaskCreation($this->container);
  63. $this->assertEquals(1, $tc->create(array('title' => 'boo', 'reference' => 1, 'project_id' => 1)));
  64. $u = new User($this->container);
  65. $this->assertEquals(2, $u->create(array('username' => 'fguillot')));
  66. $g = new BitbucketWebhook($this->container);
  67. $g->setProjectId(1);
  68. $this->assertNotFalse($g->parsePayload(
  69. 'issue:comment_created',
  70. json_decode(file_get_contents(__DIR__.'/fixtures/bitbucket_comment_created.json'), true)
  71. ));
  72. }
  73. public function testCommentCreatedWithUser()
  74. {
  75. $this->container['dispatcher']->addListener(BitbucketWebhook::EVENT_ISSUE_COMMENT, array($this, 'onCommentCreatedWithUser'));
  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' => 1, 'project_id' => 1)));
  80. $u = new User($this->container);
  81. $this->assertEquals(2, $u->create(array('username' => 'minicoders')));
  82. $pp = new ProjectPermission($this->container);
  83. $this->assertTrue($pp->addMember(1, 2));
  84. $g = new BitbucketWebhook($this->container);
  85. $g->setProjectId(1);
  86. $this->assertNotFalse($g->parsePayload(
  87. 'issue:comment_created',
  88. json_decode(file_get_contents(__DIR__.'/fixtures/bitbucket_comment_created.json'), true)
  89. ));
  90. }
  91. public function testIssueClosed()
  92. {
  93. $this->container['dispatcher']->addListener(BitbucketWebhook::EVENT_ISSUE_CLOSED, array($this, 'onIssueClosed'));
  94. $p = new Project($this->container);
  95. $this->assertEquals(1, $p->create(array('name' => 'foobar')));
  96. $tc = new TaskCreation($this->container);
  97. $this->assertEquals(1, $tc->create(array('title' => 'boo', 'reference' => 1, 'project_id' => 1)));
  98. $g = new BitbucketWebhook($this->container);
  99. $g->setProjectId(1);
  100. $this->assertNotFalse($g->parsePayload(
  101. 'issue:updated',
  102. json_decode(file_get_contents(__DIR__.'/fixtures/bitbucket_issue_closed.json'), true)
  103. ));
  104. }
  105. public function testIssueClosedWithNoTask()
  106. {
  107. $this->container['dispatcher']->addListener(BitbucketWebhook::EVENT_ISSUE_CLOSED, array($this, 'onIssueClosed'));
  108. $p = new Project($this->container);
  109. $this->assertEquals(1, $p->create(array('name' => 'foobar')));
  110. $tc = new TaskCreation($this->container);
  111. $this->assertEquals(1, $tc->create(array('title' => 'boo', 'reference' => 42, 'project_id' => 1)));
  112. $g = new BitbucketWebhook($this->container);
  113. $g->setProjectId(1);
  114. $this->assertFalse($g->parsePayload(
  115. 'issue:updated',
  116. json_decode(file_get_contents(__DIR__.'/fixtures/bitbucket_issue_closed.json'), true)
  117. ));
  118. $this->assertEmpty($this->container['dispatcher']->getCalledListeners());
  119. }
  120. public function testIssueReopened()
  121. {
  122. $this->container['dispatcher']->addListener(BitbucketWebhook::EVENT_ISSUE_REOPENED, array($this, 'onIssueReopened'));
  123. $p = new Project($this->container);
  124. $this->assertEquals(1, $p->create(array('name' => 'foobar')));
  125. $tc = new TaskCreation($this->container);
  126. $this->assertEquals(1, $tc->create(array('title' => 'boo', 'reference' => 1, 'project_id' => 1)));
  127. $g = new BitbucketWebhook($this->container);
  128. $g->setProjectId(1);
  129. $this->assertNotFalse($g->parsePayload(
  130. 'issue:updated',
  131. json_decode(file_get_contents(__DIR__.'/fixtures/bitbucket_issue_reopened.json'), true)
  132. ));
  133. }
  134. public function testIssueReopenedWithNoTask()
  135. {
  136. $this->container['dispatcher']->addListener(BitbucketWebhook::EVENT_ISSUE_REOPENED, array($this, 'onIssueReopened'));
  137. $p = new Project($this->container);
  138. $this->assertEquals(1, $p->create(array('name' => 'foobar')));
  139. $tc = new TaskCreation($this->container);
  140. $this->assertEquals(1, $tc->create(array('title' => 'boo', 'reference' => 42, 'project_id' => 1)));
  141. $g = new BitbucketWebhook($this->container);
  142. $g->setProjectId(1);
  143. $this->assertFalse($g->parsePayload(
  144. 'issue:updated',
  145. json_decode(file_get_contents(__DIR__.'/fixtures/bitbucket_issue_reopened.json'), true)
  146. ));
  147. $this->assertEmpty($this->container['dispatcher']->getCalledListeners());
  148. }
  149. public function testIssueUnassigned()
  150. {
  151. $this->container['dispatcher']->addListener(BitbucketWebhook::EVENT_ISSUE_ASSIGNEE_CHANGE, array($this, 'onIssueUnassigned'));
  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' => 1, 'project_id' => 1)));
  156. $g = new BitbucketWebhook($this->container);
  157. $g->setProjectId(1);
  158. $this->assertNotFalse($g->parsePayload(
  159. 'issue:updated',
  160. json_decode(file_get_contents(__DIR__.'/fixtures/bitbucket_issue_unassigned.json'), true)
  161. ));
  162. }
  163. public function testIssueAssigned()
  164. {
  165. $this->container['dispatcher']->addListener(BitbucketWebhook::EVENT_ISSUE_ASSIGNEE_CHANGE, array($this, 'onIssueAssigned'));
  166. $p = new Project($this->container);
  167. $this->assertEquals(1, $p->create(array('name' => 'foobar')));
  168. $tc = new TaskCreation($this->container);
  169. $this->assertEquals(1, $tc->create(array('title' => 'boo', 'reference' => 1, 'project_id' => 1)));
  170. $u = new User($this->container);
  171. $this->assertEquals(2, $u->create(array('username' => 'minicoders')));
  172. $pp = new ProjectPermission($this->container);
  173. $this->assertTrue($pp->addMember(1, 2));
  174. $g = new BitbucketWebhook($this->container);
  175. $g->setProjectId(1);
  176. $this->assertNotFalse($g->parsePayload(
  177. 'issue:updated',
  178. json_decode(file_get_contents(__DIR__.'/fixtures/bitbucket_issue_assigned.json'), true)
  179. ));
  180. $this->assertNotEmpty($this->container['dispatcher']->getCalledListeners());
  181. }
  182. public function testIssueAssignedWithNoPermission()
  183. {
  184. $this->container['dispatcher']->addListener(BitbucketWebhook::EVENT_ISSUE_ASSIGNEE_CHANGE, function() {});
  185. $p = new Project($this->container);
  186. $this->assertEquals(1, $p->create(array('name' => 'foobar')));
  187. $tc = new TaskCreation($this->container);
  188. $this->assertEquals(1, $tc->create(array('title' => 'boo', 'reference' => 1, 'project_id' => 1)));
  189. $u = new User($this->container);
  190. $this->assertEquals(2, $u->create(array('username' => 'minicoders')));
  191. $g = new BitbucketWebhook($this->container);
  192. $g->setProjectId(1);
  193. $this->assertFalse($g->parsePayload(
  194. 'issue:updated',
  195. json_decode(file_get_contents(__DIR__.'/fixtures/bitbucket_issue_assigned.json'), true)
  196. ));
  197. $this->assertEmpty($this->container['dispatcher']->getCalledListeners());
  198. }
  199. public function testIssueAssignedWithNoUser()
  200. {
  201. $this->container['dispatcher']->addListener(BitbucketWebhook::EVENT_ISSUE_ASSIGNEE_CHANGE, function() {});
  202. $p = new Project($this->container);
  203. $this->assertEquals(1, $p->create(array('name' => 'foobar')));
  204. $tc = new TaskCreation($this->container);
  205. $this->assertEquals(1, $tc->create(array('title' => 'boo', 'reference' => 1, 'project_id' => 1)));
  206. $g = new BitbucketWebhook($this->container);
  207. $g->setProjectId(1);
  208. $this->assertFalse($g->parsePayload(
  209. 'issue:updated',
  210. json_decode(file_get_contents(__DIR__.'/fixtures/bitbucket_issue_assigned.json'), true)
  211. ));
  212. $this->assertEmpty($this->container['dispatcher']->getCalledListeners());
  213. }
  214. public function testIssueAssignedWithNoTask()
  215. {
  216. $this->container['dispatcher']->addListener(BitbucketWebhook::EVENT_ISSUE_ASSIGNEE_CHANGE, function() {});
  217. $p = new Project($this->container);
  218. $this->assertEquals(1, $p->create(array('name' => 'foobar')));
  219. $tc = new TaskCreation($this->container);
  220. $this->assertEquals(1, $tc->create(array('title' => 'boo', 'reference' => 43, 'project_id' => 1)));
  221. $g = new BitbucketWebhook($this->container);
  222. $g->setProjectId(1);
  223. $this->assertFalse($g->parsePayload(
  224. 'issue:updated',
  225. json_decode(file_get_contents(__DIR__.'/fixtures/bitbucket_issue_assigned.json'), true)
  226. ));
  227. $this->assertEmpty($this->container['dispatcher']->getCalledListeners());
  228. }
  229. public function onCommit($event)
  230. {
  231. $data = $event->getAll();
  232. $this->assertEquals(1, $data['project_id']);
  233. $this->assertEquals(2, $data['task_id']);
  234. $this->assertEquals('test2', $data['title']);
  235. $this->assertEquals("Test another commit #2\n\n\n[Commit made by @Frederic Guillot on Bitbucket](https://bitbucket.org/minicoders/test-webhook/commits/824059cce7667d3f8d8780cc707391be821e0ea6)", $data['commit_comment']);
  236. $this->assertEquals("Test another commit #2\n", $data['commit_message']);
  237. $this->assertEquals('https://bitbucket.org/minicoders/test-webhook/commits/824059cce7667d3f8d8780cc707391be821e0ea6', $data['commit_url']);
  238. }
  239. public function onIssueOpened($event)
  240. {
  241. $data = $event->getAll();
  242. $this->assertEquals(1, $data['project_id']);
  243. $this->assertEquals(1, $data['reference']);
  244. $this->assertEquals('My new issue', $data['title']);
  245. $this->assertEquals("**test**\n\n[Bitbucket Issue](https://bitbucket.org/minicoders/test-webhook/issue/1/my-new-issue)", $data['description']);
  246. }
  247. public function onCommentCreatedWithNoUser($event)
  248. {
  249. $data = $event->getAll();
  250. $this->assertEquals(1, $data['project_id']);
  251. $this->assertEquals(1, $data['task_id']);
  252. $this->assertEquals(0, $data['user_id']);
  253. $this->assertEquals(19176252, $data['reference']);
  254. $this->assertEquals("1. step1\n2. step2\n\n[By @Frederic Guillot on Bitbucket](https://bitbucket.org/minicoders/test-webhook/issue/1#comment-19176252)", $data['comment']);
  255. }
  256. public function onCommentCreatedWithNotMember($event)
  257. {
  258. $data = $event->getAll();
  259. $this->assertEquals(1, $data['project_id']);
  260. $this->assertEquals(1, $data['task_id']);
  261. $this->assertEquals(0, $data['user_id']);
  262. $this->assertEquals(19176252, $data['reference']);
  263. $this->assertEquals("1. step1\n2. step2\n\n[By @Frederic Guillot on Bitbucket](https://bitbucket.org/minicoders/test-webhook/issue/1#comment-19176252)", $data['comment']);
  264. }
  265. public function onCommentCreatedWithUser($event)
  266. {
  267. $data = $event->getAll();
  268. $this->assertEquals(1, $data['project_id']);
  269. $this->assertEquals(1, $data['task_id']);
  270. $this->assertEquals(2, $data['user_id']);
  271. $this->assertEquals(19176252, $data['reference']);
  272. $this->assertEquals("1. step1\n2. step2\n\n[By @Frederic Guillot on Bitbucket](https://bitbucket.org/minicoders/test-webhook/issue/1#comment-19176252)", $data['comment']);
  273. }
  274. public function onIssueClosed($event)
  275. {
  276. $data = $event->getAll();
  277. $this->assertEquals(1, $data['project_id']);
  278. $this->assertEquals(1, $data['task_id']);
  279. $this->assertEquals(1, $data['reference']);
  280. }
  281. public function onIssueReopened($event)
  282. {
  283. $data = $event->getAll();
  284. $this->assertEquals(1, $data['project_id']);
  285. $this->assertEquals(1, $data['task_id']);
  286. $this->assertEquals(1, $data['reference']);
  287. }
  288. public function onIssueAssigned($event)
  289. {
  290. $data = $event->getAll();
  291. $this->assertEquals(1, $data['project_id']);
  292. $this->assertEquals(1, $data['task_id']);
  293. $this->assertEquals(1, $data['reference']);
  294. $this->assertEquals(2, $data['owner_id']);
  295. }
  296. public function onIssueUnassigned($event)
  297. {
  298. $data = $event->getAll();
  299. $this->assertEquals(1, $data['project_id']);
  300. $this->assertEquals(1, $data['task_id']);
  301. $this->assertEquals(1, $data['reference']);
  302. $this->assertEquals(0, $data['owner_id']);
  303. }
  304. }