3 * File for the TestActivity class
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 $
14 * This file is part of CiviCRM
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.
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.
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/>.
32 * Include class definitions
34 require_once 'CiviTest/CiviUnitTestCase.php';
38 * Test APIv3 civicrm_activity_* functions
40 * @package CiviCRM_APIv3
41 * @subpackage API_Activity
44 class api_v3_ActivityTest
extends CiviUnitTestCase
{
47 protected $_entity = 'activity';
48 protected $_apiversion = 3;
49 protected $test_activity_type_value;
50 protected $_contactID;
53 * Test setup for every test
55 * Connect to the database, truncate the tables that will be used
56 * and redirect stdin to a temporary file
58 public function setUp() {
59 // Connect to the database
62 $this->_contactID
= $this->individualCreate();
63 //create activity types
64 $activityTypes = $this->callAPISuccess('option_value', 'create', array(
65 'option_group_id' => 2,
66 'name' => 'Test activity type',
67 'label' => 'Test activity type',
70 $this->test_activity_type_value
= $activityTypes['values'][0]['value'];
71 $this->test_activity_type_id
= $activityTypes['id'];
72 $this->_params
= array(
73 'source_contact_id' => $this->_contactID
,
74 'activity_type_id' => $this->test_activity_type_value
,
75 'subject' => 'test activity type id',
76 'activity_date_time' => '2011-06-02 14:36:13',
80 'location' => 'Pensulvania',
81 'details' => 'a test activity',
83 $this->_params2
= array(
84 'source_contact_id' => $this->_contactID
,
85 'subject' => 'Eat & drink',
86 'activity_date_time' => date('Ymd'),
88 'location' => 'Napier',
89 'details' => 'discuss & eat',
91 'activity_type_id' => $this->test_activity_type_value
,
93 // create a logged in USER since the code references it for source_contact_id
94 $this->createLoggedInUser();
98 * Tears down the fixture, for example, closes a network connection.
99 * This method is called after a test is executed.
103 function tearDown() {
104 $tablesToTruncate = array(
107 'civicrm_activity_contact',
109 $this->quickCleanup($tablesToTruncate, TRUE);
110 $this->callAPISuccess('option_value', 'delete', array('id' => $this->test_activity_type_id
));
114 * check with empty array
116 function testActivityCreateEmpty() {
117 $result = $this->callAPIFailure('activity', 'create', array());
121 * check if required fields are not passed
123 function testActivityCreateWithoutRequired() {
125 'subject' => 'this case should fail',
126 'scheduled_date_time' => date('Ymd'),
128 $result = $this->callAPIFailure('activity', 'create', $params);
132 * Test civicrm_activity_create() with mismatched activity_type_id
135 function testActivityCreateMismatchNameType() {
137 'source_contact_id' => $this->_contactID
,
138 'subject' => 'Test activity',
139 'activity_date_time' => date('Ymd'),
141 'location' => 'Pensulvania',
142 'details' => 'a test activity',
144 'activity_name' => 'Fubar activity type',
145 'activity_type_id' => 5,
146 'scheduled_date_time' => date('Ymd'),
149 $result = $this->callAPIFailure('activity', 'create', $params);
153 * Test civicrm_activity_id() with missing source_contact_id is put with the current user.
155 function testActivityCreateWithMissingContactId() {
157 'subject' => 'Make-it-Happen Meeting',
158 'activity_date_time' => date('Ymd'),
160 'location' => 'Pensulvania',
161 'details' => 'a test activity',
163 'activity_name' => 'Test activity type',
166 $result = $this->callAPISuccess('activity', 'create', $params);
170 * Test civicrm_activity_id() with non-numeric source_contact_id
172 function testActivityCreateWithNonNumericContactId() {
174 'source_contact_id' => 'fubar',
175 'subject' => 'Make-it-Happen Meeting',
176 'activity_date_time' => date('Ymd'),
178 'location' => 'Pensulvania',
179 'details' => 'a test activity',
181 'activity_name' => 'Test activity type',
184 $result = $this->callAPIFailure('activity', 'create', $params);
188 * Ensure that an invalid activity type causes failure
189 * oddly enough this test was failing because the creation of the invalid type
190 * got added to the set up routine. Probably a mis-fix on a test
192 function testActivityCreateWithNonNumericActivityTypeId() {
194 'source_contact_id' => $this->_contactID
,
195 'subject' => 'Make-it-Happen Meeting',
196 'activity_date_time' => date('Ymd'),
198 'location' => 'Pensulvania',
199 'details' => 'a test activity',
201 'activity_type_id' => 'Invalid Test activity type',
204 $result = $this->callAPIFailure('activity', 'create', $params);
208 * check with incorrect required fields
210 function testActivityCreateWithUnknownActivityTypeId() {
212 'source_contact_id' => $this->_contactID
,
213 'subject' => 'Make-it-Happen Meeting',
214 'activity_date_time' => date('Ymd'),
216 'location' => 'Pensulvania',
217 'details' => 'a test activity',
219 'activity_type_id' => 699,
222 $result = $this->callAPIFailure('activity', 'create', $params);
225 function testActivityCreateWithInvalidPriority() {
227 'source_contact_id' => $this->_contactID
,
228 'subject' => 'Make-it-Happen Meeting',
229 'activity_date_time' => date('Ymd'),
231 'location' => 'Pensulvania',
232 'details' => 'a test activity',
235 'activity_type_id' => 1,
238 $result = $this->callAPIFailure('activity', 'create', $params,
239 "'44' is not a valid option for field priority_id");
240 $this->assertEquals('priority_id', $result['error_field']);
245 function testActivityCreateWithValidStringPriority() {
247 'source_contact_id' => $this->_contactID
,
248 'subject' => 'Make-it-Happen Meeting',
249 'activity_date_time' => date('Ymd'),
251 'location' => 'Pensulvania',
252 'details' => 'a test activity',
254 'priority_id' => 'Urgent',
255 'activity_type_id' => 1,
258 $result = $this->callAPISuccess('activity', 'create', $params);
259 $this->assertEquals(1, $result['values'][$result['id']]['priority_id']);
262 function testActivityCreateWithInValidStringPriority() {
264 'source_contact_id' => $this->_contactID
,
265 'subject' => 'Make-it-Happen Meeting',
266 'activity_date_time' => date('Ymd'),
268 'location' => 'Pensulvania',
269 'details' => 'a test activity',
271 'priority_id' => 'ergUrgent',
272 'activity_type_id' => 1,
275 $result = $this->callAPIFailure('activity', 'create', $params,
276 "'ergUrgent' is not a valid option for field priority_id");
280 * Test civicrm_activity_create() with valid parameters
282 function testActivityCreate() {
284 $result = $this->callAPISuccess('activity', 'create', $this->_params
);
285 $result = $this->callAPISuccess('activity', 'get', $this->_params
);
286 $this->assertEquals($result['values'][$result['id']]['duration'], 120, 'in line ' . __LINE__
);
287 $this->assertEquals($result['values'][$result['id']]['subject'], 'test activity type id', 'in line ' . __LINE__
);
288 $this->assertEquals($result['values'][$result['id']]['activity_date_time'], '2011-06-02 14:36:13', 'in line ' . __LINE__
);
289 $this->assertEquals($result['values'][$result['id']]['location'], 'Pensulvania', 'in line ' . __LINE__
);
290 $this->assertEquals($result['values'][$result['id']]['details'], 'a test activity', 'in line ' . __LINE__
);
291 $this->assertEquals($result['values'][$result['id']]['status_id'], 2, 'in line ' . __LINE__
);
292 $this->assertEquals($result['values'][$result['id']]['id'], $result['id'], 'in line ' . __LINE__
);
296 * Test civicrm_activity_create() with valid parameters - use type_id
298 function testActivityCreateCampaignTypeID() {
299 $this->enableCiviCampaign();
304 'source_contact_id' => $this->_contactID
,
305 'subject' => 'Make-it-Happen Meeting',
306 'activity_date_time' => '20110316',
308 'location' => 'Pensulvania',
309 'details' => 'a test activity',
311 'activity_type_id' => 29,
315 $result = $this->callAPISuccess('activity', 'create', $params);
316 //todo test target & assignee are set
318 //$this->assertEquals($result['values'][$result['id']]['source_contact_id'], $this->_contactID, 'in line ' . __LINE__);
319 $result = $this->callAPISuccess('activity', 'get', array('id' => $result['id']));
320 $this->assertEquals($result['values'][$result['id']]['duration'], 120, 'in line ' . __LINE__
);
321 $this->assertEquals($result['values'][$result['id']]['subject'], 'Make-it-Happen Meeting', 'in line ' . __LINE__
);
322 $this->assertEquals($result['values'][$result['id']]['activity_date_time'], '2011-03-16 00:00:00', 'in line ' . __LINE__
);
323 $this->assertEquals($result['values'][$result['id']]['location'], 'Pensulvania', 'in line ' . __LINE__
);
324 $this->assertEquals($result['values'][$result['id']]['details'], 'a test activity', 'in line ' . __LINE__
);
325 $this->assertEquals($result['values'][$result['id']]['status_id'], 1, 'in line ' . __LINE__
);
328 function testActivityReturnTargetAssignee() {
330 $description = "Example demonstrates setting & retrieving the target & source";
331 $subfile = "GetTargetandAssignee";
333 'source_contact_id' => $this->_contactID
,
334 'subject' => 'Make-it-Happen Meeting',
335 'activity_date_time' => '20110316',
337 'location' => 'Pensulvania',
338 'details' => 'a test activity',
340 'activity_type_id' => 1,
342 'target_contact_id' => $this->_contactID
,
343 'assignee_contact_id' => $this->_contactID
,
346 $result = $this->callAPIAndDocument('activity', 'create', $params, __FUNCTION__
, __FILE__
, $description, $subfile);
347 $result = $this->callAPISuccess('activity', 'get', array('id' => $result['id'], 'version' => $this->_apiversion
, 'return.assignee_contact_id' => 1, 'return.target_contact_id' => 1));
349 $this->assertEquals($this->_contactID
, $result['values'][$result['id']]['assignee_contact_id'][0], 'in line ' . __LINE__
);
350 $this->assertEquals($this->_contactID
, $result['values'][$result['id']]['target_contact_id'][0], 'in line ' . __LINE__
);
353 function testActivityCreateExample() {
355 * Test civicrm_activity_create() using example code
357 require_once 'api/v3/examples/ActivityCreate.php';
358 $result = activity_create_example();
359 $expectedResult = activity_create_expectedresult();
360 $this->assertEquals($result, $expectedResult);
364 * Test civicrm_activity_create() with valid parameters
365 * and some custom data
367 function testActivityCreateCustom() {
368 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__
, __FILE__
);
369 $params = $this->_params
;
370 $params['custom_' . $ids['custom_field_id']] = "custom string";
371 $result = $this->callAPIAndDocument($this->_entity
, 'create', $params, __FUNCTION__
, __FILE__
);
372 $result = $this->callAPISuccess($this->_entity
, 'get', array('return.custom_' . $ids['custom_field_id'] => 1, 'version' => 3, 'id' => $result['id']));
373 $this->assertEquals("custom string", $result['values'][$result['id']]['custom_' . $ids['custom_field_id']], ' in line ' . __LINE__
);
375 $this->customFieldDelete($ids['custom_field_id']);
376 $this->customGroupDelete($ids['custom_group_id']);
380 * Test civicrm_activity_create() with valid parameters
381 * and some custom data
383 function testActivityCreateCustomContactRefField() {
385 $this->callAPISuccess('contact', 'create', array('id' => $this->_contactID
, 'sort_name' => 'Contact, Test'));
386 $subfile = 'ContactRefCustomField';
387 $description = "demonstrates create with Contact Reference Custom Field";
388 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__
, __FILE__
);
390 'custom_group_id' => $ids['custom_group_id'],
391 'name' => 'Worker_Lookup',
392 'label' => 'Worker Lookup',
393 'html_type' => 'Autocomplete-Select',
394 'data_type' => 'ContactReference',
396 'is_searchable' => 1,
400 $customField = $this->callAPISuccess('custom_field', 'create', $params);
401 $params = $this->_params
;
402 $params['custom_' . $customField['id']] = "$this->_contactID";
404 $result = $this->callAPIAndDocument($this->_entity
, 'create', $params, __FUNCTION__
, __FILE__
, $description, $subfile, 'Create');
405 $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');
407 $this->assertEquals('Anderson, Anthony', $result['values'][$result['id']]['custom_' . $customField['id']], ' in line ' . __LINE__
);
408 $this->assertEquals($this->_contactID
, $result['values'][$result['id']]['custom_' . $customField['id'] . "_id"], ' in line ' . __LINE__
);
409 $this->assertEquals('Anderson, Anthony', $result['values'][$result['id']]['custom_' . $customField['id'] . '_1'], ' in line ' . __LINE__
);
410 $this->assertEquals($this->_contactID
, $result['values'][$result['id']]['custom_' . $customField['id'] . "_1_id"], ' in line ' . __LINE__
);
411 $this->customFieldDelete($ids['custom_field_id']);
412 $this->customGroupDelete($ids['custom_group_id']);
416 * Test civicrm_activity_create() with an invalid text status_id
418 function testActivityCreateBadTextStatus() {
421 'source_contact_id' => $this->_contactID
,
422 'subject' => 'Discussion on Apis for v3',
423 'activity_date_time' => date('Ymd'),
425 'location' => 'Pensulvania',
426 'details' => 'a test activity',
427 'status_id' => 'Invalid',
428 'activity_name' => 'Test activity type',
431 $result = $this->callAPIFailure('activity', 'create', $params);
435 * Test civicrm_activity_create() with an invalid text status_id
437 function testActivityCreateSupportActivityStatus() {
440 'source_contact_id' => $this->_contactID
,
441 'subject' => 'Discussion on Apis for v3',
442 'activity_date_time' => date('Ymd'),
444 'location' => 'Pensulvania',
445 'details' => 'a test activity',
446 'activity_status_id' => 'Invalid',
447 'activity_name' => 'Test activity type',
450 $result = $this->callAPIFailure('activity', 'create', $params,
451 "'Invalid' is not a valid option for field status_id");
456 * Test civicrm_activity_create() with valid parameters,
457 * using a text status_id
459 function testActivityCreateTextStatus() {
463 'source_contact_id' => $this->_contactID
,
464 'subject' => 'Make-it-Happen Meeting',
465 'activity_date_time' => date('Ymd'),
467 'location' => 'Pensulvania',
468 'details' => 'a test activity',
469 'status_id' => 'Scheduled',
470 'activity_name' => 'Test activity type',
473 $result = $this->callAPISuccess('activity', 'create', $params);
474 $this->assertEquals($result['values'][$result['id']]['duration'], 120, 'in line ' . __LINE__
);
475 $this->assertEquals($result['values'][$result['id']]['subject'], 'Make-it-Happen Meeting', 'in line ' . __LINE__
);
476 $this->assertEquals($result['values'][$result['id']]['activity_date_time'], date('Ymd') . '000000', 'in line ' . __LINE__
);
477 $this->assertEquals($result['values'][$result['id']]['location'], 'Pensulvania', 'in line ' . __LINE__
);
478 $this->assertEquals($result['values'][$result['id']]['details'], 'a test activity', 'in line ' . __LINE__
);
482 * Test civicrm_activity_get() with no params
484 function testActivityGetEmpty() {
485 $result = $this->callAPISuccess('activity', 'get', array());
489 * Test civicrm_activity_get() with a good activity ID
491 function testActivityGetGoodID1() {
492 // Insert rows in civicrm_activity creating activities 4 and
494 $description = "Function demonstrates getting asignee_contact_id & using it to get the contact";
495 $subfile = 'ReturnAssigneeContact';
496 $activity = $this->callAPISuccess('activity', 'create', $this->_params
);
498 $contact = $this->callAPISuccess('Contact', 'Create', array(
499 'first_name' => "The Rock",
500 'last_name' =>'roccky',
501 'contact_type' => 'Individual',
503 'api.activity.create' => array(
504 'id' => $activity['id'], 'assignee_contact_id' => '$value.id',
509 'activity_id' => $activity['id'],
510 'version' => $this->_apiversion
,
512 'return.assignee_contact_id' => 1,
513 'api.contact.get' => array(
514 'id' => '$value.source_contact_id',
518 $result = $this->callAPIAndDocument('Activity', 'Get', $params, __FUNCTION__
, __FILE__
, $description, $subfile);
520 $this->assertEquals($activity['id'], $result['id'], 'In line ' . __LINE__
);
522 $this->assertEquals($contact['id'], $result['values'][0]['assignee_contact_id'][0], 'In line ' . __LINE__
);
524 $this->assertEquals($this->_contactID
, $result['values'][0]['api.contact.get']['values'][0]['contact_id'], 'In line ' . __LINE__
);
525 $this->assertEquals($this->test_activity_type_value
, $result['values'][0]['activity_type_id'], 'In line ' . __LINE__
);
526 $this->assertEquals("test activity type id", $result['values'][0]['subject'], 'In line ' . __LINE__
);
530 * test that get functioning does filtering
532 function testGetFilter() {
534 'source_contact_id' => $this->_contactID
,
535 'subject' => 'Make-it-Happen Meeting',
536 'activity_date_time' => '20110316',
538 'location' => 'Pensulvania',
539 'details' => 'a test activity',
541 'activity_name' => 'Test activity type',
544 $result = $this->callAPISuccess('Activity', 'Create', $params);
545 $this->callAPISuccess('Activity', 'Get', array('subject' => 'Make-it-Happen Meeting'));
546 $this->assertEquals(1, $result['count']);
547 $this->assertEquals('Make-it-Happen Meeting', $result['values'][$result['id']]['subject']);
548 $this->callAPISuccess('Activity', 'Delete', array('id' => $result['id']));
551 * test that get functioning does filtering
553 function testGetStatusID() {
555 'source_contact_id' => $this->_contactID
,
556 'subject' => 'Make-it-Happen Meeting',
557 'activity_date_time' => '20110316',
559 'location' => 'Pensulvania',
560 'details' => 'a test activity',
562 'activity_name' => 'Test activity type',
565 $this->callAPISuccess('Activity', 'Create', $params);
566 $result = $this->callAPISuccess('Activity', 'Get', array('activity_status_id' => '1'));
567 $this->assertEquals(1, $result['count'], 'one activity of status 1 should exist');
569 $result = $this->callAPISuccess('Activity', 'Get', array('status_id' => '1'));
570 $this->assertEquals(1, $result['count'], 'status_id should also work');
572 $result = $this->callAPISuccess('Activity', 'Get', array('activity_status_id' => '2'));
573 $this->assertEquals(0, $result['count'], 'No activities of status 1 should exist');
574 $result = $this->callAPISuccess('Activity', 'Get', array(
575 'version' => $this->_apiversion
,
576 'status_id' => '2'));
577 $this->assertEquals(0, $result['count'], 'No activities of status 1 should exist');
583 * test that get functioning does filtering
585 function testGetFilterMaxDate() {
587 'source_contact_id' => $this->_contactID
,
588 'subject' => 'Make-it-Happen Meeting',
589 'activity_date_time' => '20110101',
591 'location' => 'Pensulvania',
592 'details' => 'a test activity',
594 'activity_name' => 'Test activity type',
595 'version' => $this->_apiversion
,
598 $activityOne = $this->callAPISuccess('Activity', 'Create', $params);
599 $params['activity_date_time'] = 20120216;
600 $activityTwo = $this->callAPISuccess('Activity', 'Create', $params);
601 $result = $this->callAPISuccess('Activity', 'Get', array(
604 $description = "demonstrates _low filter (at time of writing doesn't work if contact_id is set";
605 $subfile = "DateTimeLow";
606 $this->assertEquals(2, $result['count']);
609 'filter.activity_date_time_low' => '20120101000000',
612 $result = $this->callAPIAndDocument('Activity', 'Get', $params, __FUNCTION__
, __FILE__
, $description, $subfile);
613 $this->assertEquals(1, $result['count'], 'in line ' . __LINE__
);
614 $description = "demonstrates _high filter (at time of writing doesn't work if contact_id is set";
615 $subfile = "DateTimeHigh";
616 $this->assertEquals('2012-02-16 00:00:00', $result['values'][0]['activity_date_time'], 'in line ' . __LINE__
);
618 'source_contact_id' => $this->_contactID
,
620 'filter.activity_date_time_high' => '20120101000000',
623 $result = $this->callAPIAndDocument('Activity', 'Get', $params, __FUNCTION__
, __FILE__
, $description, $subfile);
625 $this->assertEquals(1, $result['count']);
626 $this->assertEquals('2011-01-01 00:00:00', $result['values'][0]['activity_date_time'], 'in line ' . __LINE__
);
628 $this->callAPISuccess('Activity', 'Delete', array('version' => 3, 'id' => $activityOne['id']));
629 $this->callAPISuccess('Activity', 'Delete', array('version' => 3, 'id' => $activityTwo['id']));
633 * Test civicrm_activity_get() with a good activity ID which
634 * has associated custom data
636 function testActivityGetGoodIDCustom() {
637 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__
, __FILE__
);
639 $params = $this->_params
;
640 $params['custom_' . $ids['custom_field_id']] = "custom string";
642 $result = $this->callAPISuccess($this->_entity
, 'create', $params);
644 // Retrieve the test value
646 'activity_type_id' => $this->test_activity_type_value
,
648 'return.custom_' . $ids['custom_field_id'] => 1,
650 $result = $this->callAPIAndDocument('activity', 'get', $params, __FUNCTION__
, __FILE__
);
651 $this->assertEquals("custom string", $result['values'][0]['custom_' . $ids['custom_field_id']]);
653 $this->assertEquals($this->test_activity_type_value
, $result['values'][0]['activity_type_id']);
654 $this->assertEquals('test activity type id', $result['values'][0]['subject'], 'In line ' . __LINE__
);
655 $this->customFieldDelete($ids['custom_field_id']);
656 $this->customGroupDelete($ids['custom_group_id']);
660 * Test civicrm_activity_get() with a good activity ID which
661 * has associated custom data
663 function testActivityGetContact_idCustom() {
664 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__
, __FILE__
);
666 $params = $this->_params
;
667 $params['custom_' . $ids['custom_field_id']] = "custom string";
669 $result = $this->callAPISuccess($this->_entity
, 'create', $params);
670 // Retrieve the test value
672 'contact_id' => $this->_params
['source_contact_id'],
673 'activity_type_id' => $this->test_activity_type_value
,
675 'return.custom_' . $ids['custom_field_id'] => 1,
677 $result = $this->callAPIAndDocument('activity', 'get', $params, __FUNCTION__
, __FILE__
);
678 $this->assertEquals("custom string", $result['values'][0]['custom_' . $ids['custom_field_id']], ' in line ' . __LINE__
);
680 $this->assertEquals($this->test_activity_type_value
, $result['values'][0]['activity_type_id'], 'In line ' . __LINE__
);
681 $this->assertEquals('test activity type id', $result['values'][0]['subject'], 'In line ' . __LINE__
);
682 $this->assertEquals($result['values'][0]['id'], $result['id'], 'in line ' . __LINE__
);
686 * check activity deletion with empty params
688 function testDeleteActivityForEmptyParams() {
689 $params = array('version' => $this->_apiversion
);
690 $result = $this->callAPIFailure('activity', 'delete', $params);
694 * check activity deletion without activity id
696 function testDeleteActivityWithoutId() {
698 'activity_name' => 'Meeting',
699 'version' => $this->_apiversion
,
701 $result = $this->callAPIFailure('activity', 'delete', $params);
705 * check activity deletion without activity type
707 function testDeleteActivityWithoutActivityType() {
708 $params = array('id' => 1);
709 $result = $this->callAPIFailure('activity', 'delete', $params);
713 * check activity deletion with incorrect data
715 function testDeleteActivityWithIncorrectActivityType() {
718 'activity_name' => 'Test Activity',
721 $result = $this->callAPIFailure('activity', 'delete', $params);
725 * check activity deletion with correct data
727 function testDeleteActivity() {
728 $result = $this->callAPISuccess('activity', 'create', $this->_params
);
730 'id' => $result['id'],
731 'version' => $this->_apiversion
,
734 $result = $this->callAPISuccess('activity', 'delete', $params);
735 $this->documentMe($params, $result, __FUNCTION__
, __FILE__
);
739 * check if required fields are not passed
741 function testActivityUpdateWithoutRequired() {
743 'subject' => 'this case should fail',
744 'scheduled_date_time' => date('Ymd'),
747 $result = $this->callAPIFailure('activity', 'create', $params);
751 * Test civicrm_activity_update() with non-numeric id
753 function testActivityUpdateWithNonNumericId() {
755 'id' => 'lets break it',
756 'activity_name' => 'Meeting',
757 'subject' => 'this case should fail',
758 'scheduled_date_time' => date('Ymd'),
761 $result = $this->callAPIFailure('activity', 'create', $params);
765 * check with incorrect required fields
767 function testActivityUpdateWithIncorrectContactActivityType() {
770 'activity_name' => 'Test Activity',
771 'subject' => 'this case should fail',
772 'scheduled_date_time' => date('Ymd'),
773 'source_contact_id' => $this->_contactID
,
776 $result = $this->callAPIFailure('activity', 'create', $params,
777 'Invalid Activity Id');
781 * Test civicrm_activity_update() to update an existing activity
783 function testActivityUpdate() {
784 $result = $this->callAPISuccess('activity', 'create', $this->_params
);
787 'id' => $result['id'],
788 'subject' => 'Make-it-Happen Meeting',
789 'activity_date_time' => '20091011123456',
791 'location' => '21, Park Avenue',
792 'details' => 'Lets update Meeting',
794 'source_contact_id' => $this->_contactID
,
798 $result = $this->callAPISuccess('activity', 'create', $params);
799 //hack on date comparison - really we should make getAndCheck smarter to handle dates
800 $params['activity_date_time'] = '2009-10-11 12:34:56';
801 // we also unset source_contact_id since it is stored in an aux table
802 unset($params['source_contact_id']);
803 $this->getAndCheck($params, $result['id'], 'activity');
807 * Test civicrm_activity_update() with valid parameters
808 * and some custom data
810 function testActivityUpdateCustom() {
811 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__
, __FILE__
);
813 $params = $this->_params
;
815 // Create an activity with custom data
816 //this has been updated from the previous 'old format' function - need to make it work
818 'source_contact_id' => $this->_contactID
,
819 'subject' => 'Make-it-Happen Meeting',
820 'activity_date_time' => '2009-10-18',
822 'location' => 'Pensulvania',
823 'details' => 'a test activity to check the update api',
825 'activity_name' => 'Test activity type',
826 'version' => $this->_apiversion
,
827 'custom_' . $ids['custom_field_id'] => 'custom string',
829 $result = $this->callAPISuccess('activity', 'create', $params);
831 $activityId = $result['id'];
832 $result = $this->callAPISuccess($this->_entity
, 'get', array('return.custom_' . $ids['custom_field_id'] => 1, 'version' => 3, 'id' => $result['id']));
833 $this->assertEquals("custom string", $result['values'][$result['id']]['custom_' . $ids['custom_field_id']], ' in line ' . __LINE__
);
834 $this->assertEquals("2009-10-18 00:00:00", $result['values'][$result['id']]['activity_date_time'], ' in line ' . __LINE__
);
835 $fields = $this->callAPISuccess('activity', 'getfields', array('version' => $this->_apiversion
));
836 $this->assertTrue(is_array($fields['values']['custom_' . $ids['custom_field_id']]));
838 // Update the activity with custom data
841 'source_contact_id' => $this->_contactID
,
842 'subject' => 'Make-it-Happen Meeting',
844 'activity_name' => 'Test activity type',
845 // add this since dates are messed up
846 'activity_date_time' => date('Ymd'),
847 'custom_' . $ids['custom_field_id'] => 'Updated my test data',
848 'version' => $this->_apiversion
,
850 $result = $this->callAPISuccess('Activity', 'Create', $params);
852 $result = $this->callAPISuccess($this->_entity
, 'get', array('return.custom_' . $ids['custom_field_id'] => 1, 'version' => 3, 'id' => $result['id']));
853 $this->assertEquals("Updated my test data", $result['values'][$result['id']]['custom_' . $ids['custom_field_id']], ' in line ' . __LINE__
);
857 * Test civicrm_activity_update() for core activity fields
858 * and some custom data
860 function testActivityUpdateCheckCoreFields() {
861 $params = $this->_params
;
862 $contact1Params = array(
863 'first_name' => 'John',
864 'middle_name' => 'J.',
865 'last_name' => 'Anderson',
868 'email' => 'john_anderson@civicrm.org',
869 'contact_type' => 'Individual',
872 $contact1 = $this->individualCreate($contact1Params);
873 $contact2Params = array(
874 'first_name' => 'Michal',
875 'middle_name' => 'J.',
876 'last_name' => 'Anderson',
879 'email' => 'michal_anderson@civicrm.org',
880 'contact_type' => 'Individual',
883 $contact2 = $this->individualCreate($contact2Params);
885 $params['assignee_contact_id'] = array($contact1, $contact2);
886 $params['target_contact_id'] = array($contact2 => $contact2);
887 $result = $this->callAPISuccess('Activity', 'Create', $params);
889 $activityId = $result['id'];
891 'return.assignee_contact_id' => 1,
892 'return.target_contact_id' => 1,
893 'version' => $this->_apiversion
,
896 $result = $this->callAPISuccess($this->_entity
, 'get',$getParams );
897 $assignee = $result['values'][$result['id']]['assignee_contact_id'];
898 $target = $result['values'][$result['id']]['target_contact_id'];
899 $this->assertEquals(2, count($assignee), ' in line ' . __LINE__
);
900 $this->assertEquals(1, count($target), ' in line ' . __LINE__
);
901 $this->assertEquals(TRUE, in_array($contact1, $assignee), ' in line ' . __LINE__
);
902 $this->assertEquals(TRUE, in_array($contact2, $target), ' in line ' . __LINE__
);
904 $contact3Params = array(
905 'first_name' => 'Jijo',
906 'middle_name' => 'J.',
907 'last_name' => 'Anderson',
910 'email' => 'jijo_anderson@civicrm.org',
911 'contact_type' => 'Individual',
914 $contact4Params = array(
915 'first_name' => 'Grant',
916 'middle_name' => 'J.',
917 'last_name' => 'Anderson',
920 'email' => 'grant_anderson@civicrm.org',
921 'contact_type' => 'Individual',
924 $contact3 = $this->individualCreate($contact3Params);
925 $contact4 = $this->individualCreate($contact4Params);
928 $params['id'] = $activityId;
929 $params['assignee_contact_id'] = array($contact3 => $contact3);
930 $params['target_contact_id'] = array($contact4 => $contact4);
932 $result = $this->callAPISuccess('activity', 'create', $params);
934 $this->assertEquals($activityId, $result['id'], ' in line ' . __LINE__
);
936 $result = $this->callAPISuccess(
940 'return.assignee_contact_id' => 1,
941 'return.target_contact_id' => 1,
942 'return.source_contact_id' => 1,
943 'id' => $result['id']
947 $assignee = $result['values'][$result['id']]['assignee_contact_id'];
948 $target = $result['values'][$result['id']]['target_contact_id'];
950 $this->assertEquals(1, count($assignee), ' in line ' . __LINE__
);
951 $this->assertEquals(1, count($target), ' in line ' . __LINE__
);
952 $this->assertEquals(TRUE, in_array($contact3, $assignee), ' in line ' . __LINE__
);
953 $this->assertEquals(TRUE, in_array($contact4, $target), ' in line ' . __LINE__
);
955 foreach ($this->_params
as $fld => $val) {
956 $this->assertEquals($val, $result['values'][$result['id']][$fld]);
961 * Test civicrm_activity_update() where the DB has a date_time
962 * value and there is none in the update params.
964 function testActivityUpdateNotDate() {
965 $result = $this->callAPISuccess('activity', 'create', $this->_params
);
968 'id' => $result['id'],
969 'subject' => 'Make-it-Happen Meeting',
971 'location' => '21, Park Avenue',
972 'details' => 'Lets update Meeting',
974 'source_contact_id' => $this->_contactID
,
978 $result = $this->callAPISuccess('activity', 'create', $params);
979 //hack on date comparison - really we should make getAndCheck smarter to handle dates
980 $params['activity_date_time'] = $this->_params
['activity_date_time'];
981 // we also unset source_contact_id since it is stored in an aux table
982 unset($params['source_contact_id']);
983 $this->getAndCheck($params, $result['id'], 'activity');
987 * check activity update with status
989 function testActivityUpdateWithStatus() {
990 $activity = $this->callAPISuccess('activity', 'create', $this->_params
);
992 'id' => $activity['id'],
993 'source_contact_id' => $this->_contactID
,
994 'subject' => 'Hurry update works',
996 'activity_name' => 'Test activity type',
999 $result = $this->callAPISuccess('activity', 'create', $params);
1000 $this->assertEquals($result['id'], $activity['id']);
1001 $this->assertEquals($result['values'][$activity['id']]['subject'], 'Hurry update works');
1002 $this->assertEquals($result['values'][$activity['id']]['status_id'], 1
1007 * Test civicrm_activity_update() where the source_contact_id
1008 * is not in the update params.
1010 function testActivityUpdateKeepSource() {
1011 $activity = $this->callAPISuccess('activity', 'create', $this->_params
);
1012 // Updating the activity but not providing anything for the source contact
1013 // (It was set as $this->_contactID earlier.)
1015 'id' => $activity['id'],
1016 'subject' => 'Updated Make-it-Happen Meeting',
1018 'location' => '21, Park Avenue',
1019 'details' => 'Lets update Meeting',
1021 'activity_name' => 'Test activity type',
1025 $result = $this->callAPISuccess('activity', 'create', $params);
1026 $findactivity = $this->callAPISuccess('Activity', 'Get', array('id' => $activity['id']));
1030 * Test civicrm_activities_contact_get()
1032 function testActivitiesContactGet() {
1033 $activity = $this->callAPISuccess('activity', 'create', $this->_params
);
1034 $activity2 = $this->callAPISuccess('activity', 'create', $this->_params2
);
1035 // Get activities associated with contact $this->_contactID
1037 'contact_id' => $this->_contactID
,
1039 $result = $this->callAPISuccess('activity', 'get', $params);
1041 $this->assertEquals(2, $result['count'], 'In line ' . __LINE__
);
1042 $this->assertEquals($this->test_activity_type_value
, $result['values'][$activity['id']]['activity_type_id'], 'In line ' . __LINE__
);
1043 $this->assertEquals('Test activity type', $result['values'][$activity['id']]['activity_name'], 'In line ' . __LINE__
);
1044 $this->assertEquals('Test activity type', $result['values'][$activity2['id']]['activity_name'], 'In line ' . __LINE__
);
1047 * test chained Activity format
1049 function testchainedActivityGet() {
1051 $activity = $this->callAPISuccess('Contact', 'Create', array(
1052 'display_name' => "bob brown",
1053 'contact_type' => 'Individual',
1054 'api.activity_type.create' => array(
1056 'label' => 'send out letters',
1061 ), 'api.activity.create' => array('subject' => 'send letter', 'activity_type_id' => '$value.api.activity_type.create.values.0.value'),
1064 $result = $this->callAPISuccess('Activity', 'Get', array(
1065 'id' => $activity['id'],
1066 'return.assignee_contact_id' => 1,
1067 'api.contact.get' => array('api.pledge.get' => 1),
1072 * Test civicrm_activity_contact_get() with invalid Contact Id
1074 function testActivitiesContactGetWithInvalidContactId() {
1075 $params = array('contact_id' => 'contact');
1076 $result = $this->callAPIFailure('activity', 'get', $params);
1080 * Test civicrm_activity_contact_get() with contact having no Activity
1082 function testActivitiesContactGetHavingNoActivity() {
1084 'first_name' => 'dan',
1085 'last_name' => 'conberg',
1086 'email' => 'dan.conberg@w.co.in',
1087 'contact_type' => 'Individual',
1090 $contact = $this->callAPISuccess('contact', 'create', $params);
1092 'contact_id' => $contact['id'],
1094 $result = $this->callAPISuccess('activity', 'get', $params);
1095 $this->assertEquals($result['count'], 0, 'in line ' . __LINE__
);
1098 function testGetFields() {
1099 $params = array('action' => 'create');
1100 $result = $this->callAPIAndDocument('activity', 'getfields', $params, __FUNCTION__
, __FILE__
, NULL, NULL, 'getfields');
1101 $this->assertTrue(is_array($result['values']), 'get fields doesnt return values array in line ' . __LINE__
);
1102 // $this->assertTrue(is_array($result['values']['priority_id']['options']));
1103 foreach ($result['values'] as $key => $value) {
1104 $this->assertTrue(is_array($value), $key . " is not an array in line " . __LINE__
);