CRM-16367 phpcs fixes
[civicrm-core.git] / tests / phpunit / CRM / Contribute / Form / ContributionTest.php
CommitLineData
a084385f
EM
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
28require_once 'CiviTest/CiviUnitTestCase.php';
29require_once 'CiviTest/CiviMailUtils.php';
30
31
32/**
33 * Test APIv3 civicrm_contribute_* functions
34 *
35 * @package CiviCRM_APIv3
36 * @subpackage API_Contribution
37 */
38class 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
3e6a1f4a
EM
73 /**
74 * Products.
75 *
76 * @var array
77 */
78 protected $products = array();
79
39f47c0d
EM
80 /**
81 * Dummy payment processor.
82 *
83 * @var array
84 */
85 protected $paymentProcessor = array();
86
a084385f
EM
87 /**
88 * Setup function.
89 */
90 public function setUp() {
39f47c0d 91 $this->_apiversion = 3;
a084385f 92 parent::setUp();
39f47c0d 93 $this->createLoggedInUser();
a084385f 94
a084385f
EM
95 $this->_individualId = $this->individualCreate();
96 $paymentProcessor = $this->processorCreate();
97 $this->_params = array(
98 'contact_id' => $this->_individualId,
99 'receive_date' => '20120511',
100 'total_amount' => 100.00,
101 'financial_type_id' => $this->_financialTypeId,
102 'non_deductible_amount' => 10.00,
103 'fee_amount' => 5.00,
104 'net_amount' => 95.00,
105 'source' => 'SSF',
106 'contribution_status_id' => 1,
107 );
108 $this->_processorParams = array(
109 'domain_id' => 1,
110 'name' => 'Dummy',
111 'payment_processor_type_id' => 10,
112 'financial_account_id' => 12,
113 'is_active' => 1,
114 'user_name' => '',
115 'url_site' => 'http://dummy.com',
116 'url_recur' => 'http://dummy.com',
117 'billing_mode' => 1,
118 );
119 $this->_pageParams = array(
120 'title' => 'Test Contribution Page',
121 'financial_type_id' => 1,
122 'currency' => 'USD',
123 'financial_account_id' => 1,
124 'payment_processor' => $paymentProcessor->id,
125 'is_active' => 1,
126 'is_allow_other_amount' => 1,
127 'min_amount' => 10,
128 'max_amount' => 1000,
129 );
130 $instruments = $this->callAPISuccess('contribution', 'getoptions', array('field' => 'payment_instrument_id'));
131 $this->paymentInstruments = $instruments['values'];
3e6a1f4a
EM
132 $product1 = $this->callAPISuccess('product', 'create', array(
133 'name' => 'Smurf',
134 'options' => 'brainy smurf, clumsy smurf, papa smurf',
135 ));
136
137 $this->products[] = $product1['values'][$product1['id']];
39f47c0d
EM
138 $this->paymentProcessor = $this->processorCreate();
139
a084385f
EM
140 }
141
142 /**
143 * Clean up after each test.
144 */
145 public function tearDown() {
146 $this->quickCleanUpFinancialEntities();
945f423d 147 $this->quickCleanup(array('civicrm_note'));
a084385f
EM
148 }
149
150 /**
151 * Test the submit function on the contribution page.
152 */
153 public function testSubmit() {
154 $form = new CRM_Contribute_Form_Contribution();
155 $form->testSubmit(array(
156 'total_amount' => 50,
157 'financial_type_id' => 1,
158 'receive_date' => '04/21/2015',
159 'receive_date_time' => '11:27PM',
160 'contact_id' => $this->_individualId,
161 'payment_instrument_id' => array_search('Check', $this->paymentInstruments),
5e27919e 162 'contribution_status_id' => 1,
31760f81
EM
163 ),
164 CRM_Core_Action::ADD);
a084385f
EM
165 $this->callAPISuccessGetCount('Contribution', array('contact_id' => $this->_individualId), 1);
166 }
167
168 /**
169 * Test the submit function on the contribution page.
170 */
171 public function testSubmitCreditCard() {
172 $form = new CRM_Contribute_Form_Contribution();
173 $form->testSubmit(array(
174 'total_amount' => 50,
175 'financial_type_id' => 1,
176 'receive_date' => '04/21/2015',
177 'receive_date_time' => '11:27PM',
178 'contact_id' => $this->_individualId,
179 'payment_instrument_id' => array_search('Credit Card', $this->paymentInstruments),
5e27919e 180 'contribution_status_id' => 1,
31760f81 181 ), CRM_Core_Action::ADD);
a084385f
EM
182 $this->callAPISuccessGetCount('Contribution', array('contact_id' => $this->_individualId), 1);
183 }
184
185 /**
186 * Test the submit function on the contribution page.
187 */
188 public function testSubmitEmailReceipt() {
189 $form = new CRM_Contribute_Form_Contribution();
190 require_once 'CiviTest/CiviMailUtils.php';
191 $mut = new CiviMailUtils($this, TRUE);
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 'is_email_receipt' => TRUE,
199 'from_email_address' => 'test@test.com',
5e27919e 200 'contribution_status_id' => 1,
31760f81 201 ), CRM_Core_Action::ADD);
a084385f
EM
202 $this->callAPISuccessGetCount('Contribution', array('contact_id' => $this->_individualId), 1);
203 $mut->checkMailLog(array(
204 '<p>Please print this receipt for your records.</p>',
205 )
206 );
207 $mut->stop();
208 }
df729ab1 209
31760f81
EM
210 /**
211 * Test that a contribution is assigned against a pledge.
212 */
213 public function testUpdatePledge() {
214 $pledge = $this->callAPISuccess('pledge', 'create', array(
215 'contact_id' => $this->_individualId,
216 'pledge_create_date' => date('Ymd'),
217 'start_date' => date('Ymd'),
218 'amount' => 100.00,
219 'pledge_status_id' => '2',
220 'pledge_financial_type_id' => '1',
221 'pledge_original_installment_amount' => 20,
222 'frequency_interval' => 5,
223 'frequency_unit' => 'year',
224 'frequency_day' => 15,
225 'installments' => 2,
226 'sequential' => 1,
227 ));
228 $pledgePaymentID = $this->callAPISuccess('pledge_payment', 'getvalue', array(
229 'pledge_id' => $pledge['id'],
230 'options' => array('limit' => 1),
231 'return' => 'id',
232 ));
233 $form = new CRM_Contribute_Form_Contribution();
234 $form->testSubmit(array(
235 'total_amount' => 50,
236 'financial_type_id' => 1,
237 'receive_date' => '04/21/2015',
238 'receive_date_time' => '11:27PM',
239 'contact_id' => $this->_individualId,
240 'payment_instrument_id' => array_search('Check', $this->paymentInstruments),
241 'pledge_payment_id' => $pledgePaymentID,
5e27919e 242 'contribution_status_id' => 1,
31760f81
EM
243 ), CRM_Core_Action::ADD);
244 $pledgePayment = $this->callAPISuccess('pledge_payment', 'getsingle', array('id' => $pledgePaymentID));
245 $this->assertNotEmpty($pledgePayment['contribution_id']);
246 $this->assertEquals($pledgePayment['actual_amount'], 50);
247 $this->assertEquals(1, $pledgePayment['status_id']);
248 }
ef353929 249
3e6a1f4a
EM
250 /**
251 * Test functions involving premiums.
252 */
253 public function testPremiumUpdate() {
254 $form = new CRM_Contribute_Form_Contribution();
43bf07d6 255 $mut = new CiviMailUtils($this, TRUE);
3e6a1f4a
EM
256 $form->testSubmit(array(
257 'total_amount' => 50,
258 'financial_type_id' => 1,
259 'receive_date' => '04/21/2015',
260 'receive_date_time' => '11:27PM',
261 'contact_id' => $this->_individualId,
262 'payment_instrument_id' => array_search('Check', $this->paymentInstruments),
263 'contribution_status_id' => 1,
264 'product_name' => array($this->products[0]['id'], 1),
265 'fulfilled_date' => '',
43bf07d6
EM
266 'is_email_receipt' => TRUE,
267 'from_email_address' => 'test@test.com',
3e6a1f4a
EM
268 ), CRM_Core_Action::ADD);
269 $contributionProduct = $this->callAPISuccess('contribution_product', 'getsingle', array());
270 $this->assertEquals('clumsy smurf', $contributionProduct['product_option']);
43bf07d6
EM
271 $mut->checkMailLog(array(
272 'Premium Information',
273 'Smurf',
274 'clumsy smurf',
275 ));
276 $mut->stop();
3e6a1f4a
EM
277 }
278
39f47c0d
EM
279 /**
280 * Test functions involving premiums.
281 */
282 public function testPremiumUpdateCreditCard() {
283 $form = new CRM_Contribute_Form_Contribution();
284 $mut = new CiviMailUtils($this, TRUE);
285 $form->testSubmit(array(
286 'total_amount' => 50,
287 'financial_type_id' => 1,
288 'receive_date' => '04/21/2015',
289 'receive_date_time' => '11:27PM',
290 'contact_id' => $this->_individualId,
291 'payment_instrument_id' => array_search('Check', $this->paymentInstruments),
292 'contribution_status_id' => 1,
293 'product_name' => array($this->products[0]['id'], 1),
294 'fulfilled_date' => '',
295 'is_email_receipt' => TRUE,
296 'from_email_address' => 'test@test.com',
297 'payment_processor_id' => $this->paymentProcessor->id,
298 'credit_card_exp_date' => array('M' => 5, 'Y' => 2012),
299 'credit_card_number' => '411111111111111',
300 ), CRM_Core_Action::ADD,
301 'live');
302 $contributionProduct = $this->callAPISuccess('contribution_product', 'getsingle', array());
303 $this->assertEquals('clumsy smurf', $contributionProduct['product_option']);
304 $mut->checkMailLog(array(
305 'Premium Information',
306 'Smurf',
307 'clumsy smurf',
308 ));
309 $mut->stop();
310 }
311
945f423d
EM
312 /**
313 * Test the submit function on the contribution page.
314 */
315 public function testSubmitWithNote() {
316 $form = new CRM_Contribute_Form_Contribution();
317 $form->testSubmit(array(
318 'total_amount' => 50,
319 'financial_type_id' => 1,
320 'receive_date' => '04/21/2015',
321 'receive_date_time' => '11:27PM',
322 'contact_id' => $this->_individualId,
323 'payment_instrument_id' => array_search('Check', $this->paymentInstruments),
324 'contribution_status_id' => 1,
325 'note' => 'Super cool and interesting stuff',
326 ),
327 CRM_Core_Action::ADD);
328 $this->callAPISuccessGetCount('Contribution', array('contact_id' => $this->_individualId), 1);
329 $note = $this->callAPISuccessGetSingle('note', array('entity_table' => 'civicrm_contribution'));
330 $this->assertEquals($note['note'], 'Super cool and interesting stuff');
331 }
332
333 /**
334 * Test the submit function on the contribution page.
335 */
336 public function testSubmitWithNoteCreditCard() {
337 $form = new CRM_Contribute_Form_Contribution();
338
339 $form->testSubmit(array(
340 'total_amount' => 50,
341 'financial_type_id' => 1,
342 'receive_date' => '04/21/2015',
343 'receive_date_time' => '11:27PM',
344 'contact_id' => $this->_individualId,
345 'payment_instrument_id' => array_search('Check', $this->paymentInstruments),
346 'contribution_status_id' => 1,
347 'note' => 'Super cool and interesting stuff',
1ea0b4ac 348 ) + $this->getCreditCardParams(),
945f423d
EM
349 CRM_Core_Action::ADD);
350 $this->callAPISuccessGetCount('Contribution', array('contact_id' => $this->_individualId), 1);
351 $note = $this->callAPISuccessGetSingle('note', array('entity_table' => 'civicrm_contribution'));
352 $this->assertEquals($note['note'], 'Super cool and interesting stuff');
353 }
354
355 /**
356 * Get parameters for credit card submit calls.
357 *
358 * @return array
359 * Credit card specific parameters.
360 */
1ea0b4ac 361 protected function getCreditCardParams() {
945f423d
EM
362 return array(
363 'payment_processor_id' => $this->paymentProcessor->id,
364 'credit_card_exp_date' => array('M' => 5, 'Y' => 2012),
365 'credit_card_number' => '411111111111111',
366 );
367 }
945f423d 368
2dcbf765 369}