Merge pull request #16469 from civicrm/5.22
[civicrm-core.git] / tests / phpunit / api / v3 / CaseTypeTest.php
CommitLineData
eaefbeb2
N
1<?php
2/*
3 +--------------------------------------------------------------------+
7d61e75f 4 | Copyright CiviCRM LLC. All rights reserved. |
eaefbeb2 5 | |
7d61e75f
TO
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
eaefbeb2 9 +--------------------------------------------------------------------+
d25dd0ee 10 */
eaefbeb2 11
e9479dcf
EM
12/**
13 * Class api_v3_CaseTypeTest
acb109b7 14 * @group headless
e9479dcf 15 */
10ffff26 16class api_v3_CaseTypeTest extends CiviCaseTestCase {
eaefbeb2 17
00be9182 18 public function setUp() {
9099cab3 19 $this->quickCleanup(['civicrm_case_type']);
eaefbeb2 20 parent::setUp();
eaefbeb2 21
9099cab3 22 $this->fixtures['Application_with_Definition'] = [
10ffff26
TO
23 'title' => 'Application with Definition',
24 'name' => 'Application_with_Definition',
25 'is_active' => 1,
26 'weight' => 4,
9099cab3
CW
27 'definition' => [
28 'activityTypes' => [
29 ['name' => 'First act'],
30 ],
31 'activitySets' => [
32 [
10ffff26
TO
33 'name' => 'set1',
34 'label' => 'Label 1',
35 'timeline' => 1,
9099cab3
CW
36 'activityTypes' => [
37 ['name' => 'Open Case', 'status' => 'Completed'],
38 ],
39 ],
40 ],
41 'timelineActivityTypes' => [
42 ['name' => 'Open Case', 'status' => 'Completed'],
43 ],
44 'caseRoles' => [
45 ['name' => 'First role', 'creator' => 1, 'manager' => 1],
46 ],
47 ],
48 ];
eaefbeb2
N
49 }
50
51 /**
52 * Tears down the fixture, for example, closes a network connection.
6d6dc885 53 *
eaefbeb2 54 * This method is called after a test is executed.
eaefbeb2 55 */
00be9182 56 public function tearDown() {
10ffff26 57 parent::tearDown();
9099cab3 58 $this->quickCleanup(['civicrm_case_type', 'civicrm_uf_match']);
eaefbeb2
N
59 }
60
61 /**
6d6dc885 62 * Check with empty array.
eaefbeb2 63 */
00be9182 64 public function testCaseTypeCreateEmpty() {
9099cab3 65 $this->callAPIFailure('CaseType', 'create', []);
eaefbeb2
N
66 }
67
68 /**
6d6dc885 69 * Check if required fields are not passed.
eaefbeb2 70 */
00be9182 71 public function testCaseTypeCreateWithoutRequired() {
9099cab3 72 $params = [
eaefbeb2 73 'name' => 'this case should fail',
9099cab3 74 ];
9eebe75f 75 $this->callAPIFailure('CaseType', 'create', $params);
eaefbeb2 76
9099cab3 77 $params = [
eaefbeb2
N
78 'name' => 'this case should fail',
79 'weight' => 4,
9099cab3 80 ];
9eebe75f 81 $this->callAPIFailure('CaseType', 'create', $params);
eaefbeb2
N
82 }
83
a771c43f 84 /**
6d6dc885
EM
85 * Test create methods with valid data.
86 *
87 * Success expected.
a771c43f 88 */
00be9182 89 public function testCaseTypeCreate() {
6d6dc885 90 // Create Case Type.
9099cab3 91 $params = [
eaefbeb2
N
92 'title' => 'Application',
93 'name' => 'Application',
94 'is_active' => 1,
95 'weight' => 4,
9099cab3 96 ];
eaefbeb2
N
97
98 $result = $this->callAPISuccess('CaseType', 'create', $params);
99 $id = $result['id'];
100
6d6dc885 101 // Check result.
9099cab3 102 $result = $this->callAPISuccess('CaseType', 'get', ['id' => $id]);
a15773db 103 $this->assertEquals($result['values'][$id]['id'], $id);
ba4a1892 104 $this->assertEquals($result['values'][$id]['title'], $params['title']);
eaefbeb2
N
105 }
106
076f81b6 107 /**
6d6dc885 108 * Create a case with an invalid name.
076f81b6 109 */
00be9182 110 public function testCaseTypeCreate_invalidName() {
076f81b6 111 // Create Case Type
9099cab3 112 $params = [
076f81b6 113 'title' => 'Application',
39b959db
SL
114 // spaces are not allowed
115 'name' => 'Appl ication',
076f81b6 116 'is_active' => 1,
117 'weight' => 4,
9099cab3 118 ];
076f81b6 119
120 $this->callAPIFailure('CaseType', 'create', $params);
121 }
122
eaefbeb2 123 /**
6d6dc885 124 * Test update (create with id) function with valid parameters.
eaefbeb2 125 */
00be9182 126 public function testCaseTypeUpdate() {
eaefbeb2 127 // Create Case Type
9099cab3 128 $params = [
eaefbeb2
N
129 'title' => 'Application',
130 'name' => 'Application',
131 'is_active' => 1,
132 'weight' => 4,
9099cab3 133 ];
eaefbeb2
N
134 $result = $this->callAPISuccess('CaseType', 'create', $params);
135 $id = $result['id'];
9099cab3 136 $result = $this->callAPISuccess('CaseType', 'get', ['id' => $id]);
eaefbeb2
N
137 $caseType = $result['values'][$id];
138
6d6dc885 139 // Update Case Type.
9099cab3 140 $params = ['id' => $id];
eaefbeb2 141 $params['title'] = $caseType['title'] = 'Something Else';
9eebe75f 142 $this->callAPISuccess('CaseType', 'create', $params);
eaefbeb2 143
6d6dc885 144 // Verify that updated case Type is exactly equal to the original with new title.
9099cab3 145 $result = $this->callAPISuccess('CaseType', 'get', ['id' => $id]);
6d6dc885 146 $this->assertEquals($result['values'][$id], $caseType);
eaefbeb2
N
147 }
148
149 /**
6d6dc885 150 * Test delete function with valid parameters.
eaefbeb2 151 */
00be9182 152 public function testCaseTypeDelete_New() {
6d6dc885 153 // Create Case Type.
9099cab3 154 $params = [
eaefbeb2
N
155 'title' => 'Application',
156 'name' => 'Application',
157 'is_active' => 1,
158 'weight' => 4,
9099cab3 159 ];
eaefbeb2
N
160 $result = $this->callAPISuccess('CaseType', 'create', $params);
161
162 $id = $result['id'];
9099cab3 163 $this->callAPISuccess('CaseType', 'delete', ['id' => $id]);
eaefbeb2
N
164
165 // Check result - case type should no longer exist
9099cab3 166 $result = $this->callAPISuccess('CaseType', 'get', ['id' => $id]);
eaefbeb2
N
167 $this->assertEquals(0, $result['count']);
168 }
a771c43f 169
170 /**
6d6dc885
EM
171 * Test create methods with xml file.
172 *
173 * Success expected.
a771c43f 174 */
00be9182 175 public function testCaseTypeCreateWithDefinition() {
a771c43f 176 // Create Case Type
10ffff26 177 $params = $this->fixtures['Application_with_Definition'];
a771c43f 178 $result = $this->callAPISuccess('CaseType', 'create', $params);
179 $id = $result['id'];
180
181 // Check result
9099cab3 182 $result = $this->callAPISuccess('CaseType', 'get', ['id' => $id]);
a15773db 183 $this->assertEquals($result['values'][$id]['id'], $id);
ba4a1892
TM
184 $this->assertEquals($result['values'][$id]['title'], $params['title']);
185 $this->assertEquals($result['values'][$id]['definition'], $params['definition']);
10ffff26
TO
186
187 $caseXml = CRM_Case_XMLRepository::singleton()->retrieve('Application_with_Definition');
188 $this->assertTrue($caseXml instanceof SimpleXMLElement);
189 }
190
191 /**
192 * Create a CaseType+case then delete the CaseType.
193 */
00be9182 194 public function testCaseTypeDelete_InUse() {
10ffff26
TO
195 // Create Case Type
196 $params = $this->fixtures['Application_with_Definition'];
197 $createCaseType = $this->callAPISuccess('CaseType', 'create', $params);
198
9099cab3 199 $createCase = $this->callAPISuccess('Case', 'create', [
10ffff26
TO
200 'case_type_id' => $createCaseType['id'],
201 'contact_id' => $this->_loggedInUser,
202 'subject' => 'Example',
9099cab3 203 ]);
10ffff26
TO
204
205 // Deletion fails while case-type is in-use
9099cab3 206 $deleteCaseType = $this->callAPIFailure('CaseType', 'delete', ['id' => $createCaseType['id']]);
6bf8b6b7 207 $this->assertEquals("You can not delete this case type -- it is assigned to 1 existing case record(s). If you do not want this case type to be used going forward, consider disabling it instead.", $deleteCaseType['error_message']);
9099cab3 208 $getCaseType = $this->callAPISuccess('CaseType', 'get', ['id' => $createCaseType['id']]);
10ffff26
TO
209 $this->assertEquals(1, $getCaseType['count']);
210
6d6dc885 211 // Deletion succeeds when it's not in-use.
9099cab3 212 $this->callAPISuccess('Case', 'delete', ['id' => $createCase['id']]);
10ffff26 213
6d6dc885 214 // Check result - case type should no longer exist.
9099cab3
CW
215 $this->callAPISuccess('CaseType', 'delete', ['id' => $createCaseType['id']]);
216 $getCaseType = $this->callAPISuccess('CaseType', 'get', ['id' => $createCaseType['id']]);
10ffff26 217 $this->assertEquals(0, $getCaseType['count']);
a771c43f 218 }
96025800 219
31c28ed5
CW
220 /**
221 * Test the api returns case statuses filtered by case type.
222 *
223 * Api getoptions should respect the case statuses declared in the case type definition.
224 *
225 * @throws \Exception
226 */
227 public function testCaseStatusByCaseType() {
228 $statusName = md5(mt_rand());
9099cab3 229 $template = $this->callAPISuccess('CaseType', 'getsingle', ['id' => $this->caseTypeId]);
31c28ed5
CW
230 unset($template['id']);
231 $template['name'] = $template['title'] = 'test_case_type';
9099cab3 232 $template['definition']['statuses'] = ['Closed', $statusName];
31c28ed5 233 $this->callAPISuccess('CaseType', 'create', $template);
9099cab3 234 $this->callAPISuccess('OptionValue', 'create', [
31c28ed5
CW
235 'option_group_id' => 'case_status',
236 'name' => $statusName,
237 'label' => $statusName,
238 'weight' => 99,
9099cab3
CW
239 ]);
240 $result = $this->callAPISuccess('Case', 'getoptions', ['field' => 'status_id', 'case_type_id' => 'test_case_type', 'context' => 'validate']);
31c28ed5
CW
241 $this->assertEquals($template['definition']['statuses'], array_values($result['values']));
242 }
243
c4bfbde3
CW
244 public function testDefinitionGroups() {
245 $gid1 = $this->groupCreate(['name' => 'testDefinitionGroups1', 'title' => 'testDefinitionGroups1']);
246 $gid2 = $this->groupCreate(['name' => 'testDefinitionGroups2', 'title' => 'testDefinitionGroups2']);
247 $def = $this->fixtures['Application_with_Definition'];
248 $def['definition']['caseRoles'][] = [
249 'name' => 'Second role',
250 'groups' => ['testDefinitionGroups1', 'testDefinitionGroups2'],
251 ];
252 $def['definition']['caseRoles'][] = [
253 'name' => 'Third role',
254 'groups' => 'testDefinitionGroups2',
255 ];
256 $def['definition']['activityAsgmtGrps'] = $gid1;
257 $createCaseType = $this->callAPISuccess('CaseType', 'create', $def);
258 $caseType = $this->callAPISuccess('CaseType', 'getsingle', ['id' => $createCaseType['id']]);
259
260 // Assert the group id got converted to array with name not id
261 $this->assertEquals(['testDefinitionGroups1'], $caseType['definition']['activityAsgmtGrps']);
262
263 // Assert multiple groups are stored
264 $this->assertEquals(['testDefinitionGroups1', 'testDefinitionGroups2'], $caseType['definition']['caseRoles'][1]['groups']);
265
266 // Assert single group got converted to array
267 $this->assertEquals(['testDefinitionGroups2'], $caseType['definition']['caseRoles'][2]['groups']);
268
269 }
270
eaefbeb2 271}