Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
81621fee | 4 | | CiviCRM version 4.7 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
e7112fa7 | 6 | | Copyright CiviCRM LLC (c) 2004-2015 | |
6a488035 TO |
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 | +--------------------------------------------------------------------+ | |
d25dd0ee | 26 | */ |
6a488035 | 27 | |
6a488035 TO |
28 | /** |
29 | * Test APIv3 civicrm_contribute_* functions | |
30 | * | |
6c6e6187 TO |
31 | * @package CiviCRM_APIv3 |
32 | * @subpackage API_Contribution | |
6a488035 | 33 | */ |
6a488035 TO |
34 | class api_v3_ContributionTest extends CiviUnitTestCase { |
35 | ||
36 | /** | |
fe482240 | 37 | * Assume empty database with just civicrm_data. |
6a488035 TO |
38 | */ |
39 | protected $_individualId; | |
40 | protected $_contribution; | |
4ab7d517 | 41 | protected $_financialTypeId = 1; |
6a488035 TO |
42 | protected $_apiversion; |
43 | protected $_entity = 'Contribution'; | |
44 | public $debug = 0; | |
45 | protected $_params; | |
858d0bf8 EM |
46 | protected $_ids = array(); |
47 | protected $_pageParams = array(); | |
1eade77d | 48 | /** |
49 | * Payment processor ID (dummy processor). | |
50 | * | |
51 | * @var int | |
52 | */ | |
53 | protected $paymentProcessorID; | |
b7c9bc4c | 54 | |
1e52837d EM |
55 | /** |
56 | * Parameters to create payment processor. | |
57 | * | |
58 | * @var array | |
59 | */ | |
60 | protected $_processorParams = array(); | |
61 | ||
62 | /** | |
63 | * ID of created event. | |
64 | * | |
65 | * @var int | |
66 | */ | |
67 | protected $_eventID; | |
68 | ||
22f80e87 EM |
69 | /** |
70 | * Setup function. | |
71 | */ | |
00be9182 | 72 | public function setUp() { |
6a488035 TO |
73 | parent::setUp(); |
74 | ||
75 | $this->_apiversion = 3; | |
76 | $this->_individualId = $this->individualCreate(); | |
6a488035 TO |
77 | $this->_params = array( |
78 | 'contact_id' => $this->_individualId, | |
79 | 'receive_date' => '20120511', | |
80 | 'total_amount' => 100.00, | |
5896d037 | 81 | 'financial_type_id' => $this->_financialTypeId, |
6a488035 TO |
82 | 'non_deductible_amount' => 10.00, |
83 | 'fee_amount' => 5.00, | |
84 | 'net_amount' => 95.00, | |
85 | 'source' => 'SSF', | |
86 | 'contribution_status_id' => 1, | |
6a488035 TO |
87 | ); |
88 | $this->_processorParams = array( | |
89 | 'domain_id' => 1, | |
90 | 'name' => 'Dummy', | |
97502bac | 91 | 'payment_processor_type_id' => CRM_Core_PseudoConstant::getKey('CRM_Financial_BAO_PaymentProcessor', 'payment_processor_type_id', 'Dummy'), |
6a488035 TO |
92 | 'financial_account_id' => 12, |
93 | 'is_active' => 1, | |
94 | 'user_name' => '', | |
95 | 'url_site' => 'http://dummy.com', | |
96 | 'url_recur' => 'http://dummy.com', | |
97 | 'billing_mode' => 1, | |
98 | ); | |
1eade77d | 99 | $this->paymentProcessorID = $this->processorCreate(); |
6a488035 | 100 | $this->_pageParams = array( |
6a488035 TO |
101 | 'title' => 'Test Contribution Page', |
102 | 'financial_type_id' => 1, | |
103 | 'currency' => 'USD', | |
104 | 'financial_account_id' => 1, | |
1eade77d | 105 | 'payment_processor' => $this->paymentProcessorID, |
6a488035 TO |
106 | 'is_active' => 1, |
107 | 'is_allow_other_amount' => 1, | |
108 | 'min_amount' => 10, | |
109 | 'max_amount' => 1000, | |
5896d037 | 110 | ); |
6a488035 TO |
111 | } |
112 | ||
22f80e87 EM |
113 | /** |
114 | * Clean up after each test. | |
115 | */ | |
00be9182 | 116 | public function tearDown() { |
39d632fd | 117 | $this->quickCleanUpFinancialEntities(); |
225d474b | 118 | $this->quickCleanup(array('civicrm_uf_match')); |
6a488035 TO |
119 | } |
120 | ||
22f80e87 EM |
121 | /** |
122 | * Test Get. | |
123 | */ | |
00be9182 | 124 | public function testGetContribution() { |
6a488035 TO |
125 | $p = array( |
126 | 'contact_id' => $this->_individualId, | |
127 | 'receive_date' => '2010-01-20', | |
128 | 'total_amount' => 100.00, | |
4ab7d517 | 129 | 'financial_type_id' => $this->_financialTypeId, |
6a488035 TO |
130 | 'non_deductible_amount' => 10.00, |
131 | 'fee_amount' => 5.00, | |
132 | 'net_amount' => 95.00, | |
133 | 'trxn_id' => 23456, | |
134 | 'invoice_id' => 78910, | |
135 | 'source' => 'SSF', | |
136 | 'contribution_status_id' => 1, | |
6a488035 | 137 | ); |
2f45e1c2 | 138 | $this->_contribution = $this->callAPISuccess('contribution', 'create', $p); |
6a488035 TO |
139 | |
140 | $params = array( | |
141 | 'contribution_id' => $this->_contribution['id'], | |
6a488035 | 142 | ); |
e0e3c51b | 143 | |
2f45e1c2 | 144 | $contribution = $this->callAPIAndDocument('contribution', 'get', $params, __FUNCTION__, __FILE__); |
4ab7d517 | 145 | $financialParams['id'] = $this->_financialTypeId; |
6c6e6187 | 146 | $default = NULL; |
858d0bf8 | 147 | CRM_Financial_BAO_FinancialType::retrieve($financialParams, $default); |
2f45e1c2 | 148 | |
e0e3c51b | 149 | $this->assertEquals(1, $contribution['count']); |
2bfae985 | 150 | $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId); |
22f80e87 EM |
151 | // Note there was an assertion converting financial_type_id to 'Donation' which wasn't working. |
152 | // Passing back a string rather than an id seems like an error/cruft. | |
153 | // If it is to be introduced we should discuss. | |
6a488035 | 154 | $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'], 1); |
2bfae985 EM |
155 | $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00); |
156 | $this->assertEquals($contribution['values'][$contribution['id']]['non_deductible_amount'], 10.00); | |
157 | $this->assertEquals($contribution['values'][$contribution['id']]['fee_amount'], 5.00); | |
158 | $this->assertEquals($contribution['values'][$contribution['id']]['net_amount'], 95.00); | |
159 | $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 23456); | |
160 | $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 78910); | |
161 | $this->assertEquals($contribution['values'][$contribution['id']]['contribution_source'], 'SSF'); | |
162 | $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status'], 'Completed'); | |
22f80e87 | 163 | // Create a second contribution - we are testing that 'id' gets the right contribution id (not the contact id). |
6a488035 TO |
164 | $p['trxn_id'] = '3847'; |
165 | $p['invoice_id'] = '3847'; | |
166 | ||
2f45e1c2 | 167 | $contribution2 = $this->callAPISuccess('contribution', 'create', $p); |
168 | ||
22f80e87 | 169 | // Now we have 2 - test getcount. |
4ab7d517 | 170 | $contribution = $this->callAPISuccess('contribution', 'getcount', array()); |
6a488035 | 171 | $this->assertEquals(2, $contribution); |
22f80e87 | 172 | // Test id only format. |
4ab7d517 | 173 | $contribution = $this->callAPISuccess('contribution', 'get', array( |
174 | 'id' => $this->_contribution['id'], | |
175 | 'format.only_id' => 1, | |
176 | )); | |
22f80e87 EM |
177 | $this->assertEquals($this->_contribution['id'], $contribution, print_r($contribution, TRUE)); |
178 | // Test id only format. | |
2f45e1c2 | 179 | $contribution = $this->callAPISuccess('contribution', 'get', array( |
4ab7d517 | 180 | 'id' => $contribution2['id'], |
181 | 'format.only_id' => 1, | |
182 | )); | |
183 | $this->assertEquals($contribution2['id'], $contribution); | |
22f80e87 | 184 | // Test id as field. |
4ab7d517 | 185 | $contribution = $this->callAPISuccess('contribution', 'get', array( |
186 | 'id' => $this->_contribution['id'], | |
187 | )); | |
2bfae985 | 188 | $this->assertEquals(1, $contribution['count']); |
4ab7d517 | 189 | |
22f80e87 | 190 | // Test get by contact id works. |
4ab7d517 | 191 | $contribution = $this->callAPISuccess('contribution', 'get', array('contact_id' => $this->_individualId)); |
6a488035 | 192 | |
2bfae985 | 193 | $this->assertEquals(2, $contribution['count']); |
2f45e1c2 | 194 | $this->callAPISuccess('Contribution', 'Delete', array( |
6a488035 | 195 | 'id' => $this->_contribution['id'], |
4ab7d517 | 196 | )); |
2f45e1c2 | 197 | $this->callAPISuccess('Contribution', 'Delete', array( |
6a488035 | 198 | 'id' => $contribution2['id'], |
4ab7d517 | 199 | )); |
6a488035 TO |
200 | } |
201 | ||
71d5a412 | 202 | /** |
203 | * Test that test contributions can be retrieved. | |
204 | */ | |
205 | public function testGetTestContribution() { | |
206 | $this->callAPISuccess('Contribution', 'create', array_merge($this->_params, array('is_test' => 1))); | |
207 | $this->callAPISuccessGetSingle('Contribution', array('is_test' => 1)); | |
208 | } | |
209 | ||
6c6e6187 | 210 | /** |
22f80e87 | 211 | * We need to ensure previous tested behaviour still works as part of the api contract. |
6c6e6187 | 212 | */ |
00be9182 | 213 | public function testGetContributionLegacyBehaviour() { |
6a488035 TO |
214 | $p = array( |
215 | 'contact_id' => $this->_individualId, | |
216 | 'receive_date' => '2010-01-20', | |
217 | 'total_amount' => 100.00, | |
4ab7d517 | 218 | 'contribution_type_id' => $this->_financialTypeId, |
6a488035 TO |
219 | 'non_deductible_amount' => 10.00, |
220 | 'fee_amount' => 5.00, | |
221 | 'net_amount' => 95.00, | |
222 | 'trxn_id' => 23456, | |
223 | 'invoice_id' => 78910, | |
224 | 'source' => 'SSF', | |
225 | 'contribution_status_id' => 1, | |
6a488035 | 226 | ); |
71d5a412 | 227 | $this->_contribution = $this->callAPISuccess('Contribution', 'create', $p); |
6a488035 TO |
228 | |
229 | $params = array( | |
230 | 'contribution_id' => $this->_contribution['id'], | |
6a488035 | 231 | ); |
2f45e1c2 | 232 | $contribution = $this->callAPIAndDocument('contribution', 'get', $params, __FUNCTION__, __FILE__); |
4ab7d517 | 233 | $financialParams['id'] = $this->_financialTypeId; |
6c6e6187 | 234 | $default = NULL; |
858d0bf8 | 235 | CRM_Financial_BAO_FinancialType::retrieve($financialParams, $default); |
2f45e1c2 | 236 | |
6c6e6187 | 237 | $this->assertEquals(1, $contribution['count']); |
2bfae985 | 238 | $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId); |
4ab7d517 | 239 | $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'], $this->_financialTypeId); |
240 | $this->assertEquals($contribution['values'][$contribution['id']]['contribution_type_id'], $this->_financialTypeId); | |
2bfae985 EM |
241 | $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00); |
242 | $this->assertEquals($contribution['values'][$contribution['id']]['non_deductible_amount'], 10.00); | |
243 | $this->assertEquals($contribution['values'][$contribution['id']]['fee_amount'], 5.00); | |
244 | $this->assertEquals($contribution['values'][$contribution['id']]['net_amount'], 95.00); | |
245 | $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 23456); | |
246 | $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 78910); | |
247 | $this->assertEquals($contribution['values'][$contribution['id']]['contribution_source'], 'SSF'); | |
248 | $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status'], 'Completed'); | |
22f80e87 EM |
249 | |
250 | // Create a second contribution - we are testing that 'id' gets the right contribution id (not the contact id). | |
6a488035 TO |
251 | $p['trxn_id'] = '3847'; |
252 | $p['invoice_id'] = '3847'; | |
253 | ||
2f45e1c2 | 254 | $contribution2 = $this->callAPISuccess('contribution', 'create', $p); |
6a488035 | 255 | |
6a488035 | 256 | // now we have 2 - test getcount |
4ab7d517 | 257 | $contribution = $this->callAPISuccess('contribution', 'getcount', array()); |
6a488035 TO |
258 | $this->assertEquals(2, $contribution); |
259 | //test id only format | |
4ab7d517 | 260 | $contribution = $this->callAPISuccess('contribution', 'get', array( |
261 | 'id' => $this->_contribution['id'], | |
262 | 'format.only_id' => 1, | |
263 | )); | |
22f80e87 | 264 | $this->assertEquals($this->_contribution['id'], $contribution, print_r($contribution, TRUE)); |
6a488035 | 265 | //test id only format |
4ab7d517 | 266 | $contribution = $this->callAPISuccess('contribution', 'get', array( |
267 | 'id' => $contribution2['id'], | |
268 | 'format.only_id' => 1, | |
269 | )); | |
6a488035 | 270 | $this->assertEquals($contribution2['id'], $contribution); |
2f45e1c2 | 271 | $contribution = $this->callAPISuccess('contribution', 'get', array( |
6a488035 TO |
272 | 'id' => $this->_contribution['id'], |
273 | )); | |
274 | //test id as field | |
2bfae985 | 275 | $this->assertEquals(1, $contribution['count']); |
6a488035 TO |
276 | // $this->assertEquals($this->_contribution['id'], $contribution['id'] ) ; |
277 | //test get by contact id works | |
4ab7d517 | 278 | $contribution = $this->callAPISuccess('contribution', 'get', array('contact_id' => $this->_individualId)); |
6a488035 | 279 | |
2bfae985 | 280 | $this->assertEquals(2, $contribution['count']); |
2f45e1c2 | 281 | $this->callAPISuccess('Contribution', 'Delete', array( |
5896d037 | 282 | 'id' => $this->_contribution['id'], |
6a488035 | 283 | )); |
2f45e1c2 | 284 | $this->callAPISuccess('Contribution', 'Delete', array( |
5896d037 | 285 | 'id' => $contribution2['id'], |
6a488035 TO |
286 | )); |
287 | } | |
5896d037 | 288 | |
a1a2a83d TO |
289 | /** |
290 | * Create an contribution_id=FALSE and financial_type_id=Donation. | |
291 | */ | |
00be9182 | 292 | public function testCreateEmptyContributionIDUseDonation() { |
6a488035 TO |
293 | $params = array( |
294 | 'contribution_id' => FALSE, | |
295 | 'contact_id' => 1, | |
296 | 'total_amount' => 1, | |
6c6e6187 | 297 | 'check_permissions' => FALSE, |
6a488035 TO |
298 | 'financial_type_id' => 'Donation', |
299 | ); | |
858d0bf8 | 300 | $this->callAPISuccess('contribution', 'create', $params); |
6c6e6187 | 301 | } |
6a488035 | 302 | |
6a488035 | 303 | /** |
1e52837d EM |
304 | * Check with complete array + custom field. |
305 | * | |
6a488035 TO |
306 | * Note that the test is written on purpose without any |
307 | * variables specific to participant so it can be replicated into other entities | |
308 | * and / or moved to the automated test suite | |
309 | */ | |
00be9182 | 310 | public function testCreateWithCustom() { |
6a488035 TO |
311 | $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, __FILE__); |
312 | ||
313 | $params = $this->_params; | |
314 | $params['custom_' . $ids['custom_field_id']] = "custom string"; | |
315 | ||
6c6e6187 | 316 | $result = $this->callAPIAndDocument($this->_entity, 'create', $params, __FUNCTION__, __FILE__); |
6a488035 | 317 | $this->assertEquals($result['id'], $result['values'][$result['id']]['id']); |
2f45e1c2 | 318 | $check = $this->callAPISuccess($this->_entity, 'get', array( |
4ab7d517 | 319 | 'return.custom_' . $ids['custom_field_id'] => 1, |
320 | 'id' => $result['id'], | |
321 | )); | |
6a488035 TO |
322 | $this->customFieldDelete($ids['custom_field_id']); |
323 | $this->customGroupDelete($ids['custom_group_id']); | |
22f80e87 | 324 | $this->assertEquals("custom string", $check['values'][$check['id']]['custom_' . $ids['custom_field_id']]); |
6a488035 TO |
325 | } |
326 | ||
327 | /** | |
fd786d03 EM |
328 | * Check with complete array + custom field. |
329 | * | |
6a488035 TO |
330 | * Note that the test is written on purpose without any |
331 | * variables specific to participant so it can be replicated into other entities | |
332 | * and / or moved to the automated test suite | |
333 | */ | |
00be9182 | 334 | public function testCreateGetFieldsWithCustom() { |
5896d037 | 335 | $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, __FILE__); |
6a488035 | 336 | $idsContact = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, 'ContactTest.php'); |
5896d037 | 337 | $result = $this->callAPISuccess('Contribution', 'getfields', array()); |
6a488035 TO |
338 | $this->assertArrayHasKey('custom_' . $ids['custom_field_id'], $result['values']); |
339 | $this->assertArrayNotHasKey('custom_' . $idsContact['custom_field_id'], $result['values']); | |
340 | $this->customFieldDelete($ids['custom_field_id']); | |
341 | $this->customGroupDelete($ids['custom_group_id']); | |
342 | $this->customFieldDelete($idsContact['custom_field_id']); | |
343 | $this->customGroupDelete($idsContact['custom_group_id']); | |
344 | } | |
345 | ||
00be9182 | 346 | public function testCreateContributionNoLineItems() { |
6a488035 TO |
347 | |
348 | $params = array( | |
349 | 'contact_id' => $this->_individualId, | |
350 | 'receive_date' => '20120511', | |
351 | 'total_amount' => 100.00, | |
5896d037 | 352 | 'financial_type_id' => $this->_financialTypeId, |
6a488035 TO |
353 | 'payment_instrument_id' => 1, |
354 | 'non_deductible_amount' => 10.00, | |
355 | 'fee_amount' => 50.00, | |
356 | 'net_amount' => 90.00, | |
357 | 'trxn_id' => 12345, | |
358 | 'invoice_id' => 67890, | |
359 | 'source' => 'SSF', | |
360 | 'contribution_status_id' => 1, | |
6a488035 TO |
361 | 'skipLineItem' => 1, |
362 | ); | |
363 | ||
2f45e1c2 | 364 | $contribution = $this->callAPISuccess('contribution', 'create', $params); |
6c6e6187 | 365 | $lineItems = $this->callAPISuccess('line_item', 'get', array( |
6a488035 TO |
366 | 'entity_id' => $contribution['id'], |
367 | 'entity_table' => 'civicrm_contribution', | |
368 | 'sequential' => 1, | |
369 | )); | |
370 | $this->assertEquals(0, $lineItems['count']); | |
371 | } | |
5896d037 | 372 | |
a1a2a83d | 373 | /** |
eceb18cc | 374 | * Test checks that passing in line items suppresses the create mechanism. |
6a488035 | 375 | */ |
00be9182 | 376 | public function testCreateContributionChainedLineItems() { |
6a488035 TO |
377 | $params = array( |
378 | 'contact_id' => $this->_individualId, | |
379 | 'receive_date' => '20120511', | |
380 | 'total_amount' => 100.00, | |
4ab7d517 | 381 | 'financial_type_id' => $this->_financialTypeId, |
6a488035 TO |
382 | 'payment_instrument_id' => 1, |
383 | 'non_deductible_amount' => 10.00, | |
384 | 'fee_amount' => 50.00, | |
385 | 'net_amount' => 90.00, | |
386 | 'trxn_id' => 12345, | |
387 | 'invoice_id' => 67890, | |
388 | 'source' => 'SSF', | |
389 | 'contribution_status_id' => 1, | |
6a488035 TO |
390 | 'skipLineItem' => 1, |
391 | 'api.line_item.create' => array( | |
392 | array( | |
393 | 'price_field_id' => 1, | |
394 | 'qty' => 2, | |
395 | 'line_total' => '20', | |
396 | 'unit_price' => '10', | |
397 | ), | |
398 | array( | |
399 | 'price_field_id' => 1, | |
400 | 'qty' => 1, | |
401 | 'line_total' => '80', | |
402 | 'unit_price' => '80', | |
403 | ), | |
404 | ), | |
405 | ); | |
406 | ||
5c49fee0 | 407 | $description = "Create Contribution with Nested Line Items."; |
6a488035 | 408 | $subfile = "CreateWithNestedLineItems"; |
6c6e6187 | 409 | $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__, __FILE__, $description, $subfile); |
2f45e1c2 | 410 | |
6c6e6187 | 411 | $lineItems = $this->callAPISuccess('line_item', 'get', array( |
6a488035 | 412 | 'entity_id' => $contribution['id'], |
4ede4532 | 413 | 'contribution_id' => $contribution['id'], |
6a488035 TO |
414 | 'entity_table' => 'civicrm_contribution', |
415 | 'sequential' => 1, | |
416 | )); | |
417 | $this->assertEquals(2, $lineItems['count']); | |
418 | } | |
419 | ||
00be9182 | 420 | public function testCreateContributionOffline() { |
6a488035 TO |
421 | $params = array( |
422 | 'contact_id' => $this->_individualId, | |
423 | 'receive_date' => '20120511', | |
424 | 'total_amount' => 100.00, | |
425 | 'financial_type_id' => 1, | |
426 | 'trxn_id' => 12345, | |
427 | 'invoice_id' => 67890, | |
428 | 'source' => 'SSF', | |
429 | 'contribution_status_id' => 1, | |
6a488035 TO |
430 | ); |
431 | ||
4ab7d517 | 432 | $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__, __FILE__); |
2bfae985 EM |
433 | $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId); |
434 | $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00); | |
5896d037 | 435 | $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'], 1); |
2bfae985 EM |
436 | $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 12345); |
437 | $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 67890); | |
438 | $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF'); | |
439 | $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status_id'], 1); | |
6c6e6187 | 440 | $lineItems = $this->callAPISuccess('line_item', 'get', array( |
6a488035 | 441 | 'entity_id' => $contribution['id'], |
4ede4532 | 442 | 'contribution_id' => $contribution['id'], |
6a488035 TO |
443 | 'entity_table' => 'civicrm_contribution', |
444 | 'sequential' => 1, | |
5896d037 | 445 | )); |
6a488035 TO |
446 | $this->assertEquals(1, $lineItems['count']); |
447 | $this->assertEquals($contribution['id'], $lineItems['values'][0]['entity_id']); | |
4ede4532 | 448 | $this->assertEquals($contribution['id'], $lineItems['values'][0]['contribution_id']); |
6a488035 TO |
449 | $this->_checkFinancialRecords($contribution, 'offline'); |
450 | $this->contributionGetnCheck($params, $contribution['id']); | |
451 | } | |
5896d037 | 452 | |
f70a6752 | 453 | /** |
28de42d1 | 454 | * Test create with valid payment instrument. |
6a488035 | 455 | */ |
00be9182 | 456 | public function testCreateContributionWithPaymentInstrument() { |
6a488035 | 457 | $params = $this->_params + array('payment_instrument' => 'EFT'); |
53191813 | 458 | $contribution = $this->callAPISuccess('contribution', 'create', $params); |
6c6e6187 | 459 | $contribution = $this->callAPISuccess('contribution', 'get', array( |
53191813 | 460 | 'sequential' => 1, |
21dfd5f5 | 461 | 'id' => $contribution['id'], |
53191813 | 462 | )); |
6a488035 | 463 | $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]); |
53191813 CW |
464 | $this->assertEquals('EFT', $contribution['values'][0]['payment_instrument']); |
465 | ||
5896d037 | 466 | $this->callAPISuccess('contribution', 'create', array( |
92915c55 TO |
467 | 'id' => $contribution['id'], |
468 | 'payment_instrument' => 'Credit Card', | |
469 | )); | |
6c6e6187 | 470 | $contribution = $this->callAPISuccess('contribution', 'get', array( |
53191813 | 471 | 'sequential' => 1, |
21dfd5f5 | 472 | 'id' => $contribution['id'], |
53191813 CW |
473 | )); |
474 | $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]); | |
475 | $this->assertEquals('Credit Card', $contribution['values'][0]['payment_instrument']); | |
6a488035 TO |
476 | } |
477 | ||
00be9182 | 478 | public function testGetContributionByPaymentInstrument() { |
6a488035 | 479 | $params = $this->_params + array('payment_instrument' => 'EFT'); |
53191813 | 480 | $params2 = $this->_params + array('payment_instrument' => 'Cash'); |
6c6e6187 TO |
481 | $this->callAPISuccess('contribution', 'create', $params); |
482 | $this->callAPISuccess('contribution', 'create', $params2); | |
5896d037 | 483 | $contribution = $this->callAPISuccess('contribution', 'get', array( |
92915c55 | 484 | 'sequential' => 1, |
7d543448 | 485 | 'contribution_payment_instrument' => 'Cash', |
92915c55 | 486 | )); |
6a488035 | 487 | $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]); |
ff977830 EM |
488 | $this->assertEquals('Cash', $contribution['values'][0]['payment_instrument']); |
489 | $this->assertEquals(1, $contribution['count']); | |
868e247d | 490 | $contribution = $this->callAPISuccess('contribution', 'get', array('sequential' => 1, 'payment_instrument' => 'Cash')); |
6a488035 | 491 | $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]); |
868e247d | 492 | $this->assertEquals('Cash', $contribution['values'][0]['payment_instrument']); |
6a488035 | 493 | $this->assertEquals(1, $contribution['count']); |
5896d037 | 494 | $contribution = $this->callAPISuccess('contribution', 'get', array( |
92915c55 TO |
495 | 'sequential' => 1, |
496 | 'payment_instrument_id' => 5, | |
497 | )); | |
6a488035 | 498 | $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]); |
6c6e6187 TO |
499 | $this->assertEquals('EFT', $contribution['values'][0]['payment_instrument']); |
500 | $this->assertEquals(1, $contribution['count']); | |
5896d037 | 501 | $contribution = $this->callAPISuccess('contribution', 'get', array( |
92915c55 TO |
502 | 'sequential' => 1, |
503 | 'payment_instrument' => 'EFT', | |
504 | )); | |
6a488035 | 505 | $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]); |
53191813 CW |
506 | $this->assertEquals('EFT', $contribution['values'][0]['payment_instrument']); |
507 | $this->assertEquals(1, $contribution['count']); | |
5896d037 | 508 | $contribution = $this->callAPISuccess('contribution', 'create', array( |
92915c55 TO |
509 | 'id' => $contribution['id'], |
510 | 'payment_instrument' => 'Credit Card', | |
511 | )); | |
5896d037 | 512 | $contribution = $this->callAPISuccess('contribution', 'get', array('sequential' => 1, 'id' => $contribution['id'])); |
6a488035 | 513 | $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]); |
6c6e6187 TO |
514 | $this->assertEquals('Credit Card', $contribution['values'][0]['payment_instrument']); |
515 | $this->assertEquals(1, $contribution['count']); | |
6a488035 TO |
516 | } |
517 | ||
b5a37491 EM |
518 | /** |
519 | * CRM-16227 introduces invoice_id as a parameter. | |
520 | */ | |
521 | public function testGetContributionByInvoice() { | |
522 | $this->callAPISuccess('Contribution', 'create', array_merge($this->_params, array('invoice_id' => 'curly'))); | |
523 | $this->callAPISuccess('Contribution', 'create', array_merge($this->_params), array('invoice_id' => 'churlish')); | |
524 | $this->callAPISuccessGetCount('Contribution', array(), 2); | |
525 | $this->callAPISuccessGetSingle('Contribution', array('invoice_id' => 'curly')); | |
526 | // The following don't work. They are the format we are trying to introduce but although the form uses this format | |
527 | // CRM_Contact_BAO_Query::convertFormValues puts them into the other format & the where only supports that. | |
528 | // ideally the where clause would support this format (as it does on contact_BAO_Query) and those lines would | |
529 | // come out of convertFormValues | |
530 | // $this->callAPISuccessGetSingle('Contribution', array('invoice_id' => array('LIKE' => '%ish%'))); | |
531 | // $this->callAPISuccessGetSingle('Contribution', array('invoice_id' => array('NOT IN' => array('curly')))); | |
532 | // $this->callAPISuccessGetCount('Contribution', array('invoice_id' => array('LIKE' => '%ly%')), 2); | |
533 | // $this->callAPISuccessGetCount('Contribution', array('invoice_id' => array('IN' => array('curly', 'churlish'))), | |
534 | // 2); | |
535 | } | |
536 | ||
c490a46a | 537 | /** |
eceb18cc | 538 | * Create test with unique field name on source. |
c490a46a | 539 | */ |
00be9182 | 540 | public function testCreateContributionSource() { |
6a488035 TO |
541 | |
542 | $params = array( | |
543 | 'contact_id' => $this->_individualId, | |
544 | 'receive_date' => date('Ymd'), | |
545 | 'total_amount' => 100.00, | |
4ab7d517 | 546 | 'financial_type_id' => $this->_financialTypeId, |
6a488035 TO |
547 | 'payment_instrument_id' => 1, |
548 | 'non_deductible_amount' => 10.00, | |
549 | 'fee_amount' => 50.00, | |
550 | 'net_amount' => 90.00, | |
551 | 'trxn_id' => 12345, | |
552 | 'invoice_id' => 67890, | |
553 | 'contribution_source' => 'SSF', | |
554 | 'contribution_status_id' => 1, | |
6a488035 TO |
555 | ); |
556 | ||
4ab7d517 | 557 | $contribution = $this->callAPISuccess('contribution', 'create', $params); |
2bfae985 EM |
558 | $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00); |
559 | $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF'); | |
6a488035 | 560 | } |
d5d148ab | 561 | |
d424ffde | 562 | /** |
eceb18cc | 563 | * Create test with unique field name on source. |
d424ffde | 564 | */ |
00be9182 | 565 | public function testCreateDefaultNow() { |
d5d148ab EM |
566 | |
567 | $params = $this->_params; | |
568 | unset($params['receive_date']); | |
569 | ||
570 | $contribution = $this->callAPISuccess('contribution', 'create', $params); | |
571 | $contribution = $this->callAPISuccessGetSingle('contribution', array('id' => $contribution['id'])); | |
572 | $this->assertEquals(date('Y-m-d'), date('Y-m-d', strtotime($contribution['receive_date']))); | |
573 | } | |
574 | ||
c490a46a | 575 | /** |
55d2c6f1 | 576 | * Create test with unique field name on source. |
c490a46a | 577 | */ |
55d2c6f1 | 578 | public function testCreateContributionSourceInvalidContact() { |
6a488035 TO |
579 | |
580 | $params = array( | |
581 | 'contact_id' => 999, | |
582 | 'receive_date' => date('Ymd'), | |
583 | 'total_amount' => 100.00, | |
4ab7d517 | 584 | 'financial_type_id' => $this->_financialTypeId, |
6a488035 TO |
585 | 'payment_instrument_id' => 1, |
586 | 'non_deductible_amount' => 10.00, | |
587 | 'fee_amount' => 50.00, | |
588 | 'net_amount' => 90.00, | |
589 | 'trxn_id' => 12345, | |
590 | 'invoice_id' => 67890, | |
591 | 'contribution_source' => 'SSF', | |
592 | 'contribution_status_id' => 1, | |
6a488035 TO |
593 | ); |
594 | ||
858d0bf8 | 595 | $this->callAPIFailure('contribution', 'create', $params, 'contact_id is not valid : 999'); |
6a488035 TO |
596 | } |
597 | ||
00be9182 | 598 | public function testCreateContributionSourceInvalidContContact() { |
6a488035 TO |
599 | |
600 | $params = array( | |
601 | 'contribution_contact_id' => 999, | |
602 | 'receive_date' => date('Ymd'), | |
603 | 'total_amount' => 100.00, | |
4ab7d517 | 604 | 'financial_type_id' => $this->_financialTypeId, |
6a488035 TO |
605 | 'payment_instrument_id' => 1, |
606 | 'non_deductible_amount' => 10.00, | |
607 | 'fee_amount' => 50.00, | |
608 | 'net_amount' => 90.00, | |
609 | 'trxn_id' => 12345, | |
610 | 'invoice_id' => 67890, | |
611 | 'contribution_source' => 'SSF', | |
612 | 'contribution_status_id' => 1, | |
6a488035 TO |
613 | ); |
614 | ||
858d0bf8 | 615 | $this->callAPIFailure('contribution', 'create', $params, 'contact_id is not valid : 999'); |
6a488035 TO |
616 | } |
617 | ||
858d0bf8 | 618 | /** |
442cf836 | 619 | * Test note created correctly. |
858d0bf8 | 620 | */ |
00be9182 | 621 | public function testCreateContributionWithNote() { |
5c49fee0 | 622 | $description = "Demonstrates creating contribution with Note Entity."; |
5896d037 TO |
623 | $subfile = "ContributionCreateWithNote"; |
624 | $params = array( | |
6a488035 TO |
625 | 'contact_id' => $this->_individualId, |
626 | 'receive_date' => '2012-01-01', | |
627 | 'total_amount' => 100.00, | |
4ab7d517 | 628 | 'financial_type_id' => $this->_financialTypeId, |
6a488035 TO |
629 | 'payment_instrument_id' => 1, |
630 | 'non_deductible_amount' => 10.00, | |
631 | 'fee_amount' => 50.00, | |
632 | 'net_amount' => 90.00, | |
633 | 'trxn_id' => 12345, | |
634 | 'invoice_id' => 67890, | |
635 | 'source' => 'SSF', | |
636 | 'contribution_status_id' => 1, | |
6a488035 TO |
637 | 'note' => 'my contribution note', |
638 | ); | |
639 | ||
4ab7d517 | 640 | $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__, __FILE__, $description, $subfile); |
5896d037 | 641 | $result = $this->callAPISuccess('note', 'get', array( |
92915c55 TO |
642 | 'entity_table' => 'civicrm_contribution', |
643 | 'entity_id' => $contribution['id'], | |
644 | 'sequential' => 1, | |
645 | )); | |
6a488035 | 646 | $this->assertEquals('my contribution note', $result['values'][0]['note']); |
4ab7d517 | 647 | $this->callAPISuccess('contribution', 'delete', array('id' => $contribution['id'])); |
6a488035 TO |
648 | } |
649 | ||
00be9182 | 650 | public function testCreateContributionWithNoteUniqueNameAliases() { |
5896d037 | 651 | $params = array( |
6a488035 TO |
652 | 'contact_id' => $this->_individualId, |
653 | 'receive_date' => '2012-01-01', | |
654 | 'total_amount' => 100.00, | |
4ab7d517 | 655 | 'financial_type_id' => $this->_financialTypeId, |
6a488035 TO |
656 | 'payment_instrument_id' => 1, |
657 | 'non_deductible_amount' => 10.00, | |
658 | 'fee_amount' => 50.00, | |
659 | 'net_amount' => 90.00, | |
660 | 'trxn_id' => 12345, | |
661 | 'invoice_id' => 67890, | |
662 | 'source' => 'SSF', | |
663 | 'contribution_status_id' => 1, | |
6a488035 TO |
664 | 'contribution_note' => 'my contribution note', |
665 | ); | |
666 | ||
4ab7d517 | 667 | $contribution = $this->callAPISuccess('contribution', 'create', $params); |
5896d037 | 668 | $result = $this->callAPISuccess('note', 'get', array( |
92915c55 TO |
669 | 'entity_table' => 'civicrm_contribution', |
670 | 'entity_id' => $contribution['id'], | |
671 | 'sequential' => 1, | |
672 | )); | |
6a488035 | 673 | $this->assertEquals('my contribution note', $result['values'][0]['note']); |
4ab7d517 | 674 | $this->callAPISuccess('contribution', 'delete', array('id' => $contribution['id'])); |
6a488035 | 675 | } |
c490a46a CW |
676 | |
677 | /** | |
55d2c6f1 | 678 | * This is the test for creating soft credits. |
c490a46a | 679 | */ |
55d2c6f1 | 680 | public function testCreateContributionWithSoftCredit() { |
5c49fee0 | 681 | $description = "Demonstrates creating contribution with SoftCredit."; |
5896d037 TO |
682 | $subfile = "ContributionCreateWithSoftCredit"; |
683 | $contact2 = $this->callAPISuccess('Contact', 'create', array( | |
92915c55 TO |
684 | 'display_name' => 'superman', |
685 | 'contact_type' => 'Individual', | |
686 | )); | |
55d2c6f1 | 687 | $softParams = array( |
bcc03b98 | 688 | 'contact_id' => $contact2['id'], |
689 | 'amount' => 50, | |
21dfd5f5 | 690 | 'soft_credit_type_id' => 3, |
6a488035 TO |
691 | ); |
692 | ||
55d2c6f1 | 693 | $params = $this->_params + array('soft_credit' => array(1 => $softParams)); |
4ab7d517 | 694 | $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__, __FILE__, $description, $subfile); |
6c6e6187 | 695 | $result = $this->callAPISuccess('contribution', 'get', array('return' => 'soft_credit', 'sequential' => 1)); |
a1c68fd2 | 696 | |
55d2c6f1 EM |
697 | $this->assertEquals($softParams['contact_id'], $result['values'][0]['soft_credit'][1]['contact_id']); |
698 | $this->assertEquals($softParams['amount'], $result['values'][0]['soft_credit'][1]['amount']); | |
699 | $this->assertEquals($softParams['soft_credit_type_id'], $result['values'][0]['soft_credit'][1]['soft_credit_type']); | |
6a516bd6 DG |
700 | |
701 | $this->callAPISuccess('contribution', 'delete', array('id' => $contribution['id'])); | |
702 | $this->callAPISuccess('contact', 'delete', array('id' => $contact2['id'])); | |
703 | } | |
704 | ||
00be9182 | 705 | public function testCreateContributionWithSoftCreditDefaults() { |
5c49fee0 | 706 | $description = "Demonstrates creating contribution with Soft Credit defaults for amount and type."; |
5896d037 TO |
707 | $subfile = "ContributionCreateWithSoftCreditDefaults"; |
708 | $contact2 = $this->callAPISuccess('Contact', 'create', array( | |
92915c55 TO |
709 | 'display_name' => 'superman', |
710 | 'contact_type' => 'Individual', | |
711 | )); | |
6a516bd6 | 712 | $params = $this->_params + array( |
442cf836 EM |
713 | 'soft_credit_to' => $contact2['id'], |
714 | ); | |
6a516bd6 | 715 | $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__, __FILE__, $description, $subfile); |
6c6e6187 | 716 | $result = $this->callAPISuccess('contribution', 'get', array('return' => 'soft_credit', 'sequential' => 1)); |
6a516bd6 DG |
717 | |
718 | $this->assertEquals($contact2['id'], $result['values'][0]['soft_credit'][1]['contact_id']); | |
719 | // Default soft credit amount = contribution.total_amount | |
720 | $this->assertEquals($this->_params['total_amount'], $result['values'][0]['soft_credit'][1]['amount']); | |
721 | $this->assertEquals(CRM_Core_OptionGroup::getDefaultValue("soft_credit_type"), $result['values'][0]['soft_credit'][1]['soft_credit_type']); | |
722 | ||
723 | $this->callAPISuccess('contribution', 'delete', array('id' => $contribution['id'])); | |
724 | $this->callAPISuccess('contact', 'delete', array('id' => $contact2['id'])); | |
725 | } | |
726 | ||
00be9182 | 727 | public function testCreateContributionWithHonoreeContact() { |
5c49fee0 | 728 | $description = "Demonstrates creating contribution with Soft Credit by passing in honor_contact_id."; |
5896d037 TO |
729 | $subfile = "ContributionCreateWithHonoreeContact"; |
730 | $contact2 = $this->callAPISuccess('Contact', 'create', array( | |
92915c55 TO |
731 | 'display_name' => 'superman', |
732 | 'contact_type' => 'Individual', | |
733 | )); | |
6a516bd6 | 734 | $params = $this->_params + array( |
442cf836 EM |
735 | 'honor_contact_id' => $contact2['id'], |
736 | ); | |
6a516bd6 | 737 | $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__, __FILE__, $description, $subfile); |
6c6e6187 | 738 | $result = $this->callAPISuccess('contribution', 'get', array('return' => 'soft_credit', 'sequential' => 1)); |
6a516bd6 DG |
739 | |
740 | $this->assertEquals($contact2['id'], $result['values'][0]['soft_credit'][1]['contact_id']); | |
741 | // Default soft credit amount = contribution.total_amount | |
742 | // Legacy mode in create api (honor_contact_id param) uses the standard "In Honor of" soft credit type | |
743 | $this->assertEquals($this->_params['total_amount'], $result['values'][0]['soft_credit'][1]['amount']); | |
744 | $this->assertEquals(CRM_Core_OptionGroup::getValue('soft_credit_type', 'in_honor_of', 'name'), $result['values'][0]['soft_credit'][1]['soft_credit_type']); | |
6a488035 | 745 | |
4ab7d517 | 746 | $this->callAPISuccess('contribution', 'delete', array('id' => $contribution['id'])); |
747 | $this->callAPISuccess('contact', 'delete', array('id' => $contact2['id'])); | |
6a488035 TO |
748 | } |
749 | ||
750 | /** | |
92c99a4a | 751 | * Test using example code. |
6a488035 | 752 | */ |
00be9182 | 753 | public function testContributionCreateExample() { |
6a488035 | 754 | //make sure at least on page exists since there is a truncate in tear down |
8be629ac | 755 | $this->callAPISuccess('contribution_page', 'create', $this->_pageParams); |
3ec6e38d | 756 | require_once 'api/v3/examples/Contribution/Create.php'; |
5896d037 | 757 | $result = contribution_create_example(); |
006d6361 | 758 | $id = $result['id']; |
6a488035 | 759 | $expectedResult = contribution_create_expectedresult(); |
8e342a79 | 760 | $this->checkArrayEquals($expectedResult, $result); |
006d6361 | 761 | $this->contributionDelete($id); |
6a488035 TO |
762 | } |
763 | ||
a1a2a83d | 764 | /** |
f55c5fa8 | 765 | * Function tests that additional financial records are created when fee amount is recorded. |
6a488035 | 766 | */ |
00be9182 | 767 | public function testCreateContributionWithFee() { |
6a488035 TO |
768 | $params = array( |
769 | 'contact_id' => $this->_individualId, | |
770 | 'receive_date' => '20120511', | |
771 | 'total_amount' => 100.00, | |
772 | 'fee_amount' => 50, | |
773 | 'financial_type_id' => 1, | |
774 | 'trxn_id' => 12345, | |
775 | 'invoice_id' => 67890, | |
776 | 'source' => 'SSF', | |
777 | 'contribution_status_id' => 1, | |
6a488035 TO |
778 | ); |
779 | ||
4ab7d517 | 780 | $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__, __FILE__); |
2bfae985 EM |
781 | $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId); |
782 | $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00); | |
e0e3c51b EM |
783 | $this->assertEquals($contribution['values'][$contribution['id']]['fee_amount'], 50.00); |
784 | $this->assertEquals($contribution['values'][$contribution['id']]['net_amount'], 50.00); | |
5896d037 | 785 | $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'], 1); |
2bfae985 EM |
786 | $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 12345); |
787 | $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 67890); | |
788 | $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF'); | |
789 | $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status_id'], 1); | |
e0e3c51b | 790 | |
6c6e6187 | 791 | $lineItems = $this->callAPISuccess('line_item', 'get', array( |
4ab7d517 | 792 | |
6a488035 TO |
793 | 'entity_id' => $contribution['id'], |
794 | 'entity_table' => 'civicrm_contribution', | |
795 | 'sequential' => 1, | |
5896d037 | 796 | )); |
6a488035 TO |
797 | $this->assertEquals(1, $lineItems['count']); |
798 | $this->assertEquals($contribution['id'], $lineItems['values'][0]['entity_id']); | |
4ede4532 | 799 | $this->assertEquals($contribution['id'], $lineItems['values'][0]['contribution_id']); |
6c6e6187 | 800 | $lineItems = $this->callAPISuccess('line_item', 'get', array( |
4ab7d517 | 801 | |
5896d037 TO |
802 | 'entity_id' => $contribution['id'], |
803 | 'contribution_id' => $contribution['id'], | |
804 | 'entity_table' => 'civicrm_contribution', | |
805 | 'sequential' => 1, | |
6a488035 TO |
806 | )); |
807 | $this->assertEquals(1, $lineItems['count']); | |
808 | $this->_checkFinancialRecords($contribution, 'feeAmount'); | |
809 | } | |
810 | ||
811 | ||
f70a6752 | 812 | /** |
442cf836 | 813 | * Function tests that additional financial records are created when online contribution is created. |
6a488035 | 814 | */ |
00be9182 | 815 | public function testCreateContributionOnline() { |
858d0bf8 | 816 | CRM_Financial_BAO_PaymentProcessor::create($this->_processorParams); |
5896d037 | 817 | $contributionPage = $this->callAPISuccess('contribution_page', 'create', $this->_pageParams); |
fc928539 | 818 | $this->assertAPISuccess($contributionPage); |
6a488035 TO |
819 | $params = array( |
820 | 'contact_id' => $this->_individualId, | |
821 | 'receive_date' => '20120511', | |
822 | 'total_amount' => 100.00, | |
823 | 'financial_type_id' => 1, | |
824 | 'contribution_page_id' => $contributionPage['id'], | |
825 | 'payment_processor' => 1, | |
826 | 'trxn_id' => 12345, | |
827 | 'invoice_id' => 67890, | |
828 | 'source' => 'SSF', | |
829 | 'contribution_status_id' => 1, | |
4ab7d517 | 830 | |
6a488035 TO |
831 | ); |
832 | ||
4ab7d517 | 833 | $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__, __FILE__); |
2bfae985 EM |
834 | $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId); |
835 | $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00); | |
5896d037 | 836 | $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'], 1); |
2bfae985 EM |
837 | $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 12345); |
838 | $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 67890); | |
839 | $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF'); | |
840 | $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status_id'], 1); | |
6a488035 TO |
841 | $this->_checkFinancialRecords($contribution, 'online'); |
842 | } | |
843 | ||
f70a6752 | 844 | /** |
442cf836 EM |
845 | * Check handling of financial type. |
846 | * | |
100fef9d | 847 | * In the interests of removing financial type / contribution type checks from |
f70a6752 | 848 | * legacy format function lets test that the api is doing this for us |
849 | */ | |
00be9182 | 850 | public function testCreateInvalidFinancialType() { |
f70a6752 | 851 | $params = $this->_params; |
852 | $params['financial_type_id'] = 99999; | |
858d0bf8 | 853 | $this->callAPIFailure($this->_entity, 'create', $params, "'99999' is not a valid option for field financial_type_id"); |
f70a6752 | 854 | } |
855 | ||
4302618d | 856 | /** |
442cf836 EM |
857 | * Check handling of financial type. |
858 | * | |
100fef9d | 859 | * In the interests of removing financial type / contribution type checks from |
4302618d | 860 | * legacy format function lets test that the api is doing this for us |
861 | */ | |
00be9182 | 862 | public function testValidNamedFinancialType() { |
4302618d | 863 | $params = $this->_params; |
864 | $params['financial_type_id'] = 'Donation'; | |
858d0bf8 | 865 | $this->callAPISuccess($this->_entity, 'create', $params); |
4302618d | 866 | } |
867 | ||
f70a6752 | 868 | /** |
92c99a4a EM |
869 | * Tests that additional financial records are created. |
870 | * | |
871 | * Checks when online contribution with pay later option is created | |
6a488035 | 872 | */ |
00be9182 | 873 | public function testCreateContributionPayLaterOnline() { |
858d0bf8 | 874 | CRM_Financial_BAO_PaymentProcessor::create($this->_processorParams); |
6a488035 | 875 | $this->_pageParams['is_pay_later'] = 1; |
5896d037 | 876 | $contributionPage = $this->callAPISuccess('contribution_page', 'create', $this->_pageParams); |
fc928539 | 877 | $this->assertAPISuccess($contributionPage); |
6a488035 TO |
878 | $params = array( |
879 | 'contact_id' => $this->_individualId, | |
880 | 'receive_date' => '20120511', | |
881 | 'total_amount' => 100.00, | |
882 | 'financial_type_id' => 1, | |
883 | 'contribution_page_id' => $contributionPage['id'], | |
884 | 'trxn_id' => 12345, | |
885 | 'is_pay_later' => 1, | |
886 | 'invoice_id' => 67890, | |
887 | 'source' => 'SSF', | |
888 | 'contribution_status_id' => 2, | |
4ab7d517 | 889 | |
6a488035 TO |
890 | ); |
891 | ||
4ab7d517 | 892 | $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__, __FILE__); |
2bfae985 EM |
893 | $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId); |
894 | $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00); | |
5896d037 | 895 | $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'], 1); |
2bfae985 EM |
896 | $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 12345); |
897 | $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 67890); | |
898 | $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF'); | |
899 | $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status_id'], 2); | |
6a488035 TO |
900 | $this->_checkFinancialRecords($contribution, 'payLater'); |
901 | } | |
902 | ||
a1a2a83d | 903 | /** |
1e52837d | 904 | * Function tests that additional financial records are created for online contribution with pending option. |
6a488035 | 905 | */ |
00be9182 | 906 | public function testCreateContributionPendingOnline() { |
6a488035 | 907 | $paymentProcessor = CRM_Financial_BAO_PaymentProcessor::create($this->_processorParams); |
5896d037 | 908 | $contributionPage = $this->callAPISuccess('contribution_page', 'create', $this->_pageParams); |
fc928539 | 909 | $this->assertAPISuccess($contributionPage); |
6a488035 TO |
910 | $params = array( |
911 | 'contact_id' => $this->_individualId, | |
912 | 'receive_date' => '20120511', | |
913 | 'total_amount' => 100.00, | |
914 | 'financial_type_id' => 1, | |
915 | 'contribution_page_id' => $contributionPage['id'], | |
916 | 'trxn_id' => 12345, | |
917 | 'invoice_id' => 67890, | |
918 | 'source' => 'SSF', | |
919 | 'contribution_status_id' => 2, | |
6a488035 TO |
920 | ); |
921 | ||
4ab7d517 | 922 | $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__, __FILE__); |
2bfae985 EM |
923 | $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId); |
924 | $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00); | |
5896d037 | 925 | $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'], 1); |
2bfae985 EM |
926 | $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 12345); |
927 | $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 67890); | |
928 | $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF'); | |
929 | $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status_id'], 2); | |
6a488035 TO |
930 | $this->_checkFinancialRecords($contribution, 'pending'); |
931 | } | |
932 | ||
e748bf60 | 933 | /** |
92c99a4a | 934 | * Test that BAO defaults work. |
e748bf60 | 935 | */ |
00be9182 | 936 | public function testCreateBAODefaults() { |
e748bf60 EM |
937 | unset($this->_params['contribution_source_id'], $this->_params['payment_instrument_id']); |
938 | $contribution = $this->callAPISuccess('contribution', 'create', $this->_params); | |
5896d037 | 939 | $contribution = $this->callAPISuccess('contribution', 'getsingle', array( |
92915c55 TO |
940 | 'id' => $contribution['id'], |
941 | 'api.contribution.delete' => 1, | |
942 | )); | |
e748bf60 | 943 | $this->assertEquals(1, $contribution['contribution_status_id']); |
12879069 | 944 | $this->assertEquals('Check', $contribution['payment_instrument']); |
e748bf60 EM |
945 | } |
946 | ||
a1a2a83d | 947 | /** |
1e52837d | 948 | * Function tests that line items, financial records are updated when contribution amount is changed. |
6a488035 | 949 | */ |
00be9182 | 950 | public function testCreateUpdateContributionChangeTotal() { |
4ab7d517 | 951 | $contribution = $this->callAPISuccess('contribution', 'create', $this->_params); |
6c6e6187 | 952 | $lineItems = $this->callAPISuccess('line_item', 'getvalue', array( |
4ab7d517 | 953 | |
6a488035 TO |
954 | 'entity_id' => $contribution['id'], |
955 | 'entity_table' => 'civicrm_contribution', | |
956 | 'sequential' => 1, | |
957 | 'return' => 'line_total', | |
958 | )); | |
959 | $this->assertEquals('100.00', $lineItems); | |
960 | $trxnAmount = $this->_getFinancialTrxnAmount($contribution['id']); | |
961 | // Financial trxn SUM = 100 + 5 (fee) | |
962 | $this->assertEquals('105.00', $trxnAmount); | |
963 | $newParams = array( | |
4ab7d517 | 964 | |
6a488035 | 965 | 'id' => $contribution['id'], |
21dfd5f5 | 966 | 'total_amount' => '125', |
5896d037 | 967 | ); |
694769da | 968 | $contribution = $this->callAPISuccess('contribution', 'create', $newParams); |
4ab7d517 | 969 | |
6c6e6187 | 970 | $lineItems = $this->callAPISuccess('line_item', 'getvalue', array( |
6a488035 | 971 | |
5896d037 TO |
972 | 'entity_id' => $contribution['id'], |
973 | 'entity_table' => 'civicrm_contribution', | |
974 | 'sequential' => 1, | |
975 | 'return' => 'line_total', | |
6a488035 TO |
976 | )); |
977 | ||
978 | $this->assertEquals('125.00', $lineItems); | |
979 | $trxnAmount = $this->_getFinancialTrxnAmount($contribution['id']); | |
28de42d1 EM |
980 | |
981 | // Financial trxn SUM = 125 + 5 (fee). | |
6a488035 | 982 | $this->assertEquals('130.00', $trxnAmount); |
28de42d1 | 983 | $this->assertEquals('125.00', $this->_getFinancialItemAmount($contribution['id'])); |
6a488035 TO |
984 | } |
985 | ||
a1a2a83d | 986 | /** |
1e52837d | 987 | * Function tests that line items, financial records are updated when pay later contribution is received. |
6a488035 | 988 | */ |
00be9182 | 989 | public function testCreateUpdateContributionPayLater() { |
6a488035 TO |
990 | $contribParams = array( |
991 | 'contact_id' => $this->_individualId, | |
992 | 'receive_date' => '2012-01-01', | |
993 | 'total_amount' => 100.00, | |
4ab7d517 | 994 | 'financial_type_id' => $this->_financialTypeId, |
6a488035 | 995 | 'payment_instrument_id' => 1, |
8f39a111 | 996 | 'contribution_status_id' => 2, |
997 | 'is_pay_later' => 1, | |
4ab7d517 | 998 | |
6a488035 | 999 | ); |
4ab7d517 | 1000 | $contribution = $this->callAPISuccess('contribution', 'create', $contribParams); |
6a488035 TO |
1001 | |
1002 | $newParams = array_merge($contribParams, array( | |
5896d037 TO |
1003 | 'id' => $contribution['id'], |
1004 | 'contribution_status_id' => 1, | |
1005 | ) | |
c71ae314 | 1006 | ); |
694769da | 1007 | $contribution = $this->callAPISuccess('contribution', 'create', $newParams); |
6a488035 | 1008 | $contribution = $contribution['values'][$contribution['id']]; |
6c6e6187 | 1009 | $this->assertEquals($contribution['contribution_status_id'], '1'); |
6a488035 TO |
1010 | $this->_checkFinancialItem($contribution['id'], 'paylater'); |
1011 | $this->_checkFinancialTrxn($contribution, 'payLater'); | |
1012 | } | |
1013 | ||
a1a2a83d | 1014 | /** |
eceb18cc | 1015 | * Function tests that financial records are updated when Payment Instrument is changed. |
6a488035 | 1016 | */ |
00be9182 | 1017 | public function testCreateUpdateContributionPaymentInstrument() { |
6a488035 TO |
1018 | $instrumentId = $this->_addPaymentInstrument(); |
1019 | $contribParams = array( | |
1020 | 'contact_id' => $this->_individualId, | |
1021 | 'total_amount' => 100.00, | |
4ab7d517 | 1022 | 'financial_type_id' => $this->_financialTypeId, |
6a488035 TO |
1023 | 'payment_instrument_id' => 4, |
1024 | 'contribution_status_id' => 1, | |
4ab7d517 | 1025 | |
6a488035 | 1026 | ); |
4ab7d517 | 1027 | $contribution = $this->callAPISuccess('contribution', 'create', $contribParams); |
6a488035 TO |
1028 | |
1029 | $newParams = array_merge($contribParams, array( | |
5896d037 TO |
1030 | 'id' => $contribution['id'], |
1031 | 'payment_instrument_id' => $instrumentId, | |
1032 | ) | |
6a488035 | 1033 | ); |
694769da | 1034 | $contribution = $this->callAPISuccess('contribution', 'create', $newParams); |
fc928539 | 1035 | $this->assertAPISuccess($contribution); |
4ecc6d4b | 1036 | $this->_checkFinancialTrxn($contribution, 'paymentInstrument', $instrumentId); |
6a488035 TO |
1037 | } |
1038 | ||
a1a2a83d | 1039 | /** |
eceb18cc | 1040 | * Function tests that financial records are added when Contribution is Refunded. |
6a488035 | 1041 | */ |
00be9182 | 1042 | public function testCreateUpdateContributionRefund() { |
797d4c52 | 1043 | $contributionParams = array( |
6a488035 TO |
1044 | 'contact_id' => $this->_individualId, |
1045 | 'receive_date' => '2012-01-01', | |
1046 | 'total_amount' => 100.00, | |
4ab7d517 | 1047 | 'financial_type_id' => $this->_financialTypeId, |
6a488035 TO |
1048 | 'payment_instrument_id' => 4, |
1049 | 'contribution_status_id' => 1, | |
797d4c52 | 1050 | 'trxn_id' => 'original_payment', |
1051 | ); | |
1052 | $contribution = $this->callAPISuccess('contribution', 'create', $contributionParams); | |
1053 | $newParams = array_merge($contributionParams, array( | |
1054 | 'id' => $contribution['id'], | |
1055 | 'contribution_status_id' => 'Refunded', | |
1056 | 'cancel_date' => '2015-01-01 09:00', | |
1057 | 'refund_trxn_id' => 'the refund', | |
1058 | ) | |
1059 | ); | |
1060 | ||
1061 | $contribution = $this->callAPISuccess('contribution', 'create', $newParams); | |
1062 | $this->_checkFinancialTrxn($contribution, 'refund'); | |
1063 | $this->_checkFinancialItem($contribution['id'], 'refund'); | |
1064 | $this->assertEquals('original_payment', $this->callAPISuccessGetValue('Contribution', array( | |
1065 | 'id' => $contribution['id'], | |
1066 | 'return' => 'trxn_id', | |
1067 | ))); | |
1068 | } | |
4ab7d517 | 1069 | |
797d4c52 | 1070 | /** |
1071 | * Function tests that trxn_id is set when passed in. | |
1072 | * | |
1073 | * Here we ensure that the civicrm_financial_trxn.trxn_id & the civicrm_contribution.trxn_id are set | |
1074 | * when trxn_id is passed in. | |
1075 | */ | |
1076 | public function testCreateUpdateContributionRefundTrxnIDPassedIn() { | |
1077 | $contributionParams = array( | |
1078 | 'contact_id' => $this->_individualId, | |
1079 | 'receive_date' => '2012-01-01', | |
1080 | 'total_amount' => 100.00, | |
1081 | 'financial_type_id' => $this->_financialTypeId, | |
1082 | 'payment_instrument_id' => 4, | |
1083 | 'contribution_status_id' => 1, | |
1084 | 'trxn_id' => 'original_payment', | |
6a488035 | 1085 | ); |
797d4c52 | 1086 | $contribution = $this->callAPISuccess('contribution', 'create', $contributionParams); |
1087 | $newParams = array_merge($contributionParams, array( | |
1088 | 'id' => $contribution['id'], | |
1089 | 'contribution_status_id' => 'Refunded', | |
1090 | 'cancel_date' => '2015-01-01 09:00', | |
1091 | 'trxn_id' => 'the refund', | |
1092 | ) | |
1093 | ); | |
1094 | ||
1095 | $contribution = $this->callAPISuccess('contribution', 'create', $newParams); | |
1096 | $this->_checkFinancialTrxn($contribution, 'refund'); | |
1097 | $this->_checkFinancialItem($contribution['id'], 'refund'); | |
1098 | $this->assertEquals('the refund', $this->callAPISuccessGetValue('Contribution', array( | |
1099 | 'id' => $contribution['id'], | |
1100 | 'return' => 'trxn_id', | |
1101 | ))); | |
1102 | } | |
1103 | ||
1104 | /** | |
1105 | * Function tests that trxn_id is set when passed in. | |
1106 | * | |
1107 | * Here we ensure that the civicrm_contribution.trxn_id is set | |
1108 | * when trxn_id is passed in but if refund_trxn_id is different then that | |
1109 | * is kept for the refund transaction. | |
1110 | */ | |
1111 | public function testCreateUpdateContributionRefundRefundAndTrxnIDPassedIn() { | |
1112 | $contributionParams = array( | |
1113 | 'contact_id' => $this->_individualId, | |
1114 | 'receive_date' => '2012-01-01', | |
1115 | 'total_amount' => 100.00, | |
1116 | 'financial_type_id' => $this->_financialTypeId, | |
1117 | 'payment_instrument_id' => 4, | |
1118 | 'contribution_status_id' => 1, | |
1119 | 'trxn_id' => 'original_payment', | |
1120 | ); | |
1121 | $contribution = $this->callAPISuccess('contribution', 'create', $contributionParams); | |
1122 | $newParams = array_merge($contributionParams, array( | |
5896d037 | 1123 | 'id' => $contribution['id'], |
b7990bb6 | 1124 | 'contribution_status_id' => 'Refunded', |
1125 | 'cancel_date' => '2015-01-01 09:00', | |
797d4c52 | 1126 | 'trxn_id' => 'cont id', |
1127 | 'refund_trxn_id' => 'the refund', | |
6a488035 TO |
1128 | ) |
1129 | ); | |
1130 | ||
694769da | 1131 | $contribution = $this->callAPISuccess('contribution', 'create', $newParams); |
6a488035 TO |
1132 | $this->_checkFinancialTrxn($contribution, 'refund'); |
1133 | $this->_checkFinancialItem($contribution['id'], 'refund'); | |
797d4c52 | 1134 | $this->assertEquals('cont id', $this->callAPISuccessGetValue('Contribution', array( |
1135 | 'id' => $contribution['id'], | |
1136 | 'return' => 'trxn_id', | |
1137 | ))); | |
1138 | } | |
1139 | ||
1140 | /** | |
1141 | * Function tests that refund_trxn_id is set when passed in empty. | |
1142 | * | |
1143 | * Here we ensure that the civicrm_contribution.trxn_id is set | |
1144 | * when trxn_id is passed in but if refund_trxn_id isset but empty then that | |
1145 | * is kept for the refund transaction. | |
1146 | */ | |
1147 | public function testCreateUpdateContributionRefundRefundNullTrxnIDPassedIn() { | |
1148 | $contributionParams = array( | |
1149 | 'contact_id' => $this->_individualId, | |
1150 | 'receive_date' => '2012-01-01', | |
1151 | 'total_amount' => 100.00, | |
1152 | 'financial_type_id' => $this->_financialTypeId, | |
1153 | 'payment_instrument_id' => 4, | |
1154 | 'contribution_status_id' => 1, | |
1155 | 'trxn_id' => 'original_payment', | |
1156 | ); | |
1157 | $contribution = $this->callAPISuccess('contribution', 'create', $contributionParams); | |
1158 | $newParams = array_merge($contributionParams, array( | |
1159 | 'id' => $contribution['id'], | |
1160 | 'contribution_status_id' => 'Refunded', | |
1161 | 'cancel_date' => '2015-01-01 09:00', | |
1162 | 'trxn_id' => 'cont id', | |
1163 | 'refund_trxn_id' => '', | |
1164 | ) | |
1165 | ); | |
1166 | ||
1167 | $contribution = $this->callAPISuccess('contribution', 'create', $newParams); | |
1168 | $this->_checkFinancialTrxn($contribution, 'refund', NULL, array('trxn_id' => NULL)); | |
1169 | $this->_checkFinancialItem($contribution['id'], 'refund'); | |
1170 | $this->assertEquals('cont id', $this->callAPISuccessGetValue('Contribution', array( | |
1171 | 'id' => $contribution['id'], | |
1172 | 'return' => 'trxn_id', | |
1173 | ))); | |
8f39a111 | 1174 | } |
c71ae314 | 1175 | |
a1a2a83d | 1176 | /** |
eceb18cc | 1177 | * Function tests invalid contribution status change. |
c71ae314 | 1178 | */ |
00be9182 | 1179 | public function testCreateUpdateContributionInValidStatusChange() { |
c71ae314 PN |
1180 | $contribParams = array( |
1181 | 'contact_id' => 1, | |
1182 | 'receive_date' => '2012-01-01', | |
1183 | 'total_amount' => 100.00, | |
1184 | 'financial_type_id' => 1, | |
1185 | 'payment_instrument_id' => 1, | |
1186 | 'contribution_status_id' => 1, | |
c71ae314 | 1187 | ); |
4ab7d517 | 1188 | $contribution = $this->callAPISuccess('contribution', 'create', $contribParams); |
c71ae314 | 1189 | $newParams = array_merge($contribParams, array( |
5896d037 TO |
1190 | 'id' => $contribution['id'], |
1191 | 'contribution_status_id' => 2, | |
c71ae314 PN |
1192 | ) |
1193 | ); | |
6c6e6187 | 1194 | $this->callAPIFailure('contribution', 'create', $newParams, ts('Cannot change contribution status from Completed to Pending.')); |
c71ae314 | 1195 | |
6a488035 TO |
1196 | } |
1197 | ||
a1a2a83d | 1198 | /** |
eceb18cc | 1199 | * Function tests that financial records are added when Pending Contribution is Canceled. |
6a488035 | 1200 | */ |
00be9182 | 1201 | public function testCreateUpdateContributionCancelPending() { |
6a488035 TO |
1202 | $contribParams = array( |
1203 | 'contact_id' => $this->_individualId, | |
1204 | 'receive_date' => '2012-01-01', | |
1205 | 'total_amount' => 100.00, | |
4ab7d517 | 1206 | 'financial_type_id' => $this->_financialTypeId, |
6a488035 TO |
1207 | 'payment_instrument_id' => 1, |
1208 | 'contribution_status_id' => 2, | |
c71ae314 | 1209 | 'is_pay_later' => 1, |
4ab7d517 | 1210 | |
6a488035 | 1211 | ); |
4ab7d517 | 1212 | $contribution = $this->callAPISuccess('contribution', 'create', $contribParams); |
6a488035 | 1213 | $newParams = array_merge($contribParams, array( |
5896d037 TO |
1214 | 'id' => $contribution['id'], |
1215 | 'contribution_status_id' => 3, | |
6a488035 TO |
1216 | ) |
1217 | ); | |
694769da | 1218 | $contribution = $this->callAPISuccess('contribution', 'create', $newParams); |
6a488035 TO |
1219 | $this->_checkFinancialTrxn($contribution, 'cancelPending'); |
1220 | $this->_checkFinancialItem($contribution['id'], 'cancelPending'); | |
1221 | } | |
1222 | ||
a1a2a83d | 1223 | /** |
eceb18cc | 1224 | * Function tests that financial records are added when Financial Type is Changed. |
6a488035 | 1225 | */ |
00be9182 | 1226 | public function testCreateUpdateContributionChangeFinancialType() { |
6a488035 TO |
1227 | $contribParams = array( |
1228 | 'contact_id' => $this->_individualId, | |
1229 | 'receive_date' => '2012-01-01', | |
1230 | 'total_amount' => 100.00, | |
1231 | 'financial_type_id' => 1, | |
1232 | 'payment_instrument_id' => 1, | |
1233 | 'contribution_status_id' => 1, | |
4ab7d517 | 1234 | |
6a488035 | 1235 | ); |
4ab7d517 | 1236 | $contribution = $this->callAPISuccess('contribution', 'create', $contribParams); |
6a488035 | 1237 | $newParams = array_merge($contribParams, array( |
5896d037 TO |
1238 | 'id' => $contribution['id'], |
1239 | 'financial_type_id' => 3, | |
6a488035 TO |
1240 | ) |
1241 | ); | |
694769da | 1242 | $contribution = $this->callAPISuccess('contribution', 'create', $newParams); |
6a488035 TO |
1243 | $this->_checkFinancialTrxn($contribution, 'changeFinancial'); |
1244 | $this->_checkFinancialItem($contribution['id'], 'changeFinancial'); | |
1245 | } | |
1246 | ||
694769da | 1247 | /** |
1e52837d | 1248 | * Test that update does not change status id CRM-15105. |
694769da | 1249 | */ |
00be9182 | 1250 | public function testCreateUpdateWithoutChangingPendingStatus() { |
694769da VU |
1251 | $contribution = $this->callAPISuccess('contribution', 'create', array_merge($this->_params, array('contribution_status_id' => 2))); |
1252 | $this->callAPISuccess('contribution', 'create', array('id' => $contribution['id'], 'source' => 'new source')); | |
5896d037 | 1253 | $contribution = $this->callAPISuccess('contribution', 'getsingle', array( |
92915c55 TO |
1254 | 'id' => $contribution['id'], |
1255 | 'api.contribution.delete' => 1, | |
1256 | )); | |
694769da VU |
1257 | $this->assertEquals(2, $contribution['contribution_status_id']); |
1258 | } | |
a1a2a83d TO |
1259 | |
1260 | /** | |
28de42d1 EM |
1261 | * Test Updating a Contribution. |
1262 | * | |
a1a2a83d TO |
1263 | * CHANGE: we require the API to do an incremental update |
1264 | */ | |
00be9182 | 1265 | public function testCreateUpdateContribution() { |
6a488035 | 1266 | |
78ab0ca4 | 1267 | $contributionID = $this->contributionCreate(array( |
1268 | 'contact_id' => $this->_individualId, | |
1269 | 'trxn_id' => 212355, | |
1270 | 'financial_type_id' => $this->_financialTypeId, | |
1271 | 'invoice_id' => 'old_invoice', | |
1272 | )); | |
6a488035 TO |
1273 | $old_params = array( |
1274 | 'contribution_id' => $contributionID, | |
6a488035 | 1275 | ); |
4ab7d517 | 1276 | $original = $this->callAPISuccess('contribution', 'get', $old_params); |
2bfae985 | 1277 | $this->assertEquals($original['id'], $contributionID); |
6a488035 TO |
1278 | //set up list of old params, verify |
1279 | ||
1280 | //This should not be required on update: | |
1281 | $old_contact_id = $original['values'][$contributionID]['contact_id']; | |
7d543448 | 1282 | $old_payment_instrument = $original['values'][$contributionID]['instrument_id']; |
6a488035 TO |
1283 | $old_fee_amount = $original['values'][$contributionID]['fee_amount']; |
1284 | $old_source = $original['values'][$contributionID]['contribution_source']; | |
1285 | ||
6a488035 TO |
1286 | $old_trxn_id = $original['values'][$contributionID]['trxn_id']; |
1287 | $old_invoice_id = $original['values'][$contributionID]['invoice_id']; | |
1288 | ||
1289 | //check against values in CiviUnitTestCase::createContribution() | |
2bfae985 EM |
1290 | $this->assertEquals($old_contact_id, $this->_individualId); |
1291 | $this->assertEquals($old_fee_amount, 5.00); | |
1292 | $this->assertEquals($old_source, 'SSF'); | |
1293 | $this->assertEquals($old_trxn_id, 212355); | |
78ab0ca4 | 1294 | $this->assertEquals($old_invoice_id, 'old_invoice'); |
6a488035 TO |
1295 | $params = array( |
1296 | 'id' => $contributionID, | |
1297 | 'contact_id' => $this->_individualId, | |
1298 | 'total_amount' => 110.00, | |
4ab7d517 | 1299 | 'financial_type_id' => $this->_financialTypeId, |
6a488035 TO |
1300 | 'non_deductible_amount' => 10.00, |
1301 | 'net_amount' => 100.00, | |
1302 | 'contribution_status_id' => 1, | |
1303 | 'note' => 'Donating for Nobel Cause', | |
4ab7d517 | 1304 | |
6a488035 TO |
1305 | ); |
1306 | ||
4ab7d517 | 1307 | $contribution = $this->callAPISuccess('contribution', 'create', $params); |
6a488035 TO |
1308 | |
1309 | $new_params = array( | |
1310 | 'contribution_id' => $contribution['id'], | |
4ab7d517 | 1311 | |
6a488035 | 1312 | ); |
4ab7d517 | 1313 | $contribution = $this->callAPISuccess('contribution', 'get', $new_params); |
6a488035 | 1314 | |
2bfae985 EM |
1315 | $this->assertEquals($contribution['values'][$contributionID]['contact_id'], $this->_individualId); |
1316 | $this->assertEquals($contribution['values'][$contributionID]['total_amount'], 110.00); | |
ff977830 | 1317 | $this->assertEquals($contribution['values'][$contributionID]['financial_type_id'], $this->_financialTypeId); |
7d543448 | 1318 | $this->assertEquals($contribution['values'][$contributionID]['instrument_id'], $old_payment_instrument); |
2bfae985 EM |
1319 | $this->assertEquals($contribution['values'][$contributionID]['non_deductible_amount'], 10.00); |
1320 | $this->assertEquals($contribution['values'][$contributionID]['fee_amount'], $old_fee_amount); | |
1321 | $this->assertEquals($contribution['values'][$contributionID]['net_amount'], 100.00); | |
1322 | $this->assertEquals($contribution['values'][$contributionID]['trxn_id'], $old_trxn_id); | |
1323 | $this->assertEquals($contribution['values'][$contributionID]['invoice_id'], $old_invoice_id); | |
1324 | $this->assertEquals($contribution['values'][$contributionID]['contribution_source'], $old_source); | |
1325 | $this->assertEquals($contribution['values'][$contributionID]['contribution_status'], 'Completed'); | |
6a488035 TO |
1326 | $params = array( |
1327 | 'contribution_id' => $contributionID, | |
4ab7d517 | 1328 | |
6a488035 | 1329 | ); |
4ab7d517 | 1330 | $result = $this->callAPISuccess('contribution', 'delete', $params); |
22f80e87 | 1331 | $this->assertAPISuccess($result); |
6a488035 TO |
1332 | } |
1333 | ||
1334 | ///////////////// civicrm_contribution_delete methods | |
a1a2a83d TO |
1335 | |
1336 | /** | |
1337 | * Attempt (but fail) to delete a contribution without parameters. | |
1338 | */ | |
00be9182 | 1339 | public function testDeleteEmptyParamsContribution() { |
4ab7d517 | 1340 | $params = array(); |
858d0bf8 | 1341 | $this->callAPIFailure('contribution', 'delete', $params); |
6a488035 TO |
1342 | } |
1343 | ||
00be9182 | 1344 | public function testDeleteParamsNotArrayContribution() { |
6a488035 | 1345 | $params = 'contribution_id= 1'; |
d0e1eff2 | 1346 | $contribution = $this->callAPIFailure('contribution', 'delete', $params); |
6a488035 TO |
1347 | $this->assertEquals($contribution['error_message'], 'Input variable `params` is not an array'); |
1348 | } | |
1349 | ||
00be9182 | 1350 | public function testDeleteWrongParamContribution() { |
6a488035 TO |
1351 | $params = array( |
1352 | 'contribution_source' => 'SSF', | |
4ab7d517 | 1353 | |
6a488035 | 1354 | ); |
858d0bf8 | 1355 | $this->callAPIFailure('contribution', 'delete', $params); |
6a488035 TO |
1356 | } |
1357 | ||
00be9182 | 1358 | public function testDeleteContribution() { |
78ab0ca4 | 1359 | $contributionID = $this->contributionCreate(array( |
1360 | 'contact_id' => $this->_individualId, | |
1361 | 'financial_type_id' => $this->_financialTypeId, | |
1362 | )); | |
6a488035 TO |
1363 | $params = array( |
1364 | 'id' => $contributionID, | |
6a488035 | 1365 | ); |
4ab7d517 | 1366 | $this->callAPIAndDocument('contribution', 'delete', $params, __FUNCTION__, __FILE__); |
6a488035 TO |
1367 | } |
1368 | ||
1369 | /** | |
d177a2a6 | 1370 | * Test civicrm_contribution_search with empty params. |
1e52837d | 1371 | * |
d177a2a6 | 1372 | * All available contributions expected. |
6a488035 | 1373 | */ |
00be9182 | 1374 | public function testSearchEmptyParams() { |
4ab7d517 | 1375 | $params = array(); |
6a488035 TO |
1376 | |
1377 | $p = array( | |
1378 | 'contact_id' => $this->_individualId, | |
1379 | 'receive_date' => date('Ymd'), | |
1380 | 'total_amount' => 100.00, | |
4ab7d517 | 1381 | 'financial_type_id' => $this->_financialTypeId, |
6a488035 TO |
1382 | 'non_deductible_amount' => 10.00, |
1383 | 'fee_amount' => 5.00, | |
1384 | 'net_amount' => 95.00, | |
1385 | 'trxn_id' => 23456, | |
1386 | 'invoice_id' => 78910, | |
1387 | 'source' => 'SSF', | |
1388 | 'contribution_status_id' => 1, | |
4ab7d517 | 1389 | |
6a488035 | 1390 | ); |
4ab7d517 | 1391 | $contribution = $this->callAPISuccess('contribution', 'create', $p); |
6a488035 | 1392 | |
4ab7d517 | 1393 | $result = $this->callAPISuccess('contribution', 'get', $params); |
6a488035 TO |
1394 | // We're taking the first element. |
1395 | $res = $result['values'][$contribution['id']]; | |
1396 | ||
2bfae985 EM |
1397 | $this->assertEquals($p['contact_id'], $res['contact_id']); |
1398 | $this->assertEquals($p['total_amount'], $res['total_amount']); | |
5896d037 | 1399 | $this->assertEquals($p['financial_type_id'], $res['financial_type_id']); |
2bfae985 EM |
1400 | $this->assertEquals($p['net_amount'], $res['net_amount']); |
1401 | $this->assertEquals($p['non_deductible_amount'], $res['non_deductible_amount']); | |
1402 | $this->assertEquals($p['fee_amount'], $res['fee_amount']); | |
1403 | $this->assertEquals($p['trxn_id'], $res['trxn_id']); | |
1404 | $this->assertEquals($p['invoice_id'], $res['invoice_id']); | |
1405 | $this->assertEquals($p['source'], $res['contribution_source']); | |
6a488035 | 1406 | // contribution_status_id = 1 => Completed |
2bfae985 | 1407 | $this->assertEquals('Completed', $res['contribution_status']); |
6a488035 TO |
1408 | |
1409 | $this->contributionDelete($contribution['id']); | |
1410 | } | |
1411 | ||
1412 | /** | |
d177a2a6 | 1413 | * Test civicrm_contribution_search. Success expected. |
6a488035 | 1414 | */ |
00be9182 | 1415 | public function testSearch() { |
6a488035 TO |
1416 | $p1 = array( |
1417 | 'contact_id' => $this->_individualId, | |
1418 | 'receive_date' => date('Ymd'), | |
1419 | 'total_amount' => 100.00, | |
4ab7d517 | 1420 | 'financial_type_id' => $this->_financialTypeId, |
6a488035 TO |
1421 | 'non_deductible_amount' => 10.00, |
1422 | 'contribution_status_id' => 1, | |
4ab7d517 | 1423 | |
6a488035 | 1424 | ); |
4ab7d517 | 1425 | $contribution1 = $this->callAPISuccess('contribution', 'create', $p1); |
6a488035 TO |
1426 | |
1427 | $p2 = array( | |
1428 | 'contact_id' => $this->_individualId, | |
1429 | 'receive_date' => date('Ymd'), | |
1430 | 'total_amount' => 200.00, | |
4ab7d517 | 1431 | 'financial_type_id' => $this->_financialTypeId, |
6a488035 TO |
1432 | 'non_deductible_amount' => 20.00, |
1433 | 'trxn_id' => 5454565, | |
1434 | 'invoice_id' => 1212124, | |
1435 | 'fee_amount' => 50.00, | |
1436 | 'net_amount' => 60.00, | |
1437 | 'contribution_status_id' => 2, | |
4ab7d517 | 1438 | |
6a488035 | 1439 | ); |
2f45e1c2 | 1440 | $contribution2 = $this->callAPISuccess('contribution', 'create', $p2); |
6a488035 TO |
1441 | |
1442 | $params = array( | |
1443 | 'contribution_id' => $contribution2['id'], | |
4ab7d517 | 1444 | |
6a488035 | 1445 | ); |
2f45e1c2 | 1446 | $result = $this->callAPISuccess('contribution', 'get', $params); |
6a488035 TO |
1447 | $res = $result['values'][$contribution2['id']]; |
1448 | ||
2bfae985 EM |
1449 | $this->assertEquals($p2['contact_id'], $res['contact_id']); |
1450 | $this->assertEquals($p2['total_amount'], $res['total_amount']); | |
5896d037 | 1451 | $this->assertEquals($p2['financial_type_id'], $res['financial_type_id']); |
2bfae985 EM |
1452 | $this->assertEquals($p2['net_amount'], $res['net_amount']); |
1453 | $this->assertEquals($p2['non_deductible_amount'], $res['non_deductible_amount']); | |
1454 | $this->assertEquals($p2['fee_amount'], $res['fee_amount']); | |
1455 | $this->assertEquals($p2['trxn_id'], $res['trxn_id']); | |
1456 | $this->assertEquals($p2['invoice_id'], $res['invoice_id']); | |
6a488035 | 1457 | // contribution_status_id = 2 => Pending |
2bfae985 | 1458 | $this->assertEquals('Pending', $res['contribution_status']); |
6a488035 TO |
1459 | |
1460 | $this->contributionDelete($contribution1['id']); | |
1461 | $this->contributionDelete($contribution2['id']); | |
1462 | } | |
2f45e1c2 | 1463 | |
0efa8efe | 1464 | /** |
eceb18cc | 1465 | * Test completing a transaction via the API. |
0efa8efe | 1466 | * |
1467 | * Note that we are creating a logged in user because email goes out from | |
1468 | * that person | |
1469 | */ | |
00be9182 | 1470 | public function testCompleteTransaction() { |
5896d037 | 1471 | $mut = new CiviMailUtils($this, TRUE); |
0efa8efe | 1472 | $this->createLoggedInUser(); |
6c6e6187 TO |
1473 | $params = array_merge($this->_params, array('contribution_status_id' => 2)); |
1474 | $contribution = $this->callAPISuccess('contribution', 'create', $params); | |
66d3f9be | 1475 | $this->callAPISuccess('contribution', 'completetransaction', array( |
0efa8efe | 1476 | 'id' => $contribution['id'], |
66d3f9be | 1477 | )); |
6c6e6187 | 1478 | $contribution = $this->callAPISuccess('contribution', 'get', array('id' => $contribution['id'], 'sequential' => 1)); |
0efa8efe | 1479 | $this->assertEquals('Completed', $contribution['values'][0]['contribution_status']); |
1480 | $mut->checkMailLog(array( | |
1481 | 'Receipt - Contribution', | |
1482 | 'Please print this confirmation for your records.', | |
1483 | )); | |
46fa5206 EM |
1484 | $mut->stop(); |
1485 | } | |
1486 | ||
080a561b | 1487 | /** |
1488 | * Test completing a transaction via the API. | |
1489 | * | |
1490 | * Note that we are creating a logged in user because email goes out from | |
1491 | * that person | |
1492 | */ | |
1493 | public function testCompleteTransactionFeeAmount() { | |
1494 | $this->createLoggedInUser(); | |
1495 | $params = array_merge($this->_params, array('contribution_status_id' => 2)); | |
1496 | $contribution = $this->callAPISuccess('contribution', 'create', $params); | |
1497 | $this->callAPISuccess('contribution', 'completetransaction', array( | |
1498 | 'id' => $contribution['id'], | |
1499 | 'fee_amount' => '.56', | |
1500 | 'trxn_id' => '7778888', | |
1501 | )); | |
1502 | $contribution = $this->callAPISuccess('contribution', 'getsingle', array('id' => $contribution['id'], 'sequential' => 1)); | |
1503 | $this->assertEquals('Completed', $contribution['contribution_status']); | |
1504 | $this->assertEquals('7778888', $contribution['trxn_id']); | |
1505 | $this->assertEquals('.56', $contribution['fee_amount']); | |
1506 | $this->assertEquals('99.44', $contribution['net_amount']); | |
1507 | } | |
1508 | ||
d97c96dc EM |
1509 | /** |
1510 | * Test repeat contribution successfully creates line items. | |
1511 | */ | |
1e52837d | 1512 | public function testRepeatTransaction() { |
d97c96dc EM |
1513 | $paymentProcessorID = $this->paymentProcessorCreate(); |
1514 | $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array( | |
1515 | 'contact_id' => $this->_individualId, | |
1516 | 'installments' => '12', | |
1517 | 'frequency_interval' => '1', | |
1518 | 'amount' => '500', | |
1519 | 'contribution_status_id' => 1, | |
1520 | 'start_date' => '2012-01-01 00:00:00', | |
1521 | 'currency' => 'USD', | |
1522 | 'frequency_unit' => 'month', | |
1523 | 'payment_processor_id' => $paymentProcessorID, | |
1524 | )); | |
1525 | $originalContribution = $this->callAPISuccess('contribution', 'create', array_merge( | |
1526 | $this->_params, | |
1527 | array('contribution_recur_id' => $contributionRecur['id'])) | |
1528 | ); | |
1529 | ||
1530 | $this->callAPISuccess('contribution', 'repeattransaction', array( | |
1531 | 'original_contribution_id' => $originalContribution['id'], | |
1532 | 'contribution_status_id' => 'Completed', | |
1533 | 'trxn_id' => uniqid(), | |
1534 | )); | |
1535 | $lineItemParams = array( | |
1536 | 'entity_id' => $originalContribution['id'], | |
1537 | 'sequential' => 1, | |
1538 | 'return' => array( | |
1539 | 'entity_table', | |
1540 | 'qty', | |
1541 | 'unit_price', | |
1542 | 'line_total', | |
1543 | 'label', | |
1544 | 'financial_type_id', | |
1545 | 'deductible_amount', | |
1546 | 'price_field_value_id', | |
1547 | 'price_field_id', | |
1e52837d | 1548 | ), |
d97c96dc EM |
1549 | ); |
1550 | $lineItem1 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array( | |
1551 | 'entity_id' => $originalContribution['id'], | |
1552 | ))); | |
1553 | $lineItem2 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array( | |
1554 | 'entity_id' => $originalContribution['id'] + 1, | |
1555 | ))); | |
1556 | unset($lineItem1['values'][0]['id'], $lineItem1['values'][0]['entity_id']); | |
1557 | unset($lineItem2['values'][0]['id'], $lineItem2['values'][0]['entity_id']); | |
1558 | $this->assertEquals($lineItem1['values'][0], $lineItem2['values'][0]); | |
1559 | ||
1560 | $this->quickCleanUpFinancialEntities(); | |
1561 | } | |
1562 | ||
1eade77d | 1563 | /** |
1564 | * Test repeat contribution accepts recur_id instead of original_contribution_id. | |
1565 | */ | |
1566 | public function testRepeatTransactionAcceptRecurID() { | |
1567 | $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array( | |
1568 | 'contact_id' => $this->_individualId, | |
1569 | 'installments' => '12', | |
1570 | 'frequency_interval' => '1', | |
1571 | 'amount' => '100', | |
1572 | 'contribution_status_id' => 1, | |
1573 | 'start_date' => '2012-01-01 00:00:00', | |
1574 | 'currency' => 'USD', | |
1575 | 'frequency_unit' => 'month', | |
1576 | 'payment_processor_id' => $this->paymentProcessorID, | |
1577 | )); | |
1578 | $this->callAPISuccess('contribution', 'create', array_merge( | |
1579 | $this->_params, | |
1580 | array('contribution_recur_id' => $contributionRecur['id'])) | |
1581 | ); | |
1582 | ||
1583 | $this->callAPISuccess('contribution', 'repeattransaction', array( | |
1584 | 'contribution_recur_id' => $contributionRecur['id'], | |
1585 | 'contribution_status_id' => 'Completed', | |
1586 | 'trxn_id' => uniqid(), | |
1587 | )); | |
1588 | ||
1589 | $this->quickCleanUpFinancialEntities(); | |
1590 | } | |
1591 | ||
c03f1689 EM |
1592 | /** |
1593 | * CRM-16397 test appropriate action if total amount has changed for single line items. | |
1594 | */ | |
1595 | public function testRepeatTransactionAlteredAmount() { | |
1596 | $paymentProcessorID = $this->paymentProcessorCreate(); | |
c03f1689 EM |
1597 | $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array( |
1598 | 'contact_id' => $this->_individualId, | |
1599 | 'installments' => '12', | |
1600 | 'frequency_interval' => '1', | |
1601 | 'amount' => '500', | |
1602 | 'contribution_status_id' => 1, | |
1603 | 'start_date' => '2012-01-01 00:00:00', | |
1604 | 'currency' => 'USD', | |
1605 | 'frequency_unit' => 'month', | |
1606 | 'payment_processor_id' => $paymentProcessorID, | |
1607 | )); | |
1608 | $originalContribution = $this->callAPISuccess('contribution', 'create', array_merge( | |
1609 | $this->_params, | |
1610 | array( | |
1611 | 'contribution_recur_id' => $contributionRecur['id'], | |
c03f1689 EM |
1612 | )) |
1613 | ); | |
1614 | ||
1615 | $this->callAPISuccess('contribution', 'repeattransaction', array( | |
1616 | 'original_contribution_id' => $originalContribution['id'], | |
1617 | 'contribution_status_id' => 'Completed', | |
1618 | 'trxn_id' => uniqid(), | |
1619 | 'total_amount' => '400', | |
1620 | 'fee_amount' => 50, | |
c03f1689 EM |
1621 | )); |
1622 | $lineItemParams = array( | |
1623 | 'entity_id' => $originalContribution['id'], | |
1624 | 'sequential' => 1, | |
1625 | 'return' => array( | |
1626 | 'entity_table', | |
1627 | 'qty', | |
1628 | 'unit_price', | |
1629 | 'line_total', | |
1630 | 'label', | |
1631 | 'financial_type_id', | |
1632 | 'deductible_amount', | |
1633 | 'price_field_value_id', | |
1634 | 'price_field_id', | |
1635 | ), | |
1636 | ); | |
1637 | $this->callAPISuccessGetSingle('contribution', array( | |
1638 | 'total_amount' => 400, | |
c03f1689 EM |
1639 | 'fee_amount' => 50, |
1640 | 'net_amount' => 350, | |
1641 | )); | |
1642 | $lineItem1 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array( | |
1643 | 'entity_id' => $originalContribution['id'], | |
1644 | ))); | |
1645 | $expectedLineItem = array_merge( | |
1646 | $lineItem1['values'][0], array( | |
1647 | 'line_total' => '400.00', | |
1648 | 'unit_price' => '400.00', | |
542d9e2c EM |
1649 | ) |
1650 | ); | |
c03f1689 EM |
1651 | |
1652 | $lineItem2 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array( | |
1653 | 'entity_id' => $originalContribution['id'] + 1, | |
1654 | ))); | |
1655 | unset($expectedLineItem['id'], $expectedLineItem['entity_id']); | |
1656 | unset($lineItem2['values'][0]['id'], $lineItem2['values'][0]['entity_id']); | |
1657 | $this->assertEquals($expectedLineItem, $lineItem2['values'][0]); | |
c02c17df | 1658 | } |
c03f1689 | 1659 | |
3c49d90c | 1660 | /** |
1661 | * CRM-17718 test appropriate action if financial type has changed for single line items. | |
1662 | */ | |
1663 | public function testRepeatTransactionPassedInFinancialType() { | |
1664 | $originalContribution = $this->setUpRecurringContribution(); | |
1665 | ||
1666 | $this->callAPISuccess('contribution', 'repeattransaction', array( | |
1667 | 'original_contribution_id' => $originalContribution['id'], | |
1668 | 'contribution_status_id' => 'Completed', | |
1669 | 'trxn_id' => uniqid(), | |
1670 | 'financial_type_id' => 2, | |
1671 | )); | |
1672 | $lineItemParams = array( | |
1673 | 'entity_id' => $originalContribution['id'], | |
1674 | 'sequential' => 1, | |
1675 | 'return' => array( | |
1676 | 'entity_table', | |
1677 | 'qty', | |
1678 | 'unit_price', | |
1679 | 'line_total', | |
1680 | 'label', | |
1681 | 'financial_type_id', | |
1682 | 'deductible_amount', | |
1683 | 'price_field_value_id', | |
1684 | 'price_field_id', | |
1685 | ), | |
1686 | ); | |
1687 | ||
1688 | $this->callAPISuccessGetSingle('contribution', array( | |
1689 | 'total_amount' => 100, | |
1690 | 'financial_type_id' => 2, | |
1691 | )); | |
1692 | $lineItem1 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array( | |
1693 | 'entity_id' => $originalContribution['id'], | |
1694 | ))); | |
1695 | $expectedLineItem = array_merge( | |
1696 | $lineItem1['values'][0], array( | |
1697 | 'line_total' => '100.00', | |
1698 | 'unit_price' => '100.00', | |
1699 | 'financial_type_id' => 2, | |
1700 | ) | |
1701 | ); | |
1702 | ||
1703 | $lineItem2 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array( | |
1704 | 'entity_id' => $originalContribution['id'] + 1, | |
1705 | ))); | |
1706 | unset($expectedLineItem['id'], $expectedLineItem['entity_id']); | |
1707 | unset($lineItem2['values'][0]['id'], $lineItem2['values'][0]['entity_id']); | |
1708 | $this->assertEquals($expectedLineItem, $lineItem2['values'][0]); | |
1709 | } | |
1710 | ||
7f4ef731 | 1711 | /** |
1712 | * CRM-17718 test appropriate action if financial type has changed for single line items. | |
1713 | */ | |
1714 | public function testRepeatTransactionUpdatedFinancialType() { | |
1715 | $originalContribution = $this->setUpRecurringContribution(array(), array('financial_type_id' => 2)); | |
1716 | ||
1717 | $this->callAPISuccess('contribution', 'repeattransaction', array( | |
1718 | 'contribution_recur_id' => $originalContribution['id'], | |
1719 | 'contribution_status_id' => 'Completed', | |
1720 | 'trxn_id' => uniqid(), | |
1721 | )); | |
1722 | $lineItemParams = array( | |
1723 | 'entity_id' => $originalContribution['id'], | |
1724 | 'sequential' => 1, | |
1725 | 'return' => array( | |
1726 | 'entity_table', | |
1727 | 'qty', | |
1728 | 'unit_price', | |
1729 | 'line_total', | |
1730 | 'label', | |
1731 | 'financial_type_id', | |
1732 | 'deductible_amount', | |
1733 | 'price_field_value_id', | |
1734 | 'price_field_id', | |
1735 | ), | |
1736 | ); | |
1737 | ||
1738 | $this->callAPISuccessGetSingle('contribution', array( | |
1739 | 'total_amount' => 100, | |
1740 | 'financial_type_id' => 2, | |
1741 | )); | |
1742 | $lineItem1 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array( | |
1743 | 'entity_id' => $originalContribution['id'], | |
1744 | ))); | |
1745 | $expectedLineItem = array_merge( | |
1746 | $lineItem1['values'][0], array( | |
1747 | 'line_total' => '100.00', | |
1748 | 'unit_price' => '100.00', | |
1749 | 'financial_type_id' => 2, | |
1750 | ) | |
1751 | ); | |
1752 | ||
1753 | $lineItem2 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array( | |
1754 | 'entity_id' => $originalContribution['id'] + 1, | |
1755 | ))); | |
1756 | unset($expectedLineItem['id'], $expectedLineItem['entity_id']); | |
1757 | unset($lineItem2['values'][0]['id'], $lineItem2['values'][0]['entity_id']); | |
1758 | $this->assertEquals($expectedLineItem, $lineItem2['values'][0]); | |
1759 | } | |
1760 | ||
c02c17df | 1761 | /** |
1eade77d | 1762 | * CRM-16397 test appropriate action if campaign has been passed in. |
c02c17df | 1763 | */ |
1764 | public function testRepeatTransactionPassedInCampaign() { | |
1765 | $paymentProcessorID = $this->paymentProcessorCreate(); | |
1766 | $campaignID = $this->campaignCreate(); | |
1767 | $campaignID2 = $this->campaignCreate(); | |
1768 | $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array( | |
1769 | 'contact_id' => $this->_individualId, | |
1770 | 'installments' => '12', | |
1771 | 'frequency_interval' => '1', | |
1772 | 'amount' => '100', | |
1773 | 'contribution_status_id' => 1, | |
1774 | 'start_date' => '2012-01-01 00:00:00', | |
1775 | 'currency' => 'USD', | |
1776 | 'frequency_unit' => 'month', | |
1777 | 'payment_processor_id' => $paymentProcessorID, | |
1778 | )); | |
1779 | $originalContribution = $this->callAPISuccess('contribution', 'create', array_merge( | |
1780 | $this->_params, | |
1781 | array( | |
1782 | 'contribution_recur_id' => $contributionRecur['id'], | |
1783 | 'campaign_id' => $campaignID, | |
1784 | )) | |
1785 | ); | |
1786 | ||
1787 | $this->callAPISuccess('contribution', 'repeattransaction', array( | |
1788 | 'original_contribution_id' => $originalContribution['id'], | |
1789 | 'contribution_status_id' => 'Completed', | |
1790 | 'trxn_id' => uniqid(), | |
1791 | 'campaign_id' => $campaignID2, | |
1792 | )); | |
1793 | ||
1794 | $this->callAPISuccessGetSingle('contribution', array( | |
1795 | 'total_amount' => 100, | |
1796 | 'campaign_id' => $campaignID2, | |
1797 | )); | |
1798 | } | |
1799 | ||
1800 | /** | |
1801 | * CRM-17718 campaign stored on contribution recur gets priority. | |
1802 | * | |
1803 | * This reflects the fact we permit people to update them. | |
1804 | */ | |
1805 | public function testRepeatTransactionUpdatedCampaign() { | |
1806 | $paymentProcessorID = $this->paymentProcessorCreate(); | |
1807 | $campaignID = $this->campaignCreate(); | |
1808 | $campaignID2 = $this->campaignCreate(); | |
1809 | $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array( | |
1810 | 'contact_id' => $this->_individualId, | |
1811 | 'installments' => '12', | |
1812 | 'frequency_interval' => '1', | |
1813 | 'amount' => '100', | |
1814 | 'contribution_status_id' => 1, | |
1815 | 'start_date' => '2012-01-01 00:00:00', | |
1816 | 'currency' => 'USD', | |
1817 | 'frequency_unit' => 'month', | |
1818 | 'payment_processor_id' => $paymentProcessorID, | |
1819 | 'campaign_id' => $campaignID, | |
1820 | )); | |
1821 | $originalContribution = $this->callAPISuccess('contribution', 'create', array_merge( | |
1822 | $this->_params, | |
1823 | array( | |
1824 | 'contribution_recur_id' => $contributionRecur['id'], | |
1825 | 'campaign_id' => $campaignID2, | |
1826 | )) | |
1827 | ); | |
1828 | ||
1829 | $this->callAPISuccess('contribution', 'repeattransaction', array( | |
1830 | 'original_contribution_id' => $originalContribution['id'], | |
1831 | 'contribution_status_id' => 'Completed', | |
1832 | 'trxn_id' => uniqid(), | |
1833 | )); | |
1834 | ||
1835 | $this->callAPISuccessGetSingle('contribution', array( | |
1836 | 'total_amount' => 100, | |
1837 | 'campaign_id' => $campaignID, | |
1838 | )); | |
c03f1689 EM |
1839 | } |
1840 | ||
2936c3b5 EM |
1841 | /** |
1842 | * Test completing a transaction does not 'mess' with net amount (CRM-15960). | |
1843 | */ | |
1844 | public function testCompleteTransactionNetAmountOK() { | |
1845 | $this->createLoggedInUser(); | |
1846 | $params = array_merge($this->_params, array('contribution_status_id' => 2)); | |
1847 | unset($params['net_amount']); | |
1848 | $contribution = $this->callAPISuccess('contribution', 'create', $params); | |
1849 | $this->callAPISuccess('contribution', 'completetransaction', array( | |
1850 | 'id' => $contribution['id'], | |
1851 | )); | |
1852 | $contribution = $this->callAPISuccess('contribution', 'getsingle', array('id' => $contribution['id'])); | |
1853 | $this->assertEquals('Completed', $contribution['contribution_status']); | |
1854 | $this->assertTrue(($contribution['total_amount'] - $contribution['net_amount']) == $contribution['fee_amount']); | |
1855 | } | |
1856 | ||
46fa5206 | 1857 | /** |
1e52837d | 1858 | * CRM-14151 - Test completing a transaction via the API. |
46fa5206 | 1859 | */ |
00be9182 | 1860 | public function testCompleteTransactionWithReceiptDateSet() { |
5896d037 | 1861 | $mut = new CiviMailUtils($this, TRUE); |
46fa5206 | 1862 | $this->createLoggedInUser(); |
6c6e6187 TO |
1863 | $params = array_merge($this->_params, array('contribution_status_id' => 2, 'receipt_date' => 'now')); |
1864 | $contribution = $this->callAPISuccess('contribution', 'create', $params); | |
7104593e | 1865 | $this->callAPISuccess('contribution', 'completetransaction', array('id' => $contribution['id'], 'trxn_date' => date('Y-m-d'))); |
6c6e6187 | 1866 | $contribution = $this->callAPISuccess('contribution', 'get', array('id' => $contribution['id'], 'sequential' => 1)); |
46fa5206 | 1867 | $this->assertEquals('Completed', $contribution['values'][0]['contribution_status']); |
7104593e | 1868 | $this->assertEquals(date('Y-m-d'), date('Y-m-d', strtotime($contribution['values'][0]['receive_date']))); |
46fa5206 EM |
1869 | $mut->checkMailLog(array( |
1870 | 'Receipt - Contribution', | |
1871 | 'Please print this confirmation for your records.', | |
1872 | )); | |
0efa8efe | 1873 | $mut->stop(); |
1874 | } | |
1875 | ||
91259407 | 1876 | /** |
1877 | * Test completing first transaction in a recurring series. | |
1878 | * | |
1879 | * The status should be set to 'in progress' and the next scheduled payment date calculated. | |
1880 | */ | |
1881 | public function testCompleteTransactionSetStatusToInProgress() { | |
1882 | $paymentProcessorID = $this->paymentProcessorCreate(); | |
1883 | $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array( | |
1884 | 'contact_id' => $this->_individualId, | |
1885 | 'installments' => '12', | |
1886 | 'frequency_interval' => '1', | |
1887 | 'amount' => '500', | |
1888 | 'contribution_status_id' => 'Pending', | |
1889 | 'start_date' => '2012-01-01 00:00:00', | |
1890 | 'currency' => 'USD', | |
1891 | 'frequency_unit' => 'month', | |
1892 | 'payment_processor_id' => $paymentProcessorID, | |
1893 | )); | |
1894 | $contribution = $this->callAPISuccess('contribution', 'create', array_merge( | |
1895 | $this->_params, | |
1896 | array( | |
1897 | 'contribution_recur_id' => $contributionRecur['id'], | |
1898 | 'contribution_status_id' => 'Pending', | |
1899 | )) | |
1900 | ); | |
1901 | $this->callAPISuccess('Contribution', 'completetransaction', array('id' => $contribution)); | |
1902 | $contributionRecur = $this->callAPISuccessGetSingle('ContributionRecur', array( | |
1903 | 'id' => $contributionRecur['id'], | |
1904 | 'return' => array('next_sched_contribution_date', 'contribution_status_id'), | |
1905 | )); | |
1906 | $this->assertEquals(5, $contributionRecur['contribution_status_id']); | |
1907 | $this->assertEquals(date('Y-m-d 00:00:00', strtotime('+1 month')), $contributionRecur['next_sched_contribution_date']); | |
1908 | } | |
1909 | ||
0efa8efe | 1910 | /** |
eceb18cc | 1911 | * Test completing a transaction with an event via the API. |
0efa8efe | 1912 | * |
1913 | * Note that we are creating a logged in user because email goes out from | |
1914 | * that person | |
1915 | */ | |
00be9182 | 1916 | public function testCompleteTransactionWithParticipantRecord() { |
5896d037 | 1917 | $mut = new CiviMailUtils($this, TRUE); |
0efa8efe | 1918 | $mut->clearMessages(); |
1919 | $this->createLoggedInUser(); | |
1920 | $contributionID = $this->createPendingParticipantContribution(); | |
66d3f9be | 1921 | $this->callAPISuccess('contribution', 'completetransaction', array( |
5896d037 TO |
1922 | 'id' => $contributionID, |
1923 | ) | |
0efa8efe | 1924 | ); |
5896d037 | 1925 | $participantStatus = $this->callAPISuccessGetValue('participant', array( |
92915c55 TO |
1926 | 'id' => $this->_ids['participant'], |
1927 | 'return' => 'participant_status_id', | |
1928 | )); | |
0efa8efe | 1929 | $this->assertEquals(1, $participantStatus); |
1930 | $mut->checkMailLog(array( | |
1931 | 'Annual CiviCRM meet', | |
1932 | 'Event', | |
afc59fef | 1933 | 'This letter is a confirmation that your registration has been received and your status has been updated to Registered.', |
0efa8efe | 1934 | )); |
66d3f9be EM |
1935 | $mut->stop(); |
1936 | } | |
1937 | ||
1938 | /** | |
eceb18cc | 1939 | * Test membership is renewed when transaction completed. |
66d3f9be | 1940 | */ |
00be9182 | 1941 | public function testCompleteTransactionMembershipPriceSet() { |
66d3f9be | 1942 | $this->createPriceSetWithPage('membership'); |
4ff927bc | 1943 | $stateOfGrace = $this->callAPISuccess('MembershipStatus', 'getvalue', array( |
1944 | 'name' => 'Grace', | |
1945 | 'return' => 'id') | |
1946 | ); | |
66d3f9be | 1947 | $this->setUpPendingContribution($this->_ids['price_field_value'][0]); |
4ff927bc | 1948 | $membership = $this->callAPISuccess('membership', 'getsingle', array('id' => $this->_ids['membership'])); |
1949 | $logs = $this->callAPISuccess('MembershipLog', 'get', array( | |
1950 | 'membership_id' => $this->_ids['membership'], | |
1951 | )); | |
1952 | $this->assertEquals(1, $logs['count']); | |
1953 | $this->assertEquals($stateOfGrace, $membership['status_id']); | |
6c6e6187 | 1954 | $this->callAPISuccess('contribution', 'completetransaction', array('id' => $this->_ids['contribution'])); |
66d3f9be EM |
1955 | $membership = $this->callAPISuccess('membership', 'getsingle', array('id' => $this->_ids['membership'])); |
1956 | $this->assertEquals(date('Y-m-d', strtotime('yesterday + 1 year')), $membership['end_date']); | |
4ff927bc | 1957 | $this->callAPISuccessGetSingle('LineItem', array( |
1958 | 'entity_id' => $this->_ids['membership'], | |
1959 | 'entity_table' => 'civicrm_membership', | |
1960 | )); | |
1961 | $logs = $this->callAPISuccess('MembershipLog', 'get', array( | |
1962 | 'membership_id' => $this->_ids['membership'], | |
1963 | )); | |
1964 | $this->assertEquals(2, $logs['count']); | |
1965 | $this->assertNotEquals($stateOfGrace, $logs['values'][2]['status_id']); | |
66d3f9be EM |
1966 | $this->cleanUpAfterPriceSets(); |
1967 | } | |
1968 | ||
1969 | /** | |
eceb18cc | 1970 | * Test membership is renewed when transaction completed. |
66d3f9be | 1971 | */ |
00be9182 | 1972 | public function testCompleteTransactionMembershipPriceSetTwoTerms() { |
66d3f9be EM |
1973 | $this->createPriceSetWithPage('membership'); |
1974 | $this->setUpPendingContribution($this->_ids['price_field_value'][1]); | |
6c6e6187 | 1975 | $this->callAPISuccess('contribution', 'completetransaction', array('id' => $this->_ids['contribution'])); |
66d3f9be EM |
1976 | $membership = $this->callAPISuccess('membership', 'getsingle', array('id' => $this->_ids['membership'])); |
1977 | $this->assertEquals(date('Y-m-d', strtotime('yesterday + 2 years')), $membership['end_date']); | |
1978 | $this->cleanUpAfterPriceSets(); | |
1979 | } | |
1980 | ||
00be9182 | 1981 | public function cleanUpAfterPriceSets() { |
1cf3c2b1 | 1982 | $this->quickCleanUpFinancialEntities(); |
66d3f9be | 1983 | $this->contactDelete($this->_ids['contact']); |
66d3f9be EM |
1984 | } |
1985 | ||
1986 | ||
1987 | /** | |
1e52837d EM |
1988 | * Create price set with contribution test for test setup. |
1989 | * | |
100fef9d | 1990 | * This could be merged with 4.5 function setup in api_v3_ContributionPageTest::setUpContributionPage |
28de42d1 | 1991 | * on parent class at some point (fn is not in 4.4). |
1e52837d | 1992 | * |
66d3f9be EM |
1993 | * @param $entity |
1994 | * @param array $params | |
1995 | */ | |
00be9182 | 1996 | public function createPriceSetWithPage($entity, $params = array()) { |
66d3f9be EM |
1997 | $membershipTypeID = $this->membershipTypeCreate(); |
1998 | $contributionPageResult = $this->callAPISuccess('contribution_page', 'create', array( | |
1999 | 'title' => "Test Contribution Page", | |
2000 | 'financial_type_id' => 1, | |
2001 | 'currency' => 'NZD', | |
2002 | 'goal_amount' => 50, | |
2003 | 'is_pay_later' => 1, | |
2004 | 'is_monetary' => TRUE, | |
2005 | 'is_email_receipt' => FALSE, | |
2006 | )); | |
2007 | $priceSet = $this->callAPISuccess('price_set', 'create', array( | |
2008 | 'is_quick_config' => 0, | |
2009 | 'extends' => 'CiviMember', | |
2010 | 'financial_type_id' => 1, | |
21dfd5f5 | 2011 | 'title' => 'my Page', |
66d3f9be EM |
2012 | )); |
2013 | $priceSetID = $priceSet['id']; | |
2014 | ||
5896d037 | 2015 | CRM_Price_BAO_PriceSet::addTo('civicrm_contribution_page', $contributionPageResult['id'], $priceSetID); |
66d3f9be | 2016 | $priceField = $this->callAPISuccess('price_field', 'create', array( |
5896d037 | 2017 | 'price_set_id' => $priceSetID, |
66d3f9be EM |
2018 | 'label' => 'Goat Breed', |
2019 | 'html_type' => 'Radio', | |
2020 | )); | |
2021 | $priceFieldValue = $this->callAPISuccess('price_field_value', 'create', array( | |
5896d037 | 2022 | 'price_set_id' => $priceSetID, |
66d3f9be EM |
2023 | 'price_field_id' => $priceField['id'], |
2024 | 'label' => 'Long Haired Goat', | |
2025 | 'amount' => 20, | |
43dbd988 | 2026 | 'financial_type_id' => 'Donation', |
66d3f9be EM |
2027 | 'membership_type_id' => $membershipTypeID, |
2028 | 'membership_num_terms' => 1, | |
2029 | ) | |
2030 | ); | |
2031 | $this->_ids['price_field_value'] = array($priceFieldValue['id']); | |
2032 | $priceFieldValue = $this->callAPISuccess('price_field_value', 'create', array( | |
5896d037 | 2033 | 'price_set_id' => $priceSetID, |
66d3f9be EM |
2034 | 'price_field_id' => $priceField['id'], |
2035 | 'label' => 'Shoe-eating Goat', | |
2036 | 'amount' => 10, | |
43dbd988 | 2037 | 'financial_type_id' => 'Donation', |
66d3f9be EM |
2038 | 'membership_type_id' => $membershipTypeID, |
2039 | 'membership_num_terms' => 2, | |
2040 | ) | |
2041 | ); | |
2042 | $this->_ids['price_field_value'][] = $priceFieldValue['id']; | |
2043 | $this->_ids['price_set'] = $priceSetID; | |
2044 | $this->_ids['contribution_page'] = $contributionPageResult['id']; | |
2045 | $this->_ids['price_field'] = array($priceField['id']); | |
0efa8efe | 2046 | |
66d3f9be EM |
2047 | $this->_ids['membership_type'] = $membershipTypeID; |
2048 | } | |
2049 | ||
2050 | /** | |
eceb18cc | 2051 | * Set up a pending transaction with a specific price field id. |
1e52837d | 2052 | * |
100fef9d | 2053 | * @param int $priceFieldValueID |
66d3f9be | 2054 | */ |
5896d037 | 2055 | public function setUpPendingContribution($priceFieldValueID) { |
66d3f9be EM |
2056 | $contactID = $this->individualCreate(); |
2057 | $membership = $this->callAPISuccess('membership', 'create', array( | |
2058 | 'contact_id' => $contactID, | |
2059 | 'membership_type_id' => $this->_ids['membership_type'], | |
2060 | 'start_date' => 'yesterday - 1 year', | |
2061 | 'end_date' => 'yesterday', | |
4ff927bc | 2062 | 'join_date' => 'yesterday - 1 year', |
66d3f9be EM |
2063 | )); |
2064 | $contribution = $this->callAPISuccess('contribution', 'create', array( | |
2065 | 'domain_id' => 1, | |
2066 | 'contact_id' => $contactID, | |
2067 | 'receive_date' => date('Ymd'), | |
2068 | 'total_amount' => 100.00, | |
2069 | 'financial_type_id' => 1, | |
2070 | 'payment_instrument_id' => 'Credit Card', | |
2071 | 'non_deductible_amount' => 10.00, | |
2072 | 'trxn_id' => 'jdhfi88', | |
2073 | 'invoice_id' => 'djfhiewuyr', | |
2074 | 'source' => 'SSF', | |
2075 | 'contribution_status_id' => 2, | |
2076 | 'contribution_page_id' => $this->_ids['contribution_page'], | |
2077 | 'api.membership_payment.create' => array('membership_id' => $membership['id']), | |
2078 | )); | |
2079 | ||
2080 | $this->callAPISuccess('line_item', 'create', array( | |
2081 | 'entity_id' => $contribution['id'], | |
2082 | 'entity_table' => 'civicrm_contribution', | |
1274acef | 2083 | 'contribution_id' => $contribution['id'], |
66d3f9be EM |
2084 | 'price_field_id' => $this->_ids['price_field'][0], |
2085 | 'qty' => 1, | |
2086 | 'unit_price' => 20, | |
2087 | 'line_total' => 20, | |
2088 | 'financial_type_id' => 1, | |
2089 | 'price_field_value_id' => $priceFieldValueID, | |
2090 | )); | |
2091 | $this->_ids['contact'] = $contactID; | |
2092 | $this->_ids['contribution'] = $contribution['id']; | |
2093 | $this->_ids['membership'] = $membership['id']; | |
0efa8efe | 2094 | } |
2095 | ||
2f45e1c2 | 2096 | /** |
eceb18cc | 2097 | * Test sending a mail via the API. |
6a488035 | 2098 | */ |
00be9182 | 2099 | public function testSendMail() { |
5896d037 | 2100 | $mut = new CiviMailUtils($this, TRUE); |
6c6e6187 | 2101 | $contribution = $this->callAPISuccess('contribution', 'create', $this->_params); |
858d0bf8 | 2102 | $this->callAPISuccess('contribution', 'sendconfirmation', array( |
5896d037 TO |
2103 | 'id' => $contribution['id'], |
2104 | 'receipt_from_email' => 'api@civicrm.org', | |
6a488035 TO |
2105 | ) |
2106 | ); | |
6a488035 TO |
2107 | $mut->checkMailLog(array( |
2108 | '$ 100.00', | |
2109 | 'Contribution Information', | |
2110 | 'Please print this confirmation for your records', | |
2111 | ), array( | |
21dfd5f5 | 2112 | 'Event', |
6a488035 TO |
2113 | ) |
2114 | ); | |
2115 | $mut->stop(); | |
2116 | } | |
2117 | ||
2f45e1c2 | 2118 | /** |
eceb18cc | 2119 | * Test sending a mail via the API. |
6a488035 | 2120 | */ |
00be9182 | 2121 | public function testSendMailEvent() { |
5896d037 | 2122 | $mut = new CiviMailUtils($this, TRUE); |
6c6e6187 | 2123 | $contribution = $this->callAPISuccess('contribution', 'create', $this->_params); |
2f45e1c2 | 2124 | $event = $this->eventCreate(array( |
6a488035 TO |
2125 | 'is_email_confirm' => 1, |
2126 | 'confirm_from_email' => 'test@civicrm.org', | |
2127 | )); | |
2128 | $this->_eventID = $event['id']; | |
2129 | $participantParams = array( | |
2130 | 'contact_id' => $this->_individualId, | |
2131 | 'event_id' => $this->_eventID, | |
2132 | 'status_id' => 1, | |
2133 | 'role_id' => 1, | |
2134 | // to ensure it matches later on | |
2135 | 'register_date' => '2007-07-21 00:00:00', | |
2136 | 'source' => 'Online Event Registration: API Testing', | |
4ab7d517 | 2137 | |
6a488035 | 2138 | ); |
2f45e1c2 | 2139 | $participant = $this->callAPISuccess('participant', 'create', $participantParams); |
2140 | $this->callAPISuccess('participant_payment', 'create', array( | |
6a488035 TO |
2141 | 'participant_id' => $participant['id'], |
2142 | 'contribution_id' => $contribution['id'], | |
2f45e1c2 | 2143 | )); |
66d3f9be | 2144 | $this->callAPISuccess('contribution', 'sendconfirmation', array( |
5896d037 TO |
2145 | 'id' => $contribution['id'], |
2146 | 'receipt_from_email' => 'api@civicrm.org', | |
6a488035 TO |
2147 | ) |
2148 | ); | |
2149 | ||
6a488035 TO |
2150 | $mut->checkMailLog(array( |
2151 | 'Annual CiviCRM meet', | |
2152 | 'Event', | |
2153 | 'To: "Mr. Anthony Anderson II" <anthony_anderson@civicrm.org>', | |
6c6e6187 | 2154 | ), array() |
6a488035 TO |
2155 | ); |
2156 | $mut->stop(); | |
2157 | } | |
2158 | ||
4302618d | 2159 | /** |
1e52837d EM |
2160 | * This function does a GET & compares the result against the $params. |
2161 | * | |
2162 | * Use as a double check on Creates. | |
2163 | * | |
2164 | * @param array $params | |
2165 | * @param int $id | |
67f947ac | 2166 | * @param bool $delete |
6c6e6187 | 2167 | */ |
1e52837d | 2168 | public function contributionGetnCheck($params, $id, $delete = TRUE) { |
6a488035 | 2169 | |
4ab7d517 | 2170 | $contribution = $this->callAPISuccess('Contribution', 'Get', array( |
6a488035 | 2171 | 'id' => $id, |
4ab7d517 | 2172 | |
5896d037 | 2173 | )); |
6a488035 TO |
2174 | |
2175 | if ($delete) { | |
4ab7d517 | 2176 | $this->callAPISuccess('contribution', 'delete', array('id' => $id)); |
6a488035 | 2177 | } |
2bfae985 | 2178 | $this->assertAPISuccess($contribution, 0); |
6a488035 TO |
2179 | $values = $contribution['values'][$contribution['id']]; |
2180 | $params['receive_date'] = date('Y-m-d H:i:s', strtotime($params['receive_date'])); | |
2181 | // this is not returned in id format | |
2182 | unset($params['payment_instrument_id']); | |
2183 | $params['contribution_source'] = $params['source']; | |
2184 | unset($params['source']); | |
2185 | foreach ($params as $key => $value) { | |
22f80e87 | 2186 | $this->assertEquals($value, $values[$key], $key . " value: $value doesn't match " . print_r($values, TRUE)); |
6a488035 TO |
2187 | } |
2188 | } | |
2189 | ||
0efa8efe | 2190 | /** |
1e52837d | 2191 | * Create a pending contribution & linked pending participant record (along with an event). |
0efa8efe | 2192 | */ |
5896d037 | 2193 | public function createPendingParticipantContribution() { |
6c6e6187 | 2194 | $event = $this->eventCreate(array('is_email_confirm' => 1, 'confirm_from_email' => 'test@civicrm.org')); |
0efa8efe | 2195 | $participantID = $this->participantCreate(array('event_id' => $event['id'], 'status_id' => 6)); |
5896d037 | 2196 | $this->_ids['participant'] = $participantID; |
0efa8efe | 2197 | $params = array_merge($this->_params, array('contribution_status_id' => 2, 'financial_type_id' => 'Event Fee')); |
6c6e6187 | 2198 | $contribution = $this->callAPISuccess('contribution', 'create', $params); |
5896d037 | 2199 | $this->callAPISuccess('participant_payment', 'create', array( |
92915c55 TO |
2200 | 'contribution_id' => $contribution['id'], |
2201 | 'participant_id' => $participantID, | |
2202 | )); | |
858d0bf8 | 2203 | $this->callAPISuccess('line_item', 'get', array( |
0efa8efe | 2204 | 'entity_id' => $contribution['id'], |
2205 | 'entity_table' => 'civicrm_contribution', | |
2206 | 'api.line_item.create' => array( | |
2207 | 'entity_id' => $participantID, | |
2208 | 'entity_table' => 'civicrm_participant', | |
2209 | ), | |
2210 | )); | |
2211 | return $contribution['id']; | |
2212 | } | |
2213 | ||
4cbe18b8 | 2214 | /** |
1e52837d EM |
2215 | * Get financial transaction amount. |
2216 | * | |
100fef9d | 2217 | * @param int $contId |
4cbe18b8 EM |
2218 | * |
2219 | * @return null|string | |
f4d89200 | 2220 | */ |
2da40d21 | 2221 | public function _getFinancialTrxnAmount($contId) { |
6c6e6187 | 2222 | $query = "SELECT |
6a488035 TO |
2223 | SUM( ft.total_amount ) AS total |
2224 | FROM civicrm_financial_trxn AS ft | |
2225 | LEFT JOIN civicrm_entity_financial_trxn AS ceft ON ft.id = ceft.financial_trxn_id | |
2226 | WHERE ceft.entity_table = 'civicrm_contribution' | |
2227 | AND ceft.entity_id = {$contId}"; | |
2228 | ||
6c6e6187 TO |
2229 | $result = CRM_Core_DAO::singleValueQuery($query); |
2230 | return $result; | |
2231 | } | |
6a488035 | 2232 | |
4cbe18b8 | 2233 | /** |
100fef9d | 2234 | * @param int $contId |
4cbe18b8 EM |
2235 | * |
2236 | * @return null|string | |
f4d89200 | 2237 | */ |
2da40d21 | 2238 | public function _getFinancialItemAmount($contId) { |
6c6e6187 TO |
2239 | $lineItem = key(CRM_Price_BAO_LineItem::getLineItems($contId, 'contribution')); |
2240 | $query = "SELECT | |
6a488035 TO |
2241 | SUM(amount) |
2242 | FROM civicrm_financial_item | |
2243 | WHERE entity_table = 'civicrm_line_item' | |
2244 | AND entity_id = {$lineItem}"; | |
6c6e6187 TO |
2245 | $result = CRM_Core_DAO::singleValueQuery($query); |
2246 | return $result; | |
2247 | } | |
6a488035 | 2248 | |
4cbe18b8 | 2249 | /** |
100fef9d | 2250 | * @param int $contId |
4cbe18b8 EM |
2251 | * @param $context |
2252 | */ | |
00be9182 | 2253 | public function _checkFinancialItem($contId, $context) { |
6c6e6187 TO |
2254 | if ($context != 'paylater') { |
2255 | $params = array( | |
5896d037 TO |
2256 | 'entity_id' => $contId, |
2257 | 'entity_table' => 'civicrm_contribution', | |
6c6e6187 TO |
2258 | ); |
2259 | $trxn = current(CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($params, TRUE)); | |
2260 | $entityParams = array( | |
6a488035 TO |
2261 | 'financial_trxn_id' => $trxn['financial_trxn_id'], |
2262 | 'entity_table' => 'civicrm_financial_item', | |
6c6e6187 TO |
2263 | ); |
2264 | $entityTrxn = current(CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($entityParams)); | |
2265 | $params = array( | |
6a488035 | 2266 | 'id' => $entityTrxn['entity_id'], |
6c6e6187 TO |
2267 | ); |
2268 | } | |
2269 | if ($context == 'paylater') { | |
2270 | $lineItems = CRM_Price_BAO_LineItem::getLineItems($contId, 'contribution'); | |
2271 | foreach ($lineItems as $key => $item) { | |
2272 | $params = array( | |
5896d037 TO |
2273 | 'entity_id' => $key, |
2274 | 'entity_table' => 'civicrm_line_item', | |
6c6e6187 TO |
2275 | ); |
2276 | $compareParams = array('status_id' => 1); | |
2277 | $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $params, $compareParams); | |
2278 | } | |
2279 | } | |
2280 | elseif ($context == 'refund') { | |
2281 | $compareParams = array( | |
5896d037 TO |
2282 | 'status_id' => 1, |
2283 | 'financial_account_id' => 1, | |
2284 | 'amount' => -100, | |
6c6e6187 TO |
2285 | ); |
2286 | } | |
2287 | elseif ($context == 'cancelPending') { | |
2288 | $compareParams = array( | |
5896d037 TO |
2289 | 'status_id' => 3, |
2290 | 'financial_account_id' => 1, | |
2291 | 'amount' => -100, | |
6c6e6187 TO |
2292 | ); |
2293 | } | |
2294 | elseif ($context == 'changeFinancial') { | |
2295 | $lineKey = key(CRM_Price_BAO_LineItem::getLineItems($contId, 'contribution')); | |
2296 | $params = array( | |
5896d037 TO |
2297 | 'entity_id' => $lineKey, |
2298 | 'amount' => -100, | |
6c6e6187 TO |
2299 | ); |
2300 | $compareParams = array( | |
5896d037 | 2301 | 'financial_account_id' => 1, |
6c6e6187 TO |
2302 | ); |
2303 | $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $params, $compareParams); | |
2304 | $params = array( | |
5896d037 TO |
2305 | 'financial_account_id' => 3, |
2306 | 'entity_id' => $lineKey, | |
6c6e6187 TO |
2307 | ); |
2308 | $compareParams = array( | |
5896d037 | 2309 | 'amount' => 100, |
6c6e6187 TO |
2310 | ); |
2311 | } | |
2312 | if ($context != 'paylater') { | |
2313 | $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $params, $compareParams); | |
2314 | } | |
2315 | } | |
6a488035 | 2316 | |
4cbe18b8 | 2317 | /** |
4ff927bc | 2318 | * @param array $contribution |
2319 | * @param string $context | |
100fef9d | 2320 | * @param int $instrumentId |
4cbe18b8 | 2321 | */ |
797d4c52 | 2322 | public function _checkFinancialTrxn($contribution, $context, $instrumentId = NULL, $extraParams = array()) { |
6c6e6187 | 2323 | $trxnParams = array( |
5896d037 TO |
2324 | 'entity_id' => $contribution['id'], |
2325 | 'entity_table' => 'civicrm_contribution', | |
6c6e6187 TO |
2326 | ); |
2327 | $trxn = current(CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($trxnParams, TRUE)); | |
2328 | $params = array( | |
5896d037 | 2329 | 'id' => $trxn['financial_trxn_id'], |
6c6e6187 TO |
2330 | ); |
2331 | if ($context == 'payLater') { | |
2332 | $relationTypeId = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Accounts Receivable Account is' ")); | |
2333 | $compareParams = array( | |
5896d037 TO |
2334 | 'status_id' => 1, |
2335 | 'from_financial_account_id' => CRM_Contribute_PseudoConstant::financialAccountType($contribution['financial_type_id'], $relationTypeId), | |
6c6e6187 TO |
2336 | ); |
2337 | } | |
2338 | elseif ($context == 'refund') { | |
2339 | $compareParams = array( | |
5896d037 TO |
2340 | 'to_financial_account_id' => 6, |
2341 | 'total_amount' => -100, | |
2342 | 'status_id' => 7, | |
b7990bb6 | 2343 | 'trxn_date' => '2015-01-01 09:00:00', |
797d4c52 | 2344 | 'trxn_id' => 'the refund', |
6c6e6187 TO |
2345 | ); |
2346 | } | |
2347 | elseif ($context == 'cancelPending') { | |
2348 | $compareParams = array( | |
ed4d0aea | 2349 | 'to_financial_account_id' => 7, |
5896d037 TO |
2350 | 'total_amount' => -100, |
2351 | 'status_id' => 3, | |
6c6e6187 TO |
2352 | ); |
2353 | } | |
2354 | elseif ($context == 'changeFinancial' || $context == 'paymentInstrument') { | |
2355 | $entityParams = array( | |
5896d037 TO |
2356 | 'entity_id' => $contribution['id'], |
2357 | 'entity_table' => 'civicrm_contribution', | |
2358 | 'amount' => -100, | |
6c6e6187 TO |
2359 | ); |
2360 | $trxn = current(CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($entityParams)); | |
2361 | $trxnParams1 = array( | |
6a488035 | 2362 | 'id' => $trxn['financial_trxn_id'], |
6c6e6187 TO |
2363 | ); |
2364 | $compareParams = array( | |
5896d037 TO |
2365 | 'total_amount' => -100, |
2366 | 'status_id' => 1, | |
6c6e6187 TO |
2367 | ); |
2368 | if ($context == 'paymentInstrument') { | |
2369 | $compareParams += array( | |
5896d037 TO |
2370 | 'to_financial_account_id' => CRM_Financial_BAO_FinancialTypeAccount::getInstrumentFinancialAccount(4), |
2371 | 'payment_instrument_id' => 4, | |
6c6e6187 TO |
2372 | ); |
2373 | } | |
2374 | else { | |
2375 | $compareParams['to_financial_account_id'] = 12; | |
2376 | } | |
797d4c52 | 2377 | $this->assertDBCompareValues('CRM_Financial_DAO_FinancialTrxn', $trxnParams1, array_merge($compareParams, $extraParams)); |
6c6e6187 TO |
2378 | $compareParams['total_amount'] = 100; |
2379 | if ($context == 'paymentInstrument') { | |
2380 | $compareParams['to_financial_account_id'] = CRM_Financial_BAO_FinancialTypeAccount::getInstrumentFinancialAccount($instrumentId); | |
2381 | $compareParams['payment_instrument_id'] = $instrumentId; | |
2382 | } | |
2383 | else { | |
2384 | $compareParams['to_financial_account_id'] = 12; | |
2385 | } | |
2386 | } | |
2387 | ||
797d4c52 | 2388 | $this->assertDBCompareValues('CRM_Financial_DAO_FinancialTrxn', $params, array_merge($compareParams, $extraParams)); |
6c6e6187 | 2389 | } |
6a488035 | 2390 | |
4cbe18b8 EM |
2391 | /** |
2392 | * @return mixed | |
2393 | */ | |
5896d037 | 2394 | public function _addPaymentInstrument() { |
6c6e6187 TO |
2395 | $gId = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionGroup', 'payment_instrument', 'id', 'name'); |
2396 | $optionParams = array( | |
5896d037 TO |
2397 | 'option_group_id' => $gId, |
2398 | 'label' => 'Test Card', | |
2399 | 'name' => 'Test Card', | |
2400 | 'value' => '6', | |
2401 | 'weight' => '6', | |
2402 | 'is_active' => 1, | |
6c6e6187 TO |
2403 | ); |
2404 | $optionValue = $this->callAPISuccess('option_value', 'create', $optionParams); | |
2405 | $relationTypeId = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Asset Account is' ")); | |
2406 | $financialParams = array( | |
5896d037 TO |
2407 | 'entity_table' => 'civicrm_option_value', |
2408 | 'entity_id' => $optionValue['id'], | |
2409 | 'account_relationship' => $relationTypeId, | |
2410 | 'financial_account_id' => 7, | |
6c6e6187 TO |
2411 | ); |
2412 | CRM_Financial_BAO_FinancialTypeAccount::add($financialParams, CRM_Core_DAO::$_nullArray); | |
2413 | $this->assertNotEmpty($optionValue['values'][$optionValue['id']]['value']); | |
2414 | return $optionValue['values'][$optionValue['id']]['value']; | |
2415 | } | |
6a488035 | 2416 | |
4cbe18b8 | 2417 | /** |
c490a46a | 2418 | * @param array $params |
4cbe18b8 EM |
2419 | * @param $context |
2420 | */ | |
6c6e6187 TO |
2421 | public function _checkFinancialRecords($params, $context) { |
2422 | $entityParams = array( | |
5896d037 TO |
2423 | 'entity_id' => $params['id'], |
2424 | 'entity_table' => 'civicrm_contribution', | |
6c6e6187 | 2425 | ); |
e0e3c51b EM |
2426 | $contribution = $this->callAPISuccess('contribution', 'getsingle', array('id' => $params['id'])); |
2427 | $this->assertEquals($contribution['total_amount'] - $contribution['fee_amount'], $contribution['net_amount']); | |
6c6e6187 TO |
2428 | if ($context == 'pending') { |
2429 | $trxn = CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($entityParams); | |
2430 | $this->assertNull($trxn, 'No Trxn to be created until IPN callback'); | |
2431 | return; | |
2432 | } | |
2433 | $trxn = current(CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($entityParams)); | |
2434 | $trxnParams = array( | |
5896d037 | 2435 | 'id' => $trxn['financial_trxn_id'], |
6c6e6187 TO |
2436 | ); |
2437 | if ($context != 'online' && $context != 'payLater') { | |
2438 | $compareParams = array( | |
5896d037 TO |
2439 | 'to_financial_account_id' => 6, |
2440 | 'total_amount' => 100, | |
2441 | 'status_id' => 1, | |
6c6e6187 TO |
2442 | ); |
2443 | } | |
2444 | if ($context == 'feeAmount') { | |
2445 | $compareParams['fee_amount'] = 50; | |
2446 | } | |
2447 | elseif ($context == 'online') { | |
2448 | $compareParams = array( | |
5896d037 TO |
2449 | 'to_financial_account_id' => 12, |
2450 | 'total_amount' => 100, | |
2451 | 'status_id' => 1, | |
6c6e6187 TO |
2452 | ); |
2453 | } | |
2454 | elseif ($context == 'payLater') { | |
2455 | $compareParams = array( | |
5896d037 TO |
2456 | 'to_financial_account_id' => 7, |
2457 | 'total_amount' => 100, | |
2458 | 'status_id' => 2, | |
6c6e6187 TO |
2459 | ); |
2460 | } | |
2461 | $this->assertDBCompareValues('CRM_Financial_DAO_FinancialTrxn', $trxnParams, $compareParams); | |
2462 | $entityParams = array( | |
5896d037 TO |
2463 | 'financial_trxn_id' => $trxn['financial_trxn_id'], |
2464 | 'entity_table' => 'civicrm_financial_item', | |
6c6e6187 TO |
2465 | ); |
2466 | $entityTrxn = current(CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($entityParams)); | |
2467 | $fitemParams = array( | |
5896d037 | 2468 | 'id' => $entityTrxn['entity_id'], |
6c6e6187 TO |
2469 | ); |
2470 | $compareParams = array( | |
5896d037 TO |
2471 | 'amount' => 100, |
2472 | 'status_id' => 1, | |
2473 | 'financial_account_id' => 1, | |
6c6e6187 TO |
2474 | ); |
2475 | if ($context == 'payLater') { | |
2476 | $compareParams = array( | |
5896d037 TO |
2477 | 'amount' => 100, |
2478 | 'status_id' => 3, | |
2479 | 'financial_account_id' => 1, | |
6c6e6187 TO |
2480 | ); |
2481 | } | |
2482 | $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $fitemParams, $compareParams); | |
2483 | if ($context == 'feeAmount') { | |
2484 | $maxParams = array( | |
5896d037 TO |
2485 | 'entity_id' => $params['id'], |
2486 | 'entity_table' => 'civicrm_contribution', | |
6c6e6187 TO |
2487 | ); |
2488 | $maxTrxn = current(CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($maxParams, TRUE)); | |
2489 | $trxnParams = array( | |
5896d037 | 2490 | 'id' => $maxTrxn['financial_trxn_id'], |
6c6e6187 TO |
2491 | ); |
2492 | $compareParams = array( | |
5896d037 TO |
2493 | 'to_financial_account_id' => 5, |
2494 | 'from_financial_account_id' => 6, | |
2495 | 'total_amount' => 50, | |
2496 | 'status_id' => 1, | |
6c6e6187 TO |
2497 | ); |
2498 | $trxnId = CRM_Core_BAO_FinancialTrxn::getFinancialTrxnId($params['id'], 'DESC'); | |
2499 | $this->assertDBCompareValues('CRM_Financial_DAO_FinancialTrxn', $trxnParams, $compareParams); | |
2500 | $fitemParams = array( | |
5896d037 TO |
2501 | 'entity_id' => $trxnId['financialTrxnId'], |
2502 | 'entity_table' => 'civicrm_financial_trxn', | |
6c6e6187 TO |
2503 | ); |
2504 | $compareParams = array( | |
5896d037 TO |
2505 | 'amount' => 50, |
2506 | 'status_id' => 1, | |
2507 | 'financial_account_id' => 5, | |
6c6e6187 TO |
2508 | ); |
2509 | $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $fitemParams, $compareParams); | |
2510 | } | |
2511 | } | |
96025800 | 2512 | |
3c49d90c | 2513 | /** |
2514 | * Set up the basic recurring contribution for tests. | |
2515 | * | |
2516 | * @param array $generalParams | |
2517 | * Parameters that can be merged into the recurring AND the contribution. | |
7f4ef731 | 2518 | * |
2519 | * @param array $recurParams | |
2520 | * Parameters to merge into the recur only. | |
3c49d90c | 2521 | * |
2522 | * @return array|int | |
2523 | */ | |
7f4ef731 | 2524 | protected function setUpRecurringContribution($generalParams = array(), $recurParams = array()) { |
3c49d90c | 2525 | $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array_merge(array( |
2526 | 'contact_id' => $this->_individualId, | |
2527 | 'installments' => '12', | |
2528 | 'frequency_interval' => '1', | |
2529 | 'amount' => '100', | |
2530 | 'contribution_status_id' => 1, | |
2531 | 'start_date' => '2012-01-01 00:00:00', | |
2532 | 'currency' => 'USD', | |
2533 | 'frequency_unit' => 'month', | |
2534 | 'payment_processor_id' => $this->paymentProcessorID, | |
7f4ef731 | 2535 | ), $generalParams, $recurParams)); |
3c49d90c | 2536 | $originalContribution = $this->callAPISuccess('contribution', 'create', array_merge( |
2537 | $this->_params, | |
2538 | array( | |
2539 | 'contribution_recur_id' => $contributionRecur['id'], | |
2540 | ), $generalParams) | |
2541 | ); | |
2542 | return $originalContribution; | |
2543 | } | |
2544 | ||
6a488035 | 2545 | } |