4 +--------------------------------------------------------------------+
5 | CiviCRM version 4.3 |
6 +--------------------------------------------------------------------+
7 | Copyright CiviCRM LLC (c) 2004-2013 |
8 +--------------------------------------------------------------------+
9 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
29 require_once 'CiviTest/CiviUnitTestCase.php';
33 * Test APIv3 civicrm_participant_* functions
35 * @package CiviCRM_APIv3
36 * @subpackage API_Event
39 class api_v3_ParticipantPaymentTest
extends CiviUnitTestCase
{
41 protected $_apiversion;
42 protected $_contactID;
43 protected $_createdParticipants;
44 protected $_participantID;
46 protected $_participantPaymentID;
47 protected $_contributionTypeId;
48 public $_eNoticeCompliant = TRUE;
52 'name' => 'Participant Create',
53 'description' => 'Test all Participant Create API methods.',
54 'group' => 'CiviCRM API Tests',
59 $this->_apiversion
= 3;
61 $tablesToTruncate = array(
62 'civicrm_contribution',
65 $this->quickCleanup($tablesToTruncate);
66 $event = $this->eventCreate(NULL);
67 $this->_eventID
= $event['id'];
69 $this->_contactID
= $this->individualCreate(NULL);
71 $this->_createdParticipants
= array();
72 $this->_individualId
= $this->individualCreate(NULL);
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
));
79 $this->_contactID3
= $this->individualCreate(NULL);
80 $this->_participantID4
= $this->participantCreate(array('contactID' => $this->_contactID3
, 'eventID' => $this->_eventID
, 'version' => $this->_apiversion
));
84 $this->eventDelete($this->_eventID
);
88 'civicrm_contribution',
89 'civicrm_participant',
90 'civicrm_participant_payment',
92 'civicrm_financial_item',
93 'civicrm_financial_trxn',
94 'civicrm_entity_financial_trxn',
97 $this->contributionTypeDelete();
100 ///////////////// civicrm_participant_payment_create methods
103 * Test civicrm_participant_payment_create with wrong params type
105 function testPaymentCreateWrongParamsType() {
106 $params = 'a string';
107 $result = $this->callAPIFailure('participant_payment', 'create', $params);
111 * Test civicrm_participant_payment_create with empty params
113 function testPaymentCreateEmptyParams() {
115 $result = $this->callAPIFailure('participant_payment', 'create', $params);
119 * check without contribution_id
121 function testPaymentCreateMissingContributionId() {
122 //Without Payment EntityID
124 'participant_id' => $this->_participantID
,
125 'version' => $this->_apiversion
,
128 $participantPayment = $this->callAPIFailure('participant_payment', 'create', $params);
132 * check with valid array
134 function testPaymentCreate() {
135 //Create Contribution & get contribution ID
136 $contributionID = $this->contributionCreate($this->_contactID
);
138 //Create Participant Payment record With Values
140 'participant_id' => $this->_participantID
,
141 'contribution_id' => $contributionID,
142 'version' => $this->_apiversion
,
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__
);
150 //delete created contribution
151 $this->contributionDelete($contributionID);
155 ///////////////// civicrm_participant_payment_create methods
158 * Test civicrm_participant_payment_create with wrong params type
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__
);
167 * check with empty array
169 function testPaymentUpdateEmpty() {
171 $participantPayment = $this->callAPIFailure('participant_payment', 'create', $params);
175 * check with missing participant_id
177 function testPaymentUpdateMissingParticipantId() {
178 //WithoutParticipantId
180 'contribution_id' => '3',
181 'version' => $this->_apiversion
,
184 $participantPayment = $this->callAPIFailure('participant_payment', 'create', $params);
188 * check with missing contribution_id
190 function testPaymentUpdateMissingContributionId() {
192 'participant_id' => $this->_participantID
,
193 'version' => $this->_apiversion
,
195 $participantPayment = $this->callAPIFailure('participant_payment', 'create', $params);
199 * check financial records for offline Participants
201 function testPaymentOffline() {
203 // create contribution w/o fee
204 $contributionID = $this->contributionCreate($this->_contactID
, $this->_contributionTypeId
, NULL, NULL, 4, FALSE);
206 $this->_participantPaymentID
= $this->participantPaymentCreate($this->_participantID
, $contributionID);
208 'id' => $this->_participantPaymentID
,
209 'participant_id' => $this->_participantID
,
210 'contribution_id' => $contributionID,
211 'version' => $this->_apiversion
,
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');
221 'id' => $this->_participantPaymentID
,
222 'version' => $this->_apiversion
,
224 $deletePayment = civicrm_api('participant_payment', 'delete', $params);
225 $this->assertEquals($deletePayment['is_error'], 0);
229 * check financial records for online Participant
231 function testPaymentOnline() {
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
,
241 $contributionID = $this->onlineContributionCreate($contributionParams, 1);
243 $this->_participantPaymentID
= $this->participantPaymentCreate($this->_participantID
, $contributionID);
245 'id' => $this->_participantPaymentID
,
246 'participant_id' => $this->_participantID
,
247 'contribution_id' => $contributionID,
248 'version' => $this->_apiversion
,
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');
258 'id' => $this->_participantPaymentID
,
259 'version' => $this->_apiversion
,
261 $deletePayment = civicrm_api('participant_payment', 'delete', $params);
262 $this->assertEquals($deletePayment['is_error'], 0);
266 * check financial records for online Participant pay later scenario
268 function testPaymentPayLaterOnline() {
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,
280 $contributionID = $this->onlineContributionCreate($contributionParams, 1);
282 $this->_participantPaymentID
= $this->participantPaymentCreate($this->_participantID
, $contributionID);
284 'id' => $this->_participantPaymentID
,
285 'participant_id' => $this->_participantID
,
286 'contribution_id' => $contributionID,
287 'version' => $this->_apiversion
,
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));
297 'id' => $this->_participantPaymentID
,
298 'version' => $this->_apiversion
,
300 $deletePayment = civicrm_api('participant_payment', 'delete', $params);
301 $this->assertEquals($deletePayment['is_error'], 0);
304 ///////////////// civicrm_participant_payment_delete methods
307 * Test civicrm_participant_payment_delete with wrong params type
309 function testPaymentDeleteWrongParamsType() {
310 $params = 'a string';
311 $result = $this->callAPIFailure('participant_payment', 'delete', $params);
315 * check with empty array
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']);
324 * check with wrong id
326 function testPaymentDeleteWithWrongID() {
329 'version' => $this->_apiversion
,
331 $deletePayment = $this->callAPIFailure('participant_payment', 'delete', $params);
332 $this->assertEquals($deletePayment['error_message'], 'Mandatory key(s) missing from params array: id');
336 * check with valid array
338 function testPaymentDelete() {
340 // create contribution
341 $contributionID = $this->contributionCreate($this->_contactID
, $this->_contributionTypeId
);
343 $this->_participantPaymentID
= $this->participantPaymentCreate($this->_participantID
, $contributionID);
346 'id' => $this->_participantPaymentID
,
347 'version' => $this->_apiversion
,
350 $result = civicrm_api('participant_payment', 'delete', $params);
351 $this->documentMe($params, $result, __FUNCTION__
, __FILE__
);
352 $this->assertAPISuccess($result);
355 ///////////////// civicrm_participantPayment_get methods
358 * Test civicrm_participantPayment_get with wrong params type.
360 public function testGetWrongParamsType() {
362 $GetWrongParamsType = civicrm_api('participant_payment', 'get', $params);
363 $this->assertEquals($GetWrongParamsType['error_message'], 'Input variable `params` is not an array');
367 * Test civicrm_participantPayment_get - success expected.
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);
374 //Create Participant Payment record With Values
376 'participant_id' => $this->_participantID4
,
377 'contribution_id' => $contributionID,
378 'version' => $this->_apiversion
,
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');
387 function _checkFinancialRecords($params, $context) {
388 $entityParams = array(
389 'entity_id' => $params['id'],
390 'entity_table' => 'civicrm_contribution',
392 $trxn = current(CRM_Financial_BAO_FinancialItem
::retrieveEntityFinancialTrxn($entityParams));
394 'id' => $trxn['financial_trxn_id'],
399 $compareParams = array(
400 'to_financial_account_id' => 12,
401 'total_amount' => 100,
407 $compareParams = array(
408 'to_financial_account_id' => 6,
409 'total_amount' => 100,
414 $compareParams = array(
415 'to_financial_account_id' => 7,
416 'total_amount' => 100,
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',
427 $entityTrxn = current(CRM_Financial_BAO_FinancialItem
::retrieveEntityFinancialTrxn($entityParams));
428 $fitemParams = array(
429 'id' => $entityTrxn['entity_id'],
431 if ($context == 'offline' ||
$context == 'online') {
432 $compareParams = array(
435 'financial_account_id' => 1,
438 elseif ($context == 'payLater') {
439 $compareParams = array(
442 'financial_account_id' => 1,
445 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $fitemParams, $compareParams);