3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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 +--------------------------------------------------------------------+
29 require_once 'CiviTest/CiviUnitTestCase.php';
30 require_once 'CiviTest/Contact.php';
31 require_once 'CiviTest/Custom.php';
34 * Class CRM_Contribute_BAO_ContributionTest
36 class CRM_Contribute_BAO_ContributionTest
extends CiviUnitTestCase
{
42 'name' => 'Contribution BAOs',
43 'description' => 'Test all Contribute_BAO_Contribution methods.',
44 'group' => 'CiviCRM BAO Tests',
56 * create() method (create and update modes)
58 function testCreate() {
59 $contactId = Contact
::createIndividual();
60 $ids = array('contribution' => NULL);
63 'contact_id' => $contactId,
65 'financial_type_id' => 1,
66 'contribution_status_id' => 1,
67 'payment_instrument_id' => 1,
68 'source' => 'STUDENT',
69 'receive_date' => '20080522000000',
70 'receipt_date' => '20080522000000',
71 'non_deductible_amount' => 0.00,
72 'total_amount' => 200.00,
75 'trxn_id' => '22ereerwww444444',
76 'invoice_id' => '86ed39c9e9ee6ef6031621ce0eafe7eb81',
77 'thankyou_date' => '20080522',
80 $contribution = CRM_Contribute_BAO_Contribution
::create($params, $ids);
82 $this->assertEquals($params['trxn_id'], $contribution->trxn_id
, 'Check for transcation id creation.');
83 $this->assertEquals($contactId, $contribution->contact_id
, 'Check for contact id creation.');
85 //update contribution amount
86 $ids = array('contribution' => $contribution->id
);
87 $params['fee_amount'] = 10;
88 $params['net_amount'] = 190;
90 $contribution = CRM_Contribute_BAO_Contribution
::create($params, $ids);
92 $this->assertEquals($params['trxn_id'], $contribution->trxn_id
, 'Check for transcation id .');
93 $this->assertEquals($params['net_amount'], $contribution->net_amount
, 'Check for Amount updation.');
96 $this->contributionDelete($contribution->id
);
99 Contact
::delete($contactId);
103 * create() method with custom data
105 function testCreateWithCustomData() {
106 $contactId = Contact
::createIndividual();
107 $ids = array('contribution' => NULL);
110 $customGroup = Custom
::createGroup(array(), 'Contribution');
112 'label' => 'testFld',
113 'data_type' => 'String',
114 'html_type' => 'Text',
116 'custom_group_id' => $customGroup->id
,
118 $customField = CRM_Core_BAO_CustomField
::create($fields);
121 'contact_id' => $contactId,
123 'financial_type_id' => 1,
124 'contribution_status_id' => 1,
125 'payment_instrument_id' => 1,
126 'source' => 'STUDENT',
127 'receive_date' => '20080522000000',
128 'receipt_date' => '20080522000000',
130 'non_deductible_amount' => 0.00,
131 'total_amount' => 200.00,
134 'trxn_id' => '22ereerwww322323',
135 'invoice_id' => '22ed39c9e9ee6ef6031621ce0eafe6da70',
136 'thankyou_date' => '20080522',
140 $params['custom'] = array(
141 $customField->id
=> array(
143 'value' => 'Test custom value',
145 'custom_field_id' => $customField->id
,
146 'custom_group_id' => $customGroup->id
,
147 'table_name' => $customGroup->table_name
,
148 'column_name' => $customField->column_name
,
155 $contribution = CRM_Contribute_BAO_Contribution
::create($params, $ids);
157 // Check that the custom field value is saved
158 $customValueParams = array(
159 'entityID' => $contribution->id
,
160 'custom_' . $customField->id
=> 1,
162 $values = CRM_Core_BAO_CustomValueTable
::getValues($customValueParams);
163 $this->assertEquals('Test custom value', $values['custom_' . $customField->id
], 'Check the custom field value');
165 $this->assertEquals($params['trxn_id'], $contribution->trxn_id
, 'Check for transcation id creation.');
166 $this->assertEquals($contactId, $contribution->contact_id
, 'Check for contact id for Conribution.');
168 $this->contributionDelete($contribution->id
);
169 Custom
::deleteField($customField);
170 Custom
::deleteGroup($customGroup);
171 Contact
::delete($contactId);
175 * deleteContribution() method
177 function testDeleteContribution() {
178 $contactId = Contact
::createIndividual();
179 $ids = array('contribution' => NULL);
182 'contact_id' => $contactId,
184 'financial_type_id' => 1,
185 'contribution_status_id' => 1,
186 'payment_instrument_id' => 1,
187 'source' => 'STUDENT',
188 'receive_date' => '20080522000000',
189 'receipt_date' => '20080522000000',
191 'non_deductible_amount' => 0.00,
192 'total_amount' => 200.00,
195 'trxn_id' => '33ereerwww322323',
196 'invoice_id' => '33ed39c9e9ee6ef6031621ce0eafe6da70',
197 'thankyou_date' => '20080522',
200 $contribution = CRM_Contribute_BAO_Contribution
::create($params, $ids);
202 $this->assertEquals($params['trxn_id'], $contribution->trxn_id
, 'Check for transcation id creation.');
203 $this->assertEquals($contactId, $contribution->contact_id
, 'Check for contact id creation.');
205 $contributiondelete = CRM_Contribute_BAO_Contribution
::deleteContribution($contribution->id
);
207 $this->assertDBNull('CRM_Contribute_DAO_Contribution', $contribution->trxn_id
,
208 'id', 'trxn_id', 'Database check for deleted Contribution.'
210 Contact
::delete($contactId);
214 * create honor-contact method
216 function testcreateAndGetHonorContact() {
217 $firstName = 'John_' . substr(sha1(rand()), 0, 7);
218 $lastName = 'Smith_' . substr(sha1(rand()), 0, 7);
219 $email = "{$firstName}.{$lastName}@example.com";
221 //Get profile id of name honoree_individual used to create profileContact
222 $honoreeProfileId = NULL;
223 $ufGroupDAO = new CRM_Core_DAO_UFGroup();
224 $ufGroupDAO->name
= 'honoree_individual';
225 if ($ufGroupDAO->find(TRUE)) {
226 $honoreeProfileId = $ufGroupDAO->id
;
231 'first_name' => $firstName,
232 'last_name' => $lastName,
235 $softParam = array('soft_credit_type_id' => 1);
237 $honoreeContactId = CRM_Contact_BAO_Contact
::createProfileContact($params, CRM_Core_DAO
::$_nullArray,
238 NULL, NULL, $honoreeProfileId
241 $this->assertDBCompareValue('CRM_Contact_DAO_Contact', $honoreeContactId, 'first_name', 'id', $firstName,
242 'Database check for created honor contact record.'
244 //create contribution on behalf of honary.
246 $contactId = Contact
::createIndividual();
247 $softParam['contact_id'] = $honoreeContactId;
249 $ids = array('contribution' => NULL);
251 'contact_id' => $contactId,
253 'financial_type_id' => 4,
254 'contribution_status_id' => 1,
255 'receive_date' => date('Ymd'),
256 'total_amount' => 66,
259 $contribution = CRM_Contribute_BAO_Contribution
::create($param, $ids);
260 $id = $contribution->id
;
261 $softParam['contribution_id'] = $id;
262 $softParam['currency'] = $contribution->currency
;
263 $softParam['amount'] = $contribution->total_amount
;
265 //Create Soft Contribution for honoree contact
266 CRM_Contribute_BAO_ContributionSoft
::add($softParam);
268 $this->assertDBCompareValue('CRM_Contribute_DAO_ContributionSoft', $id, 'contact_id',
269 'contribution_id', $honoreeContactId, 'Check DB for honor contact of the contribution'
271 //get honory information
272 $getHonorContact = CRM_Contribute_BAO_Contribution
::getHonorContacts($honoreeContactId);
274 $this->assertDBCompareValue('CRM_Contact_DAO_Contact', $honoreeContactId, 'first_name', 'id', $firstName,
275 'Database check for created honor contact record.'
278 //get annual contribution information
279 $annual = CRM_Contribute_BAO_Contribution
::annual($contactId);
281 $config = CRM_Core_Config
::singleton();
282 $currencySymbol = CRM_Core_DAO
::getFieldValue('CRM_Financial_DAO_Currency',$config->defaultCurrency
,'symbol','name') ;
283 $this->assertDBCompareValue('CRM_Contribute_DAO_Contribution', $id, 'total_amount',
284 'id', ltrim($annual[2], $currencySymbol), 'Check DB for total amount of the contribution'
287 //Delete honor contact
288 Contact
::delete($honoreeContactId);
290 //Delete Contribution record
291 $this->contributionDelete($contribution->id
);
293 //Delete contributor contact
294 Contact
::delete($contactId);
298 * display sort name during
299 * contribution batch update through profile
302 function testsortName() {
304 'first_name' => 'Shane',
305 'last_name' => 'Whatson',
306 'contact_type' => 'Individual',
309 $contact = CRM_Contact_BAO_Contact
::add($params);
311 //Now check $contact is object of contact DAO..
312 $this->assertInstanceOf('CRM_Contact_DAO_Contact', $contact, 'Check for created object');
314 $contactId = $contact->id
;
316 $ids = array('contribution' => NULL);
319 'contact_id' => $contactId,
321 'financial_type_id' => 1,
322 'contribution_status_id' => 1,
323 'payment_instrument_id' => 1,
324 'source' => 'STUDENT',
325 'receive_date' => '20080522000000',
326 'receipt_date' => '20080522000000',
328 'non_deductible_amount' => 0.00,
329 'total_amount' => 300.00,
332 'trxn_id' => '22ereerwww323',
333 'invoice_id' => '22ed39c9e9ee621ce0eafe6da70',
334 'thankyou_date' => '20080522',
337 $contribution = CRM_Contribute_BAO_Contribution
::create($param, $ids);
339 $this->assertEquals($param['trxn_id'], $contribution->trxn_id
, 'Check for transcation id creation.');
340 $this->assertEquals($contactId, $contribution->contact_id
, 'Check for contact id creation.');
342 //display sort name during batch update
343 $sortName = CRM_Contribute_BAO_Contribution
::sortName($contribution->id
);
345 $this->assertEquals('Whatson, Shane', $sortName, 'Check for sort name.');
347 //Delete Contribution
348 $this->contributionDelete($contribution->id
);
350 Contact
::delete($contactId);
354 * Add premium during online Contribution
358 function testAddPremium() {
359 $contactId = Contact
::createIndividual();
367 'name' => 'TEST Premium',
369 'imageOption' => 'noImage',
370 'MAX_FILE_SIZE' => 2097152,
373 'min_contribution' => 100,
376 $premium = CRM_Contribute_BAO_ManagePremiums
::add($params, $ids);
378 $this->assertEquals('TEST Premium', $premium->name
, 'Check for premium name.');
380 $ids = array('contribution' => NULL);
383 'contact_id' => $contactId,
385 'financial_type_id' => 1,
386 'contribution_status_id' => 1,
387 'payment_instrument_id' => 1,
388 'source' => 'STUDENT',
389 'receive_date' => '20080522000000',
390 'receipt_date' => '20080522000000',
392 'non_deductible_amount' => 0.00,
393 'total_amount' => 300.00,
396 'trxn_id' => '33erdfrwvw434',
397 'invoice_id' => '98ed34f7u9hh672ce0eafe8fb92',
398 'thankyou_date' => '20080522',
401 $contribution = CRM_Contribute_BAO_Contribution
::create($param, $ids);
403 $this->assertEquals($param['trxn_id'], $contribution->trxn_id
, 'Check for transcation id creation.');
404 $this->assertEquals($contactId, $contribution->contact_id
, 'Check for contact id creation.');
406 //parameter for adding premium to contribution
408 'product_id' => $premium->id
,
409 'contribution_id' => $contribution->id
,
410 'product_option' => NULL,
413 $contributionProduct = CRM_Contribute_BAO_Contribution
::addPremium($data);
414 $this->assertEquals($contributionProduct->product_id
, $premium->id
, 'Check for Product id .');
417 CRM_Contribute_BAO_ManagePremiums
::del($premium->id
);
418 $this->assertDBNull('CRM_Contribute_DAO_Product', $premium->name
,
419 'id', 'name', 'Database check for deleted Product.'
422 //Delete Contribution
423 $this->contributionDelete($contribution->id
);
425 Contact
::delete($contactId);
429 * Check duplicate contribution id
430 * during the contribution import
431 * checkDuplicateIds();
433 function testcheckDuplicateIds() {
434 $contactId = Contact
::createIndividual();
436 $ids = array('contribution' => NULL);
439 'contact_id' => $contactId,
441 'financial_type_id' => 1,
442 'contribution_status_id' => 1,
443 'payment_instrument_id' => 1,
444 'source' => 'STUDENT',
445 'receive_date' => '20080522000000',
446 'receipt_date' => '20080522000000',
448 'non_deductible_amount' => 0.00,
449 'total_amount' => 300.00,
452 'trxn_id' => '76ereeswww835',
453 'invoice_id' => '93ed39a9e9hd621bs0eafe3da82',
454 'thankyou_date' => '20080522',
457 $contribution = CRM_Contribute_BAO_Contribution
::create($param, $ids);
459 $this->assertEquals($param['trxn_id'], $contribution->trxn_id
, 'Check for transcation id creation.');
460 $this->assertEquals($contactId, $contribution->contact_id
, 'Check for contact id creation.');
462 'id' => $contribution->id
,
463 'trxn_id' => $contribution->trxn_id
,
464 'invoice_id' => $contribution->invoice_id
,
466 $contributionID = CRM_Contribute_BAO_Contribution
::checkDuplicateIds($data);
467 $this->assertEquals($contributionID, $contribution->id
, 'Check for duplicate transcation id .');
469 // Delete Contribution
470 $this->contributionDelete($contribution->id
);
472 Contact
::delete($contactId);