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