CRM-19184 - Activities target contact list does not truncate
[civicrm-core.git] / tests / phpunit / CRM / Activity / BAO / ActivityTest.php
1 <?php
2
3 /**
4 * Class CRM_Activity_BAO_ActivityTest
5 * @group headless
6 */
7 class CRM_Activity_BAO_ActivityTest extends CiviUnitTestCase {
8 public function setUp() {
9 parent::setUp();
10 }
11
12 public function tearDown() {
13 // truncate a few tables
14 $tablesToTruncate = array('civicrm_contact', 'civicrm_activity', 'civicrm_activity_contact');
15 $this->quickCleanup($tablesToTruncate);
16 }
17
18 /**
19 * Test case for create() method.
20 */
21 public function testCreate() {
22 $contactId = $this->individualCreate();
23
24 $params = array(
25 'source_contact_id' => $contactId,
26 'subject' => 'Scheduling Meeting',
27 'activity_type_id' => 2,
28 );
29
30 CRM_Activity_BAO_Activity::create($params);
31
32 $activityId = $this->assertDBNotNull('CRM_Activity_DAO_Activity', 'Scheduling Meeting', 'id',
33 'subject', 'Database check for created activity.'
34 );
35
36 // Now call create() to modify an existing Activity
37
38 $params = array(
39 'id' => $activityId,
40 'source_contact_id' => $contactId,
41 'subject' => 'Scheduling Interview',
42 'activity_type_id' => 3,
43 );
44
45 CRM_Activity_BAO_Activity::create($params);
46
47 $activityTypeId = $this->assertDBNotNull('CRM_Activity_DAO_Activity', 'Scheduling Interview',
48 'activity_type_id',
49 'subject', 'Database check on updated activity record.'
50 );
51 $this->assertEquals($activityTypeId, 3, 'Verify activity type id is 3.');
52
53 $this->contactDelete($contactId);
54 }
55
56 /**
57 * Test case for getContactActivity() method.
58 *
59 * getContactActivity() method get activities detail for given target contact id.
60 */
61 public function testGetContactActivity() {
62 $contactId = $this->individualCreate();
63 $params = array(
64 'first_name' => 'liz',
65 'last_name' => 'hurleey',
66 );
67 $targetContactId = $this->individualCreate($params);
68
69 $params = array(
70 'source_contact_id' => $contactId,
71 'subject' => 'Scheduling Meeting',
72 'activity_type_id' => 2,
73 'target_contact_id' => array($targetContactId),
74 'activity_date_time' => date('Ymd'),
75 );
76
77 $this->callAPISuccess('Activity', 'create', $params);
78
79 $activityId = $this->assertDBNotNull('CRM_Activity_DAO_Activity', 'Scheduling Meeting',
80 'id',
81 'subject', 'Database check for created activity.'
82 );
83
84 $activities = CRM_Activity_BAO_Activity::getContactActivity($targetContactId);
85
86 $this->assertEquals($activities[$activityId]['subject'], 'Scheduling Meeting', 'Verify activity subject is correct.');
87
88 $this->contactDelete($contactId);
89 $this->contactDelete($targetContactId);
90 }
91
92 /**
93 * Test case for retrieve() method.
94 *
95 * Retrieve($params, $defaults) method return activity detail for given params
96 * and set defaults.
97 */
98 public function testRetrieve() {
99 $contactId = $this->individualCreate();
100 $params = array(
101 'first_name' => 'liz',
102 'last_name' => 'hurleey',
103 );
104 $targetContactId = $this->individualCreate($params);
105
106 $params = array(
107 'source_contact_id' => $contactId,
108 'subject' => 'Scheduling Meeting',
109 'activity_type_id' => 2,
110 'target_contact_id' => array($targetContactId),
111 'activity_date_time' => date('Ymd'),
112 );
113
114 CRM_Activity_BAO_Activity::create($params);
115
116 $activityId = $this->assertDBNotNull('CRM_Activity_DAO_Activity', 'Scheduling Meeting', 'id',
117 'subject', 'Database check for created activity.'
118 );
119
120 $activityTargetId = $this->assertDBNotNull('CRM_Activity_DAO_ActivityContact', $targetContactId,
121 'id', 'contact_id',
122 'Database check for created activity target.'
123 );
124
125 $defaults = array();
126 $activity = CRM_Activity_BAO_Activity::retrieve($params, $defaults);
127
128 $this->assertEquals($activity->subject, 'Scheduling Meeting', 'Verify activity subject is correct.');
129 $this->assertEquals($activity->activity_type_id, 2, 'Verify activity type id is correct.');
130 $this->assertEquals($defaults['source_contact_id'], $contactId, 'Verify source contact id is correct.');
131
132 $this->assertEquals($defaults['subject'], 'Scheduling Meeting', 'Verify activity subject is correct.');
133 $this->assertEquals($defaults['activity_type_id'], 2, 'Verify activity type id is correct.');
134
135 $this->assertEquals($defaults['target_contact'][0], $targetContactId, 'Verify target contact id is correct.');
136
137 $this->contactDelete($contactId);
138 $this->contactDelete($targetContactId);
139 }
140
141 /**
142 * Test case for deleteActivity() method.
143 *
144 * deleteActivity($params) method deletes activity for given params.
145 */
146 public function testDeleteActivity() {
147 $contactId = $this->individualCreate();
148 $params = array(
149 'first_name' => 'liz',
150 'last_name' => 'hurleey',
151 );
152 $targetContactId = $this->individualCreate($params);
153
154 $params = array(
155 'source_contact_id' => $contactId,
156 'source_record_id' => $contactId,
157 'subject' => 'Scheduling Meeting',
158 'activity_type_id' => 2,
159 'target_contact_id' => array($targetContactId),
160 'activity_date_time' => date('Ymd'),
161 );
162
163 CRM_Activity_BAO_Activity::create($params);
164
165 $activityId = $this->assertDBNotNull('CRM_Activity_DAO_Activity', 'Scheduling Meeting', 'id',
166 'subject', 'Database check for created activity.'
167 );
168
169 $activityTargetId = $this->assertDBNotNull('CRM_Activity_DAO_ActivityContact', $targetContactId,
170 'id', 'contact_id',
171 'Database check for created activity target.'
172 );
173 $params = array(
174 'source_contact_id' => $contactId,
175 'source_record_id' => $contactId,
176 'subject' => 'Scheduling Meeting',
177 'activity_type_id' => 2,
178 );
179
180 CRM_Activity_BAO_Activity::deleteActivity($params);
181
182 $this->assertDBNull('CRM_Activity_DAO_Activity', 'Scheduling Meeting', 'id',
183 'subject', 'Database check for deleted activity.'
184 );
185 $this->contactDelete($contactId);
186 $this->contactDelete($targetContactId);
187 }
188
189 /**
190 * Test case for deleteActivityTarget() method.
191 *
192 * deleteActivityTarget($activityId) method deletes activity target for given activity id.
193 */
194 public function testDeleteActivityTarget() {
195 $contactId = $this->individualCreate();
196 $params = array(
197 'first_name' => 'liz',
198 'last_name' => 'hurleey',
199 );
200 $targetContactId = $this->individualCreate($params);
201
202 $params = array(
203 'source_contact_id' => $contactId,
204 'subject' => 'Scheduling Meeting',
205 'activity_type_id' => 2,
206 'target_contact_id' => array($targetContactId),
207 'activity_date_time' => date('Ymd'),
208 );
209
210 CRM_Activity_BAO_Activity::create($params);
211
212 $activityId = $this->assertDBNotNull('CRM_Activity_DAO_Activity', 'Scheduling Meeting', 'id',
213 'subject', 'Database check for created activity.'
214 );
215
216 $activityTargetId = $this->assertDBNotNull('CRM_Activity_DAO_ActivityContact', $targetContactId,
217 'id', 'contact_id',
218 'Database check for created activity target.'
219 );
220
221 CRM_Activity_BAO_Activity::deleteActivityContact($activityId, 3);
222
223 $this->assertDBNull('CRM_Activity_DAO_ActivityContact', $targetContactId, 'id',
224 'contact_id', 'Database check for deleted activity target.'
225 );
226
227 $this->contactDelete($contactId);
228 $this->contactDelete($targetContactId);
229 }
230
231 /**
232 * Test case for deleteActivityAssignment() method.
233 *
234 * deleteActivityAssignment($activityId) method deletes activity assignment for given activity id.
235 */
236 public function testDeleteActivityAssignment() {
237 $contactId = $this->individualCreate();
238 $params = array(
239 'first_name' => 'liz',
240 'last_name' => 'hurleey',
241 );
242 $assigneeContactId = $this->individualCreate($params);
243
244 $params = array(
245 'source_contact_id' => $contactId,
246 'subject' => 'Scheduling Meeting',
247 'activity_type_id' => 2,
248 'assignee_contact_id' => array($assigneeContactId),
249 'activity_date_time' => date('Ymd'),
250 );
251
252 CRM_Activity_BAO_Activity::create($params);
253
254 $activityId = $this->assertDBNotNull('CRM_Activity_DAO_Activity', 'Scheduling Meeting', 'id',
255 'subject', 'Database check for created activity.'
256 );
257
258 $activityAssignmentId = $this->assertDBNotNull('CRM_Activity_DAO_ActivityContact',
259 $assigneeContactId, 'id', 'contact_id',
260 'Database check for created activity assignment.'
261 );
262
263 CRM_Activity_BAO_Activity::deleteActivityContact($activityId, 1);
264
265 $this->assertDBNull('CRM_Activity_DAO_ActivityContact', $assigneeContactId, 'id',
266 'contact_id', 'Database check for deleted activity assignment.'
267 );
268
269 $this->contactDelete($contactId);
270 $this->contactDelete($assigneeContactId);
271 }
272
273 /**
274 * Test getActivitiesCount BAO method.
275 */
276 public function testGetActivitiesCountforAdminDashboard() {
277 $op = new PHPUnit_Extensions_Database_Operation_Insert();
278 $op->execute($this->_dbconn,
279 $this->createFlatXMLDataSet(
280 dirname(__FILE__) . '/activities_for_dashboard_count.xml'
281 )
282 );
283
284 $params = array(
285 'contact_id' => NULL,
286 'admin' => TRUE,
287 'caseId' => NULL,
288 'context' => 'home',
289 'activity_type_id' => NULL,
290 'offset' => 0,
291 'rowCount' => 0,
292 'sort' => NULL,
293 );
294 $activityCount = CRM_Activity_BAO_Activity::getActivitiesCount($params);
295
296 //since we are loading activities from dataset, we know total number of activities
297 // 8 schedule activities that should be shown on dashboard
298 $count = 8;
299 $this->assertEquals($count, $activityCount);
300 }
301
302 /**
303 * Test getActivitiesCount BAO method.
304 */
305 public function testGetActivitiesCountforNonAdminDashboard() {
306 $op = new PHPUnit_Extensions_Database_Operation_Insert();
307 $op->execute($this->_dbconn,
308 $this->createFlatXMLDataSet(
309 dirname(__FILE__) . '/activities_for_dashboard_count.xml'
310 )
311 );
312
313 $params = array(
314 'contact_id' => 9,
315 'admin' => FALSE,
316 'caseId' => NULL,
317 'context' => 'home',
318 'activity_type_id' => NULL,
319 'offset' => 0,
320 'rowCount' => 0,
321 'sort' => NULL,
322 );
323
324 $activityCount = CRM_Activity_BAO_Activity::getActivitiesCount($params);
325
326 //since we are loading activities from dataset, we know total number of activities for this contact
327 // 5 activities ( 2 scheduled, 3 Completed ), note that dashboard shows only scheduled activities
328 $count = 2;
329 $this->assertEquals($count, $activityCount);
330 }
331
332 /**
333 * Test getActivitiesCount BAO method.
334 */
335 public function testGetActivitiesCountforContactSummary() {
336 $op = new PHPUnit_Extensions_Database_Operation_Insert();
337 $op->execute($this->_dbconn,
338 $this->createFlatXMLDataSet(
339 dirname(__FILE__) . '/activities_for_dashboard_count.xml'
340 )
341 );
342
343 $params = array(
344 'contact_id' => 9,
345 'admin' => FALSE,
346 'caseId' => NULL,
347 'context' => 'activity',
348 'activity_type_id' => NULL,
349 'offset' => 0,
350 'rowCount' => 0,
351 'sort' => NULL,
352 );
353 $activityCount = CRM_Activity_BAO_Activity::getActivitiesCount($params);
354
355 //since we are loading activities from dataset, we know total number of activities for this contact
356 // 5 activities, Contact Summary should show all activities
357 $count = 5;
358 $this->assertEquals($count, $activityCount);
359 }
360
361 /**
362 * CRM-18706 - Test Include/Exclude Activity Filters
363 */
364 public function testActivityFilters() {
365 $op = new PHPUnit_Extensions_Database_Operation_Insert();
366 $op->execute($this->_dbconn,
367 $this->createFlatXMLDataSet(
368 dirname(__FILE__) . '/activities_for_dashboard_count.xml'
369 )
370 );
371
372 global $_GET;
373 $_GET = array(
374 'cid' => 9,
375 'context' => 'activity',
376 'activity_type_id' => 1,
377 'is_unit_test' => 1,
378 );
379 $obj = new CRM_Activity_Page_AJAX();
380
381 $activities = $obj->getContactActivity();
382 // This should include activities of type Meeting only.
383 foreach ($activities['data'] as $key => $value) {
384 $this->assertEquals('Meeting', $value['activity_type']);
385 }
386 unset($_GET['activity_type_id']);
387
388 $_GET['activity_type_exclude_id'] = 1;
389 $activities = $obj->getContactActivity();
390 // None of the activities should be of type Meeting.
391 foreach ($activities['data'] as $key => $value) {
392 $this->assertNotEquals('Meeting', $value['activity_type']);
393 }
394 }
395
396 /**
397 * Test getActivitiesCount BAO method.
398 */
399 public function testGetActivitiesCountforContactSummaryWithNoActivities() {
400 $op = new PHPUnit_Extensions_Database_Operation_Insert();
401 $op->execute($this->_dbconn,
402 $this->createFlatXMLDataSet(
403 dirname(__FILE__) . '/activities_for_dashboard_count.xml'
404 )
405 );
406
407 $params = array(
408 'contact_id' => 17,
409 'admin' => FALSE,
410 'caseId' => NULL,
411 'context' => 'home',
412 'activity_type_id' => NULL,
413 'offset' => 0,
414 'rowCount' => 0,
415 'sort' => NULL,
416 );
417 $activityCount = CRM_Activity_BAO_Activity::getActivitiesCount($params);
418
419 //since we are loading activities from dataset, we know total number of activities for this contact
420 // this contact does not have any activity
421 $this->assertEquals(0, $activityCount);
422 }
423
424 /**
425 * Test getActivities BAO method.
426 */
427 public function testGetActivitiesforAdminDashboard() {
428 $op = new PHPUnit_Extensions_Database_Operation_Insert();
429 $op->execute($this->_dbconn,
430 $this->createFlatXMLDataSet(
431 dirname(__FILE__) . '/activities_for_dashboard_count.xml'
432 )
433 );
434
435 $params = array(
436 'contact_id' => 5,
437 'admin' => TRUE,
438 'caseId' => NULL,
439 'context' => 'home',
440 'activity_type_id' => NULL,
441 'offset' => 0,
442 'rowCount' => 0,
443 'sort' => NULL,
444 );
445 $activities = CRM_Activity_BAO_Activity::getActivities($params);
446
447 //since we are loading activities from dataset, we know total number of activities
448 // 8 schedule activities that should be shown on dashboard
449 $count = 8;
450 $this->assertEquals($count, count($activities));
451
452 foreach ($activities as $key => $value) {
453 $this->assertEquals($value['subject'], "subject {$key}", 'Verify activity subject is correct.');
454 $this->assertEquals($value['activity_type_id'], 2, 'Verify activity type is correct.');
455 $this->assertEquals($value['status_id'], 1, 'Verify all activities are scheduled.');
456 }
457 }
458
459 /**
460 * Test getActivities BAO method.
461 */
462 public function testGetActivitiesforNonAdminDashboard() {
463 $op = new PHPUnit_Extensions_Database_Operation_Insert();
464 $op->execute($this->_dbconn,
465 $this->createFlatXMLDataSet(
466 dirname(__FILE__) . '/activities_for_dashboard_count.xml'
467 )
468 );
469
470 $contactID = 9;
471 $params = array(
472 'contact_id' => $contactID,
473 'admin' => FALSE,
474 'caseId' => NULL,
475 'context' => 'home',
476 'activity_type_id' => NULL,
477 'offset' => 0,
478 'rowCount' => 0,
479 'sort' => NULL,
480 );
481 $activities = CRM_Activity_BAO_Activity::getActivities($params);
482
483 //since we are loading activities from dataset, we know total number of activities for this contact
484 // 5 activities ( 2 scheduled, 3 Completed ), note that dashboard shows only scheduled activities
485 $count = 2;
486 $this->assertEquals($count, count($activities));
487
488 foreach ($activities as $key => $value) {
489 $this->assertEquals($value['subject'], "subject {$key}", 'Verify activity subject is correct.');
490 $this->assertEquals($value['activity_type_id'], 2, 'Verify activity type is correct.');
491 $this->assertEquals($value['status_id'], 1, 'Verify all activities are scheduled.');
492
493 if ($key == 3) {
494 $this->assertArrayHasKey($contactID, $value['target_contact_name']);
495 }
496 elseif ($key == 4) {
497 $this->assertArrayHasKey($contactID, $value['assignee_contact_name']);
498 }
499 }
500 }
501
502 /**
503 * Test target contact count.
504 */
505 public function testTargetCountforContactSummary() {
506 $targetCount = 5;
507 $contactId = $this->individualCreate();
508 for ($i = 0; $i < $targetCount; $i++) {
509 $targetContactIDs[] = $this->individualCreate(array(), $i);
510 }
511 // create activities with 5 target contacts
512 $activityParams = array(
513 'source_contact_id' => $contactId,
514 'target_contact_id' => $targetContactIDs,
515 );
516 $this->activityCreate($activityParams);
517
518 $params = array(
519 'contact_id' => $contactId,
520 'context' => 'activity',
521 );
522 $activities = CRM_Activity_BAO_Activity::getActivities($params);
523
524 //verify target count
525 $this->assertEquals($targetCount, $activities[1]['target_contact_counter']);
526 }
527
528 /**
529 * Test getActivities BAO method.
530 */
531 public function testGetActivitiesforContactSummary() {
532 $op = new PHPUnit_Extensions_Database_Operation_Insert();
533 $op->execute($this->_dbconn,
534 $this->createFlatXMLDataSet(
535 dirname(__FILE__) . '/activities_for_dashboard_count.xml'
536 )
537 );
538
539 $contactID = 9;
540 $params = array(
541 'contact_id' => $contactID,
542 'admin' => FALSE,
543 'caseId' => NULL,
544 'context' => 'activity',
545 'activity_type_id' => NULL,
546 'offset' => 0,
547 'rowCount' => 0,
548 'sort' => NULL,
549 );
550 $activities = CRM_Activity_BAO_Activity::getActivities($params);
551
552 //since we are loading activities from dataset, we know total number of activities for this contact
553 // 5 activities, Contact Summary should show all activities
554 $count = 5;
555 $this->assertEquals($count, count($activities));
556
557 foreach ($activities as $key => $value) {
558 $this->assertEquals($value['subject'], "subject {$key}", 'Verify activity subject is correct.');
559
560 if ($key > 8) {
561 $this->assertEquals($value['status_id'], 2, 'Verify all activities are scheduled.');
562 }
563 else {
564 $this->assertEquals($value['status_id'], 1, 'Verify all activities are scheduled.');
565 }
566
567 if ($key > 8) {
568 $this->assertEquals($value['activity_type_id'], 1, 'Verify activity type is correct.');
569 }
570 else {
571 $this->assertEquals($value['activity_type_id'], 2, 'Verify activity type is correct.');
572 }
573
574 if ($key == 3) {
575 $this->assertArrayHasKey($contactID, $value['target_contact_name']);
576 }
577 elseif ($key == 4) {
578 $this->assertArrayHasKey($contactID, $value['assignee_contact_name']);
579 }
580 }
581 }
582
583 /**
584 * Test getActivities BAO method.
585 */
586 public function testGetActivitiesforContactSummaryWithNoActivities() {
587 $op = new PHPUnit_Extensions_Database_Operation_Insert();
588 $op->execute($this->_dbconn,
589 $this->createFlatXMLDataSet(
590 dirname(__FILE__) . '/activities_for_dashboard_count.xml'
591 )
592 );
593
594 $params = array(
595 'contact_id' => 17,
596 'admin' => FALSE,
597 'caseId' => NULL,
598 'context' => 'home',
599 'activity_type_id' => NULL,
600 'offset' => 0,
601 'rowCount' => 0,
602 'sort' => NULL,
603 );
604 $activities = CRM_Activity_BAO_Activity::getActivities($params);
605
606 //since we are loading activities from dataset, we know total number of activities for this contact
607 // This contact does not have any activities
608 $this->assertEquals(0, count($activities));
609 }
610
611 }