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