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