3 * +--------------------------------------------------------------------+
4 * | CiviCRM version 4.7 |
5 * +--------------------------------------------------------------------+
6 * | Copyright CiviCRM LLC (c) 2004-2015 |
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
37 class api_v3_ParticipantPaymentTest
extends CiviUnitTestCase
{
39 protected $_apiversion = 3;
40 protected $_contactID;
41 protected $_createdParticipants;
42 protected $_participantID;
44 protected $_participantPaymentID;
45 protected $_financialTypeId;
50 public function setUp() {
52 $this->useTransaction(TRUE);
53 $event = $this->eventCreate(NULL);
54 $this->_eventID
= $event['id'];
55 $this->_contactID
= $this->individualCreate();
56 $this->_createdParticipants
= array();
57 $this->_individualId
= $this->individualCreate();
58 $this->_financialTypeId
= 1;
60 $this->_participantID
= $this->participantCreate(array(
61 'contactID' => $this->_contactID
,
62 'eventID' => $this->_eventID
,
64 $this->_contactID2
= $this->individualCreate();
65 $this->_participantID2
= $this->participantCreate(array(
66 'contactID' => $this->_contactID2
,
67 'eventID' => $this->_eventID
,
69 $this->_participantID3
= $this->participantCreate(array(
70 'contactID' => $this->_contactID2
,
71 'eventID' => $this->_eventID
,
74 $this->_contactID3
= $this->individualCreate();
75 $this->_participantID4
= $this->participantCreate(array(
76 'contactID' => $this->_contactID3
,
77 'eventID' => $this->_eventID
,
82 * Test civicrm_participant_payment_create with wrong params type.
84 public function testPaymentCreateWrongParamsType() {
86 $this->callAPIFailure('participant_payment', 'create', $params);
90 * Test civicrm_participant_payment_create with empty params.
92 public function testPaymentCreateEmptyParams() {
94 $this->callAPIFailure('participant_payment', 'create', $params);
98 * Check without contribution_id.
100 public function testPaymentCreateMissingContributionId() {
101 //Without Payment EntityID
103 'participant_id' => $this->_participantID
,
105 $this->callAPIFailure('participant_payment', 'create', $params);
109 * Check with valid array.
111 public function testPaymentCreate() {
112 //Create Contribution & get contribution ID
113 $contributionID = $this->contributionCreate(array('contact_id' => $this->_contactID
));
115 //Create Participant Payment record With Values
117 'participant_id' => $this->_participantID
,
118 'contribution_id' => $contributionID,
121 $result = $this->callAPIAndDocument('participant_payment', 'create', $params, __FUNCTION__
, __FILE__
);
122 $this->assertTrue(array_key_exists('id', $result));
124 //delete created contribution
125 $this->contributionDelete($contributionID);
129 ///////////////// civicrm_participant_payment_create methods
132 * Test civicrm_participant payment create with wrong params type.
134 public function testPaymentUpdateWrongParamsType() {
135 $params = 'a string';
136 $result = $this->callAPIFailure('participant_payment', 'create', $params);
137 $this->assertEquals('Input variable `params` is not an array', $result['error_message']);
141 * Check with empty array.
143 public function testPaymentUpdateEmpty() {
144 $this->callAPIFailure('participant_payment', 'create', array());
148 * Check with missing participant_id.
150 public function testPaymentUpdateMissingParticipantId() {
152 'contribution_id' => '3',
154 $this->callAPIFailure('participant_payment', 'create', $params);
158 * Check with missing contribution_id.
160 public function testPaymentUpdateMissingContributionId() {
162 'participant_id' => $this->_participantID
,
164 $participantPayment = $this->callAPIFailure('participant_payment', 'create', $params);
168 * Check financial records for offline Participants.
170 public function testPaymentOffline() {
172 // create contribution w/o fee
173 $contributionID = $this->contributionCreate(array(
174 'contact_id' => $this->_contactID
,
175 'financial_type_id' => $this->_financialTypeId
,
176 'payment_instrument_id' => 4,
181 $this->_participantPaymentID
= $this->participantPaymentCreate($this->_participantID
, $contributionID);
183 'id' => $this->_participantPaymentID
,
184 'participant_id' => $this->_participantID
,
185 'contribution_id' => $contributionID,
189 $participantPayment = $this->callAPISuccess('participant_payment', 'create', $params);
190 $this->assertEquals($participantPayment['id'], $this->_participantPaymentID
);
191 $this->assertTrue(array_key_exists('id', $participantPayment));
192 // check Financial records
193 $this->_checkFinancialRecords($params, 'offline');
195 'id' => $this->_participantPaymentID
,
197 $deletePayment = $this->callAPISuccess('participant_payment', 'delete', $params);
201 * Check financial records for online Participant.
203 public function testPaymentOnline() {
205 $paymentProcessor = $this->processorCreate();
206 $pageParams['processor_id'] = $paymentProcessor->id
;
207 $contributionPage = $this->contributionPageCreate($pageParams);
208 $contributionParams = array(
209 'contact_id' => $this->_contactID
,
210 'contribution_page_id' => $contributionPage['id'],
211 'payment_processor' => $paymentProcessor->id
,
212 'financial_type_id' => 1,
214 $contributionID = $this->contributionCreate($contributionParams);
216 $this->_participantPaymentID
= $this->participantPaymentCreate($this->_participantID
, $contributionID);
218 'id' => $this->_participantPaymentID
,
219 'participant_id' => $this->_participantID
,
220 'contribution_id' => $contributionID,
224 $participantPayment = $this->callAPISuccess('participant_payment', 'create', $params);
225 $this->assertEquals($participantPayment['id'], $this->_participantPaymentID
);
226 $this->assertTrue(array_key_exists('id', $participantPayment));
227 // check Financial records
228 $this->_checkFinancialRecords($params, 'online');
230 'id' => $this->_participantPaymentID
,
232 $this->callAPISuccess('participant_payment', 'delete', $params);
236 * Check financial records for online Participant pay later scenario.
238 public function testPaymentPayLaterOnline() {
240 $paymentProcessor = $this->processorCreate();
241 $pageParams['processor_id'] = $paymentProcessor->id
;
242 $pageParams['is_pay_later'] = 1;
243 $contributionPage = $this->contributionPageCreate($pageParams);
244 $contributionParams = array(
245 'contact_id' => $this->_contactID
,
246 'contribution_page_id' => $contributionPage['id'],
247 'contribution_status_id' => 2,
249 'financial_type_id' => 1,
251 $contributionID = $this->contributionCreate($contributionParams);
253 $this->_participantPaymentID
= $this->participantPaymentCreate($this->_participantID
, $contributionID);
255 'id' => $this->_participantPaymentID
,
256 'participant_id' => $this->_participantID
,
257 'contribution_id' => $contributionID,
261 $participantPayment = $this->callAPISuccess('participant_payment', 'create', $params);
262 // check Financial Records
263 $this->_checkFinancialRecords($params, 'payLater');
264 $this->assertEquals($participantPayment['id'], $this->_participantPaymentID
);
265 $this->assertTrue(array_key_exists('id', $participantPayment));
267 'id' => $this->_participantPaymentID
,
269 $this->callAPISuccess('participant_payment', 'delete', $params);
274 * Test civicrm_participant_payment_delete with wrong params type.
276 public function testPaymentDeleteWrongParamsType() {
277 $params = 'a string';
278 $this->callAPIFailure('participant_payment', 'delete', $params);
282 * Check with empty array.
284 public function testPaymentDeleteWithEmptyParams() {
286 $deletePayment = $this->callAPIFailure('participant_payment', 'delete', $params);
287 $this->assertEquals('Mandatory key(s) missing from params array: id', $deletePayment['error_message']);
291 * Check with wrong id.
293 public function testPaymentDeleteWithWrongID() {
297 $deletePayment = $this->callAPIFailure('participant_payment', 'delete', $params);
298 $this->assertEquals($deletePayment['error_message'], 'Error while deleting participantPayment');
302 * Check with valid array.
304 public function testPaymentDelete() {
305 $contributionID = $this->contributionCreate(array(
306 'contact_id' => $this->_contactID
,
309 $this->_participantPaymentID
= $this->participantPaymentCreate($this->_participantID
, $contributionID);
312 'id' => $this->_participantPaymentID
,
314 $this->callAPIAndDocument('participant_payment', 'delete', $params, __FUNCTION__
, __FILE__
);
318 * Test civicrm_participantPayment_get - success expected.
320 public function testGet() {
321 $contributionID = $this->contributionCreate(array('contact_id' => $this->_contactID3
));
322 $this->participantPaymentCreate($this->_participantID4
, $contributionID);
324 //Create Participant Payment record With Values
326 'participant_id' => $this->_participantID4
,
327 'contribution_id' => $contributionID,
330 $result = $this->callAPIAndDocument('participant_payment', 'get', $params, __FUNCTION__
, __FILE__
);
331 $this->assertEquals($result['values'][$result['id']]['participant_id'], $this->_participantID4
, 'Check Participant Id');
332 $this->assertEquals($result['values'][$result['id']]['contribution_id'], $contributionID, 'Check Contribution Id');
336 * @param array $params
339 public function _checkFinancialRecords($params, $context) {
340 $entityParams = array(
341 'entity_id' => $params['id'],
342 'entity_table' => 'civicrm_contribution',
344 $trxn = current(CRM_Financial_BAO_FinancialItem
::retrieveEntityFinancialTrxn($entityParams));
346 'id' => $trxn['financial_trxn_id'],
351 $compareParams = array(
352 'to_financial_account_id' => 12,
353 'total_amount' => 100,
359 $compareParams = array(
360 'to_financial_account_id' => 6,
361 'total_amount' => 100,
367 $compareParams = array(
368 'to_financial_account_id' => 7,
369 'total_amount' => 100,
375 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialTrxn', $trxnParams, $compareParams);
376 $entityParams = array(
377 'financial_trxn_id' => $trxn['financial_trxn_id'],
378 'entity_table' => 'civicrm_financial_item',
380 $entityTrxn = current(CRM_Financial_BAO_FinancialItem
::retrieveEntityFinancialTrxn($entityParams));
381 $fitemParams = array(
382 'id' => $entityTrxn['entity_id'],
384 if ($context == 'offline' ||
$context == 'online') {
385 $compareParams = array(
388 'financial_account_id' => 1,
391 elseif ($context == 'payLater') {
392 $compareParams = array(
395 'financial_account_id' => 1,
398 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $fitemParams, $compareParams);