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