unit test for case audit
[civicrm-core.git] / tests / phpunit / CRM / Case / XMLProcessor / ReportTest.php
1 <?php
2 require_once 'CiviTest/CiviCaseTestCase.php';
3
4 /**
5 * Class CRM_Case_XMLProcessor_ReportTest
6 * @group headless
7 */
8 class CRM_Case_XMLProcessor_ReportTest extends CiviCaseTestCase {
9
10 public function setUp() {
11 parent::setUp();
12
13 $this->simplifyCaseTypeDefinition();
14
15 $this->report = new CRM_Case_XMLProcessor_Report();
16 }
17
18 public function tearDown() {
19 parent::tearDown();
20 }
21
22 /**
23 * Test that getCaseReport has the right output.
24 *
25 * @param $activitySetName string Also acts as data provider test identifier.
26 * @param $expected array
27 *
28 * @dataProvider caseReportDataProvider
29 */
30 public function testGetCaseReport($activitySetName, $expected) {
31 $client_id = $this->individualCreate([
32 'first_name' => 'Casey',
33 'middle_name' => '',
34 'last_name' => 'Reportee',
35 'prefix_id' => NULL,
36 'suffix_id' => NULL,
37 ]);
38 $caseObj = $this->createCase($client_id, $this->_loggedInUser);
39 $case_id = $caseObj->id;
40
41 // Add an additional meeting activity not in the timeline to the case.
42 $meetingTypeId = $this->callAPISuccess('OptionValue', 'getsingle', [
43 'return' => ["value"],
44 'option_group_id' => 'activity_type',
45 'name' => 'Meeting',
46 ]);
47 $this->callAPISuccess('activity', 'create', [
48 'case_id' => $case_id,
49 'activity_type_id' => $meetingTypeId['value'],
50 'activity_date_time' => '20191114123456',
51 'subject' => 'Test Meeting',
52 'source_contact_id' => $this->_loggedInUser,
53 'target_contact_id' => $client_id,
54 ]);
55
56 $caseReportParams = [
57 'is_redact' => FALSE,
58 'include_activities' => 1,
59 ];
60
61 // run the thing we're testing and get the output vars
62 $template = CRM_Case_XMLProcessor_Report::populateCaseReportTemplate($client_id, $case_id, $activitySetName, $caseReportParams, $this->report);
63 $assigned_vars = $template->get_template_vars();
64
65 // Update $expected now since dataprovider doesn't have access to the variables from setup() because it runs before setup.
66 $this->updateExpectedBecauseDataProviderEvaluatesBeforeEverything($expected, $client_id, $case_id);
67
68 foreach ($expected as $key => $value) {
69 // does the assigned template var match the expected value?
70 $this->assertEquals($value, $assigned_vars[$key], "$activitySetName: $key does not match" . print_r($assigned_vars[$key], TRUE));
71 }
72 }
73
74 /**
75 * Data provider for testGetCaseReport
76 * @return array
77 */
78 public function caseReportDataProvider() {
79 return [
80 [
81 // activity set name
82 'standard_timeline',
83 // some expected assigned vars of CRM_Core_Smarty template
84 [
85 'case' => [
86 'clientName' => 'Casey Reportee',
87 'subject' => 'Case Subject',
88 'start_date' => '2019-11-14',
89 'end_date' => NULL,
90 'caseType' => 'Housing Support',
91 'caseTypeName' => 'housing_support',
92 'status' => 'Ongoing',
93 ],
94 'activities' => [
95 0 => [
96 'fields' => [
97 0 => [
98 'label' => 'Client',
99 'value' => 'Casey Reportee',
100 'type' => 'String',
101 ],
102 1 => [
103 'label' => 'Activity Type',
104 'value' => 'Open Case',
105 'type' => 'String',
106 ],
107 2 => [
108 'label' => 'Subject',
109 'value' => 'Case Subject',
110 'type' => 'Memo',
111 ],
112 3 => [
113 'label' => 'Created By',
114 // data providers run before everything, so update this later
115 'value' => 'placeholder',
116 'type' => 'String',
117 ],
118 4 => [
119 'label' => 'Reported By',
120 // see above - need to update this later
121 'value' => 'placeholder',
122 'type' => 'String',
123 ],
124 5 => [
125 'label' => 'Medium',
126 'value' => 'Phone',
127 'type' => 'String',
128 ],
129 6 => [
130 'label' => 'Location',
131 'value' => NULL,
132 'type' => 'String',
133 ],
134 7 => [
135 'label' => 'Date and Time',
136 'value' => '2019-11-14 00:00:00',
137 'type' => 'Date',
138 ],
139 8 => [
140 'label' => 'Details',
141 'value' => NULL,
142 'type' => 'Memo',
143 ],
144 9 => [
145 'label' => 'Status',
146 'value' => 'Completed',
147 'type' => 'String',
148 ],
149 10 => [
150 'label' => 'Priority',
151 'value' => 'Normal',
152 'type' => 'String',
153 ],
154 ],
155 'editURL' => 'placeholder',
156 'customGroups' => NULL,
157 ],
158 1 => [
159 'fields' => [
160 0 => [
161 'label' => 'Client',
162 'value' => 'Casey Reportee',
163 'type' => 'String',
164 ],
165 1 => [
166 'label' => 'Activity Type',
167 'value' => 'Medical evaluation',
168 'type' => 'String',
169 ],
170 2 => [
171 'label' => 'Subject',
172 'value' => '',
173 'type' => 'Memo',
174 ],
175 3 => [
176 'label' => 'Created By',
177 // see above - need to update this later
178 'value' => 'placeholder',
179 'type' => 'String',
180 ],
181 4 => [
182 'label' => 'Reported By',
183 // see above - need to update this later
184 'value' => 'placeholder',
185 'type' => 'String',
186 ],
187 5 => [
188 'label' => 'Location',
189 'value' => NULL,
190 'type' => 'String',
191 ],
192 6 => [
193 'label' => 'Date and Time',
194 'value' => '2019-11-15 00:00:00',
195 'type' => 'Date',
196 ],
197 7 => [
198 'label' => 'Details',
199 'value' => NULL,
200 'type' => 'Memo',
201 ],
202 8 => [
203 'label' => 'Status',
204 'value' => 'Scheduled',
205 'type' => 'String',
206 ],
207 9 => [
208 'label' => 'Priority',
209 'value' => 'Normal',
210 'type' => 'String',
211 ],
212 ],
213 'editURL' => 'placeholder',
214 'customGroups' => NULL,
215 ],
216 ],
217 ],
218 ],
219 [
220 // activity set name is blank here, meaning don't filter the activities
221 '',
222 // some expected assigned vars of CRM_Core_Smarty template
223 [
224 'case' => [
225 'clientName' => 'Casey Reportee',
226 'subject' => 'Case Subject',
227 'start_date' => '2019-11-14',
228 'end_date' => NULL,
229 'caseType' => 'Housing Support',
230 'caseTypeName' => 'housing_support',
231 'status' => 'Ongoing',
232 ],
233 'activities' => [
234 0 => [
235 'fields' => [
236 0 => [
237 'label' => 'Client',
238 'value' => 'Casey Reportee',
239 'type' => 'String',
240 ],
241 1 => [
242 'label' => 'Activity Type',
243 'value' => 'Open Case',
244 'type' => 'String',
245 ],
246 2 => [
247 'label' => 'Subject',
248 'value' => 'Case Subject',
249 'type' => 'Memo',
250 ],
251 3 => [
252 'label' => 'Created By',
253 // data providers run before everything, so update this later
254 'value' => 'placeholder',
255 'type' => 'String',
256 ],
257 4 => [
258 'label' => 'Reported By',
259 // see above - need to update this later
260 'value' => 'placeholder',
261 'type' => 'String',
262 ],
263 5 => [
264 'label' => 'Medium',
265 'value' => 'Phone',
266 'type' => 'String',
267 ],
268 6 => [
269 'label' => 'Location',
270 'value' => NULL,
271 'type' => 'String',
272 ],
273 7 => [
274 'label' => 'Date and Time',
275 'value' => '2019-11-14 00:00:00',
276 'type' => 'Date',
277 ],
278 8 => [
279 'label' => 'Details',
280 'value' => NULL,
281 'type' => 'Memo',
282 ],
283 9 => [
284 'label' => 'Status',
285 'value' => 'Completed',
286 'type' => 'String',
287 ],
288 10 => [
289 'label' => 'Priority',
290 'value' => 'Normal',
291 'type' => 'String',
292 ],
293 ],
294 'editURL' => 'placeholder',
295 'customGroups' => NULL,
296 ],
297 1 => [
298 'fields' => [
299 0 => [
300 'label' => 'Client',
301 'value' => 'Casey Reportee',
302 'type' => 'String',
303 ],
304 1 => [
305 'label' => 'Activity Type',
306 'value' => 'Medical evaluation',
307 'type' => 'String',
308 ],
309 2 => [
310 'label' => 'Subject',
311 'value' => '',
312 'type' => 'Memo',
313 ],
314 3 => [
315 'label' => 'Created By',
316 // see above - need to update this later
317 'value' => 'placeholder',
318 'type' => 'String',
319 ],
320 4 => [
321 'label' => 'Reported By',
322 // see above - need to update this later
323 'value' => 'placeholder',
324 'type' => 'String',
325 ],
326 5 => [
327 'label' => 'Location',
328 'value' => NULL,
329 'type' => 'String',
330 ],
331 6 => [
332 'label' => 'Date and Time',
333 'value' => '2019-11-15 00:00:00',
334 'type' => 'Date',
335 ],
336 7 => [
337 'label' => 'Details',
338 'value' => NULL,
339 'type' => 'Memo',
340 ],
341 8 => [
342 'label' => 'Status',
343 'value' => 'Scheduled',
344 'type' => 'String',
345 ],
346 9 => [
347 'label' => 'Priority',
348 'value' => 'Normal',
349 'type' => 'String',
350 ],
351 ],
352 'editURL' => 'placeholder',
353 'customGroups' => NULL,
354 ],
355 2 => [
356 'fields' => [
357 0 => [
358 'label' => 'Client',
359 'value' => 'Casey Reportee',
360 'type' => 'String',
361 ],
362 1 => [
363 'label' => 'Activity Type',
364 'value' => 'Meeting',
365 'type' => 'String',
366 ],
367 2 => [
368 'label' => 'Subject',
369 'value' => 'Test Meeting',
370 'type' => 'Memo',
371 ],
372 3 => [
373 'label' => 'Created By',
374 // see above - need to update this later
375 'value' => 'placeholder',
376 'type' => 'String',
377 ],
378 4 => [
379 'label' => 'Reported By',
380 // see above - need to update this later
381 'value' => 'placeholder',
382 'type' => 'String',
383 ],
384 5 => [
385 'label' => 'Location',
386 'value' => NULL,
387 'type' => 'String',
388 ],
389 6 => [
390 'label' => 'Date and Time',
391 'value' => '2019-11-14 12:34:56',
392 'type' => 'Date',
393 ],
394 7 => [
395 'label' => 'Details',
396 'value' => NULL,
397 'type' => 'Memo',
398 ],
399 8 => [
400 'label' => 'Status',
401 'value' => 'Completed',
402 'type' => 'String',
403 ],
404 9 => [
405 'label' => 'Priority',
406 'value' => 'Normal',
407 'type' => 'String',
408 ],
409 ],
410 'editURL' => 'placeholder',
411 'customGroups' => NULL,
412 ],
413 ],
414 ],
415 ],
416 ];
417 }
418
419 /**
420 * Since data providers get evaluated before setup and other variable
421 * assignments, we call this during the test to update placeholders we set
422 * in the data provider.
423 * Maybe it doesn't really make sense to use a data provider here, but kinda.
424 *
425 * @param &$expected array Contains the placeholders to update.
426 * @param $client_id int
427 * @param $case_id int
428 */
429 private function updateExpectedBecauseDataProviderEvaluatesBeforeEverything(&$expected, $client_id, $case_id) {
430 $display_name = $this->callAPISuccess('Contact', 'getsingle', [
431 'return' => ["display_name"],
432 'id' => $this->_loggedInUser,
433 ]);
434
435 foreach ($expected['activities'] as $idx => $activity) {
436 $expected['activities'][$idx]['fields'][3]['value'] = $display_name['display_name'];
437 $expected['activities'][$idx]['fields'][4]['value'] = $display_name['display_name'];
438
439 $activityTypeId = $this->callAPISuccess('OptionValue', 'getsingle', [
440 'return' => ["value"],
441 'option_group_id' => 'activity_type',
442 'name' => $expected['activities'][$idx]['fields'][1]['value'],
443 ]);
444 $expected['activities'][$idx]['editURL'] = CRM_Utils_System::url('civicrm/case/activity', "reset=1&cid={$client_id}&caseid={$case_id}&action=update&atype={$activityTypeId['value']}&id=" . ($idx + 1));
445 }
446 }
447
448 /**
449 * Create and return a new case object.
450 * @param $clientId
451 * @param $loggedInUser
452 * @return CRM_Case_BAO_Case
453 */
454 private function createCase($clientId, $loggedInUser) {
455 $caseParams = [
456 'activity_subject' => 'Case Subject',
457 'client_id' => $clientId,
458 'case_type_id' => $this->caseTypeId,
459 'status_id' => 1,
460 'case_type' => $this->caseType,
461 'subject' => 'Case Subject',
462 'start_date' => '2019-11-14',
463 'start_date_time' => '20191114000000',
464 'medium_id' => 2,
465 'activity_details' => '',
466 ];
467 $form = new CRM_Case_Form_Case();
468 $caseObj = $form->testSubmit($caseParams, "OpenCase", $loggedInUser, "standalone");
469 return $caseObj;
470 }
471
472 /**
473 * We don't need so many activities as in the stock case type. Just makes
474 * dataprovider unnecessarily long. Just take the first two.
475 * @return void
476 */
477 private function simplifyCaseTypeDefinition() {
478 $caseType = $this->callAPISuccess('CaseType', 'getsingle', ['id' => $this->caseTypeId]);
479 $newActivitySet = array_slice($caseType['definition']['activitySets'][0]['activityTypes'], 0, 2);
480 $caseType['definition']['activitySets'][0]['activityTypes'] = $newActivitySet;
481 $this->callAPISuccess('CaseType', 'create', $caseType);
482 }
483
484 }