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
35 require_once 'CiviTest/CiviCaseTestCase.php';
38 * Test APIv3 civicrm_case_* functions
40 * @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 * Test setup for every test.
57 * Connect to the database, truncate the tables that will be used
58 * and redirect stdin to a temporary file.
60 public function setUp() {
61 $this->_entity
= 'case';
65 $activityTypes = $this->callAPISuccess('option_value', 'get', array(
66 'option_group_id' => 2,
67 'name' => 'Follow Up',
68 'label' => 'Follow Up',
71 $this->followup_activity_type_value
= $activityTypes['values'][0]['value'];
73 $this->_params
= array(
74 'case_type_id' => $this->caseTypeId
,
75 'subject' => 'Test case',
81 * Check with empty array.
83 public function testCaseCreateEmpty() {
84 $this->callAPIFailure('case', 'create', array());
88 * Check if required fields are not passed.
90 public function testCaseCreateWithoutRequired() {
92 'subject' => 'this case should fail',
96 $this->callAPIFailure('case', 'create', $params);
100 * Test create function with valid parameters.
102 public function testCaseCreate() {
103 $params = $this->_params
;
104 // Test using label instead of value.
105 unset($params['case_type_id']);
106 $params['case_type'] = $this->caseType
;
107 $result = $this->callAPIAndDocument('case', 'create', $params, __FUNCTION__
, __FILE__
);
111 $result = $this->callAPISuccess('case', 'get', array('id' => $id));
112 $this->assertEquals($result['values'][$id]['id'], $id);
113 $this->assertEquals($result['values'][$id]['case_type_id'], $this->caseTypeId
);
114 $this->assertEquals($result['values'][$id]['subject'], $params['subject']);
118 * Test update (create with id) function with valid parameters.
120 public function testCaseUpdate() {
121 $params = $this->_params
;
122 // Test using name instead of value
123 unset($params['case_type_id']);
124 $params['case_type'] = $this->caseType
;
125 $result = $this->callAPISuccess('case', 'create', $params);
127 $case = $this->callAPISuccess('case', 'getsingle', array('id' => $id));
130 $params = array('id' => $id);
131 $params['subject'] = $case['subject'] = $case['case_subject'] = 'Something Else';
132 $this->callAPISuccess('case', 'create', $params);
134 // Verify that updated case is exactly equal to the original with new subject.
135 $result = $this->callAPISuccess('case', 'getsingle', array('case_id' => $id));
136 $this->assertAPIArrayComparison($result, $case);
140 * Test delete function with valid parameters.
142 public function testCaseDelete() {
144 $result = $this->callAPISuccess('case', 'create', $this->_params
);
146 // Move Case to Trash
148 $result = $this->callAPISuccess('case', 'delete', array('id' => $id, 'move_to_trash' => 1));
150 // Check result - also check that 'case_id' works as well as 'id'
151 $result = $this->callAPISuccess('case', 'get', array('case_id' => $id));
152 $this->assertEquals(1, $result['values'][$id]['is_deleted']);
154 // Delete Case Permanently - also check that 'case_id' works as well as 'id'
155 $result = $this->callAPISuccess('case', 'delete', array('case_id' => $id));
157 // Check result - case should no longer exist
158 $result = $this->callAPISuccess('case', 'get', array('id' => $id));
159 $this->assertEquals(0, $result['count']);
163 * Test get function based on activity.
165 public function testCaseGetByActivity() {
167 $result = $this->callAPISuccess('case', 'create', $this->_params
);
170 // Check result - we should get a list of activity ids
171 $result = $this->callAPISuccess('case', 'get', array('id' => $id, 'return' => 'activities'));
172 $case = $result['values'][$id];
173 $activity = $case['activities'][0];
175 // Fetch case based on an activity id
176 $result = $this->callAPISuccess('case', 'get', array(
177 'activity_id' => $activity,
178 'return' => 'activities',
180 $this->assertEquals(FALSE, empty($result['values'][$id]));
181 $this->assertEquals($result['values'][$id], $case);
185 * Test get function based on contact id.
187 public function testCaseGetByContact() {
189 $result = $this->callAPISuccess('case', 'create', $this->_params
);
192 // Store result for later
193 $case = $this->callAPISuccess('case', 'getsingle', array('id' => $id, 'return' => array('activities', 'contacts')));
195 // Fetch case based on client contact id
196 $result = $this->callAPISuccess('case', 'get', array(
197 'client_id' => $this->_params
['contact_id'],
198 'return' => array('activities', 'contacts'),
200 $this->assertAPIArrayComparison($result['values'][$id], $case);
204 * Test get function based on subject.
206 public function testCaseGetBySubject() {
208 $result = $this->callAPISuccess('case', 'create', $this->_params
);
211 // Store result for later
212 $case = $this->callAPISuccess('case', 'getsingle', array('id' => $id, 'return' => 'subject'));
214 // Fetch case based on client contact id
215 $result = $this->callAPISuccess('case', 'get', array(
216 'subject' => $this->_params
['subject'],
217 'return' => array('subject'),
219 $this->assertAPIArrayComparison($result['values'][$id], $case);
223 * Test get function based on wrong subject.
225 public function testCaseGetByWrongSubject() {
226 $result = $this->callAPISuccess('case', 'create', $this->_params
);
228 // Append 'wrong' to subject so that it is no longer the same.
229 $result = $this->callAPISuccess('case', 'get', array(
230 'subject' => $this->_params
['subject'] . 'wrong',
231 'return' => array('activities', 'contacts'),
233 $this->assertEquals(0, $result['count']);
237 * Test get function with no criteria.
239 public function testCaseGetNoCriteria() {
240 $result = $this->callAPISuccess('case', 'create', $this->_params
);
243 // Store result for later
244 $case = $this->callAPISuccess('case', 'getsingle', array('id' => $id, 'return' => 'contact_id'));
246 $result = $this->callAPISuccess('case', 'get', array('limit' => 0, 'return' => array('contact_id')));
247 $this->assertAPIArrayComparison($result['values'][$id], $case);
251 * Test activity api create for case activities.
253 public function testCaseActivityCreate() {
254 $params = $this->_params
;
255 $this->callAPISuccess('case', 'create', $params);
259 'activity_type_id' => $this->followup_activity_type_value
,
260 'subject' => 'Test followup',
261 'source_contact_id' => $this->_loggedInUser
,
262 'target_contact_id' => $this->_params
['contact_id'],
264 $result = $this->callAPISuccess('activity', 'create', $params);
265 $this->assertEquals($result['values'][$result['id']]['activity_type_id'], $params['activity_type_id']);
267 // might need this for other tests that piggyback on this one
268 $this->_caseActivityId
= $result['values'][$result['id']]['id'];
270 // Check other DB tables populated properly - is there a better way to do this? assertDBState() requires that we know the id already.
271 $dao = new CRM_Case_DAO_CaseActivity();
273 $dao->activity_id
= $this->_caseActivityId
;
274 $this->assertEquals($dao->find(), 1, 'case_activity table not populated correctly in line ' . __LINE__
);
277 $dao = new CRM_Activity_DAO_ActivityContact();
278 $dao->activity_id
= $this->_caseActivityId
;
279 $dao->contact_id
= $this->_params
['contact_id'];
280 $dao->record_type_id
= 3;
281 $this->assertEquals($dao->find(), 1, 'activity_contact table not populated correctly in line ' . __LINE__
);
284 // TODO: There's more things we could check
288 * Test activity api update for case activities.
290 public function testCaseActivityUpdate() {
291 // Need to create the case and activity before we can update it
292 $this->testCaseActivityCreate();
295 'activity_id' => $this->_caseActivityId
,
297 'activity_type_id' => 14,
298 'source_contact_id' => $this->_loggedInUser
,
299 'subject' => 'New subject',
301 $result = $this->callAPISuccess('activity', 'create', $params);
303 $this->assertEquals($result['values'][$result['id']]['subject'], $params['subject']);
305 // id should be one greater, since this is a new revision
306 $this->assertEquals($result['values'][$result['id']]['id'],
307 $this->_caseActivityId +
1,
308 'in line ' . __LINE__
310 $this->assertEquals($result['values'][$result['id']]['original_id'],
311 $this->_caseActivityId
,
312 'in line ' . __LINE__
315 // Check revision is as expected
317 'activity_id' => $this->_caseActivityId
,
319 $revActivity = $this->callAPISuccess('activity', 'get', $revParams);
320 $this->assertEquals($revActivity['values'][$this->_caseActivityId
]['is_current_revision'],
322 $this->assertEquals($revActivity['values'][$this->_caseActivityId
]['is_deleted'],
326 //TODO: check some more things
329 public function testCaseActivityUpdateCustom() {
330 // Create a case first
331 $result = $this->callAPISuccess('case', 'create', $this->_params
);
333 // Create custom field group
334 // Note the second parameter is Activity on purpose, not Case.
335 $custom_ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__
, 'ActivityTest.php');
339 'case_id' => $result['id'],
341 'activity_type_id' => 14,
342 'subject' => 'Test followup',
343 'source_contact_id' => $this->_loggedInUser
,
344 'target_contact_id' => $this->_params
['contact_id'],
345 'custom_' . $custom_ids['custom_field_id'] => "custom string",
347 $result = $this->callAPISuccess('activity', 'create', $params);
349 $aid = $result['values'][$result['id']]['id'];
353 'activity_id' => $aid,
355 'activity_type_id' => 14,
356 'source_contact_id' => $this->_loggedInUser
,
357 'subject' => 'New subject',
359 $this->callAPISuccess('activity', 'create', $params);
361 // Retrieve revision and check custom fields got copied.
363 'activity_id' => $aid +
1,
364 'return.custom_' . $custom_ids['custom_field_id'] => 1,
366 $revAct = $this->callAPISuccess('activity', 'get', $revParams);
368 $this->assertEquals($revAct['values'][$aid +
1]['custom_' . $custom_ids['custom_field_id']], "custom string",
369 "Error message: " . CRM_Utils_Array
::value('error_message', $revAct));
371 $this->customFieldDelete($custom_ids['custom_field_id']);
372 $this->customGroupDelete($custom_ids['custom_group_id']);
375 public function testCaseGetByStatus() {
376 // Create 2 cases with different status ids.
377 $case1 = $this->callAPISuccess('Case', 'create', array(
379 'subject' => "Test case 1",
380 'case_type_id' => $this->caseTypeId
,
381 'status_id' => "Open",
384 $this->callAPISuccess('Case', 'create', array(
386 'subject' => "Test case 2",
387 'case_type_id' => $this->caseTypeId
,
388 'status_id' => "Urgent",
391 $result = $this->callAPISuccessGetSingle('Case', array(
394 'status_id' => "Open",
396 $this->assertEquals($case1['id'], $result['id']);