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
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' => 'Pensulvania',
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.
99 * This method is called after a test is executed.
102 public function tearDown() {
103 $tablesToTruncate = array(
106 'civicrm_activity_contact',
108 $this->quickCleanup($tablesToTruncate, TRUE);
109 $this->callAPISuccess('option_value', 'delete', array('id' => $this->test_activity_type_id
));
113 * Check with empty array
115 public function testActivityCreateEmpty() {
116 $result = $this->callAPIFailure('activity', 'create', array());
120 * Check if required fields are not passed
122 public function testActivityCreateWithoutRequired() {
124 'subject' => 'this case should fail',
125 'scheduled_date_time' => date('Ymd'),
127 $result = $this->callAPIFailure('activity', 'create', $params);
131 * Test civicrm_activity_create() with mismatched activity_type_id
134 public function testActivityCreateMismatchNameType() {
136 'source_contact_id' => $this->_contactID
,
137 'subject' => 'Test activity',
138 'activity_date_time' => date('Ymd'),
140 'location' => 'Pensulvania',
141 'details' => 'a test activity',
143 'activity_name' => 'Fubar activity type',
144 'activity_type_id' => 5,
145 'scheduled_date_time' => date('Ymd'),
148 $result = $this->callAPIFailure('activity', 'create', $params);
152 * Test civicrm_activity_id() with missing source_contact_id is put with the current user.
154 public function testActivityCreateWithMissingContactId() {
156 'subject' => 'Make-it-Happen Meeting',
157 'activity_date_time' => date('Ymd'),
159 'location' => 'Pensulvania',
160 'details' => 'a test activity',
162 'activity_name' => 'Test activity type',
165 $result = $this->callAPISuccess('activity', 'create', $params);
169 * Test civicrm_activity_id() with non-numeric source_contact_id
171 public function testActivityCreateWithNonNumericContactId() {
173 'source_contact_id' => 'fubar',
174 'subject' => 'Make-it-Happen Meeting',
175 'activity_date_time' => date('Ymd'),
177 'location' => 'Pensulvania',
178 'details' => 'a test activity',
180 'activity_name' => 'Test activity type',
183 $result = $this->callAPIFailure('activity', 'create', $params);
187 * Ensure that an invalid activity type causes failure
188 * oddly enough this test was failing because the creation of the invalid type
189 * got added to the set up routine. Probably a mis-fix on a test
191 public function testActivityCreateWithNonNumericActivityTypeId() {
193 'source_contact_id' => $this->_contactID
,
194 'subject' => 'Make-it-Happen Meeting',
195 'activity_date_time' => date('Ymd'),
197 'location' => 'Pensulvania',
198 'details' => 'a test activity',
200 'activity_type_id' => 'Invalid Test activity type',
203 $result = $this->callAPIFailure('activity', 'create', $params);
207 * Check with incorrect required fields
209 public function testActivityCreateWithUnknownActivityTypeId() {
211 'source_contact_id' => $this->_contactID
,
212 'subject' => 'Make-it-Happen Meeting',
213 'activity_date_time' => date('Ymd'),
215 'location' => 'Pensulvania',
216 'details' => 'a test activity',
218 'activity_type_id' => 699,
221 $result = $this->callAPIFailure('activity', 'create', $params);
224 public function testActivityCreateWithInvalidPriority() {
226 'source_contact_id' => $this->_contactID
,
227 'subject' => 'Make-it-Happen Meeting',
228 'activity_date_time' => date('Ymd'),
230 'location' => 'Pensulvania',
231 'details' => 'a test activity',
234 'activity_type_id' => 1,
237 $result = $this->callAPIFailure('activity', 'create', $params,
238 "'44' is not a valid option for field priority_id");
239 $this->assertEquals('priority_id', $result['error_field']);
244 public function testActivityCreateWithValidStringPriority() {
246 'source_contact_id' => $this->_contactID
,
247 'subject' => 'Make-it-Happen Meeting',
248 'activity_date_time' => date('Ymd'),
250 'location' => 'Pensulvania',
251 'details' => 'a test activity',
253 'priority_id' => 'Urgent',
254 'activity_type_id' => 1,
257 $result = $this->callAPISuccess('activity', 'create', $params);
258 $this->assertEquals(1, $result['values'][$result['id']]['priority_id']);
261 public function testActivityCreateWithInValidStringPriority() {
263 'source_contact_id' => $this->_contactID
,
264 'subject' => 'Make-it-Happen Meeting',
265 'activity_date_time' => date('Ymd'),
267 'location' => 'Pensulvania',
268 'details' => 'a test activity',
270 'priority_id' => 'ergUrgent',
271 'activity_type_id' => 1,
274 $result = $this->callAPIFailure('activity', 'create', $params,
275 "'ergUrgent' is not a valid option for field priority_id");
279 * Test civicrm_activity_create() with valid parameters
281 public function testActivityCreate() {
283 $result = $this->callAPISuccess('activity', 'create', $this->_params
);
284 $result = $this->callAPISuccess('activity', 'get', $this->_params
);
285 $this->assertEquals($result['values'][$result['id']]['duration'], 120, 'in line ' . __LINE__
);
286 $this->assertEquals($result['values'][$result['id']]['subject'], 'test activity type id', 'in line ' . __LINE__
);
287 $this->assertEquals($result['values'][$result['id']]['activity_date_time'], '2011-06-02 14:36:13', 'in line ' . __LINE__
);
288 $this->assertEquals($result['values'][$result['id']]['location'], 'Pensulvania', 'in line ' . __LINE__
);
289 $this->assertEquals($result['values'][$result['id']]['details'], 'a test activity', 'in line ' . __LINE__
);
290 $this->assertEquals($result['values'][$result['id']]['status_id'], 2, 'in line ' . __LINE__
);
291 $this->assertEquals($result['values'][$result['id']]['id'], $result['id'], 'in line ' . __LINE__
);
295 * Test civicrm_activity_create() with valid parameters - use type_id
297 public function testActivityCreateCampaignTypeID() {
298 $this->enableCiviCampaign();
303 'source_contact_id' => $this->_contactID
,
304 'subject' => 'Make-it-Happen Meeting',
305 'activity_date_time' => '20110316',
307 'location' => 'Pensulvania',
308 'details' => 'a test activity',
310 'activity_type_id' => 29,
314 $result = $this->callAPISuccess('activity', 'create', $params);
315 //todo test target & assignee are set
317 //$this->assertEquals($result['values'][$result['id']]['source_contact_id'], $this->_contactID, 'in line ' . __LINE__);
318 $result = $this->callAPISuccess('activity', 'get', array('id' => $result['id']));
319 $this->assertEquals($result['values'][$result['id']]['duration'], 120, 'in line ' . __LINE__
);
320 $this->assertEquals($result['values'][$result['id']]['subject'], 'Make-it-Happen Meeting', 'in line ' . __LINE__
);
321 $this->assertEquals($result['values'][$result['id']]['activity_date_time'], '2011-03-16 00:00:00', 'in line ' . __LINE__
);
322 $this->assertEquals($result['values'][$result['id']]['location'], 'Pensulvania', 'in line ' . __LINE__
);
323 $this->assertEquals($result['values'][$result['id']]['details'], 'a test activity', 'in line ' . __LINE__
);
324 $this->assertEquals($result['values'][$result['id']]['status_id'], 1, 'in line ' . __LINE__
);
327 public function testActivityReturnTargetAssignee() {
329 $description = "Example demonstrates setting & retrieving the target & source";
330 $subfile = "GetTargetandAssignee";
332 'source_contact_id' => $this->_contactID
,
333 'subject' => 'Make-it-Happen Meeting',
334 'activity_date_time' => '20110316',
336 'location' => 'Pensulvania',
337 'details' => 'a test activity',
339 'activity_type_id' => 1,
341 'target_contact_id' => $this->_contactID
,
342 'assignee_contact_id' => $this->_contactID
,
345 $result = $this->callAPIAndDocument('activity', 'create', $params, __FUNCTION__
, __FILE__
, $description, $subfile);
346 $result = $this->callAPISuccess('activity', 'get', array('id' => $result['id'], 'version' => $this->_apiversion
, 'return.assignee_contact_id' => 1, 'return.target_contact_id' => 1));
348 $this->assertEquals($this->_contactID
, $result['values'][$result['id']]['assignee_contact_id'][0], 'in line ' . __LINE__
);
349 $this->assertEquals($this->_contactID
, $result['values'][$result['id']]['target_contact_id'][0], 'in line ' . __LINE__
);
352 public function testActivityCreateExample() {
354 * Test civicrm_activity_create() using example code
356 require_once 'api/v3/examples/Activity/Create.php';
357 $result = activity_create_example();
358 $expectedResult = activity_create_expectedresult();
359 $this->assertEquals($result, $expectedResult);
363 * Test civicrm_activity_create() with valid parameters
364 * and some custom data
366 public function testActivityCreateCustom() {
367 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__
, __FILE__
);
368 $params = $this->_params
;
369 $params['custom_' . $ids['custom_field_id']] = "custom string";
370 $result = $this->callAPIAndDocument($this->_entity
, 'create', $params, __FUNCTION__
, __FILE__
);
371 $result = $this->callAPISuccess($this->_entity
, 'get', array('return.custom_' . $ids['custom_field_id'] => 1, 'version' => 3, 'id' => $result['id']));
372 $this->assertEquals("custom string", $result['values'][$result['id']]['custom_' . $ids['custom_field_id']], ' in line ' . __LINE__
);
374 $this->customFieldDelete($ids['custom_field_id']);
375 $this->customGroupDelete($ids['custom_group_id']);
379 * Test civicrm_activity_create() with valid parameters
380 * and some custom data
382 public function testActivityCreateCustomContactRefField() {
384 $this->callAPISuccess('contact', 'create', array('id' => $this->_contactID
, 'sort_name' => 'Contact, Test'));
385 $subfile = 'ContactRefCustomField';
386 $description = "demonstrates create with Contact Reference Custom Field";
387 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__
, __FILE__
);
389 'custom_group_id' => $ids['custom_group_id'],
390 'name' => 'Worker_Lookup',
391 'label' => 'Worker Lookup',
392 'html_type' => 'Autocomplete-Select',
393 'data_type' => 'ContactReference',
395 'is_searchable' => 1,
399 $customField = $this->callAPISuccess('custom_field', 'create', $params);
400 $params = $this->_params
;
401 $params['custom_' . $customField['id']] = "$this->_contactID";
403 $result = $this->callAPIAndDocument($this->_entity
, 'create', $params, __FUNCTION__
, __FILE__
, $description, $subfile, 'Create');
404 $result = $this->callAPIAndDocument($this->_entity
, 'get', array('return.custom_' . $customField['id'] => 1, 'id' => $result['id']), __FUNCTION__
, __FILE__
, 'Get with Contact Ref Custom Field', 'ContactRefCustomFieldGet', 'get');
406 $this->assertEquals('Anderson, Anthony', $result['values'][$result['id']]['custom_' . $customField['id']], ' in line ' . __LINE__
);
407 $this->assertEquals($this->_contactID
, $result['values'][$result['id']]['custom_' . $customField['id'] . "_id"], ' in line ' . __LINE__
);
408 $this->assertEquals('Anderson, Anthony', $result['values'][$result['id']]['custom_' . $customField['id'] . '_1'], ' in line ' . __LINE__
);
409 $this->assertEquals($this->_contactID
, $result['values'][$result['id']]['custom_' . $customField['id'] . "_1_id"], ' in line ' . __LINE__
);
410 $this->customFieldDelete($ids['custom_field_id']);
411 $this->customGroupDelete($ids['custom_group_id']);
415 * Test civicrm_activity_create() with an invalid text status_id
417 public function testActivityCreateBadTextStatus() {
420 'source_contact_id' => $this->_contactID
,
421 'subject' => 'Discussion on Apis for v3',
422 'activity_date_time' => date('Ymd'),
424 'location' => 'Pensulvania',
425 'details' => 'a test activity',
426 'status_id' => 'Invalid',
427 'activity_name' => 'Test activity type',
430 $result = $this->callAPIFailure('activity', 'create', $params);
434 * Test civicrm_activity_create() with an invalid text status_id
436 public function testActivityCreateSupportActivityStatus() {
439 'source_contact_id' => $this->_contactID
,
440 'subject' => 'Discussion on Apis for v3',
441 'activity_date_time' => date('Ymd'),
443 'location' => 'Pensulvania',
444 'details' => 'a test activity',
445 'activity_status_id' => 'Invalid',
446 'activity_name' => 'Test activity type',
449 $result = $this->callAPIFailure('activity', 'create', $params,
450 "'Invalid' is not a valid option for field status_id");
455 * Test civicrm_activity_create() with valid parameters,
456 * using a text status_id
458 public function testActivityCreateTextStatus() {
462 'source_contact_id' => $this->_contactID
,
463 'subject' => 'Make-it-Happen Meeting',
464 'activity_date_time' => date('Ymd'),
466 'location' => 'Pensulvania',
467 'details' => 'a test activity',
468 'status_id' => 'Scheduled',
469 'activity_name' => 'Test activity type',
472 $result = $this->callAPISuccess('activity', 'create', $params);
473 $this->assertEquals($result['values'][$result['id']]['duration'], 120, 'in line ' . __LINE__
);
474 $this->assertEquals($result['values'][$result['id']]['subject'], 'Make-it-Happen Meeting', 'in line ' . __LINE__
);
475 $this->assertEquals($result['values'][$result['id']]['activity_date_time'], date('Ymd') . '000000', 'in line ' . __LINE__
);
476 $this->assertEquals($result['values'][$result['id']]['location'], 'Pensulvania', 'in line ' . __LINE__
);
477 $this->assertEquals($result['values'][$result['id']]['details'], 'a test activity', 'in line ' . __LINE__
);
481 * Test civicrm_activity_get() with no params
483 public function testActivityGetEmpty() {
484 $result = $this->callAPISuccess('activity', 'get', array());
488 * Test civicrm_activity_get() with a good activity ID
490 public function testActivityGetGoodID1() {
491 // Insert rows in civicrm_activity creating activities 4 and
493 $description = "Function demonstrates getting asignee_contact_id & using it to get the contact";
494 $subfile = 'ReturnAssigneeContact';
495 $activity = $this->callAPISuccess('activity', 'create', $this->_params
);
497 $contact = $this->callAPISuccess('Contact', 'Create', array(
498 'first_name' => "The Rock",
499 'last_name' =>'roccky',
500 'contact_type' => 'Individual',
502 'api.activity.create' => array(
503 'id' => $activity['id'], 'assignee_contact_id' => '$value.id',
508 'activity_id' => $activity['id'],
509 'version' => $this->_apiversion
,
511 'return.assignee_contact_id' => 1,
512 'api.contact.get' => array(
513 'id' => '$value.source_contact_id',
517 $result = $this->callAPIAndDocument('Activity', 'Get', $params, __FUNCTION__
, __FILE__
, $description, $subfile);
519 $this->assertEquals($activity['id'], $result['id'], 'In line ' . __LINE__
);
521 $this->assertEquals($contact['id'], $result['values'][0]['assignee_contact_id'][0], 'In line ' . __LINE__
);
523 $this->assertEquals($this->_contactID
, $result['values'][0]['api.contact.get']['values'][0]['contact_id'], 'In line ' . __LINE__
);
524 $this->assertEquals($this->test_activity_type_value
, $result['values'][0]['activity_type_id'], 'In line ' . __LINE__
);
525 $this->assertEquals("test activity type id", $result['values'][0]['subject'], 'In line ' . __LINE__
);
529 * test that get functioning does filtering
531 public function testGetFilter() {
533 'source_contact_id' => $this->_contactID
,
534 'subject' => 'Make-it-Happen Meeting',
535 'activity_date_time' => '20110316',
537 'location' => 'Pensulvania',
538 'details' => 'a test activity',
540 'activity_name' => 'Test activity type',
543 $result = $this->callAPISuccess('Activity', 'Create', $params);
544 $this->callAPISuccess('Activity', 'Get', array('subject' => 'Make-it-Happen Meeting'));
545 $this->assertEquals(1, $result['count']);
546 $this->assertEquals('Make-it-Happen Meeting', $result['values'][$result['id']]['subject']);
547 $this->callAPISuccess('Activity', 'Delete', array('id' => $result['id']));
552 * Test civicrm_activity_get() with filter target_contact_id
554 public function testActivityGetTargetFilter() {
555 $params = $this->_params
;
556 $contact1Params = array(
557 'first_name' => 'John',
558 'middle_name' => 'J.',
559 'last_name' => 'Anderson',
562 'email' => 'john_anderson@civicrm.org',
563 'contact_type' => 'Individual',
566 $contact1 = $this->individualCreate($contact1Params);
567 $contact2Params = array(
568 'first_name' => 'Michal',
569 'middle_name' => 'J.',
570 'last_name' => 'Anderson',
573 'email' => 'michal_anderson@civicrm.org',
574 'contact_type' => 'Individual',
577 $contact2 = $this->individualCreate($contact2Params);
579 $params['assignee_contact_id'] = array($contact1, $contact2);
580 $params['target_contact_id'] = array($contact2 => $contact2);
581 $activity = $this->callAPISuccess('Activity', 'Create', $params);
583 $activityget = $this->callAPISuccess('Activity', 'get', array('id' => $activity['id'], 'target_contact_id' => $contact2,'return.target_contact_id' => 1));
584 $this->assertEquals($activity['id'], $activityget['id'], 'In line ' . __LINE__
);
585 $this->assertEquals($contact2, $activityget['values'][$activityget['id']]['target_contact_id'][0], 'In line ' . __LINE__
);
587 $activityget = $this->callAPISuccess('activity', 'get', array('target_contact_id' => $this->_contactID
,'return.target_contact_id' => 1,'id' => $activity['id']));
588 if ($activityget['count'] > 0) {
589 $this->assertNotEquals($contact2, $activityget['values'][$activityget['id']]['target_contact_id'][0], 'In line ' . __LINE__
);
594 * test that get functioning does filtering
596 public function testGetStatusID() {
598 'source_contact_id' => $this->_contactID
,
599 'subject' => 'Make-it-Happen Meeting',
600 'activity_date_time' => '20110316',
602 'location' => 'Pensulvania',
603 'details' => 'a test activity',
605 'activity_name' => 'Test activity type',
608 $this->callAPISuccess('Activity', 'Create', $params);
609 $result = $this->callAPISuccess('Activity', 'Get', array('activity_status_id' => '1'));
610 $this->assertEquals(1, $result['count'], 'one activity of status 1 should exist');
612 $result = $this->callAPISuccess('Activity', 'Get', array('status_id' => '1'));
613 $this->assertEquals(1, $result['count'], 'status_id should also work');
615 $result = $this->callAPISuccess('Activity', 'Get', array('activity_status_id' => '2'));
616 $this->assertEquals(0, $result['count'], 'No activities of status 1 should exist');
617 $result = $this->callAPISuccess('Activity', 'Get', array(
618 'version' => $this->_apiversion
,
619 'status_id' => '2'));
620 $this->assertEquals(0, $result['count'], 'No activities of status 1 should exist');
626 * test that get functioning does filtering
628 public function testGetFilterMaxDate() {
630 'source_contact_id' => $this->_contactID
,
631 'subject' => 'Make-it-Happen Meeting',
632 'activity_date_time' => '20110101',
634 'location' => 'Pensulvania',
635 'details' => 'a test activity',
637 'activity_name' => 'Test activity type',
638 'version' => $this->_apiversion
,
641 $activityOne = $this->callAPISuccess('Activity', 'Create', $params);
642 $params['activity_date_time'] = 20120216;
643 $activityTwo = $this->callAPISuccess('Activity', 'Create', $params);
644 $result = $this->callAPISuccess('Activity', 'Get', array(
647 $description = "demonstrates _low filter (at time of writing doesn't work if contact_id is set";
648 $subfile = "DateTimeLow";
649 $this->assertEquals(2, $result['count']);
652 'filter.activity_date_time_low' => '20120101000000',
655 $result = $this->callAPIAndDocument('Activity', 'Get', $params, __FUNCTION__
, __FILE__
, $description, $subfile);
656 $this->assertEquals(1, $result['count'], 'in line ' . __LINE__
);
657 $description = "demonstrates _high filter (at time of writing doesn't work if contact_id is set";
658 $subfile = "DateTimeHigh";
659 $this->assertEquals('2012-02-16 00:00:00', $result['values'][0]['activity_date_time'], 'in line ' . __LINE__
);
661 'source_contact_id' => $this->_contactID
,
663 'filter.activity_date_time_high' => '20120101000000',
666 $result = $this->callAPIAndDocument('Activity', 'Get', $params, __FUNCTION__
, __FILE__
, $description, $subfile);
668 $this->assertEquals(1, $result['count']);
669 $this->assertEquals('2011-01-01 00:00:00', $result['values'][0]['activity_date_time'], 'in line ' . __LINE__
);
671 $this->callAPISuccess('Activity', 'Delete', array('version' => 3, 'id' => $activityOne['id']));
672 $this->callAPISuccess('Activity', 'Delete', array('version' => 3, 'id' => $activityTwo['id']));
676 * Test civicrm_activity_get() with a good activity ID which
677 * has associated custom data
679 public function testActivityGetGoodIDCustom() {
680 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__
, __FILE__
);
682 $params = $this->_params
;
683 $params['custom_' . $ids['custom_field_id']] = "custom string";
685 $result = $this->callAPISuccess($this->_entity
, 'create', $params);
687 // Retrieve the test value
689 'activity_type_id' => $this->test_activity_type_value
,
691 'return.custom_' . $ids['custom_field_id'] => 1,
693 $result = $this->callAPIAndDocument('activity', 'get', $params, __FUNCTION__
, __FILE__
);
694 $this->assertEquals("custom string", $result['values'][0]['custom_' . $ids['custom_field_id']]);
696 $this->assertEquals($this->test_activity_type_value
, $result['values'][0]['activity_type_id']);
697 $this->assertEquals('test activity type id', $result['values'][0]['subject'], 'In line ' . __LINE__
);
698 $this->customFieldDelete($ids['custom_field_id']);
699 $this->customGroupDelete($ids['custom_group_id']);
703 * Test civicrm_activity_get() with a good activity ID which
704 * has associated custom data
706 public function testActivityGetContact_idCustom() {
707 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__
, __FILE__
);
709 $params = $this->_params
;
710 $params['custom_' . $ids['custom_field_id']] = "custom string";
712 $result = $this->callAPISuccess($this->_entity
, 'create', $params);
713 // Retrieve the test value
715 'contact_id' => $this->_params
['source_contact_id'],
716 'activity_type_id' => $this->test_activity_type_value
,
718 'return.custom_' . $ids['custom_field_id'] => 1,
720 $result = $this->callAPIAndDocument('activity', 'get', $params, __FUNCTION__
, __FILE__
);
721 $this->assertEquals("custom string", $result['values'][0]['custom_' . $ids['custom_field_id']], ' in line ' . __LINE__
);
723 $this->assertEquals($this->test_activity_type_value
, $result['values'][0]['activity_type_id'], 'In line ' . __LINE__
);
724 $this->assertEquals('test activity type id', $result['values'][0]['subject'], 'In line ' . __LINE__
);
725 $this->assertEquals($result['values'][0]['id'], $result['id'], 'in line ' . __LINE__
);
729 * Check activity deletion with empty params
731 public function testDeleteActivityForEmptyParams() {
732 $params = array('version' => $this->_apiversion
);
733 $result = $this->callAPIFailure('activity', 'delete', $params);
737 * Check activity deletion without activity id
739 public function testDeleteActivityWithoutId() {
741 'activity_name' => 'Meeting',
742 'version' => $this->_apiversion
,
744 $result = $this->callAPIFailure('activity', 'delete', $params);
748 * Check activity deletion without activity type
750 public function testDeleteActivityWithoutActivityType() {
751 $params = array('id' => 1);
752 $result = $this->callAPIFailure('activity', 'delete', $params);
756 * Check activity deletion with incorrect data
758 public function testDeleteActivityWithIncorrectActivityType() {
761 'activity_name' => 'Test Activity',
764 $result = $this->callAPIFailure('activity', 'delete', $params);
768 * Check activity deletion with correct data
770 public function testDeleteActivity() {
771 $result = $this->callAPISuccess('activity', 'create', $this->_params
);
773 'id' => $result['id'],
774 'version' => $this->_apiversion
,
777 $result = $this->callAPISuccess('activity', 'delete', $params);
778 $this->documentMe($params, $result, __FUNCTION__
, __FILE__
);
782 * Check if required fields are not passed
784 public function testActivityUpdateWithoutRequired() {
786 'subject' => 'this case should fail',
787 'scheduled_date_time' => date('Ymd'),
790 $result = $this->callAPIFailure('activity', 'create', $params);
794 * Test civicrm_activity_update() with non-numeric id
796 public function testActivityUpdateWithNonNumericId() {
798 'id' => 'lets break it',
799 'activity_name' => 'Meeting',
800 'subject' => 'this case should fail',
801 'scheduled_date_time' => date('Ymd'),
804 $result = $this->callAPIFailure('activity', 'create', $params);
808 * Check with incorrect required fields
810 public function testActivityUpdateWithIncorrectContactActivityType() {
813 'activity_name' => 'Test Activity',
814 'subject' => 'this case should fail',
815 'scheduled_date_time' => date('Ymd'),
816 'source_contact_id' => $this->_contactID
,
819 $result = $this->callAPIFailure('activity', 'create', $params,
820 'Invalid Activity Id');
824 * Test civicrm_activity_update() to update an existing activity
826 public function testActivityUpdate() {
827 $result = $this->callAPISuccess('activity', 'create', $this->_params
);
830 'id' => $result['id'],
831 'subject' => 'Make-it-Happen Meeting',
832 'activity_date_time' => '20091011123456',
834 'location' => '21, Park Avenue',
835 'details' => 'Lets update Meeting',
837 'source_contact_id' => $this->_contactID
,
841 $result = $this->callAPISuccess('activity', 'create', $params);
842 //hack on date comparison - really we should make getAndCheck smarter to handle dates
843 $params['activity_date_time'] = '2009-10-11 12:34:56';
844 // we also unset source_contact_id since it is stored in an aux table
845 unset($params['source_contact_id']);
846 $this->getAndCheck($params, $result['id'], 'activity');
850 * Test civicrm_activity_update() with valid parameters
851 * and some custom data
853 public function testActivityUpdateCustom() {
854 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__
, __FILE__
);
856 $params = $this->_params
;
858 // Create an activity with custom data
859 //this has been updated from the previous 'old format' function - need to make it work
861 'source_contact_id' => $this->_contactID
,
862 'subject' => 'Make-it-Happen Meeting',
863 'activity_date_time' => '2009-10-18',
865 'location' => 'Pensulvania',
866 'details' => 'a test activity to check the update api',
868 'activity_name' => 'Test activity type',
869 'version' => $this->_apiversion
,
870 'custom_' . $ids['custom_field_id'] => 'custom string',
872 $result = $this->callAPISuccess('activity', 'create', $params);
874 $activityId = $result['id'];
875 $result = $this->callAPISuccess($this->_entity
, 'get', array('return.custom_' . $ids['custom_field_id'] => 1, 'version' => 3, 'id' => $result['id']));
876 $this->assertEquals("custom string", $result['values'][$result['id']]['custom_' . $ids['custom_field_id']], ' in line ' . __LINE__
);
877 $this->assertEquals("2009-10-18 00:00:00", $result['values'][$result['id']]['activity_date_time'], ' in line ' . __LINE__
);
878 $fields = $this->callAPISuccess('activity', 'getfields', array('version' => $this->_apiversion
));
879 $this->assertTrue(is_array($fields['values']['custom_' . $ids['custom_field_id']]));
881 // Update the activity with custom data
884 'source_contact_id' => $this->_contactID
,
885 'subject' => 'Make-it-Happen Meeting',
887 'activity_name' => 'Test activity type',
888 // add this since dates are messed up
889 'activity_date_time' => date('Ymd'),
890 'custom_' . $ids['custom_field_id'] => 'Updated my test data',
891 'version' => $this->_apiversion
,
893 $result = $this->callAPISuccess('Activity', 'Create', $params);
895 $result = $this->callAPISuccess($this->_entity
, 'get', array('return.custom_' . $ids['custom_field_id'] => 1, 'version' => 3, 'id' => $result['id']));
896 $this->assertEquals("Updated my test data", $result['values'][$result['id']]['custom_' . $ids['custom_field_id']], ' in line ' . __LINE__
);
900 * Test civicrm_activity_update() for core activity fields
901 * and some custom data
903 public function testActivityUpdateCheckCoreFields() {
904 $params = $this->_params
;
905 $contact1Params = array(
906 'first_name' => 'John',
907 'middle_name' => 'J.',
908 'last_name' => 'Anderson',
911 'email' => 'john_anderson@civicrm.org',
912 'contact_type' => 'Individual',
915 $contact1 = $this->individualCreate($contact1Params);
916 $contact2Params = array(
917 'first_name' => 'Michal',
918 'middle_name' => 'J.',
919 'last_name' => 'Anderson',
922 'email' => 'michal_anderson@civicrm.org',
923 'contact_type' => 'Individual',
926 $contact2 = $this->individualCreate($contact2Params);
928 $params['assignee_contact_id'] = array($contact1, $contact2);
929 $params['target_contact_id'] = array($contact2 => $contact2);
930 $result = $this->callAPISuccess('Activity', 'Create', $params);
932 $activityId = $result['id'];
934 'return.assignee_contact_id' => 1,
935 'return.target_contact_id' => 1,
936 'version' => $this->_apiversion
,
939 $result = $this->callAPISuccess($this->_entity
, 'get',$getParams );
940 $assignee = $result['values'][$result['id']]['assignee_contact_id'];
941 $target = $result['values'][$result['id']]['target_contact_id'];
942 $this->assertEquals(2, count($assignee), ' in line ' . __LINE__
);
943 $this->assertEquals(1, count($target), ' in line ' . __LINE__
);
944 $this->assertEquals(TRUE, in_array($contact1, $assignee), ' in line ' . __LINE__
);
945 $this->assertEquals(TRUE, in_array($contact2, $target), ' in line ' . __LINE__
);
947 $contact3Params = array(
948 'first_name' => 'Jijo',
949 'middle_name' => 'J.',
950 'last_name' => 'Anderson',
953 'email' => 'jijo_anderson@civicrm.org',
954 'contact_type' => 'Individual',
957 $contact4Params = array(
958 'first_name' => 'Grant',
959 'middle_name' => 'J.',
960 'last_name' => 'Anderson',
963 'email' => 'grant_anderson@civicrm.org',
964 'contact_type' => 'Individual',
967 $contact3 = $this->individualCreate($contact3Params);
968 $contact4 = $this->individualCreate($contact4Params);
971 $params['id'] = $activityId;
972 $params['assignee_contact_id'] = array($contact3 => $contact3);
973 $params['target_contact_id'] = array($contact4 => $contact4);
975 $result = $this->callAPISuccess('activity', 'create', $params);
977 $this->assertEquals($activityId, $result['id'], ' in line ' . __LINE__
);
979 $result = $this->callAPISuccess(
983 'return.assignee_contact_id' => 1,
984 'return.target_contact_id' => 1,
985 'return.source_contact_id' => 1,
986 'id' => $result['id']
990 $assignee = $result['values'][$result['id']]['assignee_contact_id'];
991 $target = $result['values'][$result['id']]['target_contact_id'];
993 $this->assertEquals(1, count($assignee), ' in line ' . __LINE__
);
994 $this->assertEquals(1, count($target), ' in line ' . __LINE__
);
995 $this->assertEquals(TRUE, in_array($contact3, $assignee), ' in line ' . __LINE__
);
996 $this->assertEquals(TRUE, in_array($contact4, $target), ' in line ' . __LINE__
);
998 foreach ($this->_params
as $fld => $val) {
999 $this->assertEquals($val, $result['values'][$result['id']][$fld]);
1004 * Test civicrm_activity_update() where the DB has a date_time
1005 * value and there is none in the update params.
1007 public function testActivityUpdateNotDate() {
1008 $result = $this->callAPISuccess('activity', 'create', $this->_params
);
1011 'id' => $result['id'],
1012 'subject' => 'Make-it-Happen Meeting',
1014 'location' => '21, Park Avenue',
1015 'details' => 'Lets update Meeting',
1017 'source_contact_id' => $this->_contactID
,
1021 $result = $this->callAPISuccess('activity', 'create', $params);
1022 //hack on date comparison - really we should make getAndCheck smarter to handle dates
1023 $params['activity_date_time'] = $this->_params
['activity_date_time'];
1024 // we also unset source_contact_id since it is stored in an aux table
1025 unset($params['source_contact_id']);
1026 $this->getAndCheck($params, $result['id'], 'activity');
1030 * Check activity update with status
1032 public function testActivityUpdateWithStatus() {
1033 $activity = $this->callAPISuccess('activity', 'create', $this->_params
);
1035 'id' => $activity['id'],
1036 'source_contact_id' => $this->_contactID
,
1037 'subject' => 'Hurry update works',
1039 'activity_name' => 'Test activity type',
1042 $result = $this->callAPISuccess('activity', 'create', $params);
1043 $this->assertEquals($result['id'], $activity['id']);
1044 $this->assertEquals($result['values'][$activity['id']]['subject'], 'Hurry update works');
1045 $this->assertEquals($result['values'][$activity['id']]['status_id'], 1
1050 * Test civicrm_activity_update() where the source_contact_id
1051 * is not in the update params.
1053 public function testActivityUpdateKeepSource() {
1054 $activity = $this->callAPISuccess('activity', 'create', $this->_params
);
1055 // Updating the activity but not providing anything for the source contact
1056 // (It was set as $this->_contactID earlier.)
1058 'id' => $activity['id'],
1059 'subject' => 'Updated Make-it-Happen Meeting',
1061 'location' => '21, Park Avenue',
1062 'details' => 'Lets update Meeting',
1064 'activity_name' => 'Test activity type',
1068 $result = $this->callAPISuccess('activity', 'create', $params);
1069 $findactivity = $this->callAPISuccess('Activity', 'Get', array('id' => $activity['id']));
1073 * Test civicrm_activities_contact_get()
1075 public function testActivitiesContactGet() {
1076 $activity = $this->callAPISuccess('activity', 'create', $this->_params
);
1077 $activity2 = $this->callAPISuccess('activity', 'create', $this->_params2
);
1078 // Get activities associated with contact $this->_contactID
1080 'contact_id' => $this->_contactID
,
1082 $result = $this->callAPISuccess('activity', 'get', $params);
1084 $this->assertEquals(2, $result['count'], 'In line ' . __LINE__
);
1085 $this->assertEquals($this->test_activity_type_value
, $result['values'][$activity['id']]['activity_type_id'], 'In line ' . __LINE__
);
1086 $this->assertEquals('Test activity type', $result['values'][$activity['id']]['activity_name'], 'In line ' . __LINE__
);
1087 $this->assertEquals('Test activity type', $result['values'][$activity2['id']]['activity_name'], 'In line ' . __LINE__
);
1090 * test chained Activity format
1092 public function testchainedActivityGet() {
1094 $activity = $this->callAPISuccess('Contact', 'Create', array(
1095 'display_name' => "bob brown",
1096 'contact_type' => 'Individual',
1097 'api.activity_type.create' => array(
1099 'label' => 'send out letters',
1104 ), 'api.activity.create' => array('subject' => 'send letter', 'activity_type_id' => '$value.api.activity_type.create.values.0.value'),
1107 $result = $this->callAPISuccess('Activity', 'Get', array(
1108 'id' => $activity['id'],
1109 'return.assignee_contact_id' => 1,
1110 'api.contact.get' => array('api.pledge.get' => 1),
1115 * Test civicrm_activity_contact_get() with invalid Contact Id
1117 public function testActivitiesContactGetWithInvalidContactId() {
1118 $params = array('contact_id' => 'contact');
1119 $result = $this->callAPIFailure('activity', 'get', $params);
1123 * Test civicrm_activity_contact_get() with contact having no Activity
1125 public function testActivitiesContactGetHavingNoActivity() {
1127 'first_name' => 'dan',
1128 'last_name' => 'conberg',
1129 'email' => 'dan.conberg@w.co.in',
1130 'contact_type' => 'Individual',
1133 $contact = $this->callAPISuccess('contact', 'create', $params);
1135 'contact_id' => $contact['id'],
1137 $result = $this->callAPISuccess('activity', 'get', $params);
1138 $this->assertEquals($result['count'], 0, 'in line ' . __LINE__
);
1141 public function testGetFields() {
1142 $params = array('action' => 'create');
1143 $result = $this->callAPIAndDocument('activity', 'getfields', $params, __FUNCTION__
, __FILE__
, NULL, NULL, 'getfields');
1144 $this->assertTrue(is_array($result['values']), 'get fields doesnt return values array in line ' . __LINE__
);
1145 // $this->assertTrue(is_array($result['values']['priority_id']['options']));
1146 foreach ($result['values'] as $key => $value) {
1147 $this->assertTrue(is_array($value), $key . " is not an array in line " . __LINE__
);