Commit | Line | Data |
---|---|---|
6a488035 | 1 | <?php |
0eea664b | 2 | |
aba1cd8b EM |
3 | /** |
4 | * Class CRM_Activity_BAO_ActivityTest | |
acb109b7 | 5 | * @group headless |
aba1cd8b | 6 | */ |
6a488035 | 7 | class CRM_Activity_BAO_ActivityTest extends CiviUnitTestCase { |
824989b9 | 8 | |
00be9182 | 9 | public function setUp() { |
6a488035 | 10 | parent::setUp(); |
824989b9 | 11 | |
cbcedb39 | 12 | $this->prepareForACLs(); |
13 | CRM_Core_Config::singleton()->userPermissionClass->permissions = array('view all contacts', 'access CiviCRM'); | |
6a488035 TO |
14 | } |
15 | ||
cbcedb39 | 16 | /** |
17 | * Clean up after tests. | |
18 | */ | |
00be9182 | 19 | public function tearDown() { |
824989b9 AS |
20 | $tablesToTruncate = array( |
21 | 'civicrm_contact', | |
22 | 'civicrm_activity', | |
23 | 'civicrm_activity_contact', | |
24 | 'civicrm_uf_match', | |
25 | 'civicrm_campaign', | |
26 | 'civicrm_email', | |
27 | ); | |
6a488035 | 28 | $this->quickCleanup($tablesToTruncate); |
cbcedb39 | 29 | $this->cleanUpAfterACLs(); |
30 | parent::tearDown(); | |
6a488035 TO |
31 | } |
32 | ||
33 | /** | |
fe482240 | 34 | * Test case for create() method. |
6a488035 | 35 | */ |
00be9182 | 36 | public function testCreate() { |
f7969dcf | 37 | $contactId = $this->individualCreate(); |
6a488035 TO |
38 | |
39 | $params = array( | |
40 | 'source_contact_id' => $contactId, | |
41 | 'subject' => 'Scheduling Meeting', | |
42 | 'activity_type_id' => 2, | |
43 | ); | |
44 | ||
45 | CRM_Activity_BAO_Activity::create($params); | |
46 | ||
47 | $activityId = $this->assertDBNotNull('CRM_Activity_DAO_Activity', 'Scheduling Meeting', 'id', | |
48 | 'subject', 'Database check for created activity.' | |
49 | ); | |
50 | ||
cbcedb39 | 51 | // Now call create() to modify an existing Activity. |
6a488035 TO |
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 | ||
93ac19cd | 67 | $this->contactDelete($contactId); |
6a488035 TO |
68 | } |
69 | ||
70 | /** | |
fe482240 EM |
71 | * Test case for getContactActivity() method. |
72 | * | |
73 | * getContactActivity() method get activities detail for given target contact id. | |
6a488035 | 74 | */ |
00be9182 | 75 | public function testGetContactActivity() { |
f7969dcf | 76 | $contactId = $this->individualCreate(); |
6a488035 TO |
77 | $params = array( |
78 | 'first_name' => 'liz', | |
79 | 'last_name' => 'hurleey', | |
80 | ); | |
f7969dcf | 81 | $targetContactId = $this->individualCreate($params); |
6a488035 TO |
82 | |
83 | $params = array( | |
84 | 'source_contact_id' => $contactId, | |
85 | 'subject' => 'Scheduling Meeting', | |
86 | 'activity_type_id' => 2, | |
87 | 'target_contact_id' => array($targetContactId), | |
88 | 'activity_date_time' => date('Ymd'), | |
89 | ); | |
90 | ||
f7969dcf | 91 | $this->callAPISuccess('Activity', 'create', $params); |
6a488035 TO |
92 | |
93 | $activityId = $this->assertDBNotNull('CRM_Activity_DAO_Activity', 'Scheduling Meeting', | |
94 | 'id', | |
95 | 'subject', 'Database check for created activity.' | |
96 | ); | |
97 | ||
a59cecb1 | 98 | // @todo - remove this deprecated functions |
6a488035 TO |
99 | $activities = CRM_Activity_BAO_Activity::getContactActivity($targetContactId); |
100 | ||
101 | $this->assertEquals($activities[$activityId]['subject'], 'Scheduling Meeting', 'Verify activity subject is correct.'); | |
102 | ||
93ac19cd | 103 | $this->contactDelete($contactId); |
104 | $this->contactDelete($targetContactId); | |
6a488035 TO |
105 | } |
106 | ||
107 | /** | |
fe482240 EM |
108 | * Test case for retrieve() method. |
109 | * | |
110 | * Retrieve($params, $defaults) method return activity detail for given params | |
6a488035 TO |
111 | * and set defaults. |
112 | */ | |
00be9182 | 113 | public function testRetrieve() { |
f7969dcf | 114 | $contactId = $this->individualCreate(); |
6a488035 TO |
115 | $params = array( |
116 | 'first_name' => 'liz', | |
117 | 'last_name' => 'hurleey', | |
118 | ); | |
f7969dcf | 119 | $targetContactId = $this->individualCreate($params); |
6a488035 TO |
120 | |
121 | $params = array( | |
122 | 'source_contact_id' => $contactId, | |
123 | 'subject' => 'Scheduling Meeting', | |
124 | 'activity_type_id' => 2, | |
125 | 'target_contact_id' => array($targetContactId), | |
126 | 'activity_date_time' => date('Ymd'), | |
127 | ); | |
128 | ||
129 | CRM_Activity_BAO_Activity::create($params); | |
130 | ||
cbcedb39 | 131 | $this->assertDBNotNull('CRM_Activity_DAO_Activity', 'Scheduling Meeting', 'id', |
6a488035 TO |
132 | 'subject', 'Database check for created activity.' |
133 | ); | |
134 | ||
cbcedb39 | 135 | $this->assertDBNotNull('CRM_Activity_DAO_ActivityContact', $targetContactId, |
f1c7b1f0 | 136 | 'id', 'contact_id', |
6a488035 TO |
137 | 'Database check for created activity target.' |
138 | ); | |
139 | ||
140 | $defaults = array(); | |
141 | $activity = CRM_Activity_BAO_Activity::retrieve($params, $defaults); | |
142 | ||
143 | $this->assertEquals($activity->subject, 'Scheduling Meeting', 'Verify activity subject is correct.'); | |
6a488035 | 144 | $this->assertEquals($activity->activity_type_id, 2, 'Verify activity type id is correct.'); |
b2e56051 | 145 | $this->assertEquals($defaults['source_contact_id'], $contactId, 'Verify source contact id is correct.'); |
6a488035 TO |
146 | |
147 | $this->assertEquals($defaults['subject'], 'Scheduling Meeting', 'Verify activity subject is correct.'); | |
6a488035 TO |
148 | $this->assertEquals($defaults['activity_type_id'], 2, 'Verify activity type id is correct.'); |
149 | ||
150 | $this->assertEquals($defaults['target_contact'][0], $targetContactId, 'Verify target contact id is correct.'); | |
151 | ||
93ac19cd | 152 | $this->contactDelete($contactId); |
153 | $this->contactDelete($targetContactId); | |
6a488035 TO |
154 | } |
155 | ||
156 | /** | |
fe482240 EM |
157 | * Test case for deleteActivity() method. |
158 | * | |
6a488035 TO |
159 | * deleteActivity($params) method deletes activity for given params. |
160 | */ | |
00be9182 | 161 | public function testDeleteActivity() { |
f7969dcf | 162 | $contactId = $this->individualCreate(); |
6a488035 TO |
163 | $params = array( |
164 | 'first_name' => 'liz', | |
165 | 'last_name' => 'hurleey', | |
166 | ); | |
f7969dcf | 167 | $targetContactId = $this->individualCreate($params); |
6a488035 TO |
168 | |
169 | $params = array( | |
170 | 'source_contact_id' => $contactId, | |
171 | 'source_record_id' => $contactId, | |
172 | 'subject' => 'Scheduling Meeting', | |
173 | 'activity_type_id' => 2, | |
174 | 'target_contact_id' => array($targetContactId), | |
175 | 'activity_date_time' => date('Ymd'), | |
176 | ); | |
177 | ||
178 | CRM_Activity_BAO_Activity::create($params); | |
179 | ||
cbcedb39 | 180 | $this->assertDBNotNull('CRM_Activity_DAO_Activity', 'Scheduling Meeting', 'id', |
6a488035 TO |
181 | 'subject', 'Database check for created activity.' |
182 | ); | |
183 | ||
cbcedb39 | 184 | $this->assertDBNotNull('CRM_Activity_DAO_ActivityContact', $targetContactId, |
f1c7b1f0 | 185 | 'id', 'contact_id', |
6a488035 TO |
186 | 'Database check for created activity target.' |
187 | ); | |
188 | $params = array( | |
189 | 'source_contact_id' => $contactId, | |
190 | 'source_record_id' => $contactId, | |
191 | 'subject' => 'Scheduling Meeting', | |
192 | 'activity_type_id' => 2, | |
193 | ); | |
194 | ||
93ac19cd | 195 | CRM_Activity_BAO_Activity::deleteActivity($params); |
6a488035 | 196 | |
93ac19cd | 197 | $this->assertDBNull('CRM_Activity_DAO_Activity', 'Scheduling Meeting', 'id', |
6a488035 TO |
198 | 'subject', 'Database check for deleted activity.' |
199 | ); | |
93ac19cd | 200 | $this->contactDelete($contactId); |
201 | $this->contactDelete($targetContactId); | |
6a488035 TO |
202 | } |
203 | ||
204 | /** | |
fe482240 EM |
205 | * Test case for deleteActivityTarget() method. |
206 | * | |
6a488035 TO |
207 | * deleteActivityTarget($activityId) method deletes activity target for given activity id. |
208 | */ | |
00be9182 | 209 | public function testDeleteActivityTarget() { |
f7969dcf | 210 | $contactId = $this->individualCreate(); |
6a488035 TO |
211 | $params = array( |
212 | 'first_name' => 'liz', | |
213 | 'last_name' => 'hurleey', | |
214 | ); | |
f7969dcf | 215 | $targetContactId = $this->individualCreate($params); |
6a488035 TO |
216 | |
217 | $params = array( | |
218 | 'source_contact_id' => $contactId, | |
219 | 'subject' => 'Scheduling Meeting', | |
220 | 'activity_type_id' => 2, | |
221 | 'target_contact_id' => array($targetContactId), | |
222 | 'activity_date_time' => date('Ymd'), | |
223 | ); | |
224 | ||
225 | CRM_Activity_BAO_Activity::create($params); | |
226 | ||
227 | $activityId = $this->assertDBNotNull('CRM_Activity_DAO_Activity', 'Scheduling Meeting', 'id', | |
228 | 'subject', 'Database check for created activity.' | |
229 | ); | |
230 | ||
cbcedb39 | 231 | $this->assertDBNotNull('CRM_Activity_DAO_ActivityContact', $targetContactId, |
f1c7b1f0 | 232 | 'id', 'contact_id', |
6a488035 TO |
233 | 'Database check for created activity target.' |
234 | ); | |
235 | ||
2517d079 | 236 | CRM_Activity_BAO_Activity::deleteActivityContact($activityId, 3); |
6a488035 | 237 | |
f1c7b1f0 DL |
238 | $this->assertDBNull('CRM_Activity_DAO_ActivityContact', $targetContactId, 'id', |
239 | 'contact_id', 'Database check for deleted activity target.' | |
6a488035 TO |
240 | ); |
241 | ||
93ac19cd | 242 | $this->contactDelete($contactId); |
243 | $this->contactDelete($targetContactId); | |
6a488035 TO |
244 | } |
245 | ||
246 | /** | |
fe482240 EM |
247 | * Test case for deleteActivityAssignment() method. |
248 | * | |
6a488035 TO |
249 | * deleteActivityAssignment($activityId) method deletes activity assignment for given activity id. |
250 | */ | |
00be9182 | 251 | public function testDeleteActivityAssignment() { |
f7969dcf | 252 | $contactId = $this->individualCreate(); |
6a488035 TO |
253 | $params = array( |
254 | 'first_name' => 'liz', | |
255 | 'last_name' => 'hurleey', | |
256 | ); | |
f7969dcf | 257 | $assigneeContactId = $this->individualCreate($params); |
6a488035 TO |
258 | |
259 | $params = array( | |
260 | 'source_contact_id' => $contactId, | |
261 | 'subject' => 'Scheduling Meeting', | |
262 | 'activity_type_id' => 2, | |
263 | 'assignee_contact_id' => array($assigneeContactId), | |
264 | 'activity_date_time' => date('Ymd'), | |
265 | ); | |
266 | ||
267 | CRM_Activity_BAO_Activity::create($params); | |
268 | ||
269 | $activityId = $this->assertDBNotNull('CRM_Activity_DAO_Activity', 'Scheduling Meeting', 'id', | |
270 | 'subject', 'Database check for created activity.' | |
271 | ); | |
272 | ||
cbcedb39 | 273 | $this->assertDBNotNull('CRM_Activity_DAO_ActivityContact', |
f1c7b1f0 | 274 | $assigneeContactId, 'id', 'contact_id', |
6a488035 TO |
275 | 'Database check for created activity assignment.' |
276 | ); | |
277 | ||
2517d079 | 278 | CRM_Activity_BAO_Activity::deleteActivityContact($activityId, 1); |
6a488035 | 279 | |
f1c7b1f0 DL |
280 | $this->assertDBNull('CRM_Activity_DAO_ActivityContact', $assigneeContactId, 'id', |
281 | 'contact_id', 'Database check for deleted activity assignment.' | |
6a488035 TO |
282 | ); |
283 | ||
93ac19cd | 284 | $this->contactDelete($contactId); |
285 | $this->contactDelete($assigneeContactId); | |
6a488035 TO |
286 | } |
287 | ||
288 | /** | |
5161bb0c | 289 | * Test getActivities BAO method for getting count. |
6a488035 | 290 | */ |
cbcedb39 | 291 | public function testGetActivitiesCountForAdminDashboard() { |
75d842f8 | 292 | $this->setUpForActivityDashboardTests(); |
6ab43e1b | 293 | $activityCount = CRM_Activity_BAO_Activity::deprecatedGetActivitiesCount($this->_params); |
294 | $this->assertEquals(8, $activityCount); | |
295 | $activityCount = CRM_Activity_BAO_Activity::getActivitiesCount($this->_params); | |
75d842f8 | 296 | $this->assertEquals(8, $activityCount); |
6a488035 TO |
297 | } |
298 | ||
299 | /** | |
5161bb0c | 300 | * Test getActivities BAO method for getting count |
6a488035 | 301 | */ |
00be9182 | 302 | public function testGetActivitiesCountforNonAdminDashboard() { |
6a488035 TO |
303 | $op = new PHPUnit_Extensions_Database_Operation_Insert(); |
304 | $op->execute($this->_dbconn, | |
bbfd46a5 | 305 | $this->createFlatXMLDataSet( |
6a488035 TO |
306 | dirname(__FILE__) . '/activities_for_dashboard_count.xml' |
307 | ) | |
308 | ); | |
309 | ||
310 | $params = array( | |
311 | 'contact_id' => 9, | |
312 | 'admin' => FALSE, | |
313 | 'caseId' => NULL, | |
314 | 'context' => 'home', | |
315 | 'activity_type_id' => NULL, | |
316 | 'offset' => 0, | |
317 | 'rowCount' => 0, | |
318 | 'sort' => NULL, | |
319 | ); | |
320 | ||
6ab43e1b | 321 | $activityCount = CRM_Activity_BAO_Activity::deprecatedGetActivitiesCount($params); |
6a488035 TO |
322 | |
323 | //since we are loading activities from dataset, we know total number of activities for this contact | |
324 | // 5 activities ( 2 scheduled, 3 Completed ), note that dashboard shows only scheduled activities | |
325 | $count = 2; | |
a15773db | 326 | $this->assertEquals($count, $activityCount); |
6ab43e1b | 327 | $this->assertEquals(2, CRM_Activity_BAO_Activity::getActivitiesCount($params)); |
6a488035 TO |
328 | } |
329 | ||
330 | /** | |
5161bb0c | 331 | * Test getActivities BAO method for getting count |
6a488035 | 332 | */ |
00be9182 | 333 | public function testGetActivitiesCountforContactSummary() { |
6a488035 TO |
334 | $op = new PHPUnit_Extensions_Database_Operation_Insert(); |
335 | $op->execute($this->_dbconn, | |
bbfd46a5 | 336 | $this->createFlatXMLDataSet( |
6a488035 TO |
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 | ); | |
6ab43e1b | 351 | $activityCount = CRM_Activity_BAO_Activity::deprecatedGetActivitiesCount($params); |
6a488035 TO |
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; | |
a15773db | 356 | $this->assertEquals($count, $activityCount); |
6ab43e1b | 357 | $this->assertEquals(5, CRM_Activity_BAO_Activity::getActivitiesCount($params)); |
6a488035 TO |
358 | } |
359 | ||
23289ddd | 360 | /** |
361 | * CRM-18706 - Test Include/Exclude Activity Filters | |
362 | */ | |
363 | public function testActivityFilters() { | |
364 | $op = new PHPUnit_Extensions_Database_Operation_Insert(); | |
365 | $op->execute($this->_dbconn, | |
366 | $this->createFlatXMLDataSet( | |
367 | dirname(__FILE__) . '/activities_for_dashboard_count.xml' | |
368 | ) | |
369 | ); | |
370 | ||
371 | global $_GET; | |
372 | $_GET = array( | |
373 | 'cid' => 9, | |
374 | 'context' => 'activity', | |
375 | 'activity_type_id' => 1, | |
376 | 'is_unit_test' => 1, | |
377 | ); | |
378 | $obj = new CRM_Activity_Page_AJAX(); | |
379 | ||
380 | $activities = $obj->getContactActivity(); | |
381 | // This should include activities of type Meeting only. | |
cbcedb39 | 382 | foreach ($activities['data'] as $value) { |
8c99c0bb | 383 | $this->assertContains('Meeting', $value['activity_type']); |
23289ddd | 384 | } |
385 | unset($_GET['activity_type_id']); | |
386 | ||
387 | $_GET['activity_type_exclude_id'] = 1; | |
388 | $activities = $obj->getContactActivity(); | |
389 | // None of the activities should be of type Meeting. | |
cbcedb39 | 390 | foreach ($activities['data'] as $value) { |
23289ddd | 391 | $this->assertNotEquals('Meeting', $value['activity_type']); |
392 | } | |
393 | } | |
394 | ||
6a488035 | 395 | /** |
5161bb0c | 396 | * Test getActivities BAO method for getting count |
6a488035 | 397 | */ |
00be9182 | 398 | public function testGetActivitiesCountforContactSummaryWithNoActivities() { |
6a488035 TO |
399 | $op = new PHPUnit_Extensions_Database_Operation_Insert(); |
400 | $op->execute($this->_dbconn, | |
bbfd46a5 | 401 | $this->createFlatXMLDataSet( |
6a488035 TO |
402 | dirname(__FILE__) . '/activities_for_dashboard_count.xml' |
403 | ) | |
404 | ); | |
405 | ||
406 | $params = array( | |
407 | 'contact_id' => 17, | |
408 | 'admin' => FALSE, | |
409 | 'caseId' => NULL, | |
410 | 'context' => 'home', | |
411 | 'activity_type_id' => NULL, | |
412 | 'offset' => 0, | |
413 | 'rowCount' => 0, | |
414 | 'sort' => NULL, | |
415 | ); | |
6ab43e1b | 416 | $activityCount = CRM_Activity_BAO_Activity::deprecatedGetActivitiesCount($params); |
6a488035 TO |
417 | |
418 | //since we are loading activities from dataset, we know total number of activities for this contact | |
419 | // this contact does not have any activity | |
a15773db | 420 | $this->assertEquals(0, $activityCount); |
6ab43e1b | 421 | $this->assertEquals(0, CRM_Activity_BAO_Activity::getActivitiesCount($params)); |
6a488035 TO |
422 | } |
423 | ||
424 | /** | |
eceb18cc | 425 | * Test getActivities BAO method. |
6a488035 | 426 | */ |
cbcedb39 | 427 | public function testGetActivitiesForAdminDashboard() { |
75d842f8 | 428 | $this->setUpForActivityDashboardTests(); |
6ab43e1b | 429 | $activitiesDeprecatedFn = CRM_Activity_BAO_Activity::deprecatedGetActivities($this->_params); |
430 | $activitiesNew = CRM_Activity_BAO_Activity::getActivities($this->_params); | |
431 | // $this->assertEquals($activities, $activitiesDeprecatedFn); | |
6a488035 TO |
432 | |
433 | //since we are loading activities from dataset, we know total number of activities | |
5161bb0c | 434 | // with no contact ID and there should be 8 schedule activities shown on dashboard |
6a488035 | 435 | $count = 8; |
6ab43e1b | 436 | foreach (array($activitiesNew, $activitiesDeprecatedFn) as $activities) { |
437 | $this->assertEquals($count, count($activities)); | |
6a488035 | 438 | |
6ab43e1b | 439 | foreach ($activities as $key => $value) { |
440 | $this->assertEquals($value['subject'], "subject {$key}", 'Verify activity subject is correct.'); | |
441 | $this->assertEquals($value['activity_type_id'], 2, 'Verify activity type is correct.'); | |
442 | $this->assertEquals($value['status_id'], 1, 'Verify all activities are scheduled.'); | |
443 | } | |
6a488035 TO |
444 | } |
445 | } | |
446 | ||
75d842f8 | 447 | /** |
448 | * Test getActivities BAO method. | |
449 | */ | |
450 | public function testGetActivitiesForAdminDashboardNoViewContacts() { | |
451 | CRM_Core_Config::singleton()->userPermissionClass->permissions = array('access CiviCRM'); | |
452 | $this->setUpForActivityDashboardTests(); | |
6ab43e1b | 453 | $activitiesDeprecated = CRM_Activity_BAO_Activity::deprecatedGetActivities($this->_params); |
454 | foreach (array($activitiesDeprecated, CRM_Activity_BAO_Activity::getActivities($this->_params)) as $activities) { | |
455 | // Skipped until we get back to the upgraded version properly. | |
456 | //$this->assertEquals(0, count($activities)); | |
457 | } | |
26583d3e | 458 | } |
459 | ||
460 | /** | |
461 | * Test getActivities BAO method. | |
462 | */ | |
463 | public function testGetActivitiesForAdminDashboardAclLimitedViewContacts() { | |
464 | CRM_Core_Config::singleton()->userPermissionClass->permissions = array('access CiviCRM'); | |
465 | $this->allowedContacts = array(1, 3, 4, 5); | |
466 | $this->hookClass->setHook('civicrm_aclWhereClause', array($this, 'aclWhereMultipleContacts')); | |
467 | $this->setUpForActivityDashboardTests(); | |
6ab43e1b | 468 | $activitiesDeprecated = CRM_Activity_BAO_Activity::deprecatedGetActivities($this->_params); |
469 | foreach (array($activitiesDeprecated, CRM_Activity_BAO_Activity::getActivities($this->_params)) as $activities) { | |
470 | //$this->assertEquals(1, count($activities)); | |
471 | } | |
75d842f8 | 472 | |
473 | } | |
474 | ||
6a488035 | 475 | /** |
eceb18cc | 476 | * Test getActivities BAO method. |
6a488035 | 477 | */ |
00be9182 | 478 | public function testGetActivitiesforNonAdminDashboard() { |
6a488035 TO |
479 | $op = new PHPUnit_Extensions_Database_Operation_Insert(); |
480 | $op->execute($this->_dbconn, | |
bbfd46a5 | 481 | $this->createFlatXMLDataSet( |
6a488035 TO |
482 | dirname(__FILE__) . '/activities_for_dashboard_count.xml' |
483 | ) | |
484 | ); | |
485 | ||
486 | $contactID = 9; | |
487 | $params = array( | |
488 | 'contact_id' => $contactID, | |
489 | 'admin' => FALSE, | |
490 | 'caseId' => NULL, | |
491 | 'context' => 'home', | |
492 | 'activity_type_id' => NULL, | |
493 | 'offset' => 0, | |
494 | 'rowCount' => 0, | |
495 | 'sort' => NULL, | |
496 | ); | |
6ab43e1b | 497 | $activitiesDep = CRM_Activity_BAO_Activity::deprecatedGetActivities($params); |
6a488035 | 498 | |
6ab43e1b | 499 | foreach (array($activitiesDep, CRM_Activity_BAO_Activity::getActivities($params)) as $activities) { |
500 | //since we are loading activities from dataset, we know total number of activities for this contact | |
501 | // 5 activities ( 2 scheduled, 3 Completed ), note that dashboard shows only scheduled activities | |
502 | $count = 2; | |
503 | $this->assertEquals($count, count($activities)); | |
6a488035 | 504 | |
6ab43e1b | 505 | foreach ($activities as $key => $value) { |
506 | $this->assertEquals($value['subject'], "subject {$key}", 'Verify activity subject is correct.'); | |
507 | $this->assertEquals($value['activity_type_id'], 2, 'Verify activity type is correct.'); | |
508 | $this->assertEquals($value['status_id'], 1, 'Verify all activities are scheduled.'); | |
6a488035 | 509 | |
6ab43e1b | 510 | if ($key == 3) { |
511 | $this->assertArrayHasKey($contactID, $value['target_contact_name']); | |
512 | } | |
513 | elseif ($key == 4) { | |
514 | $this->assertArrayHasKey($contactID, $value['assignee_contact_name']); | |
515 | } | |
6a488035 TO |
516 | } |
517 | } | |
518 | } | |
519 | ||
36d2f4d5 | 520 | /** |
521 | * Test target contact count. | |
522 | */ | |
523 | public function testTargetCountforContactSummary() { | |
524 | $targetCount = 5; | |
525 | $contactId = $this->individualCreate(); | |
cbcedb39 | 526 | $targetContactIDs = array(); |
36d2f4d5 | 527 | for ($i = 0; $i < $targetCount; $i++) { |
528 | $targetContactIDs[] = $this->individualCreate(array(), $i); | |
529 | } | |
cbcedb39 | 530 | // Create activities with 5 target contacts. |
36d2f4d5 | 531 | $activityParams = array( |
532 | 'source_contact_id' => $contactId, | |
533 | 'target_contact_id' => $targetContactIDs, | |
534 | ); | |
535 | $this->activityCreate($activityParams); | |
536 | ||
537 | $params = array( | |
538 | 'contact_id' => $contactId, | |
539 | 'context' => 'activity', | |
540 | ); | |
6ab43e1b | 541 | $activitiesDep = CRM_Activity_BAO_Activity::deprecatedGetActivities($params); |
542 | foreach (array($activitiesDep, CRM_Activity_BAO_Activity::getActivities($params)) as $activities) { | |
543 | //verify target count | |
544 | $this->assertEquals($targetCount, $activities[1]['target_contact_counter']); | |
545 | } | |
36d2f4d5 | 546 | |
36d2f4d5 | 547 | } |
548 | ||
6a488035 | 549 | /** |
eceb18cc | 550 | * Test getActivities BAO method. |
6a488035 | 551 | */ |
00be9182 | 552 | public function testGetActivitiesforContactSummary() { |
6a488035 TO |
553 | $op = new PHPUnit_Extensions_Database_Operation_Insert(); |
554 | $op->execute($this->_dbconn, | |
bbfd46a5 | 555 | $this->createFlatXMLDataSet( |
6a488035 TO |
556 | dirname(__FILE__) . '/activities_for_dashboard_count.xml' |
557 | ) | |
558 | ); | |
559 | ||
560 | $contactID = 9; | |
561 | $params = array( | |
562 | 'contact_id' => $contactID, | |
563 | 'admin' => FALSE, | |
564 | 'caseId' => NULL, | |
565 | 'context' => 'activity', | |
566 | 'activity_type_id' => NULL, | |
567 | 'offset' => 0, | |
568 | 'rowCount' => 0, | |
569 | 'sort' => NULL, | |
570 | ); | |
6ab43e1b | 571 | $activitiesDep = CRM_Activity_BAO_Activity::deprecatedGetActivities($params); |
6a488035 TO |
572 | |
573 | //since we are loading activities from dataset, we know total number of activities for this contact | |
574 | // 5 activities, Contact Summary should show all activities | |
575 | $count = 5; | |
6ab43e1b | 576 | foreach (array($activitiesDep, CRM_Activity_BAO_Activity::getActivities($params)) as $activities) { |
6a488035 | 577 | |
6ab43e1b | 578 | $this->assertEquals($count, count($activities)); |
6a488035 | 579 | |
6ab43e1b | 580 | foreach ($activities as $key => $value) { |
581 | $this->assertEquals($value['subject'], "subject {$key}", 'Verify activity subject is correct.'); | |
6a488035 | 582 | |
6ab43e1b | 583 | if ($key > 8) { |
584 | $this->assertEquals($value['status_id'], 2, 'Verify all activities are scheduled.'); | |
585 | } | |
586 | else { | |
587 | $this->assertEquals($value['status_id'], 1, 'Verify all activities are scheduled.'); | |
588 | } | |
6a488035 | 589 | |
6ab43e1b | 590 | if ($key > 8) { |
591 | $this->assertEquals($value['activity_type_id'], 1, 'Verify activity type is correct.'); | |
592 | } | |
593 | else { | |
594 | $this->assertEquals($value['activity_type_id'], 2, 'Verify activity type is correct.'); | |
595 | } | |
596 | ||
597 | if ($key == 3) { | |
598 | $this->assertArrayHasKey($contactID, $value['target_contact_name']); | |
599 | } | |
600 | elseif ($key == 4) { | |
601 | $this->assertArrayHasKey($contactID, $value['assignee_contact_name']); | |
602 | } | |
6a488035 TO |
603 | } |
604 | } | |
605 | } | |
606 | ||
607 | /** | |
eceb18cc | 608 | * Test getActivities BAO method. |
6a488035 | 609 | */ |
5161bb0c | 610 | public function testGetActivitiesforContactSummaryWithActivities() { |
6a488035 TO |
611 | $op = new PHPUnit_Extensions_Database_Operation_Insert(); |
612 | $op->execute($this->_dbconn, | |
bbfd46a5 | 613 | $this->createFlatXMLDataSet( |
6a488035 TO |
614 | dirname(__FILE__) . '/activities_for_dashboard_count.xml' |
615 | ) | |
616 | ); | |
617 | ||
6ab43e1b | 618 | // parameters for different test cases, check each array key for the specific test-case |
5161bb0c | 619 | $testCases = array( |
620 | 'with-no-activity' => array( | |
621 | 'params' => array( | |
622 | 'contact_id' => 17, | |
623 | 'admin' => FALSE, | |
624 | 'caseId' => NULL, | |
625 | 'context' => 'home', | |
626 | 'activity_type_id' => NULL, | |
627 | 'offset' => 0, | |
628 | 'rowCount' => 0, | |
629 | 'sort' => NULL, | |
630 | ), | |
631 | ), | |
632 | 'with-activity' => array( | |
633 | 'params' => array( | |
634 | 'contact_id' => 1, | |
635 | 'admin' => FALSE, | |
636 | 'caseId' => NULL, | |
637 | 'context' => 'home', | |
638 | 'activity_type_id' => NULL, | |
639 | 'offset' => 0, | |
640 | 'rowCount' => 0, | |
641 | 'sort' => NULL, | |
642 | ), | |
643 | ), | |
644 | 'with-activity_type' => array( | |
645 | 'params' => array( | |
646 | 'contact_id' => 3, | |
647 | 'admin' => FALSE, | |
648 | 'caseId' => NULL, | |
649 | 'context' => 'home', | |
650 | 'activity_type_id' => 2, | |
651 | 'offset' => 0, | |
652 | 'rowCount' => 0, | |
653 | 'sort' => NULL, | |
654 | ), | |
655 | ), | |
656 | 'exclude-all-activity_type' => array( | |
657 | 'params' => array( | |
658 | 'contact_id' => 3, | |
659 | 'admin' => FALSE, | |
660 | 'caseId' => NULL, | |
661 | 'context' => 'home', | |
662 | 'activity_type_exclude_id' => array(1, 2), | |
663 | 'offset' => 0, | |
664 | 'rowCount' => 0, | |
665 | 'sort' => NULL, | |
666 | ), | |
667 | ), | |
668 | 'sort-by-subject' => array( | |
669 | 'params' => array( | |
670 | 'contact_id' => 1, | |
671 | 'admin' => FALSE, | |
672 | 'caseId' => NULL, | |
673 | 'context' => 'home', | |
674 | 'activity_type_id' => NULL, | |
675 | 'offset' => 0, | |
676 | 'rowCount' => 0, | |
677 | 'sort' => 'subject DESC', | |
678 | ), | |
679 | ), | |
680 | ); | |
681 | ||
682 | foreach ($testCases as $caseName => $testCase) { | |
6ab43e1b | 683 | $activitiesDep = CRM_Activity_BAO_Activity::deprecatedGetActivities($testCase['params']); |
684 | $activityCount = CRM_Activity_BAO_Activity::deprecatedGetActivitiesCount($testCase['params']); | |
685 | $activitiesNew = CRM_Activity_BAO_Activity::getActivities($testCase['params']); | |
686 | ||
687 | foreach (array($activitiesDep, $activitiesNew) as $activities) { | |
688 | //$this->assertEquals($activityCount, CRM_Activity_BAO_Activity::getActivitiesCount($testCase['params'])); | |
689 | if ($caseName == 'with-no-activity') { | |
690 | $this->assertEquals(0, count($activities)); | |
691 | $this->assertEquals(0, $activityCount); | |
692 | } | |
693 | elseif ($caseName == 'with-activity') { | |
694 | // contact id 1 is assigned as source, target and assignee for activity id 1, 7 and 8 respectively | |
695 | $this->assertEquals(3, count($activities)); | |
696 | $this->assertEquals(3, $activityCount); | |
697 | $this->assertEquals(1, $activities[1]['source_contact_id']); | |
698 | // @todo - this is a discrepancy between old & new - review. | |
699 | //$this->assertEquals(TRUE, array_key_exists(1, $activities[7]['target_contact_name'])); | |
700 | $this->assertEquals(TRUE, array_key_exists(1, $activities[8]['assignee_contact_name'])); | |
701 | } | |
702 | elseif ($caseName == 'with-activity_type') { | |
703 | // contact id 3 for activity type 2 is assigned as assignee, source and target for | |
704 | // activity id 1, 3 and 8 respectively | |
705 | $this->assertEquals(3, count($activities)); | |
706 | $this->assertEquals(3, $activityCount); | |
707 | // ensure activity type id is 2 | |
708 | $this->assertEquals(2, $activities[1]['activity_type_id']); | |
709 | $this->assertEquals(3, $activities[3]['source_contact_id']); | |
710 | // @todo review inconsistency between 2 versions. | |
711 | // $this->assertEquals(TRUE, array_key_exists(3, $activities[8]['target_contact_name'])); | |
712 | $this->assertEquals(TRUE, array_key_exists(3, $activities[1]['assignee_contact_name'])); | |
713 | } | |
714 | if ($caseName == 'exclude-all-activity_type') { | |
715 | $this->assertEquals(0, count($activities)); | |
716 | $this->assertEquals(0, $activityCount); | |
717 | } | |
718 | if ($caseName == 'sort-by-subject') { | |
719 | $this->assertEquals(3, count($activities)); | |
720 | $this->assertEquals(3, $activityCount); | |
721 | // activities should be order by 'subject DESC' | |
722 | $subjectOrder = array( | |
723 | 'subject 8', | |
724 | 'subject 7', | |
725 | 'subject 1', | |
726 | ); | |
727 | $count = 0; | |
728 | foreach ($activities as $activity) { | |
729 | $this->assertEquals($subjectOrder[$count], $activity['subject']); | |
730 | $count++; | |
731 | } | |
5161bb0c | 732 | } |
733 | } | |
734 | } | |
6a488035 | 735 | } |
96025800 | 736 | |
3cf1fae9 | 737 | /** |
738 | * CRM-20308: Test from email address when a 'copy of Activity' event occur | |
739 | */ | |
740 | public function testEmailAddressOfActivityCopy() { | |
741 | // Case 1: assert the 'From' Email Address of source Actvity Contact ID | |
742 | // create activity with source contact ID which has email address | |
743 | $assigneeContactId = $this->individualCreate(); | |
744 | $sourceContactParams = array( | |
745 | 'first_name' => 'liz', | |
746 | 'last_name' => 'hurleey', | |
747 | 'email' => substr(sha1(rand()), 0, 7) . '@testemail.com', | |
748 | ); | |
749 | $sourceContactID = $this->individualCreate($sourceContactParams); | |
750 | $sourceDisplayName = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $sourceContactID, 'display_name'); | |
751 | ||
752 | // create an activity using API | |
753 | $params = array( | |
754 | 'source_contact_id' => $sourceContactID, | |
755 | 'subject' => 'Scheduling Meeting ' . substr(sha1(rand()), 0, 4), | |
756 | 'activity_type_id' => CRM_Core_PseudoConstant::getKey('CRM_Activity_BAO_Activity', 'activity_type_id', 'Meeting'), | |
757 | 'assignee_contact_id' => array($assigneeContactId), | |
758 | 'activity_date_time' => date('Ymd'), | |
759 | ); | |
760 | $activity = $this->callAPISuccess('Activity', 'create', $params); | |
761 | ||
762 | // Check that from address is in "Source-Display-Name <source-email>" | |
763 | $formAddress = CRM_Case_BAO_Case::getReceiptFrom($activity['id']); | |
764 | $expectedFromAddress = sprintf("%s <%s>", $sourceDisplayName, $sourceContactParams['email']); | |
765 | $this->assertEquals($expectedFromAddress, $formAddress); | |
3cf1fae9 | 766 | |
767 | // Case 2: System Default From Address | |
768 | // but first erase the email address of existing source contact ID | |
769 | $withoutEmailParams = array( | |
770 | 'email' => '', | |
771 | ); | |
772 | $sourceContactID = $this->individualCreate($withoutEmailParams); | |
773 | $params = array( | |
774 | 'source_contact_id' => $sourceContactID, | |
775 | 'subject' => 'Scheduling Meeting ' . substr(sha1(rand()), 0, 4), | |
776 | 'activity_type_id' => CRM_Core_PseudoConstant::getKey('CRM_Activity_BAO_Activity', 'activity_type_id', 'Meeting'), | |
777 | 'activity_date_time' => date('Ymd'), | |
778 | ); | |
779 | $activity = $this->callAPISuccess('Activity', 'create', $params); | |
780 | // fetch domain info | |
781 | $domainInfo = $this->callAPISuccess('Domain', 'getsingle', array('id' => CRM_Core_Config::domainID())); | |
782 | ||
783 | $formAddress = CRM_Case_BAO_Case::getReceiptFrom($activity['id']); | |
784 | if (!empty($domainInfo['from_email'])) { | |
785 | $expectedFromAddress = sprintf("%s <%s>", $domainInfo['from_name'], $domainInfo['from_email']); | |
786 | } | |
787 | // Case 3: fetch default Organization Contact email address | |
788 | elseif (!empty($domainInfo['domain_email'])) { | |
789 | $expectedFromAddress = sprintf("%s <%s>", $domainInfo['name'], $domainInfo['domain_email']); | |
790 | } | |
e88906fc | 791 | $this->assertEquals($expectedFromAddress, $formAddress); |
3cf1fae9 | 792 | |
793 | // TODO: Case 4 about checking the $formAddress on basis of logged contact ID respectively needs, | |
794 | // to change the domain setting, which isn't straight forward in test environment | |
795 | } | |
796 | ||
75d842f8 | 797 | /** |
798 | * Set up for testing activity queries. | |
799 | */ | |
800 | protected function setUpForActivityDashboardTests() { | |
801 | $op = new PHPUnit_Extensions_Database_Operation_Insert(); | |
802 | $op->execute($this->_dbconn, | |
803 | $this->createFlatXMLDataSet( | |
804 | dirname(__FILE__) . '/activities_for_dashboard_count.xml' | |
805 | ) | |
806 | ); | |
807 | ||
808 | $this->_params = array( | |
809 | 'contact_id' => NULL, | |
810 | 'admin' => TRUE, | |
811 | 'caseId' => NULL, | |
812 | 'context' => 'home', | |
813 | 'activity_type_id' => NULL, | |
814 | 'offset' => 0, | |
815 | 'rowCount' => 0, | |
816 | 'sort' => NULL, | |
817 | ); | |
818 | } | |
819 | ||
824989b9 AS |
820 | public function testSendEmailBasic() { |
821 | $contactId = $this->individualCreate(); | |
822 | ||
823 | // create a logged in USER since the code references it for sendEmail user. | |
824 | $this->createLoggedInUser(); | |
825 | $session = CRM_Core_Session::singleton(); | |
826 | $loggedInUser = $session->get('userID'); | |
827 | ||
828 | $contact = $this->civicrm_api('contact', 'getsingle', array('id' => $contactId, 'version' => $this->_apiversion)); | |
829 | $contactDetailsIntersectKeys = array( | |
830 | 'contact_id' => '', | |
831 | 'sort_name' => '', | |
832 | 'display_name' => '', | |
833 | 'do_not_email' => '', | |
834 | 'preferred_mail_format' => '', | |
835 | 'is_deceased' => '', | |
836 | 'email' => '', | |
837 | 'on_hold' => '', | |
838 | ); | |
839 | $contactDetails = array( | |
840 | array_intersect_key($contact, $contactDetailsIntersectKeys), | |
841 | ); | |
842 | ||
843 | $subject = __FUNCTION__ . ' subject'; | |
844 | $html = __FUNCTION__ . ' html'; | |
845 | $text = __FUNCTION__ . ' text'; | |
846 | $userID = $loggedInUser; | |
847 | ||
848 | list($sent, $activity_id) = $email_result = CRM_Activity_BAO_Activity::sendEmail( | |
849 | $contactDetails, | |
850 | $subject, | |
851 | $text, | |
852 | $html, | |
853 | $contact['email'], | |
854 | $userID, | |
855 | $from = __FUNCTION__ . '@example.com' | |
856 | ); | |
857 | ||
858 | $activity = $this->civicrm_api('activity', 'getsingle', array('id' => $activity_id, 'version' => $this->_apiversion)); | |
859 | $details = "-ALTERNATIVE ITEM 0- | |
860 | $html | |
861 | -ALTERNATIVE ITEM 1- | |
862 | $text | |
863 | -ALTERNATIVE END- | |
864 | "; | |
865 | $this->assertEquals($activity['details'], $details, 'Activity details does not match.'); | |
866 | $this->assertEquals($activity['subject'], $subject, 'Activity subject does not match.'); | |
867 | } | |
868 | ||
869 | public function testSendEmailWithCampaign() { | |
870 | // Create a contact and contactDetails array. | |
871 | $contactId = $this->individualCreate(); | |
872 | ||
873 | // create a logged in USER since the code references it for sendEmail user. | |
874 | $this->createLoggedInUser(); | |
875 | $session = CRM_Core_Session::singleton(); | |
876 | $loggedInUser = $session->get('userID'); | |
877 | ||
878 | $contact = $this->civicrm_api('contact', 'getsingle', array('id' => $contactId, 'version' => $this->_apiversion)); | |
879 | $contactDetailsIntersectKeys = array( | |
880 | 'contact_id' => '', | |
881 | 'sort_name' => '', | |
882 | 'display_name' => '', | |
883 | 'do_not_email' => '', | |
884 | 'preferred_mail_format' => '', | |
885 | 'is_deceased' => '', | |
886 | 'email' => '', | |
887 | 'on_hold' => '', | |
888 | ); | |
889 | $contactDetails = array( | |
890 | array_intersect_key($contact, $contactDetailsIntersectKeys), | |
891 | ); | |
892 | ||
893 | // Create a campaign. | |
894 | $result = $this->civicrm_api('Campaign', 'create', array( | |
895 | 'version' => $this->_apiversion, | |
896 | 'title' => __FUNCTION__ . ' campaign', | |
897 | )); | |
898 | $campaign_id = $result['id']; | |
899 | ||
900 | $subject = __FUNCTION__ . ' subject'; | |
901 | $html = __FUNCTION__ . ' html'; | |
902 | $text = __FUNCTION__ . ' text'; | |
903 | $userID = $loggedInUser; | |
904 | ||
905 | list($sent, $activity_id) = $email_result = CRM_Activity_BAO_Activity::sendEmail( | |
906 | $contactDetails, | |
907 | $subject, | |
908 | $text, | |
909 | $html, | |
910 | $contact['email'], | |
911 | $userID, | |
912 | $from = __FUNCTION__ . '@example.com', | |
913 | $attachments = NULL, | |
914 | $cc = NULL, | |
915 | $bcc = NULL, | |
916 | $contactIds = NULL, | |
917 | $additionalDetails = NULL, | |
918 | $campaign_id | |
919 | ); | |
920 | $activity = $this->civicrm_api('activity', 'getsingle', array('id' => $activity_id, 'version' => $this->_apiversion)); | |
921 | $this->assertEquals($activity['campaign_id'], $campaign_id, 'Activity campaign_id does not match.'); | |
922 | } | |
923 | ||
6a488035 | 924 | } |