Merge pull request #7797 from JKingsnorth/CRM-17977
[civicrm-core.git] / tests / phpunit / api / v3 / CaseTest.php
1 <?php
2 /**
3 * @file
4 * File for the TestCase class
5 *
6 * (PHP 5)
7 *
8 * @author Walt Haas <walt@dharmatech.org> (801) 534-1262
9 * @copyright Copyright CiviCRM LLC (C) 2009
10 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html
11 * GNU Affero General Public License version 3
12 * @version $Id: ActivityTest.php 31254 2010-12-15 10:09:29Z eileen $
13 * @package CiviCRM
14 *
15 * This file is part of CiviCRM
16 *
17 * CiviCRM is free software; you can redistribute it and/or
18 * modify it under the terms of the GNU Affero General Public License
19 * as published by the Free Software Foundation; either version 3 of
20 * the License, or (at your option) any later version.
21 *
22 * CiviCRM is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU Affero General Public License for more details.
26 *
27 * You should have received a copy of the GNU Affero General Public
28 * License along with this program. If not, see
29 * <http://www.gnu.org/licenses/>.
30 */
31
32 /**
33 * Include class definitions
34 */
35
36 /**
37 * Test APIv3 civicrm_case_* functions
38 *
39 * @package CiviCRM_APIv3
40 * @group headless
41 */
42 class api_v3_CaseTest extends CiviCaseTestCase {
43 protected $_params;
44 protected $_entity;
45 protected $_apiversion = 3;
46 protected $followup_activity_type_value;
47 /**
48 * Activity ID of created case.
49 *
50 * @var int
51 */
52 protected $_caseActivityId;
53
54 /**
55 * Test setup for every test.
56 *
57 * Connect to the database, truncate the tables that will be used
58 * and redirect stdin to a temporary file.
59 */
60 public function setUp() {
61 $this->_entity = 'case';
62
63 parent::setUp();
64
65 $activityTypes = $this->callAPISuccess('option_value', 'get', array(
66 'option_group_id' => 2,
67 'name' => 'Follow Up',
68 'label' => 'Follow Up',
69 'sequential' => 1,
70 ));
71 $this->followup_activity_type_value = $activityTypes['values'][0]['value'];
72
73 $this->_params = array(
74 'case_type_id' => $this->caseTypeId,
75 'subject' => 'Test case',
76 'contact_id' => 17,
77 );
78 }
79
80 /**
81 * Check with empty array.
82 */
83 public function testCaseCreateEmpty() {
84 $this->callAPIFailure('case', 'create', array());
85 }
86
87 /**
88 * Check if required fields are not passed.
89 */
90 public function testCaseCreateWithoutRequired() {
91 $params = array(
92 'subject' => 'this case should fail',
93 'case_type_id' => 1,
94 );
95
96 $this->callAPIFailure('case', 'create', $params);
97 }
98
99 /**
100 * Test create function with valid parameters.
101 */
102 public function testCaseCreate() {
103 $params = $this->_params;
104 // Test using label instead of value.
105 unset($params['case_type_id']);
106 $params['case_type'] = $this->caseType;
107 $result = $this->callAPIAndDocument('case', 'create', $params, __FUNCTION__, __FILE__);
108 $id = $result['id'];
109
110 // Check result
111 $result = $this->callAPISuccess('case', 'get', array('id' => $id));
112 $this->assertEquals($result['values'][$id]['id'], $id);
113 $this->assertEquals($result['values'][$id]['case_type_id'], $this->caseTypeId);
114 $this->assertEquals($result['values'][$id]['subject'], $params['subject']);
115 }
116
117 /**
118 * Test update (create with id) function with valid parameters.
119 */
120 public function testCaseUpdate() {
121 $params = $this->_params;
122 // Test using name instead of value
123 unset($params['case_type_id']);
124 $params['case_type'] = $this->caseType;
125 $result = $this->callAPISuccess('case', 'create', $params);
126 $id = $result['id'];
127 $case = $this->callAPISuccess('case', 'getsingle', array('id' => $id));
128
129 // Update Case.
130 $params = array('id' => $id);
131 $params['subject'] = $case['subject'] = 'Something Else';
132 $this->callAPISuccess('case', 'create', $params);
133
134 // Verify that updated case is exactly equal to the original with new subject.
135 $result = $this->callAPISuccessGetSingle('Case', array('case_id' => $id));
136 $this->assertAPIArrayComparison($result, $case);
137 }
138
139 /**
140 * Test delete function with valid parameters.
141 */
142 public function testCaseDelete() {
143 // Create Case
144 $result = $this->callAPISuccess('case', 'create', $this->_params);
145
146 // Move Case to Trash
147 $id = $result['id'];
148 $result = $this->callAPISuccess('case', 'delete', array('id' => $id, 'move_to_trash' => 1));
149
150 // Check result - also check that 'case_id' works as well as 'id'
151 $result = $this->callAPISuccess('case', 'get', array('case_id' => $id));
152 $this->assertEquals(1, $result['values'][$id]['is_deleted']);
153
154 // Delete Case Permanently - also check that 'case_id' works as well as 'id'
155 $result = $this->callAPISuccess('case', 'delete', array('case_id' => $id));
156
157 // Check result - case should no longer exist
158 $result = $this->callAPISuccess('case', 'get', array('id' => $id));
159 $this->assertEquals(0, $result['count']);
160 }
161
162 /**
163 * Test get function based on activity.
164 */
165 public function testCaseGetByActivity() {
166 // Create Case
167 $result = $this->callAPISuccess('case', 'create', $this->_params);
168 $id = $result['id'];
169
170 // Check result - we should get a list of activity ids
171 $result = $this->callAPISuccess('case', 'get', array('id' => $id, 'return' => 'activities'));
172 $case = $result['values'][$id];
173 $activity = $case['activities'][0];
174
175 // Fetch case based on an activity id
176 $result = $this->callAPISuccess('case', 'get', array(
177 'activity_id' => $activity,
178 'return' => 'activities',
179 ));
180 $this->assertEquals(FALSE, empty($result['values'][$id]));
181 $this->assertEquals($result['values'][$id], $case);
182 }
183
184 /**
185 * Test get function based on contact id.
186 */
187 public function testCaseGetByContact() {
188 // Create Case
189 $result = $this->callAPISuccess('case', 'create', $this->_params);
190 $id = $result['id'];
191
192 // Store result for later
193 $case = $this->callAPISuccessGetSingle('case', array('id' => $id, 'return' => array('activities', 'contacts')));
194
195 // Fetch case based on client contact id
196 $result = $this->callAPISuccess('case', 'get', array(
197 'client_id' => $this->_params['contact_id'],
198 'return' => array('activities', 'contacts'),
199 ));
200 $this->assertAPIArrayComparison($result['values'][$id], $case);
201 }
202
203 /**
204 * Test get function based on subject.
205 */
206 public function testCaseGetBySubject() {
207 // Create Case
208 $result = $this->callAPISuccess('case', 'create', $this->_params);
209 $id = $result['id'];
210
211 // Store result for later
212 $case = $this->callAPISuccessGetSingle('Case', array('id' => $id, 'return' => 'subject'));
213
214 // Fetch case based on client contact id
215 $result = $this->callAPISuccess('case', 'get', array(
216 'subject' => $this->_params['subject'],
217 'return' => array('subject'),
218 ));
219 $this->assertAPIArrayComparison($result['values'][$id], $case);
220 }
221
222 /**
223 * Test get function based on wrong subject.
224 */
225 public function testCaseGetByWrongSubject() {
226 $result = $this->callAPISuccess('case', 'create', $this->_params);
227
228 // Append 'wrong' to subject so that it is no longer the same.
229 $result = $this->callAPISuccess('case', 'get', array(
230 'subject' => $this->_params['subject'] . 'wrong',
231 'return' => array('activities', 'contacts'),
232 ));
233 $this->assertEquals(0, $result['count']);
234 }
235
236 /**
237 * Test get function with no criteria.
238 */
239 public function testCaseGetNoCriteria() {
240 $result = $this->callAPISuccess('case', 'create', $this->_params);
241 $id = $result['id'];
242
243 // Store result for later
244 $case = $this->callAPISuccessGetSingle('Case', array('id' => $id, 'return' => 'contact_id'));
245
246 $result = $this->callAPISuccess('case', 'get', array('limit' => 0, 'return' => array('contact_id')));
247 $this->assertAPIArrayComparison($result['values'][$id], $case);
248 }
249
250 /**
251 * Test activity api create for case activities.
252 */
253 public function testCaseActivityCreate() {
254 $params = $this->_params;
255 $this->callAPISuccess('case', 'create', $params);
256 $params = array(
257 'case_id' => 1,
258 // follow up
259 'activity_type_id' => $this->followup_activity_type_value,
260 'subject' => 'Test followup',
261 'source_contact_id' => $this->_loggedInUser,
262 'target_contact_id' => $this->_params['contact_id'],
263 );
264 $result = $this->callAPISuccess('activity', 'create', $params);
265 $this->assertEquals($result['values'][$result['id']]['activity_type_id'], $params['activity_type_id']);
266
267 // might need this for other tests that piggyback on this one
268 $this->_caseActivityId = $result['values'][$result['id']]['id'];
269
270 // Check other DB tables populated properly - is there a better way to do this? assertDBState() requires that we know the id already.
271 $dao = new CRM_Case_DAO_CaseActivity();
272 $dao->case_id = 1;
273 $dao->activity_id = $this->_caseActivityId;
274 $this->assertEquals($dao->find(), 1, 'case_activity table not populated correctly in line ' . __LINE__);
275 $dao->free();
276
277 $dao = new CRM_Activity_DAO_ActivityContact();
278 $dao->activity_id = $this->_caseActivityId;
279 $dao->contact_id = $this->_params['contact_id'];
280 $dao->record_type_id = 3;
281 $this->assertEquals($dao->find(), 1, 'activity_contact table not populated correctly in line ' . __LINE__);
282 $dao->free();
283
284 // TODO: There's more things we could check
285 }
286
287 /**
288 * Test activity api update for case activities.
289 */
290 public function testCaseActivityUpdate() {
291 // Need to create the case and activity before we can update it
292 $this->testCaseActivityCreate();
293
294 $params = array(
295 'activity_id' => $this->_caseActivityId,
296 'case_id' => 1,
297 'activity_type_id' => 14,
298 'source_contact_id' => $this->_loggedInUser,
299 'subject' => 'New subject',
300 );
301 $result = $this->callAPISuccess('activity', 'create', $params);
302
303 $this->assertEquals($result['values'][$result['id']]['subject'], $params['subject']);
304
305 // id should be one greater, since this is a new revision
306 $this->assertEquals($result['values'][$result['id']]['id'],
307 $this->_caseActivityId + 1,
308 'in line ' . __LINE__
309 );
310 $this->assertEquals($result['values'][$result['id']]['original_id'],
311 $this->_caseActivityId,
312 'in line ' . __LINE__
313 );
314
315 // Check revision is as expected
316 $revParams = array(
317 'activity_id' => $this->_caseActivityId,
318 );
319 $revActivity = $this->callAPISuccess('activity', 'get', $revParams);
320 $this->assertEquals($revActivity['values'][$this->_caseActivityId]['is_current_revision'],
321 0);
322 $this->assertEquals($revActivity['values'][$this->_caseActivityId]['is_deleted'],
323 0
324 );
325
326 //TODO: check some more things
327 }
328
329 public function testCaseActivityUpdateCustom() {
330 // Create a case first
331 $result = $this->callAPISuccess('case', 'create', $this->_params);
332
333 // Create custom field group
334 // Note the second parameter is Activity on purpose, not Case.
335 $custom_ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, 'ActivityTest.php');
336
337 // create activity
338 $params = array(
339 'case_id' => $result['id'],
340 // follow up
341 'activity_type_id' => 14,
342 'subject' => 'Test followup',
343 'source_contact_id' => $this->_loggedInUser,
344 'target_contact_id' => $this->_params['contact_id'],
345 'custom_' . $custom_ids['custom_field_id'] => "custom string",
346 );
347 $result = $this->callAPISuccess('activity', 'create', $params);
348
349 $aid = $result['values'][$result['id']]['id'];
350
351 // Update activity
352 $params = array(
353 'activity_id' => $aid,
354 'case_id' => 1,
355 'activity_type_id' => 14,
356 'source_contact_id' => $this->_loggedInUser,
357 'subject' => 'New subject',
358 );
359 $this->callAPISuccess('activity', 'create', $params);
360
361 // Retrieve revision and check custom fields got copied.
362 $revParams = array(
363 'activity_id' => $aid + 1,
364 'return.custom_' . $custom_ids['custom_field_id'] => 1,
365 );
366 $revAct = $this->callAPISuccess('activity', 'get', $revParams);
367
368 $this->assertEquals($revAct['values'][$aid + 1]['custom_' . $custom_ids['custom_field_id']], "custom string",
369 "Error message: " . CRM_Utils_Array::value('error_message', $revAct));
370
371 $this->customFieldDelete($custom_ids['custom_field_id']);
372 $this->customGroupDelete($custom_ids['custom_group_id']);
373 }
374
375 public function testCaseGetByStatus() {
376 // Create 2 cases with different status ids.
377 $case1 = $this->callAPISuccess('Case', 'create', array(
378 'contact_id' => 17,
379 'subject' => "Test case 1",
380 'case_type_id' => $this->caseTypeId,
381 'status_id' => "Open",
382 'sequential' => 1,
383 ));
384 $this->callAPISuccess('Case', 'create', array(
385 'contact_id' => 17,
386 'subject' => "Test case 2",
387 'case_type_id' => $this->caseTypeId,
388 'status_id' => "Urgent",
389 'sequential' => 1,
390 ));
391 $result = $this->callAPISuccessGetSingle('Case', array(
392 'sequential' => 1,
393 'contact_id' => 17,
394 'status_id' => "Open",
395 ));
396 $this->assertEquals($case1['id'], $result['id']);
397 }
398
399 }