Merge pull request #14322 from AlainBenbassat/5.14
[civicrm-core.git] / tests / phpunit / CRM / Case / BAO / CaseTest.php
1 <?php
2
3 /**
4 * Class CRM_Case_BAO_CaseTest
5 * @group headless
6 */
7 class CRM_Case_BAO_CaseTest extends CiviUnitTestCase {
8
9 public function setUp() {
10 parent::setUp();
11
12 $this->tablesToTruncate = array(
13 'civicrm_activity',
14 'civicrm_contact',
15 'civicrm_custom_group',
16 'civicrm_custom_field',
17 'civicrm_case',
18 'civicrm_case_contact',
19 'civicrm_case_activity',
20 'civicrm_case_type',
21 'civicrm_activity_contact',
22 'civicrm_managed',
23 'civicrm_relationship',
24 'civicrm_relationship_type',
25 );
26
27 $this->quickCleanup($this->tablesToTruncate);
28
29 $this->loadAllFixtures();
30
31 CRM_Core_BAO_ConfigSetting::enableComponent('CiviCase');
32 }
33
34 /**
35 * Make sure that the latest case activity works accurately.
36 */
37 public function testCaseActivity() {
38 $userID = $this->createLoggedInUser();
39
40 $addTimeline = civicrm_api3('Case', 'addtimeline', [
41 'case_id' => 1,
42 'timeline' => "standard_timeline",
43 ]);
44
45 $query = CRM_Case_BAO_Case::getCaseActivityQuery('recent', $userID, ' civicrm_case.id IN( 1 )');
46 $res = CRM_Core_DAO::executeQuery($query);
47 $openCaseType = CRM_Core_PseudoConstant::getKey('CRM_Activity_BAO_Activity', 'activity_type_id', 'Open Case');
48 while ($res->fetch()) {
49 $message = 'Failed asserting that the case activity query has a activity_type_id property:';
50 $this->assertObjectHasAttribute('activity_type_id', $res, $message . PHP_EOL . print_r($res, TRUE));
51 $message = 'Failed asserting that the latest activity from Case ID 1 was "Open Case":';
52 $this->assertEquals($openCaseType, $res->activity_type_id, $message . PHP_EOL . print_r($res, TRUE));
53 }
54 }
55
56 protected function tearDown() {
57 parent::tearDown();
58 $this->quickCleanup($this->tablesToTruncate, TRUE);
59 }
60
61 public function testAddCaseToContact() {
62 $params = array(
63 'case_id' => 1,
64 'contact_id' => 17,
65 );
66 CRM_Case_BAO_CaseContact::create($params);
67
68 $recent = CRM_Utils_Recent::get();
69 $this->assertEquals('Test Contact - Housing Support', $recent[0]['title']);
70 }
71
72 /**
73 * Create and return case object of given Client ID.
74 * @param $clientId
75 * @return CRM_Case_BAO_Case
76 */
77 private function createCase($clientId) {
78 $caseParams = array(
79 'activity_subject' => 'Case Subject',
80 'client_id' => $clientId,
81 'case_type_id' => 1,
82 'status_id' => 1,
83 'case_type' => 'housing_support',
84 'subject' => 'Case Subject',
85 'start_date' => date("Y-m-d"),
86 'start_date_time' => date("YmdHis"),
87 'medium_id' => 2,
88 'activity_details' => '',
89 );
90 $form = new CRM_Case_Form_Case();
91 $caseObj = $form->testSubmit($caseParams, "OpenCase", $clientId, "standalone");
92 return $caseObj;
93 }
94
95 /**
96 * Create case role relationship between given contacts for provided case ID.
97 *
98 * @param $contactIdA
99 * @param $contactIdB
100 * @param $caseId
101 * @param bool $isActive
102 */
103 private function createCaseRoleRelationship($contactIdA, $contactIdB, $caseId, $isActive = TRUE) {
104 $relationshipType = $this->relationshipTypeCreate([
105 'contact_type_b' => 'Individual',
106 ]);
107
108 $this->callAPISuccess('Relationship', 'create', array(
109 'contact_id_a' => $contactIdA,
110 'contact_id_b' => $contactIdB,
111 'relationship_type_id' => $relationshipType,
112 'case_id' => $caseId,
113 'is_active' => $isActive,
114 ));
115 }
116
117 /**
118 * Asserts number of cases for given logged in user.
119 *
120 * @param $loggedInUser
121 * @param $caseId
122 * @param $caseCount
123 */
124 private function assertCasesOfUser($loggedInUser, $caseId, $caseCount) {
125 $summary = CRM_Case_BAO_Case::getCasesSummary(FALSE);
126 $upcomingCases = CRM_Case_BAO_Case::getCases(FALSE, array(), 'dashboard', TRUE);
127 $caseRoles = CRM_Case_BAO_Case::getCaseRoles($loggedInUser, $caseId);
128
129 $this->assertEquals($caseCount, $upcomingCases, 'Upcoming case count must be ' . $caseCount);
130 $this->assertEquals($caseCount, $summary['rows']['Housing Support']['Ongoing']['count'], 'Housing Support Ongoing case summary must be ' . $caseCount);
131 $this->assertEquals($caseCount, count($caseRoles), 'Total case roles for logged in users must be ' . $caseCount);
132 }
133
134 /**
135 * Test that Case count is exactly one for logged in user for user's active role.
136 */
137 public function testActiveCaseRole() {
138 $individual = $this->individualCreate();
139 $caseObj = $this->createCase($individual);
140 $caseId = $caseObj->id;
141 $loggedInUser = $this->createLoggedInUser();
142 $this->createCaseRoleRelationship($individual, $loggedInUser, $caseId);
143 $this->assertCasesOfUser($loggedInUser, $caseId, 1);
144 }
145
146 /**
147 * Test that case count is zero for logged in user for user's inactive role.
148 */
149 public function testInactiveCaseRole() {
150 $individual = $this->individualCreate();
151 $caseObj = $this->createCase($individual);
152 $caseId = $caseObj->id;
153 $loggedInUser = $this->createLoggedInUser();
154 $this->createCaseRoleRelationship($individual, $loggedInUser, $caseId, FALSE);
155 $this->assertCasesOfUser($loggedInUser, $caseId, 0);
156 }
157
158 public function testGetCaseType() {
159 $caseTypeLabel = CRM_Case_BAO_Case::getCaseType(1);
160 $this->assertEquals('Housing Support', $caseTypeLabel);
161 }
162
163 public function testRetrieveCaseIdsByContactId() {
164 $caseIds = CRM_Case_BAO_Case::retrieveCaseIdsByContactId(3, FALSE, 'housing_support');
165 $this->assertEquals(array(1), $caseIds);
166 }
167
168 /**
169 * FIXME: need to create an activity to run this test
170 * function testGetCases() {
171 * $cases = CRM_Case_BAO_Case::getCases(TRUE, 3);
172 * $this->assertEquals('Housing Support', $cases[1]['case_type']);
173 * $this->assertEquals(1, $cases[1]['case_type_id']);
174 * }
175 */
176 public function testGetCasesSummary() {
177 $cases = CRM_Case_BAO_Case::getCasesSummary(TRUE, 3);
178 $this->assertEquals(1, $cases['rows']['Housing Support']['Ongoing']['count']);
179 }
180
181 /* FIXME: requires activities
182 * function testGetRelatedCases() {
183 * }
184 */
185
186 }