Merge pull request #17641 from MegaphoneJon/core-1590
[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 * @throws \CRM_Core_Exception
323 */
324 public function testActivityCreate() {
325
326 $this->callAPISuccess('activity', 'create', $this->_params);
327 $result = $this->callAPISuccess('activity', 'get', $this->_params);
328 $this->assertEquals($result['values'][$result['id']]['duration'], 120);
329 $this->assertEquals($result['values'][$result['id']]['subject'], 'test activity type id');
330 $this->assertEquals($result['values'][$result['id']]['activity_date_time'], '2011-06-02 14:36:13');
331 $this->assertEquals($result['values'][$result['id']]['location'], 'Pennsylvania');
332 $this->assertEquals($result['values'][$result['id']]['details'], 'a test activity');
333 $this->assertEquals($result['values'][$result['id']]['status_id'], 2);
334 $this->assertEquals($result['values'][$result['id']]['id'], $result['id']);
335 }
336
337 /**
338 * Test civicrm_activity_create() with valid parameters - use type_id.
339 * @param int $version
340 * @dataProvider versionThreeAndFour
341 */
342 public function testActivityCreateCampaignTypeID($version) {
343 $this->_apiversion = $version;
344 $this->enableCiviCampaign();
345
346 $params = [
347 'source_contact_id' => $this->_contactID,
348 'subject' => 'Make-it-Happen Meeting',
349 'activity_date_time' => '20110316',
350 'duration' => 120,
351 'location' => 'Pennsylvania',
352 'details' => 'a test activity',
353 'status_id' => 1,
354 'activity_type_id' => 29,
355 ];
356
357 $result = $this->callAPISuccess('activity', 'create', $params);
358
359 $result = $this->callAPISuccess('activity', 'getsingle', ['id' => $result['id']]);
360 $this->assertEquals($result['duration'], 120);
361 $this->assertEquals($result['subject'], 'Make-it-Happen Meeting');
362 $this->assertEquals($result['activity_date_time'], '2011-03-16 00:00:00');
363 $this->assertEquals($result['location'], 'Pennsylvania');
364 $this->assertEquals($result['details'], 'a test activity');
365 $this->assertEquals($result['status_id'], 1);
366
367 $priorities = $this->callAPISuccess('activity', 'getoptions', ['field' => 'priority_id']);
368 $this->assertEquals($result['priority_id'], array_search('Normal', $priorities['values']));
369 }
370
371 /**
372 * Test get returns target and assignee contacts.
373 * @dataProvider versionThreeAndFour
374 * @var int $version
375 */
376 public function testActivityReturnTargetAssignee($version) {
377 $this->_apiversion = $version;
378 $description = "Demonstrates setting & retrieving activity target & source.";
379 $subfile = "GetTargetandAssignee";
380 $params = [
381 'source_contact_id' => $this->_contactID,
382 'subject' => 'Make-it-Happen Meeting',
383 'activity_date_time' => '20110316',
384 'duration' => 120,
385 'location' => 'Pennsylvania',
386 'details' => 'a test activity',
387 'status_id' => 1,
388 'activity_type_id' => 1,
389 'priority_id' => 1,
390 'target_contact_id' => $this->_contactID,
391 'assignee_contact_id' => $this->_contactID,
392 ];
393
394 $result = $this->callAPIAndDocument('activity', 'create', $params, __FUNCTION__, __FILE__, $description, $subfile);
395
396 $actContacts = $this->callAPISuccess('ActivityContact', 'get', [
397 'activity_id' => $result['id'],
398 ]);
399 $this->assertCount(3, $actContacts['values']);
400
401 if ($version === 4) {
402 $this->markTestIncomplete("APIv4 doesn't retrieve activity contacts directly.");
403 }
404
405 $result = $this->callAPISuccess('activity', 'get', [
406 'id' => $result['id'],
407 'return.assignee_contact_id' => 1,
408 'return.target_contact_id' => 1,
409 ]);
410
411 $this->assertEquals($this->_contactID, $result['values'][$result['id']]['assignee_contact_id'][0]);
412 $this->assertEquals($this->_contactID, $result['values'][$result['id']]['target_contact_id'][0]);
413 }
414
415 /**
416 * Test get returns target and assignee contact names.
417 */
418 public function testActivityReturnTargetAssigneeName() {
419
420 $description = "Demonstrates retrieving activity target & source contact names.";
421 $subfile = "GetTargetandAssigneeName";
422 $target1 = $this->callAPISuccess('Contact', 'create', ['contact_type' => 'Individual', 'first_name' => 'A', 'last_name' => 'Cat']);
423 $target2 = $this->callAPISuccess('Contact', 'create', ['contact_type' => 'Individual', 'first_name' => 'B', 'last_name' => 'Good']);
424 $assignee = $this->callAPISuccess('Contact', 'create', ['contact_type' => 'Individual', 'first_name' => 'C', 'last_name' => 'Shore']);
425 $source = $this->callAPISuccess('Contact', 'create', ['contact_type' => 'Individual', 'first_name' => 'D', 'last_name' => 'Bug']);
426
427 $params = [
428 'source_contact_id' => $source['id'],
429 'subject' => 'Make-it-Happen Meeting',
430 'activity_date_time' => '20170316',
431 'status_id' => 1,
432 'activity_type_id' => 1,
433 'target_contact_id' => [$target1['id'], $target2['id']],
434 'assignee_contact_id' => $assignee['id'],
435 ];
436
437 $result = $this->callAPISuccess('activity', 'create', $params);
438 $result = $this->callAPIAndDocument('activity', 'getsingle', [
439 'id' => $result['id'],
440 'return' => ['source_contact_name', 'target_contact_name', 'assignee_contact_name', 'subject'],
441 ], __FUNCTION__, __FILE__, $description, $subfile);
442
443 $this->assertEquals($params['subject'], $result['subject']);
444 $this->assertEquals($source['id'], $result['source_contact_id']);
445 $this->assertEquals('D Bug', $result['source_contact_name']);
446 $this->assertEquals('A Cat', $result['target_contact_name'][$target1['id']]);
447 $this->assertEquals('B Good', $result['target_contact_name'][$target2['id']]);
448 $this->assertEquals('C Shore', $result['assignee_contact_name'][$assignee['id']]);
449 $this->assertEquals($assignee['id'], $result['assignee_contact_id'][0]);
450 }
451
452 /**
453 * Test civicrm_activity_create() with valid parameters and custom data.
454 */
455 public function testActivityCreateCustom() {
456 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, __FILE__);
457 $params = $this->_params;
458 $params['custom_' . $ids['custom_field_id']] = "custom string";
459 $result = $this->callAPIAndDocument($this->_entity, 'create', $params, __FUNCTION__, __FILE__);
460 $result = $this->callAPISuccess($this->_entity, 'get', [
461 'return.custom_' . $ids['custom_field_id'] => 1,
462 'id' => $result['id'],
463 ]);
464 $this->assertEquals("custom string", $result['values'][$result['id']]['custom_' . $ids['custom_field_id']], ' in line ' . __LINE__);
465
466 $this->customFieldDelete($ids['custom_field_id']);
467 $this->customGroupDelete($ids['custom_group_id']);
468 }
469
470 /**
471 * Test civicrm_activity_create() using example code.
472 */
473 public function testActivityCreateExample() {
474 require_once 'api/v3/examples/Activity/Create.ex.php';
475 $result = activity_create_example();
476 $expectedResult = activity_create_expectedresult();
477 // Compare everything *except* timestamps.
478 unset($result['values'][1]['created_date']);
479 unset($result['values'][1]['modified_date']);
480 unset($expectedResult['values'][1]['created_date']);
481 unset($expectedResult['values'][1]['modified_date']);
482 $this->assertEquals($result, $expectedResult);
483 }
484
485 /**
486 * Test civicrm_activity_create() with valid parameters and custom data.
487 */
488 public function testActivityCreateCustomSubType() {
489 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, __FILE__);
490 $this->callAPISuccess('CustomGroup', 'create', [
491 'extends_entity_column_value' => $this->test_activity_type_value,
492 'id' => $ids['custom_group_id'],
493 'extends' => 'Activity',
494 'is_active' => TRUE,
495 ]);
496 $params = $this->_params;
497 $params['custom_' . $ids['custom_field_id']] = "custom string";
498 $result = $this->callAPIAndDocument($this->_entity, 'create', $params, __FUNCTION__, __FILE__);
499 $result = $this->callAPISuccess($this->_entity, 'get', [
500 'return.custom_' . $ids['custom_field_id'] => 1,
501 'id' => $result['id'],
502 ]);
503 $this->assertEquals("custom string", $result['values'][$result['id']]['custom_' . $ids['custom_field_id']]);
504
505 $this->customFieldDelete($ids['custom_field_id']);
506 $this->customGroupDelete($ids['custom_group_id']);
507 }
508
509 /**
510 * Test civicrm_activity_create() with valid parameters and custom data.
511 */
512 public function testActivityCreateCustomContactRefField() {
513
514 $this->callAPISuccess('contact', 'create', ['id' => $this->_contactID, 'sort_name' => 'Contact, Test']);
515 $subfile = 'ContactRefCustomField';
516 $description = "Demonstrates create with Contact Reference Custom Field.";
517 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, __FILE__);
518 $params = [
519 'custom_group_id' => $ids['custom_group_id'],
520 'name' => 'Worker_Lookup',
521 'label' => 'Worker Lookup',
522 'html_type' => 'Autocomplete-Select',
523 'data_type' => 'ContactReference',
524 'weight' => 4,
525 'is_searchable' => 1,
526 'is_active' => 1,
527 ];
528
529 $customField = $this->callAPISuccess('custom_field', 'create', $params);
530 $params = $this->_params;
531 $params['custom_' . $customField['id']] = "$this->_contactID";
532
533 $result = $this->callAPIAndDocument($this->_entity, 'create', $params, __FUNCTION__, __FILE__, $description, $subfile);
534 $result = $this->callAPIAndDocument($this->_entity, 'get', [
535 'return.custom_' . $customField['id'] => 1,
536 'id' => $result['id'],
537 ], __FUNCTION__, __FILE__, 'Get with Contact Ref Custom Field', 'ContactRefCustomFieldGet');
538
539 $this->assertEquals('Anderson, Anthony', $result['values'][$result['id']]['custom_' . $customField['id']]);
540 $this->assertEquals($this->_contactID, $result['values'][$result['id']]['custom_' . $customField['id'] . "_id"], ' in line ' . __LINE__);
541 $this->assertEquals('Anderson, Anthony', $result['values'][$result['id']]['custom_' . $customField['id'] . '_1'], ' in line ' . __LINE__);
542 $this->assertEquals($this->_contactID, $result['values'][$result['id']]['custom_' . $customField['id'] . "_1_id"], ' in line ' . __LINE__);
543 $this->customFieldDelete($ids['custom_field_id']);
544 $this->customGroupDelete($ids['custom_group_id']);
545 }
546
547 /**
548 * Test civicrm_activity_create() with an invalid text status_id.
549 */
550 public function testActivityCreateBadTextStatus() {
551
552 $params = [
553 'source_contact_id' => $this->_contactID,
554 'subject' => 'Discussion on Apis for v3',
555 'activity_date_time' => date('Ymd'),
556 'duration' => 120,
557 'location' => 'Pennsylvania',
558 'details' => 'a test activity',
559 'status_id' => 'Invalid',
560 'activity_name' => 'Test activity type',
561 ];
562
563 $this->callAPIFailure('activity', 'create', $params);
564 }
565
566 /**
567 * Test civicrm_activity_create() with an invalid text status_id.
568 */
569 public function testActivityCreateSupportActivityStatus() {
570
571 $params = [
572 'source_contact_id' => $this->_contactID,
573 'subject' => 'Discussion on Apis for v3',
574 'activity_date_time' => date('Ymd'),
575 'duration' => 120,
576 'location' => 'Pennsylvania',
577 'details' => 'a test activity',
578 'activity_status_id' => 'Invalid',
579 'activity_name' => 'Test activity type',
580 ];
581
582 $this->callAPIFailure('activity', 'create', $params,
583 "'Invalid' is not a valid option for field status_id");
584 }
585
586 /**
587 * Test civicrm_activity_create() with using a text status_id.
588 */
589 public function testActivityCreateTextStatus() {
590
591 $params = [
592 'source_contact_id' => $this->_contactID,
593 'subject' => 'Make-it-Happen Meeting',
594 'activity_date_time' => date('Ymd'),
595 'duration' => 120,
596 'location' => 'Pennsylvania',
597 'details' => 'a test activity',
598 'status_id' => 'Scheduled',
599 'activity_name' => 'Test activity type',
600 ];
601
602 $result = $this->callAPISuccess('activity', 'create', $params);
603 $this->assertEquals($result['values'][$result['id']]['duration'], 120);
604 $this->assertEquals($result['values'][$result['id']]['subject'], 'Make-it-Happen Meeting');
605 $this->assertEquals($result['values'][$result['id']]['activity_date_time'], date('Ymd') . '000000');
606 $this->assertEquals($result['values'][$result['id']]['location'], 'Pennsylvania');
607 $this->assertEquals($result['values'][$result['id']]['details'], 'a test activity');
608 }
609
610 /**
611 * Test civicrm_activity_get() with no params
612 */
613 public function testActivityGetEmpty() {
614 $this->callAPISuccess('activity', 'get', []);
615 }
616
617 /**
618 * Test civicrm_activity_get() with a good activity ID
619 */
620 public function testActivityGetGoodID1() {
621 // Insert rows in civicrm_activity creating activities 4 and 13
622 $description = "Demonstrates getting assignee_contact_id & using it to get the contact.";
623 $subfile = 'ReturnAssigneeContact';
624 $activity = $this->callAPISuccess('activity', 'create', $this->_params);
625
626 $contact = $this->callAPISuccess('Contact', 'Create', [
627 'first_name' => "The Rock",
628 'last_name' => 'roccky',
629 'contact_type' => 'Individual',
630 'version' => 3,
631 'api.activity.create' => [
632 'id' => $activity['id'],
633 'assignee_contact_id' => '$value.id',
634 ],
635 ]);
636
637 $params = [
638 'activity_id' => $activity['id'],
639 'version' => $this->_apiversion,
640 'sequential' => 1,
641 'return.assignee_contact_id' => 1,
642 'api.contact.get' => [
643 'id' => '$value.source_contact_id',
644 ],
645 ];
646
647 $result = $this->callAPIAndDocument('Activity', 'Get', $params, __FUNCTION__, __FILE__, $description, $subfile);
648
649 $this->assertEquals($activity['id'], $result['id']);
650
651 $this->assertEquals($contact['id'], $result['values'][0]['assignee_contact_id'][0]);
652
653 $this->assertEquals($this->_contactID, $result['values'][0]['api.contact.get']['values'][0]['contact_id']);
654 $this->assertEquals($this->test_activity_type_value, $result['values'][0]['activity_type_id']);
655 $this->assertEquals("test activity type id", $result['values'][0]['subject']);
656 }
657
658 /**
659 * test that get functioning does filtering.
660 */
661 public function testGetFilter() {
662 $params = [
663 'source_contact_id' => $this->_contactID,
664 'subject' => 'Make-it-Happen Meeting',
665 'activity_date_time' => '20110316',
666 'duration' => 120,
667 'location' => 'Pennsylvania',
668 'details' => 'a test activity',
669 'status_id' => 1,
670 'activity_name' => 'Test activity type',
671 'priority_id' => 1,
672 ];
673 $result = $this->callAPISuccess('Activity', 'Create', $params);
674 $this->callAPISuccess('Activity', 'Get', ['subject' => 'Make-it-Happen Meeting']);
675 $this->assertEquals(1, $result['count']);
676 $this->assertEquals('Make-it-Happen Meeting', $result['values'][$result['id']]['subject']);
677 $this->callAPISuccess('Activity', 'Delete', ['id' => $result['id']]);
678 }
679
680 /**
681 * Test civicrm_activity_get() with filter target_contact_id
682 */
683 public function testActivityGetTargetFilter() {
684 $params = $this->_params;
685 $contact1Params = [
686 'first_name' => 'John',
687 'middle_name' => 'J.',
688 'last_name' => 'Anderson',
689 'prefix_id' => 3,
690 'suffix_id' => 3,
691 'email' => 'john_anderson@civicrm.org',
692 'contact_type' => 'Individual',
693 ];
694
695 $contact1 = $this->individualCreate($contact1Params);
696 $contact2Params = [
697 'first_name' => 'Michal',
698 'middle_name' => 'J.',
699 'last_name' => 'Anderson',
700 'prefix_id' => 3,
701 'suffix_id' => 3,
702 'email' => 'michal_anderson@civicrm.org',
703 'contact_type' => 'Individual',
704 ];
705
706 $contact2 = $this->individualCreate($contact2Params);
707
708 $this->callAPISuccess('OptionValue', 'get', ['name' => 'Activity Targets', 'api.OptionValue.create' => ['label' => 'oh so creative']]);
709
710 $params['assignee_contact_id'] = [$contact1, $contact2];
711 $params['target_contact_id'] = [$contact2 => $contact2];
712 $activity = $this->callAPISuccess('Activity', 'Create', $params);
713
714 $activityGet = $this->callAPISuccess('Activity', 'get', [
715 'id' => $activity['id'],
716 'target_contact_id' => $contact2,
717 'return.target_contact_id' => 1,
718 ]);
719 $this->assertEquals($activity['id'], $activityGet['id']);
720 $this->assertEquals($contact2, $activityGet['values'][$activityGet['id']]['target_contact_id'][0]);
721
722 $activityGet = $this->callAPISuccess('activity', 'get', [
723 'target_contact_id' => $this->_contactID,
724 'return.target_contact_id' => 1,
725 'id' => $activity['id'],
726 ]);
727 if ($activityGet['count'] > 0) {
728 $this->assertNotEquals($contact2, $activityGet['values'][$activityGet['id']]['target_contact_id'][0]);
729 }
730 }
731
732 /**
733 * Test that activity.get api works when filtering on subject.
734 */
735 public function testActivityGetSubjectFilter() {
736 $subject = 'test activity ' . __FUNCTION__ . mt_rand();
737 $params = $this->_params;
738 $params['subject'] = $subject;
739 $this->callAPISuccess('Activity', 'Create', $params);
740 $activityGet = $this->callAPISuccess('activity', 'getsingle', [
741 'subject' => $subject,
742 ]);
743 $this->assertEquals($activityGet['subject'], $subject);
744 }
745
746 /**
747 * Test that activity.get api works when filtering on details.
748 */
749 public function testActivityGetDetailsFilter() {
750 $details = 'test activity ' . __FUNCTION__ . mt_rand();
751 $params = $this->_params;
752 $params['details'] = $details;
753 $activity = $this->callAPISuccess('Activity', 'Create', $params);
754 $activityget = $this->callAPISuccess('activity', 'getsingle', [
755 'details' => $details,
756 ]);
757 $this->assertEquals($activityget['details'], $details);
758 }
759
760 /**
761 * Test that activity.get api works when filtering on tag.
762 */
763 public function testActivityGetTagFilter() {
764 $tag = $this->callAPISuccess('Tag', 'create', ['name' => mt_rand(), 'used_for' => 'Activities']);
765 $activity = $this->callAPISuccess('Activity', 'Create', $this->_params);
766 $this->callAPISuccess('EntityTag', 'create', ['entity_table' => 'civicrm_activity', 'tag_id' => $tag['id'], 'entity_id' => $activity['id']]);
767 $activityget = $this->callAPISuccess('activity', 'getsingle', [
768 'tag_id' => $tag['id'],
769 ]);
770 $this->assertEquals($activityget['id'], $activity['id']);
771 }
772
773 /**
774 * Return tag info
775 */
776 public function testJoinOnTags() {
777 $tagName = 'act_tag_nm_' . mt_rand();
778 $tagDescription = 'act_tag_ds_' . mt_rand();
779 $tagColor = '#' . substr(md5(mt_rand()), 0, 6);
780 $tag = $this->callAPISuccess('Tag', 'create', ['name' => $tagName, 'color' => $tagColor, 'description' => $tagDescription, 'used_for' => 'Activities']);
781 $activity = $this->callAPISuccess('Activity', 'Create', $this->_params);
782 $this->callAPISuccess('EntityTag', 'create', ['entity_table' => 'civicrm_activity', 'tag_id' => $tag['id'], 'entity_id' => $activity['id']]);
783 $activityget = $this->callAPISuccess('activity', 'getsingle', [
784 'id' => $activity['id'],
785 'return' => ['tag_id.name', 'tag_id.description', 'tag_id.color'],
786 ]);
787 $this->assertEquals($tagName, $activityget['tag_id'][$tag['id']]['tag_id.name']);
788 $this->assertEquals($tagColor, $activityget['tag_id'][$tag['id']]['tag_id.color']);
789 $this->assertEquals($tagDescription, $activityget['tag_id'][$tag['id']]['tag_id.description']);
790 }
791
792 /**
793 * Test that activity.get api works to filter on and return files.
794 */
795 public function testActivityGetFile() {
796 $activity = $this->callAPISuccess('Activity', 'create', $this->_params);
797 $activity2 = $this->callAPISuccess('Activity', 'create', $this->_params2);
798 $file = $this->callAPISuccess('Attachment', 'create', [
799 'name' => 'actAttachment.txt',
800 'mime_type' => 'text/plain',
801 'description' => 'My test description',
802 'content' => 'My test content',
803 'entity_table' => 'civicrm_activity',
804 'entity_id' => $activity2['id'],
805 ]);
806 $activityget = $this->callAPISuccess('activity', 'getsingle', [
807 'file_id' => $file['id'],
808 'return' => 'file_id',
809 ]);
810 $this->assertEquals($activityget['id'], $activity2['id']);
811 $this->assertEquals($file['id'], $activityget['file_id'][0]);
812 }
813
814 /**
815 * test that get functioning does filtering.
816 */
817 public function testGetStatusID() {
818 $params = [
819 'source_contact_id' => $this->_contactID,
820 'subject' => 'Make-it-Happen Meeting',
821 'activity_date_time' => '20110316',
822 'duration' => 120,
823 'location' => 'Pennsylvania',
824 'details' => 'a test activity',
825 'status_id' => 1,
826 'activity_name' => 'Test activity type',
827 'priority_id' => 1,
828 ];
829 $this->callAPISuccess('Activity', 'Create', $params);
830 $result = $this->callAPISuccess('Activity', 'Get', ['activity_status_id' => '1']);
831 $this->assertEquals(1, $result['count'], 'one activity of status 1 should exist');
832
833 $result = $this->callAPISuccess('Activity', 'Get', ['status_id' => '1']);
834 $this->assertEquals(1, $result['count'], 'status_id should also work');
835
836 $result = $this->callAPISuccess('Activity', 'Get', ['activity_status_id' => '2']);
837 $this->assertEquals(0, $result['count'], 'No activities of status 1 should exist');
838 $result = $this->callAPISuccess('Activity', 'Get', [
839 'version' => $this->_apiversion,
840 'status_id' => '2',
841 ]);
842 $this->assertEquals(0, $result['count'], 'No activities of status 1 should exist');
843
844 }
845
846 /**
847 * test that get functioning does filtering.
848 */
849 public function testGetFilterMaxDate() {
850 $params = [
851 'source_contact_id' => $this->_contactID,
852 'subject' => 'Make-it-Happen Meeting',
853 'activity_date_time' => '20110101',
854 'duration' => 120,
855 'location' => 'Pennsylvania',
856 'details' => 'a test activity',
857 'status_id' => 1,
858 'activity_name' => 'Test activity type',
859 'version' => $this->_apiversion,
860 'priority_id' => 1,
861 ];
862 $activityOne = $this->callAPISuccess('Activity', 'Create', $params);
863 $params['activity_date_time'] = 20120216;
864 $activityTwo = $this->callAPISuccess('Activity', 'Create', $params);
865 $result = $this->callAPISuccess('Activity', 'Get', [
866 'version' => 3,
867 ]);
868 $description = "Demonstrates _low filter (at time of writing doesn't work if contact_id is set.";
869 $subfile = "DateTimeLow";
870 $this->assertEquals(2, $result['count']);
871 $params = [
872 'version' => 3,
873 'filter.activity_date_time_low' => '20120101000000',
874 'sequential' => 1,
875 ];
876 $result = $this->callAPIAndDocument('Activity', 'Get', $params, __FUNCTION__, __FILE__, $description, $subfile);
877 $this->assertEquals(1, $result['count']);
878 $description = "Demonstrates _high filter (at time of writing doesn't work if contact_id is set.";
879 $subfile = "DateTimeHigh";
880 $this->assertEquals('2012-02-16 00:00:00', $result['values'][0]['activity_date_time']);
881 $params = [
882 'source_contact_id' => $this->_contactID,
883 'version' => 3,
884 'filter.activity_date_time_high' => '20120101000000',
885 'sequential' => 1,
886 ];
887 $result = $this->callAPIAndDocument('Activity', 'Get', $params, __FUNCTION__, __FILE__, $description, $subfile);
888
889 $this->assertEquals(1, $result['count']);
890 $this->assertEquals('2011-01-01 00:00:00', $result['values'][0]['activity_date_time']);
891
892 $this->callAPISuccess('Activity', 'Delete', ['version' => 3, 'id' => $activityOne['id']]);
893 $this->callAPISuccess('Activity', 'Delete', ['version' => 3, 'id' => $activityTwo['id']]);
894 }
895
896 /**
897 * Test civicrm_activity_get() with a good activity ID which
898 * has associated custom data
899 */
900 public function testActivityGetGoodIDCustom() {
901 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, __FILE__);
902
903 $params = $this->_params;
904 $params['custom_' . $ids['custom_field_id']] = "custom string";
905
906 $this->callAPISuccess($this->_entity, 'create', $params);
907
908 // Retrieve the test value.
909 $params = [
910 'activity_type_id' => $this->test_activity_type_value,
911 'sequential' => 1,
912 'return.custom_' . $ids['custom_field_id'] => 1,
913 ];
914 $result = $this->callAPIAndDocument('activity', 'get', $params, __FUNCTION__, __FILE__);
915 $this->assertEquals("custom string", $result['values'][0]['custom_' . $ids['custom_field_id']]);
916
917 $this->assertEquals($this->test_activity_type_value, $result['values'][0]['activity_type_id']);
918 $this->assertEquals('test activity type id', $result['values'][0]['subject']);
919 $this->customFieldDelete($ids['custom_field_id']);
920 $this->customGroupDelete($ids['custom_group_id']);
921 }
922
923 /**
924 * Test civicrm_activity_get() with a good activity ID which
925 * has associated custom data
926 */
927 public function testActivityGetContact_idCustom() {
928 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, __FILE__);
929
930 $params = $this->_params;
931 $params['custom_' . $ids['custom_field_id']] = "custom string";
932
933 $result = $this->callAPISuccess($this->_entity, 'create', $params);
934 // Retrieve the test value
935 $params = [
936 'contact_id' => $this->_params['source_contact_id'],
937 'activity_type_id' => $this->test_activity_type_value,
938 'sequential' => 1,
939 'return.custom_' . $ids['custom_field_id'] => 1,
940 ];
941 $result = $this->callAPIAndDocument('activity', 'get', $params, __FUNCTION__, __FILE__);
942 $this->assertEquals("custom string", $result['values'][0]['custom_' . $ids['custom_field_id']], ' in line ' . __LINE__);
943
944 $this->assertEquals($this->test_activity_type_value, $result['values'][0]['activity_type_id']);
945 $this->assertEquals('test activity type id', $result['values'][0]['subject']);
946 $this->assertEquals($result['values'][0]['id'], $result['id']);
947 }
948
949 /**
950 * Check activity deletion without activity id.
951 */
952 public function testDeleteActivityWithoutId() {
953 $this->callAPIFailure('activity', 'delete', ['activity_name' => 'Meeting'], 'Mandatory key(s) missing from params array: id');
954 }
955
956 /**
957 * Check activity deletion without activity type.
958 */
959 public function testDeleteActivityWithInvalidID() {
960 $this->callAPIFailure('activity', 'delete', ['id' => 1], 'Could not delete Activity: 1');
961 }
962
963 /**
964 * Check activity deletion with correct data.
965 */
966 public function testDeleteActivity() {
967 $result = $this->callAPISuccess('activity', 'create', $this->_params);
968 $params = ['id' => $result['id']];
969 $this->callAPIAndDocument('activity', 'delete', $params, __FUNCTION__, __FILE__);
970 }
971
972 /**
973 * Check if required fields are not passed.
974 */
975 public function testActivityUpdateWithoutRequired() {
976 $this->callAPIFailure('activity', 'create', [
977 'subject' => 'this case should fail',
978 'scheduled_date_time' => date('Ymd'),
979 ]);
980 }
981
982 /**
983 * Test civicrm_activity_update() with non-numeric id
984 */
985 public function testActivityUpdateWithNonNumericId() {
986 $params = [
987 'id' => 'lets break it',
988 'activity_name' => 'Meeting',
989 'subject' => 'this case should fail',
990 'scheduled_date_time' => date('Ymd'),
991 ];
992
993 $result = $this->callAPIFailure('activity', 'create', $params);
994 }
995
996 /**
997 * Check with incorrect required fields.
998 */
999 public function testActivityUpdateWithIncorrectContactActivityType() {
1000 $params = [
1001 'id' => 1,
1002 'activity_name' => 'Test Activity',
1003 'subject' => 'this case should fail',
1004 'scheduled_date_time' => date('Ymd'),
1005 'source_contact_id' => $this->_contactID,
1006 ];
1007
1008 $result = $this->callAPIFailure('activity', 'create', $params,
1009 'Invalid Activity Id');
1010 }
1011
1012 /**
1013 * Test civicrm_activity_update() to update an existing activity
1014 */
1015 public function testActivityUpdate() {
1016 $result = $this->callAPISuccess('activity', 'create', $this->_params);
1017 $this->_contactID2 = $this->individualCreate();
1018
1019 $params = [
1020 'id' => $result['id'],
1021 'subject' => 'Make-it-Happen Meeting',
1022 'activity_date_time' => '20091011123456',
1023 'duration' => 120,
1024 'location' => '21, Park Avenue',
1025 'details' => 'Lets update Meeting',
1026 'status_id' => 1,
1027 'source_contact_id' => $this->_contactID,
1028 'assignee_contact_id' => $this->_contactID2,
1029 'priority_id' => 1,
1030 ];
1031
1032 $result = $this->callAPISuccess('activity', 'create', $params);
1033 //hack on date comparison - really we should make getAndCheck smarter to handle dates
1034 $params['activity_date_time'] = '2009-10-11 12:34:56';
1035 // we also unset source_contact_id since it is stored in an aux table
1036 unset($params['source_contact_id']);
1037 //Check if assignee created.
1038 $assignee = $this->callAPISuccess('ActivityContact', 'get', [
1039 'activity_id' => $result['id'],
1040 'return' => ["contact_id"],
1041 'record_type_id' => "Activity Assignees",
1042 ]);
1043 $this->assertNotEmpty($assignee['values']);
1044
1045 //clear assignee contacts.
1046 $updateParams = [
1047 'id' => $result['id'],
1048 'assignee_contact_id' => [],
1049 ];
1050 $activity = $this->callAPISuccess('activity', 'create', $updateParams);
1051 $assignee = $this->callAPISuccess('ActivityContact', 'get', [
1052 'activity_id' => $activity['id'],
1053 'return' => ["contact_id"],
1054 'record_type_id' => "Activity Assignees",
1055 ]);
1056 $this->assertEmpty($assignee['values']);
1057 $this->getAndCheck($params, $result['id'], 'activity');
1058 }
1059
1060 /**
1061 * Test civicrm_activity_update() with valid parameters
1062 * and some custom data
1063 */
1064 public function testActivityUpdateCustom() {
1065 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, __FILE__);
1066
1067 $params = $this->_params;
1068
1069 // Create an activity with custom data
1070 //this has been updated from the previous 'old format' function - need to make it work
1071 $params = [
1072 'source_contact_id' => $this->_contactID,
1073 'subject' => 'Make-it-Happen Meeting',
1074 'activity_date_time' => '2009-10-18',
1075 'duration' => 120,
1076 'location' => 'Pennsylvania',
1077 'details' => 'a test activity to check the update api',
1078 'status_id' => 1,
1079 'activity_name' => 'Test activity type',
1080 'version' => $this->_apiversion,
1081 'custom_' . $ids['custom_field_id'] => 'custom string',
1082 ];
1083 $result = $this->callAPISuccess('activity', 'create', $params);
1084
1085 $activityId = $result['id'];
1086 $result = $this->callAPISuccess($this->_entity, 'get', [
1087 'return.custom_' . $ids['custom_field_id'] => 1,
1088 'version' => 3,
1089 'id' => $result['id'],
1090 ]);
1091 $this->assertEquals("custom string", $result['values'][$result['id']]['custom_' . $ids['custom_field_id']]);
1092 $this->assertEquals("2009-10-18 00:00:00", $result['values'][$result['id']]['activity_date_time']);
1093 $fields = $this->callAPISuccess('activity', 'getfields', ['version' => $this->_apiversion]);
1094 $this->assertTrue(is_array($fields['values']['custom_' . $ids['custom_field_id']]));
1095
1096 // Update the activity with custom data.
1097 $params = [
1098 'id' => $activityId,
1099 'source_contact_id' => $this->_contactID,
1100 'subject' => 'Make-it-Happen Meeting',
1101 'status_id' => 1,
1102 'activity_name' => 'Test activity type',
1103 // add this since dates are messed up
1104 'activity_date_time' => date('Ymd'),
1105 'custom_' . $ids['custom_field_id'] => 'Updated my test data',
1106 'version' => $this->_apiversion,
1107 ];
1108 $result = $this->callAPISuccess('Activity', 'Create', $params);
1109
1110 $result = $this->callAPISuccess($this->_entity, 'get', [
1111 'return.custom_' . $ids['custom_field_id'] => 1,
1112 'version' => 3,
1113 'id' => $result['id'],
1114 ]);
1115 $this->assertEquals("Updated my test data", $result['values'][$result['id']]['custom_' . $ids['custom_field_id']]);
1116 }
1117
1118 /**
1119 * Test civicrm_activity_update() for core activity fields
1120 * and some custom data
1121 */
1122 public function testActivityUpdateCheckCoreFields() {
1123 $params = $this->_params;
1124 $contact1Params = [
1125 'first_name' => 'John',
1126 'middle_name' => 'J.',
1127 'last_name' => 'Anderson',
1128 'prefix_id' => 3,
1129 'suffix_id' => 3,
1130 'email' => 'john_anderson@civicrm.org',
1131 'contact_type' => 'Individual',
1132 ];
1133
1134 $contact1 = $this->individualCreate($contact1Params);
1135 $contact2Params = [
1136 'first_name' => 'Michal',
1137 'middle_name' => 'J.',
1138 'last_name' => 'Anderson',
1139 'prefix_id' => 3,
1140 'suffix_id' => 3,
1141 'email' => 'michal_anderson@civicrm.org',
1142 'contact_type' => 'Individual',
1143 ];
1144
1145 $contact2 = $this->individualCreate($contact2Params);
1146
1147 $params['assignee_contact_id'] = [$contact1, $contact2];
1148 $params['target_contact_id'] = [$contact2 => $contact2];
1149 $result = $this->callAPISuccess('Activity', 'Create', $params);
1150
1151 $activityId = $result['id'];
1152 $getParams = [
1153 'return.assignee_contact_id' => 1,
1154 'return.target_contact_id' => 1,
1155 'version' => $this->_apiversion,
1156 'id' => $activityId,
1157 ];
1158 $result = $this->callAPISuccess($this->_entity, 'get', $getParams);
1159 $assignee = $result['values'][$result['id']]['assignee_contact_id'];
1160 $target = $result['values'][$result['id']]['target_contact_id'];
1161 $this->assertEquals(2, count($assignee), ' in line ' . __LINE__);
1162 $this->assertEquals(1, count($target), ' in line ' . __LINE__);
1163 $this->assertEquals(TRUE, in_array($contact1, $assignee), ' in line ' . __LINE__);
1164 $this->assertEquals(TRUE, in_array($contact2, $target), ' in line ' . __LINE__);
1165
1166 $contact3Params = [
1167 'first_name' => 'Jijo',
1168 'middle_name' => 'J.',
1169 'last_name' => 'Anderson',
1170 'prefix_id' => 3,
1171 'suffix_id' => 3,
1172 'email' => 'jijo_anderson@civicrm.org',
1173 'contact_type' => 'Individual',
1174 ];
1175
1176 $contact4Params = [
1177 'first_name' => 'Grant',
1178 'middle_name' => 'J.',
1179 'last_name' => 'Anderson',
1180 'prefix_id' => 3,
1181 'suffix_id' => 3,
1182 'email' => 'grant_anderson@civicrm.org',
1183 'contact_type' => 'Individual',
1184 ];
1185
1186 $contact3 = $this->individualCreate($contact3Params);
1187 $contact4 = $this->individualCreate($contact4Params);
1188
1189 $params = [];
1190 $params['id'] = $activityId;
1191 $params['assignee_contact_id'] = [$contact3 => $contact3];
1192 $params['target_contact_id'] = [$contact4 => $contact4];
1193
1194 $result = $this->callAPISuccess('activity', 'create', $params);
1195
1196 $this->assertEquals($activityId, $result['id'], ' in line ' . __LINE__);
1197
1198 $result = $this->callAPISuccess(
1199 $this->_entity,
1200 'get',
1201 [
1202 'return.assignee_contact_id' => 1,
1203 'return.target_contact_id' => 1,
1204 'return.source_contact_id' => 1,
1205 'id' => $result['id'],
1206 ]
1207 );
1208
1209 $assignee = $result['values'][$result['id']]['assignee_contact_id'];
1210 $target = $result['values'][$result['id']]['target_contact_id'];
1211
1212 $this->assertEquals(1, count($assignee), ' in line ' . __LINE__);
1213 $this->assertEquals(1, count($target), ' in line ' . __LINE__);
1214 $this->assertEquals(TRUE, in_array($contact3, $assignee), ' in line ' . __LINE__);
1215 $this->assertEquals(TRUE, in_array($contact4, $target), ' in line ' . __LINE__);
1216 $this->_params['activity_type_id'] = $this->test_activity_type_value;
1217 foreach ($this->_params as $fld => $val) {
1218 $this->assertEquals($val, $result['values'][$result['id']][$fld]);
1219 }
1220 }
1221
1222 /**
1223 * Test civicrm_activity_update() where the DB has a date_time
1224 * value and there is none in the update params.
1225 */
1226 public function testActivityUpdateNotDate() {
1227 $result = $this->callAPISuccess('activity', 'create', $this->_params);
1228
1229 $params = [
1230 'id' => $result['id'],
1231 'subject' => 'Make-it-Happen Meeting',
1232 'duration' => 120,
1233 'location' => '21, Park Avenue',
1234 'details' => 'Lets update Meeting',
1235 'status_id' => 1,
1236 'source_contact_id' => $this->_contactID,
1237 'priority_id' => 1,
1238 ];
1239
1240 $result = $this->callAPISuccess('activity', 'create', $params);
1241 //hack on date comparison - really we should make getAndCheck smarter to handle dates
1242 $params['activity_date_time'] = $this->_params['activity_date_time'];
1243 // we also unset source_contact_id since it is stored in an aux table
1244 unset($params['source_contact_id']);
1245 $this->getAndCheck($params, $result['id'], 'activity');
1246 }
1247
1248 /**
1249 * Check activity update with status.
1250 */
1251 public function testActivityUpdateWithStatus() {
1252 $activity = $this->callAPISuccess('activity', 'create', $this->_params);
1253 $params = [
1254 'id' => $activity['id'],
1255 'source_contact_id' => $this->_contactID,
1256 'subject' => 'Hurry update works',
1257 'status_id' => 1,
1258 'activity_name' => 'Test activity type',
1259 ];
1260
1261 $result = $this->callAPISuccess('activity', 'create', $params);
1262 $this->assertEquals($result['id'], $activity['id']);
1263 $this->assertEquals($result['values'][$activity['id']]['subject'], 'Hurry update works');
1264 $this->assertEquals($result['values'][$activity['id']]['status_id'], 1
1265 );
1266 }
1267
1268 /**
1269 * Test civicrm_activity_update() where the source_contact_id
1270 * is not in the update params.
1271 */
1272 public function testActivityUpdateKeepSource() {
1273 $activity = $this->callAPISuccess('activity', 'create', $this->_params);
1274 // Updating the activity but not providing anything for the source contact
1275 // (It was set as $this->_contactID earlier.)
1276 $params = [
1277 'id' => $activity['id'],
1278 'subject' => 'Updated Make-it-Happen Meeting',
1279 'duration' => 120,
1280 'location' => '21, Park Avenue',
1281 'details' => 'Lets update Meeting',
1282 'status_id' => 1,
1283 'activity_name' => 'Test activity type',
1284 'priority_id' => 1,
1285 ];
1286
1287 $result = $this->callAPISuccess('activity', 'create', $params);
1288 $findactivity = $this->callAPISuccess('Activity', 'Get', ['id' => $activity['id']]);
1289 }
1290
1291 /**
1292 * Test civicrm_activities_contact_get()
1293 */
1294 public function testActivitiesContactGet() {
1295 $activity = $this->callAPISuccess('activity', 'create', $this->_params);
1296 $activity2 = $this->callAPISuccess('activity', 'create', $this->_params2);
1297 // Get activities associated with contact $this->_contactID.
1298 $params = [
1299 'contact_id' => $this->_contactID,
1300 ];
1301 $result = $this->callAPISuccess('activity', 'get', $params);
1302
1303 $this->assertEquals(2, $result['count']);
1304 $this->assertEquals($this->test_activity_type_value, $result['values'][$activity['id']]['activity_type_id']);
1305 $this->assertEquals('Test activity type', $result['values'][$activity['id']]['activity_name']);
1306 $this->assertEquals('Test activity type', $result['values'][$activity2['id']]['activity_name']);
1307 }
1308
1309 /**
1310 * Test chained Activity format.
1311 */
1312 public function testChainedActivityGet() {
1313
1314 $activity = $this->callAPISuccess('Contact', 'Create', [
1315 'display_name' => "bob brown",
1316 'contact_type' => 'Individual',
1317 'api.activity_type.create' => [
1318 'weight' => '2',
1319 'label' => 'send out letters',
1320 'filter' => 0,
1321 'is_active' => 1,
1322 'is_optgroup' => 1,
1323 'is_default' => 0,
1324 ],
1325 'api.activity.create' => [
1326 'subject' => 'send letter',
1327 'activity_type_id' => '$value.api.activity_type.create.values.0.value',
1328 ],
1329 ]);
1330
1331 $result = $this->callAPISuccess('Activity', 'Get', [
1332 'id' => $activity['id'],
1333 'return.assignee_contact_id' => 1,
1334 'api.contact.get' => ['api.pledge.get' => 1],
1335 ]);
1336 }
1337
1338 /**
1339 * Test civicrm_activity_contact_get() with invalid Contact ID.
1340 */
1341 public function testActivitiesContactGetWithInvalidContactId() {
1342 $params = ['contact_id' => 'contact'];
1343 $this->callAPIFailure('activity', 'get', $params);
1344 }
1345
1346 /**
1347 * Test civicrm_activity_contact_get() with contact having no Activity.
1348 */
1349 public function testActivitiesContactGetHavingNoActivity() {
1350 $params = [
1351 'first_name' => 'dan',
1352 'last_name' => 'conberg',
1353 'email' => 'dan.conberg@w.co.in',
1354 'contact_type' => 'Individual',
1355 ];
1356
1357 $contact = $this->callAPISuccess('contact', 'create', $params);
1358 $params = [
1359 'contact_id' => $contact['id'],
1360 ];
1361 $result = $this->callAPISuccess('activity', 'get', $params);
1362 $this->assertEquals($result['count'], 0);
1363 }
1364
1365 /**
1366 * Test getfields function.
1367 */
1368 public function testGetFields() {
1369 $params = ['action' => 'create'];
1370 $result = $this->callAPIAndDocument('activity', 'getfields', $params, __FUNCTION__, __FILE__, NULL, NULL);
1371 $this->assertTrue(is_array($result['values']), 'get fields doesn\'t return values array');
1372 foreach ($result['values'] as $key => $value) {
1373 $this->assertTrue(is_array($value), $key . " is not an array");
1374 }
1375 }
1376
1377 /**
1378 * Test or operator in api params
1379 */
1380 public function testGetWithOr() {
1381 $acts = [
1382 'test or 1' => 'orOperator',
1383 'test or 2' => 'orOperator',
1384 'test or 3' => 'nothing',
1385 ];
1386 foreach ($acts as $subject => $details) {
1387 $params = $this->_params;
1388 $params['subject'] = $subject;
1389 $params['details'] = $details;
1390 $this->callAPISuccess('Activity', 'create', $params);
1391 }
1392 $result = $this->callAPISuccess('Activity', 'get', [
1393 'details' => 'orOperator',
1394 ]);
1395 $this->assertEquals(2, $result['count']);
1396 $result = $this->callAPISuccess('Activity', 'get', [
1397 'details' => 'orOperator',
1398 'subject' => 'test or 3',
1399 ]);
1400 $this->assertEquals(0, $result['count']);
1401 $result = $this->callAPISuccess('Activity', 'get', [
1402 'details' => 'orOperator',
1403 'subject' => 'test or 3',
1404 'options' => ['or' => [['details', 'subject']]],
1405 ]);
1406 $this->assertEquals(3, $result['count']);
1407 }
1408
1409 /**
1410 * Test handling of is_overdue calculated field
1411 */
1412 public function testGetOverdue() {
1413 $overdueAct = $this->callAPISuccess('Activity', 'create', [
1414 'activity_date_time' => 'now - 1 week',
1415 'status_id' => 'Scheduled',
1416 ] + $this->_params);
1417 $completedAct = $this->callAPISuccess('Activity', 'create', [
1418 'activity_date_time' => 'now - 1 week',
1419 'status_id' => 'Completed',
1420 ] + $this->_params);
1421 $ids = [$overdueAct['id'], $completedAct['id']];
1422
1423 // Test sorting
1424 $completedFirst = $this->callAPISuccess('Activity', 'get', [
1425 'id' => ['IN' => $ids],
1426 'options' => ['sort' => 'is_overdue ASC'],
1427 ]);
1428 $this->assertEquals(array_reverse($ids), array_keys($completedFirst['values']));
1429 $overdueFirst = $this->callAPISuccess('Activity', 'get', [
1430 'id' => ['IN' => $ids],
1431 'options' => ['sort' => 'is_overdue DESC'],
1432 'return' => 'is_overdue',
1433 ]);
1434 $this->assertEquals($ids, array_keys($overdueFirst['values']));
1435
1436 // Test return value
1437 $this->assertEquals(1, $overdueFirst['values'][$overdueAct['id']]['is_overdue']);
1438 $this->assertEquals(0, $overdueFirst['values'][$completedAct['id']]['is_overdue']);
1439
1440 // Test filtering
1441 $onlyOverdue = $this->callAPISuccess('Activity', 'get', [
1442 'id' => ['IN' => $ids],
1443 'is_overdue' => 1,
1444 ]);
1445 $this->assertEquals([$overdueAct['id']], array_keys($onlyOverdue['values']));
1446 }
1447
1448 }