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