Merge pull request #6986 from saurabhbatra96/CRM-17120-function-caching-test
[civicrm-core.git] / tests / phpunit / api / v3 / ParticipantPaymentTest.php
1 <?php
2 /**
3 * +--------------------------------------------------------------------+
4 * | CiviCRM version 4.7 |
5 * +--------------------------------------------------------------------+
6 * | Copyright CiviCRM LLC (c) 2004-2015 |
7 * +--------------------------------------------------------------------+
8 * | This file is a part of CiviCRM. |
9 * | |
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. |
13 * | |
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. |
18 * | |
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 * +--------------------------------------------------------------------+
26 */
27
28 require_once 'CiviTest/CiviUnitTestCase.php';
29
30
31 /**
32 * Test APIv3 civicrm_participant_* functions
33 *
34 * @package CiviCRM_APIv3
35 * @subpackage API_Event
36 */
37 class api_v3_ParticipantPaymentTest extends CiviUnitTestCase {
38
39 protected $_apiversion = 3;
40 protected $_contactID;
41 protected $_createdParticipants;
42 protected $_participantID;
43 protected $_eventID;
44 protected $_participantPaymentID;
45 protected $_financialTypeId;
46
47 /**
48 * Set up for tests.
49 */
50 public function setUp() {
51 parent::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;
59
60 $this->_participantID = $this->participantCreate(array(
61 'contactID' => $this->_contactID,
62 'eventID' => $this->_eventID,
63 ));
64 $this->_contactID2 = $this->individualCreate();
65 $this->_participantID2 = $this->participantCreate(array(
66 'contactID' => $this->_contactID2,
67 'eventID' => $this->_eventID,
68 ));
69 $this->_participantID3 = $this->participantCreate(array(
70 'contactID' => $this->_contactID2,
71 'eventID' => $this->_eventID,
72 ));
73
74 $this->_contactID3 = $this->individualCreate();
75 $this->_participantID4 = $this->participantCreate(array(
76 'contactID' => $this->_contactID3,
77 'eventID' => $this->_eventID,
78 ));
79 }
80
81 /**
82 * Test civicrm_participant_payment_create with wrong params type.
83 */
84 public function testPaymentCreateWrongParamsType() {
85 $params = 'a string';
86 $this->callAPIFailure('participant_payment', 'create', $params);
87 }
88
89 /**
90 * Test civicrm_participant_payment_create with empty params.
91 */
92 public function testPaymentCreateEmptyParams() {
93 $params = array();
94 $this->callAPIFailure('participant_payment', 'create', $params);
95 }
96
97 /**
98 * Check without contribution_id.
99 */
100 public function testPaymentCreateMissingContributionId() {
101 //Without Payment EntityID
102 $params = array(
103 'participant_id' => $this->_participantID,
104 );
105 $this->callAPIFailure('participant_payment', 'create', $params);
106 }
107
108 /**
109 * Check with valid array.
110 */
111 public function testPaymentCreate() {
112 //Create Contribution & get contribution ID
113 $contributionID = $this->contributionCreate(array('contact_id' => $this->_contactID));
114
115 //Create Participant Payment record With Values
116 $params = array(
117 'participant_id' => $this->_participantID,
118 'contribution_id' => $contributionID,
119 );
120
121 $result = $this->callAPIAndDocument('participant_payment', 'create', $params, __FUNCTION__, __FILE__);
122 $this->assertTrue(array_key_exists('id', $result));
123
124 //delete created contribution
125 $this->contributionDelete($contributionID);
126 }
127
128
129 ///////////////// civicrm_participant_payment_create methods
130
131 /**
132 * Test civicrm_participant payment create with wrong params type.
133 */
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']);
138 }
139
140 /**
141 * Check with empty array.
142 */
143 public function testPaymentUpdateEmpty() {
144 $this->callAPIFailure('participant_payment', 'create', array());
145 }
146
147 /**
148 * Check with missing participant_id.
149 */
150 public function testPaymentUpdateMissingParticipantId() {
151 $params = array(
152 'contribution_id' => '3',
153 );
154 $this->callAPIFailure('participant_payment', 'create', $params);
155 }
156
157 /**
158 * Check with missing contribution_id.
159 */
160 public function testPaymentUpdateMissingContributionId() {
161 $params = array(
162 'participant_id' => $this->_participantID,
163 );
164 $participantPayment = $this->callAPIFailure('participant_payment', 'create', $params);
165 }
166
167 /**
168 * Check financial records for offline Participants.
169 */
170 public function testPaymentOffline() {
171
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,
177 'fee_amount' => 0,
178 'net_amount' => 100,
179 ));
180
181 $this->_participantPaymentID = $this->participantPaymentCreate($this->_participantID, $contributionID);
182 $params = array(
183 'id' => $this->_participantPaymentID,
184 'participant_id' => $this->_participantID,
185 'contribution_id' => $contributionID,
186 );
187
188 // Update Payment
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');
194 $params = array(
195 'id' => $this->_participantPaymentID,
196 );
197 $deletePayment = $this->callAPISuccess('participant_payment', 'delete', $params);
198 }
199
200 /**
201 * Check financial records for online Participant.
202 */
203 public function testPaymentOnline() {
204
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,
213 );
214 $contributionID = $this->contributionCreate($contributionParams);
215
216 $this->_participantPaymentID = $this->participantPaymentCreate($this->_participantID, $contributionID);
217 $params = array(
218 'id' => $this->_participantPaymentID,
219 'participant_id' => $this->_participantID,
220 'contribution_id' => $contributionID,
221 );
222
223 // Update Payment
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');
229 $params = array(
230 'id' => $this->_participantPaymentID,
231 );
232 $this->callAPISuccess('participant_payment', 'delete', $params);
233 }
234
235 /**
236 * Check financial records for online Participant pay later scenario.
237 */
238 public function testPaymentPayLaterOnline() {
239
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,
248 'is_pay_later' => 1,
249 'financial_type_id' => 1,
250 );
251 $contributionID = $this->contributionCreate($contributionParams);
252
253 $this->_participantPaymentID = $this->participantPaymentCreate($this->_participantID, $contributionID);
254 $params = array(
255 'id' => $this->_participantPaymentID,
256 'participant_id' => $this->_participantID,
257 'contribution_id' => $contributionID,
258 );
259
260 // Update Payment
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));
266 $params = array(
267 'id' => $this->_participantPaymentID,
268 );
269 $this->callAPISuccess('participant_payment', 'delete', $params);
270 }
271
272
273 /**
274 * Test civicrm_participant_payment_delete with wrong params type.
275 */
276 public function testPaymentDeleteWrongParamsType() {
277 $params = 'a string';
278 $this->callAPIFailure('participant_payment', 'delete', $params);
279 }
280
281 /**
282 * Check with empty array.
283 */
284 public function testPaymentDeleteWithEmptyParams() {
285 $params = array();
286 $deletePayment = $this->callAPIFailure('participant_payment', 'delete', $params);
287 $this->assertEquals('Mandatory key(s) missing from params array: id', $deletePayment['error_message']);
288 }
289
290 /**
291 * Check with wrong id.
292 */
293 public function testPaymentDeleteWithWrongID() {
294 $params = array(
295 'id' => 0,
296 );
297 $deletePayment = $this->callAPIFailure('participant_payment', 'delete', $params);
298 $this->assertEquals($deletePayment['error_message'], 'Error while deleting participantPayment');
299 }
300
301 /**
302 * Check with valid array.
303 */
304 public function testPaymentDelete() {
305 $contributionID = $this->contributionCreate(array(
306 'contact_id' => $this->_contactID,
307 ));
308
309 $this->_participantPaymentID = $this->participantPaymentCreate($this->_participantID, $contributionID);
310
311 $params = array(
312 'id' => $this->_participantPaymentID,
313 );
314 $this->callAPIAndDocument('participant_payment', 'delete', $params, __FUNCTION__, __FILE__);
315 }
316
317 /**
318 * Test civicrm_participantPayment_get - success expected.
319 */
320 public function testGet() {
321 $contributionID = $this->contributionCreate(array('contact_id' => $this->_contactID3));
322 $this->participantPaymentCreate($this->_participantID4, $contributionID);
323
324 //Create Participant Payment record With Values
325 $params = array(
326 'participant_id' => $this->_participantID4,
327 'contribution_id' => $contributionID,
328 );
329
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');
333 }
334
335 /**
336 * @param array $params
337 * @param $context
338 */
339 public function _checkFinancialRecords($params, $context) {
340 $entityParams = array(
341 'entity_id' => $params['id'],
342 'entity_table' => 'civicrm_contribution',
343 );
344 $trxn = current(CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($entityParams));
345 $trxnParams = array(
346 'id' => $trxn['financial_trxn_id'],
347 );
348
349 switch ($context) {
350 case 'online':
351 $compareParams = array(
352 'to_financial_account_id' => 12,
353 'total_amount' => 100,
354 'status_id' => 1,
355 );
356 break;
357
358 case 'offline':
359 $compareParams = array(
360 'to_financial_account_id' => 6,
361 'total_amount' => 100,
362 'status_id' => 1,
363 );
364 break;
365
366 case 'payLater':
367 $compareParams = array(
368 'to_financial_account_id' => 7,
369 'total_amount' => 100,
370 'status_id' => 2,
371 );
372 break;
373 }
374
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',
379 );
380 $entityTrxn = current(CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($entityParams));
381 $fitemParams = array(
382 'id' => $entityTrxn['entity_id'],
383 );
384 if ($context == 'offline' || $context == 'online') {
385 $compareParams = array(
386 'amount' => 100,
387 'status_id' => 1,
388 'financial_account_id' => 1,
389 );
390 }
391 elseif ($context == 'payLater') {
392 $compareParams = array(
393 'amount' => 100,
394 'status_id' => 3,
395 'financial_account_id' => 1,
396 );
397 }
398 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $fitemParams, $compareParams);
399 }
400
401 }