/tests/units/NotificationTest.php

https://gitlab.com/x33n/kanboard · PHP · 336 lines · 247 code · 77 blank · 12 comment · 0 complexity · 7d498493f86d6800f2f1ea95eb55cd0c 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\Comment;
  7. use Model\User;
  8. use Model\File;
  9. use Model\Project;
  10. use Model\ProjectPermission;
  11. use Model\Notification;
  12. use Subscriber\NotificationSubscriber;
  13. class NotificationTest extends Base
  14. {
  15. public function testFilterNone()
  16. {
  17. $u = new User($this->container);
  18. $n = new Notification($this->container);
  19. $this->assertEquals(2, $u->create(array('username' => 'user1', 'notifications_filter' => Notification::FILTER_NONE)));
  20. $this->assertTrue($n->filterNone($u->getById(2), array()));
  21. $this->assertEquals(3, $u->create(array('username' => 'user2', 'notifications_filter' => Notification::FILTER_BOTH)));
  22. $this->assertFalse($n->filterNone($u->getById(3), array()));
  23. }
  24. public function testFilterCreator()
  25. {
  26. $u = new User($this->container);
  27. $n = new Notification($this->container);
  28. $this->assertEquals(2, $u->create(array('username' => 'user1', 'notifications_filter' => Notification::FILTER_CREATOR)));
  29. $this->assertTrue($n->filterCreator($u->getById(2), array('task' => array('creator_id' => 2))));
  30. $this->assertEquals(3, $u->create(array('username' => 'user2', 'notifications_filter' => Notification::FILTER_CREATOR)));
  31. $this->assertFalse($n->filterCreator($u->getById(3), array('task' => array('creator_id' => 1))));
  32. $this->assertEquals(4, $u->create(array('username' => 'user3', 'notifications_filter' => Notification::FILTER_NONE)));
  33. $this->assertFalse($n->filterCreator($u->getById(4), array('task' => array('creator_id' => 2))));
  34. }
  35. public function testFilterAssignee()
  36. {
  37. $u = new User($this->container);
  38. $n = new Notification($this->container);
  39. $this->assertEquals(2, $u->create(array('username' => 'user1', 'notifications_filter' => Notification::FILTER_ASSIGNEE)));
  40. $this->assertTrue($n->filterAssignee($u->getById(2), array('task' => array('owner_id' => 2))));
  41. $this->assertEquals(3, $u->create(array('username' => 'user2', 'notifications_filter' => Notification::FILTER_ASSIGNEE)));
  42. $this->assertFalse($n->filterAssignee($u->getById(3), array('task' => array('owner_id' => 1))));
  43. $this->assertEquals(4, $u->create(array('username' => 'user3', 'notifications_filter' => Notification::FILTER_NONE)));
  44. $this->assertFalse($n->filterAssignee($u->getById(4), array('task' => array('owner_id' => 2))));
  45. }
  46. public function testFilterBoth()
  47. {
  48. $u = new User($this->container);
  49. $n = new Notification($this->container);
  50. $this->assertEquals(2, $u->create(array('username' => 'user1', 'notifications_filter' => Notification::FILTER_BOTH)));
  51. $this->assertTrue($n->filterBoth($u->getById(2), array('task' => array('owner_id' => 2, 'creator_id' => 1))));
  52. $this->assertTrue($n->filterBoth($u->getById(2), array('task' => array('owner_id' => 0, 'creator_id' => 2))));
  53. $this->assertEquals(3, $u->create(array('username' => 'user2', 'notifications_filter' => Notification::FILTER_BOTH)));
  54. $this->assertFalse($n->filterBoth($u->getById(3), array('task' => array('owner_id' => 1, 'creator_id' => 1))));
  55. $this->assertFalse($n->filterBoth($u->getById(3), array('task' => array('owner_id' => 2, 'creator_id' => 1))));
  56. $this->assertEquals(4, $u->create(array('username' => 'user3', 'notifications_filter' => Notification::FILTER_NONE)));
  57. $this->assertFalse($n->filterBoth($u->getById(4), array('task' => array('owner_id' => 2, 'creator_id' => 1))));
  58. }
  59. public function testFilterProject()
  60. {
  61. $u = new User($this->container);
  62. $n = new Notification($this->container);
  63. $p = new Project($this->container);
  64. $this->assertEquals(1, $p->create(array('name' => 'UnitTest1')));
  65. $this->assertEquals(2, $p->create(array('name' => 'UnitTest2')));
  66. // No project selected
  67. $this->assertTrue($n->filterProject($u->getById(1), array()));
  68. // User that select only some projects
  69. $this->assertEquals(2, $u->create(array('username' => 'user2', 'notifications_filter' => Notification::FILTER_NONE)));
  70. $n->saveSettings(2, array('notifications_enabled' => 1, 'projects' => array(2 => true)));
  71. $this->assertFalse($n->filterProject($u->getById(2), array('task' => array('project_id' => 1))));
  72. $this->assertTrue($n->filterProject($u->getById(2), array('task' => array('project_id' => 2))));
  73. }
  74. public function testFilterUserWithNoFilter()
  75. {
  76. $u = new User($this->container);
  77. $n = new Notification($this->container);
  78. $p = new Project($this->container);
  79. $this->assertEquals(2, $u->create(array('username' => 'user2', 'notifications_filter' => Notification::FILTER_NONE)));
  80. $this->assertTrue($n->shouldReceiveNotification($u->getById(2), array('task' => array('project_id' => 1))));
  81. }
  82. public function testFilterUserWithAssigneeFilter()
  83. {
  84. $u = new User($this->container);
  85. $n = new Notification($this->container);
  86. $p = new Project($this->container);
  87. $this->assertEquals(2, $u->create(array('username' => 'user2', 'notifications_filter' => Notification::FILTER_ASSIGNEE)));
  88. $this->assertTrue($n->shouldReceiveNotification($u->getById(2), array('task' => array('project_id' => 1, 'owner_id' => 2))));
  89. $this->assertFalse($n->shouldReceiveNotification($u->getById(2), array('task' => array('project_id' => 1, 'owner_id' => 1))));
  90. }
  91. public function testFilterUserWithCreatorFilter()
  92. {
  93. $u = new User($this->container);
  94. $n = new Notification($this->container);
  95. $p = new Project($this->container);
  96. $this->assertEquals(2, $u->create(array('username' => 'user2', 'notifications_filter' => Notification::FILTER_CREATOR)));
  97. $this->assertTrue($n->shouldReceiveNotification($u->getById(2), array('task' => array('project_id' => 1, 'creator_id' => 2))));
  98. $this->assertFalse($n->shouldReceiveNotification($u->getById(2), array('task' => array('project_id' => 1, 'creator_id' => 1))));
  99. }
  100. public function testFilterUserWithBothFilter()
  101. {
  102. $u = new User($this->container);
  103. $n = new Notification($this->container);
  104. $p = new Project($this->container);
  105. $this->assertEquals(2, $u->create(array('username' => 'user2', 'notifications_filter' => Notification::FILTER_BOTH)));
  106. $this->assertTrue($n->shouldReceiveNotification($u->getById(2), array('task' => array('project_id' => 1, 'creator_id' => 2, 'owner_id' => 3))));
  107. $this->assertTrue($n->shouldReceiveNotification($u->getById(2), array('task' => array('project_id' => 1, 'creator_id' => 0, 'owner_id' => 2))));
  108. $this->assertFalse($n->shouldReceiveNotification($u->getById(2), array('task' => array('project_id' => 1, 'creator_id' => 4, 'owner_id' => 1))));
  109. $this->assertFalse($n->shouldReceiveNotification($u->getById(2), array('task' => array('project_id' => 1, 'creator_id' => 5, 'owner_id' => 0))));
  110. }
  111. public function testFilterUserWithBothFilterAndProjectSelected()
  112. {
  113. $u = new User($this->container);
  114. $n = new Notification($this->container);
  115. $p = new Project($this->container);
  116. $this->assertEquals(1, $p->create(array('name' => 'UnitTest1')));
  117. $this->assertEquals(2, $p->create(array('name' => 'UnitTest2')));
  118. $this->assertEquals(2, $u->create(array('username' => 'user2', 'notifications_filter' => Notification::FILTER_BOTH)));
  119. $n->saveSettings(2, array('notifications_enabled' => 1, 'projects' => array(2 => true)));
  120. $this->assertFalse($n->shouldReceiveNotification($u->getById(2), array('task' => array('project_id' => 1, 'creator_id' => 2, 'owner_id' => 3))));
  121. $this->assertFalse($n->shouldReceiveNotification($u->getById(2), array('task' => array('project_id' => 1, 'creator_id' => 0, 'owner_id' => 2))));
  122. $this->assertTrue($n->shouldReceiveNotification($u->getById(2), array('task' => array('project_id' => 2, 'creator_id' => 2, 'owner_id' => 3))));
  123. $this->assertTrue($n->shouldReceiveNotification($u->getById(2), array('task' => array('project_id' => 2, 'creator_id' => 0, 'owner_id' => 2))));
  124. }
  125. public function testGetProjectMembersWithNotifications()
  126. {
  127. $u = new User($this->container);
  128. $p = new Project($this->container);
  129. $n = new Notification($this->container);
  130. $pp = new ProjectPermission($this->container);
  131. $this->assertEquals(1, $p->create(array('name' => 'UnitTest1')));
  132. // Email + Notifications enabled
  133. $this->assertNotFalse($u->create(array('username' => 'user1', 'email' => 'user1@here', 'notifications_enabled' => 1)));
  134. // No email + Notifications enabled
  135. $this->assertNotFalse($u->create(array('username' => 'user2', 'email' => '', 'notifications_enabled' => 1)));
  136. // Email + Notifications enabled
  137. $this->assertNotFalse($u->create(array('username' => 'user3', 'email' => 'user3@here', 'notifications_enabled' => 1)));
  138. // No email + notifications disabled
  139. $this->assertNotFalse($u->create(array('username' => 'user4')));
  140. // Nobody is member of any projects
  141. $this->assertEmpty($pp->getMembers(1));
  142. $this->assertEmpty($n->getUsersWithNotificationEnabled(1));
  143. // We allow all users to be member of our projects
  144. $this->assertTrue($pp->addMember(1, 1));
  145. $this->assertTrue($pp->addMember(1, 2));
  146. $this->assertTrue($pp->addMember(1, 3));
  147. $this->assertTrue($pp->addMember(1, 4));
  148. $this->assertNotEmpty($pp->getMembers(1));
  149. $users = $n->getUsersWithNotificationEnabled(1);
  150. $this->assertNotEmpty($users);
  151. $this->assertEquals(2, count($users));
  152. $this->assertEquals('user1@here', $users[0]['email']);
  153. $this->assertEquals('user3@here', $users[1]['email']);
  154. }
  155. public function testGetUsersWithNotificationsWhenEverybodyAllowed()
  156. {
  157. $u = new User($this->container);
  158. $p = new Project($this->container);
  159. $n = new Notification($this->container);
  160. $pp = new ProjectPermission($this->container);
  161. $this->assertEquals(1, $p->create(array('name' => 'UnitTest1', 'is_everybody_allowed' => 1)));
  162. $this->assertTrue($pp->isEverybodyAllowed(1));
  163. // Email + Notifications enabled
  164. $this->assertNotFalse($u->create(array('username' => 'user1', 'email' => 'user1@here', 'notifications_enabled' => 1)));
  165. // No email + Notifications enabled
  166. $this->assertNotFalse($u->create(array('username' => 'user2', 'email' => '', 'notifications_enabled' => 1)));
  167. // Email + Notifications enabled
  168. $this->assertNotFalse($u->create(array('username' => 'user3', 'email' => 'user3@here', 'notifications_enabled' => 1)));
  169. // No email + notifications disabled
  170. $this->assertNotFalse($u->create(array('username' => 'user4')));
  171. $users = $n->getUsersWithNotificationEnabled(1);
  172. $this->assertNotEmpty($users);
  173. $this->assertEquals(2, count($users));
  174. $this->assertEquals('user1@here', $users[0]['email']);
  175. $this->assertEquals('user3@here', $users[1]['email']);
  176. }
  177. public function testGetMailContent()
  178. {
  179. $n = new Notification($this->container);
  180. $p = new Project($this->container);
  181. $tf = new TaskFinder($this->container);
  182. $tc = new TaskCreation($this->container);
  183. $s = new Subtask($this->container);
  184. $c = new Comment($this->container);
  185. $f = new File($this->container);
  186. $this->assertEquals(1, $p->create(array('name' => 'test')));
  187. $this->assertEquals(1, $tc->create(array('title' => 'test', 'project_id' => 1)));
  188. $this->assertEquals(1, $s->create(array('title' => 'test', 'task_id' => 1)));
  189. $this->assertEquals(1, $c->create(array('comment' => 'test', 'task_id' => 1, 'user_id' => 1)));
  190. $this->assertEquals(1, $f->create(1, 'test', 'blah', 123));
  191. $task = $tf->getDetails(1);
  192. $subtask = $s->getById(1, true);
  193. $comment = $c->getById(1);
  194. $file = $c->getById(1);
  195. $this->assertNotEmpty($task);
  196. $this->assertNotEmpty($subtask);
  197. $this->assertNotEmpty($comment);
  198. $this->assertNotEmpty($file);
  199. foreach (Subscriber\NotificationSubscriber::getSubscribedEvents() as $event => $values) {
  200. $this->assertNotEmpty($n->getMailContent($event, array('task' => $task, 'comment' => $comment, 'subtask' => $subtask, 'file' => $file, 'changes' => array())));
  201. }
  202. }
  203. public function testGetEmailSubject()
  204. {
  205. $n = new Notification($this->container);
  206. $this->assertEquals(
  207. '[test][Task opened] blah (#2)',
  208. $n->getMailSubject('task.open', array('task' => array('id' => 2, 'title' => 'blah', 'project_name' => 'test')))
  209. );
  210. }
  211. public function testSendNotificationsToCreator()
  212. {
  213. $u = new User($this->container);
  214. $p = new Project($this->container);
  215. $n = new Notification($this->container);
  216. $pp = new ProjectPermission($this->container);
  217. $this->assertEquals(1, $p->create(array('name' => 'UnitTest1')));
  218. $this->assertEquals(2, $u->create(array('username' => 'user1', 'email' => 'user1@here', 'notifications_enabled' => 1)));
  219. $this->assertTrue($pp->addMember(1, 2));
  220. $this->container['emailClient']->expects($this->once())
  221. ->method('send')
  222. ->with(
  223. $this->equalTo('user1@here'),
  224. $this->equalTo('user1'),
  225. $this->equalTo('[test][Task opened] blah (#2)'),
  226. $this->stringContains('blah')
  227. );
  228. $n->sendNotifications('task.open', array('task' => array(
  229. 'id' => 2, 'title' => 'blah', 'project_name' => 'test', 'project_id' => 1, 'owner_id' => 0, 'creator_id' => 2
  230. )));
  231. }
  232. public function testSendNotificationsToAnotherAssignee()
  233. {
  234. $u = new User($this->container);
  235. $p = new Project($this->container);
  236. $n = new Notification($this->container);
  237. $pp = new ProjectPermission($this->container);
  238. $this->assertEquals(1, $p->create(array('name' => 'UnitTest1')));
  239. $this->assertEquals(2, $u->create(array('username' => 'user1', 'email' => 'user1@here', 'notifications_enabled' => 1)));
  240. $this->assertTrue($pp->addMember(1, 2));
  241. $this->container['emailClient']->expects($this->never())->method('send');
  242. $n->sendNotifications('task.open', array('task' => array(
  243. 'id' => 2, 'title' => 'blah', 'project_name' => 'test', 'project_id' => 1, 'owner_id' => 1, 'creator_id' => 1
  244. )));
  245. }
  246. public function testSendNotificationsToNotMember()
  247. {
  248. $u = new User($this->container);
  249. $p = new Project($this->container);
  250. $n = new Notification($this->container);
  251. $pp = new ProjectPermission($this->container);
  252. $this->assertEquals(1, $p->create(array('name' => 'UnitTest1')));
  253. $this->assertEquals(2, $u->create(array('username' => 'user1', 'email' => 'user1@here', 'notifications_enabled' => 1)));
  254. $this->container['emailClient']->expects($this->never())->method('send');
  255. $n->sendNotifications('task.open', array('task' => array(
  256. 'id' => 2, 'title' => 'blah', 'project_name' => 'test', 'project_id' => 1, 'owner_id' => 0, 'creator_id' => 2
  257. )));
  258. }
  259. }