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',
155 'value' => 'Casey Reportee',
159 'label' => 'Activity Type',
160 'value' => 'Open Case',
164 'label' => 'Subject',
165 'value' => 'Case Subject',
169 'label' => 'Created By',
170 // data providers run before everything, so update this later
171 'value' => 'placeholder',
175 'label' => 'Reported By',
176 // see above - need to update this later
177 'value' => 'placeholder',
186 'label' => 'Location',
191 'label' => 'Date and Time',
192 'value' => '2019-11-14 00:00:00',
196 'label' => 'Details',
202 'value' => 'Completed',
206 'label' => 'Priority',
211 'editURL' => 'placeholder',
212 'customGroups' => NULL,
218 'value' => 'Casey Reportee',
222 'label' => 'Activity Type',
223 'value' => 'Medical evaluation',
227 'label' => 'Subject',
232 'label' => 'Created By',
233 // see above - need to update this later
234 'value' => 'placeholder',
238 'label' => 'Reported By',
239 // see above - need to update this later
240 'value' => 'placeholder',
244 'label' => 'Location',
249 'label' => 'Date and Time',
250 'value' => '2019-11-15 00:00:00',
254 'label' => 'Details',
260 'value' => 'Scheduled',
264 'label' => 'Priority',
269 'editURL' => 'placeholder',
270 'customGroups' => NULL,
276 // activity set name is blank here, meaning don't filter the activities
278 // Some expected assigned vars of CRM_Core_Smarty template.
279 // In particular now we will have Meeting in the output.
282 'clientName' => 'Casey Reportee',
283 'subject' => 'Case Subject',
284 'start_date' => '2019-11-14',
286 'caseType' => 'Housing Support',
287 'caseTypeName' => 'housing_support',
288 'status' => 'Ongoing',
295 'value' => 'Casey Reportee',
299 'label' => 'Activity Type',
300 'value' => 'Open Case',
304 'label' => 'Subject',
305 'value' => 'Case Subject',
309 'label' => 'Created By',
310 // data providers run before everything, so update this later
311 'value' => 'placeholder',
315 'label' => 'Reported By',
316 // see above - need to update this later
317 'value' => 'placeholder',
326 'label' => 'Location',
331 'label' => 'Date and Time',
332 'value' => '2019-11-14 00:00:00',
336 'label' => 'Details',
342 'value' => 'Completed',
346 'label' => 'Priority',
351 'editURL' => 'placeholder',
352 'customGroups' => NULL,
358 'value' => 'Casey Reportee',
362 'label' => 'Activity Type',
363 'value' => 'Medical evaluation',
367 'label' => 'Subject',
372 'label' => 'Created By',
373 // see above - need to update this later
374 'value' => 'placeholder',
378 'label' => 'Reported By',
379 // see above - need to update this later
380 'value' => 'placeholder',
384 'label' => 'Location',
389 'label' => 'Date and Time',
390 'value' => '2019-11-15 00:00:00',
394 'label' => 'Details',
400 'value' => 'Scheduled',
404 'label' => 'Priority',
409 'editURL' => 'placeholder',
410 'customGroups' => NULL,
416 'value' => 'Casey Reportee',
420 'label' => 'Activity Type',
421 'value' => 'Meeting',
425 'label' => 'Subject',
426 'value' => 'Test Meeting',
430 'label' => 'Created By',
431 // see above - need to update this later
432 'value' => 'placeholder',
436 'label' => 'Reported By',
437 // see above - need to update this later
438 'value' => 'placeholder',
442 'label' => 'Location',
447 'label' => 'Date and Time',
448 'value' => '2019-11-14 12:34:56',
452 'label' => 'Details',
458 'value' => 'Completed',
462 'label' => 'Priority',
467 'editURL' => 'placeholder',
468 'customGroups' => NULL,
477 * Since data providers get evaluated before setup and other variable
478 * assignments, we call this during the test to update placeholders we set
479 * in the data provider.
480 * Maybe it doesn't really make sense to use a data provider here, but kinda.
482 * @param &$expected array Contains the placeholders to update.
483 * @param $client_id int
484 * @param $case_id int
486 private function updateExpectedBecauseDataProviderEvaluatesBeforeEverything(&$expected, $client_id, $case_id) {
487 $display_name = $this->callAPISuccess('Contact', 'getsingle', [
488 'return' => ["display_name"],
489 'id' => $this->_loggedInUser
,
492 foreach ($expected['activities'] as $idx => $activity) {
493 $expected['activities'][$idx]['fields'][3]['value'] = $display_name['display_name'];
494 $expected['activities'][$idx]['fields'][4]['value'] = $display_name['display_name'];
496 $activityTypeId = $this->callAPISuccess('OptionValue', 'getsingle', [
497 'return' => ["value"],
498 'option_group_id' => 'activity_type',
499 'name' => $expected['activities'][$idx]['fields'][1]['value'],
501 $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));
506 * Create and return a new case object.
508 * @param $loggedInUser
509 * @return CRM_Case_BAO_Case
511 private function createCase($clientId, $loggedInUser) {
513 'activity_subject' => 'Case Subject',
514 'client_id' => $clientId,
515 'case_type_id' => $this->caseTypeId
,
517 'case_type' => $this->caseType
,
518 'subject' => 'Case Subject',
519 'start_date' => '2019-11-14',
520 'start_date_time' => '20191114000000',
522 'activity_details' => '',
524 $form = new CRM_Case_Form_Case();
525 $caseObj = $form->testSubmit($caseParams, "OpenCase", $loggedInUser, "standalone");
530 * We don't need so many activities as in the stock case type. Just makes
531 * dataprovider unnecessarily long. Just take the first two.
534 private function simplifyCaseTypeDefinition() {
535 $caseType = $this->callAPISuccess('CaseType', 'getsingle', ['id' => $this->caseTypeId
]);
536 $newActivitySet = array_slice($caseType['definition']['activitySets'][0]['activityTypes'], 0, 2);
537 $caseType['definition']['activitySets'][0]['activityTypes'] = $newActivitySet;
538 $this->callAPISuccess('CaseType', 'create', $caseType);
542 * Add Meeting to the standard timeline.
544 private function addMeetingToTimeline() {
545 $caseType = $this->callAPISuccess('CaseType', 'getsingle', ['id' => $this->caseTypeId
]);
546 $activityTypes = $caseType['definition']['activitySets'][0]['activityTypes'];
547 // Make a copy of the second activity type and change the type.
548 $activityType = $activityTypes[1];
549 $activityType['name'] = 'Meeting';
550 $activityType['label'] = 'Meeting';
552 $activityTypes[] = $activityType;
553 $caseType['definition']['activitySets'][0]['activityTypes'] = $activityTypes;
554 $this->callAPISuccess('CaseType', 'create', $caseType);