CRM-12274
[civicrm-core.git] / tests / phpunit / api / v3 / ParticipantPaymentTest.php
CommitLineData
6a488035
TO
1<?php
2
3/*
4 +--------------------------------------------------------------------+
5 | CiviCRM version 4.3 |
6 +--------------------------------------------------------------------+
7 | Copyright CiviCRM LLC (c) 2004-2013 |
8 +--------------------------------------------------------------------+
9 | This file is a part of CiviCRM. |
10 | |
11 | CiviCRM is free software; you can copy, modify, and distribute it |
12 | under the terms of the GNU Affero General Public License |
13 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
14 | |
15 | CiviCRM is distributed in the hope that it will be useful, but |
16 | WITHOUT ANY WARRANTY; without even the implied warranty of |
17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
18 | See the GNU Affero General Public License for more details. |
19 | |
20 | You should have received a copy of the GNU Affero General Public |
21 | License and the CiviCRM Licensing Exception along |
22 | with this program; if not, contact CiviCRM LLC |
23 | at info[AT]civicrm[DOT]org. If you have questions about the |
24 | GNU Affero General Public License or the licensing of CiviCRM, |
25 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
26 +--------------------------------------------------------------------+
27 */
28
29require_once 'CiviTest/CiviUnitTestCase.php';
30
31
32/**
33 * Test APIv3 civicrm_participant_* functions
34 *
35 * @package CiviCRM_APIv3
36 * @subpackage API_Event
37 */
38
39class api_v3_ParticipantPaymentTest extends CiviUnitTestCase {
40
41 protected $_apiversion;
42 protected $_contactID;
43 protected $_createdParticipants;
44 protected $_participantID;
45 protected $_eventID;
46 protected $_participantPaymentID;
47 protected $_contributionTypeId;
48 public $_eNoticeCompliant = TRUE;
49
50 function get_info() {
51 return array(
52 'name' => 'Participant Create',
53 'description' => 'Test all Participant Create API methods.',
54 'group' => 'CiviCRM API Tests',
55 );
56 }
57
58 function setUp() {
59 $this->_apiversion = 3;
60 parent::setUp();
61 $tablesToTruncate = array(
62 'civicrm_contribution',
63 'civicrm_contact',
64 );
65 $this->quickCleanup($tablesToTruncate);
66 $event = $this->eventCreate(NULL);
67 $this->_eventID = $event['id'];
68
69 $this->_contactID = $this->individualCreate(NULL);
70
71 $this->_createdParticipants = array();
72 $this->_individualId = $this->individualCreate(NULL);
73
74 $this->_participantID = $this->participantCreate(array('contactID' => $this->_contactID, 'eventID' => $this->_eventID));
75 $this->_contactID2 = $this->individualCreate(NULL);
76 $this->_participantID2 = $this->participantCreate(array('contactID' => $this->_contactID2, 'eventID' => $this->_eventID, 'version' => $this->_apiversion));
77 $this->_participantID3 = $this->participantCreate(array('contactID' => $this->_contactID2, 'eventID' => $this->_eventID, 'version' => $this->_apiversion));
78
79 $this->_contactID3 = $this->individualCreate(NULL);
80 $this->_participantID4 = $this->participantCreate(array('contactID' => $this->_contactID3, 'eventID' => $this->_eventID, 'version' => $this->_apiversion));
81 }
82
83 function tearDown() {
84 $this->eventDelete($this->_eventID);
85 $this->quickCleanup(
86 array(
87 'civicrm_contact',
88 'civicrm_contribution',
89 'civicrm_participant',
90 'civicrm_participant_payment',
91 'civicrm_line_item',
92 'civicrm_financial_item',
93 'civicrm_financial_trxn',
94 'civicrm_entity_financial_trxn',
95 )
96 );
97 $this->contributionTypeDelete();
98 }
99
100 ///////////////// civicrm_participant_payment_create methods
101
102 /**
103 * Test civicrm_participant_payment_create with wrong params type
104 */
105 function testPaymentCreateWrongParamsType() {
106 $params = 'a string';
107 $result = civicrm_api('participant_payment', 'create', $params);
108 $this->assertEquals(1, $result['is_error'], 'In line ' . __LINE__);
109 }
110
111 /**
112 * Test civicrm_participant_payment_create with empty params
113 */
114 function testPaymentCreateEmptyParams() {
115 $params = array();
116 $result = civicrm_api('participant_payment', 'create', $params);
117 $this->assertEquals(1, $result['is_error'], 'In line ' . __LINE__);
118 }
119
120 /**
121 * check without contribution_id
122 */
123 function testPaymentCreateMissingContributionId() {
124 //Without Payment EntityID
125 $params = array(
126 'participant_id' => $this->_participantID,
127 'version' => $this->_apiversion,
128 );
129
130 $participantPayment = civicrm_api('participant_payment', 'create', $params);
131 $this->assertEquals($participantPayment['is_error'], 1);
132 }
133
134 /**
135 * check with valid array
136 */
137 function testPaymentCreate() {
138 //Create Contribution & get contribution ID
139 $contributionID = $this->contributionCreate($this->_contactID);
140
141 //Create Participant Payment record With Values
142 $params = array(
143 'participant_id' => $this->_participantID,
144 'contribution_id' => $contributionID,
145 'version' => $this->_apiversion,
146 );
147
148 $result = civicrm_api('participant_payment', 'create', $params);
149 $this->documentMe($params, $result, __FUNCTION__, __FILE__);
150 $this->assertEquals($result['is_error'], 0, 'in line ' . __LINE__);
151 $this->assertTrue(array_key_exists('id', $result), 'in line ' . __LINE__);
152
153 //delete created contribution
154 $this->contributionDelete($contributionID);
155 }
156
157
158 ///////////////// civicrm_participant_payment_create methods
159
160 /**
161 * Test civicrm_participant_payment_create with wrong params type
162 */
163 function testPaymentUpdateWrongParamsType() {
164 $params = 'a string';
165 $result = civicrm_api('participant_payment', 'create', $params);
166
167 $this->assertEquals(1, $result['is_error'], 'In line ' . __LINE__);
168 $this->assertEquals('Input variable `params` is not an array', $result['error_message'], 'In line ' . __LINE__);
169 }
170
171 /**
172 * check with empty array
173 */
174 function testPaymentUpdateEmpty() {
175 $params = array();
176 $participantPayment = civicrm_api('participant_payment', 'create', $params);
177 $this->assertEquals($participantPayment['is_error'], 1);
178 }
179
180 /**
181 * check with missing participant_id
182 */
183 function testPaymentUpdateMissingParticipantId() {
184 //WithoutParticipantId
185 $params = array(
186 'contribution_id' => '3',
187 'version' => $this->_apiversion,
188 );
189
190 $participantPayment = civicrm_api('participant_payment', 'create', $params);
191 $this->assertEquals($participantPayment['is_error'], 1);
192 }
193
194 /**
195 * check with missing contribution_id
196 */
197 function testPaymentUpdateMissingContributionId() {
198 $params = array(
199 'participant_id' => $this->_participantID,
200 'version' => $this->_apiversion,
201 );
202 $participantPayment = civicrm_api('participant_payment', 'create', $params);
203 $this->assertEquals($participantPayment['is_error'], 1);
204 }
205
206 /**
207 * check financial records for offline Participants
208 */
209 function testPaymentOffline() {
210
211 // create contribution w/o fee
212 $contributionID = $this->contributionCreate($this->_contactID, $this->_contributionTypeId, NULL, NULL, 4, FALSE);
213
214 $this->_participantPaymentID = $this->participantPaymentCreate($this->_participantID, $contributionID);
215 $params = array(
216 'id' => $this->_participantPaymentID,
217 'participant_id' => $this->_participantID,
218 'contribution_id' => $contributionID,
219 'version' => $this->_apiversion,
220 );
221
222 // Update Payment
223 $participantPayment = civicrm_api('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, 'offline');
228 $params = array(
229 'id' => $this->_participantPaymentID,
230 'version' => $this->_apiversion,
231 );
232 $deletePayment = civicrm_api('participant_payment', 'delete', $params);
233 $this->assertEquals($deletePayment['is_error'], 0);
234 }
235
236 /**
237 * check financial records for online Participant
238 */
239 function testPaymentOnline() {
240
241 $paymentProcessor = $this->processorCreate();
242 $pageParams['processor_id'] = $paymentProcessor->id;
243 $contributionPage = $this->contributionPageCreate($pageParams);
244 $contributionParams = array(
245 'contact_id' => $this->_contactID,
246 'contribution_page_id' => $contributionPage['id'],
247 'payment_processor' => $paymentProcessor->id,
248 );
249 $contributionID = $this->onlineContributionCreate($contributionParams, 1);
250
251 $this->_participantPaymentID = $this->participantPaymentCreate($this->_participantID, $contributionID);
252 $params = array(
253 'id' => $this->_participantPaymentID,
254 'participant_id' => $this->_participantID,
255 'contribution_id' => $contributionID,
256 'version' => $this->_apiversion,
257 );
258
259 // Update Payment
260 $participantPayment = civicrm_api('participant_payment', 'create', $params);
261 $this->assertEquals($participantPayment['id'], $this->_participantPaymentID);
262 $this->assertTrue(array_key_exists('id', $participantPayment));
263 // check Financial records
264 $this->_checkFinancialRecords($params, 'online');
265 $params = array(
266 'id' => $this->_participantPaymentID,
267 'version' => $this->_apiversion,
268 );
269 $deletePayment = civicrm_api('participant_payment', 'delete', $params);
270 $this->assertEquals($deletePayment['is_error'], 0);
271 }
272
273 /**
274 * check financial records for online Participant pay later scenario
275 */
276 function testPaymentPayLaterOnline() {
277
278 $paymentProcessor = $this->processorCreate();
279 $pageParams['processor_id'] = $paymentProcessor->id;
280 $pageParams['is_pay_later'] = 1;
281 $contributionPage = $this->contributionPageCreate($pageParams);
282 $contributionParams = array(
283 'contact_id' => $this->_contactID,
284 'contribution_page_id' => $contributionPage['id'],
285 'contribution_status_id' => 2,
286 'is_pay_later' => 1,
287 );
288 $contributionID = $this->onlineContributionCreate($contributionParams, 1);
289
290 $this->_participantPaymentID = $this->participantPaymentCreate($this->_participantID, $contributionID);
291 $params = array(
292 'id' => $this->_participantPaymentID,
293 'participant_id' => $this->_participantID,
294 'contribution_id' => $contributionID,
295 'version' => $this->_apiversion,
296 );
297
298 // Update Payment
299 $participantPayment = civicrm_api('participant_payment', 'create', $params);
300 // check Financial Records
301 $this->_checkFinancialRecords($params, 'payLater');
302 $this->assertEquals($participantPayment['id'], $this->_participantPaymentID);
303 $this->assertTrue(array_key_exists('id', $participantPayment));
304 $params = array(
305 'id' => $this->_participantPaymentID,
306 'version' => $this->_apiversion,
307 );
308 $deletePayment = civicrm_api('participant_payment', 'delete', $params);
309 $this->assertEquals($deletePayment['is_error'], 0);
310 }
311
312 ///////////////// civicrm_participant_payment_delete methods
313
314 /**
315 * Test civicrm_participant_payment_delete with wrong params type
316 */
317 function testPaymentDeleteWrongParamsType() {
318 $params = 'a string';
319 $result = civicrm_api('participant_payment', 'delete', $params);
320 $this->assertEquals(1, $result['is_error'], 'In line ' . __LINE__);
321 }
322
323 /**
324 * check with empty array
325 */
326 function testPaymentDeleteWithEmptyParams() {
327 $params = array('version' => $this->_apiversion);
328 $deletePayment = civicrm_api('participant_payment', 'delete', $params);
329 $this->assertEquals(1, $deletePayment['is_error']);
330 $this->assertEquals('Mandatory key(s) missing from params array: id', $deletePayment['error_message']);
331 }
332
333 /**
334 * check with wrong id
335 */
336 function testPaymentDeleteWithWrongID() {
337 $params = array(
338 'id' => 0,
339 'version' => $this->_apiversion,
340 );
341 $deletePayment = civicrm_api('participant_payment', 'delete', $params);
342 $this->assertEquals($deletePayment['is_error'], 1);
343 $this->assertEquals($deletePayment['error_message'], 'Mandatory key(s) missing from params array: id');
344 }
345
346 /**
347 * check with valid array
348 */
349 function testPaymentDelete() {
350
351 // create contribution
352 $contributionID = $this->contributionCreate($this->_contactID, $this->_contributionTypeId);
353
354 $this->_participantPaymentID = $this->participantPaymentCreate($this->_participantID, $contributionID);
355
356 $params = array(
357 'id' => $this->_participantPaymentID,
358 'version' => $this->_apiversion,
359 );
360
361 $result = civicrm_api('participant_payment', 'delete', $params);
362 $this->documentMe($params, $result, __FUNCTION__, __FILE__);
363 $this->assertEquals($result['is_error'], 0);
364 }
365
366 ///////////////// civicrm_participantPayment_get methods
367
368 /**
369 * Test civicrm_participantPayment_get with wrong params type.
370 */
371 public function testGetWrongParamsType() {
372 $params = 'eeee';
373 $GetWrongParamsType = civicrm_api('participant_payment', 'get', $params);
374 $this->assertEquals($GetWrongParamsType['error_message'], 'Input variable `params` is not an array');
375 }
376
377 /**
378 * Test civicrm_participantPayment_get with empty params.
379 */
380 public function testGetEmptyParams() {
381 $params = array();
382 $GetEmptyParams = civicrm_api('participant_payment', 'get', $params);
383 $this->assertEquals($GetEmptyParams['error_message'], 'Mandatory key(s) missing from params array: version');
384 }
385
386 /**
387 * Test civicrm_participantPayment_get - success expected.
388 */
389 public function testGet() {
390 //Create Contribution & get contribution ID
391 $contributionID = $this->contributionCreate($this->_contactID3, $this->_contributionTypeId);
392 $participantPaymentID = $this->participantPaymentCreate($this->_participantID4, $contributionID);
393
394 //Create Participant Payment record With Values
395 $params = array(
396 'participant_id' => $this->_participantID4,
397 'contribution_id' => $contributionID,
398 'version' => $this->_apiversion,
399 );
400
401 $result = civicrm_api('participant_payment', 'get', $params);
402 $this->documentMe($params, $result, __FUNCTION__, __FILE__);
403 $this->assertEquals($result['values'][$result['id']]['participant_id'], $this->_participantID4, 'Check Participant Id');
404 $this->assertEquals($result['values'][$result['id']]['contribution_id'], $contributionID, 'Check Contribution Id');
405 }
406
407 function _checkFinancialRecords($params, $context) {
408 $entityParams = array(
409 'entity_id' => $params['id'],
410 'entity_table' => 'civicrm_contribution',
411 );
412 $trxn = current(CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($entityParams));
413 $trxnParams = array(
414 'id' => $trxn['financial_trxn_id'],
415 );
416
417 switch ($context) {
418 case 'online':
419 $compareParams = array(
420 'to_financial_account_id' => 12,
421 'total_amount' => 100,
422 'status_id' => 1,
423 );
424 break;
425
426 case 'offline':
427 $compareParams = array(
428 'to_financial_account_id' => 6,
429 'total_amount' => 100,
430 'status_id' => 1,
431 );
432 break;
433 case 'payLater':
434 $compareParams = array(
435 'to_financial_account_id' => 7,
436 'total_amount' => 100,
437 'status_id' => 2,
438 );
439 break;
440 }
441
442 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialTrxn', $trxnParams, $compareParams);
443 $entityParams = array(
444 'financial_trxn_id' => $trxn['financial_trxn_id'],
445 'entity_table' => 'civicrm_financial_item',
446 );
447 $entityTrxn = current(CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($entityParams));
448 $fitemParams = array(
449 'id' => $entityTrxn['entity_id'],
450 );
451 if ($context == 'offline' || $context == 'online') {
452 $compareParams = array(
453 'amount' => 100,
454 'status_id' => 1,
455 'financial_account_id' => 1,
456 );
457 }
458 elseif ($context == 'payLater') {
459 $compareParams = array(
460 'amount' => 100,
461 'status_id' => 3,
462 'financial_account_id' => 1,
463 );
464 }
465 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $fitemParams, $compareParams);
466 }
467}
468