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 $this->callAPISuccess('option_value', 'delete', array('id' => $this->test_activity_type_id
));
113 * Check fails with empty array.
115 public function testActivityCreateEmpty() {
116 $this->callAPIFailure('activity', 'create', array());
120 * Check if required fields are not passed.
122 public function testActivityCreateWithoutRequired() {
124 'subject' => 'this case should fail',
125 'scheduled_date_time' => date('Ymd'),
127 $result = $this->callAPIFailure('activity', 'create', $params);
131 * Test civicrm_activity_create() with mismatched activity_type_id
134 public function testActivityCreateMismatchNameType() {
136 'source_contact_id' => $this->_contactID
,
137 'subject' => 'Test activity',
138 'activity_date_time' => date('Ymd'),
140 'location' => 'Pennsylvania',
141 'details' => 'a test activity',
143 'activity_name' => 'Fubar activity type',
144 'activity_type_id' => 5,
145 'scheduled_date_time' => date('Ymd'),
148 $result = $this->callAPIFailure('activity', 'create', $params);
152 * Test civicrm_activity_id() with missing source_contact_id is put with the current user.
154 public function testActivityCreateWithMissingContactId() {
156 'subject' => 'Make-it-Happen Meeting',
157 'activity_date_time' => date('Ymd'),
159 'location' => 'Pennsylvania',
160 'details' => 'a test activity',
162 'activity_name' => 'Test activity type',
165 $this->callAPISuccess('activity', 'create', $params);
169 * Test civicrm_activity_id() with non-numeric source_contact_id.
171 public function testActivityCreateWithNonNumericContactId() {
173 'source_contact_id' => 'fubar',
174 'subject' => 'Make-it-Happen Meeting',
175 'activity_date_time' => date('Ymd'),
177 'location' => 'Pennsylvania',
178 'details' => 'a test activity',
180 'activity_name' => 'Test activity type',
183 $this->callAPIFailure('activity', 'create', $params);
187 * 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 public function testActivityCreateWithNonNumericActivityTypeId() {
194 'source_contact_id' => $this->_contactID
,
195 'subject' => 'Make-it-Happen Meeting',
196 'activity_date_time' => date('Ymd'),
198 'location' => 'Pennsylvania',
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 public function testActivityCreateWithUnknownActivityTypeId() {
212 'source_contact_id' => $this->_contactID
,
213 'subject' => 'Make-it-Happen Meeting',
214 'activity_date_time' => date('Ymd'),
216 'location' => 'Pennsylvania',
217 'details' => 'a test activity',
219 'activity_type_id' => 699,
222 $result = $this->callAPIFailure('activity', 'create', $params);
225 public function testActivityCreateWithInvalidPriority() {
227 'source_contact_id' => $this->_contactID
,
228 'subject' => 'Make-it-Happen Meeting',
229 'activity_date_time' => date('Ymd'),
231 'location' => 'Pennsylvania',
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 * Test create succeeds with valid string for priority.
247 public function testActivityCreateWithValidStringPriority() {
249 'source_contact_id' => $this->_contactID
,
250 'subject' => 'Make-it-Happen Meeting',
251 'activity_date_time' => date('Ymd'),
253 'location' => 'Pennsylvania',
254 'details' => 'a test activity',
256 'priority_id' => 'Urgent',
257 'activity_type_id' => 1,
260 $result = $this->callAPISuccess('activity', 'create', $params);
261 $this->assertEquals(1, $result['values'][$result['id']]['priority_id']);
265 * Test create fails with invalid priority string.
267 public function testActivityCreateWithInValidStringPriority() {
269 'source_contact_id' => $this->_contactID
,
270 'subject' => 'Make-it-Happen Meeting',
271 'activity_date_time' => date('Ymd'),
273 'location' => 'Pennsylvania',
274 'details' => 'a test activity',
276 'priority_id' => 'ergUrgent',
277 'activity_type_id' => 1,
280 $this->callAPIFailure('activity', 'create', $params,
281 "'ergUrgent' is not a valid option for field priority_id");
285 * Test civicrm_activity_create() with valid parameters.
287 public function testActivityCreate() {
289 $this->callAPISuccess('activity', 'create', $this->_params
);
290 $result = $this->callAPISuccess('activity', 'get', $this->_params
);
291 $this->assertEquals($result['values'][$result['id']]['duration'], 120);
292 $this->assertEquals($result['values'][$result['id']]['subject'], 'test activity type id');
293 $this->assertEquals($result['values'][$result['id']]['activity_date_time'], '2011-06-02 14:36:13');
294 $this->assertEquals($result['values'][$result['id']]['location'], 'Pennsylvania');
295 $this->assertEquals($result['values'][$result['id']]['details'], 'a test activity');
296 $this->assertEquals($result['values'][$result['id']]['status_id'], 2);
297 $this->assertEquals($result['values'][$result['id']]['id'], $result['id']);
301 * Test civicrm_activity_create() with valid parameters - use type_id.
303 public function testActivityCreateCampaignTypeID() {
304 $this->enableCiviCampaign();
307 'source_contact_id' => $this->_contactID
,
308 'subject' => 'Make-it-Happen Meeting',
309 'activity_date_time' => '20110316',
311 'location' => 'Pennsylvania',
312 'details' => 'a test activity',
314 'activity_type_id' => 29,
317 $result = $this->callAPISuccess('activity', 'create', $params);
319 $result = $this->callAPISuccess('activity', 'getsingle', array('id' => $result['id']));
320 $this->assertEquals($result['duration'], 120);
321 $this->assertEquals($result['subject'], 'Make-it-Happen Meeting');
322 $this->assertEquals($result['activity_date_time'], '2011-03-16 00:00:00');
323 $this->assertEquals($result['location'], 'Pennsylvania');
324 $this->assertEquals($result['details'], 'a test activity');
325 $this->assertEquals($result['status_id'], 1);
327 $priorities = $this->callAPISuccess('activity', 'getoptions', array('field' => 'priority_id'));
328 $this->assertEquals($result['priority_id'], array_search('Normal', $priorities['values']));
332 * Test get returns target and assignee contacts.
334 public function testActivityReturnTargetAssignee() {
336 $description = "Demonstrates setting & retrieving activity target & source.";
337 $subfile = "GetTargetandAssignee";
339 'source_contact_id' => $this->_contactID
,
340 'subject' => 'Make-it-Happen Meeting',
341 'activity_date_time' => '20110316',
343 'location' => 'Pennsylvania',
344 'details' => 'a test activity',
346 'activity_type_id' => 1,
348 'target_contact_id' => $this->_contactID
,
349 'assignee_contact_id' => $this->_contactID
,
352 $result = $this->callAPIAndDocument('activity', 'create', $params, __FUNCTION__
, __FILE__
, $description, $subfile);
353 $result = $this->callAPISuccess('activity', 'get', array(
354 'id' => $result['id'],
355 'version' => $this->_apiversion
,
356 'return.assignee_contact_id' => 1,
357 'return.target_contact_id' => 1,
360 $this->assertEquals($this->_contactID
, $result['values'][$result['id']]['assignee_contact_id'][0]);
361 $this->assertEquals($this->_contactID
, $result['values'][$result['id']]['target_contact_id'][0]);
365 * Test civicrm_activity_create() using example code.
367 public function testActivityCreateExample() {
368 require_once 'api/v3/examples/Activity/Create.php';
369 $result = activity_create_example();
370 $expectedResult = activity_create_expectedresult();
371 $this->assertEquals($result, $expectedResult);
375 * Test civicrm_activity_create() with valid parameters and custom data.
377 public function testActivityCreateCustom() {
378 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__
, __FILE__
);
379 $params = $this->_params
;
380 $params['custom_' . $ids['custom_field_id']] = "custom string";
381 $result = $this->callAPIAndDocument($this->_entity
, 'create', $params, __FUNCTION__
, __FILE__
);
382 $result = $this->callAPISuccess($this->_entity
, 'get', array(
383 'return.custom_' . $ids['custom_field_id'] => 1,
384 'id' => $result['id'],
386 $this->assertEquals("custom string", $result['values'][$result['id']]['custom_' . $ids['custom_field_id']], ' in line ' . __LINE__
);
388 $this->customFieldDelete($ids['custom_field_id']);
389 $this->customGroupDelete($ids['custom_group_id']);
393 * Test civicrm_activity_create() with valid parameters and custom data.
395 public function testActivityCreateCustomSubType() {
396 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__
, __FILE__
);
397 $this->callAPISuccess('CustomGroup', 'create', array(
398 'extends_entity_column_value' => $this->test_activity_type_value
,
399 'id' => $ids['custom_group_id'],
400 'extends' => 'Activity',
403 $params = $this->_params
;
404 $params['custom_' . $ids['custom_field_id']] = "custom string";
405 $result = $this->callAPIAndDocument($this->_entity
, 'create', $params, __FUNCTION__
, __FILE__
);
406 $result = $this->callAPISuccess($this->_entity
, 'get', array(
407 'return.custom_' . $ids['custom_field_id'] => 1,
408 'id' => $result['id'],
410 $this->assertEquals("custom string", $result['values'][$result['id']]['custom_' . $ids['custom_field_id']]);
412 $this->customFieldDelete($ids['custom_field_id']);
413 $this->customGroupDelete($ids['custom_group_id']);
417 * Test civicrm_activity_create() with valid parameters and custom data.
419 public function testActivityCreateCustomContactRefField() {
421 $this->callAPISuccess('contact', 'create', array('id' => $this->_contactID
, 'sort_name' => 'Contact, Test'));
422 $subfile = 'ContactRefCustomField';
423 $description = "Demonstrates create with Contact Reference Custom Field.";
424 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__
, __FILE__
);
426 'custom_group_id' => $ids['custom_group_id'],
427 'name' => 'Worker_Lookup',
428 'label' => 'Worker Lookup',
429 'html_type' => 'Autocomplete-Select',
430 'data_type' => 'ContactReference',
432 'is_searchable' => 1,
436 $customField = $this->callAPISuccess('custom_field', 'create', $params);
437 $params = $this->_params
;
438 $params['custom_' . $customField['id']] = "$this->_contactID";
440 $result = $this->callAPIAndDocument($this->_entity
, 'create', $params, __FUNCTION__
, __FILE__
, $description, $subfile);
441 $result = $this->callAPIAndDocument($this->_entity
, 'get', array(
442 'return.custom_' . $customField['id'] => 1,
443 'id' => $result['id'],
444 ), __FUNCTION__
, __FILE__
, 'Get with Contact Ref Custom Field', 'ContactRefCustomFieldGet');
446 $this->assertEquals('Anderson, Anthony', $result['values'][$result['id']]['custom_' . $customField['id']]);
447 $this->assertEquals($this->_contactID
, $result['values'][$result['id']]['custom_' . $customField['id'] . "_id"], ' in line ' . __LINE__
);
448 $this->assertEquals('Anderson, Anthony', $result['values'][$result['id']]['custom_' . $customField['id'] . '_1'], ' in line ' . __LINE__
);
449 $this->assertEquals($this->_contactID
, $result['values'][$result['id']]['custom_' . $customField['id'] . "_1_id"], ' in line ' . __LINE__
);
450 $this->customFieldDelete($ids['custom_field_id']);
451 $this->customGroupDelete($ids['custom_group_id']);
455 * Test civicrm_activity_create() with an invalid text status_id.
457 public function testActivityCreateBadTextStatus() {
460 'source_contact_id' => $this->_contactID
,
461 'subject' => 'Discussion on Apis for v3',
462 'activity_date_time' => date('Ymd'),
464 'location' => 'Pennsylvania',
465 'details' => 'a test activity',
466 'status_id' => 'Invalid',
467 'activity_name' => 'Test activity type',
470 $this->callAPIFailure('activity', 'create', $params);
474 * Test civicrm_activity_create() with an invalid text status_id.
476 public function testActivityCreateSupportActivityStatus() {
479 'source_contact_id' => $this->_contactID
,
480 'subject' => 'Discussion on Apis for v3',
481 'activity_date_time' => date('Ymd'),
483 'location' => 'Pennsylvania',
484 'details' => 'a test activity',
485 'activity_status_id' => 'Invalid',
486 'activity_name' => 'Test activity type',
489 $result = $this->callAPIFailure('activity', 'create', $params,
490 "'Invalid' is not a valid option for field status_id");
495 * Test civicrm_activity_create() with using a text status_id.
497 public function testActivityCreateTextStatus() {
500 'source_contact_id' => $this->_contactID
,
501 'subject' => 'Make-it-Happen Meeting',
502 'activity_date_time' => date('Ymd'),
504 'location' => 'Pennsylvania',
505 'details' => 'a test activity',
506 'status_id' => 'Scheduled',
507 'activity_name' => 'Test activity type',
510 $result = $this->callAPISuccess('activity', 'create', $params);
511 $this->assertEquals($result['values'][$result['id']]['duration'], 120);
512 $this->assertEquals($result['values'][$result['id']]['subject'], 'Make-it-Happen Meeting');
513 $this->assertEquals($result['values'][$result['id']]['activity_date_time'], date('Ymd') . '000000');
514 $this->assertEquals($result['values'][$result['id']]['location'], 'Pennsylvania');
515 $this->assertEquals($result['values'][$result['id']]['details'], 'a test activity');
519 * Test civicrm_activity_get() with no params
521 public function testActivityGetEmpty() {
522 $result = $this->callAPISuccess('activity', 'get', array());
526 * Test civicrm_activity_get() with a good activity ID
528 public function testActivityGetGoodID1() {
529 // Insert rows in civicrm_activity creating activities 4 and 13
530 $description = "Demonstrates getting assignee_contact_id & using it to get the contact.";
531 $subfile = 'ReturnAssigneeContact';
532 $activity = $this->callAPISuccess('activity', 'create', $this->_params
);
534 $contact = $this->callAPISuccess('Contact', 'Create', array(
535 'first_name' => "The Rock",
536 'last_name' => 'roccky',
537 'contact_type' => 'Individual',
539 'api.activity.create' => array(
540 'id' => $activity['id'],
541 'assignee_contact_id' => '$value.id',
546 'activity_id' => $activity['id'],
547 'version' => $this->_apiversion
,
549 'return.assignee_contact_id' => 1,
550 'api.contact.get' => array(
551 'id' => '$value.source_contact_id',
555 $result = $this->callAPIAndDocument('Activity', 'Get', $params, __FUNCTION__
, __FILE__
, $description, $subfile);
557 $this->assertEquals($activity['id'], $result['id']);
559 $this->assertEquals($contact['id'], $result['values'][0]['assignee_contact_id'][0]);
561 $this->assertEquals($this->_contactID
, $result['values'][0]['api.contact.get']['values'][0]['contact_id']);
562 $this->assertEquals($this->test_activity_type_value
, $result['values'][0]['activity_type_id']);
563 $this->assertEquals("test activity type id", $result['values'][0]['subject']);
567 * test that get functioning does filtering.
569 public function testGetFilter() {
571 'source_contact_id' => $this->_contactID
,
572 'subject' => 'Make-it-Happen Meeting',
573 'activity_date_time' => '20110316',
575 'location' => 'Pennsylvania',
576 'details' => 'a test activity',
578 'activity_name' => 'Test activity type',
581 $result = $this->callAPISuccess('Activity', 'Create', $params);
582 $this->callAPISuccess('Activity', 'Get', array('subject' => 'Make-it-Happen Meeting'));
583 $this->assertEquals(1, $result['count']);
584 $this->assertEquals('Make-it-Happen Meeting', $result['values'][$result['id']]['subject']);
585 $this->callAPISuccess('Activity', 'Delete', array('id' => $result['id']));
590 * Test civicrm_activity_get() with filter target_contact_id
592 public function testActivityGetTargetFilter() {
593 $params = $this->_params
;
594 $contact1Params = array(
595 'first_name' => 'John',
596 'middle_name' => 'J.',
597 'last_name' => 'Anderson',
600 'email' => 'john_anderson@civicrm.org',
601 'contact_type' => 'Individual',
604 $contact1 = $this->individualCreate($contact1Params);
605 $contact2Params = array(
606 'first_name' => 'Michal',
607 'middle_name' => 'J.',
608 'last_name' => 'Anderson',
611 'email' => 'michal_anderson@civicrm.org',
612 'contact_type' => 'Individual',
615 $contact2 = $this->individualCreate($contact2Params);
617 $params['assignee_contact_id'] = array($contact1, $contact2);
618 $params['target_contact_id'] = array($contact2 => $contact2);
619 $activity = $this->callAPISuccess('Activity', 'Create', $params);
621 $activityget = $this->callAPISuccess('Activity', 'get', array(
622 'id' => $activity['id'],
623 'target_contact_id' => $contact2,
624 'return.target_contact_id' => 1,
626 $this->assertEquals($activity['id'], $activityget['id']);
627 $this->assertEquals($contact2, $activityget['values'][$activityget['id']]['target_contact_id'][0]);
629 $activityget = $this->callAPISuccess('activity', 'get', array(
630 'target_contact_id' => $this->_contactID
,
631 'return.target_contact_id' => 1,
632 'id' => $activity['id'],
634 if ($activityget['count'] > 0) {
635 $this->assertNotEquals($contact2, $activityget['values'][$activityget['id']]['target_contact_id'][0]);
640 * test that get functioning does filtering.
642 public function testGetStatusID() {
644 'source_contact_id' => $this->_contactID
,
645 'subject' => 'Make-it-Happen Meeting',
646 'activity_date_time' => '20110316',
648 'location' => 'Pennsylvania',
649 'details' => 'a test activity',
651 'activity_name' => 'Test activity type',
654 $this->callAPISuccess('Activity', 'Create', $params);
655 $result = $this->callAPISuccess('Activity', 'Get', array('activity_status_id' => '1'));
656 $this->assertEquals(1, $result['count'], 'one activity of status 1 should exist');
658 $result = $this->callAPISuccess('Activity', 'Get', array('status_id' => '1'));
659 $this->assertEquals(1, $result['count'], 'status_id should also work');
661 $result = $this->callAPISuccess('Activity', 'Get', array('activity_status_id' => '2'));
662 $this->assertEquals(0, $result['count'], 'No activities of status 1 should exist');
663 $result = $this->callAPISuccess('Activity', 'Get', array(
664 'version' => $this->_apiversion
,
667 $this->assertEquals(0, $result['count'], 'No activities of status 1 should exist');
672 * test that get functioning does filtering.
674 public function testGetFilterMaxDate() {
676 'source_contact_id' => $this->_contactID
,
677 'subject' => 'Make-it-Happen Meeting',
678 'activity_date_time' => '20110101',
680 'location' => 'Pennsylvania',
681 'details' => 'a test activity',
683 'activity_name' => 'Test activity type',
684 'version' => $this->_apiversion
,
687 $activityOne = $this->callAPISuccess('Activity', 'Create', $params);
688 $params['activity_date_time'] = 20120216;
689 $activityTwo = $this->callAPISuccess('Activity', 'Create', $params);
690 $result = $this->callAPISuccess('Activity', 'Get', array(
693 $description = "Demonstrates _low filter (at time of writing doesn't work if contact_id is set.";
694 $subfile = "DateTimeLow";
695 $this->assertEquals(2, $result['count']);
698 'filter.activity_date_time_low' => '20120101000000',
701 $result = $this->callAPIAndDocument('Activity', 'Get', $params, __FUNCTION__
, __FILE__
, $description, $subfile);
702 $this->assertEquals(1, $result['count']);
703 $description = "Demonstrates _high filter (at time of writing doesn't work if contact_id is set.";
704 $subfile = "DateTimeHigh";
705 $this->assertEquals('2012-02-16 00:00:00', $result['values'][0]['activity_date_time']);
707 'source_contact_id' => $this->_contactID
,
709 'filter.activity_date_time_high' => '20120101000000',
712 $result = $this->callAPIAndDocument('Activity', 'Get', $params, __FUNCTION__
, __FILE__
, $description, $subfile);
714 $this->assertEquals(1, $result['count']);
715 $this->assertEquals('2011-01-01 00:00:00', $result['values'][0]['activity_date_time']);
717 $this->callAPISuccess('Activity', 'Delete', array('version' => 3, 'id' => $activityOne['id']));
718 $this->callAPISuccess('Activity', 'Delete', array('version' => 3, 'id' => $activityTwo['id']));
722 * Test civicrm_activity_get() with a good activity ID which
723 * has associated custom data
725 public function testActivityGetGoodIDCustom() {
726 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__
, __FILE__
);
728 $params = $this->_params
;
729 $params['custom_' . $ids['custom_field_id']] = "custom string";
731 $this->callAPISuccess($this->_entity
, 'create', $params);
733 // Retrieve the test value.
735 'activity_type_id' => $this->test_activity_type_value
,
737 'return.custom_' . $ids['custom_field_id'] => 1,
739 $result = $this->callAPIAndDocument('activity', 'get', $params, __FUNCTION__
, __FILE__
);
740 $this->assertEquals("custom string", $result['values'][0]['custom_' . $ids['custom_field_id']]);
742 $this->assertEquals($this->test_activity_type_value
, $result['values'][0]['activity_type_id']);
743 $this->assertEquals('test activity type id', $result['values'][0]['subject']);
744 $this->customFieldDelete($ids['custom_field_id']);
745 $this->customGroupDelete($ids['custom_group_id']);
749 * Test civicrm_activity_get() with a good activity ID which
750 * has associated custom data
752 public function testActivityGetContact_idCustom() {
753 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__
, __FILE__
);
755 $params = $this->_params
;
756 $params['custom_' . $ids['custom_field_id']] = "custom string";
758 $result = $this->callAPISuccess($this->_entity
, 'create', $params);
759 // Retrieve the test value
761 'contact_id' => $this->_params
['source_contact_id'],
762 'activity_type_id' => $this->test_activity_type_value
,
764 'return.custom_' . $ids['custom_field_id'] => 1,
766 $result = $this->callAPIAndDocument('activity', 'get', $params, __FUNCTION__
, __FILE__
);
767 $this->assertEquals("custom string", $result['values'][0]['custom_' . $ids['custom_field_id']], ' in line ' . __LINE__
);
769 $this->assertEquals($this->test_activity_type_value
, $result['values'][0]['activity_type_id']);
770 $this->assertEquals('test activity type id', $result['values'][0]['subject']);
771 $this->assertEquals($result['values'][0]['id'], $result['id']);
775 * Check activity deletion with empty params.
777 public function testDeleteActivityForEmptyParams() {
778 $params = array('version' => $this->_apiversion
);
779 $this->callAPIFailure('activity', 'delete', $params);
783 * Check activity deletion without activity id.
785 public function testDeleteActivityWithoutId() {
787 'activity_name' => 'Meeting',
788 'version' => $this->_apiversion
,
790 $result = $this->callAPIFailure('activity', 'delete', $params);
794 * Check activity deletion without activity type.
796 public function testDeleteActivityWithoutActivityType() {
797 $params = array('id' => 1);
798 $result = $this->callAPIFailure('activity', 'delete', $params);
802 * Check activity deletion with incorrect data.
804 public function testDeleteActivityWithIncorrectActivityType() {
807 'activity_name' => 'Test Activity',
810 $result = $this->callAPIFailure('activity', 'delete', $params);
814 * Check activity deletion with correct data.
816 public function testDeleteActivity() {
817 $result = $this->callAPISuccess('activity', 'create', $this->_params
);
819 'id' => $result['id'],
820 'version' => $this->_apiversion
,
823 $this->callAPIAndDocument('activity', 'delete', $params, __FUNCTION__
, __FILE__
);
827 * Check if required fields are not passed.
829 public function testActivityUpdateWithoutRequired() {
831 'subject' => 'this case should fail',
832 'scheduled_date_time' => date('Ymd'),
835 $result = $this->callAPIFailure('activity', 'create', $params);
839 * Test civicrm_activity_update() with non-numeric id
841 public function testActivityUpdateWithNonNumericId() {
843 'id' => 'lets break it',
844 'activity_name' => 'Meeting',
845 'subject' => 'this case should fail',
846 'scheduled_date_time' => date('Ymd'),
849 $result = $this->callAPIFailure('activity', 'create', $params);
853 * Check with incorrect required fields.
855 public function testActivityUpdateWithIncorrectContactActivityType() {
858 'activity_name' => 'Test Activity',
859 'subject' => 'this case should fail',
860 'scheduled_date_time' => date('Ymd'),
861 'source_contact_id' => $this->_contactID
,
864 $result = $this->callAPIFailure('activity', 'create', $params,
865 'Invalid Activity Id');
869 * Test civicrm_activity_update() to update an existing activity
871 public function testActivityUpdate() {
872 $result = $this->callAPISuccess('activity', 'create', $this->_params
);
875 'id' => $result['id'],
876 'subject' => 'Make-it-Happen Meeting',
877 'activity_date_time' => '20091011123456',
879 'location' => '21, Park Avenue',
880 'details' => 'Lets update Meeting',
882 'source_contact_id' => $this->_contactID
,
886 $result = $this->callAPISuccess('activity', 'create', $params);
887 //hack on date comparison - really we should make getAndCheck smarter to handle dates
888 $params['activity_date_time'] = '2009-10-11 12:34:56';
889 // we also unset source_contact_id since it is stored in an aux table
890 unset($params['source_contact_id']);
891 $this->getAndCheck($params, $result['id'], 'activity');
895 * Test civicrm_activity_update() with valid parameters
896 * and some custom data
898 public function testActivityUpdateCustom() {
899 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__
, __FILE__
);
901 $params = $this->_params
;
903 // Create an activity with custom data
904 //this has been updated from the previous 'old format' function - need to make it work
906 'source_contact_id' => $this->_contactID
,
907 'subject' => 'Make-it-Happen Meeting',
908 'activity_date_time' => '2009-10-18',
910 'location' => 'Pennsylvania',
911 'details' => 'a test activity to check the update api',
913 'activity_name' => 'Test activity type',
914 'version' => $this->_apiversion
,
915 'custom_' . $ids['custom_field_id'] => 'custom string',
917 $result = $this->callAPISuccess('activity', 'create', $params);
919 $activityId = $result['id'];
920 $result = $this->callAPISuccess($this->_entity
, 'get', array(
921 'return.custom_' . $ids['custom_field_id'] => 1,
923 'id' => $result['id'],
925 $this->assertEquals("custom string", $result['values'][$result['id']]['custom_' . $ids['custom_field_id']]);
926 $this->assertEquals("2009-10-18 00:00:00", $result['values'][$result['id']]['activity_date_time']);
927 $fields = $this->callAPISuccess('activity', 'getfields', array('version' => $this->_apiversion
));
928 $this->assertTrue(is_array($fields['values']['custom_' . $ids['custom_field_id']]));
930 // Update the activity with custom data.
933 'source_contact_id' => $this->_contactID
,
934 'subject' => 'Make-it-Happen Meeting',
936 'activity_name' => 'Test activity type',
937 // add this since dates are messed up
938 'activity_date_time' => date('Ymd'),
939 'custom_' . $ids['custom_field_id'] => 'Updated my test data',
940 'version' => $this->_apiversion
,
942 $result = $this->callAPISuccess('Activity', 'Create', $params);
944 $result = $this->callAPISuccess($this->_entity
, 'get', array(
945 'return.custom_' . $ids['custom_field_id'] => 1,
947 'id' => $result['id'],
949 $this->assertEquals("Updated my test data", $result['values'][$result['id']]['custom_' . $ids['custom_field_id']]);
953 * Test civicrm_activity_update() for core activity fields
954 * and some custom data
956 public function testActivityUpdateCheckCoreFields() {
957 $params = $this->_params
;
958 $contact1Params = array(
959 'first_name' => 'John',
960 'middle_name' => 'J.',
961 'last_name' => 'Anderson',
964 'email' => 'john_anderson@civicrm.org',
965 'contact_type' => 'Individual',
968 $contact1 = $this->individualCreate($contact1Params);
969 $contact2Params = array(
970 'first_name' => 'Michal',
971 'middle_name' => 'J.',
972 'last_name' => 'Anderson',
975 'email' => 'michal_anderson@civicrm.org',
976 'contact_type' => 'Individual',
979 $contact2 = $this->individualCreate($contact2Params);
981 $params['assignee_contact_id'] = array($contact1, $contact2);
982 $params['target_contact_id'] = array($contact2 => $contact2);
983 $result = $this->callAPISuccess('Activity', 'Create', $params);
985 $activityId = $result['id'];
987 'return.assignee_contact_id' => 1,
988 'return.target_contact_id' => 1,
989 'version' => $this->_apiversion
,
992 $result = $this->callAPISuccess($this->_entity
, 'get', $getParams);
993 $assignee = $result['values'][$result['id']]['assignee_contact_id'];
994 $target = $result['values'][$result['id']]['target_contact_id'];
995 $this->assertEquals(2, count($assignee), ' in line ' . __LINE__
);
996 $this->assertEquals(1, count($target), ' in line ' . __LINE__
);
997 $this->assertEquals(TRUE, in_array($contact1, $assignee), ' in line ' . __LINE__
);
998 $this->assertEquals(TRUE, in_array($contact2, $target), ' in line ' . __LINE__
);
1000 $contact3Params = array(
1001 'first_name' => 'Jijo',
1002 'middle_name' => 'J.',
1003 'last_name' => 'Anderson',
1006 'email' => 'jijo_anderson@civicrm.org',
1007 'contact_type' => 'Individual',
1010 $contact4Params = array(
1011 'first_name' => 'Grant',
1012 'middle_name' => 'J.',
1013 'last_name' => 'Anderson',
1016 'email' => 'grant_anderson@civicrm.org',
1017 'contact_type' => 'Individual',
1020 $contact3 = $this->individualCreate($contact3Params);
1021 $contact4 = $this->individualCreate($contact4Params);
1024 $params['id'] = $activityId;
1025 $params['assignee_contact_id'] = array($contact3 => $contact3);
1026 $params['target_contact_id'] = array($contact4 => $contact4);
1028 $result = $this->callAPISuccess('activity', 'create', $params);
1030 $this->assertEquals($activityId, $result['id'], ' in line ' . __LINE__
);
1032 $result = $this->callAPISuccess(
1036 'return.assignee_contact_id' => 1,
1037 'return.target_contact_id' => 1,
1038 'return.source_contact_id' => 1,
1039 'id' => $result['id'],
1043 $assignee = $result['values'][$result['id']]['assignee_contact_id'];
1044 $target = $result['values'][$result['id']]['target_contact_id'];
1046 $this->assertEquals(1, count($assignee), ' in line ' . __LINE__
);
1047 $this->assertEquals(1, count($target), ' in line ' . __LINE__
);
1048 $this->assertEquals(TRUE, in_array($contact3, $assignee), ' in line ' . __LINE__
);
1049 $this->assertEquals(TRUE, in_array($contact4, $target), ' in line ' . __LINE__
);
1051 foreach ($this->_params
as $fld => $val) {
1052 $this->assertEquals($val, $result['values'][$result['id']][$fld]);
1057 * Test civicrm_activity_update() where the DB has a date_time
1058 * value and there is none in the update params.
1060 public function testActivityUpdateNotDate() {
1061 $result = $this->callAPISuccess('activity', 'create', $this->_params
);
1064 'id' => $result['id'],
1065 'subject' => 'Make-it-Happen Meeting',
1067 'location' => '21, Park Avenue',
1068 'details' => 'Lets update Meeting',
1070 'source_contact_id' => $this->_contactID
,
1074 $result = $this->callAPISuccess('activity', 'create', $params);
1075 //hack on date comparison - really we should make getAndCheck smarter to handle dates
1076 $params['activity_date_time'] = $this->_params
['activity_date_time'];
1077 // we also unset source_contact_id since it is stored in an aux table
1078 unset($params['source_contact_id']);
1079 $this->getAndCheck($params, $result['id'], 'activity');
1083 * Check activity update with status.
1085 public function testActivityUpdateWithStatus() {
1086 $activity = $this->callAPISuccess('activity', 'create', $this->_params
);
1088 'id' => $activity['id'],
1089 'source_contact_id' => $this->_contactID
,
1090 'subject' => 'Hurry update works',
1092 'activity_name' => 'Test activity type',
1095 $result = $this->callAPISuccess('activity', 'create', $params);
1096 $this->assertEquals($result['id'], $activity['id']);
1097 $this->assertEquals($result['values'][$activity['id']]['subject'], 'Hurry update works');
1098 $this->assertEquals($result['values'][$activity['id']]['status_id'], 1
1103 * Test civicrm_activity_update() where the source_contact_id
1104 * is not in the update params.
1106 public function testActivityUpdateKeepSource() {
1107 $activity = $this->callAPISuccess('activity', 'create', $this->_params
);
1108 // Updating the activity but not providing anything for the source contact
1109 // (It was set as $this->_contactID earlier.)
1111 'id' => $activity['id'],
1112 'subject' => 'Updated Make-it-Happen Meeting',
1114 'location' => '21, Park Avenue',
1115 'details' => 'Lets update Meeting',
1117 'activity_name' => 'Test activity type',
1121 $result = $this->callAPISuccess('activity', 'create', $params);
1122 $findactivity = $this->callAPISuccess('Activity', 'Get', array('id' => $activity['id']));
1126 * Test civicrm_activities_contact_get()
1128 public function testActivitiesContactGet() {
1129 $activity = $this->callAPISuccess('activity', 'create', $this->_params
);
1130 $activity2 = $this->callAPISuccess('activity', 'create', $this->_params2
);
1131 // Get activities associated with contact $this->_contactID.
1133 'contact_id' => $this->_contactID
,
1135 $result = $this->callAPISuccess('activity', 'get', $params);
1137 $this->assertEquals(2, $result['count']);
1138 $this->assertEquals($this->test_activity_type_value
, $result['values'][$activity['id']]['activity_type_id']);
1139 $this->assertEquals('Test activity type', $result['values'][$activity['id']]['activity_name']);
1140 $this->assertEquals('Test activity type', $result['values'][$activity2['id']]['activity_name']);
1144 * Test chained Activity format.
1146 public function testChainedActivityGet() {
1148 $activity = $this->callAPISuccess('Contact', 'Create', array(
1149 'display_name' => "bob brown",
1150 'contact_type' => 'Individual',
1151 'api.activity_type.create' => array(
1153 'label' => 'send out letters',
1159 'api.activity.create' => array(
1160 'subject' => 'send letter',
1161 'activity_type_id' => '$value.api.activity_type.create.values.0.value',
1165 $result = $this->callAPISuccess('Activity', 'Get', array(
1166 'id' => $activity['id'],
1167 'return.assignee_contact_id' => 1,
1168 'api.contact.get' => array('api.pledge.get' => 1),
1173 * Test civicrm_activity_contact_get() with invalid Contact ID.
1175 public function testActivitiesContactGetWithInvalidContactId() {
1176 $params = array('contact_id' => 'contact');
1177 $this->callAPIFailure('activity', 'get', $params);
1181 * Test civicrm_activity_contact_get() with contact having no Activity.
1183 public function testActivitiesContactGetHavingNoActivity() {
1185 'first_name' => 'dan',
1186 'last_name' => 'conberg',
1187 'email' => 'dan.conberg@w.co.in',
1188 'contact_type' => 'Individual',
1191 $contact = $this->callAPISuccess('contact', 'create', $params);
1193 'contact_id' => $contact['id'],
1195 $result = $this->callAPISuccess('activity', 'get', $params);
1196 $this->assertEquals($result['count'], 0);
1200 * Test getfields function.
1202 public function testGetFields() {
1203 $params = array('action' => 'create');
1204 $result = $this->callAPIAndDocument('activity', 'getfields', $params, __FUNCTION__
, __FILE__
, NULL, NULL);
1205 $this->assertTrue(is_array($result['values']), 'get fields doesn\'t return values array');
1206 foreach ($result['values'] as $key => $value) {
1207 $this->assertTrue(is_array($value), $key . " is not an array");