Merge pull request #10030 from eileenmcnaughton/test
[civicrm-core.git] / tests / phpunit / api / v3 / CaseTest.php
CommitLineData
6a488035
TO
1<?php
2/**
fe482240
EM
3 * @file
4 * File for the TestCase class
6a488035
TO
5 *
6 * (PHP 5)
7 *
6c6e6187
TO
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
6a488035 11 * GNU Affero General Public License version 3
6c6e6187
TO
12 * @version $Id: ActivityTest.php 31254 2010-12-15 10:09:29Z eileen $
13 * @package CiviCRM
6a488035
TO
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/**
9e959479 33 * Include class definitions
6a488035 34 */
6a488035
TO
35
36/**
37 * Test APIv3 civicrm_case_* functions
38 *
6c6e6187 39 * @package CiviCRM_APIv3
acb109b7 40 * @group headless
6a488035 41 */
1d8a8d12 42class api_v3_CaseTest extends CiviCaseTestCase {
6a488035
TO
43 protected $_params;
44 protected $_entity;
6c6e6187 45 protected $_apiversion = 3;
6a488035 46 protected $followup_activity_type_value;
9e959479
EM
47 /**
48 * Activity ID of created case.
49 *
50 * @var int
51 */
52 protected $_caseActivityId;
b7c9bc4c 53
6a488035 54 /**
fe482240 55 * Test setup for every test.
6a488035 56 *
d177a2a6 57 * Connect to the database, truncate the tables that will be used
9e959479 58 * and redirect stdin to a temporary file.
6a488035
TO
59 */
60 public function setUp() {
6a488035
TO
61 $this->_entity = 'case';
62
63 parent::setUp();
0b6f58fa 64
4e420887 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 ));
6a488035 71 $this->followup_activity_type_value = $activityTypes['values'][0]['value'];
6a488035 72
6a488035
TO
73 $this->_params = array(
74 'case_type_id' => $this->caseTypeId,
75 'subject' => 'Test case',
76 'contact_id' => 17,
6a488035 77 );
6a488035
TO
78 }
79
80 /**
fe482240 81 * Check with empty array.
6a488035 82 */
00be9182 83 public function testCaseCreateEmpty() {
fe482240 84 $this->callAPIFailure('case', 'create', array());
6a488035
TO
85 }
86
87 /**
fe482240 88 * Check if required fields are not passed.
6a488035 89 */
00be9182 90 public function testCaseCreateWithoutRequired() {
6a488035
TO
91 $params = array(
92 'subject' => 'this case should fail',
93 'case_type_id' => 1,
6a488035
TO
94 );
95
225d474b 96 $this->callAPIFailure('case', 'create', $params);
6a488035
TO
97 }
98
09d55aa3 99 /**
100 * Test Getlist with id and case_id
101 */
102 public function testCaseGetListById() {
103 $params = $this->_params;
104 $params['contact_id'] = $this->individualCreate();
105
106 //Create 3 sample Cases.
107 $case1 = $this->callAPISuccess('case', 'create', $params);
108 $params['subject'] = 'Test Case 2';
109 $case2 = $this->callAPISuccess('case', 'create', $params);
110 $params['subject'] = 'Test Case 3';
111 $case3 = $this->callAPISuccess('case', 'create', $params);
112
113 $getParams = array(
114 'id' => array($case1['id']),
115 'extra' => array('contact_id'),
116 'params' => array(
117 'version' => 3,
118 'case_id' => array('!=' => $case2['id']),
119 'case_id.is_deleted' => 0,
120 'case_id.status_id' => array('!=' => "Closed"),
121 'case_id.end_date' => array('IS NULL' => 1),
122 ),
123 );
124 $result = $this->callAPISuccess('case', 'getlist', $getParams);
125
126 //Only 1 case should be returned.
127 $this->assertEquals(count($result['values']), 1);
128 $this->assertEquals($result['values'][0]['id'], $case1['id']);
129 }
130
6a488035 131 /**
fe482240 132 * Test create function with valid parameters.
6a488035 133 */
00be9182 134 public function testCaseCreate() {
6a488035 135 $params = $this->_params;
9e959479 136 // Test using label instead of value.
6a488035 137 unset($params['case_type_id']);
82de141d 138 $params['case_type'] = $this->caseType;
32191489 139 $result = $this->callAPIAndDocument('case', 'create', $params, __FUNCTION__, __FILE__);
6a488035
TO
140 $id = $result['id'];
141
142 // Check result
4e420887 143 $result = $this->callAPISuccess('case', 'get', array('id' => $id));
9e959479
EM
144 $this->assertEquals($result['values'][$id]['id'], $id);
145 $this->assertEquals($result['values'][$id]['case_type_id'], $this->caseTypeId);
146 $this->assertEquals($result['values'][$id]['subject'], $params['subject']);
6a488035
TO
147 }
148
149 /**
9e959479 150 * Test update (create with id) function with valid parameters.
6a488035 151 */
00be9182 152 public function testCaseUpdate() {
6a77ff3d
CW
153 $params = $this->_params;
154 // Test using name instead of value
8ffdec17 155 unset($params['case_type_id']);
82de141d 156 $params['case_type'] = $this->caseType;
4e420887 157 $result = $this->callAPISuccess('case', 'create', $params);
6a488035 158 $id = $result['id'];
e61d8c45 159 $case = $this->callAPISuccess('case', 'getsingle', array('id' => $id));
6a488035 160
9e959479 161 // Update Case.
4e420887 162 $params = array('id' => $id);
9937542b 163 $params['subject'] = $case['subject'] = 'Something Else';
9e959479 164 $this->callAPISuccess('case', 'create', $params);
6a488035 165
9e959479 166 // Verify that updated case is exactly equal to the original with new subject.
9937542b 167 $result = $this->callAPISuccessGetSingle('Case', array('case_id' => $id));
e61d8c45 168 $this->assertAPIArrayComparison($result, $case);
6a488035
TO
169 }
170
171 /**
fe482240 172 * Test delete function with valid parameters.
6a488035 173 */
00be9182 174 public function testCaseDelete() {
6a488035 175 // Create Case
4e420887 176 $result = $this->callAPISuccess('case', 'create', $this->_params);
6a488035
TO
177
178 // Move Case to Trash
179 $id = $result['id'];
8572e6de 180 $this->callAPISuccess('case', 'delete', array('id' => $id, 'move_to_trash' => 1));
6a488035
TO
181
182 // Check result - also check that 'case_id' works as well as 'id'
4e420887 183 $result = $this->callAPISuccess('case', 'get', array('case_id' => $id));
9e959479 184 $this->assertEquals(1, $result['values'][$id]['is_deleted']);
6a488035 185
8572e6de
CW
186 // Restore Case from Trash
187 $this->callAPISuccess('case', 'restore', array('id' => $id));
188
189 // Check result
190 $result = $this->callAPISuccess('case', 'get', array('case_id' => $id));
191 $this->assertEquals(0, $result['values'][$id]['is_deleted']);
192
193 // Delete Case Permanently
194 $this->callAPISuccess('case', 'delete', array('case_id' => $id));
6a488035
TO
195
196 // Check result - case should no longer exist
4e420887 197 $result = $this->callAPISuccess('case', 'get', array('id' => $id));
198 $this->assertEquals(0, $result['count']);
6a488035
TO
199 }
200
201 /**
fe482240 202 * Test get function based on activity.
6a488035 203 */
00be9182 204 public function testCaseGetByActivity() {
6a488035 205 // Create Case
4e420887 206 $result = $this->callAPISuccess('case', 'create', $this->_params);
6a488035
TO
207 $id = $result['id'];
208
209 // Check result - we should get a list of activity ids
e61d8c45 210 $result = $this->callAPISuccess('case', 'get', array('id' => $id, 'return' => 'activities'));
6a488035
TO
211 $case = $result['values'][$id];
212 $activity = $case['activities'][0];
213
214 // Fetch case based on an activity id
92915c55
TO
215 $result = $this->callAPISuccess('case', 'get', array(
216 'activity_id' => $activity,
e61d8c45 217 'return' => 'activities',
92915c55 218 ));
9e959479
EM
219 $this->assertEquals(FALSE, empty($result['values'][$id]));
220 $this->assertEquals($result['values'][$id], $case);
6a488035
TO
221 }
222
223 /**
fe482240 224 * Test get function based on contact id.
6a488035 225 */
00be9182 226 public function testCaseGetByContact() {
6a488035 227 // Create Case
4e420887 228 $result = $this->callAPISuccess('case', 'create', $this->_params);
6a488035
TO
229 $id = $result['id'];
230
231 // Store result for later
9937542b 232 $case = $this->callAPISuccessGetSingle('case', array('id' => $id, 'return' => array('activities', 'contacts')));
6a488035
TO
233
234 // Fetch case based on client contact id
92915c55
TO
235 $result = $this->callAPISuccess('case', 'get', array(
236 'client_id' => $this->_params['contact_id'],
79d7553f 237 'return' => array('activities', 'contacts'),
92915c55 238 ));
4e420887 239 $this->assertAPIArrayComparison($result['values'][$id], $case);
6a488035
TO
240 }
241
f21557af 242 /**
fe482240 243 * Test get function based on subject.
f21557af 244 */
00be9182 245 public function testCaseGetBySubject() {
f21557af 246 // Create Case
247 $result = $this->callAPISuccess('case', 'create', $this->_params);
248 $id = $result['id'];
249
250 // Store result for later
9937542b 251 $case = $this->callAPISuccessGetSingle('Case', array('id' => $id, 'return' => 'subject'));
f21557af 252
253 // Fetch case based on client contact id
92915c55
TO
254 $result = $this->callAPISuccess('case', 'get', array(
255 'subject' => $this->_params['subject'],
e61d8c45 256 'return' => array('subject'),
92915c55 257 ));
f21557af 258 $this->assertAPIArrayComparison($result['values'][$id], $case);
259 }
260
261 /**
fe482240 262 * Test get function based on wrong subject.
f21557af 263 */
00be9182 264 public function testCaseGetByWrongSubject() {
f21557af 265 $result = $this->callAPISuccess('case', 'create', $this->_params);
f21557af 266
9e959479 267 // Append 'wrong' to subject so that it is no longer the same.
92915c55
TO
268 $result = $this->callAPISuccess('case', 'get', array(
269 'subject' => $this->_params['subject'] . 'wrong',
79d7553f 270 'return' => array('activities', 'contacts'),
92915c55 271 ));
9e959479 272 $this->assertEquals(0, $result['count']);
f21557af 273 }
274
275 /**
fe482240 276 * Test get function with no criteria.
f21557af 277 */
00be9182 278 public function testCaseGetNoCriteria() {
f21557af 279 $result = $this->callAPISuccess('case', 'create', $this->_params);
280 $id = $result['id'];
281
282 // Store result for later
9937542b 283 $case = $this->callAPISuccessGetSingle('Case', array('id' => $id, 'return' => 'contact_id'));
f21557af 284
e61d8c45 285 $result = $this->callAPISuccess('case', 'get', array('limit' => 0, 'return' => array('contact_id')));
f21557af 286 $this->assertAPIArrayComparison($result['values'][$id], $case);
287 }
288
6a488035 289 /**
fe482240 290 * Test activity api create for case activities.
6a488035 291 */
00be9182 292 public function testCaseActivityCreate() {
6a488035 293 $params = $this->_params;
bc4b6f0f 294 $case = $this->callAPISuccess('case', 'create', $params);
6a488035 295 $params = array(
bc4b6f0f 296 'case_id' => $case['id'],
6a488035
TO
297 // follow up
298 'activity_type_id' => $this->followup_activity_type_value,
bc4b6f0f 299 'subject' => 'Test followup 123',
6a488035
TO
300 'source_contact_id' => $this->_loggedInUser,
301 'target_contact_id' => $this->_params['contact_id'],
6a488035 302 );
4e420887 303 $result = $this->callAPISuccess('activity', 'create', $params);
9e959479 304 $this->assertEquals($result['values'][$result['id']]['activity_type_id'], $params['activity_type_id']);
6a488035
TO
305
306 // might need this for other tests that piggyback on this one
307 $this->_caseActivityId = $result['values'][$result['id']]['id'];
308
309 // Check other DB tables populated properly - is there a better way to do this? assertDBState() requires that we know the id already.
4e420887 310 $dao = new CRM_Case_DAO_CaseActivity();
bc4b6f0f 311 $dao->case_id = $case['id'];
6a488035
TO
312 $dao->activity_id = $this->_caseActivityId;
313 $this->assertEquals($dao->find(), 1, 'case_activity table not populated correctly in line ' . __LINE__);
314 $dao->free();
315
b319d00a 316 $dao = new CRM_Activity_DAO_ActivityContact();
6a488035 317 $dao->activity_id = $this->_caseActivityId;
b319d00a
DL
318 $dao->contact_id = $this->_params['contact_id'];
319 $dao->record_type_id = 3;
320 $this->assertEquals($dao->find(), 1, 'activity_contact table not populated correctly in line ' . __LINE__);
6a488035
TO
321 $dao->free();
322
bc4b6f0f
CW
323 // Check that fetching an activity by case id works, as well as returning case_id
324 $result = $this->callAPISuccessGetSingle('Activity', array(
325 'case_id' => $case['id'],
326 'activity_type_id' => $this->followup_activity_type_value,
327 'subject' => 'Test followup 123',
328 'return' => array('case_id'),
329 ));
330 $this->assertEquals($case['id'], $result['case_id']);
6a488035
TO
331 }
332
333 /**
fe482240 334 * Test activity api update for case activities.
6a488035 335 */
00be9182 336 public function testCaseActivityUpdate() {
6a488035
TO
337 // Need to create the case and activity before we can update it
338 $this->testCaseActivityCreate();
339
340 $params = array(
341 'activity_id' => $this->_caseActivityId,
342 'case_id' => 1,
343 'activity_type_id' => 14,
344 'source_contact_id' => $this->_loggedInUser,
345 'subject' => 'New subject',
6a488035 346 );
4e420887 347 $result = $this->callAPISuccess('activity', 'create', $params);
6a488035 348
9e959479 349 $this->assertEquals($result['values'][$result['id']]['subject'], $params['subject']);
6a488035
TO
350
351 // id should be one greater, since this is a new revision
352 $this->assertEquals($result['values'][$result['id']]['id'],
353 $this->_caseActivityId + 1,
354 'in line ' . __LINE__
355 );
356 $this->assertEquals($result['values'][$result['id']]['original_id'],
357 $this->_caseActivityId,
358 'in line ' . __LINE__
359 );
360
361 // Check revision is as expected
362 $revParams = array(
363 'activity_id' => $this->_caseActivityId,
6a488035 364 );
4e420887 365 $revActivity = $this->callAPISuccess('activity', 'get', $revParams);
6a488035 366 $this->assertEquals($revActivity['values'][$this->_caseActivityId]['is_current_revision'],
4e420887 367 0);
6a488035 368 $this->assertEquals($revActivity['values'][$this->_caseActivityId]['is_deleted'],
4e420887 369 0
6a488035
TO
370 );
371
372 //TODO: check some more things
373 }
374
00be9182 375 public function testCaseActivityUpdateCustom() {
6a488035 376 // Create a case first
4e420887 377 $result = $this->callAPISuccess('case', 'create', $this->_params);
6a488035
TO
378
379 // Create custom field group
380 // Note the second parameter is Activity on purpose, not Case.
381 $custom_ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, 'ActivityTest.php');
382
383 // create activity
384 $params = array(
4e420887 385 'case_id' => $result['id'],
6a488035
TO
386 // follow up
387 'activity_type_id' => 14,
388 'subject' => 'Test followup',
389 'source_contact_id' => $this->_loggedInUser,
390 'target_contact_id' => $this->_params['contact_id'],
391 'custom_' . $custom_ids['custom_field_id'] => "custom string",
6a488035 392 );
4e420887 393 $result = $this->callAPISuccess('activity', 'create', $params);
6a488035
TO
394
395 $aid = $result['values'][$result['id']]['id'];
396
397 // Update activity
398 $params = array(
399 'activity_id' => $aid,
400 'case_id' => 1,
401 'activity_type_id' => 14,
402 'source_contact_id' => $this->_loggedInUser,
403 'subject' => 'New subject',
6a488035 404 );
9e959479 405 $this->callAPISuccess('activity', 'create', $params);
6a488035 406
9e959479 407 // Retrieve revision and check custom fields got copied.
6a488035
TO
408 $revParams = array(
409 'activity_id' => $aid + 1,
6a488035
TO
410 'return.custom_' . $custom_ids['custom_field_id'] => 1,
411 );
4e420887 412 $revAct = $this->callAPISuccess('activity', 'get', $revParams);
6a488035 413
6a488035 414 $this->assertEquals($revAct['values'][$aid + 1]['custom_' . $custom_ids['custom_field_id']], "custom string",
9e959479 415 "Error message: " . CRM_Utils_Array::value('error_message', $revAct));
6a488035
TO
416
417 $this->customFieldDelete($custom_ids['custom_field_id']);
418 $this->customGroupDelete($custom_ids['custom_group_id']);
419 }
96025800 420
37606d10
SB
421 public function testCaseGetByStatus() {
422 // Create 2 cases with different status ids.
423 $case1 = $this->callAPISuccess('Case', 'create', array(
424 'contact_id' => 17,
425 'subject' => "Test case 1",
426 'case_type_id' => $this->caseTypeId,
427 'status_id' => "Open",
428 'sequential' => 1,
429 ));
430 $this->callAPISuccess('Case', 'create', array(
431 'contact_id' => 17,
432 'subject' => "Test case 2",
433 'case_type_id' => $this->caseTypeId,
434 'status_id' => "Urgent",
435 'sequential' => 1,
436 ));
437 $result = $this->callAPISuccessGetSingle('Case', array(
438 'sequential' => 1,
439 'contact_id' => 17,
440 'status_id' => "Open",
441 ));
442 $this->assertEquals($case1['id'], $result['id']);
443 }
444
0ab56350
CW
445 public function testCaseGetWithRoles() {
446 $case1 = $this->callAPISuccess('Case', 'create', array(
447 'contact_id' => 17,
448 'subject' => "Test case with roles",
449 'case_type_id' => $this->caseTypeId,
450 'status_id' => "Open",
451 ));
452 $result = $this->callAPISuccessGetSingle('Case', array(
453 'id' => $case1['id'],
454 'status_id' => "Open",
455 'return' => array('contacts'),
456 ));
457 foreach ($result['contacts'] as $contact) {
458 if ($contact['role'] == 'Client') {
459 $this->assertEquals(17, $contact['contact_id']);
460 }
461 elseif ($contact['role'] == 'Homeless Services Coordinator') {
462 $this->assertEquals(1, $contact['creator']);
463 $this->assertEquals(1, $contact['manager']);
464 }
465 }
466 }
467
468 public function testCaseGetWithDefinition() {
469 $case1 = $this->callAPISuccess('Case', 'create', array(
470 'contact_id' => 17,
471 'subject' => "Test case with definition",
472 'case_type_id' => $this->caseTypeId,
473 'status_id' => "Open",
474 ));
475 $result1 = $this->callAPISuccessGetSingle('Case', array(
476 'id' => $case1['id'],
477 'status_id' => "Open",
478 'return' => array('case_type_id.definition'),
479 ));
480 $result2 = $this->callAPISuccessGetSingle('Case', array(
481 'id' => $case1['id'],
482 'status_id' => "Open",
483 'return' => array('case_type_id', 'case_type_id.definition'),
484 ));
485 $this->assertEquals($result1['case_type_id.definition'], $result2['case_type_id.definition']);
486 $def = $result1['case_type_id.definition'];
487 $this->assertEquals(array('name' => 'Open Case', 'max_instances' => 1), $def['activityTypes'][0]);
488 $this->assertNotEmpty($def['activitySets'][0]['activityTypes']);
489 $this->assertNotEmpty($def['caseRoles'][0]['manager']);
490 $this->assertNotEmpty($def['caseRoles'][0]['creator']);
491 }
492
9ef16723
CW
493 public function testCaseGetTags() {
494 $case1 = $this->callAPISuccess('Case', 'create', array(
495 'contact_id' => 17,
496 'subject' => "Test case with tags",
497 'case_type_id' => $this->caseTypeId,
498 'status_id' => "Open",
499 ));
500 $tag1 = $this->tagCreate(array(
501 'name' => 'CaseTag1',
502 'used_for' => 'civicrm_case',
503 ));
504 $tag2 = $this->tagCreate(array(
505 'name' => 'CaseTag2',
506 'used_for' => 'civicrm_case',
507 ));
508 $this->callAPISuccess('EntityTag', 'create', array(
509 'entity_table' => 'civicrm_case',
510 'entity_id' => $case1['id'],
511 'tag_id' => $tag1['id'],
512 ));
513 $this->callAPIFailure('Case', 'getsingle', array(
514 'tag_id' => $tag2['id'],
515 ));
516 $result = $this->callAPISuccessGetSingle('Case', array(
517 'tag_id' => $tag1['id'],
518 'return' => 'tag_id.name',
519 ));
520 $this->assertEquals('CaseTag1', $result['tag_id'][$tag1['id']]['tag_id.name']);
521 }
522
10befc1f
CW
523 /**
524 * Test that a chained api call can use the operator syntax.
525 *
526 * E.g. array('IN' => $value.contact_id)
527 *
528 * @throws \Exception
529 */
530 public function testCaseGetChainedOp() {
531 $contact1 = $this->individualCreate(array(), 1);
532 $contact2 = $this->individualCreate(array(), 2);
533 $case1 = $this->callAPISuccess('Case', 'create', array(
534 'contact_id' => $contact1,
535 'subject' => "Test case 1",
536 'case_type_id' => $this->caseTypeId,
537 ));
538 $case2 = $this->callAPISuccess('Case', 'create', array(
539 'contact_id' => $contact2,
540 'subject' => "Test case 2",
541 'case_type_id' => $this->caseTypeId,
542 ));
543 $case3 = $this->callAPISuccess('Case', 'create', array(
544 'contact_id' => array($contact1, $contact2),
545 'subject' => "Test case 3",
546 'case_type_id' => $this->caseTypeId,
547 ));
548
549 // Fetch case 1 and all cases with the same client. Chained get should return case 3.
550 $result = $this->callAPISuccessGetSingle('Case', array(
551 'id' => $case1['id'],
552 'return' => 'contact_id',
553 'api.Case.get' => array(
554 'contact_id' => array('IN' => "\$value.contact_id"),
555 'id' => array('!=' => "\$value.id"),
556 ),
557 ));
558 $this->assertEquals($case3['id'], $result['api.Case.get']['id']);
559
560 // Fetch case 3 and all cases with the same clients. Chained get should return case 1&2.
561 $result = $this->callAPISuccessGetSingle('Case', array(
562 'id' => $case3['id'],
563 'return' => array('contact_id'),
564 'api.Case.get' => array(
565 'return' => 'id',
566 'contact_id' => array('IN' => "\$value.contact_id"),
567 'id' => array('!=' => "\$value.id"),
568 ),
569 ));
570 $this->assertEquals(array($case1['id'], $case2['id']), array_keys(CRM_Utils_Array::rekey($result['api.Case.get']['values'], 'id')));
571 }
572
242220e2
CW
573 /**
574 * Test the ability to order by client using the join syntax.
575 *
576 * For multi-client cases, should order by the first client.
577 */
578 public function testCaseGetOrderByClient() {
579 $contact1 = $this->individualCreate(array('first_name' => 'Aa', 'last_name' => 'Zz'));
4c6cc364 580 $contact2 = $this->individualCreate(array('first_name' => 'Bb', 'last_name' => 'Zz'));
242220e2
CW
581 $contact3 = $this->individualCreate(array('first_name' => 'Cc', 'last_name' => 'Xx'));
582
583 $case1 = $this->callAPISuccess('Case', 'create', array(
584 'contact_id' => $contact1,
585 'subject' => "Test case 1",
586 'case_type_id' => $this->caseTypeId,
587 ));
588 $case2 = $this->callAPISuccess('Case', 'create', array(
589 'contact_id' => $contact2,
590 'subject' => "Test case 2",
591 'case_type_id' => $this->caseTypeId,
592 ));
593 $case3 = $this->callAPISuccess('Case', 'create', array(
594 'contact_id' => array($contact3, $contact1),
595 'subject' => "Test case 3",
596 'case_type_id' => $this->caseTypeId,
597 ));
598
599 $result = $this->callAPISuccess('Case', 'get', array(
600 'contact_id' => array('IN' => array($contact1, $contact2, $contact3)),
601 'sequential' => 1,
602 'return' => 'id',
603 'options' => array('sort' => 'contact_id.first_name'),
604 ));
605 $this->assertEquals($case3['id'], $result['values'][2]['id']);
606 $this->assertEquals($case2['id'], $result['values'][1]['id']);
607 $this->assertEquals($case1['id'], $result['values'][0]['id']);
608
609 $result = $this->callAPISuccess('Case', 'get', array(
610 'contact_id' => array('IN' => array($contact1, $contact2, $contact3)),
611 'sequential' => 1,
612 'return' => 'id',
4c6cc364 613 'options' => array('sort' => 'contact_id.last_name ASC, contact_id.first_name DESC'),
242220e2
CW
614 ));
615 $this->assertEquals($case1['id'], $result['values'][2]['id']);
616 $this->assertEquals($case2['id'], $result['values'][1]['id']);
617 $this->assertEquals($case3['id'], $result['values'][0]['id']);
618
619 $result = $this->callAPISuccess('Case', 'get', array(
620 'contact_id' => array('IN' => array($contact1, $contact2, $contact3)),
621 'sequential' => 1,
622 'return' => 'id',
623 'options' => array('sort' => 'contact_id.first_name DESC'),
624 ));
625 $this->assertEquals($case1['id'], $result['values'][2]['id']);
626 $this->assertEquals($case2['id'], $result['values'][1]['id']);
627 $this->assertEquals($case3['id'], $result['values'][0]['id']);
628
629 $result = $this->callAPISuccess('Case', 'get', array(
630 'contact_id' => array('IN' => array($contact1, $contact2, $contact3)),
631 'sequential' => 1,
632 'return' => 'id',
4c6cc364 633 'options' => array('sort' => 'case_type_id, contact_id DESC, status_id'),
242220e2
CW
634 ));
635 $this->assertEquals($case1['id'], $result['values'][2]['id']);
636 $this->assertEquals($case2['id'], $result['values'][1]['id']);
637 $this->assertEquals($case3['id'], $result['values'][0]['id']);
638 $this->assertCount(3, $result['values']);
639 }
640
ae76ce5e
CW
641 /**
642 * Test the ability to add a timeline to an existing case.
643 *
644 * See the case.addtimeline api.
645 *
646 * @throws \Exception
647 */
648 public function testCaseAddtimeline() {
649 $caseSpec = array(
650 'title' => 'Application with Definition',
651 'name' => 'Application_with_Definition',
652 'is_active' => 1,
653 'weight' => 4,
654 'definition' => array(
655 'activityTypes' => array(
656 array('name' => 'Follow up'),
657 ),
658 'activitySets' => array(
659 array(
660 'name' => 'set1',
661 'label' => 'Label 1',
662 'timeline' => 1,
663 'activityTypes' => array(
664 array('name' => 'Open Case', 'status' => 'Completed'),
665 ),
666 ),
667 array(
668 'name' => 'set2',
669 'label' => 'Label 2',
670 'timeline' => 1,
671 'activityTypes' => array(
672 array('name' => 'Follow up'),
673 ),
674 ),
675 ),
676 'caseRoles' => array(
677 array('name' => 'Homeless Services Coordinator', 'creator' => 1, 'manager' => 1),
678 ),
679 ),
680 );
681 $cid = $this->individualCreate();
682 $caseType = $this->callAPISuccess('CaseType', 'create', $caseSpec);
683 $case = $this->callAPISuccess('Case', 'create', array(
684 'case_type_id' => $caseType['id'],
685 'contact_id' => $cid,
686 'subject' => 'Test case with timeline',
687 ));
688 // Created case should only have 1 activity per the spec
689 $result = $this->callAPISuccessGetSingle('Activity', array('case_id' => $case['id'], 'return' => 'activity_type_id.name'));
690 $this->assertEquals('Open Case', $result['activity_type_id.name']);
691 // Add timeline
692 $timeline = civicrm_api('Case', 'addtimeline', array(
693 'case_id' => $case['id'],
694 'timeline' => 'set2',
695 'version' => 3,
696 ));
697 $result = $this->callAPISuccess('Activity', 'get', array(
698 'case_id' => $case['id'],
699 'return' => 'activity_type_id.name',
700 'sequential' => 1,
701 'options' => array('sort' => 'id'),
702 ));
703 $this->assertEquals(2, $result['count']);
704 $this->assertEquals('Follow up', $result['values'][1]['activity_type_id.name']);
705 }
706
a6bc7218
CW
707
708 /**
709 * Test the case merge function.
710 *
711 * 2 cases should be mergeable into 1
712 *
713 * @throws \Exception
714 */
715 public function testCaseMerge() {
716 $contact1 = $this->individualCreate(array(), 1);
717 $case1 = $this->callAPISuccess('Case', 'create', array(
718 'contact_id' => $contact1,
719 'subject' => "Test case 1",
720 'case_type_id' => $this->caseTypeId,
721 ));
722 $case2 = $this->callAPISuccess('Case', 'create', array(
723 'contact_id' => $contact1,
724 'subject' => "Test case 2",
725 'case_type_id' => $this->caseTypeId,
726 ));
727 $result = $this->callAPISuccess('Case', 'getcount', array('contact_id' => $contact1));
728 $this->assertEquals(2, $result);
729
730 $this->callAPISuccess('Case', 'merge', array('case_id_1' => $case1['id'], 'case_id_2' => $case2['id']));
731
732 $result = $this->callAPISuccess('Case', 'getsingle', array('id' => $case2['id']));
733 $this->assertEquals(1, $result['is_deleted']);
734 }
735
6a488035 736}