Action schedule API modifications
[civicrm-core.git] / tests / phpunit / api / v3 / ParticipantPaymentTest.php
CommitLineData
6a488035 1<?php
0728d72f 2/**
6a488035 3 +--------------------------------------------------------------------+
232624b1 4 | CiviCRM version 4.4 |
6a488035
TO
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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
28require_once 'CiviTest/CiviUnitTestCase.php';
29
30
31/**
32 * Test APIv3 civicrm_participant_* functions
33 *
34 * @package CiviCRM_APIv3
35 * @subpackage API_Event
36 */
37
38class api_v3_ParticipantPaymentTest extends CiviUnitTestCase {
39
0728d72f 40 protected $_apiversion = 3;
6a488035
TO
41 protected $_contactID;
42 protected $_createdParticipants;
43 protected $_participantID;
44 protected $_eventID;
45 protected $_participantPaymentID;
46 protected $_contributionTypeId;
47 public $_eNoticeCompliant = TRUE;
48
49 function get_info() {
50 return array(
51 'name' => 'Participant Create',
52 'description' => 'Test all Participant Create API methods.',
53 'group' => 'CiviCRM API Tests',
54 );
55 }
56
57 function setUp() {
6a488035
TO
58 parent::setUp();
59 $tablesToTruncate = array(
60 'civicrm_contribution',
61 'civicrm_contact',
62 );
63 $this->quickCleanup($tablesToTruncate);
64 $event = $this->eventCreate(NULL);
65 $this->_eventID = $event['id'];
e4d5f1e2 66 $this->_contactID = $this->individualCreate();
6a488035 67 $this->_createdParticipants = array();
e4d5f1e2 68 $this->_individualId = $this->individualCreate();
6a488035
TO
69
70 $this->_participantID = $this->participantCreate(array('contactID' => $this->_contactID, 'eventID' => $this->_eventID));
e4d5f1e2 71 $this->_contactID2 = $this->individualCreate();
0728d72f 72 $this->_participantID2 = $this->participantCreate(array('contactID' => $this->_contactID2, 'eventID' => $this->_eventID));
73 $this->_participantID3 = $this->participantCreate(array('contactID' => $this->_contactID2, 'eventID' => $this->_eventID));
6a488035 74
e4d5f1e2 75 $this->_contactID3 = $this->individualCreate();
0728d72f 76 $this->_participantID4 = $this->participantCreate(array('contactID' => $this->_contactID3, 'eventID' => $this->_eventID));
6a488035
TO
77 }
78
79 function tearDown() {
80 $this->eventDelete($this->_eventID);
81 $this->quickCleanup(
82 array(
83 'civicrm_contact',
84 'civicrm_contribution',
85 'civicrm_participant',
86 'civicrm_participant_payment',
87 'civicrm_line_item',
88 'civicrm_financial_item',
89 'civicrm_financial_trxn',
90 'civicrm_entity_financial_trxn',
0728d72f 91 ),
92 TRUE
6a488035 93 );
6a488035
TO
94 }
95
96 ///////////////// civicrm_participant_payment_create methods
97
98 /**
99 * Test civicrm_participant_payment_create with wrong params type
100 */
101 function testPaymentCreateWrongParamsType() {
102 $params = 'a string';
d0e1eff2 103 $result = $this->callAPIFailure('participant_payment', 'create', $params);
6a488035
TO
104 }
105
106 /**
107 * Test civicrm_participant_payment_create with empty params
108 */
109 function testPaymentCreateEmptyParams() {
110 $params = array();
d0e1eff2 111 $result = $this->callAPIFailure('participant_payment', 'create', $params);
6a488035
TO
112 }
113
114 /**
115 * check without contribution_id
116 */
117 function testPaymentCreateMissingContributionId() {
118 //Without Payment EntityID
119 $params = array(
0728d72f 120 'participant_id' => $this->_participantID, );
6a488035 121
d0e1eff2 122 $participantPayment = $this->callAPIFailure('participant_payment', 'create', $params);
6a488035
TO
123 }
124
125 /**
126 * check with valid array
127 */
128 function testPaymentCreate() {
129 //Create Contribution & get contribution ID
130 $contributionID = $this->contributionCreate($this->_contactID);
131
132 //Create Participant Payment record With Values
133 $params = array(
134 'participant_id' => $this->_participantID,
135 'contribution_id' => $contributionID,
6a488035
TO
136 );
137
0728d72f 138 $result = $this->callAPIAndDocument('participant_payment', 'create', $params, __FUNCTION__, __FILE__);
6a488035
TO
139 $this->assertTrue(array_key_exists('id', $result), 'in line ' . __LINE__);
140
141 //delete created contribution
142 $this->contributionDelete($contributionID);
143 }
144
145
146 ///////////////// civicrm_participant_payment_create methods
147
148 /**
149 * Test civicrm_participant_payment_create with wrong params type
150 */
151 function testPaymentUpdateWrongParamsType() {
152 $params = 'a string';
d0e1eff2 153 $result = $this->callAPIFailure('participant_payment', 'create', $params);
6a488035
TO
154 $this->assertEquals('Input variable `params` is not an array', $result['error_message'], 'In line ' . __LINE__);
155 }
156
157 /**
158 * check with empty array
159 */
160 function testPaymentUpdateEmpty() {
161 $params = array();
d0e1eff2 162 $participantPayment = $this->callAPIFailure('participant_payment', 'create', $params);
6a488035
TO
163 }
164
165 /**
166 * check with missing participant_id
167 */
168 function testPaymentUpdateMissingParticipantId() {
169 //WithoutParticipantId
170 $params = array(
0728d72f 171 'contribution_id' => '3', );
6a488035 172
d0e1eff2 173 $participantPayment = $this->callAPIFailure('participant_payment', 'create', $params);
6a488035
TO
174 }
175
176 /**
177 * check with missing contribution_id
178 */
179 function testPaymentUpdateMissingContributionId() {
180 $params = array(
0728d72f 181 'participant_id' => $this->_participantID, );
d0e1eff2 182 $participantPayment = $this->callAPIFailure('participant_payment', 'create', $params);
6a488035
TO
183 }
184
185 /**
186 * check financial records for offline Participants
187 */
188 function testPaymentOffline() {
189
190 // create contribution w/o fee
191 $contributionID = $this->contributionCreate($this->_contactID, $this->_contributionTypeId, NULL, NULL, 4, FALSE);
192
193 $this->_participantPaymentID = $this->participantPaymentCreate($this->_participantID, $contributionID);
194 $params = array(
195 'id' => $this->_participantPaymentID,
196 'participant_id' => $this->_participantID,
0728d72f 197 'contribution_id' => $contributionID, );
6a488035
TO
198
199 // Update Payment
0728d72f 200 $participantPayment = $this->callAPISuccess('participant_payment', 'create', $params);
6a488035
TO
201 $this->assertEquals($participantPayment['id'], $this->_participantPaymentID);
202 $this->assertTrue(array_key_exists('id', $participantPayment));
203 // check Financial records
204 $this->_checkFinancialRecords($params, 'offline');
205 $params = array(
206 'id' => $this->_participantPaymentID,
6a488035 207 );
0728d72f 208 $deletePayment = $this->callAPISuccess('participant_payment', 'delete', $params);
6a488035
TO
209 }
210
211 /**
212 * check financial records for online Participant
213 */
214 function testPaymentOnline() {
215
216 $paymentProcessor = $this->processorCreate();
217 $pageParams['processor_id'] = $paymentProcessor->id;
218 $contributionPage = $this->contributionPageCreate($pageParams);
219 $contributionParams = array(
220 'contact_id' => $this->_contactID,
221 'contribution_page_id' => $contributionPage['id'],
222 'payment_processor' => $paymentProcessor->id,
223 );
224 $contributionID = $this->onlineContributionCreate($contributionParams, 1);
225
226 $this->_participantPaymentID = $this->participantPaymentCreate($this->_participantID, $contributionID);
227 $params = array(
228 'id' => $this->_participantPaymentID,
229 'participant_id' => $this->_participantID,
0728d72f 230 'contribution_id' => $contributionID, );
6a488035
TO
231
232 // Update Payment
0728d72f 233 $participantPayment = $this->callAPISuccess('participant_payment', 'create', $params);
6a488035
TO
234 $this->assertEquals($participantPayment['id'], $this->_participantPaymentID);
235 $this->assertTrue(array_key_exists('id', $participantPayment));
236 // check Financial records
237 $this->_checkFinancialRecords($params, 'online');
238 $params = array(
239 'id' => $this->_participantPaymentID,
6a488035 240 );
0728d72f 241 $deletePayment = $this->callAPISuccess('participant_payment', 'delete', $params);
6a488035
TO
242 }
243
244 /**
245 * check financial records for online Participant pay later scenario
246 */
247 function testPaymentPayLaterOnline() {
248
249 $paymentProcessor = $this->processorCreate();
250 $pageParams['processor_id'] = $paymentProcessor->id;
251 $pageParams['is_pay_later'] = 1;
252 $contributionPage = $this->contributionPageCreate($pageParams);
253 $contributionParams = array(
254 'contact_id' => $this->_contactID,
255 'contribution_page_id' => $contributionPage['id'],
256 'contribution_status_id' => 2,
257 'is_pay_later' => 1,
258 );
259 $contributionID = $this->onlineContributionCreate($contributionParams, 1);
260
261 $this->_participantPaymentID = $this->participantPaymentCreate($this->_participantID, $contributionID);
262 $params = array(
263 'id' => $this->_participantPaymentID,
264 'participant_id' => $this->_participantID,
0728d72f 265 'contribution_id' => $contributionID, );
6a488035
TO
266
267 // Update Payment
0728d72f 268 $participantPayment = $this->callAPISuccess('participant_payment', 'create', $params);
6a488035
TO
269 // check Financial Records
270 $this->_checkFinancialRecords($params, 'payLater');
271 $this->assertEquals($participantPayment['id'], $this->_participantPaymentID);
272 $this->assertTrue(array_key_exists('id', $participantPayment));
273 $params = array(
274 'id' => $this->_participantPaymentID,
6a488035 275 );
0728d72f 276 $deletePayment = $this->callAPISuccess('participant_payment', 'delete', $params);
6a488035
TO
277 }
278
279 ///////////////// civicrm_participant_payment_delete methods
280
281 /**
282 * Test civicrm_participant_payment_delete with wrong params type
283 */
284 function testPaymentDeleteWrongParamsType() {
285 $params = 'a string';
d0e1eff2 286 $result = $this->callAPIFailure('participant_payment', 'delete', $params);
6a488035
TO
287 }
288
289 /**
290 * check with empty array
291 */
292 function testPaymentDeleteWithEmptyParams() {
0728d72f 293 $params = array();
d0e1eff2 294 $deletePayment = $this->callAPIFailure('participant_payment', 'delete', $params);
6a488035
TO
295 $this->assertEquals('Mandatory key(s) missing from params array: id', $deletePayment['error_message']);
296 }
297
298 /**
299 * check with wrong id
300 */
301 function testPaymentDeleteWithWrongID() {
302 $params = array(
0728d72f 303 'id' => 0, );
d0e1eff2 304 $deletePayment = $this->callAPIFailure('participant_payment', 'delete', $params);
6a488035
TO
305 $this->assertEquals($deletePayment['error_message'], 'Mandatory key(s) missing from params array: id');
306 }
307
308 /**
309 * check with valid array
310 */
311 function testPaymentDelete() {
312
313 // create contribution
314 $contributionID = $this->contributionCreate($this->_contactID, $this->_contributionTypeId);
315
316 $this->_participantPaymentID = $this->participantPaymentCreate($this->_participantID, $contributionID);
317
318 $params = array(
319 'id' => $this->_participantPaymentID,
6a488035
TO
320 );
321
0728d72f 322 $result = $this->callAPIAndDocument('participant_payment', 'delete', $params, __FUNCTION__, __FILE__);
6a488035
TO
323 }
324
325 ///////////////// civicrm_participantPayment_get methods
6a488035
TO
326 /**
327 * Test civicrm_participantPayment_get - success expected.
328 */
329 public function testGet() {
330 //Create Contribution & get contribution ID
331 $contributionID = $this->contributionCreate($this->_contactID3, $this->_contributionTypeId);
332 $participantPaymentID = $this->participantPaymentCreate($this->_participantID4, $contributionID);
333
334 //Create Participant Payment record With Values
335 $params = array(
336 'participant_id' => $this->_participantID4,
337 'contribution_id' => $contributionID,
6a488035
TO
338 );
339
0728d72f 340 $result = $this->callAPIAndDocument('participant_payment', 'get', $params, __FUNCTION__, __FILE__);
6a488035
TO
341 $this->assertEquals($result['values'][$result['id']]['participant_id'], $this->_participantID4, 'Check Participant Id');
342 $this->assertEquals($result['values'][$result['id']]['contribution_id'], $contributionID, 'Check Contribution Id');
343 }
344
345 function _checkFinancialRecords($params, $context) {
346 $entityParams = array(
347 'entity_id' => $params['id'],
348 'entity_table' => 'civicrm_contribution',
349 );
350 $trxn = current(CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($entityParams));
351 $trxnParams = array(
352 'id' => $trxn['financial_trxn_id'],
353 );
354
355 switch ($context) {
356 case 'online':
357 $compareParams = array(
358 'to_financial_account_id' => 12,
359 'total_amount' => 100,
360 'status_id' => 1,
361 );
362 break;
363
364 case 'offline':
365 $compareParams = array(
366 'to_financial_account_id' => 6,
367 'total_amount' => 100,
368 'status_id' => 1,
369 );
370 break;
371 case 'payLater':
372 $compareParams = array(
373 'to_financial_account_id' => 7,
374 'total_amount' => 100,
375 'status_id' => 2,
376 );
377 break;
378 }
379
380 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialTrxn', $trxnParams, $compareParams);
381 $entityParams = array(
382 'financial_trxn_id' => $trxn['financial_trxn_id'],
383 'entity_table' => 'civicrm_financial_item',
384 );
385 $entityTrxn = current(CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($entityParams));
386 $fitemParams = array(
387 'id' => $entityTrxn['entity_id'],
388 );
389 if ($context == 'offline' || $context == 'online') {
390 $compareParams = array(
391 'amount' => 100,
392 'status_id' => 1,
393 'financial_account_id' => 1,
394 );
395 }
396 elseif ($context == 'payLater') {
397 $compareParams = array(
398 'amount' => 100,
399 'status_id' => 3,
400 'financial_account_id' => 1,
401 );
402 }
403 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $fitemParams, $compareParams);
404 }
405}
406