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';
32 class CRM_Contribute_BAO_ContributionTest
extends CiviUnitTestCase
{
35 'name' => 'Contribution BAOs',
36 'description' => 'Test all Contribute_BAO_Contribution methods.',
37 'group' => 'CiviCRM BAO Tests',
49 * create() method (create and update modes)
51 function testCreate() {
52 $contactId = Contact
::createIndividual();
53 $ids = array('contribution' => NULL);
56 'contact_id' => $contactId,
58 'financial_type_id' => 1,
59 'contribution_status_id' => 1,
60 'payment_instrument_id' => 1,
61 'source' => 'STUDENT',
62 'receive_date' => '20080522000000',
63 'receipt_date' => '20080522000000',
64 'non_deductible_amount' => 0.00,
65 'total_amount' => 200.00,
68 'trxn_id' => '22ereerwww444444',
69 'invoice_id' => '86ed39c9e9ee6ef6031621ce0eafe7eb81',
70 'thankyou_date' => '20080522',
73 $contribution = CRM_Contribute_BAO_Contribution
::create($params, $ids);
75 $this->assertEquals($params['trxn_id'], $contribution->trxn_id
, 'Check for transcation id creation.');
76 $this->assertEquals($contactId, $contribution->contact_id
, 'Check for contact id creation.');
78 //update contribution amount
79 $ids = array('contribution' => $contribution->id
);
80 $params['fee_amount'] = 10;
81 $params['net_amount'] = 190;
83 $contribution = CRM_Contribute_BAO_Contribution
::create($params, $ids);
85 $this->assertEquals($params['trxn_id'], $contribution->trxn_id
, 'Check for transcation id .');
86 $this->assertEquals($params['net_amount'], $contribution->net_amount
, 'Check for Amount updation.');
89 $this->contributionDelete($contribution->id
);
92 Contact
::delete($contactId);
96 * create() method with custom data
98 function testCreateWithCustomData() {
99 $contactId = Contact
::createIndividual();
100 $ids = array('contribution' => NULL);
103 $customGroup = Custom
::createGroup(array(), 'Contribution');
105 'label' => 'testFld',
106 'data_type' => 'String',
107 'html_type' => 'Text',
109 'custom_group_id' => $customGroup->id
,
111 $customField = CRM_Core_BAO_CustomField
::create($fields);
114 'contact_id' => $contactId,
116 'financial_type_id' => 1,
117 'contribution_status_id' => 1,
118 'payment_instrument_id' => 1,
119 'source' => 'STUDENT',
120 'receive_date' => '20080522000000',
121 'receipt_date' => '20080522000000',
123 'non_deductible_amount' => 0.00,
124 'total_amount' => 200.00,
127 'trxn_id' => '22ereerwww322323',
128 'invoice_id' => '22ed39c9e9ee6ef6031621ce0eafe6da70',
129 'thankyou_date' => '20080522',
133 $params['custom'] = array(
134 $customField->id
=> array(
136 'value' => 'Test custom value',
138 'custom_field_id' => $customField->id
,
139 'custom_group_id' => $customGroup->id
,
140 'table_name' => $customGroup->table_name
,
141 'column_name' => $customField->column_name
,
148 $contribution = CRM_Contribute_BAO_Contribution
::create($params, $ids);
150 // Check that the custom field value is saved
151 $customValueParams = array(
152 'entityID' => $contribution->id
,
153 'custom_' . $customField->id
=> 1,
155 $values = CRM_Core_BAO_CustomValueTable
::getValues($customValueParams);
156 $this->assertEquals('Test custom value', $values['custom_' . $customField->id
], 'Check the custom field value');
158 $this->assertEquals($params['trxn_id'], $contribution->trxn_id
, 'Check for transcation id creation.');
159 $this->assertEquals($contactId, $contribution->contact_id
, 'Check for contact id for Conribution.');
161 $this->contributionDelete($contribution->id
);
162 Custom
::deleteField($customField);
163 Custom
::deleteGroup($customGroup);
164 Contact
::delete($contactId);
168 * deleteContribution() method
170 function testDeleteContribution() {
171 $contactId = Contact
::createIndividual();
172 $ids = array('contribution' => NULL);
175 'contact_id' => $contactId,
177 'financial_type_id' => 1,
178 'contribution_status_id' => 1,
179 'payment_instrument_id' => 1,
180 'source' => 'STUDENT',
181 'receive_date' => '20080522000000',
182 'receipt_date' => '20080522000000',
184 'non_deductible_amount' => 0.00,
185 'total_amount' => 200.00,
188 'trxn_id' => '33ereerwww322323',
189 'invoice_id' => '33ed39c9e9ee6ef6031621ce0eafe6da70',
190 'thankyou_date' => '20080522',
193 $contribution = CRM_Contribute_BAO_Contribution
::create($params, $ids);
195 $this->assertEquals($params['trxn_id'], $contribution->trxn_id
, 'Check for transcation id creation.');
196 $this->assertEquals($contactId, $contribution->contact_id
, 'Check for contact id creation.');
198 $contributiondelete = CRM_Contribute_BAO_Contribution
::deleteContribution($contribution->id
);
200 $this->assertDBNull('CRM_Contribute_DAO_Contribution', $contribution->trxn_id
,
201 'id', 'trxn_id', 'Database check for deleted Contribution.'
203 Contact
::delete($contactId);
207 * create honor-contact method
209 function testcreateAndGetHonorContact() {
210 $firstName = 'John_' . substr(sha1(rand()), 0, 7);
211 $lastName = 'Smith_' . substr(sha1(rand()), 0, 7);
212 $email = "{$firstName}.{$lastName}@example.com";
214 //Get profile id of name honoree_individual used to create profileContact
215 $honoreeProfileId = NULL;
216 $ufGroupDAO = new CRM_Core_DAO_UFGroup();
217 $ufGroupDAO->name
= 'honoree_individual';
218 if ($ufGroupDAO->find(TRUE)) {
219 $honoreeProfileId = $ufGroupDAO->id
;
224 'first_name' => $firstName,
225 'last_name' => $lastName,
228 $softParam = array('soft_credit_type_id' => 1);
230 $honoreeContactId = CRM_Contact_BAO_Contact
::createProfileContact($params, CRM_Core_DAO
::$_nullArray,
231 NULL, NULL, $honoreeProfileId
234 $this->assertDBCompareValue('CRM_Contact_DAO_Contact', $honoreeContactId, 'first_name', 'id', $firstName,
235 'Database check for created honor contact record.'
237 //create contribution on behalf of honary.
239 $contactId = Contact
::createIndividual();
240 $softParam['contact_id'] = $honoreeContactId;
242 $ids = array('contribution' => NULL);
244 'contact_id' => $contactId,
246 'financial_type_id' => 4,
247 'contribution_status_id' => 1,
248 'receive_date' => date('Ymd'),
249 'total_amount' => 66,
252 $contribution = CRM_Contribute_BAO_Contribution
::create($param, $ids);
253 $id = $contribution->id
;
254 $softParam['contribution_id'] = $id;
255 $softParam['currency'] = $contribution->currency
;
256 $softParam['amount'] = $contribution->total_amount
;
258 //Create Soft Contribution for honoree contact
259 CRM_Contribute_BAO_ContributionSoft
::add($softParam);
261 $this->assertDBCompareValue('CRM_Contribute_DAO_ContributionSoft', $id, 'contact_id',
262 'contribution_id', $honoreeContactId, 'Check DB for honor contact of the contribution'
264 //get honory information
265 $getHonorContact = CRM_Contribute_BAO_Contribution
::getHonorContacts($honoreeContactId);
267 $this->assertDBCompareValue('CRM_Contact_DAO_Contact', $honoreeContactId, 'first_name', 'id', $firstName,
268 'Database check for created honor contact record.'
271 //get annual contribution information
272 $annual = CRM_Contribute_BAO_Contribution
::annual($contactId);
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'
280 //Delete honor contact
281 Contact
::delete($honoreeContactId);
283 //Delete Contribution record
284 $this->contributionDelete($contribution->id
);
286 //Delete contributor contact
287 Contact
::delete($contactId);
291 * display sort name during
292 * contribution batch update through profile
295 function testsortName() {
297 'first_name' => 'Shane',
298 'last_name' => 'Whatson',
299 'contact_type' => 'Individual',
302 $contact = CRM_Contact_BAO_Contact
::add($params);
304 //Now check $contact is object of contact DAO..
305 $this->assertInstanceOf('CRM_Contact_DAO_Contact', $contact, 'Check for created object');
307 $contactId = $contact->id
;
309 $ids = array('contribution' => NULL);
312 'contact_id' => $contactId,
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',
321 'non_deductible_amount' => 0.00,
322 'total_amount' => 300.00,
325 'trxn_id' => '22ereerwww323',
326 'invoice_id' => '22ed39c9e9ee621ce0eafe6da70',
327 'thankyou_date' => '20080522',
330 $contribution = CRM_Contribute_BAO_Contribution
::create($param, $ids);
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.');
335 //display sort name during batch update
336 $sortName = CRM_Contribute_BAO_Contribution
::sortName($contribution->id
);
338 $this->assertEquals('Whatson, Shane', $sortName, 'Check for sort name.');
340 //Delete Contribution
341 $this->contributionDelete($contribution->id
);
343 Contact
::delete($contactId);
347 * Add premium during online Contribution
351 function testAddPremium() {
352 $contactId = Contact
::createIndividual();
360 'name' => 'TEST Premium',
362 'imageOption' => 'noImage',
363 'MAX_FILE_SIZE' => 2097152,
366 'min_contribution' => 100,
369 $premium = CRM_Contribute_BAO_ManagePremiums
::add($params, $ids);
371 $this->assertEquals('TEST Premium', $premium->name
, 'Check for premium name.');
373 $ids = array('contribution' => NULL);
376 'contact_id' => $contactId,
378 'financial_type_id' => 1,
379 'contribution_status_id' => 1,
380 'payment_instrument_id' => 1,
381 'source' => 'STUDENT',
382 'receive_date' => '20080522000000',
383 'receipt_date' => '20080522000000',
385 'non_deductible_amount' => 0.00,
386 'total_amount' => 300.00,
389 'trxn_id' => '33erdfrwvw434',
390 'invoice_id' => '98ed34f7u9hh672ce0eafe8fb92',
391 'thankyou_date' => '20080522',
394 $contribution = CRM_Contribute_BAO_Contribution
::create($param, $ids);
396 $this->assertEquals($param['trxn_id'], $contribution->trxn_id
, 'Check for transcation id creation.');
397 $this->assertEquals($contactId, $contribution->contact_id
, 'Check for contact id creation.');
399 //parameter for adding premium to contribution
401 'product_id' => $premium->id
,
402 'contribution_id' => $contribution->id
,
403 'product_option' => NULL,
406 $contributionProduct = CRM_Contribute_BAO_Contribution
::addPremium($data);
407 $this->assertEquals($contributionProduct->product_id
, $premium->id
, 'Check for Product id .');
410 CRM_Contribute_BAO_ManagePremiums
::del($premium->id
);
411 $this->assertDBNull('CRM_Contribute_DAO_Product', $premium->name
,
412 'id', 'name', 'Database check for deleted Product.'
415 //Delete Contribution
416 $this->contributionDelete($contribution->id
);
418 Contact
::delete($contactId);
422 * Check duplicate contribution id
423 * during the contribution import
424 * checkDuplicateIds();
426 function testcheckDuplicateIds() {
427 $contactId = Contact
::createIndividual();
429 $ids = array('contribution' => NULL);
432 'contact_id' => $contactId,
434 'financial_type_id' => 1,
435 'contribution_status_id' => 1,
436 'payment_instrument_id' => 1,
437 'source' => 'STUDENT',
438 'receive_date' => '20080522000000',
439 'receipt_date' => '20080522000000',
441 'non_deductible_amount' => 0.00,
442 'total_amount' => 300.00,
445 'trxn_id' => '76ereeswww835',
446 'invoice_id' => '93ed39a9e9hd621bs0eafe3da82',
447 'thankyou_date' => '20080522',
450 $contribution = CRM_Contribute_BAO_Contribution
::create($param, $ids);
452 $this->assertEquals($param['trxn_id'], $contribution->trxn_id
, 'Check for transcation id creation.');
453 $this->assertEquals($contactId, $contribution->contact_id
, 'Check for contact id creation.');
455 'id' => $contribution->id
,
456 'trxn_id' => $contribution->trxn_id
,
457 'invoice_id' => $contribution->invoice_id
,
459 $contributionID = CRM_Contribute_BAO_Contribution
::checkDuplicateIds($data);
460 $this->assertEquals($contributionID, $contribution->id
, 'Check for duplicate transcation id .');
462 // Delete Contribution
463 $this->contributionDelete($contribution->id
);
465 Contact
::delete($contactId);