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