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/Activity/Create.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']));
553 * Test civicrm_activity_get() with filter target_contact_id
555 function testActivityGetTargetFilter() {
556 $params = $this->_params
;
557 $contact1Params = array(
558 'first_name' => 'John',
559 'middle_name' => 'J.',
560 'last_name' => 'Anderson',
563 'email' => 'john_anderson@civicrm.org',
564 'contact_type' => 'Individual',
567 $contact1 = $this->individualCreate($contact1Params);
568 $contact2Params = array(
569 'first_name' => 'Michal',
570 'middle_name' => 'J.',
571 'last_name' => 'Anderson',
574 'email' => 'michal_anderson@civicrm.org',
575 'contact_type' => 'Individual',
578 $contact2 = $this->individualCreate($contact2Params);
580 $params['assignee_contact_id'] = array($contact1, $contact2);
581 $params['target_contact_id'] = array($contact2 => $contact2);
582 $activity = $this->callAPISuccess('Activity', 'Create', $params);
584 $activityget = $this->callAPISuccess('Activity', 'get', array('id' => $activity['id'], 'target_contact_id' => $contact2,'return.target_contact_id' => 1));
585 $this->assertEquals($activity['id'], $activityget['id'], 'In line ' . __LINE__
);
586 $this->assertEquals($contact2, $activityget['values'][$activityget['id']]['target_contact_id'][0], 'In line ' . __LINE__
);
588 $activityget = $this->callAPISuccess('activity', 'get', array('target_contact_id' => $this->_contactID
,'return.target_contact_id' => 1,'id' => $activity['id']));
589 if ($activityget['count'] > 0) {
590 $this->assertNotEquals($contact2, $activityget['values'][$activityget['id']]['target_contact_id'][0], 'In line ' . __LINE__
);
595 * test that get functioning does filtering
597 function testGetStatusID() {
599 'source_contact_id' => $this->_contactID
,
600 'subject' => 'Make-it-Happen Meeting',
601 'activity_date_time' => '20110316',
603 'location' => 'Pensulvania',
604 'details' => 'a test activity',
606 'activity_name' => 'Test activity type',
609 $this->callAPISuccess('Activity', 'Create', $params);
610 $result = $this->callAPISuccess('Activity', 'Get', array('activity_status_id' => '1'));
611 $this->assertEquals(1, $result['count'], 'one activity of status 1 should exist');
613 $result = $this->callAPISuccess('Activity', 'Get', array('status_id' => '1'));
614 $this->assertEquals(1, $result['count'], 'status_id should also work');
616 $result = $this->callAPISuccess('Activity', 'Get', array('activity_status_id' => '2'));
617 $this->assertEquals(0, $result['count'], 'No activities of status 1 should exist');
618 $result = $this->callAPISuccess('Activity', 'Get', array(
619 'version' => $this->_apiversion
,
620 'status_id' => '2'));
621 $this->assertEquals(0, $result['count'], 'No activities of status 1 should exist');
627 * test that get functioning does filtering
629 function testGetFilterMaxDate() {
631 'source_contact_id' => $this->_contactID
,
632 'subject' => 'Make-it-Happen Meeting',
633 'activity_date_time' => '20110101',
635 'location' => 'Pensulvania',
636 'details' => 'a test activity',
638 'activity_name' => 'Test activity type',
639 'version' => $this->_apiversion
,
642 $activityOne = $this->callAPISuccess('Activity', 'Create', $params);
643 $params['activity_date_time'] = 20120216;
644 $activityTwo = $this->callAPISuccess('Activity', 'Create', $params);
645 $result = $this->callAPISuccess('Activity', 'Get', array(
648 $description = "demonstrates _low filter (at time of writing doesn't work if contact_id is set";
649 $subfile = "DateTimeLow";
650 $this->assertEquals(2, $result['count']);
653 'filter.activity_date_time_low' => '20120101000000',
656 $result = $this->callAPIAndDocument('Activity', 'Get', $params, __FUNCTION__
, __FILE__
, $description, $subfile);
657 $this->assertEquals(1, $result['count'], 'in line ' . __LINE__
);
658 $description = "demonstrates _high filter (at time of writing doesn't work if contact_id is set";
659 $subfile = "DateTimeHigh";
660 $this->assertEquals('2012-02-16 00:00:00', $result['values'][0]['activity_date_time'], 'in line ' . __LINE__
);
662 'source_contact_id' => $this->_contactID
,
664 'filter.activity_date_time_high' => '20120101000000',
667 $result = $this->callAPIAndDocument('Activity', 'Get', $params, __FUNCTION__
, __FILE__
, $description, $subfile);
669 $this->assertEquals(1, $result['count']);
670 $this->assertEquals('2011-01-01 00:00:00', $result['values'][0]['activity_date_time'], 'in line ' . __LINE__
);
672 $this->callAPISuccess('Activity', 'Delete', array('version' => 3, 'id' => $activityOne['id']));
673 $this->callAPISuccess('Activity', 'Delete', array('version' => 3, 'id' => $activityTwo['id']));
677 * Test civicrm_activity_get() with a good activity ID which
678 * has associated custom data
680 function testActivityGetGoodIDCustom() {
681 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__
, __FILE__
);
683 $params = $this->_params
;
684 $params['custom_' . $ids['custom_field_id']] = "custom string";
686 $result = $this->callAPISuccess($this->_entity
, 'create', $params);
688 // Retrieve the test value
690 'activity_type_id' => $this->test_activity_type_value
,
692 'return.custom_' . $ids['custom_field_id'] => 1,
694 $result = $this->callAPIAndDocument('activity', 'get', $params, __FUNCTION__
, __FILE__
);
695 $this->assertEquals("custom string", $result['values'][0]['custom_' . $ids['custom_field_id']]);
697 $this->assertEquals($this->test_activity_type_value
, $result['values'][0]['activity_type_id']);
698 $this->assertEquals('test activity type id', $result['values'][0]['subject'], 'In line ' . __LINE__
);
699 $this->customFieldDelete($ids['custom_field_id']);
700 $this->customGroupDelete($ids['custom_group_id']);
704 * Test civicrm_activity_get() with a good activity ID which
705 * has associated custom data
707 function testActivityGetContact_idCustom() {
708 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__
, __FILE__
);
710 $params = $this->_params
;
711 $params['custom_' . $ids['custom_field_id']] = "custom string";
713 $result = $this->callAPISuccess($this->_entity
, 'create', $params);
714 // Retrieve the test value
716 'contact_id' => $this->_params
['source_contact_id'],
717 'activity_type_id' => $this->test_activity_type_value
,
719 'return.custom_' . $ids['custom_field_id'] => 1,
721 $result = $this->callAPIAndDocument('activity', 'get', $params, __FUNCTION__
, __FILE__
);
722 $this->assertEquals("custom string", $result['values'][0]['custom_' . $ids['custom_field_id']], ' in line ' . __LINE__
);
724 $this->assertEquals($this->test_activity_type_value
, $result['values'][0]['activity_type_id'], 'In line ' . __LINE__
);
725 $this->assertEquals('test activity type id', $result['values'][0]['subject'], 'In line ' . __LINE__
);
726 $this->assertEquals($result['values'][0]['id'], $result['id'], 'in line ' . __LINE__
);
730 * check activity deletion with empty params
732 function testDeleteActivityForEmptyParams() {
733 $params = array('version' => $this->_apiversion
);
734 $result = $this->callAPIFailure('activity', 'delete', $params);
738 * check activity deletion without activity id
740 function testDeleteActivityWithoutId() {
742 'activity_name' => 'Meeting',
743 'version' => $this->_apiversion
,
745 $result = $this->callAPIFailure('activity', 'delete', $params);
749 * check activity deletion without activity type
751 function testDeleteActivityWithoutActivityType() {
752 $params = array('id' => 1);
753 $result = $this->callAPIFailure('activity', 'delete', $params);
757 * check activity deletion with incorrect data
759 function testDeleteActivityWithIncorrectActivityType() {
762 'activity_name' => 'Test Activity',
765 $result = $this->callAPIFailure('activity', 'delete', $params);
769 * check activity deletion with correct data
771 function testDeleteActivity() {
772 $result = $this->callAPISuccess('activity', 'create', $this->_params
);
774 'id' => $result['id'],
775 'version' => $this->_apiversion
,
778 $result = $this->callAPISuccess('activity', 'delete', $params);
779 $this->documentMe($params, $result, __FUNCTION__
, __FILE__
);
783 * check if required fields are not passed
785 function testActivityUpdateWithoutRequired() {
787 'subject' => 'this case should fail',
788 'scheduled_date_time' => date('Ymd'),
791 $result = $this->callAPIFailure('activity', 'create', $params);
795 * Test civicrm_activity_update() with non-numeric id
797 function testActivityUpdateWithNonNumericId() {
799 'id' => 'lets break it',
800 'activity_name' => 'Meeting',
801 'subject' => 'this case should fail',
802 'scheduled_date_time' => date('Ymd'),
805 $result = $this->callAPIFailure('activity', 'create', $params);
809 * check with incorrect required fields
811 function testActivityUpdateWithIncorrectContactActivityType() {
814 'activity_name' => 'Test Activity',
815 'subject' => 'this case should fail',
816 'scheduled_date_time' => date('Ymd'),
817 'source_contact_id' => $this->_contactID
,
820 $result = $this->callAPIFailure('activity', 'create', $params,
821 'Invalid Activity Id');
825 * Test civicrm_activity_update() to update an existing activity
827 function testActivityUpdate() {
828 $result = $this->callAPISuccess('activity', 'create', $this->_params
);
831 'id' => $result['id'],
832 'subject' => 'Make-it-Happen Meeting',
833 'activity_date_time' => '20091011123456',
835 'location' => '21, Park Avenue',
836 'details' => 'Lets update Meeting',
838 'source_contact_id' => $this->_contactID
,
842 $result = $this->callAPISuccess('activity', 'create', $params);
843 //hack on date comparison - really we should make getAndCheck smarter to handle dates
844 $params['activity_date_time'] = '2009-10-11 12:34:56';
845 // we also unset source_contact_id since it is stored in an aux table
846 unset($params['source_contact_id']);
847 $this->getAndCheck($params, $result['id'], 'activity');
851 * Test civicrm_activity_update() with valid parameters
852 * and some custom data
854 function testActivityUpdateCustom() {
855 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__
, __FILE__
);
857 $params = $this->_params
;
859 // Create an activity with custom data
860 //this has been updated from the previous 'old format' function - need to make it work
862 'source_contact_id' => $this->_contactID
,
863 'subject' => 'Make-it-Happen Meeting',
864 'activity_date_time' => '2009-10-18',
866 'location' => 'Pensulvania',
867 'details' => 'a test activity to check the update api',
869 'activity_name' => 'Test activity type',
870 'version' => $this->_apiversion
,
871 'custom_' . $ids['custom_field_id'] => 'custom string',
873 $result = $this->callAPISuccess('activity', 'create', $params);
875 $activityId = $result['id'];
876 $result = $this->callAPISuccess($this->_entity
, 'get', array('return.custom_' . $ids['custom_field_id'] => 1, 'version' => 3, 'id' => $result['id']));
877 $this->assertEquals("custom string", $result['values'][$result['id']]['custom_' . $ids['custom_field_id']], ' in line ' . __LINE__
);
878 $this->assertEquals("2009-10-18 00:00:00", $result['values'][$result['id']]['activity_date_time'], ' in line ' . __LINE__
);
879 $fields = $this->callAPISuccess('activity', 'getfields', array('version' => $this->_apiversion
));
880 $this->assertTrue(is_array($fields['values']['custom_' . $ids['custom_field_id']]));
882 // Update the activity with custom data
885 'source_contact_id' => $this->_contactID
,
886 'subject' => 'Make-it-Happen Meeting',
888 'activity_name' => 'Test activity type',
889 // add this since dates are messed up
890 'activity_date_time' => date('Ymd'),
891 'custom_' . $ids['custom_field_id'] => 'Updated my test data',
892 'version' => $this->_apiversion
,
894 $result = $this->callAPISuccess('Activity', 'Create', $params);
896 $result = $this->callAPISuccess($this->_entity
, 'get', array('return.custom_' . $ids['custom_field_id'] => 1, 'version' => 3, 'id' => $result['id']));
897 $this->assertEquals("Updated my test data", $result['values'][$result['id']]['custom_' . $ids['custom_field_id']], ' in line ' . __LINE__
);
901 * Test civicrm_activity_update() for core activity fields
902 * and some custom data
904 function testActivityUpdateCheckCoreFields() {
905 $params = $this->_params
;
906 $contact1Params = array(
907 'first_name' => 'John',
908 'middle_name' => 'J.',
909 'last_name' => 'Anderson',
912 'email' => 'john_anderson@civicrm.org',
913 'contact_type' => 'Individual',
916 $contact1 = $this->individualCreate($contact1Params);
917 $contact2Params = array(
918 'first_name' => 'Michal',
919 'middle_name' => 'J.',
920 'last_name' => 'Anderson',
923 'email' => 'michal_anderson@civicrm.org',
924 'contact_type' => 'Individual',
927 $contact2 = $this->individualCreate($contact2Params);
929 $params['assignee_contact_id'] = array($contact1, $contact2);
930 $params['target_contact_id'] = array($contact2 => $contact2);
931 $result = $this->callAPISuccess('Activity', 'Create', $params);
933 $activityId = $result['id'];
935 'return.assignee_contact_id' => 1,
936 'return.target_contact_id' => 1,
937 'version' => $this->_apiversion
,
940 $result = $this->callAPISuccess($this->_entity
, 'get',$getParams );
941 $assignee = $result['values'][$result['id']]['assignee_contact_id'];
942 $target = $result['values'][$result['id']]['target_contact_id'];
943 $this->assertEquals(2, count($assignee), ' in line ' . __LINE__
);
944 $this->assertEquals(1, count($target), ' in line ' . __LINE__
);
945 $this->assertEquals(TRUE, in_array($contact1, $assignee), ' in line ' . __LINE__
);
946 $this->assertEquals(TRUE, in_array($contact2, $target), ' in line ' . __LINE__
);
948 $contact3Params = array(
949 'first_name' => 'Jijo',
950 'middle_name' => 'J.',
951 'last_name' => 'Anderson',
954 'email' => 'jijo_anderson@civicrm.org',
955 'contact_type' => 'Individual',
958 $contact4Params = array(
959 'first_name' => 'Grant',
960 'middle_name' => 'J.',
961 'last_name' => 'Anderson',
964 'email' => 'grant_anderson@civicrm.org',
965 'contact_type' => 'Individual',
968 $contact3 = $this->individualCreate($contact3Params);
969 $contact4 = $this->individualCreate($contact4Params);
972 $params['id'] = $activityId;
973 $params['assignee_contact_id'] = array($contact3 => $contact3);
974 $params['target_contact_id'] = array($contact4 => $contact4);
976 $result = $this->callAPISuccess('activity', 'create', $params);
978 $this->assertEquals($activityId, $result['id'], ' in line ' . __LINE__
);
980 $result = $this->callAPISuccess(
984 'return.assignee_contact_id' => 1,
985 'return.target_contact_id' => 1,
986 'return.source_contact_id' => 1,
987 'id' => $result['id']
991 $assignee = $result['values'][$result['id']]['assignee_contact_id'];
992 $target = $result['values'][$result['id']]['target_contact_id'];
994 $this->assertEquals(1, count($assignee), ' in line ' . __LINE__
);
995 $this->assertEquals(1, count($target), ' in line ' . __LINE__
);
996 $this->assertEquals(TRUE, in_array($contact3, $assignee), ' in line ' . __LINE__
);
997 $this->assertEquals(TRUE, in_array($contact4, $target), ' in line ' . __LINE__
);
999 foreach ($this->_params
as $fld => $val) {
1000 $this->assertEquals($val, $result['values'][$result['id']][$fld]);
1005 * Test civicrm_activity_update() where the DB has a date_time
1006 * value and there is none in the update params.
1008 function testActivityUpdateNotDate() {
1009 $result = $this->callAPISuccess('activity', 'create', $this->_params
);
1012 'id' => $result['id'],
1013 'subject' => 'Make-it-Happen Meeting',
1015 'location' => '21, Park Avenue',
1016 'details' => 'Lets update Meeting',
1018 'source_contact_id' => $this->_contactID
,
1022 $result = $this->callAPISuccess('activity', 'create', $params);
1023 //hack on date comparison - really we should make getAndCheck smarter to handle dates
1024 $params['activity_date_time'] = $this->_params
['activity_date_time'];
1025 // we also unset source_contact_id since it is stored in an aux table
1026 unset($params['source_contact_id']);
1027 $this->getAndCheck($params, $result['id'], 'activity');
1031 * check activity update with status
1033 function testActivityUpdateWithStatus() {
1034 $activity = $this->callAPISuccess('activity', 'create', $this->_params
);
1036 'id' => $activity['id'],
1037 'source_contact_id' => $this->_contactID
,
1038 'subject' => 'Hurry update works',
1040 'activity_name' => 'Test activity type',
1043 $result = $this->callAPISuccess('activity', 'create', $params);
1044 $this->assertEquals($result['id'], $activity['id']);
1045 $this->assertEquals($result['values'][$activity['id']]['subject'], 'Hurry update works');
1046 $this->assertEquals($result['values'][$activity['id']]['status_id'], 1
1051 * Test civicrm_activity_update() where the source_contact_id
1052 * is not in the update params.
1054 function testActivityUpdateKeepSource() {
1055 $activity = $this->callAPISuccess('activity', 'create', $this->_params
);
1056 // Updating the activity but not providing anything for the source contact
1057 // (It was set as $this->_contactID earlier.)
1059 'id' => $activity['id'],
1060 'subject' => 'Updated Make-it-Happen Meeting',
1062 'location' => '21, Park Avenue',
1063 'details' => 'Lets update Meeting',
1065 'activity_name' => 'Test activity type',
1069 $result = $this->callAPISuccess('activity', 'create', $params);
1070 $findactivity = $this->callAPISuccess('Activity', 'Get', array('id' => $activity['id']));
1074 * Test civicrm_activities_contact_get()
1076 function testActivitiesContactGet() {
1077 $activity = $this->callAPISuccess('activity', 'create', $this->_params
);
1078 $activity2 = $this->callAPISuccess('activity', 'create', $this->_params2
);
1079 // Get activities associated with contact $this->_contactID
1081 'contact_id' => $this->_contactID
,
1083 $result = $this->callAPISuccess('activity', 'get', $params);
1085 $this->assertEquals(2, $result['count'], 'In line ' . __LINE__
);
1086 $this->assertEquals($this->test_activity_type_value
, $result['values'][$activity['id']]['activity_type_id'], 'In line ' . __LINE__
);
1087 $this->assertEquals('Test activity type', $result['values'][$activity['id']]['activity_name'], 'In line ' . __LINE__
);
1088 $this->assertEquals('Test activity type', $result['values'][$activity2['id']]['activity_name'], 'In line ' . __LINE__
);
1091 * test chained Activity format
1093 function testchainedActivityGet() {
1095 $activity = $this->callAPISuccess('Contact', 'Create', array(
1096 'display_name' => "bob brown",
1097 'contact_type' => 'Individual',
1098 'api.activity_type.create' => array(
1100 'label' => 'send out letters',
1105 ), 'api.activity.create' => array('subject' => 'send letter', 'activity_type_id' => '$value.api.activity_type.create.values.0.value'),
1108 $result = $this->callAPISuccess('Activity', 'Get', array(
1109 'id' => $activity['id'],
1110 'return.assignee_contact_id' => 1,
1111 'api.contact.get' => array('api.pledge.get' => 1),
1116 * Test civicrm_activity_contact_get() with invalid Contact Id
1118 function testActivitiesContactGetWithInvalidContactId() {
1119 $params = array('contact_id' => 'contact');
1120 $result = $this->callAPIFailure('activity', 'get', $params);
1124 * Test civicrm_activity_contact_get() with contact having no Activity
1126 function testActivitiesContactGetHavingNoActivity() {
1128 'first_name' => 'dan',
1129 'last_name' => 'conberg',
1130 'email' => 'dan.conberg@w.co.in',
1131 'contact_type' => 'Individual',
1134 $contact = $this->callAPISuccess('contact', 'create', $params);
1136 'contact_id' => $contact['id'],
1138 $result = $this->callAPISuccess('activity', 'get', $params);
1139 $this->assertEquals($result['count'], 0, 'in line ' . __LINE__
);
1142 function testGetFields() {
1143 $params = array('action' => 'create');
1144 $result = $this->callAPIAndDocument('activity', 'getfields', $params, __FUNCTION__
, __FILE__
, NULL, NULL, 'getfields');
1145 $this->assertTrue(is_array($result['values']), 'get fields doesnt return values array in line ' . __LINE__
);
1146 // $this->assertTrue(is_array($result['values']['priority_id']['options']));
1147 foreach ($result['values'] as $key => $value) {
1148 $this->assertTrue(is_array($value), $key . " is not an array in line " . __LINE__
);