Merge pull request #10439 from jitendrapurohit/CRM-20657
[civicrm-core.git] / tests / phpunit / api / v3 / ParticipantPaymentTest.php
CommitLineData
6a488035 1<?php
0728d72f 2/**
92915c55 3 * +--------------------------------------------------------------------+
81621fee 4 * | CiviCRM version 4.7 |
92915c55 5 * +--------------------------------------------------------------------+
15a4309a 6 * | Copyright CiviCRM LLC (c) 2004-2017 |
92915c55
TO
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 * +--------------------------------------------------------------------+
6a488035
TO
26 */
27
6a488035
TO
28/**
29 * Test APIv3 civicrm_participant_* functions
30 *
6c6e6187
TO
31 * @package CiviCRM_APIv3
32 * @subpackage API_Event
acb109b7 33 * @group headless
6a488035 34 */
6a488035
TO
35class api_v3_ParticipantPaymentTest extends CiviUnitTestCase {
36
0728d72f 37 protected $_apiversion = 3;
6a488035
TO
38 protected $_contactID;
39 protected $_createdParticipants;
40 protected $_participantID;
41 protected $_eventID;
42 protected $_participantPaymentID;
69fccd8f 43 protected $_financialTypeId;
b7c9bc4c 44
78ab0ca4 45 /**
46 * Set up for tests.
47 */
00be9182 48 public function setUp() {
6a488035 49 parent::setUp();
924cd81a 50 $this->useTransaction(TRUE);
6a488035
TO
51 $event = $this->eventCreate(NULL);
52 $this->_eventID = $event['id'];
e4d5f1e2 53 $this->_contactID = $this->individualCreate();
6a488035 54 $this->_createdParticipants = array();
e4d5f1e2 55 $this->_individualId = $this->individualCreate();
69fccd8f 56 $this->_financialTypeId = 1;
6a488035 57
92915c55
TO
58 $this->_participantID = $this->participantCreate(array(
59 'contactID' => $this->_contactID,
408b79bf 60 'eventID' => $this->_eventID,
92915c55 61 ));
e4d5f1e2 62 $this->_contactID2 = $this->individualCreate();
92915c55
TO
63 $this->_participantID2 = $this->participantCreate(array(
64 'contactID' => $this->_contactID2,
408b79bf 65 'eventID' => $this->_eventID,
92915c55
TO
66 ));
67 $this->_participantID3 = $this->participantCreate(array(
68 'contactID' => $this->_contactID2,
408b79bf 69 'eventID' => $this->_eventID,
92915c55 70 ));
6a488035 71
e4d5f1e2 72 $this->_contactID3 = $this->individualCreate();
92915c55
TO
73 $this->_participantID4 = $this->participantCreate(array(
74 'contactID' => $this->_contactID3,
408b79bf 75 'eventID' => $this->_eventID,
92915c55 76 ));
6a488035
TO
77 }
78
6a488035 79 /**
fe482240 80 * Test civicrm_participant_payment_create with wrong params type.
6a488035 81 */
00be9182 82 public function testPaymentCreateWrongParamsType() {
6a488035 83 $params = 'a string';
69fccd8f 84 $this->callAPIFailure('participant_payment', 'create', $params);
6a488035
TO
85 }
86
87 /**
fe482240 88 * Test civicrm_participant_payment_create with empty params.
6a488035 89 */
00be9182 90 public function testPaymentCreateEmptyParams() {
6a488035 91 $params = array();
69fccd8f 92 $this->callAPIFailure('participant_payment', 'create', $params);
6a488035
TO
93 }
94
95 /**
fe482240 96 * Check without contribution_id.
6a488035 97 */
00be9182 98 public function testPaymentCreateMissingContributionId() {
6a488035
TO
99 //Without Payment EntityID
100 $params = array(
92915c55
TO
101 'participant_id' => $this->_participantID,
102 );
69fccd8f 103 $this->callAPIFailure('participant_payment', 'create', $params);
6a488035
TO
104 }
105
106 /**
eceb18cc 107 * Check with valid array.
6a488035 108 */
00be9182 109 public function testPaymentCreate() {
6a488035 110 //Create Contribution & get contribution ID
69fccd8f 111 $contributionID = $this->contributionCreate(array('contact_id' => $this->_contactID));
6a488035
TO
112
113 //Create Participant Payment record With Values
114 $params = array(
115 'participant_id' => $this->_participantID,
116 'contribution_id' => $contributionID,
6a488035
TO
117 );
118
0728d72f 119 $result = $this->callAPIAndDocument('participant_payment', 'create', $params, __FUNCTION__, __FILE__);
ba4a1892 120 $this->assertTrue(array_key_exists('id', $result));
6a488035
TO
121
122 //delete created contribution
123 $this->contributionDelete($contributionID);
124 }
125
126
127 ///////////////// civicrm_participant_payment_create methods
128
129 /**
fe482240 130 * Test civicrm_participant payment create with wrong params type.
6a488035 131 */
00be9182 132 public function testPaymentUpdateWrongParamsType() {
6a488035 133 $params = 'a string';
d0e1eff2 134 $result = $this->callAPIFailure('participant_payment', 'create', $params);
ba4a1892 135 $this->assertEquals('Input variable `params` is not an array', $result['error_message']);
6a488035
TO
136 }
137
138 /**
eceb18cc 139 * Check with empty array.
6a488035 140 */
00be9182 141 public function testPaymentUpdateEmpty() {
fe482240 142 $this->callAPIFailure('participant_payment', 'create', array());
6a488035
TO
143 }
144
145 /**
fe482240 146 * Check with missing participant_id.
6a488035 147 */
00be9182 148 public function testPaymentUpdateMissingParticipantId() {
6a488035 149 $params = array(
92915c55
TO
150 'contribution_id' => '3',
151 );
fe482240 152 $this->callAPIFailure('participant_payment', 'create', $params);
6a488035
TO
153 }
154
155 /**
fe482240 156 * Check with missing contribution_id.
6a488035 157 */
00be9182 158 public function testPaymentUpdateMissingContributionId() {
6a488035 159 $params = array(
92915c55
TO
160 'participant_id' => $this->_participantID,
161 );
d0e1eff2 162 $participantPayment = $this->callAPIFailure('participant_payment', 'create', $params);
6a488035
TO
163 }
164
165 /**
eceb18cc 166 * Check financial records for offline Participants.
6a488035 167 */
00be9182 168 public function testPaymentOffline() {
6a488035
TO
169
170 // create contribution w/o fee
69fccd8f 171 $contributionID = $this->contributionCreate(array(
172 'contact_id' => $this->_contactID,
173 'financial_type_id' => $this->_financialTypeId,
174 'payment_instrument_id' => 4,
175 'fee_amount' => 0,
176 'net_amount' => 100,
177 ));
6a488035
TO
178
179 $this->_participantPaymentID = $this->participantPaymentCreate($this->_participantID, $contributionID);
180 $params = array(
181 'id' => $this->_participantPaymentID,
182 'participant_id' => $this->_participantID,
92915c55
TO
183 'contribution_id' => $contributionID,
184 );
6a488035
TO
185
186 // Update Payment
0728d72f 187 $participantPayment = $this->callAPISuccess('participant_payment', 'create', $params);
6a488035
TO
188 $this->assertEquals($participantPayment['id'], $this->_participantPaymentID);
189 $this->assertTrue(array_key_exists('id', $participantPayment));
190 // check Financial records
191 $this->_checkFinancialRecords($params, 'offline');
192 $params = array(
193 'id' => $this->_participantPaymentID,
6a488035 194 );
0728d72f 195 $deletePayment = $this->callAPISuccess('participant_payment', 'delete', $params);
6a488035
TO
196 }
197
198 /**
eceb18cc 199 * Check financial records for online Participant.
6a488035 200 */
00be9182 201 public function testPaymentOnline() {
6a488035 202
8950ecdc 203 $pageParams['processor_id'] = $this->processorCreate();
6a488035
TO
204 $contributionPage = $this->contributionPageCreate($pageParams);
205 $contributionParams = array(
92915c55
TO
206 'contact_id' => $this->_contactID,
207 'contribution_page_id' => $contributionPage['id'],
8950ecdc 208 'payment_processor' => $pageParams['processor_id'],
78ab0ca4 209 'financial_type_id' => 1,
6a488035 210 );
78ab0ca4 211 $contributionID = $this->contributionCreate($contributionParams);
6a488035
TO
212
213 $this->_participantPaymentID = $this->participantPaymentCreate($this->_participantID, $contributionID);
214 $params = array(
215 'id' => $this->_participantPaymentID,
216 'participant_id' => $this->_participantID,
92915c55
TO
217 'contribution_id' => $contributionID,
218 );
6a488035
TO
219
220 // Update Payment
0728d72f 221 $participantPayment = $this->callAPISuccess('participant_payment', 'create', $params);
6a488035
TO
222 $this->assertEquals($participantPayment['id'], $this->_participantPaymentID);
223 $this->assertTrue(array_key_exists('id', $participantPayment));
224 // check Financial records
225 $this->_checkFinancialRecords($params, 'online');
226 $params = array(
227 'id' => $this->_participantPaymentID,
6a488035 228 );
78ab0ca4 229 $this->callAPISuccess('participant_payment', 'delete', $params);
6a488035
TO
230 }
231
232 /**
eceb18cc 233 * Check financial records for online Participant pay later scenario.
6a488035 234 */
00be9182 235 public function testPaymentPayLaterOnline() {
8950ecdc 236 $pageParams['processor_id'] = $this->processorCreate();
6a488035
TO
237 $pageParams['is_pay_later'] = 1;
238 $contributionPage = $this->contributionPageCreate($pageParams);
239 $contributionParams = array(
240 'contact_id' => $this->_contactID,
241 'contribution_page_id' => $contributionPage['id'],
242 'contribution_status_id' => 2,
243 'is_pay_later' => 1,
78ab0ca4 244 'financial_type_id' => 1,
6a488035 245 );
78ab0ca4 246 $contributionID = $this->contributionCreate($contributionParams);
6a488035
TO
247
248 $this->_participantPaymentID = $this->participantPaymentCreate($this->_participantID, $contributionID);
249 $params = array(
250 'id' => $this->_participantPaymentID,
251 'participant_id' => $this->_participantID,
92915c55
TO
252 'contribution_id' => $contributionID,
253 );
6a488035
TO
254
255 // Update Payment
0728d72f 256 $participantPayment = $this->callAPISuccess('participant_payment', 'create', $params);
6a488035
TO
257 // check Financial Records
258 $this->_checkFinancialRecords($params, 'payLater');
259 $this->assertEquals($participantPayment['id'], $this->_participantPaymentID);
260 $this->assertTrue(array_key_exists('id', $participantPayment));
261 $params = array(
262 'id' => $this->_participantPaymentID,
6a488035 263 );
78ab0ca4 264 $this->callAPISuccess('participant_payment', 'delete', $params);
6a488035
TO
265 }
266
6a488035
TO
267
268 /**
fe482240 269 * Test civicrm_participant_payment_delete with wrong params type.
6a488035 270 */
00be9182 271 public function testPaymentDeleteWrongParamsType() {
6a488035 272 $params = 'a string';
fe482240 273 $this->callAPIFailure('participant_payment', 'delete', $params);
6a488035
TO
274 }
275
276 /**
eceb18cc 277 * Check with empty array.
6a488035 278 */
00be9182 279 public function testPaymentDeleteWithEmptyParams() {
0728d72f 280 $params = array();
d0e1eff2 281 $deletePayment = $this->callAPIFailure('participant_payment', 'delete', $params);
6a488035
TO
282 $this->assertEquals('Mandatory key(s) missing from params array: id', $deletePayment['error_message']);
283 }
284
285 /**
eceb18cc 286 * Check with wrong id.
6a488035 287 */
00be9182 288 public function testPaymentDeleteWithWrongID() {
6a488035 289 $params = array(
92915c55
TO
290 'id' => 0,
291 );
d0e1eff2 292 $deletePayment = $this->callAPIFailure('participant_payment', 'delete', $params);
274775cc 293 $this->assertEquals($deletePayment['error_message'], 'Error while deleting participantPayment');
6a488035
TO
294 }
295
296 /**
eceb18cc 297 * Check with valid array.
6a488035 298 */
00be9182 299 public function testPaymentDelete() {
69fccd8f 300 $contributionID = $this->contributionCreate(array(
301 'contact_id' => $this->_contactID,
302 ));
6a488035
TO
303
304 $this->_participantPaymentID = $this->participantPaymentCreate($this->_participantID, $contributionID);
305
306 $params = array(
307 'id' => $this->_participantPaymentID,
6a488035 308 );
69fccd8f 309 $this->callAPIAndDocument('participant_payment', 'delete', $params, __FUNCTION__, __FILE__);
6a488035
TO
310 }
311
6a488035
TO
312 /**
313 * Test civicrm_participantPayment_get - success expected.
314 */
315 public function testGet() {
69fccd8f 316 $contributionID = $this->contributionCreate(array('contact_id' => $this->_contactID3));
317 $this->participantPaymentCreate($this->_participantID4, $contributionID);
6a488035
TO
318
319 //Create Participant Payment record With Values
320 $params = array(
321 'participant_id' => $this->_participantID4,
322 'contribution_id' => $contributionID,
6a488035
TO
323 );
324
0728d72f 325 $result = $this->callAPIAndDocument('participant_payment', 'get', $params, __FUNCTION__, __FILE__);
6a488035
TO
326 $this->assertEquals($result['values'][$result['id']]['participant_id'], $this->_participantID4, 'Check Participant Id');
327 $this->assertEquals($result['values'][$result['id']]['contribution_id'], $contributionID, 'Check Contribution Id');
328 }
329
4cbe18b8 330 /**
c490a46a 331 * @param array $params
4cbe18b8
EM
332 * @param $context
333 */
00be9182 334 public function _checkFinancialRecords($params, $context) {
6a488035
TO
335 $entityParams = array(
336 'entity_id' => $params['id'],
337 'entity_table' => 'civicrm_contribution',
338 );
339 $trxn = current(CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($entityParams));
340 $trxnParams = array(
341 'id' => $trxn['financial_trxn_id'],
342 );
343
344 switch ($context) {
345 case 'online':
346 $compareParams = array(
347 'to_financial_account_id' => 12,
348 'total_amount' => 100,
349 'status_id' => 1,
350 );
351 break;
352
353 case 'offline':
354 $compareParams = array(
355 'to_financial_account_id' => 6,
356 'total_amount' => 100,
357 'status_id' => 1,
358 );
359 break;
6c6e6187 360
6a488035
TO
361 case 'payLater':
362 $compareParams = array(
363 'to_financial_account_id' => 7,
364 'total_amount' => 100,
365 'status_id' => 2,
366 );
367 break;
368 }
369
370 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialTrxn', $trxnParams, $compareParams);
371 $entityParams = array(
372 'financial_trxn_id' => $trxn['financial_trxn_id'],
373 'entity_table' => 'civicrm_financial_item',
374 );
375 $entityTrxn = current(CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($entityParams));
376 $fitemParams = array(
377 'id' => $entityTrxn['entity_id'],
378 );
379 if ($context == 'offline' || $context == 'online') {
380 $compareParams = array(
381 'amount' => 100,
382 'status_id' => 1,
383 'financial_account_id' => 1,
384 );
385 }
386 elseif ($context == 'payLater') {
387 $compareParams = array(
388 'amount' => 100,
389 'status_id' => 3,
390 'financial_account_id' => 1,
391 );
392 }
393 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $fitemParams, $compareParams);
394 }
96025800 395
b677b23b
JP
396 /**
397 * test getParticipantIds() function
398 */
399 public function testGetParticipantIds() {
400 $contributionID = $this->contributionCreate(array('contact_id' => $this->_contactID));
401 $expectedParticipants = array($this->_participantID, $this->_participantID2);
402
403 //Create Participant Payment record With Values
404 foreach ($expectedParticipants as $pid) {
405 $params = array(
406 'participant_id' => $pid,
407 'contribution_id' => $contributionID,
408 );
409 $this->callAPISuccess('participant_payment', 'create', $params);
410 }
411 //Check if all participants are listed.
412 $participants = CRM_Event_BAO_Participant::getParticipantIds($contributionID);
413 $this->checkArrayEquals($expectedParticipants, $participants);
414 //delete created contribution
415 $this->contributionDelete($contributionID);
416 }
417
6a488035 418}