Merge pull request #3837 from eileenmcnaughton/CRM-15113
[civicrm-core.git] / tests / phpunit / CRM / Contribute / BAO / ContributionTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.4 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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 * createHonorContact();
209 */
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";
214
215 $honorId = NULL;
216 $params = array(
217 'honor_type_id' => 1,
218 'honor_prefix_id' => 3,
219 'honor_first_name' => $firstName,
220 'honor_last_name' => $lastName,
221 'honor_email' => $email,
222 );
223 $contact = CRM_Contribute_BAO_Contribution::createHonorContact($params, $honorId);
224
225 $this->assertDBCompareValue('CRM_Contact_DAO_Contact', $contact, 'first_name', 'id', $firstName,
226 'Database check for created honor contact record.'
227 );
228 //create contribution on behalf of honary.
229
230 $contactId = Contact::createIndividual();
231
232 $ids = array('contribution' => NULL);
233 $param = array(
234 'contact_id' => $contactId,
235 'currency' => 'USD',
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,
242 );
243
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'
248 );
249 //get honory information
250 $getHonorContact = CRM_Contribute_BAO_Contribution::getHonorContacts($contact);
251
252 $this->assertDBCompareValue('CRM_Contact_DAO_Contact', $contact, 'first_name', 'id', $firstName,
253 'Database check for created honor contact record.'
254 );
255
256 //get annual contribution information
257 $annual = CRM_Contribute_BAO_Contribution::annual($contactId);
258
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'
263 );
264
265 //Delete honor contact
266 Contact::delete($contact);
267
268 //Delete Contribution record
269 $this->contributionDelete($contribution->id);
270
271 //Delete contributor contact
272 Contact::delete($contactId);
273 }
274
275 /**
276 * display sort name during
277 * contribution batch update through profile
278 * sortName();
279 */
280 function testsortName() {
281 $params = array(
282 'first_name' => 'Shane',
283 'last_name' => 'Whatson',
284 'contact_type' => 'Individual',
285 );
286
287 $contact = CRM_Contact_BAO_Contact::add($params);
288
289 //Now check $contact is object of contact DAO..
290 $this->assertInstanceOf('CRM_Contact_DAO_Contact', $contact, 'Check for created object');
291
292 $contactId = $contact->id;
293
294 $ids = array('contribution' => NULL);
295
296 $param = array(
297 'contact_id' => $contactId,
298 'currency' => 'USD',
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',
305 'id' => NULL,
306 'non_deductible_amount' => 0.00,
307 'total_amount' => 300.00,
308 'fee_amount' => 5,
309 'net_amount' => 295,
310 'trxn_id' => '22ereerwww323',
311 'invoice_id' => '22ed39c9e9ee621ce0eafe6da70',
312 'thankyou_date' => '20080522',
313 );
314
315 $contribution = CRM_Contribute_BAO_Contribution::create($param, $ids);
316
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.');
319
320 //display sort name during batch update
321 $sortName = CRM_Contribute_BAO_Contribution::sortName($contribution->id);
322
323 $this->assertEquals('Whatson, Shane', $sortName, 'Check for sort name.');
324
325 //Delete Contribution
326 $this->contributionDelete($contribution->id);
327 //Delete Contact
328 Contact::delete($contactId);
329 }
330
331 /**
332 * Add premium during online Contribution
333 *
334 * AddPremium();
335 */
336 function testAddPremium() {
337 $contactId = Contact::createIndividual();
338
339 $ids = array(
340 'premium' => NULL,
341 );
342
343
344 $params = array(
345 'name' => 'TEST Premium',
346 'sku' => 111,
347 'imageOption' => 'noImage',
348 'MAX_FILE_SIZE' => 2097152,
349 'price' => 100.00,
350 'cost' => 90.00,
351 'min_contribution' => 100,
352 'is_active' => 1,
353 );
354 $premium = CRM_Contribute_BAO_ManagePremiums::add($params, $ids);
355
356 $this->assertEquals('TEST Premium', $premium->name, 'Check for premium name.');
357
358 $ids = array('contribution' => NULL);
359
360 $param = array(
361 'contact_id' => $contactId,
362 'currency' => 'USD',
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',
369 'id' => NULL,
370 'non_deductible_amount' => 0.00,
371 'total_amount' => 300.00,
372 'fee_amount' => 5,
373 'net_amount' => 295,
374 'trxn_id' => '33erdfrwvw434',
375 'invoice_id' => '98ed34f7u9hh672ce0eafe8fb92',
376 'thankyou_date' => '20080522',
377 );
378
379 $contribution = CRM_Contribute_BAO_Contribution::create($param, $ids);
380
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.');
383
384 //parameter for adding premium to contribution
385 $data = array(
386 'product_id' => $premium->id,
387 'contribution_id' => $contribution->id,
388 'product_option' => NULL,
389 'quantity' => 1,
390 );
391 $contributionProduct = CRM_Contribute_BAO_Contribution::addPremium($data);
392 $this->assertEquals($contributionProduct->product_id, $premium->id, 'Check for Product id .');
393
394 //Delete Product
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.'
398 );
399
400 //Delete Contribution
401 $this->contributionDelete($contribution->id);
402 //Delete Contact
403 Contact::delete($contactId);
404 }
405
406 /**
407 * Check duplicate contribution id
408 * during the contribution import
409 * checkDuplicateIds();
410 */
411 function testcheckDuplicateIds() {
412 $contactId = Contact::createIndividual();
413
414 $ids = array('contribution' => NULL);
415
416 $param = array(
417 'contact_id' => $contactId,
418 'currency' => 'USD',
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',
425 'id' => NULL,
426 'non_deductible_amount' => 0.00,
427 'total_amount' => 300.00,
428 'fee_amount' => 5,
429 'net_amount' => 295,
430 'trxn_id' => '76ereeswww835',
431 'invoice_id' => '93ed39a9e9hd621bs0eafe3da82',
432 'thankyou_date' => '20080522',
433 );
434
435 $contribution = CRM_Contribute_BAO_Contribution::create($param, $ids);
436
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.');
439 $data = array(
440 'id' => $contribution->id,
441 'trxn_id' => $contribution->trxn_id,
442 'invoice_id' => $contribution->invoice_id,
443 );
444 $contributionID = CRM_Contribute_BAO_Contribution::checkDuplicateIds($data);
445 $this->assertEquals($contributionID, $contribution->id, 'Check for duplicate transcation id .');
446
447 // Delete Contribution
448 $this->contributionDelete($contribution->id);
449 // Delete Contact
450 Contact::delete($contactId);
451 }
452 }
453
454