CRM-20276 update line item financial amount when updating line item.
[civicrm-core.git] / tests / phpunit / CRM / Contribute / Form / ContributionTest.php
CommitLineData
a084385f
EM
1<?php
2/*
3 +--------------------------------------------------------------------+
81621fee 4 | CiviCRM version 4.7 |
a084385f 5 +--------------------------------------------------------------------+
15a4309a 6 | Copyright CiviCRM LLC (c) 2004-2017 |
a084385f
EM
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
a084385f
EM
28/**
29 * Test APIv3 civicrm_contribute_* functions
30 *
31 * @package CiviCRM_APIv3
32 * @subpackage API_Contribution
acb109b7 33 * @group headless
a084385f
EM
34 */
35class CRM_Contribute_Form_ContributionTest extends CiviUnitTestCase {
36
37 /**
38 * Assume empty database with just civicrm_data.
39 */
40 protected $_individualId;
41 protected $_contribution;
42 protected $_financialTypeId = 1;
43 protected $_apiversion;
44 protected $_entity = 'Contribution';
45 protected $_params;
46 protected $_ids = array();
47 protected $_pageParams = array();
48
49 /**
50 * Parameters to create payment processor.
51 *
52 * @var array
53 */
54 protected $_processorParams = array();
55
56 /**
57 * ID of created event.
58 *
59 * @var int
60 */
61 protected $_eventID;
62
63 /**
64 * Payment instrument mapping.
65 *
66 * @var array
67 */
68 protected $paymentInstruments = array();
69
3e6a1f4a
EM
70 /**
71 * Products.
72 *
73 * @var array
74 */
75 protected $products = array();
76
39f47c0d
EM
77 /**
78 * Dummy payment processor.
79 *
7758bd2b 80 * @var CRM_Core_Payment_Dummy
39f47c0d 81 */
7758bd2b 82 protected $paymentProcessor;
39f47c0d 83
22e39333 84 /**
85 * Payment processor ID.
86 *
87 * @var int
88 */
89 protected $paymentProcessorID;
90
a084385f
EM
91 /**
92 * Setup function.
93 */
94 public function setUp() {
39f47c0d 95 $this->_apiversion = 3;
a084385f 96 parent::setUp();
39f47c0d 97 $this->createLoggedInUser();
a084385f 98
a084385f 99 $this->_individualId = $this->individualCreate();
a084385f
EM
100 $this->_params = array(
101 'contact_id' => $this->_individualId,
102 'receive_date' => '20120511',
103 'total_amount' => 100.00,
104 'financial_type_id' => $this->_financialTypeId,
105 'non_deductible_amount' => 10.00,
106 'fee_amount' => 5.00,
107 'net_amount' => 95.00,
108 'source' => 'SSF',
109 'contribution_status_id' => 1,
110 );
111 $this->_processorParams = array(
112 'domain_id' => 1,
113 'name' => 'Dummy',
114 'payment_processor_type_id' => 10,
115 'financial_account_id' => 12,
116 'is_active' => 1,
117 'user_name' => '',
118 'url_site' => 'http://dummy.com',
119 'url_recur' => 'http://dummy.com',
120 'billing_mode' => 1,
121 );
7758bd2b 122
a084385f
EM
123 $instruments = $this->callAPISuccess('contribution', 'getoptions', array('field' => 'payment_instrument_id'));
124 $this->paymentInstruments = $instruments['values'];
3e6a1f4a
EM
125 $product1 = $this->callAPISuccess('product', 'create', array(
126 'name' => 'Smurf',
127 'options' => 'brainy smurf, clumsy smurf, papa smurf',
128 ));
129
130 $this->products[] = $product1['values'][$product1['id']];
22e39333 131 $this->paymentProcessor = $this->dummyProcessorCreate();
132 $processor = $this->paymentProcessor->getPaymentProcessor();
133 $this->paymentProcessorID = $processor['id'];
a084385f
EM
134 }
135
136 /**
137 * Clean up after each test.
138 */
139 public function tearDown() {
140 $this->quickCleanUpFinancialEntities();
739a8336 141 $this->quickCleanup(array('civicrm_note', 'civicrm_uf_match', 'civicrm_address'));
a084385f
EM
142 }
143
144 /**
145 * Test the submit function on the contribution page.
146 */
147 public function testSubmit() {
148 $form = new CRM_Contribute_Form_Contribution();
149 $form->testSubmit(array(
150 'total_amount' => 50,
151 'financial_type_id' => 1,
152 'receive_date' => '04/21/2015',
153 'receive_date_time' => '11:27PM',
154 'contact_id' => $this->_individualId,
155 'payment_instrument_id' => array_search('Check', $this->paymentInstruments),
5e27919e 156 'contribution_status_id' => 1,
31760f81
EM
157 ),
158 CRM_Core_Action::ADD);
2b308818
DG
159 $contribution = $this->callAPISuccessGetSingle('Contribution', array('contact_id' => $this->_individualId));
160 $this->assertEmpty($contribution['amount_level']);
a084385f
EM
161 }
162
163 /**
164 * Test the submit function on the contribution page.
165 */
166 public function testSubmitCreditCard() {
167 $form = new CRM_Contribute_Form_Contribution();
168 $form->testSubmit(array(
169 'total_amount' => 50,
170 'financial_type_id' => 1,
171 'receive_date' => '04/21/2015',
172 'receive_date_time' => '11:27PM',
173 'contact_id' => $this->_individualId,
174 'payment_instrument_id' => array_search('Credit Card', $this->paymentInstruments),
5e27919e 175 'contribution_status_id' => 1,
31760f81 176 ), CRM_Core_Action::ADD);
7758bd2b
EM
177 $this->callAPISuccessGetCount('Contribution', array(
178 'contact_id' => $this->_individualId,
179 'contribution_status_id' => 'Completed',
180 ),
181 1);
a084385f
EM
182 }
183
f8453bef 184 /**
185 * Test the submit function on the contribution page.
186 */
187 public function testSubmitCreditCardPayPal() {
188 $form = new CRM_Contribute_Form_Contribution();
189 $paymentProcessorID = $this->paymentProcessorCreate(array('is_test' => 0));
190 $form->_mode = 'Live';
16f3bd02 191 $error = FALSE;
f8453bef 192 try {
193 $form->testSubmit(array(
194 'total_amount' => 50,
195 'financial_type_id' => 1,
196 'receive_date' => '04/21/2015',
197 'receive_date_time' => '11:27PM',
198 'contact_id' => $this->_individualId,
f8453bef 199 'contribution_status_id' => 1,
200 'credit_card_number' => 4444333322221111,
201 'cvv2' => 123,
202 'credit_card_exp_date' => array(
203 'M' => 9,
204 'Y' => 2025,
205 ),
206 'credit_card_type' => 'Visa',
207 'billing_first_name' => 'Junko',
208 'billing_middle_name' => '',
209 'billing_last_name' => 'Adams',
210 'billing_street_address-5' => '790L Lincoln St S',
211 'billing_city-5' => 'Maryknoll',
212 'billing_state_province_id-5' => 1031,
213 'billing_postal_code-5' => 10545,
214 'billing_country_id-5' => 1228,
215 'frequency_interval' => 1,
216 'frequency_unit' => 'month',
217 'installments' => '',
218 'hidden_AdditionalDetail' => 1,
219 'hidden_Premium' => 1,
220 'from_email_address' => '"civi45" <civi45@civicrm.com>',
221 'receipt_date' => '',
222 'receipt_date_time' => '',
223 'payment_processor_id' => $paymentProcessorID,
224 'currency' => 'USD',
dc0ca56c 225 'source' => 'bob sled race',
f8453bef 226 ), CRM_Core_Action::ADD);
227 }
228 catch (Civi\Payment\Exception\PaymentProcessorException $e) {
16f3bd02 229 $error = TRUE;
f8453bef 230 }
632196ea 231
f8453bef 232 $this->callAPISuccessGetCount('Contribution', array(
233 'contact_id' => $this->_individualId,
5e67c92d 234 'contribution_status_id' => $error ? 'Failed' : 'Completed',
16f3bd02 235 'payment_instrument_id' => $this->callAPISuccessGetValue('PaymentProcessor', array(
236 'return' => 'payment_instrument_id',
237 'id' => $paymentProcessorID,
238 )),
f8453bef 239 ), 1);
dc0ca56c 240 $contact = $this->callAPISuccessGetSingle('Contact', array('id' => $this->_individualId));
48e27df7 241 $this->assertTrue(empty($contact['source']));
f8453bef 242 }
243
22e39333 244 /**
245 * Test the submit function on the contribution page.
246 */
247 public function testSubmitCreditCardFee() {
248 $form = new CRM_Contribute_Form_Contribution();
22e39333 249 $this->paymentProcessor->setDoDirectPaymentResult(array('is_error' => 0, 'trxn_id' => 'tx', 'fee_amount' => .08));
250 $form->_mode = 'Live';
251 $form->testSubmit(array(
252 'total_amount' => 50,
253 'financial_type_id' => 1,
254 'receive_date' => '04/21/2015',
255 'receive_date_time' => '11:27PM',
256 'contact_id' => $this->_individualId,
257 'payment_instrument_id' => array_search('Credit Card', $this->paymentInstruments),
258 'contribution_status_id' => 1,
259 'credit_card_number' => 4444333322221111,
260 'cvv2' => 123,
261 'credit_card_exp_date' => array(
262 'M' => 9,
263 'Y' => 2025,
264 ),
265 'credit_card_type' => 'Visa',
266 'billing_first_name' => 'Junko',
267 'billing_middle_name' => '',
268 'billing_last_name' => 'Adams',
269 'billing_street_address-5' => '790L Lincoln St S',
270 'billing_city-5' => 'Maryknoll',
271 'billing_state_province_id-5' => 1031,
272 'billing_postal_code-5' => 10545,
273 'billing_country_id-5' => 1228,
274 'frequency_interval' => 1,
275 'frequency_unit' => 'month',
276 'installments' => '',
277 'hidden_AdditionalDetail' => 1,
278 'hidden_Premium' => 1,
279 'from_email_address' => '"civi45" <civi45@civicrm.com>',
280 'receipt_date' => '',
281 'receipt_date_time' => '',
282 'payment_processor_id' => $this->paymentProcessorID,
283 'currency' => 'USD',
284 'source' => '',
285 ), CRM_Core_Action::ADD);
286
287 $contribution = $this->callAPISuccessGetSingle('Contribution', array(
288 'contact_id' => $this->_individualId,
289 'contribution_status_id' => 'Completed',
290 ));
291 $this->assertEquals('50', $contribution['total_amount']);
292 $this->assertEquals(.08, $contribution['fee_amount']);
293 $this->assertEquals(49.92, $contribution['net_amount']);
294 $this->assertEquals('tx', $contribution['trxn_id']);
2b308818 295 $this->assertEmpty($contribution['amount_level']);
22e39333 296 }
297
11829025
JMW
298 /**
299 * Test a fully deductible contribution submitted by credit card (CRM-16669).
300 */
301 public function testSubmitCreditCardFullyDeductible() {
302 $form = new CRM_Contribute_Form_Contribution();
303 $form->_mode = 'Live';
304 $form->testSubmit(array(
305 'total_amount' => 50,
306 'financial_type_id' => 1,
307 'receive_date' => '04/21/2015',
308 'receive_date_time' => '11:27PM',
309 'contact_id' => $this->_individualId,
310 'payment_instrument_id' => array_search('Credit Card', $this->paymentInstruments),
311 'contribution_status_id' => 1,
312 'credit_card_number' => 4444333322221111,
313 'cvv2' => 123,
314 'credit_card_exp_date' => array(
315 'M' => 9,
316 'Y' => 2025,
317 ),
318 'credit_card_type' => 'Visa',
319 'billing_first_name' => 'Junko',
320 'billing_middle_name' => '',
321 'billing_last_name' => 'Adams',
322 'billing_street_address-5' => '790L Lincoln St S',
323 'billing_city-5' => 'Maryknoll',
324 'billing_state_province_id-5' => 1031,
325 'billing_postal_code-5' => 10545,
326 'billing_country_id-5' => 1228,
327 'frequency_interval' => 1,
328 'frequency_unit' => 'month',
329 'installments' => '',
330 'hidden_AdditionalDetail' => 1,
331 'hidden_Premium' => 1,
332 'from_email_address' => '"civi45" <civi45@civicrm.com>',
333 'receipt_date' => '',
334 'receipt_date_time' => '',
335 'payment_processor_id' => $this->paymentProcessorID,
336 'currency' => 'USD',
337 'source' => '',
338 ), CRM_Core_Action::ADD);
339
340 $contribution = $this->callAPISuccessGetSingle('Contribution', array(
341 'contact_id' => $this->_individualId,
342 'contribution_status_id' => 'Completed',
343 ));
344 $this->assertEquals('50', $contribution['total_amount']);
345 $this->assertEquals(0, $contribution['non_deductible_amount']);
346 }
347
7126b55c 348 /**
7758bd2b
EM
349 * Test the submit function with an invalid payment.
350 *
351 * We expect the contribution to be created but left pending. The payment has failed.
352 *
353 * Test covers CRM-16417 change to keep failed transactions.
354 *
355 * We are left with
356 * - 1 Contribution with status = Pending
357 * - 1 Line item
358 * - 1 civicrm_financial_item. This is linked to the line item and has a status of 3
7126b55c 359 */
7758bd2b 360 public function testSubmitCreditCardInvalid() {
7126b55c 361 $form = new CRM_Contribute_Form_Contribution();
7758bd2b
EM
362 $this->paymentProcessor->setDoDirectPaymentResult(array('is_error' => 1));
363 try {
364 $form->testSubmit(array(
365 'total_amount' => 50,
366 'financial_type_id' => 1,
367 'receive_date' => '04/21/2015',
368 'receive_date_time' => '11:27PM',
369 'contact_id' => $this->_individualId,
370 'payment_instrument_id' => array_search('Credit Card', $this->paymentInstruments),
22e39333 371 'payment_processor_id' => $this->paymentProcessorID,
7758bd2b
EM
372 'credit_card_exp_date' => array('M' => 5, 'Y' => 2012),
373 'credit_card_number' => '411111111111111',
374 ), CRM_Core_Action::ADD,
739a8336 375 'live'
376 );
7758bd2b
EM
377 }
378 catch (\Civi\Payment\Exception\PaymentProcessorException $e) {
40f5ec68
EM
379 $this->callAPISuccessGetCount('Contribution', array(
380 'contact_id' => $this->_individualId,
382c5680 381 'contribution_status_id' => 'Failed',
40f5ec68 382 ), 1);
7758bd2b
EM
383 $lineItem = $this->callAPISuccessGetSingle('line_item', array());
384 $this->assertEquals('50.00', $lineItem['unit_price']);
385 $this->assertEquals('50.00', $lineItem['line_total']);
386 $this->assertEquals(1, $lineItem['qty']);
387 $this->assertEquals(1, $lineItem['financial_type_id']);
388 $financialItem = $this->callAPISuccessGetSingle('financial_item', array(
389 'civicrm_line_item' => $lineItem['id'],
390 'entity_id' => $lineItem['id'],
391 ));
392 $this->assertEquals('50.00', $financialItem['amount']);
393 $this->assertEquals(3, $financialItem['status_id']);
394 return;
395 }
396 $this->fail('An expected exception has not been raised.');
7126b55c
EM
397 }
398
739a8336 399 /**
400 * Test the submit function creates a billing address if provided.
401 */
402 public function testSubmitCreditCardWithBillingAddress() {
403 $form = new CRM_Contribute_Form_Contribution();
404 $form->testSubmit(array(
405 'total_amount' => 50,
406 'financial_type_id' => 1,
407 'receive_date' => '04/21/2015',
408 'receive_date_time' => '11:27PM',
409 'contact_id' => $this->_individualId,
410 'payment_instrument_id' => array_search('Credit Card', $this->paymentInstruments),
22e39333 411 'payment_processor_id' => $this->paymentProcessorID,
739a8336 412 'credit_card_exp_date' => array('M' => 5, 'Y' => 2025),
413 'credit_card_number' => '411111111111111',
414 'billing_city-5' => 'Vancouver',
415 ), CRM_Core_Action::ADD,
416 'live'
417 );
418 $contribution = $this->callAPISuccessGetSingle('Contribution', array('return' => 'address_id'));
419 $this->assertNotEmpty($contribution['address_id']);
989a6bb8 420 // CRM-18490 : There is a unwanted test leakage due to below getsingle Api as it only fails in Jenkin
421 // for now we are only fetching address on based on Address ID (removed filter location_type_id and city)
739a8336 422 $this->callAPISuccessGetSingle('Address', array(
739a8336 423 'id' => $contribution['address_id'],
424 ));
425
426 }
427
428 /**
429 * Test the submit function does not create a billing address if no details provided.
430 */
431 public function testSubmitCreditCardWithNoBillingAddress() {
432 $form = new CRM_Contribute_Form_Contribution();
433 $form->testSubmit(array(
434 'total_amount' => 50,
435 'financial_type_id' => 1,
436 'receive_date' => '04/21/2015',
437 'receive_date_time' => '11:27PM',
438 'contact_id' => $this->_individualId,
439 'payment_instrument_id' => array_search('Credit Card', $this->paymentInstruments),
22e39333 440 'payment_processor_id' => $this->paymentProcessorID,
739a8336 441 'credit_card_exp_date' => array('M' => 5, 'Y' => 2025),
442 'credit_card_number' => '411111111111111',
443 ), CRM_Core_Action::ADD,
444 'live'
445 );
446 $contribution = $this->callAPISuccessGetSingle('Contribution', array('return' => 'address_id'));
447 $this->assertEmpty($contribution['address_id']);
448 $this->callAPISuccessGetCount('Address', array(
449 'city' => 'Vancouver',
450 'location_type_id' => 5,
451 ), 0);
452
453 }
454
a084385f
EM
455 /**
456 * Test the submit function on the contribution page.
457 */
458 public function testSubmitEmailReceipt() {
459 $form = new CRM_Contribute_Form_Contribution();
a084385f
EM
460 $mut = new CiviMailUtils($this, TRUE);
461 $form->testSubmit(array(
462 'total_amount' => 50,
463 'financial_type_id' => 1,
464 'receive_date' => '04/21/2015',
465 'receive_date_time' => '11:27PM',
466 'contact_id' => $this->_individualId,
467 'is_email_receipt' => TRUE,
468 'from_email_address' => 'test@test.com',
5e27919e 469 'contribution_status_id' => 1,
31760f81 470 ), CRM_Core_Action::ADD);
a084385f
EM
471 $this->callAPISuccessGetCount('Contribution', array('contact_id' => $this->_individualId), 1);
472 $mut->checkMailLog(array(
473 '<p>Please print this receipt for your records.</p>',
474 )
475 );
476 $mut->stop();
477 }
df729ab1 478
0335c5f1 479 /**
480 * Ensure that price field are shown during pay later/pending Contribution
481 */
482 public function testEmailReceiptOnPayLater() {
483 $donationFT = CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_FinancialType', 'Donation', 'id', 'name');
484 $paramsSet = array(
485 'title' => 'Price Set' . substr(sha1(rand()), 0, 4),
486 'is_active' => TRUE,
487 'financial_type_id' => $donationFT,
488 'extends' => 2,
489 );
490 $paramsSet['name'] = CRM_Utils_String::titleToVar($paramsSet['title']);
491
492 $priceset = CRM_Price_BAO_PriceSet::create($paramsSet);
493 $priceSetId = $priceset->id;
494
495 //Checking for priceset added in the table.
496 $this->assertDBCompareValue('CRM_Price_BAO_PriceSet', $priceSetId, 'title',
497 'id', $paramsSet['title'], 'Check DB for created priceset'
498 );
499 $paramsField = array(
500 'label' => 'Price Field',
501 'name' => CRM_Utils_String::titleToVar('Price Field'),
502 'html_type' => 'CheckBox',
503 'option_label' => array('1' => 'Price Field 1', '2' => 'Price Field 2'),
504 'option_value' => array('1' => 100, '2' => 200),
505 'option_name' => array('1' => 'Price Field 1', '2' => 'Price Field 2'),
506 'option_weight' => array('1' => 1, '2' => 2),
507 'option_amount' => array('1' => 100, '2' => 200),
508 'is_display_amounts' => 1,
509 'weight' => 1,
510 'options_per_line' => 1,
511 'is_active' => array('1' => 1, '2' => 1),
512 'price_set_id' => $priceset->id,
513 'is_enter_qty' => 1,
514 'financial_type_id' => $donationFT,
515 );
516 $priceField = CRM_Price_BAO_PriceField::create($paramsField);
517 $priceFieldValue = $this->callAPISuccess('PriceFieldValue', 'get', array('price_field_id' => $priceField->id));
518
519 $params = array(
520 'total_amount' => 100,
521 'financial_type_id' => $donationFT,
522 'receive_date' => '04/21/2015',
523 'receive_date_time' => '11:27PM',
524 'contact_id' => $this->_individualId,
525 'is_email_receipt' => TRUE,
526 'from_email_address' => 'test@test.com',
527 'price_set_id' => $priceSetId,
f527e012 528 'contribution_status_id' => CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id', 'Pending'),
0335c5f1 529 );
530
531 foreach ($priceFieldValue['values'] as $id => $price) {
532 if ($price['amount'] == 100) {
533 $params['price_' . $priceField->id] = array($id => 1);
534 }
535 }
536 $form = new CRM_Contribute_Form_Contribution();
537 $mut = new CiviMailUtils($this, TRUE);
538 $form->_priceSet = current(CRM_Price_BAO_PriceSet::getSetDetail($priceSetId));
539 $form->testSubmit($params, CRM_Core_Action::ADD);
540
541 $mut->checkMailLog(array(
542 'Financial Type: Donation
543---------------------------------------------------------
544Item Qty Each Total
545----------------------------------------------------------
546Price Field - Price Field 1 1 $ 100.00 $ 100.00
547',
548 )
549 );
550 $mut->stop();
551 }
552
31760f81
EM
553 /**
554 * Test that a contribution is assigned against a pledge.
555 */
556 public function testUpdatePledge() {
557 $pledge = $this->callAPISuccess('pledge', 'create', array(
558 'contact_id' => $this->_individualId,
559 'pledge_create_date' => date('Ymd'),
560 'start_date' => date('Ymd'),
561 'amount' => 100.00,
562 'pledge_status_id' => '2',
563 'pledge_financial_type_id' => '1',
564 'pledge_original_installment_amount' => 20,
565 'frequency_interval' => 5,
566 'frequency_unit' => 'year',
567 'frequency_day' => 15,
568 'installments' => 2,
569 'sequential' => 1,
570 ));
571 $pledgePaymentID = $this->callAPISuccess('pledge_payment', 'getvalue', array(
572 'pledge_id' => $pledge['id'],
573 'options' => array('limit' => 1),
574 'return' => 'id',
575 ));
576 $form = new CRM_Contribute_Form_Contribution();
577 $form->testSubmit(array(
578 'total_amount' => 50,
579 'financial_type_id' => 1,
580 'receive_date' => '04/21/2015',
581 'receive_date_time' => '11:27PM',
582 'contact_id' => $this->_individualId,
583 'payment_instrument_id' => array_search('Check', $this->paymentInstruments),
584 'pledge_payment_id' => $pledgePaymentID,
5e27919e 585 'contribution_status_id' => 1,
31760f81
EM
586 ), CRM_Core_Action::ADD);
587 $pledgePayment = $this->callAPISuccess('pledge_payment', 'getsingle', array('id' => $pledgePaymentID));
588 $this->assertNotEmpty($pledgePayment['contribution_id']);
589 $this->assertEquals($pledgePayment['actual_amount'], 50);
590 $this->assertEquals(1, $pledgePayment['status_id']);
591 }
ef353929 592
3e6a1f4a
EM
593 /**
594 * Test functions involving premiums.
595 */
596 public function testPremiumUpdate() {
597 $form = new CRM_Contribute_Form_Contribution();
43bf07d6 598 $mut = new CiviMailUtils($this, TRUE);
3e6a1f4a
EM
599 $form->testSubmit(array(
600 'total_amount' => 50,
601 'financial_type_id' => 1,
602 'receive_date' => '04/21/2015',
603 'receive_date_time' => '11:27PM',
604 'contact_id' => $this->_individualId,
605 'payment_instrument_id' => array_search('Check', $this->paymentInstruments),
606 'contribution_status_id' => 1,
607 'product_name' => array($this->products[0]['id'], 1),
608 'fulfilled_date' => '',
43bf07d6
EM
609 'is_email_receipt' => TRUE,
610 'from_email_address' => 'test@test.com',
3e6a1f4a
EM
611 ), CRM_Core_Action::ADD);
612 $contributionProduct = $this->callAPISuccess('contribution_product', 'getsingle', array());
613 $this->assertEquals('clumsy smurf', $contributionProduct['product_option']);
43bf07d6
EM
614 $mut->checkMailLog(array(
615 'Premium Information',
616 'Smurf',
617 'clumsy smurf',
618 ));
619 $mut->stop();
3e6a1f4a
EM
620 }
621
39f47c0d
EM
622 /**
623 * Test functions involving premiums.
624 */
625 public function testPremiumUpdateCreditCard() {
626 $form = new CRM_Contribute_Form_Contribution();
627 $mut = new CiviMailUtils($this, TRUE);
628 $form->testSubmit(array(
629 'total_amount' => 50,
630 'financial_type_id' => 1,
631 'receive_date' => '04/21/2015',
632 'receive_date_time' => '11:27PM',
633 'contact_id' => $this->_individualId,
634 'payment_instrument_id' => array_search('Check', $this->paymentInstruments),
635 'contribution_status_id' => 1,
636 'product_name' => array($this->products[0]['id'], 1),
637 'fulfilled_date' => '',
638 'is_email_receipt' => TRUE,
639 'from_email_address' => 'test@test.com',
22e39333 640 'payment_processor_id' => $this->paymentProcessorID,
7758bd2b 641 'credit_card_exp_date' => array('M' => 5, 'Y' => 2026),
39f47c0d
EM
642 'credit_card_number' => '411111111111111',
643 ), CRM_Core_Action::ADD,
644 'live');
645 $contributionProduct = $this->callAPISuccess('contribution_product', 'getsingle', array());
646 $this->assertEquals('clumsy smurf', $contributionProduct['product_option']);
647 $mut->checkMailLog(array(
648 'Premium Information',
649 'Smurf',
650 'clumsy smurf',
651 ));
652 $mut->stop();
653 }
654
945f423d
EM
655 /**
656 * Test the submit function on the contribution page.
657 */
658 public function testSubmitWithNote() {
659 $form = new CRM_Contribute_Form_Contribution();
660 $form->testSubmit(array(
661 'total_amount' => 50,
662 'financial_type_id' => 1,
663 'receive_date' => '04/21/2015',
664 'receive_date_time' => '11:27PM',
665 'contact_id' => $this->_individualId,
666 'payment_instrument_id' => array_search('Check', $this->paymentInstruments),
667 'contribution_status_id' => 1,
668 'note' => 'Super cool and interesting stuff',
669 ),
670 CRM_Core_Action::ADD);
671 $this->callAPISuccessGetCount('Contribution', array('contact_id' => $this->_individualId), 1);
672 $note = $this->callAPISuccessGetSingle('note', array('entity_table' => 'civicrm_contribution'));
673 $this->assertEquals($note['note'], 'Super cool and interesting stuff');
674 }
675
676 /**
677 * Test the submit function on the contribution page.
678 */
679 public function testSubmitWithNoteCreditCard() {
680 $form = new CRM_Contribute_Form_Contribution();
681
682 $form->testSubmit(array(
683 'total_amount' => 50,
684 'financial_type_id' => 1,
685 'receive_date' => '04/21/2015',
686 'receive_date_time' => '11:27PM',
687 'contact_id' => $this->_individualId,
688 'payment_instrument_id' => array_search('Check', $this->paymentInstruments),
689 'contribution_status_id' => 1,
690 'note' => 'Super cool and interesting stuff',
1ea0b4ac 691 ) + $this->getCreditCardParams(),
945f423d
EM
692 CRM_Core_Action::ADD);
693 $this->callAPISuccessGetCount('Contribution', array('contact_id' => $this->_individualId), 1);
694 $note = $this->callAPISuccessGetSingle('note', array('entity_table' => 'civicrm_contribution'));
695 $this->assertEquals($note['note'], 'Super cool and interesting stuff');
696 }
697
fa18b745
K
698 /**
699 * Test that if a negative contribution is entered it does not get reset to $0
700 */
701 public function testEnterNegativeContribution() {
702 $form = new CRM_Contribute_Form_Contribution();
703 $form->testSubmit(array(
704 'total_amount' => -5,
705 'financial_type_id' => 1,
706 'receive_date' => '04/24/2016',
707 'receive_date_time' => '11:27PM',
708 'contact_id' => $this->_individualId,
709 'payment_instrument_id' => array_search('Check', $this->paymentInstruments),
710 'contribution_status_id' => 1,
711 ),
712 CRM_Core_Action::ADD);
713 $this->callAPISuccessGetCount('Contribution', array('contact_id' => $this->_individualId), 1);
714
715 $contribution = $this->callAPISuccessGetSingle('Contribution', array(
716 'contact_id' => $this->_individualId,
717 ));
718 $this->assertEquals(-5, $contribution['total_amount']);
719 }
720
55e55e84 721 /**
722 * Test the submit function on the contribution page.
723 */
724 public function testSubmitUpdate() {
725 $form = new CRM_Contribute_Form_Contribution();
726
727 $form->testSubmit(array(
728 'total_amount' => 50,
729 'financial_type_id' => 1,
730 'receive_date' => '04/21/2015',
731 'receive_date_time' => '11:27PM',
732 'contact_id' => $this->_individualId,
733 'payment_instrument_id' => array_search('Check', $this->paymentInstruments),
734 'contribution_status_id' => 1,
735 'price_set_id' => 0,
736 ),
737 CRM_Core_Action::ADD);
738 $contribution = $this->callAPISuccessGetSingle('Contribution', array('contact_id' => $this->_individualId));
739 $form->testSubmit(array(
740 'total_amount' => 45,
741 'net_amount' => 45,
742 'financial_type_id' => 1,
743 'receive_date' => '04/21/2015',
744 'receive_date_time' => '11:27PM',
745 'contact_id' => $this->_individualId,
746 'payment_instrument_id' => array_search('Check', $this->paymentInstruments),
747 'contribution_status_id' => 1,
748 'price_set_id' => 0,
749 'id' => $contribution['id'],
750 ),
751 CRM_Core_Action::UPDATE);
da77cbbd
EM
752 $contribution = $this->callAPISuccessGetSingle('Contribution', array('contact_id' => $this->_individualId));
753 $this->assertEquals(45, (int) $contribution['total_amount']);
754
55e55e84 755 $financialTransactions = $this->callAPISuccess('FinancialTrxn', 'get', array('sequential' => TRUE));
756 $this->assertEquals(2, $financialTransactions['count']);
757 $this->assertEquals(50, $financialTransactions['values'][0]['total_amount']);
a4ed7706
EM
758 $this->assertEquals(-5, $financialTransactions['values'][1]['total_amount']);
759 $this->assertEquals(-5, $financialTransactions['values'][1]['net_amount']);
55e55e84 760 $lineItem = $this->callAPISuccessGetSingle('LineItem', array());
761 $this->assertEquals(45, $lineItem['line_total']);
762 }
763
945f423d
EM
764 /**
765 * Get parameters for credit card submit calls.
766 *
767 * @return array
768 * Credit card specific parameters.
769 */
1ea0b4ac 770 protected function getCreditCardParams() {
945f423d 771 return array(
22e39333 772 'payment_processor_id' => $this->paymentProcessorID,
945f423d
EM
773 'credit_card_exp_date' => array('M' => 5, 'Y' => 2012),
774 'credit_card_number' => '411111111111111',
775 );
776 }
945f423d 777
7a1f3919
PN
778 /**
779 * Test the submit function for FT with tax.
780 */
781 public function testSubmitSaleTax() {
782 $this->enableTaxAndInvoicing();
783 $this->relationForFinancialTypeWithFinancialAccount($this->_financialTypeId);
784 $form = new CRM_Contribute_Form_Contribution();
785
786 $form->testSubmit(array(
787 'total_amount' => 100,
788 'financial_type_id' => $this->_financialTypeId,
789 'receive_date' => '04/21/2015',
790 'receive_date_time' => '11:27PM',
791 'contact_id' => $this->_individualId,
792 'payment_instrument_id' => array_search('Check', $this->paymentInstruments),
793 'contribution_status_id' => 1,
794 'price_set_id' => 0,
795 ),
796 CRM_Core_Action::ADD
797 );
798 $contribution = $this->callAPISuccessGetSingle('Contribution',
799 array(
800 'contact_id' => $this->_individualId,
801 'return' => array('tax_amount', 'total_amount'),
802 )
803 );
804 $this->assertEquals(110, $contribution['total_amount']);
805 $this->assertEquals(10, $contribution['tax_amount']);
806 $this->callAPISuccessGetCount('FinancialTrxn', array(), 1);
807 $this->callAPISuccessGetCount('FinancialItem', array(), 2);
808 $lineItem = $this->callAPISuccessGetSingle('LineItem', array('contribution_id' => $contribution['id']));
809 $this->assertEquals(100, $lineItem['line_total']);
810 $this->assertEquals(10, $lineItem['tax_amount']);
811 }
812
1ec04220
PN
813 /**
814 * Test the submit function for FT without tax.
815 */
816 public function testSubmitWithOutSaleTax() {
817 $this->enableTaxAndInvoicing();
818 $this->relationForFinancialTypeWithFinancialAccount($this->_financialTypeId);
819 $form = new CRM_Contribute_Form_Contribution();
820
821 $form->testSubmit(array(
822 'total_amount' => 100,
823 'financial_type_id' => 3,
824 'receive_date' => '04/21/2015',
825 'receive_date_time' => '11:27PM',
826 'contact_id' => $this->_individualId,
827 'payment_instrument_id' => array_search('Check', $this->paymentInstruments),
828 'contribution_status_id' => 1,
829 'price_set_id' => 0,
830 ),
831 CRM_Core_Action::ADD
832 );
833 $contribution = $this->callAPISuccessGetSingle('Contribution',
834 array(
835 'contact_id' => $this->_individualId,
836 'return' => array('tax_amount', 'total_amount'),
837 )
838 );
839 $this->assertEquals(100, $contribution['total_amount']);
840 $this->assertEquals(NULL, $contribution['tax_amount']);
841 $this->callAPISuccessGetCount('FinancialTrxn', array(), 1);
842 $this->callAPISuccessGetCount('FinancialItem', array(), 1);
843 $lineItem = $this->callAPISuccessGetSingle(
844 'LineItem',
845 array(
846 'contribution_id' => $contribution['id'],
847 'return' => array('line_total', 'tax_amount'),
848 )
849 );
850 $this->assertEquals(100, $lineItem['line_total']);
851 $this->assertTrue(empty($lineItem['tax_amount']));
852 }
853
f1662cbd 854 /**
9308f1c2 855 * Create a contribution & then edit it via backoffice form, checking tax with: default price_set
f1662cbd 856 *
857 * @throws \Exception
858 */
335c41e7 859 public function testReSubmitSaleTax() {
335c41e7
K
860 $this->enableTaxAndInvoicing();
861 $this->relationForFinancialTypeWithFinancialAccount($this->_financialTypeId);
cf28d075 862 list($form, $contribution) = $this->doInitialSubmit();
863 $this->assertEquals(110, $contribution['total_amount']);
864 $this->assertEquals(10, $contribution['tax_amount']);
865 $this->assertEquals(110, $contribution['net_amount']);
335c41e7 866
cf28d075 867 $mut = new CiviMailUtils($this, TRUE);
868 // Testing here if when we edit something trivial like adding a check_number tax, net, total amount stay the same:
335c41e7 869 $form->testSubmit(array(
cf28d075 870 'id' => $contribution['id'],
871 'tax_amount' => $contribution['tax_amount'],
872 'financial_type_id' => $contribution['financial_type_id'],
873 'receive_date' => $contribution['receive_date'],
874 'payment_instrument_id' => $contribution['payment_instrument_id'],
335c41e7 875 'price_set_id' => 0,
cf28d075 876 'check_number' => 12345,
877 'contribution_status_id' => 1,
878 'is_email_receipt' => 1,
879 'from_email_address' => 'demo@example.com',
335c41e7 880 ),
cf28d075 881 CRM_Core_Action::UPDATE
335c41e7
K
882 );
883 $contribution = $this->callAPISuccessGetSingle('Contribution',
884 array(
885 'contribution_id' => 1,
886 'return' => array('tax_amount', 'total_amount', 'net_amount', 'financial_type_id', 'receive_date', 'payment_instrument_id'),
887 )
888 );
f1662cbd 889 $this->assertEquals(110, $contribution['total_amount']);
890 $this->assertEquals(10, $contribution['tax_amount']);
9308f1c2 891 $this->assertEquals(110, $contribution['net_amount']);
335c41e7 892
cf28d075 893 $strings = array(
894 'Total Tax Amount : $ 10.00',
895 'Total Amount : $ 110.00',
896 'Date Received: April 21st, 2015',
897 'Paid By: Check',
898 'Check Number: 12345',
899 );
900
901 $mut->checkMailLog($strings);
902 $this->callAPISuccessGetCount('FinancialTrxn', array(), 3);
903 $items = $this->callAPISuccess('FinancialItem', 'get', array('sequential' => 1));
904 $this->assertEquals(2, $items['count']);
905 $this->assertEquals('Contribution Amount', $items['values'][0]['description']);
906 $this->assertEquals('Sales Tax', $items['values'][1]['description']);
907
908 $this->assertEquals(100, $items['values'][0]['amount']);
909 $this->assertEquals(10, $items['values'][1]['amount']);
910 }
911
912 /**
913 * Create a contribution & then edit it via backoffice form, checking tax with: default price_set
914 *
915 * @throws \Exception
916 */
917 public function testReSubmitSaleTaxAlteredAmount() {
918 $this->enableTaxAndInvoicing();
919 $this->relationForFinancialTypeWithFinancialAccount($this->_financialTypeId);
920 list($form, $contribution) = $this->doInitialSubmit();
921
f1662cbd 922 $mut = new CiviMailUtils($this, TRUE);
9308f1c2 923 // Testing here if when we edit something trivial like adding a check_number tax, net, total amount stay the same:
335c41e7 924 $form->testSubmit(array(
f1662cbd 925 'id' => $contribution['id'],
cf28d075 926 'total_amount' => 200,
927 'tax_amount' => 20,
335c41e7
K
928 'financial_type_id' => $contribution['financial_type_id'],
929 'receive_date' => $contribution['receive_date'],
930 'payment_instrument_id' => $contribution['payment_instrument_id'],
931 'price_set_id' => 0,
932 'check_number' => 12345,
f1662cbd 933 'contribution_status_id' => 1,
934 'is_email_receipt' => 1,
935 'from_email_address' => 'demo@example.com',
335c41e7
K
936 ),
937 CRM_Core_Action::UPDATE
938 );
939 $contribution = $this->callAPISuccessGetSingle('Contribution',
940 array(
9308f1c2
K
941 'contribution_id' => 1,
942 'return' => array('tax_amount', 'total_amount', 'net_amount', 'financial_type_id', 'receive_date', 'payment_instrument_id'),
335c41e7
K
943 )
944 );
cf28d075 945 $this->assertEquals(220, $contribution['total_amount']);
946 $this->assertEquals(20, $contribution['tax_amount']);
947 $this->assertEquals(220, $contribution['net_amount']);
9308f1c2 948
f1662cbd 949 $strings = array(
cf28d075 950 'Total Tax Amount : $ 20.00',
951 'Total Amount : $ 220.00',
f1662cbd 952 'Date Received: April 21st, 2015',
953 'Paid By: Check',
954 'Check Number: 12345',
955 );
335c41e7 956
f1662cbd 957 $mut->checkMailLog($strings);
cf28d075 958 $this->callAPISuccessGetCount('FinancialTrxn', array(), 4);
959 $items = $this->callAPISuccess('FinancialItem', 'get', array('sequential' => 1));
960 $this->assertEquals(4, $items['count']);
961 $this->assertEquals('Contribution Amount', $items['values'][0]['description']);
962 $this->assertEquals('Sales Tax', $items['values'][1]['description']);
963 $this->assertEquals('Contribution Amount', $items['values'][0]['description']);
964 $this->assertEquals('Sales Tax', $items['values'][1]['description']);
965
966 $this->assertEquals(100, $items['values'][0]['amount']);
967 $this->assertEquals(10, $items['values'][1]['amount']);
8a477059 968 $this->assertEquals(200, $items['values'][2]['amount']);
cf28d075 969 $this->assertEquals(20, $items['values'][3]['amount']);
970 }
971
972 /**
973 * Do the first contributions, in preparation for an edit-submit.
974 *
975 * @return array
976 *
977 * @throws \Exception
978 */
979 protected function doInitialSubmit() {
980 $form = new CRM_Contribute_Form_Contribution();
981
982 $form->testSubmit(array(
983 'total_amount' => 100,
984 'financial_type_id' => $this->_financialTypeId,
985 'receive_date' => '04/21/2015',
986 'receive_date_time' => '11:27PM',
987 'contact_id' => $this->_individualId,
988 'payment_instrument_id' => array_search('Check', $this->paymentInstruments),
989 'contribution_status_id' => 1,
990 'price_set_id' => 0,
991 ),
992 CRM_Core_Action::ADD
993 );
994 $contribution = $this->callAPISuccessGetSingle('Contribution',
995 array(
996 'contribution_id' => 1,
997 'return' => array(
998 'tax_amount',
999 'total_amount',
1000 'net_amount',
1001 'financial_type_id',
1002 'receive_date',
1003 'payment_instrument_id',
1004 ),
1005 )
1006 );
1007 $this->assertEquals(110, $contribution['total_amount']);
1008 $this->assertEquals(10, $contribution['tax_amount']);
1009 $this->assertEquals(110, $contribution['net_amount']);
1010 return array($form, $contribution);
335c41e7
K
1011 }
1012
a55e39e9 1013 /**
1014 * function to test card_type and pan truncation.
1015 */
1016 public function testCardTypeAndPanTruncation() {
1017 $form = new CRM_Contribute_Form_Contribution();
1018 $form->testSubmit(
1019 array(
1020 'total_amount' => 100,
1021 'financial_type_id' => 3,
1022 'receive_date' => '04/21/2015',
1023 'receive_date_time' => '11:27PM',
1024 'contact_id' => $this->_individualId,
1025 'payment_instrument_id' => array_search('Credit Card', $this->paymentInstruments),
1026 'contribution_status_id' => 1,
1027 'credit_card_type' => 'Visa',
1028 'pan_truncation' => 4567,
1029 'price_set_id' => 0,
1030 ),
1031 CRM_Core_Action::ADD
1032 );
1033 $contribution = $this->callAPISuccessGetSingle('Contribution',
1034 array(
1035 'contact_id' => $this->_individualId,
1036 'return' => array('id'),
1037 )
1038 );
1039 $lastFinancialTrxnId = CRM_Core_BAO_FinancialTrxn::getFinancialTrxnId($contribution['id'], 'DESC');
1040 $financialTrxn = $this->callAPISuccessGetSingle(
1041 'FinancialTrxn',
1042 array(
1043 'id' => $lastFinancialTrxnId['financialTrxnId'],
1044 'return' => array('card_type_id.label', 'pan_truncation'),
1045 )
1046 );
1047 $this->assertEquals(CRM_Utils_Array::value('card_type_id.label', $financialTrxn), 'Visa');
1048 $this->assertEquals(CRM_Utils_Array::value('pan_truncation', $financialTrxn), 4567);
1049 }
1050
1051 /**
1052 * function to test card_type and pan truncation.
1053 */
1054 public function testCardTypeAndPanTruncationLiveMode() {
1055 $visaID = CRM_Core_PseudoConstant::getKey('CRM_Core_BAO_FinancialTrxn', 'card_type_id', 'Visa');
1056 $form = new CRM_Contribute_Form_Contribution();
1057 $form->_mode = 'Live';
a55e39e9 1058 $form->testSubmit(
1059 array(
1060 'total_amount' => 50,
1061 'financial_type_id' => 1,
1062 'receive_date' => '04/21/2015',
1063 'receive_date_time' => '11:27PM',
1064 'contact_id' => $this->_individualId,
1065 'credit_card_number' => 4444333322221111,
1066 'payment_instrument_id' => array_search('Credit Card', $this->paymentInstruments),
1067 'cvv2' => 123,
1068 'credit_card_exp_date' => array(
1069 'M' => 9,
1070 'Y' => date('Y', strtotime('+5 years')),
1071 ),
1072 'credit_card_type' => 'Visa',
1073 'billing_first_name' => 'Junko',
1074 'billing_middle_name' => '',
1075 'billing_last_name' => 'Adams',
1076 'billing_street_address-5' => '790L Lincoln St S',
1077 'billing_city-5' => 'Maryknoll',
1078 'billing_state_province_id-5' => 1031,
1079 'billing_postal_code-5' => 10545,
1080 'billing_country_id-5' => 1228,
1081 'frequency_interval' => 1,
1082 'frequency_unit' => 'month',
1083 'installments' => '',
1084 'hidden_AdditionalDetail' => 1,
1085 'hidden_Premium' => 1,
1086 'from_email_address' => '"civi45" <civi45@civicrm.com>',
1087 'receipt_date' => '',
1088 'receipt_date_time' => '',
1089 'payment_processor_id' => $this->paymentProcessorID,
1090 'currency' => 'USD',
1091 'source' => 'bob sled race',
1092 ),
1093 CRM_Core_Action::ADD
1094 );
1095 $contribution = $this->callAPISuccessGetSingle('Contribution', array('contact_id' => $this->_individualId));
1096 $lastFinancialTrxnId = CRM_Core_BAO_FinancialTrxn::getFinancialTrxnId($contribution['id'], 'DESC');
1097 $financialTrxn = $this->callAPISuccessGetSingle(
1098 'FinancialTrxn',
1099 array(
1100 'id' => $lastFinancialTrxnId['financialTrxnId'],
1101 'return' => array('card_type_id', 'pan_truncation'),
1102 )
1103 );
016569ee 1104 $this->assertEquals($visaID, $financialTrxn['card_type_id']);
1105 $this->assertEquals(1111, $financialTrxn['pan_truncation']);
a55e39e9 1106 }
1107
2dcbf765 1108}