984d21c4f9904865444ff1af6443c2ae222edfc3
[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 * Setup function.
82 */
83 public function setUp() {
84 parent::setUp();
85
86 $this->_apiversion = 3;
87 $this->_individualId = $this->individualCreate();
88 $paymentProcessor = $this->processorCreate();
89 $this->_params = array(
90 'contact_id' => $this->_individualId,
91 'receive_date' => '20120511',
92 'total_amount' => 100.00,
93 'financial_type_id' => $this->_financialTypeId,
94 'non_deductible_amount' => 10.00,
95 'fee_amount' => 5.00,
96 'net_amount' => 95.00,
97 'source' => 'SSF',
98 'contribution_status_id' => 1,
99 );
100 $this->_processorParams = array(
101 'domain_id' => 1,
102 'name' => 'Dummy',
103 'payment_processor_type_id' => 10,
104 'financial_account_id' => 12,
105 'is_active' => 1,
106 'user_name' => '',
107 'url_site' => 'http://dummy.com',
108 'url_recur' => 'http://dummy.com',
109 'billing_mode' => 1,
110 );
111 $this->_pageParams = array(
112 'title' => 'Test Contribution Page',
113 'financial_type_id' => 1,
114 'currency' => 'USD',
115 'financial_account_id' => 1,
116 'payment_processor' => $paymentProcessor->id,
117 'is_active' => 1,
118 'is_allow_other_amount' => 1,
119 'min_amount' => 10,
120 'max_amount' => 1000,
121 );
122 $instruments = $this->callAPISuccess('contribution', 'getoptions', array('field' => 'payment_instrument_id'));
123 $this->paymentInstruments = $instruments['values'];
124 $product1 = $this->callAPISuccess('product', 'create', array(
125 'name' => 'Smurf',
126 'options' => 'brainy smurf, clumsy smurf, papa smurf',
127 ));
128
129 $this->products[] = $product1['values'][$product1['id']];
130 }
131
132 /**
133 * Clean up after each test.
134 */
135 public function tearDown() {
136 $this->quickCleanUpFinancialEntities();
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('contact_id' => $this->_individualId), 1);
172 }
173
174 /**
175 * Test the submit function on the contribution page.
176 */
177 public function testSubmitEmailReceipt() {
178 $form = new CRM_Contribute_Form_Contribution();
179 require_once 'CiviTest/CiviMailUtils.php';
180 $mut = new CiviMailUtils($this, TRUE);
181 $form->testSubmit(array(
182 'total_amount' => 50,
183 'financial_type_id' => 1,
184 'receive_date' => '04/21/2015',
185 'receive_date_time' => '11:27PM',
186 'contact_id' => $this->_individualId,
187 'is_email_receipt' => TRUE,
188 'from_email_address' => 'test@test.com',
189 'contribution_status_id' => 1,
190 ), CRM_Core_Action::ADD);
191 $this->callAPISuccessGetCount('Contribution', array('contact_id' => $this->_individualId), 1);
192 $mut->checkMailLog(array(
193 '<p>Please print this receipt for your records.</p>',
194 )
195 );
196 $mut->stop();
197 }
198
199 /**
200 * Test that a contribution is assigned against a pledge.
201 */
202 public function testUpdatePledge() {
203 $pledge = $this->callAPISuccess('pledge', 'create', array(
204 'contact_id' => $this->_individualId,
205 'pledge_create_date' => date('Ymd'),
206 'start_date' => date('Ymd'),
207 'amount' => 100.00,
208 'pledge_status_id' => '2',
209 'pledge_financial_type_id' => '1',
210 'pledge_original_installment_amount' => 20,
211 'frequency_interval' => 5,
212 'frequency_unit' => 'year',
213 'frequency_day' => 15,
214 'installments' => 2,
215 'sequential' => 1,
216 ));
217 $pledgePaymentID = $this->callAPISuccess('pledge_payment', 'getvalue', array(
218 'pledge_id' => $pledge['id'],
219 'options' => array('limit' => 1),
220 'return' => 'id',
221 ));
222 $form = new CRM_Contribute_Form_Contribution();
223 $form->testSubmit(array(
224 'total_amount' => 50,
225 'financial_type_id' => 1,
226 'receive_date' => '04/21/2015',
227 'receive_date_time' => '11:27PM',
228 'contact_id' => $this->_individualId,
229 'payment_instrument_id' => array_search('Check', $this->paymentInstruments),
230 'pledge_payment_id' => $pledgePaymentID,
231 'contribution_status_id' => 1,
232 ), CRM_Core_Action::ADD);
233 $pledgePayment = $this->callAPISuccess('pledge_payment', 'getsingle', array('id' => $pledgePaymentID));
234 $this->assertNotEmpty($pledgePayment['contribution_id']);
235 $this->assertEquals($pledgePayment['actual_amount'], 50);
236 $this->assertEquals(1, $pledgePayment['status_id']);
237 }
238
239 /**
240 * Test functions involving premiums.
241 */
242 public function testPremiumUpdate() {
243 $form = new CRM_Contribute_Form_Contribution();
244 $form->testSubmit(array(
245 'total_amount' => 50,
246 'financial_type_id' => 1,
247 'receive_date' => '04/21/2015',
248 'receive_date_time' => '11:27PM',
249 'contact_id' => $this->_individualId,
250 'payment_instrument_id' => array_search('Check', $this->paymentInstruments),
251 'contribution_status_id' => 1,
252 'product_name' => array($this->products[0]['id'], 1),
253 'fulfilled_date' => '',
254 ), CRM_Core_Action::ADD);
255 $contributionProduct = $this->callAPISuccess('contribution_product', 'getsingle', array());
256 $this->assertEquals('clumsy smurf', $contributionProduct['product_option']);
257 }
258
259 }