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
35 require_once 'CiviTest/CiviUnitTestCase.php';
39 * Test APIv3 civicrm_activity_* functions
41 * @package CiviCRM_APIv3
42 * @subpackage API_Activity
44 class api_v3_ActivityTest
extends CiviUnitTestCase
{
47 protected $_entity = 'activity';
48 protected $_apiversion = 3;
49 protected $test_activity_type_value;
50 protected $_contactID;
53 * Test setup for every test.
55 * Connect to the database, truncate the tables that will be used
56 * and redirect stdin to a temporary file
58 public function setUp() {
59 // Connect to the database
62 $this->_contactID
= $this->individualCreate();
63 //create activity types
64 $activityTypes = $this->callAPISuccess('option_value', 'create', array(
65 'option_group_id' => 2,
66 'name' => 'Test activity type',
67 'label' => 'Test activity type',
70 $this->test_activity_type_value
= $activityTypes['values'][0]['value'];
71 $this->test_activity_type_id
= $activityTypes['id'];
72 $this->_params
= array(
73 'source_contact_id' => $this->_contactID
,
74 'activity_type_id' => $this->test_activity_type_value
,
75 'subject' => 'test activity type id',
76 'activity_date_time' => '2011-06-02 14:36:13',
80 'location' => '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',
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,
318 $result = $this->callAPISuccess('activity', 'create', $params);
320 $result = $this->callAPISuccess('activity', 'get', array('id' => $result['id']));
321 $this->assertEquals($result['values'][$result['id']]['duration'], 120);
322 $this->assertEquals($result['values'][$result['id']]['subject'], 'Make-it-Happen Meeting');
323 $this->assertEquals($result['values'][$result['id']]['activity_date_time'], '2011-03-16 00:00:00');
324 $this->assertEquals($result['values'][$result['id']]['location'], 'Pennsylvania');
325 $this->assertEquals($result['values'][$result['id']]['details'], 'a test activity');
326 $this->assertEquals($result['values'][$result['id']]['status_id'], 1);
330 * Test get returns target and assignee contacts.
332 public function testActivityReturnTargetAssignee() {
334 $description = "Example demonstrates setting & retrieving the target & source";
335 $subfile = "GetTargetandAssignee";
337 'source_contact_id' => $this->_contactID
,
338 'subject' => 'Make-it-Happen Meeting',
339 'activity_date_time' => '20110316',
341 'location' => 'Pennsylvania',
342 'details' => 'a test activity',
344 'activity_type_id' => 1,
346 'target_contact_id' => $this->_contactID
,
347 'assignee_contact_id' => $this->_contactID
,
350 $result = $this->callAPIAndDocument('activity', 'create', $params, __FUNCTION__
, __FILE__
, $description, $subfile);
351 $result = $this->callAPISuccess('activity', 'get', array(
352 'id' => $result['id'],
353 'version' => $this->_apiversion
,
354 'return.assignee_contact_id' => 1,
355 'return.target_contact_id' => 1,
358 $this->assertEquals($this->_contactID
, $result['values'][$result['id']]['assignee_contact_id'][0]);
359 $this->assertEquals($this->_contactID
, $result['values'][$result['id']]['target_contact_id'][0]);
363 * Test civicrm_activity_create() using example code.
365 public function testActivityCreateExample() {
366 require_once 'api/v3/examples/Activity/Create.php';
367 $result = activity_create_example();
368 $expectedResult = activity_create_expectedresult();
369 $this->assertEquals($result, $expectedResult);
373 * Test civicrm_activity_create() with valid parameters and custom data.
375 public function testActivityCreateCustom() {
376 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__
, __FILE__
);
377 $params = $this->_params
;
378 $params['custom_' . $ids['custom_field_id']] = "custom string";
379 $result = $this->callAPIAndDocument($this->_entity
, 'create', $params, __FUNCTION__
, __FILE__
);
380 $result = $this->callAPISuccess($this->_entity
, 'get', array(
381 'return.custom_' . $ids['custom_field_id'] => 1,
382 'id' => $result['id'],
384 $this->assertEquals("custom string", $result['values'][$result['id']]['custom_' . $ids['custom_field_id']], ' in line ' . __LINE__
);
386 $this->customFieldDelete($ids['custom_field_id']);
387 $this->customGroupDelete($ids['custom_group_id']);
391 * Test civicrm_activity_create() with valid parameters and custom data.
393 public function testActivityCreateCustomContactRefField() {
395 $this->callAPISuccess('contact', 'create', array('id' => $this->_contactID
, 'sort_name' => 'Contact, Test'));
396 $subfile = 'ContactRefCustomField';
397 $description = "demonstrates create with Contact Reference Custom Field";
398 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__
, __FILE__
);
400 'custom_group_id' => $ids['custom_group_id'],
401 'name' => 'Worker_Lookup',
402 'label' => 'Worker Lookup',
403 'html_type' => 'Autocomplete-Select',
404 'data_type' => 'ContactReference',
406 'is_searchable' => 1,
410 $customField = $this->callAPISuccess('custom_field', 'create', $params);
411 $params = $this->_params
;
412 $params['custom_' . $customField['id']] = "$this->_contactID";
414 $result = $this->callAPIAndDocument($this->_entity
, 'create', $params, __FUNCTION__
, __FILE__
, $description, $subfile, 'Create');
415 $result = $this->callAPIAndDocument($this->_entity
, 'get', array(
416 'return.custom_' . $customField['id'] => 1,
417 'id' => $result['id'],
418 ), __FUNCTION__
, __FILE__
, 'Get with Contact Ref Custom Field', 'ContactRefCustomFieldGet', 'get');
420 $this->assertEquals('Anderson, Anthony', $result['values'][$result['id']]['custom_' . $customField['id']]);
421 $this->assertEquals($this->_contactID
, $result['values'][$result['id']]['custom_' . $customField['id'] . "_id"], ' in line ' . __LINE__
);
422 $this->assertEquals('Anderson, Anthony', $result['values'][$result['id']]['custom_' . $customField['id'] . '_1'], ' in line ' . __LINE__
);
423 $this->assertEquals($this->_contactID
, $result['values'][$result['id']]['custom_' . $customField['id'] . "_1_id"], ' in line ' . __LINE__
);
424 $this->customFieldDelete($ids['custom_field_id']);
425 $this->customGroupDelete($ids['custom_group_id']);
429 * Test civicrm_activity_create() with an invalid text status_id.
431 public function testActivityCreateBadTextStatus() {
434 'source_contact_id' => $this->_contactID
,
435 'subject' => 'Discussion on Apis for v3',
436 'activity_date_time' => date('Ymd'),
438 'location' => 'Pennsylvania',
439 'details' => 'a test activity',
440 'status_id' => 'Invalid',
441 'activity_name' => 'Test activity type',
444 $this->callAPIFailure('activity', 'create', $params);
448 * Test civicrm_activity_create() with an invalid text status_id.
450 public function testActivityCreateSupportActivityStatus() {
453 'source_contact_id' => $this->_contactID
,
454 'subject' => 'Discussion on Apis for v3',
455 'activity_date_time' => date('Ymd'),
457 'location' => 'Pennsylvania',
458 'details' => 'a test activity',
459 'activity_status_id' => 'Invalid',
460 'activity_name' => 'Test activity type',
463 $result = $this->callAPIFailure('activity', 'create', $params,
464 "'Invalid' is not a valid option for field status_id");
469 * Test civicrm_activity_create() with using a text status_id.
471 public function testActivityCreateTextStatus() {
474 'source_contact_id' => $this->_contactID
,
475 'subject' => 'Make-it-Happen Meeting',
476 'activity_date_time' => date('Ymd'),
478 'location' => 'Pennsylvania',
479 'details' => 'a test activity',
480 'status_id' => 'Scheduled',
481 'activity_name' => 'Test activity type',
484 $result = $this->callAPISuccess('activity', 'create', $params);
485 $this->assertEquals($result['values'][$result['id']]['duration'], 120);
486 $this->assertEquals($result['values'][$result['id']]['subject'], 'Make-it-Happen Meeting');
487 $this->assertEquals($result['values'][$result['id']]['activity_date_time'], date('Ymd') . '000000');
488 $this->assertEquals($result['values'][$result['id']]['location'], 'Pennsylvania');
489 $this->assertEquals($result['values'][$result['id']]['details'], 'a test activity');
493 * Test civicrm_activity_get() with no params
495 public function testActivityGetEmpty() {
496 $result = $this->callAPISuccess('activity', 'get', array());
500 * Test civicrm_activity_get() with a good activity ID
502 public function testActivityGetGoodID1() {
503 // Insert rows in civicrm_activity creating activities 4 and
505 $description = "Function demonstrates getting asignee_contact_id & using it to get the contact";
506 $subfile = 'ReturnAssigneeContact';
507 $activity = $this->callAPISuccess('activity', 'create', $this->_params
);
509 $contact = $this->callAPISuccess('Contact', 'Create', array(
510 'first_name' => "The Rock",
511 'last_name' => 'roccky',
512 'contact_type' => 'Individual',
514 'api.activity.create' => array(
515 'id' => $activity['id'],
516 'assignee_contact_id' => '$value.id',
521 'activity_id' => $activity['id'],
522 'version' => $this->_apiversion
,
524 'return.assignee_contact_id' => 1,
525 'api.contact.get' => array(
526 'id' => '$value.source_contact_id',
530 $result = $this->callAPIAndDocument('Activity', 'Get', $params, __FUNCTION__
, __FILE__
, $description, $subfile);
532 $this->assertEquals($activity['id'], $result['id'], 'In line ' . __LINE__
);
534 $this->assertEquals($contact['id'], $result['values'][0]['assignee_contact_id'][0], 'In line ' . __LINE__
);
536 $this->assertEquals($this->_contactID
, $result['values'][0]['api.contact.get']['values'][0]['contact_id'], 'In line ' . __LINE__
);
537 $this->assertEquals($this->test_activity_type_value
, $result['values'][0]['activity_type_id'], 'In line ' . __LINE__
);
538 $this->assertEquals("test activity type id", $result['values'][0]['subject'], 'In line ' . __LINE__
);
542 * test that get functioning does filtering.
544 public function testGetFilter() {
546 'source_contact_id' => $this->_contactID
,
547 'subject' => 'Make-it-Happen Meeting',
548 'activity_date_time' => '20110316',
550 'location' => 'Pennsylvania',
551 'details' => 'a test activity',
553 'activity_name' => 'Test activity type',
556 $result = $this->callAPISuccess('Activity', 'Create', $params);
557 $this->callAPISuccess('Activity', 'Get', array('subject' => 'Make-it-Happen Meeting'));
558 $this->assertEquals(1, $result['count']);
559 $this->assertEquals('Make-it-Happen Meeting', $result['values'][$result['id']]['subject']);
560 $this->callAPISuccess('Activity', 'Delete', array('id' => $result['id']));
565 * Test civicrm_activity_get() with filter target_contact_id
567 public function testActivityGetTargetFilter() {
568 $params = $this->_params
;
569 $contact1Params = array(
570 'first_name' => 'John',
571 'middle_name' => 'J.',
572 'last_name' => 'Anderson',
575 'email' => 'john_anderson@civicrm.org',
576 'contact_type' => 'Individual',
579 $contact1 = $this->individualCreate($contact1Params);
580 $contact2Params = array(
581 'first_name' => 'Michal',
582 'middle_name' => 'J.',
583 'last_name' => 'Anderson',
586 'email' => 'michal_anderson@civicrm.org',
587 'contact_type' => 'Individual',
590 $contact2 = $this->individualCreate($contact2Params);
592 $params['assignee_contact_id'] = array($contact1, $contact2);
593 $params['target_contact_id'] = array($contact2 => $contact2);
594 $activity = $this->callAPISuccess('Activity', 'Create', $params);
596 $activityget = $this->callAPISuccess('Activity', 'get', array(
597 'id' => $activity['id'],
598 'target_contact_id' => $contact2,
599 'return.target_contact_id' => 1,
601 $this->assertEquals($activity['id'], $activityget['id'], 'In line ' . __LINE__
);
602 $this->assertEquals($contact2, $activityget['values'][$activityget['id']]['target_contact_id'][0], 'In line ' . __LINE__
);
604 $activityget = $this->callAPISuccess('activity', 'get', array(
605 'target_contact_id' => $this->_contactID
,
606 'return.target_contact_id' => 1,
607 'id' => $activity['id'],
609 if ($activityget['count'] > 0) {
610 $this->assertNotEquals($contact2, $activityget['values'][$activityget['id']]['target_contact_id'][0], 'In line ' . __LINE__
);
615 * test that get functioning does filtering.
617 public function testGetStatusID() {
619 'source_contact_id' => $this->_contactID
,
620 'subject' => 'Make-it-Happen Meeting',
621 'activity_date_time' => '20110316',
623 'location' => 'Pennsylvania',
624 'details' => 'a test activity',
626 'activity_name' => 'Test activity type',
629 $this->callAPISuccess('Activity', 'Create', $params);
630 $result = $this->callAPISuccess('Activity', 'Get', array('activity_status_id' => '1'));
631 $this->assertEquals(1, $result['count'], 'one activity of status 1 should exist');
633 $result = $this->callAPISuccess('Activity', 'Get', array('status_id' => '1'));
634 $this->assertEquals(1, $result['count'], 'status_id should also work');
636 $result = $this->callAPISuccess('Activity', 'Get', array('activity_status_id' => '2'));
637 $this->assertEquals(0, $result['count'], 'No activities of status 1 should exist');
638 $result = $this->callAPISuccess('Activity', 'Get', array(
639 'version' => $this->_apiversion
,
642 $this->assertEquals(0, $result['count'], 'No activities of status 1 should exist');
647 * test that get functioning does filtering.
649 public function testGetFilterMaxDate() {
651 'source_contact_id' => $this->_contactID
,
652 'subject' => 'Make-it-Happen Meeting',
653 'activity_date_time' => '20110101',
655 'location' => 'Pennsylvania',
656 'details' => 'a test activity',
658 'activity_name' => 'Test activity type',
659 'version' => $this->_apiversion
,
662 $activityOne = $this->callAPISuccess('Activity', 'Create', $params);
663 $params['activity_date_time'] = 20120216;
664 $activityTwo = $this->callAPISuccess('Activity', 'Create', $params);
665 $result = $this->callAPISuccess('Activity', 'Get', array(
668 $description = "demonstrates _low filter (at time of writing doesn't work if contact_id is set";
669 $subfile = "DateTimeLow";
670 $this->assertEquals(2, $result['count']);
673 'filter.activity_date_time_low' => '20120101000000',
676 $result = $this->callAPIAndDocument('Activity', 'Get', $params, __FUNCTION__
, __FILE__
, $description, $subfile);
677 $this->assertEquals(1, $result['count']);
678 $description = "demonstrates _high filter (at time of writing doesn't work if contact_id is set";
679 $subfile = "DateTimeHigh";
680 $this->assertEquals('2012-02-16 00:00:00', $result['values'][0]['activity_date_time']);
682 'source_contact_id' => $this->_contactID
,
684 'filter.activity_date_time_high' => '20120101000000',
687 $result = $this->callAPIAndDocument('Activity', 'Get', $params, __FUNCTION__
, __FILE__
, $description, $subfile);
689 $this->assertEquals(1, $result['count']);
690 $this->assertEquals('2011-01-01 00:00:00', $result['values'][0]['activity_date_time']);
692 $this->callAPISuccess('Activity', 'Delete', array('version' => 3, 'id' => $activityOne['id']));
693 $this->callAPISuccess('Activity', 'Delete', array('version' => 3, 'id' => $activityTwo['id']));
697 * Test civicrm_activity_get() with a good activity ID which
698 * has associated custom data
700 public function testActivityGetGoodIDCustom() {
701 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__
, __FILE__
);
703 $params = $this->_params
;
704 $params['custom_' . $ids['custom_field_id']] = "custom string";
706 $this->callAPISuccess($this->_entity
, 'create', $params);
708 // Retrieve the test value.
710 'activity_type_id' => $this->test_activity_type_value
,
712 'return.custom_' . $ids['custom_field_id'] => 1,
714 $result = $this->callAPIAndDocument('activity', 'get', $params, __FUNCTION__
, __FILE__
);
715 $this->assertEquals("custom string", $result['values'][0]['custom_' . $ids['custom_field_id']]);
717 $this->assertEquals($this->test_activity_type_value
, $result['values'][0]['activity_type_id']);
718 $this->assertEquals('test activity type id', $result['values'][0]['subject'], 'In line ' . __LINE__
);
719 $this->customFieldDelete($ids['custom_field_id']);
720 $this->customGroupDelete($ids['custom_group_id']);
724 * Test civicrm_activity_get() with a good activity ID which
725 * has associated custom data
727 public function testActivityGetContact_idCustom() {
728 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__
, __FILE__
);
730 $params = $this->_params
;
731 $params['custom_' . $ids['custom_field_id']] = "custom string";
733 $result = $this->callAPISuccess($this->_entity
, 'create', $params);
734 // Retrieve the test value
736 'contact_id' => $this->_params
['source_contact_id'],
737 'activity_type_id' => $this->test_activity_type_value
,
739 'return.custom_' . $ids['custom_field_id'] => 1,
741 $result = $this->callAPIAndDocument('activity', 'get', $params, __FUNCTION__
, __FILE__
);
742 $this->assertEquals("custom string", $result['values'][0]['custom_' . $ids['custom_field_id']], ' in line ' . __LINE__
);
744 $this->assertEquals($this->test_activity_type_value
, $result['values'][0]['activity_type_id'], 'In line ' . __LINE__
);
745 $this->assertEquals('test activity type id', $result['values'][0]['subject'], 'In line ' . __LINE__
);
746 $this->assertEquals($result['values'][0]['id'], $result['id']);
750 * Check activity deletion with empty params.
752 public function testDeleteActivityForEmptyParams() {
753 $params = array('version' => $this->_apiversion
);
754 $this->callAPIFailure('activity', 'delete', $params);
758 * Check activity deletion without activity id.
760 public function testDeleteActivityWithoutId() {
762 'activity_name' => 'Meeting',
763 'version' => $this->_apiversion
,
765 $result = $this->callAPIFailure('activity', 'delete', $params);
769 * Check activity deletion without activity type.
771 public function testDeleteActivityWithoutActivityType() {
772 $params = array('id' => 1);
773 $result = $this->callAPIFailure('activity', 'delete', $params);
777 * Check activity deletion with incorrect data.
779 public function testDeleteActivityWithIncorrectActivityType() {
782 'activity_name' => 'Test Activity',
785 $result = $this->callAPIFailure('activity', 'delete', $params);
789 * Check activity deletion with correct data.
791 public function testDeleteActivity() {
792 $result = $this->callAPISuccess('activity', 'create', $this->_params
);
794 'id' => $result['id'],
795 'version' => $this->_apiversion
,
798 $result = $this->callAPISuccess('activity', 'delete', $params);
799 $this->documentMe($params, $result, __FUNCTION__
, __FILE__
);
803 * Check if required fields are not passed.
805 public function testActivityUpdateWithoutRequired() {
807 'subject' => 'this case should fail',
808 'scheduled_date_time' => date('Ymd'),
811 $result = $this->callAPIFailure('activity', 'create', $params);
815 * Test civicrm_activity_update() with non-numeric id
817 public function testActivityUpdateWithNonNumericId() {
819 'id' => 'lets break it',
820 'activity_name' => 'Meeting',
821 'subject' => 'this case should fail',
822 'scheduled_date_time' => date('Ymd'),
825 $result = $this->callAPIFailure('activity', 'create', $params);
829 * Check with incorrect required fields.
831 public function testActivityUpdateWithIncorrectContactActivityType() {
834 'activity_name' => 'Test Activity',
835 'subject' => 'this case should fail',
836 'scheduled_date_time' => date('Ymd'),
837 'source_contact_id' => $this->_contactID
,
840 $result = $this->callAPIFailure('activity', 'create', $params,
841 'Invalid Activity Id');
845 * Test civicrm_activity_update() to update an existing activity
847 public function testActivityUpdate() {
848 $result = $this->callAPISuccess('activity', 'create', $this->_params
);
851 'id' => $result['id'],
852 'subject' => 'Make-it-Happen Meeting',
853 'activity_date_time' => '20091011123456',
855 'location' => '21, Park Avenue',
856 'details' => 'Lets update Meeting',
858 'source_contact_id' => $this->_contactID
,
862 $result = $this->callAPISuccess('activity', 'create', $params);
863 //hack on date comparison - really we should make getAndCheck smarter to handle dates
864 $params['activity_date_time'] = '2009-10-11 12:34:56';
865 // we also unset source_contact_id since it is stored in an aux table
866 unset($params['source_contact_id']);
867 $this->getAndCheck($params, $result['id'], 'activity');
871 * Test civicrm_activity_update() with valid parameters
872 * and some custom data
874 public function testActivityUpdateCustom() {
875 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__
, __FILE__
);
877 $params = $this->_params
;
879 // Create an activity with custom data
880 //this has been updated from the previous 'old format' function - need to make it work
882 'source_contact_id' => $this->_contactID
,
883 'subject' => 'Make-it-Happen Meeting',
884 'activity_date_time' => '2009-10-18',
886 'location' => 'Pennsylvania',
887 'details' => 'a test activity to check the update api',
889 'activity_name' => 'Test activity type',
890 'version' => $this->_apiversion
,
891 'custom_' . $ids['custom_field_id'] => 'custom string',
893 $result = $this->callAPISuccess('activity', 'create', $params);
895 $activityId = $result['id'];
896 $result = $this->callAPISuccess($this->_entity
, 'get', array(
897 'return.custom_' . $ids['custom_field_id'] => 1,
899 'id' => $result['id'],
901 $this->assertEquals("custom string", $result['values'][$result['id']]['custom_' . $ids['custom_field_id']]);
902 $this->assertEquals("2009-10-18 00:00:00", $result['values'][$result['id']]['activity_date_time']);
903 $fields = $this->callAPISuccess('activity', 'getfields', array('version' => $this->_apiversion
));
904 $this->assertTrue(is_array($fields['values']['custom_' . $ids['custom_field_id']]));
906 // Update the activity with custom data.
909 'source_contact_id' => $this->_contactID
,
910 'subject' => 'Make-it-Happen Meeting',
912 'activity_name' => 'Test activity type',
913 // add this since dates are messed up
914 'activity_date_time' => date('Ymd'),
915 'custom_' . $ids['custom_field_id'] => 'Updated my test data',
916 'version' => $this->_apiversion
,
918 $result = $this->callAPISuccess('Activity', 'Create', $params);
920 $result = $this->callAPISuccess($this->_entity
, 'get', array(
921 'return.custom_' . $ids['custom_field_id'] => 1,
923 'id' => $result['id'],
925 $this->assertEquals("Updated my test data", $result['values'][$result['id']]['custom_' . $ids['custom_field_id']]);
929 * Test civicrm_activity_update() for core activity fields
930 * and some custom data
932 public function testActivityUpdateCheckCoreFields() {
933 $params = $this->_params
;
934 $contact1Params = array(
935 'first_name' => 'John',
936 'middle_name' => 'J.',
937 'last_name' => 'Anderson',
940 'email' => 'john_anderson@civicrm.org',
941 'contact_type' => 'Individual',
944 $contact1 = $this->individualCreate($contact1Params);
945 $contact2Params = array(
946 'first_name' => 'Michal',
947 'middle_name' => 'J.',
948 'last_name' => 'Anderson',
951 'email' => 'michal_anderson@civicrm.org',
952 'contact_type' => 'Individual',
955 $contact2 = $this->individualCreate($contact2Params);
957 $params['assignee_contact_id'] = array($contact1, $contact2);
958 $params['target_contact_id'] = array($contact2 => $contact2);
959 $result = $this->callAPISuccess('Activity', 'Create', $params);
961 $activityId = $result['id'];
963 'return.assignee_contact_id' => 1,
964 'return.target_contact_id' => 1,
965 'version' => $this->_apiversion
,
968 $result = $this->callAPISuccess($this->_entity
, 'get', $getParams);
969 $assignee = $result['values'][$result['id']]['assignee_contact_id'];
970 $target = $result['values'][$result['id']]['target_contact_id'];
971 $this->assertEquals(2, count($assignee), ' in line ' . __LINE__
);
972 $this->assertEquals(1, count($target), ' in line ' . __LINE__
);
973 $this->assertEquals(TRUE, in_array($contact1, $assignee), ' in line ' . __LINE__
);
974 $this->assertEquals(TRUE, in_array($contact2, $target), ' in line ' . __LINE__
);
976 $contact3Params = array(
977 'first_name' => 'Jijo',
978 'middle_name' => 'J.',
979 'last_name' => 'Anderson',
982 'email' => 'jijo_anderson@civicrm.org',
983 'contact_type' => 'Individual',
986 $contact4Params = array(
987 'first_name' => 'Grant',
988 'middle_name' => 'J.',
989 'last_name' => 'Anderson',
992 'email' => 'grant_anderson@civicrm.org',
993 'contact_type' => 'Individual',
996 $contact3 = $this->individualCreate($contact3Params);
997 $contact4 = $this->individualCreate($contact4Params);
1000 $params['id'] = $activityId;
1001 $params['assignee_contact_id'] = array($contact3 => $contact3);
1002 $params['target_contact_id'] = array($contact4 => $contact4);
1004 $result = $this->callAPISuccess('activity', 'create', $params);
1006 $this->assertEquals($activityId, $result['id'], ' in line ' . __LINE__
);
1008 $result = $this->callAPISuccess(
1012 'return.assignee_contact_id' => 1,
1013 'return.target_contact_id' => 1,
1014 'return.source_contact_id' => 1,
1015 'id' => $result['id'],
1019 $assignee = $result['values'][$result['id']]['assignee_contact_id'];
1020 $target = $result['values'][$result['id']]['target_contact_id'];
1022 $this->assertEquals(1, count($assignee), ' in line ' . __LINE__
);
1023 $this->assertEquals(1, count($target), ' in line ' . __LINE__
);
1024 $this->assertEquals(TRUE, in_array($contact3, $assignee), ' in line ' . __LINE__
);
1025 $this->assertEquals(TRUE, in_array($contact4, $target), ' in line ' . __LINE__
);
1027 foreach ($this->_params
as $fld => $val) {
1028 $this->assertEquals($val, $result['values'][$result['id']][$fld]);
1033 * Test civicrm_activity_update() where the DB has a date_time
1034 * value and there is none in the update params.
1036 public function testActivityUpdateNotDate() {
1037 $result = $this->callAPISuccess('activity', 'create', $this->_params
);
1040 'id' => $result['id'],
1041 'subject' => 'Make-it-Happen Meeting',
1043 'location' => '21, Park Avenue',
1044 'details' => 'Lets update Meeting',
1046 'source_contact_id' => $this->_contactID
,
1050 $result = $this->callAPISuccess('activity', 'create', $params);
1051 //hack on date comparison - really we should make getAndCheck smarter to handle dates
1052 $params['activity_date_time'] = $this->_params
['activity_date_time'];
1053 // we also unset source_contact_id since it is stored in an aux table
1054 unset($params['source_contact_id']);
1055 $this->getAndCheck($params, $result['id'], 'activity');
1059 * Check activity update with status.
1061 public function testActivityUpdateWithStatus() {
1062 $activity = $this->callAPISuccess('activity', 'create', $this->_params
);
1064 'id' => $activity['id'],
1065 'source_contact_id' => $this->_contactID
,
1066 'subject' => 'Hurry update works',
1068 'activity_name' => 'Test activity type',
1071 $result = $this->callAPISuccess('activity', 'create', $params);
1072 $this->assertEquals($result['id'], $activity['id']);
1073 $this->assertEquals($result['values'][$activity['id']]['subject'], 'Hurry update works');
1074 $this->assertEquals($result['values'][$activity['id']]['status_id'], 1
1079 * Test civicrm_activity_update() where the source_contact_id
1080 * is not in the update params.
1082 public function testActivityUpdateKeepSource() {
1083 $activity = $this->callAPISuccess('activity', 'create', $this->_params
);
1084 // Updating the activity but not providing anything for the source contact
1085 // (It was set as $this->_contactID earlier.)
1087 'id' => $activity['id'],
1088 'subject' => 'Updated Make-it-Happen Meeting',
1090 'location' => '21, Park Avenue',
1091 'details' => 'Lets update Meeting',
1093 'activity_name' => 'Test activity type',
1097 $result = $this->callAPISuccess('activity', 'create', $params);
1098 $findactivity = $this->callAPISuccess('Activity', 'Get', array('id' => $activity['id']));
1102 * Test civicrm_activities_contact_get()
1104 public function testActivitiesContactGet() {
1105 $activity = $this->callAPISuccess('activity', 'create', $this->_params
);
1106 $activity2 = $this->callAPISuccess('activity', 'create', $this->_params2
);
1107 // Get activities associated with contact $this->_contactID.
1109 'contact_id' => $this->_contactID
,
1111 $result = $this->callAPISuccess('activity', 'get', $params);
1113 $this->assertEquals(2, $result['count'], 'In line ' . __LINE__
);
1114 $this->assertEquals($this->test_activity_type_value
, $result['values'][$activity['id']]['activity_type_id']);
1115 $this->assertEquals('Test activity type', $result['values'][$activity['id']]['activity_name']);
1116 $this->assertEquals('Test activity type', $result['values'][$activity2['id']]['activity_name']);
1120 * Test chained Activity format.
1122 public function testChainedActivityGet() {
1124 $activity = $this->callAPISuccess('Contact', 'Create', array(
1125 'display_name' => "bob brown",
1126 'contact_type' => 'Individual',
1127 'api.activity_type.create' => array(
1129 'label' => 'send out letters',
1135 'api.activity.create' => array(
1136 'subject' => 'send letter',
1137 'activity_type_id' => '$value.api.activity_type.create.values.0.value',
1141 $result = $this->callAPISuccess('Activity', 'Get', array(
1142 'id' => $activity['id'],
1143 'return.assignee_contact_id' => 1,
1144 'api.contact.get' => array('api.pledge.get' => 1),
1149 * Test civicrm_activity_contact_get() with invalid Contact ID.
1151 public function testActivitiesContactGetWithInvalidContactId() {
1152 $params = array('contact_id' => 'contact');
1153 $this->callAPIFailure('activity', 'get', $params);
1157 * Test civicrm_activity_contact_get() with contact having no Activity.
1159 public function testActivitiesContactGetHavingNoActivity() {
1161 'first_name' => 'dan',
1162 'last_name' => 'conberg',
1163 'email' => 'dan.conberg@w.co.in',
1164 'contact_type' => 'Individual',
1167 $contact = $this->callAPISuccess('contact', 'create', $params);
1169 'contact_id' => $contact['id'],
1171 $result = $this->callAPISuccess('activity', 'get', $params);
1172 $this->assertEquals($result['count'], 0);
1176 * Test getfields function.
1178 public function testGetFields() {
1179 $params = array('action' => 'create');
1180 $result = $this->callAPIAndDocument('activity', 'getfields', $params, __FUNCTION__
, __FILE__
, NULL, NULL, 'getfields');
1181 $this->assertTrue(is_array($result['values']), 'get fields doesn\'t return values array');
1182 foreach ($result['values'] as $key => $value) {
1183 $this->assertTrue(is_array($value), $key . " is not an array");