Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /** | |
d177a2a6 | 3 | * @file |
6a488035 TO |
4 | * File for the TestActivity class |
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 | /** | |
33 | * Include class definitions | |
34 | */ | |
35 | require_once 'CiviTest/CiviUnitTestCase.php'; | |
36 | ||
37 | ||
38 | /** | |
d177a2a6 | 39 | * Test APIv3 civicrm_activity_* functions |
6a488035 | 40 | * |
6c6e6187 TO |
41 | * @package CiviCRM_APIv3 |
42 | * @subpackage API_Activity | |
6a488035 | 43 | */ |
6a488035 TO |
44 | class api_v3_ActivityTest extends CiviUnitTestCase { |
45 | protected $_params; | |
46 | protected $_params2; | |
47 | protected $_entity = 'activity'; | |
48 | protected $_apiversion = 3; | |
49 | protected $test_activity_type_value; | |
b8a3da18 | 50 | protected $_contactID; |
9f1b81e0 | 51 | |
6a488035 | 52 | /** |
d177a2a6 | 53 | * Test setup for every test. |
6a488035 | 54 | * |
d177a2a6 EM |
55 | * Connect to the database, truncate the tables that will be used |
56 | * and redirect stdin to a temporary file | |
6a488035 TO |
57 | */ |
58 | public function setUp() { | |
e3aa0dd1 | 59 | // Connect to the database |
6a488035 | 60 | parent::setUp(); |
6a488035 | 61 | |
b8a3da18 | 62 | $this->_contactID = $this->individualCreate(); |
6a488035 | 63 | //create activity types |
b8a3da18 | 64 | $activityTypes = $this->callAPISuccess('option_value', 'create', array( |
65 | 'option_group_id' => 2, | |
66 | 'name' => 'Test activity type', | |
67 | 'label' => 'Test activity type', | |
21dfd5f5 | 68 | 'sequential' => 1, |
b8a3da18 | 69 | )); |
6a488035 TO |
70 | $this->test_activity_type_value = $activityTypes['values'][0]['value']; |
71 | $this->test_activity_type_id = $activityTypes['id']; | |
72 | $this->_params = array( | |
b8a3da18 | 73 | 'source_contact_id' => $this->_contactID, |
6a488035 TO |
74 | 'activity_type_id' => $this->test_activity_type_value, |
75 | 'subject' => 'test activity type id', | |
76 | 'activity_date_time' => '2011-06-02 14:36:13', | |
77 | 'status_id' => 2, | |
78 | 'priority_id' => 1, | |
79 | 'duration' => 120, | |
e3aa0dd1 | 80 | 'location' => 'Pennsylvania', |
6a488035 | 81 | 'details' => 'a test activity', |
6a488035 TO |
82 | ); |
83 | $this->_params2 = array( | |
b8a3da18 | 84 | 'source_contact_id' => $this->_contactID, |
6a488035 TO |
85 | 'subject' => 'Eat & drink', |
86 | 'activity_date_time' => date('Ymd'), | |
87 | 'duration' => 120, | |
88 | 'location' => 'Napier', | |
89 | 'details' => 'discuss & eat', | |
90 | 'status_id' => 1, | |
91 | 'activity_type_id' => $this->test_activity_type_value, | |
6a488035 TO |
92 | ); |
93 | // create a logged in USER since the code references it for source_contact_id | |
94 | $this->createLoggedInUser(); | |
95 | } | |
96 | ||
97 | /** | |
98 | * Tears down the fixture, for example, closes a network connection. | |
e3aa0dd1 | 99 | * |
6a488035 | 100 | * This method is called after a test is executed. |
6a488035 | 101 | */ |
00be9182 | 102 | public function tearDown() { |
6a488035 TO |
103 | $tablesToTruncate = array( |
104 | 'civicrm_contact', | |
105 | 'civicrm_activity', | |
b319d00a | 106 | 'civicrm_activity_contact', |
6a488035 TO |
107 | ); |
108 | $this->quickCleanup($tablesToTruncate, TRUE); | |
9f1b81e0 | 109 | $this->callAPISuccess('option_value', 'delete', array('id' => $this->test_activity_type_id)); |
6a488035 TO |
110 | } |
111 | ||
112 | /** | |
e3aa0dd1 | 113 | * Check fails with empty array. |
6a488035 | 114 | */ |
00be9182 | 115 | public function testActivityCreateEmpty() { |
e3aa0dd1 | 116 | $this->callAPIFailure('activity', 'create', array()); |
6a488035 TO |
117 | } |
118 | ||
119 | /** | |
eceb18cc | 120 | * Check if required fields are not passed. |
6a488035 | 121 | */ |
00be9182 | 122 | public function testActivityCreateWithoutRequired() { |
6a488035 TO |
123 | $params = array( |
124 | 'subject' => 'this case should fail', | |
125 | 'scheduled_date_time' => date('Ymd'), | |
6a488035 | 126 | ); |
d0e1eff2 | 127 | $result = $this->callAPIFailure('activity', 'create', $params); |
6a488035 TO |
128 | } |
129 | ||
130 | /** | |
d177a2a6 EM |
131 | * Test civicrm_activity_create() with mismatched activity_type_id |
132 | * and activity_name. | |
6a488035 | 133 | */ |
00be9182 | 134 | public function testActivityCreateMismatchNameType() { |
6a488035 | 135 | $params = array( |
b8a3da18 | 136 | 'source_contact_id' => $this->_contactID, |
6a488035 TO |
137 | 'subject' => 'Test activity', |
138 | 'activity_date_time' => date('Ymd'), | |
139 | 'duration' => 120, | |
e3aa0dd1 | 140 | 'location' => 'Pennsylvania', |
6a488035 TO |
141 | 'details' => 'a test activity', |
142 | 'status_id' => 1, | |
143 | 'activity_name' => 'Fubar activity type', | |
144 | 'activity_type_id' => 5, | |
145 | 'scheduled_date_time' => date('Ymd'), | |
6a488035 TO |
146 | ); |
147 | ||
d0e1eff2 | 148 | $result = $this->callAPIFailure('activity', 'create', $params); |
6a488035 TO |
149 | } |
150 | ||
151 | /** | |
d177a2a6 | 152 | * Test civicrm_activity_id() with missing source_contact_id is put with the current user. |
6a488035 | 153 | */ |
00be9182 | 154 | public function testActivityCreateWithMissingContactId() { |
6a488035 TO |
155 | $params = array( |
156 | 'subject' => 'Make-it-Happen Meeting', | |
157 | 'activity_date_time' => date('Ymd'), | |
158 | 'duration' => 120, | |
e3aa0dd1 | 159 | 'location' => 'Pennsylvania', |
6a488035 TO |
160 | 'details' => 'a test activity', |
161 | 'status_id' => 1, | |
162 | 'activity_name' => 'Test activity type', | |
6a488035 TO |
163 | ); |
164 | ||
d177a2a6 | 165 | $this->callAPISuccess('activity', 'create', $params); |
6a488035 TO |
166 | } |
167 | ||
168 | /** | |
d177a2a6 | 169 | * Test civicrm_activity_id() with non-numeric source_contact_id. |
6a488035 | 170 | */ |
00be9182 | 171 | public function testActivityCreateWithNonNumericContactId() { |
6a488035 TO |
172 | $params = array( |
173 | 'source_contact_id' => 'fubar', | |
174 | 'subject' => 'Make-it-Happen Meeting', | |
175 | 'activity_date_time' => date('Ymd'), | |
176 | 'duration' => 120, | |
e3aa0dd1 | 177 | 'location' => 'Pennsylvania', |
6a488035 TO |
178 | 'details' => 'a test activity', |
179 | 'status_id' => 1, | |
180 | 'activity_name' => 'Test activity type', | |
6a488035 TO |
181 | ); |
182 | ||
e3aa0dd1 | 183 | $this->callAPIFailure('activity', 'create', $params); |
6a488035 TO |
184 | } |
185 | ||
6a488035 | 186 | /** |
fe482240 EM |
187 | * Ensure that an invalid activity type causes failure. |
188 | * | |
189 | * Oddly enough this test was failing because the creation of the invalid type | |
feb2a730 | 190 | * got added to the set up routine. Probably a mis-fix on a test |
6a488035 | 191 | */ |
00be9182 | 192 | public function testActivityCreateWithNonNumericActivityTypeId() { |
6a488035 | 193 | $params = array( |
b8a3da18 | 194 | 'source_contact_id' => $this->_contactID, |
6a488035 TO |
195 | 'subject' => 'Make-it-Happen Meeting', |
196 | 'activity_date_time' => date('Ymd'), | |
197 | 'duration' => 120, | |
e3aa0dd1 | 198 | 'location' => 'Pennsylvania', |
6a488035 TO |
199 | 'details' => 'a test activity', |
200 | 'status_id' => 1, | |
feb2a730 | 201 | 'activity_type_id' => 'Invalid Test activity type', |
6a488035 TO |
202 | ); |
203 | ||
d0e1eff2 | 204 | $result = $this->callAPIFailure('activity', 'create', $params); |
6a488035 TO |
205 | } |
206 | ||
207 | /** | |
e3aa0dd1 | 208 | * Check with incorrect required fields. |
6a488035 | 209 | */ |
00be9182 | 210 | public function testActivityCreateWithUnknownActivityTypeId() { |
6a488035 | 211 | $params = array( |
b8a3da18 | 212 | 'source_contact_id' => $this->_contactID, |
6a488035 TO |
213 | 'subject' => 'Make-it-Happen Meeting', |
214 | 'activity_date_time' => date('Ymd'), | |
215 | 'duration' => 120, | |
e3aa0dd1 | 216 | 'location' => 'Pennsylvania', |
6a488035 TO |
217 | 'details' => 'a test activity', |
218 | 'status_id' => 1, | |
219 | 'activity_type_id' => 699, | |
6a488035 TO |
220 | ); |
221 | ||
d0e1eff2 | 222 | $result = $this->callAPIFailure('activity', 'create', $params); |
6a488035 TO |
223 | } |
224 | ||
00be9182 | 225 | public function testActivityCreateWithInvalidPriority() { |
6a488035 | 226 | $params = array( |
b8a3da18 | 227 | 'source_contact_id' => $this->_contactID, |
6a488035 TO |
228 | 'subject' => 'Make-it-Happen Meeting', |
229 | 'activity_date_time' => date('Ymd'), | |
230 | 'duration' => 120, | |
e3aa0dd1 | 231 | 'location' => 'Pennsylvania', |
6a488035 TO |
232 | 'details' => 'a test activity', |
233 | 'status_id' => 1, | |
234 | 'priority_id' => 44, | |
235 | 'activity_type_id' => 1, | |
6a488035 TO |
236 | ); |
237 | ||
b8a3da18 | 238 | $result = $this->callAPIFailure('activity', 'create', $params, |
239 | "'44' is not a valid option for field priority_id"); | |
283f988c | 240 | $this->assertEquals('priority_id', $result['error_field']); |
6a488035 TO |
241 | } |
242 | ||
feb2a730 | 243 | |
e3aa0dd1 | 244 | /** |
245 | * Test create succeeds with valid string for priority. | |
246 | */ | |
00be9182 | 247 | public function testActivityCreateWithValidStringPriority() { |
6a488035 | 248 | $params = array( |
b8a3da18 | 249 | 'source_contact_id' => $this->_contactID, |
6a488035 TO |
250 | 'subject' => 'Make-it-Happen Meeting', |
251 | 'activity_date_time' => date('Ymd'), | |
252 | 'duration' => 120, | |
e3aa0dd1 | 253 | 'location' => 'Pennsylvania', |
6a488035 TO |
254 | 'details' => 'a test activity', |
255 | 'status_id' => 1, | |
256 | 'priority_id' => 'Urgent', | |
257 | 'activity_type_id' => 1, | |
6a488035 TO |
258 | ); |
259 | ||
b8a3da18 | 260 | $result = $this->callAPISuccess('activity', 'create', $params); |
6a488035 TO |
261 | $this->assertEquals(1, $result['values'][$result['id']]['priority_id']); |
262 | } | |
263 | ||
e3aa0dd1 | 264 | /** |
265 | * Test create fails with invalid priority string. | |
266 | */ | |
00be9182 | 267 | public function testActivityCreateWithInValidStringPriority() { |
6a488035 | 268 | $params = array( |
b8a3da18 | 269 | 'source_contact_id' => $this->_contactID, |
6a488035 TO |
270 | 'subject' => 'Make-it-Happen Meeting', |
271 | 'activity_date_time' => date('Ymd'), | |
272 | 'duration' => 120, | |
e3aa0dd1 | 273 | 'location' => 'Pennsylvania', |
6a488035 TO |
274 | 'details' => 'a test activity', |
275 | 'status_id' => 1, | |
276 | 'priority_id' => 'ergUrgent', | |
277 | 'activity_type_id' => 1, | |
6a488035 TO |
278 | ); |
279 | ||
e3aa0dd1 | 280 | $this->callAPIFailure('activity', 'create', $params, |
b8a3da18 | 281 | "'ergUrgent' is not a valid option for field priority_id"); |
6a488035 TO |
282 | } |
283 | ||
284 | /** | |
e3aa0dd1 | 285 | * Test civicrm_activity_create() with valid parameters. |
6a488035 | 286 | */ |
00be9182 | 287 | public function testActivityCreate() { |
6a488035 | 288 | |
e3aa0dd1 | 289 | $this->callAPISuccess('activity', 'create', $this->_params); |
b8a3da18 | 290 | $result = $this->callAPISuccess('activity', 'get', $this->_params); |
e3aa0dd1 | 291 | $this->assertEquals($result['values'][$result['id']]['duration'], 120); |
292 | $this->assertEquals($result['values'][$result['id']]['subject'], 'test activity type id'); | |
293 | $this->assertEquals($result['values'][$result['id']]['activity_date_time'], '2011-06-02 14:36:13'); | |
294 | $this->assertEquals($result['values'][$result['id']]['location'], 'Pennsylvania'); | |
295 | $this->assertEquals($result['values'][$result['id']]['details'], 'a test activity'); | |
296 | $this->assertEquals($result['values'][$result['id']]['status_id'], 2); | |
297 | $this->assertEquals($result['values'][$result['id']]['id'], $result['id']); | |
6a488035 TO |
298 | } |
299 | ||
300 | /** | |
d177a2a6 | 301 | * Test civicrm_activity_create() with valid parameters - use type_id. |
6a488035 | 302 | */ |
00be9182 | 303 | public function testActivityCreateCampaignTypeID() { |
07fd63f5 | 304 | $this->enableCiviCampaign(); |
6a488035 | 305 | |
6a488035 | 306 | $params = array( |
b8a3da18 | 307 | 'source_contact_id' => $this->_contactID, |
6a488035 TO |
308 | 'subject' => 'Make-it-Happen Meeting', |
309 | 'activity_date_time' => '20110316', | |
310 | 'duration' => 120, | |
e3aa0dd1 | 311 | 'location' => 'Pennsylvania', |
6a488035 TO |
312 | 'details' => 'a test activity', |
313 | 'status_id' => 1, | |
314 | 'activity_type_id' => 29, | |
6a488035 TO |
315 | 'priority_id' => 1, |
316 | ); | |
317 | ||
b8a3da18 | 318 | $result = $this->callAPISuccess('activity', 'create', $params); |
6a488035 | 319 | |
2b46f1da | 320 | $result = $this->callAPISuccess('activity', 'get', array('id' => $result['id'])); |
e3aa0dd1 | 321 | $this->assertEquals($result['values'][$result['id']]['duration'], 120); |
322 | $this->assertEquals($result['values'][$result['id']]['subject'], 'Make-it-Happen Meeting'); | |
323 | $this->assertEquals($result['values'][$result['id']]['activity_date_time'], '2011-03-16 00:00:00'); | |
324 | $this->assertEquals($result['values'][$result['id']]['location'], 'Pennsylvania'); | |
325 | $this->assertEquals($result['values'][$result['id']]['details'], 'a test activity'); | |
326 | $this->assertEquals($result['values'][$result['id']]['status_id'], 1); | |
6a488035 TO |
327 | } |
328 | ||
e3aa0dd1 | 329 | /** |
330 | * Test get returns target and assignee contacts. | |
331 | */ | |
00be9182 | 332 | public function testActivityReturnTargetAssignee() { |
6a488035 | 333 | |
5c49fee0 | 334 | $description = "Demonstrates setting & retrieving activity target & source."; |
92915c55 TO |
335 | $subfile = "GetTargetandAssignee"; |
336 | $params = array( | |
b8a3da18 | 337 | 'source_contact_id' => $this->_contactID, |
6a488035 TO |
338 | 'subject' => 'Make-it-Happen Meeting', |
339 | 'activity_date_time' => '20110316', | |
340 | 'duration' => 120, | |
e3aa0dd1 | 341 | 'location' => 'Pennsylvania', |
6a488035 TO |
342 | 'details' => 'a test activity', |
343 | 'status_id' => 1, | |
344 | 'activity_type_id' => 1, | |
6a488035 | 345 | 'priority_id' => 1, |
b8a3da18 | 346 | 'target_contact_id' => $this->_contactID, |
347 | 'assignee_contact_id' => $this->_contactID, | |
6a488035 TO |
348 | ); |
349 | ||
9f1b81e0 | 350 | $result = $this->callAPIAndDocument('activity', 'create', $params, __FUNCTION__, __FILE__, $description, $subfile); |
92915c55 | 351 | $result = $this->callAPISuccess('activity', 'get', array( |
c301f76e | 352 | 'id' => $result['id'], |
353 | 'version' => $this->_apiversion, | |
354 | 'return.assignee_contact_id' => 1, | |
355 | 'return.target_contact_id' => 1, | |
356 | )); | |
6a488035 | 357 | |
e3aa0dd1 | 358 | $this->assertEquals($this->_contactID, $result['values'][$result['id']]['assignee_contact_id'][0]); |
359 | $this->assertEquals($this->_contactID, $result['values'][$result['id']]['target_contact_id'][0]); | |
6a488035 TO |
360 | } |
361 | ||
e3aa0dd1 | 362 | /** |
363 | * Test civicrm_activity_create() using example code. | |
364 | */ | |
00be9182 | 365 | public function testActivityCreateExample() { |
3ec6e38d | 366 | require_once 'api/v3/examples/Activity/Create.php'; |
6a488035 TO |
367 | $result = activity_create_example(); |
368 | $expectedResult = activity_create_expectedresult(); | |
369 | $this->assertEquals($result, $expectedResult); | |
370 | } | |
371 | ||
6a488035 | 372 | /** |
d177a2a6 | 373 | * Test civicrm_activity_create() with valid parameters and custom data. |
6a488035 | 374 | */ |
00be9182 | 375 | public function testActivityCreateCustom() { |
6a488035 TO |
376 | $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, __FILE__); |
377 | $params = $this->_params; | |
378 | $params['custom_' . $ids['custom_field_id']] = "custom string"; | |
9f1b81e0 | 379 | $result = $this->callAPIAndDocument($this->_entity, 'create', $params, __FUNCTION__, __FILE__); |
92915c55 | 380 | $result = $this->callAPISuccess($this->_entity, 'get', array( |
c301f76e | 381 | 'return.custom_' . $ids['custom_field_id'] => 1, |
c301f76e | 382 | 'id' => $result['id'], |
383 | )); | |
6a488035 TO |
384 | $this->assertEquals("custom string", $result['values'][$result['id']]['custom_' . $ids['custom_field_id']], ' in line ' . __LINE__); |
385 | ||
386 | $this->customFieldDelete($ids['custom_field_id']); | |
387 | $this->customGroupDelete($ids['custom_group_id']); | |
388 | } | |
389 | ||
390 | /** | |
d177a2a6 | 391 | * Test civicrm_activity_create() with valid parameters and custom data. |
6a488035 | 392 | */ |
00be9182 | 393 | public function testActivityCreateCustomContactRefField() { |
6a488035 | 394 | |
b8a3da18 | 395 | $this->callAPISuccess('contact', 'create', array('id' => $this->_contactID, 'sort_name' => 'Contact, Test')); |
92915c55 | 396 | $subfile = 'ContactRefCustomField'; |
5c49fee0 | 397 | $description = "Demonstrates create with Contact Reference Custom Field."; |
92915c55 TO |
398 | $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, __FILE__); |
399 | $params = array( | |
6a488035 TO |
400 | 'custom_group_id' => $ids['custom_group_id'], |
401 | 'name' => 'Worker_Lookup', | |
402 | 'label' => 'Worker Lookup', | |
403 | 'html_type' => 'Autocomplete-Select', | |
404 | 'data_type' => 'ContactReference', | |
405 | 'weight' => 4, | |
406 | 'is_searchable' => 1, | |
407 | 'is_active' => 1, | |
6a488035 TO |
408 | ); |
409 | ||
b8a3da18 | 410 | $customField = $this->callAPISuccess('custom_field', 'create', $params); |
6a488035 | 411 | $params = $this->_params; |
b8a3da18 | 412 | $params['custom_' . $customField['id']] = "$this->_contactID"; |
6a488035 | 413 | |
a828d7b8 | 414 | $result = $this->callAPIAndDocument($this->_entity, 'create', $params, __FUNCTION__, __FILE__, $description, $subfile); |
92915c55 | 415 | $result = $this->callAPIAndDocument($this->_entity, 'get', array( |
c301f76e | 416 | 'return.custom_' . $customField['id'] => 1, |
417 | 'id' => $result['id'], | |
a828d7b8 | 418 | ), __FUNCTION__, __FILE__, 'Get with Contact Ref Custom Field', 'ContactRefCustomFieldGet'); |
6a488035 | 419 | |
e3aa0dd1 | 420 | $this->assertEquals('Anderson, Anthony', $result['values'][$result['id']]['custom_' . $customField['id']]); |
b8a3da18 | 421 | $this->assertEquals($this->_contactID, $result['values'][$result['id']]['custom_' . $customField['id'] . "_id"], ' in line ' . __LINE__); |
422 | $this->assertEquals('Anderson, Anthony', $result['values'][$result['id']]['custom_' . $customField['id'] . '_1'], ' in line ' . __LINE__); | |
423 | $this->assertEquals($this->_contactID, $result['values'][$result['id']]['custom_' . $customField['id'] . "_1_id"], ' in line ' . __LINE__); | |
6a488035 TO |
424 | $this->customFieldDelete($ids['custom_field_id']); |
425 | $this->customGroupDelete($ids['custom_group_id']); | |
426 | } | |
427 | ||
428 | /** | |
e3aa0dd1 | 429 | * Test civicrm_activity_create() with an invalid text status_id. |
6a488035 | 430 | */ |
00be9182 | 431 | public function testActivityCreateBadTextStatus() { |
6a488035 TO |
432 | |
433 | $params = array( | |
b8a3da18 | 434 | 'source_contact_id' => $this->_contactID, |
6a488035 TO |
435 | 'subject' => 'Discussion on Apis for v3', |
436 | 'activity_date_time' => date('Ymd'), | |
437 | 'duration' => 120, | |
e3aa0dd1 | 438 | 'location' => 'Pennsylvania', |
6a488035 TO |
439 | 'details' => 'a test activity', |
440 | 'status_id' => 'Invalid', | |
441 | 'activity_name' => 'Test activity type', | |
6a488035 TO |
442 | ); |
443 | ||
e3aa0dd1 | 444 | $this->callAPIFailure('activity', 'create', $params); |
feb2a730 | 445 | } |
446 | ||
447 | /** | |
e3aa0dd1 | 448 | * Test civicrm_activity_create() with an invalid text status_id. |
feb2a730 | 449 | */ |
00be9182 | 450 | public function testActivityCreateSupportActivityStatus() { |
feb2a730 | 451 | |
452 | $params = array( | |
b8a3da18 | 453 | 'source_contact_id' => $this->_contactID, |
feb2a730 | 454 | 'subject' => 'Discussion on Apis for v3', |
455 | 'activity_date_time' => date('Ymd'), | |
456 | 'duration' => 120, | |
e3aa0dd1 | 457 | 'location' => 'Pennsylvania', |
feb2a730 | 458 | 'details' => 'a test activity', |
459 | 'activity_status_id' => 'Invalid', | |
460 | 'activity_name' => 'Test activity type', | |
6a488035 | 461 | ); |
feb2a730 | 462 | |
b8a3da18 | 463 | $result = $this->callAPIFailure('activity', 'create', $params, |
464 | "'Invalid' is not a valid option for field status_id"); | |
6a488035 TO |
465 | } |
466 | ||
feb2a730 | 467 | |
6a488035 | 468 | /** |
e3aa0dd1 | 469 | * Test civicrm_activity_create() with using a text status_id. |
6a488035 | 470 | */ |
00be9182 | 471 | public function testActivityCreateTextStatus() { |
6a488035 | 472 | |
6a488035 | 473 | $params = array( |
b8a3da18 | 474 | 'source_contact_id' => $this->_contactID, |
6a488035 TO |
475 | 'subject' => 'Make-it-Happen Meeting', |
476 | 'activity_date_time' => date('Ymd'), | |
477 | 'duration' => 120, | |
e3aa0dd1 | 478 | 'location' => 'Pennsylvania', |
6a488035 TO |
479 | 'details' => 'a test activity', |
480 | 'status_id' => 'Scheduled', | |
481 | 'activity_name' => 'Test activity type', | |
6a488035 TO |
482 | ); |
483 | ||
b8a3da18 | 484 | $result = $this->callAPISuccess('activity', 'create', $params); |
e3aa0dd1 | 485 | $this->assertEquals($result['values'][$result['id']]['duration'], 120); |
486 | $this->assertEquals($result['values'][$result['id']]['subject'], 'Make-it-Happen Meeting'); | |
487 | $this->assertEquals($result['values'][$result['id']]['activity_date_time'], date('Ymd') . '000000'); | |
488 | $this->assertEquals($result['values'][$result['id']]['location'], 'Pennsylvania'); | |
489 | $this->assertEquals($result['values'][$result['id']]['details'], 'a test activity'); | |
6a488035 TO |
490 | } |
491 | ||
492 | /** | |
d177a2a6 | 493 | * Test civicrm_activity_get() with no params |
6a488035 | 494 | */ |
00be9182 | 495 | public function testActivityGetEmpty() { |
b8a3da18 | 496 | $result = $this->callAPISuccess('activity', 'get', array()); |
6a488035 TO |
497 | } |
498 | ||
499 | /** | |
d177a2a6 | 500 | * Test civicrm_activity_get() with a good activity ID |
6a488035 | 501 | */ |
00be9182 | 502 | public function testActivityGetGoodID1() { |
5c49fee0 | 503 | // Insert rows in civicrm_activity creating activities 4 and 13 |
a828d7b8 | 504 | $description = "Demonstrates getting assignee_contact_id & using it to get the contact."; |
92915c55 TO |
505 | $subfile = 'ReturnAssigneeContact'; |
506 | $activity = $this->callAPISuccess('activity', 'create', $this->_params); | |
6a488035 | 507 | |
b8a3da18 | 508 | $contact = $this->callAPISuccess('Contact', 'Create', array( |
92915c55 TO |
509 | 'first_name' => "The Rock", |
510 | 'last_name' => 'roccky', | |
511 | 'contact_type' => 'Individual', | |
512 | 'version' => 3, | |
513 | 'api.activity.create' => array( | |
514 | 'id' => $activity['id'], | |
515 | 'assignee_contact_id' => '$value.id', | |
516 | ), | |
517 | )); | |
6a488035 TO |
518 | |
519 | $params = array( | |
520 | 'activity_id' => $activity['id'], | |
521 | 'version' => $this->_apiversion, | |
522 | 'sequential' => 1, | |
523 | 'return.assignee_contact_id' => 1, | |
524 | 'api.contact.get' => array( | |
525 | 'id' => '$value.source_contact_id', | |
526 | ), | |
527 | ); | |
528 | ||
9f1b81e0 | 529 | $result = $this->callAPIAndDocument('Activity', 'Get', $params, __FUNCTION__, __FILE__, $description, $subfile); |
6a488035 TO |
530 | |
531 | $this->assertEquals($activity['id'], $result['id'], 'In line ' . __LINE__); | |
6a488035 TO |
532 | |
533 | $this->assertEquals($contact['id'], $result['values'][0]['assignee_contact_id'][0], 'In line ' . __LINE__); | |
534 | ||
b8a3da18 | 535 | $this->assertEquals($this->_contactID, $result['values'][0]['api.contact.get']['values'][0]['contact_id'], 'In line ' . __LINE__); |
6a488035 TO |
536 | $this->assertEquals($this->test_activity_type_value, $result['values'][0]['activity_type_id'], 'In line ' . __LINE__); |
537 | $this->assertEquals("test activity type id", $result['values'][0]['subject'], 'In line ' . __LINE__); | |
538 | } | |
539 | ||
d424ffde | 540 | /** |
eceb18cc | 541 | * test that get functioning does filtering. |
42d30b83 | 542 | */ |
00be9182 | 543 | public function testGetFilter() { |
6a488035 | 544 | $params = array( |
b8a3da18 | 545 | 'source_contact_id' => $this->_contactID, |
6a488035 TO |
546 | 'subject' => 'Make-it-Happen Meeting', |
547 | 'activity_date_time' => '20110316', | |
548 | 'duration' => 120, | |
e3aa0dd1 | 549 | 'location' => 'Pennsylvania', |
6a488035 TO |
550 | 'details' => 'a test activity', |
551 | 'status_id' => 1, | |
552 | 'activity_name' => 'Test activity type', | |
6a488035 TO |
553 | 'priority_id' => 1, |
554 | ); | |
9f1b81e0 | 555 | $result = $this->callAPISuccess('Activity', 'Create', $params); |
556 | $this->callAPISuccess('Activity', 'Get', array('subject' => 'Make-it-Happen Meeting')); | |
6a488035 TO |
557 | $this->assertEquals(1, $result['count']); |
558 | $this->assertEquals('Make-it-Happen Meeting', $result['values'][$result['id']]['subject']); | |
9f1b81e0 | 559 | $this->callAPISuccess('Activity', 'Delete', array('id' => $result['id'])); |
6a488035 | 560 | } |
6e1bb60c N |
561 | |
562 | ||
563 | /** | |
d177a2a6 | 564 | * Test civicrm_activity_get() with filter target_contact_id |
6e1bb60c | 565 | */ |
00be9182 | 566 | public function testActivityGetTargetFilter() { |
6e1bb60c N |
567 | $params = $this->_params; |
568 | $contact1Params = array( | |
569 | 'first_name' => 'John', | |
570 | 'middle_name' => 'J.', | |
571 | 'last_name' => 'Anderson', | |
572 | 'prefix_id' => 3, | |
573 | 'suffix_id' => 3, | |
574 | 'email' => 'john_anderson@civicrm.org', | |
575 | 'contact_type' => 'Individual', | |
576 | ); | |
577 | ||
578 | $contact1 = $this->individualCreate($contact1Params); | |
579 | $contact2Params = array( | |
580 | 'first_name' => 'Michal', | |
581 | 'middle_name' => 'J.', | |
582 | 'last_name' => 'Anderson', | |
583 | 'prefix_id' => 3, | |
584 | 'suffix_id' => 3, | |
585 | 'email' => 'michal_anderson@civicrm.org', | |
586 | 'contact_type' => 'Individual', | |
587 | ); | |
588 | ||
589 | $contact2 = $this->individualCreate($contact2Params); | |
590 | ||
591 | $params['assignee_contact_id'] = array($contact1, $contact2); | |
592 | $params['target_contact_id'] = array($contact2 => $contact2); | |
593 | $activity = $this->callAPISuccess('Activity', 'Create', $params); | |
594 | ||
92915c55 | 595 | $activityget = $this->callAPISuccess('Activity', 'get', array( |
c301f76e | 596 | 'id' => $activity['id'], |
597 | 'target_contact_id' => $contact2, | |
598 | 'return.target_contact_id' => 1, | |
599 | )); | |
6e1bb60c N |
600 | $this->assertEquals($activity['id'], $activityget['id'], 'In line ' . __LINE__); |
601 | $this->assertEquals($contact2, $activityget['values'][$activityget['id']]['target_contact_id'][0], 'In line ' . __LINE__); | |
602 | ||
92915c55 | 603 | $activityget = $this->callAPISuccess('activity', 'get', array( |
c301f76e | 604 | 'target_contact_id' => $this->_contactID, |
605 | 'return.target_contact_id' => 1, | |
606 | 'id' => $activity['id'], | |
607 | )); | |
6e1bb60c N |
608 | if ($activityget['count'] > 0) { |
609 | $this->assertNotEquals($contact2, $activityget['values'][$activityget['id']]['target_contact_id'][0], 'In line ' . __LINE__); | |
610 | } | |
611 | } | |
612 | ||
d424ffde | 613 | /** |
eceb18cc | 614 | * test that get functioning does filtering. |
42d30b83 | 615 | */ |
00be9182 | 616 | public function testGetStatusID() { |
6a488035 | 617 | $params = array( |
b8a3da18 | 618 | 'source_contact_id' => $this->_contactID, |
6a488035 TO |
619 | 'subject' => 'Make-it-Happen Meeting', |
620 | 'activity_date_time' => '20110316', | |
621 | 'duration' => 120, | |
e3aa0dd1 | 622 | 'location' => 'Pennsylvania', |
6a488035 TO |
623 | 'details' => 'a test activity', |
624 | 'status_id' => 1, | |
625 | 'activity_name' => 'Test activity type', | |
6a488035 TO |
626 | 'priority_id' => 1, |
627 | ); | |
b8a3da18 | 628 | $this->callAPISuccess('Activity', 'Create', $params); |
2b46f1da | 629 | $result = $this->callAPISuccess('Activity', 'Get', array('activity_status_id' => '1')); |
6a488035 TO |
630 | $this->assertEquals(1, $result['count'], 'one activity of status 1 should exist'); |
631 | ||
2b46f1da | 632 | $result = $this->callAPISuccess('Activity', 'Get', array('status_id' => '1')); |
6a488035 TO |
633 | $this->assertEquals(1, $result['count'], 'status_id should also work'); |
634 | ||
2b46f1da | 635 | $result = $this->callAPISuccess('Activity', 'Get', array('activity_status_id' => '2')); |
6a488035 | 636 | $this->assertEquals(0, $result['count'], 'No activities of status 1 should exist'); |
b8a3da18 | 637 | $result = $this->callAPISuccess('Activity', 'Get', array( |
92915c55 | 638 | 'version' => $this->_apiversion, |
c301f76e | 639 | 'status_id' => '2', |
92915c55 | 640 | )); |
6a488035 TO |
641 | $this->assertEquals(0, $result['count'], 'No activities of status 1 should exist'); |
642 | ||
42d30b83 | 643 | } |
6a488035 | 644 | |
d424ffde | 645 | /** |
eceb18cc | 646 | * test that get functioning does filtering. |
42d30b83 | 647 | */ |
00be9182 | 648 | public function testGetFilterMaxDate() { |
6a488035 | 649 | $params = array( |
b8a3da18 | 650 | 'source_contact_id' => $this->_contactID, |
6a488035 TO |
651 | 'subject' => 'Make-it-Happen Meeting', |
652 | 'activity_date_time' => '20110101', | |
653 | 'duration' => 120, | |
e3aa0dd1 | 654 | 'location' => 'Pennsylvania', |
6a488035 TO |
655 | 'details' => 'a test activity', |
656 | 'status_id' => 1, | |
657 | 'activity_name' => 'Test activity type', | |
658 | 'version' => $this->_apiversion, | |
659 | 'priority_id' => 1, | |
660 | ); | |
b8a3da18 | 661 | $activityOne = $this->callAPISuccess('Activity', 'Create', $params); |
6a488035 | 662 | $params['activity_date_time'] = 20120216; |
b8a3da18 | 663 | $activityTwo = $this->callAPISuccess('Activity', 'Create', $params); |
664 | $result = $this->callAPISuccess('Activity', 'Get', array( | |
92915c55 TO |
665 | 'version' => 3, |
666 | )); | |
5c49fee0 | 667 | $description = "Demonstrates _low filter (at time of writing doesn't work if contact_id is set."; |
6a488035 TO |
668 | $subfile = "DateTimeLow"; |
669 | $this->assertEquals(2, $result['count']); | |
670 | $params = array( | |
671 | 'version' => 3, | |
672 | 'filter.activity_date_time_low' => '20120101000000', | |
673 | 'sequential' => 1, | |
674 | ); | |
9f1b81e0 | 675 | $result = $this->callAPIAndDocument('Activity', 'Get', $params, __FUNCTION__, __FILE__, $description, $subfile); |
e3aa0dd1 | 676 | $this->assertEquals(1, $result['count']); |
5c49fee0 | 677 | $description = "Demonstrates _high filter (at time of writing doesn't work if contact_id is set."; |
6a488035 | 678 | $subfile = "DateTimeHigh"; |
e3aa0dd1 | 679 | $this->assertEquals('2012-02-16 00:00:00', $result['values'][0]['activity_date_time']); |
6a488035 | 680 | $params = array( |
b8a3da18 | 681 | 'source_contact_id' => $this->_contactID, |
6a488035 TO |
682 | 'version' => 3, |
683 | 'filter.activity_date_time_high' => '20120101000000', | |
684 | 'sequential' => 1, | |
685 | ); | |
9f1b81e0 | 686 | $result = $this->callAPIAndDocument('Activity', 'Get', $params, __FUNCTION__, __FILE__, $description, $subfile); |
6a488035 TO |
687 | |
688 | $this->assertEquals(1, $result['count']); | |
e3aa0dd1 | 689 | $this->assertEquals('2011-01-01 00:00:00', $result['values'][0]['activity_date_time']); |
6a488035 | 690 | |
b8a3da18 | 691 | $this->callAPISuccess('Activity', 'Delete', array('version' => 3, 'id' => $activityOne['id'])); |
692 | $this->callAPISuccess('Activity', 'Delete', array('version' => 3, 'id' => $activityTwo['id'])); | |
6a488035 TO |
693 | } |
694 | ||
695 | /** | |
d177a2a6 EM |
696 | * Test civicrm_activity_get() with a good activity ID which |
697 | * has associated custom data | |
6a488035 | 698 | */ |
00be9182 | 699 | public function testActivityGetGoodIDCustom() { |
6a488035 TO |
700 | $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, __FILE__); |
701 | ||
702 | $params = $this->_params; | |
703 | $params['custom_' . $ids['custom_field_id']] = "custom string"; | |
704 | ||
e3aa0dd1 | 705 | $this->callAPISuccess($this->_entity, 'create', $params); |
9f1b81e0 | 706 | |
e3aa0dd1 | 707 | // Retrieve the test value. |
6a488035 TO |
708 | $params = array( |
709 | 'activity_type_id' => $this->test_activity_type_value, | |
6a488035 TO |
710 | 'sequential' => 1, |
711 | 'return.custom_' . $ids['custom_field_id'] => 1, | |
712 | ); | |
9f1b81e0 | 713 | $result = $this->callAPIAndDocument('activity', 'get', $params, __FUNCTION__, __FILE__); |
714 | $this->assertEquals("custom string", $result['values'][0]['custom_' . $ids['custom_field_id']]); | |
6a488035 | 715 | |
9f1b81e0 | 716 | $this->assertEquals($this->test_activity_type_value, $result['values'][0]['activity_type_id']); |
6a488035 TO |
717 | $this->assertEquals('test activity type id', $result['values'][0]['subject'], 'In line ' . __LINE__); |
718 | $this->customFieldDelete($ids['custom_field_id']); | |
719 | $this->customGroupDelete($ids['custom_group_id']); | |
720 | } | |
721 | ||
722 | /** | |
d177a2a6 EM |
723 | * Test civicrm_activity_get() with a good activity ID which |
724 | * has associated custom data | |
6a488035 | 725 | */ |
00be9182 | 726 | public function testActivityGetContact_idCustom() { |
6a488035 TO |
727 | $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, __FILE__); |
728 | ||
729 | $params = $this->_params; | |
730 | $params['custom_' . $ids['custom_field_id']] = "custom string"; | |
731 | ||
b8a3da18 | 732 | $result = $this->callAPISuccess($this->_entity, 'create', $params); |
e3aa0dd1 | 733 | // Retrieve the test value |
6a488035 TO |
734 | $params = array( |
735 | 'contact_id' => $this->_params['source_contact_id'], | |
736 | 'activity_type_id' => $this->test_activity_type_value, | |
6a488035 TO |
737 | 'sequential' => 1, |
738 | 'return.custom_' . $ids['custom_field_id'] => 1, | |
739 | ); | |
9f1b81e0 | 740 | $result = $this->callAPIAndDocument('activity', 'get', $params, __FUNCTION__, __FILE__); |
6a488035 TO |
741 | $this->assertEquals("custom string", $result['values'][0]['custom_' . $ids['custom_field_id']], ' in line ' . __LINE__); |
742 | ||
6a488035 TO |
743 | $this->assertEquals($this->test_activity_type_value, $result['values'][0]['activity_type_id'], 'In line ' . __LINE__); |
744 | $this->assertEquals('test activity type id', $result['values'][0]['subject'], 'In line ' . __LINE__); | |
e3aa0dd1 | 745 | $this->assertEquals($result['values'][0]['id'], $result['id']); |
6a488035 TO |
746 | } |
747 | ||
748 | /** | |
e3aa0dd1 | 749 | * Check activity deletion with empty params. |
6a488035 | 750 | */ |
00be9182 | 751 | public function testDeleteActivityForEmptyParams() { |
6a488035 | 752 | $params = array('version' => $this->_apiversion); |
e3aa0dd1 | 753 | $this->callAPIFailure('activity', 'delete', $params); |
6a488035 TO |
754 | } |
755 | ||
756 | /** | |
e3aa0dd1 | 757 | * Check activity deletion without activity id. |
6a488035 | 758 | */ |
00be9182 | 759 | public function testDeleteActivityWithoutId() { |
6a488035 TO |
760 | $params = array( |
761 | 'activity_name' => 'Meeting', | |
762 | 'version' => $this->_apiversion, | |
763 | ); | |
d0e1eff2 | 764 | $result = $this->callAPIFailure('activity', 'delete', $params); |
6a488035 TO |
765 | } |
766 | ||
767 | /** | |
eceb18cc | 768 | * Check activity deletion without activity type. |
6a488035 | 769 | */ |
00be9182 | 770 | public function testDeleteActivityWithoutActivityType() { |
6a488035 | 771 | $params = array('id' => 1); |
d0e1eff2 | 772 | $result = $this->callAPIFailure('activity', 'delete', $params); |
6a488035 TO |
773 | } |
774 | ||
775 | /** | |
eceb18cc | 776 | * Check activity deletion with incorrect data. |
6a488035 | 777 | */ |
00be9182 | 778 | public function testDeleteActivityWithIncorrectActivityType() { |
6a488035 TO |
779 | $params = array( |
780 | 'id' => 1, | |
781 | 'activity_name' => 'Test Activity', | |
782 | ); | |
783 | ||
d0e1eff2 | 784 | $result = $this->callAPIFailure('activity', 'delete', $params); |
6a488035 TO |
785 | } |
786 | ||
787 | /** | |
eceb18cc | 788 | * Check activity deletion with correct data. |
6a488035 | 789 | */ |
00be9182 | 790 | public function testDeleteActivity() { |
b8a3da18 | 791 | $result = $this->callAPISuccess('activity', 'create', $this->_params); |
6a488035 TO |
792 | $params = array( |
793 | 'id' => $result['id'], | |
794 | 'version' => $this->_apiversion, | |
795 | ); | |
796 | ||
a828d7b8 | 797 | $this->callAPIAndDocument('activity', 'delete', $params, __FUNCTION__, __FILE__); |
6a488035 TO |
798 | } |
799 | ||
6a488035 | 800 | /** |
eceb18cc | 801 | * Check if required fields are not passed. |
6a488035 | 802 | */ |
00be9182 | 803 | public function testActivityUpdateWithoutRequired() { |
6a488035 TO |
804 | $params = array( |
805 | 'subject' => 'this case should fail', | |
806 | 'scheduled_date_time' => date('Ymd'), | |
807 | ); | |
808 | ||
d0e1eff2 | 809 | $result = $this->callAPIFailure('activity', 'create', $params); |
6a488035 TO |
810 | } |
811 | ||
812 | /** | |
813 | * Test civicrm_activity_update() with non-numeric id | |
814 | */ | |
00be9182 | 815 | public function testActivityUpdateWithNonNumericId() { |
6a488035 TO |
816 | $params = array( |
817 | 'id' => 'lets break it', | |
818 | 'activity_name' => 'Meeting', | |
819 | 'subject' => 'this case should fail', | |
820 | 'scheduled_date_time' => date('Ymd'), | |
821 | ); | |
822 | ||
d0e1eff2 | 823 | $result = $this->callAPIFailure('activity', 'create', $params); |
6a488035 TO |
824 | } |
825 | ||
826 | /** | |
eceb18cc | 827 | * Check with incorrect required fields. |
6a488035 | 828 | */ |
00be9182 | 829 | public function testActivityUpdateWithIncorrectContactActivityType() { |
6a488035 TO |
830 | $params = array( |
831 | 'id' => 1, | |
832 | 'activity_name' => 'Test Activity', | |
833 | 'subject' => 'this case should fail', | |
834 | 'scheduled_date_time' => date('Ymd'), | |
b8a3da18 | 835 | 'source_contact_id' => $this->_contactID, |
6a488035 TO |
836 | ); |
837 | ||
b8a3da18 | 838 | $result = $this->callAPIFailure('activity', 'create', $params, |
839 | 'Invalid Activity Id'); | |
6a488035 TO |
840 | } |
841 | ||
842 | /** | |
d177a2a6 | 843 | * Test civicrm_activity_update() to update an existing activity |
6a488035 | 844 | */ |
00be9182 | 845 | public function testActivityUpdate() { |
b8a3da18 | 846 | $result = $this->callAPISuccess('activity', 'create', $this->_params); |
6a488035 TO |
847 | |
848 | $params = array( | |
849 | 'id' => $result['id'], | |
850 | 'subject' => 'Make-it-Happen Meeting', | |
851 | 'activity_date_time' => '20091011123456', | |
852 | 'duration' => 120, | |
853 | 'location' => '21, Park Avenue', | |
854 | 'details' => 'Lets update Meeting', | |
855 | 'status_id' => 1, | |
b8a3da18 | 856 | 'source_contact_id' => $this->_contactID, |
6a488035 | 857 | 'priority_id' => 1, |
6a488035 TO |
858 | ); |
859 | ||
b8a3da18 | 860 | $result = $this->callAPISuccess('activity', 'create', $params); |
6a488035 TO |
861 | //hack on date comparison - really we should make getAndCheck smarter to handle dates |
862 | $params['activity_date_time'] = '2009-10-11 12:34:56'; | |
42d30b83 DL |
863 | // we also unset source_contact_id since it is stored in an aux table |
864 | unset($params['source_contact_id']); | |
6a488035 TO |
865 | $this->getAndCheck($params, $result['id'], 'activity'); |
866 | } | |
867 | ||
868 | /** | |
d177a2a6 EM |
869 | * Test civicrm_activity_update() with valid parameters |
870 | * and some custom data | |
6a488035 | 871 | */ |
00be9182 | 872 | public function testActivityUpdateCustom() { |
6a488035 TO |
873 | $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, __FILE__); |
874 | ||
875 | $params = $this->_params; | |
876 | ||
e3aa0dd1 | 877 | // Create an activity with custom data |
6a488035 TO |
878 | //this has been updated from the previous 'old format' function - need to make it work |
879 | $params = array( | |
b8a3da18 | 880 | 'source_contact_id' => $this->_contactID, |
6a488035 TO |
881 | 'subject' => 'Make-it-Happen Meeting', |
882 | 'activity_date_time' => '2009-10-18', | |
883 | 'duration' => 120, | |
e3aa0dd1 | 884 | 'location' => 'Pennsylvania', |
6a488035 TO |
885 | 'details' => 'a test activity to check the update api', |
886 | 'status_id' => 1, | |
887 | 'activity_name' => 'Test activity type', | |
888 | 'version' => $this->_apiversion, | |
889 | 'custom_' . $ids['custom_field_id'] => 'custom string', | |
890 | ); | |
b8a3da18 | 891 | $result = $this->callAPISuccess('activity', 'create', $params); |
6a488035 TO |
892 | |
893 | $activityId = $result['id']; | |
92915c55 | 894 | $result = $this->callAPISuccess($this->_entity, 'get', array( |
c301f76e | 895 | 'return.custom_' . $ids['custom_field_id'] => 1, |
896 | 'version' => 3, | |
897 | 'id' => $result['id'], | |
898 | )); | |
e3aa0dd1 | 899 | $this->assertEquals("custom string", $result['values'][$result['id']]['custom_' . $ids['custom_field_id']]); |
900 | $this->assertEquals("2009-10-18 00:00:00", $result['values'][$result['id']]['activity_date_time']); | |
b8a3da18 | 901 | $fields = $this->callAPISuccess('activity', 'getfields', array('version' => $this->_apiversion)); |
6a488035 TO |
902 | $this->assertTrue(is_array($fields['values']['custom_' . $ids['custom_field_id']])); |
903 | ||
e3aa0dd1 | 904 | // Update the activity with custom data. |
6a488035 TO |
905 | $params = array( |
906 | 'id' => $activityId, | |
b8a3da18 | 907 | 'source_contact_id' => $this->_contactID, |
6a488035 TO |
908 | 'subject' => 'Make-it-Happen Meeting', |
909 | 'status_id' => 1, | |
910 | 'activity_name' => 'Test activity type', | |
911 | // add this since dates are messed up | |
912 | 'activity_date_time' => date('Ymd'), | |
913 | 'custom_' . $ids['custom_field_id'] => 'Updated my test data', | |
914 | 'version' => $this->_apiversion, | |
915 | ); | |
b8a3da18 | 916 | $result = $this->callAPISuccess('Activity', 'Create', $params); |
6a488035 | 917 | |
92915c55 | 918 | $result = $this->callAPISuccess($this->_entity, 'get', array( |
c301f76e | 919 | 'return.custom_' . $ids['custom_field_id'] => 1, |
920 | 'version' => 3, | |
921 | 'id' => $result['id'], | |
922 | )); | |
e3aa0dd1 | 923 | $this->assertEquals("Updated my test data", $result['values'][$result['id']]['custom_' . $ids['custom_field_id']]); |
6a488035 TO |
924 | } |
925 | ||
926 | /** | |
d177a2a6 EM |
927 | * Test civicrm_activity_update() for core activity fields |
928 | * and some custom data | |
6a488035 | 929 | */ |
00be9182 | 930 | public function testActivityUpdateCheckCoreFields() { |
6a488035 TO |
931 | $params = $this->_params; |
932 | $contact1Params = array( | |
933 | 'first_name' => 'John', | |
934 | 'middle_name' => 'J.', | |
935 | 'last_name' => 'Anderson', | |
936 | 'prefix_id' => 3, | |
937 | 'suffix_id' => 3, | |
938 | 'email' => 'john_anderson@civicrm.org', | |
939 | 'contact_type' => 'Individual', | |
940 | ); | |
941 | ||
942 | $contact1 = $this->individualCreate($contact1Params); | |
943 | $contact2Params = array( | |
944 | 'first_name' => 'Michal', | |
945 | 'middle_name' => 'J.', | |
946 | 'last_name' => 'Anderson', | |
947 | 'prefix_id' => 3, | |
948 | 'suffix_id' => 3, | |
949 | 'email' => 'michal_anderson@civicrm.org', | |
950 | 'contact_type' => 'Individual', | |
951 | ); | |
952 | ||
953 | $contact2 = $this->individualCreate($contact2Params); | |
954 | ||
955 | $params['assignee_contact_id'] = array($contact1, $contact2); | |
956 | $params['target_contact_id'] = array($contact2 => $contact2); | |
b8a3da18 | 957 | $result = $this->callAPISuccess('Activity', 'Create', $params); |
6a488035 | 958 | |
6a488035 | 959 | $activityId = $result['id']; |
6a488035 TO |
960 | $getParams = array( |
961 | 'return.assignee_contact_id' => 1, | |
962 | 'return.target_contact_id' => 1, | |
963 | 'version' => $this->_apiversion, | |
964 | 'id' => $activityId, | |
965 | ); | |
481a74f4 | 966 | $result = $this->callAPISuccess($this->_entity, 'get', $getParams); |
6a488035 TO |
967 | $assignee = $result['values'][$result['id']]['assignee_contact_id']; |
968 | $target = $result['values'][$result['id']]['target_contact_id']; | |
969 | $this->assertEquals(2, count($assignee), ' in line ' . __LINE__); | |
970 | $this->assertEquals(1, count($target), ' in line ' . __LINE__); | |
971 | $this->assertEquals(TRUE, in_array($contact1, $assignee), ' in line ' . __LINE__); | |
972 | $this->assertEquals(TRUE, in_array($contact2, $target), ' in line ' . __LINE__); | |
973 | ||
974 | $contact3Params = array( | |
975 | 'first_name' => 'Jijo', | |
976 | 'middle_name' => 'J.', | |
977 | 'last_name' => 'Anderson', | |
978 | 'prefix_id' => 3, | |
979 | 'suffix_id' => 3, | |
980 | 'email' => 'jijo_anderson@civicrm.org', | |
981 | 'contact_type' => 'Individual', | |
982 | ); | |
983 | ||
984 | $contact4Params = array( | |
985 | 'first_name' => 'Grant', | |
986 | 'middle_name' => 'J.', | |
987 | 'last_name' => 'Anderson', | |
988 | 'prefix_id' => 3, | |
989 | 'suffix_id' => 3, | |
990 | 'email' => 'grant_anderson@civicrm.org', | |
991 | 'contact_type' => 'Individual', | |
992 | ); | |
993 | ||
994 | $contact3 = $this->individualCreate($contact3Params); | |
995 | $contact4 = $this->individualCreate($contact4Params); | |
996 | ||
997 | $params = array(); | |
998 | $params['id'] = $activityId; | |
6a488035 TO |
999 | $params['assignee_contact_id'] = array($contact3 => $contact3); |
1000 | $params['target_contact_id'] = array($contact4 => $contact4); | |
1001 | ||
b8a3da18 | 1002 | $result = $this->callAPISuccess('activity', 'create', $params); |
6a488035 TO |
1003 | |
1004 | $this->assertEquals($activityId, $result['id'], ' in line ' . __LINE__); | |
1005 | ||
b8a3da18 | 1006 | $result = $this->callAPISuccess( |
42d30b83 DL |
1007 | $this->_entity, |
1008 | 'get', | |
1009 | array( | |
1010 | 'return.assignee_contact_id' => 1, | |
1011 | 'return.target_contact_id' => 1, | |
1012 | 'return.source_contact_id' => 1, | |
21dfd5f5 | 1013 | 'id' => $result['id'], |
42d30b83 DL |
1014 | ) |
1015 | ); | |
6a488035 TO |
1016 | |
1017 | $assignee = $result['values'][$result['id']]['assignee_contact_id']; | |
1018 | $target = $result['values'][$result['id']]['target_contact_id']; | |
1019 | ||
1020 | $this->assertEquals(1, count($assignee), ' in line ' . __LINE__); | |
1021 | $this->assertEquals(1, count($target), ' in line ' . __LINE__); | |
1022 | $this->assertEquals(TRUE, in_array($contact3, $assignee), ' in line ' . __LINE__); | |
1023 | $this->assertEquals(TRUE, in_array($contact4, $target), ' in line ' . __LINE__); | |
1024 | ||
1025 | foreach ($this->_params as $fld => $val) { | |
9f1b81e0 | 1026 | $this->assertEquals($val, $result['values'][$result['id']][$fld]); |
6a488035 TO |
1027 | } |
1028 | } | |
1029 | ||
1030 | /** | |
d177a2a6 EM |
1031 | * Test civicrm_activity_update() where the DB has a date_time |
1032 | * value and there is none in the update params. | |
6a488035 | 1033 | */ |
00be9182 | 1034 | public function testActivityUpdateNotDate() { |
b8a3da18 | 1035 | $result = $this->callAPISuccess('activity', 'create', $this->_params); |
6a488035 TO |
1036 | |
1037 | $params = array( | |
1038 | 'id' => $result['id'], | |
1039 | 'subject' => 'Make-it-Happen Meeting', | |
1040 | 'duration' => 120, | |
1041 | 'location' => '21, Park Avenue', | |
1042 | 'details' => 'Lets update Meeting', | |
1043 | 'status_id' => 1, | |
b8a3da18 | 1044 | 'source_contact_id' => $this->_contactID, |
6a488035 | 1045 | 'priority_id' => 1, |
6a488035 TO |
1046 | ); |
1047 | ||
b8a3da18 | 1048 | $result = $this->callAPISuccess('activity', 'create', $params); |
6a488035 TO |
1049 | //hack on date comparison - really we should make getAndCheck smarter to handle dates |
1050 | $params['activity_date_time'] = $this->_params['activity_date_time']; | |
42d30b83 DL |
1051 | // we also unset source_contact_id since it is stored in an aux table |
1052 | unset($params['source_contact_id']); | |
6a488035 TO |
1053 | $this->getAndCheck($params, $result['id'], 'activity'); |
1054 | } | |
1055 | ||
1056 | /** | |
eceb18cc | 1057 | * Check activity update with status. |
6a488035 | 1058 | */ |
00be9182 | 1059 | public function testActivityUpdateWithStatus() { |
b8a3da18 | 1060 | $activity = $this->callAPISuccess('activity', 'create', $this->_params); |
6a488035 TO |
1061 | $params = array( |
1062 | 'id' => $activity['id'], | |
b8a3da18 | 1063 | 'source_contact_id' => $this->_contactID, |
6a488035 TO |
1064 | 'subject' => 'Hurry update works', |
1065 | 'status_id' => 1, | |
1066 | 'activity_name' => 'Test activity type', | |
6a488035 TO |
1067 | ); |
1068 | ||
b8a3da18 | 1069 | $result = $this->callAPISuccess('activity', 'create', $params); |
9f1b81e0 | 1070 | $this->assertEquals($result['id'], $activity['id']); |
1071 | $this->assertEquals($result['values'][$activity['id']]['subject'], 'Hurry update works'); | |
1072 | $this->assertEquals($result['values'][$activity['id']]['status_id'], 1 | |
6a488035 TO |
1073 | ); |
1074 | } | |
1075 | ||
1076 | /** | |
d177a2a6 EM |
1077 | * Test civicrm_activity_update() where the source_contact_id |
1078 | * is not in the update params. | |
6a488035 | 1079 | */ |
00be9182 | 1080 | public function testActivityUpdateKeepSource() { |
b8a3da18 | 1081 | $activity = $this->callAPISuccess('activity', 'create', $this->_params); |
e3aa0dd1 | 1082 | // Updating the activity but not providing anything for the source contact |
1083 | // (It was set as $this->_contactID earlier.) | |
6a488035 TO |
1084 | $params = array( |
1085 | 'id' => $activity['id'], | |
1086 | 'subject' => 'Updated Make-it-Happen Meeting', | |
1087 | 'duration' => 120, | |
1088 | 'location' => '21, Park Avenue', | |
1089 | 'details' => 'Lets update Meeting', | |
1090 | 'status_id' => 1, | |
1091 | 'activity_name' => 'Test activity type', | |
1092 | 'priority_id' => 1, | |
6a488035 TO |
1093 | ); |
1094 | ||
b8a3da18 | 1095 | $result = $this->callAPISuccess('activity', 'create', $params); |
9f1b81e0 | 1096 | $findactivity = $this->callAPISuccess('Activity', 'Get', array('id' => $activity['id'])); |
6a488035 TO |
1097 | } |
1098 | ||
1099 | /** | |
d177a2a6 | 1100 | * Test civicrm_activities_contact_get() |
6a488035 | 1101 | */ |
00be9182 | 1102 | public function testActivitiesContactGet() { |
b8a3da18 | 1103 | $activity = $this->callAPISuccess('activity', 'create', $this->_params); |
1104 | $activity2 = $this->callAPISuccess('activity', 'create', $this->_params2); | |
e3aa0dd1 | 1105 | // Get activities associated with contact $this->_contactID. |
6a488035 | 1106 | $params = array( |
b8a3da18 | 1107 | 'contact_id' => $this->_contactID, |
6a488035 | 1108 | ); |
b8a3da18 | 1109 | $result = $this->callAPISuccess('activity', 'get', $params); |
6a488035 | 1110 | |
6a488035 | 1111 | $this->assertEquals(2, $result['count'], 'In line ' . __LINE__); |
e3aa0dd1 | 1112 | $this->assertEquals($this->test_activity_type_value, $result['values'][$activity['id']]['activity_type_id']); |
1113 | $this->assertEquals('Test activity type', $result['values'][$activity['id']]['activity_name']); | |
1114 | $this->assertEquals('Test activity type', $result['values'][$activity2['id']]['activity_name']); | |
6a488035 | 1115 | } |
92915c55 | 1116 | |
d424ffde | 1117 | /** |
e3aa0dd1 | 1118 | * Test chained Activity format. |
42d30b83 | 1119 | */ |
e3aa0dd1 | 1120 | public function testChainedActivityGet() { |
6a488035 | 1121 | |
b8a3da18 | 1122 | $activity = $this->callAPISuccess('Contact', 'Create', array( |
92915c55 TO |
1123 | 'display_name' => "bob brown", |
1124 | 'contact_type' => 'Individual', | |
1125 | 'api.activity_type.create' => array( | |
1126 | 'weight' => '2', | |
1127 | 'label' => 'send out letters', | |
1128 | 'filter' => 0, | |
1129 | 'is_active' => 1, | |
1130 | 'is_optgroup' => 1, | |
1131 | 'is_default' => 0, | |
1132 | ), | |
1133 | 'api.activity.create' => array( | |
1134 | 'subject' => 'send letter', | |
c301f76e | 1135 | 'activity_type_id' => '$value.api.activity_type.create.values.0.value', |
92915c55 TO |
1136 | ), |
1137 | )); | |
6a488035 | 1138 | |
b8a3da18 | 1139 | $result = $this->callAPISuccess('Activity', 'Get', array( |
92915c55 TO |
1140 | 'id' => $activity['id'], |
1141 | 'return.assignee_contact_id' => 1, | |
1142 | 'api.contact.get' => array('api.pledge.get' => 1), | |
1143 | )); | |
6a488035 TO |
1144 | } |
1145 | ||
6a488035 | 1146 | /** |
e3aa0dd1 | 1147 | * Test civicrm_activity_contact_get() with invalid Contact ID. |
6a488035 | 1148 | */ |
00be9182 | 1149 | public function testActivitiesContactGetWithInvalidContactId() { |
6a488035 | 1150 | $params = array('contact_id' => 'contact'); |
e3aa0dd1 | 1151 | $this->callAPIFailure('activity', 'get', $params); |
6a488035 TO |
1152 | } |
1153 | ||
1154 | /** | |
e3aa0dd1 | 1155 | * Test civicrm_activity_contact_get() with contact having no Activity. |
6a488035 | 1156 | */ |
00be9182 | 1157 | public function testActivitiesContactGetHavingNoActivity() { |
6a488035 TO |
1158 | $params = array( |
1159 | 'first_name' => 'dan', | |
1160 | 'last_name' => 'conberg', | |
1161 | 'email' => 'dan.conberg@w.co.in', | |
1162 | 'contact_type' => 'Individual', | |
6a488035 TO |
1163 | ); |
1164 | ||
b8a3da18 | 1165 | $contact = $this->callAPISuccess('contact', 'create', $params); |
6a488035 TO |
1166 | $params = array( |
1167 | 'contact_id' => $contact['id'], | |
6a488035 | 1168 | ); |
b8a3da18 | 1169 | $result = $this->callAPISuccess('activity', 'get', $params); |
e3aa0dd1 | 1170 | $this->assertEquals($result['count'], 0); |
6a488035 TO |
1171 | } |
1172 | ||
e3aa0dd1 | 1173 | /** |
1174 | * Test getfields function. | |
1175 | */ | |
00be9182 | 1176 | public function testGetFields() { |
b8a3da18 | 1177 | $params = array('action' => 'create'); |
a828d7b8 | 1178 | $result = $this->callAPIAndDocument('activity', 'getfields', $params, __FUNCTION__, __FILE__, NULL, NULL); |
e3aa0dd1 | 1179 | $this->assertTrue(is_array($result['values']), 'get fields doesn\'t return values array'); |
6a488035 | 1180 | foreach ($result['values'] as $key => $value) { |
e3aa0dd1 | 1181 | $this->assertTrue(is_array($value), $key . " is not an array"); |
6a488035 TO |
1182 | } |
1183 | } | |
96025800 | 1184 | |
6a488035 | 1185 | } |