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',
109 $this->quickCleanup($tablesToTruncate, TRUE);
110 $this->callAPISuccess('option_value', 'delete', array('id' => $this->test_activity_type_id
));
114 * Check fails with empty array.
116 public function testActivityCreateEmpty() {
117 $this->callAPIFailure('activity', 'create', array());
121 * Check if required fields are not passed.
123 public function testActivityCreateWithoutRequired() {
125 'subject' => 'this case should fail',
126 'scheduled_date_time' => date('Ymd'),
128 $result = $this->callAPIFailure('activity', 'create', $params);
132 * Test civicrm_activity_create() with mismatched activity_type_id
135 public function testActivityCreateMismatchNameType() {
137 'source_contact_id' => $this->_contactID
,
138 'subject' => 'Test activity',
139 'activity_date_time' => date('Ymd'),
141 'location' => 'Pennsylvania',
142 'details' => 'a test activity',
144 'activity_name' => 'Fubar activity type',
145 'activity_type_id' => 5,
146 'scheduled_date_time' => date('Ymd'),
149 $result = $this->callAPIFailure('activity', 'create', $params);
153 * Test civicrm_activity_id() with missing source_contact_id is put with the current user.
155 public function testActivityCreateWithMissingContactId() {
157 'subject' => 'Make-it-Happen Meeting',
158 'activity_date_time' => date('Ymd'),
160 'location' => 'Pennsylvania',
161 'details' => 'a test activity',
163 'activity_name' => 'Test activity type',
166 $this->callAPISuccess('activity', 'create', $params);
170 * Test civicrm_activity_id() with non-numeric source_contact_id.
172 public function testActivityCreateWithNonNumericContactId() {
174 'source_contact_id' => 'fubar',
175 'subject' => 'Make-it-Happen Meeting',
176 'activity_date_time' => date('Ymd'),
178 'location' => 'Pennsylvania',
179 'details' => 'a test activity',
181 'activity_name' => 'Test activity type',
184 $this->callAPIFailure('activity', 'create', $params);
188 * Ensure that an invalid activity type causes failure.
190 * Oddly enough this test was failing because the creation of the invalid type
191 * got added to the set up routine. Probably a mis-fix on a test
193 public function testActivityCreateWithNonNumericActivityTypeId() {
195 'source_contact_id' => $this->_contactID
,
196 'subject' => 'Make-it-Happen Meeting',
197 'activity_date_time' => date('Ymd'),
199 'location' => 'Pennsylvania',
200 'details' => 'a test activity',
202 'activity_type_id' => 'Invalid Test activity type',
205 $result = $this->callAPIFailure('activity', 'create', $params);
209 * Check with incorrect required fields.
211 public function testActivityCreateWithUnknownActivityTypeId() {
213 'source_contact_id' => $this->_contactID
,
214 'subject' => 'Make-it-Happen Meeting',
215 'activity_date_time' => date('Ymd'),
217 'location' => 'Pennsylvania',
218 'details' => 'a test activity',
220 'activity_type_id' => 699,
223 $result = $this->callAPIFailure('activity', 'create', $params);
226 public function testActivityCreateWithInvalidPriority() {
228 'source_contact_id' => $this->_contactID
,
229 'subject' => 'Make-it-Happen Meeting',
230 'activity_date_time' => date('Ymd'),
232 'location' => 'Pennsylvania',
233 'details' => 'a test activity',
236 'activity_type_id' => 1,
239 $result = $this->callAPIFailure('activity', 'create', $params,
240 "'44' is not a valid option for field priority_id");
241 $this->assertEquals('priority_id', $result['error_field']);
246 * Test create succeeds with valid string for priority.
248 public function testActivityCreateWithValidStringPriority() {
250 'source_contact_id' => $this->_contactID
,
251 'subject' => 'Make-it-Happen Meeting',
252 'activity_date_time' => date('Ymd'),
254 'location' => 'Pennsylvania',
255 'details' => 'a test activity',
257 'priority_id' => 'Urgent',
258 'activity_type_id' => 1,
261 $result = $this->callAPISuccess('activity', 'create', $params);
262 $this->assertEquals(1, $result['values'][$result['id']]['priority_id']);
266 * Test create fails with invalid priority string.
268 public function testActivityCreateWithInValidStringPriority() {
270 'source_contact_id' => $this->_contactID
,
271 'subject' => 'Make-it-Happen Meeting',
272 'activity_date_time' => date('Ymd'),
274 'location' => 'Pennsylvania',
275 'details' => 'a test activity',
277 'priority_id' => 'ergUrgent',
278 'activity_type_id' => 1,
281 $this->callAPIFailure('activity', 'create', $params,
282 "'ergUrgent' is not a valid option for field priority_id");
286 * Test civicrm_activity_create() with valid parameters.
288 public function testActivityCreate() {
290 $this->callAPISuccess('activity', 'create', $this->_params
);
291 $result = $this->callAPISuccess('activity', 'get', $this->_params
);
292 $this->assertEquals($result['values'][$result['id']]['duration'], 120);
293 $this->assertEquals($result['values'][$result['id']]['subject'], 'test activity type id');
294 $this->assertEquals($result['values'][$result['id']]['activity_date_time'], '2011-06-02 14:36:13');
295 $this->assertEquals($result['values'][$result['id']]['location'], 'Pennsylvania');
296 $this->assertEquals($result['values'][$result['id']]['details'], 'a test activity');
297 $this->assertEquals($result['values'][$result['id']]['status_id'], 2);
298 $this->assertEquals($result['values'][$result['id']]['id'], $result['id']);
302 * Test civicrm_activity_create() with valid parameters - use type_id.
304 public function testActivityCreateCampaignTypeID() {
305 $this->enableCiviCampaign();
308 'source_contact_id' => $this->_contactID
,
309 'subject' => 'Make-it-Happen Meeting',
310 'activity_date_time' => '20110316',
312 'location' => 'Pennsylvania',
313 'details' => 'a test activity',
315 'activity_type_id' => 29,
319 $result = $this->callAPISuccess('activity', 'create', $params);
321 $result = $this->callAPISuccess('activity', 'get', array('id' => $result['id']));
322 $this->assertEquals($result['values'][$result['id']]['duration'], 120);
323 $this->assertEquals($result['values'][$result['id']]['subject'], 'Make-it-Happen Meeting');
324 $this->assertEquals($result['values'][$result['id']]['activity_date_time'], '2011-03-16 00:00:00');
325 $this->assertEquals($result['values'][$result['id']]['location'], 'Pennsylvania');
326 $this->assertEquals($result['values'][$result['id']]['details'], 'a test activity');
327 $this->assertEquals($result['values'][$result['id']]['status_id'], 1);
331 * Test get returns target and assignee contacts.
333 public function testActivityReturnTargetAssignee() {
335 $description = "Demonstrates setting & retrieving activity target & source.";
336 $subfile = "GetTargetandAssignee";
338 'source_contact_id' => $this->_contactID
,
339 'subject' => 'Make-it-Happen Meeting',
340 'activity_date_time' => '20110316',
342 'location' => 'Pennsylvania',
343 'details' => 'a test activity',
345 'activity_type_id' => 1,
347 'target_contact_id' => $this->_contactID
,
348 'assignee_contact_id' => $this->_contactID
,
351 $result = $this->callAPIAndDocument('activity', 'create', $params, __FUNCTION__
, __FILE__
, $description, $subfile);
352 $result = $this->callAPISuccess('activity', 'get', array(
353 'id' => $result['id'],
354 'version' => $this->_apiversion
,
355 'return.assignee_contact_id' => 1,
356 'return.target_contact_id' => 1,
359 $this->assertEquals($this->_contactID
, $result['values'][$result['id']]['assignee_contact_id'][0]);
360 $this->assertEquals($this->_contactID
, $result['values'][$result['id']]['target_contact_id'][0]);
364 * Test civicrm_activity_create() using example code.
366 public function testActivityCreateExample() {
367 require_once 'api/v3/examples/Activity/Create.php';
368 $result = activity_create_example();
369 $expectedResult = activity_create_expectedresult();
370 $this->assertEquals($result, $expectedResult);
374 * Test civicrm_activity_create() with valid parameters and custom data.
376 public function testActivityCreateCustom() {
377 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__
, __FILE__
);
378 $params = $this->_params
;
379 $params['custom_' . $ids['custom_field_id']] = "custom string";
380 $result = $this->callAPIAndDocument($this->_entity
, 'create', $params, __FUNCTION__
, __FILE__
);
381 $result = $this->callAPISuccess($this->_entity
, 'get', array(
382 'return.custom_' . $ids['custom_field_id'] => 1,
383 'id' => $result['id'],
385 $this->assertEquals("custom string", $result['values'][$result['id']]['custom_' . $ids['custom_field_id']], ' in line ' . __LINE__
);
387 $this->customFieldDelete($ids['custom_field_id']);
388 $this->customGroupDelete($ids['custom_group_id']);
392 * Test civicrm_activity_create() with valid parameters and custom data.
394 public function testActivityCreateCustomContactRefField() {
396 $this->callAPISuccess('contact', 'create', array('id' => $this->_contactID
, 'sort_name' => 'Contact, Test'));
397 $subfile = 'ContactRefCustomField';
398 $description = "Demonstrates create with Contact Reference Custom Field.";
399 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__
, __FILE__
);
401 'custom_group_id' => $ids['custom_group_id'],
402 'name' => 'Worker_Lookup',
403 'label' => 'Worker Lookup',
404 'html_type' => 'Autocomplete-Select',
405 'data_type' => 'ContactReference',
407 'is_searchable' => 1,
411 $customField = $this->callAPISuccess('custom_field', 'create', $params);
412 $params = $this->_params
;
413 $params['custom_' . $customField['id']] = "$this->_contactID";
415 $result = $this->callAPIAndDocument($this->_entity
, 'create', $params, __FUNCTION__
, __FILE__
, $description, $subfile);
416 $result = $this->callAPIAndDocument($this->_entity
, 'get', array(
417 'return.custom_' . $customField['id'] => 1,
418 'id' => $result['id'],
419 ), __FUNCTION__
, __FILE__
, 'Get with Contact Ref Custom Field', 'ContactRefCustomFieldGet');
421 $this->assertEquals('Anderson, Anthony', $result['values'][$result['id']]['custom_' . $customField['id']]);
422 $this->assertEquals($this->_contactID
, $result['values'][$result['id']]['custom_' . $customField['id'] . "_id"], ' in line ' . __LINE__
);
423 $this->assertEquals('Anderson, Anthony', $result['values'][$result['id']]['custom_' . $customField['id'] . '_1'], ' in line ' . __LINE__
);
424 $this->assertEquals($this->_contactID
, $result['values'][$result['id']]['custom_' . $customField['id'] . "_1_id"], ' in line ' . __LINE__
);
425 $this->customFieldDelete($ids['custom_field_id']);
426 $this->customGroupDelete($ids['custom_group_id']);
430 * Test civicrm_activity_create() with an invalid text status_id.
432 public function testActivityCreateBadTextStatus() {
435 'source_contact_id' => $this->_contactID
,
436 'subject' => 'Discussion on Apis for v3',
437 'activity_date_time' => date('Ymd'),
439 'location' => 'Pennsylvania',
440 'details' => 'a test activity',
441 'status_id' => 'Invalid',
442 'activity_name' => 'Test activity type',
445 $this->callAPIFailure('activity', 'create', $params);
449 * Test civicrm_activity_create() with an invalid text status_id.
451 public function testActivityCreateSupportActivityStatus() {
454 'source_contact_id' => $this->_contactID
,
455 'subject' => 'Discussion on Apis for v3',
456 'activity_date_time' => date('Ymd'),
458 'location' => 'Pennsylvania',
459 'details' => 'a test activity',
460 'activity_status_id' => 'Invalid',
461 'activity_name' => 'Test activity type',
464 $result = $this->callAPIFailure('activity', 'create', $params,
465 "'Invalid' is not a valid option for field status_id");
470 * Test civicrm_activity_create() with using a text status_id.
472 public function testActivityCreateTextStatus() {
475 'source_contact_id' => $this->_contactID
,
476 'subject' => 'Make-it-Happen Meeting',
477 'activity_date_time' => date('Ymd'),
479 'location' => 'Pennsylvania',
480 'details' => 'a test activity',
481 'status_id' => 'Scheduled',
482 'activity_name' => 'Test activity type',
485 $result = $this->callAPISuccess('activity', 'create', $params);
486 $this->assertEquals($result['values'][$result['id']]['duration'], 120);
487 $this->assertEquals($result['values'][$result['id']]['subject'], 'Make-it-Happen Meeting');
488 $this->assertEquals($result['values'][$result['id']]['activity_date_time'], date('Ymd') . '000000');
489 $this->assertEquals($result['values'][$result['id']]['location'], 'Pennsylvania');
490 $this->assertEquals($result['values'][$result['id']]['details'], 'a test activity');
494 * Test civicrm_activity_get() with no params
496 public function testActivityGetEmpty() {
497 $result = $this->callAPISuccess('activity', 'get', array());
501 * Test civicrm_activity_get() with a good activity ID
503 public function testActivityGetGoodID1() {
504 // Insert rows in civicrm_activity creating activities 4 and 13
505 $description = "Demonstrates getting assignee_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 $this->callAPIAndDocument('activity', 'delete', $params, __FUNCTION__
, __FILE__
);
802 * Check if required fields are not passed.
804 public function testActivityUpdateWithoutRequired() {
806 'subject' => 'this case should fail',
807 'scheduled_date_time' => date('Ymd'),
810 $result = $this->callAPIFailure('activity', 'create', $params);
814 * Test civicrm_activity_update() with non-numeric id
816 public function testActivityUpdateWithNonNumericId() {
818 'id' => 'lets break it',
819 'activity_name' => 'Meeting',
820 'subject' => 'this case should fail',
821 'scheduled_date_time' => date('Ymd'),
824 $result = $this->callAPIFailure('activity', 'create', $params);
828 * Check with incorrect required fields.
830 public function testActivityUpdateWithIncorrectContactActivityType() {
833 'activity_name' => 'Test Activity',
834 'subject' => 'this case should fail',
835 'scheduled_date_time' => date('Ymd'),
836 'source_contact_id' => $this->_contactID
,
839 $result = $this->callAPIFailure('activity', 'create', $params,
840 'Invalid Activity Id');
844 * Test civicrm_activity_update() to update an existing activity
846 public function testActivityUpdate() {
847 $result = $this->callAPISuccess('activity', 'create', $this->_params
);
850 'id' => $result['id'],
851 'subject' => 'Make-it-Happen Meeting',
852 'activity_date_time' => '20091011123456',
854 'location' => '21, Park Avenue',
855 'details' => 'Lets update Meeting',
857 'source_contact_id' => $this->_contactID
,
861 $result = $this->callAPISuccess('activity', 'create', $params);
862 //hack on date comparison - really we should make getAndCheck smarter to handle dates
863 $params['activity_date_time'] = '2009-10-11 12:34:56';
864 // we also unset source_contact_id since it is stored in an aux table
865 unset($params['source_contact_id']);
866 $this->getAndCheck($params, $result['id'], 'activity');
870 * Test civicrm_activity_update() with valid parameters
871 * and some custom data
873 public function testActivityUpdateCustom() {
874 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__
, __FILE__
);
876 $params = $this->_params
;
878 // Create an activity with custom data
879 //this has been updated from the previous 'old format' function - need to make it work
881 'source_contact_id' => $this->_contactID
,
882 'subject' => 'Make-it-Happen Meeting',
883 'activity_date_time' => '2009-10-18',
885 'location' => 'Pennsylvania',
886 'details' => 'a test activity to check the update api',
888 'activity_name' => 'Test activity type',
889 'version' => $this->_apiversion
,
890 'custom_' . $ids['custom_field_id'] => 'custom string',
892 $result = $this->callAPISuccess('activity', 'create', $params);
894 $activityId = $result['id'];
895 $result = $this->callAPISuccess($this->_entity
, 'get', array(
896 'return.custom_' . $ids['custom_field_id'] => 1,
898 'id' => $result['id'],
900 $this->assertEquals("custom string", $result['values'][$result['id']]['custom_' . $ids['custom_field_id']]);
901 $this->assertEquals("2009-10-18 00:00:00", $result['values'][$result['id']]['activity_date_time']);
902 $fields = $this->callAPISuccess('activity', 'getfields', array('version' => $this->_apiversion
));
903 $this->assertTrue(is_array($fields['values']['custom_' . $ids['custom_field_id']]));
905 // Update the activity with custom data.
908 'source_contact_id' => $this->_contactID
,
909 'subject' => 'Make-it-Happen Meeting',
911 'activity_name' => 'Test activity type',
912 // add this since dates are messed up
913 'activity_date_time' => date('Ymd'),
914 'custom_' . $ids['custom_field_id'] => 'Updated my test data',
915 'version' => $this->_apiversion
,
917 $result = $this->callAPISuccess('Activity', 'Create', $params);
919 $result = $this->callAPISuccess($this->_entity
, 'get', array(
920 'return.custom_' . $ids['custom_field_id'] => 1,
922 'id' => $result['id'],
924 $this->assertEquals("Updated my test data", $result['values'][$result['id']]['custom_' . $ids['custom_field_id']]);
928 * Test civicrm_activity_update() for core activity fields
929 * and some custom data
931 public function testActivityUpdateCheckCoreFields() {
932 $params = $this->_params
;
933 $contact1Params = array(
934 'first_name' => 'John',
935 'middle_name' => 'J.',
936 'last_name' => 'Anderson',
939 'email' => 'john_anderson@civicrm.org',
940 'contact_type' => 'Individual',
943 $contact1 = $this->individualCreate($contact1Params);
944 $contact2Params = array(
945 'first_name' => 'Michal',
946 'middle_name' => 'J.',
947 'last_name' => 'Anderson',
950 'email' => 'michal_anderson@civicrm.org',
951 'contact_type' => 'Individual',
954 $contact2 = $this->individualCreate($contact2Params);
956 $params['assignee_contact_id'] = array($contact1, $contact2);
957 $params['target_contact_id'] = array($contact2 => $contact2);
958 $result = $this->callAPISuccess('Activity', 'Create', $params);
960 $activityId = $result['id'];
962 'return.assignee_contact_id' => 1,
963 'return.target_contact_id' => 1,
964 'version' => $this->_apiversion
,
967 $result = $this->callAPISuccess($this->_entity
, 'get', $getParams);
968 $assignee = $result['values'][$result['id']]['assignee_contact_id'];
969 $target = $result['values'][$result['id']]['target_contact_id'];
970 $this->assertEquals(2, count($assignee), ' in line ' . __LINE__
);
971 $this->assertEquals(1, count($target), ' in line ' . __LINE__
);
972 $this->assertEquals(TRUE, in_array($contact1, $assignee), ' in line ' . __LINE__
);
973 $this->assertEquals(TRUE, in_array($contact2, $target), ' in line ' . __LINE__
);
975 $contact3Params = array(
976 'first_name' => 'Jijo',
977 'middle_name' => 'J.',
978 'last_name' => 'Anderson',
981 'email' => 'jijo_anderson@civicrm.org',
982 'contact_type' => 'Individual',
985 $contact4Params = array(
986 'first_name' => 'Grant',
987 'middle_name' => 'J.',
988 'last_name' => 'Anderson',
991 'email' => 'grant_anderson@civicrm.org',
992 'contact_type' => 'Individual',
995 $contact3 = $this->individualCreate($contact3Params);
996 $contact4 = $this->individualCreate($contact4Params);
999 $params['id'] = $activityId;
1000 $params['assignee_contact_id'] = array($contact3 => $contact3);
1001 $params['target_contact_id'] = array($contact4 => $contact4);
1003 $result = $this->callAPISuccess('activity', 'create', $params);
1005 $this->assertEquals($activityId, $result['id'], ' in line ' . __LINE__
);
1007 $result = $this->callAPISuccess(
1011 'return.assignee_contact_id' => 1,
1012 'return.target_contact_id' => 1,
1013 'return.source_contact_id' => 1,
1014 'id' => $result['id'],
1018 $assignee = $result['values'][$result['id']]['assignee_contact_id'];
1019 $target = $result['values'][$result['id']]['target_contact_id'];
1021 $this->assertEquals(1, count($assignee), ' in line ' . __LINE__
);
1022 $this->assertEquals(1, count($target), ' in line ' . __LINE__
);
1023 $this->assertEquals(TRUE, in_array($contact3, $assignee), ' in line ' . __LINE__
);
1024 $this->assertEquals(TRUE, in_array($contact4, $target), ' in line ' . __LINE__
);
1026 foreach ($this->_params
as $fld => $val) {
1027 $this->assertEquals($val, $result['values'][$result['id']][$fld]);
1032 * Test civicrm_activity_update() where the DB has a date_time
1033 * value and there is none in the update params.
1035 public function testActivityUpdateNotDate() {
1036 $result = $this->callAPISuccess('activity', 'create', $this->_params
);
1039 'id' => $result['id'],
1040 'subject' => 'Make-it-Happen Meeting',
1042 'location' => '21, Park Avenue',
1043 'details' => 'Lets update Meeting',
1045 'source_contact_id' => $this->_contactID
,
1049 $result = $this->callAPISuccess('activity', 'create', $params);
1050 //hack on date comparison - really we should make getAndCheck smarter to handle dates
1051 $params['activity_date_time'] = $this->_params
['activity_date_time'];
1052 // we also unset source_contact_id since it is stored in an aux table
1053 unset($params['source_contact_id']);
1054 $this->getAndCheck($params, $result['id'], 'activity');
1058 * Check activity update with status.
1060 public function testActivityUpdateWithStatus() {
1061 $activity = $this->callAPISuccess('activity', 'create', $this->_params
);
1063 'id' => $activity['id'],
1064 'source_contact_id' => $this->_contactID
,
1065 'subject' => 'Hurry update works',
1067 'activity_name' => 'Test activity type',
1070 $result = $this->callAPISuccess('activity', 'create', $params);
1071 $this->assertEquals($result['id'], $activity['id']);
1072 $this->assertEquals($result['values'][$activity['id']]['subject'], 'Hurry update works');
1073 $this->assertEquals($result['values'][$activity['id']]['status_id'], 1
1078 * Test civicrm_activity_update() where the source_contact_id
1079 * is not in the update params.
1081 public function testActivityUpdateKeepSource() {
1082 $activity = $this->callAPISuccess('activity', 'create', $this->_params
);
1083 // Updating the activity but not providing anything for the source contact
1084 // (It was set as $this->_contactID earlier.)
1086 'id' => $activity['id'],
1087 'subject' => 'Updated Make-it-Happen Meeting',
1089 'location' => '21, Park Avenue',
1090 'details' => 'Lets update Meeting',
1092 'activity_name' => 'Test activity type',
1096 $result = $this->callAPISuccess('activity', 'create', $params);
1097 $findactivity = $this->callAPISuccess('Activity', 'Get', array('id' => $activity['id']));
1101 * Test civicrm_activities_contact_get()
1103 public function testActivitiesContactGet() {
1104 $activity = $this->callAPISuccess('activity', 'create', $this->_params
);
1105 $activity2 = $this->callAPISuccess('activity', 'create', $this->_params2
);
1106 // Get activities associated with contact $this->_contactID.
1108 'contact_id' => $this->_contactID
,
1110 $result = $this->callAPISuccess('activity', 'get', $params);
1112 $this->assertEquals(2, $result['count'], 'In line ' . __LINE__
);
1113 $this->assertEquals($this->test_activity_type_value
, $result['values'][$activity['id']]['activity_type_id']);
1114 $this->assertEquals('Test activity type', $result['values'][$activity['id']]['activity_name']);
1115 $this->assertEquals('Test activity type', $result['values'][$activity2['id']]['activity_name']);
1119 * Test chained Activity format.
1121 public function testChainedActivityGet() {
1123 $activity = $this->callAPISuccess('Contact', 'Create', array(
1124 'display_name' => "bob brown",
1125 'contact_type' => 'Individual',
1126 'api.activity_type.create' => array(
1128 'label' => 'send out letters',
1134 'api.activity.create' => array(
1135 'subject' => 'send letter',
1136 'activity_type_id' => '$value.api.activity_type.create.values.0.value',
1140 $result = $this->callAPISuccess('Activity', 'Get', array(
1141 'id' => $activity['id'],
1142 'return.assignee_contact_id' => 1,
1143 'api.contact.get' => array('api.pledge.get' => 1),
1148 * Test civicrm_activity_contact_get() with invalid Contact ID.
1150 public function testActivitiesContactGetWithInvalidContactId() {
1151 $params = array('contact_id' => 'contact');
1152 $this->callAPIFailure('activity', 'get', $params);
1156 * Test civicrm_activity_contact_get() with contact having no Activity.
1158 public function testActivitiesContactGetHavingNoActivity() {
1160 'first_name' => 'dan',
1161 'last_name' => 'conberg',
1162 'email' => 'dan.conberg@w.co.in',
1163 'contact_type' => 'Individual',
1166 $contact = $this->callAPISuccess('contact', 'create', $params);
1168 'contact_id' => $contact['id'],
1170 $result = $this->callAPISuccess('activity', 'get', $params);
1171 $this->assertEquals($result['count'], 0);
1175 * Test getfields function.
1177 public function testGetFields() {
1178 $params = array('action' => 'create');
1179 $result = $this->callAPIAndDocument('activity', 'getfields', $params, __FUNCTION__
, __FILE__
, NULL, NULL);
1180 $this->assertTrue(is_array($result['values']), 'get fields doesn\'t return values array');
1181 foreach ($result['values'] as $key => $value) {
1182 $this->assertTrue(is_array($value), $key . " is not an array");