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