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