Merge pull request #2263 from kurund/CRM-12753
[civicrm-core.git] / tests / phpunit / api / v3 / ActivityTest.php
1 <?php
2 /**
3 * File for the TestActivity class
4 *
5 * (PHP 5)
6 *
7 * @author Walt Haas <walt@dharmatech.org> (801) 534-1262
8 * @copyright Copyright CiviCRM LLC (C) 2009
9 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html
10 * GNU Affero General Public License version 3
11 * @version $Id: ActivityTest.php 31254 2010-12-15 10:09:29Z eileen $
12 * @package CiviCRM
13 *
14 * This file is part of CiviCRM
15 *
16 * CiviCRM is free software; you can redistribute it and/or
17 * modify it under the terms of the GNU Affero General Public License
18 * as published by the Free Software Foundation; either version 3 of
19 * the License, or (at your option) any later version.
20 *
21 * CiviCRM is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU Affero General Public License for more details.
25 *
26 * You should have received a copy of the GNU Affero General Public
27 * License along with this program. If not, see
28 * <http://www.gnu.org/licenses/>.
29 */
30
31 /**
32 * Include class definitions
33 */
34 require_once 'CiviTest/CiviUnitTestCase.php';
35
36
37 /**
38 * Test APIv3 civicrm_activity_* functions
39 *
40 * @package CiviCRM_APIv3
41 * @subpackage API_Activity
42 */
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' => 'Pensulvania',
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 * This method is called after a test is executed.
100 *
101 * @access protected
102 */
103 function tearDown() {
104 $tablesToTruncate = array(
105 'civicrm_contact',
106 'civicrm_activity',
107 'civicrm_activity_contact',
108 );
109 $this->quickCleanup($tablesToTruncate, TRUE);
110 $this->callAPISuccess('option_value', 'delete', array('id' => $this->test_activity_type_id));
111 }
112
113 /**
114 * check with empty array
115 */
116 function testActivityCreateEmpty() {
117 $result = $this->callAPIFailure('activity', 'create', array());
118 }
119
120 /**
121 * check if required fields are not passed
122 */
123 function testActivityCreateWithoutRequired() {
124 $params = array(
125 'subject' => 'this case should fail',
126 'scheduled_date_time' => date('Ymd'),
127 );
128 $result = $this->callAPIFailure('activity', 'create', $params);
129 }
130
131 /**
132 * Test civicrm_activity_create() with mismatched activity_type_id
133 * and activity_name
134 */
135 function testActivityCreateMismatchNameType() {
136 $params = array(
137 'source_contact_id' => $this->_contactID,
138 'subject' => 'Test activity',
139 'activity_date_time' => date('Ymd'),
140 'duration' => 120,
141 'location' => 'Pensulvania',
142 'details' => 'a test activity',
143 'status_id' => 1,
144 'activity_name' => 'Fubar activity type',
145 'activity_type_id' => 5,
146 'scheduled_date_time' => date('Ymd'),
147 );
148
149 $result = $this->callAPIFailure('activity', 'create', $params);
150 }
151
152 /**
153 * Test civicrm_activity_id() with missing source_contact_id is put with the current user.
154 */
155 function testActivityCreateWithMissingContactId() {
156 $params = array(
157 'subject' => 'Make-it-Happen Meeting',
158 'activity_date_time' => date('Ymd'),
159 'duration' => 120,
160 'location' => 'Pensulvania',
161 'details' => 'a test activity',
162 'status_id' => 1,
163 'activity_name' => 'Test activity type',
164 );
165
166 $result = $this->callAPISuccess('activity', 'create', $params);
167 }
168
169 /**
170 * Test civicrm_activity_id() with non-numeric source_contact_id
171 */
172 function testActivityCreateWithNonNumericContactId() {
173 $params = array(
174 'source_contact_id' => 'fubar',
175 'subject' => 'Make-it-Happen Meeting',
176 'activity_date_time' => date('Ymd'),
177 'duration' => 120,
178 'location' => 'Pensulvania',
179 'details' => 'a test activity',
180 'status_id' => 1,
181 'activity_name' => 'Test activity type',
182 );
183
184 $result = $this->callAPIFailure('activity', 'create', $params);
185 }
186
187 /**
188 * Ensure that an invalid activity type causes failure
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 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' => 'Pensulvania',
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 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' => 'Pensulvania',
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 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' => 'Pensulvania',
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 function testActivityCreateWithValidStringPriority() {
246 $params = array(
247 'source_contact_id' => $this->_contactID,
248 'subject' => 'Make-it-Happen Meeting',
249 'activity_date_time' => date('Ymd'),
250 'duration' => 120,
251 'location' => 'Pensulvania',
252 'details' => 'a test activity',
253 'status_id' => 1,
254 'priority_id' => 'Urgent',
255 'activity_type_id' => 1,
256 );
257
258 $result = $this->callAPISuccess('activity', 'create', $params);
259 $this->assertEquals(1, $result['values'][$result['id']]['priority_id']);
260 }
261
262 function testActivityCreateWithInValidStringPriority() {
263 $params = array(
264 'source_contact_id' => $this->_contactID,
265 'subject' => 'Make-it-Happen Meeting',
266 'activity_date_time' => date('Ymd'),
267 'duration' => 120,
268 'location' => 'Pensulvania',
269 'details' => 'a test activity',
270 'status_id' => 1,
271 'priority_id' => 'ergUrgent',
272 'activity_type_id' => 1,
273 );
274
275 $result = $this->callAPIFailure('activity', 'create', $params,
276 "'ergUrgent' is not a valid option for field priority_id");
277 }
278
279 /**
280 * Test civicrm_activity_create() with valid parameters
281 */
282 function testActivityCreate() {
283
284 $result = $this->callAPISuccess('activity', 'create', $this->_params);
285 $result = $this->callAPISuccess('activity', 'get', $this->_params);
286 $this->assertEquals($result['values'][$result['id']]['duration'], 120, 'in line ' . __LINE__);
287 $this->assertEquals($result['values'][$result['id']]['subject'], 'test activity type id', 'in line ' . __LINE__);
288 $this->assertEquals($result['values'][$result['id']]['activity_date_time'], '2011-06-02 14:36:13', 'in line ' . __LINE__);
289 $this->assertEquals($result['values'][$result['id']]['location'], 'Pensulvania', 'in line ' . __LINE__);
290 $this->assertEquals($result['values'][$result['id']]['details'], 'a test activity', 'in line ' . __LINE__);
291 $this->assertEquals($result['values'][$result['id']]['status_id'], 2, 'in line ' . __LINE__);
292 $this->assertEquals($result['values'][$result['id']]['id'], $result['id'], 'in line ' . __LINE__);
293 }
294
295 /**
296 * Test civicrm_activity_create() with valid parameters - use type_id
297 */
298 function testActivityCreateCampaignTypeID() {
299 $this->enableCiviCampaign();
300
301 $defaults = array();
302
303 $params = array(
304 'source_contact_id' => $this->_contactID,
305 'subject' => 'Make-it-Happen Meeting',
306 'activity_date_time' => '20110316',
307 'duration' => 120,
308 'location' => 'Pensulvania',
309 'details' => 'a test activity',
310 'status_id' => 1,
311 'activity_type_id' => 29,
312 'priority_id' => 1,
313 );
314
315 $result = $this->callAPISuccess('activity', 'create', $params);
316 //todo test target & assignee are set
317
318 //$this->assertEquals($result['values'][$result['id']]['source_contact_id'], $this->_contactID, 'in line ' . __LINE__);
319 $result = $this->callAPISuccess('activity', 'get', array('id' => $result['id']));
320 $this->assertEquals($result['values'][$result['id']]['duration'], 120, 'in line ' . __LINE__);
321 $this->assertEquals($result['values'][$result['id']]['subject'], 'Make-it-Happen Meeting', 'in line ' . __LINE__);
322 $this->assertEquals($result['values'][$result['id']]['activity_date_time'], '2011-03-16 00:00:00', 'in line ' . __LINE__);
323 $this->assertEquals($result['values'][$result['id']]['location'], 'Pensulvania', 'in line ' . __LINE__);
324 $this->assertEquals($result['values'][$result['id']]['details'], 'a test activity', 'in line ' . __LINE__);
325 $this->assertEquals($result['values'][$result['id']]['status_id'], 1, 'in line ' . __LINE__);
326 }
327
328 function testActivityReturnTargetAssignee() {
329
330 $description = "Example demonstrates setting & retrieving the target & source";
331 $subfile = "GetTargetandAssignee";
332 $params = array(
333 'source_contact_id' => $this->_contactID,
334 'subject' => 'Make-it-Happen Meeting',
335 'activity_date_time' => '20110316',
336 'duration' => 120,
337 'location' => 'Pensulvania',
338 'details' => 'a test activity',
339 'status_id' => 1,
340 'activity_type_id' => 1,
341 'priority_id' => 1,
342 'target_contact_id' => $this->_contactID,
343 'assignee_contact_id' => $this->_contactID,
344 );
345
346 $result = $this->callAPIAndDocument('activity', 'create', $params, __FUNCTION__, __FILE__, $description, $subfile);
347 $result = $this->callAPISuccess('activity', 'get', array('id' => $result['id'], 'version' => $this->_apiversion, 'return.assignee_contact_id' => 1, 'return.target_contact_id' => 1));
348
349 $this->assertEquals($this->_contactID, $result['values'][$result['id']]['assignee_contact_id'][0], 'in line ' . __LINE__);
350 $this->assertEquals($this->_contactID, $result['values'][$result['id']]['target_contact_id'][0], 'in line ' . __LINE__);
351 }
352
353 function testActivityCreateExample() {
354 /**
355 * Test civicrm_activity_create() using example code
356 */
357 require_once 'api/v3/examples/ActivityCreate.php';
358 $result = activity_create_example();
359 $expectedResult = activity_create_expectedresult();
360 $this->assertEquals($result, $expectedResult);
361 }
362
363 /**
364 * Test civicrm_activity_create() with valid parameters
365 * and some custom data
366 */
367 function testActivityCreateCustom() {
368 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, __FILE__);
369 $params = $this->_params;
370 $params['custom_' . $ids['custom_field_id']] = "custom string";
371 $result = $this->callAPIAndDocument($this->_entity, 'create', $params, __FUNCTION__, __FILE__);
372 $result = $this->callAPISuccess($this->_entity, 'get', array('return.custom_' . $ids['custom_field_id'] => 1, 'version' => 3, 'id' => $result['id']));
373 $this->assertEquals("custom string", $result['values'][$result['id']]['custom_' . $ids['custom_field_id']], ' in line ' . __LINE__);
374
375 $this->customFieldDelete($ids['custom_field_id']);
376 $this->customGroupDelete($ids['custom_group_id']);
377 }
378
379 /**
380 * Test civicrm_activity_create() with valid parameters
381 * and some custom data
382 */
383 function testActivityCreateCustomContactRefField() {
384
385 $this->callAPISuccess('contact', 'create', array('id' => $this->_contactID, 'sort_name' => 'Contact, Test'));
386 $subfile = 'ContactRefCustomField';
387 $description = "demonstrates create with Contact Reference Custom Field";
388 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, __FILE__);
389 $params = array(
390 'custom_group_id' => $ids['custom_group_id'],
391 'name' => 'Worker_Lookup',
392 'label' => 'Worker Lookup',
393 'html_type' => 'Autocomplete-Select',
394 'data_type' => 'ContactReference',
395 'weight' => 4,
396 'is_searchable' => 1,
397 'is_active' => 1,
398 );
399
400 $customField = $this->callAPISuccess('custom_field', 'create', $params);
401 $params = $this->_params;
402 $params['custom_' . $customField['id']] = "$this->_contactID";
403
404 $result = $this->callAPIAndDocument($this->_entity, 'create', $params, __FUNCTION__, __FILE__, $description, $subfile, 'Create');
405 $result = $this->callAPIAndDocument($this->_entity, 'get', array('return.custom_' . $customField['id'] => 1, 'id' => $result['id']), __FUNCTION__, __FILE__, 'Get with Contact Ref Custom Field', 'ContactRefCustomFieldGet', 'get');
406
407 $this->assertEquals('Anderson, Anthony', $result['values'][$result['id']]['custom_' . $customField['id']], ' in line ' . __LINE__);
408 $this->assertEquals($this->_contactID, $result['values'][$result['id']]['custom_' . $customField['id'] . "_id"], ' in line ' . __LINE__);
409 $this->assertEquals('Anderson, Anthony', $result['values'][$result['id']]['custom_' . $customField['id'] . '_1'], ' in line ' . __LINE__);
410 $this->assertEquals($this->_contactID, $result['values'][$result['id']]['custom_' . $customField['id'] . "_1_id"], ' in line ' . __LINE__);
411 $this->customFieldDelete($ids['custom_field_id']);
412 $this->customGroupDelete($ids['custom_group_id']);
413 }
414
415 /**
416 * Test civicrm_activity_create() with an invalid text status_id
417 */
418 function testActivityCreateBadTextStatus() {
419
420 $params = array(
421 'source_contact_id' => $this->_contactID,
422 'subject' => 'Discussion on Apis for v3',
423 'activity_date_time' => date('Ymd'),
424 'duration' => 120,
425 'location' => 'Pensulvania',
426 'details' => 'a test activity',
427 'status_id' => 'Invalid',
428 'activity_name' => 'Test activity type',
429 );
430
431 $result = $this->callAPIFailure('activity', 'create', $params);
432 }
433
434 /**
435 * Test civicrm_activity_create() with an invalid text status_id
436 */
437 function testActivityCreateSupportActivityStatus() {
438
439 $params = array(
440 'source_contact_id' => $this->_contactID,
441 'subject' => 'Discussion on Apis for v3',
442 'activity_date_time' => date('Ymd'),
443 'duration' => 120,
444 'location' => 'Pensulvania',
445 'details' => 'a test activity',
446 'activity_status_id' => 'Invalid',
447 'activity_name' => 'Test activity type',
448 );
449
450 $result = $this->callAPIFailure('activity', 'create', $params,
451 "'Invalid' is not a valid option for field status_id");
452 }
453
454
455 /**
456 * Test civicrm_activity_create() with valid parameters,
457 * using a text status_id
458 */
459 function testActivityCreateTextStatus() {
460
461
462 $params = array(
463 'source_contact_id' => $this->_contactID,
464 'subject' => 'Make-it-Happen Meeting',
465 'activity_date_time' => date('Ymd'),
466 'duration' => 120,
467 'location' => 'Pensulvania',
468 'details' => 'a test activity',
469 'status_id' => 'Scheduled',
470 'activity_name' => 'Test activity type',
471 );
472
473 $result = $this->callAPISuccess('activity', 'create', $params);
474 $this->assertEquals($result['values'][$result['id']]['duration'], 120, 'in line ' . __LINE__);
475 $this->assertEquals($result['values'][$result['id']]['subject'], 'Make-it-Happen Meeting', 'in line ' . __LINE__);
476 $this->assertEquals($result['values'][$result['id']]['activity_date_time'], date('Ymd') . '000000', 'in line ' . __LINE__);
477 $this->assertEquals($result['values'][$result['id']]['location'], 'Pensulvania', 'in line ' . __LINE__);
478 $this->assertEquals($result['values'][$result['id']]['details'], 'a test activity', 'in line ' . __LINE__);
479 }
480
481 /**
482 * Test civicrm_activity_get() with no params
483 */
484 function testActivityGetEmpty() {
485 $result = $this->callAPISuccess('activity', 'get', array());
486 }
487
488 /**
489 * Test civicrm_activity_get() with a good activity ID
490 */
491 function testActivityGetGoodID1() {
492 // Insert rows in civicrm_activity creating activities 4 and
493 // 13
494 $description = "Function demonstrates getting asignee_contact_id & using it to get the contact";
495 $subfile = 'ReturnAssigneeContact';
496 $activity = $this->callAPISuccess('activity', 'create', $this->_params);
497
498 $contact = $this->callAPISuccess('Contact', 'Create', array(
499 'first_name' => "The Rock",
500 'last_name' =>'roccky',
501 'contact_type' => 'Individual',
502 'version' => 3,
503 'api.activity.create' => array(
504 'id' => $activity['id'], 'assignee_contact_id' => '$value.id',
505 ),
506 ));
507
508 $params = array(
509 'activity_id' => $activity['id'],
510 'version' => $this->_apiversion,
511 'sequential' => 1,
512 'return.assignee_contact_id' => 1,
513 'api.contact.get' => array(
514 'id' => '$value.source_contact_id',
515 ),
516 );
517
518 $result = $this->callAPIAndDocument('Activity', 'Get', $params, __FUNCTION__, __FILE__, $description, $subfile);
519
520 $this->assertEquals($activity['id'], $result['id'], 'In line ' . __LINE__);
521
522 $this->assertEquals($contact['id'], $result['values'][0]['assignee_contact_id'][0], 'In line ' . __LINE__);
523
524 $this->assertEquals($this->_contactID, $result['values'][0]['api.contact.get']['values'][0]['contact_id'], 'In line ' . __LINE__);
525 $this->assertEquals($this->test_activity_type_value, $result['values'][0]['activity_type_id'], 'In line ' . __LINE__);
526 $this->assertEquals("test activity type id", $result['values'][0]['subject'], 'In line ' . __LINE__);
527 }
528
529 /*
530 * test that get functioning does filtering
531 */
532 function testGetFilter() {
533 $params = array(
534 'source_contact_id' => $this->_contactID,
535 'subject' => 'Make-it-Happen Meeting',
536 'activity_date_time' => '20110316',
537 'duration' => 120,
538 'location' => 'Pensulvania',
539 'details' => 'a test activity',
540 'status_id' => 1,
541 'activity_name' => 'Test activity type',
542 'priority_id' => 1,
543 );
544 $result = $this->callAPISuccess('Activity', 'Create', $params);
545 $this->callAPISuccess('Activity', 'Get', array('subject' => 'Make-it-Happen Meeting'));
546 $this->assertEquals(1, $result['count']);
547 $this->assertEquals('Make-it-Happen Meeting', $result['values'][$result['id']]['subject']);
548 $this->callAPISuccess('Activity', 'Delete', array('id' => $result['id']));
549 }
550 /*
551 * test that get functioning does filtering
552 */
553 function testGetStatusID() {
554 $params = array(
555 'source_contact_id' => $this->_contactID,
556 'subject' => 'Make-it-Happen Meeting',
557 'activity_date_time' => '20110316',
558 'duration' => 120,
559 'location' => 'Pensulvania',
560 'details' => 'a test activity',
561 'status_id' => 1,
562 'activity_name' => 'Test activity type',
563 'priority_id' => 1,
564 );
565 $this->callAPISuccess('Activity', 'Create', $params);
566 $result = $this->callAPISuccess('Activity', 'Get', array('activity_status_id' => '1'));
567 $this->assertEquals(1, $result['count'], 'one activity of status 1 should exist');
568
569 $result = $this->callAPISuccess('Activity', 'Get', array('status_id' => '1'));
570 $this->assertEquals(1, $result['count'], 'status_id should also work');
571
572 $result = $this->callAPISuccess('Activity', 'Get', array('activity_status_id' => '2'));
573 $this->assertEquals(0, $result['count'], 'No activities of status 1 should exist');
574 $result = $this->callAPISuccess('Activity', 'Get', array(
575 'version' => $this->_apiversion,
576 'status_id' => '2'));
577 $this->assertEquals(0, $result['count'], 'No activities of status 1 should exist');
578
579
580 }
581
582 /*
583 * test that get functioning does filtering
584 */
585 function testGetFilterMaxDate() {
586 $params = array(
587 'source_contact_id' => $this->_contactID,
588 'subject' => 'Make-it-Happen Meeting',
589 'activity_date_time' => '20110101',
590 'duration' => 120,
591 'location' => 'Pensulvania',
592 'details' => 'a test activity',
593 'status_id' => 1,
594 'activity_name' => 'Test activity type',
595 'version' => $this->_apiversion,
596 'priority_id' => 1,
597 );
598 $activityOne = $this->callAPISuccess('Activity', 'Create', $params);
599 $params['activity_date_time'] = 20120216;
600 $activityTwo = $this->callAPISuccess('Activity', 'Create', $params);
601 $result = $this->callAPISuccess('Activity', 'Get', array(
602 'version' => 3,
603 ));
604 $description = "demonstrates _low filter (at time of writing doesn't work if contact_id is set";
605 $subfile = "DateTimeLow";
606 $this->assertEquals(2, $result['count']);
607 $params = array(
608 'version' => 3,
609 'filter.activity_date_time_low' => '20120101000000',
610 'sequential' => 1,
611 );
612 $result = $this->callAPIAndDocument('Activity', 'Get', $params, __FUNCTION__, __FILE__, $description, $subfile);
613 $this->assertEquals(1, $result['count'], 'in line ' . __LINE__);
614 $description = "demonstrates _high filter (at time of writing doesn't work if contact_id is set";
615 $subfile = "DateTimeHigh";
616 $this->assertEquals('2012-02-16 00:00:00', $result['values'][0]['activity_date_time'], 'in line ' . __LINE__);
617 $params = array(
618 'source_contact_id' => $this->_contactID,
619 'version' => 3,
620 'filter.activity_date_time_high' => '20120101000000',
621 'sequential' => 1,
622 );
623 $result = $this->callAPIAndDocument('Activity', 'Get', $params, __FUNCTION__, __FILE__, $description, $subfile);
624
625 $this->assertEquals(1, $result['count']);
626 $this->assertEquals('2011-01-01 00:00:00', $result['values'][0]['activity_date_time'], 'in line ' . __LINE__);
627
628 $this->callAPISuccess('Activity', 'Delete', array('version' => 3, 'id' => $activityOne['id']));
629 $this->callAPISuccess('Activity', 'Delete', array('version' => 3, 'id' => $activityTwo['id']));
630 }
631
632 /**
633 * Test civicrm_activity_get() with a good activity ID which
634 * has associated custom data
635 */
636 function testActivityGetGoodIDCustom() {
637 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, __FILE__);
638
639 $params = $this->_params;
640 $params['custom_' . $ids['custom_field_id']] = "custom string";
641
642 $result = $this->callAPISuccess($this->_entity, 'create', $params);
643
644 // Retrieve the test value
645 $params = array(
646 'activity_type_id' => $this->test_activity_type_value,
647 'sequential' => 1,
648 'return.custom_' . $ids['custom_field_id'] => 1,
649 );
650 $result = $this->callAPIAndDocument('activity', 'get', $params, __FUNCTION__, __FILE__);
651 $this->assertEquals("custom string", $result['values'][0]['custom_' . $ids['custom_field_id']]);
652
653 $this->assertEquals($this->test_activity_type_value, $result['values'][0]['activity_type_id']);
654 $this->assertEquals('test activity type id', $result['values'][0]['subject'], 'In line ' . __LINE__);
655 $this->customFieldDelete($ids['custom_field_id']);
656 $this->customGroupDelete($ids['custom_group_id']);
657 }
658
659 /**
660 * Test civicrm_activity_get() with a good activity ID which
661 * has associated custom data
662 */
663 function testActivityGetContact_idCustom() {
664 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, __FILE__);
665
666 $params = $this->_params;
667 $params['custom_' . $ids['custom_field_id']] = "custom string";
668
669 $result = $this->callAPISuccess($this->_entity, 'create', $params);
670 // Retrieve the test value
671 $params = array(
672 'contact_id' => $this->_params['source_contact_id'],
673 'activity_type_id' => $this->test_activity_type_value,
674 'sequential' => 1,
675 'return.custom_' . $ids['custom_field_id'] => 1,
676 );
677 $result = $this->callAPIAndDocument('activity', 'get', $params, __FUNCTION__, __FILE__);
678 $this->assertEquals("custom string", $result['values'][0]['custom_' . $ids['custom_field_id']], ' in line ' . __LINE__);
679
680 $this->assertEquals($this->test_activity_type_value, $result['values'][0]['activity_type_id'], 'In line ' . __LINE__);
681 $this->assertEquals('test activity type id', $result['values'][0]['subject'], 'In line ' . __LINE__);
682 $this->assertEquals($result['values'][0]['id'], $result['id'], 'in line ' . __LINE__);
683 }
684
685 /**
686 * check activity deletion with empty params
687 */
688 function testDeleteActivityForEmptyParams() {
689 $params = array('version' => $this->_apiversion);
690 $result = $this->callAPIFailure('activity', 'delete', $params);
691 }
692
693 /**
694 * check activity deletion without activity id
695 */
696 function testDeleteActivityWithoutId() {
697 $params = array(
698 'activity_name' => 'Meeting',
699 'version' => $this->_apiversion,
700 );
701 $result = $this->callAPIFailure('activity', 'delete', $params);
702 }
703
704 /**
705 * check activity deletion without activity type
706 */
707 function testDeleteActivityWithoutActivityType() {
708 $params = array('id' => 1);
709 $result = $this->callAPIFailure('activity', 'delete', $params);
710 }
711
712 /**
713 * check activity deletion with incorrect data
714 */
715 function testDeleteActivityWithIncorrectActivityType() {
716 $params = array(
717 'id' => 1,
718 'activity_name' => 'Test Activity',
719 );
720
721 $result = $this->callAPIFailure('activity', 'delete', $params);
722 }
723
724 /**
725 * check activity deletion with correct data
726 */
727 function testDeleteActivity() {
728 $result = $this->callAPISuccess('activity', 'create', $this->_params);
729 $params = array(
730 'id' => $result['id'],
731 'version' => $this->_apiversion,
732 );
733
734 $result = $this->callAPISuccess('activity', 'delete', $params);
735 $this->documentMe($params, $result, __FUNCTION__, __FILE__);
736 }
737
738 /**
739 * check if required fields are not passed
740 */
741 function testActivityUpdateWithoutRequired() {
742 $params = array(
743 'subject' => 'this case should fail',
744 'scheduled_date_time' => date('Ymd'),
745 );
746
747 $result = $this->callAPIFailure('activity', 'create', $params);
748 }
749
750 /**
751 * Test civicrm_activity_update() with non-numeric id
752 */
753 function testActivityUpdateWithNonNumericId() {
754 $params = array(
755 'id' => 'lets break it',
756 'activity_name' => 'Meeting',
757 'subject' => 'this case should fail',
758 'scheduled_date_time' => date('Ymd'),
759 );
760
761 $result = $this->callAPIFailure('activity', 'create', $params);
762 }
763
764 /**
765 * check with incorrect required fields
766 */
767 function testActivityUpdateWithIncorrectContactActivityType() {
768 $params = array(
769 'id' => 1,
770 'activity_name' => 'Test Activity',
771 'subject' => 'this case should fail',
772 'scheduled_date_time' => date('Ymd'),
773 'source_contact_id' => $this->_contactID,
774 );
775
776 $result = $this->callAPIFailure('activity', 'create', $params,
777 'Invalid Activity Id');
778 }
779
780 /**
781 * Test civicrm_activity_update() to update an existing activity
782 */
783 function testActivityUpdate() {
784 $result = $this->callAPISuccess('activity', 'create', $this->_params);
785
786 $params = array(
787 'id' => $result['id'],
788 'subject' => 'Make-it-Happen Meeting',
789 'activity_date_time' => '20091011123456',
790 'duration' => 120,
791 'location' => '21, Park Avenue',
792 'details' => 'Lets update Meeting',
793 'status_id' => 1,
794 'source_contact_id' => $this->_contactID,
795 'priority_id' => 1,
796 );
797
798 $result = $this->callAPISuccess('activity', 'create', $params);
799 //hack on date comparison - really we should make getAndCheck smarter to handle dates
800 $params['activity_date_time'] = '2009-10-11 12:34:56';
801 // we also unset source_contact_id since it is stored in an aux table
802 unset($params['source_contact_id']);
803 $this->getAndCheck($params, $result['id'], 'activity');
804 }
805
806 /**
807 * Test civicrm_activity_update() with valid parameters
808 * and some custom data
809 */
810 function testActivityUpdateCustom() {
811 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, __FILE__);
812
813 $params = $this->_params;
814
815 // Create an activity with custom data
816 //this has been updated from the previous 'old format' function - need to make it work
817 $params = array(
818 'source_contact_id' => $this->_contactID,
819 'subject' => 'Make-it-Happen Meeting',
820 'activity_date_time' => '2009-10-18',
821 'duration' => 120,
822 'location' => 'Pensulvania',
823 'details' => 'a test activity to check the update api',
824 'status_id' => 1,
825 'activity_name' => 'Test activity type',
826 'version' => $this->_apiversion,
827 'custom_' . $ids['custom_field_id'] => 'custom string',
828 );
829 $result = $this->callAPISuccess('activity', 'create', $params);
830
831 $activityId = $result['id'];
832 $result = $this->callAPISuccess($this->_entity, 'get', array('return.custom_' . $ids['custom_field_id'] => 1, 'version' => 3, 'id' => $result['id']));
833 $this->assertEquals("custom string", $result['values'][$result['id']]['custom_' . $ids['custom_field_id']], ' in line ' . __LINE__);
834 $this->assertEquals("2009-10-18 00:00:00", $result['values'][$result['id']]['activity_date_time'], ' in line ' . __LINE__);
835 $fields = $this->callAPISuccess('activity', 'getfields', array('version' => $this->_apiversion));
836 $this->assertTrue(is_array($fields['values']['custom_' . $ids['custom_field_id']]));
837
838 // Update the activity with custom data
839 $params = array(
840 'id' => $activityId,
841 'source_contact_id' => $this->_contactID,
842 'subject' => 'Make-it-Happen Meeting',
843 'status_id' => 1,
844 'activity_name' => 'Test activity type',
845 // add this since dates are messed up
846 'activity_date_time' => date('Ymd'),
847 'custom_' . $ids['custom_field_id'] => 'Updated my test data',
848 'version' => $this->_apiversion,
849 );
850 $result = $this->callAPISuccess('Activity', 'Create', $params);
851
852 $result = $this->callAPISuccess($this->_entity, 'get', array('return.custom_' . $ids['custom_field_id'] => 1, 'version' => 3, 'id' => $result['id']));
853 $this->assertEquals("Updated my test data", $result['values'][$result['id']]['custom_' . $ids['custom_field_id']], ' in line ' . __LINE__);
854 }
855
856 /**
857 * Test civicrm_activity_update() for core activity fields
858 * and some custom data
859 */
860 function testActivityUpdateCheckCoreFields() {
861 $params = $this->_params;
862 $contact1Params = array(
863 'first_name' => 'John',
864 'middle_name' => 'J.',
865 'last_name' => 'Anderson',
866 'prefix_id' => 3,
867 'suffix_id' => 3,
868 'email' => 'john_anderson@civicrm.org',
869 'contact_type' => 'Individual',
870 );
871
872 $contact1 = $this->individualCreate($contact1Params);
873 $contact2Params = array(
874 'first_name' => 'Michal',
875 'middle_name' => 'J.',
876 'last_name' => 'Anderson',
877 'prefix_id' => 3,
878 'suffix_id' => 3,
879 'email' => 'michal_anderson@civicrm.org',
880 'contact_type' => 'Individual',
881 );
882
883 $contact2 = $this->individualCreate($contact2Params);
884
885 $params['assignee_contact_id'] = array($contact1, $contact2);
886 $params['target_contact_id'] = array($contact2 => $contact2);
887 $result = $this->callAPISuccess('Activity', 'Create', $params);
888
889 $activityId = $result['id'];
890 $getParams = array(
891 'return.assignee_contact_id' => 1,
892 'return.target_contact_id' => 1,
893 'version' => $this->_apiversion,
894 'id' => $activityId,
895 );
896 $result = $this->callAPISuccess($this->_entity, 'get',$getParams );
897 $assignee = $result['values'][$result['id']]['assignee_contact_id'];
898 $target = $result['values'][$result['id']]['target_contact_id'];
899 $this->assertEquals(2, count($assignee), ' in line ' . __LINE__);
900 $this->assertEquals(1, count($target), ' in line ' . __LINE__);
901 $this->assertEquals(TRUE, in_array($contact1, $assignee), ' in line ' . __LINE__);
902 $this->assertEquals(TRUE, in_array($contact2, $target), ' in line ' . __LINE__);
903
904 $contact3Params = array(
905 'first_name' => 'Jijo',
906 'middle_name' => 'J.',
907 'last_name' => 'Anderson',
908 'prefix_id' => 3,
909 'suffix_id' => 3,
910 'email' => 'jijo_anderson@civicrm.org',
911 'contact_type' => 'Individual',
912 );
913
914 $contact4Params = array(
915 'first_name' => 'Grant',
916 'middle_name' => 'J.',
917 'last_name' => 'Anderson',
918 'prefix_id' => 3,
919 'suffix_id' => 3,
920 'email' => 'grant_anderson@civicrm.org',
921 'contact_type' => 'Individual',
922 );
923
924 $contact3 = $this->individualCreate($contact3Params);
925 $contact4 = $this->individualCreate($contact4Params);
926
927 $params = array();
928 $params['id'] = $activityId;
929 $params['assignee_contact_id'] = array($contact3 => $contact3);
930 $params['target_contact_id'] = array($contact4 => $contact4);
931
932 $result = $this->callAPISuccess('activity', 'create', $params);
933
934 $this->assertEquals($activityId, $result['id'], ' in line ' . __LINE__);
935
936 $result = $this->callAPISuccess(
937 $this->_entity,
938 'get',
939 array(
940 'return.assignee_contact_id' => 1,
941 'return.target_contact_id' => 1,
942 'return.source_contact_id' => 1,
943 'id' => $result['id']
944 )
945 );
946
947 $assignee = $result['values'][$result['id']]['assignee_contact_id'];
948 $target = $result['values'][$result['id']]['target_contact_id'];
949
950 $this->assertEquals(1, count($assignee), ' in line ' . __LINE__);
951 $this->assertEquals(1, count($target), ' in line ' . __LINE__);
952 $this->assertEquals(TRUE, in_array($contact3, $assignee), ' in line ' . __LINE__);
953 $this->assertEquals(TRUE, in_array($contact4, $target), ' in line ' . __LINE__);
954
955 foreach ($this->_params as $fld => $val) {
956 $this->assertEquals($val, $result['values'][$result['id']][$fld]);
957 }
958 }
959
960 /**
961 * Test civicrm_activity_update() where the DB has a date_time
962 * value and there is none in the update params.
963 */
964 function testActivityUpdateNotDate() {
965 $result = $this->callAPISuccess('activity', 'create', $this->_params);
966
967 $params = array(
968 'id' => $result['id'],
969 'subject' => 'Make-it-Happen Meeting',
970 'duration' => 120,
971 'location' => '21, Park Avenue',
972 'details' => 'Lets update Meeting',
973 'status_id' => 1,
974 'source_contact_id' => $this->_contactID,
975 'priority_id' => 1,
976 );
977
978 $result = $this->callAPISuccess('activity', 'create', $params);
979 //hack on date comparison - really we should make getAndCheck smarter to handle dates
980 $params['activity_date_time'] = $this->_params['activity_date_time'];
981 // we also unset source_contact_id since it is stored in an aux table
982 unset($params['source_contact_id']);
983 $this->getAndCheck($params, $result['id'], 'activity');
984 }
985
986 /**
987 * check activity update with status
988 */
989 function testActivityUpdateWithStatus() {
990 $activity = $this->callAPISuccess('activity', 'create', $this->_params);
991 $params = array(
992 'id' => $activity['id'],
993 'source_contact_id' => $this->_contactID,
994 'subject' => 'Hurry update works',
995 'status_id' => 1,
996 'activity_name' => 'Test activity type',
997 );
998
999 $result = $this->callAPISuccess('activity', 'create', $params);
1000 $this->assertEquals($result['id'], $activity['id']);
1001 $this->assertEquals($result['values'][$activity['id']]['subject'], 'Hurry update works');
1002 $this->assertEquals($result['values'][$activity['id']]['status_id'], 1
1003 );
1004 }
1005
1006 /**
1007 * Test civicrm_activity_update() where the source_contact_id
1008 * is not in the update params.
1009 */
1010 function testActivityUpdateKeepSource() {
1011 $activity = $this->callAPISuccess('activity', 'create', $this->_params);
1012 // Updating the activity but not providing anything for the source contact
1013 // (It was set as $this->_contactID earlier.)
1014 $params = array(
1015 'id' => $activity['id'],
1016 'subject' => 'Updated Make-it-Happen Meeting',
1017 'duration' => 120,
1018 'location' => '21, Park Avenue',
1019 'details' => 'Lets update Meeting',
1020 'status_id' => 1,
1021 'activity_name' => 'Test activity type',
1022 'priority_id' => 1,
1023 );
1024
1025 $result = $this->callAPISuccess('activity', 'create', $params);
1026 $findactivity = $this->callAPISuccess('Activity', 'Get', array('id' => $activity['id']));
1027 }
1028
1029 /**
1030 * Test civicrm_activities_contact_get()
1031 */
1032 function testActivitiesContactGet() {
1033 $activity = $this->callAPISuccess('activity', 'create', $this->_params);
1034 $activity2 = $this->callAPISuccess('activity', 'create', $this->_params2);
1035 // Get activities associated with contact $this->_contactID
1036 $params = array(
1037 'contact_id' => $this->_contactID,
1038 );
1039 $result = $this->callAPISuccess('activity', 'get', $params);
1040
1041 $this->assertEquals(2, $result['count'], 'In line ' . __LINE__);
1042 $this->assertEquals($this->test_activity_type_value, $result['values'][$activity['id']]['activity_type_id'], 'In line ' . __LINE__);
1043 $this->assertEquals('Test activity type', $result['values'][$activity['id']]['activity_name'], 'In line ' . __LINE__);
1044 $this->assertEquals('Test activity type', $result['values'][$activity2['id']]['activity_name'], 'In line ' . __LINE__);
1045 }
1046 /*
1047 * test chained Activity format
1048 */
1049 function testchainedActivityGet() {
1050
1051 $activity = $this->callAPISuccess('Contact', 'Create', array(
1052 'display_name' => "bob brown",
1053 'contact_type' => 'Individual',
1054 'api.activity_type.create' => array(
1055 'weight' => '2',
1056 'label' => 'send out letters',
1057 'filter' => 0,
1058 'is_active' => 1,
1059 'is_optgroup' => 1,
1060 'is_default' => 0,
1061 ), 'api.activity.create' => array('subject' => 'send letter', 'activity_type_id' => '$value.api.activity_type.create.values.0.value'),
1062 ));
1063
1064 $result = $this->callAPISuccess('Activity', 'Get', array(
1065 'id' => $activity['id'],
1066 'return.assignee_contact_id' => 1,
1067 'api.contact.get' => array('api.pledge.get' => 1),
1068 ));
1069 }
1070
1071 /**
1072 * Test civicrm_activity_contact_get() with invalid Contact Id
1073 */
1074 function testActivitiesContactGetWithInvalidContactId() {
1075 $params = array('contact_id' => 'contact');
1076 $result = $this->callAPIFailure('activity', 'get', $params);
1077 }
1078
1079 /**
1080 * Test civicrm_activity_contact_get() with contact having no Activity
1081 */
1082 function testActivitiesContactGetHavingNoActivity() {
1083 $params = array(
1084 'first_name' => 'dan',
1085 'last_name' => 'conberg',
1086 'email' => 'dan.conberg@w.co.in',
1087 'contact_type' => 'Individual',
1088 );
1089
1090 $contact = $this->callAPISuccess('contact', 'create', $params);
1091 $params = array(
1092 'contact_id' => $contact['id'],
1093 );
1094 $result = $this->callAPISuccess('activity', 'get', $params);
1095 $this->assertEquals($result['count'], 0, 'in line ' . __LINE__);
1096 }
1097
1098 function testGetFields() {
1099 $params = array('action' => 'create');
1100 $result = $this->callAPIAndDocument('activity', 'getfields', $params, __FUNCTION__, __FILE__, NULL, NULL, 'getfields');
1101 $this->assertTrue(is_array($result['values']), 'get fields doesnt return values array in line ' . __LINE__);
1102 // $this->assertTrue(is_array($result['values']['priority_id']['options']));
1103 foreach ($result['values'] as $key => $value) {
1104 $this->assertTrue(is_array($value), $key . " is not an array in line " . __LINE__);
1105 }
1106 }
1107 }