3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
28 require_once 'CiviTest/Contact.php';
29 require_once 'CiviTest/Custom.php';
32 * Class CRM_Contribute_BAO_ContributionTest
34 class CRM_Contribute_BAO_ContributionTest
extends CiviUnitTestCase
{
37 * Create() method (create and update modes).
39 public function testCreate() {
40 $contactId = Contact
::createIndividual();
41 $ids = array('contribution' => NULL);
44 'contact_id' => $contactId,
46 'financial_type_id' => 1,
47 'contribution_status_id' => 1,
48 'payment_instrument_id' => 1,
49 'source' => 'STUDENT',
50 'receive_date' => '20080522000000',
51 'receipt_date' => '20080522000000',
52 'non_deductible_amount' => 0.00,
53 'total_amount' => 200.00,
56 'trxn_id' => '22ereerwww444444',
57 'invoice_id' => '86ed39c9e9ee6ef6031621ce0eafe7eb81',
58 'thankyou_date' => '20080522',
61 $contribution = CRM_Contribute_BAO_Contribution
::create($params, $ids);
63 $this->assertEquals($params['trxn_id'], $contribution->trxn_id
, 'Check for transcation id creation.');
64 $this->assertEquals($contactId, $contribution->contact_id
, 'Check for contact id creation.');
66 //update contribution amount
67 $ids = array('contribution' => $contribution->id
);
68 $params['fee_amount'] = 10;
69 $params['net_amount'] = 190;
71 $contribution = CRM_Contribute_BAO_Contribution
::create($params, $ids);
73 $this->assertEquals($params['trxn_id'], $contribution->trxn_id
, 'Check for transcation id .');
74 $this->assertEquals($params['net_amount'], $contribution->net_amount
, 'Check for Amount updation.');
77 $this->contributionDelete($contribution->id
);
80 Contact
::delete($contactId);
84 * Create() method with custom data.
86 public function testCreateWithCustomData() {
87 $contactId = Contact
::createIndividual();
88 $ids = array('contribution' => NULL);
91 $customGroup = Custom
::createGroup(array(), 'Contribution');
94 'data_type' => 'String',
95 'html_type' => 'Text',
97 'custom_group_id' => $customGroup->id
,
99 $customField = CRM_Core_BAO_CustomField
::create($fields);
102 'contact_id' => $contactId,
104 'financial_type_id' => 1,
105 'contribution_status_id' => 1,
106 'payment_instrument_id' => 1,
107 'source' => 'STUDENT',
108 'receive_date' => '20080522000000',
109 'receipt_date' => '20080522000000',
111 'non_deductible_amount' => 0.00,
112 'total_amount' => 200.00,
115 'trxn_id' => '22ereerwww322323',
116 'invoice_id' => '22ed39c9e9ee6ef6031621ce0eafe6da70',
117 'thankyou_date' => '20080522',
120 $params['custom'] = array(
121 $customField->id
=> array(
123 'value' => 'Test custom value',
125 'custom_field_id' => $customField->id
,
126 'custom_group_id' => $customGroup->id
,
127 'table_name' => $customGroup->table_name
,
128 'column_name' => $customField->column_name
,
134 $contribution = CRM_Contribute_BAO_Contribution
::create($params, $ids);
136 // Check that the custom field value is saved
137 $customValueParams = array(
138 'entityID' => $contribution->id
,
139 'custom_' . $customField->id
=> 1,
141 $values = CRM_Core_BAO_CustomValueTable
::getValues($customValueParams);
142 $this->assertEquals('Test custom value', $values['custom_' . $customField->id
], 'Check the custom field value');
144 $this->assertEquals($params['trxn_id'], $contribution->trxn_id
, 'Check for transcation id creation.');
145 $this->assertEquals($contactId, $contribution->contact_id
, 'Check for contact id for Conribution.');
147 $this->contributionDelete($contribution->id
);
148 Custom
::deleteField($customField);
149 Custom
::deleteGroup($customGroup);
150 Contact
::delete($contactId);
154 * DeleteContribution() method
156 public function testDeleteContribution() {
157 $contactId = Contact
::createIndividual();
158 $ids = array('contribution' => NULL);
161 'contact_id' => $contactId,
163 'financial_type_id' => 1,
164 'contribution_status_id' => 1,
165 'payment_instrument_id' => 1,
166 'source' => 'STUDENT',
167 'receive_date' => '20080522000000',
168 'receipt_date' => '20080522000000',
170 'non_deductible_amount' => 0.00,
171 'total_amount' => 200.00,
174 'trxn_id' => '33ereerwww322323',
175 'invoice_id' => '33ed39c9e9ee6ef6031621ce0eafe6da70',
176 'thankyou_date' => '20080522',
179 $contribution = CRM_Contribute_BAO_Contribution
::create($params, $ids);
181 $this->assertEquals($params['trxn_id'], $contribution->trxn_id
, 'Check for transcation id creation.');
182 $this->assertEquals($contactId, $contribution->contact_id
, 'Check for contact id creation.');
184 $contributiondelete = CRM_Contribute_BAO_Contribution
::deleteContribution($contribution->id
);
186 $this->assertDBNull('CRM_Contribute_DAO_Contribution', $contribution->trxn_id
,
187 'id', 'trxn_id', 'Database check for deleted Contribution.'
189 Contact
::delete($contactId);
193 * Create honor-contact method
195 public function testcreateAndGetHonorContact() {
196 $firstName = 'John_' . substr(sha1(rand()), 0, 7);
197 $lastName = 'Smith_' . substr(sha1(rand()), 0, 7);
198 $email = "{$firstName}.{$lastName}@example.com";
200 //Get profile id of name honoree_individual used to create profileContact
201 $honoreeProfileId = NULL;
202 $ufGroupDAO = new CRM_Core_DAO_UFGroup();
203 $ufGroupDAO->name
= 'honoree_individual';
204 if ($ufGroupDAO->find(TRUE)) {
205 $honoreeProfileId = $ufGroupDAO->id
;
210 'first_name' => $firstName,
211 'last_name' => $lastName,
214 $softParam = array('soft_credit_type_id' => 1);
216 $honoreeContactId = CRM_Contact_BAO_Contact
::createProfileContact($params, CRM_Core_DAO
::$_nullArray,
217 NULL, NULL, $honoreeProfileId
220 $this->assertDBCompareValue('CRM_Contact_DAO_Contact', $honoreeContactId, 'first_name', 'id', $firstName,
221 'Database check for created honor contact record.'
223 //create contribution on behalf of honary.
225 $contactId = Contact
::createIndividual();
226 $softParam['contact_id'] = $honoreeContactId;
228 $ids = array('contribution' => NULL);
230 'contact_id' => $contactId,
232 'financial_type_id' => 4,
233 'contribution_status_id' => 1,
234 'receive_date' => date('Ymd'),
235 'total_amount' => 66,
238 $contribution = CRM_Contribute_BAO_Contribution
::create($param, $ids);
239 $id = $contribution->id
;
240 $softParam['contribution_id'] = $id;
241 $softParam['currency'] = $contribution->currency
;
242 $softParam['amount'] = $contribution->total_amount
;
244 //Create Soft Contribution for honoree contact
245 CRM_Contribute_BAO_ContributionSoft
::add($softParam);
247 $this->assertDBCompareValue('CRM_Contribute_DAO_ContributionSoft', $id, 'contact_id',
248 'contribution_id', $honoreeContactId, 'Check DB for honor contact of the contribution'
250 //get honorary information
251 $getHonorContact = CRM_Contribute_BAO_Contribution
::getHonorContacts($honoreeContactId);
252 $this->assertEquals(array(
254 'honor_type' => 'In Honor of',
256 'display_name' => 'John Doe',
257 'type' => 'Event Fee',
259 'amount' => '$ 66.00',
261 'receive_date' => date('Y-m-d 00:00:00'),
262 'contribution_status' => 'Completed',
264 ), $getHonorContact);
266 $this->assertDBCompareValue('CRM_Contact_DAO_Contact', $honoreeContactId, 'first_name', 'id', $firstName,
267 'Database check for created honor contact record.'
270 //get annual contribution information
271 $annual = CRM_Contribute_BAO_Contribution
::annual($contactId);
273 $config = CRM_Core_Config
::singleton();
274 $currencySymbol = CRM_Core_DAO
::getFieldValue('CRM_Financial_DAO_Currency', $config->defaultCurrency
, 'symbol', 'name');
275 $this->assertDBCompareValue('CRM_Contribute_DAO_Contribution', $id, 'total_amount',
276 'id', ltrim($annual[2], $currencySymbol), 'Check DB for total amount of the contribution'
279 //Delete honor contact
280 Contact
::delete($honoreeContactId);
282 //Delete Contribution record
283 $this->contributionDelete($contribution->id
);
285 //Delete contributor contact
286 Contact
::delete($contactId);
290 * Display sort name during.
291 * Update multiple contributions
294 public function testsortName() {
296 'first_name' => 'Shane',
297 'last_name' => 'Whatson',
298 'contact_type' => 'Individual',
301 $contact = CRM_Contact_BAO_Contact
::add($params);
303 //Now check $contact is object of contact DAO..
304 $this->assertInstanceOf('CRM_Contact_DAO_Contact', $contact, 'Check for created object');
306 $contactId = $contact->id
;
308 $ids = array('contribution' => NULL);
311 'contact_id' => $contactId,
313 'financial_type_id' => 1,
314 'contribution_status_id' => 1,
315 'payment_instrument_id' => 1,
316 'source' => 'STUDENT',
317 'receive_date' => '20080522000000',
318 'receipt_date' => '20080522000000',
320 'non_deductible_amount' => 0.00,
321 'total_amount' => 300.00,
324 'trxn_id' => '22ereerwww323',
325 'invoice_id' => '22ed39c9e9ee621ce0eafe6da70',
326 'thankyou_date' => '20080522',
329 $contribution = CRM_Contribute_BAO_Contribution
::create($param, $ids);
331 $this->assertEquals($param['trxn_id'], $contribution->trxn_id
, 'Check for transcation id creation.');
332 $this->assertEquals($contactId, $contribution->contact_id
, 'Check for contact id creation.');
334 //display sort name during Update multiple contributions
335 $sortName = CRM_Contribute_BAO_Contribution
::sortName($contribution->id
);
337 $this->assertEquals('Whatson, Shane', $sortName, 'Check for sort name.');
339 //Delete Contribution
340 $this->contributionDelete($contribution->id
);
342 Contact
::delete($contactId);
346 * Add premium during online Contribution.
350 public function testAddPremium() {
351 $contactId = Contact
::createIndividual();
358 'name' => 'TEST Premium',
360 'imageOption' => 'noImage',
361 'MAX_FILE_SIZE' => 2097152,
364 'min_contribution' => 100,
367 $premium = CRM_Contribute_BAO_ManagePremiums
::add($params, $ids);
369 $this->assertEquals('TEST Premium', $premium->name
, 'Check for premium name.');
371 $ids = array('contribution' => NULL);
374 'contact_id' => $contactId,
376 'financial_type_id' => 1,
377 'contribution_status_id' => 1,
378 'payment_instrument_id' => 1,
379 'source' => 'STUDENT',
380 'receive_date' => '20080522000000',
381 'receipt_date' => '20080522000000',
383 'non_deductible_amount' => 0.00,
384 'total_amount' => 300.00,
387 'trxn_id' => '33erdfrwvw434',
388 'invoice_id' => '98ed34f7u9hh672ce0eafe8fb92',
389 'thankyou_date' => '20080522',
392 $contribution = CRM_Contribute_BAO_Contribution
::create($param, $ids);
394 $this->assertEquals($param['trxn_id'], $contribution->trxn_id
, 'Check for transcation id creation.');
395 $this->assertEquals($contactId, $contribution->contact_id
, 'Check for contact id creation.');
397 //parameter for adding premium to contribution
399 'product_id' => $premium->id
,
400 'contribution_id' => $contribution->id
,
401 'product_option' => NULL,
404 $contributionProduct = CRM_Contribute_BAO_Contribution
::addPremium($data);
405 $this->assertEquals($contributionProduct->product_id
, $premium->id
, 'Check for Product id .');
408 CRM_Contribute_BAO_ManagePremiums
::del($premium->id
);
409 $this->assertDBNull('CRM_Contribute_DAO_Product', $premium->name
,
410 'id', 'name', 'Database check for deleted Product.'
413 //Delete Contribution
414 $this->contributionDelete($contribution->id
);
416 Contact
::delete($contactId);
420 * Check duplicate contribution id.
421 * during the contribution import
422 * checkDuplicateIds();
424 public function testcheckDuplicateIds() {
425 $contactId = Contact
::createIndividual();
427 $ids = array('contribution' => NULL);
430 'contact_id' => $contactId,
432 'financial_type_id' => 1,
433 'contribution_status_id' => 1,
434 'payment_instrument_id' => 1,
435 'source' => 'STUDENT',
436 'receive_date' => '20080522000000',
437 'receipt_date' => '20080522000000',
439 'non_deductible_amount' => 0.00,
440 'total_amount' => 300.00,
443 'trxn_id' => '76ereeswww835',
444 'invoice_id' => '93ed39a9e9hd621bs0eafe3da82',
445 'thankyou_date' => '20080522',
448 $contribution = CRM_Contribute_BAO_Contribution
::create($param, $ids);
450 $this->assertEquals($param['trxn_id'], $contribution->trxn_id
, 'Check for transcation id creation.');
451 $this->assertEquals($contactId, $contribution->contact_id
, 'Check for contact id creation.');
453 'id' => $contribution->id
,
454 'trxn_id' => $contribution->trxn_id
,
455 'invoice_id' => $contribution->invoice_id
,
457 $contributionID = CRM_Contribute_BAO_Contribution
::checkDuplicateIds($data);
458 $this->assertEquals($contributionID, $contribution->id
, 'Check for duplicate transcation id .');
460 // Delete Contribution
461 $this->contributionDelete($contribution->id
);
463 Contact
::delete($contactId);
467 * Check credit note id creation
468 * when a contribution is cancelled or refunded
469 * createCreditNoteId();
471 public function testCreateCreditNoteId() {
472 $contactId = Contact
::createIndividual();
474 $ids = array('contribution' => NULL);
477 'contact_id' => $contactId,
479 'financial_type_id' => 1,
480 'contribution_status_id' => 3,
481 'payment_instrument_id' => 1,
482 'source' => 'STUDENT',
483 'receive_date' => '20080522000000',
484 'receipt_date' => '20080522000000',
486 'non_deductible_amount' => 0.00,
487 'total_amount' => 300.00,
490 'trxn_id' => '76ereeswww835',
491 'invoice_id' => '93ed39a9e9hd621bs0eafe3da82',
492 'thankyou_date' => '20080522',
495 $creditNoteId = CRM_Contribute_BAO_Contribution
::createCreditNoteId();
496 $contribution = CRM_Contribute_BAO_Contribution
::create($param, $ids);
497 $this->assertEquals($contactId, $contribution->contact_id
, 'Check for contact id creation.');
498 $this->assertEquals($creditNoteId, $contribution->creditnote_id
, 'Check if credit note id is created correctly.');
500 // Delete Contribution
501 $this->contributionDelete($contribution->id
);
503 Contact
::delete($contactId);
507 * Create() method (create and update modes).
509 public function testIsPaymentFlag() {
510 $contactId = Contact
::createIndividual();
511 $ids = array('contribution' => NULL);
514 'contact_id' => $contactId,
516 'financial_type_id' => 1,
517 'contribution_status_id' => 1,
518 'payment_instrument_id' => 1,
519 'source' => 'STUDENT',
520 'receive_date' => '20080522000000',
521 'receipt_date' => '20080522000000',
522 'non_deductible_amount' => 0.00,
523 'total_amount' => 200.00,
526 'trxn_id' => '22ereerwww4444xx',
527 'invoice_id' => '86ed39c9e9ee6ef6541621ce0eafe7eb81',
528 'thankyou_date' => '20080522',
531 $contribution = CRM_Contribute_BAO_Contribution
::create($params, $ids);
533 $this->assertEquals($params['trxn_id'], $contribution->trxn_id
, 'Check for transcation id creation.');
534 $this->assertEquals($contactId, $contribution->contact_id
, 'Check for contact id creation.');
537 'trxn_id' => $params['trxn_id'],
541 $financialTrxn = CRM_Core_BAO_FinancialTrxn
::retrieve($trxnArray, $defaults);
542 $this->assertEquals(2, $financialTrxn->N
, 'Mismatch count for is payment flag.');
543 //update contribution amount
544 $ids = array('contribution' => $contribution->id
);
545 $params['total_amount'] = 150;
547 $contribution = CRM_Contribute_BAO_Contribution
::create($params, $ids);
549 $this->assertEquals($params['trxn_id'], $contribution->trxn_id
, 'Check for transcation id .');
550 $this->assertEquals($params['total_amount'], $contribution->total_amount
, 'Check for Amount updation.');
552 'trxn_id' => $params['trxn_id'],
556 $financialTrxn = CRM_Core_BAO_FinancialTrxn
::retrieve($trxnArray, $defaults);
557 $this->assertEquals(3, $financialTrxn->N
, 'Mismatch count for is payment flag.');
558 $trxnArray['is_payment'] = 0;
559 $financialTrxn = CRM_Core_BAO_FinancialTrxn
::retrieve($trxnArray, $defaults);
560 $this->assertEquals(NULL, $financialTrxn, 'Mismatch count for is payment flag.');
562 //Delete Contribution
563 $this->contributionDelete($contribution->id
);
566 Contact
::delete($contactId);
570 * Create() method (create and update modes).
572 public function testIsPaymentFlagForPending() {
573 $contactId = Contact
::createIndividual();
574 $ids = array('contribution' => NULL);
577 'contact_id' => $contactId,
579 'financial_type_id' => 1,
580 'contribution_status_id' => 2,
581 'payment_instrument_id' => 1,
582 'source' => 'STUDENT',
584 'receive_date' => '20080522000000',
585 'receipt_date' => '20080522000000',
586 'non_deductible_amount' => 0.00,
587 'total_amount' => 200.00,
590 'trxn_id' => '22ereerwww4444yy',
591 'invoice_id' => '86ed39c9e9yy6ef6541621ce0eafe7eb81',
592 'thankyou_date' => '20080522',
595 $contribution = CRM_Contribute_BAO_Contribution
::create($params, $ids);
597 $this->assertEquals($params['trxn_id'], $contribution->trxn_id
, 'Check for transcation id creation.');
598 $this->assertEquals($contactId, $contribution->contact_id
, 'Check for contact id creation.');
601 'trxn_id' => $params['trxn_id'],
605 $financialTrxn = CRM_Core_BAO_FinancialTrxn
::retrieve($trxnArray, $defaults);
606 $this->assertEquals(2, $financialTrxn->N
, 'Mismatch count for is payment flag.');
607 $trxnArray['is_payment'] = 1;
608 $financialTrxn = CRM_Core_BAO_FinancialTrxn
::retrieve($trxnArray, $defaults);
609 $this->assertEquals(NULL, $financialTrxn, 'Mismatch count for is payment flag.');
610 //update contribution amount
611 $ids = array('contribution' => $contribution->id
);
612 $params['contribution_status_id'] = 1;
614 $contribution = CRM_Contribute_BAO_Contribution
::create($params, $ids);
616 $this->assertEquals($params['trxn_id'], $contribution->trxn_id
, 'Check for transcation id .');
617 $this->assertEquals($params['contribution_status_id'], $contribution->contribution_status_id
, 'Check for status updation.');
619 'trxn_id' => $params['trxn_id'],
623 $financialTrxn = CRM_Core_BAO_FinancialTrxn
::retrieve($trxnArray, $defaults);
624 $this->assertEquals(1, $financialTrxn->N
, 'Mismatch count for is payment flag.');
625 $trxnArray['is_payment'] = 0;
626 $financialTrxn = CRM_Core_BAO_FinancialTrxn
::retrieve($trxnArray, $defaults);
627 $this->assertEquals(2, $financialTrxn->N
, 'Mismatch count for is payment flag.');
629 //Delete Contribution
630 $this->contributionDelete($contribution->id
);
633 Contact
::delete($contactId);
637 * addPayments() method (add and edit modes of participant)
639 public function testAddPayments() {
640 list($lineItems, $contribution) = $this->addParticipantWithContribution();
641 foreach ($lineItems as $value) {
642 CRM_Contribute_BAO_Contribution
::addPayments($value, array($contribution));
644 $this->checkItemValues($contribution);
648 * checks db values for financial item
650 public function checkItemValues($contribution) {
651 $relationTypeId = key(CRM_Core_PseudoConstant
::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Accounts Receivable Account is' "));
652 $toFinancialAccount = CRM_Contribute_PseudoConstant
::financialAccountType(4, $relationTypeId);
653 $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')
654 INNER JOIN civicrm_entity_financial_trxn eft1 ON (eft1.financial_trxn_id = eft.financial_trxn_id AND eft1.entity_table = 'civicrm_financial_item')
655 WHERE eft.entity_id = %1 AND ft.to_financial_account_id <> %2";
657 $queryParams[1] = array($contribution->id
, 'Integer');
658 $queryParams[2] = array($toFinancialAccount, 'Integer');
660 $dao = CRM_Core_DAO
::executeQuery($query, $queryParams);
661 $amounts = array(100.00, 50.00);
662 while ($dao->fetch()) {
663 $this->assertEquals(150.00, $dao->total_amount
, 'Mismatch of total amount paid.');
664 $this->assertEquals($dao->amount
, array_pop($amounts), 'Mismatch of amount proportionally assigned to financial item');
667 Contact
::delete($this->_contactId
);
668 Event
::delete($this->_eventId
);
672 * assignProportionalLineItems() method (add and edit modes of participant)
674 public function testAssignProportionalLineItems() {
675 list($lineItems, $contribution) = $this->addParticipantWithContribution();
676 $contributions['total_amount'] = $contribution->total_amount
;
678 'contribution_id' => $contribution->id
,
679 'total_amount' => 150.00,
681 $trxn = new CRM_Financial_DAO_FinancialTrxn();
682 $trxn->orderBy('id DESC');
684 CRM_Contribute_BAO_Contribution
::assignProportionalLineItems($params, $trxn, $contributions);
685 $this->checkItemValues($contribution);
689 * Add participant with contribution
693 protected function addParticipantWithContribution() {
694 // creating price set, price field
695 require_once 'CiviTest/Event.php';
696 $this->_contactId
= Contact
::createIndividual();
697 $this->_eventId
= Event
::create($this->_contactId
);
698 $paramsSet['title'] = 'Price Set' . substr(sha1(rand()), 0, 4);
699 $paramsSet['name'] = CRM_Utils_String
::titleToVar($paramsSet['title']);
700 $paramsSet['is_active'] = TRUE;
701 $paramsSet['financial_type_id'] = 4;
702 $paramsSet['extends'] = 1;
704 $priceset = CRM_Price_BAO_PriceSet
::create($paramsSet);
705 $priceSetId = $priceset->id
;
707 //Checking for priceset added in the table.
708 $this->assertDBCompareValue('CRM_Price_BAO_PriceSet', $priceSetId, 'title',
709 'id', $paramsSet['title'], 'Check DB for created priceset'
711 $paramsField = array(
712 'label' => 'Price Field',
713 'name' => CRM_Utils_String
::titleToVar('Price Field'),
714 'html_type' => 'CheckBox',
715 'option_label' => array('1' => 'Price Field 1', '2' => 'Price Field 2'),
716 'option_value' => array('1' => 100, '2' => 200),
717 'option_name' => array('1' => 'Price Field 1', '2' => 'Price Field 2'),
718 'option_weight' => array('1' => 1, '2' => 2),
719 'option_amount' => array('1' => 100, '2' => 200),
720 'is_display_amounts' => 1,
722 'options_per_line' => 1,
723 'is_active' => array('1' => 1, '2' => 1),
724 'price_set_id' => $priceset->id
,
726 'financial_type_id' => CRM_Core_DAO
::getFieldValue('CRM_Financial_DAO_FinancialType', 'Event Fee', 'id', 'name'),
728 $priceField = CRM_Price_BAO_PriceField
::create($paramsField);
729 $eventParams = array(
730 'id' => $this->_eventId
,
731 'financial_type_id' => 4,
734 CRM_Event_BAO_Event
::create($eventParams);
735 CRM_Price_BAO_PriceSet
::addTo('civicrm_event', $this->_eventId
, $priceSetId);
737 $priceFields = $this->callAPISuccess('PriceFieldValue', 'get', array('price_field_id' => $priceField->id
));
738 $participantParams = array(
739 'financial_type_id' => 4,
740 'event_id' => $this->_eventId
,
743 'fee_currency' => 'USD',
744 'contact_id' => $this->_contactId
,
746 $participant = CRM_Event_BAO_Participant
::add($participantParams);
747 $contributionParams = array(
748 'total_amount' => 150,
750 'contact_id' => $this->_contactId
,
751 'financial_type_id' => 4,
752 'contribution_status_id' => 1,
753 'partial_payment_total' => 300.00,
754 'partial_amount_pay' => 150,
755 'contribution_mode' => 'participant',
756 'participant_id' => $participant->id
,
759 foreach ($priceFields['values'] as $key => $priceField) {
760 $lineItems[1][$key] = array(
761 'price_field_id' => $priceField['price_field_id'],
762 'price_field_value_id' => $priceField['id'],
763 'label' => $priceField['label'],
764 'field_title' => $priceField['label'],
766 'unit_price' => $priceField['amount'],
767 'line_total' => $priceField['amount'],
768 'financial_type_id' => $priceField['financial_type_id'],
771 $contributionParams['line_item'] = $lineItems;
772 $contributions = CRM_Contribute_BAO_Contribution
::create($contributionParams);
774 $paymentParticipant = array(
775 'participant_id' => $participant->id
,
776 'contribution_id' => $contributions->id
,
779 CRM_Event_BAO_ParticipantPayment
::create($paymentParticipant, $ids);
781 return array($lineItems, $contributions);
785 * checkLineItems() check if total amount matches the sum of line total
787 public function testcheckLineItems() {
790 'receive_date' => '2010-01-20',
791 'total_amount' => 100,
792 'financial_type_id' => 3,
793 'line_items' => array(
795 'line_item' => array(
797 'entity_table' => 'civicrm_contribution',
798 'price_field_id' => 8,
799 'price_field_value_id' => 16,
806 'entity_table' => 'civicrm_contribution',
807 'price_field_id' => 8,
808 'price_field_value_id' => 17,
813 'financial_type_id' => 1,
821 $error = CRM_Contribute_BAO_Contribution
::checkLineItems($params);
822 $this->fail("Missed expected exception");
824 catch (Exception
$e) {
825 $this->assertEquals("Line item total doesn't match with total amount.", $e->getMessage());
827 $this->assertEquals(3, $params['line_items'][0]['line_item'][0]['financial_type_id']);
828 $params['total_amount'] = 300;
829 CRM_Contribute_BAO_Contribution
::checkLineItems($params);