Merge pull request #17253 from mattwire/utf8convertblocksize
[civicrm-core.git] / tests / phpunit / CRM / Core / BAO / FinancialTrxnTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 * Class CRM_Core_BAO_FinancialTrxnTest
14 * @group headless
15 */
16 class CRM_Core_BAO_FinancialTrxnTest extends CiviUnitTestCase {
17
18 public function setUp() {
19 parent::setUp();
20 }
21
22 /**
23 * Check method create().
24 *
25 * @throws \CRM_Core_Exception
26 */
27 public function testCreate() {
28 $contactId = $this->individualCreate();
29 $financialTypeId = 1;
30 $this->contributionCreate([
31 'contact_id' => $contactId,
32 'financial_type_id' => $financialTypeId,
33 ]);
34 $params = [
35 'contribution_id' => $financialTypeId,
36 'to_financial_account_id' => 1,
37 'trxn_date' => 20091021184930,
38 'trxn_type' => 'Debit',
39 'total_amount' => 10,
40 'net_amount' => 90.00,
41 'currency' => 'USD',
42 'payment_processor' => 'Dummy',
43 'trxn_id' => 'test_01014000',
44 ];
45 $FinancialTrxn = CRM_Core_BAO_FinancialTrxn::create($params);
46
47 $result = $this->assertDBNotNull('CRM_Core_BAO_FinancialTrxn', $FinancialTrxn->id,
48 'total_amount', 'id',
49 'Database check on updated financial trxn record.'
50 );
51
52 $this->assertEquals($result, 10, 'Verify financial trxn total_amount.');
53 }
54
55 /**
56 * Test getTotalPayments function.
57 *
58 * @throws \CRM_Core_Exception
59 */
60 public function testGetTotalPayments() {
61 $contactId = $this->individualCreate();
62
63 $params = [
64 'contact_id' => $contactId,
65 'currency' => 'USD',
66 'financial_type_id' => 1,
67 'contribution_status_id' => 2,
68 'payment_instrument_id' => 1,
69 'source' => 'STUDENT',
70 'is_pay_later' => 1,
71 'receive_date' => '20080522000000',
72 'receipt_date' => '20080522000000',
73 'non_deductible_amount' => 0.00,
74 'total_amount' => 200.00,
75 'fee_amount' => 5,
76 'net_amount' => 195,
77 'trxn_id' => '22ereerwwe4444yy',
78 'invoice_id' => '86ed39e9e9yy6ef6541621ce0eafe7eb81',
79 'thankyou_date' => '20080522',
80 ];
81
82 $contribution = $this->callAPISuccess('Contribution', 'create', $params);
83 $contribution = $contribution['values'][$contribution['id']];
84
85 $totalPaymentAmount = CRM_Core_BAO_FinancialTrxn::getTotalPayments($contribution['id']);
86 $this->assertEquals(0, $totalPaymentAmount, 'Amount not matching.');
87
88 $params['id'] = $contribution['id'];
89 $params['contribution_status_id'] = 1;
90
91 $contribution = $this->callAPISuccess('Contribution', 'create', $params);
92
93 $totalPaymentAmount = CRM_Core_BAO_FinancialTrxn::getTotalPayments($contribution['id']);
94 $this->assertEquals('200.00', $totalPaymentAmount, 'Amount not matching.');
95 }
96
97 /**
98 * Tests the lines of code that used to be in the getPartialPaymentTrxn fn.
99 *
100 * @throws \CRM_Core_Exception
101 */
102 public function testGetTotalPaymentsParticipantOrder() {
103 $orderID = $this->createPartiallyPaidParticipantOrder()['id'];
104 $params = [
105 'contribution_id' => $orderID,
106 'total_amount' => 100.00,
107 ];
108 $this->callAPISuccess('Payment', 'create', $params);
109 $totalPaymentAmount = CRM_Core_BAO_FinancialTrxn::getTotalPayments($orderID);
110 $this->assertEquals('250.00', $totalPaymentAmount, 'Amount does not match.');
111 }
112
113 /**
114 * Test for createDeferredTrxn().
115 *
116 * @throws \CRM_Core_Exception
117 */
118 public function testCreateDeferredTrxn() {
119 Civi::settings()->set('deferred_revenue_enabled', TRUE);
120 $cid = $this->individualCreate();
121 $params = [
122 'contact_id' => $cid,
123 'receive_date' => '2016-01-20',
124 'total_amount' => 622,
125 'financial_type_id' => 4,
126 'contribution_status_id' => 'Pending',
127 'api.Payment.create' => ['total_amount' => 622, 'trxn_date' => '2016-01-20'],
128 ];
129 $contribution = $this->callAPISuccess('Order', 'create', $params);
130 $lineItems[1] = CRM_Price_BAO_LineItem::getLineItemsByContributionID($contribution['id']);
131 $lineItemId = key($lineItems[1]);
132 $lineItems[1][$lineItemId]['financial_item_id'] = CRM_Core_DAO::singleValueQuery("SELECT id FROM civicrm_financial_item WHERE entity_table = 'civicrm_line_item' AND entity_id = {$lineItemId}");
133
134 $trxn = $this->callAPISuccess('FinancialTrxn', 'get', ['total_amount' => 622]);
135 $this->assertEquals(date('Ymd', strtotime('2016-01-20')), date('Ymd', strtotime($trxn['values'][$trxn['id']]['trxn_date'])));
136
137 $contributionObj = $this->getContributionObject($contribution['id']);
138 $contributionObj->revenue_recognition_date = date('Ymd', strtotime('+1 month'));
139 CRM_Core_BAO_FinancialTrxn::createDeferredTrxn($lineItems, $contributionObj);
140 $trxn = $this->callAPISuccess('FinancialTrxn', 'get', ['total_amount' => 622, 'id' => ['NOT IN' => [$trxn['id']]]]);
141
142 $this->assertEquals(date('Ymd', strtotime($trxn['values'][$trxn['id']]['trxn_date'])), date('Ymd', strtotime('+1 month')));
143 }
144
145 /**
146 * Test for updateCreditCardDetails().
147 *
148 * @throws \CRM_Core_Exception
149 */
150 public function testUpdateCreditCardDetailsUsingContributionAPI() {
151 $cid = $this->individualCreate();
152 $params = [
153 'contact_id' => $cid,
154 'receive_date' => '2016-01-20',
155 'total_amount' => 100,
156 'financial_type_id' => 1,
157 ];
158 $contribution = $this->callAPISuccess('Contribution', 'create', $params);
159 $lastFinancialTrxnId = CRM_Core_BAO_FinancialTrxn::getFinancialTrxnId($contribution['id'], 'DESC');
160 $financialTrxn = $this->callAPISuccessGetSingle(
161 'FinancialTrxn',
162 [
163 'id' => $lastFinancialTrxnId['financialTrxnId'],
164 'return' => ['card_type_id', 'pan_truncation'],
165 ]
166 );
167 $this->assertEquals(CRM_Utils_Array::value('card_type_id', $financialTrxn), NULL);
168 $this->assertEquals(CRM_Utils_Array::value('pan_truncation', $financialTrxn), NULL);
169 $params = [
170 'card_type_id' => 2,
171 'pan_truncation' => 4567,
172 'id' => $contribution['id'],
173 ];
174 $this->callAPISuccess('Contribution', 'create', $params);
175 $financialTrxn = $this->callAPISuccessGetSingle(
176 'FinancialTrxn',
177 [
178 'id' => $lastFinancialTrxnId['financialTrxnId'],
179 'return' => ['card_type_id', 'pan_truncation'],
180 ]
181 );
182 $this->assertEquals($financialTrxn['card_type_id'], 2);
183 $this->assertEquals($financialTrxn['pan_truncation'], 4567);
184 }
185
186 /**
187 * Test for updateCreditCardDetails().
188 *
189 * @throws \CRM_Core_Exception
190 */
191 public function testUpdateCreditCardDetails() {
192 $cid = $this->individualCreate();
193 $params = [
194 'contact_id' => $cid,
195 'receive_date' => '2016-01-20',
196 'total_amount' => 100,
197 'financial_type_id' => 1,
198 ];
199 $contribution = $this->callAPISuccess('Contribution', 'create', $params);
200 $lastFinancialTrxnId = CRM_Core_BAO_FinancialTrxn::getFinancialTrxnId($contribution['id'], 'DESC');
201 $financialTrxn = $this->callAPISuccessGetSingle(
202 'FinancialTrxn',
203 [
204 'id' => $lastFinancialTrxnId['financialTrxnId'],
205 'return' => ['card_type_id', 'pan_truncation'],
206 ]
207 );
208 $this->assertEquals(CRM_Utils_Array::value('card_type_id', $financialTrxn), NULL);
209 $this->assertEquals(CRM_Utils_Array::value('pan_truncation', $financialTrxn), NULL);
210 CRM_Core_BAO_FinancialTrxn::updateCreditCardDetails($contribution['id'], 4567, 2);
211 $financialTrxn = $this->callAPISuccessGetSingle(
212 'FinancialTrxn',
213 [
214 'id' => $lastFinancialTrxnId['financialTrxnId'],
215 'return' => ['card_type_id', 'pan_truncation'],
216 ]
217 );
218 $this->assertEquals($financialTrxn['card_type_id'], 2);
219 $this->assertEquals($financialTrxn['pan_truncation'], 4567);
220 }
221
222 /**
223 * Test testGetContributionBalance function.
224 *
225 * @throws \CRM_Core_Exception
226 */
227 public function testGetContributionBalance() {
228 //create the contribution that isn't paid yet
229 $contactId = $this->individualCreate();
230 $params = [
231 'contact_id' => $contactId,
232 'currency' => 'USD',
233 'financial_type_id' => 1,
234 'contribution_status_id' => 'Pending',
235 'payment_instrument_id' => 4,
236 'total_amount' => 300.00,
237 'fee_amount' => 0.00,
238 'net_amount' => 300.00,
239 ];
240 $contribution = $this->callAPISuccess('Contribution', 'create', $params);
241 //make a payment one cent short
242 $params = [
243 'contribution_id' => $contribution['id'],
244 'total_amount' => 299.99,
245 ];
246 $this->callAPISuccess('Payment', 'create', $params);
247 //amount owed should be one cent
248 $amountOwed = CRM_Contribute_BAO_Contribution::getContributionBalance($contribution['id']);
249 $this->assertEquals(0.01, $amountOwed, 'Amount does not match');
250 }
251
252 }