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_contact',
127 $this->quickCleanup($tablesToTruncate, TRUE);
128 civicrm_api('option_value', 'delete', array('version' => 3, 'id' => $this->test_activity_type_id
));
132 * check with empty array
134 function testActivityCreateEmpty() {
135 $params = array('version' => $this->_apiversion
);
136 $result = $this->callAPIFailure('activity', 'create', $params);
140 * check if required fields are not passed
142 function testActivityCreateWithoutRequired() {
144 'subject' => 'this case should fail',
145 'scheduled_date_time' => date('Ymd'),
146 'version' => $this->_apiversion
,
149 $result = $this->callAPIFailure('activity', 'create', $params);
153 * Test civicrm_activity_create() with mismatched activity_type_id
156 function testActivityCreateMismatchNameType() {
158 'source_contact_id' => 17,
159 'subject' => 'Test activity',
160 'activity_date_time' => date('Ymd'),
162 'location' => 'Pensulvania',
163 'details' => 'a test activity',
165 'activity_name' => 'Fubar activity type',
166 'activity_type_id' => 5,
167 'scheduled_date_time' => date('Ymd'),
168 'version' => $this->_apiversion
,
171 $result = $this->callAPIFailure('activity', 'create', $params);
175 * Test civicrm_activity_id() with missing source_contact_id is put with the current user.
177 function testActivityCreateWithMissingContactId() {
179 'subject' => 'Make-it-Happen Meeting',
180 'activity_date_time' => date('Ymd'),
182 'location' => 'Pensulvania',
183 'details' => 'a test activity',
185 'activity_name' => 'Test activity type',
186 'version' => $this->_apiversion
,
189 $result = civicrm_api('activity', 'create', $params);
190 // we should use the session contact ID, CRM-8180
191 $this->assertAPISuccess($result);
195 * Test civicrm_activity_id() with non-numeric source_contact_id
197 function testActivityCreateWithNonNumericContactId() {
199 'source_contact_id' => 'fubar',
200 'subject' => 'Make-it-Happen Meeting',
201 'activity_date_time' => date('Ymd'),
203 'location' => 'Pensulvania',
204 'details' => 'a test activity',
206 'activity_name' => 'Test activity type',
207 'version' => $this->_apiversion
,
210 $result = civicrm_api('activity', 'create', $params);
212 $this->assertAPIFailure($result,
213 "In line " . __LINE__
218 * Test civicrm_activity_id() with non-numeric duration
219 * @todo Come back to this in later stages
221 /// we don't offer single parameter correctness checking at the moment
222 //function testActivityCreateWithNonNumericDuration( )
225 // 'source_contact_id' => 17,
226 // 'subject' => 'Discussion on Apis for v3',
227 // 'activity_date_time' => date('Ymd'),
228 // 'duration' => 'fubar',
229 // 'location' => 'Pensulvania',
230 // 'details' => 'a test activity',
232 // 'activity_name' => 'Test activity type'
235 // $result = civicrm_activity_create($params);
237 // $this->assertEquals( $result['is_error'], 1,
238 // "In line " . __LINE__ );
242 * Ensure that an invalid activity type causes failure
243 * oddly enough this test was failing because the creation of the invalid type
244 * got added to the set up routine. Probably a mis-fix on a test
246 function testActivityCreateWithNonNumericActivityTypeId() {
248 'source_contact_id' => 17,
249 'subject' => 'Make-it-Happen Meeting',
250 'activity_date_time' => date('Ymd'),
252 'location' => 'Pensulvania',
253 'details' => 'a test activity',
255 'activity_type_id' => 'Invalid Test activity type',
256 'version' => $this->_apiversion
,
259 $result = $this->callAPIFailure('activity', 'create', $params);
263 * check with incorrect required fields
265 function testActivityCreateWithUnknownActivityTypeId() {
267 'source_contact_id' => 17,
268 'subject' => 'Make-it-Happen Meeting',
269 'activity_date_time' => date('Ymd'),
271 'location' => 'Pensulvania',
272 'details' => 'a test activity',
274 'activity_type_id' => 699,
275 'version' => $this->_apiversion
,
278 $result = $this->callAPIFailure('activity', 'create', $params);
281 function testActivityCreateWithInvalidPriority() {
283 'source_contact_id' => 17,
284 'subject' => 'Make-it-Happen Meeting',
285 'activity_date_time' => date('Ymd'),
287 'location' => 'Pensulvania',
288 'details' => 'a test activity',
291 'activity_type_id' => 1,
292 'version' => $this->_apiversion
,
295 $result = $this->callAPIFailure('activity', 'create', $params);
296 $this->assertEquals("'44' is not a valid option for field priority_id", $result['error_message']);
297 $this->assertEquals(2001, $result['error_code']);
298 $this->assertEquals('priority_id', $result['error_field']);
303 function testActivityCreateWithValidStringPriority() {
305 'source_contact_id' => 17,
306 'subject' => 'Make-it-Happen Meeting',
307 'activity_date_time' => date('Ymd'),
309 'location' => 'Pensulvania',
310 'details' => 'a test activity',
312 'priority_id' => 'Urgent',
313 'activity_type_id' => 1,
314 'version' => $this->_apiversion
,
317 $result = civicrm_api('activity', 'create', $params);
318 $this->assertAPISuccess($result);
319 $this->assertEquals(1, $result['values'][$result['id']]['priority_id']);
322 function testActivityCreateWithInValidStringPriority() {
324 'source_contact_id' => 17,
325 'subject' => 'Make-it-Happen Meeting',
326 'activity_date_time' => date('Ymd'),
328 'location' => 'Pensulvania',
329 'details' => 'a test activity',
331 'priority_id' => 'ergUrgent',
332 'activity_type_id' => 1,
333 'version' => $this->_apiversion
,
336 $result = $this->callAPIFailure('activity', 'create', $params);
337 $this->assertEquals("'ergUrgent' is not a valid option for field priority_id", $result['error_message']);
341 * Test civicrm_activity_create() with valid parameters
343 function testActivityCreate() {
345 $result = civicrm_api('activity', 'create', $this->_params
);
346 $this->assertAPISuccess($result, ' in line ' . __LINE__
);
347 $result = civicrm_api('activity', 'get', $this->_params
);
348 $this->assertAPISuccess($result, ' in line ' . __LINE__
);
349 $this->assertEquals($result['values'][$result['id']]['duration'], 120, 'in line ' . __LINE__
);
350 $this->assertEquals($result['values'][$result['id']]['subject'], 'test activity type id', 'in line ' . __LINE__
);
351 $this->assertEquals($result['values'][$result['id']]['activity_date_time'], '2011-06-02 14:36:13', 'in line ' . __LINE__
);
352 $this->assertEquals($result['values'][$result['id']]['location'], 'Pensulvania', 'in line ' . __LINE__
);
353 $this->assertEquals($result['values'][$result['id']]['details'], 'a test activity', 'in line ' . __LINE__
);
354 $this->assertEquals($result['values'][$result['id']]['status_id'], 2, 'in line ' . __LINE__
);
355 $this->assertEquals($result['values'][$result['id']]['id'], $result['id'], 'in line ' . __LINE__
);
359 * Test civicrm_activity_create() with valid parameters - use type_id
361 function testActivityCreateCampaignTypeID() {
362 // force reload of config object
363 $config = CRM_Core_Config
::singleton(TRUE, TRUE);
365 require_once 'CRM/Core/BAO/ConfigSetting.php';
366 CRM_Core_BAO_ConfigSetting
::enableComponent('CiviCampaign');
367 //flush cache by calling with reset
368 $activityTypes = CRM_Core_PseudoConstant
::activityType(TRUE, TRUE, TRUE, 'name', TRUE);
373 'source_contact_id' => 17,
374 'subject' => 'Make-it-Happen Meeting',
375 'activity_date_time' => '20110316',
377 'location' => 'Pensulvania',
378 'details' => 'a test activity',
380 'activity_type_id' => 29,
381 'version' => $this->_apiversion
,
385 $result = civicrm_api('activity', 'create', $params);
386 //todo test target & assignee are set
387 $this->assertAPISuccess($result);
389 //$this->assertEquals($result['values'][$result['id']]['source_contact_id'], 17, 'in line ' . __LINE__);
390 $result = civicrm_api('activity', 'get', array('id' => $result['id'], 'version' => $this->_apiversion
));
391 $this->assertEquals($result['values'][$result['id']]['duration'], 120, 'in line ' . __LINE__
);
392 $this->assertEquals($result['values'][$result['id']]['subject'], 'Make-it-Happen Meeting', 'in line ' . __LINE__
);
393 $this->assertEquals($result['values'][$result['id']]['activity_date_time'], '2011-03-16 00:00:00', 'in line ' . __LINE__
);
394 $this->assertEquals($result['values'][$result['id']]['location'], 'Pensulvania', 'in line ' . __LINE__
);
395 $this->assertEquals($result['values'][$result['id']]['details'], 'a test activity', 'in line ' . __LINE__
);
396 $this->assertEquals($result['values'][$result['id']]['status_id'], 1, 'in line ' . __LINE__
);
399 function testActivityReturnTargetAssignee() {
401 $description = "Example demonstrates setting & retrieving the target & source";
402 $subfile = "GetTargetandAssignee";
404 'source_contact_id' => 17,
405 'subject' => 'Make-it-Happen Meeting',
406 'activity_date_time' => '20110316',
408 'location' => 'Pensulvania',
409 'details' => 'a test activity',
411 'activity_type_id' => 1,
412 'version' => $this->_apiversion
,
414 'target_contact_id' => 17,
415 'assignee_contact_id' => 17,
418 $result = civicrm_api('activity', 'create', $params);
419 //todo test target & assignee are set
420 $this->assertAPISuccess($result);
422 $this->documentMe($params, $result, __FUNCTION__
, __FILE__
, $description, $subfile);
423 $result = civicrm_api('activity', 'get', array('id' => $result['id'], 'version' => $this->_apiversion
, 'return.assignee_contact_id' => 1, 'return.target_contact_id' => 1));
425 $this->assertEquals(17, $result['values'][$result['id']]['assignee_contact_id'][0], 'in line ' . __LINE__
);
426 $this->assertEquals(17, $result['values'][$result['id']]['target_contact_id'][0], 'in line ' . __LINE__
);
429 function testActivityCreateExample() {
431 * Test civicrm_activity_create() using example code
433 require_once 'api/v3/examples/ActivityCreate.php';
434 $result = activity_create_example();
435 $expectedResult = activity_create_expectedresult();
436 $this->assertEquals($result, $expectedResult);
440 * Test civicrm_activity_create() with valid parameters for unique fields -
441 * set up to see if unique fields work but activity_subject doesn't
443 function testActivityCreateUniqueName( )
445 $this->markTestSkipped('test to see if api will take unique names but it doesn\'t yet');
446 /*fields with unique names activity_id,
447 * activity_subject,activity_duration
448 * activity_location, activity_status_id
453 'source_contact_id' => 17,
454 'activity_subject' => 'Make-it-Happen Meeting',
455 'activity_date_time' => date('Ymd'),
456 'activity_duration' => 120,
457 'activity_location' => 'Pensulvania',
458 'details' => 'a test activity',
459 'activity_status_id' => 1,
460 'activity_name' => 'Test activity type',
461 'version' => $this->_apiversion,
464 $result = civicrm_api('activity', 'create' , $params );
465 $this->assertEquals( $result['is_error'], 0,
466 "Error message: " . CRM_Utils_Array::value( 'error_message', $result ) );
468 $this->assertEquals( $result['values'][$result['id']]['duration'], 120 );
469 // This field gets lost
470 $this->assertEquals( $result['values'][$result['id']]['subject'], 'Make-it-Happen Meeting' );
471 $this->assertEquals( $result['values'][$result['id']]['activity_date_time'], date('Ymd') . '000000' );
472 $this->assertEquals( $result['values'][$result['id']]['location'], 'Pensulvania' );
473 $this->assertEquals( $result['values'][$result['id']]['details'], 'a test activity' );
474 $this->assertEquals( $result['values'][$result['id']]['status_id'], 1 );
480 * Test civicrm_activity_create() with valid parameters
481 * and some custom data
483 function testActivityCreateCustom() {
484 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__
, __FILE__
);
485 $params = $this->_params
;
486 $params['custom_' . $ids['custom_field_id']] = "custom string";
487 $result = civicrm_api($this->_entity
, 'create', $params);
488 $this->documentMe($params, $result, __FUNCTION__
, __FILE__
);
489 $this->assertAPISuccess($result, ' in line ' . __LINE__
);
490 $result = civicrm_api($this->_entity
, 'get', array('return.custom_' . $ids['custom_field_id'] => 1, 'version' => 3, 'id' => $result['id']));
491 $this->assertEquals("custom string", $result['values'][$result['id']]['custom_' . $ids['custom_field_id']], ' in line ' . __LINE__
);
493 $this->customFieldDelete($ids['custom_field_id']);
494 $this->customGroupDelete($ids['custom_group_id']);
498 * Test civicrm_activity_create() with valid parameters
499 * and some custom data
501 function testActivityCreateCustomContactRefField() {
503 civicrm_api('contact', 'create', array('version' => 3, 'id' => 17, 'sort_name' => 'Contact, Test'));
504 $subfile = 'ContactRefCustomField';
505 $description = "demonstrates create with Contact Reference Custom Field";
506 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__
, __FILE__
);
508 'custom_group_id' => $ids['custom_group_id'],
509 'name' => 'Worker_Lookup',
510 'label' => 'Worker Lookup',
511 'html_type' => 'Autocomplete-Select',
512 'data_type' => 'ContactReference',
514 'is_searchable' => 1,
516 'version' => $this->_apiversion
,
519 $customField = civicrm_api('custom_field', 'create', $params);
520 $params = $this->_params
;
521 $params['custom_' . $customField['id']] = "17";
523 $result = civicrm_api($this->_entity
, 'create', $params);
524 $this->documentMe($params, $result, __FUNCTION__
, __FILE__
, $description, $subfile);
525 $this->assertAPISuccess($result, ' in line ' . __LINE__
);
526 $result = civicrm_api($this->_entity
, 'get', array('return.custom_' . $customField['id'] => 1, 'version' => 3, 'id' => $result['id']));
527 $this->documentMe($params, $result, __FUNCTION__
, __FILE__
, 'Get with Contact Ref Custom Field', 'ContactRefCustomFieldGet');
529 $this->assertEquals('Contact, Test', $result['values'][$result['id']]['custom_' . $customField['id']], ' in line ' . __LINE__
);
530 $this->assertEquals(17, $result['values'][$result['id']]['custom_' . $customField['id'] . "_id"], ' in line ' . __LINE__
);
531 $this->assertEquals('Contact, Test', $result['values'][$result['id']]['custom_' . $customField['id'] . '_1'], ' in line ' . __LINE__
);
532 $this->assertEquals(17, $result['values'][$result['id']]['custom_' . $customField['id'] . "_1_id"], ' in line ' . __LINE__
);
533 $this->customFieldDelete($ids['custom_field_id']);
534 $this->customGroupDelete($ids['custom_group_id']);
538 * Test civicrm_activity_create() with an invalid text status_id
540 function testActivityCreateBadTextStatus() {
543 'source_contact_id' => 17,
544 'subject' => 'Discussion on Apis for v3',
545 'activity_date_time' => date('Ymd'),
547 'location' => 'Pensulvania',
548 'details' => 'a test activity',
549 'status_id' => 'Invalid',
550 'activity_name' => 'Test activity type',
551 'version' => $this->_apiversion
,
554 $result = $this->callAPIFailure('activity', 'create', $params);
558 * Test civicrm_activity_create() with an invalid text status_id
560 function testActivityCreateSupportActivityStatus() {
563 'source_contact_id' => 17,
564 'subject' => 'Discussion on Apis for v3',
565 'activity_date_time' => date('Ymd'),
567 'location' => 'Pensulvania',
568 'details' => 'a test activity',
569 'activity_status_id' => 'Invalid',
570 'activity_name' => 'Test activity type',
571 'version' => $this->_apiversion
,
574 $result = $this->callAPIFailure('activity', 'create', $params);
575 $this->assertEquals("'Invalid' is not a valid option for field status_id", $result['error_message']);
580 * Test civicrm_activity_create() with valid parameters,
581 * using a text status_id
583 function testActivityCreateTextStatus() {
587 'source_contact_id' => 17,
588 'subject' => 'Make-it-Happen Meeting',
589 'activity_date_time' => date('Ymd'),
591 'location' => 'Pensulvania',
592 'details' => 'a test activity',
593 'status_id' => 'Scheduled',
594 'activity_name' => 'Test activity type',
595 'version' => $this->_apiversion
,
598 $result = civicrm_api('activity', 'create', $params);
599 $this->assertAPISuccess($result);
600 $this->assertEquals($result['values'][$result['id']]['duration'], 120, 'in line ' . __LINE__
);
601 $this->assertEquals($result['values'][$result['id']]['subject'], 'Make-it-Happen Meeting', 'in line ' . __LINE__
);
602 $this->assertEquals($result['values'][$result['id']]['activity_date_time'], date('Ymd') . '000000', 'in line ' . __LINE__
);
603 $this->assertEquals($result['values'][$result['id']]['location'], 'Pensulvania', 'in line ' . __LINE__
);
604 $this->assertEquals($result['values'][$result['id']]['details'], 'a test activity', 'in line ' . __LINE__
);
608 * Test civicrm_activity_get() with no params
610 function testActivityGetEmpty() {
611 $params = array('version' => $this->_apiversion
);
612 $result = civicrm_api('activity', 'get', $params);
613 $this->assertAPISuccess($result);
617 * Test civicrm_activity_get() with a good activity ID
619 function testActivityGetGoodID1() {
620 // Insert rows in civicrm_activity creating activities 4 and
622 $description = "Function demonstrates getting asignee_contact_id & using it to get the contact";
623 $subfile = 'ReturnAssigneeContact';
624 $activity = civicrm_api('activity', 'create', $this->_params
);
626 $contact = civicrm_api('Contact', 'Create', array(
627 'first_name' => "The Rock",
628 'last_name' =>'roccky',
629 'contact_type' => 'Individual',
631 'api.activity.create' => array(
632 'id' => $activity['id'], 'assignee_contact_id' => '$value.id',
637 'activity_id' => $activity['id'],
638 'version' => $this->_apiversion
,
640 'return.assignee_contact_id' => 1,
641 'api.contact.get' => array(
642 'id' => '$value.source_contact_id',
646 $result = civicrm_api('Activity', 'Get', $params);
647 $this->documentMe($params, $result, __FUNCTION__
, __FILE__
, $description, $subfile);
649 $this->assertAPISuccess($result);
651 $this->assertEquals($activity['id'], $result['id'], 'In line ' . __LINE__
);
653 $this->assertEquals($contact['id'], $result['values'][0]['assignee_contact_id'][0], 'In line ' . __LINE__
);
655 $this->assertEquals(17, $result['values'][0]['api.contact.get']['values'][0]['contact_id'], 'In line ' . __LINE__
);
656 $this->assertEquals($this->test_activity_type_value
, $result['values'][0]['activity_type_id'], 'In line ' . __LINE__
);
657 $this->assertEquals("test activity type id", $result['values'][0]['subject'], 'In line ' . __LINE__
);
661 * test that get functioning does filtering
663 function testGetFilter() {
665 'source_contact_id' => 17,
666 'subject' => 'Make-it-Happen Meeting',
667 'activity_date_time' => '20110316',
669 'location' => 'Pensulvania',
670 'details' => 'a test activity',
672 'activity_name' => 'Test activity type',
673 'version' => $this->_apiversion
,
676 civicrm_api('Activity', 'Create', $params);
677 $result = civicrm_api('Activity', 'Get', array('version' => 3, 'subject' => 'Make-it-Happen Meeting'));
678 $this->assertEquals(1, $result['count']);
679 $this->assertEquals('Make-it-Happen Meeting', $result['values'][$result['id']]['subject']);
680 civicrm_api('Activity', 'Delete', array('version' => 3, 'id' => $result['id']));
683 * test that get functioning does filtering
685 function testGetStatusID() {
687 'source_contact_id' => 17,
688 'subject' => 'Make-it-Happen Meeting',
689 'activity_date_time' => '20110316',
691 'location' => 'Pensulvania',
692 'details' => 'a test activity',
694 'activity_name' => 'Test activity type',
695 'version' => $this->_apiversion
,
698 civicrm_api('Activity', 'Create', $params);
699 $result = civicrm_api('Activity', 'Get', array(
700 'version' => $this->_apiversion
,
701 'activity_status_id' => '1'));
702 $this->assertEquals(1, $result['count'], 'one activity of status 1 should exist');
704 $result = civicrm_api('Activity', 'Get', array(
705 'version' => $this->_apiversion
,
706 'status_id' => '1'));
707 $this->assertEquals(1, $result['count'], 'status_id should also work');
709 $result = civicrm_api('Activity', 'Get', array(
710 'version' => $this->_apiversion
,
711 'activity_status_id' => '2'));
712 $this->assertEquals(0, $result['count'], 'No activities of status 1 should exist');
713 $result = civicrm_api('Activity', 'Get', array(
714 'version' => $this->_apiversion
,
715 'status_id' => '2'));
716 $this->assertEquals(0, $result['count'], 'No activities of status 1 should exist');
722 * test that get functioning does filtering
724 function testGetFilterMaxDate() {
726 'source_contact_id' => 17,
727 'subject' => 'Make-it-Happen Meeting',
728 'activity_date_time' => '20110101',
730 'location' => 'Pensulvania',
731 'details' => 'a test activity',
733 'activity_name' => 'Test activity type',
734 'version' => $this->_apiversion
,
737 $activityOne = civicrm_api('Activity', 'Create', $params);
738 $params['activity_date_time'] = 20120216;
739 $activityTwo = civicrm_api('Activity', 'Create', $params);
740 $result = civicrm_api('Activity', 'Get', array(
743 $description = "demonstrates _low filter (at time of writing doesn't work if contact_id is set";
744 $subfile = "DateTimeLow";
745 $this->assertEquals(2, $result['count']);
748 'filter.activity_date_time_low' => '20120101000000',
751 $result = civicrm_api('Activity', 'Get', $params);
752 $this->documentMe($params, $result, __FUNCTION__
, __FILE__
, $description, $subfile);
753 $this->assertEquals(1, $result['count'], 'in line ' . __LINE__
);
754 $description = "demonstrates _high filter (at time of writing doesn't work if contact_id is set";
755 $subfile = "DateTimeHigh";
756 $this->assertEquals('2012-02-16 00:00:00', $result['values'][0]['activity_date_time'], 'in line ' . __LINE__
);
758 'source_contact_id' => 17,
760 'filter.activity_date_time_high' => '20120101000000',
763 $result = civicrm_api('Activity', 'Get', $params);
764 $this->documentMe($params, $result, __FUNCTION__
, __FILE__
, $description, $subfile);
766 $this->assertEquals(1, $result['count']);
767 $this->assertEquals('2011-01-01 00:00:00', $result['values'][0]['activity_date_time'], 'in line ' . __LINE__
);
769 civicrm_api('Activity', 'Delete', array('version' => 3, 'id' => $activityOne['id']));
770 civicrm_api('Activity', 'Delete', array('version' => 3, 'id' => $activityTwo['id']));
774 * Test civicrm_activity_get() with a good activity ID which
775 * has associated custom data
777 function testActivityGetGoodIDCustom() {
778 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__
, __FILE__
);
780 $params = $this->_params
;
781 $params['custom_' . $ids['custom_field_id']] = "custom string";
783 $result = civicrm_api($this->_entity
, 'create', $params);
784 $this->assertAPISuccess($result, ' in line ' . __LINE__
);
785 // Retrieve the test value
787 'activity_type_id' => $this->test_activity_type_value
,
790 'return.custom_' . $ids['custom_field_id'] => 1,
792 $result = civicrm_api('activity', 'get', $params, TRUE);
793 $this->documentMe($params, $result, __FUNCTION__
, __FILE__
);
794 $this->assertAPISuccess($result, ' in line ' . __LINE__
);
795 $this->assertEquals("custom string", $result['values'][0]['custom_' . $ids['custom_field_id']], ' in line ' . __LINE__
);
798 $this->assertEquals($this->test_activity_type_value
, $result['values'][0]['activity_type_id'], 'In line ' . __LINE__
);
799 $this->assertEquals('test activity type id', $result['values'][0]['subject'], 'In line ' . __LINE__
);
800 $this->customFieldDelete($ids['custom_field_id']);
801 $this->customGroupDelete($ids['custom_group_id']);
805 * Test civicrm_activity_get() with a good activity ID which
806 * has associated custom data
808 function testActivityGetContact_idCustom() {
809 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__
, __FILE__
);
811 $params = $this->_params
;
812 $params['custom_' . $ids['custom_field_id']] = "custom string";
814 $result = civicrm_api($this->_entity
, 'create', $params);
815 // Retrieve the test value
817 'contact_id' => $this->_params
['source_contact_id'],
818 'activity_type_id' => $this->test_activity_type_value
,
821 'return.custom_' . $ids['custom_field_id'] => 1,
823 $result = civicrm_api('activity', 'get', $params, TRUE);
824 $this->assertAPISuccess($result, 'in line ' . __LINE__
);
825 $this->documentMe($params, $result, __FUNCTION__
, __FILE__
);
826 $this->assertAPISuccess($result);
827 $this->assertEquals("custom string", $result['values'][0]['custom_' . $ids['custom_field_id']], ' in line ' . __LINE__
);
829 $this->assertEquals($this->test_activity_type_value
, $result['values'][0]['activity_type_id'], 'In line ' . __LINE__
);
830 $this->assertEquals('test activity type id', $result['values'][0]['subject'], 'In line ' . __LINE__
);
831 $this->assertEquals($result['values'][0]['id'], $result['id'], 'in line ' . __LINE__
);
835 * check activity deletion with empty params
837 function testDeleteActivityForEmptyParams() {
838 $params = array('version' => $this->_apiversion
);
839 $result = $this->callAPIFailure('activity', 'delete', $params);
843 * check activity deletion without activity id
845 function testDeleteActivityWithoutId() {
847 'activity_name' => 'Meeting',
848 'version' => $this->_apiversion
,
850 $result = $this->callAPIFailure('activity', 'delete', $params);
854 * check activity deletion without activity type
856 function testDeleteActivityWithoutActivityType() {
857 $params = array('id' => 1);
858 $result = $this->callAPIFailure('activity', 'delete', $params);
862 * check activity deletion with incorrect data
864 function testDeleteActivityWithIncorrectActivityType() {
867 'activity_name' => 'Test Activity',
870 $result = $this->callAPIFailure('activity', 'delete', $params);
874 * check activity deletion with correct data
876 function testDeleteActivity() {
877 $result = civicrm_api('activity', 'create', $this->_params
);
879 'id' => $result['id'],
880 'version' => $this->_apiversion
,
883 $result = civicrm_api('activity', 'delete', $params);
884 $this->documentMe($params, $result, __FUNCTION__
, __FILE__
);
885 $this->assertAPISuccess($result);
889 * check if required fields are not passed
891 function testActivityUpdateWithoutRequired() {
893 'subject' => 'this case should fail',
894 'scheduled_date_time' => date('Ymd'),
897 $result = $this->callAPIFailure('activity', 'create', $params);
901 * Test civicrm_activity_update() with non-numeric id
903 function testActivityUpdateWithNonNumericId() {
905 'id' => 'lets break it',
906 'activity_name' => 'Meeting',
907 'subject' => 'this case should fail',
908 'scheduled_date_time' => date('Ymd'),
911 $result = $this->callAPIFailure('activity', 'create', $params);
915 * check with incorrect required fields
917 function testActivityUpdateWithIncorrectContactActivityType() {
920 'activity_name' => 'Test Activity',
921 'subject' => 'this case should fail',
922 'scheduled_date_time' => date('Ymd'),
923 'source_contact_id' => 17,
926 $result = $this->callAPIFailure('activity', 'create', $params);
927 $this->assertEquals($result['error_message'], 'Invalid Activity Id', "In line " . __LINE__
);
931 * Test civicrm_activity_update() to update an existing activity
933 function testActivityUpdate() {
934 $result = civicrm_api('activity', 'create', $this->_params
);
937 'id' => $result['id'],
938 'subject' => 'Make-it-Happen Meeting',
939 'activity_date_time' => '20091011123456',
941 'location' => '21, Park Avenue',
942 'details' => 'Lets update Meeting',
944 'source_contact_id' => 17,
946 'version' => $this->_apiversion
,
949 $result = civicrm_api('activity', 'create', $params);
950 $this->assertAPISuccess($result, 'in line ' . __LINE__
);
951 //hack on date comparison - really we should make getAndCheck smarter to handle dates
952 $params['activity_date_time'] = '2009-10-11 12:34:56';
953 // we also unset source_contact_id since it is stored in an aux table
954 unset($params['source_contact_id']);
955 $this->getAndCheck($params, $result['id'], 'activity');
959 * Test civicrm_activity_update() with valid parameters
960 * and some custom data
962 function testActivityUpdateCustom() {
963 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__
, __FILE__
);
965 $params = $this->_params
;
967 // Create an activity with custom data
968 //this has been updated from the previous 'old format' function - need to make it work
970 'source_contact_id' => 17,
971 'subject' => 'Make-it-Happen Meeting',
972 'activity_date_time' => '2009-10-18',
974 'location' => 'Pensulvania',
975 'details' => 'a test activity to check the update api',
977 'activity_name' => 'Test activity type',
978 'version' => $this->_apiversion
,
979 'custom_' . $ids['custom_field_id'] => 'custom string',
981 $result = civicrm_api('activity', 'create', $params);
983 $activityId = $result['id'];
984 $this->assertAPISuccess($result);
985 $result = civicrm_api($this->_entity
, 'get', array('return.custom_' . $ids['custom_field_id'] => 1, 'version' => 3, 'id' => $result['id']));
986 $this->assertEquals("custom string", $result['values'][$result['id']]['custom_' . $ids['custom_field_id']], ' in line ' . __LINE__
);
987 $this->assertEquals("2009-10-18 00:00:00", $result['values'][$result['id']]['activity_date_time'], ' in line ' . __LINE__
);
988 $fields = civicrm_api('activity', 'getfields', array('version' => $this->_apiversion
));
989 $this->assertTrue(is_array($fields['values']['custom_' . $ids['custom_field_id']]));
991 // Update the activity with custom data
994 'source_contact_id' => 17,
995 'subject' => 'Make-it-Happen Meeting',
997 'activity_name' => 'Test activity type',
998 // add this since dates are messed up
999 'activity_date_time' => date('Ymd'),
1000 'custom_' . $ids['custom_field_id'] => 'Updated my test data',
1001 'version' => $this->_apiversion
,
1003 $result = civicrm_api('Activity', 'Create', $params);
1004 $this->assertAPISuccess($result);
1006 $result = civicrm_api($this->_entity
, 'get', array('return.custom_' . $ids['custom_field_id'] => 1, 'version' => 3, 'id' => $result['id']));
1007 $this->assertEquals("Updated my test data", $result['values'][$result['id']]['custom_' . $ids['custom_field_id']], ' in line ' . __LINE__
);
1011 * Test civicrm_activity_update() for core activity fields
1012 * and some custom data
1014 function testActivityUpdateCheckCoreFields() {
1015 $params = $this->_params
;
1016 $contact1Params = array(
1017 'first_name' => 'John',
1018 'middle_name' => 'J.',
1019 'last_name' => 'Anderson',
1022 'email' => 'john_anderson@civicrm.org',
1023 'contact_type' => 'Individual',
1026 $contact1 = $this->individualCreate($contact1Params);
1027 $contact2Params = array(
1028 'first_name' => 'Michal',
1029 'middle_name' => 'J.',
1030 'last_name' => 'Anderson',
1033 'email' => 'michal_anderson@civicrm.org',
1034 'contact_type' => 'Individual',
1037 $contact2 = $this->individualCreate($contact2Params);
1039 $params['assignee_contact_id'] = array($contact1, $contact2);
1040 $params['target_contact_id'] = array($contact2 => $contact2);
1041 $result = civicrm_api('Activity', 'Create', $params);
1043 $activityId = $result['id'];
1044 $this->assertAPISuccess($result);
1046 'return.assignee_contact_id' => 1,
1047 'return.target_contact_id' => 1,
1048 'version' => $this->_apiversion
,
1049 'id' => $activityId,
1051 $result = civicrm_api($this->_entity
, 'get',$getParams );
1052 $assignee = $result['values'][$result['id']]['assignee_contact_id'];
1053 $target = $result['values'][$result['id']]['target_contact_id'];
1054 $this->assertEquals(2, count($assignee), ' in line ' . __LINE__
);
1055 $this->assertEquals(1, count($target), ' in line ' . __LINE__
);
1056 $this->assertEquals(TRUE, in_array($contact1, $assignee), ' in line ' . __LINE__
);
1057 $this->assertEquals(TRUE, in_array($contact2, $target), ' in line ' . __LINE__
);
1059 $contact3Params = array(
1060 'first_name' => 'Jijo',
1061 'middle_name' => 'J.',
1062 'last_name' => 'Anderson',
1065 'email' => 'jijo_anderson@civicrm.org',
1066 'contact_type' => 'Individual',
1069 $contact4Params = array(
1070 'first_name' => 'Grant',
1071 'middle_name' => 'J.',
1072 'last_name' => 'Anderson',
1075 'email' => 'grant_anderson@civicrm.org',
1076 'contact_type' => 'Individual',
1079 $contact3 = $this->individualCreate($contact3Params);
1080 $contact4 = $this->individualCreate($contact4Params);
1083 $params['id'] = $activityId;
1084 $params['version'] = $this->_apiversion
;
1085 $params['assignee_contact_id'] = array($contact3 => $contact3);
1086 $params['target_contact_id'] = array($contact4 => $contact4);
1088 $result = civicrm_api('activity', 'create', $params);
1089 $this->assertAPISuccess($result);
1091 $this->assertEquals($activityId, $result['id'], ' in line ' . __LINE__
);
1093 $result = civicrm_api(
1097 'return.assignee_contact_id' => 1,
1098 'return.target_contact_id' => 1,
1099 'return.source_contact_id' => 1,
1101 'id' => $result['id']
1105 $assignee = $result['values'][$result['id']]['assignee_contact_id'];
1106 $target = $result['values'][$result['id']]['target_contact_id'];
1108 $this->assertEquals(1, count($assignee), ' in line ' . __LINE__
);
1109 $this->assertEquals(1, count($target), ' in line ' . __LINE__
);
1110 $this->assertEquals(TRUE, in_array($contact3, $assignee), ' in line ' . __LINE__
);
1111 $this->assertEquals(TRUE, in_array($contact4, $target), ' in line ' . __LINE__
);
1113 foreach ($this->_params
as $fld => $val) {
1114 if ($fld == 'version') {
1117 $this->assertEquals($val, $result['values'][$result['id']][$fld], ' in line ' . __LINE__
);
1122 * Test civicrm_activity_update() where the DB has a date_time
1123 * value and there is none in the update params.
1125 function testActivityUpdateNotDate() {
1126 $result = civicrm_api('activity', 'create', $this->_params
);
1129 'id' => $result['id'],
1130 'subject' => 'Make-it-Happen Meeting',
1132 'location' => '21, Park Avenue',
1133 'details' => 'Lets update Meeting',
1135 'source_contact_id' => 17,
1137 'version' => $this->_apiversion
,
1140 $result = civicrm_api('activity', 'create', $params);
1141 $this->assertAPISuccess($result, 'in line ' . __LINE__
);
1142 //hack on date comparison - really we should make getAndCheck smarter to handle dates
1143 $params['activity_date_time'] = $this->_params
['activity_date_time'];
1144 // we also unset source_contact_id since it is stored in an aux table
1145 unset($params['source_contact_id']);
1146 $this->getAndCheck($params, $result['id'], 'activity');
1150 * check activity update with status
1152 function testActivityUpdateWithStatus() {
1153 $activity = civicrm_api('activity', 'create', $this->_params
);
1154 $this->assertAPISuccess($activity, "In line " . __LINE__
);
1156 'id' => $activity['id'],
1157 'source_contact_id' => 17,
1158 'subject' => 'Hurry update works',
1160 'activity_name' => 'Test activity type',
1161 'version' => $this->_apiversion
,
1164 $result = civicrm_api('activity', 'create', $params);
1165 $this->assertAPISuccess($result, "In line " . __LINE__
);
1166 $this->assertEquals($result['id'], $activity['id'], "In line " . __LINE__
);
1167 /*$this->assertEquals($result['values'][$activity['id']]['source_contact_id'], 17,
1168 "In line " . __LINE__
1170 $this->assertEquals($result['values'][$activity['id']]['subject'], 'Hurry update works',
1171 "In line " . __LINE__
1173 $this->assertEquals($result['values'][$activity['id']]['status_id'], 1,
1174 "In line " . __LINE__
1179 * Test civicrm_activity_update() where the source_contact_id
1180 * is not in the update params.
1182 function testActivityUpdateKeepSource() {
1183 $activity = civicrm_api('activity', 'create', $this->_params
);
1184 // Updating the activity but not providing anything for the source contact
1185 // (It was set as 17 earlier.)
1187 'id' => $activity['id'],
1188 'subject' => 'Updated Make-it-Happen Meeting',
1190 'location' => '21, Park Avenue',
1191 'details' => 'Lets update Meeting',
1193 'activity_name' => 'Test activity type',
1195 'version' => $this->_apiversion
,
1198 $result = civicrm_api('activity', 'create', $params);
1199 $this->assertAPISuccess($result, 'in line ' . __LINE__
);
1200 $findactivity = civicrm_api('Activity', 'Get', array('id' => $activity['id'], 'version' => 3));
1202 $this->assertAPISuccess($findactivity);
1206 * Test civicrm_activities_contact_get()
1208 function testActivitiesContactGet() {
1209 $activity = civicrm_api('activity', 'create', $this->_params
);
1210 $activity2 = civicrm_api('activity', 'create', $this->_params2
);
1211 // Get activities associated with contact 17
1214 'version' => $this->_apiversion
,
1216 $result = civicrm_api('activity', 'get', $params);
1218 $this->assertAPISuccess($result, 'in line ' . __LINE__
);
1219 $this->assertEquals(2, $result['count'], 'In line ' . __LINE__
);
1220 $this->assertEquals($this->test_activity_type_value
, $result['values'][$activity['id']]['activity_type_id'], 'In line ' . __LINE__
);
1221 $this->assertEquals('Test activity type', $result['values'][$activity['id']]['activity_name'], 'In line ' . __LINE__
);
1222 $this->assertEquals('Test activity type', $result['values'][$activity2['id']]['activity_name'], 'In line ' . __LINE__
);
1225 * test chained Activity format
1227 function testchainedActivityGet() {
1229 $activity = civicrm_api('Contact', 'Create', array(
1230 'version' => $this->_apiversion
,
1231 'display_name' => "bob brown",
1232 'contact_type' => 'Individual',
1233 'api.activity_type.create' => array(
1235 'label' => 'send out letters',
1240 ), 'api.activity.create' => array('subject' => 'send letter', 'activity_type_id' => '$value.api.activity_type.create.values.0.value'),
1243 $this->assertAPISuccess($activity, 'in line ' . __LINE__
);
1244 $result = civicrm_api('Activity', 'Get', array(
1246 'id' => $activity['id'],
1247 'return.assignee_contact_id' => 1,
1248 'api.contact.get' => array('api.pledge.get' => 1),
1253 * Test civicrm_activity_contact_get() with invalid Contact Id
1255 function testActivitiesContactGetWithInvalidContactId() {
1256 $params = array('contact_id' => 'contact');
1257 $result = $this->callAPIFailure('activity', 'get', $params);
1261 * Test civicrm_activity_contact_get() with contact having no Activity
1263 function testActivitiesContactGetHavingNoActivity() {
1265 'first_name' => 'dan',
1266 'last_name' => 'conberg',
1267 'email' => 'dan.conberg@w.co.in',
1268 'contact_type' => 'Individual',
1269 'version' => $this->_apiversion
,
1272 $contact = civicrm_api('contact', 'create', $params);
1274 'contact_id' => $contact['id'],
1275 'version' => $this->_apiversion
,
1277 $result = civicrm_api('activity', 'get', $params);
1278 $this->assertAPISuccess($result);
1279 $this->assertEquals($result['count'], 0, 'in line ' . __LINE__
);
1282 function testGetFields() {
1283 $params = array('version' => 3, 'action' => 'create');
1284 $result = civicrm_api('activity', 'getfields', $params);
1285 $this->documentMe($params, $result, __FUNCTION__
, __FILE__
, NULL, NULL, 'getfields');
1286 $this->assertTrue(is_array($result['values']), 'get fields doesnt return values array in line ' . __LINE__
);
1287 // $this->assertTrue(is_array($result['values']['priority_id']['options']));
1288 foreach ($result['values'] as $key => $value) {
1289 $this->assertTrue(is_array($value), $key . " is not an array in line " . __LINE__
);