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