3 * File for the TestActivity class
7 * @author Walt Haas <walt@dharmatech.org> (801) 534-1262
8 * @copyright Copyright CiviCRM LLC (C) 2009
9 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html
10 * GNU Affero General Public License version 3
11 * @version $Id: ActivityTest.php 31254 2010-12-15 10:09:29Z eileen $
14 * This file is part of CiviCRM
16 * CiviCRM is free software; you can redistribute it and/or
17 * modify it under the terms of the GNU Affero General Public License
18 * as published by the Free Software Foundation; either version 3 of
19 * the License, or (at your option) any later version.
21 * CiviCRM is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU Affero General Public License for more details.
26 * You should have received a copy of the GNU Affero General Public
27 * License along with this program. If not, see
28 * <http://www.gnu.org/licenses/>.
32 * Include class definitions
34 require_once 'CiviTest/CiviUnitTestCase.php';
38 * Test APIv3 civicrm_activity_* functions
40 * @package CiviCRM_APIv3
41 * @subpackage API_Activity
43 class api_v3_ActivityTest
extends CiviUnitTestCase
{
46 protected $_entity = 'activity';
47 protected $_apiversion = 3;
48 protected $test_activity_type_value;
49 protected $_contactID;
52 * Test setup for every test
54 * Connect to the database, truncate the tables that will be used
55 * and redirect stdin to a temporary file
57 public function setUp() {
58 // Connect to the database
61 $this->_contactID
= $this->individualCreate();
62 //create activity types
63 $activityTypes = $this->callAPISuccess('option_value', 'create', array(
64 'option_group_id' => 2,
65 'name' => 'Test activity type',
66 'label' => 'Test activity type',
69 $this->test_activity_type_value
= $activityTypes['values'][0]['value'];
70 $this->test_activity_type_id
= $activityTypes['id'];
71 $this->_params
= array(
72 'source_contact_id' => $this->_contactID
,
73 'activity_type_id' => $this->test_activity_type_value
,
74 'subject' => 'test activity type id',
75 'activity_date_time' => '2011-06-02 14:36:13',
79 'location' => 'Pensulvania',
80 'details' => 'a test activity',
82 $this->_params2
= array(
83 'source_contact_id' => $this->_contactID
,
84 'subject' => 'Eat & drink',
85 'activity_date_time' => date('Ymd'),
87 'location' => 'Napier',
88 'details' => 'discuss & eat',
90 'activity_type_id' => $this->test_activity_type_value
,
92 // create a logged in USER since the code references it for source_contact_id
93 $this->createLoggedInUser();
97 * Tears down the fixture, for example, closes a network connection.
98 * This method is called after a test is executed.
100 public function tearDown() {
101 $tablesToTruncate = array(
104 'civicrm_activity_contact',
106 $this->quickCleanup($tablesToTruncate, TRUE);
107 $this->callAPISuccess('option_value', 'delete', array('id' => $this->test_activity_type_id
));
111 * Check with empty array
113 public function testActivityCreateEmpty() {
114 $result = $this->callAPIFailure('activity', 'create', array());
118 * Check if required fields are not passed
120 public function testActivityCreateWithoutRequired() {
122 'subject' => 'this case should fail',
123 'scheduled_date_time' => date('Ymd'),
125 $result = $this->callAPIFailure('activity', 'create', $params);
129 * Test civicrm_activity_create() with mismatched activity_type_id
132 public function testActivityCreateMismatchNameType() {
134 'source_contact_id' => $this->_contactID
,
135 'subject' => 'Test activity',
136 'activity_date_time' => date('Ymd'),
138 'location' => 'Pensulvania',
139 'details' => 'a test activity',
141 'activity_name' => 'Fubar activity type',
142 'activity_type_id' => 5,
143 'scheduled_date_time' => date('Ymd'),
146 $result = $this->callAPIFailure('activity', 'create', $params);
150 * Test civicrm_activity_id() with missing source_contact_id is put with the current user.
152 public function testActivityCreateWithMissingContactId() {
154 'subject' => 'Make-it-Happen Meeting',
155 'activity_date_time' => date('Ymd'),
157 'location' => 'Pensulvania',
158 'details' => 'a test activity',
160 'activity_name' => 'Test activity type',
163 $result = $this->callAPISuccess('activity', 'create', $params);
167 * Test civicrm_activity_id() with non-numeric source_contact_id
169 public function testActivityCreateWithNonNumericContactId() {
171 'source_contact_id' => 'fubar',
172 'subject' => 'Make-it-Happen Meeting',
173 'activity_date_time' => date('Ymd'),
175 'location' => 'Pensulvania',
176 'details' => 'a test activity',
178 'activity_name' => 'Test activity type',
181 $result = $this->callAPIFailure('activity', 'create', $params);
185 * Ensure that an invalid activity type causes failure
186 * oddly enough this test was failing because the creation of the invalid type
187 * got added to the set up routine. Probably a mis-fix on a test
189 public function testActivityCreateWithNonNumericActivityTypeId() {
191 'source_contact_id' => $this->_contactID
,
192 'subject' => 'Make-it-Happen Meeting',
193 'activity_date_time' => date('Ymd'),
195 'location' => 'Pensulvania',
196 'details' => 'a test activity',
198 'activity_type_id' => 'Invalid Test activity type',
201 $result = $this->callAPIFailure('activity', 'create', $params);
205 * Check with incorrect required fields
207 public function testActivityCreateWithUnknownActivityTypeId() {
209 'source_contact_id' => $this->_contactID
,
210 'subject' => 'Make-it-Happen Meeting',
211 'activity_date_time' => date('Ymd'),
213 'location' => 'Pensulvania',
214 'details' => 'a test activity',
216 'activity_type_id' => 699,
219 $result = $this->callAPIFailure('activity', 'create', $params);
222 public function testActivityCreateWithInvalidPriority() {
224 'source_contact_id' => $this->_contactID
,
225 'subject' => 'Make-it-Happen Meeting',
226 'activity_date_time' => date('Ymd'),
228 'location' => 'Pensulvania',
229 'details' => 'a test activity',
232 'activity_type_id' => 1,
235 $result = $this->callAPIFailure('activity', 'create', $params,
236 "'44' is not a valid option for field priority_id");
237 $this->assertEquals('priority_id', $result['error_field']);
241 public function testActivityCreateWithValidStringPriority() {
243 'source_contact_id' => $this->_contactID
,
244 'subject' => 'Make-it-Happen Meeting',
245 'activity_date_time' => date('Ymd'),
247 'location' => 'Pensulvania',
248 'details' => 'a test activity',
250 'priority_id' => 'Urgent',
251 'activity_type_id' => 1,
254 $result = $this->callAPISuccess('activity', 'create', $params);
255 $this->assertEquals(1, $result['values'][$result['id']]['priority_id']);
258 public function testActivityCreateWithInValidStringPriority() {
260 'source_contact_id' => $this->_contactID
,
261 'subject' => 'Make-it-Happen Meeting',
262 'activity_date_time' => date('Ymd'),
264 'location' => 'Pensulvania',
265 'details' => 'a test activity',
267 'priority_id' => 'ergUrgent',
268 'activity_type_id' => 1,
271 $result = $this->callAPIFailure('activity', 'create', $params,
272 "'ergUrgent' is not a valid option for field priority_id");
276 * Test civicrm_activity_create() with valid parameters
278 public function testActivityCreate() {
280 $result = $this->callAPISuccess('activity', 'create', $this->_params
);
281 $result = $this->callAPISuccess('activity', 'get', $this->_params
);
282 $this->assertEquals($result['values'][$result['id']]['duration'], 120, 'in line ' . __LINE__
);
283 $this->assertEquals($result['values'][$result['id']]['subject'], 'test activity type id', 'in line ' . __LINE__
);
284 $this->assertEquals($result['values'][$result['id']]['activity_date_time'], '2011-06-02 14:36:13', 'in line ' . __LINE__
);
285 $this->assertEquals($result['values'][$result['id']]['location'], 'Pensulvania', 'in line ' . __LINE__
);
286 $this->assertEquals($result['values'][$result['id']]['details'], 'a test activity', 'in line ' . __LINE__
);
287 $this->assertEquals($result['values'][$result['id']]['status_id'], 2, 'in line ' . __LINE__
);
288 $this->assertEquals($result['values'][$result['id']]['id'], $result['id'], 'in line ' . __LINE__
);
292 * Test civicrm_activity_create() with valid parameters - use type_id
294 public function testActivityCreateCampaignTypeID() {
295 $this->enableCiviCampaign();
300 'source_contact_id' => $this->_contactID
,
301 'subject' => 'Make-it-Happen Meeting',
302 'activity_date_time' => '20110316',
304 'location' => 'Pensulvania',
305 'details' => 'a test activity',
307 'activity_type_id' => 29,
311 $result = $this->callAPISuccess('activity', 'create', $params);
312 //todo test target & assignee are set
314 //$this->assertEquals($result['values'][$result['id']]['source_contact_id'], $this->_contactID, 'in line ' . __LINE__);
315 $result = $this->callAPISuccess('activity', 'get', array('id' => $result['id']));
316 $this->assertEquals($result['values'][$result['id']]['duration'], 120, 'in line ' . __LINE__
);
317 $this->assertEquals($result['values'][$result['id']]['subject'], 'Make-it-Happen Meeting', 'in line ' . __LINE__
);
318 $this->assertEquals($result['values'][$result['id']]['activity_date_time'], '2011-03-16 00:00:00', 'in line ' . __LINE__
);
319 $this->assertEquals($result['values'][$result['id']]['location'], 'Pensulvania', 'in line ' . __LINE__
);
320 $this->assertEquals($result['values'][$result['id']]['details'], 'a test activity', 'in line ' . __LINE__
);
321 $this->assertEquals($result['values'][$result['id']]['status_id'], 1, 'in line ' . __LINE__
);
324 public function testActivityReturnTargetAssignee() {
326 $description = "Example demonstrates setting & retrieving the target & source";
327 $subfile = "GetTargetandAssignee";
329 'source_contact_id' => $this->_contactID
,
330 'subject' => 'Make-it-Happen Meeting',
331 'activity_date_time' => '20110316',
333 'location' => 'Pensulvania',
334 'details' => 'a test activity',
336 'activity_type_id' => 1,
338 'target_contact_id' => $this->_contactID
,
339 'assignee_contact_id' => $this->_contactID
,
342 $result = $this->callAPIAndDocument('activity', 'create', $params, __FUNCTION__
, __FILE__
, $description, $subfile);
343 $result = $this->callAPISuccess('activity', 'get', array(
344 'id' => $result['id'],
345 'version' => $this->_apiversion
,
346 'return.assignee_contact_id' => 1,
347 'return.target_contact_id' => 1,
350 $this->assertEquals($this->_contactID
, $result['values'][$result['id']]['assignee_contact_id'][0], 'in line ' . __LINE__
);
351 $this->assertEquals($this->_contactID
, $result['values'][$result['id']]['target_contact_id'][0], 'in line ' . __LINE__
);
354 public function testActivityCreateExample() {
356 * Test civicrm_activity_create() using example code
358 require_once 'api/v3/examples/Activity/Create.php';
359 $result = activity_create_example();
360 $expectedResult = activity_create_expectedresult();
361 $this->assertEquals($result, $expectedResult);
365 * Test civicrm_activity_create() with valid parameters
366 * and some custom data
368 public function testActivityCreateCustom() {
369 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__
, __FILE__
);
370 $params = $this->_params
;
371 $params['custom_' . $ids['custom_field_id']] = "custom string";
372 $result = $this->callAPIAndDocument($this->_entity
, 'create', $params, __FUNCTION__
, __FILE__
);
373 $result = $this->callAPISuccess($this->_entity
, 'get', array(
374 'return.custom_' . $ids['custom_field_id'] => 1,
376 'id' => $result['id'],
378 $this->assertEquals("custom string", $result['values'][$result['id']]['custom_' . $ids['custom_field_id']], ' in line ' . __LINE__
);
380 $this->customFieldDelete($ids['custom_field_id']);
381 $this->customGroupDelete($ids['custom_group_id']);
385 * Test civicrm_activity_create() with valid parameters
386 * and some custom data
388 public function testActivityCreateCustomContactRefField() {
390 $this->callAPISuccess('contact', 'create', array('id' => $this->_contactID
, 'sort_name' => 'Contact, Test'));
391 $subfile = 'ContactRefCustomField';
392 $description = "demonstrates create with Contact Reference Custom Field";
393 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__
, __FILE__
);
395 'custom_group_id' => $ids['custom_group_id'],
396 'name' => 'Worker_Lookup',
397 'label' => 'Worker Lookup',
398 'html_type' => 'Autocomplete-Select',
399 'data_type' => 'ContactReference',
401 'is_searchable' => 1,
405 $customField = $this->callAPISuccess('custom_field', 'create', $params);
406 $params = $this->_params
;
407 $params['custom_' . $customField['id']] = "$this->_contactID";
409 $result = $this->callAPIAndDocument($this->_entity
, 'create', $params, __FUNCTION__
, __FILE__
, $description, $subfile, 'Create');
410 $result = $this->callAPIAndDocument($this->_entity
, 'get', array(
411 'return.custom_' . $customField['id'] => 1,
412 'id' => $result['id'],
413 ), __FUNCTION__
, __FILE__
, 'Get with Contact Ref Custom Field', 'ContactRefCustomFieldGet', 'get');
415 $this->assertEquals('Anderson, Anthony', $result['values'][$result['id']]['custom_' . $customField['id']], ' in line ' . __LINE__
);
416 $this->assertEquals($this->_contactID
, $result['values'][$result['id']]['custom_' . $customField['id'] . "_id"], ' in line ' . __LINE__
);
417 $this->assertEquals('Anderson, Anthony', $result['values'][$result['id']]['custom_' . $customField['id'] . '_1'], ' in line ' . __LINE__
);
418 $this->assertEquals($this->_contactID
, $result['values'][$result['id']]['custom_' . $customField['id'] . "_1_id"], ' in line ' . __LINE__
);
419 $this->customFieldDelete($ids['custom_field_id']);
420 $this->customGroupDelete($ids['custom_group_id']);
424 * Test civicrm_activity_create() with an invalid text status_id
426 public function testActivityCreateBadTextStatus() {
429 'source_contact_id' => $this->_contactID
,
430 'subject' => 'Discussion on Apis for v3',
431 'activity_date_time' => date('Ymd'),
433 'location' => 'Pensulvania',
434 'details' => 'a test activity',
435 'status_id' => 'Invalid',
436 'activity_name' => 'Test activity type',
439 $result = $this->callAPIFailure('activity', 'create', $params);
443 * Test civicrm_activity_create() with an invalid text status_id
445 public function testActivityCreateSupportActivityStatus() {
448 'source_contact_id' => $this->_contactID
,
449 'subject' => 'Discussion on Apis for v3',
450 'activity_date_time' => date('Ymd'),
452 'location' => 'Pensulvania',
453 'details' => 'a test activity',
454 'activity_status_id' => 'Invalid',
455 'activity_name' => 'Test activity type',
458 $result = $this->callAPIFailure('activity', 'create', $params,
459 "'Invalid' is not a valid option for field status_id");
464 * Test civicrm_activity_create() with valid parameters,
465 * using a text status_id
467 public function testActivityCreateTextStatus() {
470 'source_contact_id' => $this->_contactID
,
471 'subject' => 'Make-it-Happen Meeting',
472 'activity_date_time' => date('Ymd'),
474 'location' => 'Pensulvania',
475 'details' => 'a test activity',
476 'status_id' => 'Scheduled',
477 'activity_name' => 'Test activity type',
480 $result = $this->callAPISuccess('activity', 'create', $params);
481 $this->assertEquals($result['values'][$result['id']]['duration'], 120, 'in line ' . __LINE__
);
482 $this->assertEquals($result['values'][$result['id']]['subject'], 'Make-it-Happen Meeting', 'in line ' . __LINE__
);
483 $this->assertEquals($result['values'][$result['id']]['activity_date_time'], date('Ymd') . '000000', 'in line ' . __LINE__
);
484 $this->assertEquals($result['values'][$result['id']]['location'], 'Pensulvania', 'in line ' . __LINE__
);
485 $this->assertEquals($result['values'][$result['id']]['details'], 'a test activity', 'in line ' . __LINE__
);
489 * Test civicrm_activity_get() with no params
491 public function testActivityGetEmpty() {
492 $result = $this->callAPISuccess('activity', 'get', array());
496 * Test civicrm_activity_get() with a good activity ID
498 public function testActivityGetGoodID1() {
499 // Insert rows in civicrm_activity creating activities 4 and
501 $description = "Function demonstrates getting asignee_contact_id & using it to get the contact";
502 $subfile = 'ReturnAssigneeContact';
503 $activity = $this->callAPISuccess('activity', 'create', $this->_params
);
505 $contact = $this->callAPISuccess('Contact', 'Create', array(
506 'first_name' => "The Rock",
507 'last_name' => 'roccky',
508 'contact_type' => 'Individual',
510 'api.activity.create' => array(
511 'id' => $activity['id'],
512 'assignee_contact_id' => '$value.id',
517 'activity_id' => $activity['id'],
518 'version' => $this->_apiversion
,
520 'return.assignee_contact_id' => 1,
521 'api.contact.get' => array(
522 'id' => '$value.source_contact_id',
526 $result = $this->callAPIAndDocument('Activity', 'Get', $params, __FUNCTION__
, __FILE__
, $description, $subfile);
528 $this->assertEquals($activity['id'], $result['id'], 'In line ' . __LINE__
);
530 $this->assertEquals($contact['id'], $result['values'][0]['assignee_contact_id'][0], 'In line ' . __LINE__
);
532 $this->assertEquals($this->_contactID
, $result['values'][0]['api.contact.get']['values'][0]['contact_id'], 'In line ' . __LINE__
);
533 $this->assertEquals($this->test_activity_type_value
, $result['values'][0]['activity_type_id'], 'In line ' . __LINE__
);
534 $this->assertEquals("test activity type id", $result['values'][0]['subject'], 'In line ' . __LINE__
);
538 * test that get functioning does filtering
540 public function testGetFilter() {
542 'source_contact_id' => $this->_contactID
,
543 'subject' => 'Make-it-Happen Meeting',
544 'activity_date_time' => '20110316',
546 'location' => 'Pensulvania',
547 'details' => 'a test activity',
549 'activity_name' => 'Test activity type',
552 $result = $this->callAPISuccess('Activity', 'Create', $params);
553 $this->callAPISuccess('Activity', 'Get', array('subject' => 'Make-it-Happen Meeting'));
554 $this->assertEquals(1, $result['count']);
555 $this->assertEquals('Make-it-Happen Meeting', $result['values'][$result['id']]['subject']);
556 $this->callAPISuccess('Activity', 'Delete', array('id' => $result['id']));
561 * Test civicrm_activity_get() with filter target_contact_id
563 public function testActivityGetTargetFilter() {
564 $params = $this->_params
;
565 $contact1Params = array(
566 'first_name' => 'John',
567 'middle_name' => 'J.',
568 'last_name' => 'Anderson',
571 'email' => 'john_anderson@civicrm.org',
572 'contact_type' => 'Individual',
575 $contact1 = $this->individualCreate($contact1Params);
576 $contact2Params = array(
577 'first_name' => 'Michal',
578 'middle_name' => 'J.',
579 'last_name' => 'Anderson',
582 'email' => 'michal_anderson@civicrm.org',
583 'contact_type' => 'Individual',
586 $contact2 = $this->individualCreate($contact2Params);
588 $params['assignee_contact_id'] = array($contact1, $contact2);
589 $params['target_contact_id'] = array($contact2 => $contact2);
590 $activity = $this->callAPISuccess('Activity', 'Create', $params);
592 $activityget = $this->callAPISuccess('Activity', 'get', array(
593 'id' => $activity['id'],
594 'target_contact_id' => $contact2,
595 'return.target_contact_id' => 1,
597 $this->assertEquals($activity['id'], $activityget['id'], 'In line ' . __LINE__
);
598 $this->assertEquals($contact2, $activityget['values'][$activityget['id']]['target_contact_id'][0], 'In line ' . __LINE__
);
600 $activityget = $this->callAPISuccess('activity', 'get', array(
601 'target_contact_id' => $this->_contactID
,
602 'return.target_contact_id' => 1,
603 'id' => $activity['id'],
605 if ($activityget['count'] > 0) {
606 $this->assertNotEquals($contact2, $activityget['values'][$activityget['id']]['target_contact_id'][0], 'In line ' . __LINE__
);
611 * test that get functioning does filtering
613 public function testGetStatusID() {
615 'source_contact_id' => $this->_contactID
,
616 'subject' => 'Make-it-Happen Meeting',
617 'activity_date_time' => '20110316',
619 'location' => 'Pensulvania',
620 'details' => 'a test activity',
622 'activity_name' => 'Test activity type',
625 $this->callAPISuccess('Activity', 'Create', $params);
626 $result = $this->callAPISuccess('Activity', 'Get', array('activity_status_id' => '1'));
627 $this->assertEquals(1, $result['count'], 'one activity of status 1 should exist');
629 $result = $this->callAPISuccess('Activity', 'Get', array('status_id' => '1'));
630 $this->assertEquals(1, $result['count'], 'status_id should also work');
632 $result = $this->callAPISuccess('Activity', 'Get', array('activity_status_id' => '2'));
633 $this->assertEquals(0, $result['count'], 'No activities of status 1 should exist');
634 $result = $this->callAPISuccess('Activity', 'Get', array(
635 'version' => $this->_apiversion
,
638 $this->assertEquals(0, $result['count'], 'No activities of status 1 should exist');
643 * test that get functioning does filtering
645 public function testGetFilterMaxDate() {
647 'source_contact_id' => $this->_contactID
,
648 'subject' => 'Make-it-Happen Meeting',
649 'activity_date_time' => '20110101',
651 'location' => 'Pensulvania',
652 'details' => 'a test activity',
654 'activity_name' => 'Test activity type',
655 'version' => $this->_apiversion
,
658 $activityOne = $this->callAPISuccess('Activity', 'Create', $params);
659 $params['activity_date_time'] = 20120216;
660 $activityTwo = $this->callAPISuccess('Activity', 'Create', $params);
661 $result = $this->callAPISuccess('Activity', 'Get', array(
664 $description = "demonstrates _low filter (at time of writing doesn't work if contact_id is set";
665 $subfile = "DateTimeLow";
666 $this->assertEquals(2, $result['count']);
669 'filter.activity_date_time_low' => '20120101000000',
672 $result = $this->callAPIAndDocument('Activity', 'Get', $params, __FUNCTION__
, __FILE__
, $description, $subfile);
673 $this->assertEquals(1, $result['count'], 'in line ' . __LINE__
);
674 $description = "demonstrates _high filter (at time of writing doesn't work if contact_id is set";
675 $subfile = "DateTimeHigh";
676 $this->assertEquals('2012-02-16 00:00:00', $result['values'][0]['activity_date_time'], 'in line ' . __LINE__
);
678 'source_contact_id' => $this->_contactID
,
680 'filter.activity_date_time_high' => '20120101000000',
683 $result = $this->callAPIAndDocument('Activity', 'Get', $params, __FUNCTION__
, __FILE__
, $description, $subfile);
685 $this->assertEquals(1, $result['count']);
686 $this->assertEquals('2011-01-01 00:00:00', $result['values'][0]['activity_date_time'], 'in line ' . __LINE__
);
688 $this->callAPISuccess('Activity', 'Delete', array('version' => 3, 'id' => $activityOne['id']));
689 $this->callAPISuccess('Activity', 'Delete', array('version' => 3, 'id' => $activityTwo['id']));
693 * Test civicrm_activity_get() with a good activity ID which
694 * has associated custom data
696 public function testActivityGetGoodIDCustom() {
697 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__
, __FILE__
);
699 $params = $this->_params
;
700 $params['custom_' . $ids['custom_field_id']] = "custom string";
702 $result = $this->callAPISuccess($this->_entity
, 'create', $params);
704 // Retrieve the test value
706 'activity_type_id' => $this->test_activity_type_value
,
708 'return.custom_' . $ids['custom_field_id'] => 1,
710 $result = $this->callAPIAndDocument('activity', 'get', $params, __FUNCTION__
, __FILE__
);
711 $this->assertEquals("custom string", $result['values'][0]['custom_' . $ids['custom_field_id']]);
713 $this->assertEquals($this->test_activity_type_value
, $result['values'][0]['activity_type_id']);
714 $this->assertEquals('test activity type id', $result['values'][0]['subject'], 'In line ' . __LINE__
);
715 $this->customFieldDelete($ids['custom_field_id']);
716 $this->customGroupDelete($ids['custom_group_id']);
720 * Test civicrm_activity_get() with a good activity ID which
721 * has associated custom data
723 public function testActivityGetContact_idCustom() {
724 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__
, __FILE__
);
726 $params = $this->_params
;
727 $params['custom_' . $ids['custom_field_id']] = "custom string";
729 $result = $this->callAPISuccess($this->_entity
, 'create', $params);
730 // Retrieve the test value
732 'contact_id' => $this->_params
['source_contact_id'],
733 'activity_type_id' => $this->test_activity_type_value
,
735 'return.custom_' . $ids['custom_field_id'] => 1,
737 $result = $this->callAPIAndDocument('activity', 'get', $params, __FUNCTION__
, __FILE__
);
738 $this->assertEquals("custom string", $result['values'][0]['custom_' . $ids['custom_field_id']], ' in line ' . __LINE__
);
740 $this->assertEquals($this->test_activity_type_value
, $result['values'][0]['activity_type_id'], 'In line ' . __LINE__
);
741 $this->assertEquals('test activity type id', $result['values'][0]['subject'], 'In line ' . __LINE__
);
742 $this->assertEquals($result['values'][0]['id'], $result['id'], 'in line ' . __LINE__
);
746 * Check activity deletion with empty params
748 public function testDeleteActivityForEmptyParams() {
749 $params = array('version' => $this->_apiversion
);
750 $result = $this->callAPIFailure('activity', 'delete', $params);
754 * Check activity deletion without activity id
756 public function testDeleteActivityWithoutId() {
758 'activity_name' => 'Meeting',
759 'version' => $this->_apiversion
,
761 $result = $this->callAPIFailure('activity', 'delete', $params);
765 * Check activity deletion without activity type
767 public function testDeleteActivityWithoutActivityType() {
768 $params = array('id' => 1);
769 $result = $this->callAPIFailure('activity', 'delete', $params);
773 * Check activity deletion with incorrect data
775 public function testDeleteActivityWithIncorrectActivityType() {
778 'activity_name' => 'Test Activity',
781 $result = $this->callAPIFailure('activity', 'delete', $params);
785 * Check activity deletion with correct data
787 public function testDeleteActivity() {
788 $result = $this->callAPISuccess('activity', 'create', $this->_params
);
790 'id' => $result['id'],
791 'version' => $this->_apiversion
,
794 $result = $this->callAPISuccess('activity', 'delete', $params);
795 $this->documentMe($params, $result, __FUNCTION__
, __FILE__
);
799 * Check if required fields are not passed
801 public function testActivityUpdateWithoutRequired() {
803 'subject' => 'this case should fail',
804 'scheduled_date_time' => date('Ymd'),
807 $result = $this->callAPIFailure('activity', 'create', $params);
811 * Test civicrm_activity_update() with non-numeric id
813 public function testActivityUpdateWithNonNumericId() {
815 'id' => 'lets break it',
816 'activity_name' => 'Meeting',
817 'subject' => 'this case should fail',
818 'scheduled_date_time' => date('Ymd'),
821 $result = $this->callAPIFailure('activity', 'create', $params);
825 * Check with incorrect required fields
827 public function testActivityUpdateWithIncorrectContactActivityType() {
830 'activity_name' => 'Test Activity',
831 'subject' => 'this case should fail',
832 'scheduled_date_time' => date('Ymd'),
833 'source_contact_id' => $this->_contactID
,
836 $result = $this->callAPIFailure('activity', 'create', $params,
837 'Invalid Activity Id');
841 * Test civicrm_activity_update() to update an existing activity
843 public function testActivityUpdate() {
844 $result = $this->callAPISuccess('activity', 'create', $this->_params
);
847 'id' => $result['id'],
848 'subject' => 'Make-it-Happen Meeting',
849 'activity_date_time' => '20091011123456',
851 'location' => '21, Park Avenue',
852 'details' => 'Lets update Meeting',
854 'source_contact_id' => $this->_contactID
,
858 $result = $this->callAPISuccess('activity', 'create', $params);
859 //hack on date comparison - really we should make getAndCheck smarter to handle dates
860 $params['activity_date_time'] = '2009-10-11 12:34:56';
861 // we also unset source_contact_id since it is stored in an aux table
862 unset($params['source_contact_id']);
863 $this->getAndCheck($params, $result['id'], 'activity');
867 * Test civicrm_activity_update() with valid parameters
868 * and some custom data
870 public function testActivityUpdateCustom() {
871 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__
, __FILE__
);
873 $params = $this->_params
;
875 // Create an activity with custom data
876 //this has been updated from the previous 'old format' function - need to make it work
878 'source_contact_id' => $this->_contactID
,
879 'subject' => 'Make-it-Happen Meeting',
880 'activity_date_time' => '2009-10-18',
882 'location' => 'Pensulvania',
883 'details' => 'a test activity to check the update api',
885 'activity_name' => 'Test activity type',
886 'version' => $this->_apiversion
,
887 'custom_' . $ids['custom_field_id'] => 'custom string',
889 $result = $this->callAPISuccess('activity', 'create', $params);
891 $activityId = $result['id'];
892 $result = $this->callAPISuccess($this->_entity
, 'get', array(
893 'return.custom_' . $ids['custom_field_id'] => 1,
895 'id' => $result['id'],
897 $this->assertEquals("custom string", $result['values'][$result['id']]['custom_' . $ids['custom_field_id']], ' in line ' . __LINE__
);
898 $this->assertEquals("2009-10-18 00:00:00", $result['values'][$result['id']]['activity_date_time'], ' in line ' . __LINE__
);
899 $fields = $this->callAPISuccess('activity', 'getfields', array('version' => $this->_apiversion
));
900 $this->assertTrue(is_array($fields['values']['custom_' . $ids['custom_field_id']]));
902 // Update the activity with custom data
905 'source_contact_id' => $this->_contactID
,
906 'subject' => 'Make-it-Happen Meeting',
908 'activity_name' => 'Test activity type',
909 // add this since dates are messed up
910 'activity_date_time' => date('Ymd'),
911 'custom_' . $ids['custom_field_id'] => 'Updated my test data',
912 'version' => $this->_apiversion
,
914 $result = $this->callAPISuccess('Activity', 'Create', $params);
916 $result = $this->callAPISuccess($this->_entity
, 'get', array(
917 'return.custom_' . $ids['custom_field_id'] => 1,
919 'id' => $result['id'],
921 $this->assertEquals("Updated my test data", $result['values'][$result['id']]['custom_' . $ids['custom_field_id']], ' in line ' . __LINE__
);
925 * Test civicrm_activity_update() for core activity fields
926 * and some custom data
928 public function testActivityUpdateCheckCoreFields() {
929 $params = $this->_params
;
930 $contact1Params = array(
931 'first_name' => 'John',
932 'middle_name' => 'J.',
933 'last_name' => 'Anderson',
936 'email' => 'john_anderson@civicrm.org',
937 'contact_type' => 'Individual',
940 $contact1 = $this->individualCreate($contact1Params);
941 $contact2Params = array(
942 'first_name' => 'Michal',
943 'middle_name' => 'J.',
944 'last_name' => 'Anderson',
947 'email' => 'michal_anderson@civicrm.org',
948 'contact_type' => 'Individual',
951 $contact2 = $this->individualCreate($contact2Params);
953 $params['assignee_contact_id'] = array($contact1, $contact2);
954 $params['target_contact_id'] = array($contact2 => $contact2);
955 $result = $this->callAPISuccess('Activity', 'Create', $params);
957 $activityId = $result['id'];
959 'return.assignee_contact_id' => 1,
960 'return.target_contact_id' => 1,
961 'version' => $this->_apiversion
,
964 $result = $this->callAPISuccess($this->_entity
, 'get', $getParams);
965 $assignee = $result['values'][$result['id']]['assignee_contact_id'];
966 $target = $result['values'][$result['id']]['target_contact_id'];
967 $this->assertEquals(2, count($assignee), ' in line ' . __LINE__
);
968 $this->assertEquals(1, count($target), ' in line ' . __LINE__
);
969 $this->assertEquals(TRUE, in_array($contact1, $assignee), ' in line ' . __LINE__
);
970 $this->assertEquals(TRUE, in_array($contact2, $target), ' in line ' . __LINE__
);
972 $contact3Params = array(
973 'first_name' => 'Jijo',
974 'middle_name' => 'J.',
975 'last_name' => 'Anderson',
978 'email' => 'jijo_anderson@civicrm.org',
979 'contact_type' => 'Individual',
982 $contact4Params = array(
983 'first_name' => 'Grant',
984 'middle_name' => 'J.',
985 'last_name' => 'Anderson',
988 'email' => 'grant_anderson@civicrm.org',
989 'contact_type' => 'Individual',
992 $contact3 = $this->individualCreate($contact3Params);
993 $contact4 = $this->individualCreate($contact4Params);
996 $params['id'] = $activityId;
997 $params['assignee_contact_id'] = array($contact3 => $contact3);
998 $params['target_contact_id'] = array($contact4 => $contact4);
1000 $result = $this->callAPISuccess('activity', 'create', $params);
1002 $this->assertEquals($activityId, $result['id'], ' in line ' . __LINE__
);
1004 $result = $this->callAPISuccess(
1008 'return.assignee_contact_id' => 1,
1009 'return.target_contact_id' => 1,
1010 'return.source_contact_id' => 1,
1011 'id' => $result['id'],
1015 $assignee = $result['values'][$result['id']]['assignee_contact_id'];
1016 $target = $result['values'][$result['id']]['target_contact_id'];
1018 $this->assertEquals(1, count($assignee), ' in line ' . __LINE__
);
1019 $this->assertEquals(1, count($target), ' in line ' . __LINE__
);
1020 $this->assertEquals(TRUE, in_array($contact3, $assignee), ' in line ' . __LINE__
);
1021 $this->assertEquals(TRUE, in_array($contact4, $target), ' in line ' . __LINE__
);
1023 foreach ($this->_params
as $fld => $val) {
1024 $this->assertEquals($val, $result['values'][$result['id']][$fld]);
1029 * Test civicrm_activity_update() where the DB has a date_time
1030 * value and there is none in the update params.
1032 public function testActivityUpdateNotDate() {
1033 $result = $this->callAPISuccess('activity', 'create', $this->_params
);
1036 'id' => $result['id'],
1037 'subject' => 'Make-it-Happen Meeting',
1039 'location' => '21, Park Avenue',
1040 'details' => 'Lets update Meeting',
1042 'source_contact_id' => $this->_contactID
,
1046 $result = $this->callAPISuccess('activity', 'create', $params);
1047 //hack on date comparison - really we should make getAndCheck smarter to handle dates
1048 $params['activity_date_time'] = $this->_params
['activity_date_time'];
1049 // we also unset source_contact_id since it is stored in an aux table
1050 unset($params['source_contact_id']);
1051 $this->getAndCheck($params, $result['id'], 'activity');
1055 * Check activity update with status
1057 public function testActivityUpdateWithStatus() {
1058 $activity = $this->callAPISuccess('activity', 'create', $this->_params
);
1060 'id' => $activity['id'],
1061 'source_contact_id' => $this->_contactID
,
1062 'subject' => 'Hurry update works',
1064 'activity_name' => 'Test activity type',
1067 $result = $this->callAPISuccess('activity', 'create', $params);
1068 $this->assertEquals($result['id'], $activity['id']);
1069 $this->assertEquals($result['values'][$activity['id']]['subject'], 'Hurry update works');
1070 $this->assertEquals($result['values'][$activity['id']]['status_id'], 1
1075 * Test civicrm_activity_update() where the source_contact_id
1076 * is not in the update params.
1078 public function testActivityUpdateKeepSource() {
1079 $activity = $this->callAPISuccess('activity', 'create', $this->_params
);
1080 // Updating the activity but not providing anything for the source contact
1081 // (It was set as $this->_contactID earlier.)
1083 'id' => $activity['id'],
1084 'subject' => 'Updated Make-it-Happen Meeting',
1086 'location' => '21, Park Avenue',
1087 'details' => 'Lets update Meeting',
1089 'activity_name' => 'Test activity type',
1093 $result = $this->callAPISuccess('activity', 'create', $params);
1094 $findactivity = $this->callAPISuccess('Activity', 'Get', array('id' => $activity['id']));
1098 * Test civicrm_activities_contact_get()
1100 public function testActivitiesContactGet() {
1101 $activity = $this->callAPISuccess('activity', 'create', $this->_params
);
1102 $activity2 = $this->callAPISuccess('activity', 'create', $this->_params2
);
1103 // Get activities associated with contact $this->_contactID
1105 'contact_id' => $this->_contactID
,
1107 $result = $this->callAPISuccess('activity', 'get', $params);
1109 $this->assertEquals(2, $result['count'], 'In line ' . __LINE__
);
1110 $this->assertEquals($this->test_activity_type_value
, $result['values'][$activity['id']]['activity_type_id'], 'In line ' . __LINE__
);
1111 $this->assertEquals('Test activity type', $result['values'][$activity['id']]['activity_name'], 'In line ' . __LINE__
);
1112 $this->assertEquals('Test activity type', $result['values'][$activity2['id']]['activity_name'], 'In line ' . __LINE__
);
1116 * test chained Activity format
1118 public function testchainedActivityGet() {
1120 $activity = $this->callAPISuccess('Contact', 'Create', array(
1121 'display_name' => "bob brown",
1122 'contact_type' => 'Individual',
1123 'api.activity_type.create' => array(
1125 'label' => 'send out letters',
1131 'api.activity.create' => array(
1132 'subject' => 'send letter',
1133 'activity_type_id' => '$value.api.activity_type.create.values.0.value',
1137 $result = $this->callAPISuccess('Activity', 'Get', array(
1138 'id' => $activity['id'],
1139 'return.assignee_contact_id' => 1,
1140 'api.contact.get' => array('api.pledge.get' => 1),
1145 * Test civicrm_activity_contact_get() with invalid Contact Id
1147 public function testActivitiesContactGetWithInvalidContactId() {
1148 $params = array('contact_id' => 'contact');
1149 $result = $this->callAPIFailure('activity', 'get', $params);
1153 * Test civicrm_activity_contact_get() with contact having no Activity
1155 public function testActivitiesContactGetHavingNoActivity() {
1157 'first_name' => 'dan',
1158 'last_name' => 'conberg',
1159 'email' => 'dan.conberg@w.co.in',
1160 'contact_type' => 'Individual',
1163 $contact = $this->callAPISuccess('contact', 'create', $params);
1165 'contact_id' => $contact['id'],
1167 $result = $this->callAPISuccess('activity', 'get', $params);
1168 $this->assertEquals($result['count'], 0, 'in line ' . __LINE__
);
1171 public function testGetFields() {
1172 $params = array('action' => 'create');
1173 $result = $this->callAPIAndDocument('activity', 'getfields', $params, __FUNCTION__
, __FILE__
, NULL, NULL, 'getfields');
1174 $this->assertTrue(is_array($result['values']), 'get fields doesnt return values array in line ' . __LINE__
);
1175 // $this->assertTrue(is_array($result['values']['priority_id']['options']));
1176 foreach ($result['values'] as $key => $value) {
1177 $this->assertTrue(is_array($value), $key . " is not an array in line " . __LINE__
);