Merge pull request #13926 from pradpnayak/NoticeErrorProfile
[civicrm-core.git] / tests / phpunit / api / v3 / CaseTypeTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
26 */
27
28 /**
29 * Class api_v3_CaseTypeTest
30 * @group headless
31 */
32 class api_v3_CaseTypeTest extends CiviCaseTestCase {
33
34 public function setUp() {
35 $this->quickCleanup(array('civicrm_case_type'));
36 parent::setUp();
37
38 $this->fixtures['Application_with_Definition'] = array(
39 'title' => 'Application with Definition',
40 'name' => 'Application_with_Definition',
41 'is_active' => 1,
42 'weight' => 4,
43 'definition' => array(
44 'activityTypes' => array(
45 array('name' => 'First act'),
46 ),
47 'activitySets' => array(
48 array(
49 'name' => 'set1',
50 'label' => 'Label 1',
51 'timeline' => 1,
52 'activityTypes' => array(
53 array('name' => 'Open Case', 'status' => 'Completed'),
54 ),
55 ),
56 ),
57 'timelineActivityTypes' => array(
58 array('name' => 'Open Case', 'status' => 'Completed'),
59 ),
60 'caseRoles' => array(
61 array('name' => 'First role', 'creator' => 1, 'manager' => 1),
62 ),
63 ),
64 );
65 }
66
67 /**
68 * Tears down the fixture, for example, closes a network connection.
69 *
70 * This method is called after a test is executed.
71 */
72 public function tearDown() {
73 parent::tearDown();
74 $this->quickCleanup(array('civicrm_case_type', 'civicrm_uf_match'));
75 }
76
77 /**
78 * Check with empty array.
79 */
80 public function testCaseTypeCreateEmpty() {
81 $this->callAPIFailure('CaseType', 'create', array());
82 }
83
84 /**
85 * Check if required fields are not passed.
86 */
87 public function testCaseTypeCreateWithoutRequired() {
88 $params = array(
89 'name' => 'this case should fail',
90 );
91 $this->callAPIFailure('CaseType', 'create', $params);
92
93 $params = array(
94 'name' => 'this case should fail',
95 'weight' => 4,
96 );
97 $this->callAPIFailure('CaseType', 'create', $params);
98 }
99
100 /**
101 * Test create methods with valid data.
102 *
103 * Success expected.
104 */
105 public function testCaseTypeCreate() {
106 // Create Case Type.
107 $params = array(
108 'title' => 'Application',
109 'name' => 'Application',
110 'is_active' => 1,
111 'weight' => 4,
112 );
113
114 $result = $this->callAPISuccess('CaseType', 'create', $params);
115 $id = $result['id'];
116
117 // Check result.
118 $result = $this->callAPISuccess('CaseType', 'get', array('id' => $id));
119 $this->assertEquals($result['values'][$id]['id'], $id);
120 $this->assertEquals($result['values'][$id]['title'], $params['title']);
121 }
122
123 /**
124 * Create a case with an invalid name.
125 */
126 public function testCaseTypeCreate_invalidName() {
127 // Create Case Type
128 $params = array(
129 'title' => 'Application',
130 'name' => 'Appl ication', // spaces are not allowed
131 'is_active' => 1,
132 'weight' => 4,
133 );
134
135 $this->callAPIFailure('CaseType', 'create', $params);
136 }
137
138
139 /**
140 * Test update (create with id) function with valid parameters.
141 */
142 public function testCaseTypeUpdate() {
143 // Create Case Type
144 $params = array(
145 'title' => 'Application',
146 'name' => 'Application',
147 'is_active' => 1,
148 'weight' => 4,
149 );
150 $result = $this->callAPISuccess('CaseType', 'create', $params);
151 $id = $result['id'];
152 $result = $this->callAPISuccess('CaseType', 'get', array('id' => $id));
153 $caseType = $result['values'][$id];
154
155 // Update Case Type.
156 $params = array('id' => $id);
157 $params['title'] = $caseType['title'] = 'Something Else';
158 $this->callAPISuccess('CaseType', 'create', $params);
159
160 // Verify that updated case Type is exactly equal to the original with new title.
161 $result = $this->callAPISuccess('CaseType', 'get', array('id' => $id));
162 $this->assertEquals($result['values'][$id], $caseType);
163 }
164
165 /**
166 * Test delete function with valid parameters.
167 */
168 public function testCaseTypeDelete_New() {
169 // Create Case Type.
170 $params = array(
171 'title' => 'Application',
172 'name' => 'Application',
173 'is_active' => 1,
174 'weight' => 4,
175 );
176 $result = $this->callAPISuccess('CaseType', 'create', $params);
177
178 $id = $result['id'];
179 $this->callAPISuccess('CaseType', 'delete', array('id' => $id));
180
181 // Check result - case type should no longer exist
182 $result = $this->callAPISuccess('CaseType', 'get', array('id' => $id));
183 $this->assertEquals(0, $result['count']);
184 }
185
186 /**
187 * Test create methods with xml file.
188 *
189 * Success expected.
190 */
191 public function testCaseTypeCreateWithDefinition() {
192 // Create Case Type
193 $params = $this->fixtures['Application_with_Definition'];
194 $result = $this->callAPISuccess('CaseType', 'create', $params);
195 $id = $result['id'];
196
197 // Check result
198 $result = $this->callAPISuccess('CaseType', 'get', array('id' => $id));
199 $this->assertEquals($result['values'][$id]['id'], $id);
200 $this->assertEquals($result['values'][$id]['title'], $params['title']);
201 $this->assertEquals($result['values'][$id]['definition'], $params['definition']);
202
203 $caseXml = CRM_Case_XMLRepository::singleton()->retrieve('Application_with_Definition');
204 $this->assertTrue($caseXml instanceof SimpleXMLElement);
205 }
206
207 /**
208 * Create a CaseType+case then delete the CaseType.
209 */
210 public function testCaseTypeDelete_InUse() {
211 // Create Case Type
212 $params = $this->fixtures['Application_with_Definition'];
213 $createCaseType = $this->callAPISuccess('CaseType', 'create', $params);
214
215 $createCase = $this->callAPISuccess('Case', 'create', array(
216 'case_type_id' => $createCaseType['id'],
217 'contact_id' => $this->_loggedInUser,
218 'subject' => 'Example',
219 ));
220
221 // Deletion fails while case-type is in-use
222 $deleteCaseType = $this->callAPIFailure('CaseType', 'delete', array('id' => $createCaseType['id']));
223 $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']);
224 $getCaseType = $this->callAPISuccess('CaseType', 'get', array('id' => $createCaseType['id']));
225 $this->assertEquals(1, $getCaseType['count']);
226
227 // Deletion succeeds when it's not in-use.
228 $this->callAPISuccess('Case', 'delete', array('id' => $createCase['id']));
229
230 // Check result - case type should no longer exist.
231 $this->callAPISuccess('CaseType', 'delete', array('id' => $createCaseType['id']));
232 $getCaseType = $this->callAPISuccess('CaseType', 'get', array('id' => $createCaseType['id']));
233 $this->assertEquals(0, $getCaseType['count']);
234 }
235
236 /**
237 * Test the api returns case statuses filtered by case type.
238 *
239 * Api getoptions should respect the case statuses declared in the case type definition.
240 *
241 * @throws \Exception
242 */
243 public function testCaseStatusByCaseType() {
244 $this->markTestIncomplete('Cannot figure out why this passes locally but fails on Jenkins.');
245 $statusName = md5(mt_rand());
246 $template = $this->callAPISuccess('CaseType', 'getsingle', array('id' => $this->caseTypeId));
247 unset($template['id']);
248 $template['name'] = $template['title'] = 'test_case_type';
249 $template['definition']['statuses'] = array('Closed', $statusName);
250 $this->callAPISuccess('CaseType', 'create', $template);
251 $this->callAPISuccess('OptionValue', 'create', array(
252 'option_group_id' => 'case_status',
253 'name' => $statusName,
254 'label' => $statusName,
255 'weight' => 99,
256 ));
257 $result = $this->callAPISuccess('Case', 'getoptions', array('field' => 'status_id', 'case_type_id' => 'test_case_type', 'context' => 'validate'));
258 $this->assertEquals($template['definition']['statuses'], array_values($result['values']));
259 }
260
261 }