3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
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 +--------------------------------------------------------------------+
28 require_once 'CiviTest/CiviUnitTestCase.php';
29 require_once 'CiviTest/CiviMailUtils.php';
33 * Test APIv3 civicrm_contribute_* functions
35 * @package CiviCRM_APIv3
36 * @subpackage API_Contribution
38 class CRM_Contribute_Form_ContributionTest
extends CiviUnitTestCase
{
41 * Assume empty database with just civicrm_data.
43 protected $_individualId;
44 protected $_contribution;
45 protected $_financialTypeId = 1;
46 protected $_apiversion;
47 protected $_entity = 'Contribution';
49 protected $_ids = array();
50 protected $_pageParams = array();
53 * Parameters to create payment processor.
57 protected $_processorParams = array();
60 * ID of created event.
67 * Payment instrument mapping.
71 protected $paymentInstruments = array();
78 protected $products = array();
81 * Dummy payment processor.
83 * @var CRM_Core_Payment_Dummy
85 protected $paymentProcessor;
90 public function setUp() {
91 $this->_apiversion
= 3;
93 $this->createLoggedInUser();
95 $this->_individualId
= $this->individualCreate();
96 $this->_params
= array(
97 'contact_id' => $this->_individualId
,
98 'receive_date' => '20120511',
99 'total_amount' => 100.00,
100 'financial_type_id' => $this->_financialTypeId
,
101 'non_deductible_amount' => 10.00,
102 'fee_amount' => 5.00,
103 'net_amount' => 95.00,
105 'contribution_status_id' => 1,
107 $this->_processorParams
= array(
110 'payment_processor_type_id' => 10,
111 'financial_account_id' => 12,
114 'url_site' => 'http://dummy.com',
115 'url_recur' => 'http://dummy.com',
119 $instruments = $this->callAPISuccess('contribution', 'getoptions', array('field' => 'payment_instrument_id'));
120 $this->paymentInstruments
= $instruments['values'];
121 $product1 = $this->callAPISuccess('product', 'create', array(
123 'options' => 'brainy smurf, clumsy smurf, papa smurf',
126 $this->products
[] = $product1['values'][$product1['id']];
127 $this->paymentProcessor
= $this->processorCreate();
132 * Clean up after each test.
134 public function tearDown() {
135 $this->quickCleanUpFinancialEntities();
136 $this->quickCleanup(array('civicrm_note'));
140 * Test the submit function on the contribution page.
142 public function testSubmit() {
143 $form = new CRM_Contribute_Form_Contribution();
144 $form->testSubmit(array(
145 'total_amount' => 50,
146 'financial_type_id' => 1,
147 'receive_date' => '04/21/2015',
148 'receive_date_time' => '11:27PM',
149 'contact_id' => $this->_individualId
,
150 'payment_instrument_id' => array_search('Check', $this->paymentInstruments
),
151 'contribution_status_id' => 1,
153 CRM_Core_Action
::ADD
);
154 $this->callAPISuccessGetCount('Contribution', array('contact_id' => $this->_individualId
), 1);
158 * Test the submit function on the contribution page.
160 public function testSubmitCreditCard() {
161 $form = new CRM_Contribute_Form_Contribution();
162 $form->testSubmit(array(
163 'total_amount' => 50,
164 'financial_type_id' => 1,
165 'receive_date' => '04/21/2015',
166 'receive_date_time' => '11:27PM',
167 'contact_id' => $this->_individualId
,
168 'payment_instrument_id' => array_search('Credit Card', $this->paymentInstruments
),
169 'contribution_status_id' => 1,
170 ), CRM_Core_Action
::ADD
);
171 $this->callAPISuccessGetCount('Contribution', array(
172 'contact_id' => $this->_individualId
,
173 'contribution_status_id' => 'Completed',
179 * Test the submit function with an invalid payment.
181 * We expect the contribution to be created but left pending. The payment has failed.
183 * Test covers CRM-16417 change to keep failed transactions.
186 * - 1 Contribution with status = Pending
188 * - 1 civicrm_financial_item. This is linked to the line item and has a status of 3
190 public function testSubmitCreditCardInvalid() {
191 $form = new CRM_Contribute_Form_Contribution();
192 $this->paymentProcessor
->setDoDirectPaymentResult(array('is_error' => 1));
194 $form->testSubmit(array(
195 'total_amount' => 50,
196 'financial_type_id' => 1,
197 'receive_date' => '04/21/2015',
198 'receive_date_time' => '11:27PM',
199 'contact_id' => $this->_individualId
,
200 'payment_instrument_id' => array_search('Credit Card', $this->paymentInstruments
),
201 'payment_processor_id' => $this->paymentProcessor
->id
,
202 'credit_card_exp_date' => array('M' => 5, 'Y' => 2012),
203 'credit_card_number' => '411111111111111',
204 ), CRM_Core_Action
::ADD
,
207 catch (\Civi\Payment\Exception\PaymentProcessorException
$e) {
208 $this->callAPISuccessGetCount('Contribution', array(
209 'contact_id' => $this->_individualId
,
210 'contribution_status_id' => 'Pending',
212 $lineItem = $this->callAPISuccessGetSingle('line_item', array());
213 $this->assertEquals('50.00', $lineItem['unit_price']);
214 $this->assertEquals('50.00', $lineItem['line_total']);
215 $this->assertEquals(1, $lineItem['qty']);
216 $this->assertEquals(1, $lineItem['financial_type_id']);
217 $financialItem = $this->callAPISuccessGetSingle('financial_item', array(
218 'civicrm_line_item' => $lineItem['id'],
219 'entity_id' => $lineItem['id'],
221 $this->assertEquals('50.00', $financialItem['amount']);
222 $this->assertEquals(3, $financialItem['status_id']);
225 $this->fail('An expected exception has not been raised.');
229 * Test the submit function on the contribution page.
231 public function testSubmitEmailReceipt() {
232 $form = new CRM_Contribute_Form_Contribution();
233 require_once 'CiviTest/CiviMailUtils.php';
234 $mut = new CiviMailUtils($this, TRUE);
235 $form->testSubmit(array(
236 'total_amount' => 50,
237 'financial_type_id' => 1,
238 'receive_date' => '04/21/2015',
239 'receive_date_time' => '11:27PM',
240 'contact_id' => $this->_individualId
,
241 'is_email_receipt' => TRUE,
242 'from_email_address' => 'test@test.com',
243 'contribution_status_id' => 1,
244 ), CRM_Core_Action
::ADD
);
245 $this->callAPISuccessGetCount('Contribution', array('contact_id' => $this->_individualId
), 1);
246 $mut->checkMailLog(array(
247 '<p>Please print this receipt for your records.</p>',
254 * Test that a contribution is assigned against a pledge.
256 public function testUpdatePledge() {
257 $pledge = $this->callAPISuccess('pledge', 'create', array(
258 'contact_id' => $this->_individualId
,
259 'pledge_create_date' => date('Ymd'),
260 'start_date' => date('Ymd'),
262 'pledge_status_id' => '2',
263 'pledge_financial_type_id' => '1',
264 'pledge_original_installment_amount' => 20,
265 'frequency_interval' => 5,
266 'frequency_unit' => 'year',
267 'frequency_day' => 15,
271 $pledgePaymentID = $this->callAPISuccess('pledge_payment', 'getvalue', array(
272 'pledge_id' => $pledge['id'],
273 'options' => array('limit' => 1),
276 $form = new CRM_Contribute_Form_Contribution();
277 $form->testSubmit(array(
278 'total_amount' => 50,
279 'financial_type_id' => 1,
280 'receive_date' => '04/21/2015',
281 'receive_date_time' => '11:27PM',
282 'contact_id' => $this->_individualId
,
283 'payment_instrument_id' => array_search('Check', $this->paymentInstruments
),
284 'pledge_payment_id' => $pledgePaymentID,
285 'contribution_status_id' => 1,
286 ), CRM_Core_Action
::ADD
);
287 $pledgePayment = $this->callAPISuccess('pledge_payment', 'getsingle', array('id' => $pledgePaymentID));
288 $this->assertNotEmpty($pledgePayment['contribution_id']);
289 $this->assertEquals($pledgePayment['actual_amount'], 50);
290 $this->assertEquals(1, $pledgePayment['status_id']);
294 * Test functions involving premiums.
296 public function testPremiumUpdate() {
297 $form = new CRM_Contribute_Form_Contribution();
298 $mut = new CiviMailUtils($this, TRUE);
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('Check', $this->paymentInstruments
),
306 'contribution_status_id' => 1,
307 'product_name' => array($this->products
[0]['id'], 1),
308 'fulfilled_date' => '',
309 'is_email_receipt' => TRUE,
310 'from_email_address' => 'test@test.com',
311 ), CRM_Core_Action
::ADD
);
312 $contributionProduct = $this->callAPISuccess('contribution_product', 'getsingle', array());
313 $this->assertEquals('clumsy smurf', $contributionProduct['product_option']);
314 $mut->checkMailLog(array(
315 'Premium Information',
323 * Test functions involving premiums.
325 public function testPremiumUpdateCreditCard() {
326 $form = new CRM_Contribute_Form_Contribution();
327 $mut = new CiviMailUtils($this, TRUE);
328 $form->testSubmit(array(
329 'total_amount' => 50,
330 'financial_type_id' => 1,
331 'receive_date' => '04/21/2015',
332 'receive_date_time' => '11:27PM',
333 'contact_id' => $this->_individualId
,
334 'payment_instrument_id' => array_search('Check', $this->paymentInstruments
),
335 'contribution_status_id' => 1,
336 'product_name' => array($this->products
[0]['id'], 1),
337 'fulfilled_date' => '',
338 'is_email_receipt' => TRUE,
339 'from_email_address' => 'test@test.com',
340 'payment_processor_id' => $this->paymentProcessor
->id
,
341 'credit_card_exp_date' => array('M' => 5, 'Y' => 2026),
342 'credit_card_number' => '411111111111111',
343 ), CRM_Core_Action
::ADD
,
345 $contributionProduct = $this->callAPISuccess('contribution_product', 'getsingle', array());
346 $this->assertEquals('clumsy smurf', $contributionProduct['product_option']);
347 $mut->checkMailLog(array(
348 'Premium Information',
356 * Test the submit function on the contribution page.
358 public function testSubmitWithNote() {
359 $form = new CRM_Contribute_Form_Contribution();
360 $form->testSubmit(array(
361 'total_amount' => 50,
362 'financial_type_id' => 1,
363 'receive_date' => '04/21/2015',
364 'receive_date_time' => '11:27PM',
365 'contact_id' => $this->_individualId
,
366 'payment_instrument_id' => array_search('Check', $this->paymentInstruments
),
367 'contribution_status_id' => 1,
368 'note' => 'Super cool and interesting stuff',
370 CRM_Core_Action
::ADD
);
371 $this->callAPISuccessGetCount('Contribution', array('contact_id' => $this->_individualId
), 1);
372 $note = $this->callAPISuccessGetSingle('note', array('entity_table' => 'civicrm_contribution'));
373 $this->assertEquals($note['note'], 'Super cool and interesting stuff');
377 * Test the submit function on the contribution page.
379 public function testSubmitWithNoteCreditCard() {
380 $form = new CRM_Contribute_Form_Contribution();
382 $form->testSubmit(array(
383 'total_amount' => 50,
384 'financial_type_id' => 1,
385 'receive_date' => '04/21/2015',
386 'receive_date_time' => '11:27PM',
387 'contact_id' => $this->_individualId
,
388 'payment_instrument_id' => array_search('Check', $this->paymentInstruments
),
389 'contribution_status_id' => 1,
390 'note' => 'Super cool and interesting stuff',
391 ) +
$this->getCreditCardParams(),
392 CRM_Core_Action
::ADD
);
393 $this->callAPISuccessGetCount('Contribution', array('contact_id' => $this->_individualId
), 1);
394 $note = $this->callAPISuccessGetSingle('note', array('entity_table' => 'civicrm_contribution'));
395 $this->assertEquals($note['note'], 'Super cool and interesting stuff');
399 * Test the submit function on the contribution page.
401 public function testSubmitUpdate() {
402 $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('Check', $this->paymentInstruments
),
411 'contribution_status_id' => 1,
414 CRM_Core_Action
::ADD
);
415 $contribution = $this->callAPISuccessGetSingle('Contribution', array('contact_id' => $this->_individualId
));
416 $form->testSubmit(array(
417 'total_amount' => 45,
419 'financial_type_id' => 1,
420 'receive_date' => '04/21/2015',
421 'receive_date_time' => '11:27PM',
422 'contact_id' => $this->_individualId
,
423 'payment_instrument_id' => array_search('Check', $this->paymentInstruments
),
424 'contribution_status_id' => 1,
426 'id' => $contribution['id'],
428 CRM_Core_Action
::UPDATE
);
429 $this->callAPISuccessGetCount('Contribution', array('contact_id' => $this->_individualId
), 1);
430 $financialTransactions = $this->callAPISuccess('FinancialTrxn', 'get', array('sequential' => TRUE));
431 $this->assertEquals(2, $financialTransactions['count']);
432 $this->assertEquals(50, $financialTransactions['values'][0]['total_amount']);
433 $this->assertEquals(45, $financialTransactions['values'][1]['total_amount']);
434 $lineItem = $this->callAPISuccessGetSingle('LineItem', array());
435 $this->assertEquals(45, $lineItem['line_total']);
440 * Get parameters for credit card submit calls.
443 * Credit card specific parameters.
445 protected function getCreditCardParams() {
447 'payment_processor_id' => $this->paymentProcessor
->id
,
448 'credit_card_exp_date' => array('M' => 5, 'Y' => 2012),
449 'credit_card_number' => '411111111111111',