3 * +--------------------------------------------------------------------+
4 * | CiviCRM version 4.7 |
5 * +--------------------------------------------------------------------+
6 * | Copyright CiviCRM LLC (c) 2004-2018 |
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 * +--------------------------------------------------------------------+
29 * Test APIv3 civicrm_participant_* functions
31 * @package CiviCRM_APIv3
32 * @subpackage API_Event
35 class api_v3_ParticipantPaymentTest
extends CiviUnitTestCase
{
37 protected $_apiversion = 3;
38 protected $_contactID;
39 protected $_createdParticipants;
40 protected $_participantID;
42 protected $_participantPaymentID;
43 protected $_financialTypeId;
48 public function setUp() {
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();
56 $this->_financialTypeId
= 1;
58 $this->_participantID
= $this->participantCreate(array(
59 'contactID' => $this->_contactID
,
60 'eventID' => $this->_eventID
,
62 $this->_contactID2
= $this->individualCreate();
63 $this->_participantID2
= $this->participantCreate(array(
64 'contactID' => $this->_contactID2
,
65 'eventID' => $this->_eventID
,
67 $this->_participantID3
= $this->participantCreate(array(
68 'contactID' => $this->_contactID2
,
69 'eventID' => $this->_eventID
,
72 $this->_contactID3
= $this->individualCreate();
73 $this->_participantID4
= $this->participantCreate(array(
74 'contactID' => $this->_contactID3
,
75 'eventID' => $this->_eventID
,
80 * Test civicrm_participant_payment_create with wrong params type.
82 public function testPaymentCreateWrongParamsType() {
84 $this->callAPIFailure('participant_payment', 'create', $params);
88 * Test civicrm_participant_payment_create with empty params.
90 public function testPaymentCreateEmptyParams() {
92 $this->callAPIFailure('participant_payment', 'create', $params);
96 * Check without contribution_id.
98 public function testPaymentCreateMissingContributionId() {
99 //Without Payment EntityID
101 'participant_id' => $this->_participantID
,
103 $this->callAPIFailure('participant_payment', 'create', $params);
107 * Check with valid array.
109 public function testPaymentCreate() {
110 //Create Contribution & get contribution ID
111 $contributionID = $this->contributionCreate(array('contact_id' => $this->_contactID
));
113 //Create Participant Payment record With Values
115 'participant_id' => $this->_participantID
,
116 'contribution_id' => $contributionID,
119 $result = $this->callAPIAndDocument('participant_payment', 'create', $params, __FUNCTION__
, __FILE__
);
120 $this->assertTrue(array_key_exists('id', $result));
122 //delete created contribution
123 $this->contributionDelete($contributionID);
127 * Test getPaymentInfo() returns correct
128 * information of the participant payment
130 public function testPaymentInfoForEvent() {
131 //Create Contribution & get contribution ID
132 $contributionID = $this->contributionCreate(array('contact_id' => $this->_contactID
));
134 //Create Participant Payment record With Values
136 'participant_id' => $this->_participantID4
,
137 'contribution_id' => $contributionID,
139 $this->callAPISuccess('participant_payment', 'create', $params);
141 //Check if participant payment is correctly retrieved.
142 $paymentInfo = CRM_Contribute_BAO_Contribution
::getPaymentInfo($this->_participantID4
, 'event');
143 $this->assertEquals('Completed', $paymentInfo['contribution_status']);
144 $this->assertEquals('100.00', $paymentInfo['total']);
148 ///////////////// civicrm_participant_payment_create methods
151 * Test civicrm_participant payment create with wrong params type.
153 public function testPaymentUpdateWrongParamsType() {
154 $params = 'a string';
155 $result = $this->callAPIFailure('participant_payment', 'create', $params);
156 $this->assertEquals('Input variable `params` is not an array', $result['error_message']);
160 * Check with empty array.
162 public function testPaymentUpdateEmpty() {
163 $this->callAPIFailure('participant_payment', 'create', array());
167 * Check with missing participant_id.
169 public function testPaymentUpdateMissingParticipantId() {
171 'contribution_id' => '3',
173 $this->callAPIFailure('participant_payment', 'create', $params);
177 * Check with missing contribution_id.
179 public function testPaymentUpdateMissingContributionId() {
181 'participant_id' => $this->_participantID
,
183 $participantPayment = $this->callAPIFailure('participant_payment', 'create', $params);
187 * Check financial records for offline Participants.
189 public function testPaymentOffline() {
191 // create contribution w/o fee
192 $contributionID = $this->contributionCreate(array(
193 'contact_id' => $this->_contactID
,
194 'financial_type_id' => $this->_financialTypeId
,
195 'payment_instrument_id' => 4,
200 $this->_participantPaymentID
= $this->participantPaymentCreate($this->_participantID
, $contributionID);
202 'id' => $this->_participantPaymentID
,
203 'participant_id' => $this->_participantID
,
204 'contribution_id' => $contributionID,
208 $participantPayment = $this->callAPISuccess('participant_payment', 'create', $params);
209 $this->assertEquals($participantPayment['id'], $this->_participantPaymentID
);
210 $this->assertTrue(array_key_exists('id', $participantPayment));
211 // check Financial records
212 $this->_checkFinancialRecords($params, 'offline');
214 'id' => $this->_participantPaymentID
,
216 $deletePayment = $this->callAPISuccess('participant_payment', 'delete', $params);
220 * Check financial records for online Participant.
222 public function testPaymentOnline() {
224 $pageParams['processor_id'] = $this->processorCreate();
225 $contributionPage = $this->contributionPageCreate($pageParams);
226 $contributionParams = array(
227 'contact_id' => $this->_contactID
,
228 'contribution_page_id' => $contributionPage['id'],
229 'payment_processor' => $pageParams['processor_id'],
230 'financial_type_id' => 1,
232 $contributionID = $this->contributionCreate($contributionParams);
234 $this->_participantPaymentID
= $this->participantPaymentCreate($this->_participantID
, $contributionID);
236 'id' => $this->_participantPaymentID
,
237 'participant_id' => $this->_participantID
,
238 'contribution_id' => $contributionID,
242 $participantPayment = $this->callAPISuccess('participant_payment', 'create', $params);
243 $this->assertEquals($participantPayment['id'], $this->_participantPaymentID
);
244 $this->assertTrue(array_key_exists('id', $participantPayment));
245 // check Financial records
246 $this->_checkFinancialRecords($params, 'online');
248 'id' => $this->_participantPaymentID
,
250 $this->callAPISuccess('participant_payment', 'delete', $params);
254 * Check financial records for online Participant pay later scenario.
256 public function testPaymentPayLaterOnline() {
257 $pageParams['processor_id'] = $this->processorCreate();
258 $pageParams['is_pay_later'] = 1;
259 $contributionPage = $this->contributionPageCreate($pageParams);
260 $contributionParams = array(
261 'contact_id' => $this->_contactID
,
262 'contribution_page_id' => $contributionPage['id'],
263 'contribution_status_id' => 2,
265 'financial_type_id' => 1,
267 $contributionID = $this->contributionCreate($contributionParams);
269 $this->_participantPaymentID
= $this->participantPaymentCreate($this->_participantID
, $contributionID);
271 'id' => $this->_participantPaymentID
,
272 'participant_id' => $this->_participantID
,
273 'contribution_id' => $contributionID,
277 $participantPayment = $this->callAPISuccess('participant_payment', 'create', $params);
278 // check Financial Records
279 $this->_checkFinancialRecords($params, 'payLater');
280 $this->assertEquals($participantPayment['id'], $this->_participantPaymentID
);
281 $this->assertTrue(array_key_exists('id', $participantPayment));
283 'id' => $this->_participantPaymentID
,
285 $this->callAPISuccess('participant_payment', 'delete', $params);
290 * Test civicrm_participant_payment_delete with wrong params type.
292 public function testPaymentDeleteWrongParamsType() {
293 $params = 'a string';
294 $this->callAPIFailure('participant_payment', 'delete', $params);
298 * Check with empty array.
300 public function testPaymentDeleteWithEmptyParams() {
302 $deletePayment = $this->callAPIFailure('participant_payment', 'delete', $params);
303 $this->assertEquals('Mandatory key(s) missing from params array: id', $deletePayment['error_message']);
307 * Check with wrong id.
309 public function testPaymentDeleteWithWrongID() {
313 $deletePayment = $this->callAPIFailure('participant_payment', 'delete', $params);
314 $this->assertEquals($deletePayment['error_message'], 'Error while deleting participantPayment');
318 * Check with valid array.
320 public function testPaymentDelete() {
321 $contributionID = $this->contributionCreate(array(
322 'contact_id' => $this->_contactID
,
325 $this->_participantPaymentID
= $this->participantPaymentCreate($this->_participantID
, $contributionID);
328 'id' => $this->_participantPaymentID
,
330 $this->callAPIAndDocument('participant_payment', 'delete', $params, __FUNCTION__
, __FILE__
);
334 * Test civicrm_participantPayment_get - success expected.
336 public function testGet() {
337 $contributionID = $this->contributionCreate(array('contact_id' => $this->_contactID3
));
338 $this->participantPaymentCreate($this->_participantID4
, $contributionID);
340 //Create Participant Payment record With Values
342 'participant_id' => $this->_participantID4
,
343 'contribution_id' => $contributionID,
346 $result = $this->callAPIAndDocument('participant_payment', 'get', $params, __FUNCTION__
, __FILE__
);
347 $this->assertEquals($result['values'][$result['id']]['participant_id'], $this->_participantID4
, 'Check Participant Id');
348 $this->assertEquals($result['values'][$result['id']]['contribution_id'], $contributionID, 'Check Contribution Id');
352 * @param array $params
355 public function _checkFinancialRecords($params, $context) {
356 $entityParams = array(
357 'entity_id' => $params['id'],
358 'entity_table' => 'civicrm_contribution',
360 $trxn = current(CRM_Financial_BAO_FinancialItem
::retrieveEntityFinancialTrxn($entityParams));
362 'id' => $trxn['financial_trxn_id'],
367 $compareParams = array(
368 'to_financial_account_id' => 12,
369 'total_amount' => 100,
375 $compareParams = array(
376 'to_financial_account_id' => 6,
377 'total_amount' => 100,
383 $compareParams = array(
384 'to_financial_account_id' => 7,
385 'total_amount' => 100,
391 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialTrxn', $trxnParams, $compareParams);
392 $entityParams = array(
393 'financial_trxn_id' => $trxn['financial_trxn_id'],
394 'entity_table' => 'civicrm_financial_item',
396 $entityTrxn = current(CRM_Financial_BAO_FinancialItem
::retrieveEntityFinancialTrxn($entityParams));
397 $fitemParams = array(
398 'id' => $entityTrxn['entity_id'],
400 if ($context == 'offline' ||
$context == 'online') {
401 $compareParams = array(
404 'financial_account_id' => 1,
407 elseif ($context == 'payLater') {
408 $compareParams = array(
411 'financial_account_id' => 1,
414 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $fitemParams, $compareParams);
418 * test getParticipantIds() function
420 public function testGetParticipantIds() {
421 $contributionID = $this->contributionCreate(array('contact_id' => $this->_contactID
));
422 $expectedParticipants = array($this->_participantID
, $this->_participantID2
);
424 //Create Participant Payment record With Values
425 foreach ($expectedParticipants as $pid) {
427 'participant_id' => $pid,
428 'contribution_id' => $contributionID,
430 $this->callAPISuccess('participant_payment', 'create', $params);
432 //Check if all participants are listed.
433 $participants = CRM_Event_BAO_Participant
::getParticipantIds($contributionID);
434 $this->checkArrayEquals($expectedParticipants, $participants);
435 //delete created contribution
436 $this->contributionDelete($contributionID);