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