2 require_once 'CiviTest/CiviCaseTestCase.php';
5 * Class CRM_Case_XMLProcessor_ReportTest
8 class CRM_Case_XMLProcessor_ReportTest
extends CiviCaseTestCase
{
10 public function setUp() {
13 $this->simplifyCaseTypeDefinition();
15 $this->report
= new CRM_Case_XMLProcessor_Report();
18 public function tearDown() {
23 * Test that getCaseReport has the right output.
25 * @param $activitySetName string Also acts as data provider test identifier.
26 * @param $expected array
28 * @dataProvider caseReportDataProvider
30 public function testGetCaseReport($activitySetName, $expected) {
31 $client_id = $this->individualCreate([
32 'first_name' => 'Casey',
34 'last_name' => 'Reportee',
38 $caseObj = $this->createCase($client_id, $this->_loggedInUser
);
39 $case_id = $caseObj->id
;
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',
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,
58 'include_activities' => 1,
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();
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);
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));
75 * This is similar to testGetCaseReport but test with a timeline that
76 * does have Meeting in it.
78 public function testGetCaseReportWithMeetingInTimeline() {
79 $client_id = $this->individualCreate([
80 'first_name' => 'Casey',
82 'last_name' => 'Reportee',
86 $caseObj = $this->createCase($client_id, $this->_loggedInUser
);
87 $case_id = $caseObj->id
;
89 // Now update the timeline so it has Meeting in it.
90 $this->addMeetingToTimeline();
92 // Add a meeting activity to the case.
93 $meetingTypeId = $this->callAPISuccess('OptionValue', 'getsingle', [
94 'return' => ["value"],
95 'option_group_id' => 'activity_type',
98 $this->callAPISuccess('activity', 'create', [
99 'case_id' => $case_id,
100 'activity_type_id' => $meetingTypeId['value'],
101 'activity_date_time' => '20191114123456',
102 'subject' => 'Test Meeting',
103 'source_contact_id' => $this->_loggedInUser
,
104 'target_contact_id' => $client_id,
107 $caseReportParams = [
108 'is_redact' => FALSE,
109 'include_activities' => 1,
112 // run the thing we're testing and get the output vars
113 $template = CRM_Case_XMLProcessor_Report
::populateCaseReportTemplate($client_id, $case_id, 'standard_timeline', $caseReportParams, $this->report
);
114 $assigned_vars = $template->get_template_vars();
116 // We don't want to run all the data in the dataprovider but we know
117 // in this case it should be the same as the second one in the
118 // dataprovider so we can reuse it.
119 $expected = $this->caseReportDataProvider()[1][1];
120 $this->updateExpectedBecauseDataProviderEvaluatesBeforeEverything($expected, $client_id, $case_id);
122 foreach ($expected as $key => $value) {
123 // does the assigned template var match the expected value?
124 $this->assertEquals($value, $assigned_vars[$key], "$key does not match" . print_r($assigned_vars[$key], TRUE));
129 * Data provider for testGetCaseReport
132 public function caseReportDataProvider() {
137 // Some expected assigned vars of CRM_Core_Smarty template.
138 // In particular we shouldn't have meeting in the output since it's
139 // not in the timeline.
142 'clientName' => 'Casey Reportee',
143 'subject' => 'Case Subject',
144 'start_date' => '2019-11-14',
146 'caseType' => 'Housing Support',
147 'caseTypeName' => 'housing_support',
148 'status' => 'Ongoing',
156 'value' => 'Casey Reportee',
160 'name' => 'Activity Type',
161 'label' => 'Activity Type',
162 'value' => 'Open Case',
167 'label' => 'Subject',
168 'value' => 'Case Subject',
172 'name' => 'Created By',
173 'label' => 'Created By',
174 // data providers run before everything, so update this later
175 'value' => 'placeholder',
179 'name' => 'Reported By',
180 'label' => 'Reported By',
181 // see above - need to update this later
182 'value' => 'placeholder',
192 'name' => 'Location',
193 'label' => 'Location',
198 'name' => 'Date and Time',
199 'label' => 'Date and Time',
200 'value' => '2019-11-14 00:00:00',
205 'label' => 'Details',
212 'value' => 'Completed',
216 'name' => 'Priority',
217 'label' => 'Priority',
222 'editURL' => 'placeholder',
223 'customGroups' => NULL,
230 'value' => 'Casey Reportee',
234 'name' => 'Activity Type',
235 'label' => 'Activity Type',
236 'value' => 'Medical evaluation',
241 'label' => 'Subject',
246 'name' => 'Created By',
247 'label' => 'Created By',
248 // see above - need to update this later
249 'value' => 'placeholder',
253 'name' => 'Reported By',
254 'label' => 'Reported By',
255 // see above - need to update this later
256 'value' => 'placeholder',
260 'name' => 'Location',
261 'label' => 'Location',
266 'name' => 'Date and Time',
267 'label' => 'Date and Time',
268 'value' => '2019-11-15 00:00:00',
273 'label' => 'Details',
280 'value' => 'Scheduled',
284 'name' => 'Priority',
285 'label' => 'Priority',
290 'editURL' => 'placeholder',
291 'customGroups' => NULL,
297 // activity set name is blank here, meaning don't filter the activities
299 // Some expected assigned vars of CRM_Core_Smarty template.
300 // In particular now we will have Meeting in the output.
303 'clientName' => 'Casey Reportee',
304 'subject' => 'Case Subject',
305 'start_date' => '2019-11-14',
307 'caseType' => 'Housing Support',
308 'caseTypeName' => 'housing_support',
309 'status' => 'Ongoing',
317 'value' => 'Casey Reportee',
321 'name' => 'Activity Type',
322 'label' => 'Activity Type',
323 'value' => 'Open Case',
328 'label' => 'Subject',
329 'value' => 'Case Subject',
333 'name' => 'Created By',
334 'label' => 'Created By',
335 // data providers run before everything, so update this later
336 'value' => 'placeholder',
340 'name' => 'Reported By',
341 'label' => 'Reported By',
342 // see above - need to update this later
343 'value' => 'placeholder',
353 'name' => 'Location',
354 'label' => 'Location',
359 'name' => 'Date and Time',
360 'label' => 'Date and Time',
361 'value' => '2019-11-14 00:00:00',
366 'label' => 'Details',
373 'value' => 'Completed',
377 'name' => 'Priority',
378 'label' => 'Priority',
383 'editURL' => 'placeholder',
384 'customGroups' => NULL,
391 'value' => 'Casey Reportee',
395 'name' => 'Activity Type',
396 'label' => 'Activity Type',
397 'value' => 'Medical evaluation',
402 'label' => 'Subject',
407 'name' => 'Created By',
408 'label' => 'Created By',
409 // see above - need to update this later
410 'value' => 'placeholder',
414 'name' => 'Reported By',
415 'label' => 'Reported By',
416 // see above - need to update this later
417 'value' => 'placeholder',
421 'name' => 'Location',
422 'label' => 'Location',
427 'name' => 'Date and Time',
428 'label' => 'Date and Time',
429 'value' => '2019-11-15 00:00:00',
434 'label' => 'Details',
441 'value' => 'Scheduled',
445 'name' => 'Priority',
446 'label' => 'Priority',
451 'editURL' => 'placeholder',
452 'customGroups' => NULL,
459 'value' => 'Casey Reportee',
463 'name' => 'Activity Type',
464 'label' => 'Activity Type',
465 'value' => 'Meeting',
470 'label' => 'Subject',
471 'value' => 'Test Meeting',
475 'name' => 'Created By',
476 'label' => 'Created By',
477 // see above - need to update this later
478 'value' => 'placeholder',
482 'name' => 'Reported By',
483 'label' => 'Reported By',
484 // see above - need to update this later
485 'value' => 'placeholder',
489 'name' => 'Location',
490 'label' => 'Location',
495 'name' => 'Date and Time',
496 'label' => 'Date and Time',
497 'value' => '2019-11-14 12:34:56',
502 'label' => 'Details',
509 'value' => 'Completed',
513 'name' => 'Priority',
514 'label' => 'Priority',
519 'editURL' => 'placeholder',
520 'customGroups' => NULL,
529 * Since data providers get evaluated before setup and other variable
530 * assignments, we call this during the test to update placeholders we set
531 * in the data provider.
532 * Maybe it doesn't really make sense to use a data provider here, but kinda.
534 * @param &$expected array Contains the placeholders to update.
535 * @param $client_id int
536 * @param $case_id int
538 private function updateExpectedBecauseDataProviderEvaluatesBeforeEverything(&$expected, $client_id, $case_id) {
539 $display_name = $this->callAPISuccess('Contact', 'getsingle', [
540 'return' => ["display_name"],
541 'id' => $this->_loggedInUser
,
544 foreach ($expected['activities'] as $idx => $activity) {
545 $expected['activities'][$idx]['fields'][3]['value'] = $display_name['display_name'];
546 $expected['activities'][$idx]['fields'][4]['value'] = $display_name['display_name'];
548 $activityTypeId = $this->callAPISuccess('OptionValue', 'getsingle', [
549 'return' => ["value"],
550 'option_group_id' => 'activity_type',
551 'name' => $expected['activities'][$idx]['fields'][1]['value'],
553 $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));
558 * Create and return a new case object.
560 * @param $loggedInUser
561 * @return CRM_Case_BAO_Case
563 private function createCase($clientId, $loggedInUser) {
565 'activity_subject' => 'Case Subject',
566 'client_id' => $clientId,
567 'case_type_id' => $this->caseTypeId
,
569 'case_type' => $this->caseType
,
570 'subject' => 'Case Subject',
571 'start_date' => '2019-11-14',
572 'start_date_time' => '20191114000000',
574 'activity_details' => '',
576 $form = new CRM_Case_Form_Case();
577 $caseObj = $form->testSubmit($caseParams, "OpenCase", $loggedInUser, "standalone");
582 * We don't need so many activities as in the stock case type. Just makes
583 * dataprovider unnecessarily long. Just take the first two.
586 private function simplifyCaseTypeDefinition() {
587 $caseType = $this->callAPISuccess('CaseType', 'getsingle', ['id' => $this->caseTypeId
]);
588 $newActivitySet = array_slice($caseType['definition']['activitySets'][0]['activityTypes'], 0, 2);
589 $caseType['definition']['activitySets'][0]['activityTypes'] = $newActivitySet;
590 $this->callAPISuccess('CaseType', 'create', $caseType);
594 * Add Meeting to the standard timeline.
596 private function addMeetingToTimeline() {
597 $caseType = $this->callAPISuccess('CaseType', 'getsingle', ['id' => $this->caseTypeId
]);
598 $activityTypes = $caseType['definition']['activitySets'][0]['activityTypes'];
599 // Make a copy of the second activity type and change the type.
600 $activityType = $activityTypes[1];
601 $activityType['name'] = 'Meeting';
602 $activityType['label'] = 'Meeting';
604 $activityTypes[] = $activityType;
605 $caseType['definition']['activitySets'][0]['activityTypes'] = $activityTypes;
606 $this->callAPISuccess('CaseType', 'create', $caseType);