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 $this->test_activity_type_value
= 9999;
64 $activityTypes = $this->callAPISuccess('option_value', 'create', array(
65 'option_group_id' => 2,
66 'name' => 'Test activity type',
67 'label' => 'Test activity type',
68 'value' => $this->test_activity_type_value
,
71 $this->test_activity_type_id
= $activityTypes['id'];
72 $this->_params
= array(
73 'source_contact_id' => $this->_contactID
,
74 'activity_type_id' => 'Test activity type',
75 'subject' => 'test activity type id',
76 'activity_date_time' => '2011-06-02 14:36:13',
80 'location' => 'Pennsylvania',
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.
100 * This method is called after a test is executed.
102 public function tearDown() {
103 $tablesToTruncate = array(
106 'civicrm_activity_contact',
109 $this->quickCleanup($tablesToTruncate, TRUE);
110 $type = $this->callAPISuccess('optionValue', 'get', array('id' => $this->test_activity_type_id
));
111 if (!empty($type['count'])) {
112 $this->callAPISuccess('option_value', 'delete', array('id' => $this->test_activity_type_id
));
117 * Check fails with empty array.
119 public function testActivityCreateEmpty() {
120 $this->callAPIFailure('activity', 'create', array());
124 * Check if required fields are not passed.
126 public function testActivityCreateWithoutRequired() {
128 'subject' => 'this case should fail',
129 'scheduled_date_time' => date('Ymd'),
131 $result = $this->callAPIFailure('activity', 'create', $params);
135 * Test civicrm_activity_create() with mismatched activity_type_id
138 public function testActivityCreateMismatchNameType() {
140 'source_contact_id' => $this->_contactID
,
141 'subject' => 'Test activity',
142 'activity_date_time' => date('Ymd'),
144 'location' => 'Pennsylvania',
145 'details' => 'a test activity',
147 'activity_name' => 'Fubar activity type',
148 'activity_type_id' => 5,
149 'scheduled_date_time' => date('Ymd'),
152 $result = $this->callAPIFailure('activity', 'create', $params);
156 * Test civicrm_activity_id() with missing source_contact_id is put with the current user.
158 public function testActivityCreateWithMissingContactId() {
160 'subject' => 'Make-it-Happen Meeting',
161 'activity_date_time' => date('Ymd'),
163 'location' => 'Pennsylvania',
164 'details' => 'a test activity',
166 'activity_name' => 'Test activity type',
169 $this->callAPISuccess('activity', 'create', $params);
173 * Test civicrm_activity_id() with non-numeric source_contact_id.
175 public function testActivityCreateWithNonNumericContactId() {
177 'source_contact_id' => 'fubar',
178 'subject' => 'Make-it-Happen Meeting',
179 'activity_date_time' => date('Ymd'),
181 'location' => 'Pennsylvania',
182 'details' => 'a test activity',
184 'activity_name' => 'Test activity type',
187 $this->callAPIFailure('activity', 'create', $params);
191 * Ensure that an invalid activity type causes failure.
193 * Oddly enough this test was failing because the creation of the invalid type
194 * got added to the set up routine. Probably a mis-fix on a test
196 public function testActivityCreateWithNonNumericActivityTypeId() {
198 'source_contact_id' => $this->_contactID
,
199 'subject' => 'Make-it-Happen Meeting',
200 'activity_date_time' => date('Ymd'),
202 'location' => 'Pennsylvania',
203 'details' => 'a test activity',
205 'activity_type_id' => 'Invalid Test activity type',
208 $result = $this->callAPIFailure('activity', 'create', $params);
212 * Check with incorrect required fields.
214 public function testActivityCreateWithUnknownActivityTypeId() {
216 'source_contact_id' => $this->_contactID
,
217 'subject' => 'Make-it-Happen Meeting',
218 'activity_date_time' => date('Ymd'),
220 'location' => 'Pennsylvania',
221 'details' => 'a test activity',
223 'activity_type_id' => 699,
226 $result = $this->callAPIFailure('activity', 'create', $params);
229 public function testActivityCreateWithInvalidPriority() {
231 'source_contact_id' => $this->_contactID
,
232 'subject' => 'Make-it-Happen Meeting',
233 'activity_date_time' => date('Ymd'),
235 'location' => 'Pennsylvania',
236 'details' => 'a test activity',
239 'activity_type_id' => 1,
242 $result = $this->callAPIFailure('activity', 'create', $params,
243 "'44' is not a valid option for field priority_id");
244 $this->assertEquals('priority_id', $result['error_field']);
249 * Test create succeeds with valid string for priority.
251 public function testActivityCreateWithValidStringPriority() {
253 'source_contact_id' => $this->_contactID
,
254 'subject' => 'Make-it-Happen Meeting',
255 'activity_date_time' => date('Ymd'),
257 'location' => 'Pennsylvania',
258 'details' => 'a test activity',
260 'priority_id' => 'Urgent',
261 'activity_type_id' => 1,
264 $result = $this->callAPISuccess('activity', 'create', $params);
265 $this->assertEquals(1, $result['values'][$result['id']]['priority_id']);
269 * Test create fails with invalid priority string.
271 public function testActivityCreateWithInValidStringPriority() {
273 'source_contact_id' => $this->_contactID
,
274 'subject' => 'Make-it-Happen Meeting',
275 'activity_date_time' => date('Ymd'),
277 'location' => 'Pennsylvania',
278 'details' => 'a test activity',
280 'priority_id' => 'ergUrgent',
281 'activity_type_id' => 1,
284 $this->callAPIFailure('activity', 'create', $params,
285 "'ergUrgent' is not a valid option for field priority_id");
289 * Test civicrm_activity_create() with valid parameters.
291 public function testActivityCreate() {
293 $this->callAPISuccess('activity', 'create', $this->_params
);
294 $result = $this->callAPISuccess('activity', 'get', $this->_params
);
295 $this->assertEquals($result['values'][$result['id']]['duration'], 120);
296 $this->assertEquals($result['values'][$result['id']]['subject'], 'test activity type id');
297 $this->assertEquals($result['values'][$result['id']]['activity_date_time'], '2011-06-02 14:36:13');
298 $this->assertEquals($result['values'][$result['id']]['location'], 'Pennsylvania');
299 $this->assertEquals($result['values'][$result['id']]['details'], 'a test activity');
300 $this->assertEquals($result['values'][$result['id']]['status_id'], 2);
301 $this->assertEquals($result['values'][$result['id']]['id'], $result['id']);
305 * Test civicrm_activity_create() with valid parameters - use type_id.
307 public function testActivityCreateCampaignTypeID() {
308 $this->enableCiviCampaign();
311 'source_contact_id' => $this->_contactID
,
312 'subject' => 'Make-it-Happen Meeting',
313 'activity_date_time' => '20110316',
315 'location' => 'Pennsylvania',
316 'details' => 'a test activity',
318 'activity_type_id' => 29,
321 $result = $this->callAPISuccess('activity', 'create', $params);
323 $result = $this->callAPISuccess('activity', 'getsingle', array('id' => $result['id']));
324 $this->assertEquals($result['duration'], 120);
325 $this->assertEquals($result['subject'], 'Make-it-Happen Meeting');
326 $this->assertEquals($result['activity_date_time'], '2011-03-16 00:00:00');
327 $this->assertEquals($result['location'], 'Pennsylvania');
328 $this->assertEquals($result['details'], 'a test activity');
329 $this->assertEquals($result['status_id'], 1);
331 $priorities = $this->callAPISuccess('activity', 'getoptions', array('field' => 'priority_id'));
332 $this->assertEquals($result['priority_id'], array_search('Normal', $priorities['values']));
336 * Test get returns target and assignee contacts.
338 public function testActivityReturnTargetAssignee() {
340 $description = "Demonstrates setting & retrieving activity target & source.";
341 $subfile = "GetTargetandAssignee";
343 'source_contact_id' => $this->_contactID
,
344 'subject' => 'Make-it-Happen Meeting',
345 'activity_date_time' => '20110316',
347 'location' => 'Pennsylvania',
348 'details' => 'a test activity',
350 'activity_type_id' => 1,
352 'target_contact_id' => $this->_contactID
,
353 'assignee_contact_id' => $this->_contactID
,
356 $result = $this->callAPIAndDocument('activity', 'create', $params, __FUNCTION__
, __FILE__
, $description, $subfile);
357 $result = $this->callAPISuccess('activity', 'get', array(
358 'id' => $result['id'],
359 'version' => $this->_apiversion
,
360 'return.assignee_contact_id' => 1,
361 'return.target_contact_id' => 1,
364 $this->assertEquals($this->_contactID
, $result['values'][$result['id']]['assignee_contact_id'][0]);
365 $this->assertEquals($this->_contactID
, $result['values'][$result['id']]['target_contact_id'][0]);
369 * Test get returns target and assignee contact names.
371 public function testActivityReturnTargetAssigneeName() {
373 $description = "Demonstrates retrieving activity target & source contact names.";
374 $subfile = "GetTargetandAssigneeName";
375 $target1 = $this->callAPISuccess('Contact', 'create', array('contact_type' => 'Individual', 'first_name' => 'A', 'last_name' => 'Cat'));
376 $target2 = $this->callAPISuccess('Contact', 'create', array('contact_type' => 'Individual', 'first_name' => 'B', 'last_name' => 'Good'));
377 $assignee = $this->callAPISuccess('Contact', 'create', array('contact_type' => 'Individual', 'first_name' => 'C', 'last_name' => 'Shore'));
378 $source = $this->callAPISuccess('Contact', 'create', array('contact_type' => 'Individual', 'first_name' => 'D', 'last_name' => 'Bug'));
381 'source_contact_id' => $source['id'],
382 'subject' => 'Make-it-Happen Meeting',
383 'activity_date_time' => '20170316',
385 'activity_type_id' => 1,
386 'target_contact_id' => array($target1['id'], $target2['id']),
387 'assignee_contact_id' => $assignee['id'],
390 $result = $this->callAPISuccess('activity', 'create', $params);
391 $result = $this->callAPIAndDocument('activity', 'getsingle', array(
392 'id' => $result['id'],
393 'return' => array('source_contact_name', 'target_contact_name', 'assignee_contact_name', 'subject'),
394 ), __FUNCTION__
, __FILE__
, $description, $subfile);
396 $this->assertEquals($params['subject'], $result['subject']);
397 $this->assertEquals($source['id'], $result['source_contact_id']);
398 $this->assertEquals('D Bug', $result['source_contact_name']);
399 $this->assertEquals('A Cat', $result['target_contact_name'][$target1['id']]);
400 $this->assertEquals('B Good', $result['target_contact_name'][$target2['id']]);
401 $this->assertEquals('C Shore', $result['assignee_contact_name'][$assignee['id']]);
402 $this->assertEquals($assignee['id'], $result['assignee_contact_id'][0]);
406 * Test civicrm_activity_create() with valid parameters and custom data.
408 public function testActivityCreateCustom() {
409 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__
, __FILE__
);
410 $params = $this->_params
;
411 $params['custom_' . $ids['custom_field_id']] = "custom string";
412 $result = $this->callAPIAndDocument($this->_entity
, 'create', $params, __FUNCTION__
, __FILE__
);
413 $result = $this->callAPISuccess($this->_entity
, 'get', array(
414 'return.custom_' . $ids['custom_field_id'] => 1,
415 'id' => $result['id'],
417 $this->assertEquals("custom string", $result['values'][$result['id']]['custom_' . $ids['custom_field_id']], ' in line ' . __LINE__
);
419 $this->customFieldDelete($ids['custom_field_id']);
420 $this->customGroupDelete($ids['custom_group_id']);
424 * Test civicrm_activity_create() using example code.
426 public function testActivityCreateExample() {
427 require_once 'api/v3/examples/Activity/Create.php';
428 $result = activity_create_example();
429 $expectedResult = activity_create_expectedresult();
430 $this->assertEquals($result, $expectedResult);
434 * Test civicrm_activity_create() with valid parameters and custom data.
436 public function testActivityCreateCustomSubType() {
437 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__
, __FILE__
);
438 $this->callAPISuccess('CustomGroup', 'create', array(
439 'extends_entity_column_value' => $this->test_activity_type_value
,
440 'id' => $ids['custom_group_id'],
441 'extends' => 'Activity',
444 $params = $this->_params
;
445 $params['custom_' . $ids['custom_field_id']] = "custom string";
446 $result = $this->callAPIAndDocument($this->_entity
, 'create', $params, __FUNCTION__
, __FILE__
);
447 $result = $this->callAPISuccess($this->_entity
, 'get', array(
448 'return.custom_' . $ids['custom_field_id'] => 1,
449 'id' => $result['id'],
451 $this->assertEquals("custom string", $result['values'][$result['id']]['custom_' . $ids['custom_field_id']]);
453 $this->customFieldDelete($ids['custom_field_id']);
454 $this->customGroupDelete($ids['custom_group_id']);
458 * Test civicrm_activity_create() with valid parameters and custom data.
460 public function testActivityCreateCustomContactRefField() {
462 $this->callAPISuccess('contact', 'create', array('id' => $this->_contactID
, 'sort_name' => 'Contact, Test'));
463 $subfile = 'ContactRefCustomField';
464 $description = "Demonstrates create with Contact Reference Custom Field.";
465 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__
, __FILE__
);
467 'custom_group_id' => $ids['custom_group_id'],
468 'name' => 'Worker_Lookup',
469 'label' => 'Worker Lookup',
470 'html_type' => 'Autocomplete-Select',
471 'data_type' => 'ContactReference',
473 'is_searchable' => 1,
477 $customField = $this->callAPISuccess('custom_field', 'create', $params);
478 $params = $this->_params
;
479 $params['custom_' . $customField['id']] = "$this->_contactID";
481 $result = $this->callAPIAndDocument($this->_entity
, 'create', $params, __FUNCTION__
, __FILE__
, $description, $subfile);
482 $result = $this->callAPIAndDocument($this->_entity
, 'get', array(
483 'return.custom_' . $customField['id'] => 1,
484 'id' => $result['id'],
485 ), __FUNCTION__
, __FILE__
, 'Get with Contact Ref Custom Field', 'ContactRefCustomFieldGet');
487 $this->assertEquals('Anderson, Anthony', $result['values'][$result['id']]['custom_' . $customField['id']]);
488 $this->assertEquals($this->_contactID
, $result['values'][$result['id']]['custom_' . $customField['id'] . "_id"], ' in line ' . __LINE__
);
489 $this->assertEquals('Anderson, Anthony', $result['values'][$result['id']]['custom_' . $customField['id'] . '_1'], ' in line ' . __LINE__
);
490 $this->assertEquals($this->_contactID
, $result['values'][$result['id']]['custom_' . $customField['id'] . "_1_id"], ' in line ' . __LINE__
);
491 $this->customFieldDelete($ids['custom_field_id']);
492 $this->customGroupDelete($ids['custom_group_id']);
496 * Test civicrm_activity_create() with an invalid text status_id.
498 public function testActivityCreateBadTextStatus() {
501 'source_contact_id' => $this->_contactID
,
502 'subject' => 'Discussion on Apis for v3',
503 'activity_date_time' => date('Ymd'),
505 'location' => 'Pennsylvania',
506 'details' => 'a test activity',
507 'status_id' => 'Invalid',
508 'activity_name' => 'Test activity type',
511 $this->callAPIFailure('activity', 'create', $params);
515 * Test civicrm_activity_create() with an invalid text status_id.
517 public function testActivityCreateSupportActivityStatus() {
520 'source_contact_id' => $this->_contactID
,
521 'subject' => 'Discussion on Apis for v3',
522 'activity_date_time' => date('Ymd'),
524 'location' => 'Pennsylvania',
525 'details' => 'a test activity',
526 'activity_status_id' => 'Invalid',
527 'activity_name' => 'Test activity type',
530 $result = $this->callAPIFailure('activity', 'create', $params,
531 "'Invalid' is not a valid option for field status_id");
536 * Test civicrm_activity_create() with using a text status_id.
538 public function testActivityCreateTextStatus() {
541 'source_contact_id' => $this->_contactID
,
542 'subject' => 'Make-it-Happen Meeting',
543 'activity_date_time' => date('Ymd'),
545 'location' => 'Pennsylvania',
546 'details' => 'a test activity',
547 'status_id' => 'Scheduled',
548 'activity_name' => 'Test activity type',
551 $result = $this->callAPISuccess('activity', 'create', $params);
552 $this->assertEquals($result['values'][$result['id']]['duration'], 120);
553 $this->assertEquals($result['values'][$result['id']]['subject'], 'Make-it-Happen Meeting');
554 $this->assertEquals($result['values'][$result['id']]['activity_date_time'], date('Ymd') . '000000');
555 $this->assertEquals($result['values'][$result['id']]['location'], 'Pennsylvania');
556 $this->assertEquals($result['values'][$result['id']]['details'], 'a test activity');
560 * Test civicrm_activity_get() with no params
562 public function testActivityGetEmpty() {
563 $result = $this->callAPISuccess('activity', 'get', array());
567 * Test civicrm_activity_get() with a good activity ID
569 public function testActivityGetGoodID1() {
570 // Insert rows in civicrm_activity creating activities 4 and 13
571 $description = "Demonstrates getting assignee_contact_id & using it to get the contact.";
572 $subfile = 'ReturnAssigneeContact';
573 $activity = $this->callAPISuccess('activity', 'create', $this->_params
);
575 $contact = $this->callAPISuccess('Contact', 'Create', array(
576 'first_name' => "The Rock",
577 'last_name' => 'roccky',
578 'contact_type' => 'Individual',
580 'api.activity.create' => array(
581 'id' => $activity['id'],
582 'assignee_contact_id' => '$value.id',
587 'activity_id' => $activity['id'],
588 'version' => $this->_apiversion
,
590 'return.assignee_contact_id' => 1,
591 'api.contact.get' => array(
592 'id' => '$value.source_contact_id',
596 $result = $this->callAPIAndDocument('Activity', 'Get', $params, __FUNCTION__
, __FILE__
, $description, $subfile);
598 $this->assertEquals($activity['id'], $result['id']);
600 $this->assertEquals($contact['id'], $result['values'][0]['assignee_contact_id'][0]);
602 $this->assertEquals($this->_contactID
, $result['values'][0]['api.contact.get']['values'][0]['contact_id']);
603 $this->assertEquals($this->test_activity_type_value
, $result['values'][0]['activity_type_id']);
604 $this->assertEquals("test activity type id", $result['values'][0]['subject']);
608 * test that get functioning does filtering.
610 public function testGetFilter() {
612 'source_contact_id' => $this->_contactID
,
613 'subject' => 'Make-it-Happen Meeting',
614 'activity_date_time' => '20110316',
616 'location' => 'Pennsylvania',
617 'details' => 'a test activity',
619 'activity_name' => 'Test activity type',
622 $result = $this->callAPISuccess('Activity', 'Create', $params);
623 $this->callAPISuccess('Activity', 'Get', array('subject' => 'Make-it-Happen Meeting'));
624 $this->assertEquals(1, $result['count']);
625 $this->assertEquals('Make-it-Happen Meeting', $result['values'][$result['id']]['subject']);
626 $this->callAPISuccess('Activity', 'Delete', array('id' => $result['id']));
631 * Test civicrm_activity_get() with filter target_contact_id
633 public function testActivityGetTargetFilter() {
634 $params = $this->_params
;
635 $contact1Params = array(
636 'first_name' => 'John',
637 'middle_name' => 'J.',
638 'last_name' => 'Anderson',
641 'email' => 'john_anderson@civicrm.org',
642 'contact_type' => 'Individual',
645 $contact1 = $this->individualCreate($contact1Params);
646 $contact2Params = array(
647 'first_name' => 'Michal',
648 'middle_name' => 'J.',
649 'last_name' => 'Anderson',
652 'email' => 'michal_anderson@civicrm.org',
653 'contact_type' => 'Individual',
656 $contact2 = $this->individualCreate($contact2Params);
658 $params['assignee_contact_id'] = array($contact1, $contact2);
659 $params['target_contact_id'] = array($contact2 => $contact2);
660 $activity = $this->callAPISuccess('Activity', 'Create', $params);
662 $activityget = $this->callAPISuccess('Activity', 'get', array(
663 'id' => $activity['id'],
664 'target_contact_id' => $contact2,
665 'return.target_contact_id' => 1,
667 $this->assertEquals($activity['id'], $activityget['id']);
668 $this->assertEquals($contact2, $activityget['values'][$activityget['id']]['target_contact_id'][0]);
670 $activityget = $this->callAPISuccess('activity', 'get', array(
671 'target_contact_id' => $this->_contactID
,
672 'return.target_contact_id' => 1,
673 'id' => $activity['id'],
675 if ($activityget['count'] > 0) {
676 $this->assertNotEquals($contact2, $activityget['values'][$activityget['id']]['target_contact_id'][0]);
681 * Test that activity.get api works when filtering on subject.
683 public function testActivityGetSubjectFilter() {
684 $subject = 'test activity ' . __FUNCTION__
. mt_rand();
685 $params = $this->_params
;
686 $params['subject'] = $subject;
687 $activity = $this->callAPISuccess('Activity', 'Create', $params);
688 $activityget = $this->callAPISuccess('activity', 'getsingle', array(
689 'subject' => $subject,
691 $this->assertEquals($activityget['subject'], $subject);
695 * Test that activity.get api works when filtering on details.
697 public function testActivityGetDetailsFilter() {
698 $details = 'test activity ' . __FUNCTION__
. mt_rand();
699 $params = $this->_params
;
700 $params['details'] = $details;
701 $activity = $this->callAPISuccess('Activity', 'Create', $params);
702 $activityget = $this->callAPISuccess('activity', 'getsingle', array(
703 'details' => $details,
705 $this->assertEquals($activityget['details'], $details);
709 * Test that activity.get api works when filtering on tag.
711 public function testActivityGetTagFilter() {
712 $tag = $this->callAPISuccess('Tag', 'create', array('name' => mt_rand(), 'used_for' => 'Activities'));
713 $activity = $this->callAPISuccess('Activity', 'Create', $this->_params
);
714 $this->callAPISuccess('EntityTag', 'create', array('entity_table' => 'civicrm_activity', 'tag_id' => $tag['id'], 'entity_id' => $activity['id']));
715 $activityget = $this->callAPISuccess('activity', 'getsingle', array(
716 'tag_id' => $tag['id'],
718 $this->assertEquals($activityget['id'], $activity['id']);
724 public function testJoinOnTags() {
725 $tagName = 'act_tag_nm_' . mt_rand();
726 $tagDescription = 'act_tag_ds_' . mt_rand();
727 $tagColor = '#' . substr(md5(mt_rand()), 0, 6);
728 $tag = $this->callAPISuccess('Tag', 'create', array('name' => $tagName, 'color' => $tagColor, 'description' => $tagDescription, 'used_for' => 'Activities'));
729 $activity = $this->callAPISuccess('Activity', 'Create', $this->_params
);
730 $this->callAPISuccess('EntityTag', 'create', array('entity_table' => 'civicrm_activity', 'tag_id' => $tag['id'], 'entity_id' => $activity['id']));
731 $activityget = $this->callAPISuccess('activity', 'getsingle', array(
732 'id' => $activity['id'],
733 'return' => array('tag_id.name', 'tag_id.description', 'tag_id.color'),
735 $this->assertEquals($tagName, $activityget['tag_id'][$tag['id']]['tag_id.name']);
736 $this->assertEquals($tagColor, $activityget['tag_id'][$tag['id']]['tag_id.color']);
737 $this->assertEquals($tagDescription, $activityget['tag_id'][$tag['id']]['tag_id.description']);
742 * Test that activity.get api works to filter on and return files.
744 public function testActivityGetFile() {
745 $activity = $this->callAPISuccess('Activity', 'create', $this->_params
);
746 $activity2 = $this->callAPISuccess('Activity', 'create', $this->_params2
);
747 $file = $this->callAPISuccess('Attachment', 'create', array(
748 'name' => 'actAttachment.txt',
749 'mime_type' => 'text/plain',
750 'description' => 'My test description',
751 'content' => 'My test content',
752 'entity_table' => 'civicrm_activity',
753 'entity_id' => $activity2['id'],
755 $activityget = $this->callAPISuccess('activity', 'getsingle', array(
756 'file_id' => $file['id'],
757 'return' => 'file_id',
759 $this->assertEquals($activityget['id'], $activity2['id']);
760 $this->assertEquals($file['id'], $activityget['file_id'][0]);
764 * test that get functioning does filtering.
766 public function testGetStatusID() {
768 'source_contact_id' => $this->_contactID
,
769 'subject' => 'Make-it-Happen Meeting',
770 'activity_date_time' => '20110316',
772 'location' => 'Pennsylvania',
773 'details' => 'a test activity',
775 'activity_name' => 'Test activity type',
778 $this->callAPISuccess('Activity', 'Create', $params);
779 $result = $this->callAPISuccess('Activity', 'Get', array('activity_status_id' => '1'));
780 $this->assertEquals(1, $result['count'], 'one activity of status 1 should exist');
782 $result = $this->callAPISuccess('Activity', 'Get', array('status_id' => '1'));
783 $this->assertEquals(1, $result['count'], 'status_id should also work');
785 $result = $this->callAPISuccess('Activity', 'Get', array('activity_status_id' => '2'));
786 $this->assertEquals(0, $result['count'], 'No activities of status 1 should exist');
787 $result = $this->callAPISuccess('Activity', 'Get', array(
788 'version' => $this->_apiversion
,
791 $this->assertEquals(0, $result['count'], 'No activities of status 1 should exist');
796 * test that get functioning does filtering.
798 public function testGetFilterMaxDate() {
800 'source_contact_id' => $this->_contactID
,
801 'subject' => 'Make-it-Happen Meeting',
802 'activity_date_time' => '20110101',
804 'location' => 'Pennsylvania',
805 'details' => 'a test activity',
807 'activity_name' => 'Test activity type',
808 'version' => $this->_apiversion
,
811 $activityOne = $this->callAPISuccess('Activity', 'Create', $params);
812 $params['activity_date_time'] = 20120216;
813 $activityTwo = $this->callAPISuccess('Activity', 'Create', $params);
814 $result = $this->callAPISuccess('Activity', 'Get', array(
817 $description = "Demonstrates _low filter (at time of writing doesn't work if contact_id is set.";
818 $subfile = "DateTimeLow";
819 $this->assertEquals(2, $result['count']);
822 'filter.activity_date_time_low' => '20120101000000',
825 $result = $this->callAPIAndDocument('Activity', 'Get', $params, __FUNCTION__
, __FILE__
, $description, $subfile);
826 $this->assertEquals(1, $result['count']);
827 $description = "Demonstrates _high filter (at time of writing doesn't work if contact_id is set.";
828 $subfile = "DateTimeHigh";
829 $this->assertEquals('2012-02-16 00:00:00', $result['values'][0]['activity_date_time']);
831 'source_contact_id' => $this->_contactID
,
833 'filter.activity_date_time_high' => '20120101000000',
836 $result = $this->callAPIAndDocument('Activity', 'Get', $params, __FUNCTION__
, __FILE__
, $description, $subfile);
838 $this->assertEquals(1, $result['count']);
839 $this->assertEquals('2011-01-01 00:00:00', $result['values'][0]['activity_date_time']);
841 $this->callAPISuccess('Activity', 'Delete', array('version' => 3, 'id' => $activityOne['id']));
842 $this->callAPISuccess('Activity', 'Delete', array('version' => 3, 'id' => $activityTwo['id']));
846 * Test civicrm_activity_get() with a good activity ID which
847 * has associated custom data
849 public function testActivityGetGoodIDCustom() {
850 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__
, __FILE__
);
852 $params = $this->_params
;
853 $params['custom_' . $ids['custom_field_id']] = "custom string";
855 $this->callAPISuccess($this->_entity
, 'create', $params);
857 // Retrieve the test value.
859 'activity_type_id' => $this->test_activity_type_value
,
861 'return.custom_' . $ids['custom_field_id'] => 1,
863 $result = $this->callAPIAndDocument('activity', 'get', $params, __FUNCTION__
, __FILE__
);
864 $this->assertEquals("custom string", $result['values'][0]['custom_' . $ids['custom_field_id']]);
866 $this->assertEquals($this->test_activity_type_value
, $result['values'][0]['activity_type_id']);
867 $this->assertEquals('test activity type id', $result['values'][0]['subject']);
868 $this->customFieldDelete($ids['custom_field_id']);
869 $this->customGroupDelete($ids['custom_group_id']);
873 * Test civicrm_activity_get() with a good activity ID which
874 * has associated custom data
876 public function testActivityGetContact_idCustom() {
877 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__
, __FILE__
);
879 $params = $this->_params
;
880 $params['custom_' . $ids['custom_field_id']] = "custom string";
882 $result = $this->callAPISuccess($this->_entity
, 'create', $params);
883 // Retrieve the test value
885 'contact_id' => $this->_params
['source_contact_id'],
886 'activity_type_id' => $this->test_activity_type_value
,
888 'return.custom_' . $ids['custom_field_id'] => 1,
890 $result = $this->callAPIAndDocument('activity', 'get', $params, __FUNCTION__
, __FILE__
);
891 $this->assertEquals("custom string", $result['values'][0]['custom_' . $ids['custom_field_id']], ' in line ' . __LINE__
);
893 $this->assertEquals($this->test_activity_type_value
, $result['values'][0]['activity_type_id']);
894 $this->assertEquals('test activity type id', $result['values'][0]['subject']);
895 $this->assertEquals($result['values'][0]['id'], $result['id']);
899 * Check activity deletion with empty params.
901 public function testDeleteActivityForEmptyParams() {
902 $params = array('version' => $this->_apiversion
);
903 $this->callAPIFailure('activity', 'delete', $params);
907 * Check activity deletion without activity id.
909 public function testDeleteActivityWithoutId() {
911 'activity_name' => 'Meeting',
912 'version' => $this->_apiversion
,
914 $result = $this->callAPIFailure('activity', 'delete', $params);
918 * Check activity deletion without activity type.
920 public function testDeleteActivityWithoutActivityType() {
921 $params = array('id' => 1);
922 $result = $this->callAPIFailure('activity', 'delete', $params);
926 * Check activity deletion with incorrect data.
928 public function testDeleteActivityWithIncorrectActivityType() {
931 'activity_name' => 'Test Activity',
934 $result = $this->callAPIFailure('activity', 'delete', $params);
938 * Check activity deletion with correct data.
940 public function testDeleteActivity() {
941 $result = $this->callAPISuccess('activity', 'create', $this->_params
);
943 'id' => $result['id'],
944 'version' => $this->_apiversion
,
947 $this->callAPIAndDocument('activity', 'delete', $params, __FUNCTION__
, __FILE__
);
951 * Check if required fields are not passed.
953 public function testActivityUpdateWithoutRequired() {
955 'subject' => 'this case should fail',
956 'scheduled_date_time' => date('Ymd'),
959 $result = $this->callAPIFailure('activity', 'create', $params);
963 * Test civicrm_activity_update() with non-numeric id
965 public function testActivityUpdateWithNonNumericId() {
967 'id' => 'lets break it',
968 'activity_name' => 'Meeting',
969 'subject' => 'this case should fail',
970 'scheduled_date_time' => date('Ymd'),
973 $result = $this->callAPIFailure('activity', 'create', $params);
977 * Check with incorrect required fields.
979 public function testActivityUpdateWithIncorrectContactActivityType() {
982 'activity_name' => 'Test Activity',
983 'subject' => 'this case should fail',
984 'scheduled_date_time' => date('Ymd'),
985 'source_contact_id' => $this->_contactID
,
988 $result = $this->callAPIFailure('activity', 'create', $params,
989 'Invalid Activity Id');
993 * Test civicrm_activity_update() to update an existing activity
995 public function testActivityUpdate() {
996 $result = $this->callAPISuccess('activity', 'create', $this->_params
);
999 'id' => $result['id'],
1000 'subject' => 'Make-it-Happen Meeting',
1001 'activity_date_time' => '20091011123456',
1003 'location' => '21, Park Avenue',
1004 'details' => 'Lets update Meeting',
1006 'source_contact_id' => $this->_contactID
,
1010 $result = $this->callAPISuccess('activity', 'create', $params);
1011 //hack on date comparison - really we should make getAndCheck smarter to handle dates
1012 $params['activity_date_time'] = '2009-10-11 12:34:56';
1013 // we also unset source_contact_id since it is stored in an aux table
1014 unset($params['source_contact_id']);
1015 $this->getAndCheck($params, $result['id'], 'activity');
1019 * Test civicrm_activity_update() with valid parameters
1020 * and some custom data
1022 public function testActivityUpdateCustom() {
1023 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__
, __FILE__
);
1025 $params = $this->_params
;
1027 // Create an activity with custom data
1028 //this has been updated from the previous 'old format' function - need to make it work
1030 'source_contact_id' => $this->_contactID
,
1031 'subject' => 'Make-it-Happen Meeting',
1032 'activity_date_time' => '2009-10-18',
1034 'location' => 'Pennsylvania',
1035 'details' => 'a test activity to check the update api',
1037 'activity_name' => 'Test activity type',
1038 'version' => $this->_apiversion
,
1039 'custom_' . $ids['custom_field_id'] => 'custom string',
1041 $result = $this->callAPISuccess('activity', 'create', $params);
1043 $activityId = $result['id'];
1044 $result = $this->callAPISuccess($this->_entity
, 'get', array(
1045 'return.custom_' . $ids['custom_field_id'] => 1,
1047 'id' => $result['id'],
1049 $this->assertEquals("custom string", $result['values'][$result['id']]['custom_' . $ids['custom_field_id']]);
1050 $this->assertEquals("2009-10-18 00:00:00", $result['values'][$result['id']]['activity_date_time']);
1051 $fields = $this->callAPISuccess('activity', 'getfields', array('version' => $this->_apiversion
));
1052 $this->assertTrue(is_array($fields['values']['custom_' . $ids['custom_field_id']]));
1054 // Update the activity with custom data.
1056 'id' => $activityId,
1057 'source_contact_id' => $this->_contactID
,
1058 'subject' => 'Make-it-Happen Meeting',
1060 'activity_name' => 'Test activity type',
1061 // add this since dates are messed up
1062 'activity_date_time' => date('Ymd'),
1063 'custom_' . $ids['custom_field_id'] => 'Updated my test data',
1064 'version' => $this->_apiversion
,
1066 $result = $this->callAPISuccess('Activity', 'Create', $params);
1068 $result = $this->callAPISuccess($this->_entity
, 'get', array(
1069 'return.custom_' . $ids['custom_field_id'] => 1,
1071 'id' => $result['id'],
1073 $this->assertEquals("Updated my test data", $result['values'][$result['id']]['custom_' . $ids['custom_field_id']]);
1077 * Test civicrm_activity_update() for core activity fields
1078 * and some custom data
1080 public function testActivityUpdateCheckCoreFields() {
1081 $params = $this->_params
;
1082 $contact1Params = array(
1083 'first_name' => 'John',
1084 'middle_name' => 'J.',
1085 'last_name' => 'Anderson',
1088 'email' => 'john_anderson@civicrm.org',
1089 'contact_type' => 'Individual',
1092 $contact1 = $this->individualCreate($contact1Params);
1093 $contact2Params = array(
1094 'first_name' => 'Michal',
1095 'middle_name' => 'J.',
1096 'last_name' => 'Anderson',
1099 'email' => 'michal_anderson@civicrm.org',
1100 'contact_type' => 'Individual',
1103 $contact2 = $this->individualCreate($contact2Params);
1105 $params['assignee_contact_id'] = array($contact1, $contact2);
1106 $params['target_contact_id'] = array($contact2 => $contact2);
1107 $result = $this->callAPISuccess('Activity', 'Create', $params);
1109 $activityId = $result['id'];
1111 'return.assignee_contact_id' => 1,
1112 'return.target_contact_id' => 1,
1113 'version' => $this->_apiversion
,
1114 'id' => $activityId,
1116 $result = $this->callAPISuccess($this->_entity
, 'get', $getParams);
1117 $assignee = $result['values'][$result['id']]['assignee_contact_id'];
1118 $target = $result['values'][$result['id']]['target_contact_id'];
1119 $this->assertEquals(2, count($assignee), ' in line ' . __LINE__
);
1120 $this->assertEquals(1, count($target), ' in line ' . __LINE__
);
1121 $this->assertEquals(TRUE, in_array($contact1, $assignee), ' in line ' . __LINE__
);
1122 $this->assertEquals(TRUE, in_array($contact2, $target), ' in line ' . __LINE__
);
1124 $contact3Params = array(
1125 'first_name' => 'Jijo',
1126 'middle_name' => 'J.',
1127 'last_name' => 'Anderson',
1130 'email' => 'jijo_anderson@civicrm.org',
1131 'contact_type' => 'Individual',
1134 $contact4Params = array(
1135 'first_name' => 'Grant',
1136 'middle_name' => 'J.',
1137 'last_name' => 'Anderson',
1140 'email' => 'grant_anderson@civicrm.org',
1141 'contact_type' => 'Individual',
1144 $contact3 = $this->individualCreate($contact3Params);
1145 $contact4 = $this->individualCreate($contact4Params);
1148 $params['id'] = $activityId;
1149 $params['assignee_contact_id'] = array($contact3 => $contact3);
1150 $params['target_contact_id'] = array($contact4 => $contact4);
1152 $result = $this->callAPISuccess('activity', 'create', $params);
1154 $this->assertEquals($activityId, $result['id'], ' in line ' . __LINE__
);
1156 $result = $this->callAPISuccess(
1160 'return.assignee_contact_id' => 1,
1161 'return.target_contact_id' => 1,
1162 'return.source_contact_id' => 1,
1163 'id' => $result['id'],
1167 $assignee = $result['values'][$result['id']]['assignee_contact_id'];
1168 $target = $result['values'][$result['id']]['target_contact_id'];
1170 $this->assertEquals(1, count($assignee), ' in line ' . __LINE__
);
1171 $this->assertEquals(1, count($target), ' in line ' . __LINE__
);
1172 $this->assertEquals(TRUE, in_array($contact3, $assignee), ' in line ' . __LINE__
);
1173 $this->assertEquals(TRUE, in_array($contact4, $target), ' in line ' . __LINE__
);
1174 $this->_params
['activity_type_id'] = $this->test_activity_type_value
;
1175 foreach ($this->_params
as $fld => $val) {
1176 $this->assertEquals($val, $result['values'][$result['id']][$fld]);
1181 * Test civicrm_activity_update() where the DB has a date_time
1182 * value and there is none in the update params.
1184 public function testActivityUpdateNotDate() {
1185 $result = $this->callAPISuccess('activity', 'create', $this->_params
);
1188 'id' => $result['id'],
1189 'subject' => 'Make-it-Happen Meeting',
1191 'location' => '21, Park Avenue',
1192 'details' => 'Lets update Meeting',
1194 'source_contact_id' => $this->_contactID
,
1198 $result = $this->callAPISuccess('activity', 'create', $params);
1199 //hack on date comparison - really we should make getAndCheck smarter to handle dates
1200 $params['activity_date_time'] = $this->_params
['activity_date_time'];
1201 // we also unset source_contact_id since it is stored in an aux table
1202 unset($params['source_contact_id']);
1203 $this->getAndCheck($params, $result['id'], 'activity');
1207 * Check activity update with status.
1209 public function testActivityUpdateWithStatus() {
1210 $activity = $this->callAPISuccess('activity', 'create', $this->_params
);
1212 'id' => $activity['id'],
1213 'source_contact_id' => $this->_contactID
,
1214 'subject' => 'Hurry update works',
1216 'activity_name' => 'Test activity type',
1219 $result = $this->callAPISuccess('activity', 'create', $params);
1220 $this->assertEquals($result['id'], $activity['id']);
1221 $this->assertEquals($result['values'][$activity['id']]['subject'], 'Hurry update works');
1222 $this->assertEquals($result['values'][$activity['id']]['status_id'], 1
1227 * Test civicrm_activity_update() where the source_contact_id
1228 * is not in the update params.
1230 public function testActivityUpdateKeepSource() {
1231 $activity = $this->callAPISuccess('activity', 'create', $this->_params
);
1232 // Updating the activity but not providing anything for the source contact
1233 // (It was set as $this->_contactID earlier.)
1235 'id' => $activity['id'],
1236 'subject' => 'Updated Make-it-Happen Meeting',
1238 'location' => '21, Park Avenue',
1239 'details' => 'Lets update Meeting',
1241 'activity_name' => 'Test activity type',
1245 $result = $this->callAPISuccess('activity', 'create', $params);
1246 $findactivity = $this->callAPISuccess('Activity', 'Get', array('id' => $activity['id']));
1250 * Test civicrm_activities_contact_get()
1252 public function testActivitiesContactGet() {
1253 $activity = $this->callAPISuccess('activity', 'create', $this->_params
);
1254 $activity2 = $this->callAPISuccess('activity', 'create', $this->_params2
);
1255 // Get activities associated with contact $this->_contactID.
1257 'contact_id' => $this->_contactID
,
1259 $result = $this->callAPISuccess('activity', 'get', $params);
1261 $this->assertEquals(2, $result['count']);
1262 $this->assertEquals($this->test_activity_type_value
, $result['values'][$activity['id']]['activity_type_id']);
1263 $this->assertEquals('Test activity type', $result['values'][$activity['id']]['activity_name']);
1264 $this->assertEquals('Test activity type', $result['values'][$activity2['id']]['activity_name']);
1268 * Test chained Activity format.
1270 public function testChainedActivityGet() {
1272 $activity = $this->callAPISuccess('Contact', 'Create', array(
1273 'display_name' => "bob brown",
1274 'contact_type' => 'Individual',
1275 'api.activity_type.create' => array(
1277 'label' => 'send out letters',
1283 'api.activity.create' => array(
1284 'subject' => 'send letter',
1285 'activity_type_id' => '$value.api.activity_type.create.values.0.value',
1289 $result = $this->callAPISuccess('Activity', 'Get', array(
1290 'id' => $activity['id'],
1291 'return.assignee_contact_id' => 1,
1292 'api.contact.get' => array('api.pledge.get' => 1),
1297 * Test civicrm_activity_contact_get() with invalid Contact ID.
1299 public function testActivitiesContactGetWithInvalidContactId() {
1300 $params = array('contact_id' => 'contact');
1301 $this->callAPIFailure('activity', 'get', $params);
1305 * Test civicrm_activity_contact_get() with contact having no Activity.
1307 public function testActivitiesContactGetHavingNoActivity() {
1309 'first_name' => 'dan',
1310 'last_name' => 'conberg',
1311 'email' => 'dan.conberg@w.co.in',
1312 'contact_type' => 'Individual',
1315 $contact = $this->callAPISuccess('contact', 'create', $params);
1317 'contact_id' => $contact['id'],
1319 $result = $this->callAPISuccess('activity', 'get', $params);
1320 $this->assertEquals($result['count'], 0);
1324 * Test getfields function.
1326 public function testGetFields() {
1327 $params = array('action' => 'create');
1328 $result = $this->callAPIAndDocument('activity', 'getfields', $params, __FUNCTION__
, __FILE__
, NULL, NULL);
1329 $this->assertTrue(is_array($result['values']), 'get fields doesn\'t return values array');
1330 foreach ($result['values'] as $key => $value) {
1331 $this->assertTrue(is_array($value), $key . " is not an array");
1335 public function testGetWithOr() {
1337 'test or 1' => 'orOperator',
1338 'test or 2' => 'orOperator',
1339 'test or 3' => 'nothing',
1341 foreach ($acts as $subject => $details) {
1342 $params = $this->_params
;
1343 $params['subject'] = $subject;
1344 $params['details'] = $details;
1345 $this->callAPISuccess('Activity', 'create', $params);
1347 $result = $this->callAPISuccess('Activity', 'get', array(
1348 'details' => 'orOperator',
1350 $this->assertEquals(2, $result['count']);
1351 $result = $this->callAPISuccess('Activity', 'get', array(
1352 'details' => 'orOperator',
1353 'subject' => 'test or 3',
1355 $this->assertEquals(0, $result['count']);
1356 $result = $this->callAPISuccess('Activity', 'get', array(
1357 'details' => 'orOperator',
1358 'subject' => 'test or 3',
1359 'options' => array('or' => array(array('details', 'subject'))),
1361 $this->assertEquals(3, $result['count']);