-
-
Notifications
You must be signed in to change notification settings - Fork 827
/
Copy pathParticipantPaymentTest.php
339 lines (298 loc) · 10.2 KB
/
ParticipantPaymentTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
<?php
/*
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC. All rights reserved. |
| |
| This work is published under the GNU AGPLv3 license with some |
| permitted exceptions and without any warranty. For full license |
| and copyright information, see https://civicrm.org/licensing |
+--------------------------------------------------------------------+
*/
/**
* Test APIv3 civicrm_participant_* functions
*
* @package CiviCRM_APIv3
* @subpackage API_Event
* @group headless
*/
class api_v3_ParticipantPaymentTest extends CiviUnitTestCase {
/**
* @var int
*/
protected $contactID;
/**
* @var int
*/
protected $contactID2;
/**
* @var int
*/
protected $contactID3;
/**
* @var int
*/
protected $participantID;
/**
* @var int
*/
protected $participantID2;
/**
* @var int
*/
protected $participantID3;
/**
* @var int
*/
protected $participantID4;
/**
* Set up for tests.
*/
public function setUp(): void {
parent::setUp();
$this->useTransaction(TRUE);
$this->eventCreatePaid();
$this->contactID = $this->individualCreate();
$this->individualCreate();
$this->participantID = $this->participantCreate([
'contact_id' => $this->contactID,
'event_id' => $this->getEventID(),
]);
$contactID2 = $this->individualCreate();
$this->participantID2 = $this->participantCreate([
'contact_id' => $contactID2,
'event_id' => $this->getEventID(),
]);
$this->participantID3 = $this->participantCreate([
'contact_id' => $contactID2,
'event_id' => $this->getEventID(),
]);
$this->contactID3 = $this->individualCreate();
$this->participantID4 = $this->participantCreate([
'contact_id' => $this->contactID3,
'event_id' => $this->getEventID(),
]);
}
/**
* Check with valid array.
*/
public function testPaymentCreate(): void {
$this->callAPISuccess('ParticipantPayment', 'create', [
'participant_id' => $this->participantID,
'contribution_id' => $this->contributionCreate(['contact_id' => $this->individualCreate()]),
]);
}
/**
* Test getPaymentInfo() returns correct
* information of the participant payment
*
* @throws \CRM_Core_Exception
*/
public function testPaymentInfoForEvent(): void {
//Create Contribution & get contribution ID
$contributionID = $this->contributionCreate(['contact_id' => $this->contactID]);
//Create Participant Payment record With Values
$params = [
'participant_id' => $this->participantID4,
'contribution_id' => $contributionID,
];
$this->callAPISuccess('participantPayment', 'create', $params);
//Check if participant payment is correctly retrieved.
$paymentInfo = CRM_Contribute_BAO_Contribution::getPaymentInfo($this->participantID4, 'event');
$this->assertEquals('Completed', $paymentInfo['contribution_status']);
$this->assertEquals('100.00', $paymentInfo['total']);
}
/**
* Check financial records for offline Participants.
*/
public function testPaymentOffline(): void {
// create contribution w/o fee
$contributionID = $this->contributionCreate([
'contact_id' => $this->contactID,
'financial_type_id' => 1,
'payment_instrument_id' => 4,
'fee_amount' => 0,
'net_amount' => 100,
]);
$participantPaymentID = $this->participantPaymentCreate($this->participantID, $contributionID);
$params = [
'id' => $participantPaymentID,
'participant_id' => $this->participantID,
'contribution_id' => $contributionID,
];
// Update Payment
$participantPayment = $this->callAPISuccess('participantPayment', 'create', $params);
$this->assertEquals($participantPayment['id'], $participantPaymentID);
$this->assertArrayHasKey('id', $participantPayment);
// check Financial records
$this->_checkFinancialRecords($params, 'offline');
}
/**
* Check financial records for online Participant.
*/
public function testPaymentOnline(): void {
$pageParams['processor_id'] = $this->processorCreate();
$contributionPage = $this->contributionPageCreate($pageParams);
$contributionParams = [
'contact_id' => $this->contactID,
'contribution_page_id' => $contributionPage['id'],
'payment_processor' => $pageParams['processor_id'],
'financial_type_id' => 1,
];
$contributionID = $this->contributionCreate($contributionParams);
$participantPaymentID = $this->participantPaymentCreate($this->participantID, $contributionID);
$params = [
'id' => $participantPaymentID,
'participant_id' => $this->participantID,
'contribution_id' => $contributionID,
];
// Update Payment
$participantPayment = $this->callAPISuccess('participantPayment', 'create', $params);
$this->assertEquals($participantPayment['id'], $participantPaymentID);
$this->assertArrayHasKey('id', $participantPayment);
// check Financial records
$this->_checkFinancialRecords($params, 'online');
}
/**
* Check financial records for online Participant pay later scenario.
*/
public function testPaymentPayLaterOnline(): void {
$pageParams['processor_id'] = $this->processorCreate();
$pageParams['is_pay_later'] = 1;
$contributionPage = $this->contributionPageCreate($pageParams);
$contributionParams = [
'contact_id' => $this->contactID,
'contribution_page_id' => $contributionPage['id'],
'contribution_status_id' => 2,
'is_pay_later' => 1,
'financial_type_id' => 1,
];
$contributionID = $this->contributionCreate($contributionParams);
$participantPaymentID = $this->participantPaymentCreate($this->participantID, $contributionID);
$params = [
'id' => $participantPaymentID,
'participant_id' => $this->participantID,
'contribution_id' => $contributionID,
];
// Update Payment
$participantPayment = $this->callAPISuccess('participantPayment', 'create', $params);
// check Financial Records
$this->_checkFinancialRecords($params, 'payLater');
$this->assertEquals($participantPayment['id'], $participantPaymentID);
$this->assertArrayHasKey('id', $participantPayment);
}
/**
* Check with invalid id.
*/
public function testPaymentDeleteWithWrongID(): void {
$this->callAPIFailure('ParticipantPayment', 'delete', ['id' => 0], 'Error while deleting participantPayment');
}
/**
* Check with valid array.
*/
public function testPaymentDelete(): void {
$contributionID = $this->contributionCreate([
'contact_id' => $this->contactID,
]);
$participantPaymentID = $this->participantPaymentCreate($this->participantID, $contributionID);
$params = [
'id' => $participantPaymentID,
];
$this->callAPISuccess('participantPayment', 'delete', $params);
}
/**
* Test civicrm_participantPayment_get - success expected.
*/
public function testGet(): void {
$contributionID = $this->contributionCreate(['contact_id' => $this->contactID3]);
$this->participantPaymentCreate($this->participantID4, $contributionID);
//Create Participant Payment record With Values
$params = [
'participant_id' => $this->participantID4,
'contribution_id' => $contributionID,
];
$result = $this->callAPISuccess('participantPayment', 'get', $params);
$this->assertEquals($result['values'][$result['id']]['participant_id'], $this->participantID4, 'Check Participant Id');
$this->assertEquals($result['values'][$result['id']]['contribution_id'], $contributionID, 'Check Contribution Id');
}
/**
* @param array $params
* @param $context
*/
public function _checkFinancialRecords($params, $context): void {
$entityParams = [
'entity_id' => $params['id'],
'entity_table' => 'civicrm_contribution',
];
$trxn = current($this->retrieveEntityFinancialTrxn($entityParams));
$trxnParams = [
'id' => $trxn['financial_trxn_id'],
];
switch ($context) {
case 'online':
$compareParams = [
'to_financial_account_id' => 12,
'total_amount' => 100,
'status_id' => 1,
];
break;
case 'offline':
$compareParams = [
'to_financial_account_id' => 6,
'total_amount' => 100,
'status_id' => 1,
];
break;
case 'payLater':
$compareParams = [
'to_financial_account_id' => 7,
'total_amount' => 100,
'status_id' => 2,
];
break;
}
$this->assertDBCompareValues('CRM_Financial_DAO_FinancialTrxn', $trxnParams, $compareParams);
$entityParams = [
'financial_trxn_id' => $trxn['financial_trxn_id'],
'entity_table' => 'civicrm_financial_item',
];
$entityTrxn = current($this->retrieveEntityFinancialTrxn($entityParams));
$financialItemParams = [
'id' => $entityTrxn['entity_id'],
];
if ($context === 'offline' || $context === 'online') {
$compareParams = [
'amount' => 100,
'status_id' => 1,
'financial_account_id' => 1,
];
}
elseif ($context === 'payLater') {
$compareParams = [
'amount' => 100,
'status_id' => 3,
'financial_account_id' => 1,
];
}
$this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $financialItemParams, $compareParams);
}
/**
* test getParticipantIds() function
*/
public function testGetParticipantIDs(): void {
$contributionID = $this->contributionCreate(['contact_id' => $this->contactID]);
$expectedParticipants = [$this->participantID, $this->participantID2];
//Create Participant Payment record With Values
foreach ($expectedParticipants as $pid) {
$params = [
'participant_id' => $pid,
'contribution_id' => $contributionID,
];
$this->callAPISuccess('participantPayment', 'create', $params);
}
//Check if all participants are listed.
$participants = CRM_Event_BAO_Participant::getParticipantIds($contributionID);
$this->checkArrayEquals($expectedParticipants, $participants);
//delete created contribution
$this->contributionDelete($contributionID);
}
}