3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
29 * Test APIv3 civicrm_contribute_* functions
31 * @package CiviCRM_APIv3
32 * @subpackage API_Contribution
35 class CRM_Contribute_Form_ContributionTest
extends CiviUnitTestCase
{
38 * Assume empty database with just civicrm_data.
40 protected $_individualId;
41 protected $_contribution;
42 protected $_financialTypeId = 1;
43 protected $_apiversion;
44 protected $_entity = 'Contribution';
46 protected $_ids = array();
47 protected $_pageParams = array();
50 * Parameters to create payment processor.
54 protected $_processorParams = array();
57 * ID of created event.
64 * Payment instrument mapping.
68 protected $paymentInstruments = array();
75 protected $products = array();
78 * Dummy payment processor.
80 * @var CRM_Core_Payment_Dummy
82 protected $paymentProcessor;
85 * Payment processor ID.
89 protected $paymentProcessorID;
94 public function setUp() {
95 $this->_apiversion
= 3;
97 $this->createLoggedInUser();
99 $this->_individualId
= $this->individualCreate();
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,
109 'contribution_status_id' => 1,
111 $this->_processorParams
= array(
114 'payment_processor_type_id' => 10,
115 'financial_account_id' => 12,
118 'url_site' => 'http://dummy.com',
119 'url_recur' => 'http://dummy.com',
123 $instruments = $this->callAPISuccess('contribution', 'getoptions', array('field' => 'payment_instrument_id'));
124 $this->paymentInstruments
= $instruments['values'];
125 $product1 = $this->callAPISuccess('product', 'create', array(
127 'options' => 'brainy smurf, clumsy smurf, papa smurf',
130 $this->products
[] = $product1['values'][$product1['id']];
131 $this->paymentProcessor
= $this->dummyProcessorCreate();
132 $processor = $this->paymentProcessor
->getPaymentProcessor();
133 $this->paymentProcessorID
= $processor['id'];
137 * Clean up after each test.
139 public function tearDown() {
140 $this->quickCleanUpFinancialEntities();
141 $this->quickCleanup(array('civicrm_note', 'civicrm_uf_match', 'civicrm_address'));
145 * Test the submit function on the contribution page.
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
),
156 'contribution_status_id' => 1,
158 CRM_Core_Action
::ADD
);
159 $contribution = $this->callAPISuccessGetSingle('Contribution', array('contact_id' => $this->_individualId
));
160 $this->assertEmpty($contribution['amount_level']);
164 * Test the submit function on the contribution page.
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
),
175 'contribution_status_id' => 1,
176 ), CRM_Core_Action
::ADD
);
177 $this->callAPISuccessGetCount('Contribution', array(
178 'contact_id' => $this->_individualId
,
179 'contribution_status_id' => 'Completed',
185 * Test the submit function on the contribution page.
187 public function testSubmitCreditCardPayPal() {
188 $form = new CRM_Contribute_Form_Contribution();
189 $paymentProcessorID = $this->paymentProcessorCreate(array('is_test' => 0));
190 $form->_mode
= 'Live';
192 $form->testSubmit(array(
193 'total_amount' => 50,
194 'financial_type_id' => 1,
195 'receive_date' => '04/21/2015',
196 'receive_date_time' => '11:27PM',
197 'contact_id' => $this->_individualId
,
198 'payment_instrument_id' => array_search('Credit Card', $this->paymentInstruments
),
199 'contribution_status_id' => 1,
200 'credit_card_number' => 4444333322221111,
202 'credit_card_exp_date' => array(
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,
226 ), CRM_Core_Action
::ADD
);
228 catch (Civi\Payment\Exception\PaymentProcessorException
$e) {
229 $this->assertEquals('Transaction cannot be processed. Please use a different payment card.',
232 $this->callAPISuccessGetCount('Contribution', array(
233 'contact_id' => $this->_individualId
,
234 'contribution_status_id' => 'Pending',
239 * Test the submit function on the contribution page.
241 public function testSubmitCreditCardFee() {
242 $form = new CRM_Contribute_Form_Contribution();
243 print_r($this->paymentProcessor
);
244 $this->paymentProcessor
->setDoDirectPaymentResult(array('is_error' => 0, 'trxn_id' => 'tx', 'fee_amount' => .08));
245 $form->_mode
= 'Live';
246 $form->testSubmit(array(
247 'total_amount' => 50,
248 'financial_type_id' => 1,
249 'receive_date' => '04/21/2015',
250 'receive_date_time' => '11:27PM',
251 'contact_id' => $this->_individualId
,
252 'payment_instrument_id' => array_search('Credit Card', $this->paymentInstruments
),
253 'contribution_status_id' => 1,
254 'credit_card_number' => 4444333322221111,
256 'credit_card_exp_date' => array(
260 'credit_card_type' => 'Visa',
261 'billing_first_name' => 'Junko',
262 'billing_middle_name' => '',
263 'billing_last_name' => 'Adams',
264 'billing_street_address-5' => '790L Lincoln St S',
265 'billing_city-5' => 'Maryknoll',
266 'billing_state_province_id-5' => 1031,
267 'billing_postal_code-5' => 10545,
268 'billing_country_id-5' => 1228,
269 'frequency_interval' => 1,
270 'frequency_unit' => 'month',
271 'installments' => '',
272 'hidden_AdditionalDetail' => 1,
273 'hidden_Premium' => 1,
274 'from_email_address' => '"civi45" <civi45@civicrm.com>',
275 'receipt_date' => '',
276 'receipt_date_time' => '',
277 'payment_processor_id' => $this->paymentProcessorID
,
280 ), CRM_Core_Action
::ADD
);
282 $contribution = $this->callAPISuccessGetSingle('Contribution', array(
283 'contact_id' => $this->_individualId
,
284 'contribution_status_id' => 'Completed',
286 $this->assertEquals('50', $contribution['total_amount']);
287 $this->assertEquals(.08, $contribution['fee_amount']);
288 $this->assertEquals(49.92, $contribution['net_amount']);
289 $this->assertEquals('tx', $contribution['trxn_id']);
290 $this->assertEmpty($contribution['amount_level']);
294 * Test a fully deductible contribution submitted by credit card (CRM-16669).
296 public function testSubmitCreditCardFullyDeductible() {
297 $form = new CRM_Contribute_Form_Contribution();
298 $form->_mode
= 'Live';
299 $form->testSubmit(array(
300 'total_amount' => 50,
301 'financial_type_id' => 1,
302 'receive_date' => '04/21/2015',
303 'receive_date_time' => '11:27PM',
304 'contact_id' => $this->_individualId
,
305 'payment_instrument_id' => array_search('Credit Card', $this->paymentInstruments
),
306 'contribution_status_id' => 1,
307 'credit_card_number' => 4444333322221111,
309 'credit_card_exp_date' => array(
313 'credit_card_type' => 'Visa',
314 'billing_first_name' => 'Junko',
315 'billing_middle_name' => '',
316 'billing_last_name' => 'Adams',
317 'billing_street_address-5' => '790L Lincoln St S',
318 'billing_city-5' => 'Maryknoll',
319 'billing_state_province_id-5' => 1031,
320 'billing_postal_code-5' => 10545,
321 'billing_country_id-5' => 1228,
322 'frequency_interval' => 1,
323 'frequency_unit' => 'month',
324 'installments' => '',
325 'hidden_AdditionalDetail' => 1,
326 'hidden_Premium' => 1,
327 'from_email_address' => '"civi45" <civi45@civicrm.com>',
328 'receipt_date' => '',
329 'receipt_date_time' => '',
330 'payment_processor_id' => $this->paymentProcessorID
,
333 ), CRM_Core_Action
::ADD
);
335 $contribution = $this->callAPISuccessGetSingle('Contribution', array(
336 'contact_id' => $this->_individualId
,
337 'contribution_status_id' => 'Completed',
339 $this->assertEquals('50', $contribution['total_amount']);
340 $this->assertEquals(0, $contribution['non_deductible_amount']);
344 * Test the submit function with an invalid payment.
346 * We expect the contribution to be created but left pending. The payment has failed.
348 * Test covers CRM-16417 change to keep failed transactions.
351 * - 1 Contribution with status = Pending
353 * - 1 civicrm_financial_item. This is linked to the line item and has a status of 3
355 public function testSubmitCreditCardInvalid() {
356 $form = new CRM_Contribute_Form_Contribution();
357 $this->paymentProcessor
->setDoDirectPaymentResult(array('is_error' => 1));
359 $form->testSubmit(array(
360 'total_amount' => 50,
361 'financial_type_id' => 1,
362 'receive_date' => '04/21/2015',
363 'receive_date_time' => '11:27PM',
364 'contact_id' => $this->_individualId
,
365 'payment_instrument_id' => array_search('Credit Card', $this->paymentInstruments
),
366 'payment_processor_id' => $this->paymentProcessorID
,
367 'credit_card_exp_date' => array('M' => 5, 'Y' => 2012),
368 'credit_card_number' => '411111111111111',
369 ), CRM_Core_Action
::ADD
,
373 catch (\Civi\Payment\Exception\PaymentProcessorException
$e) {
374 $this->callAPISuccessGetCount('Contribution', array(
375 'contact_id' => $this->_individualId
,
376 'contribution_status_id' => 'Pending',
378 $lineItem = $this->callAPISuccessGetSingle('line_item', array());
379 $this->assertEquals('50.00', $lineItem['unit_price']);
380 $this->assertEquals('50.00', $lineItem['line_total']);
381 $this->assertEquals(1, $lineItem['qty']);
382 $this->assertEquals(1, $lineItem['financial_type_id']);
383 $financialItem = $this->callAPISuccessGetSingle('financial_item', array(
384 'civicrm_line_item' => $lineItem['id'],
385 'entity_id' => $lineItem['id'],
387 $this->assertEquals('50.00', $financialItem['amount']);
388 $this->assertEquals(3, $financialItem['status_id']);
391 $this->fail('An expected exception has not been raised.');
395 * Test the submit function creates a billing address if provided.
397 public function testSubmitCreditCardWithBillingAddress() {
398 $form = new CRM_Contribute_Form_Contribution();
399 $form->testSubmit(array(
400 'total_amount' => 50,
401 'financial_type_id' => 1,
402 'receive_date' => '04/21/2015',
403 'receive_date_time' => '11:27PM',
404 'contact_id' => $this->_individualId
,
405 'payment_instrument_id' => array_search('Credit Card', $this->paymentInstruments
),
406 'payment_processor_id' => $this->paymentProcessorID
,
407 'credit_card_exp_date' => array('M' => 5, 'Y' => 2025),
408 'credit_card_number' => '411111111111111',
409 'billing_city-5' => 'Vancouver',
410 ), CRM_Core_Action
::ADD
,
413 $contribution = $this->callAPISuccessGetSingle('Contribution', array('return' => 'address_id'));
414 $this->assertNotEmpty($contribution['address_id']);
415 $this->callAPISuccessGetSingle('Address', array(
416 'city' => 'Vancouver',
417 'location_type_id' => 5,
418 'id' => $contribution['address_id'],
424 * Test the submit function does not create a billing address if no details provided.
426 public function testSubmitCreditCardWithNoBillingAddress() {
427 $form = new CRM_Contribute_Form_Contribution();
428 $form->testSubmit(array(
429 'total_amount' => 50,
430 'financial_type_id' => 1,
431 'receive_date' => '04/21/2015',
432 'receive_date_time' => '11:27PM',
433 'contact_id' => $this->_individualId
,
434 'payment_instrument_id' => array_search('Credit Card', $this->paymentInstruments
),
435 'payment_processor_id' => $this->paymentProcessorID
,
436 'credit_card_exp_date' => array('M' => 5, 'Y' => 2025),
437 'credit_card_number' => '411111111111111',
438 ), CRM_Core_Action
::ADD
,
441 $contribution = $this->callAPISuccessGetSingle('Contribution', array('return' => 'address_id'));
442 $this->assertEmpty($contribution['address_id']);
443 $this->callAPISuccessGetCount('Address', array(
444 'city' => 'Vancouver',
445 'location_type_id' => 5,
451 * Test the submit function on the contribution page.
453 public function testSubmitEmailReceipt() {
454 $form = new CRM_Contribute_Form_Contribution();
455 require_once 'CiviTest/CiviMailUtils.php';
456 $mut = new CiviMailUtils($this, TRUE);
457 $form->testSubmit(array(
458 'total_amount' => 50,
459 'financial_type_id' => 1,
460 'receive_date' => '04/21/2015',
461 'receive_date_time' => '11:27PM',
462 'contact_id' => $this->_individualId
,
463 'is_email_receipt' => TRUE,
464 'from_email_address' => 'test@test.com',
465 'contribution_status_id' => 1,
466 ), CRM_Core_Action
::ADD
);
467 $this->callAPISuccessGetCount('Contribution', array('contact_id' => $this->_individualId
), 1);
468 $mut->checkMailLog(array(
469 '<p>Please print this receipt for your records.</p>',
476 * Test that a contribution is assigned against a pledge.
478 public function testUpdatePledge() {
479 $pledge = $this->callAPISuccess('pledge', 'create', array(
480 'contact_id' => $this->_individualId
,
481 'pledge_create_date' => date('Ymd'),
482 'start_date' => date('Ymd'),
484 'pledge_status_id' => '2',
485 'pledge_financial_type_id' => '1',
486 'pledge_original_installment_amount' => 20,
487 'frequency_interval' => 5,
488 'frequency_unit' => 'year',
489 'frequency_day' => 15,
493 $pledgePaymentID = $this->callAPISuccess('pledge_payment', 'getvalue', array(
494 'pledge_id' => $pledge['id'],
495 'options' => array('limit' => 1),
498 $form = new CRM_Contribute_Form_Contribution();
499 $form->testSubmit(array(
500 'total_amount' => 50,
501 'financial_type_id' => 1,
502 'receive_date' => '04/21/2015',
503 'receive_date_time' => '11:27PM',
504 'contact_id' => $this->_individualId
,
505 'payment_instrument_id' => array_search('Check', $this->paymentInstruments
),
506 'pledge_payment_id' => $pledgePaymentID,
507 'contribution_status_id' => 1,
508 ), CRM_Core_Action
::ADD
);
509 $pledgePayment = $this->callAPISuccess('pledge_payment', 'getsingle', array('id' => $pledgePaymentID));
510 $this->assertNotEmpty($pledgePayment['contribution_id']);
511 $this->assertEquals($pledgePayment['actual_amount'], 50);
512 $this->assertEquals(1, $pledgePayment['status_id']);
516 * Test functions involving premiums.
518 public function testPremiumUpdate() {
519 $form = new CRM_Contribute_Form_Contribution();
520 $mut = new CiviMailUtils($this, TRUE);
521 $form->testSubmit(array(
522 'total_amount' => 50,
523 'financial_type_id' => 1,
524 'receive_date' => '04/21/2015',
525 'receive_date_time' => '11:27PM',
526 'contact_id' => $this->_individualId
,
527 'payment_instrument_id' => array_search('Check', $this->paymentInstruments
),
528 'contribution_status_id' => 1,
529 'product_name' => array($this->products
[0]['id'], 1),
530 'fulfilled_date' => '',
531 'is_email_receipt' => TRUE,
532 'from_email_address' => 'test@test.com',
533 ), CRM_Core_Action
::ADD
);
534 $contributionProduct = $this->callAPISuccess('contribution_product', 'getsingle', array());
535 $this->assertEquals('clumsy smurf', $contributionProduct['product_option']);
536 $mut->checkMailLog(array(
537 'Premium Information',
545 * Test functions involving premiums.
547 public function testPremiumUpdateCreditCard() {
548 $form = new CRM_Contribute_Form_Contribution();
549 $mut = new CiviMailUtils($this, TRUE);
550 $form->testSubmit(array(
551 'total_amount' => 50,
552 'financial_type_id' => 1,
553 'receive_date' => '04/21/2015',
554 'receive_date_time' => '11:27PM',
555 'contact_id' => $this->_individualId
,
556 'payment_instrument_id' => array_search('Check', $this->paymentInstruments
),
557 'contribution_status_id' => 1,
558 'product_name' => array($this->products
[0]['id'], 1),
559 'fulfilled_date' => '',
560 'is_email_receipt' => TRUE,
561 'from_email_address' => 'test@test.com',
562 'payment_processor_id' => $this->paymentProcessorID
,
563 'credit_card_exp_date' => array('M' => 5, 'Y' => 2026),
564 'credit_card_number' => '411111111111111',
565 ), CRM_Core_Action
::ADD
,
567 $contributionProduct = $this->callAPISuccess('contribution_product', 'getsingle', array());
568 $this->assertEquals('clumsy smurf', $contributionProduct['product_option']);
569 $mut->checkMailLog(array(
570 'Premium Information',
578 * Test the submit function on the contribution page.
580 public function testSubmitWithNote() {
581 $form = new CRM_Contribute_Form_Contribution();
582 $form->testSubmit(array(
583 'total_amount' => 50,
584 'financial_type_id' => 1,
585 'receive_date' => '04/21/2015',
586 'receive_date_time' => '11:27PM',
587 'contact_id' => $this->_individualId
,
588 'payment_instrument_id' => array_search('Check', $this->paymentInstruments
),
589 'contribution_status_id' => 1,
590 'note' => 'Super cool and interesting stuff',
592 CRM_Core_Action
::ADD
);
593 $this->callAPISuccessGetCount('Contribution', array('contact_id' => $this->_individualId
), 1);
594 $note = $this->callAPISuccessGetSingle('note', array('entity_table' => 'civicrm_contribution'));
595 $this->assertEquals($note['note'], 'Super cool and interesting stuff');
599 * Test the submit function on the contribution page.
601 public function testSubmitWithNoteCreditCard() {
602 $form = new CRM_Contribute_Form_Contribution();
604 $form->testSubmit(array(
605 'total_amount' => 50,
606 'financial_type_id' => 1,
607 'receive_date' => '04/21/2015',
608 'receive_date_time' => '11:27PM',
609 'contact_id' => $this->_individualId
,
610 'payment_instrument_id' => array_search('Check', $this->paymentInstruments
),
611 'contribution_status_id' => 1,
612 'note' => 'Super cool and interesting stuff',
613 ) +
$this->getCreditCardParams(),
614 CRM_Core_Action
::ADD
);
615 $this->callAPISuccessGetCount('Contribution', array('contact_id' => $this->_individualId
), 1);
616 $note = $this->callAPISuccessGetSingle('note', array('entity_table' => 'civicrm_contribution'));
617 $this->assertEquals($note['note'], 'Super cool and interesting stuff');
621 * Test the submit function on the contribution page.
623 public function testSubmitUpdate() {
624 $form = new CRM_Contribute_Form_Contribution();
626 $form->testSubmit(array(
627 'total_amount' => 50,
628 'financial_type_id' => 1,
629 'receive_date' => '04/21/2015',
630 'receive_date_time' => '11:27PM',
631 'contact_id' => $this->_individualId
,
632 'payment_instrument_id' => array_search('Check', $this->paymentInstruments
),
633 'contribution_status_id' => 1,
636 CRM_Core_Action
::ADD
);
637 $contribution = $this->callAPISuccessGetSingle('Contribution', array('contact_id' => $this->_individualId
));
638 $form->testSubmit(array(
639 'total_amount' => 45,
641 'financial_type_id' => 1,
642 'receive_date' => '04/21/2015',
643 'receive_date_time' => '11:27PM',
644 'contact_id' => $this->_individualId
,
645 'payment_instrument_id' => array_search('Check', $this->paymentInstruments
),
646 'contribution_status_id' => 1,
648 'id' => $contribution['id'],
650 CRM_Core_Action
::UPDATE
);
651 $contribution = $this->callAPISuccessGetSingle('Contribution', array('contact_id' => $this->_individualId
));
652 $this->assertEquals(45, (int) $contribution['total_amount']);
654 $financialTransactions = $this->callAPISuccess('FinancialTrxn', 'get', array('sequential' => TRUE));
655 $this->assertEquals(2, $financialTransactions['count']);
656 $this->assertEquals(50, $financialTransactions['values'][0]['total_amount']);
657 $this->assertEquals(-5, $financialTransactions['values'][1]['total_amount']);
658 $this->assertEquals(-5, $financialTransactions['values'][1]['net_amount']);
659 $lineItem = $this->callAPISuccessGetSingle('LineItem', array());
660 $this->assertEquals(45, $lineItem['line_total']);
664 * Get parameters for credit card submit calls.
667 * Credit card specific parameters.
669 protected function getCreditCardParams() {
671 'payment_processor_id' => $this->paymentProcessorID
,
672 'credit_card_exp_date' => array('M' => 5, 'Y' => 2012),
673 'credit_card_number' => '411111111111111',