add extra tests to submit functions
[civicrm-core.git] / tests / phpunit / CRM / Contribute / Form / ContributionTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
26 */
27
28 require_once 'CiviTest/CiviUnitTestCase.php';
29 require_once 'CiviTest/CiviMailUtils.php';
30
31
32 /**
33 * Test APIv3 civicrm_contribute_* functions
34 *
35 * @package CiviCRM_APIv3
36 * @subpackage API_Contribution
37 */
38 class CRM_Contribute_Form_ContributionTest extends CiviUnitTestCase {
39
40 /**
41 * Assume empty database with just civicrm_data.
42 */
43 protected $_individualId;
44 protected $_contribution;
45 protected $_financialTypeId = 1;
46 protected $_apiversion;
47 protected $_entity = 'Contribution';
48 protected $_params;
49 protected $_ids = array();
50 protected $_pageParams = array();
51
52 /**
53 * Parameters to create payment processor.
54 *
55 * @var array
56 */
57 protected $_processorParams = array();
58
59 /**
60 * ID of created event.
61 *
62 * @var int
63 */
64 protected $_eventID;
65
66 /**
67 * Payment instrument mapping.
68 *
69 * @var array
70 */
71 protected $paymentInstruments = array();
72
73 /**
74 * Products.
75 *
76 * @var array
77 */
78 protected $products = array();
79
80 /**
81 * Dummy payment processor.
82 *
83 * @var CRM_Core_Payment_Dummy
84 */
85 protected $paymentProcessor;
86
87 /**
88 * Setup function.
89 */
90 public function setUp() {
91 $this->_apiversion = 3;
92 parent::setUp();
93 $this->createLoggedInUser();
94
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,
104 'source' => 'SSF',
105 'contribution_status_id' => 1,
106 );
107 $this->_processorParams = array(
108 'domain_id' => 1,
109 'name' => 'Dummy',
110 'payment_processor_type_id' => 10,
111 'financial_account_id' => 12,
112 'is_active' => 1,
113 'user_name' => '',
114 'url_site' => 'http://dummy.com',
115 'url_recur' => 'http://dummy.com',
116 'billing_mode' => 1,
117 );
118
119 $instruments = $this->callAPISuccess('contribution', 'getoptions', array('field' => 'payment_instrument_id'));
120 $this->paymentInstruments = $instruments['values'];
121 $product1 = $this->callAPISuccess('product', 'create', array(
122 'name' => 'Smurf',
123 'options' => 'brainy smurf, clumsy smurf, papa smurf',
124 ));
125
126 $this->products[] = $product1['values'][$product1['id']];
127 $this->paymentProcessor = $this->processorCreate();
128
129 }
130
131 /**
132 * Clean up after each test.
133 */
134 public function tearDown() {
135 $this->quickCleanUpFinancialEntities();
136 $this->quickCleanup(array('civicrm_note'));
137 }
138
139 /**
140 * Test the submit function on the contribution page.
141 */
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,
152 ),
153 CRM_Core_Action::ADD);
154 $this->callAPISuccessGetCount('Contribution', array('contact_id' => $this->_individualId), 1);
155 }
156
157 /**
158 * Test the submit function on the contribution page.
159 */
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',
174 ),
175 1);
176 }
177
178 /**
179 * Test the submit function with an invalid payment.
180 *
181 * We expect the contribution to be created but left pending. The payment has failed.
182 *
183 * Test covers CRM-16417 change to keep failed transactions.
184 *
185 * We are left with
186 * - 1 Contribution with status = Pending
187 * - 1 Line item
188 * - 1 civicrm_financial_item. This is linked to the line item and has a status of 3
189 */
190 public function testSubmitCreditCardInvalid() {
191 $form = new CRM_Contribute_Form_Contribution();
192 $this->paymentProcessor->setDoDirectPaymentResult(array('is_error' => 1));
193 try {
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,
205 'live');
206 }
207 catch (\Civi\Payment\Exception\PaymentProcessorException $e) {
208 $this->callAPISuccessGetCount('Contribution', array(
209 'contact_id' => $this->_individualId,
210 'contribution_status_id' => 'Pending',
211 ), 1);
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'],
220 ));
221 $this->assertEquals('50.00', $financialItem['amount']);
222 $this->assertEquals(3, $financialItem['status_id']);
223 return;
224 }
225 $this->fail('An expected exception has not been raised.');
226 }
227
228 /**
229 * Test the submit function on the contribution page.
230 */
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>',
248 )
249 );
250 $mut->stop();
251 }
252
253 /**
254 * Test that a contribution is assigned against a pledge.
255 */
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'),
261 'amount' => 100.00,
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,
268 'installments' => 2,
269 'sequential' => 1,
270 ));
271 $pledgePaymentID = $this->callAPISuccess('pledge_payment', 'getvalue', array(
272 'pledge_id' => $pledge['id'],
273 'options' => array('limit' => 1),
274 'return' => 'id',
275 ));
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']);
291 }
292
293 /**
294 * Test functions involving premiums.
295 */
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',
316 'Smurf',
317 'clumsy smurf',
318 ));
319 $mut->stop();
320 }
321
322 /**
323 * Test functions involving premiums.
324 */
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,
344 'live');
345 $contributionProduct = $this->callAPISuccess('contribution_product', 'getsingle', array());
346 $this->assertEquals('clumsy smurf', $contributionProduct['product_option']);
347 $mut->checkMailLog(array(
348 'Premium Information',
349 'Smurf',
350 'clumsy smurf',
351 ));
352 $mut->stop();
353 }
354
355 /**
356 * Test the submit function on the contribution page.
357 */
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',
369 ),
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');
374 }
375
376 /**
377 * Test the submit function on the contribution page.
378 */
379 public function testSubmitWithNoteCreditCard() {
380 $form = new CRM_Contribute_Form_Contribution();
381
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');
396 }
397
398 /**
399 * Test the submit function on the contribution page.
400 */
401 public function testSubmitUpdate() {
402 $form = new CRM_Contribute_Form_Contribution();
403
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,
412 'price_set_id' => 0,
413 ),
414 CRM_Core_Action::ADD);
415 $contribution = $this->callAPISuccessGetSingle('Contribution', array('contact_id' => $this->_individualId));
416 $form->testSubmit(array(
417 'total_amount' => 45,
418 'net_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,
425 'price_set_id' => 0,
426 'id' => $contribution['id'],
427 ),
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']);
436 }
437
438
439 /**
440 * Get parameters for credit card submit calls.
441 *
442 * @return array
443 * Credit card specific parameters.
444 */
445 protected function getCreditCardParams() {
446 return array(
447 'payment_processor_id' => $this->paymentProcessor->id,
448 'credit_card_exp_date' => array('M' => 5, 'Y' => 2012),
449 'credit_card_number' => '411111111111111',
450 );
451 }
452
453 }