Merge pull request #10157 from eileenmcnaughton/iida3
[civicrm-core.git] / tests / phpunit / CRM / Contribute / BAO / ContributionTest.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
81621fee 4 | CiviCRM version 4.7 |
6a488035 5 +--------------------------------------------------------------------+
15a4309a 6 | Copyright CiviCRM LLC (c) 2004-2017 |
6a488035
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 +--------------------------------------------------------------------+
d25dd0ee 26 */
6a488035 27
4cbe18b8
EM
28/**
29 * Class CRM_Contribute_BAO_ContributionTest
acb109b7 30 * @group headless
4cbe18b8 31 */
6a488035 32class CRM_Contribute_BAO_ContributionTest extends CiviUnitTestCase {
6a488035 33
6a488035 34 /**
2449fe69 35 * Clean up after tests.
36 */
37 public function tearDown() {
38 $this->quickCleanUpFinancialEntities();
39 $this->quickCleanUpFinancialEntities(array('civicrm_event'));
40 parent::tearDown();
41 }
42
43 /**
44 * Test create method (create and update modes).
6a488035 45 */
00be9182 46 public function testCreate() {
2449fe69 47 $contactId = $this->individualCreate();
6a488035
TO
48
49 $params = array(
50 'contact_id' => $contactId,
51 'currency' => 'USD',
92915c55 52 'financial_type_id' => 1,
6a488035
TO
53 'contribution_status_id' => 1,
54 'payment_instrument_id' => 1,
55 'source' => 'STUDENT',
56 'receive_date' => '20080522000000',
57 'receipt_date' => '20080522000000',
58 'non_deductible_amount' => 0.00,
59 'total_amount' => 200.00,
60 'fee_amount' => 5,
61 'net_amount' => 195,
62 'trxn_id' => '22ereerwww444444',
63 'invoice_id' => '86ed39c9e9ee6ef6031621ce0eafe7eb81',
64 'thankyou_date' => '20080522',
65 );
66
2449fe69 67 $contribution = CRM_Contribute_BAO_Contribution::create($params);
6a488035 68
2449fe69 69 $this->assertEquals($params['trxn_id'], $contribution->trxn_id, 'Check for transaction id creation.');
6a488035
TO
70 $this->assertEquals($contactId, $contribution->contact_id, 'Check for contact id creation.');
71
72 //update contribution amount
73 $ids = array('contribution' => $contribution->id);
74 $params['fee_amount'] = 10;
75 $params['net_amount'] = 190;
76
77 $contribution = CRM_Contribute_BAO_Contribution::create($params, $ids);
78
79 $this->assertEquals($params['trxn_id'], $contribution->trxn_id, 'Check for transcation id .');
80 $this->assertEquals($params['net_amount'], $contribution->net_amount, 'Check for Amount updation.');
6a488035
TO
81 }
82
83 /**
f43ac8d8 84 * Create() method with custom data.
6a488035 85 */
00be9182 86 public function testCreateWithCustomData() {
2449fe69 87 $contactId = $this->individualCreate();
6a488035
TO
88
89 //create custom data
2449fe69 90 $customGroup = $this->customGroupCreate(array('extends' => 'Contribution'));
91 $customGroupID = $customGroup['id'];
92 $customGroup = $customGroup['values'][$customGroupID];
93
6a488035
TO
94 $fields = array(
95 'label' => 'testFld',
96 'data_type' => 'String',
97 'html_type' => 'Text',
98 'is_active' => 1,
2449fe69 99 'custom_group_id' => $customGroupID,
6a488035
TO
100 );
101 $customField = CRM_Core_BAO_CustomField::create($fields);
102
103 $params = array(
104 'contact_id' => $contactId,
105 'currency' => 'USD',
92915c55 106 'financial_type_id' => 1,
6a488035
TO
107 'contribution_status_id' => 1,
108 'payment_instrument_id' => 1,
109 'source' => 'STUDENT',
110 'receive_date' => '20080522000000',
111 'receipt_date' => '20080522000000',
112 'id' => NULL,
113 'non_deductible_amount' => 0.00,
114 'total_amount' => 200.00,
115 'fee_amount' => 5,
116 'net_amount' => 195,
117 'trxn_id' => '22ereerwww322323',
118 'invoice_id' => '22ed39c9e9ee6ef6031621ce0eafe6da70',
119 'thankyou_date' => '20080522',
120 );
121
6a488035
TO
122 $params['custom'] = array(
123 $customField->id => array(
124 -1 => array(
125 'value' => 'Test custom value',
126 'type' => 'String',
127 'custom_field_id' => $customField->id,
2449fe69 128 'custom_group_id' => $customGroupID,
129 'table_name' => $customGroup['table_name'],
6a488035
TO
130 'column_name' => $customField->column_name,
131 'file_id' => NULL,
132 ),
133 ),
134 );
135
2449fe69 136 $contribution = CRM_Contribute_BAO_Contribution::create($params);
6a488035
TO
137
138 // Check that the custom field value is saved
139 $customValueParams = array(
140 'entityID' => $contribution->id,
141 'custom_' . $customField->id => 1,
142 );
143 $values = CRM_Core_BAO_CustomValueTable::getValues($customValueParams);
144 $this->assertEquals('Test custom value', $values['custom_' . $customField->id], 'Check the custom field value');
145
146 $this->assertEquals($params['trxn_id'], $contribution->trxn_id, 'Check for transcation id creation.');
147 $this->assertEquals($contactId, $contribution->contact_id, 'Check for contact id for Conribution.');
6a488035
TO
148 }
149
150 /**
100fef9d 151 * DeleteContribution() method
6a488035 152 */
00be9182 153 public function testDeleteContribution() {
2449fe69 154 $contactId = $this->individualCreate();
6a488035
TO
155
156 $params = array(
157 'contact_id' => $contactId,
158 'currency' => 'USD',
92915c55 159 'financial_type_id' => 1,
6a488035
TO
160 'contribution_status_id' => 1,
161 'payment_instrument_id' => 1,
162 'source' => 'STUDENT',
163 'receive_date' => '20080522000000',
164 'receipt_date' => '20080522000000',
165 'id' => NULL,
166 'non_deductible_amount' => 0.00,
167 'total_amount' => 200.00,
168 'fee_amount' => 5,
169 'net_amount' => 195,
170 'trxn_id' => '33ereerwww322323',
171 'invoice_id' => '33ed39c9e9ee6ef6031621ce0eafe6da70',
172 'thankyou_date' => '20080522',
173 );
174
2449fe69 175 $contribution = CRM_Contribute_BAO_Contribution::create($params);
6a488035
TO
176
177 $this->assertEquals($params['trxn_id'], $contribution->trxn_id, 'Check for transcation id creation.');
178 $this->assertEquals($contactId, $contribution->contact_id, 'Check for contact id creation.');
179
2449fe69 180 CRM_Contribute_BAO_Contribution::deleteContribution($contribution->id);
6a488035
TO
181
182 $this->assertDBNull('CRM_Contribute_DAO_Contribution', $contribution->trxn_id,
183 'id', 'trxn_id', 'Database check for deleted Contribution.'
184 );
6a488035
TO
185 }
186
187 /**
2449fe69 188 * Create honor-contact method.
6a488035 189 */
2449fe69 190 public function testCreateAndGetHonorContact() {
6a488035 191 $firstName = 'John_' . substr(sha1(rand()), 0, 7);
92915c55
TO
192 $lastName = 'Smith_' . substr(sha1(rand()), 0, 7);
193 $email = "{$firstName}.{$lastName}@example.com";
6a488035 194
8381af80 195 //Get profile id of name honoree_individual used to create profileContact
196 $honoreeProfileId = NULL;
197 $ufGroupDAO = new CRM_Core_DAO_UFGroup();
198 $ufGroupDAO->name = 'honoree_individual';
199 if ($ufGroupDAO->find(TRUE)) {
200 $honoreeProfileId = $ufGroupDAO->id;
201 }
202
6a488035 203 $params = array(
8381af80 204 'prefix_id' => 3,
205 'first_name' => $firstName,
206 'last_name' => $lastName,
207 'email-1' => $email,
6a488035 208 );
8381af80 209 $softParam = array('soft_credit_type_id' => 1);
210
211 $honoreeContactId = CRM_Contact_BAO_Contact::createProfileContact($params, CRM_Core_DAO::$_nullArray,
92915c55
TO
212 NULL, NULL, $honoreeProfileId
213 );
6a488035 214
8381af80 215 $this->assertDBCompareValue('CRM_Contact_DAO_Contact', $honoreeContactId, 'first_name', 'id', $firstName,
6a488035
TO
216 'Database check for created honor contact record.'
217 );
218 //create contribution on behalf of honary.
219
2449fe69 220 $contactId = $this->individualCreate(array('first_name' => 'John', 'last_name' => 'Doe'));
6a488035 221
6a488035
TO
222 $param = array(
223 'contact_id' => $contactId,
224 'currency' => 'USD',
92915c55 225 'financial_type_id' => 4,
6a488035
TO
226 'contribution_status_id' => 1,
227 'receive_date' => date('Ymd'),
228 'total_amount' => 66,
6a488035
TO
229 );
230
2449fe69 231 $contribution = CRM_Contribute_BAO_Contribution::create($param);
6a488035 232 $id = $contribution->id;
2449fe69 233 $softParam['contact_id'] = $honoreeContactId;
8381af80 234 $softParam['contribution_id'] = $id;
235 $softParam['currency'] = $contribution->currency;
236 $softParam['amount'] = $contribution->total_amount;
237
238 //Create Soft Contribution for honoree contact
239 CRM_Contribute_BAO_ContributionSoft::add($softParam);
240
241 $this->assertDBCompareValue('CRM_Contribute_DAO_ContributionSoft', $id, 'contact_id',
242 'contribution_id', $honoreeContactId, 'Check DB for honor contact of the contribution'
6a488035 243 );
e4f46be0 244 //get honorary information
8381af80 245 $getHonorContact = CRM_Contribute_BAO_Contribution::getHonorContacts($honoreeContactId);
4ff25fbb 246 $this->assertEquals(array(
247 $id => array(
248 'honor_type' => 'In Honor of',
2449fe69 249 'honorId' => $contactId,
250 'display_name' => 'Mr. John Doe II',
4ff25fbb 251 'type' => 'Event Fee',
252 'type_id' => '4',
253 'amount' => '$ 66.00',
254 'source' => NULL,
755ca72c 255 'receive_date' => date('Y-m-d 00:00:00'),
4ff25fbb 256 'contribution_status' => 'Completed',
257 ),
258 ), $getHonorContact);
6a488035 259
8381af80 260 $this->assertDBCompareValue('CRM_Contact_DAO_Contact', $honoreeContactId, 'first_name', 'id', $firstName,
6a488035
TO
261 'Database check for created honor contact record.'
262 );
263
264 //get annual contribution information
265 $annual = CRM_Contribute_BAO_Contribution::annual($contactId);
266
267 $config = CRM_Core_Config::singleton();
6c6e6187 268 $currencySymbol = CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_Currency', $config->defaultCurrency, 'symbol', 'name');
6a488035
TO
269 $this->assertDBCompareValue('CRM_Contribute_DAO_Contribution', $id, 'total_amount',
270 'id', ltrim($annual[2], $currencySymbol), 'Check DB for total amount of the contribution'
271 );
6a488035
TO
272 }
273
274 /**
eceb18cc 275 * Display sort name during.
b581842f 276 * Update multiple contributions
6a488035
TO
277 * sortName();
278 */
00be9182 279 public function testsortName() {
6a488035
TO
280 $params = array(
281 'first_name' => 'Shane',
282 'last_name' => 'Whatson',
283 'contact_type' => 'Individual',
284 );
285
286 $contact = CRM_Contact_BAO_Contact::add($params);
287
288 //Now check $contact is object of contact DAO..
289 $this->assertInstanceOf('CRM_Contact_DAO_Contact', $contact, 'Check for created object');
290
291 $contactId = $contact->id;
292
293 $ids = array('contribution' => NULL);
294
295 $param = array(
296 'contact_id' => $contactId,
297 'currency' => 'USD',
92915c55 298 'financial_type_id' => 1,
6a488035
TO
299 'contribution_status_id' => 1,
300 'payment_instrument_id' => 1,
301 'source' => 'STUDENT',
302 'receive_date' => '20080522000000',
303 'receipt_date' => '20080522000000',
304 'id' => NULL,
305 'non_deductible_amount' => 0.00,
306 'total_amount' => 300.00,
307 'fee_amount' => 5,
308 'net_amount' => 295,
309 'trxn_id' => '22ereerwww323',
310 'invoice_id' => '22ed39c9e9ee621ce0eafe6da70',
311 'thankyou_date' => '20080522',
312 );
313
314 $contribution = CRM_Contribute_BAO_Contribution::create($param, $ids);
315
316 $this->assertEquals($param['trxn_id'], $contribution->trxn_id, 'Check for transcation id creation.');
317 $this->assertEquals($contactId, $contribution->contact_id, 'Check for contact id creation.');
318
b581842f 319 //display sort name during Update multiple contributions
6a488035
TO
320 $sortName = CRM_Contribute_BAO_Contribution::sortName($contribution->id);
321
322 $this->assertEquals('Whatson, Shane', $sortName, 'Check for sort name.');
6a488035
TO
323 }
324
325 /**
eceb18cc 326 * Add premium during online Contribution.
6a488035
TO
327 *
328 * AddPremium();
329 */
00be9182 330 public function testAddPremium() {
2449fe69 331 $contactId = $this->individualCreate();
6a488035
TO
332
333 $ids = array(
334 'premium' => NULL,
335 );
336
6a488035
TO
337 $params = array(
338 'name' => 'TEST Premium',
339 'sku' => 111,
340 'imageOption' => 'noImage',
341 'MAX_FILE_SIZE' => 2097152,
342 'price' => 100.00,
343 'cost' => 90.00,
344 'min_contribution' => 100,
345 'is_active' => 1,
346 );
347 $premium = CRM_Contribute_BAO_ManagePremiums::add($params, $ids);
348
349 $this->assertEquals('TEST Premium', $premium->name, 'Check for premium name.');
350
351 $ids = array('contribution' => NULL);
352
353 $param = array(
354 'contact_id' => $contactId,
355 'currency' => 'USD',
92915c55 356 'financial_type_id' => 1,
6a488035
TO
357 'contribution_status_id' => 1,
358 'payment_instrument_id' => 1,
359 'source' => 'STUDENT',
360 'receive_date' => '20080522000000',
361 'receipt_date' => '20080522000000',
362 'id' => NULL,
363 'non_deductible_amount' => 0.00,
364 'total_amount' => 300.00,
365 'fee_amount' => 5,
366 'net_amount' => 295,
367 'trxn_id' => '33erdfrwvw434',
368 'invoice_id' => '98ed34f7u9hh672ce0eafe8fb92',
369 'thankyou_date' => '20080522',
370 );
371
372 $contribution = CRM_Contribute_BAO_Contribution::create($param, $ids);
373
374 $this->assertEquals($param['trxn_id'], $contribution->trxn_id, 'Check for transcation id creation.');
375 $this->assertEquals($contactId, $contribution->contact_id, 'Check for contact id creation.');
376
377 //parameter for adding premium to contribution
378 $data = array(
379 'product_id' => $premium->id,
380 'contribution_id' => $contribution->id,
381 'product_option' => NULL,
382 'quantity' => 1,
383 );
384 $contributionProduct = CRM_Contribute_BAO_Contribution::addPremium($data);
385 $this->assertEquals($contributionProduct->product_id, $premium->id, 'Check for Product id .');
386
387 //Delete Product
388 CRM_Contribute_BAO_ManagePremiums::del($premium->id);
389 $this->assertDBNull('CRM_Contribute_DAO_Product', $premium->name,
390 'id', 'name', 'Database check for deleted Product.'
391 );
6a488035
TO
392 }
393
394 /**
fe482240 395 * Check duplicate contribution id.
6a488035
TO
396 * during the contribution import
397 * checkDuplicateIds();
398 */
00be9182 399 public function testcheckDuplicateIds() {
2449fe69 400 $contactId = $this->individualCreate();
6a488035
TO
401
402 $param = array(
403 'contact_id' => $contactId,
404 'currency' => 'USD',
92915c55 405 'financial_type_id' => 1,
6a488035
TO
406 'contribution_status_id' => 1,
407 'payment_instrument_id' => 1,
408 'source' => 'STUDENT',
409 'receive_date' => '20080522000000',
410 'receipt_date' => '20080522000000',
411 'id' => NULL,
412 'non_deductible_amount' => 0.00,
413 'total_amount' => 300.00,
414 'fee_amount' => 5,
415 'net_amount' => 295,
416 'trxn_id' => '76ereeswww835',
417 'invoice_id' => '93ed39a9e9hd621bs0eafe3da82',
418 'thankyou_date' => '20080522',
419 );
420
2449fe69 421 $contribution = CRM_Contribute_BAO_Contribution::create($param);
6a488035
TO
422
423 $this->assertEquals($param['trxn_id'], $contribution->trxn_id, 'Check for transcation id creation.');
424 $this->assertEquals($contactId, $contribution->contact_id, 'Check for contact id creation.');
425 $data = array(
426 'id' => $contribution->id,
427 'trxn_id' => $contribution->trxn_id,
428 'invoice_id' => $contribution->invoice_id,
429 );
430 $contributionID = CRM_Contribute_BAO_Contribution::checkDuplicateIds($data);
431 $this->assertEquals($contributionID, $contribution->id, 'Check for duplicate transcation id .');
6a488035 432 }
96025800 433
8022372f
GC
434 /**
435 * Check credit note id creation
436 * when a contribution is cancelled or refunded
437 * createCreditNoteId();
438 */
439 public function testCreateCreditNoteId() {
2449fe69 440 $contactId = $this->individualCreate();
8022372f
GC
441
442 $param = array(
443 'contact_id' => $contactId,
444 'currency' => 'USD',
445 'financial_type_id' => 1,
446 'contribution_status_id' => 3,
447 'payment_instrument_id' => 1,
448 'source' => 'STUDENT',
449 'receive_date' => '20080522000000',
450 'receipt_date' => '20080522000000',
451 'id' => NULL,
452 'non_deductible_amount' => 0.00,
453 'total_amount' => 300.00,
454 'fee_amount' => 5,
455 'net_amount' => 295,
456 'trxn_id' => '76ereeswww835',
457 'invoice_id' => '93ed39a9e9hd621bs0eafe3da82',
458 'thankyou_date' => '20080522',
459 );
460
461 $creditNoteId = CRM_Contribute_BAO_Contribution::createCreditNoteId();
2449fe69 462 $contribution = CRM_Contribute_BAO_Contribution::create($param);
8022372f
GC
463 $this->assertEquals($contactId, $contribution->contact_id, 'Check for contact id creation.');
464 $this->assertEquals($creditNoteId, $contribution->creditnote_id, 'Check if credit note id is created correctly.');
8022372f
GC
465 }
466
b04e4b11
PN
467 /**
468 * Create() method (create and update modes).
469 */
470 public function testIsPaymentFlag() {
2449fe69 471 $contactId = $this->individualCreate();
b04e4b11
PN
472
473 $params = array(
474 'contact_id' => $contactId,
475 'currency' => 'USD',
476 'financial_type_id' => 1,
477 'contribution_status_id' => 1,
478 'payment_instrument_id' => 1,
479 'source' => 'STUDENT',
480 'receive_date' => '20080522000000',
481 'receipt_date' => '20080522000000',
482 'non_deductible_amount' => 0.00,
483 'total_amount' => 200.00,
484 'fee_amount' => 5,
485 'net_amount' => 195,
486 'trxn_id' => '22ereerwww4444xx',
487 'invoice_id' => '86ed39c9e9ee6ef6541621ce0eafe7eb81',
488 'thankyou_date' => '20080522',
489 );
490
2449fe69 491 $contribution = CRM_Contribute_BAO_Contribution::create($params);
b04e4b11
PN
492
493 $this->assertEquals($params['trxn_id'], $contribution->trxn_id, 'Check for transcation id creation.');
494 $this->assertEquals($contactId, $contribution->contact_id, 'Check for contact id creation.');
495
496 $trxnArray = array(
497 'trxn_id' => $params['trxn_id'],
498 'is_payment' => 1,
499 );
500 $defaults = array();
501 $financialTrxn = CRM_Core_BAO_FinancialTrxn::retrieve($trxnArray, $defaults);
4ba3c75f 502 $this->assertEquals(1, $financialTrxn->N, 'Mismatch count for is payment flag.');
b04e4b11
PN
503 //update contribution amount
504 $ids = array('contribution' => $contribution->id);
505 $params['total_amount'] = 150;
506
507 $contribution = CRM_Contribute_BAO_Contribution::create($params, $ids);
508
509 $this->assertEquals($params['trxn_id'], $contribution->trxn_id, 'Check for transcation id .');
510 $this->assertEquals($params['total_amount'], $contribution->total_amount, 'Check for Amount updation.');
511 $trxnArray = array(
512 'trxn_id' => $params['trxn_id'],
513 'is_payment' => 1,
514 );
515 $defaults = array();
516 $financialTrxn = CRM_Core_BAO_FinancialTrxn::retrieve($trxnArray, $defaults);
4ba3c75f 517 $this->assertEquals(2, $financialTrxn->N, 'Mismatch count for is payment flag.');
b04e4b11
PN
518 $trxnArray['is_payment'] = 0;
519 $financialTrxn = CRM_Core_BAO_FinancialTrxn::retrieve($trxnArray, $defaults);
4ba3c75f 520 $this->assertEquals(1, $financialTrxn->N, 'Mismatch count for is payment flag.');
b04e4b11
PN
521 }
522
a387acc9
PN
523 /**
524 * Create() method (create and update modes).
525 */
526 public function testIsPaymentFlagForPending() {
2449fe69 527 $contactId = $this->individualCreate();
a387acc9
PN
528
529 $params = array(
530 'contact_id' => $contactId,
531 'currency' => 'USD',
532 'financial_type_id' => 1,
533 'contribution_status_id' => 2,
534 'payment_instrument_id' => 1,
535 'source' => 'STUDENT',
536 'is_pay_later' => 1,
537 'receive_date' => '20080522000000',
538 'receipt_date' => '20080522000000',
539 'non_deductible_amount' => 0.00,
540 'total_amount' => 200.00,
541 'fee_amount' => 5,
542 'net_amount' => 195,
543 'trxn_id' => '22ereerwww4444yy',
544 'invoice_id' => '86ed39c9e9yy6ef6541621ce0eafe7eb81',
545 'thankyou_date' => '20080522',
546 );
547
2449fe69 548 $contribution = CRM_Contribute_BAO_Contribution::create($params);
a387acc9
PN
549
550 $this->assertEquals($params['trxn_id'], $contribution->trxn_id, 'Check for transcation id creation.');
551 $this->assertEquals($contactId, $contribution->contact_id, 'Check for contact id creation.');
552
553 $trxnArray = array(
554 'trxn_id' => $params['trxn_id'],
555 'is_payment' => 0,
556 );
557 $defaults = array();
558 $financialTrxn = CRM_Core_BAO_FinancialTrxn::retrieve($trxnArray, $defaults);
559 $this->assertEquals(2, $financialTrxn->N, 'Mismatch count for is payment flag.');
560 $trxnArray['is_payment'] = 1;
561 $financialTrxn = CRM_Core_BAO_FinancialTrxn::retrieve($trxnArray, $defaults);
562 $this->assertEquals(NULL, $financialTrxn, 'Mismatch count for is payment flag.');
563 //update contribution amount
564 $ids = array('contribution' => $contribution->id);
565 $params['contribution_status_id'] = 1;
566
567 $contribution = CRM_Contribute_BAO_Contribution::create($params, $ids);
568
569 $this->assertEquals($params['trxn_id'], $contribution->trxn_id, 'Check for transcation id .');
570 $this->assertEquals($params['contribution_status_id'], $contribution->contribution_status_id, 'Check for status updation.');
571 $trxnArray = array(
572 'trxn_id' => $params['trxn_id'],
573 'is_payment' => 1,
574 );
575 $defaults = array();
576 $financialTrxn = CRM_Core_BAO_FinancialTrxn::retrieve($trxnArray, $defaults);
577 $this->assertEquals(1, $financialTrxn->N, 'Mismatch count for is payment flag.');
578 $trxnArray['is_payment'] = 0;
579 $financialTrxn = CRM_Core_BAO_FinancialTrxn::retrieve($trxnArray, $defaults);
580 $this->assertEquals(2, $financialTrxn->N, 'Mismatch count for is payment flag.');
a387acc9
PN
581 }
582
eec619df 583 /**
0a5651eb 584 * addPayments() method (add and edit modes of participant)
eec619df
PN
585 */
586 public function testAddPayments() {
587 list($lineItems, $contribution) = $this->addParticipantWithContribution();
955ee56e 588 CRM_Contribute_BAO_Contribution::addPayments(array($contribution));
0a5651eb
PN
589 $this->checkItemValues($contribution);
590 }
591
592 /**
593 * checks db values for financial item
594 */
624195c8 595 public function checkItemValues($contribution) {
876b8ab0 596 $toFinancialAccount = CRM_Contribute_PseudoConstant::getRelationalFinancialAccount(4, 'Accounts Receivable Account is');
2449fe69 597 $query = "SELECT eft1.entity_id, ft.total_amount, eft1.amount FROM civicrm_financial_trxn ft INNER JOIN civicrm_entity_financial_trxn eft ON (eft.financial_trxn_id = ft.id AND eft.entity_table = 'civicrm_contribution')
eec619df
PN
598INNER JOIN civicrm_entity_financial_trxn eft1 ON (eft1.financial_trxn_id = eft.financial_trxn_id AND eft1.entity_table = 'civicrm_financial_item')
599WHERE eft.entity_id = %1 AND ft.to_financial_account_id <> %2";
600
601 $queryParams[1] = array($contribution->id, 'Integer');
602 $queryParams[2] = array($toFinancialAccount, 'Integer');
603
604 $dao = CRM_Core_DAO::executeQuery($query, $queryParams);
19084b68 605 $amounts = array(100.00, 50.00);
eec619df
PN
606 while ($dao->fetch()) {
607 $this->assertEquals(150.00, $dao->total_amount, 'Mismatch of total amount paid.');
19084b68 608 $this->assertEquals($dao->amount, array_pop($amounts), 'Mismatch of amount proportionally assigned to financial item');
eec619df 609 }
eec619df
PN
610 }
611
0a5651eb
PN
612 /**
613 * assignProportionalLineItems() method (add and edit modes of participant)
614 */
615 public function testAssignProportionalLineItems() {
616 list($lineItems, $contribution) = $this->addParticipantWithContribution();
0a5651eb
PN
617 $params = array(
618 'contribution_id' => $contribution->id,
619 'total_amount' => 150.00,
620 );
621 $trxn = new CRM_Financial_DAO_FinancialTrxn();
622 $trxn->orderBy('id DESC');
623 $trxn->find(TRUE);
8de1ade9 624 CRM_Contribute_BAO_Contribution::assignProportionalLineItems($params, $trxn->id, $contribution->total_amount);
0a5651eb
PN
625 $this->checkItemValues($contribution);
626 }
627
eec619df
PN
628 /**
629 * Add participant with contribution
630 *
631 * @return array
632 */
78c99516 633 public function addParticipantWithContribution() {
eec619df 634 // creating price set, price field
2449fe69 635 $this->_contactId = $this->individualCreate();
636 $event = $this->eventCreate();
637 $this->_eventId = $event['id'];
624195c8 638 $paramsSet['title'] = 'Price Set' . substr(sha1(rand()), 0, 4);
19084b68 639 $paramsSet['name'] = CRM_Utils_String::titleToVar($paramsSet['title']);
eec619df
PN
640 $paramsSet['is_active'] = TRUE;
641 $paramsSet['financial_type_id'] = 4;
642 $paramsSet['extends'] = 1;
643
644 $priceset = CRM_Price_BAO_PriceSet::create($paramsSet);
645 $priceSetId = $priceset->id;
646
647 //Checking for priceset added in the table.
648 $this->assertDBCompareValue('CRM_Price_BAO_PriceSet', $priceSetId, 'title',
649 'id', $paramsSet['title'], 'Check DB for created priceset'
650 );
651 $paramsField = array(
652 'label' => 'Price Field',
653 'name' => CRM_Utils_String::titleToVar('Price Field'),
654 'html_type' => 'CheckBox',
655 'option_label' => array('1' => 'Price Field 1', '2' => 'Price Field 2'),
656 'option_value' => array('1' => 100, '2' => 200),
657 'option_name' => array('1' => 'Price Field 1', '2' => 'Price Field 2'),
658 'option_weight' => array('1' => 1, '2' => 2),
659 'option_amount' => array('1' => 100, '2' => 200),
660 'is_display_amounts' => 1,
661 'weight' => 1,
662 'options_per_line' => 1,
663 'is_active' => array('1' => 1, '2' => 1),
664 'price_set_id' => $priceset->id,
665 'is_enter_qty' => 1,
666 'financial_type_id' => CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_FinancialType', 'Event Fee', 'id', 'name'),
667 );
668 $priceField = CRM_Price_BAO_PriceField::create($paramsField);
669 $eventParams = array(
670 'id' => $this->_eventId,
671 'financial_type_id' => 4,
672 'is_monetary' => 1,
673 );
674 CRM_Event_BAO_Event::create($eventParams);
675 CRM_Price_BAO_PriceSet::addTo('civicrm_event', $this->_eventId, $priceSetId);
e4ba8498 676
eec619df
PN
677 $priceFields = $this->callAPISuccess('PriceFieldValue', 'get', array('price_field_id' => $priceField->id));
678 $participantParams = array(
679 'financial_type_id' => 4,
680 'event_id' => $this->_eventId,
681 'role_id' => 1,
682 'status_id' => 14,
683 'fee_currency' => 'USD',
684 'contact_id' => $this->_contactId,
685 );
686 $participant = CRM_Event_BAO_Participant::add($participantParams);
687 $contributionParams = array(
688 'total_amount' => 150,
689 'currency' => 'USD',
690 'contact_id' => $this->_contactId,
691 'financial_type_id' => 4,
692 'contribution_status_id' => 1,
693 'partial_payment_total' => 300.00,
694 'partial_amount_pay' => 150,
695 'contribution_mode' => 'participant',
696 'participant_id' => $participant->id,
697 );
698
699 foreach ($priceFields['values'] as $key => $priceField) {
700 $lineItems[1][$key] = array(
701 'price_field_id' => $priceField['price_field_id'],
702 'price_field_value_id' => $priceField['id'],
703 'label' => $priceField['label'],
704 'field_title' => $priceField['label'],
705 'qty' => 1,
706 'unit_price' => $priceField['amount'],
707 'line_total' => $priceField['amount'],
708 'financial_type_id' => $priceField['financial_type_id'],
709 );
710 }
711 $contributionParams['line_item'] = $lineItems;
712 $contributions = CRM_Contribute_BAO_Contribution::create($contributionParams);
713
714 $paymentParticipant = array(
715 'participant_id' => $participant->id,
716 'contribution_id' => $contributions->id,
717 );
718 $ids = array();
719 CRM_Event_BAO_ParticipantPayment::create($paymentParticipant, $ids);
720
721 return array($lineItems, $contributions);
722 }
723
5c8b902b 724 /**
a4a31ffe 725 * checkLineItems() check if total amount matches the sum of line total
5c8b902b
PN
726 */
727 public function testcheckLineItems() {
728 $params = array(
729 'contact_id' => 202,
730 'receive_date' => '2010-01-20',
731 'total_amount' => 100,
732 'financial_type_id' => 3,
733 'line_items' => array(
734 array(
735 'line_item' => array(
736 array(
737 'entity_table' => 'civicrm_contribution',
738 'price_field_id' => 8,
739 'price_field_value_id' => 16,
740 'label' => 'test 1',
741 'qty' => 1,
742 'unit_price' => 100,
743 'line_total' => 100,
744 ),
745 array(
746 'entity_table' => 'civicrm_contribution',
747 'price_field_id' => 8,
748 'price_field_value_id' => 17,
749 'label' => 'Test 2',
750 'qty' => 1,
751 'unit_price' => 200,
752 'line_total' => 200,
753 'financial_type_id' => 1,
754 ),
755 ),
a4a31ffe 756 'params' => array(),
5c8b902b 757 ),
a4a31ffe 758 ),
5c8b902b 759 );
af004c04 760 try {
2449fe69 761 CRM_Contribute_BAO_Contribution::checkLineItems($params);
af004c04
PN
762 $this->fail("Missed expected exception");
763 }
764 catch (Exception $e) {
765 $this->assertEquals("Line item total doesn't match with total amount.", $e->getMessage());
766 }
5c8b902b
PN
767 $this->assertEquals(3, $params['line_items'][0]['line_item'][0]['financial_type_id']);
768 $params['total_amount'] = 300;
af004c04 769 CRM_Contribute_BAO_Contribution::checkLineItems($params);
5c8b902b
PN
770 }
771
88f7a518
E
772 /**
773 * Test activity amount updation.
774 */
775 public function testActivityCreate() {
776 $contactId = $this->individualCreate();
777 $defaults = array();
778
779 $params = array(
780 'contact_id' => $contactId,
781 'currency' => 'USD',
782 'financial_type_id' => 1,
783 'contribution_status_id' => 1,
784 'payment_instrument_id' => 1,
785 'source' => 'STUDENT',
786 'receive_date' => '20080522000000',
787 'receipt_date' => '20080522000000',
788 'non_deductible_amount' => 0.00,
789 'total_amount' => 100.00,
790 'trxn_id' => '22ereerwww444444',
791 'invoice_id' => '86ed39c9e9ee6ef6031621ce0eafe7eb81',
792 'thankyou_date' => '20160519',
793 );
794
795 $contribution = CRM_Contribute_BAO_Contribution::create($params);
796
797 $this->assertEquals($params['total_amount'], $contribution->total_amount, 'Check for total amount in contribution.');
798 $this->assertEquals($contactId, $contribution->contact_id, 'Check for contact id creation.');
799
800 // Check amount in activity.
801 $activityParams = array(
802 'source_record_id' => $contribution->id,
803 'activity_type_id' => CRM_Core_OptionGroup::getValue('activity_type',
804 'Contribution',
805 'name'
806 ),
807 );
808 $activity = CRM_Activity_BAO_Activity::retrieve($activityParams, $defaults);
809
810 $this->assertEquals($contribution->id, $activity->source_record_id, 'Check for activity associated with contribution.');
811 $this->assertEquals("$ 100.00 - STUDENT", $activity->subject, 'Check for total amount in activity.');
812
813 // Update contribution amount.
814 $ids = array('contribution' => $contribution->id);
815 $params['total_amount'] = 200;
816
817 $contribution = CRM_Contribute_BAO_Contribution::create($params, $ids);
818
819 $this->assertEquals($params['total_amount'], $contribution->total_amount, 'Check for total amount in contribution.');
820 $this->assertEquals($contactId, $contribution->contact_id, 'Check for contact id creation.');
821
822 // Retrieve activity again.
823 $activity = CRM_Activity_BAO_Activity::retrieve($activityParams, $defaults);
824
825 $this->assertEquals($contribution->id, $activity->source_record_id, 'Check for activity associated with contribution.');
826 $this->assertEquals("$ 200.00 - STUDENT", $activity->subject, 'Check for total amount in activity.');
827 }
828
ce7fc91a
PN
829 /**
830 * Test checkContributeSettings.
831 */
832 public function testCheckContributeSettings() {
833 $settings = CRM_Contribute_BAO_Contribution::checkContributeSettings('deferred_revenue_enabled');
834 $this->assertNull($settings);
835 $params = array(
836 'contribution_invoice_settings' => array(
837 'deferred_revenue_enabled' => '1',
838 ),
839 );
840 $this->callAPISuccess('Setting', 'create', $params);
841 $settings = CRM_Contribute_BAO_Contribution::checkContributeSettings('deferred_revenue_enabled');
842 $this->assertEquals($settings, 1, 'Check for settings has failed');
843 }
844
5c3d600f
PN
845 /**
846 * Test allowUpdateRevenueRecognitionDate.
847 */
848 public function testAllowUpdateRevenueRecognitionDate() {
849 $contactId = $this->individualCreate();
850 $params = array(
851 'contact_id' => $contactId,
852 'receive_date' => '2010-01-20',
853 'total_amount' => 100,
4303ea89 854 'financial_type_id' => 4,
5c3d600f
PN
855 );
856 $order = $this->callAPISuccess('order', 'create', $params);
857 $allowUpdate = CRM_Contribute_BAO_Contribution::allowUpdateRevenueRecognitionDate($order['id']);
858 $this->assertTrue($allowUpdate);
859
860 $event = $this->eventCreate();
861 $params = array(
862 'contact_id' => $contactId,
863 'receive_date' => '2010-01-20',
864 'total_amount' => 300,
8484a5f0 865 'financial_type_id' => $this->getFinancialTypeId('Event Fee'),
866 'contribution_status_id' => 'Completed',
5c3d600f
PN
867 );
868 $priceFields = $this->createPriceSet('event', $event['id']);
869 foreach ($priceFields['values'] as $key => $priceField) {
870 $lineItems[$key] = array(
871 'price_field_id' => $priceField['price_field_id'],
872 'price_field_value_id' => $priceField['id'],
873 'label' => $priceField['label'],
874 'field_title' => $priceField['label'],
875 'qty' => 1,
876 'unit_price' => $priceField['amount'],
877 'line_total' => $priceField['amount'],
878 'financial_type_id' => $priceField['financial_type_id'],
879 'entity_table' => 'civicrm_participant',
880 );
881 }
882 $params['line_items'][] = array(
883 'line_item' => $lineItems,
884 'params' => array(
885 'contact_id' => $contactId,
886 'event_id' => $event['id'],
887 'status_id' => 1,
888 'role_id' => 1,
889 'register_date' => '2007-07-21 00:00:00',
890 'source' => 'Online Event Registration: API Testing',
891 ),
892 );
893 $order = $this->callAPISuccess('order', 'create', $params);
894 $allowUpdate = CRM_Contribute_BAO_Contribution::allowUpdateRevenueRecognitionDate($order['id']);
895 $this->assertFalse($allowUpdate);
896
897 $params = array(
898 'contact_id' => $contactId,
899 'receive_date' => '2010-01-20',
900 'total_amount' => 200,
8484a5f0 901 'financial_type_id' => $this->getFinancialTypeId('Member Dues'),
902 'contribution_status_id' => 'Completed',
5c3d600f
PN
903 );
904 $membershipType = $this->membershipTypeCreate();
905 $priceFields = $this->createPriceSet();
906 $lineItems = array();
907 foreach ($priceFields['values'] as $key => $priceField) {
908 $lineItems[$key] = array(
909 'price_field_id' => $priceField['price_field_id'],
910 'price_field_value_id' => $priceField['id'],
911 'label' => $priceField['label'],
912 'field_title' => $priceField['label'],
913 'qty' => 1,
914 'unit_price' => $priceField['amount'],
915 'line_total' => $priceField['amount'],
916 'financial_type_id' => $priceField['financial_type_id'],
917 'entity_table' => 'civicrm_membership',
918 'membership_type_id' => $membershipType,
919 );
920 }
921 $params['line_items'][] = array(
922 'line_item' => array(array_pop($lineItems)),
923 'params' => array(
924 'contact_id' => $contactId,
925 'membership_type_id' => $membershipType,
926 'join_date' => '2006-01-21',
927 'start_date' => '2006-01-21',
928 'end_date' => '2006-12-21',
929 'source' => 'Payment',
930 'is_override' => 1,
931 'status_id' => 1,
932 ),
933 );
934 $order = $this->callAPISuccess('order', 'create', $params);
935 $allowUpdate = CRM_Contribute_BAO_Contribution::allowUpdateRevenueRecognitionDate($order['id']);
936 $this->assertFalse($allowUpdate);
937 }
938
d9553c2e
PN
939 /**
940 * Test calculateFinancialItemAmount().
941 */
942 public function testcalculateFinancialItemAmount() {
943 $testParams = array(
944 array(
945 'params' => array(),
946 'amountParams' => array(
947 'line_total' => 100,
948 'previous_line_total' => 300,
949 'diff' => 1,
950 ),
951 'context' => 'changedAmount',
952 'expectedItemAmount' => -200,
953 ),
954 array(
955 'params' => array(),
956 'amountParams' => array(
957 'line_total' => 100,
958 'previous_line_total' => 100,
959 'diff' => -1,
960 ),
961 'context' => 'changePaymentInstrument',
962 'expectedItemAmount' => -100,
963 ),
964 array(
965 'params' => array(
966 'is_quick_config' => TRUE,
967 'total_amount' => 110,
968 'tax_amount' => 10,
969 ),
970 'amountParams' => array(
971 'item_amount' => 100,
972 ),
973 'context' => 'changedAmount',
974 'expectedItemAmount' => 100,
975 ),
976 array(
977 'params' => array(
978 'is_quick_config' => TRUE,
979 'total_amount' => 110,
980 'tax_amount' => 10,
981 ),
982 'amountParams' => array(
983 'item_amount' => NULL,
984 ),
985 'context' => 'changedAmount',
986 'expectedItemAmount' => 110,
987 ),
988 array(
989 'params' => array(
990 'is_quick_config' => TRUE,
991 'total_amount' => 110,
992 'tax_amount' => 10,
993 ),
994 'amountParams' => array(
995 'item_amount' => NULL,
996 ),
997 'context' => NULL,
998 'expectedItemAmount' => 100,
999 ),
1000 );
1001 foreach ($testParams as $params) {
1002 $itemAmount = CRM_Contribute_BAO_Contribution::calculateFinancialItemAmount($params['params'], $params['amountParams'], $params['context']);
1003 $this->assertEquals($itemAmount, $params['expectedItemAmount'], 'Invalid Financial Item amount.');
1004 }
1005 }
1006
d934a732
PN
1007 /**
1008 * Test recording of amount with comma separator.
1009 */
1010 public function testCommaSeparatorAmount() {
1011 $contactId = $this->individualCreate();
1012
1013 $params = array(
1014 'contact_id' => $contactId,
1015 'currency' => 'USD',
1016 'financial_type_id' => 1,
1017 'contribution_status_id' => 8,
1018 'payment_instrument_id' => 1,
1019 'receive_date' => '20080522000000',
1020 'receipt_date' => '20080522000000',
1021 'total_amount' => '20000.00',
1022 'partial_payment_total' => '20,000.00',
1023 'partial_amount_pay' => '8,000.00',
1024 );
1025
1026 $contribution = CRM_Contribute_BAO_Contribution::create($params);
1027 $lastFinancialTrxnId = CRM_Core_BAO_FinancialTrxn::getFinancialTrxnId($contribution->id, 'DESC');
1028 $financialTrxn = $this->callAPISuccessGetSingle(
1029 'FinancialTrxn',
1030 array(
1031 'id' => $lastFinancialTrxnId['financialTrxnId'],
1032 'return' => array('total_amount'),
1033 )
1034 );
1035 $this->assertEquals($financialTrxn['total_amount'], 8000, 'Invalid Tax amount.');
1036 }
1037
adbc354b
PN
1038 /**
1039 * Test for function getSalesTaxFinancialAccounts().
1040 */
1041 public function testgetSalesTaxFinancialAccounts() {
1042 $this->enableTaxAndInvoicing();
1043 $financialType = $this->createFinancialType();
1044 $financialAccount = $this->relationForFinancialTypeWithFinancialAccount($financialType['id']);
1045 $expectedResult = array($financialAccount->financial_account_id => $financialAccount->financial_account_id);
1046 $financialType = $this->createFinancialType();
1047 $financialAccount = $this->relationForFinancialTypeWithFinancialAccount($financialType['id']);
1048 $expectedResult[$financialAccount->financial_account_id] = $financialAccount->financial_account_id;
1049 $salesTaxFinancialAccount = CRM_Contribute_BAO_Contribution::getSalesTaxFinancialAccounts();
1050 $this->assertTrue(($salesTaxFinancialAccount == $expectedResult), 'Function returned wrong values.');
1051 }
1052
0409b821
PN
1053 /**
1054 * Test for function createProportionalEntry().
1055 */
1056 public function testcreateProportionalEntry() {
2a4a2f00 1057 list($contribution, $financialAccount) = $this->createContributionWithTax();
0409b821
PN
1058 $params = array(
1059 'total_amount' => 55,
1060 'to_financial_account_id' => $financialAccount->financial_account_id,
1061 'payment_instrument_id' => 1,
1062 'trxn_date' => date('Ymd'),
1063 'status_id' => 1,
1064 );
1065 $financialTrxn = $this->callAPISuccess('FinancialTrxn', 'create', $params);
1066 $params = array(
1067 'amount' => 50,
1068 'entity_table' => 'civicrm_contribution',
1069 'entity_id' => $contribution['id'],
1070 'financial_trxn_id' => $financialTrxn['id'],
1071 );
1072 $this->callAPISuccess('EntityFinancialTrxn', 'create', $params);
1073 $entityParams = array(
1074 'contribution_total_amount' => $contribution['total_amount'],
1075 'trxn_total_amount' => 55,
1076 'line_item_amount' => 100,
1077 );
1078 $previousLineItem = CRM_Financial_BAO_FinancialItem::getPreviousFinancialItem($contribution['id']);
1079 $eftParams = array(
1080 'entity_table' => 'civicrm_financial_item',
1081 'entity_id' => $previousLineItem->id,
1082 'financial_trxn_id' => $financialTrxn['id'],
1083 );
1084 CRM_Contribute_BAO_Contribution::createProportionalEntry($entityParams, $eftParams);
1085 $trxnTestArray = array_merge($eftParams, array(
1086 'amount' => 50,
1087 ));
1088 $entityFinancialTrxn = $this->callAPISuccessGetSingle('EntityFinancialTrxn', $eftParams, $trxnTestArray);
1089 }
1090
646bc565
PN
1091 /**
1092 * Test for function getLastFinancialItemIds().
1093 */
1094 public function testgetLastFinancialItemIds() {
2a4a2f00 1095 list($contribution, $financialAccount) = $this->createContributionWithTax();
646bc565
PN
1096 list($ftIds, $taxItems) = CRM_Contribute_BAO_Contribution::getLastFinancialItemIds($contribution['id']);
1097 $this->assertEquals(count($ftIds), 1, 'Invalid count.');
1098 $this->assertEquals(count($taxItems), 1, 'Invalid count.');
1099 foreach ($taxItems as $value) {
1100 $this->assertEquals($value['amount'], 10, 'Invalid tax amount.');
1101 }
1102 }
1103
2a4a2f00
PN
1104 /**
1105 * Test for function createProportionalFinancialEntries().
1106 */
1107 public function testcreateProportionalFinancialEntries() {
1108 list($contribution, $financialAccount) = $this->createContributionWithTax();
1109 $params = array(
1110 'total_amount' => 55,
1111 'to_financial_account_id' => $financialAccount->financial_account_id,
1112 'payment_instrument_id' => 1,
1113 'trxn_date' => date('Ymd'),
1114 'status_id' => 1,
1115 );
1116 $financialTrxn = $this->callAPISuccess('FinancialTrxn', 'create', $params);
1117 $params = array(
1118 'amount' => 50,
1119 'entity_table' => 'civicrm_contribution',
1120 'entity_id' => $contribution['id'],
1121 'financial_trxn_id' => $financialTrxn['id'],
1122 );
1123 $this->callAPISuccess('EntityFinancialTrxn', 'create', $params);
1124 $entityParams = array(
1125 'contribution_total_amount' => $contribution['total_amount'],
1126 'trxn_total_amount' => 55,
1127 'trxn_id' => $financialTrxn['id'],
1128 );
1129 $lineItems = CRM_Price_BAO_LineItem::getLineItemsByContributionID($contribution['id']);
1130 list($ftIds, $taxItems) = CRM_Contribute_BAO_Contribution::getLastFinancialItemIds($contribution['id']);
1131 CRM_Contribute_BAO_Contribution::createProportionalFinancialEntries($entityParams, $lineItems, $ftIds, $taxItems);
1132 $eftParams = array(
1133 'entity_table' => 'civicrm_financial_item',
1134 'financial_trxn_id' => $financialTrxn['id'],
1135 );
1136 $entityFinancialTrxn = $this->callAPISuccess('EntityFinancialTrxn', 'Get', $eftParams);
1137 $this->assertEquals($entityFinancialTrxn['count'], 2, 'Invalid count.');
1138 $testAmount = array(5, 50);
1139 foreach ($entityFinancialTrxn['values'] as $value) {
1140 $this->assertEquals($value['amount'], array_pop($testAmount), 'Invalid amount stored in civicrm_entity_financial_trxn.');
77641de4
PN
1141 }
1142 }
1143
1144 /**
1145 * Test to check if amount is proportionally asigned for PI change.
1146 */
1147 public function testProportionallyAssignedForPIChange() {
1148 list($contribution, $financialAccount) = $this->createContributionWithTax();
1149 $params = array(
1150 'id' => $contribution['id'],
1151 'payment_instrument_id' => 3,
1152 );
1153 $this->callAPISuccess('Contribution', 'create', $params);
1154 $lastFinancialTrxnId = CRM_Core_BAO_FinancialTrxn::getFinancialTrxnId($contribution['id'], 'DESC');
1155 $eftParams = array(
1156 'entity_table' => 'civicrm_financial_item',
1157 'financial_trxn_id' => $lastFinancialTrxnId['financialTrxnId'],
1158 );
1159 $entityFinancialTrxn = $this->callAPISuccess('EntityFinancialTrxn', 'Get', $eftParams);
1160 $this->assertEquals($entityFinancialTrxn['count'], 2, 'Invalid count.');
1161 $testAmount = array(10, 100);
1162 foreach ($entityFinancialTrxn['values'] as $value) {
1163 $this->assertEquals($value['amount'], array_pop($testAmount), 'Invalid amount stored in civicrm_entity_financial_trxn.');
2a4a2f00
PN
1164 }
1165 }
1166
646bc565
PN
1167 /**
1168 * Function to create contribution with tax.
1169 */
1170 public function createContributionWithTax() {
1171 $contactId = $this->individualCreate();
1172 $this->enableTaxAndInvoicing();
1173 $financialType = $this->createFinancialType();
1174 $financialAccount = $this->relationForFinancialTypeWithFinancialAccount($financialType['id']);
1175 $form = new CRM_Contribute_Form_Contribution();
1176
1177 $form->testSubmit(array(
1178 'total_amount' => 100,
1179 'financial_type_id' => $financialType['id'],
1180 'receive_date' => '04/21/2015',
1181 'receive_date_time' => '11:27PM',
1182 'contact_id' => $contactId,
1183 'contribution_status_id' => 1,
1184 'price_set_id' => 0,
1185 ),
1186 CRM_Core_Action::ADD
1187 );
1188 $contribution = $this->callAPISuccessGetSingle('Contribution',
1189 array(
1190 'contact_id' => $contactId,
1191 'return' => array('tax_amount', 'total_amount'),
1192 )
1193 );
2a4a2f00 1194 return array($contribution, $financialAccount);
646bc565
PN
1195 }
1196
7e2ec997
E
1197 /**
1198 * Test for replaceContributionTokens.
1199 * This function tests whether the contribution tokens are replaced with values from contribution.
1200 */
1201 public function testReplaceContributionTokens() {
1202 $contactId1 = $this->individualCreate();
1203 $params = array(
1204 'contact_id' => $contactId1,
1205 'receive_date' => '20120511',
1206 'total_amount' => 100.00,
1207 'financial_type_id' => 1,
1208 'trxn_id' => 12345,
1209 'invoice_id' => 67890,
1210 'source' => 'SSF',
1211 'contribution_status_id' => 2,
1212 );
1213 $contribution1 = $this->contributionCreate($params);
1214 $contactId2 = $this->individualCreate();
1215 $params = array(
1216 'contact_id' => $contactId2,
1217 'receive_date' => '20150511',
1218 'total_amount' => 200.00,
1219 'financial_type_id' => 1,
1220 'trxn_id' => 6789,
1221 'invoice_id' => 12345,
1222 'source' => 'ABC',
1223 'contribution_status_id' => 1,
1224 );
1225 $contribution2 = $this->contributionCreate($params);
1226 $ids = array($contribution1, $contribution2);
1227
1228 $subject = "This is a test for contribution ID: {contribution.contribution_id}";
1229 $text = "Contribution Amount: {contribution.total_amount}";
1230 $html = "<p>Contribution Source: {contribution.contribution_source}</p></br>
1231 <p>Contribution Invoice ID: {contribution.invoice_id}</p></br>
1232 <p>Contribution Receive Date: {contribution.receive_date}</p></br>";
1233
1234 $subjectToken = CRM_Utils_Token::getTokens($subject);
1235 $messageToken = CRM_Utils_Token::getTokens($text);
1236 $messageToken = array_merge($messageToken, CRM_Utils_Token::getTokens($html));
1237
1238 $contributionDetails = CRM_Contribute_BAO_Contribution::replaceContributionTokens(
1239 $ids,
1240 $subject,
1241 $subjectToken,
1242 $text,
1243 $html,
1244 $messageToken,
1245 TRUE
1246 );
1247
1248 $this->assertEquals("Contribution Amount: $ 100.00", $contributionDetails[$contactId1]['text'], "The text does not match");
1249 $this->assertEquals("<p>Contribution Source: ABC</p></br>
1250 <p>Contribution Invoice ID: 12345</p></br>
1251 <p>Contribution Receive Date: May 11th, 2015</p></br>", $contributionDetails[$contactId2]['html'], "The html does not match");
1252 }
1253
6a488035 1254}