3 * +--------------------------------------------------------------------+
4 * | CiviCRM version 5 |
5 * +--------------------------------------------------------------------+
6 * | Copyright CiviCRM LLC (c) 2004-2019 |
7 * +--------------------------------------------------------------------+
8 * | This file is a part of CiviCRM. |
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. |
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. |
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 * +--------------------------------------------------------------------+
29 * Test APIv3 civicrm_participant_* functions
31 * @package CiviCRM_APIv3
32 * @subpackage API_Event
35 class api_v3_ParticipantPaymentTest
extends CiviUnitTestCase
{
37 protected $_apiversion = 3;
38 protected $_contactID;
39 protected $_createdParticipants;
40 protected $_participantID;
42 protected $_participantPaymentID;
43 protected $_financialTypeId;
48 public function 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;
58 $this->_participantID
= $this->participantCreate([
59 'contactID' => $this->_contactID
,
60 'eventID' => $this->_eventID
,
62 $this->_contactID2
= $this->individualCreate();
63 $this->_participantID2
= $this->participantCreate([
64 'contactID' => $this->_contactID2
,
65 'eventID' => $this->_eventID
,
67 $this->_participantID3
= $this->participantCreate([
68 'contactID' => $this->_contactID2
,
69 'eventID' => $this->_eventID
,
72 $this->_contactID3
= $this->individualCreate();
73 $this->_participantID4
= $this->participantCreate([
74 'contactID' => $this->_contactID3
,
75 'eventID' => $this->_eventID
,
80 * Test civicrm_participant_payment_create with wrong params type.
82 public function testPaymentCreateWrongParamsType() {
84 $this->callAPIFailure('participant_payment', 'create', $params);
88 * Test civicrm_participant_payment_create with empty params.
90 public function testPaymentCreateEmptyParams() {
92 $this->callAPIFailure('participant_payment', 'create', $params);
96 * Check without contribution_id.
98 public function testPaymentCreateMissingContributionId() {
99 //Without Payment EntityID
101 'participant_id' => $this->_participantID
,
103 $this->callAPIFailure('participant_payment', 'create', $params);
107 * Check with valid array.
109 public function testPaymentCreate() {
110 //Create Contribution & get contribution ID
111 $contributionID = $this->contributionCreate(['contact_id' => $this->_contactID
]);
113 //Create Participant Payment record With Values
115 'participant_id' => $this->_participantID
,
116 'contribution_id' => $contributionID,
119 $result = $this->callAPIAndDocument('participant_payment', 'create', $params, __FUNCTION__
, __FILE__
);
120 $this->assertTrue(array_key_exists('id', $result));
122 //delete created contribution
123 $this->contributionDelete($contributionID);
127 * Test getPaymentInfo() returns correct
128 * information of the participant payment
130 public function testPaymentInfoForEvent() {
131 //Create Contribution & get contribution ID
132 $contributionID = $this->contributionCreate(['contact_id' => $this->_contactID
]);
134 //Create Participant Payment record With Values
136 'participant_id' => $this->_participantID4
,
137 'contribution_id' => $contributionID,
139 $this->callAPISuccess('participant_payment', 'create', $params);
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']);
147 ///////////////// civicrm_participant_payment_create methods
150 * Test civicrm_participant payment create with wrong params type.
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']);
159 * Check with empty array.
161 public function testPaymentUpdateEmpty() {
162 $this->callAPIFailure('participant_payment', 'create', []);
166 * Check with missing participant_id.
168 public function testPaymentUpdateMissingParticipantId() {
170 'contribution_id' => '3',
172 $this->callAPIFailure('participant_payment', 'create', $params);
176 * Check with missing contribution_id.
178 public function testPaymentUpdateMissingContributionId() {
180 'participant_id' => $this->_participantID
,
182 $participantPayment = $this->callAPIFailure('participant_payment', 'create', $params);
186 * Check financial records for offline Participants.
188 public function testPaymentOffline() {
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,
199 $this->_participantPaymentID
= $this->participantPaymentCreate($this->_participantID
, $contributionID);
201 'id' => $this->_participantPaymentID
,
202 'participant_id' => $this->_participantID
,
203 'contribution_id' => $contributionID,
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');
213 'id' => $this->_participantPaymentID
,
215 $deletePayment = $this->callAPISuccess('participant_payment', 'delete', $params);
219 * Check financial records for online Participant.
221 public function testPaymentOnline() {
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,
231 $contributionID = $this->contributionCreate($contributionParams);
233 $this->_participantPaymentID
= $this->participantPaymentCreate($this->_participantID
, $contributionID);
235 'id' => $this->_participantPaymentID
,
236 'participant_id' => $this->_participantID
,
237 'contribution_id' => $contributionID,
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');
247 'id' => $this->_participantPaymentID
,
249 $this->callAPISuccess('participant_payment', 'delete', $params);
253 * Check financial records for online Participant pay later scenario.
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,
264 'financial_type_id' => 1,
266 $contributionID = $this->contributionCreate($contributionParams);
268 $this->_participantPaymentID
= $this->participantPaymentCreate($this->_participantID
, $contributionID);
270 'id' => $this->_participantPaymentID
,
271 'participant_id' => $this->_participantID
,
272 'contribution_id' => $contributionID,
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));
282 'id' => $this->_participantPaymentID
,
284 $this->callAPISuccess('participant_payment', 'delete', $params);
288 * Test civicrm_participant_payment_delete with wrong params type.
290 public function testPaymentDeleteWrongParamsType() {
291 $params = 'a string';
292 $this->callAPIFailure('participant_payment', 'delete', $params);
296 * Check with empty array.
298 public function testPaymentDeleteWithEmptyParams() {
300 $deletePayment = $this->callAPIFailure('participant_payment', 'delete', $params);
301 $this->assertEquals('Mandatory key(s) missing from params array: id', $deletePayment['error_message']);
305 * Check with wrong id.
307 public function testPaymentDeleteWithWrongID() {
311 $deletePayment = $this->callAPIFailure('participant_payment', 'delete', $params);
312 $this->assertEquals($deletePayment['error_message'], 'Error while deleting participantPayment');
316 * Check with valid array.
318 public function testPaymentDelete() {
319 $contributionID = $this->contributionCreate([
320 'contact_id' => $this->_contactID
,
323 $this->_participantPaymentID
= $this->participantPaymentCreate($this->_participantID
, $contributionID);
326 'id' => $this->_participantPaymentID
,
328 $this->callAPIAndDocument('participant_payment', 'delete', $params, __FUNCTION__
, __FILE__
);
332 * Test civicrm_participantPayment_get - success expected.
334 public function testGet() {
335 $contributionID = $this->contributionCreate(['contact_id' => $this->_contactID3
]);
336 $this->participantPaymentCreate($this->_participantID4
, $contributionID);
338 //Create Participant Payment record With Values
340 'participant_id' => $this->_participantID4
,
341 'contribution_id' => $contributionID,
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');
350 * @param array $params
353 public function _checkFinancialRecords($params, $context) {
355 'entity_id' => $params['id'],
356 'entity_table' => 'civicrm_contribution',
358 $trxn = current(CRM_Financial_BAO_FinancialItem
::retrieveEntityFinancialTrxn($entityParams));
360 'id' => $trxn['financial_trxn_id'],
366 'to_financial_account_id' => 12,
367 'total_amount' => 100,
374 'to_financial_account_id' => 6,
375 'total_amount' => 100,
382 'to_financial_account_id' => 7,
383 'total_amount' => 100,
389 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialTrxn', $trxnParams, $compareParams);
391 'financial_trxn_id' => $trxn['financial_trxn_id'],
392 'entity_table' => 'civicrm_financial_item',
394 $entityTrxn = current(CRM_Financial_BAO_FinancialItem
::retrieveEntityFinancialTrxn($entityParams));
396 'id' => $entityTrxn['entity_id'],
398 if ($context == 'offline' ||
$context == 'online') {
402 'financial_account_id' => 1,
405 elseif ($context == 'payLater') {
409 'financial_account_id' => 1,
412 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $fitemParams, $compareParams);
416 * test getParticipantIds() function
418 public function testGetParticipantIds() {
419 $contributionID = $this->contributionCreate(['contact_id' => $this->_contactID
]);
420 $expectedParticipants = [$this->_participantID
, $this->_participantID2
];
422 //Create Participant Payment record With Values
423 foreach ($expectedParticipants as $pid) {
425 'participant_id' => $pid,
426 'contribution_id' => $contributionID,
428 $this->callAPISuccess('participant_payment', 'create', $params);
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);