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 wrong params type.
66 public function testPaymentCreateWrongParamsType() {
68 $this->callAPIFailure('participant_payment', 'create', $params);
72 * Test civicrm_participant_payment_create with empty params.
74 public function testPaymentCreateEmptyParams() {
76 $this->callAPIFailure('participant_payment', 'create', $params);
80 * Check without contribution_id.
82 public function testPaymentCreateMissingContributionId() {
83 //Without Payment EntityID
85 'participant_id' => $this->_participantID
,
87 $this->callAPIFailure('participant_payment', 'create', $params);
91 * Check with valid array.
93 public function testPaymentCreate() {
94 //Create Contribution & get contribution ID
95 $contributionID = $this->contributionCreate(['contact_id' => $this->_contactID
]);
97 //Create Participant Payment record With Values
99 'participant_id' => $this->_participantID
,
100 'contribution_id' => $contributionID,
103 $result = $this->callAPIAndDocument('participant_payment', 'create', $params, __FUNCTION__
, __FILE__
);
104 $this->assertTrue(array_key_exists('id', $result));
106 //delete created contribution
107 $this->contributionDelete($contributionID);
111 * Test getPaymentInfo() returns correct
112 * information of the participant payment
114 public function testPaymentInfoForEvent() {
115 //Create Contribution & get contribution ID
116 $contributionID = $this->contributionCreate(['contact_id' => $this->_contactID
]);
118 //Create Participant Payment record With Values
120 'participant_id' => $this->_participantID4
,
121 'contribution_id' => $contributionID,
123 $this->callAPISuccess('participant_payment', 'create', $params);
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']);
131 ///////////////// civicrm_participant_payment_create methods
134 * Test civicrm_participant payment create with wrong params type.
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']);
143 * Check with empty array.
145 public function testPaymentUpdateEmpty() {
146 $this->callAPIFailure('participant_payment', 'create', []);
150 * Check with missing participant_id.
152 public function testPaymentUpdateMissingParticipantId() {
154 'contribution_id' => '3',
156 $this->callAPIFailure('participant_payment', 'create', $params);
160 * Check with missing contribution_id.
162 public function testPaymentUpdateMissingContributionId() {
164 'participant_id' => $this->_participantID
,
166 $participantPayment = $this->callAPIFailure('participant_payment', 'create', $params);
170 * Check financial records for offline Participants.
172 public function testPaymentOffline() {
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,
183 $this->_participantPaymentID
= $this->participantPaymentCreate($this->_participantID
, $contributionID);
185 'id' => $this->_participantPaymentID
,
186 'participant_id' => $this->_participantID
,
187 'contribution_id' => $contributionID,
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');
197 'id' => $this->_participantPaymentID
,
199 $deletePayment = $this->callAPISuccess('participant_payment', 'delete', $params);
203 * Check financial records for online Participant.
205 public function testPaymentOnline() {
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,
215 $contributionID = $this->contributionCreate($contributionParams);
217 $this->_participantPaymentID
= $this->participantPaymentCreate($this->_participantID
, $contributionID);
219 'id' => $this->_participantPaymentID
,
220 'participant_id' => $this->_participantID
,
221 'contribution_id' => $contributionID,
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');
231 'id' => $this->_participantPaymentID
,
233 $this->callAPISuccess('participant_payment', 'delete', $params);
237 * Check financial records for online Participant pay later scenario.
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,
248 'financial_type_id' => 1,
250 $contributionID = $this->contributionCreate($contributionParams);
252 $this->_participantPaymentID
= $this->participantPaymentCreate($this->_participantID
, $contributionID);
254 'id' => $this->_participantPaymentID
,
255 'participant_id' => $this->_participantID
,
256 'contribution_id' => $contributionID,
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));
266 'id' => $this->_participantPaymentID
,
268 $this->callAPISuccess('participant_payment', 'delete', $params);
272 * Test civicrm_participant_payment_delete with wrong params type.
274 public function testPaymentDeleteWrongParamsType() {
275 $params = 'a string';
276 $this->callAPIFailure('participant_payment', 'delete', $params);
280 * Check with empty array.
282 public function testPaymentDeleteWithEmptyParams() {
284 $deletePayment = $this->callAPIFailure('participant_payment', 'delete', $params);
285 $this->assertEquals('Mandatory key(s) missing from params array: id', $deletePayment['error_message']);
289 * Check with wrong id.
291 public function testPaymentDeleteWithWrongID() {
295 $deletePayment = $this->callAPIFailure('participant_payment', 'delete', $params);
296 $this->assertEquals($deletePayment['error_message'], 'Error while deleting participantPayment');
300 * Check with valid array.
302 public function testPaymentDelete() {
303 $contributionID = $this->contributionCreate([
304 'contact_id' => $this->_contactID
,
307 $this->_participantPaymentID
= $this->participantPaymentCreate($this->_participantID
, $contributionID);
310 'id' => $this->_participantPaymentID
,
312 $this->callAPIAndDocument('participant_payment', 'delete', $params, __FUNCTION__
, __FILE__
);
316 * Test civicrm_participantPayment_get - success expected.
318 public function testGet() {
319 $contributionID = $this->contributionCreate(['contact_id' => $this->_contactID3
]);
320 $this->participantPaymentCreate($this->_participantID4
, $contributionID);
322 //Create Participant Payment record With Values
324 'participant_id' => $this->_participantID4
,
325 'contribution_id' => $contributionID,
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');
334 * @param array $params
337 public function _checkFinancialRecords($params, $context) {
339 'entity_id' => $params['id'],
340 'entity_table' => 'civicrm_contribution',
342 $trxn = current(CRM_Financial_BAO_FinancialItem
::retrieveEntityFinancialTrxn($entityParams));
344 'id' => $trxn['financial_trxn_id'],
350 'to_financial_account_id' => 12,
351 'total_amount' => 100,
358 'to_financial_account_id' => 6,
359 'total_amount' => 100,
366 'to_financial_account_id' => 7,
367 'total_amount' => 100,
373 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialTrxn', $trxnParams, $compareParams);
375 'financial_trxn_id' => $trxn['financial_trxn_id'],
376 'entity_table' => 'civicrm_financial_item',
378 $entityTrxn = current(CRM_Financial_BAO_FinancialItem
::retrieveEntityFinancialTrxn($entityParams));
380 'id' => $entityTrxn['entity_id'],
382 if ($context == 'offline' ||
$context == 'online') {
386 'financial_account_id' => 1,
389 elseif ($context == 'payLater') {
393 'financial_account_id' => 1,
396 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $fitemParams, $compareParams);
400 * test getParticipantIds() function
402 public function testGetParticipantIds() {
403 $contributionID = $this->contributionCreate(['contact_id' => $this->_contactID
]);
404 $expectedParticipants = [$this->_participantID
, $this->_participantID2
];
406 //Create Participant Payment record With Values
407 foreach ($expectedParticipants as $pid) {
409 'participant_id' => $pid,
410 'contribution_id' => $contributionID,
412 $this->callAPISuccess('participant_payment', 'create', $params);
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);