Merge pull request #10155 from totten/master-pdf-save2
[civicrm-core.git] / tests / phpunit / api / v3 / ActivityTest.php
CommitLineData
6a488035
TO
1<?php
2/**
d177a2a6 3 * @file
6a488035
TO
4 * File for the TestActivity class
5 *
6 * (PHP 5)
7 *
6c6e6187
TO
8 * @author Walt Haas <walt@dharmatech.org> (801) 534-1262
9 * @copyright Copyright CiviCRM LLC (C) 2009
10 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html
6a488035 11 * GNU Affero General Public License version 3
6c6e6187
TO
12 * @version $Id: ActivityTest.php 31254 2010-12-15 10:09:29Z eileen $
13 * @package CiviCRM
6a488035
TO
14 *
15 * This file is part of CiviCRM
16 *
17 * CiviCRM is free software; you can redistribute it and/or
18 * modify it under the terms of the GNU Affero General Public License
19 * as published by the Free Software Foundation; either version 3 of
20 * the License, or (at your option) any later version.
21 *
22 * CiviCRM is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU Affero General Public License for more details.
26 *
27 * You should have received a copy of the GNU Affero General Public
28 * License along with this program. If not, see
29 * <http://www.gnu.org/licenses/>.
30 */
31
32/**
33 * Include class definitions
34 */
6a488035
TO
35
36/**
d177a2a6 37 * Test APIv3 civicrm_activity_* functions
6a488035 38 *
6c6e6187
TO
39 * @package CiviCRM_APIv3
40 * @subpackage API_Activity
acb109b7 41 * @group headless
6a488035 42 */
6a488035
TO
43class api_v3_ActivityTest extends CiviUnitTestCase {
44 protected $_params;
45 protected $_params2;
46 protected $_entity = 'activity';
47 protected $_apiversion = 3;
48 protected $test_activity_type_value;
b8a3da18 49 protected $_contactID;
9f1b81e0 50
6a488035 51 /**
d177a2a6 52 * Test setup for every test.
6a488035 53 *
d177a2a6
EM
54 * Connect to the database, truncate the tables that will be used
55 * and redirect stdin to a temporary file
6a488035
TO
56 */
57 public function setUp() {
e3aa0dd1 58 // Connect to the database
6a488035 59 parent::setUp();
6a488035 60
b8a3da18 61 $this->_contactID = $this->individualCreate();
6a488035 62 //create activity types
7b1dc1f6 63 $this->test_activity_type_value = 9999;
b8a3da18 64 $activityTypes = $this->callAPISuccess('option_value', 'create', array(
65 'option_group_id' => 2,
66 'name' => 'Test activity type',
67 'label' => 'Test activity type',
7b1dc1f6 68 'value' => $this->test_activity_type_value,
21dfd5f5 69 'sequential' => 1,
b8a3da18 70 ));
6a488035
TO
71 $this->test_activity_type_id = $activityTypes['id'];
72 $this->_params = array(
b8a3da18 73 'source_contact_id' => $this->_contactID,
7b1dc1f6 74 'activity_type_id' => 'Test activity type',
6a488035
TO
75 'subject' => 'test activity type id',
76 'activity_date_time' => '2011-06-02 14:36:13',
77 'status_id' => 2,
78 'priority_id' => 1,
79 'duration' => 120,
e3aa0dd1 80 'location' => 'Pennsylvania',
6a488035 81 'details' => 'a test activity',
6a488035
TO
82 );
83 $this->_params2 = array(
b8a3da18 84 'source_contact_id' => $this->_contactID,
6a488035
TO
85 'subject' => 'Eat & drink',
86 'activity_date_time' => date('Ymd'),
87 'duration' => 120,
88 'location' => 'Napier',
89 'details' => 'discuss & eat',
90 'status_id' => 1,
91 'activity_type_id' => $this->test_activity_type_value,
6a488035
TO
92 );
93 // create a logged in USER since the code references it for source_contact_id
94 $this->createLoggedInUser();
95 }
96
97 /**
98 * Tears down the fixture, for example, closes a network connection.
e3aa0dd1 99 *
6a488035 100 * This method is called after a test is executed.
6a488035 101 */
00be9182 102 public function tearDown() {
6a488035
TO
103 $tablesToTruncate = array(
104 'civicrm_contact',
105 'civicrm_activity',
b319d00a 106 'civicrm_activity_contact',
225d474b 107 'civicrm_uf_match',
6a488035
TO
108 );
109 $this->quickCleanup($tablesToTruncate, TRUE);
a60c0bc8
SL
110 $type = $this->callAPISuccess('optionValue', 'get', array('id' => $this->test_activity_type_id));
111 if (!empty($type['count'])) {
112 $this->callAPISuccess('option_value', 'delete', array('id' => $this->test_activity_type_id));
113 }
6a488035
TO
114 }
115
116 /**
e3aa0dd1 117 * Check fails with empty array.
6a488035 118 */
00be9182 119 public function testActivityCreateEmpty() {
e3aa0dd1 120 $this->callAPIFailure('activity', 'create', array());
6a488035
TO
121 }
122
123 /**
eceb18cc 124 * Check if required fields are not passed.
6a488035 125 */
00be9182 126 public function testActivityCreateWithoutRequired() {
6a488035
TO
127 $params = array(
128 'subject' => 'this case should fail',
129 'scheduled_date_time' => date('Ymd'),
6a488035 130 );
d0e1eff2 131 $result = $this->callAPIFailure('activity', 'create', $params);
6a488035
TO
132 }
133
134 /**
d177a2a6
EM
135 * Test civicrm_activity_create() with mismatched activity_type_id
136 * and activity_name.
6a488035 137 */
00be9182 138 public function testActivityCreateMismatchNameType() {
6a488035 139 $params = array(
b8a3da18 140 'source_contact_id' => $this->_contactID,
6a488035
TO
141 'subject' => 'Test activity',
142 'activity_date_time' => date('Ymd'),
143 'duration' => 120,
e3aa0dd1 144 'location' => 'Pennsylvania',
6a488035
TO
145 'details' => 'a test activity',
146 'status_id' => 1,
147 'activity_name' => 'Fubar activity type',
148 'activity_type_id' => 5,
149 'scheduled_date_time' => date('Ymd'),
6a488035
TO
150 );
151
d0e1eff2 152 $result = $this->callAPIFailure('activity', 'create', $params);
6a488035
TO
153 }
154
155 /**
d177a2a6 156 * Test civicrm_activity_id() with missing source_contact_id is put with the current user.
6a488035 157 */
00be9182 158 public function testActivityCreateWithMissingContactId() {
6a488035
TO
159 $params = array(
160 'subject' => 'Make-it-Happen Meeting',
161 'activity_date_time' => date('Ymd'),
162 'duration' => 120,
e3aa0dd1 163 'location' => 'Pennsylvania',
6a488035
TO
164 'details' => 'a test activity',
165 'status_id' => 1,
166 'activity_name' => 'Test activity type',
6a488035
TO
167 );
168
d177a2a6 169 $this->callAPISuccess('activity', 'create', $params);
6a488035
TO
170 }
171
172 /**
d177a2a6 173 * Test civicrm_activity_id() with non-numeric source_contact_id.
6a488035 174 */
00be9182 175 public function testActivityCreateWithNonNumericContactId() {
6a488035
TO
176 $params = array(
177 'source_contact_id' => 'fubar',
178 'subject' => 'Make-it-Happen Meeting',
179 'activity_date_time' => date('Ymd'),
180 'duration' => 120,
e3aa0dd1 181 'location' => 'Pennsylvania',
6a488035
TO
182 'details' => 'a test activity',
183 'status_id' => 1,
184 'activity_name' => 'Test activity type',
6a488035
TO
185 );
186
e3aa0dd1 187 $this->callAPIFailure('activity', 'create', $params);
6a488035
TO
188 }
189
6a488035 190 /**
fe482240
EM
191 * Ensure that an invalid activity type causes failure.
192 *
193 * Oddly enough this test was failing because the creation of the invalid type
feb2a730 194 * got added to the set up routine. Probably a mis-fix on a test
6a488035 195 */
00be9182 196 public function testActivityCreateWithNonNumericActivityTypeId() {
6a488035 197 $params = array(
b8a3da18 198 'source_contact_id' => $this->_contactID,
6a488035
TO
199 'subject' => 'Make-it-Happen Meeting',
200 'activity_date_time' => date('Ymd'),
201 'duration' => 120,
e3aa0dd1 202 'location' => 'Pennsylvania',
6a488035
TO
203 'details' => 'a test activity',
204 'status_id' => 1,
feb2a730 205 'activity_type_id' => 'Invalid Test activity type',
6a488035
TO
206 );
207
d0e1eff2 208 $result = $this->callAPIFailure('activity', 'create', $params);
6a488035
TO
209 }
210
211 /**
e3aa0dd1 212 * Check with incorrect required fields.
6a488035 213 */
00be9182 214 public function testActivityCreateWithUnknownActivityTypeId() {
6a488035 215 $params = array(
b8a3da18 216 'source_contact_id' => $this->_contactID,
6a488035
TO
217 'subject' => 'Make-it-Happen Meeting',
218 'activity_date_time' => date('Ymd'),
219 'duration' => 120,
e3aa0dd1 220 'location' => 'Pennsylvania',
6a488035
TO
221 'details' => 'a test activity',
222 'status_id' => 1,
223 'activity_type_id' => 699,
6a488035
TO
224 );
225
d0e1eff2 226 $result = $this->callAPIFailure('activity', 'create', $params);
6a488035
TO
227 }
228
00be9182 229 public function testActivityCreateWithInvalidPriority() {
6a488035 230 $params = array(
b8a3da18 231 'source_contact_id' => $this->_contactID,
6a488035
TO
232 'subject' => 'Make-it-Happen Meeting',
233 'activity_date_time' => date('Ymd'),
234 'duration' => 120,
e3aa0dd1 235 'location' => 'Pennsylvania',
6a488035
TO
236 'details' => 'a test activity',
237 'status_id' => 1,
238 'priority_id' => 44,
239 'activity_type_id' => 1,
6a488035
TO
240 );
241
b8a3da18 242 $result = $this->callAPIFailure('activity', 'create', $params,
243 "'44' is not a valid option for field priority_id");
283f988c 244 $this->assertEquals('priority_id', $result['error_field']);
6a488035
TO
245 }
246
feb2a730 247
e3aa0dd1 248 /**
249 * Test create succeeds with valid string for priority.
250 */
00be9182 251 public function testActivityCreateWithValidStringPriority() {
6a488035 252 $params = array(
b8a3da18 253 'source_contact_id' => $this->_contactID,
6a488035
TO
254 'subject' => 'Make-it-Happen Meeting',
255 'activity_date_time' => date('Ymd'),
256 'duration' => 120,
e3aa0dd1 257 'location' => 'Pennsylvania',
6a488035
TO
258 'details' => 'a test activity',
259 'status_id' => 1,
260 'priority_id' => 'Urgent',
261 'activity_type_id' => 1,
6a488035
TO
262 );
263
b8a3da18 264 $result = $this->callAPISuccess('activity', 'create', $params);
6a488035
TO
265 $this->assertEquals(1, $result['values'][$result['id']]['priority_id']);
266 }
267
e3aa0dd1 268 /**
269 * Test create fails with invalid priority string.
270 */
00be9182 271 public function testActivityCreateWithInValidStringPriority() {
6a488035 272 $params = array(
b8a3da18 273 'source_contact_id' => $this->_contactID,
6a488035
TO
274 'subject' => 'Make-it-Happen Meeting',
275 'activity_date_time' => date('Ymd'),
276 'duration' => 120,
e3aa0dd1 277 'location' => 'Pennsylvania',
6a488035
TO
278 'details' => 'a test activity',
279 'status_id' => 1,
280 'priority_id' => 'ergUrgent',
281 'activity_type_id' => 1,
6a488035
TO
282 );
283
e3aa0dd1 284 $this->callAPIFailure('activity', 'create', $params,
b8a3da18 285 "'ergUrgent' is not a valid option for field priority_id");
6a488035
TO
286 }
287
288 /**
e3aa0dd1 289 * Test civicrm_activity_create() with valid parameters.
6a488035 290 */
00be9182 291 public function testActivityCreate() {
6a488035 292
e3aa0dd1 293 $this->callAPISuccess('activity', 'create', $this->_params);
b8a3da18 294 $result = $this->callAPISuccess('activity', 'get', $this->_params);
e3aa0dd1 295 $this->assertEquals($result['values'][$result['id']]['duration'], 120);
296 $this->assertEquals($result['values'][$result['id']]['subject'], 'test activity type id');
297 $this->assertEquals($result['values'][$result['id']]['activity_date_time'], '2011-06-02 14:36:13');
298 $this->assertEquals($result['values'][$result['id']]['location'], 'Pennsylvania');
299 $this->assertEquals($result['values'][$result['id']]['details'], 'a test activity');
300 $this->assertEquals($result['values'][$result['id']]['status_id'], 2);
301 $this->assertEquals($result['values'][$result['id']]['id'], $result['id']);
6a488035
TO
302 }
303
304 /**
d177a2a6 305 * Test civicrm_activity_create() with valid parameters - use type_id.
6a488035 306 */
00be9182 307 public function testActivityCreateCampaignTypeID() {
07fd63f5 308 $this->enableCiviCampaign();
6a488035 309
6a488035 310 $params = array(
b8a3da18 311 'source_contact_id' => $this->_contactID,
6a488035
TO
312 'subject' => 'Make-it-Happen Meeting',
313 'activity_date_time' => '20110316',
314 'duration' => 120,
e3aa0dd1 315 'location' => 'Pennsylvania',
6a488035
TO
316 'details' => 'a test activity',
317 'status_id' => 1,
318 'activity_type_id' => 29,
6a488035
TO
319 );
320
b8a3da18 321 $result = $this->callAPISuccess('activity', 'create', $params);
6a488035 322
f20dc314 323 $result = $this->callAPISuccess('activity', 'getsingle', array('id' => $result['id']));
324 $this->assertEquals($result['duration'], 120);
325 $this->assertEquals($result['subject'], 'Make-it-Happen Meeting');
326 $this->assertEquals($result['activity_date_time'], '2011-03-16 00:00:00');
327 $this->assertEquals($result['location'], 'Pennsylvania');
328 $this->assertEquals($result['details'], 'a test activity');
329 $this->assertEquals($result['status_id'], 1);
330
331 $priorities = $this->callAPISuccess('activity', 'getoptions', array('field' => 'priority_id'));
332 $this->assertEquals($result['priority_id'], array_search('Normal', $priorities['values']));
6a488035
TO
333 }
334
e3aa0dd1 335 /**
336 * Test get returns target and assignee contacts.
337 */
00be9182 338 public function testActivityReturnTargetAssignee() {
6a488035 339
5c49fee0 340 $description = "Demonstrates setting & retrieving activity target & source.";
92915c55
TO
341 $subfile = "GetTargetandAssignee";
342 $params = array(
b8a3da18 343 'source_contact_id' => $this->_contactID,
6a488035
TO
344 'subject' => 'Make-it-Happen Meeting',
345 'activity_date_time' => '20110316',
346 'duration' => 120,
e3aa0dd1 347 'location' => 'Pennsylvania',
6a488035
TO
348 'details' => 'a test activity',
349 'status_id' => 1,
350 'activity_type_id' => 1,
6a488035 351 'priority_id' => 1,
b8a3da18 352 'target_contact_id' => $this->_contactID,
353 'assignee_contact_id' => $this->_contactID,
6a488035
TO
354 );
355
9f1b81e0 356 $result = $this->callAPIAndDocument('activity', 'create', $params, __FUNCTION__, __FILE__, $description, $subfile);
92915c55 357 $result = $this->callAPISuccess('activity', 'get', array(
c301f76e 358 'id' => $result['id'],
359 'version' => $this->_apiversion,
360 'return.assignee_contact_id' => 1,
361 'return.target_contact_id' => 1,
362 ));
6a488035 363
e3aa0dd1 364 $this->assertEquals($this->_contactID, $result['values'][$result['id']]['assignee_contact_id'][0]);
365 $this->assertEquals($this->_contactID, $result['values'][$result['id']]['target_contact_id'][0]);
6a488035
TO
366 }
367
db6e8cb4
CW
368 /**
369 * Test get returns target and assignee contact names.
370 */
371 public function testActivityReturnTargetAssigneeName() {
372
373 $description = "Demonstrates retrieving activity target & source contact names.";
374 $subfile = "GetTargetandAssigneeName";
375 $target1 = $this->callAPISuccess('Contact', 'create', array('contact_type' => 'Individual', 'first_name' => 'A', 'last_name' => 'Cat'));
376 $target2 = $this->callAPISuccess('Contact', 'create', array('contact_type' => 'Individual', 'first_name' => 'B', 'last_name' => 'Good'));
377 $assignee = $this->callAPISuccess('Contact', 'create', array('contact_type' => 'Individual', 'first_name' => 'C', 'last_name' => 'Shore'));
378 $source = $this->callAPISuccess('Contact', 'create', array('contact_type' => 'Individual', 'first_name' => 'D', 'last_name' => 'Bug'));
379
380 $params = array(
381 'source_contact_id' => $source['id'],
382 'subject' => 'Make-it-Happen Meeting',
383 'activity_date_time' => '20170316',
384 'status_id' => 1,
385 'activity_type_id' => 1,
386 'target_contact_id' => array($target1['id'], $target2['id']),
387 'assignee_contact_id' => $assignee['id'],
388 );
389
390 $result = $this->callAPISuccess('activity', 'create', $params);
391 $result = $this->callAPIAndDocument('activity', 'getsingle', array(
392 'id' => $result['id'],
393 'return' => array('source_contact_name', 'target_contact_name', 'assignee_contact_name', 'subject'),
394 ), __FUNCTION__, __FILE__, $description, $subfile);
395
396 $this->assertEquals($params['subject'], $result['subject']);
397 $this->assertEquals($source['id'], $result['source_contact_id']);
398 $this->assertEquals('D Bug', $result['source_contact_name']);
399 $this->assertEquals('A Cat', $result['target_contact_name'][$target1['id']]);
400 $this->assertEquals('B Good', $result['target_contact_name'][$target2['id']]);
401 $this->assertEquals('C Shore', $result['assignee_contact_name'][$assignee['id']]);
402 $this->assertEquals($assignee['id'], $result['assignee_contact_id'][0]);
403 }
404
6a488035 405 /**
d177a2a6 406 * Test civicrm_activity_create() with valid parameters and custom data.
6a488035 407 */
00be9182 408 public function testActivityCreateCustom() {
6a488035
TO
409 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, __FILE__);
410 $params = $this->_params;
411 $params['custom_' . $ids['custom_field_id']] = "custom string";
9f1b81e0 412 $result = $this->callAPIAndDocument($this->_entity, 'create', $params, __FUNCTION__, __FILE__);
92915c55 413 $result = $this->callAPISuccess($this->_entity, 'get', array(
c301f76e 414 'return.custom_' . $ids['custom_field_id'] => 1,
c301f76e 415 'id' => $result['id'],
416 ));
6a488035
TO
417 $this->assertEquals("custom string", $result['values'][$result['id']]['custom_' . $ids['custom_field_id']], ' in line ' . __LINE__);
418
419 $this->customFieldDelete($ids['custom_field_id']);
420 $this->customGroupDelete($ids['custom_group_id']);
421 }
422
7b1dc1f6
PN
423 /**
424 * Test civicrm_activity_create() using example code.
425 */
426 public function testActivityCreateExample() {
427 require_once 'api/v3/examples/Activity/Create.php';
428 $result = activity_create_example();
429 $expectedResult = activity_create_expectedresult();
430 $this->assertEquals($result, $expectedResult);
431 }
432
b62bc939
EM
433 /**
434 * Test civicrm_activity_create() with valid parameters and custom data.
435 */
436 public function testActivityCreateCustomSubType() {
437 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, __FILE__);
438 $this->callAPISuccess('CustomGroup', 'create', array(
439 'extends_entity_column_value' => $this->test_activity_type_value,
440 'id' => $ids['custom_group_id'],
441 'extends' => 'Activity',
442 'is_active' => TRUE,
443 ));
444 $params = $this->_params;
445 $params['custom_' . $ids['custom_field_id']] = "custom string";
446 $result = $this->callAPIAndDocument($this->_entity, 'create', $params, __FUNCTION__, __FILE__);
447 $result = $this->callAPISuccess($this->_entity, 'get', array(
448 'return.custom_' . $ids['custom_field_id'] => 1,
449 'id' => $result['id'],
450 ));
451 $this->assertEquals("custom string", $result['values'][$result['id']]['custom_' . $ids['custom_field_id']]);
452
453 $this->customFieldDelete($ids['custom_field_id']);
454 $this->customGroupDelete($ids['custom_group_id']);
455 }
456
6a488035 457 /**
d177a2a6 458 * Test civicrm_activity_create() with valid parameters and custom data.
6a488035 459 */
00be9182 460 public function testActivityCreateCustomContactRefField() {
6a488035 461
b8a3da18 462 $this->callAPISuccess('contact', 'create', array('id' => $this->_contactID, 'sort_name' => 'Contact, Test'));
92915c55 463 $subfile = 'ContactRefCustomField';
5c49fee0 464 $description = "Demonstrates create with Contact Reference Custom Field.";
92915c55
TO
465 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, __FILE__);
466 $params = array(
6a488035
TO
467 'custom_group_id' => $ids['custom_group_id'],
468 'name' => 'Worker_Lookup',
469 'label' => 'Worker Lookup',
470 'html_type' => 'Autocomplete-Select',
471 'data_type' => 'ContactReference',
472 'weight' => 4,
473 'is_searchable' => 1,
474 'is_active' => 1,
6a488035
TO
475 );
476
b8a3da18 477 $customField = $this->callAPISuccess('custom_field', 'create', $params);
6a488035 478 $params = $this->_params;
b8a3da18 479 $params['custom_' . $customField['id']] = "$this->_contactID";
6a488035 480
a828d7b8 481 $result = $this->callAPIAndDocument($this->_entity, 'create', $params, __FUNCTION__, __FILE__, $description, $subfile);
92915c55 482 $result = $this->callAPIAndDocument($this->_entity, 'get', array(
c301f76e 483 'return.custom_' . $customField['id'] => 1,
484 'id' => $result['id'],
a828d7b8 485 ), __FUNCTION__, __FILE__, 'Get with Contact Ref Custom Field', 'ContactRefCustomFieldGet');
6a488035 486
e3aa0dd1 487 $this->assertEquals('Anderson, Anthony', $result['values'][$result['id']]['custom_' . $customField['id']]);
b8a3da18 488 $this->assertEquals($this->_contactID, $result['values'][$result['id']]['custom_' . $customField['id'] . "_id"], ' in line ' . __LINE__);
489 $this->assertEquals('Anderson, Anthony', $result['values'][$result['id']]['custom_' . $customField['id'] . '_1'], ' in line ' . __LINE__);
490 $this->assertEquals($this->_contactID, $result['values'][$result['id']]['custom_' . $customField['id'] . "_1_id"], ' in line ' . __LINE__);
6a488035
TO
491 $this->customFieldDelete($ids['custom_field_id']);
492 $this->customGroupDelete($ids['custom_group_id']);
493 }
494
495 /**
e3aa0dd1 496 * Test civicrm_activity_create() with an invalid text status_id.
6a488035 497 */
00be9182 498 public function testActivityCreateBadTextStatus() {
6a488035
TO
499
500 $params = array(
b8a3da18 501 'source_contact_id' => $this->_contactID,
6a488035
TO
502 'subject' => 'Discussion on Apis for v3',
503 'activity_date_time' => date('Ymd'),
504 'duration' => 120,
e3aa0dd1 505 'location' => 'Pennsylvania',
6a488035
TO
506 'details' => 'a test activity',
507 'status_id' => 'Invalid',
508 'activity_name' => 'Test activity type',
6a488035
TO
509 );
510
e3aa0dd1 511 $this->callAPIFailure('activity', 'create', $params);
feb2a730 512 }
513
514 /**
e3aa0dd1 515 * Test civicrm_activity_create() with an invalid text status_id.
feb2a730 516 */
00be9182 517 public function testActivityCreateSupportActivityStatus() {
feb2a730 518
519 $params = array(
b8a3da18 520 'source_contact_id' => $this->_contactID,
feb2a730 521 'subject' => 'Discussion on Apis for v3',
522 'activity_date_time' => date('Ymd'),
523 'duration' => 120,
e3aa0dd1 524 'location' => 'Pennsylvania',
feb2a730 525 'details' => 'a test activity',
526 'activity_status_id' => 'Invalid',
527 'activity_name' => 'Test activity type',
6a488035 528 );
feb2a730 529
b8a3da18 530 $result = $this->callAPIFailure('activity', 'create', $params,
531 "'Invalid' is not a valid option for field status_id");
6a488035
TO
532 }
533
feb2a730 534
6a488035 535 /**
e3aa0dd1 536 * Test civicrm_activity_create() with using a text status_id.
6a488035 537 */
00be9182 538 public function testActivityCreateTextStatus() {
6a488035 539
6a488035 540 $params = array(
b8a3da18 541 'source_contact_id' => $this->_contactID,
6a488035
TO
542 'subject' => 'Make-it-Happen Meeting',
543 'activity_date_time' => date('Ymd'),
544 'duration' => 120,
e3aa0dd1 545 'location' => 'Pennsylvania',
6a488035
TO
546 'details' => 'a test activity',
547 'status_id' => 'Scheduled',
548 'activity_name' => 'Test activity type',
6a488035
TO
549 );
550
b8a3da18 551 $result = $this->callAPISuccess('activity', 'create', $params);
e3aa0dd1 552 $this->assertEquals($result['values'][$result['id']]['duration'], 120);
553 $this->assertEquals($result['values'][$result['id']]['subject'], 'Make-it-Happen Meeting');
554 $this->assertEquals($result['values'][$result['id']]['activity_date_time'], date('Ymd') . '000000');
555 $this->assertEquals($result['values'][$result['id']]['location'], 'Pennsylvania');
556 $this->assertEquals($result['values'][$result['id']]['details'], 'a test activity');
6a488035
TO
557 }
558
559 /**
d177a2a6 560 * Test civicrm_activity_get() with no params
6a488035 561 */
00be9182 562 public function testActivityGetEmpty() {
b8a3da18 563 $result = $this->callAPISuccess('activity', 'get', array());
6a488035
TO
564 }
565
566 /**
d177a2a6 567 * Test civicrm_activity_get() with a good activity ID
6a488035 568 */
00be9182 569 public function testActivityGetGoodID1() {
5c49fee0 570 // Insert rows in civicrm_activity creating activities 4 and 13
a828d7b8 571 $description = "Demonstrates getting assignee_contact_id & using it to get the contact.";
92915c55
TO
572 $subfile = 'ReturnAssigneeContact';
573 $activity = $this->callAPISuccess('activity', 'create', $this->_params);
6a488035 574
b8a3da18 575 $contact = $this->callAPISuccess('Contact', 'Create', array(
92915c55
TO
576 'first_name' => "The Rock",
577 'last_name' => 'roccky',
578 'contact_type' => 'Individual',
579 'version' => 3,
580 'api.activity.create' => array(
581 'id' => $activity['id'],
582 'assignee_contact_id' => '$value.id',
583 ),
584 ));
6a488035
TO
585
586 $params = array(
587 'activity_id' => $activity['id'],
588 'version' => $this->_apiversion,
589 'sequential' => 1,
590 'return.assignee_contact_id' => 1,
591 'api.contact.get' => array(
592 'id' => '$value.source_contact_id',
593 ),
594 );
595
9f1b81e0 596 $result = $this->callAPIAndDocument('Activity', 'Get', $params, __FUNCTION__, __FILE__, $description, $subfile);
6a488035 597
ba4a1892 598 $this->assertEquals($activity['id'], $result['id']);
6a488035 599
ba4a1892 600 $this->assertEquals($contact['id'], $result['values'][0]['assignee_contact_id'][0]);
6a488035 601
ba4a1892
TM
602 $this->assertEquals($this->_contactID, $result['values'][0]['api.contact.get']['values'][0]['contact_id']);
603 $this->assertEquals($this->test_activity_type_value, $result['values'][0]['activity_type_id']);
604 $this->assertEquals("test activity type id", $result['values'][0]['subject']);
6a488035
TO
605 }
606
d424ffde 607 /**
eceb18cc 608 * test that get functioning does filtering.
42d30b83 609 */
00be9182 610 public function testGetFilter() {
6a488035 611 $params = array(
b8a3da18 612 'source_contact_id' => $this->_contactID,
6a488035
TO
613 'subject' => 'Make-it-Happen Meeting',
614 'activity_date_time' => '20110316',
615 'duration' => 120,
e3aa0dd1 616 'location' => 'Pennsylvania',
6a488035
TO
617 'details' => 'a test activity',
618 'status_id' => 1,
619 'activity_name' => 'Test activity type',
6a488035
TO
620 'priority_id' => 1,
621 );
9f1b81e0 622 $result = $this->callAPISuccess('Activity', 'Create', $params);
623 $this->callAPISuccess('Activity', 'Get', array('subject' => 'Make-it-Happen Meeting'));
6a488035
TO
624 $this->assertEquals(1, $result['count']);
625 $this->assertEquals('Make-it-Happen Meeting', $result['values'][$result['id']]['subject']);
9f1b81e0 626 $this->callAPISuccess('Activity', 'Delete', array('id' => $result['id']));
6a488035 627 }
6e1bb60c
N
628
629
630 /**
d177a2a6 631 * Test civicrm_activity_get() with filter target_contact_id
6e1bb60c 632 */
00be9182 633 public function testActivityGetTargetFilter() {
6e1bb60c
N
634 $params = $this->_params;
635 $contact1Params = array(
636 'first_name' => 'John',
637 'middle_name' => 'J.',
638 'last_name' => 'Anderson',
639 'prefix_id' => 3,
640 'suffix_id' => 3,
641 'email' => 'john_anderson@civicrm.org',
642 'contact_type' => 'Individual',
643 );
644
645 $contact1 = $this->individualCreate($contact1Params);
646 $contact2Params = array(
647 'first_name' => 'Michal',
648 'middle_name' => 'J.',
649 'last_name' => 'Anderson',
650 'prefix_id' => 3,
651 'suffix_id' => 3,
652 'email' => 'michal_anderson@civicrm.org',
653 'contact_type' => 'Individual',
654 );
655
656 $contact2 = $this->individualCreate($contact2Params);
657
658 $params['assignee_contact_id'] = array($contact1, $contact2);
659 $params['target_contact_id'] = array($contact2 => $contact2);
660 $activity = $this->callAPISuccess('Activity', 'Create', $params);
661
92915c55 662 $activityget = $this->callAPISuccess('Activity', 'get', array(
c301f76e 663 'id' => $activity['id'],
664 'target_contact_id' => $contact2,
665 'return.target_contact_id' => 1,
666 ));
ba4a1892
TM
667 $this->assertEquals($activity['id'], $activityget['id']);
668 $this->assertEquals($contact2, $activityget['values'][$activityget['id']]['target_contact_id'][0]);
6e1bb60c 669
92915c55 670 $activityget = $this->callAPISuccess('activity', 'get', array(
c301f76e 671 'target_contact_id' => $this->_contactID,
672 'return.target_contact_id' => 1,
673 'id' => $activity['id'],
674 ));
6e1bb60c 675 if ($activityget['count'] > 0) {
ba4a1892 676 $this->assertNotEquals($contact2, $activityget['values'][$activityget['id']]['target_contact_id'][0]);
6e1bb60c
N
677 }
678 }
679
28fc35eb
AS
680 /**
681 * Test that activity.get api works when filtering on subject.
682 */
f21fb92b 683 public function testActivityGetSubjectFilter() {
1196e086 684 $subject = 'test activity ' . __FUNCTION__ . mt_rand();
f21fb92b
AS
685 $params = $this->_params;
686 $params['subject'] = $subject;
687 $activity = $this->callAPISuccess('Activity', 'Create', $params);
688 $activityget = $this->callAPISuccess('activity', 'getsingle', array(
689 'subject' => $subject,
f21fb92b
AS
690 ));
691 $this->assertEquals($activityget['subject'], $subject);
692 }
693
28fc35eb
AS
694 /**
695 * Test that activity.get api works when filtering on details.
696 */
f21fb92b 697 public function testActivityGetDetailsFilter() {
1196e086 698 $details = 'test activity ' . __FUNCTION__ . mt_rand();
f21fb92b
AS
699 $params = $this->_params;
700 $params['details'] = $details;
701 $activity = $this->callAPISuccess('Activity', 'Create', $params);
702 $activityget = $this->callAPISuccess('activity', 'getsingle', array(
703 'details' => $details,
f21fb92b
AS
704 ));
705 $this->assertEquals($activityget['details'], $details);
706 }
707
1196e086
CW
708 /**
709 * Test that activity.get api works when filtering on tag.
710 */
711 public function testActivityGetTagFilter() {
712 $tag = $this->callAPISuccess('Tag', 'create', array('name' => mt_rand(), 'used_for' => 'Activities'));
713 $activity = $this->callAPISuccess('Activity', 'Create', $this->_params);
714 $this->callAPISuccess('EntityTag', 'create', array('entity_table' => 'civicrm_activity', 'tag_id' => $tag['id'], 'entity_id' => $activity['id']));
715 $activityget = $this->callAPISuccess('activity', 'getsingle', array(
716 'tag_id' => $tag['id'],
717 ));
718 $this->assertEquals($activityget['id'], $activity['id']);
719 }
720
40875a91
CW
721 /**
722 * Return tag info
723 */
724 public function testJoinOnTags() {
725 $tagName = 'act_tag_nm_' . mt_rand();
726 $tagDescription = 'act_tag_ds_' . mt_rand();
727 $tagColor = '#' . substr(md5(mt_rand()), 0, 6);
728 $tag = $this->callAPISuccess('Tag', 'create', array('name' => $tagName, 'color' => $tagColor, 'description' => $tagDescription, 'used_for' => 'Activities'));
729 $activity = $this->callAPISuccess('Activity', 'Create', $this->_params);
730 $this->callAPISuccess('EntityTag', 'create', array('entity_table' => 'civicrm_activity', 'tag_id' => $tag['id'], 'entity_id' => $activity['id']));
731 $activityget = $this->callAPISuccess('activity', 'getsingle', array(
732 'id' => $activity['id'],
733 'return' => array('tag_id.name', 'tag_id.description', 'tag_id.color'),
734 ));
735 $this->assertEquals($tagName, $activityget['tag_id'][$tag['id']]['tag_id.name']);
736 $this->assertEquals($tagColor, $activityget['tag_id'][$tag['id']]['tag_id.color']);
737 $this->assertEquals($tagDescription, $activityget['tag_id'][$tag['id']]['tag_id.description']);
738 }
739
740
741 /**
742 * Test that activity.get api works to filter on and return files.
743 */
744 public function testActivityGetFile() {
745 $activity = $this->callAPISuccess('Activity', 'create', $this->_params);
746 $activity2 = $this->callAPISuccess('Activity', 'create', $this->_params2);
747 $file = $this->callAPISuccess('Attachment', 'create', array(
748 'name' => 'actAttachment.txt',
749 'mime_type' => 'text/plain',
750 'description' => 'My test description',
751 'content' => 'My test content',
752 'entity_table' => 'civicrm_activity',
753 'entity_id' => $activity2['id'],
754 ));
755 $activityget = $this->callAPISuccess('activity', 'getsingle', array(
756 'file_id' => $file['id'],
757 'return' => 'file_id',
758 ));
759 $this->assertEquals($activityget['id'], $activity2['id']);
760 $this->assertEquals($file['id'], $activityget['file_id'][0]);
761 }
762
d424ffde 763 /**
eceb18cc 764 * test that get functioning does filtering.
42d30b83 765 */
00be9182 766 public function testGetStatusID() {
6a488035 767 $params = array(
b8a3da18 768 'source_contact_id' => $this->_contactID,
6a488035
TO
769 'subject' => 'Make-it-Happen Meeting',
770 'activity_date_time' => '20110316',
771 'duration' => 120,
e3aa0dd1 772 'location' => 'Pennsylvania',
6a488035
TO
773 'details' => 'a test activity',
774 'status_id' => 1,
775 'activity_name' => 'Test activity type',
6a488035
TO
776 'priority_id' => 1,
777 );
b8a3da18 778 $this->callAPISuccess('Activity', 'Create', $params);
2b46f1da 779 $result = $this->callAPISuccess('Activity', 'Get', array('activity_status_id' => '1'));
6a488035
TO
780 $this->assertEquals(1, $result['count'], 'one activity of status 1 should exist');
781
2b46f1da 782 $result = $this->callAPISuccess('Activity', 'Get', array('status_id' => '1'));
6a488035
TO
783 $this->assertEquals(1, $result['count'], 'status_id should also work');
784
2b46f1da 785 $result = $this->callAPISuccess('Activity', 'Get', array('activity_status_id' => '2'));
6a488035 786 $this->assertEquals(0, $result['count'], 'No activities of status 1 should exist');
b8a3da18 787 $result = $this->callAPISuccess('Activity', 'Get', array(
92915c55 788 'version' => $this->_apiversion,
c301f76e 789 'status_id' => '2',
92915c55 790 ));
6a488035
TO
791 $this->assertEquals(0, $result['count'], 'No activities of status 1 should exist');
792
42d30b83 793 }
6a488035 794
d424ffde 795 /**
eceb18cc 796 * test that get functioning does filtering.
42d30b83 797 */
00be9182 798 public function testGetFilterMaxDate() {
6a488035 799 $params = array(
b8a3da18 800 'source_contact_id' => $this->_contactID,
6a488035
TO
801 'subject' => 'Make-it-Happen Meeting',
802 'activity_date_time' => '20110101',
803 'duration' => 120,
e3aa0dd1 804 'location' => 'Pennsylvania',
6a488035
TO
805 'details' => 'a test activity',
806 'status_id' => 1,
807 'activity_name' => 'Test activity type',
808 'version' => $this->_apiversion,
809 'priority_id' => 1,
810 );
b8a3da18 811 $activityOne = $this->callAPISuccess('Activity', 'Create', $params);
6a488035 812 $params['activity_date_time'] = 20120216;
b8a3da18 813 $activityTwo = $this->callAPISuccess('Activity', 'Create', $params);
814 $result = $this->callAPISuccess('Activity', 'Get', array(
92915c55
TO
815 'version' => 3,
816 ));
5c49fee0 817 $description = "Demonstrates _low filter (at time of writing doesn't work if contact_id is set.";
6a488035
TO
818 $subfile = "DateTimeLow";
819 $this->assertEquals(2, $result['count']);
820 $params = array(
821 'version' => 3,
822 'filter.activity_date_time_low' => '20120101000000',
823 'sequential' => 1,
824 );
9f1b81e0 825 $result = $this->callAPIAndDocument('Activity', 'Get', $params, __FUNCTION__, __FILE__, $description, $subfile);
e3aa0dd1 826 $this->assertEquals(1, $result['count']);
5c49fee0 827 $description = "Demonstrates _high filter (at time of writing doesn't work if contact_id is set.";
6a488035 828 $subfile = "DateTimeHigh";
e3aa0dd1 829 $this->assertEquals('2012-02-16 00:00:00', $result['values'][0]['activity_date_time']);
6a488035 830 $params = array(
b8a3da18 831 'source_contact_id' => $this->_contactID,
6a488035
TO
832 'version' => 3,
833 'filter.activity_date_time_high' => '20120101000000',
834 'sequential' => 1,
835 );
9f1b81e0 836 $result = $this->callAPIAndDocument('Activity', 'Get', $params, __FUNCTION__, __FILE__, $description, $subfile);
6a488035
TO
837
838 $this->assertEquals(1, $result['count']);
e3aa0dd1 839 $this->assertEquals('2011-01-01 00:00:00', $result['values'][0]['activity_date_time']);
6a488035 840
b8a3da18 841 $this->callAPISuccess('Activity', 'Delete', array('version' => 3, 'id' => $activityOne['id']));
842 $this->callAPISuccess('Activity', 'Delete', array('version' => 3, 'id' => $activityTwo['id']));
6a488035
TO
843 }
844
845 /**
d177a2a6
EM
846 * Test civicrm_activity_get() with a good activity ID which
847 * has associated custom data
6a488035 848 */
00be9182 849 public function testActivityGetGoodIDCustom() {
6a488035
TO
850 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, __FILE__);
851
852 $params = $this->_params;
853 $params['custom_' . $ids['custom_field_id']] = "custom string";
854
e3aa0dd1 855 $this->callAPISuccess($this->_entity, 'create', $params);
9f1b81e0 856
e3aa0dd1 857 // Retrieve the test value.
6a488035
TO
858 $params = array(
859 'activity_type_id' => $this->test_activity_type_value,
6a488035
TO
860 'sequential' => 1,
861 'return.custom_' . $ids['custom_field_id'] => 1,
862 );
9f1b81e0 863 $result = $this->callAPIAndDocument('activity', 'get', $params, __FUNCTION__, __FILE__);
864 $this->assertEquals("custom string", $result['values'][0]['custom_' . $ids['custom_field_id']]);
6a488035 865
9f1b81e0 866 $this->assertEquals($this->test_activity_type_value, $result['values'][0]['activity_type_id']);
ba4a1892 867 $this->assertEquals('test activity type id', $result['values'][0]['subject']);
6a488035
TO
868 $this->customFieldDelete($ids['custom_field_id']);
869 $this->customGroupDelete($ids['custom_group_id']);
870 }
871
872 /**
d177a2a6
EM
873 * Test civicrm_activity_get() with a good activity ID which
874 * has associated custom data
6a488035 875 */
00be9182 876 public function testActivityGetContact_idCustom() {
6a488035
TO
877 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, __FILE__);
878
879 $params = $this->_params;
880 $params['custom_' . $ids['custom_field_id']] = "custom string";
881
b8a3da18 882 $result = $this->callAPISuccess($this->_entity, 'create', $params);
e3aa0dd1 883 // Retrieve the test value
6a488035
TO
884 $params = array(
885 'contact_id' => $this->_params['source_contact_id'],
886 'activity_type_id' => $this->test_activity_type_value,
6a488035
TO
887 'sequential' => 1,
888 'return.custom_' . $ids['custom_field_id'] => 1,
889 );
9f1b81e0 890 $result = $this->callAPIAndDocument('activity', 'get', $params, __FUNCTION__, __FILE__);
6a488035
TO
891 $this->assertEquals("custom string", $result['values'][0]['custom_' . $ids['custom_field_id']], ' in line ' . __LINE__);
892
ba4a1892
TM
893 $this->assertEquals($this->test_activity_type_value, $result['values'][0]['activity_type_id']);
894 $this->assertEquals('test activity type id', $result['values'][0]['subject']);
e3aa0dd1 895 $this->assertEquals($result['values'][0]['id'], $result['id']);
6a488035
TO
896 }
897
898 /**
e3aa0dd1 899 * Check activity deletion with empty params.
6a488035 900 */
00be9182 901 public function testDeleteActivityForEmptyParams() {
6a488035 902 $params = array('version' => $this->_apiversion);
e3aa0dd1 903 $this->callAPIFailure('activity', 'delete', $params);
6a488035
TO
904 }
905
906 /**
e3aa0dd1 907 * Check activity deletion without activity id.
6a488035 908 */
00be9182 909 public function testDeleteActivityWithoutId() {
6a488035
TO
910 $params = array(
911 'activity_name' => 'Meeting',
912 'version' => $this->_apiversion,
913 );
d0e1eff2 914 $result = $this->callAPIFailure('activity', 'delete', $params);
6a488035
TO
915 }
916
917 /**
eceb18cc 918 * Check activity deletion without activity type.
6a488035 919 */
00be9182 920 public function testDeleteActivityWithoutActivityType() {
6a488035 921 $params = array('id' => 1);
d0e1eff2 922 $result = $this->callAPIFailure('activity', 'delete', $params);
6a488035
TO
923 }
924
925 /**
eceb18cc 926 * Check activity deletion with incorrect data.
6a488035 927 */
00be9182 928 public function testDeleteActivityWithIncorrectActivityType() {
6a488035
TO
929 $params = array(
930 'id' => 1,
931 'activity_name' => 'Test Activity',
932 );
933
d0e1eff2 934 $result = $this->callAPIFailure('activity', 'delete', $params);
6a488035
TO
935 }
936
937 /**
eceb18cc 938 * Check activity deletion with correct data.
6a488035 939 */
00be9182 940 public function testDeleteActivity() {
b8a3da18 941 $result = $this->callAPISuccess('activity', 'create', $this->_params);
6a488035
TO
942 $params = array(
943 'id' => $result['id'],
944 'version' => $this->_apiversion,
945 );
946
a828d7b8 947 $this->callAPIAndDocument('activity', 'delete', $params, __FUNCTION__, __FILE__);
6a488035
TO
948 }
949
6a488035 950 /**
eceb18cc 951 * Check if required fields are not passed.
6a488035 952 */
00be9182 953 public function testActivityUpdateWithoutRequired() {
6a488035
TO
954 $params = array(
955 'subject' => 'this case should fail',
956 'scheduled_date_time' => date('Ymd'),
957 );
958
d0e1eff2 959 $result = $this->callAPIFailure('activity', 'create', $params);
6a488035
TO
960 }
961
962 /**
963 * Test civicrm_activity_update() with non-numeric id
964 */
00be9182 965 public function testActivityUpdateWithNonNumericId() {
6a488035
TO
966 $params = array(
967 'id' => 'lets break it',
968 'activity_name' => 'Meeting',
969 'subject' => 'this case should fail',
970 'scheduled_date_time' => date('Ymd'),
971 );
972
d0e1eff2 973 $result = $this->callAPIFailure('activity', 'create', $params);
6a488035
TO
974 }
975
976 /**
eceb18cc 977 * Check with incorrect required fields.
6a488035 978 */
00be9182 979 public function testActivityUpdateWithIncorrectContactActivityType() {
6a488035
TO
980 $params = array(
981 'id' => 1,
982 'activity_name' => 'Test Activity',
983 'subject' => 'this case should fail',
984 'scheduled_date_time' => date('Ymd'),
b8a3da18 985 'source_contact_id' => $this->_contactID,
6a488035
TO
986 );
987
b8a3da18 988 $result = $this->callAPIFailure('activity', 'create', $params,
989 'Invalid Activity Id');
6a488035
TO
990 }
991
992 /**
d177a2a6 993 * Test civicrm_activity_update() to update an existing activity
6a488035 994 */
00be9182 995 public function testActivityUpdate() {
b8a3da18 996 $result = $this->callAPISuccess('activity', 'create', $this->_params);
6a488035
TO
997
998 $params = array(
999 'id' => $result['id'],
1000 'subject' => 'Make-it-Happen Meeting',
1001 'activity_date_time' => '20091011123456',
1002 'duration' => 120,
1003 'location' => '21, Park Avenue',
1004 'details' => 'Lets update Meeting',
1005 'status_id' => 1,
b8a3da18 1006 'source_contact_id' => $this->_contactID,
6a488035 1007 'priority_id' => 1,
6a488035
TO
1008 );
1009
b8a3da18 1010 $result = $this->callAPISuccess('activity', 'create', $params);
6a488035
TO
1011 //hack on date comparison - really we should make getAndCheck smarter to handle dates
1012 $params['activity_date_time'] = '2009-10-11 12:34:56';
42d30b83
DL
1013 // we also unset source_contact_id since it is stored in an aux table
1014 unset($params['source_contact_id']);
6a488035
TO
1015 $this->getAndCheck($params, $result['id'], 'activity');
1016 }
1017
1018 /**
d177a2a6
EM
1019 * Test civicrm_activity_update() with valid parameters
1020 * and some custom data
6a488035 1021 */
00be9182 1022 public function testActivityUpdateCustom() {
6a488035
TO
1023 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, __FILE__);
1024
1025 $params = $this->_params;
1026
e3aa0dd1 1027 // Create an activity with custom data
6a488035
TO
1028 //this has been updated from the previous 'old format' function - need to make it work
1029 $params = array(
b8a3da18 1030 'source_contact_id' => $this->_contactID,
6a488035
TO
1031 'subject' => 'Make-it-Happen Meeting',
1032 'activity_date_time' => '2009-10-18',
1033 'duration' => 120,
e3aa0dd1 1034 'location' => 'Pennsylvania',
6a488035
TO
1035 'details' => 'a test activity to check the update api',
1036 'status_id' => 1,
1037 'activity_name' => 'Test activity type',
1038 'version' => $this->_apiversion,
1039 'custom_' . $ids['custom_field_id'] => 'custom string',
1040 );
b8a3da18 1041 $result = $this->callAPISuccess('activity', 'create', $params);
6a488035
TO
1042
1043 $activityId = $result['id'];
92915c55 1044 $result = $this->callAPISuccess($this->_entity, 'get', array(
c301f76e 1045 'return.custom_' . $ids['custom_field_id'] => 1,
1046 'version' => 3,
1047 'id' => $result['id'],
1048 ));
e3aa0dd1 1049 $this->assertEquals("custom string", $result['values'][$result['id']]['custom_' . $ids['custom_field_id']]);
1050 $this->assertEquals("2009-10-18 00:00:00", $result['values'][$result['id']]['activity_date_time']);
b8a3da18 1051 $fields = $this->callAPISuccess('activity', 'getfields', array('version' => $this->_apiversion));
6a488035
TO
1052 $this->assertTrue(is_array($fields['values']['custom_' . $ids['custom_field_id']]));
1053
e3aa0dd1 1054 // Update the activity with custom data.
6a488035
TO
1055 $params = array(
1056 'id' => $activityId,
b8a3da18 1057 'source_contact_id' => $this->_contactID,
6a488035
TO
1058 'subject' => 'Make-it-Happen Meeting',
1059 'status_id' => 1,
1060 'activity_name' => 'Test activity type',
1061 // add this since dates are messed up
1062 'activity_date_time' => date('Ymd'),
1063 'custom_' . $ids['custom_field_id'] => 'Updated my test data',
1064 'version' => $this->_apiversion,
1065 );
b8a3da18 1066 $result = $this->callAPISuccess('Activity', 'Create', $params);
6a488035 1067
92915c55 1068 $result = $this->callAPISuccess($this->_entity, 'get', array(
c301f76e 1069 'return.custom_' . $ids['custom_field_id'] => 1,
1070 'version' => 3,
1071 'id' => $result['id'],
1072 ));
e3aa0dd1 1073 $this->assertEquals("Updated my test data", $result['values'][$result['id']]['custom_' . $ids['custom_field_id']]);
6a488035
TO
1074 }
1075
1076 /**
d177a2a6
EM
1077 * Test civicrm_activity_update() for core activity fields
1078 * and some custom data
6a488035 1079 */
00be9182 1080 public function testActivityUpdateCheckCoreFields() {
6a488035
TO
1081 $params = $this->_params;
1082 $contact1Params = array(
1083 'first_name' => 'John',
1084 'middle_name' => 'J.',
1085 'last_name' => 'Anderson',
1086 'prefix_id' => 3,
1087 'suffix_id' => 3,
1088 'email' => 'john_anderson@civicrm.org',
1089 'contact_type' => 'Individual',
1090 );
1091
1092 $contact1 = $this->individualCreate($contact1Params);
1093 $contact2Params = array(
1094 'first_name' => 'Michal',
1095 'middle_name' => 'J.',
1096 'last_name' => 'Anderson',
1097 'prefix_id' => 3,
1098 'suffix_id' => 3,
1099 'email' => 'michal_anderson@civicrm.org',
1100 'contact_type' => 'Individual',
1101 );
1102
1103 $contact2 = $this->individualCreate($contact2Params);
1104
1105 $params['assignee_contact_id'] = array($contact1, $contact2);
1106 $params['target_contact_id'] = array($contact2 => $contact2);
b8a3da18 1107 $result = $this->callAPISuccess('Activity', 'Create', $params);
6a488035 1108
6a488035 1109 $activityId = $result['id'];
6a488035
TO
1110 $getParams = array(
1111 'return.assignee_contact_id' => 1,
1112 'return.target_contact_id' => 1,
1113 'version' => $this->_apiversion,
1114 'id' => $activityId,
1115 );
481a74f4 1116 $result = $this->callAPISuccess($this->_entity, 'get', $getParams);
6a488035
TO
1117 $assignee = $result['values'][$result['id']]['assignee_contact_id'];
1118 $target = $result['values'][$result['id']]['target_contact_id'];
1119 $this->assertEquals(2, count($assignee), ' in line ' . __LINE__);
1120 $this->assertEquals(1, count($target), ' in line ' . __LINE__);
1121 $this->assertEquals(TRUE, in_array($contact1, $assignee), ' in line ' . __LINE__);
1122 $this->assertEquals(TRUE, in_array($contact2, $target), ' in line ' . __LINE__);
1123
1124 $contact3Params = array(
1125 'first_name' => 'Jijo',
1126 'middle_name' => 'J.',
1127 'last_name' => 'Anderson',
1128 'prefix_id' => 3,
1129 'suffix_id' => 3,
1130 'email' => 'jijo_anderson@civicrm.org',
1131 'contact_type' => 'Individual',
1132 );
1133
1134 $contact4Params = array(
1135 'first_name' => 'Grant',
1136 'middle_name' => 'J.',
1137 'last_name' => 'Anderson',
1138 'prefix_id' => 3,
1139 'suffix_id' => 3,
1140 'email' => 'grant_anderson@civicrm.org',
1141 'contact_type' => 'Individual',
1142 );
1143
1144 $contact3 = $this->individualCreate($contact3Params);
1145 $contact4 = $this->individualCreate($contact4Params);
1146
1147 $params = array();
1148 $params['id'] = $activityId;
6a488035
TO
1149 $params['assignee_contact_id'] = array($contact3 => $contact3);
1150 $params['target_contact_id'] = array($contact4 => $contact4);
1151
b8a3da18 1152 $result = $this->callAPISuccess('activity', 'create', $params);
6a488035
TO
1153
1154 $this->assertEquals($activityId, $result['id'], ' in line ' . __LINE__);
1155
b8a3da18 1156 $result = $this->callAPISuccess(
42d30b83
DL
1157 $this->_entity,
1158 'get',
1159 array(
1160 'return.assignee_contact_id' => 1,
1161 'return.target_contact_id' => 1,
1162 'return.source_contact_id' => 1,
21dfd5f5 1163 'id' => $result['id'],
42d30b83
DL
1164 )
1165 );
6a488035
TO
1166
1167 $assignee = $result['values'][$result['id']]['assignee_contact_id'];
1168 $target = $result['values'][$result['id']]['target_contact_id'];
1169
1170 $this->assertEquals(1, count($assignee), ' in line ' . __LINE__);
1171 $this->assertEquals(1, count($target), ' in line ' . __LINE__);
1172 $this->assertEquals(TRUE, in_array($contact3, $assignee), ' in line ' . __LINE__);
1173 $this->assertEquals(TRUE, in_array($contact4, $target), ' in line ' . __LINE__);
7b1dc1f6 1174 $this->_params['activity_type_id'] = $this->test_activity_type_value;
6a488035 1175 foreach ($this->_params as $fld => $val) {
9f1b81e0 1176 $this->assertEquals($val, $result['values'][$result['id']][$fld]);
6a488035
TO
1177 }
1178 }
1179
1180 /**
d177a2a6
EM
1181 * Test civicrm_activity_update() where the DB has a date_time
1182 * value and there is none in the update params.
6a488035 1183 */
00be9182 1184 public function testActivityUpdateNotDate() {
b8a3da18 1185 $result = $this->callAPISuccess('activity', 'create', $this->_params);
6a488035
TO
1186
1187 $params = array(
1188 'id' => $result['id'],
1189 'subject' => 'Make-it-Happen Meeting',
1190 'duration' => 120,
1191 'location' => '21, Park Avenue',
1192 'details' => 'Lets update Meeting',
1193 'status_id' => 1,
b8a3da18 1194 'source_contact_id' => $this->_contactID,
6a488035 1195 'priority_id' => 1,
6a488035
TO
1196 );
1197
b8a3da18 1198 $result = $this->callAPISuccess('activity', 'create', $params);
6a488035
TO
1199 //hack on date comparison - really we should make getAndCheck smarter to handle dates
1200 $params['activity_date_time'] = $this->_params['activity_date_time'];
42d30b83
DL
1201 // we also unset source_contact_id since it is stored in an aux table
1202 unset($params['source_contact_id']);
6a488035
TO
1203 $this->getAndCheck($params, $result['id'], 'activity');
1204 }
1205
1206 /**
eceb18cc 1207 * Check activity update with status.
6a488035 1208 */
00be9182 1209 public function testActivityUpdateWithStatus() {
b8a3da18 1210 $activity = $this->callAPISuccess('activity', 'create', $this->_params);
6a488035
TO
1211 $params = array(
1212 'id' => $activity['id'],
b8a3da18 1213 'source_contact_id' => $this->_contactID,
6a488035
TO
1214 'subject' => 'Hurry update works',
1215 'status_id' => 1,
1216 'activity_name' => 'Test activity type',
6a488035
TO
1217 );
1218
b8a3da18 1219 $result = $this->callAPISuccess('activity', 'create', $params);
9f1b81e0 1220 $this->assertEquals($result['id'], $activity['id']);
1221 $this->assertEquals($result['values'][$activity['id']]['subject'], 'Hurry update works');
1222 $this->assertEquals($result['values'][$activity['id']]['status_id'], 1
6a488035
TO
1223 );
1224 }
1225
1226 /**
d177a2a6
EM
1227 * Test civicrm_activity_update() where the source_contact_id
1228 * is not in the update params.
6a488035 1229 */
00be9182 1230 public function testActivityUpdateKeepSource() {
b8a3da18 1231 $activity = $this->callAPISuccess('activity', 'create', $this->_params);
e3aa0dd1 1232 // Updating the activity but not providing anything for the source contact
1233 // (It was set as $this->_contactID earlier.)
6a488035
TO
1234 $params = array(
1235 'id' => $activity['id'],
1236 'subject' => 'Updated Make-it-Happen Meeting',
1237 'duration' => 120,
1238 'location' => '21, Park Avenue',
1239 'details' => 'Lets update Meeting',
1240 'status_id' => 1,
1241 'activity_name' => 'Test activity type',
1242 'priority_id' => 1,
6a488035
TO
1243 );
1244
b8a3da18 1245 $result = $this->callAPISuccess('activity', 'create', $params);
9f1b81e0 1246 $findactivity = $this->callAPISuccess('Activity', 'Get', array('id' => $activity['id']));
6a488035
TO
1247 }
1248
1249 /**
d177a2a6 1250 * Test civicrm_activities_contact_get()
6a488035 1251 */
00be9182 1252 public function testActivitiesContactGet() {
b8a3da18 1253 $activity = $this->callAPISuccess('activity', 'create', $this->_params);
1254 $activity2 = $this->callAPISuccess('activity', 'create', $this->_params2);
e3aa0dd1 1255 // Get activities associated with contact $this->_contactID.
6a488035 1256 $params = array(
b8a3da18 1257 'contact_id' => $this->_contactID,
6a488035 1258 );
b8a3da18 1259 $result = $this->callAPISuccess('activity', 'get', $params);
6a488035 1260
ba4a1892 1261 $this->assertEquals(2, $result['count']);
e3aa0dd1 1262 $this->assertEquals($this->test_activity_type_value, $result['values'][$activity['id']]['activity_type_id']);
1263 $this->assertEquals('Test activity type', $result['values'][$activity['id']]['activity_name']);
1264 $this->assertEquals('Test activity type', $result['values'][$activity2['id']]['activity_name']);
6a488035 1265 }
92915c55 1266
d424ffde 1267 /**
e3aa0dd1 1268 * Test chained Activity format.
42d30b83 1269 */
e3aa0dd1 1270 public function testChainedActivityGet() {
6a488035 1271
b8a3da18 1272 $activity = $this->callAPISuccess('Contact', 'Create', array(
92915c55
TO
1273 'display_name' => "bob brown",
1274 'contact_type' => 'Individual',
1275 'api.activity_type.create' => array(
1276 'weight' => '2',
1277 'label' => 'send out letters',
1278 'filter' => 0,
1279 'is_active' => 1,
1280 'is_optgroup' => 1,
1281 'is_default' => 0,
1282 ),
1283 'api.activity.create' => array(
1284 'subject' => 'send letter',
c301f76e 1285 'activity_type_id' => '$value.api.activity_type.create.values.0.value',
92915c55
TO
1286 ),
1287 ));
6a488035 1288
b8a3da18 1289 $result = $this->callAPISuccess('Activity', 'Get', array(
92915c55
TO
1290 'id' => $activity['id'],
1291 'return.assignee_contact_id' => 1,
1292 'api.contact.get' => array('api.pledge.get' => 1),
1293 ));
6a488035
TO
1294 }
1295
6a488035 1296 /**
e3aa0dd1 1297 * Test civicrm_activity_contact_get() with invalid Contact ID.
6a488035 1298 */
00be9182 1299 public function testActivitiesContactGetWithInvalidContactId() {
6a488035 1300 $params = array('contact_id' => 'contact');
e3aa0dd1 1301 $this->callAPIFailure('activity', 'get', $params);
6a488035
TO
1302 }
1303
1304 /**
e3aa0dd1 1305 * Test civicrm_activity_contact_get() with contact having no Activity.
6a488035 1306 */
00be9182 1307 public function testActivitiesContactGetHavingNoActivity() {
6a488035
TO
1308 $params = array(
1309 'first_name' => 'dan',
1310 'last_name' => 'conberg',
1311 'email' => 'dan.conberg@w.co.in',
1312 'contact_type' => 'Individual',
6a488035
TO
1313 );
1314
b8a3da18 1315 $contact = $this->callAPISuccess('contact', 'create', $params);
6a488035
TO
1316 $params = array(
1317 'contact_id' => $contact['id'],
6a488035 1318 );
b8a3da18 1319 $result = $this->callAPISuccess('activity', 'get', $params);
e3aa0dd1 1320 $this->assertEquals($result['count'], 0);
6a488035
TO
1321 }
1322
e3aa0dd1 1323 /**
1324 * Test getfields function.
1325 */
00be9182 1326 public function testGetFields() {
b8a3da18 1327 $params = array('action' => 'create');
a828d7b8 1328 $result = $this->callAPIAndDocument('activity', 'getfields', $params, __FUNCTION__, __FILE__, NULL, NULL);
e3aa0dd1 1329 $this->assertTrue(is_array($result['values']), 'get fields doesn\'t return values array');
6a488035 1330 foreach ($result['values'] as $key => $value) {
e3aa0dd1 1331 $this->assertTrue(is_array($value), $key . " is not an array");
6a488035
TO
1332 }
1333 }
96025800 1334
fe59b2a7
CW
1335 public function testGetWithOr() {
1336 $acts = array(
1337 'test or 1' => 'orOperator',
1338 'test or 2' => 'orOperator',
1339 'test or 3' => 'nothing',
1340 );
1341 foreach ($acts as $subject => $details) {
1342 $params = $this->_params;
1343 $params['subject'] = $subject;
1344 $params['details'] = $details;
1345 $this->callAPISuccess('Activity', 'create', $params);
1346 }
1347 $result = $this->callAPISuccess('Activity', 'get', array(
1348 'details' => 'orOperator',
1349 ));
1350 $this->assertEquals(2, $result['count']);
1351 $result = $this->callAPISuccess('Activity', 'get', array(
1352 'details' => 'orOperator',
1353 'subject' => 'test or 3',
1354 ));
1355 $this->assertEquals(0, $result['count']);
1356 $result = $this->callAPISuccess('Activity', 'get', array(
1357 'details' => 'orOperator',
1358 'subject' => 'test or 3',
1359 'options' => array('or' => array(array('details', 'subject'))),
1360 ));
1361 $this->assertEquals(3, $result['count']);
1362 }
1363
6a488035 1364}