Merge pull request #7797 from JKingsnorth/CRM-17977
[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 */
6a488035
TO
35
36/**
37 * Test APIv3 civicrm_case_* functions
38 *
6c6e6187 39 * @package CiviCRM_APIv3
acb109b7 40 * @group headless
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
225d474b 96 $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'];
e61d8c45 127 $case = $this->callAPISuccess('case', 'getsingle', array('id' => $id));
6a488035 128
9e959479 129 // Update Case.
4e420887 130 $params = array('id' => $id);
9937542b 131 $params['subject'] = $case['subject'] = 'Something Else';
9e959479 132 $this->callAPISuccess('case', 'create', $params);
6a488035 133
9e959479 134 // Verify that updated case is exactly equal to the original with new subject.
9937542b 135 $result = $this->callAPISuccessGetSingle('Case', array('case_id' => $id));
e61d8c45 136 $this->assertAPIArrayComparison($result, $case);
6a488035
TO
137 }
138
139 /**
fe482240 140 * Test delete function with valid parameters.
6a488035 141 */
00be9182 142 public function testCaseDelete() {
6a488035 143 // Create Case
4e420887 144 $result = $this->callAPISuccess('case', 'create', $this->_params);
6a488035
TO
145
146 // Move Case to Trash
147 $id = $result['id'];
4e420887 148 $result = $this->callAPISuccess('case', 'delete', array('id' => $id, 'move_to_trash' => 1));
6a488035
TO
149
150 // Check result - also check that 'case_id' works as well as 'id'
4e420887 151 $result = $this->callAPISuccess('case', 'get', array('case_id' => $id));
9e959479 152 $this->assertEquals(1, $result['values'][$id]['is_deleted']);
6a488035
TO
153
154 // Delete Case Permanently - also check that 'case_id' works as well as 'id'
4e420887 155 $result = $this->callAPISuccess('case', 'delete', array('case_id' => $id));
6a488035
TO
156
157 // Check result - case should no longer exist
4e420887 158 $result = $this->callAPISuccess('case', 'get', array('id' => $id));
159 $this->assertEquals(0, $result['count']);
6a488035
TO
160 }
161
162 /**
fe482240 163 * Test get function based on activity.
6a488035 164 */
00be9182 165 public function testCaseGetByActivity() {
6a488035 166 // Create Case
4e420887 167 $result = $this->callAPISuccess('case', 'create', $this->_params);
6a488035
TO
168 $id = $result['id'];
169
170 // Check result - we should get a list of activity ids
e61d8c45 171 $result = $this->callAPISuccess('case', 'get', array('id' => $id, 'return' => 'activities'));
6a488035
TO
172 $case = $result['values'][$id];
173 $activity = $case['activities'][0];
174
175 // Fetch case based on an activity id
92915c55
TO
176 $result = $this->callAPISuccess('case', 'get', array(
177 'activity_id' => $activity,
e61d8c45 178 'return' => 'activities',
92915c55 179 ));
9e959479
EM
180 $this->assertEquals(FALSE, empty($result['values'][$id]));
181 $this->assertEquals($result['values'][$id], $case);
6a488035
TO
182 }
183
184 /**
fe482240 185 * Test get function based on contact id.
6a488035 186 */
00be9182 187 public function testCaseGetByContact() {
6a488035 188 // Create Case
4e420887 189 $result = $this->callAPISuccess('case', 'create', $this->_params);
6a488035
TO
190 $id = $result['id'];
191
192 // Store result for later
9937542b 193 $case = $this->callAPISuccessGetSingle('case', array('id' => $id, 'return' => array('activities', 'contacts')));
6a488035
TO
194
195 // Fetch case based on client contact id
92915c55
TO
196 $result = $this->callAPISuccess('case', 'get', array(
197 'client_id' => $this->_params['contact_id'],
79d7553f 198 'return' => array('activities', 'contacts'),
92915c55 199 ));
4e420887 200 $this->assertAPIArrayComparison($result['values'][$id], $case);
6a488035
TO
201 }
202
f21557af 203 /**
fe482240 204 * Test get function based on subject.
f21557af 205 */
00be9182 206 public function testCaseGetBySubject() {
f21557af 207 // Create Case
208 $result = $this->callAPISuccess('case', 'create', $this->_params);
209 $id = $result['id'];
210
211 // Store result for later
9937542b 212 $case = $this->callAPISuccessGetSingle('Case', array('id' => $id, 'return' => 'subject'));
f21557af 213
214 // Fetch case based on client contact id
92915c55
TO
215 $result = $this->callAPISuccess('case', 'get', array(
216 'subject' => $this->_params['subject'],
e61d8c45 217 'return' => array('subject'),
92915c55 218 ));
f21557af 219 $this->assertAPIArrayComparison($result['values'][$id], $case);
220 }
221
222 /**
fe482240 223 * Test get function based on wrong subject.
f21557af 224 */
00be9182 225 public function testCaseGetByWrongSubject() {
f21557af 226 $result = $this->callAPISuccess('case', 'create', $this->_params);
f21557af 227
9e959479 228 // Append 'wrong' to subject so that it is no longer the same.
92915c55
TO
229 $result = $this->callAPISuccess('case', 'get', array(
230 'subject' => $this->_params['subject'] . 'wrong',
79d7553f 231 'return' => array('activities', 'contacts'),
92915c55 232 ));
9e959479 233 $this->assertEquals(0, $result['count']);
f21557af 234 }
235
236 /**
fe482240 237 * Test get function with no criteria.
f21557af 238 */
00be9182 239 public function testCaseGetNoCriteria() {
f21557af 240 $result = $this->callAPISuccess('case', 'create', $this->_params);
241 $id = $result['id'];
242
243 // Store result for later
9937542b 244 $case = $this->callAPISuccessGetSingle('Case', array('id' => $id, 'return' => 'contact_id'));
f21557af 245
e61d8c45 246 $result = $this->callAPISuccess('case', 'get', array('limit' => 0, 'return' => array('contact_id')));
f21557af 247 $this->assertAPIArrayComparison($result['values'][$id], $case);
248 }
249
6a488035 250 /**
fe482240 251 * Test activity api create for case activities.
6a488035 252 */
00be9182 253 public function testCaseActivityCreate() {
6a488035 254 $params = $this->_params;
9e959479 255 $this->callAPISuccess('case', 'create', $params);
6a488035
TO
256 $params = array(
257 'case_id' => 1,
258 // follow up
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'],
6a488035 263 );
4e420887 264 $result = $this->callAPISuccess('activity', 'create', $params);
9e959479 265 $this->assertEquals($result['values'][$result['id']]['activity_type_id'], $params['activity_type_id']);
6a488035
TO
266
267 // might need this for other tests that piggyback on this one
268 $this->_caseActivityId = $result['values'][$result['id']]['id'];
269
270 // Check other DB tables populated properly - is there a better way to do this? assertDBState() requires that we know the id already.
4e420887 271 $dao = new CRM_Case_DAO_CaseActivity();
272 $dao->case_id = 1;
6a488035
TO
273 $dao->activity_id = $this->_caseActivityId;
274 $this->assertEquals($dao->find(), 1, 'case_activity table not populated correctly in line ' . __LINE__);
275 $dao->free();
276
b319d00a 277 $dao = new CRM_Activity_DAO_ActivityContact();
6a488035 278 $dao->activity_id = $this->_caseActivityId;
b319d00a
DL
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__);
6a488035
TO
282 $dao->free();
283
284 // TODO: There's more things we could check
285 }
286
287 /**
fe482240 288 * Test activity api update for case activities.
6a488035 289 */
00be9182 290 public function testCaseActivityUpdate() {
6a488035
TO
291 // Need to create the case and activity before we can update it
292 $this->testCaseActivityCreate();
293
294 $params = array(
295 'activity_id' => $this->_caseActivityId,
296 'case_id' => 1,
297 'activity_type_id' => 14,
298 'source_contact_id' => $this->_loggedInUser,
299 'subject' => 'New subject',
6a488035 300 );
4e420887 301 $result = $this->callAPISuccess('activity', 'create', $params);
6a488035 302
9e959479 303 $this->assertEquals($result['values'][$result['id']]['subject'], $params['subject']);
6a488035
TO
304
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__
309 );
310 $this->assertEquals($result['values'][$result['id']]['original_id'],
311 $this->_caseActivityId,
312 'in line ' . __LINE__
313 );
314
315 // Check revision is as expected
316 $revParams = array(
317 'activity_id' => $this->_caseActivityId,
6a488035 318 );
4e420887 319 $revActivity = $this->callAPISuccess('activity', 'get', $revParams);
6a488035 320 $this->assertEquals($revActivity['values'][$this->_caseActivityId]['is_current_revision'],
4e420887 321 0);
6a488035 322 $this->assertEquals($revActivity['values'][$this->_caseActivityId]['is_deleted'],
4e420887 323 0
6a488035
TO
324 );
325
326 //TODO: check some more things
327 }
328
00be9182 329 public function testCaseActivityUpdateCustom() {
6a488035 330 // Create a case first
4e420887 331 $result = $this->callAPISuccess('case', 'create', $this->_params);
6a488035
TO
332
333 // Create custom field group
334 // Note the second parameter is Activity on purpose, not Case.
335 $custom_ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, 'ActivityTest.php');
336
337 // create activity
338 $params = array(
4e420887 339 'case_id' => $result['id'],
6a488035
TO
340 // follow up
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",
6a488035 346 );
4e420887 347 $result = $this->callAPISuccess('activity', 'create', $params);
6a488035
TO
348
349 $aid = $result['values'][$result['id']]['id'];
350
351 // Update activity
352 $params = array(
353 'activity_id' => $aid,
354 'case_id' => 1,
355 'activity_type_id' => 14,
356 'source_contact_id' => $this->_loggedInUser,
357 'subject' => 'New subject',
6a488035 358 );
9e959479 359 $this->callAPISuccess('activity', 'create', $params);
6a488035 360
9e959479 361 // Retrieve revision and check custom fields got copied.
6a488035
TO
362 $revParams = array(
363 'activity_id' => $aid + 1,
6a488035
TO
364 'return.custom_' . $custom_ids['custom_field_id'] => 1,
365 );
4e420887 366 $revAct = $this->callAPISuccess('activity', 'get', $revParams);
6a488035 367
6a488035 368 $this->assertEquals($revAct['values'][$aid + 1]['custom_' . $custom_ids['custom_field_id']], "custom string",
9e959479 369 "Error message: " . CRM_Utils_Array::value('error_message', $revAct));
6a488035
TO
370
371 $this->customFieldDelete($custom_ids['custom_field_id']);
372 $this->customGroupDelete($custom_ids['custom_group_id']);
373 }
96025800 374
37606d10
SB
375 public function testCaseGetByStatus() {
376 // Create 2 cases with different status ids.
377 $case1 = $this->callAPISuccess('Case', 'create', array(
378 'contact_id' => 17,
379 'subject' => "Test case 1",
380 'case_type_id' => $this->caseTypeId,
381 'status_id' => "Open",
382 'sequential' => 1,
383 ));
384 $this->callAPISuccess('Case', 'create', array(
385 'contact_id' => 17,
386 'subject' => "Test case 2",
387 'case_type_id' => $this->caseTypeId,
388 'status_id' => "Urgent",
389 'sequential' => 1,
390 ));
391 $result = $this->callAPISuccessGetSingle('Case', array(
392 'sequential' => 1,
393 'contact_id' => 17,
394 'status_id' => "Open",
395 ));
396 $this->assertEquals($case1['id'], $result['id']);
397 }
398
6a488035 399}