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