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