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