| 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 | /** |
| 32 | * Test APIv3 civicrm_participant_* functions |
| 33 | * |
| 34 | * @package CiviCRM_APIv3 |
| 35 | * @subpackage API_Event |
| 36 | */ |
| 37 | |
| 38 | class api_v3_ParticipantPaymentTest extends CiviUnitTestCase { |
| 39 | |
| 40 | protected $_apiversion = 3; |
| 41 | protected $_contactID; |
| 42 | protected $_createdParticipants; |
| 43 | protected $_participantID; |
| 44 | protected $_eventID; |
| 45 | protected $_participantPaymentID; |
| 46 | protected $_contributionTypeId; |
| 47 | |
| 48 | |
| 49 | /** |
| 50 | * @return array |
| 51 | */ |
| 52 | function get_info() { |
| 53 | return array( |
| 54 | 'name' => 'Participant Create', |
| 55 | 'description' => 'Test all Participant Create API methods.', |
| 56 | 'group' => 'CiviCRM API Tests', |
| 57 | ); |
| 58 | } |
| 59 | |
| 60 | function setUp() { |
| 61 | parent::setUp(); |
| 62 | $tablesToTruncate = array( |
| 63 | 'civicrm_contribution', |
| 64 | 'civicrm_contact', |
| 65 | ); |
| 66 | $this->quickCleanup($tablesToTruncate); |
| 67 | $event = $this->eventCreate(NULL); |
| 68 | $this->_eventID = $event['id']; |
| 69 | $this->_contactID = $this->individualCreate(); |
| 70 | $this->_createdParticipants = array(); |
| 71 | $this->_individualId = $this->individualCreate(); |
| 72 | |
| 73 | $this->_participantID = $this->participantCreate(array('contactID' => $this->_contactID, 'eventID' => $this->_eventID)); |
| 74 | $this->_contactID2 = $this->individualCreate(); |
| 75 | $this->_participantID2 = $this->participantCreate(array('contactID' => $this->_contactID2, 'eventID' => $this->_eventID)); |
| 76 | $this->_participantID3 = $this->participantCreate(array('contactID' => $this->_contactID2, 'eventID' => $this->_eventID)); |
| 77 | |
| 78 | $this->_contactID3 = $this->individualCreate(); |
| 79 | $this->_participantID4 = $this->participantCreate(array('contactID' => $this->_contactID3, 'eventID' => $this->_eventID)); |
| 80 | } |
| 81 | |
| 82 | function tearDown() { |
| 83 | $this->eventDelete($this->_eventID); |
| 84 | $this->quickCleanup( |
| 85 | array( |
| 86 | 'civicrm_contact', |
| 87 | 'civicrm_contribution', |
| 88 | 'civicrm_participant', |
| 89 | 'civicrm_participant_payment', |
| 90 | 'civicrm_line_item', |
| 91 | 'civicrm_financial_item', |
| 92 | 'civicrm_financial_trxn', |
| 93 | 'civicrm_entity_financial_trxn', |
| 94 | ), |
| 95 | TRUE |
| 96 | ); |
| 97 | } |
| 98 | |
| 99 | ///////////////// civicrm_participant_payment_create methods |
| 100 | |
| 101 | /** |
| 102 | * Test civicrm_participant_payment_create with wrong params type |
| 103 | */ |
| 104 | function testPaymentCreateWrongParamsType() { |
| 105 | $params = 'a string'; |
| 106 | $result = $this->callAPIFailure('participant_payment', 'create', $params); |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Test civicrm_participant_payment_create with empty params |
| 111 | */ |
| 112 | function testPaymentCreateEmptyParams() { |
| 113 | $params = array(); |
| 114 | $result = $this->callAPIFailure('participant_payment', 'create', $params); |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Check without contribution_id |
| 119 | */ |
| 120 | function testPaymentCreateMissingContributionId() { |
| 121 | //Without Payment EntityID |
| 122 | $params = array( |
| 123 | 'participant_id' => $this->_participantID, ); |
| 124 | |
| 125 | $participantPayment = $this->callAPIFailure('participant_payment', 'create', $params); |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * Check with valid array |
| 130 | */ |
| 131 | function testPaymentCreate() { |
| 132 | //Create Contribution & get contribution ID |
| 133 | $contributionID = $this->contributionCreate($this->_contactID); |
| 134 | |
| 135 | //Create Participant Payment record With Values |
| 136 | $params = array( |
| 137 | 'participant_id' => $this->_participantID, |
| 138 | 'contribution_id' => $contributionID, |
| 139 | ); |
| 140 | |
| 141 | $result = $this->callAPIAndDocument('participant_payment', 'create', $params, __FUNCTION__, __FILE__); |
| 142 | $this->assertTrue(array_key_exists('id', $result), 'in line ' . __LINE__); |
| 143 | |
| 144 | //delete created contribution |
| 145 | $this->contributionDelete($contributionID); |
| 146 | } |
| 147 | |
| 148 | |
| 149 | ///////////////// civicrm_participant_payment_create methods |
| 150 | |
| 151 | /** |
| 152 | * Test civicrm_participant_payment_create with wrong params type |
| 153 | */ |
| 154 | function testPaymentUpdateWrongParamsType() { |
| 155 | $params = 'a string'; |
| 156 | $result = $this->callAPIFailure('participant_payment', 'create', $params); |
| 157 | $this->assertEquals('Input variable `params` is not an array', $result['error_message'], 'In line ' . __LINE__); |
| 158 | } |
| 159 | |
| 160 | /** |
| 161 | * Check with empty array |
| 162 | */ |
| 163 | function testPaymentUpdateEmpty() { |
| 164 | $params = array(); |
| 165 | $participantPayment = $this->callAPIFailure('participant_payment', 'create', $params); |
| 166 | } |
| 167 | |
| 168 | /** |
| 169 | * Check with missing participant_id |
| 170 | */ |
| 171 | function testPaymentUpdateMissingParticipantId() { |
| 172 | //WithoutParticipantId |
| 173 | $params = array( |
| 174 | 'contribution_id' => '3', ); |
| 175 | |
| 176 | $participantPayment = $this->callAPIFailure('participant_payment', 'create', $params); |
| 177 | } |
| 178 | |
| 179 | /** |
| 180 | * Check with missing contribution_id |
| 181 | */ |
| 182 | function testPaymentUpdateMissingContributionId() { |
| 183 | $params = array( |
| 184 | 'participant_id' => $this->_participantID, ); |
| 185 | $participantPayment = $this->callAPIFailure('participant_payment', 'create', $params); |
| 186 | } |
| 187 | |
| 188 | /** |
| 189 | * Check financial records for offline Participants |
| 190 | */ |
| 191 | function testPaymentOffline() { |
| 192 | |
| 193 | // create contribution w/o fee |
| 194 | $contributionID = $this->contributionCreate($this->_contactID, $this->_contributionTypeId, NULL, NULL, 4, FALSE); |
| 195 | |
| 196 | $this->_participantPaymentID = $this->participantPaymentCreate($this->_participantID, $contributionID); |
| 197 | $params = array( |
| 198 | 'id' => $this->_participantPaymentID, |
| 199 | 'participant_id' => $this->_participantID, |
| 200 | 'contribution_id' => $contributionID, ); |
| 201 | |
| 202 | // Update Payment |
| 203 | $participantPayment = $this->callAPISuccess('participant_payment', 'create', $params); |
| 204 | $this->assertEquals($participantPayment['id'], $this->_participantPaymentID); |
| 205 | $this->assertTrue(array_key_exists('id', $participantPayment)); |
| 206 | // check Financial records |
| 207 | $this->_checkFinancialRecords($params, 'offline'); |
| 208 | $params = array( |
| 209 | 'id' => $this->_participantPaymentID, |
| 210 | ); |
| 211 | $deletePayment = $this->callAPISuccess('participant_payment', 'delete', $params); |
| 212 | } |
| 213 | |
| 214 | /** |
| 215 | * Check financial records for online Participant |
| 216 | */ |
| 217 | function testPaymentOnline() { |
| 218 | |
| 219 | $paymentProcessor = $this->processorCreate(); |
| 220 | $pageParams['processor_id'] = $paymentProcessor->id; |
| 221 | $contributionPage = $this->contributionPageCreate($pageParams); |
| 222 | $contributionParams = array( |
| 223 | 'contact_id' => $this->_contactID, |
| 224 | 'contribution_page_id' => $contributionPage['id'], |
| 225 | 'payment_processor' => $paymentProcessor->id, |
| 226 | ); |
| 227 | $contributionID = $this->onlineContributionCreate($contributionParams, 1); |
| 228 | |
| 229 | $this->_participantPaymentID = $this->participantPaymentCreate($this->_participantID, $contributionID); |
| 230 | $params = array( |
| 231 | 'id' => $this->_participantPaymentID, |
| 232 | 'participant_id' => $this->_participantID, |
| 233 | 'contribution_id' => $contributionID, ); |
| 234 | |
| 235 | // Update Payment |
| 236 | $participantPayment = $this->callAPISuccess('participant_payment', 'create', $params); |
| 237 | $this->assertEquals($participantPayment['id'], $this->_participantPaymentID); |
| 238 | $this->assertTrue(array_key_exists('id', $participantPayment)); |
| 239 | // check Financial records |
| 240 | $this->_checkFinancialRecords($params, 'online'); |
| 241 | $params = array( |
| 242 | 'id' => $this->_participantPaymentID, |
| 243 | ); |
| 244 | $deletePayment = $this->callAPISuccess('participant_payment', 'delete', $params); |
| 245 | } |
| 246 | |
| 247 | /** |
| 248 | * Check financial records for online Participant pay later scenario |
| 249 | */ |
| 250 | function testPaymentPayLaterOnline() { |
| 251 | |
| 252 | $paymentProcessor = $this->processorCreate(); |
| 253 | $pageParams['processor_id'] = $paymentProcessor->id; |
| 254 | $pageParams['is_pay_later'] = 1; |
| 255 | $contributionPage = $this->contributionPageCreate($pageParams); |
| 256 | $contributionParams = array( |
| 257 | 'contact_id' => $this->_contactID, |
| 258 | 'contribution_page_id' => $contributionPage['id'], |
| 259 | 'contribution_status_id' => 2, |
| 260 | 'is_pay_later' => 1, |
| 261 | ); |
| 262 | $contributionID = $this->onlineContributionCreate($contributionParams, 1); |
| 263 | |
| 264 | $this->_participantPaymentID = $this->participantPaymentCreate($this->_participantID, $contributionID); |
| 265 | $params = array( |
| 266 | 'id' => $this->_participantPaymentID, |
| 267 | 'participant_id' => $this->_participantID, |
| 268 | 'contribution_id' => $contributionID, ); |
| 269 | |
| 270 | // Update Payment |
| 271 | $participantPayment = $this->callAPISuccess('participant_payment', 'create', $params); |
| 272 | // check Financial Records |
| 273 | $this->_checkFinancialRecords($params, 'payLater'); |
| 274 | $this->assertEquals($participantPayment['id'], $this->_participantPaymentID); |
| 275 | $this->assertTrue(array_key_exists('id', $participantPayment)); |
| 276 | $params = array( |
| 277 | 'id' => $this->_participantPaymentID, |
| 278 | ); |
| 279 | $deletePayment = $this->callAPISuccess('participant_payment', 'delete', $params); |
| 280 | } |
| 281 | |
| 282 | ///////////////// civicrm_participant_payment_delete methods |
| 283 | |
| 284 | /** |
| 285 | * Test civicrm_participant_payment_delete with wrong params type |
| 286 | */ |
| 287 | function testPaymentDeleteWrongParamsType() { |
| 288 | $params = 'a string'; |
| 289 | $result = $this->callAPIFailure('participant_payment', 'delete', $params); |
| 290 | } |
| 291 | |
| 292 | /** |
| 293 | * Check with empty array |
| 294 | */ |
| 295 | function testPaymentDeleteWithEmptyParams() { |
| 296 | $params = array(); |
| 297 | $deletePayment = $this->callAPIFailure('participant_payment', 'delete', $params); |
| 298 | $this->assertEquals('Mandatory key(s) missing from params array: id', $deletePayment['error_message']); |
| 299 | } |
| 300 | |
| 301 | /** |
| 302 | * Check with wrong id |
| 303 | */ |
| 304 | function testPaymentDeleteWithWrongID() { |
| 305 | $params = array( |
| 306 | 'id' => 0, ); |
| 307 | $deletePayment = $this->callAPIFailure('participant_payment', 'delete', $params); |
| 308 | $this->assertEquals($deletePayment['error_message'], 'Error while deleting participantPayment'); |
| 309 | } |
| 310 | |
| 311 | /** |
| 312 | * Check with valid array |
| 313 | */ |
| 314 | function testPaymentDelete() { |
| 315 | |
| 316 | // create contribution |
| 317 | $contributionID = $this->contributionCreate($this->_contactID, $this->_contributionTypeId); |
| 318 | |
| 319 | $this->_participantPaymentID = $this->participantPaymentCreate($this->_participantID, $contributionID); |
| 320 | |
| 321 | $params = array( |
| 322 | 'id' => $this->_participantPaymentID, |
| 323 | ); |
| 324 | |
| 325 | $result = $this->callAPIAndDocument('participant_payment', 'delete', $params, __FUNCTION__, __FILE__); |
| 326 | } |
| 327 | |
| 328 | ///////////////// civicrm_participantPayment_get methods |
| 329 | /** |
| 330 | * Test civicrm_participantPayment_get - success expected. |
| 331 | */ |
| 332 | public function testGet() { |
| 333 | //Create Contribution & get contribution ID |
| 334 | $contributionID = $this->contributionCreate($this->_contactID3, $this->_contributionTypeId); |
| 335 | $participantPaymentID = $this->participantPaymentCreate($this->_participantID4, $contributionID); |
| 336 | |
| 337 | //Create Participant Payment record With Values |
| 338 | $params = array( |
| 339 | 'participant_id' => $this->_participantID4, |
| 340 | 'contribution_id' => $contributionID, |
| 341 | ); |
| 342 | |
| 343 | $result = $this->callAPIAndDocument('participant_payment', 'get', $params, __FUNCTION__, __FILE__); |
| 344 | $this->assertEquals($result['values'][$result['id']]['participant_id'], $this->_participantID4, 'Check Participant Id'); |
| 345 | $this->assertEquals($result['values'][$result['id']]['contribution_id'], $contributionID, 'Check Contribution Id'); |
| 346 | } |
| 347 | |
| 348 | /** |
| 349 | * @param array $params |
| 350 | * @param $context |
| 351 | */ |
| 352 | function _checkFinancialRecords($params, $context) { |
| 353 | $entityParams = array( |
| 354 | 'entity_id' => $params['id'], |
| 355 | 'entity_table' => 'civicrm_contribution', |
| 356 | ); |
| 357 | $trxn = current(CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($entityParams)); |
| 358 | $trxnParams = array( |
| 359 | 'id' => $trxn['financial_trxn_id'], |
| 360 | ); |
| 361 | |
| 362 | switch ($context) { |
| 363 | case 'online': |
| 364 | $compareParams = array( |
| 365 | 'to_financial_account_id' => 12, |
| 366 | 'total_amount' => 100, |
| 367 | 'status_id' => 1, |
| 368 | ); |
| 369 | break; |
| 370 | |
| 371 | case 'offline': |
| 372 | $compareParams = array( |
| 373 | 'to_financial_account_id' => 6, |
| 374 | 'total_amount' => 100, |
| 375 | 'status_id' => 1, |
| 376 | ); |
| 377 | break; |
| 378 | case 'payLater': |
| 379 | $compareParams = array( |
| 380 | 'to_financial_account_id' => 7, |
| 381 | 'total_amount' => 100, |
| 382 | 'status_id' => 2, |
| 383 | ); |
| 384 | break; |
| 385 | } |
| 386 | |
| 387 | $this->assertDBCompareValues('CRM_Financial_DAO_FinancialTrxn', $trxnParams, $compareParams); |
| 388 | $entityParams = array( |
| 389 | 'financial_trxn_id' => $trxn['financial_trxn_id'], |
| 390 | 'entity_table' => 'civicrm_financial_item', |
| 391 | ); |
| 392 | $entityTrxn = current(CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($entityParams)); |
| 393 | $fitemParams = array( |
| 394 | 'id' => $entityTrxn['entity_id'], |
| 395 | ); |
| 396 | if ($context == 'offline' || $context == 'online') { |
| 397 | $compareParams = array( |
| 398 | 'amount' => 100, |
| 399 | 'status_id' => 1, |
| 400 | 'financial_account_id' => 1, |
| 401 | ); |
| 402 | } |
| 403 | elseif ($context == 'payLater') { |
| 404 | $compareParams = array( |
| 405 | 'amount' => 100, |
| 406 | 'status_id' => 3, |
| 407 | 'financial_account_id' => 1, |
| 408 | ); |
| 409 | } |
| 410 | $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $fitemParams, $compareParams); |
| 411 | } |
| 412 | } |
| 413 | |