Merge branch 'rcsheets-docstring-cleanup'
[civicrm-core.git] / tests / phpunit / CRM / Contribute / BAO / ContributionTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
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. |
13 | |
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. |
18 | |
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 +--------------------------------------------------------------------+
26 */
27
28
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 {
33 function get_info() {
34 return array(
35 'name' => 'Contribution BAOs',
36 'description' => 'Test all Contribute_BAO_Contribution methods.',
37 'group' => 'CiviCRM BAO Tests',
38 );
39 }
40
41 function setUp() {
42 parent::setUp();
43 }
44
45 function teardown() {
46 }
47
48 /**
49 * create() method (create and update modes)
50 */
51 function testCreate() {
52 $contactId = Contact::createIndividual();
53 $ids = array('contribution' => NULL);
54
55 $params = array(
56 'contact_id' => $contactId,
57 'currency' => 'USD',
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,
66 'fee_amount' => 5,
67 'net_amount' => 195,
68 'trxn_id' => '22ereerwww444444',
69 'invoice_id' => '86ed39c9e9ee6ef6031621ce0eafe7eb81',
70 'thankyou_date' => '20080522',
71 );
72
73 $contribution = CRM_Contribute_BAO_Contribution::create($params, $ids);
74
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.');
77
78 //update contribution amount
79 $ids = array('contribution' => $contribution->id);
80 $params['fee_amount'] = 10;
81 $params['net_amount'] = 190;
82
83 $contribution = CRM_Contribute_BAO_Contribution::create($params, $ids);
84
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.');
87
88 //Delete Contribution
89 $this->contributionDelete($contribution->id);
90
91 //Delete Contact
92 Contact::delete($contactId);
93 }
94
95 /**
96 * create() method with custom data
97 */
98 function testCreateWithCustomData() {
99 $contactId = Contact::createIndividual();
100 $ids = array('contribution' => NULL);
101
102 //create custom data
103 $customGroup = Custom::createGroup(array(), 'Contribution');
104 $fields = array(
105 'label' => 'testFld',
106 'data_type' => 'String',
107 'html_type' => 'Text',
108 'is_active' => 1,
109 'custom_group_id' => $customGroup->id,
110 );
111 $customField = CRM_Core_BAO_CustomField::create($fields);
112
113 $params = array(
114 'contact_id' => $contactId,
115 'currency' => 'USD',
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',
122 'id' => NULL,
123 'non_deductible_amount' => 0.00,
124 'total_amount' => 200.00,
125 'fee_amount' => 5,
126 'net_amount' => 195,
127 'trxn_id' => '22ereerwww322323',
128 'invoice_id' => '22ed39c9e9ee6ef6031621ce0eafe6da70',
129 'thankyou_date' => '20080522',
130 );
131
132
133 $params['custom'] = array(
134 $customField->id => array(
135 -1 => array(
136 'value' => 'Test custom value',
137 'type' => 'String',
138 'custom_field_id' => $customField->id,
139 'custom_group_id' => $customGroup->id,
140 'table_name' => $customGroup->table_name,
141 'column_name' => $customField->column_name,
142 'file_id' => NULL,
143 ),
144 ),
145 );
146
147
148 $contribution = CRM_Contribute_BAO_Contribution::create($params, $ids);
149
150 // Check that the custom field value is saved
151 $customValueParams = array(
152 'entityID' => $contribution->id,
153 'custom_' . $customField->id => 1,
154 );
155 $values = CRM_Core_BAO_CustomValueTable::getValues($customValueParams);
156 $this->assertEquals('Test custom value', $values['custom_' . $customField->id], 'Check the custom field value');
157
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.');
160
161 $this->contributionDelete($contribution->id);
162 Custom::deleteField($customField);
163 Custom::deleteGroup($customGroup);
164 Contact::delete($contactId);
165 }
166
167 /**
168 * deleteContribution() method
169 */
170 function testDeleteContribution() {
171 $contactId = Contact::createIndividual();
172 $ids = array('contribution' => NULL);
173
174 $params = array(
175 'contact_id' => $contactId,
176 'currency' => 'USD',
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',
183 'id' => NULL,
184 'non_deductible_amount' => 0.00,
185 'total_amount' => 200.00,
186 'fee_amount' => 5,
187 'net_amount' => 195,
188 'trxn_id' => '33ereerwww322323',
189 'invoice_id' => '33ed39c9e9ee6ef6031621ce0eafe6da70',
190 'thankyou_date' => '20080522',
191 );
192
193 $contribution = CRM_Contribute_BAO_Contribution::create($params, $ids);
194
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.');
197
198 $contributiondelete = CRM_Contribute_BAO_Contribution::deleteContribution($contribution->id);
199
200 $this->assertDBNull('CRM_Contribute_DAO_Contribution', $contribution->trxn_id,
201 'id', 'trxn_id', 'Database check for deleted Contribution.'
202 );
203 Contact::delete($contactId);
204 }
205
206 /**
207 * create honor-contact method
208 */
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";
213
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;
220 }
221
222 $params = array(
223 'prefix_id' => 3,
224 'first_name' => $firstName,
225 'last_name' => $lastName,
226 'email-1' => $email,
227 );
228 $softParam = array('soft_credit_type_id' => 1);
229
230 $honoreeContactId = CRM_Contact_BAO_Contact::createProfileContact($params, CRM_Core_DAO::$_nullArray,
231 NULL, NULL, $honoreeProfileId
232 );
233
234 $this->assertDBCompareValue('CRM_Contact_DAO_Contact', $honoreeContactId, 'first_name', 'id', $firstName,
235 'Database check for created honor contact record.'
236 );
237 //create contribution on behalf of honary.
238
239 $contactId = Contact::createIndividual();
240 $softParam['contact_id'] = $honoreeContactId;
241
242 $ids = array('contribution' => NULL);
243 $param = array(
244 'contact_id' => $contactId,
245 'currency' => 'USD',
246 'financial_type_id' => 4,
247 'contribution_status_id' => 1,
248 'receive_date' => date('Ymd'),
249 'total_amount' => 66,
250 );
251
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;
257
258 //Create Soft Contribution for honoree contact
259 CRM_Contribute_BAO_ContributionSoft::add($softParam);
260
261 $this->assertDBCompareValue('CRM_Contribute_DAO_ContributionSoft', $id, 'contact_id',
262 'contribution_id', $honoreeContactId, 'Check DB for honor contact of the contribution'
263 );
264 //get honory information
265 $getHonorContact = CRM_Contribute_BAO_Contribution::getHonorContacts($honoreeContactId);
266
267 $this->assertDBCompareValue('CRM_Contact_DAO_Contact', $honoreeContactId, 'first_name', 'id', $firstName,
268 'Database check for created honor contact record.'
269 );
270
271 //get annual contribution information
272 $annual = CRM_Contribute_BAO_Contribution::annual($contactId);
273
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'
278 );
279
280 //Delete honor contact
281 Contact::delete($honoreeContactId);
282
283 //Delete Contribution record
284 $this->contributionDelete($contribution->id);
285
286 //Delete contributor contact
287 Contact::delete($contactId);
288 }
289
290 /**
291 * display sort name during
292 * contribution batch update through profile
293 * sortName();
294 */
295 function testsortName() {
296 $params = array(
297 'first_name' => 'Shane',
298 'last_name' => 'Whatson',
299 'contact_type' => 'Individual',
300 );
301
302 $contact = CRM_Contact_BAO_Contact::add($params);
303
304 //Now check $contact is object of contact DAO..
305 $this->assertInstanceOf('CRM_Contact_DAO_Contact', $contact, 'Check for created object');
306
307 $contactId = $contact->id;
308
309 $ids = array('contribution' => NULL);
310
311 $param = array(
312 'contact_id' => $contactId,
313 'currency' => 'USD',
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',
320 'id' => NULL,
321 'non_deductible_amount' => 0.00,
322 'total_amount' => 300.00,
323 'fee_amount' => 5,
324 'net_amount' => 295,
325 'trxn_id' => '22ereerwww323',
326 'invoice_id' => '22ed39c9e9ee621ce0eafe6da70',
327 'thankyou_date' => '20080522',
328 );
329
330 $contribution = CRM_Contribute_BAO_Contribution::create($param, $ids);
331
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.');
334
335 //display sort name during batch update
336 $sortName = CRM_Contribute_BAO_Contribution::sortName($contribution->id);
337
338 $this->assertEquals('Whatson, Shane', $sortName, 'Check for sort name.');
339
340 //Delete Contribution
341 $this->contributionDelete($contribution->id);
342 //Delete Contact
343 Contact::delete($contactId);
344 }
345
346 /**
347 * Add premium during online Contribution
348 *
349 * AddPremium();
350 */
351 function testAddPremium() {
352 $contactId = Contact::createIndividual();
353
354 $ids = array(
355 'premium' => NULL,
356 );
357
358
359 $params = array(
360 'name' => 'TEST Premium',
361 'sku' => 111,
362 'imageOption' => 'noImage',
363 'MAX_FILE_SIZE' => 2097152,
364 'price' => 100.00,
365 'cost' => 90.00,
366 'min_contribution' => 100,
367 'is_active' => 1,
368 );
369 $premium = CRM_Contribute_BAO_ManagePremiums::add($params, $ids);
370
371 $this->assertEquals('TEST Premium', $premium->name, 'Check for premium name.');
372
373 $ids = array('contribution' => NULL);
374
375 $param = array(
376 'contact_id' => $contactId,
377 'currency' => 'USD',
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',
384 'id' => NULL,
385 'non_deductible_amount' => 0.00,
386 'total_amount' => 300.00,
387 'fee_amount' => 5,
388 'net_amount' => 295,
389 'trxn_id' => '33erdfrwvw434',
390 'invoice_id' => '98ed34f7u9hh672ce0eafe8fb92',
391 'thankyou_date' => '20080522',
392 );
393
394 $contribution = CRM_Contribute_BAO_Contribution::create($param, $ids);
395
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.');
398
399 //parameter for adding premium to contribution
400 $data = array(
401 'product_id' => $premium->id,
402 'contribution_id' => $contribution->id,
403 'product_option' => NULL,
404 'quantity' => 1,
405 );
406 $contributionProduct = CRM_Contribute_BAO_Contribution::addPremium($data);
407 $this->assertEquals($contributionProduct->product_id, $premium->id, 'Check for Product id .');
408
409 //Delete Product
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.'
413 );
414
415 //Delete Contribution
416 $this->contributionDelete($contribution->id);
417 //Delete Contact
418 Contact::delete($contactId);
419 }
420
421 /**
422 * Check duplicate contribution id
423 * during the contribution import
424 * checkDuplicateIds();
425 */
426 function testcheckDuplicateIds() {
427 $contactId = Contact::createIndividual();
428
429 $ids = array('contribution' => NULL);
430
431 $param = array(
432 'contact_id' => $contactId,
433 'currency' => 'USD',
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',
440 'id' => NULL,
441 'non_deductible_amount' => 0.00,
442 'total_amount' => 300.00,
443 'fee_amount' => 5,
444 'net_amount' => 295,
445 'trxn_id' => '76ereeswww835',
446 'invoice_id' => '93ed39a9e9hd621bs0eafe3da82',
447 'thankyou_date' => '20080522',
448 );
449
450 $contribution = CRM_Contribute_BAO_Contribution::create($param, $ids);
451
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.');
454 $data = array(
455 'id' => $contribution->id,
456 'trxn_id' => $contribution->trxn_id,
457 'invoice_id' => $contribution->invoice_id,
458 );
459 $contributionID = CRM_Contribute_BAO_Contribution::checkDuplicateIds($data);
460 $this->assertEquals($contributionID, $contribution->id, 'Check for duplicate transcation id .');
461
462 // Delete Contribution
463 $this->contributionDelete($contribution->id);
464 // Delete Contact
465 Contact::delete($contactId);
466 }
467 }
468
469