3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
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 +--------------------------------------------------------------------+
13 * Test APIv3 civicrm_participant_* functions
15 * @package CiviCRM_APIv3
16 * @subpackage API_Event
19 class api_v3_ParticipantPaymentTest
extends CiviUnitTestCase
{
21 protected $_apiversion = 3;
22 protected $_contactID;
23 protected $_createdParticipants;
24 protected $_participantID;
26 protected $_participantPaymentID;
27 protected $_financialTypeId;
32 public function 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;
42 $this->_participantID
= $this->participantCreate([
43 'contactID' => $this->_contactID
,
44 'eventID' => $this->_eventID
,
46 $this->_contactID2
= $this->individualCreate();
47 $this->_participantID2
= $this->participantCreate([
48 'contactID' => $this->_contactID2
,
49 'eventID' => $this->_eventID
,
51 $this->_participantID3
= $this->participantCreate([
52 'contactID' => $this->_contactID2
,
53 'eventID' => $this->_eventID
,
56 $this->_contactID3
= $this->individualCreate();
57 $this->_participantID4
= $this->participantCreate([
58 'contactID' => $this->_contactID3
,
59 'eventID' => $this->_eventID
,
64 * Test civicrm_participant_payment_create with empty params.
66 public function testPaymentCreateEmptyParams() {
68 $this->callAPIFailure('participant_payment', 'create', $params);
72 * Check without contribution_id.
74 public function testPaymentCreateMissingContributionId() {
75 //Without Payment EntityID
77 'participant_id' => $this->_participantID
,
79 $this->callAPIFailure('participant_payment', 'create', $params);
83 * Check with valid array.
85 public function testPaymentCreate() {
86 //Create Contribution & get contribution ID
87 $contributionID = $this->contributionCreate(['contact_id' => $this->_contactID
]);
89 //Create Participant Payment record With Values
91 'participant_id' => $this->_participantID
,
92 'contribution_id' => $contributionID,
95 $result = $this->callAPIAndDocument('participant_payment', 'create', $params, __FUNCTION__
, __FILE__
);
96 $this->assertTrue(array_key_exists('id', $result));
98 //delete created contribution
99 $this->contributionDelete($contributionID);
103 * Test getPaymentInfo() returns correct
104 * information of the participant payment
106 public function testPaymentInfoForEvent() {
107 //Create Contribution & get contribution ID
108 $contributionID = $this->contributionCreate(['contact_id' => $this->_contactID
]);
110 //Create Participant Payment record With Values
112 'participant_id' => $this->_participantID4
,
113 'contribution_id' => $contributionID,
115 $this->callAPISuccess('participant_payment', 'create', $params);
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']);
123 ///////////////// civicrm_participant_payment_create methods
126 * Check with empty array.
128 public function testPaymentUpdateEmpty() {
129 $this->callAPIFailure('participant_payment', 'create', []);
133 * Check with missing participant_id.
135 public function testPaymentUpdateMissingParticipantId() {
137 'contribution_id' => '3',
139 $this->callAPIFailure('participant_payment', 'create', $params);
143 * Check with missing contribution_id.
145 public function testPaymentUpdateMissingContributionId() {
147 'participant_id' => $this->_participantID
,
149 $participantPayment = $this->callAPIFailure('participant_payment', 'create', $params);
153 * Check financial records for offline Participants.
155 public function testPaymentOffline() {
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,
166 $this->_participantPaymentID
= $this->participantPaymentCreate($this->_participantID
, $contributionID);
168 'id' => $this->_participantPaymentID
,
169 'participant_id' => $this->_participantID
,
170 'contribution_id' => $contributionID,
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');
180 'id' => $this->_participantPaymentID
,
182 $deletePayment = $this->callAPISuccess('participant_payment', 'delete', $params);
186 * Check financial records for online Participant.
188 public function testPaymentOnline() {
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,
198 $contributionID = $this->contributionCreate($contributionParams);
200 $this->_participantPaymentID
= $this->participantPaymentCreate($this->_participantID
, $contributionID);
202 'id' => $this->_participantPaymentID
,
203 'participant_id' => $this->_participantID
,
204 'contribution_id' => $contributionID,
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');
214 'id' => $this->_participantPaymentID
,
216 $this->callAPISuccess('participant_payment', 'delete', $params);
220 * Check financial records for online Participant pay later scenario.
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,
231 'financial_type_id' => 1,
233 $contributionID = $this->contributionCreate($contributionParams);
235 $this->_participantPaymentID
= $this->participantPaymentCreate($this->_participantID
, $contributionID);
237 'id' => $this->_participantPaymentID
,
238 'participant_id' => $this->_participantID
,
239 'contribution_id' => $contributionID,
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));
249 'id' => $this->_participantPaymentID
,
251 $this->callAPISuccess('participant_payment', 'delete', $params);
255 * Check with empty array.
257 public function testPaymentDeleteWithEmptyParams() {
259 $deletePayment = $this->callAPIFailure('participant_payment', 'delete', $params);
260 $this->assertEquals('Mandatory key(s) missing from params array: id', $deletePayment['error_message']);
264 * Check with wrong id.
266 public function testPaymentDeleteWithWrongID() {
270 $deletePayment = $this->callAPIFailure('participant_payment', 'delete', $params);
271 $this->assertEquals($deletePayment['error_message'], 'Error while deleting participantPayment');
275 * Check with valid array.
277 public function testPaymentDelete() {
278 $contributionID = $this->contributionCreate([
279 'contact_id' => $this->_contactID
,
282 $this->_participantPaymentID
= $this->participantPaymentCreate($this->_participantID
, $contributionID);
285 'id' => $this->_participantPaymentID
,
287 $this->callAPIAndDocument('participant_payment', 'delete', $params, __FUNCTION__
, __FILE__
);
291 * Test civicrm_participantPayment_get - success expected.
293 public function testGet() {
294 $contributionID = $this->contributionCreate(['contact_id' => $this->_contactID3
]);
295 $this->participantPaymentCreate($this->_participantID4
, $contributionID);
297 //Create Participant Payment record With Values
299 'participant_id' => $this->_participantID4
,
300 'contribution_id' => $contributionID,
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');
309 * @param array $params
312 public function _checkFinancialRecords($params, $context) {
314 'entity_id' => $params['id'],
315 'entity_table' => 'civicrm_contribution',
317 $trxn = current(CRM_Financial_BAO_FinancialItem
::retrieveEntityFinancialTrxn($entityParams));
319 'id' => $trxn['financial_trxn_id'],
325 'to_financial_account_id' => 12,
326 'total_amount' => 100,
333 'to_financial_account_id' => 6,
334 'total_amount' => 100,
341 'to_financial_account_id' => 7,
342 'total_amount' => 100,
348 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialTrxn', $trxnParams, $compareParams);
350 'financial_trxn_id' => $trxn['financial_trxn_id'],
351 'entity_table' => 'civicrm_financial_item',
353 $entityTrxn = current(CRM_Financial_BAO_FinancialItem
::retrieveEntityFinancialTrxn($entityParams));
355 'id' => $entityTrxn['entity_id'],
357 if ($context == 'offline' ||
$context == 'online') {
361 'financial_account_id' => 1,
364 elseif ($context == 'payLater') {
368 'financial_account_id' => 1,
371 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $fitemParams, $compareParams);
375 * test getParticipantIds() function
377 public function testGetParticipantIds() {
378 $contributionID = $this->contributionCreate(['contact_id' => $this->_contactID
]);
379 $expectedParticipants = [$this->_participantID
, $this->_participantID2
];
381 //Create Participant Payment record With Values
382 foreach ($expectedParticipants as $pid) {
384 'participant_id' => $pid,
385 'contribution_id' => $contributionID,
387 $this->callAPISuccess('participant_payment', 'create', $params);
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);