3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
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;
50 $this->useTransaction(TRUE);
51 $event = $this->eventCreate(NULL);
52 $this->_eventID
= $event['id'];
53 $this->_contactID
= $this->individualCreate();
54 $this->_createdParticipants
= array();
55 $this->_individualId
= $this->individualCreate();
57 $this->_participantID
= $this->participantCreate(array('contactID' => $this->_contactID
, 'eventID' => $this->_eventID
));
58 $this->_contactID2
= $this->individualCreate();
59 $this->_participantID2
= $this->participantCreate(array('contactID' => $this->_contactID2
, 'eventID' => $this->_eventID
));
60 $this->_participantID3
= $this->participantCreate(array('contactID' => $this->_contactID2
, 'eventID' => $this->_eventID
));
62 $this->_contactID3
= $this->individualCreate();
63 $this->_participantID4
= $this->participantCreate(array('contactID' => $this->_contactID3
, 'eventID' => $this->_eventID
));
66 ///////////////// civicrm_participant_payment_create methods
69 * Test civicrm_participant_payment_create with wrong params type
71 function testPaymentCreateWrongParamsType() {
73 $result = $this->callAPIFailure('participant_payment', 'create', $params);
77 * Test civicrm_participant_payment_create with empty params
79 function testPaymentCreateEmptyParams() {
81 $result = $this->callAPIFailure('participant_payment', 'create', $params);
85 * Check without contribution_id
87 function testPaymentCreateMissingContributionId() {
88 //Without Payment EntityID
90 'participant_id' => $this->_participantID
, );
92 $participantPayment = $this->callAPIFailure('participant_payment', 'create', $params);
96 * Check with valid array
98 function testPaymentCreate() {
99 //Create Contribution & get contribution ID
100 $contributionID = $this->contributionCreate($this->_contactID
);
102 //Create Participant Payment record With Values
104 'participant_id' => $this->_participantID
,
105 'contribution_id' => $contributionID,
108 $result = $this->callAPIAndDocument('participant_payment', 'create', $params, __FUNCTION__
, __FILE__
);
109 $this->assertTrue(array_key_exists('id', $result), 'in line ' . __LINE__
);
111 //delete created contribution
112 $this->contributionDelete($contributionID);
116 ///////////////// civicrm_participant_payment_create methods
119 * Test civicrm_participant_payment_create with wrong params type
121 function testPaymentUpdateWrongParamsType() {
122 $params = 'a string';
123 $result = $this->callAPIFailure('participant_payment', 'create', $params);
124 $this->assertEquals('Input variable `params` is not an array', $result['error_message'], 'In line ' . __LINE__
);
128 * Check with empty array
130 function testPaymentUpdateEmpty() {
132 $participantPayment = $this->callAPIFailure('participant_payment', 'create', $params);
136 * Check with missing participant_id
138 function testPaymentUpdateMissingParticipantId() {
139 //WithoutParticipantId
141 'contribution_id' => '3', );
143 $participantPayment = $this->callAPIFailure('participant_payment', 'create', $params);
147 * Check with missing contribution_id
149 function testPaymentUpdateMissingContributionId() {
151 'participant_id' => $this->_participantID
, );
152 $participantPayment = $this->callAPIFailure('participant_payment', 'create', $params);
156 * Check financial records for offline Participants
158 function testPaymentOffline() {
160 // create contribution w/o fee
161 $contributionID = $this->contributionCreate($this->_contactID
, $this->_contributionTypeId
, NULL, NULL, 4, FALSE);
163 $this->_participantPaymentID
= $this->participantPaymentCreate($this->_participantID
, $contributionID);
165 'id' => $this->_participantPaymentID
,
166 'participant_id' => $this->_participantID
,
167 'contribution_id' => $contributionID, );
170 $participantPayment = $this->callAPISuccess('participant_payment', 'create', $params);
171 $this->assertEquals($participantPayment['id'], $this->_participantPaymentID
);
172 $this->assertTrue(array_key_exists('id', $participantPayment));
173 // check Financial records
174 $this->_checkFinancialRecords($params, 'offline');
176 'id' => $this->_participantPaymentID
,
178 $deletePayment = $this->callAPISuccess('participant_payment', 'delete', $params);
182 * Check financial records for online Participant
184 function testPaymentOnline() {
186 $paymentProcessor = $this->processorCreate();
187 $pageParams['processor_id'] = $paymentProcessor->id
;
188 $contributionPage = $this->contributionPageCreate($pageParams);
189 $contributionParams = array(
190 'contact_id' => $this->_contactID
,
191 'contribution_page_id' => $contributionPage['id'],
192 'payment_processor' => $paymentProcessor->id
,
194 $contributionID = $this->onlineContributionCreate($contributionParams, 1);
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, 'online');
209 'id' => $this->_participantPaymentID
,
211 $deletePayment = $this->callAPISuccess('participant_payment', 'delete', $params);
215 * Check financial records for online Participant pay later scenario
217 function testPaymentPayLaterOnline() {
219 $paymentProcessor = $this->processorCreate();
220 $pageParams['processor_id'] = $paymentProcessor->id
;
221 $pageParams['is_pay_later'] = 1;
222 $contributionPage = $this->contributionPageCreate($pageParams);
223 $contributionParams = array(
224 'contact_id' => $this->_contactID
,
225 'contribution_page_id' => $contributionPage['id'],
226 'contribution_status_id' => 2,
229 $contributionID = $this->onlineContributionCreate($contributionParams, 1);
231 $this->_participantPaymentID
= $this->participantPaymentCreate($this->_participantID
, $contributionID);
233 'id' => $this->_participantPaymentID
,
234 'participant_id' => $this->_participantID
,
235 'contribution_id' => $contributionID, );
238 $participantPayment = $this->callAPISuccess('participant_payment', 'create', $params);
239 // check Financial Records
240 $this->_checkFinancialRecords($params, 'payLater');
241 $this->assertEquals($participantPayment['id'], $this->_participantPaymentID
);
242 $this->assertTrue(array_key_exists('id', $participantPayment));
244 'id' => $this->_participantPaymentID
,
246 $deletePayment = $this->callAPISuccess('participant_payment', 'delete', $params);
249 ///////////////// civicrm_participant_payment_delete methods
252 * Test civicrm_participant_payment_delete with wrong params type
254 function testPaymentDeleteWrongParamsType() {
255 $params = 'a string';
256 $result = $this->callAPIFailure('participant_payment', 'delete', $params);
260 * Check with empty array
262 function testPaymentDeleteWithEmptyParams() {
264 $deletePayment = $this->callAPIFailure('participant_payment', 'delete', $params);
265 $this->assertEquals('Mandatory key(s) missing from params array: id', $deletePayment['error_message']);
269 * Check with wrong id
271 function testPaymentDeleteWithWrongID() {
274 $deletePayment = $this->callAPIFailure('participant_payment', 'delete', $params);
275 $this->assertEquals($deletePayment['error_message'], 'Error while deleting participantPayment');
279 * Check with valid array
281 function testPaymentDelete() {
283 // create contribution
284 $contributionID = $this->contributionCreate($this->_contactID
, $this->_contributionTypeId
);
286 $this->_participantPaymentID
= $this->participantPaymentCreate($this->_participantID
, $contributionID);
289 'id' => $this->_participantPaymentID
,
292 $result = $this->callAPIAndDocument('participant_payment', 'delete', $params, __FUNCTION__
, __FILE__
);
295 ///////////////// civicrm_participantPayment_get methods
297 * Test civicrm_participantPayment_get - success expected.
299 public function testGet() {
300 //Create Contribution & get contribution ID
301 $contributionID = $this->contributionCreate($this->_contactID3
, $this->_contributionTypeId
);
302 $participantPaymentID = $this->participantPaymentCreate($this->_participantID4
, $contributionID);
304 //Create Participant Payment record With Values
306 'participant_id' => $this->_participantID4
,
307 'contribution_id' => $contributionID,
310 $result = $this->callAPIAndDocument('participant_payment', 'get', $params, __FUNCTION__
, __FILE__
);
311 $this->assertEquals($result['values'][$result['id']]['participant_id'], $this->_participantID4
, 'Check Participant Id');
312 $this->assertEquals($result['values'][$result['id']]['contribution_id'], $contributionID, 'Check Contribution Id');
316 * @param array $params
319 function _checkFinancialRecords($params, $context) {
320 $entityParams = array(
321 'entity_id' => $params['id'],
322 'entity_table' => 'civicrm_contribution',
324 $trxn = current(CRM_Financial_BAO_FinancialItem
::retrieveEntityFinancialTrxn($entityParams));
326 'id' => $trxn['financial_trxn_id'],
331 $compareParams = array(
332 'to_financial_account_id' => 12,
333 'total_amount' => 100,
339 $compareParams = array(
340 'to_financial_account_id' => 6,
341 'total_amount' => 100,
346 $compareParams = array(
347 'to_financial_account_id' => 7,
348 'total_amount' => 100,
354 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialTrxn', $trxnParams, $compareParams);
355 $entityParams = array(
356 'financial_trxn_id' => $trxn['financial_trxn_id'],
357 'entity_table' => 'civicrm_financial_item',
359 $entityTrxn = current(CRM_Financial_BAO_FinancialItem
::retrieveEntityFinancialTrxn($entityParams));
360 $fitemParams = array(
361 'id' => $entityTrxn['entity_id'],
363 if ($context == 'offline' ||
$context == 'online') {
364 $compareParams = array(
367 'financial_account_id' => 1,
370 elseif ($context == 'payLater') {
371 $compareParams = array(
374 'financial_account_id' => 1,
377 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $fitemParams, $compareParams);