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 $pageParams['processor_id'] = $this->processorCreate();
206 $contributionPage = $this->contributionPageCreate($pageParams);
207 $contributionParams = array(
208 'contact_id' => $this->_contactID
,
209 'contribution_page_id' => $contributionPage['id'],
210 'payment_processor' => $pageParams['processor_id'],
211 'financial_type_id' => 1,
213 $contributionID = $this->contributionCreate($contributionParams);
215 $this->_participantPaymentID
= $this->participantPaymentCreate($this->_participantID
, $contributionID);
217 'id' => $this->_participantPaymentID
,
218 'participant_id' => $this->_participantID
,
219 'contribution_id' => $contributionID,
223 $participantPayment = $this->callAPISuccess('participant_payment', 'create', $params);
224 $this->assertEquals($participantPayment['id'], $this->_participantPaymentID
);
225 $this->assertTrue(array_key_exists('id', $participantPayment));
226 // check Financial records
227 $this->_checkFinancialRecords($params, 'online');
229 'id' => $this->_participantPaymentID
,
231 $this->callAPISuccess('participant_payment', 'delete', $params);
235 * Check financial records for online Participant pay later scenario.
237 public function testPaymentPayLaterOnline() {
238 $pageParams['processor_id'] = $this->processorCreate();
239 $pageParams['is_pay_later'] = 1;
240 $contributionPage = $this->contributionPageCreate($pageParams);
241 $contributionParams = array(
242 'contact_id' => $this->_contactID
,
243 'contribution_page_id' => $contributionPage['id'],
244 'contribution_status_id' => 2,
246 'financial_type_id' => 1,
248 $contributionID = $this->contributionCreate($contributionParams);
250 $this->_participantPaymentID
= $this->participantPaymentCreate($this->_participantID
, $contributionID);
252 'id' => $this->_participantPaymentID
,
253 'participant_id' => $this->_participantID
,
254 'contribution_id' => $contributionID,
258 $participantPayment = $this->callAPISuccess('participant_payment', 'create', $params);
259 // check Financial Records
260 $this->_checkFinancialRecords($params, 'payLater');
261 $this->assertEquals($participantPayment['id'], $this->_participantPaymentID
);
262 $this->assertTrue(array_key_exists('id', $participantPayment));
264 'id' => $this->_participantPaymentID
,
266 $this->callAPISuccess('participant_payment', 'delete', $params);
271 * Test civicrm_participant_payment_delete with wrong params type.
273 public function testPaymentDeleteWrongParamsType() {
274 $params = 'a string';
275 $this->callAPIFailure('participant_payment', 'delete', $params);
279 * Check with empty array.
281 public function testPaymentDeleteWithEmptyParams() {
283 $deletePayment = $this->callAPIFailure('participant_payment', 'delete', $params);
284 $this->assertEquals('Mandatory key(s) missing from params array: id', $deletePayment['error_message']);
288 * Check with wrong id.
290 public function testPaymentDeleteWithWrongID() {
294 $deletePayment = $this->callAPIFailure('participant_payment', 'delete', $params);
295 $this->assertEquals($deletePayment['error_message'], 'Error while deleting participantPayment');
299 * Check with valid array.
301 public function testPaymentDelete() {
302 $contributionID = $this->contributionCreate(array(
303 'contact_id' => $this->_contactID
,
306 $this->_participantPaymentID
= $this->participantPaymentCreate($this->_participantID
, $contributionID);
309 'id' => $this->_participantPaymentID
,
311 $this->callAPIAndDocument('participant_payment', 'delete', $params, __FUNCTION__
, __FILE__
);
315 * Test civicrm_participantPayment_get - success expected.
317 public function testGet() {
318 $contributionID = $this->contributionCreate(array('contact_id' => $this->_contactID3
));
319 $this->participantPaymentCreate($this->_participantID4
, $contributionID);
321 //Create Participant Payment record With Values
323 'participant_id' => $this->_participantID4
,
324 'contribution_id' => $contributionID,
327 $result = $this->callAPIAndDocument('participant_payment', 'get', $params, __FUNCTION__
, __FILE__
);
328 $this->assertEquals($result['values'][$result['id']]['participant_id'], $this->_participantID4
, 'Check Participant Id');
329 $this->assertEquals($result['values'][$result['id']]['contribution_id'], $contributionID, 'Check Contribution Id');
333 * @param array $params
336 public function _checkFinancialRecords($params, $context) {
337 $entityParams = array(
338 'entity_id' => $params['id'],
339 'entity_table' => 'civicrm_contribution',
341 $trxn = current(CRM_Financial_BAO_FinancialItem
::retrieveEntityFinancialTrxn($entityParams));
343 'id' => $trxn['financial_trxn_id'],
348 $compareParams = array(
349 'to_financial_account_id' => 12,
350 'total_amount' => 100,
356 $compareParams = array(
357 'to_financial_account_id' => 6,
358 'total_amount' => 100,
364 $compareParams = array(
365 'to_financial_account_id' => 7,
366 'total_amount' => 100,
372 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialTrxn', $trxnParams, $compareParams);
373 $entityParams = array(
374 'financial_trxn_id' => $trxn['financial_trxn_id'],
375 'entity_table' => 'civicrm_financial_item',
377 $entityTrxn = current(CRM_Financial_BAO_FinancialItem
::retrieveEntityFinancialTrxn($entityParams));
378 $fitemParams = array(
379 'id' => $entityTrxn['entity_id'],
381 if ($context == 'offline' ||
$context == 'online') {
382 $compareParams = array(
385 'financial_account_id' => 1,
388 elseif ($context == 'payLater') {
389 $compareParams = array(
392 'financial_account_id' => 1,
395 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $fitemParams, $compareParams);