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