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