Coding standards cleanup sprint.
[civicrm-core.git] / tests / phpunit / api / v3 / ParticipantPaymentTest.php
CommitLineData
6a488035 1<?php
0728d72f 2/**
92915c55
TO
3 * +--------------------------------------------------------------------+
4 * | CiviCRM version 4.6 |
5 * +--------------------------------------------------------------------+
6 * | Copyright CiviCRM LLC (c) 2004-2014 |
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
28require_once 'CiviTest/CiviUnitTestCase.php';
29
30
31/**
32 * Test APIv3 civicrm_participant_* functions
33 *
6c6e6187
TO
34 * @package CiviCRM_APIv3
35 * @subpackage API_Event
6a488035 36 */
6a488035
TO
37class api_v3_ParticipantPaymentTest extends CiviUnitTestCase {
38
0728d72f 39 protected $_apiversion = 3;
6a488035
TO
40 protected $_contactID;
41 protected $_createdParticipants;
42 protected $_participantID;
43 protected $_eventID;
44 protected $_participantPaymentID;
45 protected $_contributionTypeId;
b7c9bc4c 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();
6a488035 55
92915c55
TO
56 $this->_participantID = $this->participantCreate(array(
57 'contactID' => $this->_contactID,
58 'eventID' => $this->_eventID
59 ));
e4d5f1e2 60 $this->_contactID2 = $this->individualCreate();
92915c55
TO
61 $this->_participantID2 = $this->participantCreate(array(
62 'contactID' => $this->_contactID2,
63 'eventID' => $this->_eventID
64 ));
65 $this->_participantID3 = $this->participantCreate(array(
66 'contactID' => $this->_contactID2,
67 'eventID' => $this->_eventID
68 ));
6a488035 69
e4d5f1e2 70 $this->_contactID3 = $this->individualCreate();
92915c55
TO
71 $this->_participantID4 = $this->participantCreate(array(
72 'contactID' => $this->_contactID3,
73 'eventID' => $this->_eventID
74 ));
6a488035
TO
75 }
76
6a488035
TO
77 ///////////////// civicrm_participant_payment_create methods
78
79 /**
80 * Test civicrm_participant_payment_create with wrong params type
81 */
00be9182 82 public function testPaymentCreateWrongParamsType() {
6a488035 83 $params = 'a string';
d0e1eff2 84 $result = $this->callAPIFailure('participant_payment', 'create', $params);
6a488035
TO
85 }
86
87 /**
88 * Test civicrm_participant_payment_create with empty params
89 */
00be9182 90 public function testPaymentCreateEmptyParams() {
6a488035 91 $params = array();
d0e1eff2 92 $result = $this->callAPIFailure('participant_payment', 'create', $params);
6a488035
TO
93 }
94
95 /**
100fef9d 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 );
6a488035 103
d0e1eff2 104 $participantPayment = $this->callAPIFailure('participant_payment', 'create', $params);
6a488035
TO
105 }
106
107 /**
100fef9d 108 * Check with valid array
6a488035 109 */
00be9182 110 public function testPaymentCreate() {
6a488035
TO
111 //Create Contribution & get contribution ID
112 $contributionID = $this->contributionCreate($this->_contactID);
113
114 //Create Participant Payment record With Values
115 $params = array(
116 'participant_id' => $this->_participantID,
117 'contribution_id' => $contributionID,
6a488035
TO
118 );
119
0728d72f 120 $result = $this->callAPIAndDocument('participant_payment', 'create', $params, __FUNCTION__, __FILE__);
6a488035
TO
121 $this->assertTrue(array_key_exists('id', $result), 'in line ' . __LINE__);
122
123 //delete created contribution
124 $this->contributionDelete($contributionID);
125 }
126
127
128 ///////////////// civicrm_participant_payment_create methods
129
130 /**
131 * Test civicrm_participant_payment_create with wrong params type
132 */
00be9182 133 public function testPaymentUpdateWrongParamsType() {
6a488035 134 $params = 'a string';
d0e1eff2 135 $result = $this->callAPIFailure('participant_payment', 'create', $params);
6a488035
TO
136 $this->assertEquals('Input variable `params` is not an array', $result['error_message'], 'In line ' . __LINE__);
137 }
138
139 /**
100fef9d 140 * Check with empty array
6a488035 141 */
00be9182 142 public function testPaymentUpdateEmpty() {
6a488035 143 $params = array();
d0e1eff2 144 $participantPayment = $this->callAPIFailure('participant_payment', 'create', $params);
6a488035
TO
145 }
146
147 /**
100fef9d 148 * Check with missing participant_id
6a488035 149 */
00be9182 150 public function testPaymentUpdateMissingParticipantId() {
6a488035
TO
151 //WithoutParticipantId
152 $params = array(
92915c55
TO
153 'contribution_id' => '3',
154 );
6a488035 155
d0e1eff2 156 $participantPayment = $this->callAPIFailure('participant_payment', 'create', $params);
6a488035
TO
157 }
158
159 /**
100fef9d 160 * Check with missing contribution_id
6a488035 161 */
00be9182 162 public function testPaymentUpdateMissingContributionId() {
6a488035 163 $params = array(
92915c55
TO
164 'participant_id' => $this->_participantID,
165 );
d0e1eff2 166 $participantPayment = $this->callAPIFailure('participant_payment', 'create', $params);
6a488035
TO
167 }
168
169 /**
100fef9d 170 * Check financial records for offline Participants
6a488035 171 */
00be9182 172 public function testPaymentOffline() {
6a488035
TO
173
174 // create contribution w/o fee
175 $contributionID = $this->contributionCreate($this->_contactID, $this->_contributionTypeId, NULL, NULL, 4, FALSE);
176
177 $this->_participantPaymentID = $this->participantPaymentCreate($this->_participantID, $contributionID);
178 $params = array(
179 'id' => $this->_participantPaymentID,
180 'participant_id' => $this->_participantID,
92915c55
TO
181 'contribution_id' => $contributionID,
182 );
6a488035
TO
183
184 // Update Payment
0728d72f 185 $participantPayment = $this->callAPISuccess('participant_payment', 'create', $params);
6a488035
TO
186 $this->assertEquals($participantPayment['id'], $this->_participantPaymentID);
187 $this->assertTrue(array_key_exists('id', $participantPayment));
188 // check Financial records
189 $this->_checkFinancialRecords($params, 'offline');
190 $params = array(
191 'id' => $this->_participantPaymentID,
6a488035 192 );
0728d72f 193 $deletePayment = $this->callAPISuccess('participant_payment', 'delete', $params);
6a488035
TO
194 }
195
196 /**
100fef9d 197 * Check financial records for online Participant
6a488035 198 */
00be9182 199 public function testPaymentOnline() {
6a488035
TO
200
201 $paymentProcessor = $this->processorCreate();
202 $pageParams['processor_id'] = $paymentProcessor->id;
203 $contributionPage = $this->contributionPageCreate($pageParams);
204 $contributionParams = array(
92915c55
TO
205 'contact_id' => $this->_contactID,
206 'contribution_page_id' => $contributionPage['id'],
207 'payment_processor' => $paymentProcessor->id,
6a488035
TO
208 );
209 $contributionID = $this->onlineContributionCreate($contributionParams, 1);
210
211 $this->_participantPaymentID = $this->participantPaymentCreate($this->_participantID, $contributionID);
212 $params = array(
213 'id' => $this->_participantPaymentID,
214 'participant_id' => $this->_participantID,
92915c55
TO
215 'contribution_id' => $contributionID,
216 );
6a488035
TO
217
218 // Update Payment
0728d72f 219 $participantPayment = $this->callAPISuccess('participant_payment', 'create', $params);
6a488035
TO
220 $this->assertEquals($participantPayment['id'], $this->_participantPaymentID);
221 $this->assertTrue(array_key_exists('id', $participantPayment));
222 // check Financial records
223 $this->_checkFinancialRecords($params, 'online');
224 $params = array(
225 'id' => $this->_participantPaymentID,
6a488035 226 );
0728d72f 227 $deletePayment = $this->callAPISuccess('participant_payment', 'delete', $params);
6a488035
TO
228 }
229
230 /**
100fef9d 231 * Check financial records for online Participant pay later scenario
6a488035 232 */
00be9182 233 public function testPaymentPayLaterOnline() {
6a488035
TO
234
235 $paymentProcessor = $this->processorCreate();
236 $pageParams['processor_id'] = $paymentProcessor->id;
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,
244 );
245 $contributionID = $this->onlineContributionCreate($contributionParams, 1);
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 );
0728d72f 263 $deletePayment = $this->callAPISuccess('participant_payment', 'delete', $params);
6a488035
TO
264 }
265
266 ///////////////// civicrm_participant_payment_delete methods
267
268 /**
269 * Test civicrm_participant_payment_delete with wrong params type
270 */
00be9182 271 public function testPaymentDeleteWrongParamsType() {
6a488035 272 $params = 'a string';
d0e1eff2 273 $result = $this->callAPIFailure('participant_payment', 'delete', $params);
6a488035
TO
274 }
275
276 /**
100fef9d 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 /**
100fef9d 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 /**
100fef9d 297 * Check with valid array
6a488035 298 */
00be9182 299 public function testPaymentDelete() {
6a488035
TO
300
301 // create contribution
302 $contributionID = $this->contributionCreate($this->_contactID, $this->_contributionTypeId);
303
304 $this->_participantPaymentID = $this->participantPaymentCreate($this->_participantID, $contributionID);
305
306 $params = array(
307 'id' => $this->_participantPaymentID,
6a488035
TO
308 );
309
0728d72f 310 $result = $this->callAPIAndDocument('participant_payment', 'delete', $params, __FUNCTION__, __FILE__);
6a488035
TO
311 }
312
313 ///////////////// civicrm_participantPayment_get methods
6a488035
TO
314 /**
315 * Test civicrm_participantPayment_get - success expected.
316 */
317 public function testGet() {
318 //Create Contribution & get contribution ID
319 $contributionID = $this->contributionCreate($this->_contactID3, $this->_contributionTypeId);
320 $participantPaymentID = $this->participantPaymentCreate($this->_participantID4, $contributionID);
321
322 //Create Participant Payment record With Values
323 $params = array(
324 'participant_id' => $this->_participantID4,
325 'contribution_id' => $contributionID,
6a488035
TO
326 );
327
0728d72f 328 $result = $this->callAPIAndDocument('participant_payment', 'get', $params, __FUNCTION__, __FILE__);
6a488035
TO
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');
331 }
332
4cbe18b8 333 /**
c490a46a 334 * @param array $params
4cbe18b8
EM
335 * @param $context
336 */
00be9182 337 public function _checkFinancialRecords($params, $context) {
6a488035
TO
338 $entityParams = array(
339 'entity_id' => $params['id'],
340 'entity_table' => 'civicrm_contribution',
341 );
342 $trxn = current(CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($entityParams));
343 $trxnParams = array(
344 'id' => $trxn['financial_trxn_id'],
345 );
346
347 switch ($context) {
348 case 'online':
349 $compareParams = array(
350 'to_financial_account_id' => 12,
351 'total_amount' => 100,
352 'status_id' => 1,
353 );
354 break;
355
356 case 'offline':
357 $compareParams = array(
358 'to_financial_account_id' => 6,
359 'total_amount' => 100,
360 'status_id' => 1,
361 );
362 break;
6c6e6187 363
6a488035
TO
364 case 'payLater':
365 $compareParams = array(
366 'to_financial_account_id' => 7,
367 'total_amount' => 100,
368 'status_id' => 2,
369 );
370 break;
371 }
372
373 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialTrxn', $trxnParams, $compareParams);
374 $entityParams = array(
375 'financial_trxn_id' => $trxn['financial_trxn_id'],
376 'entity_table' => 'civicrm_financial_item',
377 );
378 $entityTrxn = current(CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($entityParams));
379 $fitemParams = array(
380 'id' => $entityTrxn['entity_id'],
381 );
382 if ($context == 'offline' || $context == 'online') {
383 $compareParams = array(
384 'amount' => 100,
385 'status_id' => 1,
386 'financial_account_id' => 1,
387 );
388 }
389 elseif ($context == 'payLater') {
390 $compareParams = array(
391 'amount' => 100,
392 'status_id' => 3,
393 'financial_account_id' => 1,
394 );
395 }
396 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $fitemParams, $compareParams);
397 }
398}