4 * File for the TestActivity class
8 * @author Walt Haas <walt@dharmatech.org> (801) 534-1262
9 * @copyright Copyright CiviCRM LLC (C) 2009
10 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html
11 * GNU Affero General Public License version 3
12 * @version $Id: ActivityTest.php 31254 2010-12-15 10:09:29Z eileen $
15 * This file is part of CiviCRM
17 * CiviCRM is free software; you can redistribute it and/or
18 * modify it under the terms of the GNU Affero General Public License
19 * as published by the Free Software Foundation; either version 3 of
20 * the License, or (at your option) any later version.
22 * CiviCRM is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU Affero General Public License for more details.
27 * You should have received a copy of the GNU Affero General Public
28 * License along with this program. If not, see
29 * <http://www.gnu.org/licenses/>.
33 * Include class definitions
37 * Test APIv3 civicrm_activity_* functions
39 * @package CiviCRM_APIv3
40 * @subpackage API_Activity
43 class api_v3_ActivityTest
extends CiviUnitTestCase
{
46 protected $_entity = 'activity';
47 protected $_apiversion = 3;
48 protected $test_activity_type_value;
49 protected $_contactID;
52 * Test setup for every test.
54 * Connect to the database, truncate the tables that will be used
55 * and redirect stdin to a temporary file
57 public function setUp() {
58 // Connect to the database
61 $this->_contactID
= $this->individualCreate();
62 //create activity types
63 $activityTypes = $this->callAPISuccess('option_value', 'create', array(
64 'option_group_id' => 2,
65 'name' => 'Test activity type',
66 'label' => 'Test activity type',
69 $this->test_activity_type_value
= $activityTypes['values'][0]['value'];
70 $this->test_activity_type_id
= $activityTypes['id'];
71 $this->_params
= array(
72 'source_contact_id' => $this->_contactID
,
73 'activity_type_id' => $this->test_activity_type_value
,
74 'subject' => 'test activity type id',
75 'activity_date_time' => '2011-06-02 14:36:13',
79 'location' => 'Pennsylvania',
80 'details' => 'a test activity',
82 $this->_params2
= array(
83 'source_contact_id' => $this->_contactID
,
84 'subject' => 'Eat & drink',
85 'activity_date_time' => date('Ymd'),
87 'location' => 'Napier',
88 'details' => 'discuss & eat',
90 'activity_type_id' => $this->test_activity_type_value
,
92 // create a logged in USER since the code references it for source_contact_id
93 $this->createLoggedInUser();
97 * Tears down the fixture, for example, closes a network connection.
99 * This method is called after a test is executed.
101 public function tearDown() {
102 $tablesToTruncate = array(
105 'civicrm_activity_contact',
108 $this->quickCleanup($tablesToTruncate, TRUE);
109 $type = $this->callAPISuccess('optionValue', 'get', array('id' => $this->test_activity_type_id
));
110 if (!empty($type['count'])) {
111 $this->callAPISuccess('option_value', 'delete', array('id' => $this->test_activity_type_id
));
116 * Check fails with empty array.
118 public function testActivityCreateEmpty() {
119 $this->callAPIFailure('activity', 'create', array());
123 * Check if required fields are not passed.
125 public function testActivityCreateWithoutRequired() {
127 'subject' => 'this case should fail',
128 'scheduled_date_time' => date('Ymd'),
130 $result = $this->callAPIFailure('activity', 'create', $params);
134 * Test civicrm_activity_create() with mismatched activity_type_id
137 public function testActivityCreateMismatchNameType() {
139 'source_contact_id' => $this->_contactID
,
140 'subject' => 'Test activity',
141 'activity_date_time' => date('Ymd'),
143 'location' => 'Pennsylvania',
144 'details' => 'a test activity',
146 'activity_name' => 'Fubar activity type',
147 'activity_type_id' => 5,
148 'scheduled_date_time' => date('Ymd'),
151 $result = $this->callAPIFailure('activity', 'create', $params);
155 * Test civicrm_activity_id() with missing source_contact_id is put with the current user.
157 public function testActivityCreateWithMissingContactId() {
159 'subject' => 'Make-it-Happen Meeting',
160 'activity_date_time' => date('Ymd'),
162 'location' => 'Pennsylvania',
163 'details' => 'a test activity',
165 'activity_name' => 'Test activity type',
168 $this->callAPISuccess('activity', 'create', $params);
172 * Test civicrm_activity_id() with non-numeric source_contact_id.
174 public function testActivityCreateWithNonNumericContactId() {
176 'source_contact_id' => 'fubar',
177 'subject' => 'Make-it-Happen Meeting',
178 'activity_date_time' => date('Ymd'),
180 'location' => 'Pennsylvania',
181 'details' => 'a test activity',
183 'activity_name' => 'Test activity type',
186 $this->callAPIFailure('activity', 'create', $params);
190 * Ensure that an invalid activity type causes failure.
192 * Oddly enough this test was failing because the creation of the invalid type
193 * got added to the set up routine. Probably a mis-fix on a test
195 public function testActivityCreateWithNonNumericActivityTypeId() {
197 'source_contact_id' => $this->_contactID
,
198 'subject' => 'Make-it-Happen Meeting',
199 'activity_date_time' => date('Ymd'),
201 'location' => 'Pennsylvania',
202 'details' => 'a test activity',
204 'activity_type_id' => 'Invalid Test activity type',
207 $result = $this->callAPIFailure('activity', 'create', $params);
211 * Check with incorrect required fields.
213 public function testActivityCreateWithUnknownActivityTypeId() {
215 'source_contact_id' => $this->_contactID
,
216 'subject' => 'Make-it-Happen Meeting',
217 'activity_date_time' => date('Ymd'),
219 'location' => 'Pennsylvania',
220 'details' => 'a test activity',
222 'activity_type_id' => 699,
225 $result = $this->callAPIFailure('activity', 'create', $params);
228 public function testActivityCreateWithInvalidPriority() {
230 'source_contact_id' => $this->_contactID
,
231 'subject' => 'Make-it-Happen Meeting',
232 'activity_date_time' => date('Ymd'),
234 'location' => 'Pennsylvania',
235 'details' => 'a test activity',
238 'activity_type_id' => 1,
241 $result = $this->callAPIFailure('activity', 'create', $params,
242 "'44' is not a valid option for field priority_id");
243 $this->assertEquals('priority_id', $result['error_field']);
248 * Test create succeeds with valid string for priority.
250 public function testActivityCreateWithValidStringPriority() {
252 'source_contact_id' => $this->_contactID
,
253 'subject' => 'Make-it-Happen Meeting',
254 'activity_date_time' => date('Ymd'),
256 'location' => 'Pennsylvania',
257 'details' => 'a test activity',
259 'priority_id' => 'Urgent',
260 'activity_type_id' => 1,
263 $result = $this->callAPISuccess('activity', 'create', $params);
264 $this->assertEquals(1, $result['values'][$result['id']]['priority_id']);
268 * Test create fails with invalid priority string.
270 public function testActivityCreateWithInValidStringPriority() {
272 'source_contact_id' => $this->_contactID
,
273 'subject' => 'Make-it-Happen Meeting',
274 'activity_date_time' => date('Ymd'),
276 'location' => 'Pennsylvania',
277 'details' => 'a test activity',
279 'priority_id' => 'ergUrgent',
280 'activity_type_id' => 1,
283 $this->callAPIFailure('activity', 'create', $params,
284 "'ergUrgent' is not a valid option for field priority_id");
288 * Test civicrm_activity_create() with valid parameters.
290 public function testActivityCreate() {
292 $this->callAPISuccess('activity', 'create', $this->_params
);
293 $result = $this->callAPISuccess('activity', 'get', $this->_params
);
294 $this->assertEquals($result['values'][$result['id']]['duration'], 120);
295 $this->assertEquals($result['values'][$result['id']]['subject'], 'test activity type id');
296 $this->assertEquals($result['values'][$result['id']]['activity_date_time'], '2011-06-02 14:36:13');
297 $this->assertEquals($result['values'][$result['id']]['location'], 'Pennsylvania');
298 $this->assertEquals($result['values'][$result['id']]['details'], 'a test activity');
299 $this->assertEquals($result['values'][$result['id']]['status_id'], 2);
300 $this->assertEquals($result['values'][$result['id']]['id'], $result['id']);
304 * Test civicrm_activity_create() with valid parameters - use type_id.
306 public function testActivityCreateCampaignTypeID() {
307 $this->enableCiviCampaign();
310 'source_contact_id' => $this->_contactID
,
311 'subject' => 'Make-it-Happen Meeting',
312 'activity_date_time' => '20110316',
314 'location' => 'Pennsylvania',
315 'details' => 'a test activity',
317 'activity_type_id' => 29,
320 $result = $this->callAPISuccess('activity', 'create', $params);
322 $result = $this->callAPISuccess('activity', 'getsingle', array('id' => $result['id']));
323 $this->assertEquals($result['duration'], 120);
324 $this->assertEquals($result['subject'], 'Make-it-Happen Meeting');
325 $this->assertEquals($result['activity_date_time'], '2011-03-16 00:00:00');
326 $this->assertEquals($result['location'], 'Pennsylvania');
327 $this->assertEquals($result['details'], 'a test activity');
328 $this->assertEquals($result['status_id'], 1);
330 $priorities = $this->callAPISuccess('activity', 'getoptions', array('field' => 'priority_id'));
331 $this->assertEquals($result['priority_id'], array_search('Normal', $priorities['values']));
335 * Test get returns target and assignee contacts.
337 public function testActivityReturnTargetAssignee() {
339 $description = "Demonstrates setting & retrieving activity target & source.";
340 $subfile = "GetTargetandAssignee";
342 'source_contact_id' => $this->_contactID
,
343 'subject' => 'Make-it-Happen Meeting',
344 'activity_date_time' => '20110316',
346 'location' => 'Pennsylvania',
347 'details' => 'a test activity',
349 'activity_type_id' => 1,
351 'target_contact_id' => $this->_contactID
,
352 'assignee_contact_id' => $this->_contactID
,
355 $result = $this->callAPIAndDocument('activity', 'create', $params, __FUNCTION__
, __FILE__
, $description, $subfile);
356 $result = $this->callAPISuccess('activity', 'get', array(
357 'id' => $result['id'],
358 'version' => $this->_apiversion
,
359 'return.assignee_contact_id' => 1,
360 'return.target_contact_id' => 1,
363 $this->assertEquals($this->_contactID
, $result['values'][$result['id']]['assignee_contact_id'][0]);
364 $this->assertEquals($this->_contactID
, $result['values'][$result['id']]['target_contact_id'][0]);
368 * Test get returns target and assignee contact names.
370 public function testActivityReturnTargetAssigneeName() {
372 $description = "Demonstrates retrieving activity target & source contact names.";
373 $subfile = "GetTargetandAssigneeName";
374 $target1 = $this->callAPISuccess('Contact', 'create', array('contact_type' => 'Individual', 'first_name' => 'A', 'last_name' => 'Cat'));
375 $target2 = $this->callAPISuccess('Contact', 'create', array('contact_type' => 'Individual', 'first_name' => 'B', 'last_name' => 'Good'));
376 $assignee = $this->callAPISuccess('Contact', 'create', array('contact_type' => 'Individual', 'first_name' => 'C', 'last_name' => 'Shore'));
377 $source = $this->callAPISuccess('Contact', 'create', array('contact_type' => 'Individual', 'first_name' => 'D', 'last_name' => 'Bug'));
380 'source_contact_id' => $source['id'],
381 'subject' => 'Make-it-Happen Meeting',
382 'activity_date_time' => '20170316',
384 'activity_type_id' => 1,
385 'target_contact_id' => array($target1['id'], $target2['id']),
386 'assignee_contact_id' => $assignee['id'],
389 $result = $this->callAPISuccess('activity', 'create', $params);
390 $result = $this->callAPIAndDocument('activity', 'getsingle', array(
391 'id' => $result['id'],
392 'return' => array('source_contact_name', 'target_contact_name', 'assignee_contact_name', 'subject'),
393 ), __FUNCTION__
, __FILE__
, $description, $subfile);
395 $this->assertEquals($params['subject'], $result['subject']);
396 $this->assertEquals($source['id'], $result['source_contact_id']);
397 $this->assertEquals('D Bug', $result['source_contact_name']);
398 $this->assertEquals('A Cat', $result['target_contact_name'][$target1['id']]);
399 $this->assertEquals('B Good', $result['target_contact_name'][$target2['id']]);
400 $this->assertEquals('C Shore', $result['assignee_contact_name'][$assignee['id']]);
401 $this->assertEquals($assignee['id'], $result['assignee_contact_id'][0]);
405 * Test civicrm_activity_create() using example code.
407 public function testActivityCreateExample() {
408 require_once 'api/v3/examples/Activity/Create.php';
409 $result = activity_create_example();
410 $expectedResult = activity_create_expectedresult();
411 $this->assertEquals($result, $expectedResult);
415 * Test civicrm_activity_create() with valid parameters and custom data.
417 public function testActivityCreateCustom() {
418 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__
, __FILE__
);
419 $params = $this->_params
;
420 $params['custom_' . $ids['custom_field_id']] = "custom string";
421 $result = $this->callAPIAndDocument($this->_entity
, 'create', $params, __FUNCTION__
, __FILE__
);
422 $result = $this->callAPISuccess($this->_entity
, 'get', array(
423 'return.custom_' . $ids['custom_field_id'] => 1,
424 'id' => $result['id'],
426 $this->assertEquals("custom string", $result['values'][$result['id']]['custom_' . $ids['custom_field_id']], ' in line ' . __LINE__
);
428 $this->customFieldDelete($ids['custom_field_id']);
429 $this->customGroupDelete($ids['custom_group_id']);
433 * Test civicrm_activity_create() with valid parameters and custom data.
435 public function testActivityCreateCustomSubType() {
436 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__
, __FILE__
);
437 $this->callAPISuccess('CustomGroup', 'create', array(
438 'extends_entity_column_value' => $this->test_activity_type_value
,
439 'id' => $ids['custom_group_id'],
440 'extends' => 'Activity',
443 $params = $this->_params
;
444 $params['custom_' . $ids['custom_field_id']] = "custom string";
445 $result = $this->callAPIAndDocument($this->_entity
, 'create', $params, __FUNCTION__
, __FILE__
);
446 $result = $this->callAPISuccess($this->_entity
, 'get', array(
447 'return.custom_' . $ids['custom_field_id'] => 1,
448 'id' => $result['id'],
450 $this->assertEquals("custom string", $result['values'][$result['id']]['custom_' . $ids['custom_field_id']]);
452 $this->customFieldDelete($ids['custom_field_id']);
453 $this->customGroupDelete($ids['custom_group_id']);
457 * Test civicrm_activity_create() with valid parameters and custom data.
459 public function testActivityCreateCustomContactRefField() {
461 $this->callAPISuccess('contact', 'create', array('id' => $this->_contactID
, 'sort_name' => 'Contact, Test'));
462 $subfile = 'ContactRefCustomField';
463 $description = "Demonstrates create with Contact Reference Custom Field.";
464 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__
, __FILE__
);
466 'custom_group_id' => $ids['custom_group_id'],
467 'name' => 'Worker_Lookup',
468 'label' => 'Worker Lookup',
469 'html_type' => 'Autocomplete-Select',
470 'data_type' => 'ContactReference',
472 'is_searchable' => 1,
476 $customField = $this->callAPISuccess('custom_field', 'create', $params);
477 $params = $this->_params
;
478 $params['custom_' . $customField['id']] = "$this->_contactID";
480 $result = $this->callAPIAndDocument($this->_entity
, 'create', $params, __FUNCTION__
, __FILE__
, $description, $subfile);
481 $result = $this->callAPIAndDocument($this->_entity
, 'get', array(
482 'return.custom_' . $customField['id'] => 1,
483 'id' => $result['id'],
484 ), __FUNCTION__
, __FILE__
, 'Get with Contact Ref Custom Field', 'ContactRefCustomFieldGet');
486 $this->assertEquals('Anderson, Anthony', $result['values'][$result['id']]['custom_' . $customField['id']]);
487 $this->assertEquals($this->_contactID
, $result['values'][$result['id']]['custom_' . $customField['id'] . "_id"], ' in line ' . __LINE__
);
488 $this->assertEquals('Anderson, Anthony', $result['values'][$result['id']]['custom_' . $customField['id'] . '_1'], ' in line ' . __LINE__
);
489 $this->assertEquals($this->_contactID
, $result['values'][$result['id']]['custom_' . $customField['id'] . "_1_id"], ' in line ' . __LINE__
);
490 $this->customFieldDelete($ids['custom_field_id']);
491 $this->customGroupDelete($ids['custom_group_id']);
495 * Test civicrm_activity_create() with an invalid text status_id.
497 public function testActivityCreateBadTextStatus() {
500 'source_contact_id' => $this->_contactID
,
501 'subject' => 'Discussion on Apis for v3',
502 'activity_date_time' => date('Ymd'),
504 'location' => 'Pennsylvania',
505 'details' => 'a test activity',
506 'status_id' => 'Invalid',
507 'activity_name' => 'Test activity type',
510 $this->callAPIFailure('activity', 'create', $params);
514 * Test civicrm_activity_create() with an invalid text status_id.
516 public function testActivityCreateSupportActivityStatus() {
519 'source_contact_id' => $this->_contactID
,
520 'subject' => 'Discussion on Apis for v3',
521 'activity_date_time' => date('Ymd'),
523 'location' => 'Pennsylvania',
524 'details' => 'a test activity',
525 'activity_status_id' => 'Invalid',
526 'activity_name' => 'Test activity type',
529 $result = $this->callAPIFailure('activity', 'create', $params,
530 "'Invalid' is not a valid option for field status_id");
535 * Test civicrm_activity_create() with using a text status_id.
537 public function testActivityCreateTextStatus() {
540 'source_contact_id' => $this->_contactID
,
541 'subject' => 'Make-it-Happen Meeting',
542 'activity_date_time' => date('Ymd'),
544 'location' => 'Pennsylvania',
545 'details' => 'a test activity',
546 'status_id' => 'Scheduled',
547 'activity_name' => 'Test activity type',
550 $result = $this->callAPISuccess('activity', 'create', $params);
551 $this->assertEquals($result['values'][$result['id']]['duration'], 120);
552 $this->assertEquals($result['values'][$result['id']]['subject'], 'Make-it-Happen Meeting');
553 $this->assertEquals($result['values'][$result['id']]['activity_date_time'], date('Ymd') . '000000');
554 $this->assertEquals($result['values'][$result['id']]['location'], 'Pennsylvania');
555 $this->assertEquals($result['values'][$result['id']]['details'], 'a test activity');
559 * Test civicrm_activity_get() with no params
561 public function testActivityGetEmpty() {
562 $result = $this->callAPISuccess('activity', 'get', array());
566 * Test civicrm_activity_get() with a good activity ID
568 public function testActivityGetGoodID1() {
569 // Insert rows in civicrm_activity creating activities 4 and 13
570 $description = "Demonstrates getting assignee_contact_id & using it to get the contact.";
571 $subfile = 'ReturnAssigneeContact';
572 $activity = $this->callAPISuccess('activity', 'create', $this->_params
);
574 $contact = $this->callAPISuccess('Contact', 'Create', array(
575 'first_name' => "The Rock",
576 'last_name' => 'roccky',
577 'contact_type' => 'Individual',
579 'api.activity.create' => array(
580 'id' => $activity['id'],
581 'assignee_contact_id' => '$value.id',
586 'activity_id' => $activity['id'],
587 'version' => $this->_apiversion
,
589 'return.assignee_contact_id' => 1,
590 'api.contact.get' => array(
591 'id' => '$value.source_contact_id',
595 $result = $this->callAPIAndDocument('Activity', 'Get', $params, __FUNCTION__
, __FILE__
, $description, $subfile);
597 $this->assertEquals($activity['id'], $result['id']);
599 $this->assertEquals($contact['id'], $result['values'][0]['assignee_contact_id'][0]);
601 $this->assertEquals($this->_contactID
, $result['values'][0]['api.contact.get']['values'][0]['contact_id']);
602 $this->assertEquals($this->test_activity_type_value
, $result['values'][0]['activity_type_id']);
603 $this->assertEquals("test activity type id", $result['values'][0]['subject']);
607 * test that get functioning does filtering.
609 public function testGetFilter() {
611 'source_contact_id' => $this->_contactID
,
612 'subject' => 'Make-it-Happen Meeting',
613 'activity_date_time' => '20110316',
615 'location' => 'Pennsylvania',
616 'details' => 'a test activity',
618 'activity_name' => 'Test activity type',
621 $result = $this->callAPISuccess('Activity', 'Create', $params);
622 $this->callAPISuccess('Activity', 'Get', array('subject' => 'Make-it-Happen Meeting'));
623 $this->assertEquals(1, $result['count']);
624 $this->assertEquals('Make-it-Happen Meeting', $result['values'][$result['id']]['subject']);
625 $this->callAPISuccess('Activity', 'Delete', array('id' => $result['id']));
630 * Test civicrm_activity_get() with filter target_contact_id
632 public function testActivityGetTargetFilter() {
633 $params = $this->_params
;
634 $contact1Params = array(
635 'first_name' => 'John',
636 'middle_name' => 'J.',
637 'last_name' => 'Anderson',
640 'email' => 'john_anderson@civicrm.org',
641 'contact_type' => 'Individual',
644 $contact1 = $this->individualCreate($contact1Params);
645 $contact2Params = array(
646 'first_name' => 'Michal',
647 'middle_name' => 'J.',
648 'last_name' => 'Anderson',
651 'email' => 'michal_anderson@civicrm.org',
652 'contact_type' => 'Individual',
655 $contact2 = $this->individualCreate($contact2Params);
657 $params['assignee_contact_id'] = array($contact1, $contact2);
658 $params['target_contact_id'] = array($contact2 => $contact2);
659 $activity = $this->callAPISuccess('Activity', 'Create', $params);
661 $activityget = $this->callAPISuccess('Activity', 'get', array(
662 'id' => $activity['id'],
663 'target_contact_id' => $contact2,
664 'return.target_contact_id' => 1,
666 $this->assertEquals($activity['id'], $activityget['id']);
667 $this->assertEquals($contact2, $activityget['values'][$activityget['id']]['target_contact_id'][0]);
669 $activityget = $this->callAPISuccess('activity', 'get', array(
670 'target_contact_id' => $this->_contactID
,
671 'return.target_contact_id' => 1,
672 'id' => $activity['id'],
674 if ($activityget['count'] > 0) {
675 $this->assertNotEquals($contact2, $activityget['values'][$activityget['id']]['target_contact_id'][0]);
680 * Test that activity.get api works when filtering on subject.
682 public function testActivityGetSubjectFilter() {
683 $subject = 'test activity ' . __FUNCTION__
. mt_rand();
684 $params = $this->_params
;
685 $params['subject'] = $subject;
686 $activity = $this->callAPISuccess('Activity', 'Create', $params);
687 $activityget = $this->callAPISuccess('activity', 'getsingle', array(
688 'subject' => $subject,
690 $this->assertEquals($activityget['subject'], $subject);
694 * Test that activity.get api works when filtering on details.
696 public function testActivityGetDetailsFilter() {
697 $details = 'test activity ' . __FUNCTION__
. mt_rand();
698 $params = $this->_params
;
699 $params['details'] = $details;
700 $activity = $this->callAPISuccess('Activity', 'Create', $params);
701 $activityget = $this->callAPISuccess('activity', 'getsingle', array(
702 'details' => $details,
704 $this->assertEquals($activityget['details'], $details);
708 * Test that activity.get api works when filtering on tag.
710 public function testActivityGetTagFilter() {
711 $tag = $this->callAPISuccess('Tag', 'create', array('name' => mt_rand(), 'used_for' => 'Activities'));
712 $activity = $this->callAPISuccess('Activity', 'Create', $this->_params
);
713 $this->callAPISuccess('EntityTag', 'create', array('entity_table' => 'civicrm_activity', 'tag_id' => $tag['id'], 'entity_id' => $activity['id']));
714 $activityget = $this->callAPISuccess('activity', 'getsingle', array(
715 'tag_id' => $tag['id'],
717 $this->assertEquals($activityget['id'], $activity['id']);
721 * test that get functioning does filtering.
723 public function testGetStatusID() {
725 'source_contact_id' => $this->_contactID
,
726 'subject' => 'Make-it-Happen Meeting',
727 'activity_date_time' => '20110316',
729 'location' => 'Pennsylvania',
730 'details' => 'a test activity',
732 'activity_name' => 'Test activity type',
735 $this->callAPISuccess('Activity', 'Create', $params);
736 $result = $this->callAPISuccess('Activity', 'Get', array('activity_status_id' => '1'));
737 $this->assertEquals(1, $result['count'], 'one activity of status 1 should exist');
739 $result = $this->callAPISuccess('Activity', 'Get', array('status_id' => '1'));
740 $this->assertEquals(1, $result['count'], 'status_id should also work');
742 $result = $this->callAPISuccess('Activity', 'Get', array('activity_status_id' => '2'));
743 $this->assertEquals(0, $result['count'], 'No activities of status 1 should exist');
744 $result = $this->callAPISuccess('Activity', 'Get', array(
745 'version' => $this->_apiversion
,
748 $this->assertEquals(0, $result['count'], 'No activities of status 1 should exist');
753 * test that get functioning does filtering.
755 public function testGetFilterMaxDate() {
757 'source_contact_id' => $this->_contactID
,
758 'subject' => 'Make-it-Happen Meeting',
759 'activity_date_time' => '20110101',
761 'location' => 'Pennsylvania',
762 'details' => 'a test activity',
764 'activity_name' => 'Test activity type',
765 'version' => $this->_apiversion
,
768 $activityOne = $this->callAPISuccess('Activity', 'Create', $params);
769 $params['activity_date_time'] = 20120216;
770 $activityTwo = $this->callAPISuccess('Activity', 'Create', $params);
771 $result = $this->callAPISuccess('Activity', 'Get', array(
774 $description = "Demonstrates _low filter (at time of writing doesn't work if contact_id is set.";
775 $subfile = "DateTimeLow";
776 $this->assertEquals(2, $result['count']);
779 'filter.activity_date_time_low' => '20120101000000',
782 $result = $this->callAPIAndDocument('Activity', 'Get', $params, __FUNCTION__
, __FILE__
, $description, $subfile);
783 $this->assertEquals(1, $result['count']);
784 $description = "Demonstrates _high filter (at time of writing doesn't work if contact_id is set.";
785 $subfile = "DateTimeHigh";
786 $this->assertEquals('2012-02-16 00:00:00', $result['values'][0]['activity_date_time']);
788 'source_contact_id' => $this->_contactID
,
790 'filter.activity_date_time_high' => '20120101000000',
793 $result = $this->callAPIAndDocument('Activity', 'Get', $params, __FUNCTION__
, __FILE__
, $description, $subfile);
795 $this->assertEquals(1, $result['count']);
796 $this->assertEquals('2011-01-01 00:00:00', $result['values'][0]['activity_date_time']);
798 $this->callAPISuccess('Activity', 'Delete', array('version' => 3, 'id' => $activityOne['id']));
799 $this->callAPISuccess('Activity', 'Delete', array('version' => 3, 'id' => $activityTwo['id']));
803 * Test civicrm_activity_get() with a good activity ID which
804 * has associated custom data
806 public function testActivityGetGoodIDCustom() {
807 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__
, __FILE__
);
809 $params = $this->_params
;
810 $params['custom_' . $ids['custom_field_id']] = "custom string";
812 $this->callAPISuccess($this->_entity
, 'create', $params);
814 // Retrieve the test value.
816 'activity_type_id' => $this->test_activity_type_value
,
818 'return.custom_' . $ids['custom_field_id'] => 1,
820 $result = $this->callAPIAndDocument('activity', 'get', $params, __FUNCTION__
, __FILE__
);
821 $this->assertEquals("custom string", $result['values'][0]['custom_' . $ids['custom_field_id']]);
823 $this->assertEquals($this->test_activity_type_value
, $result['values'][0]['activity_type_id']);
824 $this->assertEquals('test activity type id', $result['values'][0]['subject']);
825 $this->customFieldDelete($ids['custom_field_id']);
826 $this->customGroupDelete($ids['custom_group_id']);
830 * Test civicrm_activity_get() with a good activity ID which
831 * has associated custom data
833 public function testActivityGetContact_idCustom() {
834 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__
, __FILE__
);
836 $params = $this->_params
;
837 $params['custom_' . $ids['custom_field_id']] = "custom string";
839 $result = $this->callAPISuccess($this->_entity
, 'create', $params);
840 // Retrieve the test value
842 'contact_id' => $this->_params
['source_contact_id'],
843 'activity_type_id' => $this->test_activity_type_value
,
845 'return.custom_' . $ids['custom_field_id'] => 1,
847 $result = $this->callAPIAndDocument('activity', 'get', $params, __FUNCTION__
, __FILE__
);
848 $this->assertEquals("custom string", $result['values'][0]['custom_' . $ids['custom_field_id']], ' in line ' . __LINE__
);
850 $this->assertEquals($this->test_activity_type_value
, $result['values'][0]['activity_type_id']);
851 $this->assertEquals('test activity type id', $result['values'][0]['subject']);
852 $this->assertEquals($result['values'][0]['id'], $result['id']);
856 * Check activity deletion with empty params.
858 public function testDeleteActivityForEmptyParams() {
859 $params = array('version' => $this->_apiversion
);
860 $this->callAPIFailure('activity', 'delete', $params);
864 * Check activity deletion without activity id.
866 public function testDeleteActivityWithoutId() {
868 'activity_name' => 'Meeting',
869 'version' => $this->_apiversion
,
871 $result = $this->callAPIFailure('activity', 'delete', $params);
875 * Check activity deletion without activity type.
877 public function testDeleteActivityWithoutActivityType() {
878 $params = array('id' => 1);
879 $result = $this->callAPIFailure('activity', 'delete', $params);
883 * Check activity deletion with incorrect data.
885 public function testDeleteActivityWithIncorrectActivityType() {
888 'activity_name' => 'Test Activity',
891 $result = $this->callAPIFailure('activity', 'delete', $params);
895 * Check activity deletion with correct data.
897 public function testDeleteActivity() {
898 $result = $this->callAPISuccess('activity', 'create', $this->_params
);
900 'id' => $result['id'],
901 'version' => $this->_apiversion
,
904 $this->callAPIAndDocument('activity', 'delete', $params, __FUNCTION__
, __FILE__
);
908 * Check if required fields are not passed.
910 public function testActivityUpdateWithoutRequired() {
912 'subject' => 'this case should fail',
913 'scheduled_date_time' => date('Ymd'),
916 $result = $this->callAPIFailure('activity', 'create', $params);
920 * Test civicrm_activity_update() with non-numeric id
922 public function testActivityUpdateWithNonNumericId() {
924 'id' => 'lets break it',
925 'activity_name' => 'Meeting',
926 'subject' => 'this case should fail',
927 'scheduled_date_time' => date('Ymd'),
930 $result = $this->callAPIFailure('activity', 'create', $params);
934 * Check with incorrect required fields.
936 public function testActivityUpdateWithIncorrectContactActivityType() {
939 'activity_name' => 'Test Activity',
940 'subject' => 'this case should fail',
941 'scheduled_date_time' => date('Ymd'),
942 'source_contact_id' => $this->_contactID
,
945 $result = $this->callAPIFailure('activity', 'create', $params,
946 'Invalid Activity Id');
950 * Test civicrm_activity_update() to update an existing activity
952 public function testActivityUpdate() {
953 $result = $this->callAPISuccess('activity', 'create', $this->_params
);
956 'id' => $result['id'],
957 'subject' => 'Make-it-Happen Meeting',
958 'activity_date_time' => '20091011123456',
960 'location' => '21, Park Avenue',
961 'details' => 'Lets update Meeting',
963 'source_contact_id' => $this->_contactID
,
967 $result = $this->callAPISuccess('activity', 'create', $params);
968 //hack on date comparison - really we should make getAndCheck smarter to handle dates
969 $params['activity_date_time'] = '2009-10-11 12:34:56';
970 // we also unset source_contact_id since it is stored in an aux table
971 unset($params['source_contact_id']);
972 $this->getAndCheck($params, $result['id'], 'activity');
976 * Test civicrm_activity_update() with valid parameters
977 * and some custom data
979 public function testActivityUpdateCustom() {
980 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__
, __FILE__
);
982 $params = $this->_params
;
984 // Create an activity with custom data
985 //this has been updated from the previous 'old format' function - need to make it work
987 'source_contact_id' => $this->_contactID
,
988 'subject' => 'Make-it-Happen Meeting',
989 'activity_date_time' => '2009-10-18',
991 'location' => 'Pennsylvania',
992 'details' => 'a test activity to check the update api',
994 'activity_name' => 'Test activity type',
995 'version' => $this->_apiversion
,
996 'custom_' . $ids['custom_field_id'] => 'custom string',
998 $result = $this->callAPISuccess('activity', 'create', $params);
1000 $activityId = $result['id'];
1001 $result = $this->callAPISuccess($this->_entity
, 'get', array(
1002 'return.custom_' . $ids['custom_field_id'] => 1,
1004 'id' => $result['id'],
1006 $this->assertEquals("custom string", $result['values'][$result['id']]['custom_' . $ids['custom_field_id']]);
1007 $this->assertEquals("2009-10-18 00:00:00", $result['values'][$result['id']]['activity_date_time']);
1008 $fields = $this->callAPISuccess('activity', 'getfields', array('version' => $this->_apiversion
));
1009 $this->assertTrue(is_array($fields['values']['custom_' . $ids['custom_field_id']]));
1011 // Update the activity with custom data.
1013 'id' => $activityId,
1014 'source_contact_id' => $this->_contactID
,
1015 'subject' => 'Make-it-Happen Meeting',
1017 'activity_name' => 'Test activity type',
1018 // add this since dates are messed up
1019 'activity_date_time' => date('Ymd'),
1020 'custom_' . $ids['custom_field_id'] => 'Updated my test data',
1021 'version' => $this->_apiversion
,
1023 $result = $this->callAPISuccess('Activity', 'Create', $params);
1025 $result = $this->callAPISuccess($this->_entity
, 'get', array(
1026 'return.custom_' . $ids['custom_field_id'] => 1,
1028 'id' => $result['id'],
1030 $this->assertEquals("Updated my test data", $result['values'][$result['id']]['custom_' . $ids['custom_field_id']]);
1034 * Test civicrm_activity_update() for core activity fields
1035 * and some custom data
1037 public function testActivityUpdateCheckCoreFields() {
1038 $params = $this->_params
;
1039 $contact1Params = array(
1040 'first_name' => 'John',
1041 'middle_name' => 'J.',
1042 'last_name' => 'Anderson',
1045 'email' => 'john_anderson@civicrm.org',
1046 'contact_type' => 'Individual',
1049 $contact1 = $this->individualCreate($contact1Params);
1050 $contact2Params = array(
1051 'first_name' => 'Michal',
1052 'middle_name' => 'J.',
1053 'last_name' => 'Anderson',
1056 'email' => 'michal_anderson@civicrm.org',
1057 'contact_type' => 'Individual',
1060 $contact2 = $this->individualCreate($contact2Params);
1062 $params['assignee_contact_id'] = array($contact1, $contact2);
1063 $params['target_contact_id'] = array($contact2 => $contact2);
1064 $result = $this->callAPISuccess('Activity', 'Create', $params);
1066 $activityId = $result['id'];
1068 'return.assignee_contact_id' => 1,
1069 'return.target_contact_id' => 1,
1070 'version' => $this->_apiversion
,
1071 'id' => $activityId,
1073 $result = $this->callAPISuccess($this->_entity
, 'get', $getParams);
1074 $assignee = $result['values'][$result['id']]['assignee_contact_id'];
1075 $target = $result['values'][$result['id']]['target_contact_id'];
1076 $this->assertEquals(2, count($assignee), ' in line ' . __LINE__
);
1077 $this->assertEquals(1, count($target), ' in line ' . __LINE__
);
1078 $this->assertEquals(TRUE, in_array($contact1, $assignee), ' in line ' . __LINE__
);
1079 $this->assertEquals(TRUE, in_array($contact2, $target), ' in line ' . __LINE__
);
1081 $contact3Params = array(
1082 'first_name' => 'Jijo',
1083 'middle_name' => 'J.',
1084 'last_name' => 'Anderson',
1087 'email' => 'jijo_anderson@civicrm.org',
1088 'contact_type' => 'Individual',
1091 $contact4Params = array(
1092 'first_name' => 'Grant',
1093 'middle_name' => 'J.',
1094 'last_name' => 'Anderson',
1097 'email' => 'grant_anderson@civicrm.org',
1098 'contact_type' => 'Individual',
1101 $contact3 = $this->individualCreate($contact3Params);
1102 $contact4 = $this->individualCreate($contact4Params);
1105 $params['id'] = $activityId;
1106 $params['assignee_contact_id'] = array($contact3 => $contact3);
1107 $params['target_contact_id'] = array($contact4 => $contact4);
1109 $result = $this->callAPISuccess('activity', 'create', $params);
1111 $this->assertEquals($activityId, $result['id'], ' in line ' . __LINE__
);
1113 $result = $this->callAPISuccess(
1117 'return.assignee_contact_id' => 1,
1118 'return.target_contact_id' => 1,
1119 'return.source_contact_id' => 1,
1120 'id' => $result['id'],
1124 $assignee = $result['values'][$result['id']]['assignee_contact_id'];
1125 $target = $result['values'][$result['id']]['target_contact_id'];
1127 $this->assertEquals(1, count($assignee), ' in line ' . __LINE__
);
1128 $this->assertEquals(1, count($target), ' in line ' . __LINE__
);
1129 $this->assertEquals(TRUE, in_array($contact3, $assignee), ' in line ' . __LINE__
);
1130 $this->assertEquals(TRUE, in_array($contact4, $target), ' in line ' . __LINE__
);
1132 foreach ($this->_params
as $fld => $val) {
1133 $this->assertEquals($val, $result['values'][$result['id']][$fld]);
1138 * Test civicrm_activity_update() where the DB has a date_time
1139 * value and there is none in the update params.
1141 public function testActivityUpdateNotDate() {
1142 $result = $this->callAPISuccess('activity', 'create', $this->_params
);
1145 'id' => $result['id'],
1146 'subject' => 'Make-it-Happen Meeting',
1148 'location' => '21, Park Avenue',
1149 'details' => 'Lets update Meeting',
1151 'source_contact_id' => $this->_contactID
,
1155 $result = $this->callAPISuccess('activity', 'create', $params);
1156 //hack on date comparison - really we should make getAndCheck smarter to handle dates
1157 $params['activity_date_time'] = $this->_params
['activity_date_time'];
1158 // we also unset source_contact_id since it is stored in an aux table
1159 unset($params['source_contact_id']);
1160 $this->getAndCheck($params, $result['id'], 'activity');
1164 * Check activity update with status.
1166 public function testActivityUpdateWithStatus() {
1167 $activity = $this->callAPISuccess('activity', 'create', $this->_params
);
1169 'id' => $activity['id'],
1170 'source_contact_id' => $this->_contactID
,
1171 'subject' => 'Hurry update works',
1173 'activity_name' => 'Test activity type',
1176 $result = $this->callAPISuccess('activity', 'create', $params);
1177 $this->assertEquals($result['id'], $activity['id']);
1178 $this->assertEquals($result['values'][$activity['id']]['subject'], 'Hurry update works');
1179 $this->assertEquals($result['values'][$activity['id']]['status_id'], 1
1184 * Test civicrm_activity_update() where the source_contact_id
1185 * is not in the update params.
1187 public function testActivityUpdateKeepSource() {
1188 $activity = $this->callAPISuccess('activity', 'create', $this->_params
);
1189 // Updating the activity but not providing anything for the source contact
1190 // (It was set as $this->_contactID earlier.)
1192 'id' => $activity['id'],
1193 'subject' => 'Updated Make-it-Happen Meeting',
1195 'location' => '21, Park Avenue',
1196 'details' => 'Lets update Meeting',
1198 'activity_name' => 'Test activity type',
1202 $result = $this->callAPISuccess('activity', 'create', $params);
1203 $findactivity = $this->callAPISuccess('Activity', 'Get', array('id' => $activity['id']));
1207 * Test civicrm_activities_contact_get()
1209 public function testActivitiesContactGet() {
1210 $activity = $this->callAPISuccess('activity', 'create', $this->_params
);
1211 $activity2 = $this->callAPISuccess('activity', 'create', $this->_params2
);
1212 // Get activities associated with contact $this->_contactID.
1214 'contact_id' => $this->_contactID
,
1216 $result = $this->callAPISuccess('activity', 'get', $params);
1218 $this->assertEquals(2, $result['count']);
1219 $this->assertEquals($this->test_activity_type_value
, $result['values'][$activity['id']]['activity_type_id']);
1220 $this->assertEquals('Test activity type', $result['values'][$activity['id']]['activity_name']);
1221 $this->assertEquals('Test activity type', $result['values'][$activity2['id']]['activity_name']);
1225 * Test chained Activity format.
1227 public function testChainedActivityGet() {
1229 $activity = $this->callAPISuccess('Contact', 'Create', array(
1230 'display_name' => "bob brown",
1231 'contact_type' => 'Individual',
1232 'api.activity_type.create' => array(
1234 'label' => 'send out letters',
1240 'api.activity.create' => array(
1241 'subject' => 'send letter',
1242 'activity_type_id' => '$value.api.activity_type.create.values.0.value',
1246 $result = $this->callAPISuccess('Activity', 'Get', array(
1247 'id' => $activity['id'],
1248 'return.assignee_contact_id' => 1,
1249 'api.contact.get' => array('api.pledge.get' => 1),
1254 * Test civicrm_activity_contact_get() with invalid Contact ID.
1256 public function testActivitiesContactGetWithInvalidContactId() {
1257 $params = array('contact_id' => 'contact');
1258 $this->callAPIFailure('activity', 'get', $params);
1262 * Test civicrm_activity_contact_get() with contact having no Activity.
1264 public function testActivitiesContactGetHavingNoActivity() {
1266 'first_name' => 'dan',
1267 'last_name' => 'conberg',
1268 'email' => 'dan.conberg@w.co.in',
1269 'contact_type' => 'Individual',
1272 $contact = $this->callAPISuccess('contact', 'create', $params);
1274 'contact_id' => $contact['id'],
1276 $result = $this->callAPISuccess('activity', 'get', $params);
1277 $this->assertEquals($result['count'], 0);
1281 * Test getfields function.
1283 public function testGetFields() {
1284 $params = array('action' => 'create');
1285 $result = $this->callAPIAndDocument('activity', 'getfields', $params, __FUNCTION__
, __FILE__
, NULL, NULL);
1286 $this->assertTrue(is_array($result['values']), 'get fields doesn\'t return values array');
1287 foreach ($result['values'] as $key => $value) {
1288 $this->assertTrue(is_array($value), $key . " is not an array");