Merge pull request #4780 from williamtheaker/patch-1
[civicrm-core.git] / tests / phpunit / CRM / Activity / BAO / ActivityTest.php
1 <?php
2 require_once 'CiviTest/CiviUnitTestCase.php';
3 require_once 'CiviTest/Contact.php';
4
5 /**
6 * Class CRM_Activity_BAO_ActivityTest
7 */
8 class CRM_Activity_BAO_ActivityTest extends CiviUnitTestCase {
9 function setUp() {
10 parent::setUp();
11 }
12
13 function tearDown() {
14 // truncate a few tables
15 $tablesToTruncate = array('civicrm_contact', 'civicrm_activity', 'civicrm_activity_contact');
16 $this->quickCleanup($tablesToTruncate);
17 }
18
19 /**
20 * Testcases for create() method
21 * create() method Add/Edit activity.
22 */
23 function testCreate() {
24 $contactId = Contact::createIndividual();
25
26 $params = array(
27 'source_contact_id' => $contactId,
28 'subject' => 'Scheduling Meeting',
29 'activity_type_id' => 2,
30 );
31
32 CRM_Activity_BAO_Activity::create($params);
33
34 $activityId = $this->assertDBNotNull('CRM_Activity_DAO_Activity', 'Scheduling Meeting', 'id',
35 'subject', 'Database check for created activity.'
36 );
37
38 // Now call create() to modify an existing Activity
39
40 $params = array();
41 $params = array(
42 'id' => $activityId,
43 'source_contact_id' => $contactId,
44 'subject' => 'Scheduling Interview',
45 'activity_type_id' => 3,
46 );
47
48 CRM_Activity_BAO_Activity::create($params);
49
50 $activityTypeId = $this->assertDBNotNull('CRM_Activity_DAO_Activity', 'Scheduling Interview',
51 'activity_type_id',
52 'subject', 'Database check on updated activity record.'
53 );
54 $this->assertEquals($activityTypeId, 3, 'Verify activity type id is 3.');
55
56 Contact::delete($contactId);
57 }
58
59 /**
60 * Testcase for getContactActivity() method.
61 * getContactActivity() method get activities detail for given target contact id
62 */
63 function testGetContactActivity() {
64 $contactId = Contact::createIndividual();
65 $params = array(
66 'first_name' => 'liz',
67 'last_name' => 'hurleey',
68 );
69 $targetContactId = Contact::createIndividual($params);
70
71 $params = array(
72 'source_contact_id' => $contactId,
73 'subject' => 'Scheduling Meeting',
74 'activity_type_id' => 2,
75 'target_contact_id' => array($targetContactId),
76 'activity_date_time' => date('Ymd'),
77 );
78
79 CRM_Activity_BAO_Activity::create($params);
80
81 $activityId = $this->assertDBNotNull('CRM_Activity_DAO_Activity', 'Scheduling Meeting',
82 'id',
83 'subject', 'Database check for created activity.'
84 );
85
86 $activities = CRM_Activity_BAO_Activity::getContactActivity($targetContactId);
87
88 $this->assertEquals($activities[$activityId]['subject'], 'Scheduling Meeting', 'Verify activity subject is correct.');
89
90 Contact::delete($contactId);
91 Contact::delete($targetContactId);
92 }
93
94 /**
95 * Testcase for retrieve() method.
96 * retrieve($params, $defaults) method return activity detail for given params
97 * and set defaults.
98 */
99 function testRetrieve() {
100 $contactId = Contact::createIndividual();
101 $params = array(
102 'first_name' => 'liz',
103 'last_name' => 'hurleey',
104 );
105 $targetContactId = Contact::createIndividual($params);
106
107 $params = array(
108 'source_contact_id' => $contactId,
109 'subject' => 'Scheduling Meeting',
110 'activity_type_id' => 2,
111 'target_contact_id' => array($targetContactId),
112 'activity_date_time' => date('Ymd'),
113 );
114
115 CRM_Activity_BAO_Activity::create($params);
116
117 $activityId = $this->assertDBNotNull('CRM_Activity_DAO_Activity', 'Scheduling Meeting', 'id',
118 'subject', 'Database check for created activity.'
119 );
120
121 $activityTargetId = $this->assertDBNotNull('CRM_Activity_DAO_ActivityContact', $targetContactId,
122 'id', 'contact_id',
123 'Database check for created activity target.'
124 );
125
126 $defaults = array();
127 $activity = CRM_Activity_BAO_Activity::retrieve($params, $defaults);
128
129 $this->assertEquals($activity->subject, 'Scheduling Meeting', 'Verify activity subject is correct.');
130 $this->assertEquals($activity->activity_type_id, 2, 'Verify activity type id is correct.');
131 $this->assertEquals($defaults['source_contact_id'], $contactId, 'Verify source contact id is correct.');
132
133 $this->assertEquals($defaults['subject'], 'Scheduling Meeting', 'Verify activity subject is correct.');
134 $this->assertEquals($defaults['activity_type_id'], 2, 'Verify activity type id is correct.');
135
136 $this->assertEquals($defaults['target_contact'][0], $targetContactId, 'Verify target contact id is correct.');
137
138 Contact::delete($contactId);
139 Contact::delete($targetContactId);
140 }
141
142 /**
143 * Testcase for deleteActivity() method.
144 * deleteActivity($params) method deletes activity for given params.
145 */
146 function testDeleteActivity() {
147 $contactId = Contact::createIndividual();
148 $params = array(
149 'first_name' => 'liz',
150 'last_name' => 'hurleey',
151 );
152 $targetContactId = Contact::createIndividual($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 $result = CRM_Activity_BAO_Activity::deleteActivity($params);
181
182 $activityId = $this->assertDBNull('CRM_Activity_DAO_Activity', 'Scheduling Meeting', 'id',
183 'subject', 'Database check for deleted activity.'
184 );
185 Contact::delete($contactId);
186 Contact::delete($targetContactId);
187 }
188
189 /**
190 * Testcase for deleteActivityTarget() method.
191 * deleteActivityTarget($activityId) method deletes activity target for given activity id.
192 */
193 function testDeleteActivityTarget() {
194 $contactId = Contact::createIndividual();
195 $params = array(
196 'first_name' => 'liz',
197 'last_name' => 'hurleey',
198 );
199 $targetContactId = Contact::createIndividual($params);
200
201 $params = array(
202 'source_contact_id' => $contactId,
203 'subject' => 'Scheduling Meeting',
204 'activity_type_id' => 2,
205 'target_contact_id' => array($targetContactId),
206 'activity_date_time' => date('Ymd'),
207 );
208
209 CRM_Activity_BAO_Activity::create($params);
210
211 $activityId = $this->assertDBNotNull('CRM_Activity_DAO_Activity', 'Scheduling Meeting', 'id',
212 'subject', 'Database check for created activity.'
213 );
214
215 $activityTargetId = $this->assertDBNotNull('CRM_Activity_DAO_ActivityContact', $targetContactId,
216 'id', 'contact_id',
217 'Database check for created activity target.'
218 );
219
220 CRM_Activity_BAO_Activity::deleteActivityContact($activityId, 3);
221
222 $this->assertDBNull('CRM_Activity_DAO_ActivityContact', $targetContactId, 'id',
223 'contact_id', 'Database check for deleted activity target.'
224 );
225
226 Contact::delete($contactId);
227 Contact::delete($targetContactId);
228 }
229
230 /**
231 * Testcase for deleteActivityAssignment() method.
232 * deleteActivityAssignment($activityId) method deletes activity assignment for given activity id.
233 */
234 function testDeleteActivityAssignment() {
235 $contactId = Contact::createIndividual();
236 $params = array(
237 'first_name' => 'liz',
238 'last_name' => 'hurleey',
239 );
240 $assigneeContactId = Contact::createIndividual($params);
241
242 $params = array(
243 'source_contact_id' => $contactId,
244 'subject' => 'Scheduling Meeting',
245 'activity_type_id' => 2,
246 'assignee_contact_id' => array($assigneeContactId),
247 'activity_date_time' => date('Ymd'),
248 );
249
250 CRM_Activity_BAO_Activity::create($params);
251
252 $activityId = $this->assertDBNotNull('CRM_Activity_DAO_Activity', 'Scheduling Meeting', 'id',
253 'subject', 'Database check for created activity.'
254 );
255
256 $activityAssignmentId = $this->assertDBNotNull('CRM_Activity_DAO_ActivityContact',
257 $assigneeContactId, 'id', 'contact_id',
258 'Database check for created activity assignment.'
259 );
260
261 CRM_Activity_BAO_Activity::deleteActivityContact($activityId, 1);
262
263 $this->assertDBNull('CRM_Activity_DAO_ActivityContact', $assigneeContactId, 'id',
264 'contact_id', 'Database check for deleted activity assignment.'
265 );
266
267 Contact::delete($contactId);
268 Contact::delete($assigneeContactId);
269 }
270
271 /**
272 * Test getActivitiesCount BAO method
273 */
274 function testGetActivitiesCountforAdminDashboard() {
275 $op = new PHPUnit_Extensions_Database_Operation_Insert();
276 $op->execute($this->_dbconn,
277 $this->createFlatXMLDataSet(
278 dirname(__FILE__) . '/activities_for_dashboard_count.xml'
279 )
280 );
281
282 $params = array(
283 'contact_id' => NULL,
284 'admin' => TRUE,
285 'caseId' => NULL,
286 'context' => 'home',
287 'activity_type_id' => NULL,
288 'offset' => 0,
289 'rowCount' => 0,
290 'sort' => NULL,
291 );
292 $activityCount = CRM_Activity_BAO_Activity::getActivitiesCount($params);
293
294 //since we are loading activities from dataset, we know total number of activities
295 // 8 schedule activities that should be shown on dashboard
296 $count = 8;
297 $this->assertEquals($count, $activityCount, 'In line ' . __LINE__);
298 }
299
300 /**
301 * Test getActivitiesCount BAO method
302 */
303 function testGetActivitiesCountforNonAdminDashboard() {
304 $op = new PHPUnit_Extensions_Database_Operation_Insert();
305 $op->execute($this->_dbconn,
306 $this->createFlatXMLDataSet(
307 dirname(__FILE__) . '/activities_for_dashboard_count.xml'
308 )
309 );
310
311 $params = array(
312 'contact_id' => 9,
313 'admin' => FALSE,
314 'caseId' => NULL,
315 'context' => 'home',
316 'activity_type_id' => NULL,
317 'offset' => 0,
318 'rowCount' => 0,
319 'sort' => NULL,
320 );
321
322 $activityCount = CRM_Activity_BAO_Activity::getActivitiesCount($params);
323
324 //since we are loading activities from dataset, we know total number of activities for this contact
325 // 5 activities ( 2 scheduled, 3 Completed ), note that dashboard shows only scheduled activities
326 $count = 2;
327 $this->assertEquals($count, $activityCount, 'In line ' . __LINE__);
328 }
329
330 /**
331 * Test getActivitiesCount BAO method
332 */
333 function testGetActivitiesCountforContactSummary() {
334 $op = new PHPUnit_Extensions_Database_Operation_Insert();
335 $op->execute($this->_dbconn,
336 $this->createFlatXMLDataSet(
337 dirname(__FILE__) . '/activities_for_dashboard_count.xml'
338 )
339 );
340
341 $params = array(
342 'contact_id' => 9,
343 'admin' => FALSE,
344 'caseId' => NULL,
345 'context' => 'activity',
346 'activity_type_id' => NULL,
347 'offset' => 0,
348 'rowCount' => 0,
349 'sort' => NULL,
350 );
351 $activityCount = CRM_Activity_BAO_Activity::getActivitiesCount($params);
352
353 //since we are loading activities from dataset, we know total number of activities for this contact
354 // 5 activities, Contact Summary should show all activities
355 $count = 5;
356 $this->assertEquals($count, $activityCount, 'In line ' . __LINE__);
357 }
358
359 /**
360 * Test getActivitiesCount BAO method
361 */
362 function testGetActivitiesCountforContactSummaryWithNoActivities() {
363 $op = new PHPUnit_Extensions_Database_Operation_Insert();
364 $op->execute($this->_dbconn,
365 $this->createFlatXMLDataSet(
366 dirname(__FILE__) . '/activities_for_dashboard_count.xml'
367 )
368 );
369
370 $params = array(
371 'contact_id' => 17,
372 'admin' => FALSE,
373 'caseId' => NULL,
374 'context' => 'home',
375 'activity_type_id' => NULL,
376 'offset' => 0,
377 'rowCount' => 0,
378 'sort' => NULL,
379 );
380 $activityCount = CRM_Activity_BAO_Activity::getActivitiesCount($params);
381
382 //since we are loading activities from dataset, we know total number of activities for this contact
383 // this contact does not have any activity
384 $this->assertEquals(0, $activityCount, 'In line ' . __LINE__);
385 }
386
387 /**
388 * Test getActivities BAO method
389 */
390 function testGetActivitiesforAdminDashboard() {
391 $op = new PHPUnit_Extensions_Database_Operation_Insert();
392 $op->execute($this->_dbconn,
393 $this->createFlatXMLDataSet(
394 dirname(__FILE__) . '/activities_for_dashboard_count.xml'
395 )
396 );
397
398 $params = array(
399 'contact_id' => 5,
400 'admin' => TRUE,
401 'caseId' => NULL,
402 'context' => 'home',
403 'activity_type_id' => NULL,
404 'offset' => 0,
405 'rowCount' => 0,
406 'sort' => NULL,
407 );
408 $activities = CRM_Activity_BAO_Activity::getActivities($params);
409
410 //since we are loading activities from dataset, we know total number of activities
411 // 8 schedule activities that should be shown on dashboard
412 $count = 8;
413 $this->assertEquals($count, sizeof($activities), 'In line ' . __LINE__);
414
415 foreach ($activities as $key => $value) {
416 $this->assertEquals($value['subject'], "subject {$key}", 'Verify activity subject is correct.');
417 $this->assertEquals($value['activity_type_id'], 2, 'Verify activity type is correct.');
418 $this->assertEquals($value['status_id'], 1, 'Verify all activities are scheduled.');
419 }
420 }
421
422 /**
423 * Test getActivities BAO method
424 */
425 function testGetActivitiesforNonAdminDashboard() {
426 $op = new PHPUnit_Extensions_Database_Operation_Insert();
427 $op->execute($this->_dbconn,
428 $this->createFlatXMLDataSet(
429 dirname(__FILE__) . '/activities_for_dashboard_count.xml'
430 )
431 );
432
433 $contactID = 9;
434 $params = array(
435 'contact_id' => $contactID,
436 'admin' => FALSE,
437 'caseId' => NULL,
438 'context' => 'home',
439 'activity_type_id' => NULL,
440 'offset' => 0,
441 'rowCount' => 0,
442 'sort' => NULL,
443 );
444 $activities = CRM_Activity_BAO_Activity::getActivities($params);
445
446 //since we are loading activities from dataset, we know total number of activities for this contact
447 // 5 activities ( 2 scheduled, 3 Completed ), note that dashboard shows only scheduled activities
448 $count = 2;
449 $this->assertEquals($count, sizeof($activities), 'In line ' . __LINE__);
450
451 foreach ($activities as $key => $value) {
452 $this->assertEquals($value['subject'], "subject {$key}", 'Verify activity subject is correct.');
453 $this->assertEquals($value['activity_type_id'], 2, 'Verify activity type is correct.');
454 $this->assertEquals($value['status_id'], 1, 'Verify all activities are scheduled.');
455
456 if ($key == 3) {
457 $this->assertArrayHasKey($contactID, $value['target_contact_name']);
458 }
459 elseif ($key == 4) {
460 $this->assertArrayHasKey($contactID, $value['assignee_contact_name']);
461 }
462 }
463 }
464
465 /**
466 * Test getActivities BAO method
467 */
468 function testGetActivitiesforContactSummary() {
469 $op = new PHPUnit_Extensions_Database_Operation_Insert();
470 $op->execute($this->_dbconn,
471 $this->createFlatXMLDataSet(
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' => 'activity',
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, Contact Summary should show all activities
491 $count = 5;
492 $this->assertEquals($count, sizeof($activities), 'In line ' . __LINE__);
493
494 foreach ($activities as $key => $value) {
495 $this->assertEquals($value['subject'], "subject {$key}", 'Verify activity subject is correct.');
496
497 if ($key > 8) {
498 $this->assertEquals($value['status_id'], 2, 'Verify all activities are scheduled.');
499 }
500 else {
501 $this->assertEquals($value['status_id'], 1, 'Verify all activities are scheduled.');
502 }
503
504 if ($key > 8) {
505 $this->assertEquals($value['activity_type_id'], 1, 'Verify activity type is correct.');
506 }
507 else {
508 $this->assertEquals($value['activity_type_id'], 2, 'Verify activity type is correct.');
509 }
510
511 if ($key == 3) {
512 $this->assertArrayHasKey($contactID, $value['target_contact_name']);
513 }
514 elseif ($key == 4) {
515 $this->assertArrayHasKey($contactID, $value['assignee_contact_name']);
516 }
517 }
518 }
519
520 /**
521 * Test getActivities BAO method
522 */
523 function testGetActivitiesforContactSummaryWithNoActivities() {
524 $op = new PHPUnit_Extensions_Database_Operation_Insert();
525 $op->execute($this->_dbconn,
526 $this->createFlatXMLDataSet(
527 dirname(__FILE__) . '/activities_for_dashboard_count.xml'
528 )
529 );
530
531 $params = array(
532 'contact_id' => 17,
533 'admin' => FALSE,
534 'caseId' => NULL,
535 'context' => 'home',
536 'activity_type_id' => NULL,
537 'offset' => 0,
538 'rowCount' => 0,
539 'sort' => NULL,
540 );
541 $activities = CRM_Activity_BAO_Activity::getActivities($params);
542
543 //since we are loading activities from dataset, we know total number of activities for this contact
544 // This contact does not have any activities
545 $this->assertEquals(0, sizeof($activities), 'In line ' . __LINE__);
546 }
547 }
548