3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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;
54 'name' => 'Participant Create',
55 'description' => 'Test all Participant Create API methods.',
56 'group' => 'CiviCRM API Tests',
62 $tablesToTruncate = array(
63 'civicrm_contribution',
66 $this->quickCleanup($tablesToTruncate);
67 $event = $this->eventCreate(NULL);
68 $this->_eventID
= $event['id'];
69 $this->_contactID
= $this->individualCreate();
70 $this->_createdParticipants
= array();
71 $this->_individualId
= $this->individualCreate();
73 $this->_participantID
= $this->participantCreate(array('contactID' => $this->_contactID
, 'eventID' => $this->_eventID
));
74 $this->_contactID2
= $this->individualCreate();
75 $this->_participantID2
= $this->participantCreate(array('contactID' => $this->_contactID2
, 'eventID' => $this->_eventID
));
76 $this->_participantID3
= $this->participantCreate(array('contactID' => $this->_contactID2
, 'eventID' => $this->_eventID
));
78 $this->_contactID3
= $this->individualCreate();
79 $this->_participantID4
= $this->participantCreate(array('contactID' => $this->_contactID3
, 'eventID' => $this->_eventID
));
83 $this->eventDelete($this->_eventID
);
87 'civicrm_contribution',
88 'civicrm_participant',
89 'civicrm_participant_payment',
91 'civicrm_financial_item',
92 'civicrm_financial_trxn',
93 'civicrm_entity_financial_trxn',
99 ///////////////// civicrm_participant_payment_create methods
102 * Test civicrm_participant_payment_create with wrong params type
104 function testPaymentCreateWrongParamsType() {
105 $params = 'a string';
106 $result = $this->callAPIFailure('participant_payment', 'create', $params);
110 * Test civicrm_participant_payment_create with empty params
112 function testPaymentCreateEmptyParams() {
114 $result = $this->callAPIFailure('participant_payment', 'create', $params);
118 * check without contribution_id
120 function testPaymentCreateMissingContributionId() {
121 //Without Payment EntityID
123 'participant_id' => $this->_participantID
, );
125 $participantPayment = $this->callAPIFailure('participant_payment', 'create', $params);
129 * check with valid array
131 function testPaymentCreate() {
132 //Create Contribution & get contribution ID
133 $contributionID = $this->contributionCreate($this->_contactID
);
135 //Create Participant Payment record With Values
137 'participant_id' => $this->_participantID
,
138 'contribution_id' => $contributionID,
141 $result = $this->callAPIAndDocument('participant_payment', 'create', $params, __FUNCTION__
, __FILE__
);
142 $this->assertTrue(array_key_exists('id', $result), 'in line ' . __LINE__
);
144 //delete created contribution
145 $this->contributionDelete($contributionID);
149 ///////////////// civicrm_participant_payment_create methods
152 * Test civicrm_participant_payment_create with wrong params type
154 function testPaymentUpdateWrongParamsType() {
155 $params = 'a string';
156 $result = $this->callAPIFailure('participant_payment', 'create', $params);
157 $this->assertEquals('Input variable `params` is not an array', $result['error_message'], 'In line ' . __LINE__
);
161 * check with empty array
163 function testPaymentUpdateEmpty() {
165 $participantPayment = $this->callAPIFailure('participant_payment', 'create', $params);
169 * check with missing participant_id
171 function testPaymentUpdateMissingParticipantId() {
172 //WithoutParticipantId
174 'contribution_id' => '3', );
176 $participantPayment = $this->callAPIFailure('participant_payment', 'create', $params);
180 * check with missing contribution_id
182 function testPaymentUpdateMissingContributionId() {
184 'participant_id' => $this->_participantID
, );
185 $participantPayment = $this->callAPIFailure('participant_payment', 'create', $params);
189 * check financial records for offline Participants
191 function testPaymentOffline() {
193 // create contribution w/o fee
194 $contributionID = $this->contributionCreate($this->_contactID
, $this->_contributionTypeId
, NULL, NULL, 4, FALSE);
196 $this->_participantPaymentID
= $this->participantPaymentCreate($this->_participantID
, $contributionID);
198 'id' => $this->_participantPaymentID
,
199 'participant_id' => $this->_participantID
,
200 'contribution_id' => $contributionID, );
203 $participantPayment = $this->callAPISuccess('participant_payment', 'create', $params);
204 $this->assertEquals($participantPayment['id'], $this->_participantPaymentID
);
205 $this->assertTrue(array_key_exists('id', $participantPayment));
206 // check Financial records
207 $this->_checkFinancialRecords($params, 'offline');
209 'id' => $this->_participantPaymentID
,
211 $deletePayment = $this->callAPISuccess('participant_payment', 'delete', $params);
215 * check financial records for online Participant
217 function testPaymentOnline() {
219 $paymentProcessor = $this->processorCreate();
220 $pageParams['processor_id'] = $paymentProcessor->id
;
221 $contributionPage = $this->contributionPageCreate($pageParams);
222 $contributionParams = array(
223 'contact_id' => $this->_contactID
,
224 'contribution_page_id' => $contributionPage['id'],
225 'payment_processor' => $paymentProcessor->id
,
227 $contributionID = $this->onlineContributionCreate($contributionParams, 1);
229 $this->_participantPaymentID
= $this->participantPaymentCreate($this->_participantID
, $contributionID);
231 'id' => $this->_participantPaymentID
,
232 'participant_id' => $this->_participantID
,
233 'contribution_id' => $contributionID, );
236 $participantPayment = $this->callAPISuccess('participant_payment', 'create', $params);
237 $this->assertEquals($participantPayment['id'], $this->_participantPaymentID
);
238 $this->assertTrue(array_key_exists('id', $participantPayment));
239 // check Financial records
240 $this->_checkFinancialRecords($params, 'online');
242 'id' => $this->_participantPaymentID
,
244 $deletePayment = $this->callAPISuccess('participant_payment', 'delete', $params);
248 * check financial records for online Participant pay later scenario
250 function testPaymentPayLaterOnline() {
252 $paymentProcessor = $this->processorCreate();
253 $pageParams['processor_id'] = $paymentProcessor->id
;
254 $pageParams['is_pay_later'] = 1;
255 $contributionPage = $this->contributionPageCreate($pageParams);
256 $contributionParams = array(
257 'contact_id' => $this->_contactID
,
258 'contribution_page_id' => $contributionPage['id'],
259 'contribution_status_id' => 2,
262 $contributionID = $this->onlineContributionCreate($contributionParams, 1);
264 $this->_participantPaymentID
= $this->participantPaymentCreate($this->_participantID
, $contributionID);
266 'id' => $this->_participantPaymentID
,
267 'participant_id' => $this->_participantID
,
268 'contribution_id' => $contributionID, );
271 $participantPayment = $this->callAPISuccess('participant_payment', 'create', $params);
272 // check Financial Records
273 $this->_checkFinancialRecords($params, 'payLater');
274 $this->assertEquals($participantPayment['id'], $this->_participantPaymentID
);
275 $this->assertTrue(array_key_exists('id', $participantPayment));
277 'id' => $this->_participantPaymentID
,
279 $deletePayment = $this->callAPISuccess('participant_payment', 'delete', $params);
282 ///////////////// civicrm_participant_payment_delete methods
285 * Test civicrm_participant_payment_delete with wrong params type
287 function testPaymentDeleteWrongParamsType() {
288 $params = 'a string';
289 $result = $this->callAPIFailure('participant_payment', 'delete', $params);
293 * check with empty array
295 function testPaymentDeleteWithEmptyParams() {
297 $deletePayment = $this->callAPIFailure('participant_payment', 'delete', $params);
298 $this->assertEquals('Mandatory key(s) missing from params array: id', $deletePayment['error_message']);
302 * check with wrong id
304 function testPaymentDeleteWithWrongID() {
307 $deletePayment = $this->callAPIFailure('participant_payment', 'delete', $params);
308 $this->assertEquals($deletePayment['error_message'], 'Error while deleting participantPayment');
312 * check with valid array
314 function testPaymentDelete() {
316 // create contribution
317 $contributionID = $this->contributionCreate($this->_contactID
, $this->_contributionTypeId
);
319 $this->_participantPaymentID
= $this->participantPaymentCreate($this->_participantID
, $contributionID);
322 'id' => $this->_participantPaymentID
,
325 $result = $this->callAPIAndDocument('participant_payment', 'delete', $params, __FUNCTION__
, __FILE__
);
328 ///////////////// civicrm_participantPayment_get methods
330 * Test civicrm_participantPayment_get - success expected.
332 public function testGet() {
333 //Create Contribution & get contribution ID
334 $contributionID = $this->contributionCreate($this->_contactID3
, $this->_contributionTypeId
);
335 $participantPaymentID = $this->participantPaymentCreate($this->_participantID4
, $contributionID);
337 //Create Participant Payment record With Values
339 'participant_id' => $this->_participantID4
,
340 'contribution_id' => $contributionID,
343 $result = $this->callAPIAndDocument('participant_payment', 'get', $params, __FUNCTION__
, __FILE__
);
344 $this->assertEquals($result['values'][$result['id']]['participant_id'], $this->_participantID4
, 'Check Participant Id');
345 $this->assertEquals($result['values'][$result['id']]['contribution_id'], $contributionID, 'Check Contribution Id');
352 function _checkFinancialRecords($params, $context) {
353 $entityParams = array(
354 'entity_id' => $params['id'],
355 'entity_table' => 'civicrm_contribution',
357 $trxn = current(CRM_Financial_BAO_FinancialItem
::retrieveEntityFinancialTrxn($entityParams));
359 'id' => $trxn['financial_trxn_id'],
364 $compareParams = array(
365 'to_financial_account_id' => 12,
366 'total_amount' => 100,
372 $compareParams = array(
373 'to_financial_account_id' => 6,
374 'total_amount' => 100,
379 $compareParams = array(
380 'to_financial_account_id' => 7,
381 'total_amount' => 100,
387 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialTrxn', $trxnParams, $compareParams);
388 $entityParams = array(
389 'financial_trxn_id' => $trxn['financial_trxn_id'],
390 'entity_table' => 'civicrm_financial_item',
392 $entityTrxn = current(CRM_Financial_BAO_FinancialItem
::retrieveEntityFinancialTrxn($entityParams));
393 $fitemParams = array(
394 'id' => $entityTrxn['entity_id'],
396 if ($context == 'offline' ||
$context == 'online') {
397 $compareParams = array(
400 'financial_account_id' => 1,
403 elseif ($context == 'payLater') {
404 $compareParams = array(
407 'financial_account_id' => 1,
410 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $fitemParams, $compareParams);