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