3 require_once 'CiviTest/Contact.php';
6 * Class CRM_Activity_BAO_ActivityTest
8 class CRM_Activity_BAO_ActivityTest
extends CiviUnitTestCase
{
9 public function setUp() {
13 public function tearDown() {
14 // truncate a few tables
15 $tablesToTruncate = array('civicrm_contact', 'civicrm_activity', 'civicrm_activity_contact');
16 $this->quickCleanup($tablesToTruncate);
20 * Test case for create() method.
22 public function testCreate() {
23 $contactId = Contact
::createIndividual();
26 'source_contact_id' => $contactId,
27 'subject' => 'Scheduling Meeting',
28 'activity_type_id' => 2,
31 CRM_Activity_BAO_Activity
::create($params);
33 $activityId = $this->assertDBNotNull('CRM_Activity_DAO_Activity', 'Scheduling Meeting', 'id',
34 'subject', 'Database check for created activity.'
37 // Now call create() to modify an existing Activity
42 'source_contact_id' => $contactId,
43 'subject' => 'Scheduling Interview',
44 'activity_type_id' => 3,
47 CRM_Activity_BAO_Activity
::create($params);
49 $activityTypeId = $this->assertDBNotNull('CRM_Activity_DAO_Activity', 'Scheduling Interview',
51 'subject', 'Database check on updated activity record.'
53 $this->assertEquals($activityTypeId, 3, 'Verify activity type id is 3.');
55 Contact
::delete($contactId);
59 * Test case for getContactActivity() method.
61 * getContactActivity() method get activities detail for given target contact id.
63 public function testGetContactActivity() {
64 $contactId = Contact
::createIndividual();
66 'first_name' => 'liz',
67 'last_name' => 'hurleey',
69 $targetContactId = Contact
::createIndividual($params);
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'),
79 CRM_Activity_BAO_Activity
::create($params);
81 $activityId = $this->assertDBNotNull('CRM_Activity_DAO_Activity', 'Scheduling Meeting',
83 'subject', 'Database check for created activity.'
86 $activities = CRM_Activity_BAO_Activity
::getContactActivity($targetContactId);
88 $this->assertEquals($activities[$activityId]['subject'], 'Scheduling Meeting', 'Verify activity subject is correct.');
90 Contact
::delete($contactId);
91 Contact
::delete($targetContactId);
95 * Test case for retrieve() method.
97 * Retrieve($params, $defaults) method return activity detail for given params
100 public function testRetrieve() {
101 $contactId = Contact
::createIndividual();
103 'first_name' => 'liz',
104 'last_name' => 'hurleey',
106 $targetContactId = Contact
::createIndividual($params);
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'),
116 CRM_Activity_BAO_Activity
::create($params);
118 $activityId = $this->assertDBNotNull('CRM_Activity_DAO_Activity', 'Scheduling Meeting', 'id',
119 'subject', 'Database check for created activity.'
122 $activityTargetId = $this->assertDBNotNull('CRM_Activity_DAO_ActivityContact', $targetContactId,
124 'Database check for created activity target.'
128 $activity = CRM_Activity_BAO_Activity
::retrieve($params, $defaults);
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.');
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.');
137 $this->assertEquals($defaults['target_contact'][0], $targetContactId, 'Verify target contact id is correct.');
139 Contact
::delete($contactId);
140 Contact
::delete($targetContactId);
144 * Test case for deleteActivity() method.
146 * deleteActivity($params) method deletes activity for given params.
148 public function testDeleteActivity() {
149 $contactId = Contact
::createIndividual();
151 'first_name' => 'liz',
152 'last_name' => 'hurleey',
154 $targetContactId = Contact
::createIndividual($params);
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'),
165 CRM_Activity_BAO_Activity
::create($params);
167 $activityId = $this->assertDBNotNull('CRM_Activity_DAO_Activity', 'Scheduling Meeting', 'id',
168 'subject', 'Database check for created activity.'
171 $activityTargetId = $this->assertDBNotNull('CRM_Activity_DAO_ActivityContact', $targetContactId,
173 'Database check for created activity target.'
176 'source_contact_id' => $contactId,
177 'source_record_id' => $contactId,
178 'subject' => 'Scheduling Meeting',
179 'activity_type_id' => 2,
182 $result = CRM_Activity_BAO_Activity
::deleteActivity($params);
184 $activityId = $this->assertDBNull('CRM_Activity_DAO_Activity', 'Scheduling Meeting', 'id',
185 'subject', 'Database check for deleted activity.'
187 Contact
::delete($contactId);
188 Contact
::delete($targetContactId);
192 * Test case for deleteActivityTarget() method.
194 * deleteActivityTarget($activityId) method deletes activity target for given activity id.
196 public function testDeleteActivityTarget() {
197 $contactId = Contact
::createIndividual();
199 'first_name' => 'liz',
200 'last_name' => 'hurleey',
202 $targetContactId = Contact
::createIndividual($params);
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'),
212 CRM_Activity_BAO_Activity
::create($params);
214 $activityId = $this->assertDBNotNull('CRM_Activity_DAO_Activity', 'Scheduling Meeting', 'id',
215 'subject', 'Database check for created activity.'
218 $activityTargetId = $this->assertDBNotNull('CRM_Activity_DAO_ActivityContact', $targetContactId,
220 'Database check for created activity target.'
223 CRM_Activity_BAO_Activity
::deleteActivityContact($activityId, 3);
225 $this->assertDBNull('CRM_Activity_DAO_ActivityContact', $targetContactId, 'id',
226 'contact_id', 'Database check for deleted activity target.'
229 Contact
::delete($contactId);
230 Contact
::delete($targetContactId);
234 * Test case for deleteActivityAssignment() method.
236 * deleteActivityAssignment($activityId) method deletes activity assignment for given activity id.
238 public function testDeleteActivityAssignment() {
239 $contactId = Contact
::createIndividual();
241 'first_name' => 'liz',
242 'last_name' => 'hurleey',
244 $assigneeContactId = Contact
::createIndividual($params);
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'),
254 CRM_Activity_BAO_Activity
::create($params);
256 $activityId = $this->assertDBNotNull('CRM_Activity_DAO_Activity', 'Scheduling Meeting', 'id',
257 'subject', 'Database check for created activity.'
260 $activityAssignmentId = $this->assertDBNotNull('CRM_Activity_DAO_ActivityContact',
261 $assigneeContactId, 'id', 'contact_id',
262 'Database check for created activity assignment.'
265 CRM_Activity_BAO_Activity
::deleteActivityContact($activityId, 1);
267 $this->assertDBNull('CRM_Activity_DAO_ActivityContact', $assigneeContactId, 'id',
268 'contact_id', 'Database check for deleted activity assignment.'
271 Contact
::delete($contactId);
272 Contact
::delete($assigneeContactId);
276 * Test getActivitiesCount BAO method.
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'
287 'contact_id' => NULL,
291 'activity_type_id' => NULL,
296 $activityCount = CRM_Activity_BAO_Activity
::getActivitiesCount($params);
298 //since we are loading activities from dataset, we know total number of activities
299 // 8 schedule activities that should be shown on dashboard
301 $this->assertEquals($count, $activityCount);
305 * Test getActivitiesCount BAO method.
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'
320 'activity_type_id' => NULL,
326 $activityCount = CRM_Activity_BAO_Activity
::getActivitiesCount($params);
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
331 $this->assertEquals($count, $activityCount);
335 * Test getActivitiesCount BAO method.
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'
349 'context' => 'activity',
350 'activity_type_id' => NULL,
355 $activityCount = CRM_Activity_BAO_Activity
::getActivitiesCount($params);
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
360 $this->assertEquals($count, $activityCount);
364 * Test getActivitiesCount BAO method.
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'
379 'activity_type_id' => NULL,
384 $activityCount = CRM_Activity_BAO_Activity
::getActivitiesCount($params);
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);
392 * Test getActivities BAO method.
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'
407 'activity_type_id' => NULL,
412 $activities = CRM_Activity_BAO_Activity
::getActivities($params);
414 //since we are loading activities from dataset, we know total number of activities
415 // 8 schedule activities that should be shown on dashboard
417 $this->assertEquals($count, count($activities));
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.');
427 * Test getActivities BAO method.
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'
439 'contact_id' => $contactID,
443 'activity_type_id' => NULL,
448 $activities = CRM_Activity_BAO_Activity
::getActivities($params);
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
453 $this->assertEquals($count, count($activities));
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.');
461 $this->assertArrayHasKey($contactID, $value['target_contact_name']);
464 $this->assertArrayHasKey($contactID, $value['assignee_contact_name']);
470 * Test getActivities BAO method.
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'
482 'contact_id' => $contactID,
485 'context' => 'activity',
486 'activity_type_id' => NULL,
491 $activities = CRM_Activity_BAO_Activity
::getActivities($params);
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
496 $this->assertEquals($count, count($activities));
498 foreach ($activities as $key => $value) {
499 $this->assertEquals($value['subject'], "subject {$key}", 'Verify activity subject is correct.');
502 $this->assertEquals($value['status_id'], 2, 'Verify all activities are scheduled.');
505 $this->assertEquals($value['status_id'], 1, 'Verify all activities are scheduled.');
509 $this->assertEquals($value['activity_type_id'], 1, 'Verify activity type is correct.');
512 $this->assertEquals($value['activity_type_id'], 2, 'Verify activity type is correct.');
516 $this->assertArrayHasKey($contactID, $value['target_contact_name']);
519 $this->assertArrayHasKey($contactID, $value['assignee_contact_name']);
525 * Test getActivities BAO method.
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'
540 'activity_type_id' => NULL,
545 $activities = CRM_Activity_BAO_Activity
::getActivities($params);
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));