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