3 * File for the TestCase class
7 * @author Walt Haas <walt@dharmatech.org> (801) 534-1262
8 * @copyright Copyright CiviCRM LLC (C) 2009
9 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html
10 * GNU Affero General Public License version 3
11 * @version $Id: ActivityTest.php 31254 2010-12-15 10:09:29Z eileen $
14 * This file is part of CiviCRM
16 * CiviCRM is free software; you can redistribute it and/or
17 * modify it under the terms of the GNU Affero General Public License
18 * as published by the Free Software Foundation; either version 3 of
19 * the License, or (at your option) any later version.
21 * CiviCRM is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU Affero General Public License for more details.
26 * You should have received a copy of the GNU Affero General Public
27 * License along with this program. If not, see
28 * <http://www.gnu.org/licenses/>.
32 * Include class definitions
34 require_once 'CiviTest/CiviUnitTestCase.php';
37 * Test APIv3 civicrm_case_* functions
39 * @package CiviCRM_APIv3
41 class api_v3_CaseTest
extends CiviUnitTestCase
{
44 protected $_apiversion =3;
45 protected $followup_activity_type_value;
46 protected $caseTypeId;
47 protected $caseStatusGroup;
48 protected $caseTypeGroup;
49 protected $optionValues;
52 * Test setup for every test
54 * Connect to the database, truncate the tables that will be used
55 * and redirect stdin to a temporary file
57 public function setUp() {
58 $this->_entity
= 'case';
61 // CRM-9404 - set-up is a bit cumbersome but had to put something in place to set up activity types & case types
62 //. Using XML was causing breakage as id numbers were changing over time
63 // & was really hard to troubleshoot as involved truncating option_value table to mitigate this & not leaving DB in a
64 // state where tests could run afterwards without re-loading.
65 $this->caseStatusGroup
= $this->callAPISuccess('option_group', 'get', array(
66 'name' => 'case_status',
67 'format.only_id' => 1)
69 $this->caseTypeGroup
= $this->callAPISuccess('option_group', 'get', array(
70 'name' => 'case_type',
71 'format.only_id' => 1)
73 $caseTypes = $this->callAPISuccess('option_value', 'Create', array(
74 'option_group_id' => $this->caseTypeGroup
,
75 'name' => 'housing_support',
76 'label' => "Housing Support",
78 'description' => 'Help homeless individuals obtain temporary and long-term housing',
81 $this->caseTypeId
= $caseTypes['values'][0]['value'];
82 $this->optionValues
[] = $caseTypes['id'];
83 $optionValues = array(
84 'Medical evaluation' => 'Medical evaluation',
85 'Mental health evaluation' => "Mental health evaluation",
86 'Secure temporary housing' => 'Secure temporary housing',
87 'Long-term housing plan' => 'Long-term housing plan',
88 'ADC referral' => 'ADC referral',
89 'Income and benefits stabilization' => 'Income and benefits stabilization',
91 foreach ($optionValues as $name => $label) {
92 $activityTypes = $this->callAPISuccess('option_value', 'Create', array(
93 'option_group_id' => 2,
99 $this->optionValues
[] = $activityTypes['id'];
101 $tablesToTruncate = array(
104 'civicrm_custom_group',
105 'civicrm_custom_field',
107 'civicrm_case_contact',
108 'civicrm_case_activity',
109 'civicrm_activity_contact',
110 'civicrm_relationship',
111 'civicrm_relationship_type',
114 $this->quickCleanup($tablesToTruncate);
116 $this->loadAllFixtures();
118 $activityTypes = $this->callAPISuccess('option_value', 'get', array(
119 'option_group_id' => 2,
120 'name' => 'Follow Up',
121 'label' => 'Follow Up',
124 $this->followup_activity_type_value
= $activityTypes['values'][0]['value'];
126 // enable the default custom templates for the case type xml files
127 $this->customDirectories(array('template_path' => TRUE));
129 // case is not enabled by default
130 $enableResult = CRM_Core_BAO_ConfigSetting
::enableComponent('CiviCase');
131 $this->assertTrue($enableResult, 'Cannot enable CiviCase in line ' . __LINE__
);
133 $this->_params
= array(
134 'case_type_id' => $this->caseTypeId
,
135 'subject' => 'Test case',
139 // create a logged in USER since the code references it for source_contact_id
140 $this->createLoggedInUser();
141 $session = CRM_Core_Session
::singleton();
142 $this->_loggedInUser
= $session->get('userID');
143 /// note that activityType options are cached by the FULL set of options you pass in
144 // ie. because Activity api includes campaign in it's call cache is not flushed unless
145 // included in this call. Also note flush function doesn't work on this property as it sets to null not empty array
146 CRM_Core_PseudoConstant
::activityType(TRUE, TRUE, TRUE, 'name', TRUE);
150 * Tears down the fixture, for example, closes a network connection.
151 * This method is called after a test is executed.
155 function tearDown() {
156 foreach ($this->optionValues
as $id) {
157 $this->callAPISuccess('option_value', 'delete', array('id' => $id));
159 $tablesToTruncate = array(
163 'civicrm_case_contact',
164 'civicrm_case_activity',
165 'civicrm_activity_contact',
166 'civicrm_relationship',
167 'civicrm_relationship_type',
169 $this->quickCleanup($tablesToTruncate, TRUE);
171 $this->customDirectories(array('template_path' => FALSE));
175 * check with empty array
177 function testCaseCreateEmpty() {
178 $result = $this->callAPIFailure('case', 'create', array());
182 * check if required fields are not passed
184 function testCaseCreateWithoutRequired() {
186 'subject' => 'this case should fail',
190 $result = $this->callAPIFailure('case', 'create', $params);
194 * Test create function with valid parameters
196 function testCaseCreate() {
198 $params = $this->_params
;
199 // Test using label instead of value
200 unset($params['case_type_id']);
201 $params['case_type'] = 'Housing Support';
202 $result = $this->callAPISuccess('case', 'create', $params);
206 $result = $this->callAPISuccess('case', 'get', array('id' => $id));
207 $this->assertEquals($result['values'][$id]['id'], 1, 'in line ' . __LINE__
);
208 $this->assertEquals($result['values'][$id]['case_type_id'], $this->caseTypeId
, 'in line ' . __LINE__
);
209 $this->assertEquals($result['values'][$id]['subject'], $params['subject'], 'in line ' . __LINE__
);
213 * Test update (create with id) function with valid parameters
215 function testCaseUpdate() {
217 $params = $this->_params
;
218 // Test using name instead of value
219 $params['case_type_id'] = 'housing_support';
220 $result = $this->callAPISuccess('case', 'create', $params);
222 $result = $this->callAPISuccess('case', 'get', array('id' => $id));
223 $case = $result['values'][$id];
226 $params = array('id' => $id);
227 $params['subject'] = $case['subject'] = 'Something Else';
228 $result = $this->callAPISuccess('case', 'create', $params);
230 // Verify that updated case is exactly equal to the original with new subject
231 $result = $this->callAPISuccess('case', 'get', array('case_id' => $id));
232 $this->assertEquals($result['values'][$id], $case, 'in line ' . __LINE__
);
236 * Test delete function with valid parameters
238 function testCaseDelete() {
240 $result = $this->callAPISuccess('case', 'create', $this->_params
);
242 // Move Case to Trash
244 $result = $this->callAPISuccess('case', 'delete', array('id' => $id, 'move_to_trash' => 1));
246 // Check result - also check that 'case_id' works as well as 'id'
247 $result = $this->callAPISuccess('case', 'get', array('case_id' => $id));
248 $this->assertEquals(1, $result['values'][$id]['is_deleted'], 'in line ' . __LINE__
);
250 // Delete Case Permanently - also check that 'case_id' works as well as 'id'
251 $result = $this->callAPISuccess('case', 'delete', array('case_id' => $id));
253 // Check result - case should no longer exist
254 $result = $this->callAPISuccess('case', 'get', array('id' => $id));
255 $this->assertEquals(0, $result['count']);
259 * Test get function based on activity
261 function testCaseGetByActivity() {
263 $result = $this->callAPISuccess('case', 'create', $this->_params
);
266 // Check result - we should get a list of activity ids
267 $result = $this->callAPISuccess('case', 'get', array('id' => $id));
268 $case = $result['values'][$id];
269 $activity = $case['activities'][0];
271 // Fetch case based on an activity id
272 $result = $this->callAPISuccess('case', 'get', array('activity_id' => $activity, 'return' => 'activities,contacts'));
273 $this->assertEquals(FALSE, empty($result['values'][$id]), 'in line ' . __LINE__
);
274 $this->assertEquals($result['values'][$id], $case, 'in line ' . __LINE__
);
278 * Test get function based on contact id
280 function testCaseGetByContact() {
282 $result = $this->callAPISuccess('case', 'create', $this->_params
);
285 // Store result for later
286 $case = $this->callAPISuccess('case', 'getsingle', array('id' => $id));
288 // Fetch case based on client contact id
289 $result = $this->callAPISuccess('case', 'get', array('client_id' => $this->_params
['contact_id'], 'return' => array('activities', 'contacts')));
290 $this->assertAPIArrayComparison($result['values'][$id], $case);
294 * Test get function based on subject
296 function testCaseGetBySubject() {
298 $result = $this->callAPISuccess('case', 'create', $this->_params
);
301 // Store result for later
302 $case = $this->callAPISuccess('case', 'getsingle', array('id' => $id));
304 // Fetch case based on client contact id
305 $result = $this->callAPISuccess('case', 'get', array('subject' => $this->_params
['subject'], 'return' => array('activities', 'contacts')));
306 $this->assertAPIArrayComparison($result['values'][$id], $case);
310 * Test get function based on wrong subject
312 function testCaseGetByWrongSubject() {
314 $result = $this->callAPISuccess('case', 'create', $this->_params
);
317 // Append 'wrong' to subject so that it is no longer the same
318 $result = $this->callAPISuccess('case', 'get', array('subject' => $this->_params
['subject'] . 'wrong', 'return' => array('activities', 'contacts')));
319 $this->assertEquals(0, $result['count'], 'in line ' . __LINE__
);
323 * Test get function with no criteria
325 function testCaseGetNoCriteria() {
327 $result = $this->callAPISuccess('case', 'create', $this->_params
);
330 // Store result for later
331 $case = $this->callAPISuccess('case', 'getsingle', array('id' => $id));
333 $result = $this->callAPISuccess('case', 'get', array('return' => array('activities', 'contacts')));
334 $this->assertAPIArrayComparison($result['values'][$id], $case);
338 * Test activity api create for case activities
340 function testCaseActivityCreate() {
341 // Create a case first
342 $params = $this->_params
;
343 $result = $this->callAPISuccess('case', 'create', $params);
347 'activity_type_id' => $this->followup_activity_type_value
,
348 'subject' => 'Test followup',
349 'source_contact_id' => $this->_loggedInUser
,
350 'target_contact_id' => $this->_params
['contact_id'],
352 $result = $this->callAPISuccess('activity', 'create', $params);
353 $this->assertEquals($result['values'][$result['id']]['activity_type_id'], $params['activity_type_id'], 'in line ' . __LINE__
);
355 // might need this for other tests that piggyback on this one
356 $this->_caseActivityId
= $result['values'][$result['id']]['id'];
358 // Check other DB tables populated properly - is there a better way to do this? assertDBState() requires that we know the id already.
359 $dao = new CRM_Case_DAO_CaseActivity();
361 $dao->activity_id
= $this->_caseActivityId
;
362 $this->assertEquals($dao->find(), 1, 'case_activity table not populated correctly in line ' . __LINE__
);
365 $dao = new CRM_Activity_DAO_ActivityContact();
366 $dao->activity_id
= $this->_caseActivityId
;
367 $dao->contact_id
= $this->_params
['contact_id'];
368 $dao->record_type_id
= 3;
369 $this->assertEquals($dao->find(), 1, 'activity_contact table not populated correctly in line ' . __LINE__
);
372 // TODO: There's more things we could check
376 * Test activity api update for case activities
378 function testCaseActivityUpdate() {
379 // Need to create the case and activity before we can update it
380 $this->testCaseActivityCreate();
383 'activity_id' => $this->_caseActivityId
,
385 'activity_type_id' => 14,
386 'source_contact_id' => $this->_loggedInUser
,
387 'subject' => 'New subject',
389 $result = $this->callAPISuccess('activity', 'create', $params);
391 $this->assertEquals($result['values'][$result['id']]['subject'], $params['subject'], 'in line ' . __LINE__
);
393 // id should be one greater, since this is a new revision
394 $this->assertEquals($result['values'][$result['id']]['id'],
395 $this->_caseActivityId +
1,
396 'in line ' . __LINE__
398 $this->assertEquals($result['values'][$result['id']]['original_id'],
399 $this->_caseActivityId
,
400 'in line ' . __LINE__
403 // Check revision is as expected
405 'activity_id' => $this->_caseActivityId
,
407 $revActivity = $this->callAPISuccess('activity', 'get', $revParams);
408 $this->assertEquals($revActivity['values'][$this->_caseActivityId
]['is_current_revision'],
410 $this->assertEquals($revActivity['values'][$this->_caseActivityId
]['is_deleted'],
414 //TODO: check some more things
417 function testCaseActivityUpdateCustom() {
418 // Create a case first
419 $result = $this->callAPISuccess('case', 'create', $this->_params
);
421 // Create custom field group
422 // Note the second parameter is Activity on purpose, not Case.
423 $custom_ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__
, 'ActivityTest.php');
427 'case_id' => $result['id'],
429 'activity_type_id' => 14,
430 'subject' => 'Test followup',
431 'source_contact_id' => $this->_loggedInUser
,
432 'target_contact_id' => $this->_params
['contact_id'],
433 'custom_' . $custom_ids['custom_field_id'] => "custom string",
435 $result = $this->callAPISuccess('activity', 'create', $params);
437 $aid = $result['values'][$result['id']]['id'];
441 'activity_id' => $aid,
443 'activity_type_id' => 14,
444 'source_contact_id' => $this->_loggedInUser
,
445 'subject' => 'New subject',
447 $revAct = $this->callAPISuccess('activity', 'create', $params);
449 // Retrieve revision and check custom fields got copied
451 'activity_id' => $aid +
1,
452 'return.custom_' . $custom_ids['custom_field_id'] => 1,
454 $revAct = $this->callAPISuccess('activity', 'get', $revParams);
456 $this->assertEquals($revAct['values'][$aid +
1]['custom_' . $custom_ids['custom_field_id']], "custom string",
457 "Error message: " . CRM_Utils_Array
::value('error_message', $revAct) . ' in line ' . __LINE__
460 $this->customFieldDelete($custom_ids['custom_field_id']);
461 $this->customGroupDelete($custom_ids['custom_group_id']);