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