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 | |
5123cb11 SL |
9 | private $allowedContactsACL = []; |
10 | ||
00be9182 | 11 | public function setUp() { |
6a488035 | 12 | parent::setUp(); |
cbcedb39 | 13 | $this->prepareForACLs(); |
9099cab3 | 14 | CRM_Core_Config::singleton()->userPermissionClass->permissions = ['view all contacts', 'access CiviCRM']; |
1a7f0799 | 15 | $this->setupForSmsTests(); |
6a488035 TO |
16 | } |
17 | ||
cbcedb39 | 18 | /** |
19 | * Clean up after tests. | |
20 | */ | |
00be9182 | 21 | public function tearDown() { |
9099cab3 | 22 | $tablesToTruncate = [ |
824989b9 AS |
23 | 'civicrm_activity', |
24 | 'civicrm_activity_contact', | |
25 | 'civicrm_uf_match', | |
26 | 'civicrm_campaign', | |
27 | 'civicrm_email', | |
9099cab3 | 28 | ]; |
6a488035 | 29 | $this->quickCleanup($tablesToTruncate); |
cbcedb39 | 30 | $this->cleanUpAfterACLs(); |
1a7f0799 | 31 | $this->setupForSmsTests(TRUE); |
cbcedb39 | 32 | parent::tearDown(); |
6a488035 TO |
33 | } |
34 | ||
35 | /** | |
fe482240 | 36 | * Test case for create() method. |
6a488035 | 37 | */ |
00be9182 | 38 | public function testCreate() { |
f7969dcf | 39 | $contactId = $this->individualCreate(); |
6a488035 | 40 | |
9099cab3 | 41 | $params = [ |
6a488035 TO |
42 | 'source_contact_id' => $contactId, |
43 | 'subject' => 'Scheduling Meeting', | |
44 | 'activity_type_id' => 2, | |
9099cab3 | 45 | ]; |
6a488035 TO |
46 | |
47 | CRM_Activity_BAO_Activity::create($params); | |
48 | ||
49 | $activityId = $this->assertDBNotNull('CRM_Activity_DAO_Activity', 'Scheduling Meeting', 'id', | |
50 | 'subject', 'Database check for created activity.' | |
51 | ); | |
52 | ||
cbcedb39 | 53 | // Now call create() to modify an existing Activity. |
9099cab3 | 54 | $params = [ |
6a488035 TO |
55 | 'id' => $activityId, |
56 | 'source_contact_id' => $contactId, | |
57 | 'subject' => 'Scheduling Interview', | |
58 | 'activity_type_id' => 3, | |
9099cab3 | 59 | ]; |
6a488035 TO |
60 | |
61 | CRM_Activity_BAO_Activity::create($params); | |
62 | ||
63 | $activityTypeId = $this->assertDBNotNull('CRM_Activity_DAO_Activity', 'Scheduling Interview', | |
64 | 'activity_type_id', | |
65 | 'subject', 'Database check on updated activity record.' | |
66 | ); | |
67 | $this->assertEquals($activityTypeId, 3, 'Verify activity type id is 3.'); | |
68 | ||
93ac19cd | 69 | $this->contactDelete($contactId); |
6a488035 TO |
70 | } |
71 | ||
72 | /** | |
fe482240 EM |
73 | * Test case for getContactActivity() method. |
74 | * | |
75 | * getContactActivity() method get activities detail for given target contact id. | |
6a488035 | 76 | */ |
00be9182 | 77 | public function testGetContactActivity() { |
f7969dcf | 78 | $contactId = $this->individualCreate(); |
9099cab3 | 79 | $params = [ |
6a488035 TO |
80 | 'first_name' => 'liz', |
81 | 'last_name' => 'hurleey', | |
9099cab3 | 82 | ]; |
f7969dcf | 83 | $targetContactId = $this->individualCreate($params); |
6a488035 | 84 | |
9099cab3 | 85 | $params = [ |
6a488035 TO |
86 | 'source_contact_id' => $contactId, |
87 | 'subject' => 'Scheduling Meeting', | |
88 | 'activity_type_id' => 2, | |
9099cab3 | 89 | 'target_contact_id' => [$targetContactId], |
6a488035 | 90 | 'activity_date_time' => date('Ymd'), |
9099cab3 | 91 | ]; |
6a488035 | 92 | |
f7969dcf | 93 | $this->callAPISuccess('Activity', 'create', $params); |
6a488035 TO |
94 | |
95 | $activityId = $this->assertDBNotNull('CRM_Activity_DAO_Activity', 'Scheduling Meeting', | |
96 | 'id', | |
97 | 'subject', 'Database check for created activity.' | |
98 | ); | |
99 | ||
a59cecb1 | 100 | // @todo - remove this deprecated functions |
6a488035 TO |
101 | $activities = CRM_Activity_BAO_Activity::getContactActivity($targetContactId); |
102 | ||
103 | $this->assertEquals($activities[$activityId]['subject'], 'Scheduling Meeting', 'Verify activity subject is correct.'); | |
104 | ||
93ac19cd | 105 | $this->contactDelete($contactId); |
106 | $this->contactDelete($targetContactId); | |
6a488035 TO |
107 | } |
108 | ||
109 | /** | |
fe482240 EM |
110 | * Test case for retrieve() method. |
111 | * | |
112 | * Retrieve($params, $defaults) method return activity detail for given params | |
6a488035 TO |
113 | * and set defaults. |
114 | */ | |
00be9182 | 115 | public function testRetrieve() { |
f7969dcf | 116 | $contactId = $this->individualCreate(); |
9099cab3 | 117 | $params = [ |
6a488035 TO |
118 | 'first_name' => 'liz', |
119 | 'last_name' => 'hurleey', | |
9099cab3 | 120 | ]; |
f7969dcf | 121 | $targetContactId = $this->individualCreate($params); |
6a488035 | 122 | |
9099cab3 | 123 | $params = [ |
6a488035 TO |
124 | 'source_contact_id' => $contactId, |
125 | 'subject' => 'Scheduling Meeting', | |
126 | 'activity_type_id' => 2, | |
9099cab3 | 127 | 'target_contact_id' => [$targetContactId], |
6a488035 | 128 | 'activity_date_time' => date('Ymd'), |
9099cab3 | 129 | ]; |
6a488035 TO |
130 | |
131 | CRM_Activity_BAO_Activity::create($params); | |
132 | ||
cbcedb39 | 133 | $this->assertDBNotNull('CRM_Activity_DAO_Activity', 'Scheduling Meeting', 'id', |
6a488035 TO |
134 | 'subject', 'Database check for created activity.' |
135 | ); | |
136 | ||
cbcedb39 | 137 | $this->assertDBNotNull('CRM_Activity_DAO_ActivityContact', $targetContactId, |
f1c7b1f0 | 138 | 'id', 'contact_id', |
6a488035 TO |
139 | 'Database check for created activity target.' |
140 | ); | |
141 | ||
9099cab3 | 142 | $defaults = []; |
6a488035 TO |
143 | $activity = CRM_Activity_BAO_Activity::retrieve($params, $defaults); |
144 | ||
145 | $this->assertEquals($activity->subject, 'Scheduling Meeting', 'Verify activity subject is correct.'); | |
6a488035 | 146 | $this->assertEquals($activity->activity_type_id, 2, 'Verify activity type id is correct.'); |
b2e56051 | 147 | $this->assertEquals($defaults['source_contact_id'], $contactId, 'Verify source contact id is correct.'); |
6a488035 TO |
148 | |
149 | $this->assertEquals($defaults['subject'], 'Scheduling Meeting', 'Verify activity subject is correct.'); | |
6a488035 TO |
150 | $this->assertEquals($defaults['activity_type_id'], 2, 'Verify activity type id is correct.'); |
151 | ||
152 | $this->assertEquals($defaults['target_contact'][0], $targetContactId, 'Verify target contact id is correct.'); | |
153 | ||
93ac19cd | 154 | $this->contactDelete($contactId); |
155 | $this->contactDelete($targetContactId); | |
6a488035 TO |
156 | } |
157 | ||
5123cb11 SL |
158 | /** |
159 | * Test Assigning a target contact but then the logged in user cannot see the contact | |
160 | */ | |
161 | public function testTargetContactNotavaliable() { | |
162 | $contactId = $this->individualCreate(); | |
9099cab3 | 163 | $params = [ |
5123cb11 SL |
164 | 'first_name' => 'liz', |
165 | 'last_name' => 'hurleey', | |
9099cab3 | 166 | ]; |
5123cb11 SL |
167 | $targetContactId = $this->individualCreate($params); |
168 | ||
9099cab3 | 169 | $params = [ |
5123cb11 SL |
170 | 'source_contact_id' => $contactId, |
171 | 'subject' => 'Scheduling Meeting', | |
172 | 'activity_type_id' => 2, | |
9099cab3 | 173 | 'target_contact_id' => [$targetContactId], |
5123cb11 | 174 | 'activity_date_time' => date('Ymd'), |
9099cab3 | 175 | ]; |
5123cb11 SL |
176 | |
177 | CRM_Activity_BAO_Activity::create($params); | |
178 | ||
179 | // set custom hook | |
9099cab3 | 180 | $this->hookClass->setHook('civicrm_aclWhereClause', [$this, 'hook_civicrm_aclWhereClause']); |
5123cb11 | 181 | |
9099cab3 | 182 | CRM_Core_Config::singleton()->userPermissionClass->permissions = ['access CiviCRM']; |
5123cb11 | 183 | |
9099cab3 | 184 | $this->allowedContactsACL = [$contactId]; |
5123cb11 SL |
185 | |
186 | // get logged in user | |
187 | $user_id = $this->createLoggedInUser(); | |
188 | $activityGetParams = CRM_Core_Page_AJAX::defaultSortAndPagerParams(); | |
189 | $activityGetParams += ['contact_id' => $contactId]; | |
190 | $activities = CRM_Activity_BAO_Activity::getContactActivitySelector($activityGetParams); | |
995f30cc SL |
191 | // Aseert that we have sensible data to display in the contact tab |
192 | $this->assertEquals('Anderson, Anthony', $activities['data'][0]['source_contact_name']); | |
193 | // Note that becasue there is a target contact but it is not accessable the output is an empty string not n/a | |
194 | $this->assertEquals('', $activities['data'][0]['target_contact_name']); | |
195 | // verify that doing the underlying query shows we get a target contact_id | |
196 | $this->assertEquals(1, CRM_Activity_BAO_Activity::getActivities(['contact_id' => $contactId])[1]['target_contact_count']); | |
05704444 | 197 | $this->cleanUpAfterACLs(); |
5123cb11 SL |
198 | } |
199 | ||
6a488035 | 200 | /** |
fe482240 EM |
201 | * Test case for deleteActivity() method. |
202 | * | |
6a488035 TO |
203 | * deleteActivity($params) method deletes activity for given params. |
204 | */ | |
00be9182 | 205 | public function testDeleteActivity() { |
f7969dcf | 206 | $contactId = $this->individualCreate(); |
9099cab3 | 207 | $params = [ |
6a488035 TO |
208 | 'first_name' => 'liz', |
209 | 'last_name' => 'hurleey', | |
9099cab3 | 210 | ]; |
f7969dcf | 211 | $targetContactId = $this->individualCreate($params); |
6a488035 | 212 | |
9099cab3 | 213 | $params = [ |
6a488035 TO |
214 | 'source_contact_id' => $contactId, |
215 | 'source_record_id' => $contactId, | |
216 | 'subject' => 'Scheduling Meeting', | |
217 | 'activity_type_id' => 2, | |
9099cab3 | 218 | 'target_contact_id' => [$targetContactId], |
6a488035 | 219 | 'activity_date_time' => date('Ymd'), |
9099cab3 | 220 | ]; |
6a488035 TO |
221 | |
222 | CRM_Activity_BAO_Activity::create($params); | |
223 | ||
cbcedb39 | 224 | $this->assertDBNotNull('CRM_Activity_DAO_Activity', 'Scheduling Meeting', 'id', |
6a488035 TO |
225 | 'subject', 'Database check for created activity.' |
226 | ); | |
227 | ||
cbcedb39 | 228 | $this->assertDBNotNull('CRM_Activity_DAO_ActivityContact', $targetContactId, |
f1c7b1f0 | 229 | 'id', 'contact_id', |
6a488035 TO |
230 | 'Database check for created activity target.' |
231 | ); | |
9099cab3 | 232 | $params = [ |
6a488035 TO |
233 | 'source_contact_id' => $contactId, |
234 | 'source_record_id' => $contactId, | |
235 | 'subject' => 'Scheduling Meeting', | |
236 | 'activity_type_id' => 2, | |
9099cab3 | 237 | ]; |
6a488035 | 238 | |
93ac19cd | 239 | CRM_Activity_BAO_Activity::deleteActivity($params); |
6a488035 | 240 | |
93ac19cd | 241 | $this->assertDBNull('CRM_Activity_DAO_Activity', 'Scheduling Meeting', 'id', |
6a488035 TO |
242 | 'subject', 'Database check for deleted activity.' |
243 | ); | |
93ac19cd | 244 | $this->contactDelete($contactId); |
245 | $this->contactDelete($targetContactId); | |
6a488035 TO |
246 | } |
247 | ||
248 | /** | |
fe482240 EM |
249 | * Test case for deleteActivityTarget() method. |
250 | * | |
6a488035 TO |
251 | * deleteActivityTarget($activityId) method deletes activity target for given activity id. |
252 | */ | |
00be9182 | 253 | public function testDeleteActivityTarget() { |
f7969dcf | 254 | $contactId = $this->individualCreate(); |
9099cab3 | 255 | $params = [ |
6a488035 TO |
256 | 'first_name' => 'liz', |
257 | 'last_name' => 'hurleey', | |
9099cab3 | 258 | ]; |
f7969dcf | 259 | $targetContactId = $this->individualCreate($params); |
6a488035 | 260 | |
9099cab3 | 261 | $params = [ |
6a488035 TO |
262 | 'source_contact_id' => $contactId, |
263 | 'subject' => 'Scheduling Meeting', | |
264 | 'activity_type_id' => 2, | |
9099cab3 | 265 | 'target_contact_id' => [$targetContactId], |
6a488035 | 266 | 'activity_date_time' => date('Ymd'), |
9099cab3 | 267 | ]; |
6a488035 TO |
268 | |
269 | CRM_Activity_BAO_Activity::create($params); | |
270 | ||
271 | $activityId = $this->assertDBNotNull('CRM_Activity_DAO_Activity', 'Scheduling Meeting', 'id', | |
272 | 'subject', 'Database check for created activity.' | |
273 | ); | |
274 | ||
cbcedb39 | 275 | $this->assertDBNotNull('CRM_Activity_DAO_ActivityContact', $targetContactId, |
f1c7b1f0 | 276 | 'id', 'contact_id', |
6a488035 TO |
277 | 'Database check for created activity target.' |
278 | ); | |
279 | ||
2517d079 | 280 | CRM_Activity_BAO_Activity::deleteActivityContact($activityId, 3); |
6a488035 | 281 | |
f1c7b1f0 DL |
282 | $this->assertDBNull('CRM_Activity_DAO_ActivityContact', $targetContactId, 'id', |
283 | 'contact_id', 'Database check for deleted activity target.' | |
6a488035 TO |
284 | ); |
285 | ||
93ac19cd | 286 | $this->contactDelete($contactId); |
287 | $this->contactDelete($targetContactId); | |
6a488035 TO |
288 | } |
289 | ||
290 | /** | |
fe482240 EM |
291 | * Test case for deleteActivityAssignment() method. |
292 | * | |
6a488035 TO |
293 | * deleteActivityAssignment($activityId) method deletes activity assignment for given activity id. |
294 | */ | |
00be9182 | 295 | public function testDeleteActivityAssignment() { |
f7969dcf | 296 | $contactId = $this->individualCreate(); |
9099cab3 | 297 | $params = [ |
6a488035 TO |
298 | 'first_name' => 'liz', |
299 | 'last_name' => 'hurleey', | |
9099cab3 | 300 | ]; |
f7969dcf | 301 | $assigneeContactId = $this->individualCreate($params); |
6a488035 | 302 | |
9099cab3 | 303 | $params = [ |
6a488035 TO |
304 | 'source_contact_id' => $contactId, |
305 | 'subject' => 'Scheduling Meeting', | |
306 | 'activity_type_id' => 2, | |
9099cab3 | 307 | 'assignee_contact_id' => [$assigneeContactId], |
6a488035 | 308 | 'activity_date_time' => date('Ymd'), |
9099cab3 | 309 | ]; |
6a488035 TO |
310 | |
311 | CRM_Activity_BAO_Activity::create($params); | |
312 | ||
313 | $activityId = $this->assertDBNotNull('CRM_Activity_DAO_Activity', 'Scheduling Meeting', 'id', | |
314 | 'subject', 'Database check for created activity.' | |
315 | ); | |
316 | ||
cbcedb39 | 317 | $this->assertDBNotNull('CRM_Activity_DAO_ActivityContact', |
f1c7b1f0 | 318 | $assigneeContactId, 'id', 'contact_id', |
6a488035 TO |
319 | 'Database check for created activity assignment.' |
320 | ); | |
321 | ||
2517d079 | 322 | CRM_Activity_BAO_Activity::deleteActivityContact($activityId, 1); |
6a488035 | 323 | |
f1c7b1f0 DL |
324 | $this->assertDBNull('CRM_Activity_DAO_ActivityContact', $assigneeContactId, 'id', |
325 | 'contact_id', 'Database check for deleted activity assignment.' | |
6a488035 TO |
326 | ); |
327 | ||
93ac19cd | 328 | $this->contactDelete($contactId); |
329 | $this->contactDelete($assigneeContactId); | |
6a488035 TO |
330 | } |
331 | ||
332 | /** | |
5161bb0c | 333 | * Test getActivities BAO method for getting count. |
6a488035 | 334 | */ |
cbcedb39 | 335 | public function testGetActivitiesCountForAdminDashboard() { |
75d842f8 | 336 | $this->setUpForActivityDashboardTests(); |
6ab43e1b | 337 | $activityCount = CRM_Activity_BAO_Activity::getActivitiesCount($this->_params); |
75d842f8 | 338 | $this->assertEquals(8, $activityCount); |
6a488035 TO |
339 | } |
340 | ||
341 | /** | |
5161bb0c | 342 | * Test getActivities BAO method for getting count |
6a488035 | 343 | */ |
00be9182 | 344 | public function testGetActivitiesCountforNonAdminDashboard() { |
a6c2ebdc | 345 | $this->createTestActivities(); |
6a488035 | 346 | |
9099cab3 | 347 | $params = [ |
6a488035 TO |
348 | 'contact_id' => 9, |
349 | 'admin' => FALSE, | |
350 | 'caseId' => NULL, | |
351 | 'context' => 'home', | |
352 | 'activity_type_id' => NULL, | |
39b959db SL |
353 | // for dashlet the Scheduled status is set by default |
354 | 'activity_status_id' => CRM_Core_PseudoConstant::getKey('CRM_Activity_BAO_Activity', 'status_id', 'Scheduled'), | |
6a488035 TO |
355 | 'offset' => 0, |
356 | 'rowCount' => 0, | |
357 | 'sort' => NULL, | |
9099cab3 | 358 | ]; |
6a488035 | 359 | |
6a488035 TO |
360 | //since we are loading activities from dataset, we know total number of activities for this contact |
361 | // 5 activities ( 2 scheduled, 3 Completed ), note that dashboard shows only scheduled activities | |
6ab43e1b | 362 | $this->assertEquals(2, CRM_Activity_BAO_Activity::getActivitiesCount($params)); |
6a488035 TO |
363 | } |
364 | ||
365 | /** | |
5161bb0c | 366 | * Test getActivities BAO method for getting count |
6a488035 | 367 | */ |
00be9182 | 368 | public function testGetActivitiesCountforContactSummary() { |
a6c2ebdc | 369 | $this->createTestActivities(); |
6a488035 | 370 | |
9099cab3 | 371 | $params = [ |
6a488035 TO |
372 | 'contact_id' => 9, |
373 | 'admin' => FALSE, | |
374 | 'caseId' => NULL, | |
375 | 'context' => 'activity', | |
376 | 'activity_type_id' => NULL, | |
377 | 'offset' => 0, | |
378 | 'rowCount' => 0, | |
379 | 'sort' => NULL, | |
9099cab3 | 380 | ]; |
6a488035 TO |
381 | |
382 | //since we are loading activities from dataset, we know total number of activities for this contact | |
383 | // 5 activities, Contact Summary should show all activities | |
6ab43e1b | 384 | $this->assertEquals(5, CRM_Activity_BAO_Activity::getActivitiesCount($params)); |
6a488035 TO |
385 | } |
386 | ||
23289ddd | 387 | /** |
388 | * CRM-18706 - Test Include/Exclude Activity Filters | |
389 | */ | |
390 | public function testActivityFilters() { | |
a6c2ebdc | 391 | $this->createTestActivities(); |
c3a9ccbb JP |
392 | Civi::settings()->set('preserve_activity_tab_filter', 1); |
393 | $this->createLoggedInUser(); | |
23289ddd | 394 | |
395 | global $_GET; | |
9099cab3 | 396 | $_GET = [ |
23289ddd | 397 | 'cid' => 9, |
398 | 'context' => 'activity', | |
399 | 'activity_type_id' => 1, | |
400 | 'is_unit_test' => 1, | |
9099cab3 CW |
401 | ]; |
402 | $expectedFilters = [ | |
c3a9ccbb | 403 | 'activity_type_filter_id' => 1, |
9099cab3 | 404 | ]; |
23289ddd | 405 | |
c3a9ccbb JP |
406 | list($activities, $activityFilter) = CRM_Activity_Page_AJAX::getContactActivity(); |
407 | //Assert whether filters are correctly set. | |
408 | $this->checkArrayEquals($expectedFilters, $activityFilter); | |
23289ddd | 409 | // This should include activities of type Meeting only. |
cbcedb39 | 410 | foreach ($activities['data'] as $value) { |
8c99c0bb | 411 | $this->assertContains('Meeting', $value['activity_type']); |
23289ddd | 412 | } |
413 | unset($_GET['activity_type_id']); | |
414 | ||
c3a9ccbb JP |
415 | $_GET['activity_type_exclude_id'] = $expectedFilters['activity_type_exclude_filter_id'] = 1; |
416 | list($activities, $activityFilter) = CRM_Activity_Page_AJAX::getContactActivity(); | |
c1d3e301 | 417 | $this->assertEquals(['activity_type_exclude_filter_id' => 1], $activityFilter); |
23289ddd | 418 | // None of the activities should be of type Meeting. |
cbcedb39 | 419 | foreach ($activities['data'] as $value) { |
c3a9ccbb | 420 | $this->assertNotContains('Meeting', $value['activity_type']); |
23289ddd | 421 | } |
422 | } | |
423 | ||
6a488035 | 424 | /** |
5161bb0c | 425 | * Test getActivities BAO method for getting count |
6a488035 | 426 | */ |
00be9182 | 427 | public function testGetActivitiesCountforContactSummaryWithNoActivities() { |
a6c2ebdc | 428 | $this->createTestActivities(); |
6a488035 | 429 | |
9099cab3 | 430 | $params = [ |
6a488035 TO |
431 | 'contact_id' => 17, |
432 | 'admin' => FALSE, | |
433 | 'caseId' => NULL, | |
434 | 'context' => 'home', | |
435 | 'activity_type_id' => NULL, | |
436 | 'offset' => 0, | |
437 | 'rowCount' => 0, | |
438 | 'sort' => NULL, | |
9099cab3 | 439 | ]; |
6a488035 TO |
440 | |
441 | //since we are loading activities from dataset, we know total number of activities for this contact | |
442 | // this contact does not have any activity | |
6ab43e1b | 443 | $this->assertEquals(0, CRM_Activity_BAO_Activity::getActivitiesCount($params)); |
6a488035 TO |
444 | } |
445 | ||
446 | /** | |
eceb18cc | 447 | * Test getActivities BAO method. |
6a488035 | 448 | */ |
cbcedb39 | 449 | public function testGetActivitiesForAdminDashboard() { |
75d842f8 | 450 | $this->setUpForActivityDashboardTests(); |
6ab43e1b | 451 | $activitiesNew = CRM_Activity_BAO_Activity::getActivities($this->_params); |
452 | // $this->assertEquals($activities, $activitiesDeprecatedFn); | |
6a488035 TO |
453 | |
454 | //since we are loading activities from dataset, we know total number of activities | |
5161bb0c | 455 | // with no contact ID and there should be 8 schedule activities shown on dashboard |
6a488035 | 456 | $count = 8; |
9099cab3 | 457 | foreach ([$activitiesNew] as $activities) { |
6ab43e1b | 458 | $this->assertEquals($count, count($activities)); |
6a488035 | 459 | |
6ab43e1b | 460 | foreach ($activities as $key => $value) { |
461 | $this->assertEquals($value['subject'], "subject {$key}", 'Verify activity subject is correct.'); | |
462 | $this->assertEquals($value['activity_type_id'], 2, 'Verify activity type is correct.'); | |
463 | $this->assertEquals($value['status_id'], 1, 'Verify all activities are scheduled.'); | |
464 | } | |
6a488035 TO |
465 | } |
466 | } | |
467 | ||
75d842f8 | 468 | /** |
469 | * Test getActivities BAO method. | |
470 | */ | |
471 | public function testGetActivitiesForAdminDashboardNoViewContacts() { | |
9099cab3 | 472 | CRM_Core_Config::singleton()->userPermissionClass->permissions = ['access CiviCRM']; |
75d842f8 | 473 | $this->setUpForActivityDashboardTests(); |
9099cab3 | 474 | foreach ([CRM_Activity_BAO_Activity::getActivities($this->_params)] as $activities) { |
6ab43e1b | 475 | // Skipped until we get back to the upgraded version properly. |
6e793248 | 476 | $this->assertEquals(0, count($activities)); |
6ab43e1b | 477 | } |
26583d3e | 478 | } |
479 | ||
480 | /** | |
481 | * Test getActivities BAO method. | |
482 | */ | |
483 | public function testGetActivitiesForAdminDashboardAclLimitedViewContacts() { | |
9099cab3 CW |
484 | CRM_Core_Config::singleton()->userPermissionClass->permissions = ['access CiviCRM']; |
485 | $this->allowedContacts = [1, 3, 4, 5]; | |
486 | $this->hookClass->setHook('civicrm_aclWhereClause', [$this, 'aclWhereMultipleContacts']); | |
26583d3e | 487 | $this->setUpForActivityDashboardTests(); |
6e793248 | 488 | $this->assertEquals(7, count(CRM_Activity_BAO_Activity::getActivities($this->_params))); |
75d842f8 | 489 | } |
490 | ||
6a488035 | 491 | /** |
eceb18cc | 492 | * Test getActivities BAO method. |
6a488035 | 493 | */ |
00be9182 | 494 | public function testGetActivitiesforNonAdminDashboard() { |
a6c2ebdc | 495 | $this->createTestActivities(); |
6a488035 TO |
496 | |
497 | $contactID = 9; | |
9099cab3 | 498 | $params = [ |
6a488035 TO |
499 | 'contact_id' => $contactID, |
500 | 'admin' => FALSE, | |
501 | 'caseId' => NULL, | |
502 | 'context' => 'home', | |
503 | 'activity_type_id' => NULL, | |
39b959db SL |
504 | // for dashlet the Scheduled status is set by default |
505 | 'activity_status_id' => CRM_Core_PseudoConstant::getKey('CRM_Activity_BAO_Activity', 'status_id', 'Scheduled'), | |
6a488035 TO |
506 | 'offset' => 0, |
507 | 'rowCount' => 0, | |
508 | 'sort' => NULL, | |
9099cab3 | 509 | ]; |
6a488035 | 510 | |
9099cab3 | 511 | foreach ([CRM_Activity_BAO_Activity::getActivities($params)] as $activities) { |
6ab43e1b | 512 | //since we are loading activities from dataset, we know total number of activities for this contact |
513 | // 5 activities ( 2 scheduled, 3 Completed ), note that dashboard shows only scheduled activities | |
514 | $count = 2; | |
515 | $this->assertEquals($count, count($activities)); | |
6a488035 | 516 | |
6ab43e1b | 517 | foreach ($activities as $key => $value) { |
518 | $this->assertEquals($value['subject'], "subject {$key}", 'Verify activity subject is correct.'); | |
519 | $this->assertEquals($value['activity_type_id'], 2, 'Verify activity type is correct.'); | |
520 | $this->assertEquals($value['status_id'], 1, 'Verify all activities are scheduled.'); | |
6a488035 | 521 | |
6ab43e1b | 522 | if ($key == 3) { |
523 | $this->assertArrayHasKey($contactID, $value['target_contact_name']); | |
524 | } | |
525 | elseif ($key == 4) { | |
526 | $this->assertArrayHasKey($contactID, $value['assignee_contact_name']); | |
527 | } | |
6a488035 TO |
528 | } |
529 | } | |
530 | } | |
531 | ||
36d2f4d5 | 532 | /** |
533 | * Test target contact count. | |
534 | */ | |
535 | public function testTargetCountforContactSummary() { | |
536 | $targetCount = 5; | |
537 | $contactId = $this->individualCreate(); | |
9099cab3 | 538 | $targetContactIDs = []; |
36d2f4d5 | 539 | for ($i = 0; $i < $targetCount; $i++) { |
9099cab3 | 540 | $targetContactIDs[] = $this->individualCreate([], $i); |
36d2f4d5 | 541 | } |
cbcedb39 | 542 | // Create activities with 5 target contacts. |
9099cab3 | 543 | $activityParams = [ |
36d2f4d5 | 544 | 'source_contact_id' => $contactId, |
545 | 'target_contact_id' => $targetContactIDs, | |
9099cab3 | 546 | ]; |
36d2f4d5 | 547 | $this->activityCreate($activityParams); |
548 | ||
9099cab3 | 549 | $params = [ |
36d2f4d5 | 550 | 'contact_id' => $contactId, |
551 | 'context' => 'activity', | |
9099cab3 | 552 | ]; |
c2ce41b6 | 553 | $activities = CRM_Activity_BAO_Activity::getActivities($params); |
554 | //verify target count | |
555 | $this->assertEquals($targetCount, $activities[1]['target_contact_count']); | |
556 | $this->assertEquals([$targetContactIDs[0] => 'Anderson, Anthony'], $activities[1]['target_contact_name']); | |
557 | $this->assertEquals('Anderson, Anthony', $activities[1]['source_contact_name']); | |
558 | $this->assertEquals('Anderson, Anthony', $activities[1]['assignee_contact_name'][4]); | |
36d2f4d5 | 559 | } |
560 | ||
a6c2ebdc | 561 | /** |
562 | * Test getActivities BAO method. | |
563 | */ | |
564 | public function testGetActivitiesforContactSummaryWithSortOptions() { | |
565 | $this->createTestActivities(); | |
566 | $params = [ | |
567 | 'contact_id' => 9, | |
568 | 'admin' => FALSE, | |
569 | 'caseId' => NULL, | |
570 | 'context' => 'activity', | |
571 | 'activity_type_id' => NULL, | |
572 | 'offset' => 0, | |
573 | 'rowCount' => 0, | |
574 | 'sort' => 'source_contact_name desc', | |
575 | ]; | |
576 | ||
577 | $activities = CRM_Activity_BAO_Activity::getActivities($params); | |
578 | $alphaOrder = ['Test Contact 11', 'Test Contact 12', 'Test Contact 3', 'Test Contact 4', 'Test Contact 9']; | |
579 | foreach ($activities as $activity) { | |
580 | $this->assertEquals(array_pop($alphaOrder), $activity['source_contact_name']); | |
581 | } | |
582 | ||
583 | } | |
584 | ||
6a488035 | 585 | /** |
eceb18cc | 586 | * Test getActivities BAO method. |
6a488035 | 587 | */ |
c2ce41b6 | 588 | public function testGetActivitiesForContactSummary() { |
a6c2ebdc | 589 | $this->createTestActivities(); |
6a488035 TO |
590 | |
591 | $contactID = 9; | |
9099cab3 | 592 | $params = [ |
6a488035 TO |
593 | 'contact_id' => $contactID, |
594 | 'admin' => FALSE, | |
595 | 'caseId' => NULL, | |
596 | 'context' => 'activity', | |
597 | 'activity_type_id' => NULL, | |
598 | 'offset' => 0, | |
599 | 'rowCount' => 0, | |
9099cab3 | 600 | ]; |
6a488035 TO |
601 | |
602 | //since we are loading activities from dataset, we know total number of activities for this contact | |
603 | // 5 activities, Contact Summary should show all activities | |
604 | $count = 5; | |
c2ce41b6 | 605 | $activities = CRM_Activity_BAO_Activity::getActivities($params); |
606 | $this->assertEquals($count, count($activities)); | |
607 | foreach ($activities as $key => $value) { | |
608 | $this->assertEquals($value['subject'], "subject {$key}", 'Verify activity subject is correct.'); | |
6a488035 | 609 | |
c2ce41b6 | 610 | if ($key > 8) { |
611 | $this->assertEquals($value['status_id'], 2, 'Verify all activities are scheduled.'); | |
612 | } | |
613 | else { | |
614 | $this->assertEquals($value['status_id'], 1, 'Verify all activities are scheduled.'); | |
615 | } | |
6a488035 | 616 | |
c2ce41b6 | 617 | if ($key === 12) { |
618 | $this->assertEquals($value['activity_type'], 'Bulk Email', 'Verify activity type is correct.'); | |
619 | $this->assertEquals('(2 recipients)', $value['recipients']); | |
620 | $targetContactID = key($value['target_contact_name']); | |
621 | // The 2 targets have ids 10 & 11. Since they are not sorted it could be either on some systems. | |
622 | $this->assertTrue(in_array($targetContactID, [10, 11])); | |
623 | } | |
624 | elseif ($key > 8) { | |
625 | $this->assertEquals($value['activity_type_id'], 1, 'Verify activity type is correct.'); | |
626 | } | |
627 | else { | |
628 | $this->assertEquals($value['activity_type_id'], 2, 'Verify activity type is correct.'); | |
629 | } | |
6ab43e1b | 630 | |
c2ce41b6 | 631 | if ($key == 3) { |
632 | $this->assertEquals([$contactID => 'Test Contact ' . $contactID], $value['target_contact_name']); | |
633 | } | |
634 | elseif ($key == 4) { | |
635 | $this->assertArrayHasKey($contactID, $value['assignee_contact_name']); | |
6a488035 TO |
636 | } |
637 | } | |
638 | } | |
639 | ||
640 | /** | |
eceb18cc | 641 | * Test getActivities BAO method. |
6a488035 | 642 | */ |
5161bb0c | 643 | public function testGetActivitiesforContactSummaryWithActivities() { |
a6c2ebdc | 644 | $this->createTestActivities(); |
6a488035 | 645 | |
6ab43e1b | 646 | // parameters for different test cases, check each array key for the specific test-case |
9099cab3 CW |
647 | $testCases = [ |
648 | 'with-no-activity' => [ | |
649 | 'params' => [ | |
5161bb0c | 650 | 'contact_id' => 17, |
651 | 'admin' => FALSE, | |
652 | 'caseId' => NULL, | |
653 | 'context' => 'home', | |
654 | 'activity_type_id' => NULL, | |
655 | 'offset' => 0, | |
656 | 'rowCount' => 0, | |
657 | 'sort' => NULL, | |
9099cab3 CW |
658 | ], |
659 | ], | |
660 | 'with-activity' => [ | |
661 | 'params' => [ | |
5161bb0c | 662 | 'contact_id' => 1, |
663 | 'admin' => FALSE, | |
664 | 'caseId' => NULL, | |
665 | 'context' => 'home', | |
666 | 'activity_type_id' => NULL, | |
667 | 'offset' => 0, | |
668 | 'rowCount' => 0, | |
669 | 'sort' => NULL, | |
9099cab3 CW |
670 | ], |
671 | ], | |
672 | 'with-activity_type' => [ | |
673 | 'params' => [ | |
5161bb0c | 674 | 'contact_id' => 3, |
675 | 'admin' => FALSE, | |
676 | 'caseId' => NULL, | |
677 | 'context' => 'home', | |
678 | 'activity_type_id' => 2, | |
679 | 'offset' => 0, | |
680 | 'rowCount' => 0, | |
681 | 'sort' => NULL, | |
9099cab3 CW |
682 | ], |
683 | ], | |
684 | 'exclude-all-activity_type' => [ | |
685 | 'params' => [ | |
5161bb0c | 686 | 'contact_id' => 3, |
687 | 'admin' => FALSE, | |
688 | 'caseId' => NULL, | |
689 | 'context' => 'home', | |
9099cab3 | 690 | 'activity_type_exclude_id' => [1, 2], |
5161bb0c | 691 | 'offset' => 0, |
692 | 'rowCount' => 0, | |
693 | 'sort' => NULL, | |
9099cab3 CW |
694 | ], |
695 | ], | |
696 | 'sort-by-subject' => [ | |
697 | 'params' => [ | |
5161bb0c | 698 | 'contact_id' => 1, |
699 | 'admin' => FALSE, | |
700 | 'caseId' => NULL, | |
701 | 'context' => 'home', | |
702 | 'activity_type_id' => NULL, | |
703 | 'offset' => 0, | |
704 | 'rowCount' => 0, | |
705 | 'sort' => 'subject DESC', | |
9099cab3 CW |
706 | ], |
707 | ], | |
708 | ]; | |
5161bb0c | 709 | |
710 | foreach ($testCases as $caseName => $testCase) { | |
6e793248 | 711 | $activityCount = CRM_Activity_BAO_Activity::getActivitiesCount($testCase['params']); |
6ab43e1b | 712 | $activitiesNew = CRM_Activity_BAO_Activity::getActivities($testCase['params']); |
713 | ||
9099cab3 | 714 | foreach ([$activitiesNew] as $activities) { |
6ab43e1b | 715 | //$this->assertEquals($activityCount, CRM_Activity_BAO_Activity::getActivitiesCount($testCase['params'])); |
716 | if ($caseName == 'with-no-activity') { | |
717 | $this->assertEquals(0, count($activities)); | |
718 | $this->assertEquals(0, $activityCount); | |
719 | } | |
720 | elseif ($caseName == 'with-activity') { | |
721 | // contact id 1 is assigned as source, target and assignee for activity id 1, 7 and 8 respectively | |
722 | $this->assertEquals(3, count($activities)); | |
723 | $this->assertEquals(3, $activityCount); | |
724 | $this->assertEquals(1, $activities[1]['source_contact_id']); | |
725 | // @todo - this is a discrepancy between old & new - review. | |
726 | //$this->assertEquals(TRUE, array_key_exists(1, $activities[7]['target_contact_name'])); | |
727 | $this->assertEquals(TRUE, array_key_exists(1, $activities[8]['assignee_contact_name'])); | |
728 | } | |
729 | elseif ($caseName == 'with-activity_type') { | |
730 | // contact id 3 for activity type 2 is assigned as assignee, source and target for | |
731 | // activity id 1, 3 and 8 respectively | |
732 | $this->assertEquals(3, count($activities)); | |
733 | $this->assertEquals(3, $activityCount); | |
734 | // ensure activity type id is 2 | |
735 | $this->assertEquals(2, $activities[1]['activity_type_id']); | |
736 | $this->assertEquals(3, $activities[3]['source_contact_id']); | |
737 | // @todo review inconsistency between 2 versions. | |
738 | // $this->assertEquals(TRUE, array_key_exists(3, $activities[8]['target_contact_name'])); | |
739 | $this->assertEquals(TRUE, array_key_exists(3, $activities[1]['assignee_contact_name'])); | |
740 | } | |
741 | if ($caseName == 'exclude-all-activity_type') { | |
742 | $this->assertEquals(0, count($activities)); | |
743 | $this->assertEquals(0, $activityCount); | |
744 | } | |
745 | if ($caseName == 'sort-by-subject') { | |
746 | $this->assertEquals(3, count($activities)); | |
747 | $this->assertEquals(3, $activityCount); | |
748 | // activities should be order by 'subject DESC' | |
9099cab3 | 749 | $subjectOrder = [ |
6ab43e1b | 750 | 'subject 8', |
751 | 'subject 7', | |
752 | 'subject 1', | |
9099cab3 | 753 | ]; |
6ab43e1b | 754 | $count = 0; |
755 | foreach ($activities as $activity) { | |
756 | $this->assertEquals($subjectOrder[$count], $activity['subject']); | |
757 | $count++; | |
758 | } | |
5161bb0c | 759 | } |
760 | } | |
761 | } | |
6a488035 | 762 | } |
96025800 | 763 | |
49d4d222 | 764 | /** |
765 | * CRM-20793 : Test getActivities by using activity date and status filter | |
766 | */ | |
afc82ed3 | 767 | public function testByActivityDateAndStatus() { |
6e793248 | 768 | CRM_Core_Config::singleton()->userPermissionClass->permissions = ['view all contacts', 'access CiviCRM']; |
a6c2ebdc | 769 | $this->createTestActivities(); |
49d4d222 | 770 | |
771 | // activity IDs catagorised by date | |
9099cab3 CW |
772 | $lastWeekActivities = [1, 2, 3]; |
773 | $todayActivities = [4, 5, 6, 7]; | |
774 | $lastTwoMonthsActivities = [8, 9, 10, 11]; | |
775 | $lastOrNextYearActivities = [12, 13, 14, 15, 16]; | |
49d4d222 | 776 | |
777 | // date values later used to set activity date value | |
778 | $lastWeekDate = date('YmdHis', strtotime('1 week ago')); | |
779 | $today = date('YmdHis'); | |
a6d192c8 | 780 | $lastTwoMonthAgoDate = date('YmdHis', strtotime('2 months ago')); |
daff7f26 | 781 | // if current month is Jan then choose next year date otherwise the search result will include |
782 | // the previous week and last two months activities which are still in previous year and hence leads to improper result | |
783 | $lastOrNextYearDate = (date('M') == 'Jan') ? date('YmdHis', strtotime('+1 year')) : date('YmdHis', strtotime('1 year ago')); | |
49d4d222 | 784 | for ($i = 1; $i <= 16; $i++) { |
785 | if (in_array($i, $lastWeekActivities)) { | |
786 | $date = $lastWeekDate; | |
787 | } | |
a6d192c8 | 788 | elseif (in_array($i, $lastTwoMonthsActivities)) { |
789 | $date = $lastTwoMonthAgoDate; | |
49d4d222 | 790 | } |
daff7f26 | 791 | elseif (in_array($i, $lastOrNextYearActivities)) { |
792 | $date = $lastOrNextYearDate; | |
49d4d222 | 793 | } |
794 | elseif (in_array($i, $todayActivities)) { | |
795 | $date = $today; | |
796 | } | |
9099cab3 | 797 | $this->callAPISuccess('Activity', 'create', [ |
49d4d222 | 798 | 'id' => $i, |
799 | 'activity_date_time' => $date, | |
9099cab3 | 800 | ]); |
49d4d222 | 801 | } |
802 | ||
803 | // parameters for different test cases, check each array key for the specific test-case | |
9099cab3 CW |
804 | $testCases = [ |
805 | 'todays-activity' => [ | |
806 | 'params' => [ | |
49d4d222 | 807 | 'contact_id' => 1, |
808 | 'admin' => TRUE, | |
809 | 'caseId' => NULL, | |
810 | 'context' => 'activity', | |
6e793248 | 811 | 'activity_date_time_relative' => 'this.day', |
49d4d222 | 812 | 'activity_type_id' => NULL, |
813 | 'offset' => 0, | |
814 | 'rowCount' => 0, | |
815 | 'sort' => NULL, | |
9099cab3 CW |
816 | ], |
817 | ], | |
818 | 'todays-activity-filtered-by-range' => [ | |
819 | 'params' => [ | |
49d4d222 | 820 | 'contact_id' => 1, |
821 | 'admin' => TRUE, | |
822 | 'caseId' => NULL, | |
823 | 'context' => 'activity', | |
6e793248 | 824 | 'activity_date_time_low' => date('Y/m/d', strtotime('yesterday')), |
825 | 'activity_date_time_high' => date('Y/m/d'), | |
49d4d222 | 826 | 'activity_type_id' => NULL, |
827 | 'offset' => 0, | |
828 | 'rowCount' => 0, | |
829 | 'sort' => NULL, | |
9099cab3 CW |
830 | ], |
831 | ], | |
832 | 'last-week-activity' => [ | |
833 | 'params' => [ | |
49d4d222 | 834 | 'contact_id' => 1, |
835 | 'admin' => TRUE, | |
836 | 'caseId' => NULL, | |
837 | 'context' => 'activity', | |
6e793248 | 838 | 'activity_date_time_relative' => 'previous.week', |
49d4d222 | 839 | 'activity_type_id' => NULL, |
840 | 'offset' => 0, | |
841 | 'rowCount' => 0, | |
842 | 'sort' => NULL, | |
9099cab3 CW |
843 | ], |
844 | ], | |
845 | 'this-quarter-activity' => [ | |
846 | 'params' => [ | |
49d4d222 | 847 | 'contact_id' => 1, |
848 | 'admin' => TRUE, | |
849 | 'caseId' => NULL, | |
850 | 'context' => 'activity', | |
6e793248 | 851 | 'activity_date_time_relative' => 'this.quarter', |
49d4d222 | 852 | 'activity_type_id' => NULL, |
853 | 'offset' => 0, | |
854 | 'rowCount' => 0, | |
855 | 'sort' => NULL, | |
9099cab3 CW |
856 | ], |
857 | ], | |
858 | 'activity-of-all-statuses' => [ | |
859 | 'params' => [ | |
49d4d222 | 860 | 'contact_id' => 1, |
861 | 'admin' => TRUE, | |
862 | 'caseId' => NULL, | |
863 | 'context' => 'activity', | |
864 | 'activity_status_id' => '1,2', | |
865 | 'activity_type_id' => NULL, | |
866 | 'offset' => 0, | |
867 | 'rowCount' => 0, | |
868 | 'sort' => NULL, | |
9099cab3 CW |
869 | ], |
870 | ], | |
871 | ]; | |
49d4d222 | 872 | |
873 | foreach ($testCases as $caseName => $testCase) { | |
6e793248 | 874 | CRM_Utils_Date::convertFormDateToApiFormat($testCase['params'], 'activity_date_time', FALSE); |
875 | $activities = CRM_Activity_BAO_Activity::getActivities($testCase['params']); | |
876 | $activityCount = CRM_Activity_BAO_Activity::getActivitiesCount($testCase['params']); | |
877 | asort($activities); | |
878 | $activityIDs = array_keys($activities); | |
49d4d222 | 879 | |
880 | if ($caseName == 'todays-activity' || $caseName == 'todays-activity-filtered-by-range') { | |
6e793248 | 881 | // Only one of the 4 activities today relates to contact id 1. |
882 | $this->assertEquals(1, $activityCount); | |
883 | $this->assertEquals(1, count($activities)); | |
884 | $this->assertEquals([7], array_keys($activities)); | |
49d4d222 | 885 | } |
886 | elseif ($caseName == 'last-week-activity') { | |
6e793248 | 887 | // Only one of the 3 activities today relates to contact id 1. |
888 | $this->assertEquals(1, $activityCount); | |
889 | $this->assertEquals(1, count($activities)); | |
890 | $this->assertEquals([1], $activityIDs); | |
49d4d222 | 891 | } |
a6d192c8 | 892 | elseif ($caseName == 'lhis-quarter-activity') { |
893 | $this->assertEquals(count($lastTwoMonthsActivities), $activityCount); | |
6e793248 | 894 | $this->assertEquals(count($lastTwoMonthsActivities), count($activities)); |
a6d192c8 | 895 | $this->checkArrayEquals($lastTwoMonthsActivities, $activityIDs); |
49d4d222 | 896 | } |
daff7f26 | 897 | elseif ($caseName == 'last-or-next-year-activity') { |
898 | $this->assertEquals(count($lastOrNextYearActivities), $activityCount); | |
6e793248 | 899 | $this->assertEquals(count($lastOrNextYearActivities), count($activities)); |
daff7f26 | 900 | $this->checkArrayEquals($lastOrNextYearActivities, $activityIDs); |
49d4d222 | 901 | } |
902 | elseif ($caseName == 'activity-of-all-statuses') { | |
6e793248 | 903 | $this->assertEquals(3, $activityCount); |
904 | $this->assertEquals(3, count($activities)); | |
49d4d222 | 905 | } |
906 | } | |
907 | } | |
908 | ||
afc82ed3 | 909 | /** |
910 | * @dataProvider getActivityDateData | |
911 | */ | |
912 | public function testActivityRelativeDateFilter($params, $expected) { | |
913 | $thisYear = date('Y'); | |
914 | $dates = [ | |
915 | date('Y-m-d', strtotime(($thisYear - 1) . '-01-01')), | |
916 | date('Y-m-d', strtotime(($thisYear - 1) . '-12-31')), | |
917 | date('Y-m-d', strtotime($thisYear . '-01-01')), | |
918 | date('Y-m-d', strtotime($thisYear . '-12-31')), | |
919 | date('Y-m-d', strtotime(($thisYear + 1) . '-01-01')), | |
920 | date('Y-m-d', strtotime(($thisYear + 1) . '-12-31')), | |
921 | ]; | |
922 | foreach ($dates as $date) { | |
923 | $this->activityCreate(['activity_date_time' => $date]); | |
924 | } | |
6e793248 | 925 | $activitiesDep = CRM_Activity_BAO_Activity::getActivities($params); |
926 | $activityCount = CRM_Activity_BAO_Activity::getActivitiesCount($params); | |
afc82ed3 | 927 | $this->assertEquals(count($activitiesDep), $activityCount); |
928 | foreach ($activitiesDep as $activity) { | |
929 | $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'])); | |
930 | $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'])); | |
931 | } | |
932 | ||
933 | } | |
934 | ||
935 | /** | |
936 | * Get activity date data. | |
937 | * | |
938 | * Later we might migrate rework the rest of | |
939 | * testByActivityDateAndStatus | |
940 | * to use data provider methodology as it's way complex! | |
941 | * | |
942 | * @return array | |
943 | */ | |
944 | public function getActivityDateData() { | |
945 | return [ | |
946 | 'last-year-activity' => [ | |
947 | 'params' => [ | |
948 | 'contact_id' => 1, | |
949 | 'admin' => TRUE, | |
950 | 'caseId' => NULL, | |
951 | 'context' => 'activity', | |
952 | 'activity_date_relative' => 'previous.year', | |
953 | 'activity_type_id' => NULL, | |
954 | 'offset' => 0, | |
955 | 'rowCount' => 0, | |
956 | 'sort' => NULL, | |
957 | ], | |
958 | 'expected' => [ | |
959 | 'count' => 2, | |
960 | 'earliest' => strtotime('first day of january last year'), | |
961 | 'latest' => strtotime('first day of january this year'), | |
39b959db | 962 | ], |
afc82ed3 | 963 | ], |
964 | ]; | |
965 | } | |
966 | ||
3cf1fae9 | 967 | /** |
968 | * CRM-20308: Test from email address when a 'copy of Activity' event occur | |
969 | */ | |
970 | public function testEmailAddressOfActivityCopy() { | |
971 | // Case 1: assert the 'From' Email Address of source Actvity Contact ID | |
972 | // create activity with source contact ID which has email address | |
973 | $assigneeContactId = $this->individualCreate(); | |
9099cab3 | 974 | $sourceContactParams = [ |
3cf1fae9 | 975 | 'first_name' => 'liz', |
976 | 'last_name' => 'hurleey', | |
977 | 'email' => substr(sha1(rand()), 0, 7) . '@testemail.com', | |
9099cab3 | 978 | ]; |
3cf1fae9 | 979 | $sourceContactID = $this->individualCreate($sourceContactParams); |
980 | $sourceDisplayName = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $sourceContactID, 'display_name'); | |
981 | ||
982 | // create an activity using API | |
9099cab3 | 983 | $params = [ |
3cf1fae9 | 984 | 'source_contact_id' => $sourceContactID, |
985 | 'subject' => 'Scheduling Meeting ' . substr(sha1(rand()), 0, 4), | |
986 | 'activity_type_id' => CRM_Core_PseudoConstant::getKey('CRM_Activity_BAO_Activity', 'activity_type_id', 'Meeting'), | |
9099cab3 | 987 | 'assignee_contact_id' => [$assigneeContactId], |
3cf1fae9 | 988 | 'activity_date_time' => date('Ymd'), |
9099cab3 | 989 | ]; |
3cf1fae9 | 990 | $activity = $this->callAPISuccess('Activity', 'create', $params); |
991 | ||
992 | // Check that from address is in "Source-Display-Name <source-email>" | |
993 | $formAddress = CRM_Case_BAO_Case::getReceiptFrom($activity['id']); | |
994 | $expectedFromAddress = sprintf("%s <%s>", $sourceDisplayName, $sourceContactParams['email']); | |
995 | $this->assertEquals($expectedFromAddress, $formAddress); | |
3cf1fae9 | 996 | |
997 | // Case 2: System Default From Address | |
998 | // but first erase the email address of existing source contact ID | |
9099cab3 | 999 | $withoutEmailParams = [ |
3cf1fae9 | 1000 | 'email' => '', |
9099cab3 | 1001 | ]; |
3cf1fae9 | 1002 | $sourceContactID = $this->individualCreate($withoutEmailParams); |
9099cab3 | 1003 | $params = [ |
3cf1fae9 | 1004 | 'source_contact_id' => $sourceContactID, |
1005 | 'subject' => 'Scheduling Meeting ' . substr(sha1(rand()), 0, 4), | |
1006 | 'activity_type_id' => CRM_Core_PseudoConstant::getKey('CRM_Activity_BAO_Activity', 'activity_type_id', 'Meeting'), | |
1007 | 'activity_date_time' => date('Ymd'), | |
9099cab3 | 1008 | ]; |
3cf1fae9 | 1009 | $activity = $this->callAPISuccess('Activity', 'create', $params); |
1010 | // fetch domain info | |
9099cab3 | 1011 | $domainInfo = $this->callAPISuccess('Domain', 'getsingle', ['id' => CRM_Core_Config::domainID()]); |
3cf1fae9 | 1012 | |
1013 | $formAddress = CRM_Case_BAO_Case::getReceiptFrom($activity['id']); | |
1014 | if (!empty($domainInfo['from_email'])) { | |
1015 | $expectedFromAddress = sprintf("%s <%s>", $domainInfo['from_name'], $domainInfo['from_email']); | |
1016 | } | |
1017 | // Case 3: fetch default Organization Contact email address | |
1018 | elseif (!empty($domainInfo['domain_email'])) { | |
1019 | $expectedFromAddress = sprintf("%s <%s>", $domainInfo['name'], $domainInfo['domain_email']); | |
1020 | } | |
e88906fc | 1021 | $this->assertEquals($expectedFromAddress, $formAddress); |
3cf1fae9 | 1022 | |
1023 | // TODO: Case 4 about checking the $formAddress on basis of logged contact ID respectively needs, | |
1024 | // to change the domain setting, which isn't straight forward in test environment | |
1025 | } | |
1026 | ||
75d842f8 | 1027 | /** |
1028 | * Set up for testing activity queries. | |
1029 | */ | |
1030 | protected function setUpForActivityDashboardTests() { | |
a6c2ebdc | 1031 | $this->createTestActivities(); |
75d842f8 | 1032 | |
9099cab3 | 1033 | $this->_params = [ |
75d842f8 | 1034 | 'contact_id' => NULL, |
1035 | 'admin' => TRUE, | |
1036 | 'caseId' => NULL, | |
1037 | 'context' => 'home', | |
1038 | 'activity_type_id' => NULL, | |
39b959db SL |
1039 | // for dashlet the Scheduled status is set by default |
1040 | 'activity_status_id' => CRM_Core_PseudoConstant::getKey('CRM_Activity_BAO_Activity', 'status_id', 'Scheduled'), | |
75d842f8 | 1041 | 'offset' => 0, |
1042 | 'rowCount' => 0, | |
1043 | 'sort' => NULL, | |
9099cab3 | 1044 | ]; |
75d842f8 | 1045 | } |
1046 | ||
824989b9 AS |
1047 | public function testSendEmailBasic() { |
1048 | $contactId = $this->individualCreate(); | |
1049 | ||
1050 | // create a logged in USER since the code references it for sendEmail user. | |
1051 | $this->createLoggedInUser(); | |
1052 | $session = CRM_Core_Session::singleton(); | |
1053 | $loggedInUser = $session->get('userID'); | |
1054 | ||
9099cab3 CW |
1055 | $contact = $this->civicrm_api('contact', 'getsingle', ['id' => $contactId, 'version' => $this->_apiversion]); |
1056 | $contactDetailsIntersectKeys = [ | |
824989b9 AS |
1057 | 'contact_id' => '', |
1058 | 'sort_name' => '', | |
1059 | 'display_name' => '', | |
1060 | 'do_not_email' => '', | |
1061 | 'preferred_mail_format' => '', | |
1062 | 'is_deceased' => '', | |
1063 | 'email' => '', | |
1064 | 'on_hold' => '', | |
9099cab3 CW |
1065 | ]; |
1066 | $contactDetails = [ | |
824989b9 | 1067 | array_intersect_key($contact, $contactDetailsIntersectKeys), |
9099cab3 | 1068 | ]; |
824989b9 AS |
1069 | |
1070 | $subject = __FUNCTION__ . ' subject'; | |
1071 | $html = __FUNCTION__ . ' html'; | |
1072 | $text = __FUNCTION__ . ' text'; | |
1073 | $userID = $loggedInUser; | |
1074 | ||
1075 | list($sent, $activity_id) = $email_result = CRM_Activity_BAO_Activity::sendEmail( | |
1076 | $contactDetails, | |
1077 | $subject, | |
1078 | $text, | |
1079 | $html, | |
1080 | $contact['email'], | |
1081 | $userID, | |
1082 | $from = __FUNCTION__ . '@example.com' | |
1083 | ); | |
1084 | ||
9099cab3 | 1085 | $activity = $this->civicrm_api('activity', 'getsingle', ['id' => $activity_id, 'version' => $this->_apiversion]); |
824989b9 AS |
1086 | $details = "-ALTERNATIVE ITEM 0- |
1087 | $html | |
1088 | -ALTERNATIVE ITEM 1- | |
1089 | $text | |
1090 | -ALTERNATIVE END- | |
1091 | "; | |
1092 | $this->assertEquals($activity['details'], $details, 'Activity details does not match.'); | |
1093 | $this->assertEquals($activity['subject'], $subject, 'Activity subject does not match.'); | |
1094 | } | |
1095 | ||
1096 | public function testSendEmailWithCampaign() { | |
1097 | // Create a contact and contactDetails array. | |
1098 | $contactId = $this->individualCreate(); | |
1099 | ||
1100 | // create a logged in USER since the code references it for sendEmail user. | |
1101 | $this->createLoggedInUser(); | |
1102 | $session = CRM_Core_Session::singleton(); | |
1103 | $loggedInUser = $session->get('userID'); | |
1104 | ||
9099cab3 CW |
1105 | $contact = $this->civicrm_api('contact', 'getsingle', ['id' => $contactId, 'version' => $this->_apiversion]); |
1106 | $contactDetailsIntersectKeys = [ | |
824989b9 AS |
1107 | 'contact_id' => '', |
1108 | 'sort_name' => '', | |
1109 | 'display_name' => '', | |
1110 | 'do_not_email' => '', | |
1111 | 'preferred_mail_format' => '', | |
1112 | 'is_deceased' => '', | |
1113 | 'email' => '', | |
1114 | 'on_hold' => '', | |
9099cab3 CW |
1115 | ]; |
1116 | $contactDetails = [ | |
824989b9 | 1117 | array_intersect_key($contact, $contactDetailsIntersectKeys), |
9099cab3 | 1118 | ]; |
824989b9 AS |
1119 | |
1120 | // Create a campaign. | |
9099cab3 | 1121 | $result = $this->civicrm_api('Campaign', 'create', [ |
824989b9 AS |
1122 | 'version' => $this->_apiversion, |
1123 | 'title' => __FUNCTION__ . ' campaign', | |
9099cab3 | 1124 | ]); |
824989b9 AS |
1125 | $campaign_id = $result['id']; |
1126 | ||
1127 | $subject = __FUNCTION__ . ' subject'; | |
1128 | $html = __FUNCTION__ . ' html'; | |
1129 | $text = __FUNCTION__ . ' text'; | |
1130 | $userID = $loggedInUser; | |
1131 | ||
1132 | list($sent, $activity_id) = $email_result = CRM_Activity_BAO_Activity::sendEmail( | |
1133 | $contactDetails, | |
1134 | $subject, | |
1135 | $text, | |
1136 | $html, | |
1137 | $contact['email'], | |
1138 | $userID, | |
1139 | $from = __FUNCTION__ . '@example.com', | |
1140 | $attachments = NULL, | |
1141 | $cc = NULL, | |
1142 | $bcc = NULL, | |
1143 | $contactIds = NULL, | |
1144 | $additionalDetails = NULL, | |
cb5d08cd | 1145 | NULL, |
824989b9 AS |
1146 | $campaign_id |
1147 | ); | |
9099cab3 | 1148 | $activity = $this->civicrm_api('activity', 'getsingle', ['id' => $activity_id, 'version' => $this->_apiversion]); |
824989b9 AS |
1149 | $this->assertEquals($activity['campaign_id'], $campaign_id, 'Activity campaign_id does not match.'); |
1150 | } | |
1151 | ||
63483feb MM |
1152 | /** |
1153 | * @expectedException CRM_Core_Exception | |
1154 | * @expectedExceptionMessage You do not have the 'send SMS' permission | |
1155 | */ | |
1156 | public function testSendSMSWithoutPermission() { | |
1157 | $dummy = NULL; | |
1158 | $session = CRM_Core_Session::singleton(); | |
9099cab3 | 1159 | CRM_Core_Config::singleton()->userPermissionClass->permissions = ['access CiviCRM']; |
63483feb MM |
1160 | |
1161 | CRM_Activity_BAO_Activity::sendSMS( | |
1162 | $dummy, | |
1163 | $dummy, | |
1164 | $dummy, | |
1165 | $dummy, | |
1166 | $session->get('userID') | |
1167 | ); | |
1168 | } | |
1169 | ||
1a7f0799 MW |
1170 | public function testSendSmsNoPhoneNumber() { |
1171 | list($sent, $activityId, $success) = $this->createSendSmsTest(0); | |
9099cab3 | 1172 | $activity = $this->civicrm_api('activity', 'getsingle', ['id' => $activityId, 'version' => $this->_apiversion]); |
1a7f0799 MW |
1173 | |
1174 | $outBoundSmsActivityId = CRM_Core_PseudoConstant::getKey('CRM_Activity_BAO_Activity', 'activity_type_id', 'SMS'); | |
1175 | $activityStatusCompleted = CRM_Core_PseudoConstant::getKey('CRM_Activity_BAO_Activity', 'activity_status_id', 'Completed'); | |
1176 | $details = 'createSendSmsTest text'; | |
1177 | $this->assertEquals($activity['activity_type_id'], $outBoundSmsActivityId, 'Wrong activity type is set.'); | |
1178 | $this->assertEquals($activity['status_id'], $activityStatusCompleted, 'Expected activity status Completed.'); | |
1179 | $this->assertEquals($activity['subject'], 'createSendSmsTest subject', 'Activity subject does not match.'); | |
1180 | $this->assertEquals($activity['details'], $details, 'Activity details does not match.'); | |
4a0e3fe7 | 1181 | $this->assertEquals("Recipient phone number is invalid or recipient does not want to receive SMS", $sent[0], "Expected error doesn't match"); |
1a7f0799 MW |
1182 | $this->assertEquals(0, $success, "Expected success to be 0"); |
1183 | } | |
1184 | ||
1185 | public function testSendSmsFixedPhoneNumber() { | |
1186 | list($sent, $activityId, $success) = $this->createSendSmsTest(1); | |
9099cab3 | 1187 | $activity = $this->civicrm_api('activity', 'getsingle', ['id' => $activityId, 'version' => $this->_apiversion]); |
1a7f0799 MW |
1188 | |
1189 | $outBoundSmsActivityId = CRM_Core_PseudoConstant::getKey('CRM_Activity_BAO_Activity', 'activity_type_id', 'SMS'); | |
1190 | $activityStatusCompleted = CRM_Core_PseudoConstant::getKey('CRM_Activity_BAO_Activity', 'activity_status_id', 'Completed'); | |
1191 | $details = 'createSendSmsTest text'; | |
1192 | $this->assertEquals($activity['activity_type_id'], $outBoundSmsActivityId, 'Wrong activity type is set.'); | |
1193 | $this->assertEquals($activity['status_id'], $activityStatusCompleted, 'Expected activity status Completed.'); | |
1194 | $this->assertEquals($activity['subject'], 'createSendSmsTest subject', 'Activity subject does not match.'); | |
1195 | $this->assertEquals($activity['details'], $details, 'Activity details does not match.'); | |
4a0e3fe7 | 1196 | $this->assertEquals("Recipient phone number is invalid or recipient does not want to receive SMS", $sent[0], "Expected error doesn't match"); |
1a7f0799 MW |
1197 | $this->assertEquals(0, $success, "Expected success to be 0"); |
1198 | } | |
1199 | ||
1200 | public function testSendSmsMobilePhoneNumber() { | |
1201 | list($sent, $activityId, $success) = $this->createSendSmsTest(2); | |
9099cab3 | 1202 | $activity = $this->civicrm_api('activity', 'getsingle', ['id' => $activityId, 'version' => $this->_apiversion]); |
1a7f0799 MW |
1203 | |
1204 | $outBoundSmsActivityId = CRM_Core_PseudoConstant::getKey('CRM_Activity_BAO_Activity', 'activity_type_id', 'SMS'); | |
1205 | $activityStatusCompleted = CRM_Core_PseudoConstant::getKey('CRM_Activity_BAO_Activity', 'activity_status_id', 'Completed'); | |
1206 | $details = 'createSendSmsTest text'; | |
1207 | $this->assertEquals($activity['activity_type_id'], $outBoundSmsActivityId, 'Wrong activity type is set.'); | |
1208 | $this->assertEquals($activity['status_id'], $activityStatusCompleted, 'Expected activity status Completed.'); | |
1209 | $this->assertEquals($activity['subject'], 'createSendSmsTest subject', 'Activity subject does not match.'); | |
1210 | $this->assertEquals($activity['details'], $details, 'Activity details does not match.'); | |
1211 | $this->assertEquals(TRUE, $sent, "Expected sent should be true"); | |
1212 | $this->assertEquals(1, $success, "Expected success to be 1"); | |
1213 | } | |
1214 | ||
a9b7ee41 SL |
1215 | /** |
1216 | * Test that when a numbe ris specified in the To Param of the SMS provider parameters that an SMS is sent | |
1217 | * @see dev/core/#273 | |
1218 | */ | |
1219 | public function testSendSMSMobileInToProviderParam() { | |
1220 | list($sent, $activityId, $success) = $this->createSendSmsTest(2, TRUE); | |
1221 | $this->assertEquals(TRUE, $sent, "Expected sent should be true"); | |
1222 | $this->assertEquals(1, $success, "Expected success to be 1"); | |
1223 | } | |
1224 | ||
1225 | /** | |
1226 | * Test that when a numbe ris specified in the To Param of the SMS provider parameters that an SMS is sent | |
1227 | * @see dev/core/#273 | |
1228 | */ | |
1229 | public function testSendSMSMobileInToProviderParamWithDoNotSMS() { | |
1230 | list($sent, $activityId, $success) = $this->createSendSmsTest(2, TRUE, ['do_not_sms' => 1]); | |
1231 | foreach ($sent as $error) { | |
4a0e3fe7 | 1232 | $this->assertEquals('Contact Does not accept SMS', $error); |
a9b7ee41 SL |
1233 | } |
1234 | $this->assertEquals(1, count($sent), "Expected sent should a PEAR Error"); | |
1235 | $this->assertEquals(0, $success, "Expected success to be 0"); | |
1236 | } | |
1237 | ||
1a7f0799 MW |
1238 | /** |
1239 | * @param int $phoneType (0=no phone, phone_type option group (1=fixed, 2=mobile) | |
a9b7ee41 SL |
1240 | * @param bool $passPhoneTypeInContactDetails |
1241 | * @param array $additionalContactParams additional contact creation params | |
1a7f0799 | 1242 | */ |
a9b7ee41 | 1243 | public function createSendSmsTest($phoneType = 0, $passPhoneTypeInContactDetails = FALSE, $additionalContactParams = []) { |
9099cab3 | 1244 | $provider = civicrm_api3('SmsProvider', 'create', [ |
1a7f0799 MW |
1245 | 'name' => "CiviTestSMSProvider", |
1246 | 'api_type' => "1", | |
1247 | "username" => "1", | |
1248 | "password" => "1", | |
1249 | "api_type" => "1", | |
1250 | "api_url" => "1", | |
1251 | "api_params" => "a=1", | |
1252 | "is_default" => "1", | |
1253 | "is_active" => "1", | |
1254 | "domain_id" => "1", | |
9099cab3 | 1255 | ]); |
a9b7ee41 | 1256 | |
1a7f0799 MW |
1257 | $smsProviderParams['provider_id'] = $provider['id']; |
1258 | ||
1259 | // Create a contact | |
1260 | $contactId = $this->individualCreate(); | |
a9b7ee41 SL |
1261 | if (!empty($additionalContactParams)) { |
1262 | $this->callAPISuccess('contact', 'create', ['id' => $contactId] + $additionalContactParams); | |
1263 | } | |
1264 | $contactsResult = $this->callApiSuccess('contact', 'get', ['id' => $contactId, 'version' => $this->_apiversion]); | |
1a7f0799 MW |
1265 | $contactDetails = $contactsResult['values']; |
1266 | ||
1267 | // Get contactIds from contact details | |
1268 | foreach ($contactDetails as $contact) { | |
1269 | $contactIds[] = $contact['contact_id']; | |
1270 | } | |
1271 | ||
1272 | $activityParams['sms_text_message'] = __FUNCTION__ . ' text'; | |
1273 | $activityParams['activity_subject'] = __FUNCTION__ . ' subject'; | |
1274 | ||
1a7f0799 MW |
1275 | // Get a "logged in" user to set as source of Sms. |
1276 | $session = CRM_Core_Session::singleton(); | |
1277 | $sourceContactId = $session->get('userID'); | |
1278 | ||
a627697d | 1279 | // Create a user |
1a7f0799 | 1280 | $this->_testSmsContactId = $this->createLoggedInUser(); |
a627697d MW |
1281 | |
1282 | // Give user permission to 'send SMS' | |
9099cab3 | 1283 | CRM_Core_Config::singleton()->userPermissionClass->permissions = ['access CiviCRM', 'send SMS']; |
a627697d MW |
1284 | |
1285 | // Create a phone number | |
1a7f0799 MW |
1286 | switch ($phoneType) { |
1287 | case 0: | |
1288 | // No phone number | |
1289 | break; | |
1290 | ||
1291 | case 2: | |
1292 | // Create a mobile phone number | |
9099cab3 | 1293 | $phone = civicrm_api3('Phone', 'create', [ |
1a7f0799 MW |
1294 | 'contact_id' => $contactId, |
1295 | 'phone' => 123456, | |
1296 | 'phone_type_id' => "Mobile", | |
9099cab3 | 1297 | ]); |
a9b7ee41 SL |
1298 | if ($passPhoneTypeInContactDetails) { |
1299 | $contactDetails[$contactId]['phone'] = $phone['values'][$phone['id']]['phone']; | |
1300 | $contactDetails[$contactId]['phone_type_id'] = $phone['values'][$phone['id']]['phone_type_id']; | |
1301 | } | |
1a7f0799 MW |
1302 | break; |
1303 | ||
1304 | case 1: | |
1305 | // Create a fixed phone number | |
9099cab3 | 1306 | $phone = civicrm_api3('Phone', 'create', [ |
1a7f0799 MW |
1307 | 'contact_id' => $contactId, |
1308 | 'phone' => 654321, | |
1309 | 'phone_type_id' => "Phone", | |
9099cab3 | 1310 | ]); |
a9b7ee41 SL |
1311 | if ($passPhoneTypeInContactDetails) { |
1312 | $contactDetails[$contactId]['phone'] = $phone['values'][$phone['id']]['phone']; | |
1313 | $contactDetails[$contactId]['phone_type_id'] = $phone['values'][$phone['id']]['phone_type_id']; | |
1314 | } | |
1a7f0799 MW |
1315 | break; |
1316 | } | |
1317 | ||
1318 | // Now run the actual test | |
1319 | list($sent, $activityId, $success) = CRM_Activity_BAO_Activity::sendSms( | |
1320 | $contactDetails, | |
1321 | $activityParams, | |
1322 | $smsProviderParams, | |
1323 | $contactIds, | |
1324 | $sourceContactId | |
1325 | ); | |
1326 | ||
9099cab3 | 1327 | return [$sent, $activityId, $success]; |
1a7f0799 MW |
1328 | } |
1329 | ||
a6c2ebdc | 1330 | protected function createTestActivities() { |
1d291c60 | 1331 | $this->loadXMLDataSet(dirname(__FILE__) . '/activities_for_dashboard_count.xml'); |
c2ce41b6 | 1332 | // Make changes to improve variation in php since the xml method is brittle & relies on option values being unchanged. |
1333 | $this->callAPISuccess('Activity', 'create', ['id' => 12, 'activity_type_id' => 'Bulk Email']); | |
a6c2ebdc | 1334 | } |
1335 | ||
5123cb11 SL |
1336 | /** |
1337 | * ACL HOOK implementation for various tests | |
1338 | */ | |
1339 | public function hook_civicrm_aclWhereClause($type, &$tables, &$whereTables, &$contactID, &$where) { | |
1340 | if (!empty($this->allowedContactsACL)) { | |
1341 | $contact_id_list = implode(',', $this->allowedContactsACL); | |
1342 | $where = " contact_a.id IN ($contact_id_list)"; | |
1343 | } | |
1344 | } | |
1345 | ||
cb5108b8 | 1346 | public function testCaseTokens() { |
1347 | $caseTest = new CiviCaseTestCase(); | |
1348 | $caseTest->setUp(); | |
1349 | // Create a contact and contactDetails array. | |
1350 | $contactId = $this->individualCreate(); | |
1351 | ||
1352 | // create a case for this user | |
f7f1cc3b | 1353 | $result = $this->callAPISuccess('Case', 'create', [ |
cb5108b8 | 1354 | 'contact_id' => $contactId, |
1355 | 'case_type_id' => '1', | |
1356 | 'subject' => "my case", | |
1357 | 'status_id' => "Open", | |
f7f1cc3b | 1358 | ]); |
cb5108b8 | 1359 | |
1360 | $caseId = $result['id']; | |
1361 | $html_message = "<p>This is a test case with id: {case.id} and subject: {case.subject}</p>"; | |
1362 | $html_message = CRM_Utils_Token::replaceCaseTokens($caseId, $html_message); | |
1363 | ||
f7f1cc3b | 1364 | $this->assertTrue(strpos($html_message, 'id: ' . $caseId) !== 0); |
cb5108b8 | 1365 | $this->assertTrue(strpos($html_message, 'subject: my case') !== 0); |
f7f1cc3b | 1366 | $caseTest->tearDown(); |
cb5108b8 | 1367 | } |
1368 | ||
1369 | public function testSendEmailWithCaseId() { | |
f7f1cc3b SL |
1370 | $caseTest = new CiviCaseTestCase(); |
1371 | $caseTest->setUp(); | |
cb5108b8 | 1372 | // Create a contact and contactDetails array. |
1373 | $contactId = $this->individualCreate(); | |
f7f1cc3b | 1374 | $contact = $this->callAPISuccess('Contact', 'get', ['id' => $contactId]); |
cb5108b8 | 1375 | |
1376 | // create a logged in USER since the code references it for sendEmail user. | |
1377 | $this->createLoggedInUser(); | |
f7f1cc3b | 1378 | CRM_Core_Config::singleton()->userPermissionClass->permissions = ['view all contacts', 'access CiviCRM', 'access all cases and activities', 'administer CiviCase']; |
cb5108b8 | 1379 | $session = CRM_Core_Session::singleton(); |
1380 | $loggedInUser = $session->get('userID'); | |
1381 | ||
cb5108b8 | 1382 | // create a case for this user |
f7f1cc3b | 1383 | $result = $this->callAPISuccess('Case', 'create', [ |
cb5108b8 | 1384 | 'contact_id' => $contactId, |
1385 | 'case_type_id' => 1, | |
1386 | 'subject' => "my case", | |
1387 | 'status_id' => "Open", | |
f7f1cc3b | 1388 | ]); |
cb5108b8 | 1389 | |
1390 | $caseId = $result['id']; | |
1391 | ||
f7f1cc3b SL |
1392 | $subject = __FUNCTION__ . ' subject {case.subject}'; |
1393 | $html = __FUNCTION__ . ' html {case.subject}'; | |
cb5108b8 | 1394 | $text = __FUNCTION__ . ' text'; |
cb5108b8 | 1395 | |
f7f1cc3b | 1396 | $mut = new CiviMailUtils($this, TRUE); |
cb5108b8 | 1397 | list($sent, $activity_id) = $email_result = CRM_Activity_BAO_Activity::sendEmail( |
f7f1cc3b | 1398 | $contact['values'], |
cb5108b8 | 1399 | $subject, |
1400 | $text, | |
1401 | $html, | |
f7f1cc3b SL |
1402 | $contact['values'][$contactId]['email'], |
1403 | $loggedInUser, | |
cb5108b8 | 1404 | $from = __FUNCTION__ . '@example.com', |
cb5108b8 | 1405 | NULL, |
f7f1cc3b SL |
1406 | NULL, |
1407 | NULL, | |
1408 | [$contactId], | |
1409 | NULL, | |
1410 | NULL, | |
1411 | NULL, | |
cb5108b8 | 1412 | $caseId |
1413 | ); | |
f7f1cc3b SL |
1414 | $activity = $this->callAPISuccess('Activity', 'getsingle', ['id' => $activity_id, 'return' => ['case_id']]); |
1415 | $this->assertEquals($caseId, $activity['case_id'][0], 'Activity case_id does not match.'); | |
1416 | $mut->checkMailLog(['subject my case']); | |
1417 | $mut->stop(); | |
cb5108b8 | 1418 | } |
1419 | ||
6a488035 | 1420 | } |