Merge pull request #16469 from civicrm/5.22
[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 * Test APIv3 civicrm_case_* functions
34 *
35 * @package CiviCRM_APIv3
36 * @group headless
37 */
38 class api_v3_CaseTest extends CiviCaseTestCase {
39 protected $_params;
40 protected $_entity;
41 protected $_apiversion = 3;
42 protected $followup_activity_type_value;
43 /**
44 * Activity ID of created case.
45 *
46 * @var int
47 */
48 protected $_caseActivityId;
49
50 /**
51 * @var \Civi\Core\SettingsStack
52 */
53 protected $settingsStack;
54
55 /**
56 * Test setup for every test.
57 *
58 * Connect to the database, truncate the tables that will be used
59 * and redirect stdin to a temporary file.
60 */
61 public function setUp() {
62 $this->_entity = 'case';
63
64 parent::setUp();
65
66 $activityTypes = $this->callAPISuccess('option_value', 'get', [
67 'option_group_id' => 2,
68 'name' => 'Follow Up',
69 'label' => 'Follow Up',
70 'sequential' => 1,
71 ]);
72 $this->followup_activity_type_value = $activityTypes['values'][0]['value'];
73
74 $this->_params = [
75 'case_type_id' => $this->caseTypeId,
76 'subject' => 'Test case',
77 'contact_id' => 17,
78 ];
79
80 $this->settingsStack = new \Civi\Core\SettingsStack();
81 }
82
83 public function tearDown() {
84 $this->settingsStack->popAll();
85 parent::tearDown();
86 }
87
88 /**
89 * Check with empty array.
90 */
91 public function testCaseCreateEmpty() {
92 $this->callAPIFailure('case', 'create', []);
93 }
94
95 /**
96 * Check if required fields are not passed.
97 */
98 public function testCaseCreateWithoutRequired() {
99 $params = [
100 'subject' => 'this case should fail',
101 'case_type_id' => 1,
102 ];
103
104 $this->callAPIFailure('case', 'create', $params);
105 }
106
107 /**
108 * Test Getlist with id and case_id
109 */
110 public function testCaseGetListById() {
111 $params = $this->_params;
112 $params['contact_id'] = $this->individualCreate();
113
114 //Create 3 sample Cases.
115 $case1 = $this->callAPISuccess('case', 'create', $params);
116 $params['subject'] = 'Test Case 2';
117 $case2 = $this->callAPISuccess('case', 'create', $params);
118 $params['subject'] = 'Test Case 3';
119 $this->callAPISuccess('case', 'create', $params);
120
121 $getParams = [
122 'id' => [$case1['id']],
123 'extra' => ['contact_id'],
124 'params' => [
125 'version' => 3,
126 'case_id' => ['!=' => $case2['id']],
127 'case_id.is_deleted' => 0,
128 'case_id.status_id' => ['!=' => "Closed"],
129 'case_id.end_date' => ['IS NULL' => 1],
130 ],
131 ];
132 $result = $this->callAPISuccess('case', 'getlist', $getParams);
133
134 //Only 1 case should be returned.
135 $this->assertEquals(count($result['values']), 1);
136 $this->assertEquals($result['values'][0]['id'], $case1['id']);
137 }
138
139 /**
140 * Test create function with valid parameters.
141 */
142 public function testCaseCreate() {
143 $params = $this->_params;
144 // Test using label instead of value.
145 unset($params['case_type_id']);
146 $params['case_type'] = $this->caseType;
147 $result = $this->callAPIAndDocument('case', 'create', $params, __FUNCTION__, __FILE__);
148 $id = $result['id'];
149
150 // Check result
151 $result = $this->callAPISuccess('case', 'get', ['id' => $id]);
152 $this->assertEquals($result['values'][$id]['id'], $id);
153 $this->assertEquals($result['values'][$id]['case_type_id'], $this->caseTypeId);
154 $this->assertEquals($result['values'][$id]['subject'], $params['subject']);
155 }
156
157 /**
158 * Test create function with resolved status.
159 */
160 public function testCaseCreateWithResolvedStatus() {
161 $params = $this->_params;
162 // Test using label instead of value.
163 unset($params['case_type_id']);
164 $params['case_type'] = $this->caseType;
165 $params['status_id'] = 'Closed';
166 $result = $this->callAPISuccess('case', 'create', $params);
167 $id = $result['id'];
168
169 // Check result
170 $result = $this->callAPISuccess('case', 'get', ['id' => $id]);
171 $this->assertEquals($result['values'][$id]['id'], $id);
172 $this->assertEquals($result['values'][$id]['case_type_id'], $this->caseTypeId);
173 $this->assertEquals($result['values'][$id]['subject'], $params['subject']);
174 $this->assertEquals($result['values'][$id]['end_date'], date('Y-m-d'));
175
176 //Check all relationship end dates are set to case end date.
177 $relationships = $this->callAPISuccess('Relationship', 'get', [
178 'sequential' => 1,
179 'case_id' => $id,
180 ]);
181 foreach ($relationships['values'] as $key => $values) {
182 $this->assertEquals($values['end_date'], date('Y-m-d'));
183 }
184
185 //Verify there are no active relationships.
186 $activeCaseRelationships = CRM_Case_BAO_Case::getCaseRoles($result['values'][$id]['client_id'][1], $id);
187 $this->assertEquals(count($activeCaseRelationships), 0, "Checking for empty array");
188
189 //Check if getCaseRoles() is able to return inactive relationships.
190 $caseRelationships = CRM_Case_BAO_Case::getCaseRoles($result['values'][$id]['client_id'][1], $id, NULL, FALSE);
191 $this->assertEquals(count($caseRelationships), 1);
192 }
193
194 /**
195 * Test case create with valid parameters and custom data.
196 */
197 public function testCaseCreateCustom() {
198 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, __FILE__);
199 $params = $this->_params;
200 $params['custom_' . $ids['custom_field_id']] = "custom string";
201 $result = $this->callAPIAndDocument($this->_entity, 'create', $params, __FUNCTION__, __FILE__);
202 $result = $this->callAPISuccess($this->_entity, 'get', [
203 'return.custom_' . $ids['custom_field_id'] => 1,
204 'id' => $result['id'],
205 ]);
206 $this->assertEquals("custom string", $result['values'][$result['id']]['custom_' . $ids['custom_field_id']]);
207
208 $this->customFieldDelete($ids['custom_field_id']);
209 $this->customGroupDelete($ids['custom_group_id']);
210 }
211
212 /**
213 * Test update (create with id) function with valid parameters.
214 */
215 public function testCaseUpdate() {
216 $params = $this->_params;
217 // Test using name instead of value
218 unset($params['case_type_id']);
219 $params['case_type'] = $this->caseType;
220 $result = $this->callAPISuccess('case', 'create', $params);
221 $id = $result['id'];
222 $case = $this->callAPISuccess('case', 'getsingle', ['id' => $id]);
223
224 // Update Case.
225 $params = ['id' => $id];
226 $params['subject'] = $case['subject'] = 'Something Else';
227 $this->callAPISuccess('case', 'create', $params);
228
229 // Verify that updated case is equal to the original with new subject.
230 $result = $this->callAPISuccessGetSingle('Case', ['case_id' => $id]);
231 // Modification dates are likely to differ by 0-2 sec. Check manually.
232 $this->assertGreaterThanOrEqual($case['modified_date'], $result['modified_date']);
233 unset($result['modified_date'], $case['modified_date']);
234 // Everything else should be identical.
235 $this->assertAPIArrayComparison($result, $case);
236 }
237
238 /**
239 * Test update (create with id) function with valid parameters.
240 */
241 public function testCaseUpdateWithExistingCaseContact() {
242 $params = $this->_params;
243 // Test using name instead of value
244 unset($params['case_type_id']);
245 $params['case_type'] = $this->caseType;
246 $result = $this->callAPISuccess('case', 'create', $params);
247 $id = $result['id'];
248 $case = $this->callAPISuccess('case', 'getsingle', ['id' => $id]);
249
250 // Update Case, we specify existing case ID and existing contact ID to verify that CaseContact.create is not called
251 $params = $this->_params;
252 $params['id'] = $id;
253 $this->callAPISuccess('case', 'create', $params);
254
255 // Verify that updated case is equal to the original with new subject.
256 $result = $this->callAPISuccessGetSingle('Case', ['case_id' => $id]);
257 // Modification dates are likely to differ by 0-2 sec. Check manually.
258 $this->assertGreaterThanOrEqual($case['modified_date'], $result['modified_date']);
259 unset($result['modified_date'], $case['modified_date']);
260 // Everything else should be identical.
261 $this->assertAPIArrayComparison($result, $case);
262 }
263
264 /**
265 * Test case update with custom data
266 */
267 public function testCaseUpdateCustom() {
268 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, __FILE__);
269 $params = $this->_params;
270
271 // Create a case with custom data
272 $params['custom_' . $ids['custom_field_id']] = 'custom string';
273 $result = $this->callAPISuccess($this->_entity, 'create', $params);
274
275 $caseId = $result['id'];
276 $result = $this->callAPISuccess($this->_entity, 'get', [
277 'return.custom_' . $ids['custom_field_id'] => 1,
278 'version' => 3,
279 'id' => $result['id'],
280 ]);
281 $this->assertEquals("custom string", $result['values'][$result['id']]['custom_' . $ids['custom_field_id']]);
282 $fields = $this->callAPISuccess($this->_entity, 'getfields', ['version' => $this->_apiversion]);
283 $this->assertTrue(is_array($fields['values']['custom_' . $ids['custom_field_id']]));
284
285 // Update the activity with custom data.
286 $params = [
287 'id' => $caseId,
288 'custom_' . $ids['custom_field_id'] => 'Updated my test data',
289 'version' => $this->_apiversion,
290 ];
291 $result = $this->callAPISuccess($this->_entity, 'create', $params);
292
293 $result = $this->callAPISuccess($this->_entity, 'get', [
294 'return.custom_' . $ids['custom_field_id'] => 1,
295 'version' => 3,
296 'id' => $result['id'],
297 ]);
298 $this->assertEquals("Updated my test data", $result['values'][$result['id']]['custom_' . $ids['custom_field_id']]);
299 }
300
301 /**
302 * Test delete function with valid parameters.
303 */
304 public function testCaseDelete() {
305 // Create Case
306 $result = $this->callAPISuccess('case', 'create', $this->_params);
307
308 // Move Case to Trash
309 $id = $result['id'];
310 $this->callAPISuccess('case', 'delete', ['id' => $id, 'move_to_trash' => 1]);
311
312 // Check result - also check that 'case_id' works as well as 'id'
313 $result = $this->callAPISuccess('case', 'get', ['case_id' => $id]);
314 $this->assertEquals(1, $result['values'][$id]['is_deleted']);
315
316 // Restore Case from Trash
317 $this->callAPISuccess('case', 'restore', ['id' => $id]);
318
319 // Check result
320 $result = $this->callAPISuccess('case', 'get', ['case_id' => $id]);
321 $this->assertEquals(0, $result['values'][$id]['is_deleted']);
322
323 // Delete Case Permanently
324 $this->callAPISuccess('case', 'delete', ['case_id' => $id]);
325
326 // Check result - case should no longer exist
327 $result = $this->callAPISuccess('case', 'get', ['id' => $id]);
328 $this->assertEquals(0, $result['count']);
329 }
330
331 /**
332 * Test Case role relationship is correctly created
333 * for contacts.
334 */
335 public function testCaseRoleRelationships() {
336 // Create Case
337 $case = $this->callAPISuccess('case', 'create', $this->_params);
338 $relType = $this->relationshipTypeCreate(['name_a_b' => 'Test AB', 'name_b_a' => 'Test BA', 'contact_type_b' => 'Individual']);
339 $relContact = $this->individualCreate(['first_name' => 'First', 'last_name' => 'Last']);
340
341 $_REQUEST = [
342 'rel_type' => "{$relType}_b_a",
343 'rel_contact' => $relContact,
344 'case_id' => $case['id'],
345 'is_unit_test' => TRUE,
346 ];
347 $ret = CRM_Contact_Page_AJAX::relationship();
348 $this->assertEquals(0, $ret['is_error']);
349 //Check if relationship exist for the case.
350 $relationship = $this->callAPISuccess('Relationship', 'get', [
351 'sequential' => 1,
352 'relationship_type_id' => $relType,
353 'case_id' => $case['id'],
354 ]);
355 $this->assertEquals($relContact, $relationship['values'][0]['contact_id_a']);
356 $this->assertEquals($this->_params['contact_id'], $relationship['values'][0]['contact_id_b']);
357
358 //Check if activity is assigned to correct contact.
359 $activity = $this->callAPISuccess('Activity', 'get', [
360 'subject' => 'Test BA : Mr. First Last II',
361 ]);
362 $this->callAPISuccess('ActivityContact', 'get', [
363 'contact_id' => $relContact,
364 'activity_id' => $activity['id'],
365 ]);
366 }
367
368 /**
369 * Test get function based on activity.
370 */
371 public function testCaseGetByActivity() {
372 // Create Case
373 $result = $this->callAPISuccess('case', 'create', $this->_params);
374 $id = $result['id'];
375
376 // Check result - we should get a list of activity ids
377 $result = $this->callAPISuccess('case', 'get', ['id' => $id, 'return' => 'activities']);
378 $case = $result['values'][$id];
379 $activity = $case['activities'][0];
380
381 // Fetch case based on an activity id
382 $result = $this->callAPISuccess('case', 'get', [
383 'activity_id' => $activity,
384 'return' => 'activities',
385 ]);
386 $this->assertEquals(FALSE, empty($result['values'][$id]));
387 $this->assertEquals($result['values'][$id], $case);
388 }
389
390 /**
391 * Test get function based on contact id.
392 */
393 public function testCaseGetByContact() {
394 // Create Case
395 $result = $this->callAPISuccess('case', 'create', $this->_params);
396 $id = $result['id'];
397
398 // Store result for later
399 $case = $this->callAPISuccessGetSingle('case', ['id' => $id, 'return' => ['activities', 'contacts']]);
400
401 // Fetch case based on client contact id
402 $result = $this->callAPISuccess('case', 'get', [
403 'client_id' => $this->_params['contact_id'],
404 'return' => ['activities', 'contacts'],
405 ]);
406 $this->assertAPIArrayComparison($result['values'][$id], $case);
407 }
408
409 /**
410 * Test get function based on subject.
411 */
412 public function testCaseGetBySubject() {
413 // Create Case
414 $result = $this->callAPISuccess('case', 'create', $this->_params);
415 $id = $result['id'];
416
417 // Store result for later
418 $case = $this->callAPISuccessGetSingle('Case', ['id' => $id, 'return' => 'subject']);
419
420 // Fetch case based on client contact id
421 $result = $this->callAPISuccess('case', 'get', [
422 'subject' => $this->_params['subject'],
423 'return' => ['subject'],
424 ]);
425 $this->assertAPIArrayComparison($result['values'][$id], $case);
426 }
427
428 /**
429 * Test get function based on wrong subject.
430 */
431 public function testCaseGetByWrongSubject() {
432 $this->callAPISuccess('case', 'create', $this->_params);
433
434 // Append 'wrong' to subject so that it is no longer the same.
435 $result = $this->callAPISuccess('case', 'get', [
436 'subject' => $this->_params['subject'] . 'wrong',
437 'return' => ['activities', 'contacts'],
438 ]);
439 $this->assertEquals(0, $result['count']);
440 }
441
442 /**
443 * Test get function with no criteria.
444 */
445 public function testCaseGetNoCriteria() {
446 $result = $this->callAPISuccess('case', 'create', $this->_params);
447 $id = $result['id'];
448
449 // Store result for later
450 $case = $this->callAPISuccessGetSingle('Case', ['id' => $id, 'return' => 'contact_id']);
451
452 $result = $this->callAPISuccess('case', 'get', ['limit' => 0, 'return' => ['contact_id']]);
453 $this->assertAPIArrayComparison($result['values'][$id], $case);
454 }
455
456 /**
457 * Test activity api create for case activities.
458 */
459 public function testCaseActivityCreate() {
460 $params = $this->_params;
461 $case = $this->callAPISuccess('case', 'create', $params);
462 $params = [
463 'case_id' => $case['id'],
464 // follow up
465 'activity_type_id' => $this->followup_activity_type_value,
466 'subject' => 'Test followup 123',
467 'source_contact_id' => $this->_loggedInUser,
468 'target_contact_id' => $this->_params['contact_id'],
469 ];
470 $result = $this->callAPISuccess('activity', 'create', $params);
471 $this->assertEquals($result['values'][$result['id']]['activity_type_id'], $params['activity_type_id']);
472
473 // might need this for other tests that piggyback on this one
474 $this->_caseActivityId = $result['values'][$result['id']]['id'];
475
476 // Check other DB tables populated properly - is there a better way to do this? assertDBState() requires that we know the id already.
477 $dao = new CRM_Case_DAO_CaseActivity();
478 $dao->case_id = $case['id'];
479 $dao->activity_id = $this->_caseActivityId;
480 $this->assertEquals($dao->find(), 1, 'case_activity table not populated correctly');
481
482 $dao = new CRM_Activity_DAO_ActivityContact();
483 $dao->activity_id = $this->_caseActivityId;
484 $dao->contact_id = $this->_params['contact_id'];
485 $dao->record_type_id = 3;
486 $this->assertEquals($dao->find(), 1, 'activity_contact table not populated correctly');
487
488 // Check that fetching an activity by case id works, as well as returning case_id
489 $result = $this->callAPISuccessGetSingle('Activity', [
490 'case_id' => $case['id'],
491 'activity_type_id' => $this->followup_activity_type_value,
492 'subject' => 'Test followup 123',
493 'return' => ['case_id'],
494 ]);
495 $this->assertContains($case['id'], $result['case_id']);
496 }
497
498 /**
499 * Test activity api update for case activities.
500 */
501 public function testCaseActivityUpdate_Tracked() {
502 $this->settingsStack->push('civicaseActivityRevisions', TRUE);
503
504 // Need to create the case and activity before we can update it
505 $this->testCaseActivityCreate();
506
507 $params = [
508 'activity_id' => $this->_caseActivityId,
509 'case_id' => 1,
510 'activity_type_id' => 14,
511 'source_contact_id' => $this->_loggedInUser,
512 'subject' => 'New subject',
513 ];
514 $result = $this->callAPISuccess('activity', 'create', $params);
515
516 $this->assertEquals($result['values'][$result['id']]['subject'], $params['subject']);
517
518 // id should be one greater, since this is a new revision
519 $this->assertEquals($result['values'][$result['id']]['id'], $this->_caseActivityId + 1);
520 $this->assertEquals($result['values'][$result['id']]['original_id'], $this->_caseActivityId);
521
522 // Check revision is as expected
523 $revParams = [
524 'activity_id' => $this->_caseActivityId,
525 ];
526 $revActivity = $this->callAPISuccess('activity', 'get', $revParams);
527 $this->assertEquals($revActivity['values'][$this->_caseActivityId]['is_current_revision'],
528 0);
529 $this->assertEquals($revActivity['values'][$this->_caseActivityId]['is_deleted'],
530 0
531 );
532 }
533
534 /**
535 * If you disable `civicaseActivityRevisions`, then editing an activity
536 * will *not* create or change IDs.
537 */
538 public function testCaseActivityUpdate_Untracked() {
539 $this->settingsStack->push('civicaseActivityRevisions', FALSE);
540
541 // Need to create the case and activity before we can update it
542 $this->testCaseActivityCreate();
543
544 $oldIDs = CRM_Utils_SQL_Select::from('civicrm_activity')
545 ->select('id, original_id, is_current_revision')
546 ->orderBy('id')
547 ->execute()->fetchAll();
548
549 $params = [
550 'activity_id' => $this->_caseActivityId,
551 'case_id' => 1,
552 'activity_type_id' => 14,
553 'source_contact_id' => $this->_loggedInUser,
554 'subject' => 'New subject',
555 ];
556 $result = $this->callAPISuccess('activity', 'create', $params);
557 $this->assertEquals($result['values'][$result['id']]['subject'], $params['subject']);
558
559 // id should not change because we've opted out.
560 $this->assertEquals($this->_caseActivityId, $result['values'][$result['id']]['id']);
561 $this->assertEmpty($result['values'][$result['id']]['original_id']);
562
563 $newIDs = CRM_Utils_SQL_Select::from('civicrm_activity')
564 ->select('id, original_id, is_current_revision')
565 ->orderBy('id')
566 ->execute()->fetchAll();
567 $this->assertEquals($oldIDs, $newIDs);
568 }
569
570 public function testCaseActivityUpdateCustom() {
571 $this->settingsStack->push('civicaseActivityRevisions', TRUE);
572
573 // Create a case first
574 $result = $this->callAPISuccess('case', 'create', $this->_params);
575
576 // Create custom field group
577 // Note the second parameter is Activity on purpose, not Case.
578 $custom_ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, 'ActivityTest.php');
579
580 // create activity
581 $params = [
582 'case_id' => $result['id'],
583 // follow up
584 'activity_type_id' => 14,
585 'subject' => 'Test followup',
586 'source_contact_id' => $this->_loggedInUser,
587 'target_contact_id' => $this->_params['contact_id'],
588 'custom_' . $custom_ids['custom_field_id'] => "custom string",
589 ];
590 $result = $this->callAPISuccess('activity', 'create', $params);
591
592 $aid = $result['values'][$result['id']]['id'];
593
594 // Update activity
595 $params = [
596 'activity_id' => $aid,
597 'case_id' => 1,
598 'activity_type_id' => 14,
599 'source_contact_id' => $this->_loggedInUser,
600 'subject' => 'New subject',
601 ];
602 $this->callAPISuccess('activity', 'create', $params);
603
604 // Retrieve revision and check custom fields got copied.
605 $revParams = [
606 'activity_id' => $aid + 1,
607 'return.custom_' . $custom_ids['custom_field_id'] => 1,
608 ];
609 $revAct = $this->callAPISuccess('activity', 'get', $revParams);
610
611 $this->assertEquals($revAct['values'][$aid + 1]['custom_' . $custom_ids['custom_field_id']], "custom string",
612 "Error message: " . CRM_Utils_Array::value('error_message', $revAct));
613
614 $this->customFieldDelete($custom_ids['custom_field_id']);
615 $this->customGroupDelete($custom_ids['custom_group_id']);
616 }
617
618 public function testCaseGetByStatus() {
619 // Create 2 cases with different status ids.
620 $case1 = $this->callAPISuccess('Case', 'create', [
621 'contact_id' => 17,
622 'subject' => "Test case 1",
623 'case_type_id' => $this->caseTypeId,
624 'status_id' => "Open",
625 'sequential' => 1,
626 ]);
627 $this->callAPISuccess('Case', 'create', [
628 'contact_id' => 17,
629 'subject' => "Test case 2",
630 'case_type_id' => $this->caseTypeId,
631 'status_id' => "Urgent",
632 'sequential' => 1,
633 ]);
634 $result = $this->callAPISuccessGetSingle('Case', [
635 'sequential' => 1,
636 'contact_id' => 17,
637 'status_id' => "Open",
638 ]);
639 $this->assertEquals($case1['id'], $result['id']);
640 }
641
642 public function testCaseGetWithRoles() {
643 $case1 = $this->callAPISuccess('Case', 'create', [
644 'contact_id' => 17,
645 'subject' => "Test case with roles",
646 'case_type_id' => $this->caseTypeId,
647 'status_id' => "Open",
648 ]);
649 $result = $this->callAPISuccessGetSingle('Case', [
650 'id' => $case1['id'],
651 'status_id' => "Open",
652 'return' => ['contacts'],
653 ]);
654
655 $foundManager = FALSE;
656 foreach ($result['contacts'] as $contact) {
657 if ($contact['role'] == 'Client') {
658 $this->assertEquals(17, $contact['contact_id']);
659 }
660 elseif ($contact['role'] == 'Homeless Services Coordinator is') {
661 $this->assertEquals(1, $contact['creator']);
662 $this->assertEquals(1, $contact['manager']);
663 $foundManager = TRUE;
664 }
665 }
666 $this->assertTrue($foundManager);
667 }
668
669 public function testCaseGetWithDefinition() {
670 $case1 = $this->callAPISuccess('Case', 'create', [
671 'contact_id' => 17,
672 'subject' => "Test case with definition",
673 'case_type_id' => $this->caseTypeId,
674 'status_id' => "Open",
675 ]);
676 $result1 = $this->callAPISuccessGetSingle('Case', [
677 'id' => $case1['id'],
678 'status_id' => "Open",
679 'return' => ['case_type_id.definition'],
680 ]);
681 $result2 = $this->callAPISuccessGetSingle('Case', [
682 'id' => $case1['id'],
683 'status_id' => "Open",
684 'return' => ['case_type_id', 'case_type_id.definition'],
685 ]);
686 $this->assertEquals($result1['case_type_id.definition'], $result2['case_type_id.definition']);
687 $def = $result1['case_type_id.definition'];
688 $this->assertEquals(['name' => 'Open Case', 'max_instances' => 1], $def['activityTypes'][0]);
689 $this->assertNotEmpty($def['activitySets'][0]['activityTypes']);
690 $this->assertNotEmpty($def['caseRoles'][0]['manager']);
691 $this->assertNotEmpty($def['caseRoles'][0]['creator']);
692 }
693
694 public function testCaseGetTags() {
695 $case1 = $this->callAPISuccess('Case', 'create', [
696 'contact_id' => 17,
697 'subject' => "Test case with tags",
698 'case_type_id' => $this->caseTypeId,
699 'status_id' => "Open",
700 ]);
701 $tag1 = $this->tagCreate([
702 'name' => 'CaseTag1',
703 'used_for' => 'civicrm_case',
704 ]);
705 $tag2 = $this->tagCreate([
706 'name' => 'CaseTag2',
707 'used_for' => 'civicrm_case',
708 ]);
709 $this->callAPISuccess('EntityTag', 'create', [
710 'entity_table' => 'civicrm_case',
711 'entity_id' => $case1['id'],
712 'tag_id' => $tag1['id'],
713 ]);
714 $this->callAPIFailure('Case', 'getsingle', [
715 'tag_id' => $tag2['id'],
716 ]);
717 $result = $this->callAPISuccessGetSingle('Case', [
718 'tag_id' => $tag1['id'],
719 'return' => 'tag_id.name',
720 ]);
721 $this->assertEquals('CaseTag1', $result['tag_id'][$tag1['id']]['tag_id.name']);
722 }
723
724 /**
725 * Test that a chained api call can use the operator syntax.
726 *
727 * E.g. array('IN' => $value.contact_id)
728 *
729 * @throws \Exception
730 */
731 public function testCaseGetChainedOp() {
732 $contact1 = $this->individualCreate([], 1);
733 $contact2 = $this->individualCreate([], 2);
734 $case1 = $this->callAPISuccess('Case', 'create', [
735 'contact_id' => $contact1,
736 'subject' => "Test case 1",
737 'case_type_id' => $this->caseTypeId,
738 ]);
739 $case2 = $this->callAPISuccess('Case', 'create', [
740 'contact_id' => $contact2,
741 'subject' => "Test case 2",
742 'case_type_id' => $this->caseTypeId,
743 ]);
744 $case3 = $this->callAPISuccess('Case', 'create', [
745 'contact_id' => [$contact1, $contact2],
746 'subject' => "Test case 3",
747 'case_type_id' => $this->caseTypeId,
748 ]);
749
750 // Fetch case 1 and all cases with the same client. Chained get should return case 3.
751 $result = $this->callAPISuccessGetSingle('Case', [
752 'id' => $case1['id'],
753 'return' => 'contact_id',
754 'api.Case.get' => [
755 'contact_id' => ['IN' => "\$value.contact_id"],
756 'id' => ['!=' => "\$value.id"],
757 ],
758 ]);
759 $this->assertEquals($case3['id'], $result['api.Case.get']['id']);
760
761 // Fetch case 3 and all cases with the same clients. Chained get should return case 1&2.
762 $result = $this->callAPISuccessGetSingle('Case', [
763 'id' => $case3['id'],
764 'return' => ['contact_id'],
765 'api.Case.get' => [
766 'return' => 'id',
767 'contact_id' => ['IN' => "\$value.contact_id"],
768 'id' => ['!=' => "\$value.id"],
769 ],
770 ]);
771 $this->assertEquals([$case1['id'], $case2['id']], array_keys(CRM_Utils_Array::rekey($result['api.Case.get']['values'], 'id')));
772 }
773
774 /**
775 * Test the ability to order by client using the join syntax.
776 *
777 * For multi-client cases, should order by the first client.
778 */
779 public function testCaseGetOrderByClient() {
780 $contact1 = $this->individualCreate(['first_name' => 'Aa', 'last_name' => 'Zz']);
781 $contact2 = $this->individualCreate(['first_name' => 'Bb', 'last_name' => 'Zz']);
782 $contact3 = $this->individualCreate(['first_name' => 'Cc', 'last_name' => 'Xx']);
783
784 $case1 = $this->callAPISuccess('Case', 'create', [
785 'contact_id' => $contact1,
786 'subject' => "Test case 1",
787 'case_type_id' => $this->caseTypeId,
788 ]);
789 $case2 = $this->callAPISuccess('Case', 'create', [
790 'contact_id' => $contact2,
791 'subject' => "Test case 2",
792 'case_type_id' => $this->caseTypeId,
793 ]);
794 $case3 = $this->callAPISuccess('Case', 'create', [
795 'contact_id' => [$contact3, $contact1],
796 'subject' => "Test case 3",
797 'case_type_id' => $this->caseTypeId,
798 ]);
799
800 $result = $this->callAPISuccess('Case', 'get', [
801 'contact_id' => ['IN' => [$contact1, $contact2, $contact3]],
802 'sequential' => 1,
803 'return' => 'id',
804 'options' => ['sort' => 'contact_id.first_name'],
805 ]);
806 $this->assertEquals($case3['id'], $result['values'][2]['id']);
807 $this->assertEquals($case2['id'], $result['values'][1]['id']);
808 $this->assertEquals($case1['id'], $result['values'][0]['id']);
809
810 $result = $this->callAPISuccess('Case', 'get', [
811 'contact_id' => ['IN' => [$contact1, $contact2, $contact3]],
812 'sequential' => 1,
813 'return' => 'id',
814 'options' => ['sort' => 'contact_id.last_name ASC, contact_id.first_name DESC'],
815 ]);
816 $this->assertEquals($case1['id'], $result['values'][2]['id']);
817 $this->assertEquals($case2['id'], $result['values'][1]['id']);
818 $this->assertEquals($case3['id'], $result['values'][0]['id']);
819
820 $result = $this->callAPISuccess('Case', 'get', [
821 'contact_id' => ['IN' => [$contact1, $contact2, $contact3]],
822 'sequential' => 1,
823 'return' => 'id',
824 'options' => ['sort' => 'contact_id.first_name DESC'],
825 ]);
826 $this->assertEquals($case1['id'], $result['values'][2]['id']);
827 $this->assertEquals($case2['id'], $result['values'][1]['id']);
828 $this->assertEquals($case3['id'], $result['values'][0]['id']);
829
830 $result = $this->callAPISuccess('Case', 'get', [
831 'contact_id' => ['IN' => [$contact1, $contact2, $contact3]],
832 'sequential' => 1,
833 'return' => 'id',
834 'options' => ['sort' => 'case_type_id, contact_id DESC, status_id'],
835 ]);
836 $this->assertEquals($case1['id'], $result['values'][2]['id']);
837 $this->assertEquals($case2['id'], $result['values'][1]['id']);
838 $this->assertEquals($case3['id'], $result['values'][0]['id']);
839 $this->assertCount(3, $result['values']);
840 }
841
842 /**
843 * Test the ability to add a timeline to an existing case.
844 *
845 * See the case.addtimeline api.
846 *
847 * @param bool $enableRevisions
848 *
849 * @dataProvider caseActivityRevisionExamples
850 *
851 * @throws \Exception
852 */
853 public function testCaseAddtimeline($enableRevisions) {
854 $this->settingsStack->push('civicaseActivityRevisions', $enableRevisions);
855
856 $caseSpec = [
857 'title' => 'Application with Definition',
858 'name' => 'Application_with_Definition',
859 'is_active' => 1,
860 'weight' => 4,
861 'definition' => [
862 'activityTypes' => [
863 ['name' => 'Follow up'],
864 ],
865 'activitySets' => [
866 [
867 'name' => 'set1',
868 'label' => 'Label 1',
869 'timeline' => 1,
870 'activityTypes' => [
871 ['name' => 'Open Case', 'status' => 'Completed'],
872 ],
873 ],
874 [
875 'name' => 'set2',
876 'label' => 'Label 2',
877 'timeline' => 1,
878 'activityTypes' => [
879 ['name' => 'Follow up'],
880 ],
881 ],
882 ],
883 'caseRoles' => [
884 ['name' => 'Homeless Services Coordinator', 'creator' => 1, 'manager' => 1],
885 ],
886 ],
887 ];
888 $cid = $this->individualCreate();
889 $caseType = $this->callAPISuccess('CaseType', 'create', $caseSpec);
890 $case = $this->callAPISuccess('Case', 'create', [
891 'case_type_id' => $caseType['id'],
892 'contact_id' => $cid,
893 'subject' => 'Test case with timeline',
894 ]);
895 // Created case should only have 1 activity per the spec
896 $result = $this->callAPISuccessGetSingle('Activity', ['case_id' => $case['id'], 'return' => 'activity_type_id.name']);
897 $this->assertEquals('Open Case', $result['activity_type_id.name']);
898 // Add timeline.
899 $this->callAPISuccess('Case', 'addtimeline', [
900 'case_id' => $case['id'],
901 'timeline' => 'set2',
902 ]);
903 $result = $this->callAPISuccess('Activity', 'get', [
904 'case_id' => $case['id'],
905 'return' => 'activity_type_id.name',
906 'sequential' => 1,
907 'options' => ['sort' => 'id'],
908 ]);
909 $this->assertEquals(2, $result['count']);
910 $this->assertEquals('Follow up', $result['values'][1]['activity_type_id.name']);
911 }
912
913 /**
914 * Test the case merge function.
915 *
916 * 2 cases should be mergeable into 1
917 *
918 * @throws \Exception
919 */
920 public function testCaseMerge() {
921 $contact1 = $this->individualCreate([], 1);
922 $case1 = $this->callAPISuccess('Case', 'create', [
923 'contact_id' => $contact1,
924 'subject' => "Test case 1",
925 'case_type_id' => $this->caseTypeId,
926 ]);
927 $case2 = $this->callAPISuccess('Case', 'create', [
928 'contact_id' => $contact1,
929 'subject' => "Test case 2",
930 'case_type_id' => $this->caseTypeId,
931 ]);
932 $result = $this->callAPISuccess('Case', 'getcount', ['contact_id' => $contact1]);
933 $this->assertEquals(2, $result);
934
935 $this->callAPISuccess('Case', 'merge', ['case_id_1' => $case1['id'], 'case_id_2' => $case2['id']]);
936
937 $result = $this->callAPISuccess('Case', 'getsingle', ['id' => $case2['id']]);
938 $this->assertEquals(1, $result['is_deleted']);
939 }
940
941 /**
942 * Get case activity revision sample data.
943 *
944 * @return array
945 */
946 public function caseActivityRevisionExamples() {
947 $examples = [];
948 $examples[] = [FALSE];
949 $examples[] = [TRUE];
950 return $examples;
951 }
952
953 public function testTimestamps() {
954 $params = $this->_params;
955 $case_created = $this->callAPISuccess('case', 'create', $params);
956
957 $case_1 = $this->callAPISuccess('Case', 'getsingle', [
958 'id' => $case_created['id'],
959 ]);
960 $this->assertRegExp(';^\d\d\d\d-\d\d-\d\d \d\d:\d\d;', $case_1['created_date']);
961 $this->assertRegExp(';^\d\d\d\d-\d\d-\d\d \d\d:\d\d;', $case_1['modified_date']);
962 $this->assertApproxEquals(strtotime($case_1['created_date']), strtotime($case_1['modified_date']), 2);
963
964 $activity_1 = $this->callAPISuccess('activity', 'getsingle', [
965 'case_id' => $case_created['id'],
966 'options' => [
967 'limit' => 1,
968 ],
969 ]);
970 $this->assertRegExp(';^\d\d\d\d-\d\d-\d\d \d\d:\d\d;', $activity_1['created_date']);
971 $this->assertRegExp(';^\d\d\d\d-\d\d-\d\d \d\d:\d\d;', $activity_1['modified_date']);
972 $this->assertApproxEquals(strtotime($activity_1['created_date']), strtotime($activity_1['modified_date']), 2);
973
974 usleep(1.5 * 1000000);
975 $this->callAPISuccess('activity', 'create', [
976 'id' => $activity_1['id'],
977 'subject' => 'Make cheese',
978 ]);
979
980 $activity_2 = $this->callAPISuccess('activity', 'getsingle', [
981 'id' => $activity_1['id'],
982 ]);
983 $this->assertRegExp(';^\d\d\d\d-\d\d-\d\d \d\d:\d\d;', $activity_2['created_date']);
984 $this->assertRegExp(';^\d\d\d\d-\d\d-\d\d \d\d:\d\d;', $activity_2['modified_date']);
985 $this->assertNotEquals($activity_2['created_date'], $activity_2['modified_date']);
986
987 $this->assertEquals($activity_1['created_date'], $activity_2['created_date']);
988 $this->assertNotEquals($activity_1['modified_date'], $activity_2['modified_date']);
989 $this->assertLessThan($activity_2['modified_date'], $activity_1['modified_date'],
990 sprintf("Original modification time (%s) should predate later modification time (%s)", $activity_1['modified_date'], $activity_2['modified_date']));
991
992 $case_2 = $this->callAPISuccess('Case', 'getsingle', [
993 'id' => $case_created['id'],
994 ]);
995 $this->assertRegExp(';^\d\d\d\d-\d\d-\d\d \d\d:\d\d;', $case_2['created_date']);
996 $this->assertRegExp(';^\d\d\d\d-\d\d-\d\d \d\d:\d\d;', $case_2['modified_date']);
997 $this->assertEquals($case_1['created_date'], $case_2['created_date']);
998 $this->assertNotEquals($case_2['created_date'], $case_2['modified_date']);
999 }
1000
1001 }