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 testActivityCreateCustomSubType() {
395 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__
, __FILE__
);
396 $this->callAPISuccess('CustomGroup', 'create', array(
397 'extends_entity_column_value' => $this->test_activity_type_value
,
398 'id' => $ids['custom_group_id'],
399 'extends' => 'Activity',
402 $params = $this->_params
;
403 $params['custom_' . $ids['custom_field_id']] = "custom string";
404 $result = $this->callAPIAndDocument($this->_entity
, 'create', $params, __FUNCTION__
, __FILE__
);
405 $result = $this->callAPISuccess($this->_entity
, 'get', array(
406 'return.custom_' . $ids['custom_field_id'] => 1,
407 'id' => $result['id'],
409 $this->assertEquals("custom string", $result['values'][$result['id']]['custom_' . $ids['custom_field_id']]);
411 $this->customFieldDelete($ids['custom_field_id']);
412 $this->customGroupDelete($ids['custom_group_id']);
416 * Test civicrm_activity_create() with valid parameters and custom data.
418 public function testActivityCreateCustomContactRefField() {
420 $this->callAPISuccess('contact', 'create', array('id' => $this->_contactID
, 'sort_name' => 'Contact, Test'));
421 $subfile = 'ContactRefCustomField';
422 $description = "Demonstrates create with Contact Reference Custom Field.";
423 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__
, __FILE__
);
425 'custom_group_id' => $ids['custom_group_id'],
426 'name' => 'Worker_Lookup',
427 'label' => 'Worker Lookup',
428 'html_type' => 'Autocomplete-Select',
429 'data_type' => 'ContactReference',
431 'is_searchable' => 1,
435 $customField = $this->callAPISuccess('custom_field', 'create', $params);
436 $params = $this->_params
;
437 $params['custom_' . $customField['id']] = "$this->_contactID";
439 $result = $this->callAPIAndDocument($this->_entity
, 'create', $params, __FUNCTION__
, __FILE__
, $description, $subfile);
440 $result = $this->callAPIAndDocument($this->_entity
, 'get', array(
441 'return.custom_' . $customField['id'] => 1,
442 'id' => $result['id'],
443 ), __FUNCTION__
, __FILE__
, 'Get with Contact Ref Custom Field', 'ContactRefCustomFieldGet');
445 $this->assertEquals('Anderson, Anthony', $result['values'][$result['id']]['custom_' . $customField['id']]);
446 $this->assertEquals($this->_contactID
, $result['values'][$result['id']]['custom_' . $customField['id'] . "_id"], ' in line ' . __LINE__
);
447 $this->assertEquals('Anderson, Anthony', $result['values'][$result['id']]['custom_' . $customField['id'] . '_1'], ' in line ' . __LINE__
);
448 $this->assertEquals($this->_contactID
, $result['values'][$result['id']]['custom_' . $customField['id'] . "_1_id"], ' in line ' . __LINE__
);
449 $this->customFieldDelete($ids['custom_field_id']);
450 $this->customGroupDelete($ids['custom_group_id']);
454 * Test civicrm_activity_create() with an invalid text status_id.
456 public function testActivityCreateBadTextStatus() {
459 'source_contact_id' => $this->_contactID
,
460 'subject' => 'Discussion on Apis for v3',
461 'activity_date_time' => date('Ymd'),
463 'location' => 'Pennsylvania',
464 'details' => 'a test activity',
465 'status_id' => 'Invalid',
466 'activity_name' => 'Test activity type',
469 $this->callAPIFailure('activity', 'create', $params);
473 * Test civicrm_activity_create() with an invalid text status_id.
475 public function testActivityCreateSupportActivityStatus() {
478 'source_contact_id' => $this->_contactID
,
479 'subject' => 'Discussion on Apis for v3',
480 'activity_date_time' => date('Ymd'),
482 'location' => 'Pennsylvania',
483 'details' => 'a test activity',
484 'activity_status_id' => 'Invalid',
485 'activity_name' => 'Test activity type',
488 $result = $this->callAPIFailure('activity', 'create', $params,
489 "'Invalid' is not a valid option for field status_id");
494 * Test civicrm_activity_create() with using a text status_id.
496 public function testActivityCreateTextStatus() {
499 'source_contact_id' => $this->_contactID
,
500 'subject' => 'Make-it-Happen Meeting',
501 'activity_date_time' => date('Ymd'),
503 'location' => 'Pennsylvania',
504 'details' => 'a test activity',
505 'status_id' => 'Scheduled',
506 'activity_name' => 'Test activity type',
509 $result = $this->callAPISuccess('activity', 'create', $params);
510 $this->assertEquals($result['values'][$result['id']]['duration'], 120);
511 $this->assertEquals($result['values'][$result['id']]['subject'], 'Make-it-Happen Meeting');
512 $this->assertEquals($result['values'][$result['id']]['activity_date_time'], date('Ymd') . '000000');
513 $this->assertEquals($result['values'][$result['id']]['location'], 'Pennsylvania');
514 $this->assertEquals($result['values'][$result['id']]['details'], 'a test activity');
518 * Test civicrm_activity_get() with no params
520 public function testActivityGetEmpty() {
521 $result = $this->callAPISuccess('activity', 'get', array());
525 * Test civicrm_activity_get() with a good activity ID
527 public function testActivityGetGoodID1() {
528 // Insert rows in civicrm_activity creating activities 4 and 13
529 $description = "Demonstrates getting assignee_contact_id & using it to get the contact.";
530 $subfile = 'ReturnAssigneeContact';
531 $activity = $this->callAPISuccess('activity', 'create', $this->_params
);
533 $contact = $this->callAPISuccess('Contact', 'Create', array(
534 'first_name' => "The Rock",
535 'last_name' => 'roccky',
536 'contact_type' => 'Individual',
538 'api.activity.create' => array(
539 'id' => $activity['id'],
540 'assignee_contact_id' => '$value.id',
545 'activity_id' => $activity['id'],
546 'version' => $this->_apiversion
,
548 'return.assignee_contact_id' => 1,
549 'api.contact.get' => array(
550 'id' => '$value.source_contact_id',
554 $result = $this->callAPIAndDocument('Activity', 'Get', $params, __FUNCTION__
, __FILE__
, $description, $subfile);
556 $this->assertEquals($activity['id'], $result['id']);
558 $this->assertEquals($contact['id'], $result['values'][0]['assignee_contact_id'][0]);
560 $this->assertEquals($this->_contactID
, $result['values'][0]['api.contact.get']['values'][0]['contact_id']);
561 $this->assertEquals($this->test_activity_type_value
, $result['values'][0]['activity_type_id']);
562 $this->assertEquals("test activity type id", $result['values'][0]['subject']);
566 * test that get functioning does filtering.
568 public function testGetFilter() {
570 'source_contact_id' => $this->_contactID
,
571 'subject' => 'Make-it-Happen Meeting',
572 'activity_date_time' => '20110316',
574 'location' => 'Pennsylvania',
575 'details' => 'a test activity',
577 'activity_name' => 'Test activity type',
580 $result = $this->callAPISuccess('Activity', 'Create', $params);
581 $this->callAPISuccess('Activity', 'Get', array('subject' => 'Make-it-Happen Meeting'));
582 $this->assertEquals(1, $result['count']);
583 $this->assertEquals('Make-it-Happen Meeting', $result['values'][$result['id']]['subject']);
584 $this->callAPISuccess('Activity', 'Delete', array('id' => $result['id']));
589 * Test civicrm_activity_get() with filter target_contact_id
591 public function testActivityGetTargetFilter() {
592 $params = $this->_params
;
593 $contact1Params = array(
594 'first_name' => 'John',
595 'middle_name' => 'J.',
596 'last_name' => 'Anderson',
599 'email' => 'john_anderson@civicrm.org',
600 'contact_type' => 'Individual',
603 $contact1 = $this->individualCreate($contact1Params);
604 $contact2Params = array(
605 'first_name' => 'Michal',
606 'middle_name' => 'J.',
607 'last_name' => 'Anderson',
610 'email' => 'michal_anderson@civicrm.org',
611 'contact_type' => 'Individual',
614 $contact2 = $this->individualCreate($contact2Params);
616 $params['assignee_contact_id'] = array($contact1, $contact2);
617 $params['target_contact_id'] = array($contact2 => $contact2);
618 $activity = $this->callAPISuccess('Activity', 'Create', $params);
620 $activityget = $this->callAPISuccess('Activity', 'get', array(
621 'id' => $activity['id'],
622 'target_contact_id' => $contact2,
623 'return.target_contact_id' => 1,
625 $this->assertEquals($activity['id'], $activityget['id']);
626 $this->assertEquals($contact2, $activityget['values'][$activityget['id']]['target_contact_id'][0]);
628 $activityget = $this->callAPISuccess('activity', 'get', array(
629 'target_contact_id' => $this->_contactID
,
630 'return.target_contact_id' => 1,
631 'id' => $activity['id'],
633 if ($activityget['count'] > 0) {
634 $this->assertNotEquals($contact2, $activityget['values'][$activityget['id']]['target_contact_id'][0]);
639 * test that get functioning does filtering.
641 public function testGetStatusID() {
643 'source_contact_id' => $this->_contactID
,
644 'subject' => 'Make-it-Happen Meeting',
645 'activity_date_time' => '20110316',
647 'location' => 'Pennsylvania',
648 'details' => 'a test activity',
650 'activity_name' => 'Test activity type',
653 $this->callAPISuccess('Activity', 'Create', $params);
654 $result = $this->callAPISuccess('Activity', 'Get', array('activity_status_id' => '1'));
655 $this->assertEquals(1, $result['count'], 'one activity of status 1 should exist');
657 $result = $this->callAPISuccess('Activity', 'Get', array('status_id' => '1'));
658 $this->assertEquals(1, $result['count'], 'status_id should also work');
660 $result = $this->callAPISuccess('Activity', 'Get', array('activity_status_id' => '2'));
661 $this->assertEquals(0, $result['count'], 'No activities of status 1 should exist');
662 $result = $this->callAPISuccess('Activity', 'Get', array(
663 'version' => $this->_apiversion
,
666 $this->assertEquals(0, $result['count'], 'No activities of status 1 should exist');
671 * test that get functioning does filtering.
673 public function testGetFilterMaxDate() {
675 'source_contact_id' => $this->_contactID
,
676 'subject' => 'Make-it-Happen Meeting',
677 'activity_date_time' => '20110101',
679 'location' => 'Pennsylvania',
680 'details' => 'a test activity',
682 'activity_name' => 'Test activity type',
683 'version' => $this->_apiversion
,
686 $activityOne = $this->callAPISuccess('Activity', 'Create', $params);
687 $params['activity_date_time'] = 20120216;
688 $activityTwo = $this->callAPISuccess('Activity', 'Create', $params);
689 $result = $this->callAPISuccess('Activity', 'Get', array(
692 $description = "Demonstrates _low filter (at time of writing doesn't work if contact_id is set.";
693 $subfile = "DateTimeLow";
694 $this->assertEquals(2, $result['count']);
697 'filter.activity_date_time_low' => '20120101000000',
700 $result = $this->callAPIAndDocument('Activity', 'Get', $params, __FUNCTION__
, __FILE__
, $description, $subfile);
701 $this->assertEquals(1, $result['count']);
702 $description = "Demonstrates _high filter (at time of writing doesn't work if contact_id is set.";
703 $subfile = "DateTimeHigh";
704 $this->assertEquals('2012-02-16 00:00:00', $result['values'][0]['activity_date_time']);
706 'source_contact_id' => $this->_contactID
,
708 'filter.activity_date_time_high' => '20120101000000',
711 $result = $this->callAPIAndDocument('Activity', 'Get', $params, __FUNCTION__
, __FILE__
, $description, $subfile);
713 $this->assertEquals(1, $result['count']);
714 $this->assertEquals('2011-01-01 00:00:00', $result['values'][0]['activity_date_time']);
716 $this->callAPISuccess('Activity', 'Delete', array('version' => 3, 'id' => $activityOne['id']));
717 $this->callAPISuccess('Activity', 'Delete', array('version' => 3, 'id' => $activityTwo['id']));
721 * Test civicrm_activity_get() with a good activity ID which
722 * has associated custom data
724 public function testActivityGetGoodIDCustom() {
725 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__
, __FILE__
);
727 $params = $this->_params
;
728 $params['custom_' . $ids['custom_field_id']] = "custom string";
730 $this->callAPISuccess($this->_entity
, 'create', $params);
732 // Retrieve the test value.
734 'activity_type_id' => $this->test_activity_type_value
,
736 'return.custom_' . $ids['custom_field_id'] => 1,
738 $result = $this->callAPIAndDocument('activity', 'get', $params, __FUNCTION__
, __FILE__
);
739 $this->assertEquals("custom string", $result['values'][0]['custom_' . $ids['custom_field_id']]);
741 $this->assertEquals($this->test_activity_type_value
, $result['values'][0]['activity_type_id']);
742 $this->assertEquals('test activity type id', $result['values'][0]['subject']);
743 $this->customFieldDelete($ids['custom_field_id']);
744 $this->customGroupDelete($ids['custom_group_id']);
748 * Test civicrm_activity_get() with a good activity ID which
749 * has associated custom data
751 public function testActivityGetContact_idCustom() {
752 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__
, __FILE__
);
754 $params = $this->_params
;
755 $params['custom_' . $ids['custom_field_id']] = "custom string";
757 $result = $this->callAPISuccess($this->_entity
, 'create', $params);
758 // Retrieve the test value
760 'contact_id' => $this->_params
['source_contact_id'],
761 'activity_type_id' => $this->test_activity_type_value
,
763 'return.custom_' . $ids['custom_field_id'] => 1,
765 $result = $this->callAPIAndDocument('activity', 'get', $params, __FUNCTION__
, __FILE__
);
766 $this->assertEquals("custom string", $result['values'][0]['custom_' . $ids['custom_field_id']], ' in line ' . __LINE__
);
768 $this->assertEquals($this->test_activity_type_value
, $result['values'][0]['activity_type_id']);
769 $this->assertEquals('test activity type id', $result['values'][0]['subject']);
770 $this->assertEquals($result['values'][0]['id'], $result['id']);
774 * Check activity deletion with empty params.
776 public function testDeleteActivityForEmptyParams() {
777 $params = array('version' => $this->_apiversion
);
778 $this->callAPIFailure('activity', 'delete', $params);
782 * Check activity deletion without activity id.
784 public function testDeleteActivityWithoutId() {
786 'activity_name' => 'Meeting',
787 'version' => $this->_apiversion
,
789 $result = $this->callAPIFailure('activity', 'delete', $params);
793 * Check activity deletion without activity type.
795 public function testDeleteActivityWithoutActivityType() {
796 $params = array('id' => 1);
797 $result = $this->callAPIFailure('activity', 'delete', $params);
801 * Check activity deletion with incorrect data.
803 public function testDeleteActivityWithIncorrectActivityType() {
806 'activity_name' => 'Test Activity',
809 $result = $this->callAPIFailure('activity', 'delete', $params);
813 * Check activity deletion with correct data.
815 public function testDeleteActivity() {
816 $result = $this->callAPISuccess('activity', 'create', $this->_params
);
818 'id' => $result['id'],
819 'version' => $this->_apiversion
,
822 $this->callAPIAndDocument('activity', 'delete', $params, __FUNCTION__
, __FILE__
);
826 * Check if required fields are not passed.
828 public function testActivityUpdateWithoutRequired() {
830 'subject' => 'this case should fail',
831 'scheduled_date_time' => date('Ymd'),
834 $result = $this->callAPIFailure('activity', 'create', $params);
838 * Test civicrm_activity_update() with non-numeric id
840 public function testActivityUpdateWithNonNumericId() {
842 'id' => 'lets break it',
843 'activity_name' => 'Meeting',
844 'subject' => 'this case should fail',
845 'scheduled_date_time' => date('Ymd'),
848 $result = $this->callAPIFailure('activity', 'create', $params);
852 * Check with incorrect required fields.
854 public function testActivityUpdateWithIncorrectContactActivityType() {
857 'activity_name' => 'Test Activity',
858 'subject' => 'this case should fail',
859 'scheduled_date_time' => date('Ymd'),
860 'source_contact_id' => $this->_contactID
,
863 $result = $this->callAPIFailure('activity', 'create', $params,
864 'Invalid Activity Id');
868 * Test civicrm_activity_update() to update an existing activity
870 public function testActivityUpdate() {
871 $result = $this->callAPISuccess('activity', 'create', $this->_params
);
874 'id' => $result['id'],
875 'subject' => 'Make-it-Happen Meeting',
876 'activity_date_time' => '20091011123456',
878 'location' => '21, Park Avenue',
879 'details' => 'Lets update Meeting',
881 'source_contact_id' => $this->_contactID
,
885 $result = $this->callAPISuccess('activity', 'create', $params);
886 //hack on date comparison - really we should make getAndCheck smarter to handle dates
887 $params['activity_date_time'] = '2009-10-11 12:34:56';
888 // we also unset source_contact_id since it is stored in an aux table
889 unset($params['source_contact_id']);
890 $this->getAndCheck($params, $result['id'], 'activity');
894 * Test civicrm_activity_update() with valid parameters
895 * and some custom data
897 public function testActivityUpdateCustom() {
898 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__
, __FILE__
);
900 $params = $this->_params
;
902 // Create an activity with custom data
903 //this has been updated from the previous 'old format' function - need to make it work
905 'source_contact_id' => $this->_contactID
,
906 'subject' => 'Make-it-Happen Meeting',
907 'activity_date_time' => '2009-10-18',
909 'location' => 'Pennsylvania',
910 'details' => 'a test activity to check the update api',
912 'activity_name' => 'Test activity type',
913 'version' => $this->_apiversion
,
914 'custom_' . $ids['custom_field_id'] => 'custom string',
916 $result = $this->callAPISuccess('activity', 'create', $params);
918 $activityId = $result['id'];
919 $result = $this->callAPISuccess($this->_entity
, 'get', array(
920 'return.custom_' . $ids['custom_field_id'] => 1,
922 'id' => $result['id'],
924 $this->assertEquals("custom string", $result['values'][$result['id']]['custom_' . $ids['custom_field_id']]);
925 $this->assertEquals("2009-10-18 00:00:00", $result['values'][$result['id']]['activity_date_time']);
926 $fields = $this->callAPISuccess('activity', 'getfields', array('version' => $this->_apiversion
));
927 $this->assertTrue(is_array($fields['values']['custom_' . $ids['custom_field_id']]));
929 // Update the activity with custom data.
932 'source_contact_id' => $this->_contactID
,
933 'subject' => 'Make-it-Happen Meeting',
935 'activity_name' => 'Test activity type',
936 // add this since dates are messed up
937 'activity_date_time' => date('Ymd'),
938 'custom_' . $ids['custom_field_id'] => 'Updated my test data',
939 'version' => $this->_apiversion
,
941 $result = $this->callAPISuccess('Activity', 'Create', $params);
943 $result = $this->callAPISuccess($this->_entity
, 'get', array(
944 'return.custom_' . $ids['custom_field_id'] => 1,
946 'id' => $result['id'],
948 $this->assertEquals("Updated my test data", $result['values'][$result['id']]['custom_' . $ids['custom_field_id']]);
952 * Test civicrm_activity_update() for core activity fields
953 * and some custom data
955 public function testActivityUpdateCheckCoreFields() {
956 $params = $this->_params
;
957 $contact1Params = array(
958 'first_name' => 'John',
959 'middle_name' => 'J.',
960 'last_name' => 'Anderson',
963 'email' => 'john_anderson@civicrm.org',
964 'contact_type' => 'Individual',
967 $contact1 = $this->individualCreate($contact1Params);
968 $contact2Params = array(
969 'first_name' => 'Michal',
970 'middle_name' => 'J.',
971 'last_name' => 'Anderson',
974 'email' => 'michal_anderson@civicrm.org',
975 'contact_type' => 'Individual',
978 $contact2 = $this->individualCreate($contact2Params);
980 $params['assignee_contact_id'] = array($contact1, $contact2);
981 $params['target_contact_id'] = array($contact2 => $contact2);
982 $result = $this->callAPISuccess('Activity', 'Create', $params);
984 $activityId = $result['id'];
986 'return.assignee_contact_id' => 1,
987 'return.target_contact_id' => 1,
988 'version' => $this->_apiversion
,
991 $result = $this->callAPISuccess($this->_entity
, 'get', $getParams);
992 $assignee = $result['values'][$result['id']]['assignee_contact_id'];
993 $target = $result['values'][$result['id']]['target_contact_id'];
994 $this->assertEquals(2, count($assignee), ' in line ' . __LINE__
);
995 $this->assertEquals(1, count($target), ' in line ' . __LINE__
);
996 $this->assertEquals(TRUE, in_array($contact1, $assignee), ' in line ' . __LINE__
);
997 $this->assertEquals(TRUE, in_array($contact2, $target), ' in line ' . __LINE__
);
999 $contact3Params = array(
1000 'first_name' => 'Jijo',
1001 'middle_name' => 'J.',
1002 'last_name' => 'Anderson',
1005 'email' => 'jijo_anderson@civicrm.org',
1006 'contact_type' => 'Individual',
1009 $contact4Params = array(
1010 'first_name' => 'Grant',
1011 'middle_name' => 'J.',
1012 'last_name' => 'Anderson',
1015 'email' => 'grant_anderson@civicrm.org',
1016 'contact_type' => 'Individual',
1019 $contact3 = $this->individualCreate($contact3Params);
1020 $contact4 = $this->individualCreate($contact4Params);
1023 $params['id'] = $activityId;
1024 $params['assignee_contact_id'] = array($contact3 => $contact3);
1025 $params['target_contact_id'] = array($contact4 => $contact4);
1027 $result = $this->callAPISuccess('activity', 'create', $params);
1029 $this->assertEquals($activityId, $result['id'], ' in line ' . __LINE__
);
1031 $result = $this->callAPISuccess(
1035 'return.assignee_contact_id' => 1,
1036 'return.target_contact_id' => 1,
1037 'return.source_contact_id' => 1,
1038 'id' => $result['id'],
1042 $assignee = $result['values'][$result['id']]['assignee_contact_id'];
1043 $target = $result['values'][$result['id']]['target_contact_id'];
1045 $this->assertEquals(1, count($assignee), ' in line ' . __LINE__
);
1046 $this->assertEquals(1, count($target), ' in line ' . __LINE__
);
1047 $this->assertEquals(TRUE, in_array($contact3, $assignee), ' in line ' . __LINE__
);
1048 $this->assertEquals(TRUE, in_array($contact4, $target), ' in line ' . __LINE__
);
1050 foreach ($this->_params
as $fld => $val) {
1051 $this->assertEquals($val, $result['values'][$result['id']][$fld]);
1056 * Test civicrm_activity_update() where the DB has a date_time
1057 * value and there is none in the update params.
1059 public function testActivityUpdateNotDate() {
1060 $result = $this->callAPISuccess('activity', 'create', $this->_params
);
1063 'id' => $result['id'],
1064 'subject' => 'Make-it-Happen Meeting',
1066 'location' => '21, Park Avenue',
1067 'details' => 'Lets update Meeting',
1069 'source_contact_id' => $this->_contactID
,
1073 $result = $this->callAPISuccess('activity', 'create', $params);
1074 //hack on date comparison - really we should make getAndCheck smarter to handle dates
1075 $params['activity_date_time'] = $this->_params
['activity_date_time'];
1076 // we also unset source_contact_id since it is stored in an aux table
1077 unset($params['source_contact_id']);
1078 $this->getAndCheck($params, $result['id'], 'activity');
1082 * Check activity update with status.
1084 public function testActivityUpdateWithStatus() {
1085 $activity = $this->callAPISuccess('activity', 'create', $this->_params
);
1087 'id' => $activity['id'],
1088 'source_contact_id' => $this->_contactID
,
1089 'subject' => 'Hurry update works',
1091 'activity_name' => 'Test activity type',
1094 $result = $this->callAPISuccess('activity', 'create', $params);
1095 $this->assertEquals($result['id'], $activity['id']);
1096 $this->assertEquals($result['values'][$activity['id']]['subject'], 'Hurry update works');
1097 $this->assertEquals($result['values'][$activity['id']]['status_id'], 1
1102 * Test civicrm_activity_update() where the source_contact_id
1103 * is not in the update params.
1105 public function testActivityUpdateKeepSource() {
1106 $activity = $this->callAPISuccess('activity', 'create', $this->_params
);
1107 // Updating the activity but not providing anything for the source contact
1108 // (It was set as $this->_contactID earlier.)
1110 'id' => $activity['id'],
1111 'subject' => 'Updated Make-it-Happen Meeting',
1113 'location' => '21, Park Avenue',
1114 'details' => 'Lets update Meeting',
1116 'activity_name' => 'Test activity type',
1120 $result = $this->callAPISuccess('activity', 'create', $params);
1121 $findactivity = $this->callAPISuccess('Activity', 'Get', array('id' => $activity['id']));
1125 * Test civicrm_activities_contact_get()
1127 public function testActivitiesContactGet() {
1128 $activity = $this->callAPISuccess('activity', 'create', $this->_params
);
1129 $activity2 = $this->callAPISuccess('activity', 'create', $this->_params2
);
1130 // Get activities associated with contact $this->_contactID.
1132 'contact_id' => $this->_contactID
,
1134 $result = $this->callAPISuccess('activity', 'get', $params);
1136 $this->assertEquals(2, $result['count']);
1137 $this->assertEquals($this->test_activity_type_value
, $result['values'][$activity['id']]['activity_type_id']);
1138 $this->assertEquals('Test activity type', $result['values'][$activity['id']]['activity_name']);
1139 $this->assertEquals('Test activity type', $result['values'][$activity2['id']]['activity_name']);
1143 * Test chained Activity format.
1145 public function testChainedActivityGet() {
1147 $activity = $this->callAPISuccess('Contact', 'Create', array(
1148 'display_name' => "bob brown",
1149 'contact_type' => 'Individual',
1150 'api.activity_type.create' => array(
1152 'label' => 'send out letters',
1158 'api.activity.create' => array(
1159 'subject' => 'send letter',
1160 'activity_type_id' => '$value.api.activity_type.create.values.0.value',
1164 $result = $this->callAPISuccess('Activity', 'Get', array(
1165 'id' => $activity['id'],
1166 'return.assignee_contact_id' => 1,
1167 'api.contact.get' => array('api.pledge.get' => 1),
1172 * Test civicrm_activity_contact_get() with invalid Contact ID.
1174 public function testActivitiesContactGetWithInvalidContactId() {
1175 $params = array('contact_id' => 'contact');
1176 $this->callAPIFailure('activity', 'get', $params);
1180 * Test civicrm_activity_contact_get() with contact having no Activity.
1182 public function testActivitiesContactGetHavingNoActivity() {
1184 'first_name' => 'dan',
1185 'last_name' => 'conberg',
1186 'email' => 'dan.conberg@w.co.in',
1187 'contact_type' => 'Individual',
1190 $contact = $this->callAPISuccess('contact', 'create', $params);
1192 'contact_id' => $contact['id'],
1194 $result = $this->callAPISuccess('activity', 'get', $params);
1195 $this->assertEquals($result['count'], 0);
1199 * Test getfields function.
1201 public function testGetFields() {
1202 $params = array('action' => 'create');
1203 $result = $this->callAPIAndDocument('activity', 'getfields', $params, __FUNCTION__
, __FILE__
, NULL, NULL);
1204 $this->assertTrue(is_array($result['values']), 'get fields doesn\'t return values array');
1205 foreach ($result['values'] as $key => $value) {
1206 $this->assertTrue(is_array($value), $key . " is not an array");