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