Merge pull request #12337 from lcdservices/dev-core-190
[civicrm-core.git] / tests / phpunit / api / v3 / ParticipantPaymentTest.php
CommitLineData
6a488035 1<?php
0728d72f 2/**
92915c55 3 * +--------------------------------------------------------------------+
2fe49090 4 * | CiviCRM version 5 |
92915c55 5 * +--------------------------------------------------------------------+
6b83d5bd 6 * | Copyright CiviCRM LLC (c) 2004-2019 |
92915c55
TO
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 * +--------------------------------------------------------------------+
6a488035
TO
26 */
27
6a488035
TO
28/**
29 * Test APIv3 civicrm_participant_* functions
30 *
6c6e6187
TO
31 * @package CiviCRM_APIv3
32 * @subpackage API_Event
acb109b7 33 * @group headless
6a488035 34 */
6a488035
TO
35class api_v3_ParticipantPaymentTest extends CiviUnitTestCase {
36
0728d72f 37 protected $_apiversion = 3;
6a488035
TO
38 protected $_contactID;
39 protected $_createdParticipants;
40 protected $_participantID;
41 protected $_eventID;
42 protected $_participantPaymentID;
69fccd8f 43 protected $_financialTypeId;
b7c9bc4c 44
78ab0ca4 45 /**
46 * Set up for tests.
47 */
00be9182 48 public function setUp() {
6a488035 49 parent::setUp();
924cd81a 50 $this->useTransaction(TRUE);
6a488035
TO
51 $event = $this->eventCreate(NULL);
52 $this->_eventID = $event['id'];
e4d5f1e2 53 $this->_contactID = $this->individualCreate();
6a488035 54 $this->_createdParticipants = array();
e4d5f1e2 55 $this->_individualId = $this->individualCreate();
69fccd8f 56 $this->_financialTypeId = 1;
6a488035 57
92915c55
TO
58 $this->_participantID = $this->participantCreate(array(
59 'contactID' => $this->_contactID,
408b79bf 60 'eventID' => $this->_eventID,
92915c55 61 ));
e4d5f1e2 62 $this->_contactID2 = $this->individualCreate();
92915c55
TO
63 $this->_participantID2 = $this->participantCreate(array(
64 'contactID' => $this->_contactID2,
408b79bf 65 'eventID' => $this->_eventID,
92915c55
TO
66 ));
67 $this->_participantID3 = $this->participantCreate(array(
68 'contactID' => $this->_contactID2,
408b79bf 69 'eventID' => $this->_eventID,
92915c55 70 ));
6a488035 71
e4d5f1e2 72 $this->_contactID3 = $this->individualCreate();
92915c55
TO
73 $this->_participantID4 = $this->participantCreate(array(
74 'contactID' => $this->_contactID3,
408b79bf 75 'eventID' => $this->_eventID,
92915c55 76 ));
6a488035
TO
77 }
78
6a488035 79 /**
fe482240 80 * Test civicrm_participant_payment_create with wrong params type.
6a488035 81 */
00be9182 82 public function testPaymentCreateWrongParamsType() {
6a488035 83 $params = 'a string';
69fccd8f 84 $this->callAPIFailure('participant_payment', 'create', $params);
6a488035
TO
85 }
86
87 /**
fe482240 88 * Test civicrm_participant_payment_create with empty params.
6a488035 89 */
00be9182 90 public function testPaymentCreateEmptyParams() {
6a488035 91 $params = array();
69fccd8f 92 $this->callAPIFailure('participant_payment', 'create', $params);
6a488035
TO
93 }
94
95 /**
fe482240 96 * Check without contribution_id.
6a488035 97 */
00be9182 98 public function testPaymentCreateMissingContributionId() {
6a488035
TO
99 //Without Payment EntityID
100 $params = array(
92915c55
TO
101 'participant_id' => $this->_participantID,
102 );
69fccd8f 103 $this->callAPIFailure('participant_payment', 'create', $params);
6a488035
TO
104 }
105
106 /**
eceb18cc 107 * Check with valid array.
6a488035 108 */
00be9182 109 public function testPaymentCreate() {
6a488035 110 //Create Contribution & get contribution ID
69fccd8f 111 $contributionID = $this->contributionCreate(array('contact_id' => $this->_contactID));
6a488035
TO
112
113 //Create Participant Payment record With Values
114 $params = array(
115 'participant_id' => $this->_participantID,
116 'contribution_id' => $contributionID,
6a488035
TO
117 );
118
0728d72f 119 $result = $this->callAPIAndDocument('participant_payment', 'create', $params, __FUNCTION__, __FILE__);
ba4a1892 120 $this->assertTrue(array_key_exists('id', $result));
6a488035
TO
121
122 //delete created contribution
123 $this->contributionDelete($contributionID);
124 }
125
8744e7c5
JP
126 /**
127 * Test getPaymentInfo() returns correct
128 * information of the participant payment
129 */
130 public function testPaymentInfoForEvent() {
131 //Create Contribution & get contribution ID
132 $contributionID = $this->contributionCreate(array('contact_id' => $this->_contactID));
133
134 //Create Participant Payment record With Values
135 $params = array(
136 'participant_id' => $this->_participantID4,
137 'contribution_id' => $contributionID,
138 );
139 $this->callAPISuccess('participant_payment', 'create', $params);
140
141 //Check if participant payment is correctly retrieved.
142 $paymentInfo = CRM_Contribute_BAO_Contribution::getPaymentInfo($this->_participantID4, 'event');
143 $this->assertEquals('Completed', $paymentInfo['contribution_status']);
144 $this->assertEquals('100.00', $paymentInfo['total']);
145 }
146
6a488035
TO
147
148 ///////////////// civicrm_participant_payment_create methods
149
150 /**
fe482240 151 * Test civicrm_participant payment create with wrong params type.
6a488035 152 */
00be9182 153 public function testPaymentUpdateWrongParamsType() {
6a488035 154 $params = 'a string';
d0e1eff2 155 $result = $this->callAPIFailure('participant_payment', 'create', $params);
ba4a1892 156 $this->assertEquals('Input variable `params` is not an array', $result['error_message']);
6a488035
TO
157 }
158
159 /**
eceb18cc 160 * Check with empty array.
6a488035 161 */
00be9182 162 public function testPaymentUpdateEmpty() {
fe482240 163 $this->callAPIFailure('participant_payment', 'create', array());
6a488035
TO
164 }
165
166 /**
fe482240 167 * Check with missing participant_id.
6a488035 168 */
00be9182 169 public function testPaymentUpdateMissingParticipantId() {
6a488035 170 $params = array(
92915c55
TO
171 'contribution_id' => '3',
172 );
fe482240 173 $this->callAPIFailure('participant_payment', 'create', $params);
6a488035
TO
174 }
175
176 /**
fe482240 177 * Check with missing contribution_id.
6a488035 178 */
00be9182 179 public function testPaymentUpdateMissingContributionId() {
6a488035 180 $params = array(
92915c55
TO
181 'participant_id' => $this->_participantID,
182 );
d0e1eff2 183 $participantPayment = $this->callAPIFailure('participant_payment', 'create', $params);
6a488035
TO
184 }
185
186 /**
eceb18cc 187 * Check financial records for offline Participants.
6a488035 188 */
00be9182 189 public function testPaymentOffline() {
6a488035
TO
190
191 // create contribution w/o fee
69fccd8f 192 $contributionID = $this->contributionCreate(array(
193 'contact_id' => $this->_contactID,
194 'financial_type_id' => $this->_financialTypeId,
195 'payment_instrument_id' => 4,
196 'fee_amount' => 0,
197 'net_amount' => 100,
198 ));
6a488035
TO
199
200 $this->_participantPaymentID = $this->participantPaymentCreate($this->_participantID, $contributionID);
201 $params = array(
202 'id' => $this->_participantPaymentID,
203 'participant_id' => $this->_participantID,
92915c55
TO
204 'contribution_id' => $contributionID,
205 );
6a488035
TO
206
207 // Update Payment
0728d72f 208 $participantPayment = $this->callAPISuccess('participant_payment', 'create', $params);
6a488035
TO
209 $this->assertEquals($participantPayment['id'], $this->_participantPaymentID);
210 $this->assertTrue(array_key_exists('id', $participantPayment));
211 // check Financial records
212 $this->_checkFinancialRecords($params, 'offline');
213 $params = array(
214 'id' => $this->_participantPaymentID,
6a488035 215 );
0728d72f 216 $deletePayment = $this->callAPISuccess('participant_payment', 'delete', $params);
6a488035
TO
217 }
218
219 /**
eceb18cc 220 * Check financial records for online Participant.
6a488035 221 */
00be9182 222 public function testPaymentOnline() {
6a488035 223
8950ecdc 224 $pageParams['processor_id'] = $this->processorCreate();
6a488035
TO
225 $contributionPage = $this->contributionPageCreate($pageParams);
226 $contributionParams = array(
92915c55
TO
227 'contact_id' => $this->_contactID,
228 'contribution_page_id' => $contributionPage['id'],
8950ecdc 229 'payment_processor' => $pageParams['processor_id'],
78ab0ca4 230 'financial_type_id' => 1,
6a488035 231 );
78ab0ca4 232 $contributionID = $this->contributionCreate($contributionParams);
6a488035
TO
233
234 $this->_participantPaymentID = $this->participantPaymentCreate($this->_participantID, $contributionID);
235 $params = array(
236 'id' => $this->_participantPaymentID,
237 'participant_id' => $this->_participantID,
92915c55
TO
238 'contribution_id' => $contributionID,
239 );
6a488035
TO
240
241 // Update Payment
0728d72f 242 $participantPayment = $this->callAPISuccess('participant_payment', 'create', $params);
6a488035
TO
243 $this->assertEquals($participantPayment['id'], $this->_participantPaymentID);
244 $this->assertTrue(array_key_exists('id', $participantPayment));
245 // check Financial records
246 $this->_checkFinancialRecords($params, 'online');
247 $params = array(
248 'id' => $this->_participantPaymentID,
6a488035 249 );
78ab0ca4 250 $this->callAPISuccess('participant_payment', 'delete', $params);
6a488035
TO
251 }
252
253 /**
eceb18cc 254 * Check financial records for online Participant pay later scenario.
6a488035 255 */
00be9182 256 public function testPaymentPayLaterOnline() {
8950ecdc 257 $pageParams['processor_id'] = $this->processorCreate();
6a488035
TO
258 $pageParams['is_pay_later'] = 1;
259 $contributionPage = $this->contributionPageCreate($pageParams);
260 $contributionParams = array(
261 'contact_id' => $this->_contactID,
262 'contribution_page_id' => $contributionPage['id'],
263 'contribution_status_id' => 2,
264 'is_pay_later' => 1,
78ab0ca4 265 'financial_type_id' => 1,
6a488035 266 );
78ab0ca4 267 $contributionID = $this->contributionCreate($contributionParams);
6a488035
TO
268
269 $this->_participantPaymentID = $this->participantPaymentCreate($this->_participantID, $contributionID);
270 $params = array(
271 'id' => $this->_participantPaymentID,
272 'participant_id' => $this->_participantID,
92915c55
TO
273 'contribution_id' => $contributionID,
274 );
6a488035
TO
275
276 // Update Payment
0728d72f 277 $participantPayment = $this->callAPISuccess('participant_payment', 'create', $params);
6a488035
TO
278 // check Financial Records
279 $this->_checkFinancialRecords($params, 'payLater');
280 $this->assertEquals($participantPayment['id'], $this->_participantPaymentID);
281 $this->assertTrue(array_key_exists('id', $participantPayment));
282 $params = array(
283 'id' => $this->_participantPaymentID,
6a488035 284 );
78ab0ca4 285 $this->callAPISuccess('participant_payment', 'delete', $params);
6a488035
TO
286 }
287
6a488035
TO
288
289 /**
fe482240 290 * Test civicrm_participant_payment_delete with wrong params type.
6a488035 291 */
00be9182 292 public function testPaymentDeleteWrongParamsType() {
6a488035 293 $params = 'a string';
fe482240 294 $this->callAPIFailure('participant_payment', 'delete', $params);
6a488035
TO
295 }
296
297 /**
eceb18cc 298 * Check with empty array.
6a488035 299 */
00be9182 300 public function testPaymentDeleteWithEmptyParams() {
0728d72f 301 $params = array();
d0e1eff2 302 $deletePayment = $this->callAPIFailure('participant_payment', 'delete', $params);
6a488035
TO
303 $this->assertEquals('Mandatory key(s) missing from params array: id', $deletePayment['error_message']);
304 }
305
306 /**
eceb18cc 307 * Check with wrong id.
6a488035 308 */
00be9182 309 public function testPaymentDeleteWithWrongID() {
6a488035 310 $params = array(
92915c55
TO
311 'id' => 0,
312 );
d0e1eff2 313 $deletePayment = $this->callAPIFailure('participant_payment', 'delete', $params);
274775cc 314 $this->assertEquals($deletePayment['error_message'], 'Error while deleting participantPayment');
6a488035
TO
315 }
316
317 /**
eceb18cc 318 * Check with valid array.
6a488035 319 */
00be9182 320 public function testPaymentDelete() {
69fccd8f 321 $contributionID = $this->contributionCreate(array(
322 'contact_id' => $this->_contactID,
323 ));
6a488035
TO
324
325 $this->_participantPaymentID = $this->participantPaymentCreate($this->_participantID, $contributionID);
326
327 $params = array(
328 'id' => $this->_participantPaymentID,
6a488035 329 );
69fccd8f 330 $this->callAPIAndDocument('participant_payment', 'delete', $params, __FUNCTION__, __FILE__);
6a488035
TO
331 }
332
6a488035
TO
333 /**
334 * Test civicrm_participantPayment_get - success expected.
335 */
336 public function testGet() {
69fccd8f 337 $contributionID = $this->contributionCreate(array('contact_id' => $this->_contactID3));
338 $this->participantPaymentCreate($this->_participantID4, $contributionID);
6a488035
TO
339
340 //Create Participant Payment record With Values
341 $params = array(
342 'participant_id' => $this->_participantID4,
343 'contribution_id' => $contributionID,
6a488035
TO
344 );
345
0728d72f 346 $result = $this->callAPIAndDocument('participant_payment', 'get', $params, __FUNCTION__, __FILE__);
6a488035
TO
347 $this->assertEquals($result['values'][$result['id']]['participant_id'], $this->_participantID4, 'Check Participant Id');
348 $this->assertEquals($result['values'][$result['id']]['contribution_id'], $contributionID, 'Check Contribution Id');
349 }
350
4cbe18b8 351 /**
c490a46a 352 * @param array $params
4cbe18b8
EM
353 * @param $context
354 */
00be9182 355 public function _checkFinancialRecords($params, $context) {
6a488035
TO
356 $entityParams = array(
357 'entity_id' => $params['id'],
358 'entity_table' => 'civicrm_contribution',
359 );
360 $trxn = current(CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($entityParams));
361 $trxnParams = array(
362 'id' => $trxn['financial_trxn_id'],
363 );
364
365 switch ($context) {
366 case 'online':
367 $compareParams = array(
368 'to_financial_account_id' => 12,
369 'total_amount' => 100,
370 'status_id' => 1,
371 );
372 break;
373
374 case 'offline':
375 $compareParams = array(
376 'to_financial_account_id' => 6,
377 'total_amount' => 100,
378 'status_id' => 1,
379 );
380 break;
6c6e6187 381
6a488035
TO
382 case 'payLater':
383 $compareParams = array(
384 'to_financial_account_id' => 7,
385 'total_amount' => 100,
386 'status_id' => 2,
387 );
388 break;
389 }
390
391 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialTrxn', $trxnParams, $compareParams);
392 $entityParams = array(
393 'financial_trxn_id' => $trxn['financial_trxn_id'],
394 'entity_table' => 'civicrm_financial_item',
395 );
396 $entityTrxn = current(CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($entityParams));
397 $fitemParams = array(
398 'id' => $entityTrxn['entity_id'],
399 );
400 if ($context == 'offline' || $context == 'online') {
401 $compareParams = array(
402 'amount' => 100,
403 'status_id' => 1,
404 'financial_account_id' => 1,
405 );
406 }
407 elseif ($context == 'payLater') {
408 $compareParams = array(
409 'amount' => 100,
410 'status_id' => 3,
411 'financial_account_id' => 1,
412 );
413 }
414 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $fitemParams, $compareParams);
415 }
96025800 416
b677b23b
JP
417 /**
418 * test getParticipantIds() function
419 */
420 public function testGetParticipantIds() {
421 $contributionID = $this->contributionCreate(array('contact_id' => $this->_contactID));
422 $expectedParticipants = array($this->_participantID, $this->_participantID2);
423
424 //Create Participant Payment record With Values
425 foreach ($expectedParticipants as $pid) {
426 $params = array(
427 'participant_id' => $pid,
428 'contribution_id' => $contributionID,
429 );
430 $this->callAPISuccess('participant_payment', 'create', $params);
431 }
432 //Check if all participants are listed.
433 $participants = CRM_Event_BAO_Participant::getParticipantIds($contributionID);
434 $this->checkArrayEquals($expectedParticipants, $participants);
435 //delete created contribution
436 $this->contributionDelete($contributionID);
437 }
438
6a488035 439}