Merge pull request #4375 from totten/master-mysqli-compat
[civicrm-core.git] / tests / phpunit / CRM / Contribute / BAO / ContributionTest.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
06a1bc01 4 | CiviCRM version 4.5 |
6a488035 5 +--------------------------------------------------------------------+
06a1bc01 6 | Copyright CiviCRM LLC (c) 2004-2014 |
6a488035
TO
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
29require_once 'CiviTest/CiviUnitTestCase.php';
30require_once 'CiviTest/Contact.php';
31require_once 'CiviTest/Custom.php';
4cbe18b8
EM
32
33/**
34 * Class CRM_Contribute_BAO_ContributionTest
35 */
6a488035 36class CRM_Contribute_BAO_ContributionTest extends CiviUnitTestCase {
4cbe18b8
EM
37 /**
38 * @return array
39 */
6a488035
TO
40 function get_info() {
41 return array(
42 'name' => 'Contribution BAOs',
43 'description' => 'Test all Contribute_BAO_Contribution methods.',
44 'group' => 'CiviCRM BAO Tests',
45 );
46 }
47
48 function setUp() {
49 parent::setUp();
6a488035
TO
50 }
51
52 function teardown() {
6a488035
TO
53 }
54
55 /**
100fef9d 56 * Create() method (create and update modes)
6a488035
TO
57 */
58 function testCreate() {
59 $contactId = Contact::createIndividual();
60 $ids = array('contribution' => NULL);
61
62 $params = array(
63 'contact_id' => $contactId,
64 'currency' => 'USD',
65 'financial_type_id' => 1,
66 'contribution_status_id' => 1,
67 'payment_instrument_id' => 1,
68 'source' => 'STUDENT',
69 'receive_date' => '20080522000000',
70 'receipt_date' => '20080522000000',
71 'non_deductible_amount' => 0.00,
72 'total_amount' => 200.00,
73 'fee_amount' => 5,
74 'net_amount' => 195,
75 'trxn_id' => '22ereerwww444444',
76 'invoice_id' => '86ed39c9e9ee6ef6031621ce0eafe7eb81',
77 'thankyou_date' => '20080522',
78 );
79
80 $contribution = CRM_Contribute_BAO_Contribution::create($params, $ids);
81
82 $this->assertEquals($params['trxn_id'], $contribution->trxn_id, 'Check for transcation id creation.');
83 $this->assertEquals($contactId, $contribution->contact_id, 'Check for contact id creation.');
84
85 //update contribution amount
86 $ids = array('contribution' => $contribution->id);
87 $params['fee_amount'] = 10;
88 $params['net_amount'] = 190;
89
90 $contribution = CRM_Contribute_BAO_Contribution::create($params, $ids);
91
92 $this->assertEquals($params['trxn_id'], $contribution->trxn_id, 'Check for transcation id .');
93 $this->assertEquals($params['net_amount'], $contribution->net_amount, 'Check for Amount updation.');
94
95 //Delete Contribution
96 $this->contributionDelete($contribution->id);
97
98 //Delete Contact
99 Contact::delete($contactId);
100 }
101
102 /**
100fef9d 103 * Create() method with custom data
6a488035
TO
104 */
105 function testCreateWithCustomData() {
106 $contactId = Contact::createIndividual();
107 $ids = array('contribution' => NULL);
108
109 //create custom data
110 $customGroup = Custom::createGroup(array(), 'Contribution');
111 $fields = array(
112 'label' => 'testFld',
113 'data_type' => 'String',
114 'html_type' => 'Text',
115 'is_active' => 1,
116 'custom_group_id' => $customGroup->id,
117 );
118 $customField = CRM_Core_BAO_CustomField::create($fields);
119
120 $params = array(
121 'contact_id' => $contactId,
122 'currency' => 'USD',
e6ff1593 123 'financial_type_id' => 1,
6a488035
TO
124 'contribution_status_id' => 1,
125 'payment_instrument_id' => 1,
126 'source' => 'STUDENT',
127 'receive_date' => '20080522000000',
128 'receipt_date' => '20080522000000',
129 'id' => NULL,
130 'non_deductible_amount' => 0.00,
131 'total_amount' => 200.00,
132 'fee_amount' => 5,
133 'net_amount' => 195,
134 'trxn_id' => '22ereerwww322323',
135 'invoice_id' => '22ed39c9e9ee6ef6031621ce0eafe6da70',
136 'thankyou_date' => '20080522',
137 );
138
139
140 $params['custom'] = array(
141 $customField->id => array(
142 -1 => array(
143 'value' => 'Test custom value',
144 'type' => 'String',
145 'custom_field_id' => $customField->id,
146 'custom_group_id' => $customGroup->id,
147 'table_name' => $customGroup->table_name,
148 'column_name' => $customField->column_name,
149 'file_id' => NULL,
150 ),
151 ),
152 );
153
154
155 $contribution = CRM_Contribute_BAO_Contribution::create($params, $ids);
156
157 // Check that the custom field value is saved
158 $customValueParams = array(
159 'entityID' => $contribution->id,
160 'custom_' . $customField->id => 1,
161 );
162 $values = CRM_Core_BAO_CustomValueTable::getValues($customValueParams);
163 $this->assertEquals('Test custom value', $values['custom_' . $customField->id], 'Check the custom field value');
164
165 $this->assertEquals($params['trxn_id'], $contribution->trxn_id, 'Check for transcation id creation.');
166 $this->assertEquals($contactId, $contribution->contact_id, 'Check for contact id for Conribution.');
167
168 $this->contributionDelete($contribution->id);
169 Custom::deleteField($customField);
170 Custom::deleteGroup($customGroup);
171 Contact::delete($contactId);
172 }
173
174 /**
100fef9d 175 * DeleteContribution() method
6a488035
TO
176 */
177 function testDeleteContribution() {
178 $contactId = Contact::createIndividual();
179 $ids = array('contribution' => NULL);
180
181 $params = array(
182 'contact_id' => $contactId,
183 'currency' => 'USD',
e6ff1593 184 'financial_type_id' => 1,
6a488035
TO
185 'contribution_status_id' => 1,
186 'payment_instrument_id' => 1,
187 'source' => 'STUDENT',
188 'receive_date' => '20080522000000',
189 'receipt_date' => '20080522000000',
190 'id' => NULL,
191 'non_deductible_amount' => 0.00,
192 'total_amount' => 200.00,
193 'fee_amount' => 5,
194 'net_amount' => 195,
195 'trxn_id' => '33ereerwww322323',
196 'invoice_id' => '33ed39c9e9ee6ef6031621ce0eafe6da70',
197 'thankyou_date' => '20080522',
198 );
199
200 $contribution = CRM_Contribute_BAO_Contribution::create($params, $ids);
201
202 $this->assertEquals($params['trxn_id'], $contribution->trxn_id, 'Check for transcation id creation.');
203 $this->assertEquals($contactId, $contribution->contact_id, 'Check for contact id creation.');
204
205 $contributiondelete = CRM_Contribute_BAO_Contribution::deleteContribution($contribution->id);
206
207 $this->assertDBNull('CRM_Contribute_DAO_Contribution', $contribution->trxn_id,
208 'id', 'trxn_id', 'Database check for deleted Contribution.'
209 );
210 Contact::delete($contactId);
211 }
212
213 /**
100fef9d 214 * Create honor-contact method
6a488035
TO
215 */
216 function testcreateAndGetHonorContact() {
217 $firstName = 'John_' . substr(sha1(rand()), 0, 7);
218 $lastName = 'Smith_' . substr(sha1(rand()), 0, 7);
219 $email = "{$firstName}.{$lastName}@example.com";
220
8381af80 221 //Get profile id of name honoree_individual used to create profileContact
222 $honoreeProfileId = NULL;
223 $ufGroupDAO = new CRM_Core_DAO_UFGroup();
224 $ufGroupDAO->name = 'honoree_individual';
225 if ($ufGroupDAO->find(TRUE)) {
226 $honoreeProfileId = $ufGroupDAO->id;
227 }
228
6a488035 229 $params = array(
8381af80 230 'prefix_id' => 3,
231 'first_name' => $firstName,
232 'last_name' => $lastName,
233 'email-1' => $email,
6a488035 234 );
8381af80 235 $softParam = array('soft_credit_type_id' => 1);
236
237 $honoreeContactId = CRM_Contact_BAO_Contact::createProfileContact($params, CRM_Core_DAO::$_nullArray,
238 NULL, NULL, $honoreeProfileId
239 );
6a488035 240
8381af80 241 $this->assertDBCompareValue('CRM_Contact_DAO_Contact', $honoreeContactId, 'first_name', 'id', $firstName,
6a488035
TO
242 'Database check for created honor contact record.'
243 );
244 //create contribution on behalf of honary.
245
246 $contactId = Contact::createIndividual();
8381af80 247 $softParam['contact_id'] = $honoreeContactId;
6a488035
TO
248
249 $ids = array('contribution' => NULL);
250 $param = array(
251 'contact_id' => $contactId,
252 'currency' => 'USD',
e6ff1593 253 'financial_type_id' => 4,
6a488035
TO
254 'contribution_status_id' => 1,
255 'receive_date' => date('Ymd'),
256 'total_amount' => 66,
6a488035
TO
257 );
258
259 $contribution = CRM_Contribute_BAO_Contribution::create($param, $ids);
260 $id = $contribution->id;
8381af80 261 $softParam['contribution_id'] = $id;
262 $softParam['currency'] = $contribution->currency;
263 $softParam['amount'] = $contribution->total_amount;
264
265 //Create Soft Contribution for honoree contact
266 CRM_Contribute_BAO_ContributionSoft::add($softParam);
267
268 $this->assertDBCompareValue('CRM_Contribute_DAO_ContributionSoft', $id, 'contact_id',
269 'contribution_id', $honoreeContactId, 'Check DB for honor contact of the contribution'
6a488035
TO
270 );
271 //get honory information
8381af80 272 $getHonorContact = CRM_Contribute_BAO_Contribution::getHonorContacts($honoreeContactId);
6a488035 273
8381af80 274 $this->assertDBCompareValue('CRM_Contact_DAO_Contact', $honoreeContactId, 'first_name', 'id', $firstName,
6a488035
TO
275 'Database check for created honor contact record.'
276 );
277
278 //get annual contribution information
279 $annual = CRM_Contribute_BAO_Contribution::annual($contactId);
280
281 $config = CRM_Core_Config::singleton();
282 $currencySymbol = CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_Currency',$config->defaultCurrency,'symbol','name') ;
283 $this->assertDBCompareValue('CRM_Contribute_DAO_Contribution', $id, 'total_amount',
284 'id', ltrim($annual[2], $currencySymbol), 'Check DB for total amount of the contribution'
285 );
286
287 //Delete honor contact
8381af80 288 Contact::delete($honoreeContactId);
6a488035
TO
289
290 //Delete Contribution record
291 $this->contributionDelete($contribution->id);
292
293 //Delete contributor contact
294 Contact::delete($contactId);
295 }
296
297 /**
100fef9d 298 * Display sort name during
6a488035
TO
299 * contribution batch update through profile
300 * sortName();
301 */
302 function testsortName() {
303 $params = array(
304 'first_name' => 'Shane',
305 'last_name' => 'Whatson',
306 'contact_type' => 'Individual',
307 );
308
309 $contact = CRM_Contact_BAO_Contact::add($params);
310
311 //Now check $contact is object of contact DAO..
312 $this->assertInstanceOf('CRM_Contact_DAO_Contact', $contact, 'Check for created object');
313
314 $contactId = $contact->id;
315
316 $ids = array('contribution' => NULL);
317
318 $param = array(
319 'contact_id' => $contactId,
320 'currency' => 'USD',
e6ff1593 321 'financial_type_id' => 1,
6a488035
TO
322 'contribution_status_id' => 1,
323 'payment_instrument_id' => 1,
324 'source' => 'STUDENT',
325 'receive_date' => '20080522000000',
326 'receipt_date' => '20080522000000',
327 'id' => NULL,
328 'non_deductible_amount' => 0.00,
329 'total_amount' => 300.00,
330 'fee_amount' => 5,
331 'net_amount' => 295,
332 'trxn_id' => '22ereerwww323',
333 'invoice_id' => '22ed39c9e9ee621ce0eafe6da70',
334 'thankyou_date' => '20080522',
335 );
336
337 $contribution = CRM_Contribute_BAO_Contribution::create($param, $ids);
338
339 $this->assertEquals($param['trxn_id'], $contribution->trxn_id, 'Check for transcation id creation.');
340 $this->assertEquals($contactId, $contribution->contact_id, 'Check for contact id creation.');
341
342 //display sort name during batch update
343 $sortName = CRM_Contribute_BAO_Contribution::sortName($contribution->id);
344
345 $this->assertEquals('Whatson, Shane', $sortName, 'Check for sort name.');
346
347 //Delete Contribution
348 $this->contributionDelete($contribution->id);
349 //Delete Contact
350 Contact::delete($contactId);
351 }
352
353 /**
354 * Add premium during online Contribution
355 *
356 * AddPremium();
357 */
358 function testAddPremium() {
359 $contactId = Contact::createIndividual();
360
361 $ids = array(
362 'premium' => NULL,
363 );
364
365
366 $params = array(
367 'name' => 'TEST Premium',
368 'sku' => 111,
369 'imageOption' => 'noImage',
370 'MAX_FILE_SIZE' => 2097152,
371 'price' => 100.00,
372 'cost' => 90.00,
373 'min_contribution' => 100,
374 'is_active' => 1,
375 );
376 $premium = CRM_Contribute_BAO_ManagePremiums::add($params, $ids);
377
378 $this->assertEquals('TEST Premium', $premium->name, 'Check for premium name.');
379
380 $ids = array('contribution' => NULL);
381
382 $param = array(
383 'contact_id' => $contactId,
384 'currency' => 'USD',
e6ff1593 385 'financial_type_id' => 1,
6a488035
TO
386 'contribution_status_id' => 1,
387 'payment_instrument_id' => 1,
388 'source' => 'STUDENT',
389 'receive_date' => '20080522000000',
390 'receipt_date' => '20080522000000',
391 'id' => NULL,
392 'non_deductible_amount' => 0.00,
393 'total_amount' => 300.00,
394 'fee_amount' => 5,
395 'net_amount' => 295,
396 'trxn_id' => '33erdfrwvw434',
397 'invoice_id' => '98ed34f7u9hh672ce0eafe8fb92',
398 'thankyou_date' => '20080522',
399 );
400
401 $contribution = CRM_Contribute_BAO_Contribution::create($param, $ids);
402
403 $this->assertEquals($param['trxn_id'], $contribution->trxn_id, 'Check for transcation id creation.');
404 $this->assertEquals($contactId, $contribution->contact_id, 'Check for contact id creation.');
405
406 //parameter for adding premium to contribution
407 $data = array(
408 'product_id' => $premium->id,
409 'contribution_id' => $contribution->id,
410 'product_option' => NULL,
411 'quantity' => 1,
412 );
413 $contributionProduct = CRM_Contribute_BAO_Contribution::addPremium($data);
414 $this->assertEquals($contributionProduct->product_id, $premium->id, 'Check for Product id .');
415
416 //Delete Product
417 CRM_Contribute_BAO_ManagePremiums::del($premium->id);
418 $this->assertDBNull('CRM_Contribute_DAO_Product', $premium->name,
419 'id', 'name', 'Database check for deleted Product.'
420 );
421
422 //Delete Contribution
423 $this->contributionDelete($contribution->id);
424 //Delete Contact
425 Contact::delete($contactId);
426 }
427
428 /**
429 * Check duplicate contribution id
430 * during the contribution import
431 * checkDuplicateIds();
432 */
433 function testcheckDuplicateIds() {
434 $contactId = Contact::createIndividual();
435
436 $ids = array('contribution' => NULL);
437
438 $param = array(
439 'contact_id' => $contactId,
440 'currency' => 'USD',
e6ff1593 441 'financial_type_id' => 1,
6a488035
TO
442 'contribution_status_id' => 1,
443 'payment_instrument_id' => 1,
444 'source' => 'STUDENT',
445 'receive_date' => '20080522000000',
446 'receipt_date' => '20080522000000',
447 'id' => NULL,
448 'non_deductible_amount' => 0.00,
449 'total_amount' => 300.00,
450 'fee_amount' => 5,
451 'net_amount' => 295,
452 'trxn_id' => '76ereeswww835',
453 'invoice_id' => '93ed39a9e9hd621bs0eafe3da82',
454 'thankyou_date' => '20080522',
455 );
456
457 $contribution = CRM_Contribute_BAO_Contribution::create($param, $ids);
458
459 $this->assertEquals($param['trxn_id'], $contribution->trxn_id, 'Check for transcation id creation.');
460 $this->assertEquals($contactId, $contribution->contact_id, 'Check for contact id creation.');
461 $data = array(
462 'id' => $contribution->id,
463 'trxn_id' => $contribution->trxn_id,
464 'invoice_id' => $contribution->invoice_id,
465 );
466 $contributionID = CRM_Contribute_BAO_Contribution::checkDuplicateIds($data);
467 $this->assertEquals($contributionID, $contribution->id, 'Check for duplicate transcation id .');
468
469 // Delete Contribution
470 $this->contributionDelete($contribution->id);
471 // Delete Contact
472 Contact::delete($contactId);
473 }
474}
475
476