tests/phpunit - Declare `@group headless`
[civicrm-core.git] / tests / phpunit / CRM / Contribute / BAO / ContributionTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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 require_once 'CiviTest/Contact.php';
29 require_once 'CiviTest/Custom.php';
30
31 /**
32 * Class CRM_Contribute_BAO_ContributionTest
33 * @group headless
34 */
35 class CRM_Contribute_BAO_ContributionTest extends CiviUnitTestCase {
36
37 /**
38 * Create() method (create and update modes).
39 */
40 public function testCreate() {
41 $contactId = Contact::createIndividual();
42 $ids = array('contribution' => NULL);
43
44 $params = array(
45 'contact_id' => $contactId,
46 'currency' => 'USD',
47 'financial_type_id' => 1,
48 'contribution_status_id' => 1,
49 'payment_instrument_id' => 1,
50 'source' => 'STUDENT',
51 'receive_date' => '20080522000000',
52 'receipt_date' => '20080522000000',
53 'non_deductible_amount' => 0.00,
54 'total_amount' => 200.00,
55 'fee_amount' => 5,
56 'net_amount' => 195,
57 'trxn_id' => '22ereerwww444444',
58 'invoice_id' => '86ed39c9e9ee6ef6031621ce0eafe7eb81',
59 'thankyou_date' => '20080522',
60 );
61
62 $contribution = CRM_Contribute_BAO_Contribution::create($params, $ids);
63
64 $this->assertEquals($params['trxn_id'], $contribution->trxn_id, 'Check for transcation id creation.');
65 $this->assertEquals($contactId, $contribution->contact_id, 'Check for contact id creation.');
66
67 //update contribution amount
68 $ids = array('contribution' => $contribution->id);
69 $params['fee_amount'] = 10;
70 $params['net_amount'] = 190;
71
72 $contribution = CRM_Contribute_BAO_Contribution::create($params, $ids);
73
74 $this->assertEquals($params['trxn_id'], $contribution->trxn_id, 'Check for transcation id .');
75 $this->assertEquals($params['net_amount'], $contribution->net_amount, 'Check for Amount updation.');
76
77 //Delete Contribution
78 $this->contributionDelete($contribution->id);
79
80 //Delete Contact
81 Contact::delete($contactId);
82 }
83
84 /**
85 * Create() method with custom data.
86 */
87 public function testCreateWithCustomData() {
88 $contactId = Contact::createIndividual();
89 $ids = array('contribution' => NULL);
90
91 //create custom data
92 $customGroup = Custom::createGroup(array(), 'Contribution');
93 $fields = array(
94 'label' => 'testFld',
95 'data_type' => 'String',
96 'html_type' => 'Text',
97 'is_active' => 1,
98 'custom_group_id' => $customGroup->id,
99 );
100 $customField = CRM_Core_BAO_CustomField::create($fields);
101
102 $params = array(
103 'contact_id' => $contactId,
104 'currency' => 'USD',
105 'financial_type_id' => 1,
106 'contribution_status_id' => 1,
107 'payment_instrument_id' => 1,
108 'source' => 'STUDENT',
109 'receive_date' => '20080522000000',
110 'receipt_date' => '20080522000000',
111 'id' => NULL,
112 'non_deductible_amount' => 0.00,
113 'total_amount' => 200.00,
114 'fee_amount' => 5,
115 'net_amount' => 195,
116 'trxn_id' => '22ereerwww322323',
117 'invoice_id' => '22ed39c9e9ee6ef6031621ce0eafe6da70',
118 'thankyou_date' => '20080522',
119 );
120
121 $params['custom'] = array(
122 $customField->id => array(
123 -1 => array(
124 'value' => 'Test custom value',
125 'type' => 'String',
126 'custom_field_id' => $customField->id,
127 'custom_group_id' => $customGroup->id,
128 'table_name' => $customGroup->table_name,
129 'column_name' => $customField->column_name,
130 'file_id' => NULL,
131 ),
132 ),
133 );
134
135 $contribution = CRM_Contribute_BAO_Contribution::create($params, $ids);
136
137 // Check that the custom field value is saved
138 $customValueParams = array(
139 'entityID' => $contribution->id,
140 'custom_' . $customField->id => 1,
141 );
142 $values = CRM_Core_BAO_CustomValueTable::getValues($customValueParams);
143 $this->assertEquals('Test custom value', $values['custom_' . $customField->id], 'Check the custom field value');
144
145 $this->assertEquals($params['trxn_id'], $contribution->trxn_id, 'Check for transcation id creation.');
146 $this->assertEquals($contactId, $contribution->contact_id, 'Check for contact id for Conribution.');
147
148 $this->contributionDelete($contribution->id);
149 Custom::deleteField($customField);
150 Custom::deleteGroup($customGroup);
151 Contact::delete($contactId);
152 }
153
154 /**
155 * DeleteContribution() method
156 */
157 public function testDeleteContribution() {
158 $contactId = Contact::createIndividual();
159 $ids = array('contribution' => NULL);
160
161 $params = array(
162 'contact_id' => $contactId,
163 'currency' => 'USD',
164 'financial_type_id' => 1,
165 'contribution_status_id' => 1,
166 'payment_instrument_id' => 1,
167 'source' => 'STUDENT',
168 'receive_date' => '20080522000000',
169 'receipt_date' => '20080522000000',
170 'id' => NULL,
171 'non_deductible_amount' => 0.00,
172 'total_amount' => 200.00,
173 'fee_amount' => 5,
174 'net_amount' => 195,
175 'trxn_id' => '33ereerwww322323',
176 'invoice_id' => '33ed39c9e9ee6ef6031621ce0eafe6da70',
177 'thankyou_date' => '20080522',
178 );
179
180 $contribution = CRM_Contribute_BAO_Contribution::create($params, $ids);
181
182 $this->assertEquals($params['trxn_id'], $contribution->trxn_id, 'Check for transcation id creation.');
183 $this->assertEquals($contactId, $contribution->contact_id, 'Check for contact id creation.');
184
185 $contributiondelete = CRM_Contribute_BAO_Contribution::deleteContribution($contribution->id);
186
187 $this->assertDBNull('CRM_Contribute_DAO_Contribution', $contribution->trxn_id,
188 'id', 'trxn_id', 'Database check for deleted Contribution.'
189 );
190 Contact::delete($contactId);
191 }
192
193 /**
194 * Create honor-contact method
195 */
196 public function testcreateAndGetHonorContact() {
197 $firstName = 'John_' . substr(sha1(rand()), 0, 7);
198 $lastName = 'Smith_' . substr(sha1(rand()), 0, 7);
199 $email = "{$firstName}.{$lastName}@example.com";
200
201 //Get profile id of name honoree_individual used to create profileContact
202 $honoreeProfileId = NULL;
203 $ufGroupDAO = new CRM_Core_DAO_UFGroup();
204 $ufGroupDAO->name = 'honoree_individual';
205 if ($ufGroupDAO->find(TRUE)) {
206 $honoreeProfileId = $ufGroupDAO->id;
207 }
208
209 $params = array(
210 'prefix_id' => 3,
211 'first_name' => $firstName,
212 'last_name' => $lastName,
213 'email-1' => $email,
214 );
215 $softParam = array('soft_credit_type_id' => 1);
216
217 $honoreeContactId = CRM_Contact_BAO_Contact::createProfileContact($params, CRM_Core_DAO::$_nullArray,
218 NULL, NULL, $honoreeProfileId
219 );
220
221 $this->assertDBCompareValue('CRM_Contact_DAO_Contact', $honoreeContactId, 'first_name', 'id', $firstName,
222 'Database check for created honor contact record.'
223 );
224 //create contribution on behalf of honary.
225
226 $contactId = Contact::createIndividual();
227 $softParam['contact_id'] = $honoreeContactId;
228
229 $ids = array('contribution' => NULL);
230 $param = array(
231 'contact_id' => $contactId,
232 'currency' => 'USD',
233 'financial_type_id' => 4,
234 'contribution_status_id' => 1,
235 'receive_date' => date('Ymd'),
236 'total_amount' => 66,
237 );
238
239 $contribution = CRM_Contribute_BAO_Contribution::create($param, $ids);
240 $id = $contribution->id;
241 $softParam['contribution_id'] = $id;
242 $softParam['currency'] = $contribution->currency;
243 $softParam['amount'] = $contribution->total_amount;
244
245 //Create Soft Contribution for honoree contact
246 CRM_Contribute_BAO_ContributionSoft::add($softParam);
247
248 $this->assertDBCompareValue('CRM_Contribute_DAO_ContributionSoft', $id, 'contact_id',
249 'contribution_id', $honoreeContactId, 'Check DB for honor contact of the contribution'
250 );
251 //get honorary information
252 $getHonorContact = CRM_Contribute_BAO_Contribution::getHonorContacts($honoreeContactId);
253 $this->assertEquals(array(
254 $id => array(
255 'honor_type' => 'In Honor of',
256 'honorId' => $id,
257 'display_name' => 'John Doe',
258 'type' => 'Event Fee',
259 'type_id' => '4',
260 'amount' => '$ 66.00',
261 'source' => NULL,
262 'receive_date' => date('Y-m-d 00:00:00'),
263 'contribution_status' => 'Completed',
264 ),
265 ), $getHonorContact);
266
267 $this->assertDBCompareValue('CRM_Contact_DAO_Contact', $honoreeContactId, 'first_name', 'id', $firstName,
268 'Database check for created honor contact record.'
269 );
270
271 //get annual contribution information
272 $annual = CRM_Contribute_BAO_Contribution::annual($contactId);
273
274 $config = CRM_Core_Config::singleton();
275 $currencySymbol = CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_Currency', $config->defaultCurrency, 'symbol', 'name');
276 $this->assertDBCompareValue('CRM_Contribute_DAO_Contribution', $id, 'total_amount',
277 'id', ltrim($annual[2], $currencySymbol), 'Check DB for total amount of the contribution'
278 );
279
280 //Delete honor contact
281 Contact::delete($honoreeContactId);
282
283 //Delete Contribution record
284 $this->contributionDelete($contribution->id);
285
286 //Delete contributor contact
287 Contact::delete($contactId);
288 }
289
290 /**
291 * Display sort name during.
292 * Update multiple contributions
293 * sortName();
294 */
295 public function testsortName() {
296 $params = array(
297 'first_name' => 'Shane',
298 'last_name' => 'Whatson',
299 'contact_type' => 'Individual',
300 );
301
302 $contact = CRM_Contact_BAO_Contact::add($params);
303
304 //Now check $contact is object of contact DAO..
305 $this->assertInstanceOf('CRM_Contact_DAO_Contact', $contact, 'Check for created object');
306
307 $contactId = $contact->id;
308
309 $ids = array('contribution' => NULL);
310
311 $param = array(
312 'contact_id' => $contactId,
313 'currency' => 'USD',
314 'financial_type_id' => 1,
315 'contribution_status_id' => 1,
316 'payment_instrument_id' => 1,
317 'source' => 'STUDENT',
318 'receive_date' => '20080522000000',
319 'receipt_date' => '20080522000000',
320 'id' => NULL,
321 'non_deductible_amount' => 0.00,
322 'total_amount' => 300.00,
323 'fee_amount' => 5,
324 'net_amount' => 295,
325 'trxn_id' => '22ereerwww323',
326 'invoice_id' => '22ed39c9e9ee621ce0eafe6da70',
327 'thankyou_date' => '20080522',
328 );
329
330 $contribution = CRM_Contribute_BAO_Contribution::create($param, $ids);
331
332 $this->assertEquals($param['trxn_id'], $contribution->trxn_id, 'Check for transcation id creation.');
333 $this->assertEquals($contactId, $contribution->contact_id, 'Check for contact id creation.');
334
335 //display sort name during Update multiple contributions
336 $sortName = CRM_Contribute_BAO_Contribution::sortName($contribution->id);
337
338 $this->assertEquals('Whatson, Shane', $sortName, 'Check for sort name.');
339
340 //Delete Contribution
341 $this->contributionDelete($contribution->id);
342 //Delete Contact
343 Contact::delete($contactId);
344 }
345
346 /**
347 * Add premium during online Contribution.
348 *
349 * AddPremium();
350 */
351 public function testAddPremium() {
352 $contactId = Contact::createIndividual();
353
354 $ids = array(
355 'premium' => NULL,
356 );
357
358 $params = array(
359 'name' => 'TEST Premium',
360 'sku' => 111,
361 'imageOption' => 'noImage',
362 'MAX_FILE_SIZE' => 2097152,
363 'price' => 100.00,
364 'cost' => 90.00,
365 'min_contribution' => 100,
366 'is_active' => 1,
367 );
368 $premium = CRM_Contribute_BAO_ManagePremiums::add($params, $ids);
369
370 $this->assertEquals('TEST Premium', $premium->name, 'Check for premium name.');
371
372 $ids = array('contribution' => NULL);
373
374 $param = array(
375 'contact_id' => $contactId,
376 'currency' => 'USD',
377 'financial_type_id' => 1,
378 'contribution_status_id' => 1,
379 'payment_instrument_id' => 1,
380 'source' => 'STUDENT',
381 'receive_date' => '20080522000000',
382 'receipt_date' => '20080522000000',
383 'id' => NULL,
384 'non_deductible_amount' => 0.00,
385 'total_amount' => 300.00,
386 'fee_amount' => 5,
387 'net_amount' => 295,
388 'trxn_id' => '33erdfrwvw434',
389 'invoice_id' => '98ed34f7u9hh672ce0eafe8fb92',
390 'thankyou_date' => '20080522',
391 );
392
393 $contribution = CRM_Contribute_BAO_Contribution::create($param, $ids);
394
395 $this->assertEquals($param['trxn_id'], $contribution->trxn_id, 'Check for transcation id creation.');
396 $this->assertEquals($contactId, $contribution->contact_id, 'Check for contact id creation.');
397
398 //parameter for adding premium to contribution
399 $data = array(
400 'product_id' => $premium->id,
401 'contribution_id' => $contribution->id,
402 'product_option' => NULL,
403 'quantity' => 1,
404 );
405 $contributionProduct = CRM_Contribute_BAO_Contribution::addPremium($data);
406 $this->assertEquals($contributionProduct->product_id, $premium->id, 'Check for Product id .');
407
408 //Delete Product
409 CRM_Contribute_BAO_ManagePremiums::del($premium->id);
410 $this->assertDBNull('CRM_Contribute_DAO_Product', $premium->name,
411 'id', 'name', 'Database check for deleted Product.'
412 );
413
414 //Delete Contribution
415 $this->contributionDelete($contribution->id);
416 //Delete Contact
417 Contact::delete($contactId);
418 }
419
420 /**
421 * Check duplicate contribution id.
422 * during the contribution import
423 * checkDuplicateIds();
424 */
425 public function testcheckDuplicateIds() {
426 $contactId = Contact::createIndividual();
427
428 $ids = array('contribution' => NULL);
429
430 $param = array(
431 'contact_id' => $contactId,
432 'currency' => 'USD',
433 'financial_type_id' => 1,
434 'contribution_status_id' => 1,
435 'payment_instrument_id' => 1,
436 'source' => 'STUDENT',
437 'receive_date' => '20080522000000',
438 'receipt_date' => '20080522000000',
439 'id' => NULL,
440 'non_deductible_amount' => 0.00,
441 'total_amount' => 300.00,
442 'fee_amount' => 5,
443 'net_amount' => 295,
444 'trxn_id' => '76ereeswww835',
445 'invoice_id' => '93ed39a9e9hd621bs0eafe3da82',
446 'thankyou_date' => '20080522',
447 );
448
449 $contribution = CRM_Contribute_BAO_Contribution::create($param, $ids);
450
451 $this->assertEquals($param['trxn_id'], $contribution->trxn_id, 'Check for transcation id creation.');
452 $this->assertEquals($contactId, $contribution->contact_id, 'Check for contact id creation.');
453 $data = array(
454 'id' => $contribution->id,
455 'trxn_id' => $contribution->trxn_id,
456 'invoice_id' => $contribution->invoice_id,
457 );
458 $contributionID = CRM_Contribute_BAO_Contribution::checkDuplicateIds($data);
459 $this->assertEquals($contributionID, $contribution->id, 'Check for duplicate transcation id .');
460
461 // Delete Contribution
462 $this->contributionDelete($contribution->id);
463 // Delete Contact
464 Contact::delete($contactId);
465 }
466
467 /**
468 * Check credit note id creation
469 * when a contribution is cancelled or refunded
470 * createCreditNoteId();
471 */
472 public function testCreateCreditNoteId() {
473 $contactId = Contact::createIndividual();
474
475 $ids = array('contribution' => NULL);
476
477 $param = array(
478 'contact_id' => $contactId,
479 'currency' => 'USD',
480 'financial_type_id' => 1,
481 'contribution_status_id' => 3,
482 'payment_instrument_id' => 1,
483 'source' => 'STUDENT',
484 'receive_date' => '20080522000000',
485 'receipt_date' => '20080522000000',
486 'id' => NULL,
487 'non_deductible_amount' => 0.00,
488 'total_amount' => 300.00,
489 'fee_amount' => 5,
490 'net_amount' => 295,
491 'trxn_id' => '76ereeswww835',
492 'invoice_id' => '93ed39a9e9hd621bs0eafe3da82',
493 'thankyou_date' => '20080522',
494 );
495
496 $creditNoteId = CRM_Contribute_BAO_Contribution::createCreditNoteId();
497 $contribution = CRM_Contribute_BAO_Contribution::create($param, $ids);
498 $this->assertEquals($contactId, $contribution->contact_id, 'Check for contact id creation.');
499 $this->assertEquals($creditNoteId, $contribution->creditnote_id, 'Check if credit note id is created correctly.');
500
501 // Delete Contribution
502 $this->contributionDelete($contribution->id);
503 // Delete Contact
504 Contact::delete($contactId);
505 }
506
507 /**
508 * Create() method (create and update modes).
509 */
510 public function testIsPaymentFlag() {
511 $contactId = Contact::createIndividual();
512 $ids = array('contribution' => NULL);
513
514 $params = array(
515 'contact_id' => $contactId,
516 'currency' => 'USD',
517 'financial_type_id' => 1,
518 'contribution_status_id' => 1,
519 'payment_instrument_id' => 1,
520 'source' => 'STUDENT',
521 'receive_date' => '20080522000000',
522 'receipt_date' => '20080522000000',
523 'non_deductible_amount' => 0.00,
524 'total_amount' => 200.00,
525 'fee_amount' => 5,
526 'net_amount' => 195,
527 'trxn_id' => '22ereerwww4444xx',
528 'invoice_id' => '86ed39c9e9ee6ef6541621ce0eafe7eb81',
529 'thankyou_date' => '20080522',
530 );
531
532 $contribution = CRM_Contribute_BAO_Contribution::create($params, $ids);
533
534 $this->assertEquals($params['trxn_id'], $contribution->trxn_id, 'Check for transcation id creation.');
535 $this->assertEquals($contactId, $contribution->contact_id, 'Check for contact id creation.');
536
537 $trxnArray = array(
538 'trxn_id' => $params['trxn_id'],
539 'is_payment' => 1,
540 );
541 $defaults = array();
542 $financialTrxn = CRM_Core_BAO_FinancialTrxn::retrieve($trxnArray, $defaults);
543 $this->assertEquals(2, $financialTrxn->N, 'Mismatch count for is payment flag.');
544 //update contribution amount
545 $ids = array('contribution' => $contribution->id);
546 $params['total_amount'] = 150;
547
548 $contribution = CRM_Contribute_BAO_Contribution::create($params, $ids);
549
550 $this->assertEquals($params['trxn_id'], $contribution->trxn_id, 'Check for transcation id .');
551 $this->assertEquals($params['total_amount'], $contribution->total_amount, 'Check for Amount updation.');
552 $trxnArray = array(
553 'trxn_id' => $params['trxn_id'],
554 'is_payment' => 1,
555 );
556 $defaults = array();
557 $financialTrxn = CRM_Core_BAO_FinancialTrxn::retrieve($trxnArray, $defaults);
558 $this->assertEquals(3, $financialTrxn->N, 'Mismatch count for is payment flag.');
559 $trxnArray['is_payment'] = 0;
560 $financialTrxn = CRM_Core_BAO_FinancialTrxn::retrieve($trxnArray, $defaults);
561 $this->assertEquals(NULL, $financialTrxn, 'Mismatch count for is payment flag.');
562
563 //Delete Contribution
564 $this->contributionDelete($contribution->id);
565
566 //Delete Contact
567 Contact::delete($contactId);
568 }
569
570 /**
571 * Create() method (create and update modes).
572 */
573 public function testIsPaymentFlagForPending() {
574 $contactId = Contact::createIndividual();
575 $ids = array('contribution' => NULL);
576
577 $params = array(
578 'contact_id' => $contactId,
579 'currency' => 'USD',
580 'financial_type_id' => 1,
581 'contribution_status_id' => 2,
582 'payment_instrument_id' => 1,
583 'source' => 'STUDENT',
584 'is_pay_later' => 1,
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' => '22ereerwww4444yy',
592 'invoice_id' => '86ed39c9e9yy6ef6541621ce0eafe7eb81',
593 'thankyou_date' => '20080522',
594 );
595
596 $contribution = CRM_Contribute_BAO_Contribution::create($params, $ids);
597
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.');
600
601 $trxnArray = array(
602 'trxn_id' => $params['trxn_id'],
603 'is_payment' => 0,
604 );
605 $defaults = array();
606 $financialTrxn = CRM_Core_BAO_FinancialTrxn::retrieve($trxnArray, $defaults);
607 $this->assertEquals(2, $financialTrxn->N, 'Mismatch count for is payment flag.');
608 $trxnArray['is_payment'] = 1;
609 $financialTrxn = CRM_Core_BAO_FinancialTrxn::retrieve($trxnArray, $defaults);
610 $this->assertEquals(NULL, $financialTrxn, 'Mismatch count for is payment flag.');
611 //update contribution amount
612 $ids = array('contribution' => $contribution->id);
613 $params['contribution_status_id'] = 1;
614
615 $contribution = CRM_Contribute_BAO_Contribution::create($params, $ids);
616
617 $this->assertEquals($params['trxn_id'], $contribution->trxn_id, 'Check for transcation id .');
618 $this->assertEquals($params['contribution_status_id'], $contribution->contribution_status_id, 'Check for status updation.');
619 $trxnArray = array(
620 'trxn_id' => $params['trxn_id'],
621 'is_payment' => 1,
622 );
623 $defaults = array();
624 $financialTrxn = CRM_Core_BAO_FinancialTrxn::retrieve($trxnArray, $defaults);
625 $this->assertEquals(1, $financialTrxn->N, 'Mismatch count for is payment flag.');
626 $trxnArray['is_payment'] = 0;
627 $financialTrxn = CRM_Core_BAO_FinancialTrxn::retrieve($trxnArray, $defaults);
628 $this->assertEquals(2, $financialTrxn->N, 'Mismatch count for is payment flag.');
629
630 //Delete Contribution
631 $this->contributionDelete($contribution->id);
632
633 //Delete Contact
634 Contact::delete($contactId);
635 }
636
637 /**
638 * addPayments() method (add and edit modes of participant)
639 */
640 public function testAddPayments() {
641 list($lineItems, $contribution) = $this->addParticipantWithContribution();
642 foreach ($lineItems as $value) {
643 CRM_Contribute_BAO_Contribution::addPayments($value, array($contribution));
644 }
645 $this->checkItemValues($contribution);
646 }
647
648 /**
649 * checks db values for financial item
650 */
651 public function checkItemValues($contribution) {
652 $relationTypeId = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Accounts Receivable Account is' "));
653 $toFinancialAccount = CRM_Contribute_PseudoConstant::financialAccountType(4, $relationTypeId);
654 $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')
655 INNER JOIN civicrm_entity_financial_trxn eft1 ON (eft1.financial_trxn_id = eft.financial_trxn_id AND eft1.entity_table = 'civicrm_financial_item')
656 WHERE eft.entity_id = %1 AND ft.to_financial_account_id <> %2";
657
658 $queryParams[1] = array($contribution->id, 'Integer');
659 $queryParams[2] = array($toFinancialAccount, 'Integer');
660
661 $dao = CRM_Core_DAO::executeQuery($query, $queryParams);
662 $amounts = array(100.00, 50.00);
663 while ($dao->fetch()) {
664 $this->assertEquals(150.00, $dao->total_amount, 'Mismatch of total amount paid.');
665 $this->assertEquals($dao->amount, array_pop($amounts), 'Mismatch of amount proportionally assigned to financial item');
666 }
667
668 Contact::delete($this->_contactId);
669 Event::delete($this->_eventId);
670 }
671
672 /**
673 * assignProportionalLineItems() method (add and edit modes of participant)
674 */
675 public function testAssignProportionalLineItems() {
676 list($lineItems, $contribution) = $this->addParticipantWithContribution();
677 $contributions['total_amount'] = $contribution->total_amount;
678 $params = array(
679 'contribution_id' => $contribution->id,
680 'total_amount' => 150.00,
681 );
682 $trxn = new CRM_Financial_DAO_FinancialTrxn();
683 $trxn->orderBy('id DESC');
684 $trxn->find(TRUE);
685 CRM_Contribute_BAO_Contribution::assignProportionalLineItems($params, $trxn, $contributions);
686 $this->checkItemValues($contribution);
687 }
688
689 /**
690 * Add participant with contribution
691 *
692 * @return array
693 */
694 protected function addParticipantWithContribution() {
695 // creating price set, price field
696 require_once 'CiviTest/Event.php';
697 $this->_contactId = Contact::createIndividual();
698 $this->_eventId = Event::create($this->_contactId);
699 $paramsSet['title'] = 'Price Set' . substr(sha1(rand()), 0, 4);
700 $paramsSet['name'] = CRM_Utils_String::titleToVar($paramsSet['title']);
701 $paramsSet['is_active'] = TRUE;
702 $paramsSet['financial_type_id'] = 4;
703 $paramsSet['extends'] = 1;
704
705 $priceset = CRM_Price_BAO_PriceSet::create($paramsSet);
706 $priceSetId = $priceset->id;
707
708 //Checking for priceset added in the table.
709 $this->assertDBCompareValue('CRM_Price_BAO_PriceSet', $priceSetId, 'title',
710 'id', $paramsSet['title'], 'Check DB for created priceset'
711 );
712 $paramsField = array(
713 'label' => 'Price Field',
714 'name' => CRM_Utils_String::titleToVar('Price Field'),
715 'html_type' => 'CheckBox',
716 'option_label' => array('1' => 'Price Field 1', '2' => 'Price Field 2'),
717 'option_value' => array('1' => 100, '2' => 200),
718 'option_name' => array('1' => 'Price Field 1', '2' => 'Price Field 2'),
719 'option_weight' => array('1' => 1, '2' => 2),
720 'option_amount' => array('1' => 100, '2' => 200),
721 'is_display_amounts' => 1,
722 'weight' => 1,
723 'options_per_line' => 1,
724 'is_active' => array('1' => 1, '2' => 1),
725 'price_set_id' => $priceset->id,
726 'is_enter_qty' => 1,
727 'financial_type_id' => CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_FinancialType', 'Event Fee', 'id', 'name'),
728 );
729 $priceField = CRM_Price_BAO_PriceField::create($paramsField);
730 $eventParams = array(
731 'id' => $this->_eventId,
732 'financial_type_id' => 4,
733 'is_monetary' => 1,
734 );
735 CRM_Event_BAO_Event::create($eventParams);
736 CRM_Price_BAO_PriceSet::addTo('civicrm_event', $this->_eventId, $priceSetId);
737
738 $priceFields = $this->callAPISuccess('PriceFieldValue', 'get', array('price_field_id' => $priceField->id));
739 $participantParams = array(
740 'financial_type_id' => 4,
741 'event_id' => $this->_eventId,
742 'role_id' => 1,
743 'status_id' => 14,
744 'fee_currency' => 'USD',
745 'contact_id' => $this->_contactId,
746 );
747 $participant = CRM_Event_BAO_Participant::add($participantParams);
748 $contributionParams = array(
749 'total_amount' => 150,
750 'currency' => 'USD',
751 'contact_id' => $this->_contactId,
752 'financial_type_id' => 4,
753 'contribution_status_id' => 1,
754 'partial_payment_total' => 300.00,
755 'partial_amount_pay' => 150,
756 'contribution_mode' => 'participant',
757 'participant_id' => $participant->id,
758 );
759
760 foreach ($priceFields['values'] as $key => $priceField) {
761 $lineItems[1][$key] = array(
762 'price_field_id' => $priceField['price_field_id'],
763 'price_field_value_id' => $priceField['id'],
764 'label' => $priceField['label'],
765 'field_title' => $priceField['label'],
766 'qty' => 1,
767 'unit_price' => $priceField['amount'],
768 'line_total' => $priceField['amount'],
769 'financial_type_id' => $priceField['financial_type_id'],
770 );
771 }
772 $contributionParams['line_item'] = $lineItems;
773 $contributions = CRM_Contribute_BAO_Contribution::create($contributionParams);
774
775 $paymentParticipant = array(
776 'participant_id' => $participant->id,
777 'contribution_id' => $contributions->id,
778 );
779 $ids = array();
780 CRM_Event_BAO_ParticipantPayment::create($paymentParticipant, $ids);
781
782 return array($lineItems, $contributions);
783 }
784
785 /**
786 * checkLineItems() check if total amount matches the sum of line total
787 */
788 public function testcheckLineItems() {
789 $params = array(
790 'contact_id' => 202,
791 'receive_date' => '2010-01-20',
792 'total_amount' => 100,
793 'financial_type_id' => 3,
794 'line_items' => array(
795 array(
796 'line_item' => array(
797 array(
798 'entity_table' => 'civicrm_contribution',
799 'price_field_id' => 8,
800 'price_field_value_id' => 16,
801 'label' => 'test 1',
802 'qty' => 1,
803 'unit_price' => 100,
804 'line_total' => 100,
805 ),
806 array(
807 'entity_table' => 'civicrm_contribution',
808 'price_field_id' => 8,
809 'price_field_value_id' => 17,
810 'label' => 'Test 2',
811 'qty' => 1,
812 'unit_price' => 200,
813 'line_total' => 200,
814 'financial_type_id' => 1,
815 ),
816 ),
817 'params' => array(),
818 ),
819 ),
820 );
821 try {
822 $error = CRM_Contribute_BAO_Contribution::checkLineItems($params);
823 $this->fail("Missed expected exception");
824 }
825 catch (Exception $e) {
826 $this->assertEquals("Line item total doesn't match with total amount.", $e->getMessage());
827 }
828 $this->assertEquals(3, $params['line_items'][0]['line_item'][0]['financial_type_id']);
829 $params['total_amount'] = 300;
830 CRM_Contribute_BAO_Contribution::checkLineItems($params);
831 }
832
833 }