Merge pull request #10085 from totten/master-assets
[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() {
c85f1674
SL
188 $mut = new CiviMailUtils($this, TRUE);
189 $mut->clearMessages(0);
f8453bef 190 $form = new CRM_Contribute_Form_Contribution();
191 $paymentProcessorID = $this->paymentProcessorCreate(array('is_test' => 0));
192 $form->_mode = 'Live';
16f3bd02 193 $error = FALSE;
f8453bef 194 try {
195 $form->testSubmit(array(
196 'total_amount' => 50,
197 'financial_type_id' => 1,
198 'receive_date' => '04/21/2015',
199 'receive_date_time' => '11:27PM',
200 'contact_id' => $this->_individualId,
f8453bef 201 'contribution_status_id' => 1,
202 'credit_card_number' => 4444333322221111,
203 'cvv2' => 123,
204 'credit_card_exp_date' => array(
205 'M' => 9,
206 'Y' => 2025,
207 ),
208 'credit_card_type' => 'Visa',
209 'billing_first_name' => 'Junko',
210 'billing_middle_name' => '',
211 'billing_last_name' => 'Adams',
212 'billing_street_address-5' => '790L Lincoln St S',
213 'billing_city-5' => 'Maryknoll',
214 'billing_state_province_id-5' => 1031,
215 'billing_postal_code-5' => 10545,
216 'billing_country_id-5' => 1228,
217 'frequency_interval' => 1,
218 'frequency_unit' => 'month',
219 'installments' => '',
220 'hidden_AdditionalDetail' => 1,
221 'hidden_Premium' => 1,
222 'from_email_address' => '"civi45" <civi45@civicrm.com>',
c85f1674 223 'is_email_receipt' => TRUE,
f8453bef 224 'receipt_date' => '',
225 'receipt_date_time' => '',
226 'payment_processor_id' => $paymentProcessorID,
227 'currency' => 'USD',
dc0ca56c 228 'source' => 'bob sled race',
f8453bef 229 ), CRM_Core_Action::ADD);
230 }
231 catch (Civi\Payment\Exception\PaymentProcessorException $e) {
16f3bd02 232 $error = TRUE;
f8453bef 233 }
632196ea 234
f8453bef 235 $this->callAPISuccessGetCount('Contribution', array(
236 'contact_id' => $this->_individualId,
5e67c92d 237 'contribution_status_id' => $error ? 'Failed' : 'Completed',
16f3bd02 238 'payment_instrument_id' => $this->callAPISuccessGetValue('PaymentProcessor', array(
239 'return' => 'payment_instrument_id',
240 'id' => $paymentProcessorID,
241 )),
f8453bef 242 ), 1);
dc0ca56c 243 $contact = $this->callAPISuccessGetSingle('Contact', array('id' => $this->_individualId));
48e27df7 244 $this->assertTrue(empty($contact['source']));
c85f1674
SL
245 if (!$error) {
246 $msgs = $mut->getAllMessages();
247 $this->assertEquals(1, count($msgs));
248 }
249 $mut->stop();
f8453bef 250 }
251
bcf19fd7
SL
252 /**
253 * Test the submit function on the contribution page
254 */
255 public function testSubmitCreditCardWithEmailReceipt() {
256 $mut = new CiviMailUtils($this, TRUE);
257 $mut->clearMessages(0);
258 $form = new CRM_Contribute_Form_Contribution();
259 $form->_mode = 'Live';
260 $error = FALSE;
261 try {
262 $form->testSubmit(array(
263 'total_amount' => 50,
264 'financial_type_id' => 1,
265 'receive_date' => '04/21/2015',
266 'receive_date_time' => '11:27PM',
267 'contact_id' => $this->_individualId,
268 'contribution_status_id' => 1,
269 'credit_card_number' => 4444333322221111,
270 'cvv2' => 123,
271 'credit_card_exp_date' => array(
272 'M' => 9,
273 'Y' => 2025,
274 ),
275 'credit_card_type' => 'Visa',
276 'billing_first_name' => 'Junko',
277 'billing_middle_name' => '',
278 'billing_last_name' => 'Adams',
279 'billing_street_address-5' => '790L Lincoln St S',
280 'billing_city-5' => 'Maryknoll',
281 'billing_state_province_id-5' => 1031,
282 'billing_postal_code-5' => 10545,
283 'billing_country_id-5' => 1228,
284 'frequency_interval' => 1,
285 'frequency_unit' => 'month',
286 'installments' => '',
287 'hidden_AdditionalDetail' => 1,
288 'hidden_Premium' => 1,
289 'from_email_address' => '"civi45" <civi45@civicrm.com>',
290 'is_email_receipt' => TRUE,
291 'receipt_date' => '',
292 'receipt_date_time' => '',
293 'payment_processor_id' => $this->paymentProcessorID,
294 'currency' => 'USD',
295 'source' => 'bob sled race',
296 ), CRM_Core_Action::ADD);
297 }
298 catch (Civi\Payment\Exception\PaymentProcessorException $e) {
299 $error = TRUE;
300 }
301 $this->callAPISuccessGetCount('Contribution', array(
302 'contact_id' => $this->_individualId,
303 'contribution_status_id' => $error ? 'Failed' : 'Completed',
304 'payment_instrument_id' => $this->callAPISuccessGetValue('PaymentProcessor', array(
305 'return' => 'payment_instrument_id',
306 'id' => $this->paymentProcessorID,
307 )),
308 ), 1);
309 $contact = $this->callAPISuccessGetSingle('Contact', array('id' => $this->_individualId));
310 $this->assertTrue(empty($contact['source']));
311 $msgs = $mut->getAllMessages();
312 $this->assertEquals(1, count($msgs));
313 $mut->stop();
314 }
315
316
8188f8ec
SL
317 /**
318 * Test the submit function on the contribution page.
319 */
320 public function testSubmitCreditCardNoReceipt() {
321 $mut = new CiviMailUtils($this, TRUE);
aaf44e0b 322 $mut->clearMessages();
8188f8ec 323 $form = new CRM_Contribute_Form_Contribution();
8188f8ec
SL
324 $form->_mode = 'Live';
325 $error = FALSE;
326 try {
327 $form->testSubmit(array(
328 'total_amount' => 60,
329 'financial_type_id' => 1,
330 'receive_date' => '04/21/2015',
331 'receive_date_time' => '11:27PM',
332 'contact_id' => $this->_individualId,
333 'contribution_status_id' => 1,
334 'credit_card_number' => 4444333322221111,
335 'cvv2' => 123,
336 'credit_card_exp_date' => array(
337 'M' => 9,
338 'Y' => 2025,
339 ),
340 'credit_card_type' => 'Visa',
341 'billing_first_name' => 'Junko',
342 'billing_middle_name' => '',
343 'billing_last_name' => 'Adams',
344 'billing_street_address-5' => '790L Lincoln St S',
345 'billing_city-5' => 'Maryknoll',
346 'billing_state_province_id-5' => 1031,
347 'billing_postal_code-5' => 10545,
348 'billing_country_id-5' => 1228,
349 'frequency_interval' => 1,
350 'frequency_unit' => 'month',
351 'installments' => '',
352 'hidden_AdditionalDetail' => 1,
353 'hidden_Premium' => 1,
354 'from_email_address' => '"civi45" <civi45@civicrm.com>',
aaf44e0b 355 'is_email_receipt' => FALSE,
8188f8ec
SL
356 'receipt_date' => '',
357 'receipt_date_time' => '',
bcf19fd7 358 'payment_processor_id' => $this->paymentProcessorID,
8188f8ec
SL
359 'currency' => 'USD',
360 'source' => 'bob sled race',
361 ), CRM_Core_Action::ADD);
362 }
363 catch (Civi\Payment\Exception\PaymentProcessorException $e) {
364 $error = TRUE;
365 }
366
367 $this->callAPISuccessGetCount('Contribution', array(
368 'contact_id' => $this->_individualId,
369 'contribution_status_id' => $error ? 'Failed' : 'Completed',
370 'payment_instrument_id' => $this->callAPISuccessGetValue('PaymentProcessor', array(
371 'return' => 'payment_instrument_id',
bcf19fd7 372 'id' => $this->paymentProcessorID,
8188f8ec
SL
373 )),
374 ), 1);
375 $contact = $this->callAPISuccessGetSingle('Contact', array('id' => $this->_individualId));
376 $this->assertTrue(empty($contact['source']));
377 $mut->assertMailLogEmpty();
378 $mut->stop();
379 }
380
22e39333 381 /**
382 * Test the submit function on the contribution page.
383 */
384 public function testSubmitCreditCardFee() {
385 $form = new CRM_Contribute_Form_Contribution();
22e39333 386 $this->paymentProcessor->setDoDirectPaymentResult(array('is_error' => 0, 'trxn_id' => 'tx', 'fee_amount' => .08));
387 $form->_mode = 'Live';
388 $form->testSubmit(array(
389 'total_amount' => 50,
390 'financial_type_id' => 1,
391 'receive_date' => '04/21/2015',
392 'receive_date_time' => '11:27PM',
393 'contact_id' => $this->_individualId,
394 'payment_instrument_id' => array_search('Credit Card', $this->paymentInstruments),
395 'contribution_status_id' => 1,
396 'credit_card_number' => 4444333322221111,
397 'cvv2' => 123,
398 'credit_card_exp_date' => array(
399 'M' => 9,
400 'Y' => 2025,
401 ),
402 'credit_card_type' => 'Visa',
403 'billing_first_name' => 'Junko',
404 'billing_middle_name' => '',
405 'billing_last_name' => 'Adams',
406 'billing_street_address-5' => '790L Lincoln St S',
407 'billing_city-5' => 'Maryknoll',
408 'billing_state_province_id-5' => 1031,
409 'billing_postal_code-5' => 10545,
410 'billing_country_id-5' => 1228,
411 'frequency_interval' => 1,
412 'frequency_unit' => 'month',
413 'installments' => '',
414 'hidden_AdditionalDetail' => 1,
415 'hidden_Premium' => 1,
416 'from_email_address' => '"civi45" <civi45@civicrm.com>',
417 'receipt_date' => '',
418 'receipt_date_time' => '',
419 'payment_processor_id' => $this->paymentProcessorID,
420 'currency' => 'USD',
421 'source' => '',
422 ), CRM_Core_Action::ADD);
423
424 $contribution = $this->callAPISuccessGetSingle('Contribution', array(
425 'contact_id' => $this->_individualId,
426 'contribution_status_id' => 'Completed',
427 ));
428 $this->assertEquals('50', $contribution['total_amount']);
429 $this->assertEquals(.08, $contribution['fee_amount']);
430 $this->assertEquals(49.92, $contribution['net_amount']);
431 $this->assertEquals('tx', $contribution['trxn_id']);
2b308818 432 $this->assertEmpty($contribution['amount_level']);
22e39333 433 }
434
11829025
JMW
435 /**
436 * Test a fully deductible contribution submitted by credit card (CRM-16669).
437 */
438 public function testSubmitCreditCardFullyDeductible() {
439 $form = new CRM_Contribute_Form_Contribution();
440 $form->_mode = 'Live';
441 $form->testSubmit(array(
442 'total_amount' => 50,
443 'financial_type_id' => 1,
444 'receive_date' => '04/21/2015',
445 'receive_date_time' => '11:27PM',
446 'contact_id' => $this->_individualId,
447 'payment_instrument_id' => array_search('Credit Card', $this->paymentInstruments),
448 'contribution_status_id' => 1,
449 'credit_card_number' => 4444333322221111,
450 'cvv2' => 123,
451 'credit_card_exp_date' => array(
452 'M' => 9,
453 'Y' => 2025,
454 ),
455 'credit_card_type' => 'Visa',
456 'billing_first_name' => 'Junko',
457 'billing_middle_name' => '',
458 'billing_last_name' => 'Adams',
459 'billing_street_address-5' => '790L Lincoln St S',
460 'billing_city-5' => 'Maryknoll',
461 'billing_state_province_id-5' => 1031,
462 'billing_postal_code-5' => 10545,
463 'billing_country_id-5' => 1228,
464 'frequency_interval' => 1,
465 'frequency_unit' => 'month',
466 'installments' => '',
467 'hidden_AdditionalDetail' => 1,
468 'hidden_Premium' => 1,
469 'from_email_address' => '"civi45" <civi45@civicrm.com>',
470 'receipt_date' => '',
471 'receipt_date_time' => '',
472 'payment_processor_id' => $this->paymentProcessorID,
473 'currency' => 'USD',
474 'source' => '',
475 ), CRM_Core_Action::ADD);
476
477 $contribution = $this->callAPISuccessGetSingle('Contribution', array(
478 'contact_id' => $this->_individualId,
479 'contribution_status_id' => 'Completed',
480 ));
481 $this->assertEquals('50', $contribution['total_amount']);
482 $this->assertEquals(0, $contribution['non_deductible_amount']);
483 }
484
7126b55c 485 /**
7758bd2b
EM
486 * Test the submit function with an invalid payment.
487 *
488 * We expect the contribution to be created but left pending. The payment has failed.
489 *
490 * Test covers CRM-16417 change to keep failed transactions.
491 *
492 * We are left with
493 * - 1 Contribution with status = Pending
494 * - 1 Line item
495 * - 1 civicrm_financial_item. This is linked to the line item and has a status of 3
7126b55c 496 */
7758bd2b 497 public function testSubmitCreditCardInvalid() {
7126b55c 498 $form = new CRM_Contribute_Form_Contribution();
7758bd2b
EM
499 $this->paymentProcessor->setDoDirectPaymentResult(array('is_error' => 1));
500 try {
501 $form->testSubmit(array(
502 'total_amount' => 50,
503 'financial_type_id' => 1,
504 'receive_date' => '04/21/2015',
505 'receive_date_time' => '11:27PM',
506 'contact_id' => $this->_individualId,
507 'payment_instrument_id' => array_search('Credit Card', $this->paymentInstruments),
22e39333 508 'payment_processor_id' => $this->paymentProcessorID,
7758bd2b
EM
509 'credit_card_exp_date' => array('M' => 5, 'Y' => 2012),
510 'credit_card_number' => '411111111111111',
511 ), CRM_Core_Action::ADD,
739a8336 512 'live'
513 );
7758bd2b
EM
514 }
515 catch (\Civi\Payment\Exception\PaymentProcessorException $e) {
40f5ec68
EM
516 $this->callAPISuccessGetCount('Contribution', array(
517 'contact_id' => $this->_individualId,
382c5680 518 'contribution_status_id' => 'Failed',
40f5ec68 519 ), 1);
7758bd2b
EM
520 $lineItem = $this->callAPISuccessGetSingle('line_item', array());
521 $this->assertEquals('50.00', $lineItem['unit_price']);
522 $this->assertEquals('50.00', $lineItem['line_total']);
523 $this->assertEquals(1, $lineItem['qty']);
524 $this->assertEquals(1, $lineItem['financial_type_id']);
525 $financialItem = $this->callAPISuccessGetSingle('financial_item', array(
526 'civicrm_line_item' => $lineItem['id'],
527 'entity_id' => $lineItem['id'],
528 ));
529 $this->assertEquals('50.00', $financialItem['amount']);
530 $this->assertEquals(3, $financialItem['status_id']);
531 return;
532 }
533 $this->fail('An expected exception has not been raised.');
7126b55c
EM
534 }
535
739a8336 536 /**
537 * Test the submit function creates a billing address if provided.
538 */
539 public function testSubmitCreditCardWithBillingAddress() {
540 $form = new CRM_Contribute_Form_Contribution();
541 $form->testSubmit(array(
542 'total_amount' => 50,
543 'financial_type_id' => 1,
544 'receive_date' => '04/21/2015',
545 'receive_date_time' => '11:27PM',
546 'contact_id' => $this->_individualId,
547 'payment_instrument_id' => array_search('Credit Card', $this->paymentInstruments),
22e39333 548 'payment_processor_id' => $this->paymentProcessorID,
739a8336 549 'credit_card_exp_date' => array('M' => 5, 'Y' => 2025),
550 'credit_card_number' => '411111111111111',
551 'billing_city-5' => 'Vancouver',
552 ), CRM_Core_Action::ADD,
553 'live'
554 );
555 $contribution = $this->callAPISuccessGetSingle('Contribution', array('return' => 'address_id'));
556 $this->assertNotEmpty($contribution['address_id']);
989a6bb8 557 // CRM-18490 : There is a unwanted test leakage due to below getsingle Api as it only fails in Jenkin
558 // for now we are only fetching address on based on Address ID (removed filter location_type_id and city)
739a8336 559 $this->callAPISuccessGetSingle('Address', array(
739a8336 560 'id' => $contribution['address_id'],
561 ));
562
563 }
564
565 /**
566 * Test the submit function does not create a billing address if no details provided.
567 */
568 public function testSubmitCreditCardWithNoBillingAddress() {
569 $form = new CRM_Contribute_Form_Contribution();
570 $form->testSubmit(array(
571 'total_amount' => 50,
572 'financial_type_id' => 1,
573 'receive_date' => '04/21/2015',
574 'receive_date_time' => '11:27PM',
575 'contact_id' => $this->_individualId,
576 'payment_instrument_id' => array_search('Credit Card', $this->paymentInstruments),
22e39333 577 'payment_processor_id' => $this->paymentProcessorID,
739a8336 578 'credit_card_exp_date' => array('M' => 5, 'Y' => 2025),
579 'credit_card_number' => '411111111111111',
580 ), CRM_Core_Action::ADD,
581 'live'
582 );
583 $contribution = $this->callAPISuccessGetSingle('Contribution', array('return' => 'address_id'));
584 $this->assertEmpty($contribution['address_id']);
585 $this->callAPISuccessGetCount('Address', array(
586 'city' => 'Vancouver',
587 'location_type_id' => 5,
588 ), 0);
739a8336 589 }
590
a084385f
EM
591 /**
592 * Test the submit function on the contribution page.
593 */
594 public function testSubmitEmailReceipt() {
595 $form = new CRM_Contribute_Form_Contribution();
a084385f
EM
596 $mut = new CiviMailUtils($this, TRUE);
597 $form->testSubmit(array(
598 'total_amount' => 50,
599 'financial_type_id' => 1,
600 'receive_date' => '04/21/2015',
601 'receive_date_time' => '11:27PM',
602 'contact_id' => $this->_individualId,
603 'is_email_receipt' => TRUE,
604 'from_email_address' => 'test@test.com',
5e27919e 605 'contribution_status_id' => 1,
31760f81 606 ), CRM_Core_Action::ADD);
a084385f
EM
607 $this->callAPISuccessGetCount('Contribution', array('contact_id' => $this->_individualId), 1);
608 $mut->checkMailLog(array(
609 '<p>Please print this receipt for your records.</p>',
610 )
611 );
612 $mut->stop();
613 }
df729ab1 614
0335c5f1 615 /**
616 * Ensure that price field are shown during pay later/pending Contribution
617 */
618 public function testEmailReceiptOnPayLater() {
619 $donationFT = CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_FinancialType', 'Donation', 'id', 'name');
620 $paramsSet = array(
621 'title' => 'Price Set' . substr(sha1(rand()), 0, 4),
622 'is_active' => TRUE,
623 'financial_type_id' => $donationFT,
624 'extends' => 2,
625 );
626 $paramsSet['name'] = CRM_Utils_String::titleToVar($paramsSet['title']);
627
628 $priceset = CRM_Price_BAO_PriceSet::create($paramsSet);
629 $priceSetId = $priceset->id;
630
631 //Checking for priceset added in the table.
632 $this->assertDBCompareValue('CRM_Price_BAO_PriceSet', $priceSetId, 'title',
633 'id', $paramsSet['title'], 'Check DB for created priceset'
634 );
635 $paramsField = array(
636 'label' => 'Price Field',
637 'name' => CRM_Utils_String::titleToVar('Price Field'),
638 'html_type' => 'CheckBox',
639 'option_label' => array('1' => 'Price Field 1', '2' => 'Price Field 2'),
640 'option_value' => array('1' => 100, '2' => 200),
641 'option_name' => array('1' => 'Price Field 1', '2' => 'Price Field 2'),
642 'option_weight' => array('1' => 1, '2' => 2),
643 'option_amount' => array('1' => 100, '2' => 200),
644 'is_display_amounts' => 1,
645 'weight' => 1,
646 'options_per_line' => 1,
647 'is_active' => array('1' => 1, '2' => 1),
648 'price_set_id' => $priceset->id,
649 'is_enter_qty' => 1,
650 'financial_type_id' => $donationFT,
651 );
652 $priceField = CRM_Price_BAO_PriceField::create($paramsField);
653 $priceFieldValue = $this->callAPISuccess('PriceFieldValue', 'get', array('price_field_id' => $priceField->id));
654
655 $params = array(
656 'total_amount' => 100,
657 'financial_type_id' => $donationFT,
658 'receive_date' => '04/21/2015',
659 'receive_date_time' => '11:27PM',
660 'contact_id' => $this->_individualId,
661 'is_email_receipt' => TRUE,
662 'from_email_address' => 'test@test.com',
663 'price_set_id' => $priceSetId,
f527e012 664 'contribution_status_id' => CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id', 'Pending'),
0335c5f1 665 );
666
667 foreach ($priceFieldValue['values'] as $id => $price) {
668 if ($price['amount'] == 100) {
669 $params['price_' . $priceField->id] = array($id => 1);
670 }
671 }
672 $form = new CRM_Contribute_Form_Contribution();
673 $mut = new CiviMailUtils($this, TRUE);
674 $form->_priceSet = current(CRM_Price_BAO_PriceSet::getSetDetail($priceSetId));
675 $form->testSubmit($params, CRM_Core_Action::ADD);
676
677 $mut->checkMailLog(array(
678 'Financial Type: Donation
679---------------------------------------------------------
680Item Qty Each Total
681----------------------------------------------------------
682Price Field - Price Field 1 1 $ 100.00 $ 100.00
683',
684 )
685 );
686 $mut->stop();
687 }
688
31760f81
EM
689 /**
690 * Test that a contribution is assigned against a pledge.
691 */
692 public function testUpdatePledge() {
693 $pledge = $this->callAPISuccess('pledge', 'create', array(
694 'contact_id' => $this->_individualId,
695 'pledge_create_date' => date('Ymd'),
696 'start_date' => date('Ymd'),
697 'amount' => 100.00,
698 'pledge_status_id' => '2',
699 'pledge_financial_type_id' => '1',
700 'pledge_original_installment_amount' => 20,
701 'frequency_interval' => 5,
702 'frequency_unit' => 'year',
703 'frequency_day' => 15,
704 'installments' => 2,
705 'sequential' => 1,
706 ));
707 $pledgePaymentID = $this->callAPISuccess('pledge_payment', 'getvalue', array(
708 'pledge_id' => $pledge['id'],
709 'options' => array('limit' => 1),
710 'return' => 'id',
711 ));
712 $form = new CRM_Contribute_Form_Contribution();
713 $form->testSubmit(array(
714 'total_amount' => 50,
715 'financial_type_id' => 1,
716 'receive_date' => '04/21/2015',
717 'receive_date_time' => '11:27PM',
718 'contact_id' => $this->_individualId,
719 'payment_instrument_id' => array_search('Check', $this->paymentInstruments),
720 'pledge_payment_id' => $pledgePaymentID,
5e27919e 721 'contribution_status_id' => 1,
31760f81
EM
722 ), CRM_Core_Action::ADD);
723 $pledgePayment = $this->callAPISuccess('pledge_payment', 'getsingle', array('id' => $pledgePaymentID));
724 $this->assertNotEmpty($pledgePayment['contribution_id']);
725 $this->assertEquals($pledgePayment['actual_amount'], 50);
726 $this->assertEquals(1, $pledgePayment['status_id']);
727 }
ef353929 728
3e6a1f4a
EM
729 /**
730 * Test functions involving premiums.
731 */
732 public function testPremiumUpdate() {
733 $form = new CRM_Contribute_Form_Contribution();
43bf07d6 734 $mut = new CiviMailUtils($this, TRUE);
3e6a1f4a
EM
735 $form->testSubmit(array(
736 'total_amount' => 50,
737 'financial_type_id' => 1,
738 'receive_date' => '04/21/2015',
739 'receive_date_time' => '11:27PM',
740 'contact_id' => $this->_individualId,
741 'payment_instrument_id' => array_search('Check', $this->paymentInstruments),
742 'contribution_status_id' => 1,
743 'product_name' => array($this->products[0]['id'], 1),
744 'fulfilled_date' => '',
43bf07d6
EM
745 'is_email_receipt' => TRUE,
746 'from_email_address' => 'test@test.com',
3e6a1f4a
EM
747 ), CRM_Core_Action::ADD);
748 $contributionProduct = $this->callAPISuccess('contribution_product', 'getsingle', array());
749 $this->assertEquals('clumsy smurf', $contributionProduct['product_option']);
43bf07d6
EM
750 $mut->checkMailLog(array(
751 'Premium Information',
752 'Smurf',
753 'clumsy smurf',
754 ));
755 $mut->stop();
3e6a1f4a
EM
756 }
757
39f47c0d
EM
758 /**
759 * Test functions involving premiums.
760 */
761 public function testPremiumUpdateCreditCard() {
762 $form = new CRM_Contribute_Form_Contribution();
763 $mut = new CiviMailUtils($this, TRUE);
764 $form->testSubmit(array(
765 'total_amount' => 50,
766 'financial_type_id' => 1,
767 'receive_date' => '04/21/2015',
768 'receive_date_time' => '11:27PM',
769 'contact_id' => $this->_individualId,
770 'payment_instrument_id' => array_search('Check', $this->paymentInstruments),
771 'contribution_status_id' => 1,
772 'product_name' => array($this->products[0]['id'], 1),
773 'fulfilled_date' => '',
774 'is_email_receipt' => TRUE,
775 'from_email_address' => 'test@test.com',
22e39333 776 'payment_processor_id' => $this->paymentProcessorID,
7758bd2b 777 'credit_card_exp_date' => array('M' => 5, 'Y' => 2026),
39f47c0d
EM
778 'credit_card_number' => '411111111111111',
779 ), CRM_Core_Action::ADD,
780 'live');
781 $contributionProduct = $this->callAPISuccess('contribution_product', 'getsingle', array());
782 $this->assertEquals('clumsy smurf', $contributionProduct['product_option']);
783 $mut->checkMailLog(array(
784 'Premium Information',
785 'Smurf',
786 'clumsy smurf',
787 ));
788 $mut->stop();
789 }
790
945f423d
EM
791 /**
792 * Test the submit function on the contribution page.
793 */
794 public function testSubmitWithNote() {
795 $form = new CRM_Contribute_Form_Contribution();
796 $form->testSubmit(array(
797 'total_amount' => 50,
798 'financial_type_id' => 1,
799 'receive_date' => '04/21/2015',
800 'receive_date_time' => '11:27PM',
801 'contact_id' => $this->_individualId,
802 'payment_instrument_id' => array_search('Check', $this->paymentInstruments),
803 'contribution_status_id' => 1,
804 'note' => 'Super cool and interesting stuff',
805 ),
806 CRM_Core_Action::ADD);
807 $this->callAPISuccessGetCount('Contribution', array('contact_id' => $this->_individualId), 1);
808 $note = $this->callAPISuccessGetSingle('note', array('entity_table' => 'civicrm_contribution'));
809 $this->assertEquals($note['note'], 'Super cool and interesting stuff');
810 }
811
812 /**
813 * Test the submit function on the contribution page.
814 */
815 public function testSubmitWithNoteCreditCard() {
816 $form = new CRM_Contribute_Form_Contribution();
817
818 $form->testSubmit(array(
819 'total_amount' => 50,
820 'financial_type_id' => 1,
821 'receive_date' => '04/21/2015',
822 'receive_date_time' => '11:27PM',
823 'contact_id' => $this->_individualId,
824 'payment_instrument_id' => array_search('Check', $this->paymentInstruments),
825 'contribution_status_id' => 1,
826 'note' => 'Super cool and interesting stuff',
1ea0b4ac 827 ) + $this->getCreditCardParams(),
945f423d
EM
828 CRM_Core_Action::ADD);
829 $this->callAPISuccessGetCount('Contribution', array('contact_id' => $this->_individualId), 1);
830 $note = $this->callAPISuccessGetSingle('note', array('entity_table' => 'civicrm_contribution'));
831 $this->assertEquals($note['note'], 'Super cool and interesting stuff');
832 }
833
fa18b745 834 /**
71a3f5f9 835 * Test that if a negative contribution is entered it does not get reset to $0.
836 *
837 * Note that this fails locally for me & I believe there may be an issue for some sites
838 * with negative numbers. Grep for CRM-16460 to find the places I think that might
839 * be affected if you hit this.
fa18b745
K
840 */
841 public function testEnterNegativeContribution() {
842 $form = new CRM_Contribute_Form_Contribution();
843 $form->testSubmit(array(
844 'total_amount' => -5,
845 'financial_type_id' => 1,
846 'receive_date' => '04/24/2016',
847 'receive_date_time' => '11:27PM',
848 'contact_id' => $this->_individualId,
849 'payment_instrument_id' => array_search('Check', $this->paymentInstruments),
850 'contribution_status_id' => 1,
851 ),
852 CRM_Core_Action::ADD);
853 $this->callAPISuccessGetCount('Contribution', array('contact_id' => $this->_individualId), 1);
854
855 $contribution = $this->callAPISuccessGetSingle('Contribution', array(
856 'contact_id' => $this->_individualId,
857 ));
858 $this->assertEquals(-5, $contribution['total_amount']);
859 }
860
55e55e84 861 /**
862 * Test the submit function on the contribution page.
863 */
864 public function testSubmitUpdate() {
865 $form = new CRM_Contribute_Form_Contribution();
866
867 $form->testSubmit(array(
868 'total_amount' => 50,
869 'financial_type_id' => 1,
870 'receive_date' => '04/21/2015',
871 'receive_date_time' => '11:27PM',
872 'contact_id' => $this->_individualId,
873 'payment_instrument_id' => array_search('Check', $this->paymentInstruments),
874 'contribution_status_id' => 1,
875 'price_set_id' => 0,
876 ),
877 CRM_Core_Action::ADD);
878 $contribution = $this->callAPISuccessGetSingle('Contribution', array('contact_id' => $this->_individualId));
879 $form->testSubmit(array(
880 'total_amount' => 45,
881 'net_amount' => 45,
882 'financial_type_id' => 1,
883 'receive_date' => '04/21/2015',
884 'receive_date_time' => '11:27PM',
885 'contact_id' => $this->_individualId,
886 'payment_instrument_id' => array_search('Check', $this->paymentInstruments),
887 'contribution_status_id' => 1,
888 'price_set_id' => 0,
889 'id' => $contribution['id'],
890 ),
891 CRM_Core_Action::UPDATE);
da77cbbd
EM
892 $contribution = $this->callAPISuccessGetSingle('Contribution', array('contact_id' => $this->_individualId));
893 $this->assertEquals(45, (int) $contribution['total_amount']);
894
55e55e84 895 $financialTransactions = $this->callAPISuccess('FinancialTrxn', 'get', array('sequential' => TRUE));
896 $this->assertEquals(2, $financialTransactions['count']);
897 $this->assertEquals(50, $financialTransactions['values'][0]['total_amount']);
a4ed7706
EM
898 $this->assertEquals(-5, $financialTransactions['values'][1]['total_amount']);
899 $this->assertEquals(-5, $financialTransactions['values'][1]['net_amount']);
55e55e84 900 $lineItem = $this->callAPISuccessGetSingle('LineItem', array());
901 $this->assertEquals(45, $lineItem['line_total']);
902 }
903
945f423d
EM
904 /**
905 * Get parameters for credit card submit calls.
906 *
907 * @return array
908 * Credit card specific parameters.
909 */
1ea0b4ac 910 protected function getCreditCardParams() {
945f423d 911 return array(
22e39333 912 'payment_processor_id' => $this->paymentProcessorID,
945f423d
EM
913 'credit_card_exp_date' => array('M' => 5, 'Y' => 2012),
914 'credit_card_number' => '411111111111111',
915 );
916 }
945f423d 917
9beed6cf
PN
918 /**
919 * Test the submit function that completes the partially paid payment using Credit Card
920 */
921 public function testPartialPaymentWithCreditCard() {
922 // create a partially paid contribution by using back-office form
923 $form = new CRM_Contribute_Form_Contribution();
924 $form->testSubmit(
925 array(
926 'total_amount' => 50,
927 'financial_type_id' => 1,
928 'receive_date' => '04/21/2015',
929 'receive_date_time' => '11:27PM',
930 'contact_id' => $this->_individualId,
931 'payment_instrument_id' => array_search('Check', $this->paymentInstruments),
932 'check_number' => substr(sha1(rand()), 0, 7),
933 'billing_city-5' => 'Vancouver',
934 'contribution_status_id' => CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id', 'Partially paid'),
935 ), CRM_Core_Action::ADD
936 );
937
938 $contribution = $this->callAPISuccessGetSingle('Contribution', array());
939 $this->assertNotEmpty($contribution);
940 $this->assertEquals('Partially paid', $contribution['contribution_status']);
941 // pay additional amount by using Credit Card
942 $form = new CRM_Contribute_Form_AdditionalPayment();
943 $form->testSubmit(array(
944 'contribution_id' => $contribution['id'],
945 'contact_id' => $this->_individualId,
946 'total_amount' => 50,
947 'currency' => 'USD',
948 'financial_type_id' => 1,
949 'receive_date' => '04/21/2015',
950 'receive_date_time' => '11:27PM',
951 'contact_id' => $this->_individualId,
952 'payment_instrument_id' => array_search('Credit card', $this->paymentInstruments),
953 'payment_processor_id' => $this->paymentProcessorID,
954 'credit_card_exp_date' => array('M' => 5, 'Y' => 2025),
955 'credit_card_number' => '411111111111111',
956 'cvv2' => 234,
957 'credit_card_type' => 'Visa',
958 'billing_city-5' => 'Vancouver',
959 'billing_state_province_id-5' => 1059,
960 'billing_postal_code-5' => 1321312,
961 'billing_country_id-5' => 1228,
962 'trxn_date' => '2017-04-11 13:05:11',
963 ), 'live');
964 $contribution = $this->callAPISuccessGetSingle('Contribution', array());
965 $this->assertNotEmpty($contribution);
966 $this->assertEquals('Completed', $contribution['contribution_status']);
967 }
968
7a1f3919
PN
969 /**
970 * Test the submit function for FT with tax.
971 */
972 public function testSubmitSaleTax() {
973 $this->enableTaxAndInvoicing();
974 $this->relationForFinancialTypeWithFinancialAccount($this->_financialTypeId);
975 $form = new CRM_Contribute_Form_Contribution();
976
977 $form->testSubmit(array(
978 'total_amount' => 100,
979 'financial_type_id' => $this->_financialTypeId,
980 'receive_date' => '04/21/2015',
981 'receive_date_time' => '11:27PM',
982 'contact_id' => $this->_individualId,
983 'payment_instrument_id' => array_search('Check', $this->paymentInstruments),
984 'contribution_status_id' => 1,
985 'price_set_id' => 0,
986 ),
987 CRM_Core_Action::ADD
988 );
989 $contribution = $this->callAPISuccessGetSingle('Contribution',
990 array(
991 'contact_id' => $this->_individualId,
992 'return' => array('tax_amount', 'total_amount'),
993 )
994 );
995 $this->assertEquals(110, $contribution['total_amount']);
996 $this->assertEquals(10, $contribution['tax_amount']);
997 $this->callAPISuccessGetCount('FinancialTrxn', array(), 1);
998 $this->callAPISuccessGetCount('FinancialItem', array(), 2);
999 $lineItem = $this->callAPISuccessGetSingle('LineItem', array('contribution_id' => $contribution['id']));
1000 $this->assertEquals(100, $lineItem['line_total']);
1001 $this->assertEquals(10, $lineItem['tax_amount']);
1002 }
1003
1ec04220
PN
1004 /**
1005 * Test the submit function for FT without tax.
1006 */
1007 public function testSubmitWithOutSaleTax() {
1008 $this->enableTaxAndInvoicing();
1009 $this->relationForFinancialTypeWithFinancialAccount($this->_financialTypeId);
1010 $form = new CRM_Contribute_Form_Contribution();
1011
1012 $form->testSubmit(array(
1013 'total_amount' => 100,
1014 'financial_type_id' => 3,
1015 'receive_date' => '04/21/2015',
1016 'receive_date_time' => '11:27PM',
1017 'contact_id' => $this->_individualId,
1018 'payment_instrument_id' => array_search('Check', $this->paymentInstruments),
1019 'contribution_status_id' => 1,
1020 'price_set_id' => 0,
1021 ),
1022 CRM_Core_Action::ADD
1023 );
1024 $contribution = $this->callAPISuccessGetSingle('Contribution',
1025 array(
1026 'contact_id' => $this->_individualId,
1027 'return' => array('tax_amount', 'total_amount'),
1028 )
1029 );
1030 $this->assertEquals(100, $contribution['total_amount']);
1031 $this->assertEquals(NULL, $contribution['tax_amount']);
1032 $this->callAPISuccessGetCount('FinancialTrxn', array(), 1);
1033 $this->callAPISuccessGetCount('FinancialItem', array(), 1);
1034 $lineItem = $this->callAPISuccessGetSingle(
1035 'LineItem',
1036 array(
1037 'contribution_id' => $contribution['id'],
1038 'return' => array('line_total', 'tax_amount'),
1039 )
1040 );
1041 $this->assertEquals(100, $lineItem['line_total']);
1042 $this->assertTrue(empty($lineItem['tax_amount']));
1043 }
1044
f1662cbd 1045 /**
9308f1c2 1046 * Create a contribution & then edit it via backoffice form, checking tax with: default price_set
f1662cbd 1047 *
1048 * @throws \Exception
1049 */
335c41e7 1050 public function testReSubmitSaleTax() {
335c41e7
K
1051 $this->enableTaxAndInvoicing();
1052 $this->relationForFinancialTypeWithFinancialAccount($this->_financialTypeId);
cf28d075 1053 list($form, $contribution) = $this->doInitialSubmit();
1054 $this->assertEquals(110, $contribution['total_amount']);
1055 $this->assertEquals(10, $contribution['tax_amount']);
1056 $this->assertEquals(110, $contribution['net_amount']);
335c41e7 1057
cf28d075 1058 $mut = new CiviMailUtils($this, TRUE);
1059 // Testing here if when we edit something trivial like adding a check_number tax, net, total amount stay the same:
335c41e7 1060 $form->testSubmit(array(
cf28d075 1061 'id' => $contribution['id'],
1062 'tax_amount' => $contribution['tax_amount'],
1063 'financial_type_id' => $contribution['financial_type_id'],
1064 'receive_date' => $contribution['receive_date'],
1065 'payment_instrument_id' => $contribution['payment_instrument_id'],
335c41e7 1066 'price_set_id' => 0,
cf28d075 1067 'check_number' => 12345,
1068 'contribution_status_id' => 1,
1069 'is_email_receipt' => 1,
1070 'from_email_address' => 'demo@example.com',
335c41e7 1071 ),
cf28d075 1072 CRM_Core_Action::UPDATE
335c41e7
K
1073 );
1074 $contribution = $this->callAPISuccessGetSingle('Contribution',
1075 array(
1076 'contribution_id' => 1,
1077 'return' => array('tax_amount', 'total_amount', 'net_amount', 'financial_type_id', 'receive_date', 'payment_instrument_id'),
1078 )
1079 );
f1662cbd 1080 $this->assertEquals(110, $contribution['total_amount']);
1081 $this->assertEquals(10, $contribution['tax_amount']);
9308f1c2 1082 $this->assertEquals(110, $contribution['net_amount']);
335c41e7 1083
cf28d075 1084 $strings = array(
1085 'Total Tax Amount : $ 10.00',
1086 'Total Amount : $ 110.00',
1087 'Date Received: April 21st, 2015',
1088 'Paid By: Check',
1089 'Check Number: 12345',
1090 );
1091
1092 $mut->checkMailLog($strings);
1093 $this->callAPISuccessGetCount('FinancialTrxn', array(), 3);
1094 $items = $this->callAPISuccess('FinancialItem', 'get', array('sequential' => 1));
1095 $this->assertEquals(2, $items['count']);
1096 $this->assertEquals('Contribution Amount', $items['values'][0]['description']);
1097 $this->assertEquals('Sales Tax', $items['values'][1]['description']);
1098
1099 $this->assertEquals(100, $items['values'][0]['amount']);
1100 $this->assertEquals(10, $items['values'][1]['amount']);
1101 }
1102
1103 /**
1104 * Create a contribution & then edit it via backoffice form, checking tax with: default price_set
1105 *
1106 * @throws \Exception
1107 */
1108 public function testReSubmitSaleTaxAlteredAmount() {
1109 $this->enableTaxAndInvoicing();
1110 $this->relationForFinancialTypeWithFinancialAccount($this->_financialTypeId);
1111 list($form, $contribution) = $this->doInitialSubmit();
1112
f1662cbd 1113 $mut = new CiviMailUtils($this, TRUE);
9308f1c2 1114 // Testing here if when we edit something trivial like adding a check_number tax, net, total amount stay the same:
335c41e7 1115 $form->testSubmit(array(
f1662cbd 1116 'id' => $contribution['id'],
cf28d075 1117 'total_amount' => 200,
1118 'tax_amount' => 20,
335c41e7
K
1119 'financial_type_id' => $contribution['financial_type_id'],
1120 'receive_date' => $contribution['receive_date'],
1121 'payment_instrument_id' => $contribution['payment_instrument_id'],
1122 'price_set_id' => 0,
1123 'check_number' => 12345,
f1662cbd 1124 'contribution_status_id' => 1,
1125 'is_email_receipt' => 1,
1126 'from_email_address' => 'demo@example.com',
335c41e7
K
1127 ),
1128 CRM_Core_Action::UPDATE
1129 );
1130 $contribution = $this->callAPISuccessGetSingle('Contribution',
1131 array(
9308f1c2
K
1132 'contribution_id' => 1,
1133 'return' => array('tax_amount', 'total_amount', 'net_amount', 'financial_type_id', 'receive_date', 'payment_instrument_id'),
335c41e7
K
1134 )
1135 );
cf28d075 1136 $this->assertEquals(220, $contribution['total_amount']);
1137 $this->assertEquals(20, $contribution['tax_amount']);
1138 $this->assertEquals(220, $contribution['net_amount']);
9308f1c2 1139
f1662cbd 1140 $strings = array(
cf28d075 1141 'Total Tax Amount : $ 20.00',
1142 'Total Amount : $ 220.00',
f1662cbd 1143 'Date Received: April 21st, 2015',
1144 'Paid By: Check',
1145 'Check Number: 12345',
1146 );
335c41e7 1147
f1662cbd 1148 $mut->checkMailLog($strings);
cf28d075 1149 $this->callAPISuccessGetCount('FinancialTrxn', array(), 4);
1150 $items = $this->callAPISuccess('FinancialItem', 'get', array('sequential' => 1));
1151 $this->assertEquals(4, $items['count']);
1152 $this->assertEquals('Contribution Amount', $items['values'][0]['description']);
1153 $this->assertEquals('Sales Tax', $items['values'][1]['description']);
1154 $this->assertEquals('Contribution Amount', $items['values'][0]['description']);
1155 $this->assertEquals('Sales Tax', $items['values'][1]['description']);
1156
1157 $this->assertEquals(100, $items['values'][0]['amount']);
1158 $this->assertEquals(10, $items['values'][1]['amount']);
7c63ab21 1159 // @todo currently its $110 which is incorrect, the proper value should be $200
1160 // $this->assertEquals(200, $items['values'][2]['amount']);
cf28d075 1161 $this->assertEquals(20, $items['values'][3]['amount']);
1162 }
1163
1164 /**
1165 * Do the first contributions, in preparation for an edit-submit.
1166 *
1167 * @return array
1168 *
1169 * @throws \Exception
1170 */
1171 protected function doInitialSubmit() {
1172 $form = new CRM_Contribute_Form_Contribution();
1173
1174 $form->testSubmit(array(
1175 'total_amount' => 100,
1176 'financial_type_id' => $this->_financialTypeId,
1177 'receive_date' => '04/21/2015',
1178 'receive_date_time' => '11:27PM',
1179 'contact_id' => $this->_individualId,
1180 'payment_instrument_id' => array_search('Check', $this->paymentInstruments),
1181 'contribution_status_id' => 1,
1182 'price_set_id' => 0,
1183 ),
1184 CRM_Core_Action::ADD
1185 );
1186 $contribution = $this->callAPISuccessGetSingle('Contribution',
1187 array(
1188 'contribution_id' => 1,
1189 'return' => array(
1190 'tax_amount',
1191 'total_amount',
1192 'net_amount',
1193 'financial_type_id',
1194 'receive_date',
1195 'payment_instrument_id',
1196 ),
1197 )
1198 );
1199 $this->assertEquals(110, $contribution['total_amount']);
1200 $this->assertEquals(10, $contribution['tax_amount']);
1201 $this->assertEquals(110, $contribution['net_amount']);
1202 return array($form, $contribution);
335c41e7
K
1203 }
1204
a55e39e9 1205 /**
1206 * function to test card_type and pan truncation.
1207 */
1208 public function testCardTypeAndPanTruncation() {
1209 $form = new CRM_Contribute_Form_Contribution();
1210 $form->testSubmit(
1211 array(
1212 'total_amount' => 100,
1213 'financial_type_id' => 3,
1214 'receive_date' => '04/21/2015',
1215 'receive_date_time' => '11:27PM',
1216 'contact_id' => $this->_individualId,
1217 'payment_instrument_id' => array_search('Credit Card', $this->paymentInstruments),
1218 'contribution_status_id' => 1,
1219 'credit_card_type' => 'Visa',
1220 'pan_truncation' => 4567,
1221 'price_set_id' => 0,
1222 ),
1223 CRM_Core_Action::ADD
1224 );
1225 $contribution = $this->callAPISuccessGetSingle('Contribution',
1226 array(
1227 'contact_id' => $this->_individualId,
1228 'return' => array('id'),
1229 )
1230 );
1231 $lastFinancialTrxnId = CRM_Core_BAO_FinancialTrxn::getFinancialTrxnId($contribution['id'], 'DESC');
1232 $financialTrxn = $this->callAPISuccessGetSingle(
1233 'FinancialTrxn',
1234 array(
1235 'id' => $lastFinancialTrxnId['financialTrxnId'],
1236 'return' => array('card_type_id.label', 'pan_truncation'),
1237 )
1238 );
1239 $this->assertEquals(CRM_Utils_Array::value('card_type_id.label', $financialTrxn), 'Visa');
1240 $this->assertEquals(CRM_Utils_Array::value('pan_truncation', $financialTrxn), 4567);
1241 }
1242
1243 /**
1244 * function to test card_type and pan truncation.
1245 */
1246 public function testCardTypeAndPanTruncationLiveMode() {
1247 $visaID = CRM_Core_PseudoConstant::getKey('CRM_Core_BAO_FinancialTrxn', 'card_type_id', 'Visa');
1248 $form = new CRM_Contribute_Form_Contribution();
1249 $form->_mode = 'Live';
a55e39e9 1250 $form->testSubmit(
1251 array(
1252 'total_amount' => 50,
1253 'financial_type_id' => 1,
1254 'receive_date' => '04/21/2015',
1255 'receive_date_time' => '11:27PM',
1256 'contact_id' => $this->_individualId,
1257 'credit_card_number' => 4444333322221111,
1258 'payment_instrument_id' => array_search('Credit Card', $this->paymentInstruments),
1259 'cvv2' => 123,
1260 'credit_card_exp_date' => array(
1261 'M' => 9,
1262 'Y' => date('Y', strtotime('+5 years')),
1263 ),
1264 'credit_card_type' => 'Visa',
1265 'billing_first_name' => 'Junko',
1266 'billing_middle_name' => '',
1267 'billing_last_name' => 'Adams',
1268 'billing_street_address-5' => '790L Lincoln St S',
1269 'billing_city-5' => 'Maryknoll',
1270 'billing_state_province_id-5' => 1031,
1271 'billing_postal_code-5' => 10545,
1272 'billing_country_id-5' => 1228,
1273 'frequency_interval' => 1,
1274 'frequency_unit' => 'month',
1275 'installments' => '',
1276 'hidden_AdditionalDetail' => 1,
1277 'hidden_Premium' => 1,
1278 'from_email_address' => '"civi45" <civi45@civicrm.com>',
1279 'receipt_date' => '',
1280 'receipt_date_time' => '',
1281 'payment_processor_id' => $this->paymentProcessorID,
1282 'currency' => 'USD',
1283 'source' => 'bob sled race',
1284 ),
1285 CRM_Core_Action::ADD
1286 );
1287 $contribution = $this->callAPISuccessGetSingle('Contribution', array('contact_id' => $this->_individualId));
1288 $lastFinancialTrxnId = CRM_Core_BAO_FinancialTrxn::getFinancialTrxnId($contribution['id'], 'DESC');
1289 $financialTrxn = $this->callAPISuccessGetSingle(
1290 'FinancialTrxn',
1291 array(
1292 'id' => $lastFinancialTrxnId['financialTrxnId'],
1293 'return' => array('card_type_id', 'pan_truncation'),
1294 )
1295 );
016569ee 1296 $this->assertEquals($visaID, $financialTrxn['card_type_id']);
1297 $this->assertEquals(1111, $financialTrxn['pan_truncation']);
a55e39e9 1298 }
1299
2dcbf765 1300}