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() {
461 'source_contact_id' => $this->_contactID
,
462 'subject' => 'Make-it-Happen Meeting',
463 'activity_date_time' => date('Ymd'),
465 'location' => 'Pensulvania',
466 'details' => 'a test activity',
467 'status_id' => 'Scheduled',
468 'activity_name' => 'Test activity type',
471 $result = $this->callAPISuccess('activity', 'create', $params);
472 $this->assertEquals($result['values'][$result['id']]['duration'], 120, 'in line ' . __LINE__
);
473 $this->assertEquals($result['values'][$result['id']]['subject'], 'Make-it-Happen Meeting', 'in line ' . __LINE__
);
474 $this->assertEquals($result['values'][$result['id']]['activity_date_time'], date('Ymd') . '000000', 'in line ' . __LINE__
);
475 $this->assertEquals($result['values'][$result['id']]['location'], 'Pensulvania', 'in line ' . __LINE__
);
476 $this->assertEquals($result['values'][$result['id']]['details'], 'a test activity', 'in line ' . __LINE__
);
480 * Test civicrm_activity_get() with no params
482 public function testActivityGetEmpty() {
483 $result = $this->callAPISuccess('activity', 'get', array());
487 * Test civicrm_activity_get() with a good activity ID
489 public function testActivityGetGoodID1() {
490 // Insert rows in civicrm_activity creating activities 4 and
492 $description = "Function demonstrates getting asignee_contact_id & using it to get the contact";
493 $subfile = 'ReturnAssigneeContact';
494 $activity = $this->callAPISuccess('activity', 'create', $this->_params
);
496 $contact = $this->callAPISuccess('Contact', 'Create', array(
497 'first_name' => "The Rock",
498 'last_name' => 'roccky',
499 'contact_type' => 'Individual',
501 'api.activity.create' => array(
502 'id' => $activity['id'],
503 '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');
625 * test that get functioning does filtering
627 public function testGetFilterMaxDate() {
629 'source_contact_id' => $this->_contactID
,
630 'subject' => 'Make-it-Happen Meeting',
631 'activity_date_time' => '20110101',
633 'location' => 'Pensulvania',
634 'details' => 'a test activity',
636 'activity_name' => 'Test activity type',
637 'version' => $this->_apiversion
,
640 $activityOne = $this->callAPISuccess('Activity', 'Create', $params);
641 $params['activity_date_time'] = 20120216;
642 $activityTwo = $this->callAPISuccess('Activity', 'Create', $params);
643 $result = $this->callAPISuccess('Activity', 'Get', array(
646 $description = "demonstrates _low filter (at time of writing doesn't work if contact_id is set";
647 $subfile = "DateTimeLow";
648 $this->assertEquals(2, $result['count']);
651 'filter.activity_date_time_low' => '20120101000000',
654 $result = $this->callAPIAndDocument('Activity', 'Get', $params, __FUNCTION__
, __FILE__
, $description, $subfile);
655 $this->assertEquals(1, $result['count'], 'in line ' . __LINE__
);
656 $description = "demonstrates _high filter (at time of writing doesn't work if contact_id is set";
657 $subfile = "DateTimeHigh";
658 $this->assertEquals('2012-02-16 00:00:00', $result['values'][0]['activity_date_time'], 'in line ' . __LINE__
);
660 'source_contact_id' => $this->_contactID
,
662 'filter.activity_date_time_high' => '20120101000000',
665 $result = $this->callAPIAndDocument('Activity', 'Get', $params, __FUNCTION__
, __FILE__
, $description, $subfile);
667 $this->assertEquals(1, $result['count']);
668 $this->assertEquals('2011-01-01 00:00:00', $result['values'][0]['activity_date_time'], 'in line ' . __LINE__
);
670 $this->callAPISuccess('Activity', 'Delete', array('version' => 3, 'id' => $activityOne['id']));
671 $this->callAPISuccess('Activity', 'Delete', array('version' => 3, 'id' => $activityTwo['id']));
675 * Test civicrm_activity_get() with a good activity ID which
676 * has associated custom data
678 public function testActivityGetGoodIDCustom() {
679 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__
, __FILE__
);
681 $params = $this->_params
;
682 $params['custom_' . $ids['custom_field_id']] = "custom string";
684 $result = $this->callAPISuccess($this->_entity
, 'create', $params);
686 // Retrieve the test value
688 'activity_type_id' => $this->test_activity_type_value
,
690 'return.custom_' . $ids['custom_field_id'] => 1,
692 $result = $this->callAPIAndDocument('activity', 'get', $params, __FUNCTION__
, __FILE__
);
693 $this->assertEquals("custom string", $result['values'][0]['custom_' . $ids['custom_field_id']]);
695 $this->assertEquals($this->test_activity_type_value
, $result['values'][0]['activity_type_id']);
696 $this->assertEquals('test activity type id', $result['values'][0]['subject'], 'In line ' . __LINE__
);
697 $this->customFieldDelete($ids['custom_field_id']);
698 $this->customGroupDelete($ids['custom_group_id']);
702 * Test civicrm_activity_get() with a good activity ID which
703 * has associated custom data
705 public function testActivityGetContact_idCustom() {
706 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__
, __FILE__
);
708 $params = $this->_params
;
709 $params['custom_' . $ids['custom_field_id']] = "custom string";
711 $result = $this->callAPISuccess($this->_entity
, 'create', $params);
712 // Retrieve the test value
714 'contact_id' => $this->_params
['source_contact_id'],
715 'activity_type_id' => $this->test_activity_type_value
,
717 'return.custom_' . $ids['custom_field_id'] => 1,
719 $result = $this->callAPIAndDocument('activity', 'get', $params, __FUNCTION__
, __FILE__
);
720 $this->assertEquals("custom string", $result['values'][0]['custom_' . $ids['custom_field_id']], ' in line ' . __LINE__
);
722 $this->assertEquals($this->test_activity_type_value
, $result['values'][0]['activity_type_id'], 'In line ' . __LINE__
);
723 $this->assertEquals('test activity type id', $result['values'][0]['subject'], 'In line ' . __LINE__
);
724 $this->assertEquals($result['values'][0]['id'], $result['id'], 'in line ' . __LINE__
);
728 * Check activity deletion with empty params
730 public function testDeleteActivityForEmptyParams() {
731 $params = array('version' => $this->_apiversion
);
732 $result = $this->callAPIFailure('activity', 'delete', $params);
736 * Check activity deletion without activity id
738 public function testDeleteActivityWithoutId() {
740 'activity_name' => 'Meeting',
741 'version' => $this->_apiversion
,
743 $result = $this->callAPIFailure('activity', 'delete', $params);
747 * Check activity deletion without activity type
749 public function testDeleteActivityWithoutActivityType() {
750 $params = array('id' => 1);
751 $result = $this->callAPIFailure('activity', 'delete', $params);
755 * Check activity deletion with incorrect data
757 public function testDeleteActivityWithIncorrectActivityType() {
760 'activity_name' => 'Test Activity',
763 $result = $this->callAPIFailure('activity', 'delete', $params);
767 * Check activity deletion with correct data
769 public function testDeleteActivity() {
770 $result = $this->callAPISuccess('activity', 'create', $this->_params
);
772 'id' => $result['id'],
773 'version' => $this->_apiversion
,
776 $result = $this->callAPISuccess('activity', 'delete', $params);
777 $this->documentMe($params, $result, __FUNCTION__
, __FILE__
);
781 * Check if required fields are not passed
783 public function testActivityUpdateWithoutRequired() {
785 'subject' => 'this case should fail',
786 'scheduled_date_time' => date('Ymd'),
789 $result = $this->callAPIFailure('activity', 'create', $params);
793 * Test civicrm_activity_update() with non-numeric id
795 public function testActivityUpdateWithNonNumericId() {
797 'id' => 'lets break it',
798 'activity_name' => 'Meeting',
799 'subject' => 'this case should fail',
800 'scheduled_date_time' => date('Ymd'),
803 $result = $this->callAPIFailure('activity', 'create', $params);
807 * Check with incorrect required fields
809 public function testActivityUpdateWithIncorrectContactActivityType() {
812 'activity_name' => 'Test Activity',
813 'subject' => 'this case should fail',
814 'scheduled_date_time' => date('Ymd'),
815 'source_contact_id' => $this->_contactID
,
818 $result = $this->callAPIFailure('activity', 'create', $params,
819 'Invalid Activity Id');
823 * Test civicrm_activity_update() to update an existing activity
825 public function testActivityUpdate() {
826 $result = $this->callAPISuccess('activity', 'create', $this->_params
);
829 'id' => $result['id'],
830 'subject' => 'Make-it-Happen Meeting',
831 'activity_date_time' => '20091011123456',
833 'location' => '21, Park Avenue',
834 'details' => 'Lets update Meeting',
836 'source_contact_id' => $this->_contactID
,
840 $result = $this->callAPISuccess('activity', 'create', $params);
841 //hack on date comparison - really we should make getAndCheck smarter to handle dates
842 $params['activity_date_time'] = '2009-10-11 12:34:56';
843 // we also unset source_contact_id since it is stored in an aux table
844 unset($params['source_contact_id']);
845 $this->getAndCheck($params, $result['id'], 'activity');
849 * Test civicrm_activity_update() with valid parameters
850 * and some custom data
852 public function testActivityUpdateCustom() {
853 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__
, __FILE__
);
855 $params = $this->_params
;
857 // Create an activity with custom data
858 //this has been updated from the previous 'old format' function - need to make it work
860 'source_contact_id' => $this->_contactID
,
861 'subject' => 'Make-it-Happen Meeting',
862 'activity_date_time' => '2009-10-18',
864 'location' => 'Pensulvania',
865 'details' => 'a test activity to check the update api',
867 'activity_name' => 'Test activity type',
868 'version' => $this->_apiversion
,
869 'custom_' . $ids['custom_field_id'] => 'custom string',
871 $result = $this->callAPISuccess('activity', 'create', $params);
873 $activityId = $result['id'];
874 $result = $this->callAPISuccess($this->_entity
, 'get', array('return.custom_' . $ids['custom_field_id'] => 1, 'version' => 3, 'id' => $result['id']));
875 $this->assertEquals("custom string", $result['values'][$result['id']]['custom_' . $ids['custom_field_id']], ' in line ' . __LINE__
);
876 $this->assertEquals("2009-10-18 00:00:00", $result['values'][$result['id']]['activity_date_time'], ' in line ' . __LINE__
);
877 $fields = $this->callAPISuccess('activity', 'getfields', array('version' => $this->_apiversion
));
878 $this->assertTrue(is_array($fields['values']['custom_' . $ids['custom_field_id']]));
880 // Update the activity with custom data
883 'source_contact_id' => $this->_contactID
,
884 'subject' => 'Make-it-Happen Meeting',
886 'activity_name' => 'Test activity type',
887 // add this since dates are messed up
888 'activity_date_time' => date('Ymd'),
889 'custom_' . $ids['custom_field_id'] => 'Updated my test data',
890 'version' => $this->_apiversion
,
892 $result = $this->callAPISuccess('Activity', 'Create', $params);
894 $result = $this->callAPISuccess($this->_entity
, 'get', array('return.custom_' . $ids['custom_field_id'] => 1, 'version' => 3, 'id' => $result['id']));
895 $this->assertEquals("Updated my test data", $result['values'][$result['id']]['custom_' . $ids['custom_field_id']], ' in line ' . __LINE__
);
899 * Test civicrm_activity_update() for core activity fields
900 * and some custom data
902 public function testActivityUpdateCheckCoreFields() {
903 $params = $this->_params
;
904 $contact1Params = array(
905 'first_name' => 'John',
906 'middle_name' => 'J.',
907 'last_name' => 'Anderson',
910 'email' => 'john_anderson@civicrm.org',
911 'contact_type' => 'Individual',
914 $contact1 = $this->individualCreate($contact1Params);
915 $contact2Params = array(
916 'first_name' => 'Michal',
917 'middle_name' => 'J.',
918 'last_name' => 'Anderson',
921 'email' => 'michal_anderson@civicrm.org',
922 'contact_type' => 'Individual',
925 $contact2 = $this->individualCreate($contact2Params);
927 $params['assignee_contact_id'] = array($contact1, $contact2);
928 $params['target_contact_id'] = array($contact2 => $contact2);
929 $result = $this->callAPISuccess('Activity', 'Create', $params);
931 $activityId = $result['id'];
933 'return.assignee_contact_id' => 1,
934 'return.target_contact_id' => 1,
935 'version' => $this->_apiversion
,
938 $result = $this->callAPISuccess($this->_entity
, 'get', $getParams );
939 $assignee = $result['values'][$result['id']]['assignee_contact_id'];
940 $target = $result['values'][$result['id']]['target_contact_id'];
941 $this->assertEquals(2, count($assignee), ' in line ' . __LINE__
);
942 $this->assertEquals(1, count($target), ' in line ' . __LINE__
);
943 $this->assertEquals(TRUE, in_array($contact1, $assignee), ' in line ' . __LINE__
);
944 $this->assertEquals(TRUE, in_array($contact2, $target), ' in line ' . __LINE__
);
946 $contact3Params = array(
947 'first_name' => 'Jijo',
948 'middle_name' => 'J.',
949 'last_name' => 'Anderson',
952 'email' => 'jijo_anderson@civicrm.org',
953 'contact_type' => 'Individual',
956 $contact4Params = array(
957 'first_name' => 'Grant',
958 'middle_name' => 'J.',
959 'last_name' => 'Anderson',
962 'email' => 'grant_anderson@civicrm.org',
963 'contact_type' => 'Individual',
966 $contact3 = $this->individualCreate($contact3Params);
967 $contact4 = $this->individualCreate($contact4Params);
970 $params['id'] = $activityId;
971 $params['assignee_contact_id'] = array($contact3 => $contact3);
972 $params['target_contact_id'] = array($contact4 => $contact4);
974 $result = $this->callAPISuccess('activity', 'create', $params);
976 $this->assertEquals($activityId, $result['id'], ' in line ' . __LINE__
);
978 $result = $this->callAPISuccess(
982 'return.assignee_contact_id' => 1,
983 'return.target_contact_id' => 1,
984 'return.source_contact_id' => 1,
985 'id' => $result['id']
989 $assignee = $result['values'][$result['id']]['assignee_contact_id'];
990 $target = $result['values'][$result['id']]['target_contact_id'];
992 $this->assertEquals(1, count($assignee), ' in line ' . __LINE__
);
993 $this->assertEquals(1, count($target), ' in line ' . __LINE__
);
994 $this->assertEquals(TRUE, in_array($contact3, $assignee), ' in line ' . __LINE__
);
995 $this->assertEquals(TRUE, in_array($contact4, $target), ' in line ' . __LINE__
);
997 foreach ($this->_params
as $fld => $val) {
998 $this->assertEquals($val, $result['values'][$result['id']][$fld]);
1003 * Test civicrm_activity_update() where the DB has a date_time
1004 * value and there is none in the update params.
1006 public function testActivityUpdateNotDate() {
1007 $result = $this->callAPISuccess('activity', 'create', $this->_params
);
1010 'id' => $result['id'],
1011 'subject' => 'Make-it-Happen Meeting',
1013 'location' => '21, Park Avenue',
1014 'details' => 'Lets update Meeting',
1016 'source_contact_id' => $this->_contactID
,
1020 $result = $this->callAPISuccess('activity', 'create', $params);
1021 //hack on date comparison - really we should make getAndCheck smarter to handle dates
1022 $params['activity_date_time'] = $this->_params
['activity_date_time'];
1023 // we also unset source_contact_id since it is stored in an aux table
1024 unset($params['source_contact_id']);
1025 $this->getAndCheck($params, $result['id'], 'activity');
1029 * Check activity update with status
1031 public function testActivityUpdateWithStatus() {
1032 $activity = $this->callAPISuccess('activity', 'create', $this->_params
);
1034 'id' => $activity['id'],
1035 'source_contact_id' => $this->_contactID
,
1036 'subject' => 'Hurry update works',
1038 'activity_name' => 'Test activity type',
1041 $result = $this->callAPISuccess('activity', 'create', $params);
1042 $this->assertEquals($result['id'], $activity['id']);
1043 $this->assertEquals($result['values'][$activity['id']]['subject'], 'Hurry update works');
1044 $this->assertEquals($result['values'][$activity['id']]['status_id'], 1
1049 * Test civicrm_activity_update() where the source_contact_id
1050 * is not in the update params.
1052 public function testActivityUpdateKeepSource() {
1053 $activity = $this->callAPISuccess('activity', 'create', $this->_params
);
1054 // Updating the activity but not providing anything for the source contact
1055 // (It was set as $this->_contactID earlier.)
1057 'id' => $activity['id'],
1058 'subject' => 'Updated Make-it-Happen Meeting',
1060 'location' => '21, Park Avenue',
1061 'details' => 'Lets update Meeting',
1063 'activity_name' => 'Test activity type',
1067 $result = $this->callAPISuccess('activity', 'create', $params);
1068 $findactivity = $this->callAPISuccess('Activity', 'Get', array('id' => $activity['id']));
1072 * Test civicrm_activities_contact_get()
1074 public function testActivitiesContactGet() {
1075 $activity = $this->callAPISuccess('activity', 'create', $this->_params
);
1076 $activity2 = $this->callAPISuccess('activity', 'create', $this->_params2
);
1077 // Get activities associated with contact $this->_contactID
1079 'contact_id' => $this->_contactID
,
1081 $result = $this->callAPISuccess('activity', 'get', $params);
1083 $this->assertEquals(2, $result['count'], 'In line ' . __LINE__
);
1084 $this->assertEquals($this->test_activity_type_value
, $result['values'][$activity['id']]['activity_type_id'], 'In line ' . __LINE__
);
1085 $this->assertEquals('Test activity type', $result['values'][$activity['id']]['activity_name'], 'In line ' . __LINE__
);
1086 $this->assertEquals('Test activity type', $result['values'][$activity2['id']]['activity_name'], 'In line ' . __LINE__
);
1089 * test chained Activity format
1091 public function testchainedActivityGet() {
1093 $activity = $this->callAPISuccess('Contact', 'Create', array(
1094 'display_name' => "bob brown",
1095 'contact_type' => 'Individual',
1096 'api.activity_type.create' => array(
1098 'label' => 'send out letters',
1103 ), 'api.activity.create' => array('subject' => 'send letter', 'activity_type_id' => '$value.api.activity_type.create.values.0.value'),
1106 $result = $this->callAPISuccess('Activity', 'Get', array(
1107 'id' => $activity['id'],
1108 'return.assignee_contact_id' => 1,
1109 'api.contact.get' => array('api.pledge.get' => 1),
1114 * Test civicrm_activity_contact_get() with invalid Contact Id
1116 public function testActivitiesContactGetWithInvalidContactId() {
1117 $params = array('contact_id' => 'contact');
1118 $result = $this->callAPIFailure('activity', 'get', $params);
1122 * Test civicrm_activity_contact_get() with contact having no Activity
1124 public function testActivitiesContactGetHavingNoActivity() {
1126 'first_name' => 'dan',
1127 'last_name' => 'conberg',
1128 'email' => 'dan.conberg@w.co.in',
1129 'contact_type' => 'Individual',
1132 $contact = $this->callAPISuccess('contact', 'create', $params);
1134 'contact_id' => $contact['id'],
1136 $result = $this->callAPISuccess('activity', 'get', $params);
1137 $this->assertEquals($result['count'], 0, 'in line ' . __LINE__
);
1140 public function testGetFields() {
1141 $params = array('action' => 'create');
1142 $result = $this->callAPIAndDocument('activity', 'getfields', $params, __FUNCTION__
, __FILE__
, NULL, NULL, 'getfields');
1143 $this->assertTrue(is_array($result['values']), 'get fields doesnt return values array in line ' . __LINE__
);
1144 // $this->assertTrue(is_array($result['values']['priority_id']['options']));
1145 foreach ($result['values'] as $key => $value) {
1146 $this->assertTrue(is_array($value), $key . " is not an array in line " . __LINE__
);