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 +--------------------------------------------------------------------+
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
{
39 * Create() method (create and update modes).
41 public function testCreate() {
42 $contactId = Contact
::createIndividual();
43 $ids = array('contribution' => NULL);
46 'contact_id' => $contactId,
48 'financial_type_id' => 1,
49 'contribution_status_id' => 1,
50 'payment_instrument_id' => 1,
51 'source' => 'STUDENT',
52 'receive_date' => '20080522000000',
53 'receipt_date' => '20080522000000',
54 'non_deductible_amount' => 0.00,
55 'total_amount' => 200.00,
58 'trxn_id' => '22ereerwww444444',
59 'invoice_id' => '86ed39c9e9ee6ef6031621ce0eafe7eb81',
60 'thankyou_date' => '20080522',
63 $contribution = CRM_Contribute_BAO_Contribution
::create($params, $ids);
65 $this->assertEquals($params['trxn_id'], $contribution->trxn_id
, 'Check for transcation id creation.');
66 $this->assertEquals($contactId, $contribution->contact_id
, 'Check for contact id creation.');
68 //update contribution amount
69 $ids = array('contribution' => $contribution->id
);
70 $params['fee_amount'] = 10;
71 $params['net_amount'] = 190;
73 $contribution = CRM_Contribute_BAO_Contribution
::create($params, $ids);
75 $this->assertEquals($params['trxn_id'], $contribution->trxn_id
, 'Check for transcation id .');
76 $this->assertEquals($params['net_amount'], $contribution->net_amount
, 'Check for Amount updation.');
79 $this->contributionDelete($contribution->id
);
82 Contact
::delete($contactId);
86 * Create() method with custom data.
88 public function testCreateWithCustomData() {
89 $contactId = Contact
::createIndividual();
90 $ids = array('contribution' => NULL);
93 $customGroup = Custom
::createGroup(array(), 'Contribution');
96 'data_type' => 'String',
97 'html_type' => 'Text',
99 'custom_group_id' => $customGroup->id
,
101 $customField = CRM_Core_BAO_CustomField
::create($fields);
104 'contact_id' => $contactId,
106 'financial_type_id' => 1,
107 'contribution_status_id' => 1,
108 'payment_instrument_id' => 1,
109 'source' => 'STUDENT',
110 'receive_date' => '20080522000000',
111 'receipt_date' => '20080522000000',
113 'non_deductible_amount' => 0.00,
114 'total_amount' => 200.00,
117 'trxn_id' => '22ereerwww322323',
118 'invoice_id' => '22ed39c9e9ee6ef6031621ce0eafe6da70',
119 'thankyou_date' => '20080522',
122 $params['custom'] = array(
123 $customField->id
=> array(
125 'value' => 'Test custom value',
127 'custom_field_id' => $customField->id
,
128 'custom_group_id' => $customGroup->id
,
129 'table_name' => $customGroup->table_name
,
130 'column_name' => $customField->column_name
,
136 $contribution = CRM_Contribute_BAO_Contribution
::create($params, $ids);
138 // Check that the custom field value is saved
139 $customValueParams = array(
140 'entityID' => $contribution->id
,
141 'custom_' . $customField->id
=> 1,
143 $values = CRM_Core_BAO_CustomValueTable
::getValues($customValueParams);
144 $this->assertEquals('Test custom value', $values['custom_' . $customField->id
], 'Check the custom field value');
146 $this->assertEquals($params['trxn_id'], $contribution->trxn_id
, 'Check for transcation id creation.');
147 $this->assertEquals($contactId, $contribution->contact_id
, 'Check for contact id for Conribution.');
149 $this->contributionDelete($contribution->id
);
150 Custom
::deleteField($customField);
151 Custom
::deleteGroup($customGroup);
152 Contact
::delete($contactId);
156 * DeleteContribution() method
158 public function testDeleteContribution() {
159 $contactId = Contact
::createIndividual();
160 $ids = array('contribution' => NULL);
163 'contact_id' => $contactId,
165 'financial_type_id' => 1,
166 'contribution_status_id' => 1,
167 'payment_instrument_id' => 1,
168 'source' => 'STUDENT',
169 'receive_date' => '20080522000000',
170 'receipt_date' => '20080522000000',
172 'non_deductible_amount' => 0.00,
173 'total_amount' => 200.00,
176 'trxn_id' => '33ereerwww322323',
177 'invoice_id' => '33ed39c9e9ee6ef6031621ce0eafe6da70',
178 'thankyou_date' => '20080522',
181 $contribution = CRM_Contribute_BAO_Contribution
::create($params, $ids);
183 $this->assertEquals($params['trxn_id'], $contribution->trxn_id
, 'Check for transcation id creation.');
184 $this->assertEquals($contactId, $contribution->contact_id
, 'Check for contact id creation.');
186 $contributiondelete = CRM_Contribute_BAO_Contribution
::deleteContribution($contribution->id
);
188 $this->assertDBNull('CRM_Contribute_DAO_Contribution', $contribution->trxn_id
,
189 'id', 'trxn_id', 'Database check for deleted Contribution.'
191 Contact
::delete($contactId);
195 * Create honor-contact method
197 public function testcreateAndGetHonorContact() {
198 $firstName = 'John_' . substr(sha1(rand()), 0, 7);
199 $lastName = 'Smith_' . substr(sha1(rand()), 0, 7);
200 $email = "{$firstName}.{$lastName}@example.com";
202 //Get profile id of name honoree_individual used to create profileContact
203 $honoreeProfileId = NULL;
204 $ufGroupDAO = new CRM_Core_DAO_UFGroup();
205 $ufGroupDAO->name
= 'honoree_individual';
206 if ($ufGroupDAO->find(TRUE)) {
207 $honoreeProfileId = $ufGroupDAO->id
;
212 'first_name' => $firstName,
213 'last_name' => $lastName,
216 $softParam = array('soft_credit_type_id' => 1);
218 $honoreeContactId = CRM_Contact_BAO_Contact
::createProfileContact($params, CRM_Core_DAO
::$_nullArray,
219 NULL, NULL, $honoreeProfileId
222 $this->assertDBCompareValue('CRM_Contact_DAO_Contact', $honoreeContactId, 'first_name', 'id', $firstName,
223 'Database check for created honor contact record.'
225 //create contribution on behalf of honary.
227 $contactId = Contact
::createIndividual();
228 $softParam['contact_id'] = $honoreeContactId;
230 $ids = array('contribution' => NULL);
232 'contact_id' => $contactId,
234 'financial_type_id' => 4,
235 'contribution_status_id' => 1,
236 'receive_date' => date('Ymd'),
237 'total_amount' => 66,
240 $contribution = CRM_Contribute_BAO_Contribution
::create($param, $ids);
241 $id = $contribution->id
;
242 $softParam['contribution_id'] = $id;
243 $softParam['currency'] = $contribution->currency
;
244 $softParam['amount'] = $contribution->total_amount
;
246 //Create Soft Contribution for honoree contact
247 CRM_Contribute_BAO_ContributionSoft
::add($softParam);
249 $this->assertDBCompareValue('CRM_Contribute_DAO_ContributionSoft', $id, 'contact_id',
250 'contribution_id', $honoreeContactId, 'Check DB for honor contact of the contribution'
252 //get honorary information
253 $getHonorContact = CRM_Contribute_BAO_Contribution
::getHonorContacts($honoreeContactId);
254 $this->assertEquals(array(
256 'honor_type' => 'In Honor of',
258 'display_name' => 'John Doe',
259 'type' => 'Event Fee',
261 'amount' => '$ 66.00',
263 'receive_date' => date('Y-m-d H:i:s'),
264 'contribution_status' => 'Completed',
266 ), $getHonorContact);
268 $this->assertDBCompareValue('CRM_Contact_DAO_Contact', $honoreeContactId, 'first_name', 'id', $firstName,
269 'Database check for created honor contact record.'
272 //get annual contribution information
273 $annual = CRM_Contribute_BAO_Contribution
::annual($contactId);
275 $config = CRM_Core_Config
::singleton();
276 $currencySymbol = CRM_Core_DAO
::getFieldValue('CRM_Financial_DAO_Currency', $config->defaultCurrency
, 'symbol', 'name');
277 $this->assertDBCompareValue('CRM_Contribute_DAO_Contribution', $id, 'total_amount',
278 'id', ltrim($annual[2], $currencySymbol), 'Check DB for total amount of the contribution'
281 //Delete honor contact
282 Contact
::delete($honoreeContactId);
284 //Delete Contribution record
285 $this->contributionDelete($contribution->id
);
287 //Delete contributor contact
288 Contact
::delete($contactId);
292 * Display sort name during.
293 * contribution batch update through profile
296 public function testsortName() {
298 'first_name' => 'Shane',
299 'last_name' => 'Whatson',
300 'contact_type' => 'Individual',
303 $contact = CRM_Contact_BAO_Contact
::add($params);
305 //Now check $contact is object of contact DAO..
306 $this->assertInstanceOf('CRM_Contact_DAO_Contact', $contact, 'Check for created object');
308 $contactId = $contact->id
;
310 $ids = array('contribution' => NULL);
313 'contact_id' => $contactId,
315 'financial_type_id' => 1,
316 'contribution_status_id' => 1,
317 'payment_instrument_id' => 1,
318 'source' => 'STUDENT',
319 'receive_date' => '20080522000000',
320 'receipt_date' => '20080522000000',
322 'non_deductible_amount' => 0.00,
323 'total_amount' => 300.00,
326 'trxn_id' => '22ereerwww323',
327 'invoice_id' => '22ed39c9e9ee621ce0eafe6da70',
328 'thankyou_date' => '20080522',
331 $contribution = CRM_Contribute_BAO_Contribution
::create($param, $ids);
333 $this->assertEquals($param['trxn_id'], $contribution->trxn_id
, 'Check for transcation id creation.');
334 $this->assertEquals($contactId, $contribution->contact_id
, 'Check for contact id creation.');
336 //display sort name during batch update
337 $sortName = CRM_Contribute_BAO_Contribution
::sortName($contribution->id
);
339 $this->assertEquals('Whatson, Shane', $sortName, 'Check for sort name.');
341 //Delete Contribution
342 $this->contributionDelete($contribution->id
);
344 Contact
::delete($contactId);
348 * Add premium during online Contribution.
352 public function testAddPremium() {
353 $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 public 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);