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