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