Merge pull request #8835 from eileenmcnaughton/tax_amount
[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
TO
9 parent::setUp();
10 }
11
00be9182 12 public function tearDown() {
6a488035 13 // truncate a few tables
b319d00a 14 $tablesToTruncate = array('civicrm_contact', 'civicrm_activity', 'civicrm_activity_contact');
6a488035
TO
15 $this->quickCleanup($tablesToTruncate);
16 }
17
18 /**
fe482240 19 * Test case for create() method.
6a488035 20 */
00be9182 21 public function testCreate() {
f7969dcf 22 $contactId = $this->individualCreate();
6a488035
TO
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
6a488035
TO
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
93ac19cd 53 $this->contactDelete($contactId);
6a488035
TO
54 }
55
56 /**
fe482240
EM
57 * Test case for getContactActivity() method.
58 *
59 * getContactActivity() method get activities detail for given target contact id.
6a488035 60 */
00be9182 61 public function testGetContactActivity() {
f7969dcf 62 $contactId = $this->individualCreate();
6a488035
TO
63 $params = array(
64 'first_name' => 'liz',
65 'last_name' => 'hurleey',
66 );
f7969dcf 67 $targetContactId = $this->individualCreate($params);
6a488035
TO
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
f7969dcf 77 $this->callAPISuccess('Activity', 'create', $params);
6a488035
TO
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
93ac19cd 88 $this->contactDelete($contactId);
89 $this->contactDelete($targetContactId);
6a488035
TO
90 }
91
92 /**
fe482240
EM
93 * Test case for retrieve() method.
94 *
95 * Retrieve($params, $defaults) method return activity detail for given params
6a488035
TO
96 * and set defaults.
97 */
00be9182 98 public function testRetrieve() {
f7969dcf 99 $contactId = $this->individualCreate();
6a488035
TO
100 $params = array(
101 'first_name' => 'liz',
102 'last_name' => 'hurleey',
103 );
f7969dcf 104 $targetContactId = $this->individualCreate($params);
6a488035
TO
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
f1c7b1f0
DL
120 $activityTargetId = $this->assertDBNotNull('CRM_Activity_DAO_ActivityContact', $targetContactId,
121 'id', 'contact_id',
6a488035
TO
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.');
6a488035 129 $this->assertEquals($activity->activity_type_id, 2, 'Verify activity type id is correct.');
b2e56051 130 $this->assertEquals($defaults['source_contact_id'], $contactId, 'Verify source contact id is correct.');
6a488035
TO
131
132 $this->assertEquals($defaults['subject'], 'Scheduling Meeting', 'Verify activity subject is correct.');
6a488035
TO
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
93ac19cd 137 $this->contactDelete($contactId);
138 $this->contactDelete($targetContactId);
6a488035
TO
139 }
140
141 /**
fe482240
EM
142 * Test case for deleteActivity() method.
143 *
6a488035
TO
144 * deleteActivity($params) method deletes activity for given params.
145 */
00be9182 146 public function testDeleteActivity() {
f7969dcf 147 $contactId = $this->individualCreate();
6a488035
TO
148 $params = array(
149 'first_name' => 'liz',
150 'last_name' => 'hurleey',
151 );
f7969dcf 152 $targetContactId = $this->individualCreate($params);
6a488035
TO
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
f1c7b1f0
DL
169 $activityTargetId = $this->assertDBNotNull('CRM_Activity_DAO_ActivityContact', $targetContactId,
170 'id', 'contact_id',
6a488035
TO
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
93ac19cd 180 CRM_Activity_BAO_Activity::deleteActivity($params);
6a488035 181
93ac19cd 182 $this->assertDBNull('CRM_Activity_DAO_Activity', 'Scheduling Meeting', 'id',
6a488035
TO
183 'subject', 'Database check for deleted activity.'
184 );
93ac19cd 185 $this->contactDelete($contactId);
186 $this->contactDelete($targetContactId);
6a488035
TO
187 }
188
189 /**
fe482240
EM
190 * Test case for deleteActivityTarget() method.
191 *
6a488035
TO
192 * deleteActivityTarget($activityId) method deletes activity target for given activity id.
193 */
00be9182 194 public function testDeleteActivityTarget() {
f7969dcf 195 $contactId = $this->individualCreate();
6a488035
TO
196 $params = array(
197 'first_name' => 'liz',
198 'last_name' => 'hurleey',
199 );
f7969dcf 200 $targetContactId = $this->individualCreate($params);
6a488035
TO
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
f1c7b1f0
DL
216 $activityTargetId = $this->assertDBNotNull('CRM_Activity_DAO_ActivityContact', $targetContactId,
217 'id', 'contact_id',
6a488035
TO
218 'Database check for created activity target.'
219 );
220
2517d079 221 CRM_Activity_BAO_Activity::deleteActivityContact($activityId, 3);
6a488035 222
f1c7b1f0
DL
223 $this->assertDBNull('CRM_Activity_DAO_ActivityContact', $targetContactId, 'id',
224 'contact_id', 'Database check for deleted activity target.'
6a488035
TO
225 );
226
93ac19cd 227 $this->contactDelete($contactId);
228 $this->contactDelete($targetContactId);
6a488035
TO
229 }
230
231 /**
fe482240
EM
232 * Test case for deleteActivityAssignment() method.
233 *
6a488035
TO
234 * deleteActivityAssignment($activityId) method deletes activity assignment for given activity id.
235 */
00be9182 236 public function testDeleteActivityAssignment() {
f7969dcf 237 $contactId = $this->individualCreate();
6a488035
TO
238 $params = array(
239 'first_name' => 'liz',
240 'last_name' => 'hurleey',
241 );
f7969dcf 242 $assigneeContactId = $this->individualCreate($params);
6a488035
TO
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
f1c7b1f0
DL
258 $activityAssignmentId = $this->assertDBNotNull('CRM_Activity_DAO_ActivityContact',
259 $assigneeContactId, 'id', 'contact_id',
6a488035
TO
260 'Database check for created activity assignment.'
261 );
262
2517d079 263 CRM_Activity_BAO_Activity::deleteActivityContact($activityId, 1);
6a488035 264
f1c7b1f0
DL
265 $this->assertDBNull('CRM_Activity_DAO_ActivityContact', $assigneeContactId, 'id',
266 'contact_id', 'Database check for deleted activity assignment.'
6a488035
TO
267 );
268
93ac19cd 269 $this->contactDelete($contactId);
270 $this->contactDelete($assigneeContactId);
6a488035
TO
271 }
272
273 /**
eceb18cc 274 * Test getActivitiesCount BAO method.
6a488035 275 */
00be9182 276 public function testGetActivitiesCountforAdminDashboard() {
6a488035
TO
277 $op = new PHPUnit_Extensions_Database_Operation_Insert();
278 $op->execute($this->_dbconn,
bbfd46a5 279 $this->createFlatXMLDataSet(
6a488035
TO
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;
a15773db 299 $this->assertEquals($count, $activityCount);
6a488035
TO
300 }
301
302 /**
eceb18cc 303 * Test getActivitiesCount BAO method.
6a488035 304 */
00be9182 305 public function testGetActivitiesCountforNonAdminDashboard() {
6a488035
TO
306 $op = new PHPUnit_Extensions_Database_Operation_Insert();
307 $op->execute($this->_dbconn,
bbfd46a5 308 $this->createFlatXMLDataSet(
6a488035
TO
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;
a15773db 329 $this->assertEquals($count, $activityCount);
6a488035
TO
330 }
331
332 /**
eceb18cc 333 * Test getActivitiesCount BAO method.
6a488035 334 */
00be9182 335 public function testGetActivitiesCountforContactSummary() {
6a488035
TO
336 $op = new PHPUnit_Extensions_Database_Operation_Insert();
337 $op->execute($this->_dbconn,
bbfd46a5 338 $this->createFlatXMLDataSet(
6a488035
TO
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;
a15773db 358 $this->assertEquals($count, $activityCount);
6a488035
TO
359 }
360
23289ddd 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
6a488035 396 /**
eceb18cc 397 * Test getActivitiesCount BAO method.
6a488035 398 */
00be9182 399 public function testGetActivitiesCountforContactSummaryWithNoActivities() {
6a488035
TO
400 $op = new PHPUnit_Extensions_Database_Operation_Insert();
401 $op->execute($this->_dbconn,
bbfd46a5 402 $this->createFlatXMLDataSet(
6a488035
TO
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
a15773db 421 $this->assertEquals(0, $activityCount);
6a488035
TO
422 }
423
424 /**
eceb18cc 425 * Test getActivities BAO method.
6a488035 426 */
00be9182 427 public function testGetActivitiesforAdminDashboard() {
6a488035
TO
428 $op = new PHPUnit_Extensions_Database_Operation_Insert();
429 $op->execute($this->_dbconn,
bbfd46a5 430 $this->createFlatXMLDataSet(
6a488035
TO
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;
ba4a1892 450 $this->assertEquals($count, count($activities));
6a488035
TO
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 /**
eceb18cc 460 * Test getActivities BAO method.
6a488035 461 */
00be9182 462 public function testGetActivitiesforNonAdminDashboard() {
6a488035
TO
463 $op = new PHPUnit_Extensions_Database_Operation_Insert();
464 $op->execute($this->_dbconn,
bbfd46a5 465 $this->createFlatXMLDataSet(
6a488035
TO
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;
ba4a1892 486 $this->assertEquals($count, count($activities));
6a488035
TO
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 /**
eceb18cc 503 * Test getActivities BAO method.
6a488035 504 */
00be9182 505 public function testGetActivitiesforContactSummary() {
6a488035
TO
506 $op = new PHPUnit_Extensions_Database_Operation_Insert();
507 $op->execute($this->_dbconn,
bbfd46a5 508 $this->createFlatXMLDataSet(
6a488035
TO
509 dirname(__FILE__) . '/activities_for_dashboard_count.xml'
510 )
511 );
512
513 $contactID = 9;
514 $params = array(
515 'contact_id' => $contactID,
516 'admin' => FALSE,
517 'caseId' => NULL,
518 'context' => 'activity',
519 'activity_type_id' => NULL,
520 'offset' => 0,
521 'rowCount' => 0,
522 'sort' => NULL,
523 );
524 $activities = CRM_Activity_BAO_Activity::getActivities($params);
525
526 //since we are loading activities from dataset, we know total number of activities for this contact
527 // 5 activities, Contact Summary should show all activities
528 $count = 5;
ba4a1892 529 $this->assertEquals($count, count($activities));
6a488035
TO
530
531 foreach ($activities as $key => $value) {
532 $this->assertEquals($value['subject'], "subject {$key}", 'Verify activity subject is correct.');
533
534 if ($key > 8) {
535 $this->assertEquals($value['status_id'], 2, 'Verify all activities are scheduled.');
536 }
537 else {
538 $this->assertEquals($value['status_id'], 1, 'Verify all activities are scheduled.');
539 }
540
541 if ($key > 8) {
542 $this->assertEquals($value['activity_type_id'], 1, 'Verify activity type is correct.');
543 }
544 else {
545 $this->assertEquals($value['activity_type_id'], 2, 'Verify activity type is correct.');
546 }
547
548 if ($key == 3) {
549 $this->assertArrayHasKey($contactID, $value['target_contact_name']);
550 }
551 elseif ($key == 4) {
552 $this->assertArrayHasKey($contactID, $value['assignee_contact_name']);
553 }
554 }
555 }
556
557 /**
eceb18cc 558 * Test getActivities BAO method.
6a488035 559 */
00be9182 560 public function testGetActivitiesforContactSummaryWithNoActivities() {
6a488035
TO
561 $op = new PHPUnit_Extensions_Database_Operation_Insert();
562 $op->execute($this->_dbconn,
bbfd46a5 563 $this->createFlatXMLDataSet(
6a488035
TO
564 dirname(__FILE__) . '/activities_for_dashboard_count.xml'
565 )
566 );
567
568 $params = array(
569 'contact_id' => 17,
570 'admin' => FALSE,
571 'caseId' => NULL,
572 'context' => 'home',
573 'activity_type_id' => NULL,
574 'offset' => 0,
575 'rowCount' => 0,
576 'sort' => NULL,
577 );
578 $activities = CRM_Activity_BAO_Activity::getActivities($params);
579
580 //since we are loading activities from dataset, we know total number of activities for this contact
581 // This contact does not have any activities
ba4a1892 582 $this->assertEquals(0, count($activities));
6a488035 583 }
96025800 584
6a488035 585}