Test preparatory cleanup
[civicrm-core.git] / tests / phpunit / CRM / Activity / BAO / ActivityTest.php
CommitLineData
6a488035 1<?php
0eea664b 2
aba1cd8b
EM
3/**
4 * Class CRM_Activity_BAO_ActivityTest
acb109b7 5 * @group headless
aba1cd8b 6 */
6a488035 7class CRM_Activity_BAO_ActivityTest extends CiviUnitTestCase {
00be9182 8 public function setUp() {
6a488035 9 parent::setUp();
cbcedb39 10 $this->prepareForACLs();
11 CRM_Core_Config::singleton()->userPermissionClass->permissions = array('view all contacts', 'access CiviCRM');
6a488035
TO
12 }
13
cbcedb39 14 /**
15 * Clean up after tests.
16 */
00be9182 17 public function tearDown() {
cbcedb39 18 $tablesToTruncate = array('civicrm_activity', 'civicrm_activity_contact');
6a488035 19 $this->quickCleanup($tablesToTruncate);
cbcedb39 20 $this->cleanUpAfterACLs();
21 parent::tearDown();
6a488035
TO
22 }
23
24 /**
fe482240 25 * Test case for create() method.
6a488035 26 */
00be9182 27 public function testCreate() {
f7969dcf 28 $contactId = $this->individualCreate();
6a488035
TO
29
30 $params = array(
31 'source_contact_id' => $contactId,
32 'subject' => 'Scheduling Meeting',
33 'activity_type_id' => 2,
34 );
35
36 CRM_Activity_BAO_Activity::create($params);
37
38 $activityId = $this->assertDBNotNull('CRM_Activity_DAO_Activity', 'Scheduling Meeting', 'id',
39 'subject', 'Database check for created activity.'
40 );
41
cbcedb39 42 // Now call create() to modify an existing Activity.
6a488035
TO
43 $params = array(
44 'id' => $activityId,
45 'source_contact_id' => $contactId,
46 'subject' => 'Scheduling Interview',
47 'activity_type_id' => 3,
48 );
49
50 CRM_Activity_BAO_Activity::create($params);
51
52 $activityTypeId = $this->assertDBNotNull('CRM_Activity_DAO_Activity', 'Scheduling Interview',
53 'activity_type_id',
54 'subject', 'Database check on updated activity record.'
55 );
56 $this->assertEquals($activityTypeId, 3, 'Verify activity type id is 3.');
57
93ac19cd 58 $this->contactDelete($contactId);
6a488035
TO
59 }
60
61 /**
fe482240
EM
62 * Test case for getContactActivity() method.
63 *
64 * getContactActivity() method get activities detail for given target contact id.
6a488035 65 */
00be9182 66 public function testGetContactActivity() {
f7969dcf 67 $contactId = $this->individualCreate();
6a488035
TO
68 $params = array(
69 'first_name' => 'liz',
70 'last_name' => 'hurleey',
71 );
f7969dcf 72 $targetContactId = $this->individualCreate($params);
6a488035
TO
73
74 $params = array(
75 'source_contact_id' => $contactId,
76 'subject' => 'Scheduling Meeting',
77 'activity_type_id' => 2,
78 'target_contact_id' => array($targetContactId),
79 'activity_date_time' => date('Ymd'),
80 );
81
f7969dcf 82 $this->callAPISuccess('Activity', 'create', $params);
6a488035
TO
83
84 $activityId = $this->assertDBNotNull('CRM_Activity_DAO_Activity', 'Scheduling Meeting',
85 'id',
86 'subject', 'Database check for created activity.'
87 );
88
a59cecb1 89 // @todo - remove this deprecated functions
6a488035
TO
90 $activities = CRM_Activity_BAO_Activity::getContactActivity($targetContactId);
91
92 $this->assertEquals($activities[$activityId]['subject'], 'Scheduling Meeting', 'Verify activity subject is correct.');
93
93ac19cd 94 $this->contactDelete($contactId);
95 $this->contactDelete($targetContactId);
6a488035
TO
96 }
97
98 /**
fe482240
EM
99 * Test case for retrieve() method.
100 *
101 * Retrieve($params, $defaults) method return activity detail for given params
6a488035
TO
102 * and set defaults.
103 */
00be9182 104 public function testRetrieve() {
f7969dcf 105 $contactId = $this->individualCreate();
6a488035
TO
106 $params = array(
107 'first_name' => 'liz',
108 'last_name' => 'hurleey',
109 );
f7969dcf 110 $targetContactId = $this->individualCreate($params);
6a488035
TO
111
112 $params = array(
113 'source_contact_id' => $contactId,
114 'subject' => 'Scheduling Meeting',
115 'activity_type_id' => 2,
116 'target_contact_id' => array($targetContactId),
117 'activity_date_time' => date('Ymd'),
118 );
119
120 CRM_Activity_BAO_Activity::create($params);
121
cbcedb39 122 $this->assertDBNotNull('CRM_Activity_DAO_Activity', 'Scheduling Meeting', 'id',
6a488035
TO
123 'subject', 'Database check for created activity.'
124 );
125
cbcedb39 126 $this->assertDBNotNull('CRM_Activity_DAO_ActivityContact', $targetContactId,
f1c7b1f0 127 'id', 'contact_id',
6a488035
TO
128 'Database check for created activity target.'
129 );
130
131 $defaults = array();
132 $activity = CRM_Activity_BAO_Activity::retrieve($params, $defaults);
133
134 $this->assertEquals($activity->subject, 'Scheduling Meeting', 'Verify activity subject is correct.');
6a488035 135 $this->assertEquals($activity->activity_type_id, 2, 'Verify activity type id is correct.');
b2e56051 136 $this->assertEquals($defaults['source_contact_id'], $contactId, 'Verify source contact id is correct.');
6a488035
TO
137
138 $this->assertEquals($defaults['subject'], 'Scheduling Meeting', 'Verify activity subject is correct.');
6a488035
TO
139 $this->assertEquals($defaults['activity_type_id'], 2, 'Verify activity type id is correct.');
140
141 $this->assertEquals($defaults['target_contact'][0], $targetContactId, 'Verify target contact id is correct.');
142
93ac19cd 143 $this->contactDelete($contactId);
144 $this->contactDelete($targetContactId);
6a488035
TO
145 }
146
147 /**
fe482240
EM
148 * Test case for deleteActivity() method.
149 *
6a488035
TO
150 * deleteActivity($params) method deletes activity for given params.
151 */
00be9182 152 public function testDeleteActivity() {
f7969dcf 153 $contactId = $this->individualCreate();
6a488035
TO
154 $params = array(
155 'first_name' => 'liz',
156 'last_name' => 'hurleey',
157 );
f7969dcf 158 $targetContactId = $this->individualCreate($params);
6a488035
TO
159
160 $params = array(
161 'source_contact_id' => $contactId,
162 'source_record_id' => $contactId,
163 'subject' => 'Scheduling Meeting',
164 'activity_type_id' => 2,
165 'target_contact_id' => array($targetContactId),
166 'activity_date_time' => date('Ymd'),
167 );
168
169 CRM_Activity_BAO_Activity::create($params);
170
cbcedb39 171 $this->assertDBNotNull('CRM_Activity_DAO_Activity', 'Scheduling Meeting', 'id',
6a488035
TO
172 'subject', 'Database check for created activity.'
173 );
174
cbcedb39 175 $this->assertDBNotNull('CRM_Activity_DAO_ActivityContact', $targetContactId,
f1c7b1f0 176 'id', 'contact_id',
6a488035
TO
177 'Database check for created activity target.'
178 );
179 $params = array(
180 'source_contact_id' => $contactId,
181 'source_record_id' => $contactId,
182 'subject' => 'Scheduling Meeting',
183 'activity_type_id' => 2,
184 );
185
93ac19cd 186 CRM_Activity_BAO_Activity::deleteActivity($params);
6a488035 187
93ac19cd 188 $this->assertDBNull('CRM_Activity_DAO_Activity', 'Scheduling Meeting', 'id',
6a488035
TO
189 'subject', 'Database check for deleted activity.'
190 );
93ac19cd 191 $this->contactDelete($contactId);
192 $this->contactDelete($targetContactId);
6a488035
TO
193 }
194
195 /**
fe482240
EM
196 * Test case for deleteActivityTarget() method.
197 *
6a488035
TO
198 * deleteActivityTarget($activityId) method deletes activity target for given activity id.
199 */
00be9182 200 public function testDeleteActivityTarget() {
f7969dcf 201 $contactId = $this->individualCreate();
6a488035
TO
202 $params = array(
203 'first_name' => 'liz',
204 'last_name' => 'hurleey',
205 );
f7969dcf 206 $targetContactId = $this->individualCreate($params);
6a488035
TO
207
208 $params = array(
209 'source_contact_id' => $contactId,
210 'subject' => 'Scheduling Meeting',
211 'activity_type_id' => 2,
212 'target_contact_id' => array($targetContactId),
213 'activity_date_time' => date('Ymd'),
214 );
215
216 CRM_Activity_BAO_Activity::create($params);
217
218 $activityId = $this->assertDBNotNull('CRM_Activity_DAO_Activity', 'Scheduling Meeting', 'id',
219 'subject', 'Database check for created activity.'
220 );
221
cbcedb39 222 $this->assertDBNotNull('CRM_Activity_DAO_ActivityContact', $targetContactId,
f1c7b1f0 223 'id', 'contact_id',
6a488035
TO
224 'Database check for created activity target.'
225 );
226
2517d079 227 CRM_Activity_BAO_Activity::deleteActivityContact($activityId, 3);
6a488035 228
f1c7b1f0
DL
229 $this->assertDBNull('CRM_Activity_DAO_ActivityContact', $targetContactId, 'id',
230 'contact_id', 'Database check for deleted activity target.'
6a488035
TO
231 );
232
93ac19cd 233 $this->contactDelete($contactId);
234 $this->contactDelete($targetContactId);
6a488035
TO
235 }
236
237 /**
fe482240
EM
238 * Test case for deleteActivityAssignment() method.
239 *
6a488035
TO
240 * deleteActivityAssignment($activityId) method deletes activity assignment for given activity id.
241 */
00be9182 242 public function testDeleteActivityAssignment() {
f7969dcf 243 $contactId = $this->individualCreate();
6a488035
TO
244 $params = array(
245 'first_name' => 'liz',
246 'last_name' => 'hurleey',
247 );
f7969dcf 248 $assigneeContactId = $this->individualCreate($params);
6a488035
TO
249
250 $params = array(
251 'source_contact_id' => $contactId,
252 'subject' => 'Scheduling Meeting',
253 'activity_type_id' => 2,
254 'assignee_contact_id' => array($assigneeContactId),
255 'activity_date_time' => date('Ymd'),
256 );
257
258 CRM_Activity_BAO_Activity::create($params);
259
260 $activityId = $this->assertDBNotNull('CRM_Activity_DAO_Activity', 'Scheduling Meeting', 'id',
261 'subject', 'Database check for created activity.'
262 );
263
cbcedb39 264 $this->assertDBNotNull('CRM_Activity_DAO_ActivityContact',
f1c7b1f0 265 $assigneeContactId, 'id', 'contact_id',
6a488035
TO
266 'Database check for created activity assignment.'
267 );
268
2517d079 269 CRM_Activity_BAO_Activity::deleteActivityContact($activityId, 1);
6a488035 270
f1c7b1f0
DL
271 $this->assertDBNull('CRM_Activity_DAO_ActivityContact', $assigneeContactId, 'id',
272 'contact_id', 'Database check for deleted activity assignment.'
6a488035
TO
273 );
274
93ac19cd 275 $this->contactDelete($contactId);
276 $this->contactDelete($assigneeContactId);
6a488035
TO
277 }
278
279 /**
5161bb0c 280 * Test getActivities BAO method for getting count.
6a488035 281 */
cbcedb39 282 public function testGetActivitiesCountForAdminDashboard() {
6a488035
TO
283 $op = new PHPUnit_Extensions_Database_Operation_Insert();
284 $op->execute($this->_dbconn,
bbfd46a5 285 $this->createFlatXMLDataSet(
6a488035
TO
286 dirname(__FILE__) . '/activities_for_dashboard_count.xml'
287 )
288 );
289
290 $params = array(
291 'contact_id' => NULL,
292 'admin' => TRUE,
293 'caseId' => NULL,
294 'context' => 'home',
295 'activity_type_id' => NULL,
296 'offset' => 0,
297 'rowCount' => 0,
298 'sort' => NULL,
299 );
5161bb0c 300 $activityCount = CRM_Activity_BAO_Activity::getActivities($params, TRUE);
6a488035
TO
301
302 //since we are loading activities from dataset, we know total number of activities
303 // 8 schedule activities that should be shown on dashboard
304 $count = 8;
a15773db 305 $this->assertEquals($count, $activityCount);
6a488035
TO
306 }
307
308 /**
5161bb0c 309 * Test getActivities BAO method for getting count
6a488035 310 */
00be9182 311 public function testGetActivitiesCountforNonAdminDashboard() {
6a488035
TO
312 $op = new PHPUnit_Extensions_Database_Operation_Insert();
313 $op->execute($this->_dbconn,
bbfd46a5 314 $this->createFlatXMLDataSet(
6a488035
TO
315 dirname(__FILE__) . '/activities_for_dashboard_count.xml'
316 )
317 );
318
319 $params = array(
320 'contact_id' => 9,
321 'admin' => FALSE,
322 'caseId' => NULL,
323 'context' => 'home',
324 'activity_type_id' => NULL,
325 'offset' => 0,
326 'rowCount' => 0,
327 'sort' => NULL,
328 );
329
5161bb0c 330 $activityCount = CRM_Activity_BAO_Activity::getActivities($params, TRUE);
6a488035
TO
331
332 //since we are loading activities from dataset, we know total number of activities for this contact
333 // 5 activities ( 2 scheduled, 3 Completed ), note that dashboard shows only scheduled activities
334 $count = 2;
a15773db 335 $this->assertEquals($count, $activityCount);
6a488035
TO
336 }
337
338 /**
5161bb0c 339 * Test getActivities BAO method for getting count
6a488035 340 */
00be9182 341 public function testGetActivitiesCountforContactSummary() {
6a488035
TO
342 $op = new PHPUnit_Extensions_Database_Operation_Insert();
343 $op->execute($this->_dbconn,
bbfd46a5 344 $this->createFlatXMLDataSet(
6a488035
TO
345 dirname(__FILE__) . '/activities_for_dashboard_count.xml'
346 )
347 );
348
349 $params = array(
350 'contact_id' => 9,
351 'admin' => FALSE,
352 'caseId' => NULL,
353 'context' => 'activity',
354 'activity_type_id' => NULL,
355 'offset' => 0,
356 'rowCount' => 0,
357 'sort' => NULL,
358 );
5161bb0c 359 $activityCount = CRM_Activity_BAO_Activity::getActivities($params, TRUE);
6a488035
TO
360
361 //since we are loading activities from dataset, we know total number of activities for this contact
362 // 5 activities, Contact Summary should show all activities
363 $count = 5;
a15773db 364 $this->assertEquals($count, $activityCount);
6a488035
TO
365 }
366
23289ddd 367 /**
368 * CRM-18706 - Test Include/Exclude Activity Filters
369 */
370 public function testActivityFilters() {
371 $op = new PHPUnit_Extensions_Database_Operation_Insert();
372 $op->execute($this->_dbconn,
373 $this->createFlatXMLDataSet(
374 dirname(__FILE__) . '/activities_for_dashboard_count.xml'
375 )
376 );
377
378 global $_GET;
379 $_GET = array(
380 'cid' => 9,
381 'context' => 'activity',
382 'activity_type_id' => 1,
383 'is_unit_test' => 1,
384 );
385 $obj = new CRM_Activity_Page_AJAX();
386
387 $activities = $obj->getContactActivity();
388 // This should include activities of type Meeting only.
cbcedb39 389 foreach ($activities['data'] as $value) {
8c99c0bb 390 $this->assertContains('Meeting', $value['activity_type']);
23289ddd 391 }
392 unset($_GET['activity_type_id']);
393
394 $_GET['activity_type_exclude_id'] = 1;
395 $activities = $obj->getContactActivity();
396 // None of the activities should be of type Meeting.
cbcedb39 397 foreach ($activities['data'] as $value) {
23289ddd 398 $this->assertNotEquals('Meeting', $value['activity_type']);
399 }
400 }
401
6a488035 402 /**
5161bb0c 403 * Test getActivities BAO method for getting count
6a488035 404 */
00be9182 405 public function testGetActivitiesCountforContactSummaryWithNoActivities() {
6a488035
TO
406 $op = new PHPUnit_Extensions_Database_Operation_Insert();
407 $op->execute($this->_dbconn,
bbfd46a5 408 $this->createFlatXMLDataSet(
6a488035
TO
409 dirname(__FILE__) . '/activities_for_dashboard_count.xml'
410 )
411 );
412
413 $params = array(
414 'contact_id' => 17,
415 'admin' => FALSE,
416 'caseId' => NULL,
417 'context' => 'home',
418 'activity_type_id' => NULL,
419 'offset' => 0,
420 'rowCount' => 0,
421 'sort' => NULL,
422 );
5161bb0c 423 $activityCount = CRM_Activity_BAO_Activity::getActivities($params, TRUE);
6a488035
TO
424
425 //since we are loading activities from dataset, we know total number of activities for this contact
426 // this contact does not have any activity
a15773db 427 $this->assertEquals(0, $activityCount);
6a488035
TO
428 }
429
430 /**
eceb18cc 431 * Test getActivities BAO method.
6a488035 432 */
cbcedb39 433 public function testGetActivitiesForAdminDashboard() {
6a488035
TO
434 $op = new PHPUnit_Extensions_Database_Operation_Insert();
435 $op->execute($this->_dbconn,
bbfd46a5 436 $this->createFlatXMLDataSet(
6a488035
TO
437 dirname(__FILE__) . '/activities_for_dashboard_count.xml'
438 )
439 );
440
441 $params = array(
5161bb0c 442 'contact_id' => NULL,
6a488035
TO
443 'admin' => TRUE,
444 'caseId' => NULL,
445 'context' => 'home',
446 'activity_type_id' => NULL,
447 'offset' => 0,
448 'rowCount' => 0,
449 'sort' => NULL,
450 );
451 $activities = CRM_Activity_BAO_Activity::getActivities($params);
452
453 //since we are loading activities from dataset, we know total number of activities
5161bb0c 454 // with no contact ID and there should be 8 schedule activities shown on dashboard
6a488035 455 $count = 8;
ba4a1892 456 $this->assertEquals($count, count($activities));
6a488035
TO
457
458 foreach ($activities as $key => $value) {
459 $this->assertEquals($value['subject'], "subject {$key}", 'Verify activity subject is correct.');
460 $this->assertEquals($value['activity_type_id'], 2, 'Verify activity type is correct.');
461 $this->assertEquals($value['status_id'], 1, 'Verify all activities are scheduled.');
462 }
463 }
464
465 /**
eceb18cc 466 * Test getActivities BAO method.
6a488035 467 */
00be9182 468 public function testGetActivitiesforNonAdminDashboard() {
6a488035
TO
469 $op = new PHPUnit_Extensions_Database_Operation_Insert();
470 $op->execute($this->_dbconn,
bbfd46a5 471 $this->createFlatXMLDataSet(
6a488035
TO
472 dirname(__FILE__) . '/activities_for_dashboard_count.xml'
473 )
474 );
475
476 $contactID = 9;
477 $params = array(
478 'contact_id' => $contactID,
479 'admin' => FALSE,
480 'caseId' => NULL,
481 'context' => 'home',
482 'activity_type_id' => NULL,
483 'offset' => 0,
484 'rowCount' => 0,
485 'sort' => NULL,
486 );
487 $activities = CRM_Activity_BAO_Activity::getActivities($params);
488
489 //since we are loading activities from dataset, we know total number of activities for this contact
490 // 5 activities ( 2 scheduled, 3 Completed ), note that dashboard shows only scheduled activities
491 $count = 2;
ba4a1892 492 $this->assertEquals($count, count($activities));
6a488035
TO
493
494 foreach ($activities as $key => $value) {
495 $this->assertEquals($value['subject'], "subject {$key}", 'Verify activity subject is correct.');
496 $this->assertEquals($value['activity_type_id'], 2, 'Verify activity type is correct.');
497 $this->assertEquals($value['status_id'], 1, 'Verify all activities are scheduled.');
498
499 if ($key == 3) {
500 $this->assertArrayHasKey($contactID, $value['target_contact_name']);
501 }
502 elseif ($key == 4) {
503 $this->assertArrayHasKey($contactID, $value['assignee_contact_name']);
504 }
505 }
506 }
507
36d2f4d5 508 /**
509 * Test target contact count.
510 */
511 public function testTargetCountforContactSummary() {
512 $targetCount = 5;
513 $contactId = $this->individualCreate();
cbcedb39 514 $targetContactIDs = array();
36d2f4d5 515 for ($i = 0; $i < $targetCount; $i++) {
516 $targetContactIDs[] = $this->individualCreate(array(), $i);
517 }
cbcedb39 518 // Create activities with 5 target contacts.
36d2f4d5 519 $activityParams = array(
520 'source_contact_id' => $contactId,
521 'target_contact_id' => $targetContactIDs,
522 );
523 $this->activityCreate($activityParams);
524
525 $params = array(
526 'contact_id' => $contactId,
527 'context' => 'activity',
528 );
529 $activities = CRM_Activity_BAO_Activity::getActivities($params);
530
531 //verify target count
532 $this->assertEquals($targetCount, $activities[1]['target_contact_counter']);
533 }
534
6a488035 535 /**
eceb18cc 536 * Test getActivities BAO method.
6a488035 537 */
00be9182 538 public function testGetActivitiesforContactSummary() {
6a488035
TO
539 $op = new PHPUnit_Extensions_Database_Operation_Insert();
540 $op->execute($this->_dbconn,
bbfd46a5 541 $this->createFlatXMLDataSet(
6a488035
TO
542 dirname(__FILE__) . '/activities_for_dashboard_count.xml'
543 )
544 );
545
546 $contactID = 9;
547 $params = array(
548 'contact_id' => $contactID,
549 'admin' => FALSE,
550 'caseId' => NULL,
551 'context' => 'activity',
552 'activity_type_id' => NULL,
553 'offset' => 0,
554 'rowCount' => 0,
555 'sort' => NULL,
556 );
557 $activities = CRM_Activity_BAO_Activity::getActivities($params);
558
559 //since we are loading activities from dataset, we know total number of activities for this contact
560 // 5 activities, Contact Summary should show all activities
561 $count = 5;
ba4a1892 562 $this->assertEquals($count, count($activities));
6a488035
TO
563
564 foreach ($activities as $key => $value) {
565 $this->assertEquals($value['subject'], "subject {$key}", 'Verify activity subject is correct.');
566
567 if ($key > 8) {
568 $this->assertEquals($value['status_id'], 2, 'Verify all activities are scheduled.');
569 }
570 else {
571 $this->assertEquals($value['status_id'], 1, 'Verify all activities are scheduled.');
572 }
573
574 if ($key > 8) {
575 $this->assertEquals($value['activity_type_id'], 1, 'Verify activity type is correct.');
576 }
577 else {
578 $this->assertEquals($value['activity_type_id'], 2, 'Verify activity type is correct.');
579 }
580
581 if ($key == 3) {
582 $this->assertArrayHasKey($contactID, $value['target_contact_name']);
583 }
584 elseif ($key == 4) {
585 $this->assertArrayHasKey($contactID, $value['assignee_contact_name']);
586 }
587 }
588 }
589
590 /**
eceb18cc 591 * Test getActivities BAO method.
6a488035 592 */
5161bb0c 593 public function testGetActivitiesforContactSummaryWithActivities() {
6a488035
TO
594 $op = new PHPUnit_Extensions_Database_Operation_Insert();
595 $op->execute($this->_dbconn,
bbfd46a5 596 $this->createFlatXMLDataSet(
6a488035
TO
597 dirname(__FILE__) . '/activities_for_dashboard_count.xml'
598 )
599 );
600
5161bb0c 601 // parameters for different test casess, check each array key for the specific test-case
602 $testCases = array(
603 'with-no-activity' => array(
604 'params' => array(
605 'contact_id' => 17,
606 'admin' => FALSE,
607 'caseId' => NULL,
608 'context' => 'home',
609 'activity_type_id' => NULL,
610 'offset' => 0,
611 'rowCount' => 0,
612 'sort' => NULL,
613 ),
614 ),
615 'with-activity' => array(
616 'params' => array(
617 'contact_id' => 1,
618 'admin' => FALSE,
619 'caseId' => NULL,
620 'context' => 'home',
621 'activity_type_id' => NULL,
622 'offset' => 0,
623 'rowCount' => 0,
624 'sort' => NULL,
625 ),
626 ),
627 'with-activity_type' => array(
628 'params' => array(
629 'contact_id' => 3,
630 'admin' => FALSE,
631 'caseId' => NULL,
632 'context' => 'home',
633 'activity_type_id' => 2,
634 'offset' => 0,
635 'rowCount' => 0,
636 'sort' => NULL,
637 ),
638 ),
639 'exclude-all-activity_type' => array(
640 'params' => array(
641 'contact_id' => 3,
642 'admin' => FALSE,
643 'caseId' => NULL,
644 'context' => 'home',
645 'activity_type_exclude_id' => array(1, 2),
646 'offset' => 0,
647 'rowCount' => 0,
648 'sort' => NULL,
649 ),
650 ),
651 'sort-by-subject' => array(
652 'params' => array(
653 'contact_id' => 1,
654 'admin' => FALSE,
655 'caseId' => NULL,
656 'context' => 'home',
657 'activity_type_id' => NULL,
658 'offset' => 0,
659 'rowCount' => 0,
660 'sort' => 'subject DESC',
661 ),
662 ),
663 );
664
665 foreach ($testCases as $caseName => $testCase) {
666 $activities = CRM_Activity_BAO_Activity::getActivities($testCase['params']);
667 $activityCount = CRM_Activity_BAO_Activity::getActivities($testCase['params'], TRUE);
668 if ($caseName == 'with-no-activity') {
669 $this->assertEquals(0, count($activities));
670 $this->assertEquals(0, $activityCount);
671 }
672 elseif ($caseName == 'with-activity') {
673 // contact id 1 is assigned as source, target and assignee for activity id 1, 7 and 8 respectively
674 $this->assertEquals(3, count($activities));
675 $this->assertEquals(3, $activityCount);
676 $this->assertEquals(1, $activities[1]['source_contact_id']);
677 $this->assertEquals(TRUE, array_key_exists(1, $activities[7]['target_contact_name']));
678 $this->assertEquals(TRUE, array_key_exists(1, $activities[8]['assignee_contact_name']));
679 }
680 elseif ($caseName == 'with-activity_type') {
681 // contact id 3 for activity type 2 is assigned as assignee, source and target for
682 // activity id 1, 3 and 8 respectively
683 $this->assertEquals(3, count($activities));
684 $this->assertEquals(3, $activityCount);
685 // ensure activity type id is 2
686 $this->assertEquals(2, $activities[1]['activity_type_id']);
687 $this->assertEquals(3, $activities[3]['source_contact_id']);
688 $this->assertEquals(TRUE, array_key_exists(3, $activities[8]['target_contact_name']));
689 $this->assertEquals(TRUE, array_key_exists(3, $activities[1]['assignee_contact_name']));
690 }
691 if ($caseName == 'exclude-all-activity_type') {
692 $this->assertEquals(0, count($activities));
693 $this->assertEquals(0, $activityCount);
694 }
695 if ($caseName == 'sort-by-subject') {
696 $this->assertEquals(3, count($activities));
697 $this->assertEquals(3, $activityCount);
698 // activities should be order by 'subject DESC'
699 $subjectOrder = array(
700 'subject 8',
701 'subject 7',
702 'subject 1',
703 );
704 $count = 0;
705 foreach ($activities as $activity) {
706 $this->assertEquals($subjectOrder[$count], $activity['subject']);
707 $count++;
708 }
709 }
710 }
6a488035 711 }
96025800 712
3cf1fae9 713 /**
714 * CRM-20308: Test from email address when a 'copy of Activity' event occur
715 */
716 public function testEmailAddressOfActivityCopy() {
717 // Case 1: assert the 'From' Email Address of source Actvity Contact ID
718 // create activity with source contact ID which has email address
719 $assigneeContactId = $this->individualCreate();
720 $sourceContactParams = array(
721 'first_name' => 'liz',
722 'last_name' => 'hurleey',
723 'email' => substr(sha1(rand()), 0, 7) . '@testemail.com',
724 );
725 $sourceContactID = $this->individualCreate($sourceContactParams);
726 $sourceDisplayName = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $sourceContactID, 'display_name');
727
728 // create an activity using API
729 $params = array(
730 'source_contact_id' => $sourceContactID,
731 'subject' => 'Scheduling Meeting ' . substr(sha1(rand()), 0, 4),
732 'activity_type_id' => CRM_Core_PseudoConstant::getKey('CRM_Activity_BAO_Activity', 'activity_type_id', 'Meeting'),
733 'assignee_contact_id' => array($assigneeContactId),
734 'activity_date_time' => date('Ymd'),
735 );
736 $activity = $this->callAPISuccess('Activity', 'create', $params);
737
738 // Check that from address is in "Source-Display-Name <source-email>"
739 $formAddress = CRM_Case_BAO_Case::getReceiptFrom($activity['id']);
740 $expectedFromAddress = sprintf("%s <%s>", $sourceDisplayName, $sourceContactParams['email']);
741 $this->assertEquals($expectedFromAddress, $formAddress);
3cf1fae9 742
743 // Case 2: System Default From Address
744 // but first erase the email address of existing source contact ID
745 $withoutEmailParams = array(
746 'email' => '',
747 );
748 $sourceContactID = $this->individualCreate($withoutEmailParams);
749 $params = array(
750 'source_contact_id' => $sourceContactID,
751 'subject' => 'Scheduling Meeting ' . substr(sha1(rand()), 0, 4),
752 'activity_type_id' => CRM_Core_PseudoConstant::getKey('CRM_Activity_BAO_Activity', 'activity_type_id', 'Meeting'),
753 'activity_date_time' => date('Ymd'),
754 );
755 $activity = $this->callAPISuccess('Activity', 'create', $params);
756 // fetch domain info
757 $domainInfo = $this->callAPISuccess('Domain', 'getsingle', array('id' => CRM_Core_Config::domainID()));
758
759 $formAddress = CRM_Case_BAO_Case::getReceiptFrom($activity['id']);
760 if (!empty($domainInfo['from_email'])) {
761 $expectedFromAddress = sprintf("%s <%s>", $domainInfo['from_name'], $domainInfo['from_email']);
762 }
763 // Case 3: fetch default Organization Contact email address
764 elseif (!empty($domainInfo['domain_email'])) {
765 $expectedFromAddress = sprintf("%s <%s>", $domainInfo['name'], $domainInfo['domain_email']);
766 }
767 // TODO: due to unknown reason the following assertion fails on
768 // test.civicrm.org test build but works fine on local
769 // $this->assertEquals($expectedFromAddress, $formAddress);
3cf1fae9 770
771 // TODO: Case 4 about checking the $formAddress on basis of logged contact ID respectively needs,
772 // to change the domain setting, which isn't straight forward in test environment
773 }
774
6a488035 775}