3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.4 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
28 require_once 'CiviTest/CiviUnitTestCase.php';
32 * Test APIv3 civicrm_participant_* functions
34 * @package CiviCRM_APIv3
35 * @subpackage API_Event
38 class api_v3_ParticipantPaymentTest
extends CiviUnitTestCase
{
40 protected $_apiversion = 3;
41 protected $_contactID;
42 protected $_createdParticipants;
43 protected $_participantID;
45 protected $_participantPaymentID;
46 protected $_contributionTypeId;
47 public $_eNoticeCompliant = TRUE;
51 'name' => 'Participant Create',
52 'description' => 'Test all Participant Create API methods.',
53 'group' => 'CiviCRM API Tests',
59 $tablesToTruncate = array(
60 'civicrm_contribution',
63 $this->quickCleanup($tablesToTruncate);
64 $event = $this->eventCreate(NULL);
65 $this->_eventID
= $event['id'];
66 $this->_contactID
= $this->individualCreate();
67 $this->_createdParticipants
= array();
68 $this->_individualId
= $this->individualCreate();
70 $this->_participantID
= $this->participantCreate(array('contactID' => $this->_contactID
, 'eventID' => $this->_eventID
));
71 $this->_contactID2
= $this->individualCreate();
72 $this->_participantID2
= $this->participantCreate(array('contactID' => $this->_contactID2
, 'eventID' => $this->_eventID
));
73 $this->_participantID3
= $this->participantCreate(array('contactID' => $this->_contactID2
, 'eventID' => $this->_eventID
));
75 $this->_contactID3
= $this->individualCreate();
76 $this->_participantID4
= $this->participantCreate(array('contactID' => $this->_contactID3
, 'eventID' => $this->_eventID
));
80 $this->eventDelete($this->_eventID
);
84 'civicrm_contribution',
85 'civicrm_participant',
86 'civicrm_participant_payment',
88 'civicrm_financial_item',
89 'civicrm_financial_trxn',
90 'civicrm_entity_financial_trxn',
96 ///////////////// civicrm_participant_payment_create methods
99 * Test civicrm_participant_payment_create with wrong params type
101 function testPaymentCreateWrongParamsType() {
102 $params = 'a string';
103 $result = $this->callAPIFailure('participant_payment', 'create', $params);
107 * Test civicrm_participant_payment_create with empty params
109 function testPaymentCreateEmptyParams() {
111 $result = $this->callAPIFailure('participant_payment', 'create', $params);
115 * check without contribution_id
117 function testPaymentCreateMissingContributionId() {
118 //Without Payment EntityID
120 'participant_id' => $this->_participantID
, );
122 $participantPayment = $this->callAPIFailure('participant_payment', 'create', $params);
126 * check with valid array
128 function testPaymentCreate() {
129 //Create Contribution & get contribution ID
130 $contributionID = $this->contributionCreate($this->_contactID
);
132 //Create Participant Payment record With Values
134 'participant_id' => $this->_participantID
,
135 'contribution_id' => $contributionID,
138 $result = $this->callAPIAndDocument('participant_payment', 'create', $params, __FUNCTION__
, __FILE__
);
139 $this->assertTrue(array_key_exists('id', $result), 'in line ' . __LINE__
);
141 //delete created contribution
142 $this->contributionDelete($contributionID);
146 ///////////////// civicrm_participant_payment_create methods
149 * Test civicrm_participant_payment_create with wrong params type
151 function testPaymentUpdateWrongParamsType() {
152 $params = 'a string';
153 $result = $this->callAPIFailure('participant_payment', 'create', $params);
154 $this->assertEquals('Input variable `params` is not an array', $result['error_message'], 'In line ' . __LINE__
);
158 * check with empty array
160 function testPaymentUpdateEmpty() {
162 $participantPayment = $this->callAPIFailure('participant_payment', 'create', $params);
166 * check with missing participant_id
168 function testPaymentUpdateMissingParticipantId() {
169 //WithoutParticipantId
171 'contribution_id' => '3', );
173 $participantPayment = $this->callAPIFailure('participant_payment', 'create', $params);
177 * check with missing contribution_id
179 function testPaymentUpdateMissingContributionId() {
181 'participant_id' => $this->_participantID
, );
182 $participantPayment = $this->callAPIFailure('participant_payment', 'create', $params);
186 * check financial records for offline Participants
188 function testPaymentOffline() {
190 // create contribution w/o fee
191 $contributionID = $this->contributionCreate($this->_contactID
, $this->_contributionTypeId
, NULL, NULL, 4, FALSE);
193 $this->_participantPaymentID
= $this->participantPaymentCreate($this->_participantID
, $contributionID);
195 'id' => $this->_participantPaymentID
,
196 'participant_id' => $this->_participantID
,
197 'contribution_id' => $contributionID, );
200 $participantPayment = $this->callAPISuccess('participant_payment', 'create', $params);
201 $this->assertEquals($participantPayment['id'], $this->_participantPaymentID
);
202 $this->assertTrue(array_key_exists('id', $participantPayment));
203 // check Financial records
204 $this->_checkFinancialRecords($params, 'offline');
206 'id' => $this->_participantPaymentID
,
208 $deletePayment = $this->callAPISuccess('participant_payment', 'delete', $params);
212 * check financial records for online Participant
214 function testPaymentOnline() {
216 $paymentProcessor = $this->processorCreate();
217 $pageParams['processor_id'] = $paymentProcessor->id
;
218 $contributionPage = $this->contributionPageCreate($pageParams);
219 $contributionParams = array(
220 'contact_id' => $this->_contactID
,
221 'contribution_page_id' => $contributionPage['id'],
222 'payment_processor' => $paymentProcessor->id
,
224 $contributionID = $this->onlineContributionCreate($contributionParams, 1);
226 $this->_participantPaymentID
= $this->participantPaymentCreate($this->_participantID
, $contributionID);
228 'id' => $this->_participantPaymentID
,
229 'participant_id' => $this->_participantID
,
230 'contribution_id' => $contributionID, );
233 $participantPayment = $this->callAPISuccess('participant_payment', 'create', $params);
234 $this->assertEquals($participantPayment['id'], $this->_participantPaymentID
);
235 $this->assertTrue(array_key_exists('id', $participantPayment));
236 // check Financial records
237 $this->_checkFinancialRecords($params, 'online');
239 'id' => $this->_participantPaymentID
,
241 $deletePayment = $this->callAPISuccess('participant_payment', 'delete', $params);
245 * check financial records for online Participant pay later scenario
247 function testPaymentPayLaterOnline() {
249 $paymentProcessor = $this->processorCreate();
250 $pageParams['processor_id'] = $paymentProcessor->id
;
251 $pageParams['is_pay_later'] = 1;
252 $contributionPage = $this->contributionPageCreate($pageParams);
253 $contributionParams = array(
254 'contact_id' => $this->_contactID
,
255 'contribution_page_id' => $contributionPage['id'],
256 'contribution_status_id' => 2,
259 $contributionID = $this->onlineContributionCreate($contributionParams, 1);
261 $this->_participantPaymentID
= $this->participantPaymentCreate($this->_participantID
, $contributionID);
263 'id' => $this->_participantPaymentID
,
264 'participant_id' => $this->_participantID
,
265 'contribution_id' => $contributionID, );
268 $participantPayment = $this->callAPISuccess('participant_payment', 'create', $params);
269 // check Financial Records
270 $this->_checkFinancialRecords($params, 'payLater');
271 $this->assertEquals($participantPayment['id'], $this->_participantPaymentID
);
272 $this->assertTrue(array_key_exists('id', $participantPayment));
274 'id' => $this->_participantPaymentID
,
276 $deletePayment = $this->callAPISuccess('participant_payment', 'delete', $params);
279 ///////////////// civicrm_participant_payment_delete methods
282 * Test civicrm_participant_payment_delete with wrong params type
284 function testPaymentDeleteWrongParamsType() {
285 $params = 'a string';
286 $result = $this->callAPIFailure('participant_payment', 'delete', $params);
290 * check with empty array
292 function testPaymentDeleteWithEmptyParams() {
294 $deletePayment = $this->callAPIFailure('participant_payment', 'delete', $params);
295 $this->assertEquals('Mandatory key(s) missing from params array: id', $deletePayment['error_message']);
299 * check with wrong id
301 function testPaymentDeleteWithWrongID() {
304 $deletePayment = $this->callAPIFailure('participant_payment', 'delete', $params);
305 $this->assertEquals($deletePayment['error_message'], 'Mandatory key(s) missing from params array: id');
309 * check with valid array
311 function testPaymentDelete() {
313 // create contribution
314 $contributionID = $this->contributionCreate($this->_contactID
, $this->_contributionTypeId
);
316 $this->_participantPaymentID
= $this->participantPaymentCreate($this->_participantID
, $contributionID);
319 'id' => $this->_participantPaymentID
,
322 $result = $this->callAPIAndDocument('participant_payment', 'delete', $params, __FUNCTION__
, __FILE__
);
325 ///////////////// civicrm_participantPayment_get methods
327 * Test civicrm_participantPayment_get - success expected.
329 public function testGet() {
330 //Create Contribution & get contribution ID
331 $contributionID = $this->contributionCreate($this->_contactID3
, $this->_contributionTypeId
);
332 $participantPaymentID = $this->participantPaymentCreate($this->_participantID4
, $contributionID);
334 //Create Participant Payment record With Values
336 'participant_id' => $this->_participantID4
,
337 'contribution_id' => $contributionID,
340 $result = $this->callAPIAndDocument('participant_payment', 'get', $params, __FUNCTION__
, __FILE__
);
341 $this->assertEquals($result['values'][$result['id']]['participant_id'], $this->_participantID4
, 'Check Participant Id');
342 $this->assertEquals($result['values'][$result['id']]['contribution_id'], $contributionID, 'Check Contribution Id');
345 function _checkFinancialRecords($params, $context) {
346 $entityParams = array(
347 'entity_id' => $params['id'],
348 'entity_table' => 'civicrm_contribution',
350 $trxn = current(CRM_Financial_BAO_FinancialItem
::retrieveEntityFinancialTrxn($entityParams));
352 'id' => $trxn['financial_trxn_id'],
357 $compareParams = array(
358 'to_financial_account_id' => 12,
359 'total_amount' => 100,
365 $compareParams = array(
366 'to_financial_account_id' => 6,
367 'total_amount' => 100,
372 $compareParams = array(
373 'to_financial_account_id' => 7,
374 'total_amount' => 100,
380 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialTrxn', $trxnParams, $compareParams);
381 $entityParams = array(
382 'financial_trxn_id' => $trxn['financial_trxn_id'],
383 'entity_table' => 'civicrm_financial_item',
385 $entityTrxn = current(CRM_Financial_BAO_FinancialItem
::retrieveEntityFinancialTrxn($entityParams));
386 $fitemParams = array(
387 'id' => $entityTrxn['entity_id'],
389 if ($context == 'offline' ||
$context == 'online') {
390 $compareParams = array(
393 'financial_account_id' => 1,
396 elseif ($context == 'payLater') {
397 $compareParams = array(
400 'financial_account_id' => 1,
403 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $fitemParams, $compareParams);