| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | CiviCRM version 4.7 | |
| 5 | +--------------------------------------------------------------------+ |
| 6 | | Copyright CiviCRM LLC (c) 2004-2017 | |
| 7 | +--------------------------------------------------------------------+ |
| 8 | | This file is a part of CiviCRM. | |
| 9 | | | |
| 10 | | CiviCRM is free software; you can copy, modify, and distribute it | |
| 11 | | under the terms of the GNU Affero General Public License | |
| 12 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | |
| 13 | | | |
| 14 | | CiviCRM is distributed in the hope that it will be useful, but | |
| 15 | | WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 16 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | |
| 17 | | See the GNU Affero General Public License for more details. | |
| 18 | | | |
| 19 | | You should have received a copy of the GNU Affero General Public | |
| 20 | | License and the CiviCRM Licensing Exception along | |
| 21 | | with this program; if not, contact CiviCRM LLC | |
| 22 | | at info[AT]civicrm[DOT]org. If you have questions about the | |
| 23 | | GNU Affero General Public License or the licensing of CiviCRM, | |
| 24 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | |
| 25 | +--------------------------------------------------------------------+ |
| 26 | */ |
| 27 | |
| 28 | /** |
| 29 | * Test class for Batch API - civicrm_participant_* |
| 30 | * |
| 31 | * @package CiviCRM_APIv3 |
| 32 | */ |
| 33 | require_once 'CRM/Utils/DeprecatedUtils.php'; |
| 34 | |
| 35 | |
| 36 | /** |
| 37 | * Class api_v3_ParticipantTest |
| 38 | * @group headless |
| 39 | */ |
| 40 | class api_v3_ParticipantTest extends CiviUnitTestCase { |
| 41 | |
| 42 | protected $_apiversion; |
| 43 | protected $_entity; |
| 44 | protected $_contactID; |
| 45 | protected $_contactID2; |
| 46 | protected $_createdParticipants; |
| 47 | protected $_participantID; |
| 48 | protected $_eventID; |
| 49 | protected $_individualId; |
| 50 | protected $_params; |
| 51 | |
| 52 | public function setUp() { |
| 53 | $this->_apiversion = 3; |
| 54 | parent::setUp(); |
| 55 | $this->_entity = 'participant'; |
| 56 | $event = $this->eventCreate(NULL); |
| 57 | $this->_eventID = $event['id']; |
| 58 | |
| 59 | $this->_contactID = $this->individualCreate(); |
| 60 | |
| 61 | $this->_createdParticipants = array(); |
| 62 | $this->_individualId = $this->individualCreate(); |
| 63 | |
| 64 | $this->_participantID = $this->participantCreate(array( |
| 65 | 'contact_id' => $this->_contactID, |
| 66 | 'event_id' => $this->_eventID, |
| 67 | )); |
| 68 | $this->_contactID2 = $this->individualCreate(); |
| 69 | $this->_participantID2 = $this->participantCreate(array( |
| 70 | 'contact_id' => $this->_contactID2, |
| 71 | 'event_id' => $this->_eventID, |
| 72 | 'registered_by_id' => $this->_participantID, |
| 73 | )); |
| 74 | $this->_participantID3 = $this->participantCreate(array( |
| 75 | 'contact_id' => $this->_contactID2, |
| 76 | 'event_id' => $this->_eventID, |
| 77 | )); |
| 78 | $this->_params = array( |
| 79 | 'contact_id' => $this->_contactID, |
| 80 | 'event_id' => $this->_eventID, |
| 81 | 'status_id' => 1, |
| 82 | 'role_id' => 1, |
| 83 | // to ensure it matches later on |
| 84 | 'register_date' => '2007-07-21 00:00:00', |
| 85 | 'source' => 'Online Event Registration: API Testing', |
| 86 | ); |
| 87 | } |
| 88 | |
| 89 | public function tearDown() { |
| 90 | $this->eventDelete($this->_eventID); |
| 91 | $tablesToTruncate = array( |
| 92 | 'civicrm_custom_group', |
| 93 | 'civicrm_custom_field', |
| 94 | 'civicrm_contact', |
| 95 | 'civicrm_participant', |
| 96 | ); |
| 97 | // true tells quickCleanup to drop any tables that might have been created in the test |
| 98 | $this->quickCleanup($tablesToTruncate, TRUE); |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Test get participants with role_id. |
| 103 | */ |
| 104 | public function testGetParticipantWithRole() { |
| 105 | $roleId = array(1, 2, 3); |
| 106 | foreach ($roleId as $role) { |
| 107 | $this->participantCreate(array( |
| 108 | 'contact_id' => $this->individualCreate(), |
| 109 | 'role_id' => $role, |
| 110 | 'event_id' => $this->_eventID, |
| 111 | )); |
| 112 | } |
| 113 | |
| 114 | $params = array( |
| 115 | 'role_id' => 2, |
| 116 | ); |
| 117 | $result = $this->callAPISuccess('participant', 'get', $params); |
| 118 | //Assert all the returned participants has a role_id of 2 |
| 119 | foreach ($result['values'] as $pid => $values) { |
| 120 | $this->assertEquals($values['participant_role_id'], 2); |
| 121 | } |
| 122 | |
| 123 | $this->participantCreate(array( |
| 124 | 'id' => $this->_participantID, |
| 125 | 'role_id' => NULL, |
| 126 | 'event_id' => $this->_eventID, |
| 127 | )); |
| 128 | |
| 129 | $params['role_id'] = array( |
| 130 | 'IS NULL' => 1, |
| 131 | ); |
| 132 | $result = $this->callAPISuccess('participant', 'get', $params); |
| 133 | foreach ($result['values'] as $pid => $values) { |
| 134 | $this->assertEquals($values['participant_role_id'], NULL); |
| 135 | } |
| 136 | |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * Check with complete array + custom field |
| 141 | * Note that the test is written on purpose without any |
| 142 | * variables specific to participant so it can be replicated into other entities |
| 143 | * and / or moved to the automated test suite |
| 144 | */ |
| 145 | public function testCreateWithCustom() { |
| 146 | $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, __FILE__); |
| 147 | |
| 148 | $params = $this->_params; |
| 149 | $params['custom_' . $ids['custom_field_id']] = "custom string"; |
| 150 | |
| 151 | $result = $this->callAPIAndDocument($this->_entity, 'create', $params, __FUNCTION__, __FILE__); |
| 152 | |
| 153 | $this->assertEquals($result['id'], $result['values'][$result['id']]['id']); |
| 154 | |
| 155 | $check = $this->callAPISuccess($this->_entity, 'get', array('id' => $result['id'])); |
| 156 | $this->assertEquals("custom string", $check['values'][$check['id']]['custom_' . $ids['custom_field_id']], ' in line ' . __LINE__); |
| 157 | |
| 158 | $this->customFieldDelete($ids['custom_field_id']); |
| 159 | $this->customGroupDelete($ids['custom_group_id']); |
| 160 | } |
| 161 | |
| 162 | |
| 163 | ///////////////// civicrm_participant_get methods |
| 164 | |
| 165 | /** |
| 166 | * Check with wrong params type. |
| 167 | */ |
| 168 | public function testGetWrongParamsType() { |
| 169 | $params = 'a string'; |
| 170 | $result = $this->callAPIFailure('participant', 'get', $params); |
| 171 | } |
| 172 | |
| 173 | /** |
| 174 | * Test civicrm_participant_get with empty params. |
| 175 | */ |
| 176 | public function testGetEmptyParams() { |
| 177 | $this->callAPISuccess('participant', 'get', array()); |
| 178 | } |
| 179 | |
| 180 | /** |
| 181 | * Check with participant_id. |
| 182 | */ |
| 183 | public function testGetParticipantIdOnly() { |
| 184 | $params = array( |
| 185 | 'participant_id' => $this->_participantID, |
| 186 | 'return' => array( |
| 187 | 'participant_id', |
| 188 | 'event_id', |
| 189 | 'participant_register_date', |
| 190 | 'participant_source', |
| 191 | ), |
| 192 | ); |
| 193 | $result = $this->callAPISuccess('participant', 'get', $params); |
| 194 | $this->assertAPISuccess($result, " in line " . __LINE__); |
| 195 | $this->assertEquals($result['values'][$this->_participantID]['event_id'], $this->_eventID, "in line " . __LINE__); |
| 196 | $this->assertEquals($result['values'][$this->_participantID]['participant_register_date'], '2007-02-19 00:00:00', "in line " . __LINE__); |
| 197 | $this->assertEquals($result['values'][$this->_participantID]['participant_source'], 'Wimbeldon', "in line " . __LINE__); |
| 198 | $params = array( |
| 199 | 'id' => $this->_participantID, |
| 200 | 'return' => 'id,participant_register_date,event_id', |
| 201 | |
| 202 | ); |
| 203 | $result = $this->callAPISuccess('participant', 'get', $params); |
| 204 | $this->assertEquals($result['values'][$this->_participantID]['event_id'], $this->_eventID); |
| 205 | $this->assertEquals($result['values'][$this->_participantID]['participant_register_date'], '2007-02-19 00:00:00'); |
| 206 | |
| 207 | } |
| 208 | |
| 209 | /** |
| 210 | * Check with params id. |
| 211 | */ |
| 212 | public function testGetParamsAsIdOnly() { |
| 213 | $params = array( |
| 214 | 'id' => $this->_participantID, |
| 215 | ); |
| 216 | $result = $this->callAPIAndDocument('participant', 'get', $params, __FUNCTION__, __FILE__); |
| 217 | $this->assertEquals($result['values'][$this->_participantID]['event_id'], $this->_eventID); |
| 218 | $this->assertEquals($result['values'][$this->_participantID]['participant_register_date'], '2007-02-19 00:00:00'); |
| 219 | $this->assertEquals($result['values'][$this->_participantID]['participant_source'], 'Wimbeldon'); |
| 220 | $this->assertEquals($result['id'], $result['values'][$this->_participantID]['id']); |
| 221 | } |
| 222 | |
| 223 | /** |
| 224 | * Check with params id. |
| 225 | */ |
| 226 | public function testGetNestedEventGet() { |
| 227 | //create a second event & add participant to it. |
| 228 | $event = $this->eventCreate(NULL); |
| 229 | $this->callAPISuccess('participant', 'create', array( |
| 230 | 'event_id' => $event['id'], |
| 231 | 'contact_id' => $this->_contactID, |
| 232 | )); |
| 233 | |
| 234 | $description = "Demonstrates use of nested get to fetch event data with participant records."; |
| 235 | $subfile = "NestedEventGet"; |
| 236 | $params = array( |
| 237 | 'id' => $this->_participantID, |
| 238 | 'api.event.get' => 1, |
| 239 | ); |
| 240 | $result = $this->callAPIAndDocument('participant', 'get', $params, __FUNCTION__, __FILE__, $description, $subfile); |
| 241 | $this->assertEquals($result['values'][$this->_participantID]['event_id'], $this->_eventID); |
| 242 | $this->assertEquals($result['values'][$this->_participantID]['participant_register_date'], '2007-02-19 00:00:00'); |
| 243 | $this->assertEquals($result['values'][$this->_participantID]['participant_source'], 'Wimbeldon'); |
| 244 | $this->assertEquals($this->_eventID, $result['values'][$this->_participantID]['api.event.get']['id']); |
| 245 | } |
| 246 | |
| 247 | /** |
| 248 | * Check Participant Get respects return properties. |
| 249 | */ |
| 250 | public function testGetWithReturnProperties() { |
| 251 | $params = array( |
| 252 | 'contact_id' => $this->_contactID, |
| 253 | 'return.status_id' => 1, |
| 254 | 'return.participant_status_id' => 1, |
| 255 | 'options' => array('limit' => 1), |
| 256 | ); |
| 257 | $result = $this->callAPISuccess('participant', 'get', $params); |
| 258 | $this->assertArrayHasKey('participant_status_id', $result['values'][$result['id']]); |
| 259 | } |
| 260 | |
| 261 | /** |
| 262 | * Check with contact_id. |
| 263 | */ |
| 264 | public function testGetContactIdOnly() { |
| 265 | $params = array( |
| 266 | 'contact_id' => $this->_contactID, |
| 267 | ); |
| 268 | $participant = $this->callAPISuccess('participant', 'get', $params); |
| 269 | |
| 270 | $this->assertEquals($this->_participantID, $participant['id'], |
| 271 | "In line " . __LINE__ |
| 272 | ); |
| 273 | $this->assertEquals($this->_eventID, $participant['values'][$participant['id']]['event_id'], |
| 274 | "In line " . __LINE__ |
| 275 | ); |
| 276 | $this->assertEquals('2007-02-19 00:00:00', $participant['values'][$participant['id']]['participant_register_date'], |
| 277 | "In line " . __LINE__ |
| 278 | ); |
| 279 | $this->assertEquals('Wimbeldon', $participant['values'][$participant['id']]['participant_source'], |
| 280 | "In line " . __LINE__ |
| 281 | ); |
| 282 | $this->assertEquals($participant['id'], $participant['values'][$participant['id']]['id'], |
| 283 | "In line " . __LINE__ |
| 284 | ); |
| 285 | } |
| 286 | |
| 287 | /** |
| 288 | * Check with event_id. |
| 289 | * fetch first record |
| 290 | */ |
| 291 | public function testGetMultiMatchReturnFirst() { |
| 292 | $params = array( |
| 293 | 'event_id' => $this->_eventID, |
| 294 | 'rowCount' => 1, |
| 295 | ); |
| 296 | |
| 297 | $participant = $this->callAPISuccess('participant', 'get', $params); |
| 298 | $this->assertNotNull($participant['id']); |
| 299 | } |
| 300 | |
| 301 | /** |
| 302 | * Check with event_id. |
| 303 | * in v3 this should return all participants |
| 304 | */ |
| 305 | public function testGetMultiMatchNoReturnFirst() { |
| 306 | $params = array( |
| 307 | 'event_id' => $this->_eventID, |
| 308 | ); |
| 309 | $participant = $this->callAPISuccess('participant', 'get', $params); |
| 310 | $this->assertNotNull($participant['count'], 3); |
| 311 | } |
| 312 | |
| 313 | ///////////////// civicrm_participant_get methods |
| 314 | |
| 315 | /** |
| 316 | * Test civicrm_participant_get with empty params. |
| 317 | * In this case all the participant records are returned. |
| 318 | */ |
| 319 | public function testSearchEmptyParams() { |
| 320 | $result = $this->callAPISuccess('participant', 'get', array()); |
| 321 | // expecting 3 participant records |
| 322 | $this->assertEquals($result['count'], 3); |
| 323 | } |
| 324 | |
| 325 | /** |
| 326 | * Check with participant_id. |
| 327 | */ |
| 328 | public function testSearchParticipantIdOnly() { |
| 329 | $params = array( |
| 330 | 'participant_id' => $this->_participantID, |
| 331 | ); |
| 332 | $participant = $this->callAPISuccess('participant', 'get', $params); |
| 333 | $this->assertEquals($participant['values'][$this->_participantID]['event_id'], $this->_eventID); |
| 334 | $this->assertEquals($participant['values'][$this->_participantID]['participant_register_date'], '2007-02-19 00:00:00'); |
| 335 | $this->assertEquals($participant['values'][$this->_participantID]['participant_source'], 'Wimbeldon'); |
| 336 | } |
| 337 | |
| 338 | /** |
| 339 | * Check with contact_id. |
| 340 | */ |
| 341 | public function testSearchContactIdOnly() { |
| 342 | // Should get 2 participant records for this contact. |
| 343 | $params = array( |
| 344 | 'contact_id' => $this->_contactID2, |
| 345 | ); |
| 346 | $participant = $this->callAPISuccess('participant', 'get', $params); |
| 347 | |
| 348 | $this->assertEquals($participant['count'], 2); |
| 349 | } |
| 350 | |
| 351 | /** |
| 352 | * Check with event_id. |
| 353 | */ |
| 354 | public function testSearchByEvent() { |
| 355 | // Should get >= 3 participant records for this event. Also testing that last_name and event_title are returned. |
| 356 | $params = array( |
| 357 | 'event_id' => $this->_eventID, |
| 358 | 'return.last_name' => 1, |
| 359 | 'return.event_title' => 1, |
| 360 | ); |
| 361 | $participant = $this->callAPISuccess('participant', 'get', $params); |
| 362 | if ($participant['count'] < 3) { |
| 363 | $this->fail("Event search returned less than expected miniumum of 3 records."); |
| 364 | } |
| 365 | |
| 366 | $this->assertEquals($participant['values'][$this->_participantID]['last_name'], 'Anderson'); |
| 367 | $this->assertEquals($participant['values'][$this->_participantID]['event_title'], 'Annual CiviCRM meet'); |
| 368 | } |
| 369 | |
| 370 | /** |
| 371 | * Check with event_id. |
| 372 | * fetch with limit |
| 373 | */ |
| 374 | public function testSearchByEventWithLimit() { |
| 375 | // Should 2 participant records since we're passing rowCount = 2. |
| 376 | $params = array( |
| 377 | 'event_id' => $this->_eventID, |
| 378 | 'rowCount' => 2, |
| 379 | ); |
| 380 | $participant = $this->callAPISuccess('participant', 'get', $params); |
| 381 | |
| 382 | $this->assertEquals($participant['count'], 2); |
| 383 | } |
| 384 | |
| 385 | /** |
| 386 | * Test search by lead booker (registered by ID) |
| 387 | */ |
| 388 | public function testSearchByRegisteredById() { |
| 389 | $params = array( |
| 390 | 'registered_by_id' => $this->_participantID, |
| 391 | ); |
| 392 | $participant = $this->callAPISuccess('participant', 'get', $params); |
| 393 | |
| 394 | $this->assertEquals($participant['count'], 1); |
| 395 | $this->assertEquals($participant['id'], $this->_participantID2); |
| 396 | } |
| 397 | |
| 398 | ///////////////// civicrm_participant_create methods |
| 399 | |
| 400 | /** |
| 401 | * Test civicrm_participant_create with empty params. |
| 402 | */ |
| 403 | public function testCreateEmptyParams() { |
| 404 | $params = array(); |
| 405 | $result = $this->callAPIFailure('participant', 'create', $params); |
| 406 | } |
| 407 | |
| 408 | /** |
| 409 | * Check with event_id. |
| 410 | */ |
| 411 | public function testCreateMissingContactID() { |
| 412 | $params = array( |
| 413 | 'event_id' => $this->_eventID, |
| 414 | ); |
| 415 | $participant = $this->callAPIFailure('participant', 'create', $params); |
| 416 | } |
| 417 | |
| 418 | /** |
| 419 | * Check with contact_id. |
| 420 | * without event_id |
| 421 | */ |
| 422 | public function testCreateMissingEventID() { |
| 423 | $params = array( |
| 424 | 'contact_id' => $this->_contactID, |
| 425 | ); |
| 426 | $participant = $this->callAPIFailure('participant', 'create', $params); |
| 427 | } |
| 428 | |
| 429 | /** |
| 430 | * Check with contact_id & event_id |
| 431 | */ |
| 432 | public function testCreateEventIdOnly() { |
| 433 | $params = array( |
| 434 | 'contact_id' => $this->_contactID, |
| 435 | 'event_id' => $this->_eventID, |
| 436 | ); |
| 437 | $participant = $this->callAPISuccess('participant', 'create', $params); |
| 438 | $this->getAndCheck($params, $participant['id'], 'participant'); |
| 439 | } |
| 440 | |
| 441 | /** |
| 442 | * Check with complete array. |
| 443 | */ |
| 444 | public function testCreateAllParams() { |
| 445 | $params = $this->_params; |
| 446 | |
| 447 | $participant = $this->callAPISuccess('participant', 'create', $params); |
| 448 | $this->_participantID = $participant['id']; |
| 449 | // assertDBState compares expected values in $match to actual values in the DB |
| 450 | $this->assertDBState('CRM_Event_DAO_Participant', $participant['id'], $params); |
| 451 | } |
| 452 | |
| 453 | /** |
| 454 | * Test to check if receive date is being changed per CRM-9763 |
| 455 | */ |
| 456 | public function testCreateUpdateReceiveDate() { |
| 457 | $participant = $this->callAPISuccess('participant', 'create', $this->_params); |
| 458 | $update = array( |
| 459 | 'id' => $participant['id'], |
| 460 | 'status_id' => 2, |
| 461 | ); |
| 462 | $this->callAPISuccess('participant', 'create', $update); |
| 463 | $this->getAndCheck(array_merge($this->_params, $update), $participant['id'], 'participant'); |
| 464 | } |
| 465 | |
| 466 | /** |
| 467 | * Test to check if participant fee level is being changed per CRM-9781 |
| 468 | */ |
| 469 | public function testCreateUpdateParticipantFeeLevel() { |
| 470 | $myParams = $this->_params + array('participant_fee_level' => CRM_Core_DAO::VALUE_SEPARATOR . "fee" . CRM_Core_DAO::VALUE_SEPARATOR); |
| 471 | $participant = $this->callAPISuccess('participant', 'create', $myParams); |
| 472 | $update = array( |
| 473 | 'id' => $participant['id'], |
| 474 | 'status_id' => 2, |
| 475 | ); |
| 476 | $update = $this->callAPISuccess('participant', 'create', $update); |
| 477 | |
| 478 | $this->assertEquals($participant['values'][$participant['id']]['fee_level'], |
| 479 | $update['values'][$participant['id']]['fee_level'] |
| 480 | ); |
| 481 | |
| 482 | $this->callAPISuccess('participant', 'delete', array('id' => $participant['id'])); |
| 483 | } |
| 484 | |
| 485 | /** |
| 486 | * Test the line items for participant fee with multiple price field values. |
| 487 | */ |
| 488 | public function testCreateParticipantLineItems() { |
| 489 | // Create a price set for this event. |
| 490 | |
| 491 | $priceset = $this->callAPISuccess('PriceSet', 'create', array( |
| 492 | 'name' => 'my_price_set', |
| 493 | 'title' => 'My Price Set', |
| 494 | 'is_active' => 1, |
| 495 | 'extends' => 1, |
| 496 | 'financial_type_id' => 4, |
| 497 | // 'entity' => array('civicrm_event' => array($this->_eventID)), |
| 498 | )); |
| 499 | |
| 500 | // Add the price set to the event with another API call. |
| 501 | // I tried to do this at once, but it did not work. |
| 502 | |
| 503 | $priceset = $this->callAPISuccess('PriceSet', 'create', array( |
| 504 | 'entity_table' => 'civicrm_event', |
| 505 | 'entity_id' => $this->_eventID, |
| 506 | 'id' => $priceset['id'], |
| 507 | )); |
| 508 | |
| 509 | $pricefield = $this->callAPISuccess('PriceField', 'create', array( |
| 510 | 'price_set_id' => $priceset['id'], |
| 511 | 'name' => 'mypricefield', |
| 512 | 'label' => 'My Price Field', |
| 513 | 'html_type' => 'Text', |
| 514 | 'is_enter_qty' => 1, |
| 515 | 'is_display_amounts' => 1, |
| 516 | 'is_active' => 1, |
| 517 | )); |
| 518 | |
| 519 | $pfv1 = $this->callAPISuccess('PriceFieldValue', 'create', array( |
| 520 | 'price_field_id' => $pricefield['id'], |
| 521 | 'name' => 'pricefieldvalue1', |
| 522 | 'label' => 'pricefieldvalue1', |
| 523 | 'amount' => 20, |
| 524 | 'is_active' => 1, |
| 525 | 'financial_type_id' => 4, |
| 526 | )); |
| 527 | |
| 528 | $pfv2 = $this->callAPISuccess('PriceFieldValue', 'create', array( |
| 529 | 'price_field_id' => $pricefield['id'], |
| 530 | 'name' => 'pricefieldvalue2', |
| 531 | 'label' => 'pricefieldvalue2', |
| 532 | 'amount' => 5, |
| 533 | 'is_active' => 1, |
| 534 | 'financial_type_id' => 4, |
| 535 | )); |
| 536 | |
| 537 | // pay 2 times price field value 1, and 2 times price field value 2. |
| 538 | $myParams = $this->_params + array('participant_fee_level' => CRM_Core_DAO::VALUE_SEPARATOR . "pricefieldvalue1 - 2" . CRM_Core_DAO::VALUE_SEPARATOR . "pricefieldvalue2 - 2" . CRM_Core_DAO::VALUE_SEPARATOR); |
| 539 | $participant = $this->callAPISuccess('participant', 'create', $myParams); |
| 540 | |
| 541 | // expect 2 line items. |
| 542 | $lineItems = $this->callAPISuccess('LineItem', 'get', array( |
| 543 | 'entity_id' => $participant['id'], |
| 544 | 'entity_table' => 'civicrm_participant', |
| 545 | )); |
| 546 | |
| 547 | $this->assertEquals(2, $lineItems['count']); |
| 548 | |
| 549 | // Check quantity, label and unit price of lines. |
| 550 | // TODO: These assertions depend on the order of the line items, which is |
| 551 | // technically incorrect. |
| 552 | |
| 553 | $lineItem = array_pop($lineItems['values']); |
| 554 | $this->assertEquals(2, $lineItem['qty']); |
| 555 | $this->assertEquals(5, $lineItem['unit_price']); |
| 556 | $this->assertEquals('pricefieldvalue2', $lineItem['label']); |
| 557 | |
| 558 | $lineItem = array_pop($lineItems['values']); |
| 559 | $this->assertEquals(2, $lineItem['qty']); |
| 560 | $this->assertEquals(20, $lineItem['unit_price']); |
| 561 | $this->assertEquals('pricefieldvalue1', $lineItem['label']); |
| 562 | |
| 563 | // Cleanup |
| 564 | $this->callAPISuccess('participant', 'delete', array('id' => $participant['id'])); |
| 565 | |
| 566 | // TODO: I think the price set should be removed, but I don't know how |
| 567 | // to decouple it properly from the event. For the moment, I'll just comment |
| 568 | // out the lines below. |
| 569 | |
| 570 | /* |
| 571 | $this->callAPISuccess('PriceFieldValue', 'delete', array('id' => $pfv1['id'])); |
| 572 | $this->callAPISuccess('PriceFieldValue', 'delete', array('id' => $pfv2['id'])); |
| 573 | $this->callAPISuccess('PriceField', 'delete', array('id' => $pricefield['id'])); |
| 574 | $this->callAPISuccess('PriceSet', 'delete', array('id' => $priceset['id'])); |
| 575 | */ |
| 576 | } |
| 577 | |
| 578 | /** |
| 579 | * Check with complete array. |
| 580 | */ |
| 581 | public function testUpdate() { |
| 582 | $participantId = $this->participantCreate(array( |
| 583 | 'contactID' => $this->_individualId, |
| 584 | 'eventID' => $this->_eventID, |
| 585 | )); |
| 586 | $params = array( |
| 587 | 'id' => $participantId, |
| 588 | 'contact_id' => $this->_individualId, |
| 589 | 'event_id' => $this->_eventID, |
| 590 | 'status_id' => 3, |
| 591 | 'role_id' => 3, |
| 592 | 'register_date' => '2006-01-21', |
| 593 | 'source' => 'US Open', |
| 594 | ); |
| 595 | $participant = $this->callAPISuccess('participant', 'create', $params); |
| 596 | $this->getAndCheck($params, $participant['id'], 'participant'); |
| 597 | $result = $this->participantDelete($params['id']); |
| 598 | } |
| 599 | |
| 600 | /** |
| 601 | * Test to check if participant fee level is being changed per CRM-9781 |
| 602 | * Try again without a custom separater to check that one isn't added |
| 603 | * (get & check won't accept an array) |
| 604 | */ |
| 605 | public function testUpdateCreateParticipantFeeLevelNoSeparator() { |
| 606 | |
| 607 | $myParams = $this->_params + array('participant_fee_level' => "fee"); |
| 608 | $participant = $this->callAPISuccess('participant', 'create', $myParams); |
| 609 | $this->assertAPISuccess($participant); |
| 610 | $update = array( |
| 611 | 'id' => $participant['id'], |
| 612 | 'status_id' => 2, |
| 613 | ); |
| 614 | $this->callAPISuccess('participant', 'create', $update); |
| 615 | $this->assertEquals($participant['values'][$participant['id']]['fee_level'], |
| 616 | $myParams['participant_fee_level'] |
| 617 | ); |
| 618 | $this->getAndCheck($update, $participant['id'], 'participant'); |
| 619 | } |
| 620 | ///////////////// civicrm_participant_update methods |
| 621 | |
| 622 | /** |
| 623 | * Test civicrm_participant_update with wrong params type. |
| 624 | */ |
| 625 | public function testUpdateWrongParamsType() { |
| 626 | $params = 'a string'; |
| 627 | $result = $this->callAPIFailure('participant', 'create', $params); |
| 628 | $this->assertEquals('Input variable `params` is not an array', $result['error_message']); |
| 629 | } |
| 630 | |
| 631 | /** |
| 632 | * Check with empty array. |
| 633 | */ |
| 634 | public function testUpdateEmptyParams() { |
| 635 | $params = array(); |
| 636 | $participant = $this->callAPIFailure('participant', 'create', $params); |
| 637 | $this->assertEquals($participant['error_message'], 'Mandatory key(s) missing from params array: event_id, contact_id'); |
| 638 | } |
| 639 | |
| 640 | /** |
| 641 | * Check without event_id. |
| 642 | */ |
| 643 | public function testUpdateWithoutEventId() { |
| 644 | $participantId = $this->participantCreate(array('contactID' => $this->_individualId, 'eventID' => $this->_eventID)); |
| 645 | $params = array( |
| 646 | 'contact_id' => $this->_individualId, |
| 647 | 'status_id' => 3, |
| 648 | 'role_id' => 3, |
| 649 | 'register_date' => '2006-01-21', |
| 650 | 'source' => 'US Open', |
| 651 | 'event_level' => 'Donation', |
| 652 | ); |
| 653 | $participant = $this->callAPIFailure('participant', 'create', $params); |
| 654 | $this->assertEquals($participant['error_message'], 'Mandatory key(s) missing from params array: event_id'); |
| 655 | // Cleanup created participant records. |
| 656 | $result = $this->participantDelete($participantId); |
| 657 | } |
| 658 | |
| 659 | /** |
| 660 | * Check with Invalid participantId. |
| 661 | */ |
| 662 | public function testUpdateWithWrongParticipantId() { |
| 663 | $params = array( |
| 664 | 'id' => 1234, |
| 665 | 'status_id' => 3, |
| 666 | 'role_id' => 3, |
| 667 | 'register_date' => '2006-01-21', |
| 668 | 'source' => 'US Open', |
| 669 | 'event_level' => 'Donation', |
| 670 | ); |
| 671 | $participant = $this->callAPIFailure('Participant', 'update', $params); |
| 672 | } |
| 673 | |
| 674 | /** |
| 675 | * Check with Invalid ContactId. |
| 676 | */ |
| 677 | public function testUpdateWithWrongContactId() { |
| 678 | $participantId = $this->participantCreate(array( |
| 679 | 'contactID' => $this->_individualId, |
| 680 | 'eventID' => $this->_eventID, |
| 681 | ), $this->_apiversion); |
| 682 | $params = array( |
| 683 | 'id' => $participantId, |
| 684 | 'contact_id' => 12345, |
| 685 | 'status_id' => 3, |
| 686 | 'role_id' => 3, |
| 687 | 'register_date' => '2006-01-21', |
| 688 | 'source' => 'US Open', |
| 689 | 'event_level' => 'Donation', |
| 690 | ); |
| 691 | $participant = $this->callAPIFailure('participant', 'create', $params); |
| 692 | $result = $this->participantDelete($participantId); |
| 693 | } |
| 694 | |
| 695 | ///////////////// civicrm_participant_delete methods |
| 696 | |
| 697 | /** |
| 698 | * Test civicrm_participant_delete with wrong params type. |
| 699 | */ |
| 700 | public function testDeleteWrongParamsType() { |
| 701 | $params = 'a string'; |
| 702 | $result = $this->callAPIFailure('participant', 'delete', $params); |
| 703 | } |
| 704 | |
| 705 | /** |
| 706 | * Test civicrm_participant_delete with empty params. |
| 707 | */ |
| 708 | public function testDeleteEmptyParams() { |
| 709 | $params = array(); |
| 710 | $result = $this->callAPIFailure('participant', 'delete', $params); |
| 711 | } |
| 712 | |
| 713 | /** |
| 714 | * Check with participant_id. |
| 715 | */ |
| 716 | public function testParticipantDelete() { |
| 717 | $params = array( |
| 718 | 'id' => $this->_participantID, |
| 719 | ); |
| 720 | $participant = $this->callAPISuccess('participant', 'delete', $params); |
| 721 | $this->assertAPISuccess($participant); |
| 722 | $this->assertDBState('CRM_Event_DAO_Participant', $this->_participantID, NULL, TRUE); |
| 723 | } |
| 724 | |
| 725 | /** |
| 726 | * Check without participant_id. |
| 727 | * and with event_id |
| 728 | * This should return an error because required param is missing.. |
| 729 | */ |
| 730 | public function testParticipantDeleteMissingID() { |
| 731 | $params = array( |
| 732 | 'event_id' => $this->_eventID, |
| 733 | ); |
| 734 | $participant = $this->callAPIFailure('participant', 'delete', $params); |
| 735 | $this->assertNotNull($participant['error_message']); |
| 736 | } |
| 737 | |
| 738 | /** |
| 739 | * Delete with a get - a 'criteria delete' |
| 740 | */ |
| 741 | public function testNestedDelete() { |
| 742 | $description = "Criteria delete by nesting a GET & a DELETE."; |
| 743 | $subfile = "NestedDelete"; |
| 744 | $participants = $this->callAPISuccess('Participant', 'Get', array()); |
| 745 | $this->assertEquals($participants['count'], 3); |
| 746 | $params = array('contact_id' => $this->_contactID2, 'api.participant.delete' => 1); |
| 747 | $participants = $this->callAPIAndDocument('Participant', 'Get', $params, __FUNCTION__, __FILE__, $description, $subfile); |
| 748 | $check = $this->callAPISuccess('participant', 'getcount', array()); |
| 749 | $this->assertEquals(1, $check, "only one participant should be left. line " . __LINE__); |
| 750 | } |
| 751 | |
| 752 | /** |
| 753 | * Test creation of a participant with an associated contribution. |
| 754 | */ |
| 755 | public function testCreateParticipantWithPayment() { |
| 756 | $description = "Single function to create contact with partipation & contribution. |
| 757 | Note that in the case of 'contribution' the 'create' is implied (api.contribution.create)"; |
| 758 | $subfile = "CreateParticipantPayment"; |
| 759 | $params = array( |
| 760 | 'contact_type' => 'Individual', |
| 761 | 'display_name' => 'dlobo', |
| 762 | 'api.participant' => array( |
| 763 | 'event_id' => $this->_eventID, |
| 764 | 'status_id' => 1, |
| 765 | 'role_id' => 1, |
| 766 | 'format.only_id' => 1, |
| 767 | ), |
| 768 | 'api.contribution.create' => array( |
| 769 | 'financial_type_id' => 1, |
| 770 | 'total_amount' => 100, |
| 771 | 'format.only_id' => 1, |
| 772 | ), |
| 773 | 'api.participant_payment.create' => array( |
| 774 | 'contribution_id' => '$value.api.contribution.create', |
| 775 | 'participant_id' => '$value.api.participant', |
| 776 | ), |
| 777 | ); |
| 778 | |
| 779 | $result = $this->callAPIAndDocument('contact', 'create', $params, __FUNCTION__, __FILE__, $description, $subfile); |
| 780 | $this->assertEquals(1, $result['values'][$result['id']]['api.participant_payment.create']['count']); |
| 781 | $this->callAPISuccess('contact', 'delete', array('id' => $result['id'])); |
| 782 | } |
| 783 | |
| 784 | } |