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;
49 * Test setup for every test.
51 * Connect to the database, truncate the tables that will be used
52 * and redirect stdin to a temporary file
54 public function setUp() {
55 $this->_entity
= 'case';
59 $activityTypes = $this->callAPISuccess('option_value', 'get', array(
60 'option_group_id' => 2,
61 'name' => 'Follow Up',
62 'label' => 'Follow Up',
65 $this->followup_activity_type_value
= $activityTypes['values'][0]['value'];
67 $this->_params
= array(
68 'case_type_id' => $this->caseTypeId
,
69 'subject' => 'Test case',
75 * Check with empty array.
77 public function testCaseCreateEmpty() {
78 $this->callAPIFailure('case', 'create', array());
82 * Check if required fields are not passed.
84 public function testCaseCreateWithoutRequired() {
86 'subject' => 'this case should fail',
90 $result = $this->callAPIFailure('case', 'create', $params);
94 * Test create function with valid parameters.
96 public function testCaseCreate() {
98 $params = $this->_params
;
99 // Test using label instead of value
100 unset($params['case_type_id']);
101 $params['case_type'] = $this->caseType
;
102 $result = $this->callAPIAndDocument('case', 'create', $params, __FUNCTION__
, __FILE__
);
106 $result = $this->callAPISuccess('case', 'get', array('id' => $id));
107 $this->assertEquals($result['values'][$id]['id'], $id, 'in line ' . __LINE__
);
108 $this->assertEquals($result['values'][$id]['case_type_id'], $this->caseTypeId
, 'in line ' . __LINE__
);
109 $this->assertEquals($result['values'][$id]['subject'], $params['subject'], 'in line ' . __LINE__
);
113 * Test update (create with id) function with valid parameters
115 public function testCaseUpdate() {
117 $params = $this->_params
;
118 // Test using name instead of value
119 unset($params['case_type_id']);
120 $params['case_type'] = $this->caseType
;
121 $result = $this->callAPISuccess('case', 'create', $params);
123 $result = $this->callAPISuccess('case', 'get', array('id' => $id));
124 $case = $result['values'][$id];
127 $params = array('id' => $id);
128 $params['subject'] = $case['subject'] = 'Something Else';
129 $result = $this->callAPISuccess('case', 'create', $params);
131 // Verify that updated case is exactly equal to the original with new subject
132 $result = $this->callAPISuccess('case', 'get', array('case_id' => $id));
133 $this->assertEquals($result['values'][$id], $case, 'in line ' . __LINE__
);
137 * Test delete function with valid parameters.
139 public function testCaseDelete() {
141 $result = $this->callAPISuccess('case', 'create', $this->_params
);
143 // Move Case to Trash
145 $result = $this->callAPISuccess('case', 'delete', array('id' => $id, 'move_to_trash' => 1));
147 // Check result - also check that 'case_id' works as well as 'id'
148 $result = $this->callAPISuccess('case', 'get', array('case_id' => $id));
149 $this->assertEquals(1, $result['values'][$id]['is_deleted'], 'in line ' . __LINE__
);
151 // Delete Case Permanently - also check that 'case_id' works as well as 'id'
152 $result = $this->callAPISuccess('case', 'delete', array('case_id' => $id));
154 // Check result - case should no longer exist
155 $result = $this->callAPISuccess('case', 'get', array('id' => $id));
156 $this->assertEquals(0, $result['count']);
160 * Test get function based on activity.
162 public function testCaseGetByActivity() {
164 $result = $this->callAPISuccess('case', 'create', $this->_params
);
167 // Check result - we should get a list of activity ids
168 $result = $this->callAPISuccess('case', 'get', array('id' => $id));
169 $case = $result['values'][$id];
170 $activity = $case['activities'][0];
172 // Fetch case based on an activity id
173 $result = $this->callAPISuccess('case', 'get', array(
174 'activity_id' => $activity,
175 'return' => 'activities,contacts',
177 $this->assertEquals(FALSE, empty($result['values'][$id]), 'in line ' . __LINE__
);
178 $this->assertEquals($result['values'][$id], $case, 'in line ' . __LINE__
);
182 * Test get function based on contact id.
184 public function testCaseGetByContact() {
186 $result = $this->callAPISuccess('case', 'create', $this->_params
);
189 // Store result for later
190 $case = $this->callAPISuccess('case', 'getsingle', array('id' => $id));
192 // Fetch case based on client contact id
193 $result = $this->callAPISuccess('case', 'get', array(
194 'client_id' => $this->_params
['contact_id'],
195 'return' => array('activities', 'contacts'),
197 $this->assertAPIArrayComparison($result['values'][$id], $case);
201 * Test get function based on subject.
203 public function testCaseGetBySubject() {
205 $result = $this->callAPISuccess('case', 'create', $this->_params
);
208 // Store result for later
209 $case = $this->callAPISuccess('case', 'getsingle', array('id' => $id));
211 // Fetch case based on client contact id
212 $result = $this->callAPISuccess('case', 'get', array(
213 'subject' => $this->_params
['subject'],
214 'return' => array('activities', 'contacts'),
216 $this->assertAPIArrayComparison($result['values'][$id], $case);
220 * Test get function based on wrong subject.
222 public function testCaseGetByWrongSubject() {
224 $result = $this->callAPISuccess('case', 'create', $this->_params
);
227 // Append 'wrong' to subject so that it is no longer the same
228 $result = $this->callAPISuccess('case', 'get', array(
229 'subject' => $this->_params
['subject'] . 'wrong',
230 'return' => array('activities', 'contacts'),
232 $this->assertEquals(0, $result['count'], 'in line ' . __LINE__
);
236 * Test get function with no criteria.
238 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));
246 $result = $this->callAPISuccess('case', 'get', array('return' => array('activities', 'contacts')));
247 $this->assertAPIArrayComparison($result['values'][$id], $case);
251 * Test activity api create for case activities.
253 public function testCaseActivityCreate() {
254 // Create a case first
255 $params = $this->_params
;
256 $result = $this->callAPISuccess('case', 'create', $params);
260 'activity_type_id' => $this->followup_activity_type_value
,
261 'subject' => 'Test followup',
262 'source_contact_id' => $this->_loggedInUser
,
263 'target_contact_id' => $this->_params
['contact_id'],
265 $result = $this->callAPISuccess('activity', 'create', $params);
266 $this->assertEquals($result['values'][$result['id']]['activity_type_id'], $params['activity_type_id'], 'in line ' . __LINE__
);
268 // might need this for other tests that piggyback on this one
269 $this->_caseActivityId
= $result['values'][$result['id']]['id'];
271 // Check other DB tables populated properly - is there a better way to do this? assertDBState() requires that we know the id already.
272 $dao = new CRM_Case_DAO_CaseActivity();
274 $dao->activity_id
= $this->_caseActivityId
;
275 $this->assertEquals($dao->find(), 1, 'case_activity table not populated correctly in line ' . __LINE__
);
278 $dao = new CRM_Activity_DAO_ActivityContact();
279 $dao->activity_id
= $this->_caseActivityId
;
280 $dao->contact_id
= $this->_params
['contact_id'];
281 $dao->record_type_id
= 3;
282 $this->assertEquals($dao->find(), 1, 'activity_contact table not populated correctly in line ' . __LINE__
);
285 // TODO: There's more things we could check
289 * Test activity api update for case activities.
291 public function testCaseActivityUpdate() {
292 // Need to create the case and activity before we can update it
293 $this->testCaseActivityCreate();
296 'activity_id' => $this->_caseActivityId
,
298 'activity_type_id' => 14,
299 'source_contact_id' => $this->_loggedInUser
,
300 'subject' => 'New subject',
302 $result = $this->callAPISuccess('activity', 'create', $params);
304 $this->assertEquals($result['values'][$result['id']]['subject'], $params['subject'], 'in line ' . __LINE__
);
306 // id should be one greater, since this is a new revision
307 $this->assertEquals($result['values'][$result['id']]['id'],
308 $this->_caseActivityId +
1,
309 'in line ' . __LINE__
311 $this->assertEquals($result['values'][$result['id']]['original_id'],
312 $this->_caseActivityId
,
313 'in line ' . __LINE__
316 // Check revision is as expected
318 'activity_id' => $this->_caseActivityId
,
320 $revActivity = $this->callAPISuccess('activity', 'get', $revParams);
321 $this->assertEquals($revActivity['values'][$this->_caseActivityId
]['is_current_revision'],
323 $this->assertEquals($revActivity['values'][$this->_caseActivityId
]['is_deleted'],
327 //TODO: check some more things
330 public function testCaseActivityUpdateCustom() {
331 // Create a case first
332 $result = $this->callAPISuccess('case', 'create', $this->_params
);
334 // Create custom field group
335 // Note the second parameter is Activity on purpose, not Case.
336 $custom_ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__
, 'ActivityTest.php');
340 'case_id' => $result['id'],
342 'activity_type_id' => 14,
343 'subject' => 'Test followup',
344 'source_contact_id' => $this->_loggedInUser
,
345 'target_contact_id' => $this->_params
['contact_id'],
346 'custom_' . $custom_ids['custom_field_id'] => "custom string",
348 $result = $this->callAPISuccess('activity', 'create', $params);
350 $aid = $result['values'][$result['id']]['id'];
354 'activity_id' => $aid,
356 'activity_type_id' => 14,
357 'source_contact_id' => $this->_loggedInUser
,
358 'subject' => 'New subject',
360 $revAct = $this->callAPISuccess('activity', 'create', $params);
362 // Retrieve revision and check custom fields got copied
364 'activity_id' => $aid +
1,
365 'return.custom_' . $custom_ids['custom_field_id'] => 1,
367 $revAct = $this->callAPISuccess('activity', 'get', $revParams);
369 $this->assertEquals($revAct['values'][$aid +
1]['custom_' . $custom_ids['custom_field_id']], "custom string",
370 "Error message: " . CRM_Utils_Array
::value('error_message', $revAct) . ' in line ' . __LINE__
373 $this->customFieldDelete($custom_ids['custom_field_id']);
374 $this->customGroupDelete($custom_ids['custom_group_id']);