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