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