3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.4 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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
208 * createHonorContact();
210 function testcreateAndGetHonorContact() {
211 $firstName = 'John_' . substr(sha1(rand()), 0, 7);
212 $lastName = 'Smith_' . substr(sha1(rand()), 0, 7);
213 $email = "{$firstName}.{$lastName}@example.com";
217 'honor_type_id' => 1,
218 'honor_prefix_id' => 3,
219 'honor_first_name' => $firstName,
220 'honor_last_name' => $lastName,
221 'honor_email' => $email,
223 $contact = CRM_Contribute_BAO_Contribution
::createHonorContact($params, $honorId);
225 $this->assertDBCompareValue('CRM_Contact_DAO_Contact', $contact, 'first_name', 'id', $firstName,
226 'Database check for created honor contact record.'
228 //create contribution on behalf of honary.
230 $contactId = Contact
::createIndividual();
232 $ids = array('contribution' => NULL);
234 'contact_id' => $contactId,
236 'financial_type_id' => 4,
237 'contribution_status_id' => 1,
238 'receive_date' => date('Ymd'),
239 'total_amount' => 66,
240 'honor_type_id' => 1,
241 'honor_contact_id' => $contact,
244 $contribution = CRM_Contribute_BAO_Contribution
::create($param, $ids);
245 $id = $contribution->id
;
246 $this->assertDBCompareValue('CRM_Contribute_DAO_Contribution', $id, 'honor_contact_id',
247 'id', $contact, 'Check DB for honor contact of the contribution'
249 //get honory information
250 $getHonorContact = CRM_Contribute_BAO_Contribution
::getHonorContacts($contact);
252 $this->assertDBCompareValue('CRM_Contact_DAO_Contact', $contact, 'first_name', 'id', $firstName,
253 'Database check for created honor contact record.'
256 //get annual contribution information
257 $annual = CRM_Contribute_BAO_Contribution
::annual($contactId);
259 $config = CRM_Core_Config
::singleton();
260 $currencySymbol = CRM_Core_DAO
::getFieldValue('CRM_Financial_DAO_Currency',$config->defaultCurrency
,'symbol','name') ;
261 $this->assertDBCompareValue('CRM_Contribute_DAO_Contribution', $id, 'total_amount',
262 'id', ltrim($annual[2], $currencySymbol), 'Check DB for total amount of the contribution'
265 //Delete honor contact
266 Contact
::delete($contact);
268 //Delete Contribution record
269 $this->contributionDelete($contribution->id
);
271 //Delete contributor contact
272 Contact
::delete($contactId);
276 * display sort name during
277 * contribution batch update through profile
280 function testsortName() {
282 'first_name' => 'Shane',
283 'last_name' => 'Whatson',
284 'contact_type' => 'Individual',
287 $contact = CRM_Contact_BAO_Contact
::add($params);
289 //Now check $contact is object of contact DAO..
290 $this->assertInstanceOf('CRM_Contact_DAO_Contact', $contact, 'Check for created object');
292 $contactId = $contact->id
;
294 $ids = array('contribution' => NULL);
297 'contact_id' => $contactId,
299 'financial_type_id' => 1,
300 'contribution_status_id' => 1,
301 'payment_instrument_id' => 1,
302 'source' => 'STUDENT',
303 'receive_date' => '20080522000000',
304 'receipt_date' => '20080522000000',
306 'non_deductible_amount' => 0.00,
307 'total_amount' => 300.00,
310 'trxn_id' => '22ereerwww323',
311 'invoice_id' => '22ed39c9e9ee621ce0eafe6da70',
312 'thankyou_date' => '20080522',
315 $contribution = CRM_Contribute_BAO_Contribution
::create($param, $ids);
317 $this->assertEquals($param['trxn_id'], $contribution->trxn_id
, 'Check for transcation id creation.');
318 $this->assertEquals($contactId, $contribution->contact_id
, 'Check for contact id creation.');
320 //display sort name during batch update
321 $sortName = CRM_Contribute_BAO_Contribution
::sortName($contribution->id
);
323 $this->assertEquals('Whatson, Shane', $sortName, 'Check for sort name.');
325 //Delete Contribution
326 $this->contributionDelete($contribution->id
);
328 Contact
::delete($contactId);
332 * Add premium during online Contribution
336 function testAddPremium() {
337 $contactId = Contact
::createIndividual();
345 'name' => 'TEST Premium',
347 'imageOption' => 'noImage',
348 'MAX_FILE_SIZE' => 2097152,
351 'min_contribution' => 100,
354 $premium = CRM_Contribute_BAO_ManagePremiums
::add($params, $ids);
356 $this->assertEquals('TEST Premium', $premium->name
, 'Check for premium name.');
358 $ids = array('contribution' => NULL);
361 'contact_id' => $contactId,
363 'financial_type_id' => 1,
364 'contribution_status_id' => 1,
365 'payment_instrument_id' => 1,
366 'source' => 'STUDENT',
367 'receive_date' => '20080522000000',
368 'receipt_date' => '20080522000000',
370 'non_deductible_amount' => 0.00,
371 'total_amount' => 300.00,
374 'trxn_id' => '33erdfrwvw434',
375 'invoice_id' => '98ed34f7u9hh672ce0eafe8fb92',
376 'thankyou_date' => '20080522',
379 $contribution = CRM_Contribute_BAO_Contribution
::create($param, $ids);
381 $this->assertEquals($param['trxn_id'], $contribution->trxn_id
, 'Check for transcation id creation.');
382 $this->assertEquals($contactId, $contribution->contact_id
, 'Check for contact id creation.');
384 //parameter for adding premium to contribution
386 'product_id' => $premium->id
,
387 'contribution_id' => $contribution->id
,
388 'product_option' => NULL,
391 $contributionProduct = CRM_Contribute_BAO_Contribution
::addPremium($data);
392 $this->assertEquals($contributionProduct->product_id
, $premium->id
, 'Check for Product id .');
395 CRM_Contribute_BAO_ManagePremiums
::del($premium->id
);
396 $this->assertDBNull('CRM_Contribute_DAO_Product', $premium->name
,
397 'id', 'name', 'Database check for deleted Product.'
400 //Delete Contribution
401 $this->contributionDelete($contribution->id
);
403 Contact
::delete($contactId);
407 * Check duplicate contribution id
408 * during the contribution import
409 * checkDuplicateIds();
411 function testcheckDuplicateIds() {
412 $contactId = Contact
::createIndividual();
414 $ids = array('contribution' => NULL);
417 'contact_id' => $contactId,
419 'financial_type_id' => 1,
420 'contribution_status_id' => 1,
421 'payment_instrument_id' => 1,
422 'source' => 'STUDENT',
423 'receive_date' => '20080522000000',
424 'receipt_date' => '20080522000000',
426 'non_deductible_amount' => 0.00,
427 'total_amount' => 300.00,
430 'trxn_id' => '76ereeswww835',
431 'invoice_id' => '93ed39a9e9hd621bs0eafe3da82',
432 'thankyou_date' => '20080522',
435 $contribution = CRM_Contribute_BAO_Contribution
::create($param, $ids);
437 $this->assertEquals($param['trxn_id'], $contribution->trxn_id
, 'Check for transcation id creation.');
438 $this->assertEquals($contactId, $contribution->contact_id
, 'Check for contact id creation.');
440 'id' => $contribution->id
,
441 'trxn_id' => $contribution->trxn_id
,
442 'invoice_id' => $contribution->invoice_id
,
444 $contributionID = CRM_Contribute_BAO_Contribution
::checkDuplicateIds($data);
445 $this->assertEquals($contributionID, $contribution->id
, 'Check for duplicate transcation id .');
447 // Delete Contribution
448 $this->contributionDelete($contribution->id
);
450 Contact
::delete($contactId);