Merge pull request #11728 from JMAConsulting/CRM-21809
[civicrm-core.git] / tests / phpunit / api / v3 / ParticipantPaymentTest.php
1 <?php
2 /**
3 * +--------------------------------------------------------------------+
4 * | CiviCRM version 4.7 |
5 * +--------------------------------------------------------------------+
6 * | Copyright CiviCRM LLC (c) 2004-2018 |
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 = array();
55 $this->_individualId = $this->individualCreate();
56 $this->_financialTypeId = 1;
57
58 $this->_participantID = $this->participantCreate(array(
59 'contactID' => $this->_contactID,
60 'eventID' => $this->_eventID,
61 ));
62 $this->_contactID2 = $this->individualCreate();
63 $this->_participantID2 = $this->participantCreate(array(
64 'contactID' => $this->_contactID2,
65 'eventID' => $this->_eventID,
66 ));
67 $this->_participantID3 = $this->participantCreate(array(
68 'contactID' => $this->_contactID2,
69 'eventID' => $this->_eventID,
70 ));
71
72 $this->_contactID3 = $this->individualCreate();
73 $this->_participantID4 = $this->participantCreate(array(
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 = array();
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 = array(
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(array('contact_id' => $this->_contactID));
112
113 //Create Participant Payment record With Values
114 $params = array(
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(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
147
148 ///////////////// civicrm_participant_payment_create methods
149
150 /**
151 * Test civicrm_participant payment create with wrong params type.
152 */
153 public function testPaymentUpdateWrongParamsType() {
154 $params = 'a string';
155 $result = $this->callAPIFailure('participant_payment', 'create', $params);
156 $this->assertEquals('Input variable `params` is not an array', $result['error_message']);
157 }
158
159 /**
160 * Check with empty array.
161 */
162 public function testPaymentUpdateEmpty() {
163 $this->callAPIFailure('participant_payment', 'create', array());
164 }
165
166 /**
167 * Check with missing participant_id.
168 */
169 public function testPaymentUpdateMissingParticipantId() {
170 $params = array(
171 'contribution_id' => '3',
172 );
173 $this->callAPIFailure('participant_payment', 'create', $params);
174 }
175
176 /**
177 * Check with missing contribution_id.
178 */
179 public function testPaymentUpdateMissingContributionId() {
180 $params = array(
181 'participant_id' => $this->_participantID,
182 );
183 $participantPayment = $this->callAPIFailure('participant_payment', 'create', $params);
184 }
185
186 /**
187 * Check financial records for offline Participants.
188 */
189 public function testPaymentOffline() {
190
191 // create contribution w/o fee
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 ));
199
200 $this->_participantPaymentID = $this->participantPaymentCreate($this->_participantID, $contributionID);
201 $params = array(
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, 'offline');
213 $params = array(
214 'id' => $this->_participantPaymentID,
215 );
216 $deletePayment = $this->callAPISuccess('participant_payment', 'delete', $params);
217 }
218
219 /**
220 * Check financial records for online Participant.
221 */
222 public function testPaymentOnline() {
223
224 $pageParams['processor_id'] = $this->processorCreate();
225 $contributionPage = $this->contributionPageCreate($pageParams);
226 $contributionParams = array(
227 'contact_id' => $this->_contactID,
228 'contribution_page_id' => $contributionPage['id'],
229 'payment_processor' => $pageParams['processor_id'],
230 'financial_type_id' => 1,
231 );
232 $contributionID = $this->contributionCreate($contributionParams);
233
234 $this->_participantPaymentID = $this->participantPaymentCreate($this->_participantID, $contributionID);
235 $params = array(
236 'id' => $this->_participantPaymentID,
237 'participant_id' => $this->_participantID,
238 'contribution_id' => $contributionID,
239 );
240
241 // Update Payment
242 $participantPayment = $this->callAPISuccess('participant_payment', 'create', $params);
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,
249 );
250 $this->callAPISuccess('participant_payment', 'delete', $params);
251 }
252
253 /**
254 * Check financial records for online Participant pay later scenario.
255 */
256 public function testPaymentPayLaterOnline() {
257 $pageParams['processor_id'] = $this->processorCreate();
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,
265 'financial_type_id' => 1,
266 );
267 $contributionID = $this->contributionCreate($contributionParams);
268
269 $this->_participantPaymentID = $this->participantPaymentCreate($this->_participantID, $contributionID);
270 $params = array(
271 'id' => $this->_participantPaymentID,
272 'participant_id' => $this->_participantID,
273 'contribution_id' => $contributionID,
274 );
275
276 // Update Payment
277 $participantPayment = $this->callAPISuccess('participant_payment', 'create', $params);
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,
284 );
285 $this->callAPISuccess('participant_payment', 'delete', $params);
286 }
287
288
289 /**
290 * Test civicrm_participant_payment_delete with wrong params type.
291 */
292 public function testPaymentDeleteWrongParamsType() {
293 $params = 'a string';
294 $this->callAPIFailure('participant_payment', 'delete', $params);
295 }
296
297 /**
298 * Check with empty array.
299 */
300 public function testPaymentDeleteWithEmptyParams() {
301 $params = array();
302 $deletePayment = $this->callAPIFailure('participant_payment', 'delete', $params);
303 $this->assertEquals('Mandatory key(s) missing from params array: id', $deletePayment['error_message']);
304 }
305
306 /**
307 * Check with wrong id.
308 */
309 public function testPaymentDeleteWithWrongID() {
310 $params = array(
311 'id' => 0,
312 );
313 $deletePayment = $this->callAPIFailure('participant_payment', 'delete', $params);
314 $this->assertEquals($deletePayment['error_message'], 'Error while deleting participantPayment');
315 }
316
317 /**
318 * Check with valid array.
319 */
320 public function testPaymentDelete() {
321 $contributionID = $this->contributionCreate(array(
322 'contact_id' => $this->_contactID,
323 ));
324
325 $this->_participantPaymentID = $this->participantPaymentCreate($this->_participantID, $contributionID);
326
327 $params = array(
328 'id' => $this->_participantPaymentID,
329 );
330 $this->callAPIAndDocument('participant_payment', 'delete', $params, __FUNCTION__, __FILE__);
331 }
332
333 /**
334 * Test civicrm_participantPayment_get - success expected.
335 */
336 public function testGet() {
337 $contributionID = $this->contributionCreate(array('contact_id' => $this->_contactID3));
338 $this->participantPaymentCreate($this->_participantID4, $contributionID);
339
340 //Create Participant Payment record With Values
341 $params = array(
342 'participant_id' => $this->_participantID4,
343 'contribution_id' => $contributionID,
344 );
345
346 $result = $this->callAPIAndDocument('participant_payment', 'get', $params, __FUNCTION__, __FILE__);
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
351 /**
352 * @param array $params
353 * @param $context
354 */
355 public function _checkFinancialRecords($params, $context) {
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;
381
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 }
416
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
439 }