All Data Structures Functions Variables Pages
NotificationSubscriberUtilTest.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  public static function setUpBeforeClass()
40  {
41  parent::setUpBeforeClass();
42  SecurityTestHelper::createSuperAdmin();
43  $super = User::getByUsername('super');
44  Yii::app()->user->userModel = $super;
45  $user = UserTestHelper::createBasicUser('Tilly');
46  TaskTestHelper::createTaskByNameForOwner('My Task', $super);
47  AccountTestHelper::createAccountByNameForOwner('anAccount', $super);
48  }
49 
50  public function setUp()
51  {
52  parent::setUp();
53  Yii::app()->user->userModel = User::getByUsername('super');
54  }
55 
59  public function testMarkUserHasReadLatest()
60  {
61  $super = User::getByUsername('super');
62  $steven = UserTestHelper::createBasicUser('steven');
63 
64  $task = new Task();
65  $task->name = 'MyTest';
66  $nowStamp = DateTimeUtil::convertTimestampToDbFormatDateTime(time());
67  $this->assertTrue($task->save());
68 
69  $task = Task::getById($task->id);
70  $user = Yii::app()->user->userModel;
71  $notificationSubscriber = new NotificationSubscriber();
72  $notificationSubscriber->person = $steven;
73  $notificationSubscriber->hasReadLatest = false;
74  $task->notificationSubscribers->add($notificationSubscriber);
75  $this->assertTrue($task->save());
76 
77  $id = $task->id;
78  $task->forget();
79  unset($task);
80 
81  $task = Task::getById($id);
82  $this->assertEquals(0, $task->notificationSubscribers->offsetGet(0)->hasReadLatest);
83  $this->assertEquals(0, $task->notificationSubscribers->offsetGet(1)->hasReadLatest);
84  //After running for super, nothing will change.
87  $id = $task->id;
88  $task->forget();
89  unset($task);
90 
91  $task = Task::getById($id);
92  foreach ($task->notificationSubscribers as $position => $subscriber)
93  {
94  $this->assertEquals(1, $subscriber->hasReadLatest);
95  }
96  }
97 
102  public function testGetSubscriberData()
103  {
104  $user = User::getByUsername('steven');
105 
106  $tasks = Task::getByName('MyTest');
107  $task = $tasks[0];
108 
110  $this->assertContains('gravatar', $content);
111  $this->assertContains('users/default/details', $content);
112  }
113 
117  public function testGetSubscribers()
118  {
119  $user = User::getByUsername('steven');
120 
121  $tasks = Task::getByName('MyTest');
122  $task = $tasks[0];
123 
125  $found = false;
126  foreach ($subscribers as $subscriber)
127  {
128  if ($subscriber->id == $user->id)
129  {
130  $found = true;
131  }
132  }
133  $this->assertTrue($found);
134  }
135 
139  public function testResolveSubscriptionLink()
140  {
141  $tasks = Task::getByName('MyTest');
142  $task = $tasks[0];
143  $sally = UserTestHelper::createBasicUser('sally');
144  $maggi = UserTestHelper::createBasicUser('maggi');
145  $task->owner = $sally;
146  $task->requestedByUser = $maggi;
147  $task->save();
148  if (NotificationSubscriberUtil::doNotificationSubscribersContainPerson($task, Yii::app()->user->userModel) === false)
149  {
150  $notificationSubscriber = new NotificationSubscriber();
151  $notificationSubscriber->person = Yii::app()->user->userModel;
152  $task->notificationSubscribers->add($notificationSubscriber);
153  $task->save();
154  }
156  $this->assertContains('unsubscribe-model-link', $link);
157  $modelDerivationPathToItem = RuntimeUtil::getModelDerivationPathToItem('User');
158  foreach ($task->notificationSubscribers as $notificationSubscriber)
159  {
160  $user = $notificationSubscriber->person->castDown(array($modelDerivationPathToItem));
161  if ($user->id == Yii::app()->user->userModel->id)
162  {
163  $task->notificationSubscribers->remove($notificationSubscriber);
164  }
165  }
166  $task->save();
168  $this->assertContains('subscribe-model-link', $link);
169  }
170 
175  {
176  $tasks = Task::getByName('MyTest');
177  $task = $tasks[0];
178  if (NotificationSubscriberUtil::doNotificationSubscribersContainPerson($task, Yii::app()->user->userModel) === false)
179  {
180  $notificationSubscriber = new NotificationSubscriber();
181  $notificationSubscriber->person = Yii::app()->user->userModel;
182  $task->notificationSubscribers->add($notificationSubscriber);
183  $task->save();
184  }
186  $this->assertContains('detail-unsubscribe-model-link', $link);
187  $modelDerivationPathToItem = RuntimeUtil::getModelDerivationPathToItem('User');
188  foreach ($task->notificationSubscribers as $index => $notificationSubscriber)
189  {
190  $user = $notificationSubscriber->person->castDown(array($modelDerivationPathToItem));
191  if ($user->id == Yii::app()->user->userModel->id)
192  {
193  $task->notificationSubscribers->remove($notificationSubscriber);
194  }
195  }
196  $task->save();
198  $this->assertContains('detail-subscribe-model-link', $link);
199  }
200 
204  public function testAddSubscriberToModel()
205  {
206  Yii::app()->user->userModel = User::getByUsername('super');
207  $user = User::getByUsername('tilly');
208  $task = new Task();
209  $task->name = 'MyTest';
210  $task->owner = $user;
211  $nowStamp = DateTimeUtil::convertTimestampToDbFormatDateTime(time());
212  $this->assertTrue($task->save());
213  $this->assertEquals($user, $task->owner);
214 
215  //There would be two here as default subscribers are added
216  $this->assertEquals(2, count($task->notificationSubscribers));
217  $user = Yii::app()->user->userModel;
219  $task->save();
220  $this->assertEquals(2, count($task->notificationSubscribers));
221  }
222 
227  {
228  $hellodear = UserTestHelper::createBasicUser('hellodear');
229  $task = new Task();
230  $task->name = 'MyCardTest';
231  $task->owner = $hellodear;
232  $this->assertTrue($task->save());
233 
234  $task = Task::getById($task->id);
235  $user = Yii::app()->user->userModel;
237  $this->assertTrue($task->save());
239  $this->assertContains('gravatar', $content);
240  $this->assertContains('users/default/details', $content);
241  $this->assertContains('hellodear', $content);
242  $this->assertContains('task-owner', $content);
243  }
244 
249  {
250  $modelDerivationPathToItem = RuntimeUtil::getModelDerivationPathToItem('User');
251 
252  $mark = UserTestHelper::createBasicUser('mark');
253  $jim = UserTestHelper::createBasicUser('jim');
254 
255  $task = TaskTestHelper::createTaskByNameForOwner('SubTask', Yii::app()->user->userModel);
256  $this->assertEquals(1, count($task->notificationSubscribers));
257  $subscribedUser = $task->notificationSubscribers[0]->person->castDown(array($modelDerivationPathToItem));
258  $this->assertEquals(Yii::app()->user->userModel->id, $subscribedUser->id);
259 
261  $this->assertEquals(2, count($task->notificationSubscribers));
262  $subscribedUser1 = $task->notificationSubscribers[0]->person->castDown(array($modelDerivationPathToItem));
263  $subscribedUser2 = $task->notificationSubscribers[1]->person->castDown(array($modelDerivationPathToItem));
264  $this->assertTrue(in_array(Yii::app()->user->userModel->id, array($subscribedUser1->id, $subscribedUser2->id)));
265  $this->assertTrue(in_array($mark->id, array($subscribedUser1->id, $subscribedUser2->id)));
266 
268  $this->assertEquals(3, count($task->notificationSubscribers));
269  $subscribedUser1 = $task->notificationSubscribers[0]->person->castDown(array($modelDerivationPathToItem));
270  $subscribedUser2 = $task->notificationSubscribers[1]->person->castDown(array($modelDerivationPathToItem));
271  $subscribedUser3 = $task->notificationSubscribers[2]->person->castDown(array($modelDerivationPathToItem));
272  $this->assertTrue(in_array(Yii::app()->user->userModel->id, array($subscribedUser1->id, $subscribedUser2->id, $subscribedUser3->id)));
273  $this->assertTrue(in_array($mark->id, array($subscribedUser1->id, $subscribedUser2->id, $subscribedUser3->id)));
274  $this->assertTrue(in_array($jim->id, array($subscribedUser1->id, $subscribedUser2->id, $subscribedUser3->id)));
275  }
276 
282  {
283  $modelDerivationPathToItem = RuntimeUtil::getModelDerivationPathToItem('User');
284 
285  $maria = UserTestHelper::createBasicUser('maria');
286  $john = UserTestHelper::createBasicUser('john');
287 
288  $task = TaskTestHelper::createTaskByNameForOwner('SubTask2', Yii::app()->user->userModel);
291  // Just check number of subscribed users, checking if their ids are done in testProcessTaskSubscriptionRequest
292  $this->assertEquals(3, count($task->notificationSubscribers));
293 
295  $this->assertEquals(2, count($task->notificationSubscribers));
296  $subscribedUser1 = $task->notificationSubscribers[0]->person->castDown(array($modelDerivationPathToItem));
297  $subscribedUser2 = $task->notificationSubscribers[1]->person->castDown(array($modelDerivationPathToItem));
298  $this->assertTrue(in_array(Yii::app()->user->userModel->id, array($subscribedUser1->id, $subscribedUser2->id)));
299  $this->assertTrue(in_array($john->id, array($subscribedUser1->id, $subscribedUser2->id)));
300 
302  $this->assertEquals(1, count($task->notificationSubscribers));
303  $subscribedUser = $task->notificationSubscribers[0]->person->castDown(array($modelDerivationPathToItem));
304  $this->assertEquals(Yii::app()->user->userModel->id, $subscribedUser->id);
305  }
306  }
307 ?>
static addSubscriber(User $user, OwnedSecurableItem $model, $hasReadLatest=false)
static getByUsername($username)
Definition: User.php:49
static processUnsubscriptionRequest(OwnedSecurableItem $model, User $user)
static getDetailSubscriptionLink(OwnedSecurableItem $model, $row)
static markUserHasReadLatest(OwnedSecurableItem $model, User $user, $hasReadLatest=true)
Definition: Task.php:37
static resolveAndRenderTaskCardDetailsSubscribersContent(OwnedSecurableItem $model)
static processSubscriptionRequest(OwnedSecurableItem $model, User $user)
static getModelDerivationPathToItem($modelClassName)
Definition: RuntimeUtil.php:79
static doNotificationSubscribersContainPerson(OwnedSecurableItem $model, Item $item)
static getById($id, $modelClassName=null)
static getKanbanSubscriptionLink(OwnedSecurableItem $model, $row)
static getSubscriberData(OwnedSecurableItem $model)
static getModelSubscribers(OwnedSecurableItem $model)
Generated on Thu Jan 23 2020 07:10:20