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