4 * File for the TestCase class
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 $
15 * This file is part of CiviCRM
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.
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.
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/>.
33 * Include class definitions
37 * Test APIv3 civicrm_case_* functions
39 * @package CiviCRM_APIv3
42 class api_v3_CaseTest
extends CiviCaseTestCase
{
45 protected $_apiversion = 3;
46 protected $followup_activity_type_value;
48 * Activity ID of created case.
52 protected $_caseActivityId;
55 * @var \Civi\Core\SettingsStack
57 protected $settingsStack;
60 * Test setup for every test.
62 * Connect to the database, truncate the tables that will be used
63 * and redirect stdin to a temporary file.
65 public function setUp() {
66 $this->_entity
= 'case';
70 $activityTypes = $this->callAPISuccess('option_value', 'get', array(
71 'option_group_id' => 2,
72 'name' => 'Follow Up',
73 'label' => 'Follow Up',
76 $this->followup_activity_type_value
= $activityTypes['values'][0]['value'];
78 $this->_params
= array(
79 'case_type_id' => $this->caseTypeId
,
80 'subject' => 'Test case',
84 $this->settingsStack
= new \Civi\Core\
SettingsStack();
87 public function tearDown() {
88 $this->settingsStack
->popAll();
93 * Check with empty array.
95 public function testCaseCreateEmpty() {
96 $this->callAPIFailure('case', 'create', array());
100 * Check if required fields are not passed.
102 public function testCaseCreateWithoutRequired() {
104 'subject' => 'this case should fail',
108 $this->callAPIFailure('case', 'create', $params);
112 * Test Getlist with id and case_id
114 public function testCaseGetListById() {
115 $params = $this->_params
;
116 $params['contact_id'] = $this->individualCreate();
118 //Create 3 sample Cases.
119 $case1 = $this->callAPISuccess('case', 'create', $params);
120 $params['subject'] = 'Test Case 2';
121 $case2 = $this->callAPISuccess('case', 'create', $params);
122 $params['subject'] = 'Test Case 3';
123 $case3 = $this->callAPISuccess('case', 'create', $params);
126 'id' => array($case1['id']),
127 'extra' => array('contact_id'),
130 'case_id' => array('!=' => $case2['id']),
131 'case_id.is_deleted' => 0,
132 'case_id.status_id' => array('!=' => "Closed"),
133 'case_id.end_date' => array('IS NULL' => 1),
136 $result = $this->callAPISuccess('case', 'getlist', $getParams);
138 //Only 1 case should be returned.
139 $this->assertEquals(count($result['values']), 1);
140 $this->assertEquals($result['values'][0]['id'], $case1['id']);
144 * Test create function with valid parameters.
146 public function testCaseCreate() {
147 $params = $this->_params
;
148 // Test using label instead of value.
149 unset($params['case_type_id']);
150 $params['case_type'] = $this->caseType
;
151 $result = $this->callAPIAndDocument('case', 'create', $params, __FUNCTION__
, __FILE__
);
155 $result = $this->callAPISuccess('case', 'get', array('id' => $id));
156 $this->assertEquals($result['values'][$id]['id'], $id);
157 $this->assertEquals($result['values'][$id]['case_type_id'], $this->caseTypeId
);
158 $this->assertEquals($result['values'][$id]['subject'], $params['subject']);
162 * Test create function with resolved status.
164 public function testCaseCreateWithResolvedStatus() {
165 $params = $this->_params
;
166 // Test using label instead of value.
167 unset($params['case_type_id']);
168 $params['case_type'] = $this->caseType
;
169 $params['status_id'] = 'Closed';
170 $result = $this->callAPISuccess('case', 'create', $params);
174 $result = $this->callAPISuccess('case', 'get', array('id' => $id));
175 $this->assertEquals($result['values'][$id]['id'], $id);
176 $this->assertEquals($result['values'][$id]['case_type_id'], $this->caseTypeId
);
177 $this->assertEquals($result['values'][$id]['subject'], $params['subject']);
178 $this->assertEquals($result['values'][$id]['end_date'], date('Y-m-d'));
180 //Check all relationship end dates are set to case end date.
181 $relationships = $this->callAPISuccess('Relationship', 'get', array(
185 foreach ($relationships['values'] as $key => $values) {
186 $this->assertEquals($values['end_date'], date('Y-m-d'));
191 * Test case create with valid parameters and custom data.
193 public function testCaseCreateCustom() {
194 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__
, __FILE__
);
195 $params = $this->_params
;
196 $params['custom_' . $ids['custom_field_id']] = "custom string";
197 $result = $this->callAPIAndDocument($this->_entity
, 'create', $params, __FUNCTION__
, __FILE__
);
198 $result = $this->callAPISuccess($this->_entity
, 'get', array(
199 'return.custom_' . $ids['custom_field_id'] => 1,
200 'id' => $result['id'],
202 $this->assertEquals("custom string", $result['values'][$result['id']]['custom_' . $ids['custom_field_id']], ' in line ' . __LINE__
);
204 $this->customFieldDelete($ids['custom_field_id']);
205 $this->customGroupDelete($ids['custom_group_id']);
209 * Test update (create with id) function with valid parameters.
211 public function testCaseUpdate() {
212 $params = $this->_params
;
213 // Test using name instead of value
214 unset($params['case_type_id']);
215 $params['case_type'] = $this->caseType
;
216 $result = $this->callAPISuccess('case', 'create', $params);
218 $case = $this->callAPISuccess('case', 'getsingle', array('id' => $id));
221 $params = array('id' => $id);
222 $params['subject'] = $case['subject'] = 'Something Else';
223 $this->callAPISuccess('case', 'create', $params);
225 // Verify that updated case is equal to the original with new subject.
226 $result = $this->callAPISuccessGetSingle('Case', array('case_id' => $id));
227 // Modification dates are likely to differ by 0-2 sec. Check manually.
228 $this->assertGreaterThanOrEqual($case['modified_date'], $result['modified_date']);
229 unset($result['modified_date'], $case['modified_date']);
230 // Everything else should be identical.
231 $this->assertAPIArrayComparison($result, $case);
235 * Test update (create with id) function with valid parameters.
237 public function testCaseUpdateWithExistingCaseContact() {
238 $params = $this->_params
;
239 // Test using name instead of value
240 unset($params['case_type_id']);
241 $params['case_type'] = $this->caseType
;
242 $result = $this->callAPISuccess('case', 'create', $params);
244 $case = $this->callAPISuccess('case', 'getsingle', array('id' => $id));
246 // Update Case, we specify existing case ID and existing contact ID to verify that CaseContact.create is not called
247 $params = $this->_params
;
249 $this->callAPISuccess('case', 'create', $params);
251 // Verify that updated case is equal to the original with new subject.
252 $result = $this->callAPISuccessGetSingle('Case', array('case_id' => $id));
253 // Modification dates are likely to differ by 0-2 sec. Check manually.
254 $this->assertGreaterThanOrEqual($case['modified_date'], $result['modified_date']);
255 unset($result['modified_date'], $case['modified_date']);
256 // Everything else should be identical.
257 $this->assertAPIArrayComparison($result, $case);
261 * Test case update with custom data
263 public function testCaseUpdateCustom() {
264 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__
, __FILE__
);
265 $params = $this->_params
;
267 // Create a case with custom data
268 $params['custom_' . $ids['custom_field_id']] = 'custom string';
269 $result = $this->callAPISuccess($this->_entity
, 'create', $params);
271 $caseId = $result['id'];
272 $result = $this->callAPISuccess($this->_entity
, 'get', array(
273 'return.custom_' . $ids['custom_field_id'] => 1,
275 'id' => $result['id'],
277 $this->assertEquals("custom string", $result['values'][$result['id']]['custom_' . $ids['custom_field_id']]);
278 $fields = $this->callAPISuccess($this->_entity
, 'getfields', array('version' => $this->_apiversion
));
279 $this->assertTrue(is_array($fields['values']['custom_' . $ids['custom_field_id']]));
281 // Update the activity with custom data.
284 'custom_' . $ids['custom_field_id'] => 'Updated my test data',
285 'version' => $this->_apiversion
,
287 $result = $this->callAPISuccess($this->_entity
, 'create', $params);
289 $result = $this->callAPISuccess($this->_entity
, 'get', array(
290 'return.custom_' . $ids['custom_field_id'] => 1,
292 'id' => $result['id'],
294 $this->assertEquals("Updated my test data", $result['values'][$result['id']]['custom_' . $ids['custom_field_id']]);
298 * Test delete function with valid parameters.
300 public function testCaseDelete() {
302 $result = $this->callAPISuccess('case', 'create', $this->_params
);
304 // Move Case to Trash
306 $this->callAPISuccess('case', 'delete', array('id' => $id, 'move_to_trash' => 1));
308 // Check result - also check that 'case_id' works as well as 'id'
309 $result = $this->callAPISuccess('case', 'get', array('case_id' => $id));
310 $this->assertEquals(1, $result['values'][$id]['is_deleted']);
312 // Restore Case from Trash
313 $this->callAPISuccess('case', 'restore', array('id' => $id));
316 $result = $this->callAPISuccess('case', 'get', array('case_id' => $id));
317 $this->assertEquals(0, $result['values'][$id]['is_deleted']);
319 // Delete Case Permanently
320 $this->callAPISuccess('case', 'delete', array('case_id' => $id));
322 // Check result - case should no longer exist
323 $result = $this->callAPISuccess('case', 'get', array('id' => $id));
324 $this->assertEquals(0, $result['count']);
328 * Test Case role relationship is correctly created
331 public function testCaseRoleRelationships() {
333 $case = $this->callAPISuccess('case', 'create', $this->_params
);
334 $relType = $this->relationshipTypeCreate(array('name_a_b' => 'Test AB', 'name_b_a' => 'Test BA', 'contact_type_b' => 'Individual'));
335 $relContact = $this->individualCreate(array('first_name' => 'First', 'last_name' => 'Last'));
338 'rel_type' => "{$relType}_b_a",
339 'rel_contact' => $relContact,
340 'case_id' => $case['id'],
341 'is_unit_test' => TRUE,
343 $ret = CRM_Contact_Page_AJAX
::relationship();
344 $this->assertEquals(0, $ret['is_error']);
345 //Check if relationship exist for the case.
346 $relationship = $this->callAPISuccess('Relationship', 'get', array(
348 'relationship_type_id' => $relType,
349 'case_id' => $case['id'],
351 $this->assertEquals($relContact, $relationship['values'][0]['contact_id_a']);
352 $this->assertEquals($this->_params
['contact_id'], $relationship['values'][0]['contact_id_b']);
354 //Check if activity is assigned to correct contact.
355 $activity = $this->callAPISuccess('Activity', 'get', array(
356 'subject' => 'Test BA : Mr. First Last II',
358 $activityContact = $this->callAPISuccess('ActivityContact', 'get', array(
359 'contact_id' => $relContact,
360 'activity_id' => $activity['id'],
365 * Test get function based on activity.
367 public function testCaseGetByActivity() {
369 $result = $this->callAPISuccess('case', 'create', $this->_params
);
372 // Check result - we should get a list of activity ids
373 $result = $this->callAPISuccess('case', 'get', array('id' => $id, 'return' => 'activities'));
374 $case = $result['values'][$id];
375 $activity = $case['activities'][0];
377 // Fetch case based on an activity id
378 $result = $this->callAPISuccess('case', 'get', array(
379 'activity_id' => $activity,
380 'return' => 'activities',
382 $this->assertEquals(FALSE, empty($result['values'][$id]));
383 $this->assertEquals($result['values'][$id], $case);
387 * Test get function based on contact id.
389 public function testCaseGetByContact() {
391 $result = $this->callAPISuccess('case', 'create', $this->_params
);
394 // Store result for later
395 $case = $this->callAPISuccessGetSingle('case', array('id' => $id, 'return' => array('activities', 'contacts')));
397 // Fetch case based on client contact id
398 $result = $this->callAPISuccess('case', 'get', array(
399 'client_id' => $this->_params
['contact_id'],
400 'return' => array('activities', 'contacts'),
402 $this->assertAPIArrayComparison($result['values'][$id], $case);
406 * Test get function based on subject.
408 public function testCaseGetBySubject() {
410 $result = $this->callAPISuccess('case', 'create', $this->_params
);
413 // Store result for later
414 $case = $this->callAPISuccessGetSingle('Case', array('id' => $id, 'return' => 'subject'));
416 // Fetch case based on client contact id
417 $result = $this->callAPISuccess('case', 'get', array(
418 'subject' => $this->_params
['subject'],
419 'return' => array('subject'),
421 $this->assertAPIArrayComparison($result['values'][$id], $case);
425 * Test get function based on wrong subject.
427 public function testCaseGetByWrongSubject() {
428 $result = $this->callAPISuccess('case', 'create', $this->_params
);
430 // Append 'wrong' to subject so that it is no longer the same.
431 $result = $this->callAPISuccess('case', 'get', array(
432 'subject' => $this->_params
['subject'] . 'wrong',
433 'return' => array('activities', 'contacts'),
435 $this->assertEquals(0, $result['count']);
439 * Test get function with no criteria.
441 public function testCaseGetNoCriteria() {
442 $result = $this->callAPISuccess('case', 'create', $this->_params
);
445 // Store result for later
446 $case = $this->callAPISuccessGetSingle('Case', array('id' => $id, 'return' => 'contact_id'));
448 $result = $this->callAPISuccess('case', 'get', array('limit' => 0, 'return' => array('contact_id')));
449 $this->assertAPIArrayComparison($result['values'][$id], $case);
453 * Test activity api create for case activities.
455 public function testCaseActivityCreate() {
456 $params = $this->_params
;
457 $case = $this->callAPISuccess('case', 'create', $params);
459 'case_id' => $case['id'],
461 'activity_type_id' => $this->followup_activity_type_value
,
462 'subject' => 'Test followup 123',
463 'source_contact_id' => $this->_loggedInUser
,
464 'target_contact_id' => $this->_params
['contact_id'],
466 $result = $this->callAPISuccess('activity', 'create', $params);
467 $this->assertEquals($result['values'][$result['id']]['activity_type_id'], $params['activity_type_id']);
469 // might need this for other tests that piggyback on this one
470 $this->_caseActivityId
= $result['values'][$result['id']]['id'];
472 // Check other DB tables populated properly - is there a better way to do this? assertDBState() requires that we know the id already.
473 $dao = new CRM_Case_DAO_CaseActivity();
474 $dao->case_id
= $case['id'];
475 $dao->activity_id
= $this->_caseActivityId
;
476 $this->assertEquals($dao->find(), 1, 'case_activity table not populated correctly in line ' . __LINE__
);
479 $dao = new CRM_Activity_DAO_ActivityContact();
480 $dao->activity_id
= $this->_caseActivityId
;
481 $dao->contact_id
= $this->_params
['contact_id'];
482 $dao->record_type_id
= 3;
483 $this->assertEquals($dao->find(), 1, 'activity_contact table not populated correctly in line ' . __LINE__
);
486 // Check that fetching an activity by case id works, as well as returning case_id
487 $result = $this->callAPISuccessGetSingle('Activity', array(
488 'case_id' => $case['id'],
489 'activity_type_id' => $this->followup_activity_type_value
,
490 'subject' => 'Test followup 123',
491 'return' => array('case_id'),
493 $this->assertContains($case['id'], $result['case_id']);
497 * Test activity api update for case activities.
499 public function testCaseActivityUpdate_Tracked() {
500 $this->settingsStack
->push('civicaseActivityRevisions', TRUE);
502 // Need to create the case and activity before we can update it
503 $this->testCaseActivityCreate();
506 'activity_id' => $this->_caseActivityId
,
508 'activity_type_id' => 14,
509 'source_contact_id' => $this->_loggedInUser
,
510 'subject' => 'New subject',
512 $result = $this->callAPISuccess('activity', 'create', $params);
514 $this->assertEquals($result['values'][$result['id']]['subject'], $params['subject']);
516 // id should be one greater, since this is a new revision
517 $this->assertEquals($result['values'][$result['id']]['id'],
518 $this->_caseActivityId +
1,
519 'in line ' . __LINE__
521 $this->assertEquals($result['values'][$result['id']]['original_id'],
522 $this->_caseActivityId
,
523 'in line ' . __LINE__
526 // Check revision is as expected
528 'activity_id' => $this->_caseActivityId
,
530 $revActivity = $this->callAPISuccess('activity', 'get', $revParams);
531 $this->assertEquals($revActivity['values'][$this->_caseActivityId
]['is_current_revision'],
533 $this->assertEquals($revActivity['values'][$this->_caseActivityId
]['is_deleted'],
537 //TODO: check some more things
541 * If you disable `civicaseActivityRevisions`, then editing an activity
542 * will *not* create or change IDs.
544 public function testCaseActivityUpdate_Untracked() {
545 $this->settingsStack
->push('civicaseActivityRevisions', FALSE);
547 // Need to create the case and activity before we can update it
548 $this->testCaseActivityCreate();
550 $oldIDs = CRM_Utils_SQL_Select
::from('civicrm_activity')
551 ->select('id, original_id, is_current_revision')
553 ->execute()->fetchAll();
556 'activity_id' => $this->_caseActivityId
,
558 'activity_type_id' => 14,
559 'source_contact_id' => $this->_loggedInUser
,
560 'subject' => 'New subject',
562 $result = $this->callAPISuccess('activity', 'create', $params);
563 $this->assertEquals($result['values'][$result['id']]['subject'], $params['subject']);
565 // id should not change because we've opted out.
566 $this->assertEquals($this->_caseActivityId
, $result['values'][$result['id']]['id']);
567 $this->assertEmpty($result['values'][$result['id']]['original_id']);
569 $newIDs = CRM_Utils_SQL_Select
::from('civicrm_activity')
570 ->select('id, original_id, is_current_revision')
572 ->execute()->fetchAll();
573 $this->assertEquals($oldIDs, $newIDs);
576 public function testCaseActivityUpdateCustom() {
577 $this->settingsStack
->push('civicaseActivityRevisions', TRUE);
579 // Create a case first
580 $result = $this->callAPISuccess('case', 'create', $this->_params
);
582 // Create custom field group
583 // Note the second parameter is Activity on purpose, not Case.
584 $custom_ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__
, 'ActivityTest.php');
588 'case_id' => $result['id'],
590 'activity_type_id' => 14,
591 'subject' => 'Test followup',
592 'source_contact_id' => $this->_loggedInUser
,
593 'target_contact_id' => $this->_params
['contact_id'],
594 'custom_' . $custom_ids['custom_field_id'] => "custom string",
596 $result = $this->callAPISuccess('activity', 'create', $params);
598 $aid = $result['values'][$result['id']]['id'];
602 'activity_id' => $aid,
604 'activity_type_id' => 14,
605 'source_contact_id' => $this->_loggedInUser
,
606 'subject' => 'New subject',
608 $this->callAPISuccess('activity', 'create', $params);
610 // Retrieve revision and check custom fields got copied.
612 'activity_id' => $aid +
1,
613 'return.custom_' . $custom_ids['custom_field_id'] => 1,
615 $revAct = $this->callAPISuccess('activity', 'get', $revParams);
617 $this->assertEquals($revAct['values'][$aid +
1]['custom_' . $custom_ids['custom_field_id']], "custom string",
618 "Error message: " . CRM_Utils_Array
::value('error_message', $revAct));
620 $this->customFieldDelete($custom_ids['custom_field_id']);
621 $this->customGroupDelete($custom_ids['custom_group_id']);
624 public function testCaseGetByStatus() {
625 // Create 2 cases with different status ids.
626 $case1 = $this->callAPISuccess('Case', 'create', array(
628 'subject' => "Test case 1",
629 'case_type_id' => $this->caseTypeId
,
630 'status_id' => "Open",
633 $this->callAPISuccess('Case', 'create', array(
635 'subject' => "Test case 2",
636 'case_type_id' => $this->caseTypeId
,
637 'status_id' => "Urgent",
640 $result = $this->callAPISuccessGetSingle('Case', array(
643 'status_id' => "Open",
645 $this->assertEquals($case1['id'], $result['id']);
648 public function testCaseGetWithRoles() {
649 $case1 = $this->callAPISuccess('Case', 'create', array(
651 'subject' => "Test case with roles",
652 'case_type_id' => $this->caseTypeId
,
653 'status_id' => "Open",
655 $result = $this->callAPISuccessGetSingle('Case', array(
656 'id' => $case1['id'],
657 'status_id' => "Open",
658 'return' => array('contacts'),
660 foreach ($result['contacts'] as $contact) {
661 if ($contact['role'] == 'Client') {
662 $this->assertEquals(17, $contact['contact_id']);
664 elseif ($contact['role'] == 'Homeless Services Coordinator') {
665 $this->assertEquals(1, $contact['creator']);
666 $this->assertEquals(1, $contact['manager']);
671 public function testCaseGetWithDefinition() {
672 $case1 = $this->callAPISuccess('Case', 'create', array(
674 'subject' => "Test case with definition",
675 'case_type_id' => $this->caseTypeId
,
676 'status_id' => "Open",
678 $result1 = $this->callAPISuccessGetSingle('Case', array(
679 'id' => $case1['id'],
680 'status_id' => "Open",
681 'return' => array('case_type_id.definition'),
683 $result2 = $this->callAPISuccessGetSingle('Case', array(
684 'id' => $case1['id'],
685 'status_id' => "Open",
686 'return' => array('case_type_id', 'case_type_id.definition'),
688 $this->assertEquals($result1['case_type_id.definition'], $result2['case_type_id.definition']);
689 $def = $result1['case_type_id.definition'];
690 $this->assertEquals(array('name' => 'Open Case', 'max_instances' => 1), $def['activityTypes'][0]);
691 $this->assertNotEmpty($def['activitySets'][0]['activityTypes']);
692 $this->assertNotEmpty($def['caseRoles'][0]['manager']);
693 $this->assertNotEmpty($def['caseRoles'][0]['creator']);
696 public function testCaseGetTags() {
697 $case1 = $this->callAPISuccess('Case', 'create', array(
699 'subject' => "Test case with tags",
700 'case_type_id' => $this->caseTypeId
,
701 'status_id' => "Open",
703 $tag1 = $this->tagCreate(array(
704 'name' => 'CaseTag1',
705 'used_for' => 'civicrm_case',
707 $tag2 = $this->tagCreate(array(
708 'name' => 'CaseTag2',
709 'used_for' => 'civicrm_case',
711 $this->callAPISuccess('EntityTag', 'create', array(
712 'entity_table' => 'civicrm_case',
713 'entity_id' => $case1['id'],
714 'tag_id' => $tag1['id'],
716 $this->callAPIFailure('Case', 'getsingle', array(
717 'tag_id' => $tag2['id'],
719 $result = $this->callAPISuccessGetSingle('Case', array(
720 'tag_id' => $tag1['id'],
721 'return' => 'tag_id.name',
723 $this->assertEquals('CaseTag1', $result['tag_id'][$tag1['id']]['tag_id.name']);
727 * Test that a chained api call can use the operator syntax.
729 * E.g. array('IN' => $value.contact_id)
733 public function testCaseGetChainedOp() {
734 $contact1 = $this->individualCreate(array(), 1);
735 $contact2 = $this->individualCreate(array(), 2);
736 $case1 = $this->callAPISuccess('Case', 'create', array(
737 'contact_id' => $contact1,
738 'subject' => "Test case 1",
739 'case_type_id' => $this->caseTypeId
,
741 $case2 = $this->callAPISuccess('Case', 'create', array(
742 'contact_id' => $contact2,
743 'subject' => "Test case 2",
744 'case_type_id' => $this->caseTypeId
,
746 $case3 = $this->callAPISuccess('Case', 'create', array(
747 'contact_id' => array($contact1, $contact2),
748 'subject' => "Test case 3",
749 'case_type_id' => $this->caseTypeId
,
752 // Fetch case 1 and all cases with the same client. Chained get should return case 3.
753 $result = $this->callAPISuccessGetSingle('Case', array(
754 'id' => $case1['id'],
755 'return' => 'contact_id',
756 'api.Case.get' => array(
757 'contact_id' => array('IN' => "\$value.contact_id"),
758 'id' => array('!=' => "\$value.id"),
761 $this->assertEquals($case3['id'], $result['api.Case.get']['id']);
763 // Fetch case 3 and all cases with the same clients. Chained get should return case 1&2.
764 $result = $this->callAPISuccessGetSingle('Case', array(
765 'id' => $case3['id'],
766 'return' => array('contact_id'),
767 'api.Case.get' => array(
769 'contact_id' => array('IN' => "\$value.contact_id"),
770 'id' => array('!=' => "\$value.id"),
773 $this->assertEquals(array($case1['id'], $case2['id']), array_keys(CRM_Utils_Array
::rekey($result['api.Case.get']['values'], 'id')));
777 * Test the ability to order by client using the join syntax.
779 * For multi-client cases, should order by the first client.
781 public function testCaseGetOrderByClient() {
782 $contact1 = $this->individualCreate(array('first_name' => 'Aa', 'last_name' => 'Zz'));
783 $contact2 = $this->individualCreate(array('first_name' => 'Bb', 'last_name' => 'Zz'));
784 $contact3 = $this->individualCreate(array('first_name' => 'Cc', 'last_name' => 'Xx'));
786 $case1 = $this->callAPISuccess('Case', 'create', array(
787 'contact_id' => $contact1,
788 'subject' => "Test case 1",
789 'case_type_id' => $this->caseTypeId
,
791 $case2 = $this->callAPISuccess('Case', 'create', array(
792 'contact_id' => $contact2,
793 'subject' => "Test case 2",
794 'case_type_id' => $this->caseTypeId
,
796 $case3 = $this->callAPISuccess('Case', 'create', array(
797 'contact_id' => array($contact3, $contact1),
798 'subject' => "Test case 3",
799 'case_type_id' => $this->caseTypeId
,
802 $result = $this->callAPISuccess('Case', 'get', array(
803 'contact_id' => array('IN' => array($contact1, $contact2, $contact3)),
806 'options' => array('sort' => 'contact_id.first_name'),
808 $this->assertEquals($case3['id'], $result['values'][2]['id']);
809 $this->assertEquals($case2['id'], $result['values'][1]['id']);
810 $this->assertEquals($case1['id'], $result['values'][0]['id']);
812 $result = $this->callAPISuccess('Case', 'get', array(
813 'contact_id' => array('IN' => array($contact1, $contact2, $contact3)),
816 'options' => array('sort' => 'contact_id.last_name ASC, contact_id.first_name DESC'),
818 $this->assertEquals($case1['id'], $result['values'][2]['id']);
819 $this->assertEquals($case2['id'], $result['values'][1]['id']);
820 $this->assertEquals($case3['id'], $result['values'][0]['id']);
822 $result = $this->callAPISuccess('Case', 'get', array(
823 'contact_id' => array('IN' => array($contact1, $contact2, $contact3)),
826 'options' => array('sort' => 'contact_id.first_name DESC'),
828 $this->assertEquals($case1['id'], $result['values'][2]['id']);
829 $this->assertEquals($case2['id'], $result['values'][1]['id']);
830 $this->assertEquals($case3['id'], $result['values'][0]['id']);
832 $result = $this->callAPISuccess('Case', 'get', array(
833 'contact_id' => array('IN' => array($contact1, $contact2, $contact3)),
836 'options' => array('sort' => 'case_type_id, contact_id DESC, status_id'),
838 $this->assertEquals($case1['id'], $result['values'][2]['id']);
839 $this->assertEquals($case2['id'], $result['values'][1]['id']);
840 $this->assertEquals($case3['id'], $result['values'][0]['id']);
841 $this->assertCount(3, $result['values']);
845 * Test the ability to add a timeline to an existing case.
847 * See the case.addtimeline api.
849 * @dataProvider caseActivityRevisionExamples
852 public function testCaseAddtimeline($enableRevisions) {
853 $this->settingsStack
->push('civicaseActivityRevisions', $enableRevisions);
856 'title' => 'Application with Definition',
857 'name' => 'Application_with_Definition',
860 'definition' => array(
861 'activityTypes' => array(
862 array('name' => 'Follow up'),
864 'activitySets' => array(
867 'label' => 'Label 1',
869 'activityTypes' => array(
870 array('name' => 'Open Case', 'status' => 'Completed'),
875 'label' => 'Label 2',
877 'activityTypes' => array(
878 array('name' => 'Follow up'),
882 'caseRoles' => array(
883 array('name' => 'Homeless Services Coordinator', 'creator' => 1, 'manager' => 1),
887 $cid = $this->individualCreate();
888 $caseType = $this->callAPISuccess('CaseType', 'create', $caseSpec);
889 $case = $this->callAPISuccess('Case', 'create', array(
890 'case_type_id' => $caseType['id'],
891 'contact_id' => $cid,
892 'subject' => 'Test case with timeline',
894 // Created case should only have 1 activity per the spec
895 $result = $this->callAPISuccessGetSingle('Activity', array('case_id' => $case['id'], 'return' => 'activity_type_id.name'));
896 $this->assertEquals('Open Case', $result['activity_type_id.name']);
898 $timeline = civicrm_api('Case', 'addtimeline', array(
899 'case_id' => $case['id'],
900 'timeline' => 'set2',
903 $result = $this->callAPISuccess('Activity', 'get', array(
904 'case_id' => $case['id'],
905 'return' => 'activity_type_id.name',
907 'options' => array('sort' => 'id'),
909 $this->assertEquals(2, $result['count']);
910 $this->assertEquals('Follow up', $result['values'][1]['activity_type_id.name']);
914 * Test the case merge function.
916 * 2 cases should be mergeable into 1
920 public function testCaseMerge() {
921 $contact1 = $this->individualCreate(array(), 1);
922 $case1 = $this->callAPISuccess('Case', 'create', array(
923 'contact_id' => $contact1,
924 'subject' => "Test case 1",
925 'case_type_id' => $this->caseTypeId
,
927 $case2 = $this->callAPISuccess('Case', 'create', array(
928 'contact_id' => $contact1,
929 'subject' => "Test case 2",
930 'case_type_id' => $this->caseTypeId
,
932 $result = $this->callAPISuccess('Case', 'getcount', array('contact_id' => $contact1));
933 $this->assertEquals(2, $result);
935 $this->callAPISuccess('Case', 'merge', array('case_id_1' => $case1['id'], 'case_id_2' => $case2['id']));
937 $result = $this->callAPISuccess('Case', 'getsingle', array('id' => $case2['id']));
938 $this->assertEquals(1, $result['is_deleted']);
941 public function caseActivityRevisionExamples() {
943 $examples[] = array(FALSE);
944 $examples[] = array(TRUE);
948 public function testTimestamps() {
949 $params = $this->_params
;
950 $case_created = $this->callAPISuccess('case', 'create', $params);
952 $case_1 = $this->callAPISuccess('Case', 'getsingle', array(
953 'id' => $case_created['id'],
955 $this->assertRegExp(';^\d\d\d\d-\d\d-\d\d \d\d:\d\d;', $case_1['created_date']);
956 $this->assertRegExp(';^\d\d\d\d-\d\d-\d\d \d\d:\d\d;', $case_1['modified_date']);
957 $this->assertApproxEquals(strtotime($case_1['created_date']), strtotime($case_1['modified_date']), 2);
959 $activity_1 = $this->callAPISuccess('activity', 'getsingle', array(
960 'case_id' => $case_created['id'],
965 $this->assertRegExp(';^\d\d\d\d-\d\d-\d\d \d\d:\d\d;', $activity_1['created_date']);
966 $this->assertRegExp(';^\d\d\d\d-\d\d-\d\d \d\d:\d\d;', $activity_1['modified_date']);
967 $this->assertApproxEquals(strtotime($activity_1['created_date']), strtotime($activity_1['modified_date']), 2);
969 usleep(1.5 * 1000000);
970 $this->callAPISuccess('activity', 'create', array(
971 'id' => $activity_1['id'],
972 'subject' => 'Make cheese',
975 $activity_2 = $this->callAPISuccess('activity', 'getsingle', array(
976 'id' => $activity_1['id'],
978 $this->assertRegExp(';^\d\d\d\d-\d\d-\d\d \d\d:\d\d;', $activity_2['created_date']);
979 $this->assertRegExp(';^\d\d\d\d-\d\d-\d\d \d\d:\d\d;', $activity_2['modified_date']);
980 $this->assertNotEquals($activity_2['created_date'], $activity_2['modified_date']);
982 $this->assertEquals($activity_1['created_date'], $activity_2['created_date']);
983 $this->assertNotEquals($activity_1['modified_date'], $activity_2['modified_date']);
984 $this->assertLessThan($activity_2['modified_date'], $activity_1['modified_date'],
985 sprintf("Original modification time (%s) should predate later modification time (%s)", $activity_1['modified_date'], $activity_2['modified_date']));
987 $case_2 = $this->callAPISuccess('Case', 'getsingle', array(
988 'id' => $case_created['id'],
990 $this->assertRegExp(';^\d\d\d\d-\d\d-\d\d \d\d:\d\d;', $case_2['created_date']);
991 $this->assertRegExp(';^\d\d\d\d-\d\d-\d\d \d\d:\d\d;', $case_2['modified_date']);
992 $this->assertEquals($case_1['created_date'], $case_2['created_date']);
993 $this->assertNotEquals($case_2['created_date'], $case_2['modified_date']);