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 { |
39b959db | 8 | |
00be9182 | 9 | public function setUp() { |
6a488035 | 10 | parent::setUp(); |
cbcedb39 | 11 | $this->prepareForACLs(); |
12 | CRM_Core_Config::singleton()->userPermissionClass->permissions = array('view all contacts', 'access CiviCRM'); | |
1a7f0799 | 13 | $this->setupForSmsTests(); |
6a488035 TO |
14 | } |
15 | ||
cbcedb39 | 16 | /** |
17 | * Clean up after tests. | |
18 | */ | |
00be9182 | 19 | public function tearDown() { |
824989b9 | 20 | $tablesToTruncate = array( |
824989b9 AS |
21 | 'civicrm_activity', |
22 | 'civicrm_activity_contact', | |
23 | 'civicrm_uf_match', | |
24 | 'civicrm_campaign', | |
25 | 'civicrm_email', | |
26 | ); | |
6a488035 | 27 | $this->quickCleanup($tablesToTruncate); |
cbcedb39 | 28 | $this->cleanUpAfterACLs(); |
1a7f0799 | 29 | $this->setupForSmsTests(TRUE); |
cbcedb39 | 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::getActivitiesCount($this->_params); |
75d842f8 | 294 | $this->assertEquals(8, $activityCount); |
6a488035 TO |
295 | } |
296 | ||
297 | /** | |
5161bb0c | 298 | * Test getActivities BAO method for getting count |
6a488035 | 299 | */ |
00be9182 | 300 | public function testGetActivitiesCountforNonAdminDashboard() { |
a6c2ebdc | 301 | $this->createTestActivities(); |
6a488035 TO |
302 | |
303 | $params = array( | |
304 | 'contact_id' => 9, | |
305 | 'admin' => FALSE, | |
306 | 'caseId' => NULL, | |
307 | 'context' => 'home', | |
308 | 'activity_type_id' => NULL, | |
39b959db SL |
309 | // for dashlet the Scheduled status is set by default |
310 | 'activity_status_id' => CRM_Core_PseudoConstant::getKey('CRM_Activity_BAO_Activity', 'status_id', 'Scheduled'), | |
6a488035 TO |
311 | 'offset' => 0, |
312 | 'rowCount' => 0, | |
313 | 'sort' => NULL, | |
314 | ); | |
315 | ||
6a488035 TO |
316 | //since we are loading activities from dataset, we know total number of activities for this contact |
317 | // 5 activities ( 2 scheduled, 3 Completed ), note that dashboard shows only scheduled activities | |
6ab43e1b | 318 | $this->assertEquals(2, CRM_Activity_BAO_Activity::getActivitiesCount($params)); |
6a488035 TO |
319 | } |
320 | ||
321 | /** | |
5161bb0c | 322 | * Test getActivities BAO method for getting count |
6a488035 | 323 | */ |
00be9182 | 324 | public function testGetActivitiesCountforContactSummary() { |
a6c2ebdc | 325 | $this->createTestActivities(); |
6a488035 TO |
326 | |
327 | $params = array( | |
328 | 'contact_id' => 9, | |
329 | 'admin' => FALSE, | |
330 | 'caseId' => NULL, | |
331 | 'context' => 'activity', | |
332 | 'activity_type_id' => NULL, | |
333 | 'offset' => 0, | |
334 | 'rowCount' => 0, | |
335 | 'sort' => NULL, | |
336 | ); | |
6a488035 TO |
337 | |
338 | //since we are loading activities from dataset, we know total number of activities for this contact | |
339 | // 5 activities, Contact Summary should show all activities | |
6ab43e1b | 340 | $this->assertEquals(5, CRM_Activity_BAO_Activity::getActivitiesCount($params)); |
6a488035 TO |
341 | } |
342 | ||
23289ddd | 343 | /** |
344 | * CRM-18706 - Test Include/Exclude Activity Filters | |
345 | */ | |
346 | public function testActivityFilters() { | |
a6c2ebdc | 347 | $this->createTestActivities(); |
c3a9ccbb JP |
348 | Civi::settings()->set('preserve_activity_tab_filter', 1); |
349 | $this->createLoggedInUser(); | |
23289ddd | 350 | |
351 | global $_GET; | |
352 | $_GET = array( | |
353 | 'cid' => 9, | |
354 | 'context' => 'activity', | |
355 | 'activity_type_id' => 1, | |
356 | 'is_unit_test' => 1, | |
357 | ); | |
c3a9ccbb JP |
358 | $expectedFilters = array( |
359 | 'activity_type_filter_id' => 1, | |
c3a9ccbb | 360 | ); |
23289ddd | 361 | |
c3a9ccbb JP |
362 | list($activities, $activityFilter) = CRM_Activity_Page_AJAX::getContactActivity(); |
363 | //Assert whether filters are correctly set. | |
364 | $this->checkArrayEquals($expectedFilters, $activityFilter); | |
23289ddd | 365 | // This should include activities of type Meeting only. |
cbcedb39 | 366 | foreach ($activities['data'] as $value) { |
8c99c0bb | 367 | $this->assertContains('Meeting', $value['activity_type']); |
23289ddd | 368 | } |
369 | unset($_GET['activity_type_id']); | |
370 | ||
c3a9ccbb JP |
371 | $_GET['activity_type_exclude_id'] = $expectedFilters['activity_type_exclude_filter_id'] = 1; |
372 | list($activities, $activityFilter) = CRM_Activity_Page_AJAX::getContactActivity(); | |
c1d3e301 | 373 | $this->assertEquals(['activity_type_exclude_filter_id' => 1], $activityFilter); |
23289ddd | 374 | // None of the activities should be of type Meeting. |
cbcedb39 | 375 | foreach ($activities['data'] as $value) { |
c3a9ccbb | 376 | $this->assertNotContains('Meeting', $value['activity_type']); |
23289ddd | 377 | } |
378 | } | |
379 | ||
6a488035 | 380 | /** |
5161bb0c | 381 | * Test getActivities BAO method for getting count |
6a488035 | 382 | */ |
00be9182 | 383 | public function testGetActivitiesCountforContactSummaryWithNoActivities() { |
a6c2ebdc | 384 | $this->createTestActivities(); |
6a488035 TO |
385 | |
386 | $params = array( | |
387 | 'contact_id' => 17, | |
388 | 'admin' => FALSE, | |
389 | 'caseId' => NULL, | |
390 | 'context' => 'home', | |
391 | 'activity_type_id' => NULL, | |
392 | 'offset' => 0, | |
393 | 'rowCount' => 0, | |
394 | 'sort' => NULL, | |
395 | ); | |
6a488035 TO |
396 | |
397 | //since we are loading activities from dataset, we know total number of activities for this contact | |
398 | // this contact does not have any activity | |
6ab43e1b | 399 | $this->assertEquals(0, CRM_Activity_BAO_Activity::getActivitiesCount($params)); |
6a488035 TO |
400 | } |
401 | ||
402 | /** | |
eceb18cc | 403 | * Test getActivities BAO method. |
6a488035 | 404 | */ |
cbcedb39 | 405 | public function testGetActivitiesForAdminDashboard() { |
75d842f8 | 406 | $this->setUpForActivityDashboardTests(); |
6ab43e1b | 407 | $activitiesNew = CRM_Activity_BAO_Activity::getActivities($this->_params); |
408 | // $this->assertEquals($activities, $activitiesDeprecatedFn); | |
6a488035 TO |
409 | |
410 | //since we are loading activities from dataset, we know total number of activities | |
5161bb0c | 411 | // with no contact ID and there should be 8 schedule activities shown on dashboard |
6a488035 | 412 | $count = 8; |
6e793248 | 413 | foreach (array($activitiesNew) as $activities) { |
6ab43e1b | 414 | $this->assertEquals($count, count($activities)); |
6a488035 | 415 | |
6ab43e1b | 416 | foreach ($activities as $key => $value) { |
417 | $this->assertEquals($value['subject'], "subject {$key}", 'Verify activity subject is correct.'); | |
418 | $this->assertEquals($value['activity_type_id'], 2, 'Verify activity type is correct.'); | |
419 | $this->assertEquals($value['status_id'], 1, 'Verify all activities are scheduled.'); | |
420 | } | |
6a488035 TO |
421 | } |
422 | } | |
423 | ||
75d842f8 | 424 | /** |
425 | * Test getActivities BAO method. | |
426 | */ | |
427 | public function testGetActivitiesForAdminDashboardNoViewContacts() { | |
428 | CRM_Core_Config::singleton()->userPermissionClass->permissions = array('access CiviCRM'); | |
429 | $this->setUpForActivityDashboardTests(); | |
6e793248 | 430 | foreach (array(CRM_Activity_BAO_Activity::getActivities($this->_params)) as $activities) { |
6ab43e1b | 431 | // Skipped until we get back to the upgraded version properly. |
6e793248 | 432 | $this->assertEquals(0, count($activities)); |
6ab43e1b | 433 | } |
26583d3e | 434 | } |
435 | ||
436 | /** | |
437 | * Test getActivities BAO method. | |
438 | */ | |
439 | public function testGetActivitiesForAdminDashboardAclLimitedViewContacts() { | |
440 | CRM_Core_Config::singleton()->userPermissionClass->permissions = array('access CiviCRM'); | |
441 | $this->allowedContacts = array(1, 3, 4, 5); | |
442 | $this->hookClass->setHook('civicrm_aclWhereClause', array($this, 'aclWhereMultipleContacts')); | |
443 | $this->setUpForActivityDashboardTests(); | |
6e793248 | 444 | $this->assertEquals(7, count(CRM_Activity_BAO_Activity::getActivities($this->_params))); |
75d842f8 | 445 | } |
446 | ||
6a488035 | 447 | /** |
eceb18cc | 448 | * Test getActivities BAO method. |
6a488035 | 449 | */ |
00be9182 | 450 | public function testGetActivitiesforNonAdminDashboard() { |
a6c2ebdc | 451 | $this->createTestActivities(); |
6a488035 TO |
452 | |
453 | $contactID = 9; | |
454 | $params = array( | |
455 | 'contact_id' => $contactID, | |
456 | 'admin' => FALSE, | |
457 | 'caseId' => NULL, | |
458 | 'context' => 'home', | |
459 | 'activity_type_id' => NULL, | |
39b959db SL |
460 | // for dashlet the Scheduled status is set by default |
461 | 'activity_status_id' => CRM_Core_PseudoConstant::getKey('CRM_Activity_BAO_Activity', 'status_id', 'Scheduled'), | |
6a488035 TO |
462 | 'offset' => 0, |
463 | 'rowCount' => 0, | |
464 | 'sort' => NULL, | |
465 | ); | |
6a488035 | 466 | |
6e793248 | 467 | foreach (array(CRM_Activity_BAO_Activity::getActivities($params)) as $activities) { |
6ab43e1b | 468 | //since we are loading activities from dataset, we know total number of activities for this contact |
469 | // 5 activities ( 2 scheduled, 3 Completed ), note that dashboard shows only scheduled activities | |
470 | $count = 2; | |
471 | $this->assertEquals($count, count($activities)); | |
6a488035 | 472 | |
6ab43e1b | 473 | foreach ($activities as $key => $value) { |
474 | $this->assertEquals($value['subject'], "subject {$key}", 'Verify activity subject is correct.'); | |
475 | $this->assertEquals($value['activity_type_id'], 2, 'Verify activity type is correct.'); | |
476 | $this->assertEquals($value['status_id'], 1, 'Verify all activities are scheduled.'); | |
6a488035 | 477 | |
6ab43e1b | 478 | if ($key == 3) { |
479 | $this->assertArrayHasKey($contactID, $value['target_contact_name']); | |
480 | } | |
481 | elseif ($key == 4) { | |
482 | $this->assertArrayHasKey($contactID, $value['assignee_contact_name']); | |
483 | } | |
6a488035 TO |
484 | } |
485 | } | |
486 | } | |
487 | ||
36d2f4d5 | 488 | /** |
489 | * Test target contact count. | |
490 | */ | |
491 | public function testTargetCountforContactSummary() { | |
492 | $targetCount = 5; | |
493 | $contactId = $this->individualCreate(); | |
cbcedb39 | 494 | $targetContactIDs = array(); |
36d2f4d5 | 495 | for ($i = 0; $i < $targetCount; $i++) { |
496 | $targetContactIDs[] = $this->individualCreate(array(), $i); | |
497 | } | |
cbcedb39 | 498 | // Create activities with 5 target contacts. |
36d2f4d5 | 499 | $activityParams = array( |
500 | 'source_contact_id' => $contactId, | |
501 | 'target_contact_id' => $targetContactIDs, | |
502 | ); | |
503 | $this->activityCreate($activityParams); | |
504 | ||
505 | $params = array( | |
506 | 'contact_id' => $contactId, | |
507 | 'context' => 'activity', | |
508 | ); | |
c2ce41b6 | 509 | $activities = CRM_Activity_BAO_Activity::getActivities($params); |
510 | //verify target count | |
511 | $this->assertEquals($targetCount, $activities[1]['target_contact_count']); | |
512 | $this->assertEquals([$targetContactIDs[0] => 'Anderson, Anthony'], $activities[1]['target_contact_name']); | |
513 | $this->assertEquals('Anderson, Anthony', $activities[1]['source_contact_name']); | |
514 | $this->assertEquals('Anderson, Anthony', $activities[1]['assignee_contact_name'][4]); | |
36d2f4d5 | 515 | } |
516 | ||
a6c2ebdc | 517 | /** |
518 | * Test getActivities BAO method. | |
519 | */ | |
520 | public function testGetActivitiesforContactSummaryWithSortOptions() { | |
521 | $this->createTestActivities(); | |
522 | $params = [ | |
523 | 'contact_id' => 9, | |
524 | 'admin' => FALSE, | |
525 | 'caseId' => NULL, | |
526 | 'context' => 'activity', | |
527 | 'activity_type_id' => NULL, | |
528 | 'offset' => 0, | |
529 | 'rowCount' => 0, | |
530 | 'sort' => 'source_contact_name desc', | |
531 | ]; | |
532 | ||
533 | $activities = CRM_Activity_BAO_Activity::getActivities($params); | |
534 | $alphaOrder = ['Test Contact 11', 'Test Contact 12', 'Test Contact 3', 'Test Contact 4', 'Test Contact 9']; | |
535 | foreach ($activities as $activity) { | |
536 | $this->assertEquals(array_pop($alphaOrder), $activity['source_contact_name']); | |
537 | } | |
538 | ||
539 | } | |
540 | ||
6a488035 | 541 | /** |
eceb18cc | 542 | * Test getActivities BAO method. |
6a488035 | 543 | */ |
c2ce41b6 | 544 | public function testGetActivitiesForContactSummary() { |
a6c2ebdc | 545 | $this->createTestActivities(); |
6a488035 TO |
546 | |
547 | $contactID = 9; | |
548 | $params = array( | |
549 | 'contact_id' => $contactID, | |
550 | 'admin' => FALSE, | |
551 | 'caseId' => NULL, | |
552 | 'context' => 'activity', | |
553 | 'activity_type_id' => NULL, | |
554 | 'offset' => 0, | |
555 | 'rowCount' => 0, | |
6a488035 | 556 | ); |
6a488035 TO |
557 | |
558 | //since we are loading activities from dataset, we know total number of activities for this contact | |
559 | // 5 activities, Contact Summary should show all activities | |
560 | $count = 5; | |
c2ce41b6 | 561 | $activities = CRM_Activity_BAO_Activity::getActivities($params); |
562 | $this->assertEquals($count, count($activities)); | |
563 | foreach ($activities as $key => $value) { | |
564 | $this->assertEquals($value['subject'], "subject {$key}", 'Verify activity subject is correct.'); | |
6a488035 | 565 | |
c2ce41b6 | 566 | if ($key > 8) { |
567 | $this->assertEquals($value['status_id'], 2, 'Verify all activities are scheduled.'); | |
568 | } | |
569 | else { | |
570 | $this->assertEquals($value['status_id'], 1, 'Verify all activities are scheduled.'); | |
571 | } | |
6a488035 | 572 | |
c2ce41b6 | 573 | if ($key === 12) { |
574 | $this->assertEquals($value['activity_type'], 'Bulk Email', 'Verify activity type is correct.'); | |
575 | $this->assertEquals('(2 recipients)', $value['recipients']); | |
576 | $targetContactID = key($value['target_contact_name']); | |
577 | // The 2 targets have ids 10 & 11. Since they are not sorted it could be either on some systems. | |
578 | $this->assertTrue(in_array($targetContactID, [10, 11])); | |
579 | } | |
580 | elseif ($key > 8) { | |
581 | $this->assertEquals($value['activity_type_id'], 1, 'Verify activity type is correct.'); | |
582 | } | |
583 | else { | |
584 | $this->assertEquals($value['activity_type_id'], 2, 'Verify activity type is correct.'); | |
585 | } | |
6ab43e1b | 586 | |
c2ce41b6 | 587 | if ($key == 3) { |
588 | $this->assertEquals([$contactID => 'Test Contact ' . $contactID], $value['target_contact_name']); | |
589 | } | |
590 | elseif ($key == 4) { | |
591 | $this->assertArrayHasKey($contactID, $value['assignee_contact_name']); | |
6a488035 TO |
592 | } |
593 | } | |
594 | } | |
595 | ||
596 | /** | |
eceb18cc | 597 | * Test getActivities BAO method. |
6a488035 | 598 | */ |
5161bb0c | 599 | public function testGetActivitiesforContactSummaryWithActivities() { |
a6c2ebdc | 600 | $this->createTestActivities(); |
6a488035 | 601 | |
6ab43e1b | 602 | // parameters for different test cases, check each array key for the specific test-case |
5161bb0c | 603 | $testCases = array( |
604 | 'with-no-activity' => array( | |
605 | 'params' => array( | |
606 | 'contact_id' => 17, | |
607 | 'admin' => FALSE, | |
608 | 'caseId' => NULL, | |
609 | 'context' => 'home', | |
610 | 'activity_type_id' => NULL, | |
611 | 'offset' => 0, | |
612 | 'rowCount' => 0, | |
613 | 'sort' => NULL, | |
614 | ), | |
615 | ), | |
616 | 'with-activity' => array( | |
617 | 'params' => array( | |
618 | 'contact_id' => 1, | |
619 | 'admin' => FALSE, | |
620 | 'caseId' => NULL, | |
621 | 'context' => 'home', | |
622 | 'activity_type_id' => NULL, | |
623 | 'offset' => 0, | |
624 | 'rowCount' => 0, | |
625 | 'sort' => NULL, | |
626 | ), | |
627 | ), | |
628 | 'with-activity_type' => array( | |
629 | 'params' => array( | |
630 | 'contact_id' => 3, | |
631 | 'admin' => FALSE, | |
632 | 'caseId' => NULL, | |
633 | 'context' => 'home', | |
634 | 'activity_type_id' => 2, | |
635 | 'offset' => 0, | |
636 | 'rowCount' => 0, | |
637 | 'sort' => NULL, | |
638 | ), | |
639 | ), | |
640 | 'exclude-all-activity_type' => array( | |
641 | 'params' => array( | |
642 | 'contact_id' => 3, | |
643 | 'admin' => FALSE, | |
644 | 'caseId' => NULL, | |
645 | 'context' => 'home', | |
646 | 'activity_type_exclude_id' => array(1, 2), | |
647 | 'offset' => 0, | |
648 | 'rowCount' => 0, | |
649 | 'sort' => NULL, | |
650 | ), | |
651 | ), | |
652 | 'sort-by-subject' => array( | |
653 | 'params' => array( | |
654 | 'contact_id' => 1, | |
655 | 'admin' => FALSE, | |
656 | 'caseId' => NULL, | |
657 | 'context' => 'home', | |
658 | 'activity_type_id' => NULL, | |
659 | 'offset' => 0, | |
660 | 'rowCount' => 0, | |
661 | 'sort' => 'subject DESC', | |
662 | ), | |
663 | ), | |
664 | ); | |
665 | ||
666 | foreach ($testCases as $caseName => $testCase) { | |
6e793248 | 667 | $activityCount = CRM_Activity_BAO_Activity::getActivitiesCount($testCase['params']); |
6ab43e1b | 668 | $activitiesNew = CRM_Activity_BAO_Activity::getActivities($testCase['params']); |
669 | ||
6e793248 | 670 | foreach (array($activitiesNew) as $activities) { |
6ab43e1b | 671 | //$this->assertEquals($activityCount, CRM_Activity_BAO_Activity::getActivitiesCount($testCase['params'])); |
672 | if ($caseName == 'with-no-activity') { | |
673 | $this->assertEquals(0, count($activities)); | |
674 | $this->assertEquals(0, $activityCount); | |
675 | } | |
676 | elseif ($caseName == 'with-activity') { | |
677 | // contact id 1 is assigned as source, target and assignee for activity id 1, 7 and 8 respectively | |
678 | $this->assertEquals(3, count($activities)); | |
679 | $this->assertEquals(3, $activityCount); | |
680 | $this->assertEquals(1, $activities[1]['source_contact_id']); | |
681 | // @todo - this is a discrepancy between old & new - review. | |
682 | //$this->assertEquals(TRUE, array_key_exists(1, $activities[7]['target_contact_name'])); | |
683 | $this->assertEquals(TRUE, array_key_exists(1, $activities[8]['assignee_contact_name'])); | |
684 | } | |
685 | elseif ($caseName == 'with-activity_type') { | |
686 | // contact id 3 for activity type 2 is assigned as assignee, source and target for | |
687 | // activity id 1, 3 and 8 respectively | |
688 | $this->assertEquals(3, count($activities)); | |
689 | $this->assertEquals(3, $activityCount); | |
690 | // ensure activity type id is 2 | |
691 | $this->assertEquals(2, $activities[1]['activity_type_id']); | |
692 | $this->assertEquals(3, $activities[3]['source_contact_id']); | |
693 | // @todo review inconsistency between 2 versions. | |
694 | // $this->assertEquals(TRUE, array_key_exists(3, $activities[8]['target_contact_name'])); | |
695 | $this->assertEquals(TRUE, array_key_exists(3, $activities[1]['assignee_contact_name'])); | |
696 | } | |
697 | if ($caseName == 'exclude-all-activity_type') { | |
698 | $this->assertEquals(0, count($activities)); | |
699 | $this->assertEquals(0, $activityCount); | |
700 | } | |
701 | if ($caseName == 'sort-by-subject') { | |
702 | $this->assertEquals(3, count($activities)); | |
703 | $this->assertEquals(3, $activityCount); | |
704 | // activities should be order by 'subject DESC' | |
705 | $subjectOrder = array( | |
706 | 'subject 8', | |
707 | 'subject 7', | |
708 | 'subject 1', | |
709 | ); | |
710 | $count = 0; | |
711 | foreach ($activities as $activity) { | |
712 | $this->assertEquals($subjectOrder[$count], $activity['subject']); | |
713 | $count++; | |
714 | } | |
5161bb0c | 715 | } |
716 | } | |
717 | } | |
6a488035 | 718 | } |
96025800 | 719 | |
49d4d222 | 720 | /** |
721 | * CRM-20793 : Test getActivities by using activity date and status filter | |
722 | */ | |
afc82ed3 | 723 | public function testByActivityDateAndStatus() { |
6e793248 | 724 | CRM_Core_Config::singleton()->userPermissionClass->permissions = ['view all contacts', 'access CiviCRM']; |
a6c2ebdc | 725 | $this->createTestActivities(); |
49d4d222 | 726 | |
727 | // activity IDs catagorised by date | |
728 | $lastWeekActivities = array(1, 2, 3); | |
729 | $todayActivities = array(4, 5, 6, 7); | |
a6d192c8 | 730 | $lastTwoMonthsActivities = array(8, 9, 10, 11); |
daff7f26 | 731 | $lastOrNextYearActivities = array(12, 13, 14, 15, 16); |
49d4d222 | 732 | |
733 | // date values later used to set activity date value | |
734 | $lastWeekDate = date('YmdHis', strtotime('1 week ago')); | |
735 | $today = date('YmdHis'); | |
a6d192c8 | 736 | $lastTwoMonthAgoDate = date('YmdHis', strtotime('2 months ago')); |
daff7f26 | 737 | // if current month is Jan then choose next year date otherwise the search result will include |
738 | // the previous week and last two months activities which are still in previous year and hence leads to improper result | |
739 | $lastOrNextYearDate = (date('M') == 'Jan') ? date('YmdHis', strtotime('+1 year')) : date('YmdHis', strtotime('1 year ago')); | |
49d4d222 | 740 | for ($i = 1; $i <= 16; $i++) { |
741 | if (in_array($i, $lastWeekActivities)) { | |
742 | $date = $lastWeekDate; | |
743 | } | |
a6d192c8 | 744 | elseif (in_array($i, $lastTwoMonthsActivities)) { |
745 | $date = $lastTwoMonthAgoDate; | |
49d4d222 | 746 | } |
daff7f26 | 747 | elseif (in_array($i, $lastOrNextYearActivities)) { |
748 | $date = $lastOrNextYearDate; | |
49d4d222 | 749 | } |
750 | elseif (in_array($i, $todayActivities)) { | |
751 | $date = $today; | |
752 | } | |
753 | $this->callAPISuccess('Activity', 'create', array( | |
754 | 'id' => $i, | |
755 | 'activity_date_time' => $date, | |
756 | )); | |
757 | } | |
758 | ||
759 | // parameters for different test cases, check each array key for the specific test-case | |
760 | $testCases = array( | |
761 | 'todays-activity' => array( | |
762 | 'params' => array( | |
763 | 'contact_id' => 1, | |
764 | 'admin' => TRUE, | |
765 | 'caseId' => NULL, | |
766 | 'context' => 'activity', | |
6e793248 | 767 | 'activity_date_time_relative' => 'this.day', |
49d4d222 | 768 | 'activity_type_id' => NULL, |
769 | 'offset' => 0, | |
770 | 'rowCount' => 0, | |
771 | 'sort' => NULL, | |
772 | ), | |
773 | ), | |
774 | 'todays-activity-filtered-by-range' => array( | |
775 | 'params' => array( | |
776 | 'contact_id' => 1, | |
777 | 'admin' => TRUE, | |
778 | 'caseId' => NULL, | |
779 | 'context' => 'activity', | |
6e793248 | 780 | 'activity_date_time_low' => date('Y/m/d', strtotime('yesterday')), |
781 | 'activity_date_time_high' => date('Y/m/d'), | |
49d4d222 | 782 | 'activity_type_id' => NULL, |
783 | 'offset' => 0, | |
784 | 'rowCount' => 0, | |
785 | 'sort' => NULL, | |
786 | ), | |
787 | ), | |
788 | 'last-week-activity' => array( | |
789 | 'params' => array( | |
790 | 'contact_id' => 1, | |
791 | 'admin' => TRUE, | |
792 | 'caseId' => NULL, | |
793 | 'context' => 'activity', | |
6e793248 | 794 | 'activity_date_time_relative' => 'previous.week', |
49d4d222 | 795 | 'activity_type_id' => NULL, |
796 | 'offset' => 0, | |
797 | 'rowCount' => 0, | |
798 | 'sort' => NULL, | |
799 | ), | |
800 | ), | |
a6d192c8 | 801 | 'this-quarter-activity' => array( |
49d4d222 | 802 | 'params' => array( |
803 | 'contact_id' => 1, | |
804 | 'admin' => TRUE, | |
805 | 'caseId' => NULL, | |
806 | 'context' => 'activity', | |
6e793248 | 807 | 'activity_date_time_relative' => 'this.quarter', |
49d4d222 | 808 | 'activity_type_id' => NULL, |
809 | 'offset' => 0, | |
810 | 'rowCount' => 0, | |
811 | 'sort' => NULL, | |
812 | ), | |
813 | ), | |
49d4d222 | 814 | 'activity-of-all-statuses' => array( |
815 | 'params' => array( | |
816 | 'contact_id' => 1, | |
817 | 'admin' => TRUE, | |
818 | 'caseId' => NULL, | |
819 | 'context' => 'activity', | |
820 | 'activity_status_id' => '1,2', | |
821 | 'activity_type_id' => NULL, | |
822 | 'offset' => 0, | |
823 | 'rowCount' => 0, | |
824 | 'sort' => NULL, | |
825 | ), | |
826 | ), | |
827 | ); | |
828 | ||
829 | foreach ($testCases as $caseName => $testCase) { | |
6e793248 | 830 | CRM_Utils_Date::convertFormDateToApiFormat($testCase['params'], 'activity_date_time', FALSE); |
831 | $activities = CRM_Activity_BAO_Activity::getActivities($testCase['params']); | |
832 | $activityCount = CRM_Activity_BAO_Activity::getActivitiesCount($testCase['params']); | |
833 | asort($activities); | |
834 | $activityIDs = array_keys($activities); | |
49d4d222 | 835 | |
836 | if ($caseName == 'todays-activity' || $caseName == 'todays-activity-filtered-by-range') { | |
6e793248 | 837 | // Only one of the 4 activities today relates to contact id 1. |
838 | $this->assertEquals(1, $activityCount); | |
839 | $this->assertEquals(1, count($activities)); | |
840 | $this->assertEquals([7], array_keys($activities)); | |
49d4d222 | 841 | } |
842 | elseif ($caseName == 'last-week-activity') { | |
6e793248 | 843 | // Only one of the 3 activities today relates to contact id 1. |
844 | $this->assertEquals(1, $activityCount); | |
845 | $this->assertEquals(1, count($activities)); | |
846 | $this->assertEquals([1], $activityIDs); | |
49d4d222 | 847 | } |
a6d192c8 | 848 | elseif ($caseName == 'lhis-quarter-activity') { |
849 | $this->assertEquals(count($lastTwoMonthsActivities), $activityCount); | |
6e793248 | 850 | $this->assertEquals(count($lastTwoMonthsActivities), count($activities)); |
a6d192c8 | 851 | $this->checkArrayEquals($lastTwoMonthsActivities, $activityIDs); |
49d4d222 | 852 | } |
daff7f26 | 853 | elseif ($caseName == 'last-or-next-year-activity') { |
854 | $this->assertEquals(count($lastOrNextYearActivities), $activityCount); | |
6e793248 | 855 | $this->assertEquals(count($lastOrNextYearActivities), count($activities)); |
daff7f26 | 856 | $this->checkArrayEquals($lastOrNextYearActivities, $activityIDs); |
49d4d222 | 857 | } |
858 | elseif ($caseName == 'activity-of-all-statuses') { | |
6e793248 | 859 | $this->assertEquals(3, $activityCount); |
860 | $this->assertEquals(3, count($activities)); | |
49d4d222 | 861 | } |
862 | } | |
863 | } | |
864 | ||
afc82ed3 | 865 | /** |
866 | * @dataProvider getActivityDateData | |
867 | */ | |
868 | public function testActivityRelativeDateFilter($params, $expected) { | |
869 | $thisYear = date('Y'); | |
870 | $dates = [ | |
871 | date('Y-m-d', strtotime(($thisYear - 1) . '-01-01')), | |
872 | date('Y-m-d', strtotime(($thisYear - 1) . '-12-31')), | |
873 | date('Y-m-d', strtotime($thisYear . '-01-01')), | |
874 | date('Y-m-d', strtotime($thisYear . '-12-31')), | |
875 | date('Y-m-d', strtotime(($thisYear + 1) . '-01-01')), | |
876 | date('Y-m-d', strtotime(($thisYear + 1) . '-12-31')), | |
877 | ]; | |
878 | foreach ($dates as $date) { | |
879 | $this->activityCreate(['activity_date_time' => $date]); | |
880 | } | |
6e793248 | 881 | $activitiesDep = CRM_Activity_BAO_Activity::getActivities($params); |
882 | $activityCount = CRM_Activity_BAO_Activity::getActivitiesCount($params); | |
afc82ed3 | 883 | $this->assertEquals(count($activitiesDep), $activityCount); |
884 | foreach ($activitiesDep as $activity) { | |
885 | $this->assertTrue(strtotime($activity['activity_date_time']) >= $expected['earliest'], $activity['activity_date_time'] . ' should be no earlier than ' . date('Y-m-d H:i:s', $expected['earliest'])); | |
886 | $this->assertTrue(strtotime($activity['activity_date_time']) < $expected['latest'], $activity['activity_date_time'] . ' should be before ' . date('Y-m-d H:i:s', $expected['latest'])); | |
887 | } | |
888 | ||
889 | } | |
890 | ||
891 | /** | |
892 | * Get activity date data. | |
893 | * | |
894 | * Later we might migrate rework the rest of | |
895 | * testByActivityDateAndStatus | |
896 | * to use data provider methodology as it's way complex! | |
897 | * | |
898 | * @return array | |
899 | */ | |
900 | public function getActivityDateData() { | |
901 | return [ | |
902 | 'last-year-activity' => [ | |
903 | 'params' => [ | |
904 | 'contact_id' => 1, | |
905 | 'admin' => TRUE, | |
906 | 'caseId' => NULL, | |
907 | 'context' => 'activity', | |
908 | 'activity_date_relative' => 'previous.year', | |
909 | 'activity_type_id' => NULL, | |
910 | 'offset' => 0, | |
911 | 'rowCount' => 0, | |
912 | 'sort' => NULL, | |
913 | ], | |
914 | 'expected' => [ | |
915 | 'count' => 2, | |
916 | 'earliest' => strtotime('first day of january last year'), | |
917 | 'latest' => strtotime('first day of january this year'), | |
39b959db | 918 | ], |
afc82ed3 | 919 | ], |
920 | ]; | |
921 | } | |
922 | ||
3cf1fae9 | 923 | /** |
924 | * CRM-20308: Test from email address when a 'copy of Activity' event occur | |
925 | */ | |
926 | public function testEmailAddressOfActivityCopy() { | |
927 | // Case 1: assert the 'From' Email Address of source Actvity Contact ID | |
928 | // create activity with source contact ID which has email address | |
929 | $assigneeContactId = $this->individualCreate(); | |
930 | $sourceContactParams = array( | |
931 | 'first_name' => 'liz', | |
932 | 'last_name' => 'hurleey', | |
933 | 'email' => substr(sha1(rand()), 0, 7) . '@testemail.com', | |
934 | ); | |
935 | $sourceContactID = $this->individualCreate($sourceContactParams); | |
936 | $sourceDisplayName = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $sourceContactID, 'display_name'); | |
937 | ||
938 | // create an activity using API | |
939 | $params = array( | |
940 | 'source_contact_id' => $sourceContactID, | |
941 | 'subject' => 'Scheduling Meeting ' . substr(sha1(rand()), 0, 4), | |
942 | 'activity_type_id' => CRM_Core_PseudoConstant::getKey('CRM_Activity_BAO_Activity', 'activity_type_id', 'Meeting'), | |
943 | 'assignee_contact_id' => array($assigneeContactId), | |
944 | 'activity_date_time' => date('Ymd'), | |
945 | ); | |
946 | $activity = $this->callAPISuccess('Activity', 'create', $params); | |
947 | ||
948 | // Check that from address is in "Source-Display-Name <source-email>" | |
949 | $formAddress = CRM_Case_BAO_Case::getReceiptFrom($activity['id']); | |
950 | $expectedFromAddress = sprintf("%s <%s>", $sourceDisplayName, $sourceContactParams['email']); | |
951 | $this->assertEquals($expectedFromAddress, $formAddress); | |
3cf1fae9 | 952 | |
953 | // Case 2: System Default From Address | |
954 | // but first erase the email address of existing source contact ID | |
955 | $withoutEmailParams = array( | |
956 | 'email' => '', | |
957 | ); | |
958 | $sourceContactID = $this->individualCreate($withoutEmailParams); | |
959 | $params = array( | |
960 | 'source_contact_id' => $sourceContactID, | |
961 | 'subject' => 'Scheduling Meeting ' . substr(sha1(rand()), 0, 4), | |
962 | 'activity_type_id' => CRM_Core_PseudoConstant::getKey('CRM_Activity_BAO_Activity', 'activity_type_id', 'Meeting'), | |
963 | 'activity_date_time' => date('Ymd'), | |
964 | ); | |
965 | $activity = $this->callAPISuccess('Activity', 'create', $params); | |
966 | // fetch domain info | |
967 | $domainInfo = $this->callAPISuccess('Domain', 'getsingle', array('id' => CRM_Core_Config::domainID())); | |
968 | ||
969 | $formAddress = CRM_Case_BAO_Case::getReceiptFrom($activity['id']); | |
970 | if (!empty($domainInfo['from_email'])) { | |
971 | $expectedFromAddress = sprintf("%s <%s>", $domainInfo['from_name'], $domainInfo['from_email']); | |
972 | } | |
973 | // Case 3: fetch default Organization Contact email address | |
974 | elseif (!empty($domainInfo['domain_email'])) { | |
975 | $expectedFromAddress = sprintf("%s <%s>", $domainInfo['name'], $domainInfo['domain_email']); | |
976 | } | |
e88906fc | 977 | $this->assertEquals($expectedFromAddress, $formAddress); |
3cf1fae9 | 978 | |
979 | // TODO: Case 4 about checking the $formAddress on basis of logged contact ID respectively needs, | |
980 | // to change the domain setting, which isn't straight forward in test environment | |
981 | } | |
982 | ||
75d842f8 | 983 | /** |
984 | * Set up for testing activity queries. | |
985 | */ | |
986 | protected function setUpForActivityDashboardTests() { | |
a6c2ebdc | 987 | $this->createTestActivities(); |
75d842f8 | 988 | |
989 | $this->_params = array( | |
990 | 'contact_id' => NULL, | |
991 | 'admin' => TRUE, | |
992 | 'caseId' => NULL, | |
993 | 'context' => 'home', | |
994 | 'activity_type_id' => NULL, | |
39b959db SL |
995 | // for dashlet the Scheduled status is set by default |
996 | 'activity_status_id' => CRM_Core_PseudoConstant::getKey('CRM_Activity_BAO_Activity', 'status_id', 'Scheduled'), | |
75d842f8 | 997 | 'offset' => 0, |
998 | 'rowCount' => 0, | |
999 | 'sort' => NULL, | |
1000 | ); | |
1001 | } | |
1002 | ||
824989b9 AS |
1003 | public function testSendEmailBasic() { |
1004 | $contactId = $this->individualCreate(); | |
1005 | ||
1006 | // create a logged in USER since the code references it for sendEmail user. | |
1007 | $this->createLoggedInUser(); | |
1008 | $session = CRM_Core_Session::singleton(); | |
1009 | $loggedInUser = $session->get('userID'); | |
1010 | ||
1011 | $contact = $this->civicrm_api('contact', 'getsingle', array('id' => $contactId, 'version' => $this->_apiversion)); | |
1012 | $contactDetailsIntersectKeys = array( | |
1013 | 'contact_id' => '', | |
1014 | 'sort_name' => '', | |
1015 | 'display_name' => '', | |
1016 | 'do_not_email' => '', | |
1017 | 'preferred_mail_format' => '', | |
1018 | 'is_deceased' => '', | |
1019 | 'email' => '', | |
1020 | 'on_hold' => '', | |
1021 | ); | |
1022 | $contactDetails = array( | |
1023 | array_intersect_key($contact, $contactDetailsIntersectKeys), | |
1024 | ); | |
1025 | ||
1026 | $subject = __FUNCTION__ . ' subject'; | |
1027 | $html = __FUNCTION__ . ' html'; | |
1028 | $text = __FUNCTION__ . ' text'; | |
1029 | $userID = $loggedInUser; | |
1030 | ||
1031 | list($sent, $activity_id) = $email_result = CRM_Activity_BAO_Activity::sendEmail( | |
1032 | $contactDetails, | |
1033 | $subject, | |
1034 | $text, | |
1035 | $html, | |
1036 | $contact['email'], | |
1037 | $userID, | |
1038 | $from = __FUNCTION__ . '@example.com' | |
1039 | ); | |
1040 | ||
1041 | $activity = $this->civicrm_api('activity', 'getsingle', array('id' => $activity_id, 'version' => $this->_apiversion)); | |
1042 | $details = "-ALTERNATIVE ITEM 0- | |
1043 | $html | |
1044 | -ALTERNATIVE ITEM 1- | |
1045 | $text | |
1046 | -ALTERNATIVE END- | |
1047 | "; | |
1048 | $this->assertEquals($activity['details'], $details, 'Activity details does not match.'); | |
1049 | $this->assertEquals($activity['subject'], $subject, 'Activity subject does not match.'); | |
1050 | } | |
1051 | ||
1052 | public function testSendEmailWithCampaign() { | |
1053 | // Create a contact and contactDetails array. | |
1054 | $contactId = $this->individualCreate(); | |
1055 | ||
1056 | // create a logged in USER since the code references it for sendEmail user. | |
1057 | $this->createLoggedInUser(); | |
1058 | $session = CRM_Core_Session::singleton(); | |
1059 | $loggedInUser = $session->get('userID'); | |
1060 | ||
1061 | $contact = $this->civicrm_api('contact', 'getsingle', array('id' => $contactId, 'version' => $this->_apiversion)); | |
1062 | $contactDetailsIntersectKeys = array( | |
1063 | 'contact_id' => '', | |
1064 | 'sort_name' => '', | |
1065 | 'display_name' => '', | |
1066 | 'do_not_email' => '', | |
1067 | 'preferred_mail_format' => '', | |
1068 | 'is_deceased' => '', | |
1069 | 'email' => '', | |
1070 | 'on_hold' => '', | |
1071 | ); | |
1072 | $contactDetails = array( | |
1073 | array_intersect_key($contact, $contactDetailsIntersectKeys), | |
1074 | ); | |
1075 | ||
1076 | // Create a campaign. | |
1077 | $result = $this->civicrm_api('Campaign', 'create', array( | |
1078 | 'version' => $this->_apiversion, | |
1079 | 'title' => __FUNCTION__ . ' campaign', | |
1080 | )); | |
1081 | $campaign_id = $result['id']; | |
1082 | ||
1083 | $subject = __FUNCTION__ . ' subject'; | |
1084 | $html = __FUNCTION__ . ' html'; | |
1085 | $text = __FUNCTION__ . ' text'; | |
1086 | $userID = $loggedInUser; | |
1087 | ||
1088 | list($sent, $activity_id) = $email_result = CRM_Activity_BAO_Activity::sendEmail( | |
1089 | $contactDetails, | |
1090 | $subject, | |
1091 | $text, | |
1092 | $html, | |
1093 | $contact['email'], | |
1094 | $userID, | |
1095 | $from = __FUNCTION__ . '@example.com', | |
1096 | $attachments = NULL, | |
1097 | $cc = NULL, | |
1098 | $bcc = NULL, | |
1099 | $contactIds = NULL, | |
1100 | $additionalDetails = NULL, | |
cb5d08cd | 1101 | NULL, |
824989b9 AS |
1102 | $campaign_id |
1103 | ); | |
1104 | $activity = $this->civicrm_api('activity', 'getsingle', array('id' => $activity_id, 'version' => $this->_apiversion)); | |
1105 | $this->assertEquals($activity['campaign_id'], $campaign_id, 'Activity campaign_id does not match.'); | |
1106 | } | |
1107 | ||
63483feb MM |
1108 | /** |
1109 | * @expectedException CRM_Core_Exception | |
1110 | * @expectedExceptionMessage You do not have the 'send SMS' permission | |
1111 | */ | |
1112 | public function testSendSMSWithoutPermission() { | |
1113 | $dummy = NULL; | |
1114 | $session = CRM_Core_Session::singleton(); | |
f3a87cf4 | 1115 | CRM_Core_Config::singleton()->userPermissionClass->permissions = array('access CiviCRM'); |
63483feb MM |
1116 | |
1117 | CRM_Activity_BAO_Activity::sendSMS( | |
1118 | $dummy, | |
1119 | $dummy, | |
1120 | $dummy, | |
1121 | $dummy, | |
1122 | $session->get('userID') | |
1123 | ); | |
1124 | } | |
1125 | ||
1a7f0799 MW |
1126 | public function testSendSmsNoPhoneNumber() { |
1127 | list($sent, $activityId, $success) = $this->createSendSmsTest(0); | |
1128 | $activity = $this->civicrm_api('activity', 'getsingle', array('id' => $activityId, 'version' => $this->_apiversion)); | |
1129 | ||
1130 | $outBoundSmsActivityId = CRM_Core_PseudoConstant::getKey('CRM_Activity_BAO_Activity', 'activity_type_id', 'SMS'); | |
1131 | $activityStatusCompleted = CRM_Core_PseudoConstant::getKey('CRM_Activity_BAO_Activity', 'activity_status_id', 'Completed'); | |
1132 | $details = 'createSendSmsTest text'; | |
1133 | $this->assertEquals($activity['activity_type_id'], $outBoundSmsActivityId, 'Wrong activity type is set.'); | |
1134 | $this->assertEquals($activity['status_id'], $activityStatusCompleted, 'Expected activity status Completed.'); | |
1135 | $this->assertEquals($activity['subject'], 'createSendSmsTest subject', 'Activity subject does not match.'); | |
1136 | $this->assertEquals($activity['details'], $details, 'Activity details does not match.'); | |
1137 | $this->assertEquals("Recipient phone number is invalid or recipient does not want to receive SMS", $sent[0]->message, "Expected error doesn't match"); | |
1138 | $this->assertEquals(0, $success, "Expected success to be 0"); | |
1139 | } | |
1140 | ||
1141 | public function testSendSmsFixedPhoneNumber() { | |
1142 | list($sent, $activityId, $success) = $this->createSendSmsTest(1); | |
1143 | $activity = $this->civicrm_api('activity', 'getsingle', array('id' => $activityId, 'version' => $this->_apiversion)); | |
1144 | ||
1145 | $outBoundSmsActivityId = CRM_Core_PseudoConstant::getKey('CRM_Activity_BAO_Activity', 'activity_type_id', 'SMS'); | |
1146 | $activityStatusCompleted = CRM_Core_PseudoConstant::getKey('CRM_Activity_BAO_Activity', 'activity_status_id', 'Completed'); | |
1147 | $details = 'createSendSmsTest text'; | |
1148 | $this->assertEquals($activity['activity_type_id'], $outBoundSmsActivityId, 'Wrong activity type is set.'); | |
1149 | $this->assertEquals($activity['status_id'], $activityStatusCompleted, 'Expected activity status Completed.'); | |
1150 | $this->assertEquals($activity['subject'], 'createSendSmsTest subject', 'Activity subject does not match.'); | |
1151 | $this->assertEquals($activity['details'], $details, 'Activity details does not match.'); | |
1152 | $this->assertEquals("Recipient phone number is invalid or recipient does not want to receive SMS", $sent[0]->message, "Expected error doesn't match"); | |
1153 | $this->assertEquals(0, $success, "Expected success to be 0"); | |
1154 | } | |
1155 | ||
1156 | public function testSendSmsMobilePhoneNumber() { | |
1157 | list($sent, $activityId, $success) = $this->createSendSmsTest(2); | |
1158 | $activity = $this->civicrm_api('activity', 'getsingle', array('id' => $activityId, 'version' => $this->_apiversion)); | |
1159 | ||
1160 | $outBoundSmsActivityId = CRM_Core_PseudoConstant::getKey('CRM_Activity_BAO_Activity', 'activity_type_id', 'SMS'); | |
1161 | $activityStatusCompleted = CRM_Core_PseudoConstant::getKey('CRM_Activity_BAO_Activity', 'activity_status_id', 'Completed'); | |
1162 | $details = 'createSendSmsTest text'; | |
1163 | $this->assertEquals($activity['activity_type_id'], $outBoundSmsActivityId, 'Wrong activity type is set.'); | |
1164 | $this->assertEquals($activity['status_id'], $activityStatusCompleted, 'Expected activity status Completed.'); | |
1165 | $this->assertEquals($activity['subject'], 'createSendSmsTest subject', 'Activity subject does not match.'); | |
1166 | $this->assertEquals($activity['details'], $details, 'Activity details does not match.'); | |
1167 | $this->assertEquals(TRUE, $sent, "Expected sent should be true"); | |
1168 | $this->assertEquals(1, $success, "Expected success to be 1"); | |
1169 | } | |
1170 | ||
a9b7ee41 SL |
1171 | /** |
1172 | * Test that when a numbe ris specified in the To Param of the SMS provider parameters that an SMS is sent | |
1173 | * @see dev/core/#273 | |
1174 | */ | |
1175 | public function testSendSMSMobileInToProviderParam() { | |
1176 | list($sent, $activityId, $success) = $this->createSendSmsTest(2, TRUE); | |
1177 | $this->assertEquals(TRUE, $sent, "Expected sent should be true"); | |
1178 | $this->assertEquals(1, $success, "Expected success to be 1"); | |
1179 | } | |
1180 | ||
1181 | /** | |
1182 | * Test that when a numbe ris specified in the To Param of the SMS provider parameters that an SMS is sent | |
1183 | * @see dev/core/#273 | |
1184 | */ | |
1185 | public function testSendSMSMobileInToProviderParamWithDoNotSMS() { | |
1186 | list($sent, $activityId, $success) = $this->createSendSmsTest(2, TRUE, ['do_not_sms' => 1]); | |
1187 | foreach ($sent as $error) { | |
1188 | $this->assertEquals('Contact Does not accept SMS', $error->getMessage()); | |
1189 | } | |
1190 | $this->assertEquals(1, count($sent), "Expected sent should a PEAR Error"); | |
1191 | $this->assertEquals(0, $success, "Expected success to be 0"); | |
1192 | } | |
1193 | ||
1a7f0799 MW |
1194 | /** |
1195 | * @param int $phoneType (0=no phone, phone_type option group (1=fixed, 2=mobile) | |
a9b7ee41 SL |
1196 | * @param bool $passPhoneTypeInContactDetails |
1197 | * @param array $additionalContactParams additional contact creation params | |
1a7f0799 | 1198 | */ |
a9b7ee41 | 1199 | public function createSendSmsTest($phoneType = 0, $passPhoneTypeInContactDetails = FALSE, $additionalContactParams = []) { |
1a7f0799 MW |
1200 | $provider = civicrm_api3('SmsProvider', 'create', array( |
1201 | 'name' => "CiviTestSMSProvider", | |
1202 | 'api_type' => "1", | |
1203 | "username" => "1", | |
1204 | "password" => "1", | |
1205 | "api_type" => "1", | |
1206 | "api_url" => "1", | |
1207 | "api_params" => "a=1", | |
1208 | "is_default" => "1", | |
1209 | "is_active" => "1", | |
1210 | "domain_id" => "1", | |
1211 | )); | |
a9b7ee41 | 1212 | |
1a7f0799 MW |
1213 | $smsProviderParams['provider_id'] = $provider['id']; |
1214 | ||
1215 | // Create a contact | |
1216 | $contactId = $this->individualCreate(); | |
a9b7ee41 SL |
1217 | if (!empty($additionalContactParams)) { |
1218 | $this->callAPISuccess('contact', 'create', ['id' => $contactId] + $additionalContactParams); | |
1219 | } | |
1220 | $contactsResult = $this->callApiSuccess('contact', 'get', ['id' => $contactId, 'version' => $this->_apiversion]); | |
1a7f0799 MW |
1221 | $contactDetails = $contactsResult['values']; |
1222 | ||
1223 | // Get contactIds from contact details | |
1224 | foreach ($contactDetails as $contact) { | |
1225 | $contactIds[] = $contact['contact_id']; | |
1226 | } | |
1227 | ||
1228 | $activityParams['sms_text_message'] = __FUNCTION__ . ' text'; | |
1229 | $activityParams['activity_subject'] = __FUNCTION__ . ' subject'; | |
1230 | ||
1a7f0799 MW |
1231 | // Get a "logged in" user to set as source of Sms. |
1232 | $session = CRM_Core_Session::singleton(); | |
1233 | $sourceContactId = $session->get('userID'); | |
1234 | ||
a627697d | 1235 | // Create a user |
1a7f0799 | 1236 | $this->_testSmsContactId = $this->createLoggedInUser(); |
a627697d MW |
1237 | |
1238 | // Give user permission to 'send SMS' | |
1239 | CRM_Core_Config::singleton()->userPermissionClass->permissions = array('access CiviCRM', 'send SMS'); | |
1240 | ||
1241 | // Create a phone number | |
1a7f0799 MW |
1242 | switch ($phoneType) { |
1243 | case 0: | |
1244 | // No phone number | |
1245 | break; | |
1246 | ||
1247 | case 2: | |
1248 | // Create a mobile phone number | |
1249 | $phone = civicrm_api3('Phone', 'create', array( | |
1250 | 'contact_id' => $contactId, | |
1251 | 'phone' => 123456, | |
1252 | 'phone_type_id' => "Mobile", | |
1253 | )); | |
a9b7ee41 SL |
1254 | if ($passPhoneTypeInContactDetails) { |
1255 | $contactDetails[$contactId]['phone'] = $phone['values'][$phone['id']]['phone']; | |
1256 | $contactDetails[$contactId]['phone_type_id'] = $phone['values'][$phone['id']]['phone_type_id']; | |
1257 | } | |
1a7f0799 MW |
1258 | break; |
1259 | ||
1260 | case 1: | |
1261 | // Create a fixed phone number | |
1262 | $phone = civicrm_api3('Phone', 'create', array( | |
1263 | 'contact_id' => $contactId, | |
1264 | 'phone' => 654321, | |
1265 | 'phone_type_id' => "Phone", | |
1266 | )); | |
a9b7ee41 SL |
1267 | if ($passPhoneTypeInContactDetails) { |
1268 | $contactDetails[$contactId]['phone'] = $phone['values'][$phone['id']]['phone']; | |
1269 | $contactDetails[$contactId]['phone_type_id'] = $phone['values'][$phone['id']]['phone_type_id']; | |
1270 | } | |
1a7f0799 MW |
1271 | break; |
1272 | } | |
1273 | ||
1274 | // Now run the actual test | |
1275 | list($sent, $activityId, $success) = CRM_Activity_BAO_Activity::sendSms( | |
1276 | $contactDetails, | |
1277 | $activityParams, | |
1278 | $smsProviderParams, | |
1279 | $contactIds, | |
1280 | $sourceContactId | |
1281 | ); | |
1282 | ||
1283 | return array($sent, $activityId, $success); | |
1284 | } | |
1285 | ||
a6c2ebdc | 1286 | protected function createTestActivities() { |
1287 | $op = new PHPUnit_Extensions_Database_Operation_Insert(); | |
1288 | $op->execute($this->_dbconn, | |
1289 | $this->createFlatXMLDataSet( | |
1290 | dirname(__FILE__) . '/activities_for_dashboard_count.xml' | |
1291 | ) | |
1292 | ); | |
c2ce41b6 | 1293 | // Make changes to improve variation in php since the xml method is brittle & relies on option values being unchanged. |
1294 | $this->callAPISuccess('Activity', 'create', ['id' => 12, 'activity_type_id' => 'Bulk Email']); | |
a6c2ebdc | 1295 | } |
1296 | ||
6a488035 | 1297 | } |