Account Suspended
Account Suspended
This Account has been suspended.
Contact your hosting provider for more information.
 All Data Structures Functions Variables Pages
TaskTest.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 
37  class TaskTest extends ZurmoBaseTest
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  AccountTestHelper::createAccountByNameForOwner('anAccount', $super);
46  }
47 
48  public static function getDependentTestModelClassNames()
49  {
50  return array('TestManyManyRelationToItemModel');
51  }
52 
53  public function testCreateTaskWithZerosStampAndEditAgain()
54  {
55  Yii::app()->user->userModel = User::getByUsername('super');
56  $task = new Task();
57  $task->name = 'My Task';
58  $task->owner = Yii::app()->user->userModel;
59  $task->requestedByUser = Yii::app()->user->userModel;
60  $task->completedDateTime = '0000-00-00 00:00:00';
61  $saved = $task->save();
62  $this->assertTrue($saved);
63  $taskId = $task->id;
64  $task->forget();
65  unset($task);
66 
67  $task = Task::getById($taskId);
68  $task->name ='something new';
69  $saved = $task->save();
70  $this->assertTrue($saved);
71 
72  $task->delete();
73  }
74 
78  public function testCreateAndGetTaskById()
79  {
80  Yii::app()->user->userModel = User::getByUsername('super');
81  $accounts = Account::getByName('anAccount');
82 
83  $user = UserTestHelper::createBasicUser('Billy');
84  $dueStamp = DateTimeUtil::convertTimestampToDbFormatDateTime(time() + 10000);
85  $task = new Task();
86  $task->name = 'MyTask';
87  $task->owner = $user;
88  $task->requestedByUser = $user;
89  $task->dueDateTime = $dueStamp;
90  $task->status = Task::STATUS_COMPLETED;
91  $task->description = 'my test description';
92  $taskCheckListItem = new TaskCheckListItem();
93  $taskCheckListItem->name = 'Test Check List Item';
94  $task->checkListItems->add($taskCheckListItem);
95  $task->activityItems->add($accounts[0]);
96  $this->assertTrue($task->save());
97  $id = $task->id;
98  unset($task);
99  $task = Task::getById($id);
100  $this->assertEquals('MyTask', $task->name);
101  $this->assertEquals($dueStamp, $task->dueDateTime);
102  $this->assertNotNull($task->completedDateTime);
103  $this->assertEquals('my test description', $task->description);
104  $this->assertEquals($user, $task->owner);
105  $this->assertEquals($user, $task->requestedByUser);
106  $this->assertEquals(1, $task->activityItems->count());
107  $this->assertEquals($accounts[0], $task->activityItems->offsetGet(0));
108  $this->assertEquals($taskCheckListItem, $task->checkListItems->offsetGet(0));
109  foreach ($task->activityItems as $existingItem)
110  {
111  $castedDownModel = $existingItem->castDown(array('Account')); //this should not fail
112  }
113  }
114 
119  {
120  Yii::app()->user->userModel = User::getByUsername('super');
121  $accounts = Account::getByName('anAccount');
122  $accountId = $accounts[0]->id;
123  $accounts[0]->forget();
124  $task = new Task();
125  $task->activityItems->add(Account::getById($accountId));
126  foreach ($task->activityItems as $existingItem)
127  {
128  try
129  {
130  $castedDownModel = $existingItem->castDown(array(array('SecurableItem', 'OwnedSecurableItem', 'Account'))); //this should not fail
131  }
132  catch (NotFoundException $e)
133  {
134  $this->fail();
135  }
136  }
137  foreach ($task->activityItems as $existingItem)
138  {
139  try
140  {
141  $castedDownModel = $existingItem->castDown(array(array('SecurableItem', 'OwnedSecurableItem', 'Person', 'Contact'))); //this should fail
142  $this->fail();
143  }
144  catch (NotFoundException $e)
145  {
146  }
147  }
148  }
149 
153  public function testGetLabel()
154  {
155  Yii::app()->user->userModel = User::getByUsername('super');
156  $tasks = Task::getByName('MyTask');
157  $this->assertEquals(1, count($tasks));
158  $this->assertEquals('Task', $tasks[0]::getModelLabelByTypeAndLanguage('Singular'));
159  $this->assertEquals('Tasks', $tasks[0]::getModelLabelByTypeAndLanguage('Plural'));
160  }
161 
166  {
167  Yii::app()->user->userModel = User::getByUsername('super');
168  $tasks = Task::getByName('Test Task 69');
169  $this->assertEquals(0, count($tasks));
170  }
171 
175  public function testUpdateTaskFromForm()
176  {
177  Yii::app()->user->userModel = User::getByUsername('super');
178 
179  $user = User::getByUsername('billy');
180  $tasks = Task::getByName('MyTask');
181  $task = $tasks[0];
182  $this->assertEquals($task->name, 'MyTask');
183  $postData = array(
184  'owner' => array(
185  'id' => $user->id,
186  ),
187  'name' => 'New Name',
188  'dueDateTime' => '', //setting dueDate to a blank value.
189  );
190  $sanitizedPostData = PostUtil::sanitizePostByDesignerTypeForSavingModel($task, $postData);
191  $task->setAttributes($sanitizedPostData);
192  $this->assertTrue($task->save());
193  $id = $task->id;
194  unset($task);
195  $task = Task::getById($id);
196  $this->assertEquals('New Name', $task->name);
197  $this->assertEquals(null, $task->dueDateTime);
198 
199  //create new task from scratch where the DateTime attributes are not populated. It should let you save.
200  $task = new Task();
201  $postData = array(
202  'owner' => array(
203  'id' => $user->id,
204  ),
205  'name' => 'Lamazing',
206  'dueDateTime' => '', //setting dueDate to a blank value.
207  );
208  $sanitizedPostData = PostUtil::sanitizePostByDesignerTypeForSavingModel($task, $postData);
209  $task->setAttributes($sanitizedPostData);
210  $this->assertTrue($task->save());
211  $id = $task->id;
212  unset($task);
213  $task = Task::getById($id);
214  $this->assertEquals('Lamazing', $task->name);
215  $this->assertEquals(null, $task->dueDateTime);
216  }
217 
221  public function testDeleteTask()
222  {
223  Yii::app()->user->userModel = User::getByUsername('super');
224  $tasks = Task::getAll();
225  $this->assertEquals(2, count($tasks));
226  $tasks[0]->delete();
227  $tasks = Task::getAll();
228  $this->assertEquals(1, count($tasks));
229  }
230 
231  public function testManyToManyRelationInTheMiddleOfTheInheritanceHierarchy()
232  {
233  Yii::app()->user->userModel = User::getByUsername('super');
234  $accounts = Account::getByName('anAccount');
235 
236  $possibleDerivationPaths = array(
237  array('SecurableItem', 'OwnedSecurableItem', 'Account'),
238  array('SecurableItem', 'OwnedSecurableItem', 'Person', 'Contact'),
239  array('SecurableItem', 'OwnedSecurableItem', 'Opportunity'),
240  );
241 
242  $model = new TestManyManyRelationToItemModel();
243  $model->items->add($accounts[0]);
244  $this->assertTrue($model->save());
245 
246  $item = Item::getById($model->items[0]->getClassId('Item'));
247  $this->assertTrue ($item instanceof Item);
248  $this->assertFalse($item instanceof Account);
249  $this->assertTrue ($item->isSame($accounts[0]));
250  $account2 = $item->castDown($possibleDerivationPaths);
251  $this->assertTrue ($account2->isSame($accounts[0]));
252 
253  $id = $model->id;
254  unset($model);
256 
258  $this->assertEquals(1, $model->items->count());
259  $this->assertTrue ($model->items[0] instanceof Item);
260  $this->assertFalse($model->items[0] instanceof Account);
261  $this->assertTrue ($model->items[0]->isSame($accounts[0]));
262  $account3 = $model->items[0]->castDown($possibleDerivationPaths);
263  $this->assertTrue ($account3->isSame($accounts[0]));
264  }
265 
271  {
272  Yii::app()->user->userModel = User::getByUsername('super');
273 
274  //Creating a new task that is not completed. LatestDateTime should default to now, and
275  //completedDateTime should be null.
276  $task = new Task();
277  $task->name = 'aTest';
278  $nowStamp = DateTimeUtil::convertTimestampToDbFormatDateTime(time());
279  $this->assertTrue($task->save());
280  $this->assertEquals(null, $task->completedDateTime);
281 
282  //Modify the task. Complete the task. The CompletedDateTime should show as now.
283  $task = Task::getById($task->id);
284  $this->assertFalse((bool)$task->completed);
285  $this->assertEquals(null, $task->completedDateTime);
286  $task->status = Task::STATUS_COMPLETED;
287  $this->assertTrue($task->save());
288  $this->assertTrue((bool)$task->completed);
289  $this->assertNotNull($task->completedDateTime);
290  }
291 
292  public function testGetModelClassNames()
293  {
294  $modelClassNames = TasksModule::getModelClassNames();
295  $this->assertEquals(2, count($modelClassNames));
296  $this->assertEquals('Task', $modelClassNames[0]);
297  }
298 
302  public function testAddSubscriberToTask()
303  {
304  Yii::app()->user->userModel = User::getByUsername('super');
305  $user = User::getByUsername('billy');
306  $task = new Task();
307  $task->name = 'MyTest';
308  $task->owner = $user;
309  $nowStamp = DateTimeUtil::convertTimestampToDbFormatDateTime(time());
310  $this->assertTrue($task->save());
311  $this->assertEquals($user, $task->owner);
312 
313  //There would be two here as default subscribers are added
314  $this->assertEquals(2, count($task->notificationSubscribers));
315  $user = Yii::app()->user->userModel;
316  $notificationSubscriber = new NotificationSubscriber();
317  $notificationSubscriber->person = $user;
318  $notificationSubscriber->hasReadLatest = false;
319  $task->notificationSubscribers->add($notificationSubscriber);
320  $this->assertTrue($task->save());
321 
322  $task = Task::getById($task->id);
323  $subscriber = $task->notificationSubscribers->offsetGet(0);
324  $modelDerivationPathToItem = RuntimeUtil::getModelDerivationPathToItem('User');
325  $subscribedUser = $subscriber->person->castDown(array($modelDerivationPathToItem));
326  $this->assertEquals($user, $subscribedUser);
327  $this->assertEquals(3, count($task->notificationSubscribers));
328  }
329 
330  public function testAddCheckListItemsToTask()
331  {
332  Yii::app()->user->userModel = User::getByUsername('super');
333 
334  $task = new Task();
335  $task->name = 'MyTest1';
336  $nowStamp = DateTimeUtil::convertTimestampToDbFormatDateTime(time());
337  $this->assertTrue($task->save());
338 
339  $taskCheckListItem = new TaskCheckListItem();
340  $taskCheckListItem->name = 'Test Check List Item1';
341  $taskCheckListItem->completed = true;
342  $task->checkListItems->add($taskCheckListItem);
343 
344  $taskCheckListItem2 = new TaskCheckListItem();
345  $taskCheckListItem2->name = 'Test Check List Item2';
346  $task->checkListItems->add($taskCheckListItem2);
347  $this->assertTrue($task->save());
348 
349  $task = Task::getById($task->id);
350  $this->assertEquals(2, $task->checkListItems->count());
351  $fetchedCheckListItem = $task->checkListItems[0];
352  $this->assertEquals('Test Check List Item1', $fetchedCheckListItem->name);
353  $this->assertTrue((bool)$fetchedCheckListItem->completed);
354  $task->checkListItems->remove($taskCheckListItem2);
355  $this->assertTrue($task->save());
356  $task = Task::getById($task->id);
357  $this->assertEquals(1, $task->checkListItems->count());
358  }
359 
360  public function testAddCommentsToTask()
361  {
362  Yii::app()->user->userModel = User::getByUsername('super');
363 
364  $task = new Task();
365  $task->name = 'MyTest2';
366  $nowStamp = DateTimeUtil::convertTimestampToDbFormatDateTime(time());
367  $this->assertTrue($task->save());
368 
369  $comment = new Comment();
370  $comment->description = 'My Description';
371  $task->comments->add($comment);
372  $this->assertTrue($task->save());
373 
374  $task = Task::getById($task->id);
375  $this->assertEquals(1, $task->comments->count());
376  $fetchedComment = $task->comments[0];
377  $this->assertEquals('My Description', $fetchedComment->description);
378  }
379 
384  {
385  Yii::app()->user->userModel = User::getByUsername('super');
386  $tasks = Task::getByName('MyTest');
387  $isContained = NotificationSubscriberUtil::doNotificationSubscribersContainPerson($tasks[0], Yii::app()->user->userModel);
388  $this->assertTrue($isContained);
389  }
390 
391  public function testProjectValidationWithCustomPickList()
392  {
393  Yii::app()->user->userModel = User::getByUsername('super');
394  //Create a required picklist for projects
395  ModulesSearchWithDataProviderTestHelper::createDropDownAttribute(new Project(), 'pick', true);
396  $task = new Task();
397  $task->name = 'MyTestWithCustomPickList';
398  $this->assertTrue($task->save());
399  $this->assertCount(0, $task->getErrors());
400  }
401 
402  public function testAfterSaveForImportModel()
403  {
404  $testUser = UserTestHelper::createBasicUserWithEmailAddress('jimmy');
405  UserConfigurationFormAdapter::setValue($testUser, false, 'turnOffEmailNotifications');
406  Yii::app()->user->userModel = User::getByUsername('super');
407  $this->assertCount(0, EmailMessage::getAll());
408  $task = new Task();
409  $task->name = 'MyTaskWithoutImport';
410  $task->owner = $testUser;
411  $task->requestedByUser = Yii::app()->user->userModel;
412  $this->assertTrue($task->save());
413  $this->assertEquals(2, count($task->notificationSubscribers));
414  $this->assertCount(1, EmailMessage::getAll());
415 
416  //With import
417  $task = new Task();
418  $task->setScenario('importModel');
419  $task->name = 'MyTaskWithImport';
420  $task->owner = $testUser;
421  $task->requestedByUser = Yii::app()->user->userModel;
422  $this->assertTrue($task->save());
423  $this->assertEquals(2, count($task->notificationSubscribers));
424  $this->assertCount(1, EmailMessage::getAll());
425  }
426 
427  public function testTaskNotifications()
428  {
429  $super = User::getByUsername('super');
430  Yii::app()->user->userModel = $super;
431  $userOwner = UserTestHelper::createBasicUserWithEmailAddress('TaskUserOwner');
432  $userRequester = UserTestHelper::createBasicUserWithEmailAddress('TaskUserRequester');
435  $dueStamp = DateTimeUtil::convertTimestampToDbFormatDateTime(time() + 10000);
436  $completedStamp = DateTimeUtil::convertTimestampToDbFormatDateTime(time() + 9000);
437  $task = new Task();
438  $task->name = 'Task for Notifications Test';
439  $task->owner = $userOwner;
440  $task->requestedByUser = $userRequester;
441  $task->dueDateTime = $dueStamp;
442  $task->completedDateTime = $completedStamp;
443  $task->description = 'my test description';
444  $task->status = Task::STATUS_NEW;
445  $this->assertTrue($task->save());
446  //New task notification owned by other
447  $notifications = Notification::getAll();
448  $emailMessages = EmailMessage::getAll();
449  $this->assertCount(1, $notifications);
450  $this->assertCount(1, $emailMessages);
451  $this->assertContains("The task, 'Task for Notifications Test', is now owned by you.",
452  $notifications[0]->notificationMessage->textContent);
453  $this->assertContains("The task, 'Task for Notifications Test', is now owned by you.",
454  $emailMessages[0]->content->textContent);
455  //Deliver task notification
456  $task->status = TASK::STATUS_AWAITING_ACCEPTANCE;
457  $task->save();
458  $notifications = Notification::getAll();
459  $emailMessages = EmailMessage::getAll();
460  $this->assertCount(2, $notifications);
461  $this->assertCount(2, $emailMessages);
462  $this->assertContains("The task you requested, 'Task for Notifications Test', has been finished. You can now choose to accept or reject the task.",
463  $notifications[1]->notificationMessage->textContent);
464  $this->assertContains("The task you requested, 'Task for Notifications Test', has been finished. You can now choose to accept or reject the task.",
465  $emailMessages[1]->content->textContent);
466  //Reject task notification
467  $task->status = TASK::STATUS_REJECTED;
468  $task->save();
469  $notifications = Notification::getAll();
470  $emailMessages = EmailMessage::getAll();
471  $this->assertCount(3, $notifications);
472  $this->assertCount(3, $emailMessages);
473  $this->assertContains("The task, 'Task for Notifications Test', has been rejected by Clark Kent.",
474  $notifications[2]->notificationMessage->textContent);
475  $this->assertContains("The task, 'Task for Notifications Test', has been rejected by Clark Kent.",
476  $emailMessages[2]->content->textContent);
477  //Accept task notification
478  $task->status = TASK::STATUS_COMPLETED;
479  $task->save();
480  $notifications = Notification::getAll();
481  $emailMessages = EmailMessage::getAll();
482  $this->assertCount(4, $notifications);
483  $this->assertCount(4, $emailMessages);
484  $this->assertContains("The task, 'Task for Notifications Test', was accepted by Clark Kent.",
485  $notifications[3]->notificationMessage->textContent);
486  $this->assertContains("The task, 'Task for Notifications Test', was accepted by Clark Kent.",
487  $emailMessages[3]->content->textContent);
488  }
489  }
490 ?>
testAutomatedCompletedDateTimeAndLatestDateTimeChanges()
Definition: TaskTest.php:270
static getByUsername($username)
Definition: User.php:49
testAddSubscriberToTask()
Definition: TaskTest.php:302
static forgetAll()
Definition: Task.php:37
testGetTasksByNameForNonExistentName()
Definition: TaskTest.php:165
static getModelClassNames()
Definition: Module.php:565
testDoNotificationSubscribersContainPerson()
Definition: TaskTest.php:383
testDeleteTask()
Definition: TaskTest.php:221
testAddingActivityItemThatShouldCastDownAndThrowException()
Definition: TaskTest.php:118
testCreateAndGetTaskById()
Definition: TaskTest.php:78
Definition: Item.php:37
static deleteAll()
static getModelDerivationPathToItem($modelClassName)
Definition: RuntimeUtil.php:79
static doNotificationSubscribersContainPerson(OwnedSecurableItem $model, Item $item)
testGetLabel()
Definition: TaskTest.php:153
static getById($id, $modelClassName=null)
testUpdateTaskFromForm()
Definition: TaskTest.php:175
static getAll($orderBy=null, $sortDescending=false, $modelClassName=null)
static sanitizePostByDesignerTypeForSavingModel($model, $postData)
Definition: PostUtil.php:94
Generated on Fri Jul 10 2020 07:10:35
Account Suspended
Account Suspended
This Account has been suspended.
Contact your hosting provider for more information.