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