All Data Structures Functions Variables Pages
TaskNotificationUtilTest.php
1 <?php
2  /*********************************************************************************
3  * Zurmo is a customer relationship management program developed by
4  * Zurmo, Inc. Copyright (C) 2017 Zurmo Inc.
5  *
6  * Zurmo is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU Affero General Public License version 3 as published by the
8  * Free Software Foundation with the addition of the following permission added
9  * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
10  * IN WHICH THE COPYRIGHT IS OWNED BY ZURMO, ZURMO DISCLAIMS THE WARRANTY
11  * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
12  *
13  * Zurmo is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15  * FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU Affero General Public License along with
19  * this program; if not, see http://www.gnu.org/licenses or write to the Free
20  * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21  * 02110-1301 USA.
22  *
23  * You can contact Zurmo, Inc. with a mailing address at 27 North Wacker Drive
24  * Suite 370 Chicago, IL 60606. or at email address contact@zurmo.com.
25  *
26  * The interactive user interfaces in original and modified versions
27  * of this program must display Appropriate Legal Notices, as required under
28  * Section 5 of the GNU Affero General Public License version 3.
29  *
30  * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
31  * these Appropriate Legal Notices must retain the display of the Zurmo
32  * logo and Zurmo copyright notice. If the display of the logo is not reasonably
33  * feasible for technical reasons, the Appropriate Legal Notices must display the words
34  * "Copyright Zurmo Inc. 2017. All rights reserved".
35  ********************************************************************************/
36 
38  {
39  protected static $super;
40 
41  protected static $steve;
42 
43  protected static $sally;
44 
45  protected static $katie;
46 
47  public static function setUpBeforeClass()
48  {
49  parent::setUpBeforeClass();
50  Yii::app()->user->userModel = SecurityTestHelper::createSuperAdmin();
51  self::$super = Yii::app()->user->userModel;
52  self::$steve = UserTestHelper::createBasicUserWithEmailAddress('steve');
53  self::$sally = UserTestHelper::createBasicUserWithEmailAddress('sally');
54  self::$katie = UserTestHelper::createBasicUserWithEmailAddress('katie');
55  }
56 
57  public function setUp()
58  {
59  parent::setUp();
60  Yii::app()->user->userModel = User::getByUsername('super');
63  }
64 
69  {
70  $task = new Task();
71  $task->name = 'My Task';
72  $task->owner = Yii::app()->user->userModel;
73  $task->requestedByUser = Yii::app()->user->userModel;
74  $this->assertEquals(0, Yii::app()->emailHelper->getQueuedCount());
75  $this->assertEquals(0, Notification::getCount());
76  $this->assertTrue($task->save());
77  $this->assertEquals(0, Yii::app()->emailHelper->getQueuedCount());
78  $this->assertEquals(0, Notification::getCount());
79  }
80 
81  public function testTaskNewNotificationWhenOwnerIsNotCurrentUser()
82  {
83  $task = new Task();
84  $task->name = 'Her Task';
85  $task->owner = self::$sally;
86  $task->requestedByUser = Yii::app()->user->userModel;
87  $this->assertEquals(0, Yii::app()->emailHelper->getQueuedCount());
88  $this->assertEquals(0, Notification::getCount());
89  $this->assertTrue($task->save());
90  $this->assertEquals(1, Yii::app()->emailHelper->getQueuedCount());
91  $this->assertEquals(1, Notification::getCount());
92  $emailMessages = EmailMessage::getAllByFolderType(EmailFolder::TYPE_OUTBOX);
93  $this->assertCount(1, $emailMessages);
94  $this->assertEquals(1, $emailMessages[0]->recipients->count());
95  $this->assertEquals('sally@zurmo.com', $emailMessages[0]->recipients[0]->toAddress);
96  $this->assertContains('configurationEdit?id=' . self::$sally->id . '">Manage your email preferences', $emailMessages[0]->content->htmlContent);
97  }
98 
99  public function testTaskStatusBecomesInProgressNotificationWhenUsersAreNotCurrentUser()
100  {
101  $task = new Task();
102  $task->name = 'Her Task';
103  $task->owner = self::$sally;
104  $task->requestedByUser = self::$katie;
105  $this->assertEquals(0, Yii::app()->emailHelper->getQueuedCount());
106  $this->assertEquals(0, Notification::getCount());
107  $this->assertTrue($task->save());
108  $this->assertEquals(1, Yii::app()->emailHelper->getQueuedCount());
109  $this->assertEquals(1, Notification::getCount());
112  $this->assertEquals(0, Yii::app()->emailHelper->getQueuedCount());
113  $this->assertEquals(0, Notification::getCount());
114  //Now change status
115  $task->status = Task::STATUS_IN_PROGRESS;
116  $this->assertTrue($task->save());
117  //No emails should be queued up
118  $this->assertEquals(0, Yii::app()->emailHelper->getQueuedCount());
119  $this->assertEquals(0, Notification::getCount());
120  }
121 
122  public function testTaskStatusBecomesAwaitingAcceptanceNotificationWhenRequestedByUserIsCurrentUser()
123  {
124  $task = new Task();
125  $task->name = 'Her Task';
126  $task->owner = self::$sally;
127  $task->requestedByUser = Yii::app()->user->userModel;
128  $this->assertEquals(0, Yii::app()->emailHelper->getQueuedCount());
129  $this->assertEquals(0, Notification::getCount());
130  $this->assertTrue($task->save());
131  $this->assertEquals(1, Yii::app()->emailHelper->getQueuedCount());
132  $this->assertEquals(1, Notification::getCount());
135  $this->assertEquals(0, Yii::app()->emailHelper->getQueuedCount());
136  $this->assertEquals(0, Notification::getCount());
137  //Now change status
138  $task->status = Task::STATUS_AWAITING_ACCEPTANCE;
139  $this->assertTrue($task->save());
140  //No emails should be queued up
141  $this->assertEquals(0, Yii::app()->emailHelper->getQueuedCount());
142  $this->assertEquals(0, Notification::getCount());
143  }
144 
145  public function testTaskStatusBecomesAwaitingAcceptanceNotificationWhenRequestedByUserIsNotCurrentUser()
146  {
147  $task = new Task();
148  $task->name = 'Her Task';
149  $task->owner = self::$sally;
150  $task->requestedByUser = self::$katie;
151  $this->assertEquals(0, Yii::app()->emailHelper->getQueuedCount());
152  $this->assertEquals(0, Notification::getCount());
153  $this->assertTrue($task->save());
154  $this->assertEquals(1, Yii::app()->emailHelper->getQueuedCount());
155  $this->assertEquals(1, Notification::getCount());
158  $this->assertEquals(0, Yii::app()->emailHelper->getQueuedCount());
159  //Now change status
160  $task->status = Task::STATUS_AWAITING_ACCEPTANCE;
161  $this->assertTrue($task->save());
162  //One email should be queued up
163  $this->assertEquals(1, Yii::app()->emailHelper->getQueuedCount());
164  $this->assertEquals(1, Notification::getCount());
165  $emailMessages = EmailMessage::getAllByFolderType(EmailFolder::TYPE_OUTBOX);
166  $this->assertCount(1, $emailMessages);
167  $this->assertEquals(1, $emailMessages[0]->recipients->count());
168  $this->assertEquals('katie@zurmo.com', $emailMessages[0]->recipients[0]->toAddress);
169  }
170 
171  public function testTaskStatusBecomesAcceptedWithNoExtraSubscribers()
172  {
173  $task = new Task();
174  $task->name = 'Her Task';
175  $task->owner = self::$sally;
176  $task->requestedByUser = self::$katie;
177  $this->assertEquals(0, Yii::app()->emailHelper->getQueuedCount());
178  $this->assertEquals(0, Notification::getCount());
179  $this->assertTrue($task->save());
180  $this->assertEquals(1, Yii::app()->emailHelper->getQueuedCount());
181  $this->assertEquals(1, Notification::getCount());
184  $this->assertEquals(0, Yii::app()->emailHelper->getQueuedCount());
185  $this->assertEquals(0, Notification::getCount());
186  //Now change status
187  $taskId = $task->id;
188  $task->forget();
189  $task = Task::getById($taskId);
190  //$task->originalAttributeValues = array('status' => 2);
191  $task->status = Task::STATUS_COMPLETED;
192  $this->assertTrue($task->save());
193  $this->assertEquals(1, Yii::app()->emailHelper->getQueuedCount());
194  $this->assertEquals(1, Notification::getCount());
195  $emailMessages = EmailMessage::getAllByFolderType(EmailFolder::TYPE_OUTBOX);
196  $this->assertCount(1, $emailMessages);
197  $this->assertTrue('sally@zurmo.com' == $emailMessages[0]->recipients[0]->toAddress);
198  }
199 
200  public function testTaskStatusBecomesAcceptedWithExtraSubscribers()
201  {
202  $task = new Task();
203  $task->name = 'Her Task';
204  $task->owner = self::$sally;
205  $task->requestedByUser = self::$katie;
206  $notificationSubscriber = new NotificationSubscriber();
207  $notificationSubscriber->person = self::$steve;
208  $notificationSubscriber->hasReadLatest = false;
209  $task->notificationSubscribers->add($notificationSubscriber);
210  $this->assertEquals(0, Yii::app()->emailHelper->getQueuedCount());
211  $this->assertEquals(0, Notification::getCount());
212  $this->assertTrue($task->save());
213  $this->assertEquals(1, Yii::app()->emailHelper->getQueuedCount());
214  $this->assertEquals(1, Notification::getCount());
217  $taskId = $task->id;
218  $task->forget();
219  $task = Task::getById($taskId);
220  $this->assertEquals(0, Yii::app()->emailHelper->getQueuedCount());
221  //Now change status
222  $task->status = Task::STATUS_COMPLETED;
223  $this->assertTrue($task->save());
224  $this->assertEquals(1, Yii::app()->emailHelper->getQueuedCount());
225  $this->assertEquals(1, Notification::getCount());
226  $emailMessages = EmailMessage::getAllByFolderType(EmailFolder::TYPE_OUTBOX);
227  $this->assertCount(1, $emailMessages);
228  $this->assertTrue('sally@zurmo.com' == $emailMessages[0]->recipients[0]->toAddress);
229  }
230 
231  public function testTaskStatusBecomesAcceptedWhenOwnerIsCurrentUser()
232  {
233  $task = new Task();
234  $task->name = 'My Acceptance Task';
235  $task->owner = self::$sally;
236  $task->requestedByUser = self::$katie;
237  $this->assertEquals(0, Yii::app()->emailHelper->getQueuedCount());
238  $this->assertEquals(0, Notification::getCount());
239  $this->assertTrue($task->save());
240  $this->assertEquals(1, Yii::app()->emailHelper->getQueuedCount());
241  $this->assertEquals(1, Notification::getCount());
244  $taskId = $task->id;
245  $task->forget();
246  $task = Task::getById($taskId);
247  $this->assertEquals(0, Yii::app()->emailHelper->getQueuedCount());
248  $this->assertEquals(0, Notification::getCount());
249  //Now change the logged in user
250  Yii::app()->user->userModel = self::$sally;
251  //Now change status
252  $task->status = Task::STATUS_COMPLETED;
253  $this->assertTrue($task->save());
254  $this->assertEquals(0, Yii::app()->emailHelper->getQueuedCount());
255  $this->assertEquals(0, Notification::getCount());
256  $emailMessages = EmailMessage::getAllByFolderType(EmailFolder::TYPE_OUTBOX);
257  $this->assertCount(0, $emailMessages);
258  }
259 
260  public function testTaskAddCommentWithExtraSubscribers()
261  {
262  $task = new Task();
263  $task->name = 'Her Task';
264  $task->owner = self::$sally;
265  $task->requestedByUser = self::$katie;
266  $notificationSubscriber = new NotificationSubscriber();
267  $notificationSubscriber->person = self::$steve;
268  $notificationSubscriber->hasReadLatest = false;
269  $task->notificationSubscribers->add($notificationSubscriber);
270  $this->assertEquals(0, Yii::app()->emailHelper->getQueuedCount());
271  $this->assertEquals(0, Notification::getCount());
272  $this->assertTrue($task->save());
273  $this->assertEquals(1, Yii::app()->emailHelper->getQueuedCount());
274  $this->assertEquals(1, Notification::getCount());
277  $taskId = $task->id;
278  $task->forget();
279  $task = Task::getById($taskId);
280  $this->assertEquals(0, Yii::app()->emailHelper->getQueuedCount());
281  $this->assertEquals(0, Notification::getCount());
282 
283  //Now add comment
284  $comment = new Comment();
285  $comment->description = 'some comment';
286  $task->comments->add($comment);
287  $this->assertTrue($task->save());
288  $this->assertEquals(0, Yii::app()->emailHelper->getQueuedCount());
289  $this->assertEquals(0, Notification::getCount());
290  TasksNotificationUtil::submitTaskNotificationMessage($task, TasksNotificationUtil::TASK_COMMENT_CREATED_OR_UPDATED,
291  self::$super, $comment);
292  // User shouldn't get notofications because they do not have permissions on task
293  $this->assertEquals(0, Yii::app()->emailHelper->getQueuedCount());
294  $this->assertEquals(0, Notification::getCount());
295 
296  self::$sally->setRight('TasksModule', TasksModule::RIGHT_ACCESS_TASKS);
297  $this->assertTrue(self::$sally->save());
298  $task->addPermissions(self::$sally, Permission::READ_WRITE_CHANGE_PERMISSIONS);
299  $this->assertTrue($task->save());
301 
302  self::$katie->setRight('TasksModule', TasksModule::RIGHT_ACCESS_TASKS);
303  $this->assertTrue(self::$katie->save());
304  $task->addPermissions(self::$katie, Permission::READ_WRITE_CHANGE_PERMISSIONS);
305  $this->assertTrue($task->save());
307 
308  self::$steve->setRight('TasksModule', TasksModule::RIGHT_ACCESS_TASKS);
309  $this->assertTrue(self::$steve->save());
310  $task->addPermissions(self::$steve, Permission::READ_WRITE_CHANGE_PERMISSIONS);
311  $this->assertTrue($task->save());
313 
314  //Now add new comment
315  $comment = new Comment();
316  $comment->description = 'some comment 2';
317  $task->comments->add($comment);
318  $this->assertTrue($task->save());
319  $this->assertEquals(0, Yii::app()->emailHelper->getQueuedCount());
320  $this->assertEquals(0, Notification::getCount());
321  TasksNotificationUtil::submitTaskNotificationMessage($task, TasksNotificationUtil::TASK_COMMENT_CREATED_OR_UPDATED,
322  self::$super, $comment);
323 
324  $this->assertEquals(3, Yii::app()->emailHelper->getQueuedCount());
325  $this->assertEquals(3, Notification::getCount());
326  $emailMessages = EmailMessage::getAllByFolderType(EmailFolder::TYPE_OUTBOX);
327  $this->assertCount(3, $emailMessages);
328  $this->assertTrue('katie@zurmo.com' == $emailMessages[0]->recipients[0]->toAddress ||
329  'katie@zurmo.com' == $emailMessages[1]->recipients[0]->toAddress ||
330  'katie@zurmo.com' == $emailMessages[2]->recipients[0]->toAddress);
331  $this->assertTrue('sally@zurmo.com' == $emailMessages[0]->recipients[0]->toAddress ||
332  'sally@zurmo.com' == $emailMessages[1]->recipients[0]->toAddress ||
333  'sally@zurmo.com' == $emailMessages[2]->recipients[0]->toAddress);
334  $this->assertTrue('steve@zurmo.com' == $emailMessages[0]->recipients[0]->toAddress ||
335  'steve@zurmo.com' == $emailMessages[1]->recipients[0]->toAddress ||
336  'steve@zurmo.com' == $emailMessages[2]->recipients[0]->toAddress);
337  }
338 
339  public function testTaskStatusBecomesRejectedNotificationWhenOwnerIsCurrentUser()
340  {
341  $task = new Task();
342  $task->name = 'Reject Task';
343  $task->requestedByUser = self::$sally;
344  $task->owner = Yii::app()->user->userModel;
345  $this->assertEquals(0, Yii::app()->emailHelper->getQueuedCount());
346  $this->assertEquals(0, Notification::getCount());
347  $this->assertTrue($task->save());
348  //Now change status
349  $task->status = Task::STATUS_REJECTED;
350  $this->assertTrue($task->save());
351  //No emails should be queued up
352  $this->assertEquals(0, Yii::app()->emailHelper->getQueuedCount());
353  $this->assertEquals(0, Notification::getCount());
354  }
355 
356  public function testTaskStatusBecomesRejectedNotificationWhenOwnerIsNotCurrentUser()
357  {
358  $task = new Task();
359  $task->name = 'RejectOwner Task';
360  $task->requestedByUser = self::$sally;
361  $task->owner = self::$katie;
362  $this->assertEquals(0, Yii::app()->emailHelper->getQueuedCount());
363  $this->assertTrue($task->save());
364  $this->assertEquals(1, Yii::app()->emailHelper->getQueuedCount());
366  $this->assertEquals(0, Yii::app()->emailHelper->getQueuedCount());
367  //Now change status
368  $task->status = Task::STATUS_REJECTED;
369  $this->assertTrue($task->save());
370  //One email should be queued up
371  $this->assertEquals(1, Yii::app()->emailHelper->getQueuedCount());
372  $emailMessages = EmailMessage::getAllByFolderType(EmailFolder::TYPE_OUTBOX);
373  $this->assertCount(1, $emailMessages);
374  $this->assertEquals(1, $emailMessages[0]->recipients->count());
375  $this->assertEquals('katie@zurmo.com', $emailMessages[0]->recipients[0]->toAddress);
376  }
377  }
378 ?>
static securableItemGivenPermissionsForUser(SecurableItem $securableItem, User $user)
static getByUsername($username)
Definition: User.php:49
Definition: Task.php:37
static deleteAll()
static getCount(RedBeanModelJoinTablesQueryAdapter $joinTablesAdapter=null, $where=null, $modelClassName=null, $selectDistinct=false)
static getById($id, $modelClassName=null)
static submitTaskNotificationMessage(Task $task, $action, User $relatedUser=null, Comment $comment=null)
Generated on Sun May 31 2020 07:10:35