5bdc9ecc7e3be72d133f22fb1920ea5b90e16d60
[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/Activity/Create.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
552 /**
553 * Test civicrm_activity_get() with filter target_contact_id
554 */
555 function testActivityGetTargetFilter() {
556 $params = $this->_params;
557 $contact1Params = array(
558 'first_name' => 'John',
559 'middle_name' => 'J.',
560 'last_name' => 'Anderson',
561 'prefix_id' => 3,
562 'suffix_id' => 3,
563 'email' => 'john_anderson@civicrm.org',
564 'contact_type' => 'Individual',
565 );
566
567 $contact1 = $this->individualCreate($contact1Params);
568 $contact2Params = array(
569 'first_name' => 'Michal',
570 'middle_name' => 'J.',
571 'last_name' => 'Anderson',
572 'prefix_id' => 3,
573 'suffix_id' => 3,
574 'email' => 'michal_anderson@civicrm.org',
575 'contact_type' => 'Individual',
576 );
577
578 $contact2 = $this->individualCreate($contact2Params);
579
580 $params['assignee_contact_id'] = array($contact1, $contact2);
581 $params['target_contact_id'] = array($contact2 => $contact2);
582 $activity = $this->callAPISuccess('Activity', 'Create', $params);
583
584 $activityget = $this->callAPISuccess('Activity', 'get', array('id' => $activity['id'], 'target_contact_id' => $contact2,'return.target_contact_id' => 1));
585 $this->assertEquals($activity['id'], $activityget['id'], 'In line ' . __LINE__);
586 $this->assertEquals($contact2, $activityget['values'][$activityget['id']]['target_contact_id'][0], 'In line ' . __LINE__);
587
588 $activityget = $this->callAPISuccess('activity', 'get', array('target_contact_id' => $this->_contactID,'return.target_contact_id' => 1,'id' => $activity['id']));
589 if ($activityget['count'] > 0) {
590 $this->assertNotEquals($contact2, $activityget['values'][$activityget['id']]['target_contact_id'][0], 'In line ' . __LINE__);
591 }
592 }
593
594 /*
595 * test that get functioning does filtering
596 */
597 function testGetStatusID() {
598 $params = array(
599 'source_contact_id' => $this->_contactID,
600 'subject' => 'Make-it-Happen Meeting',
601 'activity_date_time' => '20110316',
602 'duration' => 120,
603 'location' => 'Pensulvania',
604 'details' => 'a test activity',
605 'status_id' => 1,
606 'activity_name' => 'Test activity type',
607 'priority_id' => 1,
608 );
609 $this->callAPISuccess('Activity', 'Create', $params);
610 $result = $this->callAPISuccess('Activity', 'Get', array('activity_status_id' => '1'));
611 $this->assertEquals(1, $result['count'], 'one activity of status 1 should exist');
612
613 $result = $this->callAPISuccess('Activity', 'Get', array('status_id' => '1'));
614 $this->assertEquals(1, $result['count'], 'status_id should also work');
615
616 $result = $this->callAPISuccess('Activity', 'Get', array('activity_status_id' => '2'));
617 $this->assertEquals(0, $result['count'], 'No activities of status 1 should exist');
618 $result = $this->callAPISuccess('Activity', 'Get', array(
619 'version' => $this->_apiversion,
620 'status_id' => '2'));
621 $this->assertEquals(0, $result['count'], 'No activities of status 1 should exist');
622
623
624 }
625
626 /*
627 * test that get functioning does filtering
628 */
629 function testGetFilterMaxDate() {
630 $params = array(
631 'source_contact_id' => $this->_contactID,
632 'subject' => 'Make-it-Happen Meeting',
633 'activity_date_time' => '20110101',
634 'duration' => 120,
635 'location' => 'Pensulvania',
636 'details' => 'a test activity',
637 'status_id' => 1,
638 'activity_name' => 'Test activity type',
639 'version' => $this->_apiversion,
640 'priority_id' => 1,
641 );
642 $activityOne = $this->callAPISuccess('Activity', 'Create', $params);
643 $params['activity_date_time'] = 20120216;
644 $activityTwo = $this->callAPISuccess('Activity', 'Create', $params);
645 $result = $this->callAPISuccess('Activity', 'Get', array(
646 'version' => 3,
647 ));
648 $description = "demonstrates _low filter (at time of writing doesn't work if contact_id is set";
649 $subfile = "DateTimeLow";
650 $this->assertEquals(2, $result['count']);
651 $params = array(
652 'version' => 3,
653 'filter.activity_date_time_low' => '20120101000000',
654 'sequential' => 1,
655 );
656 $result = $this->callAPIAndDocument('Activity', 'Get', $params, __FUNCTION__, __FILE__, $description, $subfile);
657 $this->assertEquals(1, $result['count'], 'in line ' . __LINE__);
658 $description = "demonstrates _high filter (at time of writing doesn't work if contact_id is set";
659 $subfile = "DateTimeHigh";
660 $this->assertEquals('2012-02-16 00:00:00', $result['values'][0]['activity_date_time'], 'in line ' . __LINE__);
661 $params = array(
662 'source_contact_id' => $this->_contactID,
663 'version' => 3,
664 'filter.activity_date_time_high' => '20120101000000',
665 'sequential' => 1,
666 );
667 $result = $this->callAPIAndDocument('Activity', 'Get', $params, __FUNCTION__, __FILE__, $description, $subfile);
668
669 $this->assertEquals(1, $result['count']);
670 $this->assertEquals('2011-01-01 00:00:00', $result['values'][0]['activity_date_time'], 'in line ' . __LINE__);
671
672 $this->callAPISuccess('Activity', 'Delete', array('version' => 3, 'id' => $activityOne['id']));
673 $this->callAPISuccess('Activity', 'Delete', array('version' => 3, 'id' => $activityTwo['id']));
674 }
675
676 /**
677 * Test civicrm_activity_get() with a good activity ID which
678 * has associated custom data
679 */
680 function testActivityGetGoodIDCustom() {
681 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, __FILE__);
682
683 $params = $this->_params;
684 $params['custom_' . $ids['custom_field_id']] = "custom string";
685
686 $result = $this->callAPISuccess($this->_entity, 'create', $params);
687
688 // Retrieve the test value
689 $params = array(
690 'activity_type_id' => $this->test_activity_type_value,
691 'sequential' => 1,
692 'return.custom_' . $ids['custom_field_id'] => 1,
693 );
694 $result = $this->callAPIAndDocument('activity', 'get', $params, __FUNCTION__, __FILE__);
695 $this->assertEquals("custom string", $result['values'][0]['custom_' . $ids['custom_field_id']]);
696
697 $this->assertEquals($this->test_activity_type_value, $result['values'][0]['activity_type_id']);
698 $this->assertEquals('test activity type id', $result['values'][0]['subject'], 'In line ' . __LINE__);
699 $this->customFieldDelete($ids['custom_field_id']);
700 $this->customGroupDelete($ids['custom_group_id']);
701 }
702
703 /**
704 * Test civicrm_activity_get() with a good activity ID which
705 * has associated custom data
706 */
707 function testActivityGetContact_idCustom() {
708 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, __FILE__);
709
710 $params = $this->_params;
711 $params['custom_' . $ids['custom_field_id']] = "custom string";
712
713 $result = $this->callAPISuccess($this->_entity, 'create', $params);
714 // Retrieve the test value
715 $params = array(
716 'contact_id' => $this->_params['source_contact_id'],
717 'activity_type_id' => $this->test_activity_type_value,
718 'sequential' => 1,
719 'return.custom_' . $ids['custom_field_id'] => 1,
720 );
721 $result = $this->callAPIAndDocument('activity', 'get', $params, __FUNCTION__, __FILE__);
722 $this->assertEquals("custom string", $result['values'][0]['custom_' . $ids['custom_field_id']], ' in line ' . __LINE__);
723
724 $this->assertEquals($this->test_activity_type_value, $result['values'][0]['activity_type_id'], 'In line ' . __LINE__);
725 $this->assertEquals('test activity type id', $result['values'][0]['subject'], 'In line ' . __LINE__);
726 $this->assertEquals($result['values'][0]['id'], $result['id'], 'in line ' . __LINE__);
727 }
728
729 /**
730 * check activity deletion with empty params
731 */
732 function testDeleteActivityForEmptyParams() {
733 $params = array('version' => $this->_apiversion);
734 $result = $this->callAPIFailure('activity', 'delete', $params);
735 }
736
737 /**
738 * check activity deletion without activity id
739 */
740 function testDeleteActivityWithoutId() {
741 $params = array(
742 'activity_name' => 'Meeting',
743 'version' => $this->_apiversion,
744 );
745 $result = $this->callAPIFailure('activity', 'delete', $params);
746 }
747
748 /**
749 * check activity deletion without activity type
750 */
751 function testDeleteActivityWithoutActivityType() {
752 $params = array('id' => 1);
753 $result = $this->callAPIFailure('activity', 'delete', $params);
754 }
755
756 /**
757 * check activity deletion with incorrect data
758 */
759 function testDeleteActivityWithIncorrectActivityType() {
760 $params = array(
761 'id' => 1,
762 'activity_name' => 'Test Activity',
763 );
764
765 $result = $this->callAPIFailure('activity', 'delete', $params);
766 }
767
768 /**
769 * check activity deletion with correct data
770 */
771 function testDeleteActivity() {
772 $result = $this->callAPISuccess('activity', 'create', $this->_params);
773 $params = array(
774 'id' => $result['id'],
775 'version' => $this->_apiversion,
776 );
777
778 $result = $this->callAPISuccess('activity', 'delete', $params);
779 $this->documentMe($params, $result, __FUNCTION__, __FILE__);
780 }
781
782 /**
783 * check if required fields are not passed
784 */
785 function testActivityUpdateWithoutRequired() {
786 $params = array(
787 'subject' => 'this case should fail',
788 'scheduled_date_time' => date('Ymd'),
789 );
790
791 $result = $this->callAPIFailure('activity', 'create', $params);
792 }
793
794 /**
795 * Test civicrm_activity_update() with non-numeric id
796 */
797 function testActivityUpdateWithNonNumericId() {
798 $params = array(
799 'id' => 'lets break it',
800 'activity_name' => 'Meeting',
801 'subject' => 'this case should fail',
802 'scheduled_date_time' => date('Ymd'),
803 );
804
805 $result = $this->callAPIFailure('activity', 'create', $params);
806 }
807
808 /**
809 * check with incorrect required fields
810 */
811 function testActivityUpdateWithIncorrectContactActivityType() {
812 $params = array(
813 'id' => 1,
814 'activity_name' => 'Test Activity',
815 'subject' => 'this case should fail',
816 'scheduled_date_time' => date('Ymd'),
817 'source_contact_id' => $this->_contactID,
818 );
819
820 $result = $this->callAPIFailure('activity', 'create', $params,
821 'Invalid Activity Id');
822 }
823
824 /**
825 * Test civicrm_activity_update() to update an existing activity
826 */
827 function testActivityUpdate() {
828 $result = $this->callAPISuccess('activity', 'create', $this->_params);
829
830 $params = array(
831 'id' => $result['id'],
832 'subject' => 'Make-it-Happen Meeting',
833 'activity_date_time' => '20091011123456',
834 'duration' => 120,
835 'location' => '21, Park Avenue',
836 'details' => 'Lets update Meeting',
837 'status_id' => 1,
838 'source_contact_id' => $this->_contactID,
839 'priority_id' => 1,
840 );
841
842 $result = $this->callAPISuccess('activity', 'create', $params);
843 //hack on date comparison - really we should make getAndCheck smarter to handle dates
844 $params['activity_date_time'] = '2009-10-11 12:34:56';
845 // we also unset source_contact_id since it is stored in an aux table
846 unset($params['source_contact_id']);
847 $this->getAndCheck($params, $result['id'], 'activity');
848 }
849
850 /**
851 * Test civicrm_activity_update() with valid parameters
852 * and some custom data
853 */
854 function testActivityUpdateCustom() {
855 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, __FILE__);
856
857 $params = $this->_params;
858
859 // Create an activity with custom data
860 //this has been updated from the previous 'old format' function - need to make it work
861 $params = array(
862 'source_contact_id' => $this->_contactID,
863 'subject' => 'Make-it-Happen Meeting',
864 'activity_date_time' => '2009-10-18',
865 'duration' => 120,
866 'location' => 'Pensulvania',
867 'details' => 'a test activity to check the update api',
868 'status_id' => 1,
869 'activity_name' => 'Test activity type',
870 'version' => $this->_apiversion,
871 'custom_' . $ids['custom_field_id'] => 'custom string',
872 );
873 $result = $this->callAPISuccess('activity', 'create', $params);
874
875 $activityId = $result['id'];
876 $result = $this->callAPISuccess($this->_entity, 'get', array('return.custom_' . $ids['custom_field_id'] => 1, 'version' => 3, 'id' => $result['id']));
877 $this->assertEquals("custom string", $result['values'][$result['id']]['custom_' . $ids['custom_field_id']], ' in line ' . __LINE__);
878 $this->assertEquals("2009-10-18 00:00:00", $result['values'][$result['id']]['activity_date_time'], ' in line ' . __LINE__);
879 $fields = $this->callAPISuccess('activity', 'getfields', array('version' => $this->_apiversion));
880 $this->assertTrue(is_array($fields['values']['custom_' . $ids['custom_field_id']]));
881
882 // Update the activity with custom data
883 $params = array(
884 'id' => $activityId,
885 'source_contact_id' => $this->_contactID,
886 'subject' => 'Make-it-Happen Meeting',
887 'status_id' => 1,
888 'activity_name' => 'Test activity type',
889 // add this since dates are messed up
890 'activity_date_time' => date('Ymd'),
891 'custom_' . $ids['custom_field_id'] => 'Updated my test data',
892 'version' => $this->_apiversion,
893 );
894 $result = $this->callAPISuccess('Activity', 'Create', $params);
895
896 $result = $this->callAPISuccess($this->_entity, 'get', array('return.custom_' . $ids['custom_field_id'] => 1, 'version' => 3, 'id' => $result['id']));
897 $this->assertEquals("Updated my test data", $result['values'][$result['id']]['custom_' . $ids['custom_field_id']], ' in line ' . __LINE__);
898 }
899
900 /**
901 * Test civicrm_activity_update() for core activity fields
902 * and some custom data
903 */
904 function testActivityUpdateCheckCoreFields() {
905 $params = $this->_params;
906 $contact1Params = array(
907 'first_name' => 'John',
908 'middle_name' => 'J.',
909 'last_name' => 'Anderson',
910 'prefix_id' => 3,
911 'suffix_id' => 3,
912 'email' => 'john_anderson@civicrm.org',
913 'contact_type' => 'Individual',
914 );
915
916 $contact1 = $this->individualCreate($contact1Params);
917 $contact2Params = array(
918 'first_name' => 'Michal',
919 'middle_name' => 'J.',
920 'last_name' => 'Anderson',
921 'prefix_id' => 3,
922 'suffix_id' => 3,
923 'email' => 'michal_anderson@civicrm.org',
924 'contact_type' => 'Individual',
925 );
926
927 $contact2 = $this->individualCreate($contact2Params);
928
929 $params['assignee_contact_id'] = array($contact1, $contact2);
930 $params['target_contact_id'] = array($contact2 => $contact2);
931 $result = $this->callAPISuccess('Activity', 'Create', $params);
932
933 $activityId = $result['id'];
934 $getParams = array(
935 'return.assignee_contact_id' => 1,
936 'return.target_contact_id' => 1,
937 'version' => $this->_apiversion,
938 'id' => $activityId,
939 );
940 $result = $this->callAPISuccess($this->_entity, 'get',$getParams );
941 $assignee = $result['values'][$result['id']]['assignee_contact_id'];
942 $target = $result['values'][$result['id']]['target_contact_id'];
943 $this->assertEquals(2, count($assignee), ' in line ' . __LINE__);
944 $this->assertEquals(1, count($target), ' in line ' . __LINE__);
945 $this->assertEquals(TRUE, in_array($contact1, $assignee), ' in line ' . __LINE__);
946 $this->assertEquals(TRUE, in_array($contact2, $target), ' in line ' . __LINE__);
947
948 $contact3Params = array(
949 'first_name' => 'Jijo',
950 'middle_name' => 'J.',
951 'last_name' => 'Anderson',
952 'prefix_id' => 3,
953 'suffix_id' => 3,
954 'email' => 'jijo_anderson@civicrm.org',
955 'contact_type' => 'Individual',
956 );
957
958 $contact4Params = array(
959 'first_name' => 'Grant',
960 'middle_name' => 'J.',
961 'last_name' => 'Anderson',
962 'prefix_id' => 3,
963 'suffix_id' => 3,
964 'email' => 'grant_anderson@civicrm.org',
965 'contact_type' => 'Individual',
966 );
967
968 $contact3 = $this->individualCreate($contact3Params);
969 $contact4 = $this->individualCreate($contact4Params);
970
971 $params = array();
972 $params['id'] = $activityId;
973 $params['assignee_contact_id'] = array($contact3 => $contact3);
974 $params['target_contact_id'] = array($contact4 => $contact4);
975
976 $result = $this->callAPISuccess('activity', 'create', $params);
977
978 $this->assertEquals($activityId, $result['id'], ' in line ' . __LINE__);
979
980 $result = $this->callAPISuccess(
981 $this->_entity,
982 'get',
983 array(
984 'return.assignee_contact_id' => 1,
985 'return.target_contact_id' => 1,
986 'return.source_contact_id' => 1,
987 'id' => $result['id']
988 )
989 );
990
991 $assignee = $result['values'][$result['id']]['assignee_contact_id'];
992 $target = $result['values'][$result['id']]['target_contact_id'];
993
994 $this->assertEquals(1, count($assignee), ' in line ' . __LINE__);
995 $this->assertEquals(1, count($target), ' in line ' . __LINE__);
996 $this->assertEquals(TRUE, in_array($contact3, $assignee), ' in line ' . __LINE__);
997 $this->assertEquals(TRUE, in_array($contact4, $target), ' in line ' . __LINE__);
998
999 foreach ($this->_params as $fld => $val) {
1000 $this->assertEquals($val, $result['values'][$result['id']][$fld]);
1001 }
1002 }
1003
1004 /**
1005 * Test civicrm_activity_update() where the DB has a date_time
1006 * value and there is none in the update params.
1007 */
1008 function testActivityUpdateNotDate() {
1009 $result = $this->callAPISuccess('activity', 'create', $this->_params);
1010
1011 $params = array(
1012 'id' => $result['id'],
1013 'subject' => 'Make-it-Happen Meeting',
1014 'duration' => 120,
1015 'location' => '21, Park Avenue',
1016 'details' => 'Lets update Meeting',
1017 'status_id' => 1,
1018 'source_contact_id' => $this->_contactID,
1019 'priority_id' => 1,
1020 );
1021
1022 $result = $this->callAPISuccess('activity', 'create', $params);
1023 //hack on date comparison - really we should make getAndCheck smarter to handle dates
1024 $params['activity_date_time'] = $this->_params['activity_date_time'];
1025 // we also unset source_contact_id since it is stored in an aux table
1026 unset($params['source_contact_id']);
1027 $this->getAndCheck($params, $result['id'], 'activity');
1028 }
1029
1030 /**
1031 * check activity update with status
1032 */
1033 function testActivityUpdateWithStatus() {
1034 $activity = $this->callAPISuccess('activity', 'create', $this->_params);
1035 $params = array(
1036 'id' => $activity['id'],
1037 'source_contact_id' => $this->_contactID,
1038 'subject' => 'Hurry update works',
1039 'status_id' => 1,
1040 'activity_name' => 'Test activity type',
1041 );
1042
1043 $result = $this->callAPISuccess('activity', 'create', $params);
1044 $this->assertEquals($result['id'], $activity['id']);
1045 $this->assertEquals($result['values'][$activity['id']]['subject'], 'Hurry update works');
1046 $this->assertEquals($result['values'][$activity['id']]['status_id'], 1
1047 );
1048 }
1049
1050 /**
1051 * Test civicrm_activity_update() where the source_contact_id
1052 * is not in the update params.
1053 */
1054 function testActivityUpdateKeepSource() {
1055 $activity = $this->callAPISuccess('activity', 'create', $this->_params);
1056 // Updating the activity but not providing anything for the source contact
1057 // (It was set as $this->_contactID earlier.)
1058 $params = array(
1059 'id' => $activity['id'],
1060 'subject' => 'Updated Make-it-Happen Meeting',
1061 'duration' => 120,
1062 'location' => '21, Park Avenue',
1063 'details' => 'Lets update Meeting',
1064 'status_id' => 1,
1065 'activity_name' => 'Test activity type',
1066 'priority_id' => 1,
1067 );
1068
1069 $result = $this->callAPISuccess('activity', 'create', $params);
1070 $findactivity = $this->callAPISuccess('Activity', 'Get', array('id' => $activity['id']));
1071 }
1072
1073 /**
1074 * Test civicrm_activities_contact_get()
1075 */
1076 function testActivitiesContactGet() {
1077 $activity = $this->callAPISuccess('activity', 'create', $this->_params);
1078 $activity2 = $this->callAPISuccess('activity', 'create', $this->_params2);
1079 // Get activities associated with contact $this->_contactID
1080 $params = array(
1081 'contact_id' => $this->_contactID,
1082 );
1083 $result = $this->callAPISuccess('activity', 'get', $params);
1084
1085 $this->assertEquals(2, $result['count'], 'In line ' . __LINE__);
1086 $this->assertEquals($this->test_activity_type_value, $result['values'][$activity['id']]['activity_type_id'], 'In line ' . __LINE__);
1087 $this->assertEquals('Test activity type', $result['values'][$activity['id']]['activity_name'], 'In line ' . __LINE__);
1088 $this->assertEquals('Test activity type', $result['values'][$activity2['id']]['activity_name'], 'In line ' . __LINE__);
1089 }
1090 /*
1091 * test chained Activity format
1092 */
1093 function testchainedActivityGet() {
1094
1095 $activity = $this->callAPISuccess('Contact', 'Create', array(
1096 'display_name' => "bob brown",
1097 'contact_type' => 'Individual',
1098 'api.activity_type.create' => array(
1099 'weight' => '2',
1100 'label' => 'send out letters',
1101 'filter' => 0,
1102 'is_active' => 1,
1103 'is_optgroup' => 1,
1104 'is_default' => 0,
1105 ), 'api.activity.create' => array('subject' => 'send letter', 'activity_type_id' => '$value.api.activity_type.create.values.0.value'),
1106 ));
1107
1108 $result = $this->callAPISuccess('Activity', 'Get', array(
1109 'id' => $activity['id'],
1110 'return.assignee_contact_id' => 1,
1111 'api.contact.get' => array('api.pledge.get' => 1),
1112 ));
1113 }
1114
1115 /**
1116 * Test civicrm_activity_contact_get() with invalid Contact Id
1117 */
1118 function testActivitiesContactGetWithInvalidContactId() {
1119 $params = array('contact_id' => 'contact');
1120 $result = $this->callAPIFailure('activity', 'get', $params);
1121 }
1122
1123 /**
1124 * Test civicrm_activity_contact_get() with contact having no Activity
1125 */
1126 function testActivitiesContactGetHavingNoActivity() {
1127 $params = array(
1128 'first_name' => 'dan',
1129 'last_name' => 'conberg',
1130 'email' => 'dan.conberg@w.co.in',
1131 'contact_type' => 'Individual',
1132 );
1133
1134 $contact = $this->callAPISuccess('contact', 'create', $params);
1135 $params = array(
1136 'contact_id' => $contact['id'],
1137 );
1138 $result = $this->callAPISuccess('activity', 'get', $params);
1139 $this->assertEquals($result['count'], 0, 'in line ' . __LINE__);
1140 }
1141
1142 function testGetFields() {
1143 $params = array('action' => 'create');
1144 $result = $this->callAPIAndDocument('activity', 'getfields', $params, __FUNCTION__, __FILE__, NULL, NULL, 'getfields');
1145 $this->assertTrue(is_array($result['values']), 'get fields doesnt return values array in line ' . __LINE__);
1146 // $this->assertTrue(is_array($result['values']['priority_id']['options']));
1147 foreach ($result['values'] as $key => $value) {
1148 $this->assertTrue(is_array($value), $key . " is not an array in line " . __LINE__);
1149 }
1150 }
1151 }