Merge pull request #14249 from yashodha/959_dev
[civicrm-core.git] / tests / phpunit / CRM / Contribute / BAO / ContributionTest.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
2fe49090 4 | CiviCRM version 5 |
6a488035 5 +--------------------------------------------------------------------+
6b83d5bd 6 | Copyright CiviCRM LLC (c) 2004-2019 |
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
6b60d32c 34 use CRMTraits_Financial_FinancialACLTrait;
53666099 35 use CRMTraits_Financial_PriceSetTrait;
6b60d32c 36
6a488035 37 /**
2449fe69 38 * Clean up after tests.
39 */
40 public function tearDown() {
41 $this->quickCleanUpFinancialEntities();
2449fe69 42 parent::tearDown();
43 }
44
45 /**
46 * Test create method (create and update modes).
6a488035 47 */
00be9182 48 public function testCreate() {
2449fe69 49 $contactId = $this->individualCreate();
6a488035
TO
50
51 $params = array(
52 'contact_id' => $contactId,
53 'currency' => 'USD',
92915c55 54 'financial_type_id' => 1,
6a488035
TO
55 'contribution_status_id' => 1,
56 'payment_instrument_id' => 1,
57 'source' => 'STUDENT',
58 'receive_date' => '20080522000000',
59 'receipt_date' => '20080522000000',
60 'non_deductible_amount' => 0.00,
61 'total_amount' => 200.00,
62 'fee_amount' => 5,
63 'net_amount' => 195,
64 'trxn_id' => '22ereerwww444444',
65 'invoice_id' => '86ed39c9e9ee6ef6031621ce0eafe7eb81',
66 'thankyou_date' => '20080522',
d0c97775 67 'sequential' => TRUE,
6a488035
TO
68 );
69
d0c97775 70 $contribution = $this->callAPISuccess('Contribution', 'create', $params)['values'][0];
6a488035 71
d0c97775 72 $this->assertEquals($params['trxn_id'], $contribution['trxn_id'], 'Check for transaction id creation.');
73 $this->assertEquals($contactId, $contribution['contact_id'], 'Check for contact id creation.');
6a488035
TO
74
75 //update contribution amount
d0c97775 76 $params['id'] = $contribution['id'];
6a488035
TO
77 $params['fee_amount'] = 10;
78 $params['net_amount'] = 190;
79
d0c97775 80 $contribution = $this->callAPISuccess('Contribution', 'create', $params)['values'][0];
6a488035 81
d0c97775 82 $this->assertEquals($params['trxn_id'], $contribution['trxn_id'], 'Check for transcation id .');
83 $this->assertEquals($params['net_amount'], $contribution['net_amount'], 'Check for Amount updation.');
6a488035
TO
84 }
85
86 /**
f43ac8d8 87 * Create() method with custom data.
6a488035 88 */
00be9182 89 public function testCreateWithCustomData() {
2449fe69 90 $contactId = $this->individualCreate();
6a488035
TO
91
92 //create custom data
2449fe69 93 $customGroup = $this->customGroupCreate(array('extends' => 'Contribution'));
94 $customGroupID = $customGroup['id'];
95 $customGroup = $customGroup['values'][$customGroupID];
96
6a488035
TO
97 $fields = array(
98 'label' => 'testFld',
99 'data_type' => 'String',
100 'html_type' => 'Text',
101 'is_active' => 1,
2449fe69 102 'custom_group_id' => $customGroupID,
6a488035
TO
103 );
104 $customField = CRM_Core_BAO_CustomField::create($fields);
105
106 $params = array(
107 'contact_id' => $contactId,
108 'currency' => 'USD',
92915c55 109 'financial_type_id' => 1,
6a488035
TO
110 'contribution_status_id' => 1,
111 'payment_instrument_id' => 1,
112 'source' => 'STUDENT',
113 'receive_date' => '20080522000000',
114 'receipt_date' => '20080522000000',
115 'id' => NULL,
116 'non_deductible_amount' => 0.00,
117 'total_amount' => 200.00,
118 'fee_amount' => 5,
119 'net_amount' => 195,
120 'trxn_id' => '22ereerwww322323',
121 'invoice_id' => '22ed39c9e9ee6ef6031621ce0eafe6da70',
122 'thankyou_date' => '20080522',
d0c97775 123 'skipCleanMoney' => TRUE,
6a488035
TO
124 );
125
6a488035
TO
126 $params['custom'] = array(
127 $customField->id => array(
128 -1 => array(
129 'value' => 'Test custom value',
130 'type' => 'String',
131 'custom_field_id' => $customField->id,
2449fe69 132 'custom_group_id' => $customGroupID,
133 'table_name' => $customGroup['table_name'],
6a488035
TO
134 'column_name' => $customField->column_name,
135 'file_id' => NULL,
136 ),
137 ),
138 );
139
2449fe69 140 $contribution = CRM_Contribute_BAO_Contribution::create($params);
6a488035
TO
141
142 // Check that the custom field value is saved
143 $customValueParams = array(
144 'entityID' => $contribution->id,
145 'custom_' . $customField->id => 1,
146 );
147 $values = CRM_Core_BAO_CustomValueTable::getValues($customValueParams);
148 $this->assertEquals('Test custom value', $values['custom_' . $customField->id], 'Check the custom field value');
149
150 $this->assertEquals($params['trxn_id'], $contribution->trxn_id, 'Check for transcation id creation.');
151 $this->assertEquals($contactId, $contribution->contact_id, 'Check for contact id for Conribution.');
6a488035
TO
152 }
153
d73f286e
SL
154 /**
155 * CRM-21026 Test ContributionCount after contribution created with disabled FT
156 */
157 public function testContributionCountDisabledFinancialType() {
158 $contactId = $this->individualCreate();
159 $financialType = array(
160 'name' => 'grassvariety1' . substr(sha1(rand()), 0, 7),
161 'is_reserved' => 0,
162 'is_active' => 0,
163 );
164 $finType = $this->callAPISuccess('financial_type', 'create', $financialType);
165 $params = array(
166 'contact_id' => $contactId,
167 'currency' => 'USD',
168 'financial_type_id' => $finType['id'],
169 'contribution_status_id' => 1,
170 'payment_instrument_id' => 1,
171 'source' => 'STUDENT',
172 'receive_date' => '20080522000000',
173 'receipt_date' => '20080522000000',
174 'id' => NULL,
175 'non_deductible_amount' => 0.00,
176 'total_amount' => 200.00,
177 'fee_amount' => 5,
178 'net_amount' => 195,
179 'trxn_id' => '22ereerwww322323',
180 'invoice_id' => '22ed39c9e9ee6ef6031621ce0eafe6da70',
181 'thankyou_date' => '20080522',
182 );
d0c97775 183 $this->callAPISuccess('Contribution', 'create', $params);
184 $this->callAPISuccess('financial_type', 'create', array('is_active' => 0, 'id' => $finType['id']));
d73f286e
SL
185 $contributionCount = CRM_Contribute_BAO_Contribution::contributionCount($contactId);
186 $this->assertEquals(1, $contributionCount);
187 }
188
6a488035 189 /**
100fef9d 190 * DeleteContribution() method
6a488035 191 */
00be9182 192 public function testDeleteContribution() {
2449fe69 193 $contactId = $this->individualCreate();
6a488035
TO
194
195 $params = array(
196 'contact_id' => $contactId,
197 'currency' => 'USD',
92915c55 198 'financial_type_id' => 1,
6a488035
TO
199 'contribution_status_id' => 1,
200 'payment_instrument_id' => 1,
201 'source' => 'STUDENT',
202 'receive_date' => '20080522000000',
203 'receipt_date' => '20080522000000',
204 'id' => NULL,
205 'non_deductible_amount' => 0.00,
206 'total_amount' => 200.00,
207 'fee_amount' => 5,
208 'net_amount' => 195,
209 'trxn_id' => '33ereerwww322323',
210 'invoice_id' => '33ed39c9e9ee6ef6031621ce0eafe6da70',
211 'thankyou_date' => '20080522',
55ee9063 212 'sequential' => TRUE,
6a488035
TO
213 );
214
55ee9063 215 $contribution = $this->callAPISuccess('Contribution', 'create', $params)['values'][0];
6a488035 216
55ee9063 217 CRM_Contribute_BAO_Contribution::deleteContribution($contribution['id']);
6a488035 218
55ee9063 219 $this->assertDBNull('CRM_Contribute_DAO_Contribution', $contribution['trxn_id'],
6a488035
TO
220 'id', 'trxn_id', 'Database check for deleted Contribution.'
221 );
6a488035
TO
222 }
223
224 /**
2449fe69 225 * Create honor-contact method.
6a488035 226 */
2449fe69 227 public function testCreateAndGetHonorContact() {
6a488035 228 $firstName = 'John_' . substr(sha1(rand()), 0, 7);
92915c55
TO
229 $lastName = 'Smith_' . substr(sha1(rand()), 0, 7);
230 $email = "{$firstName}.{$lastName}@example.com";
6a488035 231
8381af80 232 //Get profile id of name honoree_individual used to create profileContact
233 $honoreeProfileId = NULL;
234 $ufGroupDAO = new CRM_Core_DAO_UFGroup();
235 $ufGroupDAO->name = 'honoree_individual';
236 if ($ufGroupDAO->find(TRUE)) {
237 $honoreeProfileId = $ufGroupDAO->id;
238 }
239
6a488035 240 $params = array(
8381af80 241 'prefix_id' => 3,
242 'first_name' => $firstName,
243 'last_name' => $lastName,
244 'email-1' => $email,
6a488035 245 );
8381af80 246 $softParam = array('soft_credit_type_id' => 1);
247
248 $honoreeContactId = CRM_Contact_BAO_Contact::createProfileContact($params, CRM_Core_DAO::$_nullArray,
92915c55
TO
249 NULL, NULL, $honoreeProfileId
250 );
6a488035 251
8381af80 252 $this->assertDBCompareValue('CRM_Contact_DAO_Contact', $honoreeContactId, 'first_name', 'id', $firstName,
6a488035
TO
253 'Database check for created honor contact record.'
254 );
255 //create contribution on behalf of honary.
256
2449fe69 257 $contactId = $this->individualCreate(array('first_name' => 'John', 'last_name' => 'Doe'));
6a488035 258
6a488035
TO
259 $param = array(
260 'contact_id' => $contactId,
261 'currency' => 'USD',
92915c55 262 'financial_type_id' => 4,
6a488035
TO
263 'contribution_status_id' => 1,
264 'receive_date' => date('Ymd'),
265 'total_amount' => 66,
d0c97775 266 'sequential' => 1,
6a488035
TO
267 );
268
d0c97775 269 $contribution = $this->callAPISuccess('Contribution', 'create', $param)['values'][0];
270 $id = $contribution['id'];
2449fe69 271 $softParam['contact_id'] = $honoreeContactId;
8381af80 272 $softParam['contribution_id'] = $id;
d0c97775 273 $softParam['currency'] = $contribution['currency'];
274 $softParam['amount'] = $contribution['total_amount'];
8381af80 275
276 //Create Soft Contribution for honoree contact
277 CRM_Contribute_BAO_ContributionSoft::add($softParam);
278
279 $this->assertDBCompareValue('CRM_Contribute_DAO_ContributionSoft', $id, 'contact_id',
280 'contribution_id', $honoreeContactId, 'Check DB for honor contact of the contribution'
6a488035 281 );
e4f46be0 282 //get honorary information
8381af80 283 $getHonorContact = CRM_Contribute_BAO_Contribution::getHonorContacts($honoreeContactId);
4ff25fbb 284 $this->assertEquals(array(
285 $id => array(
286 'honor_type' => 'In Honor of',
2449fe69 287 'honorId' => $contactId,
288 'display_name' => 'Mr. John Doe II',
4ff25fbb 289 'type' => 'Event Fee',
290 'type_id' => '4',
291 'amount' => '$ 66.00',
292 'source' => NULL,
755ca72c 293 'receive_date' => date('Y-m-d 00:00:00'),
4ff25fbb 294 'contribution_status' => 'Completed',
295 ),
296 ), $getHonorContact);
6a488035 297
8381af80 298 $this->assertDBCompareValue('CRM_Contact_DAO_Contact', $honoreeContactId, 'first_name', 'id', $firstName,
6a488035
TO
299 'Database check for created honor contact record.'
300 );
301
302 //get annual contribution information
303 $annual = CRM_Contribute_BAO_Contribution::annual($contactId);
304
d0c97775 305 $currencySymbol = CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_Currency', CRM_Core_Config::singleton()->defaultCurrency, 'symbol', 'name');
6a488035
TO
306 $this->assertDBCompareValue('CRM_Contribute_DAO_Contribution', $id, 'total_amount',
307 'id', ltrim($annual[2], $currencySymbol), 'Check DB for total amount of the contribution'
308 );
6a488035
TO
309 }
310
6b60d32c 311 /**
312 * Test that financial type data is not added to the annual query if acls not enabled.
313 */
314 public function testAnnualQueryWithFinancialACLsEnabled() {
315 $this->enableFinancialACLs();
316 $this->createLoggedInUserWithFinancialACL();
317 $permittedFinancialType = CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'financial_type_id', 'Donation');
318 $sql = CRM_Contribute_BAO_Contribution::getAnnualQuery([1, 2, 3]);
319 $this->assertContains('SUM(total_amount) as amount,', $sql);
320 $this->assertContains('WHERE b.contact_id IN (1,2,3)', $sql);
321 $this->assertContains('b.financial_type_id IN (' . $permittedFinancialType . ')', $sql);
322
323 // Run it to make sure it's not bad sql.
324 CRM_Core_DAO::executeQuery($sql);
325 $this->disableFinancialACLs();
326 }
327
53666099 328 /**
329 * Test the annual query returns a correct result when multiple line items are present.
330 */
331 public function testAnnualWithMultipleLineItems() {
332 $contactID = $this->createLoggedInUserWithFinancialACL();
333 $this->createContributionWithTwoLineItemsAgainstPriceSet([
39b959db
SL
334 'contact_id' => $contactID,
335 ]
53666099 336 );
337 $this->enableFinancialACLs();
338 $sql = CRM_Contribute_BAO_Contribution::getAnnualQuery([$contactID]);
339 $result = CRM_Core_DAO::executeQuery($sql);
340 $result->fetch();
341 $this->assertEquals(300, $result->amount);
342 $this->assertEquals(1, $result->count);
343 $this->disableFinancialACLs();
344 }
345
6b60d32c 346 /**
347 * Test that financial type data is not added to the annual query if acls not enabled.
348 */
349 public function testAnnualQueryWithFinancialACLsDisabled() {
350 $sql = CRM_Contribute_BAO_Contribution::getAnnualQuery([1, 2, 3]);
351 $this->assertContains('SUM(total_amount) as amount,', $sql);
352 $this->assertContains('WHERE b.contact_id IN (1,2,3)', $sql);
353 $this->assertNotContains('b.financial_type_id', $sql);
354 //$this->assertNotContains('line_item', $sql);
355 // Run it to make sure it's not bad sql.
356 CRM_Core_DAO::executeQuery($sql);
357 }
358
c77f8667 359 /**
360 * Test that financial type data is not added to the annual query if acls not enabled.
361 */
362 public function testAnnualQueryWithFinancialHook() {
363 $this->hookClass->setHook('civicrm_selectWhereClause', array($this, 'aclIdNoZero'));
364 $sql = CRM_Contribute_BAO_Contribution::getAnnualQuery([1, 2, 3]);
365 $this->assertContains('SUM(total_amount) as amount,', $sql);
366 $this->assertContains('WHERE b.contact_id IN (1,2,3)', $sql);
367 $this->assertContains('b.id NOT IN (0)', $sql);
368 $this->assertNotContains('b.financial_type_id', $sql);
369 CRM_Core_DAO::executeQuery($sql);
370 }
371
372 /**
373 * Add ACL denying values LIKE '0'.
374 *
375 * @param string $entity
376 * @param string $clauses
377 */
378 public function aclIdNoZero($entity, &$clauses) {
379 if ($entity != 'Contribution') {
380 return;
381 }
382 $clauses['id'] = "NOT IN (0)";
383 }
384
6a488035 385 /**
eceb18cc 386 * Display sort name during.
b581842f 387 * Update multiple contributions
6a488035
TO
388 * sortName();
389 */
00be9182 390 public function testsortName() {
6a488035
TO
391 $params = array(
392 'first_name' => 'Shane',
393 'last_name' => 'Whatson',
394 'contact_type' => 'Individual',
395 );
396
397 $contact = CRM_Contact_BAO_Contact::add($params);
398
399 //Now check $contact is object of contact DAO..
400 $this->assertInstanceOf('CRM_Contact_DAO_Contact', $contact, 'Check for created object');
401
402 $contactId = $contact->id;
6a488035
TO
403 $param = array(
404 'contact_id' => $contactId,
405 'currency' => 'USD',
92915c55 406 'financial_type_id' => 1,
6a488035
TO
407 'contribution_status_id' => 1,
408 'payment_instrument_id' => 1,
409 'source' => 'STUDENT',
410 'receive_date' => '20080522000000',
411 'receipt_date' => '20080522000000',
412 'id' => NULL,
413 'non_deductible_amount' => 0.00,
414 'total_amount' => 300.00,
415 'fee_amount' => 5,
416 'net_amount' => 295,
417 'trxn_id' => '22ereerwww323',
418 'invoice_id' => '22ed39c9e9ee621ce0eafe6da70',
419 'thankyou_date' => '20080522',
d0c97775 420 'sequential' => TRUE,
6a488035
TO
421 );
422
d0c97775 423 $contribution = $this->callAPISuccess('Contribution', 'create', $param)['values'][0];
6a488035 424
d0c97775 425 $this->assertEquals($param['trxn_id'], $contribution['trxn_id'], 'Check for transcation id creation.');
426 $this->assertEquals($contactId, $contribution['contact_id'], 'Check for contact id creation.');
6a488035 427
b581842f 428 //display sort name during Update multiple contributions
d0c97775 429 $sortName = CRM_Contribute_BAO_Contribution::sortName($contribution['id']);
6a488035
TO
430
431 $this->assertEquals('Whatson, Shane', $sortName, 'Check for sort name.');
6a488035
TO
432 }
433
434 /**
eceb18cc 435 * Add premium during online Contribution.
6a488035
TO
436 *
437 * AddPremium();
438 */
00be9182 439 public function testAddPremium() {
2449fe69 440 $contactId = $this->individualCreate();
6a488035 441
6a488035
TO
442 $params = array(
443 'name' => 'TEST Premium',
444 'sku' => 111,
445 'imageOption' => 'noImage',
446 'MAX_FILE_SIZE' => 2097152,
447 'price' => 100.00,
448 'cost' => 90.00,
449 'min_contribution' => 100,
450 'is_active' => 1,
451 );
32f27499 452 $premium = CRM_Contribute_BAO_Product::create($params);
6a488035
TO
453
454 $this->assertEquals('TEST Premium', $premium->name, 'Check for premium name.');
455
a275d4b6 456 $contributionParams = array(
6a488035
TO
457 'contact_id' => $contactId,
458 'currency' => 'USD',
92915c55 459 'financial_type_id' => 1,
6a488035
TO
460 'contribution_status_id' => 1,
461 'payment_instrument_id' => 1,
462 'source' => 'STUDENT',
463 'receive_date' => '20080522000000',
464 'receipt_date' => '20080522000000',
465 'id' => NULL,
466 'non_deductible_amount' => 0.00,
467 'total_amount' => 300.00,
468 'fee_amount' => 5,
469 'net_amount' => 295,
470 'trxn_id' => '33erdfrwvw434',
471 'invoice_id' => '98ed34f7u9hh672ce0eafe8fb92',
472 'thankyou_date' => '20080522',
d0c97775 473 'sequential' => TRUE,
6a488035 474 );
a275d4b6 475 $contribution = $this->callAPISuccess('Contribution', 'create', $contributionParams)['values'][0];
6a488035 476
a275d4b6 477 $this->assertEquals($contributionParams['trxn_id'], $contribution['trxn_id'], 'Check for transcation id creation.');
d0c97775 478 $this->assertEquals($contactId, $contribution['contact_id'], 'Check for contact id creation.');
6a488035
TO
479
480 //parameter for adding premium to contribution
481 $data = array(
482 'product_id' => $premium->id,
d0c97775 483 'contribution_id' => $contribution['id'],
6a488035
TO
484 'product_option' => NULL,
485 'quantity' => 1,
486 );
487 $contributionProduct = CRM_Contribute_BAO_Contribution::addPremium($data);
488 $this->assertEquals($contributionProduct->product_id, $premium->id, 'Check for Product id .');
489
490 //Delete Product
37828d4f 491 CRM_Contribute_BAO_Product::del($premium->id);
6a488035
TO
492 $this->assertDBNull('CRM_Contribute_DAO_Product', $premium->name,
493 'id', 'name', 'Database check for deleted Product.'
494 );
6a488035
TO
495 }
496
497 /**
fe482240 498 * Check duplicate contribution id.
6a488035
TO
499 * during the contribution import
500 * checkDuplicateIds();
501 */
00be9182 502 public function testcheckDuplicateIds() {
2449fe69 503 $contactId = $this->individualCreate();
6a488035
TO
504
505 $param = array(
506 'contact_id' => $contactId,
507 'currency' => 'USD',
92915c55 508 'financial_type_id' => 1,
6a488035
TO
509 'contribution_status_id' => 1,
510 'payment_instrument_id' => 1,
511 'source' => 'STUDENT',
512 'receive_date' => '20080522000000',
513 'receipt_date' => '20080522000000',
514 'id' => NULL,
515 'non_deductible_amount' => 0.00,
516 'total_amount' => 300.00,
517 'fee_amount' => 5,
518 'net_amount' => 295,
519 'trxn_id' => '76ereeswww835',
520 'invoice_id' => '93ed39a9e9hd621bs0eafe3da82',
521 'thankyou_date' => '20080522',
d0c97775 522 'sequential' => TRUE,
6a488035
TO
523 );
524
d0c97775 525 $contribution = $this->callAPISuccess('Contribution', 'create', $param)['values'][0];
6a488035 526
d0c97775 527 $this->assertEquals($param['trxn_id'], $contribution['trxn_id'], 'Check for transcation id creation.');
528 $this->assertEquals($contactId, $contribution['contact_id'], 'Check for contact id creation.');
6a488035 529 $data = array(
d0c97775 530 'id' => $contribution['id'],
531 'trxn_id' => $contribution['trxn_id'],
532 'invoice_id' => $contribution['invoice_id'],
6a488035
TO
533 );
534 $contributionID = CRM_Contribute_BAO_Contribution::checkDuplicateIds($data);
d0c97775 535 $this->assertEquals($contributionID, $contribution['id'], 'Check for duplicate transcation id .');
6a488035 536 }
96025800 537
8022372f
GC
538 /**
539 * Check credit note id creation
540 * when a contribution is cancelled or refunded
541 * createCreditNoteId();
542 */
543 public function testCreateCreditNoteId() {
2449fe69 544 $contactId = $this->individualCreate();
8022372f
GC
545
546 $param = array(
547 'contact_id' => $contactId,
548 'currency' => 'USD',
549 'financial_type_id' => 1,
550 'contribution_status_id' => 3,
551 'payment_instrument_id' => 1,
552 'source' => 'STUDENT',
553 'receive_date' => '20080522000000',
554 'receipt_date' => '20080522000000',
555 'id' => NULL,
556 'non_deductible_amount' => 0.00,
557 'total_amount' => 300.00,
558 'fee_amount' => 5,
559 'net_amount' => 295,
560 'trxn_id' => '76ereeswww835',
561 'invoice_id' => '93ed39a9e9hd621bs0eafe3da82',
562 'thankyou_date' => '20080522',
d0c97775 563 'sequential' => TRUE,
8022372f
GC
564 );
565
566 $creditNoteId = CRM_Contribute_BAO_Contribution::createCreditNoteId();
d0c97775 567 $contribution = $this->callAPISuccess('Contribution', 'create', $param)['values'][0];
568 $this->assertEquals($contactId, $contribution['contact_id'], 'Check for contact id creation.');
569 $this->assertEquals($creditNoteId, $contribution['creditnote_id'], 'Check if credit note id is created correctly.');
8022372f
GC
570 }
571
b04e4b11
PN
572 /**
573 * Create() method (create and update modes).
574 */
575 public function testIsPaymentFlag() {
2449fe69 576 $contactId = $this->individualCreate();
b04e4b11 577
d0c97775 578 $params = [
b04e4b11
PN
579 'contact_id' => $contactId,
580 'currency' => 'USD',
581 'financial_type_id' => 1,
582 'contribution_status_id' => 1,
583 'payment_instrument_id' => 1,
584 'source' => 'STUDENT',
585 'receive_date' => '20080522000000',
586 'receipt_date' => '20080522000000',
587 'non_deductible_amount' => 0.00,
588 'total_amount' => 200.00,
589 'fee_amount' => 5,
590 'net_amount' => 195,
591 'trxn_id' => '22ereerwww4444xx',
592 'invoice_id' => '86ed39c9e9ee6ef6541621ce0eafe7eb81',
593 'thankyou_date' => '20080522',
d0c97775 594 'sequential' => TRUE,
595 ];
596 $contribution = $this->callAPISuccess('Contribution', 'create', $params)['values'][0];
b04e4b11 597
d0c97775 598 $this->assertEquals($params['trxn_id'], $contribution['trxn_id'], 'Check for transcation id creation.');
599 $this->assertEquals($contactId, $contribution['contact_id'], 'Check for contact id creation.');
b04e4b11
PN
600
601 $trxnArray = array(
602 'trxn_id' => $params['trxn_id'],
603 'is_payment' => 1,
604 );
605 $defaults = array();
606 $financialTrxn = CRM_Core_BAO_FinancialTrxn::retrieve($trxnArray, $defaults);
4ba3c75f 607 $this->assertEquals(1, $financialTrxn->N, 'Mismatch count for is payment flag.');
b04e4b11 608 //update contribution amount
d0c97775 609 $params['id'] = $contribution['id'];
b04e4b11 610 $params['total_amount'] = 150;
d0c97775 611 $contribution = $this->callAPISuccess('Contribution', 'create', $params)['values'][0];
b04e4b11 612
d0c97775 613 $this->assertEquals($params['trxn_id'], $contribution['trxn_id'], 'Check for transcation id .');
614 $this->assertEquals($params['total_amount'], $contribution['total_amount'], 'Check for Amount updation.');
b04e4b11
PN
615 $trxnArray = array(
616 'trxn_id' => $params['trxn_id'],
617 'is_payment' => 1,
618 );
619 $defaults = array();
620 $financialTrxn = CRM_Core_BAO_FinancialTrxn::retrieve($trxnArray, $defaults);
4ba3c75f 621 $this->assertEquals(2, $financialTrxn->N, 'Mismatch count for is payment flag.');
b04e4b11
PN
622 $trxnArray['is_payment'] = 0;
623 $financialTrxn = CRM_Core_BAO_FinancialTrxn::retrieve($trxnArray, $defaults);
4ba3c75f 624 $this->assertEquals(1, $financialTrxn->N, 'Mismatch count for is payment flag.');
b04e4b11
PN
625 }
626
a387acc9
PN
627 /**
628 * Create() method (create and update modes).
629 */
630 public function testIsPaymentFlagForPending() {
2449fe69 631 $contactId = $this->individualCreate();
a387acc9
PN
632
633 $params = array(
634 'contact_id' => $contactId,
635 'currency' => 'USD',
636 'financial_type_id' => 1,
637 'contribution_status_id' => 2,
638 'payment_instrument_id' => 1,
639 'source' => 'STUDENT',
640 'is_pay_later' => 1,
641 'receive_date' => '20080522000000',
642 'receipt_date' => '20080522000000',
643 'non_deductible_amount' => 0.00,
644 'total_amount' => 200.00,
645 'fee_amount' => 5,
646 'net_amount' => 195,
647 'trxn_id' => '22ereerwww4444yy',
648 'invoice_id' => '86ed39c9e9yy6ef6541621ce0eafe7eb81',
649 'thankyou_date' => '20080522',
d0c97775 650 'sequential' => TRUE,
a387acc9
PN
651 );
652
d0c97775 653 $contribution = $this->callAPISuccess('Contribution', 'create', $params)['values'][0];
a387acc9 654
d0c97775 655 $this->assertEquals($params['trxn_id'], $contribution['trxn_id'], 'Check for transaction id creation.');
656 $this->assertEquals($contactId, $contribution['contact_id'], 'Check for contact id creation.');
a387acc9
PN
657
658 $trxnArray = array(
659 'trxn_id' => $params['trxn_id'],
660 'is_payment' => 0,
661 );
662 $defaults = array();
663 $financialTrxn = CRM_Core_BAO_FinancialTrxn::retrieve($trxnArray, $defaults);
664 $this->assertEquals(2, $financialTrxn->N, 'Mismatch count for is payment flag.');
665 $trxnArray['is_payment'] = 1;
666 $financialTrxn = CRM_Core_BAO_FinancialTrxn::retrieve($trxnArray, $defaults);
667 $this->assertEquals(NULL, $financialTrxn, 'Mismatch count for is payment flag.');
668 //update contribution amount
d0c97775 669 $params['id'] = $contribution['id'];
a387acc9
PN
670 $params['contribution_status_id'] = 1;
671
d0c97775 672 $contribution = $this->callAPISuccess('Contribution', 'create', $params)['values'][0];
a387acc9 673
d0c97775 674 $this->assertEquals($params['trxn_id'], $contribution['trxn_id'], 'Check for transcation id .');
675 $this->assertEquals($params['contribution_status_id'], $contribution['contribution_status_id'], 'Check for status updation.');
a387acc9
PN
676 $trxnArray = array(
677 'trxn_id' => $params['trxn_id'],
678 'is_payment' => 1,
679 );
680 $defaults = array();
681 $financialTrxn = CRM_Core_BAO_FinancialTrxn::retrieve($trxnArray, $defaults);
682 $this->assertEquals(1, $financialTrxn->N, 'Mismatch count for is payment flag.');
683 $trxnArray['is_payment'] = 0;
684 $financialTrxn = CRM_Core_BAO_FinancialTrxn::retrieve($trxnArray, $defaults);
685 $this->assertEquals(2, $financialTrxn->N, 'Mismatch count for is payment flag.');
a387acc9
PN
686 }
687
eec619df 688 /**
0a5651eb 689 * addPayments() method (add and edit modes of participant)
eec619df
PN
690 */
691 public function testAddPayments() {
692 list($lineItems, $contribution) = $this->addParticipantWithContribution();
d0c97775 693 CRM_Contribute_BAO_Contribution::addPayments([$contribution]);
0a5651eb
PN
694 $this->checkItemValues($contribution);
695 }
696
697 /**
698 * checks db values for financial item
699 */
624195c8 700 public function checkItemValues($contribution) {
876b8ab0 701 $toFinancialAccount = CRM_Contribute_PseudoConstant::getRelationalFinancialAccount(4, 'Accounts Receivable Account is');
2449fe69 702 $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
703INNER JOIN civicrm_entity_financial_trxn eft1 ON (eft1.financial_trxn_id = eft.financial_trxn_id AND eft1.entity_table = 'civicrm_financial_item')
704WHERE eft.entity_id = %1 AND ft.to_financial_account_id <> %2";
705
706 $queryParams[1] = array($contribution->id, 'Integer');
707 $queryParams[2] = array($toFinancialAccount, 'Integer');
708
709 $dao = CRM_Core_DAO::executeQuery($query, $queryParams);
19084b68 710 $amounts = array(100.00, 50.00);
eec619df
PN
711 while ($dao->fetch()) {
712 $this->assertEquals(150.00, $dao->total_amount, 'Mismatch of total amount paid.');
19084b68 713 $this->assertEquals($dao->amount, array_pop($amounts), 'Mismatch of amount proportionally assigned to financial item');
eec619df 714 }
eec619df
PN
715 }
716
0a5651eb
PN
717 /**
718 * assignProportionalLineItems() method (add and edit modes of participant)
719 */
720 public function testAssignProportionalLineItems() {
721 list($lineItems, $contribution) = $this->addParticipantWithContribution();
0a5651eb
PN
722 $params = array(
723 'contribution_id' => $contribution->id,
724 'total_amount' => 150.00,
725 );
726 $trxn = new CRM_Financial_DAO_FinancialTrxn();
727 $trxn->orderBy('id DESC');
728 $trxn->find(TRUE);
8de1ade9 729 CRM_Contribute_BAO_Contribution::assignProportionalLineItems($params, $trxn->id, $contribution->total_amount);
0a5651eb
PN
730 $this->checkItemValues($contribution);
731 }
732
eec619df
PN
733 /**
734 * Add participant with contribution
735 *
736 * @return array
737 */
78c99516 738 public function addParticipantWithContribution() {
eec619df 739 // creating price set, price field
2449fe69 740 $this->_contactId = $this->individualCreate();
741 $event = $this->eventCreate();
742 $this->_eventId = $event['id'];
624195c8 743 $paramsSet['title'] = 'Price Set' . substr(sha1(rand()), 0, 4);
19084b68 744 $paramsSet['name'] = CRM_Utils_String::titleToVar($paramsSet['title']);
eec619df
PN
745 $paramsSet['is_active'] = TRUE;
746 $paramsSet['financial_type_id'] = 4;
747 $paramsSet['extends'] = 1;
748
749 $priceset = CRM_Price_BAO_PriceSet::create($paramsSet);
750 $priceSetId = $priceset->id;
751
752 //Checking for priceset added in the table.
753 $this->assertDBCompareValue('CRM_Price_BAO_PriceSet', $priceSetId, 'title',
754 'id', $paramsSet['title'], 'Check DB for created priceset'
755 );
756 $paramsField = array(
757 'label' => 'Price Field',
758 'name' => CRM_Utils_String::titleToVar('Price Field'),
759 'html_type' => 'CheckBox',
760 'option_label' => array('1' => 'Price Field 1', '2' => 'Price Field 2'),
761 'option_value' => array('1' => 100, '2' => 200),
762 'option_name' => array('1' => 'Price Field 1', '2' => 'Price Field 2'),
763 'option_weight' => array('1' => 1, '2' => 2),
764 'option_amount' => array('1' => 100, '2' => 200),
765 'is_display_amounts' => 1,
766 'weight' => 1,
767 'options_per_line' => 1,
768 'is_active' => array('1' => 1, '2' => 1),
769 'price_set_id' => $priceset->id,
770 'is_enter_qty' => 1,
771 'financial_type_id' => CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_FinancialType', 'Event Fee', 'id', 'name'),
772 );
773 $priceField = CRM_Price_BAO_PriceField::create($paramsField);
774 $eventParams = array(
775 'id' => $this->_eventId,
776 'financial_type_id' => 4,
777 'is_monetary' => 1,
778 );
779 CRM_Event_BAO_Event::create($eventParams);
780 CRM_Price_BAO_PriceSet::addTo('civicrm_event', $this->_eventId, $priceSetId);
e4ba8498 781
eec619df
PN
782 $priceFields = $this->callAPISuccess('PriceFieldValue', 'get', array('price_field_id' => $priceField->id));
783 $participantParams = array(
784 'financial_type_id' => 4,
785 'event_id' => $this->_eventId,
786 'role_id' => 1,
787 'status_id' => 14,
788 'fee_currency' => 'USD',
789 'contact_id' => $this->_contactId,
790 );
791 $participant = CRM_Event_BAO_Participant::add($participantParams);
792 $contributionParams = array(
793 'total_amount' => 150,
794 'currency' => 'USD',
795 'contact_id' => $this->_contactId,
796 'financial_type_id' => 4,
797 'contribution_status_id' => 1,
798 'partial_payment_total' => 300.00,
f49cdeab 799 'partial_amount_to_pay' => 150,
eec619df
PN
800 'contribution_mode' => 'participant',
801 'participant_id' => $participant->id,
d0c97775 802 'sequential' => TRUE,
eec619df
PN
803 );
804
805 foreach ($priceFields['values'] as $key => $priceField) {
806 $lineItems[1][$key] = array(
807 'price_field_id' => $priceField['price_field_id'],
808 'price_field_value_id' => $priceField['id'],
809 'label' => $priceField['label'],
810 'field_title' => $priceField['label'],
811 'qty' => 1,
812 'unit_price' => $priceField['amount'],
813 'line_total' => $priceField['amount'],
814 'financial_type_id' => $priceField['financial_type_id'],
815 );
816 }
817 $contributionParams['line_item'] = $lineItems;
d0c97775 818 $contribution = $this->callAPISuccess('Contribution', 'create', $contributionParams)['values'][0];
eec619df
PN
819
820 $paymentParticipant = array(
821 'participant_id' => $participant->id,
d0c97775 822 'contribution_id' => $contribution['id'],
eec619df 823 );
a5750507 824 CRM_Event_BAO_ParticipantPayment::create($paymentParticipant);
eec619df 825
d0c97775 826 $contributionObject = new CRM_Contribute_BAO_Contribution();
827 $contributionObject->id = $contribution['id'];
828 $contributionObject->find(TRUE);
829
830 return array($lineItems, $contributionObject);
eec619df
PN
831 }
832
5c8b902b 833 /**
a4a31ffe 834 * checkLineItems() check if total amount matches the sum of line total
5c8b902b
PN
835 */
836 public function testcheckLineItems() {
837 $params = array(
838 'contact_id' => 202,
839 'receive_date' => '2010-01-20',
840 'total_amount' => 100,
841 'financial_type_id' => 3,
842 'line_items' => array(
843 array(
844 'line_item' => array(
845 array(
846 'entity_table' => 'civicrm_contribution',
847 'price_field_id' => 8,
848 'price_field_value_id' => 16,
849 'label' => 'test 1',
850 'qty' => 1,
851 'unit_price' => 100,
852 'line_total' => 100,
853 ),
854 array(
855 'entity_table' => 'civicrm_contribution',
856 'price_field_id' => 8,
857 'price_field_value_id' => 17,
858 'label' => 'Test 2',
859 'qty' => 1,
860 'unit_price' => 200,
861 'line_total' => 200,
862 'financial_type_id' => 1,
863 ),
864 ),
a4a31ffe 865 'params' => array(),
5c8b902b 866 ),
a4a31ffe 867 ),
5c8b902b 868 );
c16c6ad8 869
af004c04 870 try {
2449fe69 871 CRM_Contribute_BAO_Contribution::checkLineItems($params);
af004c04
PN
872 $this->fail("Missed expected exception");
873 }
c16c6ad8
CR
874 catch (CRM_Contribute_Exception_CheckLineItemsException $e) {
875 $this->assertEquals(
876 CRM_Contribute_Exception_CheckLineItemsException::LINE_ITEM_DIFFERRING_TOTAL_EXCEPTON_MSG,
877 $e->getMessage()
878 );
af004c04 879 }
c16c6ad8 880
5c8b902b
PN
881 $this->assertEquals(3, $params['line_items'][0]['line_item'][0]['financial_type_id']);
882 $params['total_amount'] = 300;
c16c6ad8 883
af004c04 884 CRM_Contribute_BAO_Contribution::checkLineItems($params);
5c8b902b
PN
885 }
886
c16c6ad8
CR
887 /**
888 * Tests CRM_Contribute_BAO_Contribution::checkLineItems() method works with
889 * floating point values.
890 */
891 public function testCheckLineItemsWithFloatingPointValues() {
892 $params = array(
893 'contact_id' => 202,
894 'receive_date' => date('Y-m-d'),
895 'total_amount' => 16.67,
896 'financial_type_id' => 3,
897 'line_items' => array(
898 array(
899 'line_item' => array(
900 array(
901 'entity_table' => 'civicrm_contribution',
902 'price_field_id' => 8,
903 'price_field_value_id' => 16,
904 'label' => 'test 1',
905 'qty' => 1,
906 'unit_price' => 14.85,
907 'line_total' => 14.85,
908 ),
909 array(
910 'entity_table' => 'civicrm_contribution',
911 'price_field_id' => 8,
912 'price_field_value_id' => 17,
913 'label' => 'Test 2',
914 'qty' => 1,
915 'unit_price' => 1.66,
916 'line_total' => 1.66,
917 'financial_type_id' => 1,
918 ),
919 array(
920 'entity_table' => 'civicrm_contribution',
921 'price_field_id' => 8,
922 'price_field_value_id' => 17,
923 'label' => 'Test 2',
924 'qty' => 1,
925 'unit_price' => 0.16,
926 'line_total' => 0.16,
927 'financial_type_id' => 1,
928 ),
929 ),
930 'params' => array(),
931 ),
932 ),
933 );
934
935 $foundException = FALSE;
936
937 try {
938 CRM_Contribute_BAO_Contribution::checkLineItems($params);
939 }
940 catch (CRM_Contribute_Exception_CheckLineItemsException $e) {
941 $foundException = TRUE;
942 }
943
944 $this->assertFalse($foundException);
945 }
946
88f7a518
E
947 /**
948 * Test activity amount updation.
949 */
950 public function testActivityCreate() {
951 $contactId = $this->individualCreate();
952 $defaults = array();
953
954 $params = array(
955 'contact_id' => $contactId,
956 'currency' => 'USD',
957 'financial_type_id' => 1,
958 'contribution_status_id' => 1,
959 'payment_instrument_id' => 1,
960 'source' => 'STUDENT',
961 'receive_date' => '20080522000000',
962 'receipt_date' => '20080522000000',
963 'non_deductible_amount' => 0.00,
964 'total_amount' => 100.00,
965 'trxn_id' => '22ereerwww444444',
966 'invoice_id' => '86ed39c9e9ee6ef6031621ce0eafe7eb81',
967 'thankyou_date' => '20160519',
d0c97775 968 'sequential' => 1,
88f7a518
E
969 );
970
d0c97775 971 $contribution = $this->callAPISuccess('Contribution', 'create', $params)['values'][0];
88f7a518 972
d0c97775 973 $this->assertEquals($params['total_amount'], $contribution['total_amount'], 'Check for total amount in contribution.');
974 $this->assertEquals($contactId, $contribution['contact_id'], 'Check for contact id creation.');
88f7a518
E
975
976 // Check amount in activity.
977 $activityParams = array(
d0c97775 978 'source_record_id' => $contribution['id'],
d66c61b6 979 'activity_type_id' => CRM_Core_PseudoConstant::getKey('CRM_Activity_BAO_Activity', 'activity_type_id', 'Contribution'),
88f7a518 980 );
d66c61b6 981 // @todo use api instead.
88f7a518
E
982 $activity = CRM_Activity_BAO_Activity::retrieve($activityParams, $defaults);
983
d0c97775 984 $this->assertEquals($contribution['id'], $activity->source_record_id, 'Check for activity associated with contribution.');
88f7a518
E
985 $this->assertEquals("$ 100.00 - STUDENT", $activity->subject, 'Check for total amount in activity.');
986
d0c97775 987 $params['id'] = $contribution['id'];
88f7a518
E
988 $params['total_amount'] = 200;
989
d0c97775 990 $contribution = $this->callAPISuccess('Contribution', 'create', $params)['values'][0];
88f7a518 991
d0c97775 992 $this->assertEquals($params['total_amount'], $contribution['total_amount'], 'Check for total amount in contribution.');
993 $this->assertEquals($contactId, $contribution['contact_id'], 'Check for contact id creation.');
88f7a518
E
994
995 // Retrieve activity again.
996 $activity = CRM_Activity_BAO_Activity::retrieve($activityParams, $defaults);
997
d0c97775 998 $this->assertEquals($contribution['id'], $activity->source_record_id, 'Check for activity associated with contribution.');
88f7a518
E
999 $this->assertEquals("$ 200.00 - STUDENT", $activity->subject, 'Check for total amount in activity.');
1000 }
1001
5c3d600f
PN
1002 /**
1003 * Test allowUpdateRevenueRecognitionDate.
1004 */
1005 public function testAllowUpdateRevenueRecognitionDate() {
1006 $contactId = $this->individualCreate();
1007 $params = array(
1008 'contact_id' => $contactId,
1009 'receive_date' => '2010-01-20',
1010 'total_amount' => 100,
4303ea89 1011 'financial_type_id' => 4,
5c3d600f
PN
1012 );
1013 $order = $this->callAPISuccess('order', 'create', $params);
1014 $allowUpdate = CRM_Contribute_BAO_Contribution::allowUpdateRevenueRecognitionDate($order['id']);
1015 $this->assertTrue($allowUpdate);
1016
1017 $event = $this->eventCreate();
1018 $params = array(
1019 'contact_id' => $contactId,
1020 'receive_date' => '2010-01-20',
1021 'total_amount' => 300,
8484a5f0 1022 'financial_type_id' => $this->getFinancialTypeId('Event Fee'),
1023 'contribution_status_id' => 'Completed',
5c3d600f
PN
1024 );
1025 $priceFields = $this->createPriceSet('event', $event['id']);
1026 foreach ($priceFields['values'] as $key => $priceField) {
1027 $lineItems[$key] = array(
1028 'price_field_id' => $priceField['price_field_id'],
1029 'price_field_value_id' => $priceField['id'],
1030 'label' => $priceField['label'],
1031 'field_title' => $priceField['label'],
1032 'qty' => 1,
1033 'unit_price' => $priceField['amount'],
1034 'line_total' => $priceField['amount'],
1035 'financial_type_id' => $priceField['financial_type_id'],
1036 'entity_table' => 'civicrm_participant',
1037 );
1038 }
1039 $params['line_items'][] = array(
1040 'line_item' => $lineItems,
1041 'params' => array(
1042 'contact_id' => $contactId,
1043 'event_id' => $event['id'],
1044 'status_id' => 1,
1045 'role_id' => 1,
1046 'register_date' => '2007-07-21 00:00:00',
1047 'source' => 'Online Event Registration: API Testing',
1048 ),
1049 );
1050 $order = $this->callAPISuccess('order', 'create', $params);
1051 $allowUpdate = CRM_Contribute_BAO_Contribution::allowUpdateRevenueRecognitionDate($order['id']);
1052 $this->assertFalse($allowUpdate);
1053
1054 $params = array(
1055 'contact_id' => $contactId,
1056 'receive_date' => '2010-01-20',
1057 'total_amount' => 200,
8484a5f0 1058 'financial_type_id' => $this->getFinancialTypeId('Member Dues'),
1059 'contribution_status_id' => 'Completed',
5c3d600f
PN
1060 );
1061 $membershipType = $this->membershipTypeCreate();
1062 $priceFields = $this->createPriceSet();
1063 $lineItems = array();
1064 foreach ($priceFields['values'] as $key => $priceField) {
1065 $lineItems[$key] = array(
1066 'price_field_id' => $priceField['price_field_id'],
1067 'price_field_value_id' => $priceField['id'],
1068 'label' => $priceField['label'],
1069 'field_title' => $priceField['label'],
1070 'qty' => 1,
1071 'unit_price' => $priceField['amount'],
1072 'line_total' => $priceField['amount'],
1073 'financial_type_id' => $priceField['financial_type_id'],
1074 'entity_table' => 'civicrm_membership',
1075 'membership_type_id' => $membershipType,
1076 );
1077 }
1078 $params['line_items'][] = array(
1079 'line_item' => array(array_pop($lineItems)),
1080 'params' => array(
1081 'contact_id' => $contactId,
1082 'membership_type_id' => $membershipType,
1083 'join_date' => '2006-01-21',
1084 'start_date' => '2006-01-21',
1085 'end_date' => '2006-12-21',
1086 'source' => 'Payment',
1087 'is_override' => 1,
1088 'status_id' => 1,
1089 ),
1090 );
1091 $order = $this->callAPISuccess('order', 'create', $params);
1092 $allowUpdate = CRM_Contribute_BAO_Contribution::allowUpdateRevenueRecognitionDate($order['id']);
1093 $this->assertFalse($allowUpdate);
1094 }
1095
d9553c2e
PN
1096 /**
1097 * Test calculateFinancialItemAmount().
1098 */
1099 public function testcalculateFinancialItemAmount() {
1100 $testParams = array(
1101 array(
1102 'params' => array(),
1103 'amountParams' => array(
1104 'line_total' => 100,
1105 'previous_line_total' => 300,
1106 'diff' => 1,
1107 ),
1108 'context' => 'changedAmount',
1109 'expectedItemAmount' => -200,
1110 ),
1111 array(
1112 'params' => array(),
1113 'amountParams' => array(
1114 'line_total' => 100,
1115 'previous_line_total' => 100,
1116 'diff' => -1,
1117 ),
1118 'context' => 'changePaymentInstrument',
1119 'expectedItemAmount' => -100,
1120 ),
1121 array(
1122 'params' => array(
1123 'is_quick_config' => TRUE,
1124 'total_amount' => 110,
1125 'tax_amount' => 10,
1126 ),
1127 'amountParams' => array(
1128 'item_amount' => 100,
1129 ),
1130 'context' => 'changedAmount',
1131 'expectedItemAmount' => 100,
1132 ),
1133 array(
1134 'params' => array(
1135 'is_quick_config' => TRUE,
1136 'total_amount' => 110,
1137 'tax_amount' => 10,
1138 ),
1139 'amountParams' => array(
1140 'item_amount' => NULL,
1141 ),
1142 'context' => 'changedAmount',
1143 'expectedItemAmount' => 110,
1144 ),
1145 array(
1146 'params' => array(
1147 'is_quick_config' => TRUE,
1148 'total_amount' => 110,
1149 'tax_amount' => 10,
1150 ),
1151 'amountParams' => array(
1152 'item_amount' => NULL,
1153 ),
1154 'context' => NULL,
1155 'expectedItemAmount' => 100,
1156 ),
1157 );
1158 foreach ($testParams as $params) {
1159 $itemAmount = CRM_Contribute_BAO_Contribution::calculateFinancialItemAmount($params['params'], $params['amountParams'], $params['context']);
1160 $this->assertEquals($itemAmount, $params['expectedItemAmount'], 'Invalid Financial Item amount.');
1161 }
1162 }
1163
d934a732
PN
1164 /**
1165 * Test recording of amount with comma separator.
1166 */
1167 public function testCommaSeparatorAmount() {
1168 $contactId = $this->individualCreate();
1169
1170 $params = array(
1171 'contact_id' => $contactId,
1172 'currency' => 'USD',
1173 'financial_type_id' => 1,
1174 'contribution_status_id' => 8,
1175 'payment_instrument_id' => 1,
1176 'receive_date' => '20080522000000',
1177 'receipt_date' => '20080522000000',
1178 'total_amount' => '20000.00',
1179 'partial_payment_total' => '20,000.00',
f49cdeab 1180 'partial_amount_to_pay' => '8,000.00',
d934a732
PN
1181 );
1182
d0c97775 1183 $contribution = $this->callAPISuccess('Contribution', 'create', $params);
1184 $lastFinancialTrxnId = CRM_Core_BAO_FinancialTrxn::getFinancialTrxnId($contribution['id'], 'DESC');
d934a732
PN
1185 $financialTrxn = $this->callAPISuccessGetSingle(
1186 'FinancialTrxn',
1187 array(
1188 'id' => $lastFinancialTrxnId['financialTrxnId'],
1189 'return' => array('total_amount'),
1190 )
1191 );
1192 $this->assertEquals($financialTrxn['total_amount'], 8000, 'Invalid Tax amount.');
1193 }
1194
adbc354b
PN
1195 /**
1196 * Test for function getSalesTaxFinancialAccounts().
1197 */
1198 public function testgetSalesTaxFinancialAccounts() {
1199 $this->enableTaxAndInvoicing();
1200 $financialType = $this->createFinancialType();
1201 $financialAccount = $this->relationForFinancialTypeWithFinancialAccount($financialType['id']);
1202 $expectedResult = array($financialAccount->financial_account_id => $financialAccount->financial_account_id);
1203 $financialType = $this->createFinancialType();
1204 $financialAccount = $this->relationForFinancialTypeWithFinancialAccount($financialType['id']);
1205 $expectedResult[$financialAccount->financial_account_id] = $financialAccount->financial_account_id;
1206 $salesTaxFinancialAccount = CRM_Contribute_BAO_Contribution::getSalesTaxFinancialAccounts();
1207 $this->assertTrue(($salesTaxFinancialAccount == $expectedResult), 'Function returned wrong values.');
1208 }
1209
0409b821
PN
1210 /**
1211 * Test for function createProportionalEntry().
83644f47 1212 *
1213 * @param string $thousandSeparator
1214 * punctuation used to refer to thousands.
1215 *
1216 * @dataProvider getThousandSeparators
0409b821 1217 */
83644f47 1218 public function testCreateProportionalEntry($thousandSeparator) {
1219 $this->setCurrencySeparators($thousandSeparator);
2a4a2f00 1220 list($contribution, $financialAccount) = $this->createContributionWithTax();
0409b821
PN
1221 $params = array(
1222 'total_amount' => 55,
1223 'to_financial_account_id' => $financialAccount->financial_account_id,
1224 'payment_instrument_id' => 1,
1225 'trxn_date' => date('Ymd'),
1226 'status_id' => 1,
0409b821 1227 'entity_id' => $contribution['id'],
0409b821 1228 );
3137781d 1229 $financialTrxn = $this->callAPISuccess('FinancialTrxn', 'create', $params);
0409b821
PN
1230 $entityParams = array(
1231 'contribution_total_amount' => $contribution['total_amount'],
1232 'trxn_total_amount' => 55,
1233 'line_item_amount' => 100,
1234 );
1235 $previousLineItem = CRM_Financial_BAO_FinancialItem::getPreviousFinancialItem($contribution['id']);
1236 $eftParams = array(
1237 'entity_table' => 'civicrm_financial_item',
cf28d075 1238 'entity_id' => $previousLineItem['id'],
3137781d 1239 'financial_trxn_id' => (string) $financialTrxn['id'],
0409b821
PN
1240 );
1241 CRM_Contribute_BAO_Contribution::createProportionalEntry($entityParams, $eftParams);
1242 $trxnTestArray = array_merge($eftParams, array(
3137781d 1243 'amount' => '50.00',
0409b821 1244 ));
cf28d075 1245 $this->callAPISuccessGetSingle('EntityFinancialTrxn', $eftParams, $trxnTestArray);
0409b821
PN
1246 }
1247
c364c544
MW
1248 /**
1249 * Test for function createProportionalEntry with zero amount().
1250 *
1251 * @param string $thousandSeparator
1252 * punctuation used to refer to thousands.
1253 *
1254 * @dataProvider getThousandSeparators
1255 */
1256 public function testCreateProportionalEntryZeroAmount($thousandSeparator) {
1257 $this->setCurrencySeparators($thousandSeparator);
1258 list($contribution, $financialAccount) = $this->createContributionWithTax(array('total_amount' => 0));
1259 $params = array(
1260 'total_amount' => 0,
1261 'to_financial_account_id' => $financialAccount->financial_account_id,
1262 'payment_instrument_id' => 1,
1263 'trxn_date' => date('Ymd'),
1264 'status_id' => 1,
1265 'entity_id' => $contribution['id'],
1266 );
1267 $financialTrxn = $this->callAPISuccess('FinancialTrxn', 'create', $params);
1268 $entityParams = array(
1269 'contribution_total_amount' => $contribution['total_amount'],
1270 'trxn_total_amount' => 0,
1271 'line_item_amount' => 0,
1272 );
1273 $previousLineItem = CRM_Financial_BAO_FinancialItem::getPreviousFinancialItem($contribution['id']);
1274 $eftParams = array(
1275 'entity_table' => 'civicrm_financial_item',
1276 'entity_id' => $previousLineItem['id'],
1277 'financial_trxn_id' => (string) $financialTrxn['id'],
1278 );
1279 CRM_Contribute_BAO_Contribution::createProportionalEntry($entityParams, $eftParams);
1280 $trxnTestArray = array_merge($eftParams, array(
1281 'amount' => '0.00',
1282 ));
1283 $this->callAPISuccessGetSingle('EntityFinancialTrxn', $eftParams, $trxnTestArray);
1284 }
1285
646bc565
PN
1286 /**
1287 * Test for function getLastFinancialItemIds().
1288 */
1289 public function testgetLastFinancialItemIds() {
2a4a2f00 1290 list($contribution, $financialAccount) = $this->createContributionWithTax();
646bc565
PN
1291 list($ftIds, $taxItems) = CRM_Contribute_BAO_Contribution::getLastFinancialItemIds($contribution['id']);
1292 $this->assertEquals(count($ftIds), 1, 'Invalid count.');
1293 $this->assertEquals(count($taxItems), 1, 'Invalid count.');
1294 foreach ($taxItems as $value) {
1295 $this->assertEquals($value['amount'], 10, 'Invalid tax amount.');
1296 }
1297 }
1298
2a4a2f00
PN
1299 /**
1300 * Test for function createProportionalFinancialEntries().
1301 */
1302 public function testcreateProportionalFinancialEntries() {
1303 list($contribution, $financialAccount) = $this->createContributionWithTax();
1304 $params = array(
3137781d 1305 'total_amount' => 50,
2a4a2f00
PN
1306 'to_financial_account_id' => $financialAccount->financial_account_id,
1307 'payment_instrument_id' => 1,
1308 'trxn_date' => date('Ymd'),
1309 'status_id' => 1,
2a4a2f00 1310 'entity_id' => $contribution['id'],
2a4a2f00 1311 );
3137781d 1312 $financialTrxn = $this->callAPISuccess('FinancialTrxn', 'create', $params);
2a4a2f00
PN
1313 $entityParams = array(
1314 'contribution_total_amount' => $contribution['total_amount'],
1315 'trxn_total_amount' => 55,
1316 'trxn_id' => $financialTrxn['id'],
1317 );
1318 $lineItems = CRM_Price_BAO_LineItem::getLineItemsByContributionID($contribution['id']);
1319 list($ftIds, $taxItems) = CRM_Contribute_BAO_Contribution::getLastFinancialItemIds($contribution['id']);
1320 CRM_Contribute_BAO_Contribution::createProportionalFinancialEntries($entityParams, $lineItems, $ftIds, $taxItems);
1321 $eftParams = array(
1322 'entity_table' => 'civicrm_financial_item',
1323 'financial_trxn_id' => $financialTrxn['id'],
1324 );
1325 $entityFinancialTrxn = $this->callAPISuccess('EntityFinancialTrxn', 'Get', $eftParams);
1326 $this->assertEquals($entityFinancialTrxn['count'], 2, 'Invalid count.');
1327 $testAmount = array(5, 50);
1328 foreach ($entityFinancialTrxn['values'] as $value) {
1329 $this->assertEquals($value['amount'], array_pop($testAmount), 'Invalid amount stored in civicrm_entity_financial_trxn.');
77641de4
PN
1330 }
1331 }
1332
1333 /**
1334 * Test to check if amount is proportionally asigned for PI change.
1335 */
1336 public function testProportionallyAssignedForPIChange() {
1337 list($contribution, $financialAccount) = $this->createContributionWithTax();
1338 $params = array(
1339 'id' => $contribution['id'],
1340 'payment_instrument_id' => 3,
1341 );
1342 $this->callAPISuccess('Contribution', 'create', $params);
1343 $lastFinancialTrxnId = CRM_Core_BAO_FinancialTrxn::getFinancialTrxnId($contribution['id'], 'DESC');
1344 $eftParams = array(
1345 'entity_table' => 'civicrm_financial_item',
1346 'financial_trxn_id' => $lastFinancialTrxnId['financialTrxnId'],
1347 );
1348 $entityFinancialTrxn = $this->callAPISuccess('EntityFinancialTrxn', 'Get', $eftParams);
1349 $this->assertEquals($entityFinancialTrxn['count'], 2, 'Invalid count.');
1350 $testAmount = array(10, 100);
1351 foreach ($entityFinancialTrxn['values'] as $value) {
1352 $this->assertEquals($value['amount'], array_pop($testAmount), 'Invalid amount stored in civicrm_entity_financial_trxn.');
2a4a2f00
PN
1353 }
1354 }
1355
646bc565
PN
1356 /**
1357 * Function to create contribution with tax.
1358 */
c364c544
MW
1359 public function createContributionWithTax($params = array()) {
1360 if (!isset($params['total_amount'])) {
1361 $params['total_amount'] = 100;
1362 }
646bc565
PN
1363 $contactId = $this->individualCreate();
1364 $this->enableTaxAndInvoicing();
1365 $financialType = $this->createFinancialType();
1366 $financialAccount = $this->relationForFinancialTypeWithFinancialAccount($financialType['id']);
1367 $form = new CRM_Contribute_Form_Contribution();
1368
1369 $form->testSubmit(array(
39b959db
SL
1370 'total_amount' => $params['total_amount'],
1371 'financial_type_id' => $financialType['id'],
1372 'contact_id' => $contactId,
1373 'contribution_status_id' => 1,
1374 'price_set_id' => 0,
1375 ), CRM_Core_Action::ADD);
646bc565
PN
1376 $contribution = $this->callAPISuccessGetSingle('Contribution',
1377 array(
1378 'contact_id' => $contactId,
1379 'return' => array('tax_amount', 'total_amount'),
1380 )
1381 );
2a4a2f00 1382 return array($contribution, $financialAccount);
646bc565
PN
1383 }
1384
75c07fde
JP
1385 /**
1386 * Test processOnBehalfOrganization() function.
1387 */
1388 public function testProcessOnBehalfOrganization() {
1389 $orgInfo = array(
1390 'phone' => '11111111',
1391 'email' => 'testorg@gmail.com',
1392 'street_address' => 'test Street',
1393 'city' => 'test City',
1394 'state_province' => 'AA',
1395 'postal_code' => '222222',
1396 'country' => 'United States',
1397 );
1398 $contactID = $this->individualCreate();
1399 $orgId = $this->organizationCreate(array('organization_name' => 'testorg1'));
1400 $orgCount = $this->callAPISuccessGetCount('Contact', array(
1401 'contact_type' => "Organization",
1402 'organization_name' => "testorg1",
1403 ));
1404 $this->assertEquals($orgCount, 1);
1405
1406 $values = $params = array();
1407 $behalfOrganization = array(
1408 'organization_name' => 'testorg1',
1409 'phone' => array(
1410 1 => array(
1411 'phone' => $orgInfo['phone'],
1412 'is_primary' => 1,
1413 ),
1414 ),
1415 'email' => array(
1416 1 => array(
1417 'email' => $orgInfo['email'],
1418 'is_primary' => 1,
1419 ),
1420 ),
1421 'address' => array(
1422 3 => array(
1423 'street_address' => $orgInfo['street_address'],
1424 'city' => $orgInfo['city'],
1425 'location_type_id' => 3,
1426 'postal_code' => $orgInfo['postal_code'],
1427 'country' => 'US',
1428 'state_province' => 'AA',
1429 'is_primary' => 1,
1430 ),
1431 ),
1432 );
1433 $fields = array(
1434 'organization_name' => 1,
1435 'phone-3-1' => 1,
1436 'email-3' => 1,
1437 'street_address-3' => 1,
1438 'city-3' => 1,
1439 'postal_code-3' => 1,
1440 'country-3' => 1,
1441 'state_province-3' => 1,
1442 );
1443 CRM_Contribute_Form_Contribution_Confirm::processOnBehalfOrganization($behalfOrganization, $contactID, $values, $params, $fields);
1444
1445 //Check whether new organisation is not created.
1446 $result = $this->callAPISuccess('Contact', 'get', array(
1447 'contact_type' => "Organization",
1448 'organization_name' => "testorg1",
1449 ));
1450 $this->assertEquals($result['count'], 1);
1451
1452 //Assert all org values are updated.
1453 foreach ($orgInfo as $key => $val) {
1454 $this->assertEquals($result['values'][$orgId][$key], $val);
1455 }
1456
1457 //Check if alert is assigned to params if more than 1 dupe exists.
1458 $orgId = $this->organizationCreate(array('organization_name' => 'testorg1', 'email' => 'testorg@gmail.com'));
1459 CRM_Contribute_Form_Contribution_Confirm::processOnBehalfOrganization($behalfOrganization, $contactID, $values, $params, $fields);
1460 $this->assertEquals($params['onbehalf_dupe_alert'], 1);
1461 }
1462
7e2ec997
E
1463 /**
1464 * Test for replaceContributionTokens.
1465 * This function tests whether the contribution tokens are replaced with values from contribution.
1466 */
1467 public function testReplaceContributionTokens() {
1468 $contactId1 = $this->individualCreate();
1469 $params = array(
1470 'contact_id' => $contactId1,
1471 'receive_date' => '20120511',
1472 'total_amount' => 100.00,
1473 'financial_type_id' => 1,
1474 'trxn_id' => 12345,
1475 'invoice_id' => 67890,
1476 'source' => 'SSF',
1477 'contribution_status_id' => 2,
1478 );
1479 $contribution1 = $this->contributionCreate($params);
1480 $contactId2 = $this->individualCreate();
1481 $params = array(
1482 'contact_id' => $contactId2,
1483 'receive_date' => '20150511',
1484 'total_amount' => 200.00,
1485 'financial_type_id' => 1,
1486 'trxn_id' => 6789,
1487 'invoice_id' => 12345,
1488 'source' => 'ABC',
1489 'contribution_status_id' => 1,
1490 );
1491 $contribution2 = $this->contributionCreate($params);
1492 $ids = array($contribution1, $contribution2);
1493
1494 $subject = "This is a test for contribution ID: {contribution.contribution_id}";
1495 $text = "Contribution Amount: {contribution.total_amount}";
1496 $html = "<p>Contribution Source: {contribution.contribution_source}</p></br>
1497 <p>Contribution Invoice ID: {contribution.invoice_id}</p></br>
1498 <p>Contribution Receive Date: {contribution.receive_date}</p></br>";
1499
1500 $subjectToken = CRM_Utils_Token::getTokens($subject);
1501 $messageToken = CRM_Utils_Token::getTokens($text);
1502 $messageToken = array_merge($messageToken, CRM_Utils_Token::getTokens($html));
1503
1504 $contributionDetails = CRM_Contribute_BAO_Contribution::replaceContributionTokens(
1505 $ids,
1506 $subject,
1507 $subjectToken,
1508 $text,
1509 $html,
1510 $messageToken,
1511 TRUE
1512 );
1513
1514 $this->assertEquals("Contribution Amount: $ 100.00", $contributionDetails[$contactId1]['text'], "The text does not match");
1515 $this->assertEquals("<p>Contribution Source: ABC</p></br>
1516 <p>Contribution Invoice ID: 12345</p></br>
1517 <p>Contribution Receive Date: May 11th, 2015</p></br>", $contributionDetails[$contactId2]['html'], "The html does not match");
1518 }
1519
7b5169d0
PN
1520 /**
1521 * Test for contribution with deferred revenue.
1522 */
1523 public function testContributionWithDeferredRevenue() {
1524 $contactId = $this->individualCreate();
1525 Civi::settings()->set('deferred_revenue_enabled', TRUE);
1526 $params = array(
1527 'contact_id' => $contactId,
1528 'receive_date' => '20120511',
1529 'total_amount' => 100.00,
1530 'financial_type_id' => 'Event Fee',
1531 'trxn_id' => 12345,
1532 'invoice_id' => 67890,
1533 'source' => 'SSF',
1534 'contribution_status_id' => 'Completed',
1535 'revenue_recognition_date' => date('Ymd', strtotime("+3 month")),
1536 );
1537 $contribution = $this->callAPISuccess('contribution', 'create', $params);
1538
1539 $this->callAPISuccessGetCount('EntityFinancialTrxn', array(
1540 'entity_table' => "civicrm_contribution",
1541 'entity_id' => $contribution['id'],
1542 ), 2);
1543
1544 $checkAgainst = array(
1545 'financial_trxn_id.to_financial_account_id.name' => 'Deferred Revenue - Event Fee',
1546 'financial_trxn_id.from_financial_account_id.name' => 'Event Fee',
f818aed5 1547 'financial_trxn_id' => '2',
7b5169d0
PN
1548 );
1549 $result = $this->callAPISuccessGetSingle('EntityFinancialTrxn', array(
1550 'return' => array(
1551 "financial_trxn_id.from_financial_account_id.name",
1552 "financial_trxn_id.to_financial_account_id.name",
1553 "financial_trxn_id",
1554 ),
1555 'entity_table' => "civicrm_contribution",
1556 'entity_id' => $contribution['id'],
1557 'financial_trxn_id.is_payment' => 0,
1558 ), $checkAgainst);
1559
1560 $result = $this->callAPISuccessGetSingle('EntityFinancialTrxn', array(
1561 'entity_table' => "civicrm_financial_item",
1562 'financial_trxn_id' => $result['financial_trxn_id'],
1563 'return' => array('entity_id'),
1564 ));
1565
1566 $checkAgainst = array(
1567 'financial_account_id.name' => 'Deferred Revenue - Event Fee',
1568 'id' => $result['entity_id'],
1569 );
1570 $result = $this->callAPISuccessGetSingle('FinancialItem', array(
1571 'id' => $result['entity_id'],
1572 'return' => array("financial_account_id.name"),
1573 ), $checkAgainst);
1574 }
1575
a495d83a
PN
1576 /**
1577 * https://lab.civicrm.org/dev/financial/issues/56
1578 * Changing financial type on a contribution records correct financial items
1579 */
1580 public function testChangingFinancialTypeWithoutTax() {
1581 $ids = $values = [];
1582 $contactId = $this->individualCreate();
1583 $params = array(
1584 'contact_id' => $contactId,
1585 'receive_date' => date('YmdHis'),
1586 'total_amount' => 100.00,
1587 'financial_type_id' => 'Donation',
1588 'contribution_status_id' => 'Completed',
1589 );
1590 /* first test the scenario when sending an email */
1591 $contributionId = $this->callAPISuccess(
1592 'contribution',
1593 'create',
1594 $params
1595 )['id'];
1596
1597 // Update Financial Type.
1598 $this->callAPISuccess('contribution', 'create', [
1599 'id' => $contributionId,
1600 'financial_type_id' => 'Event Fee',
1601 ]);
1602
1603 // Get line item
1604 $lineItem = $this->callAPISuccessGetSingle('LineItem', [
1605 'contribution_id' => $contributionId,
1606 'return' => ["financial_type_id.name", "line_total"],
1607 ]);
1608
1609 $this->assertEquals(
1610 $lineItem['line_total'],
1611 100.00,
1612 'Invalid line amount.'
1613 );
1614
1615 $this->assertEquals(
1616 $lineItem['financial_type_id.name'],
1617 'Event Fee',
1618 'Invalid Financial Type stored.'
1619 );
1620
1621 // Get Financial Items.
1622 $financialItems = $this->callAPISuccess('FinancialItem', 'get', [
1623 'entity_id' => $lineItem['id'],
1624 'sequential' => 1,
1625 'entity_table' => 'civicrm_line_item',
1626 'options' => ['sort' => "id"],
1627 'return' => ["financial_account_id.name", "amount", "description"],
1628 ]);
1629
1630 $this->assertEquals($financialItems['count'], 3, 'Count mismatch.');
1631
1632 $toCheck = [
1633 ['Donation', 100.00],
1634 ['Donation', -100.00],
1635 ['Event Fee', 100.00],
1636 ];
1637
1638 foreach ($financialItems['values'] as $key => $values) {
1639 $this->assertEquals(
1640 $values['financial_account_id.name'],
1641 $toCheck[$key][0],
1642 'Invalid Financial Account stored.'
1643 );
1644 $this->assertEquals(
1645 $values['amount'],
1646 $toCheck[$key][1],
1647 'Amount mismatch.'
1648 );
1649 $this->assertEquals(
1650 $values['description'],
1651 'Contribution Amount',
1652 'Description mismatch.'
1653 );
1654 }
1655
1656 // Check transactions.
1657 $financialTransactions = $this->callAPISuccess('EntityFinancialTrxn', 'get', [
1658 'return' => ["financial_trxn_id"],
1659 'entity_table' => "civicrm_contribution",
1660 'entity_id' => $contributionId,
1661 'sequential' => 1,
1662 ]);
1663 $this->assertEquals($financialTransactions['count'], 3, 'Count mismatch.');
1664
1665 foreach ($financialTransactions['values'] as $key => $values) {
1666 $this->callAPISuccessGetCount('EntityFinancialTrxn', [
1667 'financial_trxn_id' => $values['financial_trxn_id'],
1668 'amount' => $toCheck[$key][1],
1669 'financial_trxn_id.total_amount' => $toCheck[$key][1],
1670 ], 2);
1671 }
1672 }
1673
1db3ddea
KE
1674 /**
1675 * CRM-21424 Check if the receipt update is set after composing the receipt message
1676 */
1677 public function testSendMailUpdateReceiptDate() {
1678 $ids = $values = array();
1679 $contactId = $this->individualCreate();
1680 $params = array(
1681 'contact_id' => $contactId,
1682 'receive_date' => '20120511',
1683 'total_amount' => 100.00,
1684 'financial_type_id' => 'Donation',
1685 'source' => 'SSF',
1686 'contribution_status_id' => 'Completed',
1687 );
1688 /* first test the scenario when sending an email */
1689 $contribution = $this->callAPISuccess('contribution', 'create', $params);
1690 $contributionId = $contribution['id'];
1691 $this->assertDBNull('CRM_Contribute_BAO_Contribution', $contributionId, 'receipt_date', 'id', 'After creating receipt date must be null');
1692 $input = array('receipt_update' => 0);
1693 CRM_Contribute_BAO_Contribution::sendMail($input, $ids, $contributionId, $values);
1694 $this->assertDBNull('CRM_Contribute_BAO_Contribution', $contributionId, 'receipt_date', 'id', 'After sendMail, with the explicit instruction not to update receipt date stays null');
1695 $input = array('receipt_update' => 1);
1696 CRM_Contribute_BAO_Contribution::sendMail($input, $ids, $contributionId, $values);
1697 $this->assertDBNotNull('CRM_Contribute_BAO_Contribution', $contributionId, 'receipt_date', 'id', 'After sendMail with the permission to allow update receipt date must be set');
1698
1699 /* repeat the same scenario for downloading a pdf */
1700 $contribution = $this->callAPISuccess('contribution', 'create', $params);
1701 $contributionId = $contribution['id'];
1702 $this->assertDBNull('CRM_Contribute_BAO_Contribution', $contributionId, 'receipt_date', 'id', 'After creating receipt date must be null');
1703 $input = array('receipt_update' => 0);
1704 /* setting the lasast parameter (returnmessagetext) to TRUE is done by the download of the pdf */
1705 CRM_Contribute_BAO_Contribution::sendMail($input, $ids, $contributionId, $values, TRUE);
1706 $this->assertDBNull('CRM_Contribute_BAO_Contribution', $contributionId, 'receipt_date', 'id', 'After sendMail, with the explicit instruction not to update receipt date stays null');
1707 $input = array('receipt_update' => 1);
1708 CRM_Contribute_BAO_Contribution::sendMail($input, $ids, $contributionId, $values, TRUE);
1709 $this->assertDBNotNull('CRM_Contribute_BAO_Contribution', $contributionId, 'receipt_date', 'id', 'After sendMail with the permission to allow update receipt date must be set');
1710 }
1711
6a488035 1712}