Merge pull request #4944 from civicrm/batch-5.part2
[civicrm-core.git] / tests / phpunit / api / v3 / ParticipantPaymentTest.php
1 <?php
2 /**
3 * +--------------------------------------------------------------------+
4 * | CiviCRM version 4.6 |
5 * +--------------------------------------------------------------------+
6 * | Copyright CiviCRM LLC (c) 2004-2014 |
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 $_contributionTypeId;
46
47 public function setUp() {
48 parent::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();
55
56 $this->_participantID = $this->participantCreate(array(
57 'contactID' => $this->_contactID,
58 'eventID' => $this->_eventID
59 ));
60 $this->_contactID2 = $this->individualCreate();
61 $this->_participantID2 = $this->participantCreate(array(
62 'contactID' => $this->_contactID2,
63 'eventID' => $this->_eventID
64 ));
65 $this->_participantID3 = $this->participantCreate(array(
66 'contactID' => $this->_contactID2,
67 'eventID' => $this->_eventID
68 ));
69
70 $this->_contactID3 = $this->individualCreate();
71 $this->_participantID4 = $this->participantCreate(array(
72 'contactID' => $this->_contactID3,
73 'eventID' => $this->_eventID
74 ));
75 }
76
77 ///////////////// civicrm_participant_payment_create methods
78
79 /**
80 * Test civicrm_participant_payment_create with wrong params type
81 */
82 public function testPaymentCreateWrongParamsType() {
83 $params = 'a string';
84 $result = $this->callAPIFailure('participant_payment', 'create', $params);
85 }
86
87 /**
88 * Test civicrm_participant_payment_create with empty params
89 */
90 public function testPaymentCreateEmptyParams() {
91 $params = array();
92 $result = $this->callAPIFailure('participant_payment', 'create', $params);
93 }
94
95 /**
96 * Check without contribution_id
97 */
98 public function testPaymentCreateMissingContributionId() {
99 //Without Payment EntityID
100 $params = array(
101 'participant_id' => $this->_participantID,
102 );
103
104 $participantPayment = $this->callAPIFailure('participant_payment', 'create', $params);
105 }
106
107 /**
108 * Check with valid array
109 */
110 public function testPaymentCreate() {
111 //Create Contribution & get contribution ID
112 $contributionID = $this->contributionCreate($this->_contactID);
113
114 //Create Participant Payment record With Values
115 $params = array(
116 'participant_id' => $this->_participantID,
117 'contribution_id' => $contributionID,
118 );
119
120 $result = $this->callAPIAndDocument('participant_payment', 'create', $params, __FUNCTION__, __FILE__);
121 $this->assertTrue(array_key_exists('id', $result), 'in line ' . __LINE__);
122
123 //delete created contribution
124 $this->contributionDelete($contributionID);
125 }
126
127
128 ///////////////// civicrm_participant_payment_create methods
129
130 /**
131 * Test civicrm_participant_payment_create with wrong params type
132 */
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__);
137 }
138
139 /**
140 * Check with empty array
141 */
142 public function testPaymentUpdateEmpty() {
143 $params = array();
144 $participantPayment = $this->callAPIFailure('participant_payment', 'create', $params);
145 }
146
147 /**
148 * Check with missing participant_id
149 */
150 public function testPaymentUpdateMissingParticipantId() {
151 //WithoutParticipantId
152 $params = array(
153 'contribution_id' => '3',
154 );
155
156 $participantPayment = $this->callAPIFailure('participant_payment', 'create', $params);
157 }
158
159 /**
160 * Check with missing contribution_id
161 */
162 public function testPaymentUpdateMissingContributionId() {
163 $params = array(
164 'participant_id' => $this->_participantID,
165 );
166 $participantPayment = $this->callAPIFailure('participant_payment', 'create', $params);
167 }
168
169 /**
170 * Check financial records for offline Participants
171 */
172 public function testPaymentOffline() {
173
174 // create contribution w/o fee
175 $contributionID = $this->contributionCreate($this->_contactID, $this->_contributionTypeId, NULL, NULL, 4, FALSE);
176
177 $this->_participantPaymentID = $this->participantPaymentCreate($this->_participantID, $contributionID);
178 $params = array(
179 'id' => $this->_participantPaymentID,
180 'participant_id' => $this->_participantID,
181 'contribution_id' => $contributionID,
182 );
183
184 // Update Payment
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');
190 $params = array(
191 'id' => $this->_participantPaymentID,
192 );
193 $deletePayment = $this->callAPISuccess('participant_payment', 'delete', $params);
194 }
195
196 /**
197 * Check financial records for online Participant
198 */
199 public function testPaymentOnline() {
200
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,
208 );
209 $contributionID = $this->onlineContributionCreate($contributionParams, 1);
210
211 $this->_participantPaymentID = $this->participantPaymentCreate($this->_participantID, $contributionID);
212 $params = array(
213 'id' => $this->_participantPaymentID,
214 'participant_id' => $this->_participantID,
215 'contribution_id' => $contributionID,
216 );
217
218 // Update Payment
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');
224 $params = array(
225 'id' => $this->_participantPaymentID,
226 );
227 $deletePayment = $this->callAPISuccess('participant_payment', 'delete', $params);
228 }
229
230 /**
231 * Check financial records for online Participant pay later scenario
232 */
233 public function testPaymentPayLaterOnline() {
234
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,
243 'is_pay_later' => 1,
244 );
245 $contributionID = $this->onlineContributionCreate($contributionParams, 1);
246
247 $this->_participantPaymentID = $this->participantPaymentCreate($this->_participantID, $contributionID);
248 $params = array(
249 'id' => $this->_participantPaymentID,
250 'participant_id' => $this->_participantID,
251 'contribution_id' => $contributionID,
252 );
253
254 // Update Payment
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));
260 $params = array(
261 'id' => $this->_participantPaymentID,
262 );
263 $deletePayment = $this->callAPISuccess('participant_payment', 'delete', $params);
264 }
265
266 ///////////////// civicrm_participant_payment_delete methods
267
268 /**
269 * Test civicrm_participant_payment_delete with wrong params type
270 */
271 public function testPaymentDeleteWrongParamsType() {
272 $params = 'a string';
273 $result = $this->callAPIFailure('participant_payment', 'delete', $params);
274 }
275
276 /**
277 * Check with empty array
278 */
279 public function testPaymentDeleteWithEmptyParams() {
280 $params = array();
281 $deletePayment = $this->callAPIFailure('participant_payment', 'delete', $params);
282 $this->assertEquals('Mandatory key(s) missing from params array: id', $deletePayment['error_message']);
283 }
284
285 /**
286 * Check with wrong id
287 */
288 public function testPaymentDeleteWithWrongID() {
289 $params = array(
290 'id' => 0,
291 );
292 $deletePayment = $this->callAPIFailure('participant_payment', 'delete', $params);
293 $this->assertEquals($deletePayment['error_message'], 'Error while deleting participantPayment');
294 }
295
296 /**
297 * Check with valid array
298 */
299 public function testPaymentDelete() {
300
301 // create contribution
302 $contributionID = $this->contributionCreate($this->_contactID, $this->_contributionTypeId);
303
304 $this->_participantPaymentID = $this->participantPaymentCreate($this->_participantID, $contributionID);
305
306 $params = array(
307 'id' => $this->_participantPaymentID,
308 );
309
310 $result = $this->callAPIAndDocument('participant_payment', 'delete', $params, __FUNCTION__, __FILE__);
311 }
312
313 ///////////////// civicrm_participantPayment_get methods
314 /**
315 * Test civicrm_participantPayment_get - success expected.
316 */
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);
321
322 //Create Participant Payment record With Values
323 $params = array(
324 'participant_id' => $this->_participantID4,
325 'contribution_id' => $contributionID,
326 );
327
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');
331 }
332
333 /**
334 * @param array $params
335 * @param $context
336 */
337 public function _checkFinancialRecords($params, $context) {
338 $entityParams = array(
339 'entity_id' => $params['id'],
340 'entity_table' => 'civicrm_contribution',
341 );
342 $trxn = current(CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($entityParams));
343 $trxnParams = array(
344 'id' => $trxn['financial_trxn_id'],
345 );
346
347 switch ($context) {
348 case 'online':
349 $compareParams = array(
350 'to_financial_account_id' => 12,
351 'total_amount' => 100,
352 'status_id' => 1,
353 );
354 break;
355
356 case 'offline':
357 $compareParams = array(
358 'to_financial_account_id' => 6,
359 'total_amount' => 100,
360 'status_id' => 1,
361 );
362 break;
363
364 case 'payLater':
365 $compareParams = array(
366 'to_financial_account_id' => 7,
367 'total_amount' => 100,
368 'status_id' => 2,
369 );
370 break;
371 }
372
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',
377 );
378 $entityTrxn = current(CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($entityParams));
379 $fitemParams = array(
380 'id' => $entityTrxn['entity_id'],
381 );
382 if ($context == 'offline' || $context == 'online') {
383 $compareParams = array(
384 'amount' => 100,
385 'status_id' => 1,
386 'financial_account_id' => 1,
387 );
388 }
389 elseif ($context == 'payLater') {
390 $compareParams = array(
391 'amount' => 100,
392 'status_id' => 3,
393 'financial_account_id' => 1,
394 );
395 }
396 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $fitemParams, $compareParams);
397 }
398 }