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
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 $_contributionTypeId;
47 public function setUp() {
49 $this->useTransaction(TRUE);
50 $event = $this->eventCreate(NULL);
51 $this->_eventID
= $event['id'];
52 $this->_contactID
= $this->individualCreate();
53 $this->_createdParticipants
= array();
54 $this->_individualId
= $this->individualCreate();
56 $this->_participantID
= $this->participantCreate(array(
57 'contactID' => $this->_contactID
,
58 'eventID' => $this->_eventID
,
60 $this->_contactID2
= $this->individualCreate();
61 $this->_participantID2
= $this->participantCreate(array(
62 'contactID' => $this->_contactID2
,
63 'eventID' => $this->_eventID
,
65 $this->_participantID3
= $this->participantCreate(array(
66 'contactID' => $this->_contactID2
,
67 'eventID' => $this->_eventID
,
70 $this->_contactID3
= $this->individualCreate();
71 $this->_participantID4
= $this->participantCreate(array(
72 'contactID' => $this->_contactID3
,
73 'eventID' => $this->_eventID
,
77 ///////////////// civicrm_participant_payment_create methods
80 * Test civicrm_participant_payment_create with wrong params type
82 public function testPaymentCreateWrongParamsType() {
84 $result = $this->callAPIFailure('participant_payment', 'create', $params);
88 * Test civicrm_participant_payment_create with empty params
90 public function testPaymentCreateEmptyParams() {
92 $result = $this->callAPIFailure('participant_payment', 'create', $params);
96 * Check without contribution_id
98 public function testPaymentCreateMissingContributionId() {
99 //Without Payment EntityID
101 'participant_id' => $this->_participantID
,
104 $participantPayment = $this->callAPIFailure('participant_payment', 'create', $params);
108 * Check with valid array
110 public function testPaymentCreate() {
111 //Create Contribution & get contribution ID
112 $contributionID = $this->contributionCreate($this->_contactID
);
114 //Create Participant Payment record With Values
116 'participant_id' => $this->_participantID
,
117 'contribution_id' => $contributionID,
120 $result = $this->callAPIAndDocument('participant_payment', 'create', $params, __FUNCTION__
, __FILE__
);
121 $this->assertTrue(array_key_exists('id', $result), 'in line ' . __LINE__
);
123 //delete created contribution
124 $this->contributionDelete($contributionID);
128 ///////////////// civicrm_participant_payment_create methods
131 * Test civicrm_participant_payment_create with wrong params type
133 public function testPaymentUpdateWrongParamsType() {
134 $params = 'a string';
135 $result = $this->callAPIFailure('participant_payment', 'create', $params);
136 $this->assertEquals('Input variable `params` is not an array', $result['error_message'], 'In line ' . __LINE__
);
140 * Check with empty array
142 public function testPaymentUpdateEmpty() {
144 $participantPayment = $this->callAPIFailure('participant_payment', 'create', $params);
148 * Check with missing participant_id
150 public function testPaymentUpdateMissingParticipantId() {
151 //WithoutParticipantId
153 'contribution_id' => '3',
156 $participantPayment = $this->callAPIFailure('participant_payment', 'create', $params);
160 * Check with missing contribution_id
162 public function testPaymentUpdateMissingContributionId() {
164 'participant_id' => $this->_participantID
,
166 $participantPayment = $this->callAPIFailure('participant_payment', 'create', $params);
170 * Check financial records for offline Participants
172 public function testPaymentOffline() {
174 // create contribution w/o fee
175 $contributionID = $this->contributionCreate($this->_contactID
, $this->_contributionTypeId
, NULL, NULL, 4, FALSE);
177 $this->_participantPaymentID
= $this->participantPaymentCreate($this->_participantID
, $contributionID);
179 'id' => $this->_participantPaymentID
,
180 'participant_id' => $this->_participantID
,
181 'contribution_id' => $contributionID,
185 $participantPayment = $this->callAPISuccess('participant_payment', 'create', $params);
186 $this->assertEquals($participantPayment['id'], $this->_participantPaymentID
);
187 $this->assertTrue(array_key_exists('id', $participantPayment));
188 // check Financial records
189 $this->_checkFinancialRecords($params, 'offline');
191 'id' => $this->_participantPaymentID
,
193 $deletePayment = $this->callAPISuccess('participant_payment', 'delete', $params);
197 * Check financial records for online Participant
199 public function testPaymentOnline() {
201 $paymentProcessor = $this->processorCreate();
202 $pageParams['processor_id'] = $paymentProcessor->id
;
203 $contributionPage = $this->contributionPageCreate($pageParams);
204 $contributionParams = array(
205 'contact_id' => $this->_contactID
,
206 'contribution_page_id' => $contributionPage['id'],
207 'payment_processor' => $paymentProcessor->id
,
209 $contributionID = $this->onlineContributionCreate($contributionParams, 1);
211 $this->_participantPaymentID
= $this->participantPaymentCreate($this->_participantID
, $contributionID);
213 'id' => $this->_participantPaymentID
,
214 'participant_id' => $this->_participantID
,
215 'contribution_id' => $contributionID,
219 $participantPayment = $this->callAPISuccess('participant_payment', 'create', $params);
220 $this->assertEquals($participantPayment['id'], $this->_participantPaymentID
);
221 $this->assertTrue(array_key_exists('id', $participantPayment));
222 // check Financial records
223 $this->_checkFinancialRecords($params, 'online');
225 'id' => $this->_participantPaymentID
,
227 $deletePayment = $this->callAPISuccess('participant_payment', 'delete', $params);
231 * Check financial records for online Participant pay later scenario
233 public function testPaymentPayLaterOnline() {
235 $paymentProcessor = $this->processorCreate();
236 $pageParams['processor_id'] = $paymentProcessor->id
;
237 $pageParams['is_pay_later'] = 1;
238 $contributionPage = $this->contributionPageCreate($pageParams);
239 $contributionParams = array(
240 'contact_id' => $this->_contactID
,
241 'contribution_page_id' => $contributionPage['id'],
242 'contribution_status_id' => 2,
245 $contributionID = $this->onlineContributionCreate($contributionParams, 1);
247 $this->_participantPaymentID
= $this->participantPaymentCreate($this->_participantID
, $contributionID);
249 'id' => $this->_participantPaymentID
,
250 'participant_id' => $this->_participantID
,
251 'contribution_id' => $contributionID,
255 $participantPayment = $this->callAPISuccess('participant_payment', 'create', $params);
256 // check Financial Records
257 $this->_checkFinancialRecords($params, 'payLater');
258 $this->assertEquals($participantPayment['id'], $this->_participantPaymentID
);
259 $this->assertTrue(array_key_exists('id', $participantPayment));
261 'id' => $this->_participantPaymentID
,
263 $deletePayment = $this->callAPISuccess('participant_payment', 'delete', $params);
266 ///////////////// civicrm_participant_payment_delete methods
269 * Test civicrm_participant_payment_delete with wrong params type
271 public function testPaymentDeleteWrongParamsType() {
272 $params = 'a string';
273 $result = $this->callAPIFailure('participant_payment', 'delete', $params);
277 * Check with empty array
279 public function testPaymentDeleteWithEmptyParams() {
281 $deletePayment = $this->callAPIFailure('participant_payment', 'delete', $params);
282 $this->assertEquals('Mandatory key(s) missing from params array: id', $deletePayment['error_message']);
286 * Check with wrong id
288 public function testPaymentDeleteWithWrongID() {
292 $deletePayment = $this->callAPIFailure('participant_payment', 'delete', $params);
293 $this->assertEquals($deletePayment['error_message'], 'Error while deleting participantPayment');
297 * Check with valid array
299 public function testPaymentDelete() {
301 // create contribution
302 $contributionID = $this->contributionCreate($this->_contactID
, $this->_contributionTypeId
);
304 $this->_participantPaymentID
= $this->participantPaymentCreate($this->_participantID
, $contributionID);
307 'id' => $this->_participantPaymentID
,
310 $result = $this->callAPIAndDocument('participant_payment', 'delete', $params, __FUNCTION__
, __FILE__
);
313 ///////////////// civicrm_participantPayment_get methods
315 * Test civicrm_participantPayment_get - success expected.
317 public function testGet() {
318 //Create Contribution & get contribution ID
319 $contributionID = $this->contributionCreate($this->_contactID3
, $this->_contributionTypeId
);
320 $participantPaymentID = $this->participantPaymentCreate($this->_participantID4
, $contributionID);
322 //Create Participant Payment record With Values
324 'participant_id' => $this->_participantID4
,
325 'contribution_id' => $contributionID,
328 $result = $this->callAPIAndDocument('participant_payment', 'get', $params, __FUNCTION__
, __FILE__
);
329 $this->assertEquals($result['values'][$result['id']]['participant_id'], $this->_participantID4
, 'Check Participant Id');
330 $this->assertEquals($result['values'][$result['id']]['contribution_id'], $contributionID, 'Check Contribution Id');
334 * @param array $params
337 public function _checkFinancialRecords($params, $context) {
338 $entityParams = array(
339 'entity_id' => $params['id'],
340 'entity_table' => 'civicrm_contribution',
342 $trxn = current(CRM_Financial_BAO_FinancialItem
::retrieveEntityFinancialTrxn($entityParams));
344 'id' => $trxn['financial_trxn_id'],
349 $compareParams = array(
350 'to_financial_account_id' => 12,
351 'total_amount' => 100,
357 $compareParams = array(
358 'to_financial_account_id' => 6,
359 'total_amount' => 100,
365 $compareParams = array(
366 'to_financial_account_id' => 7,
367 'total_amount' => 100,
373 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialTrxn', $trxnParams, $compareParams);
374 $entityParams = array(
375 'financial_trxn_id' => $trxn['financial_trxn_id'],
376 'entity_table' => 'civicrm_financial_item',
378 $entityTrxn = current(CRM_Financial_BAO_FinancialItem
::retrieveEntityFinancialTrxn($entityParams));
379 $fitemParams = array(
380 'id' => $entityTrxn['entity_id'],
382 if ($context == 'offline' ||
$context == 'online') {
383 $compareParams = array(
386 'financial_account_id' => 1,
389 elseif ($context == 'payLater') {
390 $compareParams = array(
393 'financial_account_id' => 1,
396 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $fitemParams, $compareParams);