Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /** | |
3 | * File for the TestCase class | |
4 | * | |
5 | * (PHP 5) | |
6 | * | |
6c6e6187 TO |
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 | |
6a488035 | 10 | * GNU Affero General Public License version 3 |
6c6e6187 TO |
11 | * @version $Id: ActivityTest.php 31254 2010-12-15 10:09:29Z eileen $ |
12 | * @package CiviCRM | |
6a488035 TO |
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 | */ | |
1d8a8d12 | 34 | require_once 'CiviTest/CiviCaseTestCase.php'; |
6a488035 TO |
35 | |
36 | /** | |
37 | * Test APIv3 civicrm_case_* functions | |
38 | * | |
6c6e6187 | 39 | * @package CiviCRM_APIv3 |
6a488035 | 40 | */ |
1d8a8d12 | 41 | class api_v3_CaseTest extends CiviCaseTestCase { |
6a488035 TO |
42 | protected $_params; |
43 | protected $_entity; | |
6c6e6187 | 44 | protected $_apiversion = 3; |
6a488035 | 45 | protected $followup_activity_type_value; |
b7c9bc4c | 46 | |
6a488035 TO |
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() { | |
6a488035 TO |
54 | $this->_entity = 'case'; |
55 | ||
56 | parent::setUp(); | |
0b6f58fa | 57 | |
4e420887 | 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 | )); | |
6a488035 | 64 | $this->followup_activity_type_value = $activityTypes['values'][0]['value']; |
6a488035 | 65 | |
6a488035 TO |
66 | $this->_params = array( |
67 | 'case_type_id' => $this->caseTypeId, | |
68 | 'subject' => 'Test case', | |
69 | 'contact_id' => 17, | |
6a488035 | 70 | ); |
6a488035 TO |
71 | } |
72 | ||
73 | /** | |
100fef9d | 74 | * Check with empty array |
6a488035 | 75 | */ |
00be9182 | 76 | public function testCaseCreateEmpty() { |
4e420887 | 77 | $result = $this->callAPIFailure('case', 'create', array()); |
6a488035 TO |
78 | } |
79 | ||
80 | /** | |
100fef9d | 81 | * Check if required fields are not passed |
6a488035 | 82 | */ |
00be9182 | 83 | public function testCaseCreateWithoutRequired() { |
6a488035 TO |
84 | $params = array( |
85 | 'subject' => 'this case should fail', | |
86 | 'case_type_id' => 1, | |
6a488035 TO |
87 | ); |
88 | ||
4e420887 | 89 | $result = $this->callAPIFailure('case', 'create', $params); |
6a488035 TO |
90 | } |
91 | ||
92 | /** | |
93 | * Test create function with valid parameters | |
94 | */ | |
00be9182 | 95 | public function testCaseCreate() { |
6a488035 TO |
96 | // Create Case |
97 | $params = $this->_params; | |
98 | // Test using label instead of value | |
99 | unset($params['case_type_id']); | |
82de141d | 100 | $params['case_type'] = $this->caseType; |
32191489 | 101 | $result = $this->callAPIAndDocument('case', 'create', $params, __FUNCTION__, __FILE__); |
6a488035 TO |
102 | $id = $result['id']; |
103 | ||
104 | // Check result | |
4e420887 | 105 | $result = $this->callAPISuccess('case', 'get', array('id' => $id)); |
8ffdec17 | 106 | $this->assertEquals($result['values'][$id]['id'], $id, 'in line ' . __LINE__); |
6a488035 TO |
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 | */ | |
00be9182 | 114 | public function testCaseUpdate() { |
6a488035 | 115 | // Create Case |
6a77ff3d CW |
116 | $params = $this->_params; |
117 | // Test using name instead of value | |
8ffdec17 | 118 | unset($params['case_type_id']); |
82de141d | 119 | $params['case_type'] = $this->caseType; |
4e420887 | 120 | $result = $this->callAPISuccess('case', 'create', $params); |
6a488035 | 121 | $id = $result['id']; |
4e420887 | 122 | $result = $this->callAPISuccess('case', 'get', array('id' => $id)); |
6a488035 TO |
123 | $case = $result['values'][$id]; |
124 | ||
125 | // Update Case | |
4e420887 | 126 | $params = array('id' => $id); |
6a488035 | 127 | $params['subject'] = $case['subject'] = 'Something Else'; |
4e420887 | 128 | $result = $this->callAPISuccess('case', 'create', $params); |
6a488035 TO |
129 | |
130 | // Verify that updated case is exactly equal to the original with new subject | |
4e420887 | 131 | $result = $this->callAPISuccess('case', 'get', array('case_id' => $id)); |
6a488035 TO |
132 | $this->assertEquals($result['values'][$id], $case, 'in line ' . __LINE__); |
133 | } | |
134 | ||
135 | /** | |
136 | * Test delete function with valid parameters | |
137 | */ | |
00be9182 | 138 | public function testCaseDelete() { |
6a488035 | 139 | // Create Case |
4e420887 | 140 | $result = $this->callAPISuccess('case', 'create', $this->_params); |
6a488035 TO |
141 | |
142 | // Move Case to Trash | |
143 | $id = $result['id']; | |
4e420887 | 144 | $result = $this->callAPISuccess('case', 'delete', array('id' => $id, 'move_to_trash' => 1)); |
6a488035 TO |
145 | |
146 | // Check result - also check that 'case_id' works as well as 'id' | |
4e420887 | 147 | $result = $this->callAPISuccess('case', 'get', array('case_id' => $id)); |
6a488035 TO |
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' | |
4e420887 | 151 | $result = $this->callAPISuccess('case', 'delete', array('case_id' => $id)); |
6a488035 TO |
152 | |
153 | // Check result - case should no longer exist | |
4e420887 | 154 | $result = $this->callAPISuccess('case', 'get', array('id' => $id)); |
155 | $this->assertEquals(0, $result['count']); | |
6a488035 TO |
156 | } |
157 | ||
158 | /** | |
159 | * Test get function based on activity | |
160 | */ | |
00be9182 | 161 | public function testCaseGetByActivity() { |
6a488035 | 162 | // Create Case |
4e420887 | 163 | $result = $this->callAPISuccess('case', 'create', $this->_params); |
6a488035 TO |
164 | $id = $result['id']; |
165 | ||
166 | // Check result - we should get a list of activity ids | |
4e420887 | 167 | $result = $this->callAPISuccess('case', 'get', array('id' => $id)); |
6a488035 TO |
168 | $case = $result['values'][$id]; |
169 | $activity = $case['activities'][0]; | |
170 | ||
171 | // Fetch case based on an activity id | |
92915c55 TO |
172 | $result = $this->callAPISuccess('case', 'get', array( |
173 | 'activity_id' => $activity, | |
79d7553f | 174 | 'return' => 'activities,contacts', |
92915c55 | 175 | )); |
6a488035 TO |
176 | $this->assertEquals(FALSE, empty($result['values'][$id]), 'in line ' . __LINE__); |
177 | $this->assertEquals($result['values'][$id], $case, 'in line ' . __LINE__); | |
178 | } | |
179 | ||
180 | /** | |
181 | * Test get function based on contact id | |
182 | */ | |
00be9182 | 183 | public function testCaseGetByContact() { |
6a488035 | 184 | // Create Case |
4e420887 | 185 | $result = $this->callAPISuccess('case', 'create', $this->_params); |
6a488035 TO |
186 | $id = $result['id']; |
187 | ||
188 | // Store result for later | |
4e420887 | 189 | $case = $this->callAPISuccess('case', 'getsingle', array('id' => $id)); |
6a488035 TO |
190 | |
191 | // Fetch case based on client contact id | |
92915c55 TO |
192 | $result = $this->callAPISuccess('case', 'get', array( |
193 | 'client_id' => $this->_params['contact_id'], | |
79d7553f | 194 | 'return' => array('activities', 'contacts'), |
92915c55 | 195 | )); |
4e420887 | 196 | $this->assertAPIArrayComparison($result['values'][$id], $case); |
6a488035 TO |
197 | } |
198 | ||
f21557af | 199 | /** |
200 | * Test get function based on subject | |
201 | */ | |
00be9182 | 202 | public function testCaseGetBySubject() { |
f21557af | 203 | // Create Case |
204 | $result = $this->callAPISuccess('case', 'create', $this->_params); | |
205 | $id = $result['id']; | |
206 | ||
207 | // Store result for later | |
208 | $case = $this->callAPISuccess('case', 'getsingle', array('id' => $id)); | |
209 | ||
210 | // Fetch case based on client contact id | |
92915c55 TO |
211 | $result = $this->callAPISuccess('case', 'get', array( |
212 | 'subject' => $this->_params['subject'], | |
79d7553f | 213 | 'return' => array('activities', 'contacts'), |
92915c55 | 214 | )); |
f21557af | 215 | $this->assertAPIArrayComparison($result['values'][$id], $case); |
216 | } | |
217 | ||
218 | /** | |
219 | * Test get function based on wrong subject | |
220 | */ | |
00be9182 | 221 | public function testCaseGetByWrongSubject() { |
f21557af | 222 | // Create Case |
223 | $result = $this->callAPISuccess('case', 'create', $this->_params); | |
224 | $id = $result['id']; | |
225 | ||
226 | // Append 'wrong' to subject so that it is no longer the same | |
92915c55 TO |
227 | $result = $this->callAPISuccess('case', 'get', array( |
228 | 'subject' => $this->_params['subject'] . 'wrong', | |
79d7553f | 229 | 'return' => array('activities', 'contacts'), |
92915c55 | 230 | )); |
f21557af | 231 | $this->assertEquals(0, $result['count'], 'in line ' . __LINE__); |
232 | } | |
233 | ||
234 | /** | |
235 | * Test get function with no criteria | |
236 | */ | |
00be9182 | 237 | public function testCaseGetNoCriteria() { |
f21557af | 238 | // Create Case |
239 | $result = $this->callAPISuccess('case', 'create', $this->_params); | |
240 | $id = $result['id']; | |
241 | ||
242 | // Store result for later | |
243 | $case = $this->callAPISuccess('case', 'getsingle', array('id' => $id)); | |
244 | ||
245 | $result = $this->callAPISuccess('case', 'get', array('return' => array('activities', 'contacts'))); | |
246 | $this->assertAPIArrayComparison($result['values'][$id], $case); | |
247 | } | |
248 | ||
6a488035 TO |
249 | /** |
250 | * Test activity api create for case activities | |
251 | */ | |
00be9182 | 252 | public function testCaseActivityCreate() { |
6a488035 TO |
253 | // Create a case first |
254 | $params = $this->_params; | |
4e420887 | 255 | $result = $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); |
6a488035 TO |
265 | $this->assertEquals($result['values'][$result['id']]['activity_type_id'], $params['activity_type_id'], 'in line ' . __LINE__); |
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 | /** | |
288 | * Test activity api update for case activities | |
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 | |
6a488035 TO |
303 | $this->assertEquals($result['values'][$result['id']]['subject'], $params['subject'], 'in line ' . __LINE__); |
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 | ); |
4e420887 | 359 | $revAct = $this->callAPISuccess('activity', 'create', $params); |
6a488035 TO |
360 | |
361 | // Retrieve revision and check custom fields got copied | |
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 TO |
368 | $this->assertEquals($revAct['values'][$aid + 1]['custom_' . $custom_ids['custom_field_id']], "custom string", |
369 | "Error message: " . CRM_Utils_Array::value('error_message', $revAct) . ' in line ' . __LINE__ | |
370 | ); | |
371 | ||
372 | $this->customFieldDelete($custom_ids['custom_field_id']); | |
373 | $this->customGroupDelete($custom_ids['custom_group_id']); | |
374 | } | |
96025800 | 375 | |
6a488035 | 376 | } |