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;
52 public $_eNoticeCompliant = TRUE;
54 * Test setup for every test
56 * Connect to the database, truncate the tables that will be used
57 * and redirect stdin to a temporary file
59 public function setUp() {
60 // Connect to the database
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',
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',
81 'location' => 'Pensulvania',
82 'details' => 'a test activity',
84 $this->_params2
= array(
85 'source_contact_id' => $this->_contactID
,
86 'subject' => 'Eat & drink',
87 'activity_date_time' => date('Ymd'),
89 'location' => 'Napier',
90 'details' => 'discuss & eat',
92 'activity_type_id' => $this->test_activity_type_value
,
94 // create a logged in USER since the code references it for source_contact_id
95 $this->createLoggedInUser();
99 * Tears down the fixture, for example, closes a network connection.
100 * This method is called after a test is executed.
104 function tearDown() {
105 $tablesToTruncate = array(
108 'civicrm_activity_contact',
110 $this->quickCleanup($tablesToTruncate, TRUE);
111 $this->callAPISuccess('option_value', 'delete', array('id' => $this->test_activity_type_id
));
115 * check with empty array
117 function testActivityCreateEmpty() {
118 $result = $this->callAPIFailure('activity', 'create', array());
122 * check if required fields are not passed
124 function testActivityCreateWithoutRequired() {
126 'subject' => 'this case should fail',
127 'scheduled_date_time' => date('Ymd'),
129 $result = $this->callAPIFailure('activity', 'create', $params);
133 * Test civicrm_activity_create() with mismatched activity_type_id
136 function testActivityCreateMismatchNameType() {
138 'source_contact_id' => $this->_contactID
,
139 'subject' => 'Test activity',
140 'activity_date_time' => date('Ymd'),
142 'location' => 'Pensulvania',
143 'details' => 'a test activity',
145 'activity_name' => 'Fubar activity type',
146 'activity_type_id' => 5,
147 'scheduled_date_time' => date('Ymd'),
150 $result = $this->callAPIFailure('activity', 'create', $params);
154 * Test civicrm_activity_id() with missing source_contact_id is put with the current user.
156 function testActivityCreateWithMissingContactId() {
158 'subject' => 'Make-it-Happen Meeting',
159 'activity_date_time' => date('Ymd'),
161 'location' => 'Pensulvania',
162 'details' => 'a test activity',
164 'activity_name' => 'Test activity type',
167 $result = $this->callAPISuccess('activity', 'create', $params);
171 * Test civicrm_activity_id() with non-numeric source_contact_id
173 function testActivityCreateWithNonNumericContactId() {
175 'source_contact_id' => 'fubar',
176 'subject' => 'Make-it-Happen Meeting',
177 'activity_date_time' => date('Ymd'),
179 'location' => 'Pensulvania',
180 'details' => 'a test activity',
182 'activity_name' => 'Test activity type',
185 $result = $this->callAPIFailure('activity', 'create', $params);
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
193 function testActivityCreateWithNonNumericActivityTypeId() {
195 'source_contact_id' => $this->_contactID
,
196 'subject' => 'Make-it-Happen Meeting',
197 'activity_date_time' => date('Ymd'),
199 'location' => 'Pensulvania',
200 'details' => 'a test activity',
202 'activity_type_id' => 'Invalid Test activity type',
205 $result = $this->callAPIFailure('activity', 'create', $params);
209 * check with incorrect required fields
211 function testActivityCreateWithUnknownActivityTypeId() {
213 'source_contact_id' => $this->_contactID
,
214 'subject' => 'Make-it-Happen Meeting',
215 'activity_date_time' => date('Ymd'),
217 'location' => 'Pensulvania',
218 'details' => 'a test activity',
220 'activity_type_id' => 699,
223 $result = $this->callAPIFailure('activity', 'create', $params);
226 function testActivityCreateWithInvalidPriority() {
228 'source_contact_id' => $this->_contactID
,
229 'subject' => 'Make-it-Happen Meeting',
230 'activity_date_time' => date('Ymd'),
232 'location' => 'Pensulvania',
233 'details' => 'a test activity',
236 'activity_type_id' => 1,
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']);
246 function testActivityCreateWithValidStringPriority() {
248 'source_contact_id' => $this->_contactID
,
249 'subject' => 'Make-it-Happen Meeting',
250 'activity_date_time' => date('Ymd'),
252 'location' => 'Pensulvania',
253 'details' => 'a test activity',
255 'priority_id' => 'Urgent',
256 'activity_type_id' => 1,
259 $result = $this->callAPISuccess('activity', 'create', $params);
260 $this->assertEquals(1, $result['values'][$result['id']]['priority_id']);
263 function testActivityCreateWithInValidStringPriority() {
265 'source_contact_id' => $this->_contactID
,
266 'subject' => 'Make-it-Happen Meeting',
267 'activity_date_time' => date('Ymd'),
269 'location' => 'Pensulvania',
270 'details' => 'a test activity',
272 'priority_id' => 'ergUrgent',
273 'activity_type_id' => 1,
276 $result = $this->callAPIFailure('activity', 'create', $params,
277 "'ergUrgent' is not a valid option for field priority_id");
281 * Test civicrm_activity_create() with valid parameters
283 function testActivityCreate() {
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__
);
297 * Test civicrm_activity_create() with valid parameters - use type_id
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);
309 'source_contact_id' => $this->_contactID
,
310 'subject' => 'Make-it-Happen Meeting',
311 'activity_date_time' => '20110316',
313 'location' => 'Pensulvania',
314 'details' => 'a test activity',
316 'activity_type_id' => 29,
320 $result = $this->callAPISuccess('activity', 'create', $params);
321 //todo test target & assignee are set
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__
);
333 function testActivityReturnTargetAssignee() {
335 $description = "Example demonstrates setting & retrieving the target & source";
336 $subfile = "GetTargetandAssignee";
338 'source_contact_id' => $this->_contactID
,
339 'subject' => 'Make-it-Happen Meeting',
340 'activity_date_time' => '20110316',
342 'location' => 'Pensulvania',
343 'details' => 'a test activity',
345 'activity_type_id' => 1,
347 'target_contact_id' => $this->_contactID
,
348 'assignee_contact_id' => $this->_contactID
,
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));
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__
);
358 function testActivityCreateExample() {
360 * Test civicrm_activity_create() using example code
362 require_once 'api/v3/examples/ActivityCreate.php';
363 $result = activity_create_example();
364 $expectedResult = activity_create_expectedresult();
365 $this->assertEquals($result, $expectedResult);
369 * Test civicrm_activity_create() with valid parameters
370 * and some custom data
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__
);
380 $this->customFieldDelete($ids['custom_field_id']);
381 $this->customGroupDelete($ids['custom_group_id']);
385 * Test civicrm_activity_create() with valid parameters
386 * and some custom data
388 function testActivityCreateCustomContactRefField() {
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__
);
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',
401 'is_searchable' => 1,
405 $customField = $this->callAPISuccess('custom_field', 'create', $params);
406 $params = $this->_params
;
407 $params['custom_' . $customField['id']] = "$this->_contactID";
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');
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']);
421 * Test civicrm_activity_create() with an invalid text status_id
423 function testActivityCreateBadTextStatus() {
426 'source_contact_id' => $this->_contactID
,
427 'subject' => 'Discussion on Apis for v3',
428 'activity_date_time' => date('Ymd'),
430 'location' => 'Pensulvania',
431 'details' => 'a test activity',
432 'status_id' => 'Invalid',
433 'activity_name' => 'Test activity type',
436 $result = $this->callAPIFailure('activity', 'create', $params);
440 * Test civicrm_activity_create() with an invalid text status_id
442 function testActivityCreateSupportActivityStatus() {
445 'source_contact_id' => $this->_contactID
,
446 'subject' => 'Discussion on Apis for v3',
447 'activity_date_time' => date('Ymd'),
449 'location' => 'Pensulvania',
450 'details' => 'a test activity',
451 'activity_status_id' => 'Invalid',
452 'activity_name' => 'Test activity type',
455 $result = $this->callAPIFailure('activity', 'create', $params,
456 "'Invalid' is not a valid option for field status_id");
461 * Test civicrm_activity_create() with valid parameters,
462 * using a text status_id
464 function testActivityCreateTextStatus() {
468 'source_contact_id' => $this->_contactID
,
469 'subject' => 'Make-it-Happen Meeting',
470 'activity_date_time' => date('Ymd'),
472 'location' => 'Pensulvania',
473 'details' => 'a test activity',
474 'status_id' => 'Scheduled',
475 'activity_name' => 'Test activity type',
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__
);
487 * Test civicrm_activity_get() with no params
489 function testActivityGetEmpty() {
490 $result = $this->callAPISuccess('activity', 'get', array());
494 * Test civicrm_activity_get() with a good activity ID
496 function testActivityGetGoodID1() {
497 // Insert rows in civicrm_activity creating activities 4 and
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
);
503 $contact = $this->callAPISuccess('Contact', 'Create', array(
504 'first_name' => "The Rock",
505 'last_name' =>'roccky',
506 'contact_type' => 'Individual',
508 'api.activity.create' => array(
509 'id' => $activity['id'], 'assignee_contact_id' => '$value.id',
514 'activity_id' => $activity['id'],
515 'version' => $this->_apiversion
,
517 'return.assignee_contact_id' => 1,
518 'api.contact.get' => array(
519 'id' => '$value.source_contact_id',
523 $result = $this->callAPIAndDocument('Activity', 'Get', $params, __FUNCTION__
, __FILE__
, $description, $subfile);
525 $this->assertEquals($activity['id'], $result['id'], 'In line ' . __LINE__
);
527 $this->assertEquals($contact['id'], $result['values'][0]['assignee_contact_id'][0], 'In line ' . __LINE__
);
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__
);
535 * test that get functioning does filtering
537 function testGetFilter() {
539 'source_contact_id' => $this->_contactID
,
540 'subject' => 'Make-it-Happen Meeting',
541 'activity_date_time' => '20110316',
543 'location' => 'Pensulvania',
544 'details' => 'a test activity',
546 'activity_name' => 'Test activity type',
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']));
556 * test that get functioning does filtering
558 function testGetStatusID() {
560 'source_contact_id' => $this->_contactID
,
561 'subject' => 'Make-it-Happen Meeting',
562 'activity_date_time' => '20110316',
564 'location' => 'Pensulvania',
565 'details' => 'a test activity',
567 'activity_name' => 'Test activity type',
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');
574 $result = $this->callAPISuccess('Activity', 'Get', array('status_id' => '1'));
575 $this->assertEquals(1, $result['count'], 'status_id should also work');
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');
588 * test that get functioning does filtering
590 function testGetFilterMaxDate() {
592 'source_contact_id' => $this->_contactID
,
593 'subject' => 'Make-it-Happen Meeting',
594 'activity_date_time' => '20110101',
596 'location' => 'Pensulvania',
597 'details' => 'a test activity',
599 'activity_name' => 'Test activity type',
600 'version' => $this->_apiversion
,
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(
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']);
614 'filter.activity_date_time_low' => '20120101000000',
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__
);
623 'source_contact_id' => $this->_contactID
,
625 'filter.activity_date_time_high' => '20120101000000',
628 $result = $this->callAPIAndDocument('Activity', 'Get', $params, __FUNCTION__
, __FILE__
, $description, $subfile);
630 $this->assertEquals(1, $result['count']);
631 $this->assertEquals('2011-01-01 00:00:00', $result['values'][0]['activity_date_time'], 'in line ' . __LINE__
);
633 $this->callAPISuccess('Activity', 'Delete', array('version' => 3, 'id' => $activityOne['id']));
634 $this->callAPISuccess('Activity', 'Delete', array('version' => 3, 'id' => $activityTwo['id']));
638 * Test civicrm_activity_get() with a good activity ID which
639 * has associated custom data
641 function testActivityGetGoodIDCustom() {
642 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__
, __FILE__
);
644 $params = $this->_params
;
645 $params['custom_' . $ids['custom_field_id']] = "custom string";
647 $result = $this->callAPISuccess($this->_entity
, 'create', $params);
649 // Retrieve the test value
651 'activity_type_id' => $this->test_activity_type_value
,
653 'return.custom_' . $ids['custom_field_id'] => 1,
655 $result = $this->callAPIAndDocument('activity', 'get', $params, __FUNCTION__
, __FILE__
);
656 $this->assertEquals("custom string", $result['values'][0]['custom_' . $ids['custom_field_id']]);
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']);
665 * Test civicrm_activity_get() with a good activity ID which
666 * has associated custom data
668 function testActivityGetContact_idCustom() {
669 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__
, __FILE__
);
671 $params = $this->_params
;
672 $params['custom_' . $ids['custom_field_id']] = "custom string";
674 $result = $this->callAPISuccess($this->_entity
, 'create', $params);
675 // Retrieve the test value
677 'contact_id' => $this->_params
['source_contact_id'],
678 'activity_type_id' => $this->test_activity_type_value
,
680 'return.custom_' . $ids['custom_field_id'] => 1,
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__
);
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__
);
691 * check activity deletion with empty params
693 function testDeleteActivityForEmptyParams() {
694 $params = array('version' => $this->_apiversion
);
695 $result = $this->callAPIFailure('activity', 'delete', $params);
699 * check activity deletion without activity id
701 function testDeleteActivityWithoutId() {
703 'activity_name' => 'Meeting',
704 'version' => $this->_apiversion
,
706 $result = $this->callAPIFailure('activity', 'delete', $params);
710 * check activity deletion without activity type
712 function testDeleteActivityWithoutActivityType() {
713 $params = array('id' => 1);
714 $result = $this->callAPIFailure('activity', 'delete', $params);
718 * check activity deletion with incorrect data
720 function testDeleteActivityWithIncorrectActivityType() {
723 'activity_name' => 'Test Activity',
726 $result = $this->callAPIFailure('activity', 'delete', $params);
730 * check activity deletion with correct data
732 function testDeleteActivity() {
733 $result = $this->callAPISuccess('activity', 'create', $this->_params
);
735 'id' => $result['id'],
736 'version' => $this->_apiversion
,
739 $result = $this->callAPISuccess('activity', 'delete', $params);
740 $this->documentMe($params, $result, __FUNCTION__
, __FILE__
);
744 * check if required fields are not passed
746 function testActivityUpdateWithoutRequired() {
748 'subject' => 'this case should fail',
749 'scheduled_date_time' => date('Ymd'),
752 $result = $this->callAPIFailure('activity', 'create', $params);
756 * Test civicrm_activity_update() with non-numeric id
758 function testActivityUpdateWithNonNumericId() {
760 'id' => 'lets break it',
761 'activity_name' => 'Meeting',
762 'subject' => 'this case should fail',
763 'scheduled_date_time' => date('Ymd'),
766 $result = $this->callAPIFailure('activity', 'create', $params);
770 * check with incorrect required fields
772 function testActivityUpdateWithIncorrectContactActivityType() {
775 'activity_name' => 'Test Activity',
776 'subject' => 'this case should fail',
777 'scheduled_date_time' => date('Ymd'),
778 'source_contact_id' => $this->_contactID
,
781 $result = $this->callAPIFailure('activity', 'create', $params,
782 'Invalid Activity Id');
786 * Test civicrm_activity_update() to update an existing activity
788 function testActivityUpdate() {
789 $result = $this->callAPISuccess('activity', 'create', $this->_params
);
792 'id' => $result['id'],
793 'subject' => 'Make-it-Happen Meeting',
794 'activity_date_time' => '20091011123456',
796 'location' => '21, Park Avenue',
797 'details' => 'Lets update Meeting',
799 'source_contact_id' => $this->_contactID
,
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');
812 * Test civicrm_activity_update() with valid parameters
813 * and some custom data
815 function testActivityUpdateCustom() {
816 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__
, __FILE__
);
818 $params = $this->_params
;
820 // Create an activity with custom data
821 //this has been updated from the previous 'old format' function - need to make it work
823 'source_contact_id' => $this->_contactID
,
824 'subject' => 'Make-it-Happen Meeting',
825 'activity_date_time' => '2009-10-18',
827 'location' => 'Pensulvania',
828 'details' => 'a test activity to check the update api',
830 'activity_name' => 'Test activity type',
831 'version' => $this->_apiversion
,
832 'custom_' . $ids['custom_field_id'] => 'custom string',
834 $result = $this->callAPISuccess('activity', 'create', $params);
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']]));
843 // Update the activity with custom data
846 'source_contact_id' => $this->_contactID
,
847 'subject' => 'Make-it-Happen Meeting',
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
,
855 $result = $this->callAPISuccess('Activity', 'Create', $params);
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__
);
862 * Test civicrm_activity_update() for core activity fields
863 * and some custom data
865 function testActivityUpdateCheckCoreFields() {
866 $params = $this->_params
;
867 $contact1Params = array(
868 'first_name' => 'John',
869 'middle_name' => 'J.',
870 'last_name' => 'Anderson',
873 'email' => 'john_anderson@civicrm.org',
874 'contact_type' => 'Individual',
877 $contact1 = $this->individualCreate($contact1Params);
878 $contact2Params = array(
879 'first_name' => 'Michal',
880 'middle_name' => 'J.',
881 'last_name' => 'Anderson',
884 'email' => 'michal_anderson@civicrm.org',
885 'contact_type' => 'Individual',
888 $contact2 = $this->individualCreate($contact2Params);
890 $params['assignee_contact_id'] = array($contact1, $contact2);
891 $params['target_contact_id'] = array($contact2 => $contact2);
892 $result = $this->callAPISuccess('Activity', 'Create', $params);
894 $activityId = $result['id'];
896 'return.assignee_contact_id' => 1,
897 'return.target_contact_id' => 1,
898 'version' => $this->_apiversion
,
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__
);
909 $contact3Params = array(
910 'first_name' => 'Jijo',
911 'middle_name' => 'J.',
912 'last_name' => 'Anderson',
915 'email' => 'jijo_anderson@civicrm.org',
916 'contact_type' => 'Individual',
919 $contact4Params = array(
920 'first_name' => 'Grant',
921 'middle_name' => 'J.',
922 'last_name' => 'Anderson',
925 'email' => 'grant_anderson@civicrm.org',
926 'contact_type' => 'Individual',
929 $contact3 = $this->individualCreate($contact3Params);
930 $contact4 = $this->individualCreate($contact4Params);
933 $params['id'] = $activityId;
934 $params['assignee_contact_id'] = array($contact3 => $contact3);
935 $params['target_contact_id'] = array($contact4 => $contact4);
937 $result = $this->callAPISuccess('activity', 'create', $params);
939 $this->assertEquals($activityId, $result['id'], ' in line ' . __LINE__
);
941 $result = $this->callAPISuccess(
945 'return.assignee_contact_id' => 1,
946 'return.target_contact_id' => 1,
947 'return.source_contact_id' => 1,
948 'id' => $result['id']
952 $assignee = $result['values'][$result['id']]['assignee_contact_id'];
953 $target = $result['values'][$result['id']]['target_contact_id'];
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__
);
960 foreach ($this->_params
as $fld => $val) {
961 $this->assertEquals($val, $result['values'][$result['id']][$fld]);
966 * Test civicrm_activity_update() where the DB has a date_time
967 * value and there is none in the update params.
969 function testActivityUpdateNotDate() {
970 $result = $this->callAPISuccess('activity', 'create', $this->_params
);
973 'id' => $result['id'],
974 'subject' => 'Make-it-Happen Meeting',
976 'location' => '21, Park Avenue',
977 'details' => 'Lets update Meeting',
979 'source_contact_id' => $this->_contactID
,
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');
992 * check activity update with status
994 function testActivityUpdateWithStatus() {
995 $activity = $this->callAPISuccess('activity', 'create', $this->_params
);
997 'id' => $activity['id'],
998 'source_contact_id' => $this->_contactID
,
999 'subject' => 'Hurry update works',
1001 'activity_name' => 'Test activity type',
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
1012 * Test civicrm_activity_update() where the source_contact_id
1013 * is not in the update params.
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.)
1020 'id' => $activity['id'],
1021 'subject' => 'Updated Make-it-Happen Meeting',
1023 'location' => '21, Park Avenue',
1024 'details' => 'Lets update Meeting',
1026 'activity_name' => 'Test activity type',
1030 $result = $this->callAPISuccess('activity', 'create', $params);
1031 $findactivity = $this->callAPISuccess('Activity', 'Get', array('id' => $activity['id']));
1035 * Test civicrm_activities_contact_get()
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
1042 'contact_id' => $this->_contactID
,
1044 $result = $this->callAPISuccess('activity', 'get', $params);
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__
);
1052 * test chained Activity format
1054 function testchainedActivityGet() {
1056 $activity = $this->callAPISuccess('Contact', 'Create', array(
1057 'display_name' => "bob brown",
1058 'contact_type' => 'Individual',
1059 'api.activity_type.create' => array(
1061 'label' => 'send out letters',
1066 ), 'api.activity.create' => array('subject' => 'send letter', 'activity_type_id' => '$value.api.activity_type.create.values.0.value'),
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),
1077 * Test civicrm_activity_contact_get() with invalid Contact Id
1079 function testActivitiesContactGetWithInvalidContactId() {
1080 $params = array('contact_id' => 'contact');
1081 $result = $this->callAPIFailure('activity', 'get', $params);
1085 * Test civicrm_activity_contact_get() with contact having no Activity
1087 function testActivitiesContactGetHavingNoActivity() {
1089 'first_name' => 'dan',
1090 'last_name' => 'conberg',
1091 'email' => 'dan.conberg@w.co.in',
1092 'contact_type' => 'Individual',
1095 $contact = $this->callAPISuccess('contact', 'create', $params);
1097 'contact_id' => $contact['id'],
1099 $result = $this->callAPISuccess('activity', 'get', $params);
1100 $this->assertEquals($result['count'], 0, 'in line ' . __LINE__
);
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__
);