Merge pull request #7764 from pradpnayak/CRM-16617-master
[civicrm-core.git] / tests / phpunit / CRM / Core / BAO / FinancialTrxnTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2016 |
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 /**
29 * Class CRM_Core_BAO_FinancialTrxnTest
30 * @group headless
31 */
32 class CRM_Core_BAO_FinancialTrxnTest extends CiviUnitTestCase {
33 public function setUp() {
34 parent::setUp();
35 }
36
37 /**
38 * Check method create().
39 */
40 public function testCreate() {
41 $contactId = $this->individualCreate();
42 $financialTypeId = 1;
43 $this->contributionCreate(array(
44 'contact_id' => $contactId,
45 'financial_type_id' => $financialTypeId,
46 ));
47 $params = array(
48 'contribution_id' => $financialTypeId,
49 'to_financial_account_id' => 1,
50 'trxn_date' => 20091021184930,
51 'trxn_type' => 'Debit',
52 'total_amount' => 10,
53 'net_amount' => 90.00,
54 'currency' => 'USD',
55 'payment_processor' => 'Dummy',
56 'trxn_id' => 'test_01014000',
57 );
58 $FinancialTrxn = CRM_Core_BAO_FinancialTrxn::create($params);
59
60 $result = $this->assertDBNotNull('CRM_Core_BAO_FinancialTrxn', $FinancialTrxn->id,
61 'total_amount', 'id',
62 'Database check on updated financial trxn record.'
63 );
64
65 $this->assertEquals($result, 10, 'Verify financial trxn total_amount.');
66 }
67
68 /**
69 * Test getTotalPayments function.
70 */
71 public function testGetTotalPayments() {
72 $contactId = $this->individualCreate();
73
74 $params = array(
75 'contact_id' => $contactId,
76 'currency' => 'USD',
77 'financial_type_id' => 1,
78 'contribution_status_id' => 2,
79 'payment_instrument_id' => 1,
80 'source' => 'STUDENT',
81 'is_pay_later' => 1,
82 'receive_date' => '20080522000000',
83 'receipt_date' => '20080522000000',
84 'non_deductible_amount' => 0.00,
85 'total_amount' => 200.00,
86 'fee_amount' => 5,
87 'net_amount' => 195,
88 'trxn_id' => '22ereerwwe4444yy',
89 'invoice_id' => '86ed39e9e9yy6ef6541621ce0eafe7eb81',
90 'thankyou_date' => '20080522',
91 );
92
93 $contribution = CRM_Contribute_BAO_Contribution::create($params);
94
95 $this->assertEquals($params['trxn_id'], $contribution->trxn_id);
96 $this->assertEquals($contactId, $contribution->contact_id);
97
98 $totalPaymentAmount = CRM_Core_BAO_FinancialTrxn::getTotalPayments($contribution->id);
99 $this->assertEquals(0, $totalPaymentAmount, 'Amount not matching.');
100 //update contribution amount
101 $params['id'] = $contribution->id;
102 $params['contribution_status_id'] = 1;
103
104 $contribution = CRM_Contribute_BAO_Contribution::create($params);
105
106 $this->assertEquals($params['trxn_id'], $contribution->trxn_id);
107 $this->assertEquals($params['contribution_status_id'], $contribution->contribution_status_id);
108
109 $totalPaymentAmount = CRM_Core_BAO_FinancialTrxn::getTotalPayments($contribution->id);
110 $this->assertEquals('200.00', $totalPaymentAmount, 'Amount not matching.');
111 }
112
113 /**
114 * Test getPartialPaymentTrxn function.
115 */
116 public function testGetPartialPaymentTrxn() {
117 $contributionTest = new CRM_Contribute_BAO_ContributionTest();
118 list($lineItems, $contribution) = $contributionTest->addParticipantWithContribution();
119 $contribution = (array) $contribution;
120 $params = array(
121 'contribution_id' => $contribution['id'],
122 'total_amount' => 100.00,
123 );
124 $trxn = CRM_Core_BAO_FinancialTrxn::getPartialPaymentTrxn($contribution, $params);
125
126 $this->assertEquals('100.00', $trxn->total_amount, 'Amount does not match.');
127
128 $totalPaymentAmount = CRM_Core_BAO_FinancialTrxn::getTotalPayments($contribution['id']);
129 $this->assertEquals('250.00', $totalPaymentAmount, 'Amount does not match.');
130 }
131
132 }