Merge remote-tracking branch 'upstream/4.4' into 4.4-master-2014-06-23-18-25-12
[civicrm-core.git] / tests / phpunit / api / v3 / CaseTest.php
1 <?php
2 /**
3 * File for the TestCase class
4 *
5 * (PHP 5)
6 *
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 $
12 * @package CiviCRM
13 *
14 * This file is part of CiviCRM
15 *
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.
20 *
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.
25 *
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/>.
29 */
30
31 /**
32 * Include class definitions
33 */
34 require_once 'CiviTest/CiviCaseTestCase.php';
35
36 /**
37 * Test APIv3 civicrm_case_* functions
38 *
39 * @package CiviCRM_APIv3
40 */
41 class api_v3_CaseTest extends CiviCaseTestCase {
42 protected $_params;
43 protected $_entity;
44 protected $_apiversion =3;
45 protected $followup_activity_type_value;
46
47 /**
48 * Test setup for every test
49 *
50 * Connect to the database, truncate the tables that will be used
51 * and redirect stdin to a temporary file
52 */
53 public function setUp() {
54 $this->_entity = 'case';
55
56 parent::setUp();
57
58 $activityTypes = $this->callAPISuccess('option_value', 'get', array(
59 'option_group_id' => 2,
60 'name' => 'Follow Up',
61 'label' => 'Follow Up',
62 'sequential' => 1,
63 ));
64 $this->followup_activity_type_value = $activityTypes['values'][0]['value'];
65
66 $this->_params = array(
67 'case_type_id' => $this->caseTypeId,
68 'subject' => 'Test case',
69 'contact_id' => 17,
70 );
71 }
72
73 /**
74 * check with empty array
75 */
76 function testCaseCreateEmpty() {
77 $result = $this->callAPIFailure('case', 'create', array());
78 }
79
80 /**
81 * check if required fields are not passed
82 */
83 function testCaseCreateWithoutRequired() {
84 $params = array(
85 'subject' => 'this case should fail',
86 'case_type_id' => 1,
87 );
88
89 $result = $this->callAPIFailure('case', 'create', $params);
90 }
91
92 /**
93 * Test create function with valid parameters
94 */
95 function testCaseCreate() {
96 // Create Case
97 $params = $this->_params;
98 // Test using label instead of value
99 unset($params['case_type_id']);
100 $params['case_type'] = 'Housing Support';
101 $result = $this->callAPISuccess('case', 'create', $params);
102 $id = $result['id'];
103
104 // Check result
105 $result = $this->callAPISuccess('case', 'get', array('id' => $id));
106 $this->assertEquals($result['values'][$id]['id'], $id, 'in line ' . __LINE__);
107 $this->assertEquals($result['values'][$id]['case_type_id'], $this->caseTypeId, 'in line ' . __LINE__);
108 $this->assertEquals($result['values'][$id]['subject'], $params['subject'], 'in line ' . __LINE__);
109 }
110
111 /**
112 * Test update (create with id) function with valid parameters
113 */
114 function testCaseUpdate() {
115 // Create Case
116 $params = $this->_params;
117 // Test using name instead of value
118 unset($params['case_type_id']);
119 $params['case_type'] = 'Housing Support';
120 $result = $this->callAPISuccess('case', 'create', $params);
121 $id = $result['id'];
122 $result = $this->callAPISuccess('case', 'get', array('id' => $id));
123 $case = $result['values'][$id];
124
125 // Update Case
126 $params = array('id' => $id);
127 $params['subject'] = $case['subject'] = 'Something Else';
128 $result = $this->callAPISuccess('case', 'create', $params);
129
130 // Verify that updated case is exactly equal to the original with new subject
131 $result = $this->callAPISuccess('case', 'get', array('case_id' => $id));
132 $this->assertEquals($result['values'][$id], $case, 'in line ' . __LINE__);
133 }
134
135 /**
136 * Test delete function with valid parameters
137 */
138 function testCaseDelete() {
139 // Create Case
140 $result = $this->callAPISuccess('case', 'create', $this->_params);
141
142 // Move Case to Trash
143 $id = $result['id'];
144 $result = $this->callAPISuccess('case', 'delete', array('id' => $id, 'move_to_trash' => 1));
145
146 // Check result - also check that 'case_id' works as well as 'id'
147 $result = $this->callAPISuccess('case', 'get', array('case_id' => $id));
148 $this->assertEquals(1, $result['values'][$id]['is_deleted'], 'in line ' . __LINE__);
149
150 // Delete Case Permanently - also check that 'case_id' works as well as 'id'
151 $result = $this->callAPISuccess('case', 'delete', array('case_id' => $id));
152
153 // Check result - case should no longer exist
154 $result = $this->callAPISuccess('case', 'get', array('id' => $id));
155 $this->assertEquals(0, $result['count']);
156 }
157
158 /**
159 * Test get function based on activity
160 */
161 function testCaseGetByActivity() {
162 // Create Case
163 $result = $this->callAPISuccess('case', 'create', $this->_params);
164 $id = $result['id'];
165
166 // Check result - we should get a list of activity ids
167 $result = $this->callAPISuccess('case', 'get', array('id' => $id));
168 $case = $result['values'][$id];
169 $activity = $case['activities'][0];
170
171 // Fetch case based on an activity id
172 $result = $this->callAPISuccess('case', 'get', array('activity_id' => $activity, 'return' => 'activities,contacts'));
173 $this->assertEquals(FALSE, empty($result['values'][$id]), 'in line ' . __LINE__);
174 $this->assertEquals($result['values'][$id], $case, 'in line ' . __LINE__);
175 }
176
177 /**
178 * Test get function based on contact id
179 */
180 function testCaseGetByContact() {
181 // Create Case
182 $result = $this->callAPISuccess('case', 'create', $this->_params);
183 $id = $result['id'];
184
185 // Store result for later
186 $case = $this->callAPISuccess('case', 'getsingle', array('id' => $id));
187
188 // Fetch case based on client contact id
189 $result = $this->callAPISuccess('case', 'get', array('client_id' => $this->_params['contact_id'], 'return' => array('activities', 'contacts')));
190 $this->assertAPIArrayComparison($result['values'][$id], $case);
191 }
192
193 /**
194 * Test get function based on subject
195 */
196 function testCaseGetBySubject() {
197 // Create Case
198 $result = $this->callAPISuccess('case', 'create', $this->_params);
199 $id = $result['id'];
200
201 // Store result for later
202 $case = $this->callAPISuccess('case', 'getsingle', array('id' => $id));
203
204 // Fetch case based on client contact id
205 $result = $this->callAPISuccess('case', 'get', array('subject' => $this->_params['subject'], 'return' => array('activities', 'contacts')));
206 $this->assertAPIArrayComparison($result['values'][$id], $case);
207 }
208
209 /**
210 * Test get function based on wrong subject
211 */
212 function testCaseGetByWrongSubject() {
213 // Create Case
214 $result = $this->callAPISuccess('case', 'create', $this->_params);
215 $id = $result['id'];
216
217 // Append 'wrong' to subject so that it is no longer the same
218 $result = $this->callAPISuccess('case', 'get', array('subject' => $this->_params['subject'] . 'wrong', 'return' => array('activities', 'contacts')));
219 $this->assertEquals(0, $result['count'], 'in line ' . __LINE__);
220 }
221
222 /**
223 * Test get function with no criteria
224 */
225 function testCaseGetNoCriteria() {
226 // Create Case
227 $result = $this->callAPISuccess('case', 'create', $this->_params);
228 $id = $result['id'];
229
230 // Store result for later
231 $case = $this->callAPISuccess('case', 'getsingle', array('id' => $id));
232
233 $result = $this->callAPISuccess('case', 'get', array('return' => array('activities', 'contacts')));
234 $this->assertAPIArrayComparison($result['values'][$id], $case);
235 }
236
237 /**
238 * Test activity api create for case activities
239 */
240 function testCaseActivityCreate() {
241 // Create a case first
242 $params = $this->_params;
243 $result = $this->callAPISuccess('case', 'create', $params);
244 $params = array(
245 'case_id' => 1,
246 // follow up
247 'activity_type_id' => $this->followup_activity_type_value,
248 'subject' => 'Test followup',
249 'source_contact_id' => $this->_loggedInUser,
250 'target_contact_id' => $this->_params['contact_id'],
251 );
252 $result = $this->callAPISuccess('activity', 'create', $params);
253 $this->assertEquals($result['values'][$result['id']]['activity_type_id'], $params['activity_type_id'], 'in line ' . __LINE__);
254
255 // might need this for other tests that piggyback on this one
256 $this->_caseActivityId = $result['values'][$result['id']]['id'];
257
258 // Check other DB tables populated properly - is there a better way to do this? assertDBState() requires that we know the id already.
259 $dao = new CRM_Case_DAO_CaseActivity();
260 $dao->case_id = 1;
261 $dao->activity_id = $this->_caseActivityId;
262 $this->assertEquals($dao->find(), 1, 'case_activity table not populated correctly in line ' . __LINE__);
263 $dao->free();
264
265 $dao = new CRM_Activity_DAO_ActivityContact();
266 $dao->activity_id = $this->_caseActivityId;
267 $dao->contact_id = $this->_params['contact_id'];
268 $dao->record_type_id = 3;
269 $this->assertEquals($dao->find(), 1, 'activity_contact table not populated correctly in line ' . __LINE__);
270 $dao->free();
271
272 // TODO: There's more things we could check
273 }
274
275 /**
276 * Test activity api update for case activities
277 */
278 function testCaseActivityUpdate() {
279 // Need to create the case and activity before we can update it
280 $this->testCaseActivityCreate();
281
282 $params = array(
283 'activity_id' => $this->_caseActivityId,
284 'case_id' => 1,
285 'activity_type_id' => 14,
286 'source_contact_id' => $this->_loggedInUser,
287 'subject' => 'New subject',
288 );
289 $result = $this->callAPISuccess('activity', 'create', $params);
290
291 $this->assertEquals($result['values'][$result['id']]['subject'], $params['subject'], 'in line ' . __LINE__);
292
293 // id should be one greater, since this is a new revision
294 $this->assertEquals($result['values'][$result['id']]['id'],
295 $this->_caseActivityId + 1,
296 'in line ' . __LINE__
297 );
298 $this->assertEquals($result['values'][$result['id']]['original_id'],
299 $this->_caseActivityId,
300 'in line ' . __LINE__
301 );
302
303 // Check revision is as expected
304 $revParams = array(
305 'activity_id' => $this->_caseActivityId,
306 );
307 $revActivity = $this->callAPISuccess('activity', 'get', $revParams);
308 $this->assertEquals($revActivity['values'][$this->_caseActivityId]['is_current_revision'],
309 0);
310 $this->assertEquals($revActivity['values'][$this->_caseActivityId]['is_deleted'],
311 0
312 );
313
314 //TODO: check some more things
315 }
316
317 function testCaseActivityUpdateCustom() {
318 // Create a case first
319 $result = $this->callAPISuccess('case', 'create', $this->_params);
320
321 // Create custom field group
322 // Note the second parameter is Activity on purpose, not Case.
323 $custom_ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, 'ActivityTest.php');
324
325 // create activity
326 $params = array(
327 'case_id' => $result['id'],
328 // follow up
329 'activity_type_id' => 14,
330 'subject' => 'Test followup',
331 'source_contact_id' => $this->_loggedInUser,
332 'target_contact_id' => $this->_params['contact_id'],
333 'custom_' . $custom_ids['custom_field_id'] => "custom string",
334 );
335 $result = $this->callAPISuccess('activity', 'create', $params);
336
337 $aid = $result['values'][$result['id']]['id'];
338
339 // Update activity
340 $params = array(
341 'activity_id' => $aid,
342 'case_id' => 1,
343 'activity_type_id' => 14,
344 'source_contact_id' => $this->_loggedInUser,
345 'subject' => 'New subject',
346 );
347 $revAct = $this->callAPISuccess('activity', 'create', $params);
348
349 // Retrieve revision and check custom fields got copied
350 $revParams = array(
351 'activity_id' => $aid + 1,
352 'return.custom_' . $custom_ids['custom_field_id'] => 1,
353 );
354 $revAct = $this->callAPISuccess('activity', 'get', $revParams);
355
356 $this->assertEquals($revAct['values'][$aid + 1]['custom_' . $custom_ids['custom_field_id']], "custom string",
357 "Error message: " . CRM_Utils_Array::value('error_message', $revAct) . ' in line ' . __LINE__
358 );
359
360 $this->customFieldDelete($custom_ids['custom_field_id']);
361 $this->customGroupDelete($custom_ids['custom_group_id']);
362 }
363 }