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 public $_eNoticeCompliant = TRUE;
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
60 $tablesToTruncate = array(
63 'civicrm_custom_group',
64 'civicrm_custom_field',
67 $this->quickCleanup($tablesToTruncate);
71 // Insert a row in civicrm_contact creating contact 17
72 $op = new PHPUnit_Extensions_Database_Operation_Insert();
73 $op->execute($this->_dbconn
,
74 new PHPUnit_Extensions_Database_DataSet_XMLDataSet(
75 dirname(__FILE__
) . '/dataset/contact_17.xml'
79 //create activity types
80 $activityTypes = civicrm_api('option_value', 'create', array(
81 'version' => API_LATEST_VERSION
, 'option_group_id' => 2,
82 'name' => 'Test activity type',
83 'label' => 'Test activity type',
86 $this->test_activity_type_value
= $activityTypes['values'][0]['value'];
87 $this->test_activity_type_id
= $activityTypes['id'];
88 $this->_params
= array(
89 'source_contact_id' => 17,
90 'activity_type_id' => $this->test_activity_type_value
,
91 'subject' => 'test activity type id',
92 'activity_date_time' => '2011-06-02 14:36:13',
96 'location' => 'Pensulvania',
97 'details' => 'a test activity',
98 'version' => $this->_apiversion
,
100 $this->_params2
= array(
101 'source_contact_id' => 17,
102 'subject' => 'Eat & drink',
103 'activity_date_time' => date('Ymd'),
105 'location' => 'Napier',
106 'details' => 'discuss & eat',
108 'activity_type_id' => $this->test_activity_type_value
,
109 'version' => $this->_apiversion
,
111 // create a logged in USER since the code references it for source_contact_id
112 $this->createLoggedInUser();
116 * Tears down the fixture, for example, closes a network connection.
117 * This method is called after a test is executed.
121 function tearDown() {
122 $tablesToTruncate = array(
125 'civicrm_activity_target',
126 'civicrm_activity_assignment',
128 $this->quickCleanup($tablesToTruncate, TRUE);
129 civicrm_api('option_value', 'delete', array('version' => 3, 'id' => $this->test_activity_type_id
));
133 * check with empty array
135 function testActivityCreateEmpty() {
136 $params = array('version' => $this->_apiversion
);
137 $result = civicrm_api('activity', 'create', $params);
138 $this->assertEquals($result['is_error'], 1,
139 "In line " . __LINE__
144 * check if required fields are not passed
146 function testActivityCreateWithoutRequired() {
148 'subject' => 'this case should fail',
149 'scheduled_date_time' => date('Ymd'),
150 'version' => $this->_apiversion
,
153 $result = civicrm_api('activity', 'create', $params);
154 $this->assertEquals($result['is_error'], 1,
155 "In line " . __LINE__
160 * Test civicrm_activity_create() with mismatched activity_type_id
163 function testActivityCreateMismatchNameType() {
165 'source_contact_id' => 17,
166 'subject' => 'Test activity',
167 'activity_date_time' => date('Ymd'),
169 'location' => 'Pensulvania',
170 'details' => 'a test activity',
172 'activity_name' => 'Fubar activity type',
173 'activity_type_id' => 5,
174 'scheduled_date_time' => date('Ymd'),
175 'version' => $this->_apiversion
,
178 $result = civicrm_api('activity', 'create', $params);
179 $this->assertEquals($result['is_error'], 1,
180 "In line " . __LINE__
185 * Test civicrm_activity_id() with missing source_contact_id is put with the current user.
187 function testActivityCreateWithMissingContactId() {
189 'subject' => 'Make-it-Happen Meeting',
190 'activity_date_time' => date('Ymd'),
192 'location' => 'Pensulvania',
193 'details' => 'a test activity',
195 'activity_name' => 'Test activity type',
196 'version' => $this->_apiversion
,
199 $result = civicrm_api('activity', 'create', $params);
201 // we should use the session contact ID, CRM-8180
202 $this->assertAPISuccess($result);
206 * Test civicrm_activity_id() with non-numeric source_contact_id
208 function testActivityCreateWithNonNumericContactId() {
210 'source_contact_id' => 'fubar',
211 'subject' => 'Make-it-Happen Meeting',
212 'activity_date_time' => date('Ymd'),
214 'location' => 'Pensulvania',
215 'details' => 'a test activity',
217 'activity_name' => 'Test activity type',
218 'version' => $this->_apiversion
,
221 $result = civicrm_api('activity', 'create', $params);
223 $this->assertEquals($result['is_error'], 1,
224 "In line " . __LINE__
229 * Test civicrm_activity_id() with non-numeric duration
230 * @todo Come back to this in later stages
232 /// we don't offer single parameter correctness checking at the moment
233 //function testActivityCreateWithNonNumericDuration( )
236 // 'source_contact_id' => 17,
237 // 'subject' => 'Discussion on Apis for v3',
238 // 'activity_date_time' => date('Ymd'),
239 // 'duration' => 'fubar',
240 // 'location' => 'Pensulvania',
241 // 'details' => 'a test activity',
243 // 'activity_name' => 'Test activity type'
246 // $result = civicrm_activity_create($params);
248 // $this->assertEquals( $result['is_error'], 1,
249 // "In line " . __LINE__ );
253 * check with incorrect required fields
255 function testActivityCreateWithNonNumericActivityTypeId() {
257 'source_contact_id' => 17,
258 'subject' => 'Make-it-Happen Meeting',
259 'activity_date_time' => date('Ymd'),
261 'location' => 'Pensulvania',
262 'details' => 'a test activity',
264 'activity_type_id' => 'Test activity type',
265 'version' => $this->_apiversion
,
268 $result = civicrm_api('activity', 'create', $params);
270 $this->assertEquals($result['is_error'], 1,
271 "In line " . __LINE__
276 * check with incorrect required fields
278 function testActivityCreateWithUnknownActivityTypeId() {
280 'source_contact_id' => 17,
281 'subject' => 'Make-it-Happen Meeting',
282 'activity_date_time' => date('Ymd'),
284 'location' => 'Pensulvania',
285 'details' => 'a test activity',
287 'activity_type_id' => 699,
288 'version' => $this->_apiversion
,
291 $result = civicrm_api('activity', 'create', $params);
293 $this->assertEquals($result['is_error'], 1,
294 "In line " . __LINE__
298 function testActivityCreateWithInvalidPriority() {
300 'source_contact_id' => 17,
301 'subject' => 'Make-it-Happen Meeting',
302 'activity_date_time' => date('Ymd'),
304 'location' => 'Pensulvania',
305 'details' => 'a test activity',
308 'activity_type_id' => 1,
309 'version' => $this->_apiversion
,
312 $result = civicrm_api('activity', 'create', $params);
313 $this->assertEquals($result['is_error'], 1, "In line " . __LINE__
);
314 $this->assertEquals('priority_id is not valid', $result['error_message']);
315 $this->assertEquals(2001,$result['error_code']);
316 $this->assertEquals('priority_id',$result['error_field']);
319 function testActivityCreateWithValidStringPriority() {
321 'source_contact_id' => 17,
322 'subject' => 'Make-it-Happen Meeting',
323 'activity_date_time' => date('Ymd'),
325 'location' => 'Pensulvania',
326 'details' => 'a test activity',
328 'priority_id' => 'Urgent',
329 'activity_type_id' => 1,
330 'version' => $this->_apiversion
,
333 $result = civicrm_api('activity', 'create', $params);
334 $this->assertEquals($result['is_error'], 0, "In line " . __LINE__
);
335 $this->assertEquals(1, $result['values'][$result['id']]['priority_id']);
338 function testActivityCreateWithInValidStringPriority() {
340 'source_contact_id' => 17,
341 'subject' => 'Make-it-Happen Meeting',
342 'activity_date_time' => date('Ymd'),
344 'location' => 'Pensulvania',
345 'details' => 'a test activity',
347 'priority_id' => 'ergUrgent',
348 'activity_type_id' => 1,
349 'version' => $this->_apiversion
,
352 $result = civicrm_api('activity', 'create', $params);
353 $this->assertEquals($result['is_error'], 1, "In line " . __LINE__
);
354 $this->assertEquals('priority_id ergUrgent is not valid', $result['error_message']);
358 * Test civicrm_activity_create() with valid parameters
360 function testActivityCreate() {
362 $result = civicrm_api('activity', 'create', $this->_params
);
363 $this->assertAPISuccess($result, ' in line ' . __LINE__
);
364 $result = civicrm_api('activity', 'get', $this->_params
);
365 $this->assertAPISuccess($result, ' in line ' . __LINE__
);
366 $this->assertEquals($result['values'][$result['id']]['source_contact_id'], 17, 'in line ' . __LINE__
);
367 $this->assertEquals($result['values'][$result['id']]['duration'], 120, 'in line ' . __LINE__
);
368 $this->assertEquals($result['values'][$result['id']]['subject'], 'test activity type id', 'in line ' . __LINE__
);
369 $this->assertEquals($result['values'][$result['id']]['activity_date_time'], '2011-06-02 14:36:13', 'in line ' . __LINE__
);
370 $this->assertEquals($result['values'][$result['id']]['location'], 'Pensulvania', 'in line ' . __LINE__
);
371 $this->assertEquals($result['values'][$result['id']]['details'], 'a test activity', 'in line ' . __LINE__
);
372 $this->assertEquals($result['values'][$result['id']]['status_id'], 2, 'in line ' . __LINE__
);
373 $this->assertEquals($result['values'][$result['id']]['id'], $result['id'], 'in line ' . __LINE__
);
377 * Test civicrm_activity_create() with valid parameters - use type_id
379 function testActivityCreateCampaignTypeID() {
380 // force reload of config object
381 $config = CRM_Core_Config
::singleton(TRUE, TRUE);
383 require_once 'CRM/Core/BAO/ConfigSetting.php';
384 CRM_Core_BAO_ConfigSetting
::enableComponent('CiviCampaign');
385 //flush cache by calling with reset
386 $activityTypes = CRM_Core_PseudoConstant
::activityType(TRUE, TRUE, TRUE, 'name', TRUE);
391 'source_contact_id' => 17,
392 'subject' => 'Make-it-Happen Meeting',
393 'activity_date_time' => '20110316',
395 'location' => 'Pensulvania',
396 'details' => 'a test activity',
398 'activity_type_id' => 29,
399 'version' => $this->_apiversion
,
403 $result = civicrm_api('activity', 'create', $params);
404 //todo test target & assignee are set
405 $this->assertEquals($result['is_error'], 0,
406 "Error message: " . CRM_Utils_Array
::value('error_message', $result) . ' in line ' . __LINE__
409 $this->assertEquals($result['values'][$result['id']]['source_contact_id'], 17, 'in line ' . __LINE__
);
410 $result = civicrm_api('activity', 'get', array('id' => $result['id'], 'version' => $this->_apiversion
));
411 $this->assertEquals($result['values'][$result['id']]['source_contact_id'], 17, 'in line ' . __LINE__
);
413 $this->assertEquals($result['values'][$result['id']]['duration'], 120, 'in line ' . __LINE__
);
414 $this->assertEquals($result['values'][$result['id']]['subject'], 'Make-it-Happen Meeting', 'in line ' . __LINE__
);
415 $this->assertEquals($result['values'][$result['id']]['activity_date_time'], '2011-03-16 00:00:00', 'in line ' . __LINE__
);
416 $this->assertEquals($result['values'][$result['id']]['location'], 'Pensulvania', 'in line ' . __LINE__
);
417 $this->assertEquals($result['values'][$result['id']]['details'], 'a test activity', 'in line ' . __LINE__
);
418 $this->assertEquals($result['values'][$result['id']]['status_id'], 1, 'in line ' . __LINE__
);
421 function testActivityReturnTargetAssignee() {
423 $description = "Example demonstrates setting & retrieving the target & source";
424 $subfile = "GetTargetandAssignee";
426 'source_contact_id' => 17,
427 'subject' => 'Make-it-Happen Meeting',
428 'activity_date_time' => '20110316',
430 'location' => 'Pensulvania',
431 'details' => 'a test activity',
433 'activity_type_id' => 1,
434 'version' => $this->_apiversion
,
436 'target_contact_id' => 17,
437 'assignee_contact_id' => 17,
440 $result = civicrm_api('activity', 'create', $params);
441 //todo test target & assignee are set
442 $this->assertEquals($result['is_error'], 0,
443 "Error message: " . CRM_Utils_Array
::value('error_message', $result) . ' in line ' . __LINE__
446 $this->documentMe($params, $result, __FUNCTION__
, __FILE__
, $description, $subfile);
447 $result = civicrm_api('activity', 'get', array('id' => $result['id'], 'version' => $this->_apiversion
, 'return.assignee_contact_id' => 1, 'return.target_contact_id' => 1));
449 $this->assertEquals(17, $result['values'][$result['id']]['assignee_contact_id'][0], 'in line ' . __LINE__
);
450 $this->assertEquals(17, $result['values'][$result['id']]['target_contact_id'][0], 'in line ' . __LINE__
);
453 function testActivityCreateExample() {
456 * Test civicrm_activity_create() using example code
458 require_once 'api/v3/examples/ActivityCreate.php';
459 $result = activity_create_example();
460 $expectedResult = activity_create_expectedresult();
461 $this->assertEquals($result, $expectedResult);
465 * Test civicrm_activity_create() with valid parameters for unique fields -
466 * set up to see if unique fields work but activity_subject doesn't
468 function testActivityCreateUniqueName( )
470 $this->markTestSkipped('test to see if api will take unique names but it doesn\'t yet');
471 /*fields with unique names activity_id,
472 * activity_subject,activity_duration
473 * activity_location, activity_status_id
478 'source_contact_id' => 17,
479 'activity_subject' => 'Make-it-Happen Meeting',
480 'activity_date_time' => date('Ymd'),
481 'activity_duration' => 120,
482 'activity_location' => 'Pensulvania',
483 'details' => 'a test activity',
484 'activity_status_id' => 1,
485 'activity_name' => 'Test activity type',
486 'version' => $this->_apiversion,
489 $result = civicrm_api('activity', 'create' , $params );
490 $this->assertEquals( $result['is_error'], 0,
491 "Error message: " . CRM_Utils_Array::value( 'error_message', $result ) );
493 $this->assertEquals( $result['values'][$result['id']]['source_contact_id'], 17 );
494 $this->assertEquals( $result['values'][$result['id']]['duration'], 120 );
495 // This field gets lost
496 $this->assertEquals( $result['values'][$result['id']]['subject'], 'Make-it-Happen Meeting' );
497 $this->assertEquals( $result['values'][$result['id']]['activity_date_time'], date('Ymd') . '000000' );
498 $this->assertEquals( $result['values'][$result['id']]['location'], 'Pensulvania' );
499 $this->assertEquals( $result['values'][$result['id']]['details'], 'a test activity' );
500 $this->assertEquals( $result['values'][$result['id']]['status_id'], 1 );
506 * Test civicrm_activity_create() with valid parameters
507 * and some custom data
509 function testActivityCreateCustom() {
510 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__
, __FILE__
);
511 $params = $this->_params
;
512 $params['custom_' . $ids['custom_field_id']] = "custom string";
513 $result = civicrm_api($this->_entity
, 'create', $params);
514 $this->assertAPISuccess($result, ' in line ' . __LINE__
);
515 $result = civicrm_api($this->_entity
, 'get', array('return.custom_' . $ids['custom_field_id'] => 1, 'version' => 3, 'id' => $result['id']));
516 $this->assertEquals("custom string", $result['values'][$result['id']]['custom_' . $ids['custom_field_id']], ' in line ' . __LINE__
);
518 $this->customFieldDelete($ids['custom_field_id']);
519 $this->customGroupDelete($ids['custom_group_id']);
523 * Test civicrm_activity_create() with valid parameters
524 * and some custom data
526 function testActivityCreateCustomContactRefField() {
528 civicrm_api('contact', 'create', array('version' => 3, 'id' => 17, 'sort_name' => 'Contact, Test'));
529 $subfile = 'ContactRefCustomField';
530 $description = "demonstrates create with Contact Reference Custom Field";
531 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__
, __FILE__
);
533 'custom_group_id' => $ids['custom_group_id'],
534 'name' => 'Worker_Lookup',
535 'label' => 'Worker Lookup',
536 'html_type' => 'Autocomplete-Select',
537 'data_type' => 'ContactReference',
539 'is_searchable' => 1,
541 'version' => $this->_apiversion
,
544 $customField = civicrm_api('custom_field', 'create', $params);
545 $params = $this->_params
;
546 $params['custom_' . $customField['id']] = "17";
548 $result = civicrm_api($this->_entity
, 'create', $params);
549 $this->documentMe($params, $result, __FUNCTION__
, __FILE__
, $description, $subfile);
550 $this->assertAPISuccess($result, ' in line ' . __LINE__
);
551 $result = civicrm_api($this->_entity
, 'get', array('return.custom_' . $customField => 1, 'version' => 3, 'id' => $result['id']));
552 $this->documentMe($params, $result, __FUNCTION__
, __FILE__
, 'Get with Contact Ref Custom Field', 'ContactRefCustomFieldGet');
554 $this->assertEquals('Contact, Test', $result['values'][$result['id']]['custom_' . $customField['id']], ' in line ' . __LINE__
);
555 $this->assertEquals(17, $result['values'][$result['id']]['custom_' . $customField['id'] . "_id"], ' in line ' . __LINE__
);
556 $this->assertEquals('Contact, Test', $result['values'][$result['id']]['custom_' . $customField['id'] . '_1'], ' in line ' . __LINE__
);
557 $this->assertEquals(17, $result['values'][$result['id']]['custom_' . $customField['id'] . "_1_id"], ' in line ' . __LINE__
);
558 $this->customFieldDelete($ids['custom_field_id']);
559 $this->customGroupDelete($ids['custom_group_id']);
563 * Test civicrm_activity_create() with an invalid text status_id
565 function testActivityCreateBadTextStatus() {
568 'source_contact_id' => 17,
569 'subject' => 'Discussion on Apis for v3',
570 'activity_date_time' => date('Ymd'),
572 'location' => 'Pensulvania',
573 'details' => 'a test activity',
574 'status_id' => 'Invalid',
575 'activity_name' => 'Test activity type',
576 'version' => $this->_apiversion
,
579 $result = civicrm_api('activity', 'create', $params);
581 $this->assertEquals($result['is_error'], 1,
582 "In line " . __LINE__
587 * Test civicrm_activity_create() with valid parameters,
588 * using a text status_id
590 function testActivityCreateTextStatus() {
594 'source_contact_id' => 17,
595 'subject' => 'Make-it-Happen Meeting',
596 'activity_date_time' => date('Ymd'),
598 'location' => 'Pensulvania',
599 'details' => 'a test activity',
600 'status_id' => 'Scheduled',
601 'activity_name' => 'Test activity type',
602 'version' => $this->_apiversion
,
605 $result = civicrm_api('activity', 'create', $params);
606 $this->assertEquals($result['is_error'], 0,
607 "Error message: " . CRM_Utils_Array
::value('error_message', $result) . ' in line ' . __LINE__
609 $this->assertEquals($result['values'][$result['id']]['source_contact_id'], 17, 'in line ' . __LINE__
);
610 $this->assertEquals($result['values'][$result['id']]['duration'], 120, 'in line ' . __LINE__
);
611 $this->assertEquals($result['values'][$result['id']]['subject'], 'Make-it-Happen Meeting', 'in line ' . __LINE__
);
612 $this->assertEquals($result['values'][$result['id']]['activity_date_time'], date('Ymd') . '000000', 'in line ' . __LINE__
);
613 $this->assertEquals($result['values'][$result['id']]['location'], 'Pensulvania', 'in line ' . __LINE__
);
614 $this->assertEquals($result['values'][$result['id']]['details'], 'a test activity', 'in line ' . __LINE__
);
615 $this->assertEquals($result['values'][$result['id']]['status_id'], 'Scheduled', 'in line ' . __LINE__
);
619 * Test civicrm_activity_get() with no params
621 function testActivityGetEmpty() {
622 $params = array('version' => $this->_apiversion
);
623 $result = civicrm_api('activity', 'get', $params);
624 $this->assertEquals(0, $result['is_error'], 'In line ' . __LINE__
);
628 * Test civicrm_activity_get() with a good activity ID
630 function testActivityGetGoodID1() {
631 // Insert rows in civicrm_activity creating activities 4 and
633 $description = "Function demonstrates getting asignee_contact_id & using it to get the contact";
634 $subfile = 'ReturnAssigneeContact';
635 $activity = civicrm_api('activity', 'create', $this->_params
);
637 $contact = civicrm_api('Contact', 'Create', array(
638 'first_name' => "The Rock",
639 'last_name' =>'roccky',
640 'contact_type' => 'Individual',
642 'api.activity.create' => array(
643 'id' => $activity['id'], 'assignee_contact_id' => '$value.id',
648 'activity_id' => $activity['id'],
649 'version' => $this->_apiversion
,
651 'return.assignee_contact_id' => 1,
652 'api.contact.get' => array(
653 'id' => '$value.source_contact_id',
657 $result = civicrm_api('Activity', 'Get', $params);
658 $this->documentMe($params, $result, __FUNCTION__
, __FILE__
, $description, $subfile);
660 $this->assertAPISuccess($result);
662 $this->assertEquals($activity['id'], $result['id'], 'In line ' . __LINE__
);
663 $this->assertEquals(17, $result['values'][0]['source_contact_id'], 'In line ' . __LINE__
);
665 $this->assertEquals($contact['id'], $result['values'][0]['assignee_contact_id'][0], 'In line ' . __LINE__
);
667 $this->assertEquals(17, $result['values'][0]['api.contact.get']['values'][0]['contact_id'], 'In line ' . __LINE__
);
668 $this->assertEquals($this->test_activity_type_value
, $result['values'][0]['activity_type_id'], 'In line ' . __LINE__
);
669 $this->assertEquals("test activity type id", $result['values'][0]['subject'], 'In line ' . __LINE__
);
673 * test that get functioning does filtering
675 function testGetFilter() {
677 'source_contact_id' => 17,
678 'subject' => 'Make-it-Happen Meeting',
679 'activity_date_time' => '20110316',
681 'location' => 'Pensulvania',
682 'details' => 'a test activity',
684 'activity_name' => 'Test activity type',
685 'version' => $this->_apiversion
,
688 civicrm_api('Activity', 'Create', $params);
689 $result = civicrm_api('Activity', 'Get', array('version' => 3, 'subject' => 'Make-it-Happen Meeting'));
690 $this->assertEquals(1, $result['count']);
691 $this->assertEquals('Make-it-Happen Meeting', $result['values'][$result['id']]['subject']);
692 civicrm_api('Activity', 'Delete', array('version' => 3, 'id' => $result['id']));
695 * test that get functioning does filtering
697 function testGetStatusID() {
699 'source_contact_id' => 17,
700 'subject' => 'Make-it-Happen Meeting',
701 'activity_date_time' => '20110316',
703 'location' => 'Pensulvania',
704 'details' => 'a test activity',
706 'activity_name' => 'Test activity type',
707 'version' => $this->_apiversion
,
710 civicrm_api('Activity', 'Create', $params);
711 $result = civicrm_api('Activity', 'Get', array(
712 'version' => $this->_apiversion
,
713 'activity_status_id' => '1'));
714 $this->assertEquals(1, $result['count'], 'one activity of status 1 should exist');
716 $result = civicrm_api('Activity', 'Get', array(
717 'version' => $this->_apiversion
,
718 'status_id' => '1'));
719 $this->assertEquals(1, $result['count'], 'status_id should also work');
721 $result = civicrm_api('Activity', 'Get', array(
722 'version' => $this->_apiversion
,
723 'activity_status_id' => '2'));
724 $this->assertEquals(0, $result['count'], 'No activities of status 1 should exist');
725 $result = civicrm_api('Activity', 'Get', array(
726 'version' => $this->_apiversion
,
727 'status_id' => '2'));
728 $this->assertEquals(0, $result['count'], 'No activities of status 1 should exist');
734 * test that get functioning does filtering
736 function testGetFilterMaxDate() {
738 'source_contact_id' => 17,
739 'subject' => 'Make-it-Happen Meeting',
740 'activity_date_time' => '20110101',
742 'location' => 'Pensulvania',
743 'details' => 'a test activity',
745 'activity_name' => 'Test activity type',
746 'version' => $this->_apiversion
,
749 $activityOne = civicrm_api('Activity', 'Create', $params);
750 $params['activity_date_time'] = 20120216;
751 $activityTwo = civicrm_api('Activity', 'Create', $params);
752 $result = civicrm_api('Activity', 'Get', array(
755 $description = "demonstrates _low filter (at time of writing doesn't work if contact_id is set";
756 $subfile = "DateTimeLow";
757 $this->assertEquals(2, $result['count']);
760 'filter.activity_date_time_low' => '20120101000000',
763 $result = civicrm_api('Activity', 'Get', $params);
764 $this->documentMe($params, $result, __FUNCTION__
, __FILE__
, $description, $subfile);
765 $this->assertEquals(1, $result['count'], 'in line ' . __LINE__
);
766 $description = "demonstrates _high filter (at time of writing doesn't work if contact_id is set";
767 $subfile = "DateTimeHigh";
768 $this->assertEquals('2012-02-16 00:00:00', $result['values'][0]['activity_date_time'], 'in line ' . __LINE__
);
770 'source_contact_id' => 17,
772 'filter.activity_date_time_high' => '20120101000000',
775 $result = civicrm_api('Activity', 'Get', $params);
776 $this->documentMe($params, $result, __FUNCTION__
, __FILE__
, $description, $subfile);
778 $this->assertEquals(1, $result['count']);
779 $this->assertEquals('2011-01-01 00:00:00', $result['values'][0]['activity_date_time'], 'in line ' . __LINE__
);
781 civicrm_api('Activity', 'Delete', array('version' => 3, 'id' => $activityOne['id']));
782 civicrm_api('Activity', 'Delete', array('version' => 3, 'id' => $activityTwo['id']));
786 * Test civicrm_activity_get() with a good activity ID which
787 * has associated custom data
789 function testActivityGetGoodIDCustom() {
790 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__
, __FILE__
);
792 $params = $this->_params
;
793 $params['custom_' . $ids['custom_field_id']] = "custom string";
795 $result = civicrm_api($this->_entity
, 'create', $params);
796 $this->assertAPISuccess($result, ' in line ' . __LINE__
);
797 // Retrieve the test value
799 'activity_type_id' => $this->test_activity_type_value
,
802 'return.custom_' . $ids['custom_field_id'] => 1,
804 $result = civicrm_api('activity', 'get', $params, TRUE);
805 $this->documentMe($params, $result, __FUNCTION__
, __FILE__
);
806 $this->assertAPISuccess($result, ' in line ' . __LINE__
);
807 $this->assertEquals("custom string", $result['values'][0]['custom_' . $ids['custom_field_id']], ' in line ' . __LINE__
);
810 $this->assertEquals(17, $result['values'][0]['source_contact_id'], 'In line ' . __LINE__
);
811 $this->assertEquals($this->test_activity_type_value
, $result['values'][0]['activity_type_id'], 'In line ' . __LINE__
);
812 $this->assertEquals('test activity type id', $result['values'][0]['subject'], 'In line ' . __LINE__
);
813 $this->customFieldDelete($ids['custom_field_id']);
814 $this->customGroupDelete($ids['custom_group_id']);
818 * Test civicrm_activity_get() with a good activity ID which
819 * has associated custom data
821 function testActivityGetContact_idCustom() {
822 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__
, __FILE__
);
824 $params = $this->_params
;
825 $params['custom_' . $ids['custom_field_id']] = "custom string";
827 $result = civicrm_api($this->_entity
, 'create', $params);
828 // Retrieve the test value
830 'contact_id' => $this->_params
['source_contact_id'],
831 'activity_type_id' => $this->test_activity_type_value
,
834 'return.custom_' . $ids['custom_field_id'] => 1,
836 $result = civicrm_api('activity', 'get', $params, TRUE);
837 $this->assertAPISuccess($result, 'in line ' . __LINE__
);
838 $this->documentMe($params, $result, __FUNCTION__
, __FILE__
);
839 $this->assertEquals(0, $result['is_error'], "Error message: " . CRM_Utils_Array
::value('error_message', $result));
840 $this->assertEquals("custom string", $result['values'][0]['custom_' . $ids['custom_field_id']], ' in line ' . __LINE__
);
842 $this->assertEquals(17, $result['values'][0]['source_contact_id'], 'In line ' . __LINE__
);
843 $this->assertEquals($this->test_activity_type_value
, $result['values'][0]['activity_type_id'], 'In line ' . __LINE__
);
844 $this->assertEquals('test activity type id', $result['values'][0]['subject'], 'In line ' . __LINE__
);
845 $this->assertEquals($result['values'][0]['id'], $result['id'], 'in line ' . __LINE__
);
849 * check activity deletion with empty params
851 function testDeleteActivityForEmptyParams() {
852 $params = array('version' => $this->_apiversion
);
853 $result = civicrm_api('activity', 'delete', $params);
854 $this->assertEquals($result['is_error'], 1,
855 "In line " . __LINE__
860 * check activity deletion without activity id
862 function testDeleteActivityWithoutId() {
864 'activity_name' => 'Meeting',
865 'version' => $this->_apiversion
,
867 $result = civicrm_api('activity', 'delete', $params);
868 $this->assertEquals($result['is_error'], 1,
869 "In line " . __LINE__
874 * check activity deletion without activity type
876 function testDeleteActivityWithoutActivityType() {
877 $params = array('id' => 1);
878 $result = civicrm_api('activity', 'delete', $params);
879 $this->assertEquals($result['is_error'], 1,
880 "In line " . __LINE__
885 * check activity deletion with incorrect data
887 function testDeleteActivityWithIncorrectActivityType() {
890 'activity_name' => 'Test Activity',
893 $result = civicrm_api('activity', 'delete', $params);
894 $this->assertEquals($result['is_error'], 1,
895 "In line " . __LINE__
900 * check activity deletion with correct data
902 function testDeleteActivity() {
903 $result = civicrm_api('activity', 'create', $this->_params
);
905 'id' => $result['id'],
906 'version' => $this->_apiversion
,
909 $result = civicrm_api('activity', 'delete', $params);
910 $this->documentMe($params, $result, __FUNCTION__
, __FILE__
);
911 $this->assertAPISuccess($result);
915 * check with empty array
917 function testActivityUpdateEmpty() {
919 $result = civicrm_api('activity', 'create', $params);
920 $this->assertEquals($result['is_error'], 1,
921 "In line " . __LINE__
926 * check if required fields are not passed
928 function testActivityUpdateWithoutRequired() {
930 'subject' => 'this case should fail',
931 'scheduled_date_time' => date('Ymd'),
934 $result = civicrm_api('activity', 'create', $params);
935 $this->assertEquals($result['is_error'], 1,
936 "In line " . __LINE__
941 * check with incorrect required fields
943 function testActivityUpdateWithIncorrectData() {
945 'activity_name' => 'Meeting',
946 'subject' => 'this case should fail',
947 'scheduled_date_time' => date('Ymd'),
950 $result = civicrm_api('activity', 'create', $params);
951 $this->assertEquals($result['is_error'], 1,
952 "In line " . __LINE__
957 * Test civicrm_activity_update() with non-numeric id
959 function testActivityUpdateWithNonNumericId() {
961 'id' => 'lets break it',
962 'activity_name' => 'Meeting',
963 'subject' => 'this case should fail',
964 'scheduled_date_time' => date('Ymd'),
967 $result = civicrm_api('activity', 'create', $params);
968 $this->assertEquals($result['is_error'], 1,
969 "In line " . __LINE__
974 * check with incorrect required fields
976 function testActivityUpdateWithIncorrectContactActivityType() {
979 'activity_name' => 'Test Activity',
980 'subject' => 'this case should fail',
981 'scheduled_date_time' => date('Ymd'),
982 'version' => $this->_apiversion
,
983 'source_contact_id' => 17,
986 $result = civicrm_api('activity', 'create', $params);
987 $this->assertEquals($result['is_error'], 1, "In line " . __LINE__
);
988 $this->assertEquals($result['error_message'], 'Invalid Activity Id', "In line " . __LINE__
);
992 * Test civicrm_activity_update() to update an existing activity
994 function testActivityUpdate() {
995 $result = civicrm_api('activity', 'create', $this->_params
);
998 'id' => $result['id'],
999 'subject' => 'Make-it-Happen Meeting',
1000 'activity_date_time' => '20091011123456',
1002 'location' => '21, Park Avenue',
1003 'details' => 'Lets update Meeting',
1005 'source_contact_id' => 17,
1007 'version' => $this->_apiversion
,
1010 $result = civicrm_api('activity', 'create', $params);
1011 $this->assertAPISuccess($result, 'in line ' . __LINE__
);
1012 //hack on date comparison - really we should make getAndCheck smarter to handle dates
1013 $params['activity_date_time'] = '2009-10-11 12:34:56';
1014 $this->getAndCheck($params, $result['id'], 'activity');
1018 * Test civicrm_activity_update() with valid parameters
1019 * and some custom data
1021 function testActivityUpdateCustom() {
1022 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__
, __FILE__
);
1024 $params = $this->_params
;
1026 // Create an activity with custom data
1027 //this has been updated from the previous 'old format' function - need to make it work
1029 'source_contact_id' => 17,
1030 'subject' => 'Make-it-Happen Meeting',
1031 'activity_date_time' => '2009-10-18',
1033 'location' => 'Pensulvania',
1034 'details' => 'a test activity to check the update api',
1036 'activity_name' => 'Test activity type',
1037 'version' => $this->_apiversion
,
1038 'custom_' . $ids['custom_field_id'] => 'custom string',
1040 $result = civicrm_api('activity', 'create', $params);
1042 $activityId = $result['id'];
1043 $this->assertEquals(0, $result['is_error'],
1044 "Error message: " . CRM_Utils_Array
::value('error_message', $result)
1046 $result = civicrm_api($this->_entity
, 'get', array('return.custom_' . $ids['custom_field_id'] => 1, 'version' => 3, 'id' => $result['id']));
1047 $this->assertEquals("custom string", $result['values'][$result['id']]['custom_' . $ids['custom_field_id']], ' in line ' . __LINE__
);
1048 $this->assertEquals("2009-10-18 00:00:00", $result['values'][$result['id']]['activity_date_time'], ' in line ' . __LINE__
);
1049 $fields = civicrm_api('activity', 'getfields', array('version' => $this->_apiversion
));
1050 $this->assertTrue(is_array($fields['values']['custom_' . $ids['custom_field_id']]));
1052 // Update the activity with custom data
1054 'id' => $activityId,
1055 'source_contact_id' => 17,
1056 'subject' => 'Make-it-Happen Meeting',
1058 'activity_name' => 'Test activity type',
1059 // add this since dates are messed up
1060 'activity_date_time' => date('Ymd'),
1061 'custom_' . $ids['custom_field_id'] => 'Updated my test data',
1062 'version' => $this->_apiversion
,
1064 $result = civicrm_api('Activity', 'Create', $params);
1065 $this->assertEquals(0, $result['is_error'],
1066 "Error message: " . CRM_Utils_Array
::value('error_message', $result)
1069 $result = civicrm_api($this->_entity
, 'get', array('return.custom_' . $ids['custom_field_id'] => 1, 'version' => 3, 'id' => $result['id']));
1070 $this->assertEquals("Updated my test data", $result['values'][$result['id']]['custom_' . $ids['custom_field_id']], ' in line ' . __LINE__
);
1074 * Test civicrm_activity_update() for core activity fields
1075 * and some custom data
1077 function testActivityUpdateCheckCoreFields() {
1078 $params = $this->_params
;
1079 $contact1Params = array(
1080 'first_name' => 'John',
1081 'middle_name' => 'J.',
1082 'last_name' => 'Anderson',
1085 'email' => 'john_anderson@civicrm.org',
1086 'contact_type' => 'Individual',
1089 $contact1 = $this->individualCreate($contact1Params);
1090 $contact2Params = array(
1091 'first_name' => 'Michal',
1092 'middle_name' => 'J.',
1093 'last_name' => 'Anderson',
1096 'email' => 'michal_anderson@civicrm.org',
1097 'contact_type' => 'Individual',
1100 $contact2 = $this->individualCreate($contact2Params);
1102 $params['assignee_contact_id'] = array($contact1, $contact2);
1103 $params['target_contact_id'] = array($contact2 => $contact2);
1104 $result = civicrm_api('Activity', 'Create', $params);
1106 $result = civicrm_api('activity', 'create', $params);
1107 $activityId = $result['id'];
1108 $this->assertAPISuccess($result);
1110 'return.assignee_contact_id' => 1,
1111 'return.target_contact_id' => 1,
1112 'version' => $this->_apiversion
,
1113 'id' => $activityId,
1115 $result = civicrm_api($this->_entity
, 'get',$getParams );
1116 $assignee = $result['values'][$result['id']]['assignee_contact_id'];
1117 $target = $result['values'][$result['id']]['target_contact_id'];
1118 $this->assertEquals(2, count($assignee), ' in line ' . __LINE__
);
1119 $this->assertEquals(1, count($target), ' in line ' . __LINE__
);
1120 $this->assertEquals(TRUE, in_array($contact1, $assignee), ' in line ' . __LINE__
);
1121 $this->assertEquals(TRUE, in_array($contact2, $target), ' in line ' . __LINE__
);
1123 $contact3Params = array(
1124 'first_name' => 'Jijo',
1125 'middle_name' => 'J.',
1126 'last_name' => 'Anderson',
1129 'email' => 'jijo_anderson@civicrm.org',
1130 'contact_type' => 'Individual',
1133 $contact4Params = array(
1134 'first_name' => 'Grant',
1135 'middle_name' => 'J.',
1136 'last_name' => 'Anderson',
1139 'email' => 'grant_anderson@civicrm.org',
1140 'contact_type' => 'Individual',
1143 $contact3 = $this->individualCreate($contact3Params);
1144 $contact4 = $this->individualCreate($contact4Params);
1147 $params['id'] = $activityId;
1148 $params['version'] = $this->_apiversion
;
1149 $params['assignee_contact_id'] = array($contact3 => $contact3);
1150 $params['target_contact_id'] = array($contact4 => $contact4);
1152 $result = civicrm_api('activity', 'create', $params);
1153 $this->assertEquals(0, $result['is_error'],
1154 "Error message: " . CRM_Utils_Array
::value('error_message', $result)
1157 $this->assertEquals($activityId, $result['id'], ' in line ' . __LINE__
);
1159 $result = civicrm_api($this->_entity
, 'get', array('return.assignee_contact_id' => 1, 'return.target_contact_id' => 1, 'version' => 3, 'id' => $result['id']));
1161 $assignee = $result['values'][$result['id']]['assignee_contact_id'];
1162 $target = $result['values'][$result['id']]['target_contact_id'];
1164 $this->assertEquals(1, count($assignee), ' in line ' . __LINE__
);
1165 $this->assertEquals(1, count($target), ' in line ' . __LINE__
);
1166 $this->assertEquals(TRUE, in_array($contact3, $assignee), ' in line ' . __LINE__
);
1167 $this->assertEquals(TRUE, in_array($contact4, $target), ' in line ' . __LINE__
);
1169 foreach ($this->_params
as $fld => $val) {
1170 if ($fld == 'version') {
1173 $this->assertEquals($val, $result['values'][$result['id']][$fld], ' in line ' . __LINE__
);
1178 * Test civicrm_activity_update() where the DB has a date_time
1179 * value and there is none in the update params.
1181 function testActivityUpdateNotDate() {
1182 $result = civicrm_api('activity', 'create', $this->_params
);
1185 'id' => $result['id'],
1186 'subject' => 'Make-it-Happen Meeting',
1188 'location' => '21, Park Avenue',
1189 'details' => 'Lets update Meeting',
1191 'source_contact_id' => 17,
1193 'version' => $this->_apiversion
,
1196 $result = civicrm_api('activity', 'create', $params);
1197 $this->assertAPISuccess($result, 'in line ' . __LINE__
);
1198 //hack on date comparison - really we should make getAndCheck smarter to handle dates
1199 $params['activity_date_time'] = $this->_params
['activity_date_time'];
1200 $this->getAndCheck($params, $result['id'], 'activity');
1204 * check activity update with status
1206 function testActivityUpdateWithStatus() {
1207 $activity = civicrm_api('activity', 'create', $this->_params
);
1208 $this->assertAPISuccess($activity, "In line " . __LINE__
);
1210 'id' => $activity['id'],
1211 'source_contact_id' => 17,
1212 'subject' => 'Hurry update works',
1214 'activity_name' => 'Test activity type',
1215 'version' => $this->_apiversion
,
1218 $result = civicrm_api('activity', 'create', $params);
1219 $this->assertAPISuccess($result, "In line " . __LINE__
);
1220 $this->assertEquals($result['id'], $activity['id'], "In line " . __LINE__
);
1221 $this->assertEquals($result['values'][$activity['id']]['source_contact_id'], 17,
1222 "In line " . __LINE__
1224 $this->assertEquals($result['values'][$activity['id']]['subject'], 'Hurry update works',
1225 "In line " . __LINE__
1227 $this->assertEquals($result['values'][$activity['id']]['status_id'], 1,
1228 "In line " . __LINE__
1233 * Test civicrm_activity_update() where the source_contact_id
1234 * is not in the update params.
1236 function testActivityUpdateKeepSource() {
1237 $activity = civicrm_api('activity', 'create', $this->_params
);
1238 // Updating the activity but not providing anything for the source contact
1239 // (It was set as 17 earlier.)
1241 'id' => $activity['id'],
1242 'subject' => 'Updated Make-it-Happen Meeting',
1244 'location' => '21, Park Avenue',
1245 'details' => 'Lets update Meeting',
1247 'activity_name' => 'Test activity type',
1249 'version' => $this->_apiversion
,
1252 $result = civicrm_api('activity', 'create', $params);
1253 $this->assertAPISuccess($result, 'in line ' . __LINE__
);
1254 $findactivity = civicrm_api('Activity', 'Get', array('id' => $activity['id'], 'version' => 3));
1256 $this->assertAPISuccess($findactivity);
1257 $this->assertEquals(17, $findactivity['values'][$findactivity['id']]['source_contact_id'], 'In line ' . __LINE__
);
1261 * Test civicrm_activities_contact_get()
1263 function testActivitiesContactGet() {
1264 $activity = civicrm_api('activity', 'create', $this->_params
);
1265 $activity2 = civicrm_api('activity', 'create', $this->_params2
);
1266 // Get activities associated with contact 17
1269 'version' => $this->_apiversion
,
1271 $result = civicrm_api('activity', 'get', $params);
1273 $this->assertAPISuccess($result, 'in line ' . __LINE__
);
1274 $this->assertEquals(2, $result['count'], 'In line ' . __LINE__
);
1275 $this->assertEquals($this->test_activity_type_value
, $result['values'][$activity['id']]['activity_type_id'], 'In line ' . __LINE__
);
1276 $this->assertEquals('Test activity type', $result['values'][$activity['id']]['activity_name'], 'In line ' . __LINE__
);
1277 $this->assertEquals('Test activity type', $result['values'][$activity2['id']]['activity_name'], 'In line ' . __LINE__
);
1280 * test chained Activity format
1282 function testchainedActivityGet() {
1284 $activity = civicrm_api('Contact', 'Create', array(
1285 'version' => $this->_apiversion
,
1286 'display_name' => "bob brown",
1287 'contact_type' => 'Individual',
1288 'api.activity_type.create' => array(
1290 'label' => 'send out letters',
1295 ), 'api.activity.create' => array('subject' => 'send letter', 'activity_type_id' => '$value.api.activity_type.create.values.0.value'),
1298 $this->assertAPISuccess($activity, 'in line ' . __LINE__
);
1299 $result = civicrm_api('Activity', 'Get', array(
1301 'id' => $activity['id'],
1302 'return.assignee_contact_id' => 1,
1303 'api.contact.get' => array('api.pledge.get' => 1),
1308 * check civicrm_activities_contact_get() with empty array
1310 function testActivityContactGetEmpty() {
1312 $result = civicrm_api('activity', 'get', $params);
1313 $this->assertEquals($result['is_error'], 1, "In line " . __LINE__
);
1317 * Test civicrm_activity_contact_get() with missing source_contact_id
1319 function testActivitiesContactGetWithInvalidParameter() {
1321 $result = civicrm_api('activity', 'get', $params);
1322 $this->assertEquals($result['is_error'], 1,
1323 "In line " . __LINE__
1328 * Test civicrm_activity_contact_get() with invalid Contact Id
1330 function testActivitiesContactGetWithInvalidContactId() {
1331 $params = array('contact_id' => NULL);
1332 $result = civicrm_api('activity', 'get', $params);
1333 $this->assertEquals($result['is_error'], 1,
1334 "In line " . __LINE__
1337 $params = array('contact_id' => 'contact');
1338 $result = civicrm_api('activity', 'get', $params);
1339 $this->assertEquals($result['is_error'], 1,
1340 "In line " . __LINE__
1343 $params = array('contact_id' => 2.4);
1344 $result = civicrm_api('activity', 'get', $params);
1345 $this->assertEquals($result['is_error'], 1,
1346 "In line " . __LINE__
1351 * Test civicrm_activity_contact_get() with contact having no Activity
1353 function testActivitiesContactGetHavingNoActivity() {
1355 'first_name' => 'dan',
1356 'last_name' => 'conberg',
1357 'email' => 'dan.conberg@w.co.in',
1358 'contact_type' => 'Individual',
1359 'version' => $this->_apiversion
,
1362 $contact = civicrm_api('contact', 'create', $params);
1364 'contact_id' => $contact['id'],
1365 'version' => $this->_apiversion
,
1367 $result = civicrm_api('activity', 'get', $params);
1368 $this->assertEquals($result['is_error'], 0, 'in line ' . __LINE__
);
1369 $this->assertEquals($result['count'], 0, 'in line ' . __LINE__
);
1372 function testGetFields() {
1373 $params = array('version' => 3, 'action' => 'create');
1374 $result = civicrm_api('activity', 'getfields', $params);
1375 $this->documentMe($params, $result, __FUNCTION__
, __FILE__
, NULL, NULL, 'getfields');
1376 $this->assertTrue(is_array($result['values']), 'get fields doesnt return values array in line ' . __LINE__
);
1377 // $this->assertTrue(is_array($result['values']['priority_id']['options']));
1378 foreach ($result['values'] as $key => $value) {
1379 $this->assertTrue(is_array($value), $key . " is not an array in line " . __LINE__
);