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