Provide a RoR-like test fixture helper
[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/CiviUnitTestCase.php';
35
36 /**
37 * Test APIv3 civicrm_case_* functions
38 *
39 * @package CiviCRM_APIv3
40 */
41 class api_v3_CaseTest extends CiviUnitTestCase {
42 protected $_params;
43 protected $_entity;
44 protected $_apiversion =3;
45 protected $followup_activity_type_value;
46 protected $caseTypeId;
47 protected $caseStatusGroup;
48 protected $caseTypeGroup;
49 protected $optionValues;
50
51 /**
52 * Test setup for every test
53 *
54 * Connect to the database, truncate the tables that will be used
55 * and redirect stdin to a temporary file
56 */
57 public function setUp() {
58 $this->_entity = 'case';
59
60 parent::setUp();
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)
68 );
69 $this->caseTypeGroup = $this->callAPISuccess('option_group', 'get', array(
70 'name' => 'case_type',
71 'format.only_id' => 1)
72 );
73 $caseTypes = $this->callAPISuccess('option_value', 'Create', array(
74 'option_group_id' => $this->caseTypeGroup,
75 'name' => 'housing_support',
76 'label' => "Housing Support",
77 'sequential' => 1,
78 'description' => 'Help homeless individuals obtain temporary and long-term housing',
79 ));
80
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',
90 );
91 foreach ($optionValues as $name => $label) {
92 $activityTypes = $this->callAPISuccess('option_value', 'Create', array(
93 'option_group_id' => 2,
94 'name' => $name,
95 'label' => $label,
96 'component_id' => 7,
97 ));
98 // store for cleanup
99 $this->optionValues[] = $activityTypes['id'];
100 }
101 $tablesToTruncate = array(
102 'civicrm_activity',
103 'civicrm_contact',
104 'civicrm_custom_group',
105 'civicrm_custom_field',
106 'civicrm_case',
107 'civicrm_case_contact',
108 'civicrm_case_activity',
109 'civicrm_activity_contact',
110 'civicrm_relationship',
111 'civicrm_relationship_type',
112 );
113
114 $this->quickCleanup($tablesToTruncate);
115
116 $this->loadAllFixtures();
117
118 $activityTypes = $this->callAPISuccess('option_value', 'get', array(
119 'option_group_id' => 2,
120 'name' => 'Follow Up',
121 'label' => 'Follow Up',
122 'sequential' => 1,
123 ));
124 $this->followup_activity_type_value = $activityTypes['values'][0]['value'];
125
126 // enable the default custom templates for the case type xml files
127 $this->customDirectories(array('template_path' => TRUE));
128
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__);
132
133 $this->_params = array(
134 'case_type_id' => $this->caseTypeId,
135 'subject' => 'Test case',
136 'contact_id' => 17,
137 );
138
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);
147 }
148
149 /**
150 * Tears down the fixture, for example, closes a network connection.
151 * This method is called after a test is executed.
152 *
153 * @access protected
154 */
155 function tearDown() {
156 foreach ($this->optionValues as $id) {
157 $this->callAPISuccess('option_value', 'delete', array('id' => $id));
158 }
159 $tablesToTruncate = array(
160 'civicrm_contact',
161 'civicrm_activity',
162 'civicrm_case',
163 'civicrm_case_contact',
164 'civicrm_case_activity',
165 'civicrm_activity_contact',
166 'civicrm_relationship',
167 'civicrm_relationship_type',
168 );
169 $this->quickCleanup($tablesToTruncate, TRUE);
170
171 $this->customDirectories(array('template_path' => FALSE));
172 }
173
174 /**
175 * check with empty array
176 */
177 function testCaseCreateEmpty() {
178 $result = $this->callAPIFailure('case', 'create', array());
179 }
180
181 /**
182 * check if required fields are not passed
183 */
184 function testCaseCreateWithoutRequired() {
185 $params = array(
186 'subject' => 'this case should fail',
187 'case_type_id' => 1,
188 );
189
190 $result = $this->callAPIFailure('case', 'create', $params);
191 }
192
193 /**
194 * Test create function with valid parameters
195 */
196 function testCaseCreate() {
197 // Create Case
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);
203 $id = $result['id'];
204
205 // Check result
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__);
210 }
211
212 /**
213 * Test update (create with id) function with valid parameters
214 */
215 function testCaseUpdate() {
216 // Create Case
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);
221 $id = $result['id'];
222 $result = $this->callAPISuccess('case', 'get', array('id' => $id));
223 $case = $result['values'][$id];
224
225 // Update Case
226 $params = array('id' => $id);
227 $params['subject'] = $case['subject'] = 'Something Else';
228 $result = $this->callAPISuccess('case', 'create', $params);
229
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__);
233 }
234
235 /**
236 * Test delete function with valid parameters
237 */
238 function testCaseDelete() {
239 // Create Case
240 $result = $this->callAPISuccess('case', 'create', $this->_params);
241
242 // Move Case to Trash
243 $id = $result['id'];
244 $result = $this->callAPISuccess('case', 'delete', array('id' => $id, 'move_to_trash' => 1));
245
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__);
249
250 // Delete Case Permanently - also check that 'case_id' works as well as 'id'
251 $result = $this->callAPISuccess('case', 'delete', array('case_id' => $id));
252
253 // Check result - case should no longer exist
254 $result = $this->callAPISuccess('case', 'get', array('id' => $id));
255 $this->assertEquals(0, $result['count']);
256 }
257
258 /**
259 * Test get function based on activity
260 */
261 function testCaseGetByActivity() {
262 // Create Case
263 $result = $this->callAPISuccess('case', 'create', $this->_params);
264 $id = $result['id'];
265
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];
270
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__);
275 }
276
277 /**
278 * Test get function based on contact id
279 */
280 function testCaseGetByContact() {
281 // Create Case
282 $result = $this->callAPISuccess('case', 'create', $this->_params);
283 $id = $result['id'];
284
285 // Store result for later
286 $case = $this->callAPISuccess('case', 'getsingle', array('id' => $id));
287
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);
291 }
292
293 /**
294 * Test get function based on subject
295 */
296 function testCaseGetBySubject() {
297 // Create Case
298 $result = $this->callAPISuccess('case', 'create', $this->_params);
299 $id = $result['id'];
300
301 // Store result for later
302 $case = $this->callAPISuccess('case', 'getsingle', array('id' => $id));
303
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);
307 }
308
309 /**
310 * Test get function based on wrong subject
311 */
312 function testCaseGetByWrongSubject() {
313 // Create Case
314 $result = $this->callAPISuccess('case', 'create', $this->_params);
315 $id = $result['id'];
316
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__);
320 }
321
322 /**
323 * Test get function with no criteria
324 */
325 function testCaseGetNoCriteria() {
326 // Create Case
327 $result = $this->callAPISuccess('case', 'create', $this->_params);
328 $id = $result['id'];
329
330 // Store result for later
331 $case = $this->callAPISuccess('case', 'getsingle', array('id' => $id));
332
333 $result = $this->callAPISuccess('case', 'get', array('return' => array('activities', 'contacts')));
334 $this->assertAPIArrayComparison($result['values'][$id], $case);
335 }
336
337 /**
338 * Test activity api create for case activities
339 */
340 function testCaseActivityCreate() {
341 // Create a case first
342 $params = $this->_params;
343 $result = $this->callAPISuccess('case', 'create', $params);
344 $params = array(
345 'case_id' => 1,
346 // follow up
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'],
351 );
352 $result = $this->callAPISuccess('activity', 'create', $params);
353 $this->assertEquals($result['values'][$result['id']]['activity_type_id'], $params['activity_type_id'], 'in line ' . __LINE__);
354
355 // might need this for other tests that piggyback on this one
356 $this->_caseActivityId = $result['values'][$result['id']]['id'];
357
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();
360 $dao->case_id = 1;
361 $dao->activity_id = $this->_caseActivityId;
362 $this->assertEquals($dao->find(), 1, 'case_activity table not populated correctly in line ' . __LINE__);
363 $dao->free();
364
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__);
370 $dao->free();
371
372 // TODO: There's more things we could check
373 }
374
375 /**
376 * Test activity api update for case activities
377 */
378 function testCaseActivityUpdate() {
379 // Need to create the case and activity before we can update it
380 $this->testCaseActivityCreate();
381
382 $params = array(
383 'activity_id' => $this->_caseActivityId,
384 'case_id' => 1,
385 'activity_type_id' => 14,
386 'source_contact_id' => $this->_loggedInUser,
387 'subject' => 'New subject',
388 );
389 $result = $this->callAPISuccess('activity', 'create', $params);
390
391 $this->assertEquals($result['values'][$result['id']]['subject'], $params['subject'], 'in line ' . __LINE__);
392
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__
397 );
398 $this->assertEquals($result['values'][$result['id']]['original_id'],
399 $this->_caseActivityId,
400 'in line ' . __LINE__
401 );
402
403 // Check revision is as expected
404 $revParams = array(
405 'activity_id' => $this->_caseActivityId,
406 );
407 $revActivity = $this->callAPISuccess('activity', 'get', $revParams);
408 $this->assertEquals($revActivity['values'][$this->_caseActivityId]['is_current_revision'],
409 0);
410 $this->assertEquals($revActivity['values'][$this->_caseActivityId]['is_deleted'],
411 0
412 );
413
414 //TODO: check some more things
415 }
416
417 function testCaseActivityUpdateCustom() {
418 // Create a case first
419 $result = $this->callAPISuccess('case', 'create', $this->_params);
420
421 // Create custom field group
422 // Note the second parameter is Activity on purpose, not Case.
423 $custom_ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, 'ActivityTest.php');
424
425 // create activity
426 $params = array(
427 'case_id' => $result['id'],
428 // follow up
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",
434 );
435 $result = $this->callAPISuccess('activity', 'create', $params);
436
437 $aid = $result['values'][$result['id']]['id'];
438
439 // Update activity
440 $params = array(
441 'activity_id' => $aid,
442 'case_id' => 1,
443 'activity_type_id' => 14,
444 'source_contact_id' => $this->_loggedInUser,
445 'subject' => 'New subject',
446 );
447 $revAct = $this->callAPISuccess('activity', 'create', $params);
448
449 // Retrieve revision and check custom fields got copied
450 $revParams = array(
451 'activity_id' => $aid + 1,
452 'return.custom_' . $custom_ids['custom_field_id'] => 1,
453 );
454 $revAct = $this->callAPISuccess('activity', 'get', $revParams);
455
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__
458 );
459
460 $this->customFieldDelete($custom_ids['custom_field_id']);
461 $this->customGroupDelete($custom_ids['custom_group_id']);
462 }
463 }