Merge pull request #7895 from cividesk/CRM-18130-master
[civicrm-core.git] / tests / phpunit / api / v3 / ActivityTest.php
... / ...
CommitLineData
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 */
43class api_v3_ActivityTest extends CiviUnitTestCase {
44 protected $_params;
45 protected $_params2;
46 protected $_entity = 'activity';
47 protected $_apiversion = 3;
48 protected $test_activity_type_value;
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 'priority_id' => 1,
316 );
317
318 $result = $this->callAPISuccess('activity', 'create', $params);
319
320 $result = $this->callAPISuccess('activity', 'get', array('id' => $result['id']));
321 $this->assertEquals($result['values'][$result['id']]['duration'], 120);
322 $this->assertEquals($result['values'][$result['id']]['subject'], 'Make-it-Happen Meeting');
323 $this->assertEquals($result['values'][$result['id']]['activity_date_time'], '2011-03-16 00:00:00');
324 $this->assertEquals($result['values'][$result['id']]['location'], 'Pennsylvania');
325 $this->assertEquals($result['values'][$result['id']]['details'], 'a test activity');
326 $this->assertEquals($result['values'][$result['id']]['status_id'], 1);
327 }
328
329 /**
330 * Test get returns target and assignee contacts.
331 */
332 public function testActivityReturnTargetAssignee() {
333
334 $description = "Demonstrates setting & retrieving activity target & source.";
335 $subfile = "GetTargetandAssignee";
336 $params = array(
337 'source_contact_id' => $this->_contactID,
338 'subject' => 'Make-it-Happen Meeting',
339 'activity_date_time' => '20110316',
340 'duration' => 120,
341 'location' => 'Pennsylvania',
342 'details' => 'a test activity',
343 'status_id' => 1,
344 'activity_type_id' => 1,
345 'priority_id' => 1,
346 'target_contact_id' => $this->_contactID,
347 'assignee_contact_id' => $this->_contactID,
348 );
349
350 $result = $this->callAPIAndDocument('activity', 'create', $params, __FUNCTION__, __FILE__, $description, $subfile);
351 $result = $this->callAPISuccess('activity', 'get', array(
352 'id' => $result['id'],
353 'version' => $this->_apiversion,
354 'return.assignee_contact_id' => 1,
355 'return.target_contact_id' => 1,
356 ));
357
358 $this->assertEquals($this->_contactID, $result['values'][$result['id']]['assignee_contact_id'][0]);
359 $this->assertEquals($this->_contactID, $result['values'][$result['id']]['target_contact_id'][0]);
360 }
361
362 /**
363 * Test civicrm_activity_create() using example code.
364 */
365 public function testActivityCreateExample() {
366 require_once 'api/v3/examples/Activity/Create.php';
367 $result = activity_create_example();
368 $expectedResult = activity_create_expectedresult();
369 $this->assertEquals($result, $expectedResult);
370 }
371
372 /**
373 * Test civicrm_activity_create() with valid parameters and custom data.
374 */
375 public function testActivityCreateCustom() {
376 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, __FILE__);
377 $params = $this->_params;
378 $params['custom_' . $ids['custom_field_id']] = "custom string";
379 $result = $this->callAPIAndDocument($this->_entity, 'create', $params, __FUNCTION__, __FILE__);
380 $result = $this->callAPISuccess($this->_entity, 'get', array(
381 'return.custom_' . $ids['custom_field_id'] => 1,
382 'id' => $result['id'],
383 ));
384 $this->assertEquals("custom string", $result['values'][$result['id']]['custom_' . $ids['custom_field_id']], ' in line ' . __LINE__);
385
386 $this->customFieldDelete($ids['custom_field_id']);
387 $this->customGroupDelete($ids['custom_group_id']);
388 }
389
390 /**
391 * Test civicrm_activity_create() with valid parameters and custom data.
392 */
393 public function testActivityCreateCustomSubType() {
394 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, __FILE__);
395 $this->callAPISuccess('CustomGroup', 'create', array(
396 'extends_entity_column_value' => $this->test_activity_type_value,
397 'id' => $ids['custom_group_id'],
398 'extends' => 'Activity',
399 'is_active' => TRUE,
400 ));
401 $params = $this->_params;
402 $params['custom_' . $ids['custom_field_id']] = "custom string";
403 $result = $this->callAPIAndDocument($this->_entity, 'create', $params, __FUNCTION__, __FILE__);
404 $result = $this->callAPISuccess($this->_entity, 'get', array(
405 'return.custom_' . $ids['custom_field_id'] => 1,
406 'id' => $result['id'],
407 ));
408 $this->assertEquals("custom string", $result['values'][$result['id']]['custom_' . $ids['custom_field_id']]);
409
410 $this->customFieldDelete($ids['custom_field_id']);
411 $this->customGroupDelete($ids['custom_group_id']);
412 }
413
414 /**
415 * Test civicrm_activity_create() with valid parameters and custom data.
416 */
417 public function testActivityCreateCustomContactRefField() {
418
419 $this->callAPISuccess('contact', 'create', array('id' => $this->_contactID, 'sort_name' => 'Contact, Test'));
420 $subfile = 'ContactRefCustomField';
421 $description = "Demonstrates create with Contact Reference Custom Field.";
422 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, __FILE__);
423 $params = array(
424 'custom_group_id' => $ids['custom_group_id'],
425 'name' => 'Worker_Lookup',
426 'label' => 'Worker Lookup',
427 'html_type' => 'Autocomplete-Select',
428 'data_type' => 'ContactReference',
429 'weight' => 4,
430 'is_searchable' => 1,
431 'is_active' => 1,
432 );
433
434 $customField = $this->callAPISuccess('custom_field', 'create', $params);
435 $params = $this->_params;
436 $params['custom_' . $customField['id']] = "$this->_contactID";
437
438 $result = $this->callAPIAndDocument($this->_entity, 'create', $params, __FUNCTION__, __FILE__, $description, $subfile);
439 $result = $this->callAPIAndDocument($this->_entity, 'get', array(
440 'return.custom_' . $customField['id'] => 1,
441 'id' => $result['id'],
442 ), __FUNCTION__, __FILE__, 'Get with Contact Ref Custom Field', 'ContactRefCustomFieldGet');
443
444 $this->assertEquals('Anderson, Anthony', $result['values'][$result['id']]['custom_' . $customField['id']]);
445 $this->assertEquals($this->_contactID, $result['values'][$result['id']]['custom_' . $customField['id'] . "_id"], ' in line ' . __LINE__);
446 $this->assertEquals('Anderson, Anthony', $result['values'][$result['id']]['custom_' . $customField['id'] . '_1'], ' in line ' . __LINE__);
447 $this->assertEquals($this->_contactID, $result['values'][$result['id']]['custom_' . $customField['id'] . "_1_id"], ' in line ' . __LINE__);
448 $this->customFieldDelete($ids['custom_field_id']);
449 $this->customGroupDelete($ids['custom_group_id']);
450 }
451
452 /**
453 * Test civicrm_activity_create() with an invalid text status_id.
454 */
455 public function testActivityCreateBadTextStatus() {
456
457 $params = array(
458 'source_contact_id' => $this->_contactID,
459 'subject' => 'Discussion on Apis for v3',
460 'activity_date_time' => date('Ymd'),
461 'duration' => 120,
462 'location' => 'Pennsylvania',
463 'details' => 'a test activity',
464 'status_id' => 'Invalid',
465 'activity_name' => 'Test activity type',
466 );
467
468 $this->callAPIFailure('activity', 'create', $params);
469 }
470
471 /**
472 * Test civicrm_activity_create() with an invalid text status_id.
473 */
474 public function testActivityCreateSupportActivityStatus() {
475
476 $params = array(
477 'source_contact_id' => $this->_contactID,
478 'subject' => 'Discussion on Apis for v3',
479 'activity_date_time' => date('Ymd'),
480 'duration' => 120,
481 'location' => 'Pennsylvania',
482 'details' => 'a test activity',
483 'activity_status_id' => 'Invalid',
484 'activity_name' => 'Test activity type',
485 );
486
487 $result = $this->callAPIFailure('activity', 'create', $params,
488 "'Invalid' is not a valid option for field status_id");
489 }
490
491
492 /**
493 * Test civicrm_activity_create() with using a text status_id.
494 */
495 public function testActivityCreateTextStatus() {
496
497 $params = array(
498 'source_contact_id' => $this->_contactID,
499 'subject' => 'Make-it-Happen Meeting',
500 'activity_date_time' => date('Ymd'),
501 'duration' => 120,
502 'location' => 'Pennsylvania',
503 'details' => 'a test activity',
504 'status_id' => 'Scheduled',
505 'activity_name' => 'Test activity type',
506 );
507
508 $result = $this->callAPISuccess('activity', 'create', $params);
509 $this->assertEquals($result['values'][$result['id']]['duration'], 120);
510 $this->assertEquals($result['values'][$result['id']]['subject'], 'Make-it-Happen Meeting');
511 $this->assertEquals($result['values'][$result['id']]['activity_date_time'], date('Ymd') . '000000');
512 $this->assertEquals($result['values'][$result['id']]['location'], 'Pennsylvania');
513 $this->assertEquals($result['values'][$result['id']]['details'], 'a test activity');
514 }
515
516 /**
517 * Test civicrm_activity_get() with no params
518 */
519 public function testActivityGetEmpty() {
520 $result = $this->callAPISuccess('activity', 'get', array());
521 }
522
523 /**
524 * Test civicrm_activity_get() with a good activity ID
525 */
526 public function testActivityGetGoodID1() {
527 // Insert rows in civicrm_activity creating activities 4 and 13
528 $description = "Demonstrates getting assignee_contact_id & using it to get the contact.";
529 $subfile = 'ReturnAssigneeContact';
530 $activity = $this->callAPISuccess('activity', 'create', $this->_params);
531
532 $contact = $this->callAPISuccess('Contact', 'Create', array(
533 'first_name' => "The Rock",
534 'last_name' => 'roccky',
535 'contact_type' => 'Individual',
536 'version' => 3,
537 'api.activity.create' => array(
538 'id' => $activity['id'],
539 'assignee_contact_id' => '$value.id',
540 ),
541 ));
542
543 $params = array(
544 'activity_id' => $activity['id'],
545 'version' => $this->_apiversion,
546 'sequential' => 1,
547 'return.assignee_contact_id' => 1,
548 'api.contact.get' => array(
549 'id' => '$value.source_contact_id',
550 ),
551 );
552
553 $result = $this->callAPIAndDocument('Activity', 'Get', $params, __FUNCTION__, __FILE__, $description, $subfile);
554
555 $this->assertEquals($activity['id'], $result['id']);
556
557 $this->assertEquals($contact['id'], $result['values'][0]['assignee_contact_id'][0]);
558
559 $this->assertEquals($this->_contactID, $result['values'][0]['api.contact.get']['values'][0]['contact_id']);
560 $this->assertEquals($this->test_activity_type_value, $result['values'][0]['activity_type_id']);
561 $this->assertEquals("test activity type id", $result['values'][0]['subject']);
562 }
563
564 /**
565 * test that get functioning does filtering.
566 */
567 public function testGetFilter() {
568 $params = array(
569 'source_contact_id' => $this->_contactID,
570 'subject' => 'Make-it-Happen Meeting',
571 'activity_date_time' => '20110316',
572 'duration' => 120,
573 'location' => 'Pennsylvania',
574 'details' => 'a test activity',
575 'status_id' => 1,
576 'activity_name' => 'Test activity type',
577 'priority_id' => 1,
578 );
579 $result = $this->callAPISuccess('Activity', 'Create', $params);
580 $this->callAPISuccess('Activity', 'Get', array('subject' => 'Make-it-Happen Meeting'));
581 $this->assertEquals(1, $result['count']);
582 $this->assertEquals('Make-it-Happen Meeting', $result['values'][$result['id']]['subject']);
583 $this->callAPISuccess('Activity', 'Delete', array('id' => $result['id']));
584 }
585
586
587 /**
588 * Test civicrm_activity_get() with filter target_contact_id
589 */
590 public function testActivityGetTargetFilter() {
591 $params = $this->_params;
592 $contact1Params = array(
593 'first_name' => 'John',
594 'middle_name' => 'J.',
595 'last_name' => 'Anderson',
596 'prefix_id' => 3,
597 'suffix_id' => 3,
598 'email' => 'john_anderson@civicrm.org',
599 'contact_type' => 'Individual',
600 );
601
602 $contact1 = $this->individualCreate($contact1Params);
603 $contact2Params = array(
604 'first_name' => 'Michal',
605 'middle_name' => 'J.',
606 'last_name' => 'Anderson',
607 'prefix_id' => 3,
608 'suffix_id' => 3,
609 'email' => 'michal_anderson@civicrm.org',
610 'contact_type' => 'Individual',
611 );
612
613 $contact2 = $this->individualCreate($contact2Params);
614
615 $params['assignee_contact_id'] = array($contact1, $contact2);
616 $params['target_contact_id'] = array($contact2 => $contact2);
617 $activity = $this->callAPISuccess('Activity', 'Create', $params);
618
619 $activityget = $this->callAPISuccess('Activity', 'get', array(
620 'id' => $activity['id'],
621 'target_contact_id' => $contact2,
622 'return.target_contact_id' => 1,
623 ));
624 $this->assertEquals($activity['id'], $activityget['id']);
625 $this->assertEquals($contact2, $activityget['values'][$activityget['id']]['target_contact_id'][0]);
626
627 $activityget = $this->callAPISuccess('activity', 'get', array(
628 'target_contact_id' => $this->_contactID,
629 'return.target_contact_id' => 1,
630 'id' => $activity['id'],
631 ));
632 if ($activityget['count'] > 0) {
633 $this->assertNotEquals($contact2, $activityget['values'][$activityget['id']]['target_contact_id'][0]);
634 }
635 }
636
637 /**
638 * test that get functioning does filtering.
639 */
640 public function testGetStatusID() {
641 $params = array(
642 'source_contact_id' => $this->_contactID,
643 'subject' => 'Make-it-Happen Meeting',
644 'activity_date_time' => '20110316',
645 'duration' => 120,
646 'location' => 'Pennsylvania',
647 'details' => 'a test activity',
648 'status_id' => 1,
649 'activity_name' => 'Test activity type',
650 'priority_id' => 1,
651 );
652 $this->callAPISuccess('Activity', 'Create', $params);
653 $result = $this->callAPISuccess('Activity', 'Get', array('activity_status_id' => '1'));
654 $this->assertEquals(1, $result['count'], 'one activity of status 1 should exist');
655
656 $result = $this->callAPISuccess('Activity', 'Get', array('status_id' => '1'));
657 $this->assertEquals(1, $result['count'], 'status_id should also work');
658
659 $result = $this->callAPISuccess('Activity', 'Get', array('activity_status_id' => '2'));
660 $this->assertEquals(0, $result['count'], 'No activities of status 1 should exist');
661 $result = $this->callAPISuccess('Activity', 'Get', array(
662 'version' => $this->_apiversion,
663 'status_id' => '2',
664 ));
665 $this->assertEquals(0, $result['count'], 'No activities of status 1 should exist');
666
667 }
668
669 /**
670 * test that get functioning does filtering.
671 */
672 public function testGetFilterMaxDate() {
673 $params = array(
674 'source_contact_id' => $this->_contactID,
675 'subject' => 'Make-it-Happen Meeting',
676 'activity_date_time' => '20110101',
677 'duration' => 120,
678 'location' => 'Pennsylvania',
679 'details' => 'a test activity',
680 'status_id' => 1,
681 'activity_name' => 'Test activity type',
682 'version' => $this->_apiversion,
683 'priority_id' => 1,
684 );
685 $activityOne = $this->callAPISuccess('Activity', 'Create', $params);
686 $params['activity_date_time'] = 20120216;
687 $activityTwo = $this->callAPISuccess('Activity', 'Create', $params);
688 $result = $this->callAPISuccess('Activity', 'Get', array(
689 'version' => 3,
690 ));
691 $description = "Demonstrates _low filter (at time of writing doesn't work if contact_id is set.";
692 $subfile = "DateTimeLow";
693 $this->assertEquals(2, $result['count']);
694 $params = array(
695 'version' => 3,
696 'filter.activity_date_time_low' => '20120101000000',
697 'sequential' => 1,
698 );
699 $result = $this->callAPIAndDocument('Activity', 'Get', $params, __FUNCTION__, __FILE__, $description, $subfile);
700 $this->assertEquals(1, $result['count']);
701 $description = "Demonstrates _high filter (at time of writing doesn't work if contact_id is set.";
702 $subfile = "DateTimeHigh";
703 $this->assertEquals('2012-02-16 00:00:00', $result['values'][0]['activity_date_time']);
704 $params = array(
705 'source_contact_id' => $this->_contactID,
706 'version' => 3,
707 'filter.activity_date_time_high' => '20120101000000',
708 'sequential' => 1,
709 );
710 $result = $this->callAPIAndDocument('Activity', 'Get', $params, __FUNCTION__, __FILE__, $description, $subfile);
711
712 $this->assertEquals(1, $result['count']);
713 $this->assertEquals('2011-01-01 00:00:00', $result['values'][0]['activity_date_time']);
714
715 $this->callAPISuccess('Activity', 'Delete', array('version' => 3, 'id' => $activityOne['id']));
716 $this->callAPISuccess('Activity', 'Delete', array('version' => 3, 'id' => $activityTwo['id']));
717 }
718
719 /**
720 * Test civicrm_activity_get() with a good activity ID which
721 * has associated custom data
722 */
723 public function testActivityGetGoodIDCustom() {
724 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, __FILE__);
725
726 $params = $this->_params;
727 $params['custom_' . $ids['custom_field_id']] = "custom string";
728
729 $this->callAPISuccess($this->_entity, 'create', $params);
730
731 // Retrieve the test value.
732 $params = array(
733 'activity_type_id' => $this->test_activity_type_value,
734 'sequential' => 1,
735 'return.custom_' . $ids['custom_field_id'] => 1,
736 );
737 $result = $this->callAPIAndDocument('activity', 'get', $params, __FUNCTION__, __FILE__);
738 $this->assertEquals("custom string", $result['values'][0]['custom_' . $ids['custom_field_id']]);
739
740 $this->assertEquals($this->test_activity_type_value, $result['values'][0]['activity_type_id']);
741 $this->assertEquals('test activity type id', $result['values'][0]['subject']);
742 $this->customFieldDelete($ids['custom_field_id']);
743 $this->customGroupDelete($ids['custom_group_id']);
744 }
745
746 /**
747 * Test civicrm_activity_get() with a good activity ID which
748 * has associated custom data
749 */
750 public function testActivityGetContact_idCustom() {
751 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, __FILE__);
752
753 $params = $this->_params;
754 $params['custom_' . $ids['custom_field_id']] = "custom string";
755
756 $result = $this->callAPISuccess($this->_entity, 'create', $params);
757 // Retrieve the test value
758 $params = array(
759 'contact_id' => $this->_params['source_contact_id'],
760 'activity_type_id' => $this->test_activity_type_value,
761 'sequential' => 1,
762 'return.custom_' . $ids['custom_field_id'] => 1,
763 );
764 $result = $this->callAPIAndDocument('activity', 'get', $params, __FUNCTION__, __FILE__);
765 $this->assertEquals("custom string", $result['values'][0]['custom_' . $ids['custom_field_id']], ' in line ' . __LINE__);
766
767 $this->assertEquals($this->test_activity_type_value, $result['values'][0]['activity_type_id']);
768 $this->assertEquals('test activity type id', $result['values'][0]['subject']);
769 $this->assertEquals($result['values'][0]['id'], $result['id']);
770 }
771
772 /**
773 * Check activity deletion with empty params.
774 */
775 public function testDeleteActivityForEmptyParams() {
776 $params = array('version' => $this->_apiversion);
777 $this->callAPIFailure('activity', 'delete', $params);
778 }
779
780 /**
781 * Check activity deletion without activity id.
782 */
783 public function testDeleteActivityWithoutId() {
784 $params = array(
785 'activity_name' => 'Meeting',
786 'version' => $this->_apiversion,
787 );
788 $result = $this->callAPIFailure('activity', 'delete', $params);
789 }
790
791 /**
792 * Check activity deletion without activity type.
793 */
794 public function testDeleteActivityWithoutActivityType() {
795 $params = array('id' => 1);
796 $result = $this->callAPIFailure('activity', 'delete', $params);
797 }
798
799 /**
800 * Check activity deletion with incorrect data.
801 */
802 public function testDeleteActivityWithIncorrectActivityType() {
803 $params = array(
804 'id' => 1,
805 'activity_name' => 'Test Activity',
806 );
807
808 $result = $this->callAPIFailure('activity', 'delete', $params);
809 }
810
811 /**
812 * Check activity deletion with correct data.
813 */
814 public function testDeleteActivity() {
815 $result = $this->callAPISuccess('activity', 'create', $this->_params);
816 $params = array(
817 'id' => $result['id'],
818 'version' => $this->_apiversion,
819 );
820
821 $this->callAPIAndDocument('activity', 'delete', $params, __FUNCTION__, __FILE__);
822 }
823
824 /**
825 * Check if required fields are not passed.
826 */
827 public function testActivityUpdateWithoutRequired() {
828 $params = array(
829 'subject' => 'this case should fail',
830 'scheduled_date_time' => date('Ymd'),
831 );
832
833 $result = $this->callAPIFailure('activity', 'create', $params);
834 }
835
836 /**
837 * Test civicrm_activity_update() with non-numeric id
838 */
839 public function testActivityUpdateWithNonNumericId() {
840 $params = array(
841 'id' => 'lets break it',
842 'activity_name' => 'Meeting',
843 'subject' => 'this case should fail',
844 'scheduled_date_time' => date('Ymd'),
845 );
846
847 $result = $this->callAPIFailure('activity', 'create', $params);
848 }
849
850 /**
851 * Check with incorrect required fields.
852 */
853 public function testActivityUpdateWithIncorrectContactActivityType() {
854 $params = array(
855 'id' => 1,
856 'activity_name' => 'Test Activity',
857 'subject' => 'this case should fail',
858 'scheduled_date_time' => date('Ymd'),
859 'source_contact_id' => $this->_contactID,
860 );
861
862 $result = $this->callAPIFailure('activity', 'create', $params,
863 'Invalid Activity Id');
864 }
865
866 /**
867 * Test civicrm_activity_update() to update an existing activity
868 */
869 public function testActivityUpdate() {
870 $result = $this->callAPISuccess('activity', 'create', $this->_params);
871
872 $params = array(
873 'id' => $result['id'],
874 'subject' => 'Make-it-Happen Meeting',
875 'activity_date_time' => '20091011123456',
876 'duration' => 120,
877 'location' => '21, Park Avenue',
878 'details' => 'Lets update Meeting',
879 'status_id' => 1,
880 'source_contact_id' => $this->_contactID,
881 'priority_id' => 1,
882 );
883
884 $result = $this->callAPISuccess('activity', 'create', $params);
885 //hack on date comparison - really we should make getAndCheck smarter to handle dates
886 $params['activity_date_time'] = '2009-10-11 12:34:56';
887 // we also unset source_contact_id since it is stored in an aux table
888 unset($params['source_contact_id']);
889 $this->getAndCheck($params, $result['id'], 'activity');
890 }
891
892 /**
893 * Test civicrm_activity_update() with valid parameters
894 * and some custom data
895 */
896 public function testActivityUpdateCustom() {
897 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, __FILE__);
898
899 $params = $this->_params;
900
901 // Create an activity with custom data
902 //this has been updated from the previous 'old format' function - need to make it work
903 $params = array(
904 'source_contact_id' => $this->_contactID,
905 'subject' => 'Make-it-Happen Meeting',
906 'activity_date_time' => '2009-10-18',
907 'duration' => 120,
908 'location' => 'Pennsylvania',
909 'details' => 'a test activity to check the update api',
910 'status_id' => 1,
911 'activity_name' => 'Test activity type',
912 'version' => $this->_apiversion,
913 'custom_' . $ids['custom_field_id'] => 'custom string',
914 );
915 $result = $this->callAPISuccess('activity', 'create', $params);
916
917 $activityId = $result['id'];
918 $result = $this->callAPISuccess($this->_entity, 'get', array(
919 'return.custom_' . $ids['custom_field_id'] => 1,
920 'version' => 3,
921 'id' => $result['id'],
922 ));
923 $this->assertEquals("custom string", $result['values'][$result['id']]['custom_' . $ids['custom_field_id']]);
924 $this->assertEquals("2009-10-18 00:00:00", $result['values'][$result['id']]['activity_date_time']);
925 $fields = $this->callAPISuccess('activity', 'getfields', array('version' => $this->_apiversion));
926 $this->assertTrue(is_array($fields['values']['custom_' . $ids['custom_field_id']]));
927
928 // Update the activity with custom data.
929 $params = array(
930 'id' => $activityId,
931 'source_contact_id' => $this->_contactID,
932 'subject' => 'Make-it-Happen Meeting',
933 'status_id' => 1,
934 'activity_name' => 'Test activity type',
935 // add this since dates are messed up
936 'activity_date_time' => date('Ymd'),
937 'custom_' . $ids['custom_field_id'] => 'Updated my test data',
938 'version' => $this->_apiversion,
939 );
940 $result = $this->callAPISuccess('Activity', 'Create', $params);
941
942 $result = $this->callAPISuccess($this->_entity, 'get', array(
943 'return.custom_' . $ids['custom_field_id'] => 1,
944 'version' => 3,
945 'id' => $result['id'],
946 ));
947 $this->assertEquals("Updated my test data", $result['values'][$result['id']]['custom_' . $ids['custom_field_id']]);
948 }
949
950 /**
951 * Test civicrm_activity_update() for core activity fields
952 * and some custom data
953 */
954 public function testActivityUpdateCheckCoreFields() {
955 $params = $this->_params;
956 $contact1Params = array(
957 'first_name' => 'John',
958 'middle_name' => 'J.',
959 'last_name' => 'Anderson',
960 'prefix_id' => 3,
961 'suffix_id' => 3,
962 'email' => 'john_anderson@civicrm.org',
963 'contact_type' => 'Individual',
964 );
965
966 $contact1 = $this->individualCreate($contact1Params);
967 $contact2Params = array(
968 'first_name' => 'Michal',
969 'middle_name' => 'J.',
970 'last_name' => 'Anderson',
971 'prefix_id' => 3,
972 'suffix_id' => 3,
973 'email' => 'michal_anderson@civicrm.org',
974 'contact_type' => 'Individual',
975 );
976
977 $contact2 = $this->individualCreate($contact2Params);
978
979 $params['assignee_contact_id'] = array($contact1, $contact2);
980 $params['target_contact_id'] = array($contact2 => $contact2);
981 $result = $this->callAPISuccess('Activity', 'Create', $params);
982
983 $activityId = $result['id'];
984 $getParams = array(
985 'return.assignee_contact_id' => 1,
986 'return.target_contact_id' => 1,
987 'version' => $this->_apiversion,
988 'id' => $activityId,
989 );
990 $result = $this->callAPISuccess($this->_entity, 'get', $getParams);
991 $assignee = $result['values'][$result['id']]['assignee_contact_id'];
992 $target = $result['values'][$result['id']]['target_contact_id'];
993 $this->assertEquals(2, count($assignee), ' in line ' . __LINE__);
994 $this->assertEquals(1, count($target), ' in line ' . __LINE__);
995 $this->assertEquals(TRUE, in_array($contact1, $assignee), ' in line ' . __LINE__);
996 $this->assertEquals(TRUE, in_array($contact2, $target), ' in line ' . __LINE__);
997
998 $contact3Params = array(
999 'first_name' => 'Jijo',
1000 'middle_name' => 'J.',
1001 'last_name' => 'Anderson',
1002 'prefix_id' => 3,
1003 'suffix_id' => 3,
1004 'email' => 'jijo_anderson@civicrm.org',
1005 'contact_type' => 'Individual',
1006 );
1007
1008 $contact4Params = array(
1009 'first_name' => 'Grant',
1010 'middle_name' => 'J.',
1011 'last_name' => 'Anderson',
1012 'prefix_id' => 3,
1013 'suffix_id' => 3,
1014 'email' => 'grant_anderson@civicrm.org',
1015 'contact_type' => 'Individual',
1016 );
1017
1018 $contact3 = $this->individualCreate($contact3Params);
1019 $contact4 = $this->individualCreate($contact4Params);
1020
1021 $params = array();
1022 $params['id'] = $activityId;
1023 $params['assignee_contact_id'] = array($contact3 => $contact3);
1024 $params['target_contact_id'] = array($contact4 => $contact4);
1025
1026 $result = $this->callAPISuccess('activity', 'create', $params);
1027
1028 $this->assertEquals($activityId, $result['id'], ' in line ' . __LINE__);
1029
1030 $result = $this->callAPISuccess(
1031 $this->_entity,
1032 'get',
1033 array(
1034 'return.assignee_contact_id' => 1,
1035 'return.target_contact_id' => 1,
1036 'return.source_contact_id' => 1,
1037 'id' => $result['id'],
1038 )
1039 );
1040
1041 $assignee = $result['values'][$result['id']]['assignee_contact_id'];
1042 $target = $result['values'][$result['id']]['target_contact_id'];
1043
1044 $this->assertEquals(1, count($assignee), ' in line ' . __LINE__);
1045 $this->assertEquals(1, count($target), ' in line ' . __LINE__);
1046 $this->assertEquals(TRUE, in_array($contact3, $assignee), ' in line ' . __LINE__);
1047 $this->assertEquals(TRUE, in_array($contact4, $target), ' in line ' . __LINE__);
1048
1049 foreach ($this->_params as $fld => $val) {
1050 $this->assertEquals($val, $result['values'][$result['id']][$fld]);
1051 }
1052 }
1053
1054 /**
1055 * Test civicrm_activity_update() where the DB has a date_time
1056 * value and there is none in the update params.
1057 */
1058 public function testActivityUpdateNotDate() {
1059 $result = $this->callAPISuccess('activity', 'create', $this->_params);
1060
1061 $params = array(
1062 'id' => $result['id'],
1063 'subject' => 'Make-it-Happen Meeting',
1064 'duration' => 120,
1065 'location' => '21, Park Avenue',
1066 'details' => 'Lets update Meeting',
1067 'status_id' => 1,
1068 'source_contact_id' => $this->_contactID,
1069 'priority_id' => 1,
1070 );
1071
1072 $result = $this->callAPISuccess('activity', 'create', $params);
1073 //hack on date comparison - really we should make getAndCheck smarter to handle dates
1074 $params['activity_date_time'] = $this->_params['activity_date_time'];
1075 // we also unset source_contact_id since it is stored in an aux table
1076 unset($params['source_contact_id']);
1077 $this->getAndCheck($params, $result['id'], 'activity');
1078 }
1079
1080 /**
1081 * Check activity update with status.
1082 */
1083 public function testActivityUpdateWithStatus() {
1084 $activity = $this->callAPISuccess('activity', 'create', $this->_params);
1085 $params = array(
1086 'id' => $activity['id'],
1087 'source_contact_id' => $this->_contactID,
1088 'subject' => 'Hurry update works',
1089 'status_id' => 1,
1090 'activity_name' => 'Test activity type',
1091 );
1092
1093 $result = $this->callAPISuccess('activity', 'create', $params);
1094 $this->assertEquals($result['id'], $activity['id']);
1095 $this->assertEquals($result['values'][$activity['id']]['subject'], 'Hurry update works');
1096 $this->assertEquals($result['values'][$activity['id']]['status_id'], 1
1097 );
1098 }
1099
1100 /**
1101 * Test civicrm_activity_update() where the source_contact_id
1102 * is not in the update params.
1103 */
1104 public function testActivityUpdateKeepSource() {
1105 $activity = $this->callAPISuccess('activity', 'create', $this->_params);
1106 // Updating the activity but not providing anything for the source contact
1107 // (It was set as $this->_contactID earlier.)
1108 $params = array(
1109 'id' => $activity['id'],
1110 'subject' => 'Updated Make-it-Happen Meeting',
1111 'duration' => 120,
1112 'location' => '21, Park Avenue',
1113 'details' => 'Lets update Meeting',
1114 'status_id' => 1,
1115 'activity_name' => 'Test activity type',
1116 'priority_id' => 1,
1117 );
1118
1119 $result = $this->callAPISuccess('activity', 'create', $params);
1120 $findactivity = $this->callAPISuccess('Activity', 'Get', array('id' => $activity['id']));
1121 }
1122
1123 /**
1124 * Test civicrm_activities_contact_get()
1125 */
1126 public function testActivitiesContactGet() {
1127 $activity = $this->callAPISuccess('activity', 'create', $this->_params);
1128 $activity2 = $this->callAPISuccess('activity', 'create', $this->_params2);
1129 // Get activities associated with contact $this->_contactID.
1130 $params = array(
1131 'contact_id' => $this->_contactID,
1132 );
1133 $result = $this->callAPISuccess('activity', 'get', $params);
1134
1135 $this->assertEquals(2, $result['count']);
1136 $this->assertEquals($this->test_activity_type_value, $result['values'][$activity['id']]['activity_type_id']);
1137 $this->assertEquals('Test activity type', $result['values'][$activity['id']]['activity_name']);
1138 $this->assertEquals('Test activity type', $result['values'][$activity2['id']]['activity_name']);
1139 }
1140
1141 /**
1142 * Test chained Activity format.
1143 */
1144 public function testChainedActivityGet() {
1145
1146 $activity = $this->callAPISuccess('Contact', 'Create', array(
1147 'display_name' => "bob brown",
1148 'contact_type' => 'Individual',
1149 'api.activity_type.create' => array(
1150 'weight' => '2',
1151 'label' => 'send out letters',
1152 'filter' => 0,
1153 'is_active' => 1,
1154 'is_optgroup' => 1,
1155 'is_default' => 0,
1156 ),
1157 'api.activity.create' => array(
1158 'subject' => 'send letter',
1159 'activity_type_id' => '$value.api.activity_type.create.values.0.value',
1160 ),
1161 ));
1162
1163 $result = $this->callAPISuccess('Activity', 'Get', array(
1164 'id' => $activity['id'],
1165 'return.assignee_contact_id' => 1,
1166 'api.contact.get' => array('api.pledge.get' => 1),
1167 ));
1168 }
1169
1170 /**
1171 * Test civicrm_activity_contact_get() with invalid Contact ID.
1172 */
1173 public function testActivitiesContactGetWithInvalidContactId() {
1174 $params = array('contact_id' => 'contact');
1175 $this->callAPIFailure('activity', 'get', $params);
1176 }
1177
1178 /**
1179 * Test civicrm_activity_contact_get() with contact having no Activity.
1180 */
1181 public function testActivitiesContactGetHavingNoActivity() {
1182 $params = array(
1183 'first_name' => 'dan',
1184 'last_name' => 'conberg',
1185 'email' => 'dan.conberg@w.co.in',
1186 'contact_type' => 'Individual',
1187 );
1188
1189 $contact = $this->callAPISuccess('contact', 'create', $params);
1190 $params = array(
1191 'contact_id' => $contact['id'],
1192 );
1193 $result = $this->callAPISuccess('activity', 'get', $params);
1194 $this->assertEquals($result['count'], 0);
1195 }
1196
1197 /**
1198 * Test getfields function.
1199 */
1200 public function testGetFields() {
1201 $params = array('action' => 'create');
1202 $result = $this->callAPIAndDocument('activity', 'getfields', $params, __FUNCTION__, __FILE__, NULL, NULL);
1203 $this->assertTrue(is_array($result['values']), 'get fields doesn\'t return values array');
1204 foreach ($result['values'] as $key => $value) {
1205 $this->assertTrue(is_array($value), $key . " is not an array");
1206 }
1207 }
1208
1209}