Merge pull request #1005 from GiantRobot/CRM-12804
[civicrm-core.git] / tests / phpunit / api / v3 / ParticipantPaymentTest.php
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
29 require_once 'CiviTest/CiviUnitTestCase.php';
30
31
32 /**
33 * Test APIv3 civicrm_participant_* functions
34 *
35 * @package CiviCRM_APIv3
36 * @subpackage API_Event
37 */
38
39 class 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 = $this->callAPIFailure('participant_payment', 'create', $params);
108 }
109
110 /**
111 * Test civicrm_participant_payment_create with empty params
112 */
113 function testPaymentCreateEmptyParams() {
114 $params = array();
115 $result = $this->callAPIFailure('participant_payment', 'create', $params);
116 }
117
118 /**
119 * check without contribution_id
120 */
121 function testPaymentCreateMissingContributionId() {
122 //Without Payment EntityID
123 $params = array(
124 'participant_id' => $this->_participantID,
125 'version' => $this->_apiversion,
126 );
127
128 $participantPayment = $this->callAPIFailure('participant_payment', 'create', $params);
129 }
130
131 /**
132 * check with valid array
133 */
134 function testPaymentCreate() {
135 //Create Contribution & get contribution ID
136 $contributionID = $this->contributionCreate($this->_contactID);
137
138 //Create Participant Payment record With Values
139 $params = array(
140 'participant_id' => $this->_participantID,
141 'contribution_id' => $contributionID,
142 'version' => $this->_apiversion,
143 );
144
145 $result = civicrm_api('participant_payment', 'create', $params);
146 $this->documentMe($params, $result, __FUNCTION__, __FILE__);
147 $this->assertAPISuccess($result, 'in line ' . __LINE__);
148 $this->assertTrue(array_key_exists('id', $result), 'in line ' . __LINE__);
149
150 //delete created contribution
151 $this->contributionDelete($contributionID);
152 }
153
154
155 ///////////////// civicrm_participant_payment_create methods
156
157 /**
158 * Test civicrm_participant_payment_create with wrong params type
159 */
160 function testPaymentUpdateWrongParamsType() {
161 $params = 'a string';
162 $result = $this->callAPIFailure('participant_payment', 'create', $params);
163 $this->assertEquals('Input variable `params` is not an array', $result['error_message'], 'In line ' . __LINE__);
164 }
165
166 /**
167 * check with empty array
168 */
169 function testPaymentUpdateEmpty() {
170 $params = array();
171 $participantPayment = $this->callAPIFailure('participant_payment', 'create', $params);
172 }
173
174 /**
175 * check with missing participant_id
176 */
177 function testPaymentUpdateMissingParticipantId() {
178 //WithoutParticipantId
179 $params = array(
180 'contribution_id' => '3',
181 'version' => $this->_apiversion,
182 );
183
184 $participantPayment = $this->callAPIFailure('participant_payment', 'create', $params);
185 }
186
187 /**
188 * check with missing contribution_id
189 */
190 function testPaymentUpdateMissingContributionId() {
191 $params = array(
192 'participant_id' => $this->_participantID,
193 'version' => $this->_apiversion,
194 );
195 $participantPayment = $this->callAPIFailure('participant_payment', 'create', $params);
196 }
197
198 /**
199 * check financial records for offline Participants
200 */
201 function testPaymentOffline() {
202
203 // create contribution w/o fee
204 $contributionID = $this->contributionCreate($this->_contactID, $this->_contributionTypeId, NULL, NULL, 4, FALSE);
205
206 $this->_participantPaymentID = $this->participantPaymentCreate($this->_participantID, $contributionID);
207 $params = array(
208 'id' => $this->_participantPaymentID,
209 'participant_id' => $this->_participantID,
210 'contribution_id' => $contributionID,
211 'version' => $this->_apiversion,
212 );
213
214 // Update Payment
215 $participantPayment = civicrm_api('participant_payment', 'create', $params);
216 $this->assertEquals($participantPayment['id'], $this->_participantPaymentID);
217 $this->assertTrue(array_key_exists('id', $participantPayment));
218 // check Financial records
219 $this->_checkFinancialRecords($params, 'offline');
220 $params = array(
221 'id' => $this->_participantPaymentID,
222 'version' => $this->_apiversion,
223 );
224 $deletePayment = civicrm_api('participant_payment', 'delete', $params);
225 $this->assertEquals($deletePayment['is_error'], 0);
226 }
227
228 /**
229 * check financial records for online Participant
230 */
231 function testPaymentOnline() {
232
233 $paymentProcessor = $this->processorCreate();
234 $pageParams['processor_id'] = $paymentProcessor->id;
235 $contributionPage = $this->contributionPageCreate($pageParams);
236 $contributionParams = array(
237 'contact_id' => $this->_contactID,
238 'contribution_page_id' => $contributionPage['id'],
239 'payment_processor' => $paymentProcessor->id,
240 );
241 $contributionID = $this->onlineContributionCreate($contributionParams, 1);
242
243 $this->_participantPaymentID = $this->participantPaymentCreate($this->_participantID, $contributionID);
244 $params = array(
245 'id' => $this->_participantPaymentID,
246 'participant_id' => $this->_participantID,
247 'contribution_id' => $contributionID,
248 'version' => $this->_apiversion,
249 );
250
251 // Update Payment
252 $participantPayment = civicrm_api('participant_payment', 'create', $params);
253 $this->assertEquals($participantPayment['id'], $this->_participantPaymentID);
254 $this->assertTrue(array_key_exists('id', $participantPayment));
255 // check Financial records
256 $this->_checkFinancialRecords($params, 'online');
257 $params = array(
258 'id' => $this->_participantPaymentID,
259 'version' => $this->_apiversion,
260 );
261 $deletePayment = civicrm_api('participant_payment', 'delete', $params);
262 $this->assertEquals($deletePayment['is_error'], 0);
263 }
264
265 /**
266 * check financial records for online Participant pay later scenario
267 */
268 function testPaymentPayLaterOnline() {
269
270 $paymentProcessor = $this->processorCreate();
271 $pageParams['processor_id'] = $paymentProcessor->id;
272 $pageParams['is_pay_later'] = 1;
273 $contributionPage = $this->contributionPageCreate($pageParams);
274 $contributionParams = array(
275 'contact_id' => $this->_contactID,
276 'contribution_page_id' => $contributionPage['id'],
277 'contribution_status_id' => 2,
278 'is_pay_later' => 1,
279 );
280 $contributionID = $this->onlineContributionCreate($contributionParams, 1);
281
282 $this->_participantPaymentID = $this->participantPaymentCreate($this->_participantID, $contributionID);
283 $params = array(
284 'id' => $this->_participantPaymentID,
285 'participant_id' => $this->_participantID,
286 'contribution_id' => $contributionID,
287 'version' => $this->_apiversion,
288 );
289
290 // Update Payment
291 $participantPayment = civicrm_api('participant_payment', 'create', $params);
292 // check Financial Records
293 $this->_checkFinancialRecords($params, 'payLater');
294 $this->assertEquals($participantPayment['id'], $this->_participantPaymentID);
295 $this->assertTrue(array_key_exists('id', $participantPayment));
296 $params = array(
297 'id' => $this->_participantPaymentID,
298 'version' => $this->_apiversion,
299 );
300 $deletePayment = civicrm_api('participant_payment', 'delete', $params);
301 $this->assertEquals($deletePayment['is_error'], 0);
302 }
303
304 ///////////////// civicrm_participant_payment_delete methods
305
306 /**
307 * Test civicrm_participant_payment_delete with wrong params type
308 */
309 function testPaymentDeleteWrongParamsType() {
310 $params = 'a string';
311 $result = $this->callAPIFailure('participant_payment', 'delete', $params);
312 }
313
314 /**
315 * check with empty array
316 */
317 function testPaymentDeleteWithEmptyParams() {
318 $params = array('version' => $this->_apiversion);
319 $deletePayment = $this->callAPIFailure('participant_payment', 'delete', $params);
320 $this->assertEquals('Mandatory key(s) missing from params array: id', $deletePayment['error_message']);
321 }
322
323 /**
324 * check with wrong id
325 */
326 function testPaymentDeleteWithWrongID() {
327 $params = array(
328 'id' => 0,
329 'version' => $this->_apiversion,
330 );
331 $deletePayment = $this->callAPIFailure('participant_payment', 'delete', $params);
332 $this->assertEquals($deletePayment['error_message'], 'Mandatory key(s) missing from params array: id');
333 }
334
335 /**
336 * check with valid array
337 */
338 function testPaymentDelete() {
339
340 // create contribution
341 $contributionID = $this->contributionCreate($this->_contactID, $this->_contributionTypeId);
342
343 $this->_participantPaymentID = $this->participantPaymentCreate($this->_participantID, $contributionID);
344
345 $params = array(
346 'id' => $this->_participantPaymentID,
347 'version' => $this->_apiversion,
348 );
349
350 $result = civicrm_api('participant_payment', 'delete', $params);
351 $this->documentMe($params, $result, __FUNCTION__, __FILE__);
352 $this->assertAPISuccess($result);
353 }
354
355 ///////////////// civicrm_participantPayment_get methods
356
357 /**
358 * Test civicrm_participantPayment_get with wrong params type.
359 */
360 public function testGetWrongParamsType() {
361 $params = 'eeee';
362 $GetWrongParamsType = civicrm_api('participant_payment', 'get', $params);
363 $this->assertEquals($GetWrongParamsType['error_message'], 'Input variable `params` is not an array');
364 }
365
366 /**
367 * Test civicrm_participantPayment_get - success expected.
368 */
369 public function testGet() {
370 //Create Contribution & get contribution ID
371 $contributionID = $this->contributionCreate($this->_contactID3, $this->_contributionTypeId);
372 $participantPaymentID = $this->participantPaymentCreate($this->_participantID4, $contributionID);
373
374 //Create Participant Payment record With Values
375 $params = array(
376 'participant_id' => $this->_participantID4,
377 'contribution_id' => $contributionID,
378 'version' => $this->_apiversion,
379 );
380
381 $result = civicrm_api('participant_payment', 'get', $params);
382 $this->documentMe($params, $result, __FUNCTION__, __FILE__);
383 $this->assertEquals($result['values'][$result['id']]['participant_id'], $this->_participantID4, 'Check Participant Id');
384 $this->assertEquals($result['values'][$result['id']]['contribution_id'], $contributionID, 'Check Contribution Id');
385 }
386
387 function _checkFinancialRecords($params, $context) {
388 $entityParams = array(
389 'entity_id' => $params['id'],
390 'entity_table' => 'civicrm_contribution',
391 );
392 $trxn = current(CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($entityParams));
393 $trxnParams = array(
394 'id' => $trxn['financial_trxn_id'],
395 );
396
397 switch ($context) {
398 case 'online':
399 $compareParams = array(
400 'to_financial_account_id' => 12,
401 'total_amount' => 100,
402 'status_id' => 1,
403 );
404 break;
405
406 case 'offline':
407 $compareParams = array(
408 'to_financial_account_id' => 6,
409 'total_amount' => 100,
410 'status_id' => 1,
411 );
412 break;
413 case 'payLater':
414 $compareParams = array(
415 'to_financial_account_id' => 7,
416 'total_amount' => 100,
417 'status_id' => 2,
418 );
419 break;
420 }
421
422 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialTrxn', $trxnParams, $compareParams);
423 $entityParams = array(
424 'financial_trxn_id' => $trxn['financial_trxn_id'],
425 'entity_table' => 'civicrm_financial_item',
426 );
427 $entityTrxn = current(CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($entityParams));
428 $fitemParams = array(
429 'id' => $entityTrxn['entity_id'],
430 );
431 if ($context == 'offline' || $context == 'online') {
432 $compareParams = array(
433 'amount' => 100,
434 'status_id' => 1,
435 'financial_account_id' => 1,
436 );
437 }
438 elseif ($context == 'payLater') {
439 $compareParams = array(
440 'amount' => 100,
441 'status_id' => 3,
442 'financial_account_id' => 1,
443 );
444 }
445 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $fitemParams, $compareParams);
446 }
447 }
448