Merge pull request #5536 from totten/4.5-httpclient
[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-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 $_contributionTypeId;
46
47 public function setUp() {
48 parent::setUp();
49 $this->useTransaction(TRUE);
50 $event = $this->eventCreate(NULL);
51 $this->_eventID = $event['id'];
52 $this->_contactID = $this->individualCreate();
53 $this->_createdParticipants = array();
54 $this->_individualId = $this->individualCreate();
55
56 $this->_participantID = $this->participantCreate(array(
57 'contactID' => $this->_contactID,
58 'eventID' => $this->_eventID,
59 ));
60 $this->_contactID2 = $this->individualCreate();
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 ));
69
70 $this->_contactID3 = $this->individualCreate();
71 $this->_participantID4 = $this->participantCreate(array(
72 'contactID' => $this->_contactID3,
73 'eventID' => $this->_eventID,
74 ));
75 }
76
77 ///////////////// civicrm_participant_payment_create methods
78
79 /**
80 * Test civicrm_participant_payment_create with wrong params type.
81 */
82 public function testPaymentCreateWrongParamsType() {
83 $params = 'a string';
84 $result = $this->callAPIFailure('participant_payment', 'create', $params);
85 }
86
87 /**
88 * Test civicrm_participant_payment_create with empty params.
89 */
90 public function testPaymentCreateEmptyParams() {
91 $params = array();
92 $result = $this->callAPIFailure('participant_payment', 'create', $params);
93 }
94
95 /**
96 * Check without contribution_id.
97 */
98 public function testPaymentCreateMissingContributionId() {
99 //Without Payment EntityID
100 $params = array(
101 'participant_id' => $this->_participantID,
102 );
103
104 $participantPayment = $this->callAPIFailure('participant_payment', 'create', $params);
105 }
106
107 /**
108 * Check with valid array.
109 */
110 public function testPaymentCreate() {
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,
118 );
119
120 $result = $this->callAPIAndDocument('participant_payment', 'create', $params, __FUNCTION__, __FILE__);
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 */
133 public function testPaymentUpdateWrongParamsType() {
134 $params = 'a string';
135 $result = $this->callAPIFailure('participant_payment', 'create', $params);
136 $this->assertEquals('Input variable `params` is not an array', $result['error_message'], 'In line ' . __LINE__);
137 }
138
139 /**
140 * Check with empty array.
141 */
142 public function testPaymentUpdateEmpty() {
143 $this->callAPIFailure('participant_payment', 'create', array());
144 }
145
146 /**
147 * Check with missing participant_id.
148 */
149 public function testPaymentUpdateMissingParticipantId() {
150 $params = array(
151 'contribution_id' => '3',
152 );
153 $this->callAPIFailure('participant_payment', 'create', $params);
154 }
155
156 /**
157 * Check with missing contribution_id.
158 */
159 public function testPaymentUpdateMissingContributionId() {
160 $params = array(
161 'participant_id' => $this->_participantID,
162 );
163 $participantPayment = $this->callAPIFailure('participant_payment', 'create', $params);
164 }
165
166 /**
167 * Check financial records for offline Participants.
168 */
169 public function testPaymentOffline() {
170
171 // create contribution w/o fee
172 $contributionID = $this->contributionCreate($this->_contactID, $this->_contributionTypeId, NULL, NULL, 4, FALSE);
173
174 $this->_participantPaymentID = $this->participantPaymentCreate($this->_participantID, $contributionID);
175 $params = array(
176 'id' => $this->_participantPaymentID,
177 'participant_id' => $this->_participantID,
178 'contribution_id' => $contributionID,
179 );
180
181 // Update Payment
182 $participantPayment = $this->callAPISuccess('participant_payment', 'create', $params);
183 $this->assertEquals($participantPayment['id'], $this->_participantPaymentID);
184 $this->assertTrue(array_key_exists('id', $participantPayment));
185 // check Financial records
186 $this->_checkFinancialRecords($params, 'offline');
187 $params = array(
188 'id' => $this->_participantPaymentID,
189 );
190 $deletePayment = $this->callAPISuccess('participant_payment', 'delete', $params);
191 }
192
193 /**
194 * Check financial records for online Participant.
195 */
196 public function testPaymentOnline() {
197
198 $paymentProcessor = $this->processorCreate();
199 $pageParams['processor_id'] = $paymentProcessor->id;
200 $contributionPage = $this->contributionPageCreate($pageParams);
201 $contributionParams = array(
202 'contact_id' => $this->_contactID,
203 'contribution_page_id' => $contributionPage['id'],
204 'payment_processor' => $paymentProcessor->id,
205 );
206 $contributionID = $this->onlineContributionCreate($contributionParams, 1);
207
208 $this->_participantPaymentID = $this->participantPaymentCreate($this->_participantID, $contributionID);
209 $params = array(
210 'id' => $this->_participantPaymentID,
211 'participant_id' => $this->_participantID,
212 'contribution_id' => $contributionID,
213 );
214
215 // Update Payment
216 $participantPayment = $this->callAPISuccess('participant_payment', 'create', $params);
217 $this->assertEquals($participantPayment['id'], $this->_participantPaymentID);
218 $this->assertTrue(array_key_exists('id', $participantPayment));
219 // check Financial records
220 $this->_checkFinancialRecords($params, 'online');
221 $params = array(
222 'id' => $this->_participantPaymentID,
223 );
224 $deletePayment = $this->callAPISuccess('participant_payment', 'delete', $params);
225 }
226
227 /**
228 * Check financial records for online Participant pay later scenario.
229 */
230 public function testPaymentPayLaterOnline() {
231
232 $paymentProcessor = $this->processorCreate();
233 $pageParams['processor_id'] = $paymentProcessor->id;
234 $pageParams['is_pay_later'] = 1;
235 $contributionPage = $this->contributionPageCreate($pageParams);
236 $contributionParams = array(
237 'contact_id' => $this->_contactID,
238 'contribution_page_id' => $contributionPage['id'],
239 'contribution_status_id' => 2,
240 'is_pay_later' => 1,
241 );
242 $contributionID = $this->onlineContributionCreate($contributionParams, 1);
243
244 $this->_participantPaymentID = $this->participantPaymentCreate($this->_participantID, $contributionID);
245 $params = array(
246 'id' => $this->_participantPaymentID,
247 'participant_id' => $this->_participantID,
248 'contribution_id' => $contributionID,
249 );
250
251 // Update Payment
252 $participantPayment = $this->callAPISuccess('participant_payment', 'create', $params);
253 // check Financial Records
254 $this->_checkFinancialRecords($params, 'payLater');
255 $this->assertEquals($participantPayment['id'], $this->_participantPaymentID);
256 $this->assertTrue(array_key_exists('id', $participantPayment));
257 $params = array(
258 'id' => $this->_participantPaymentID,
259 );
260 $deletePayment = $this->callAPISuccess('participant_payment', 'delete', $params);
261 }
262
263
264 /**
265 * Test civicrm_participant_payment_delete with wrong params type.
266 */
267 public function testPaymentDeleteWrongParamsType() {
268 $params = 'a string';
269 $this->callAPIFailure('participant_payment', 'delete', $params);
270 }
271
272 /**
273 * Check with empty array.
274 */
275 public function testPaymentDeleteWithEmptyParams() {
276 $params = array();
277 $deletePayment = $this->callAPIFailure('participant_payment', 'delete', $params);
278 $this->assertEquals('Mandatory key(s) missing from params array: id', $deletePayment['error_message']);
279 }
280
281 /**
282 * Check with wrong id.
283 */
284 public function testPaymentDeleteWithWrongID() {
285 $params = array(
286 'id' => 0,
287 );
288 $deletePayment = $this->callAPIFailure('participant_payment', 'delete', $params);
289 $this->assertEquals($deletePayment['error_message'], 'Error while deleting participantPayment');
290 }
291
292 /**
293 * Check with valid array.
294 */
295 public function testPaymentDelete() {
296
297 // create contribution
298 $contributionID = $this->contributionCreate($this->_contactID, $this->_contributionTypeId);
299
300 $this->_participantPaymentID = $this->participantPaymentCreate($this->_participantID, $contributionID);
301
302 $params = array(
303 'id' => $this->_participantPaymentID,
304 );
305
306 $result = $this->callAPIAndDocument('participant_payment', 'delete', $params, __FUNCTION__, __FILE__);
307 }
308
309 ///////////////// civicrm_participantPayment_get methods
310 /**
311 * Test civicrm_participantPayment_get - success expected.
312 */
313 public function testGet() {
314 //Create Contribution & get contribution ID
315 $contributionID = $this->contributionCreate($this->_contactID3, $this->_contributionTypeId);
316 $participantPaymentID = $this->participantPaymentCreate($this->_participantID4, $contributionID);
317
318 //Create Participant Payment record With Values
319 $params = array(
320 'participant_id' => $this->_participantID4,
321 'contribution_id' => $contributionID,
322 );
323
324 $result = $this->callAPIAndDocument('participant_payment', 'get', $params, __FUNCTION__, __FILE__);
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
329 /**
330 * @param array $params
331 * @param $context
332 */
333 public function _checkFinancialRecords($params, $context) {
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;
359
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 }
394
395 }