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