add unit test
[civicrm-core.git] / tests / phpunit / api / v3 / ContributionTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2016 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
26 */
27
28 /**
29 * Test APIv3 civicrm_contribute_* functions
30 *
31 * @package CiviCRM_APIv3
32 * @subpackage API_Contribution
33 * @group headless
34 */
35 class api_v3_ContributionTest extends CiviUnitTestCase {
36
37 /**
38 * Assume empty database with just civicrm_data.
39 */
40 protected $_individualId;
41 protected $_contribution;
42 protected $_financialTypeId = 1;
43 protected $_apiversion;
44 protected $_entity = 'Contribution';
45 public $debug = 0;
46 protected $_params;
47 protected $_ids = array();
48 protected $_pageParams = array();
49 /**
50 * Payment processor ID (dummy processor).
51 *
52 * @var int
53 */
54 protected $paymentProcessorID;
55
56 /**
57 * Parameters to create payment processor.
58 *
59 * @var array
60 */
61 protected $_processorParams = array();
62
63 /**
64 * ID of created event.
65 *
66 * @var int
67 */
68 protected $_eventID;
69
70 /**
71 * @var CiviMailUtils
72 */
73 protected $mut;
74
75 /**
76 * Setup function.
77 */
78 public function setUp() {
79 parent::setUp();
80
81 $this->_apiversion = 3;
82 $this->_individualId = $this->individualCreate();
83 $this->_params = array(
84 'contact_id' => $this->_individualId,
85 'receive_date' => '20120511',
86 'total_amount' => 100.00,
87 'financial_type_id' => $this->_financialTypeId,
88 'non_deductible_amount' => 10.00,
89 'fee_amount' => 5.00,
90 'net_amount' => 95.00,
91 'source' => 'SSF',
92 'contribution_status_id' => 1,
93 );
94 $this->_processorParams = array(
95 'domain_id' => 1,
96 'name' => 'Dummy',
97 'payment_processor_type_id' => CRM_Core_PseudoConstant::getKey('CRM_Financial_BAO_PaymentProcessor', 'payment_processor_type_id', 'Dummy'),
98 'financial_account_id' => 12,
99 'is_active' => 1,
100 'user_name' => '',
101 'url_site' => 'http://dummy.com',
102 'url_recur' => 'http://dummy.com',
103 'billing_mode' => 1,
104 );
105 $this->paymentProcessorID = $this->processorCreate();
106 $this->_pageParams = array(
107 'title' => 'Test Contribution Page',
108 'financial_type_id' => 1,
109 'currency' => 'USD',
110 'financial_account_id' => 1,
111 'payment_processor' => $this->paymentProcessorID,
112 'is_active' => 1,
113 'is_allow_other_amount' => 1,
114 'min_amount' => 10,
115 'max_amount' => 1000,
116 );
117 }
118
119 /**
120 * Clean up after each test.
121 */
122 public function tearDown() {
123 $this->quickCleanUpFinancialEntities();
124 $this->quickCleanup(array('civicrm_uf_match'));
125 }
126
127 /**
128 * Test Get.
129 */
130 public function testGetContribution() {
131 $p = array(
132 'contact_id' => $this->_individualId,
133 'receive_date' => '2010-01-20',
134 'total_amount' => 100.00,
135 'financial_type_id' => $this->_financialTypeId,
136 'non_deductible_amount' => 10.00,
137 'fee_amount' => 5.00,
138 'net_amount' => 95.00,
139 'trxn_id' => 23456,
140 'invoice_id' => 78910,
141 'source' => 'SSF',
142 'contribution_status_id' => 1,
143 );
144 $this->_contribution = $this->callAPISuccess('contribution', 'create', $p);
145
146 $params = array(
147 'contribution_id' => $this->_contribution['id'],
148 );
149
150 $contribution = $this->callAPIAndDocument('contribution', 'get', $params, __FUNCTION__, __FILE__);
151 $financialParams['id'] = $this->_financialTypeId;
152 $default = NULL;
153 CRM_Financial_BAO_FinancialType::retrieve($financialParams, $default);
154
155 $this->assertEquals(1, $contribution['count']);
156 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId);
157 // Note there was an assertion converting financial_type_id to 'Donation' which wasn't working.
158 // Passing back a string rather than an id seems like an error/cruft.
159 // If it is to be introduced we should discuss.
160 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'], 1);
161 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
162 $this->assertEquals($contribution['values'][$contribution['id']]['non_deductible_amount'], 10.00);
163 $this->assertEquals($contribution['values'][$contribution['id']]['fee_amount'], 5.00);
164 $this->assertEquals($contribution['values'][$contribution['id']]['net_amount'], 95.00);
165 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 23456);
166 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 78910);
167 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_source'], 'SSF');
168 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status'], 'Completed');
169 // Create a second contribution - we are testing that 'id' gets the right contribution id (not the contact id).
170 $p['trxn_id'] = '3847';
171 $p['invoice_id'] = '3847';
172
173 $contribution2 = $this->callAPISuccess('contribution', 'create', $p);
174
175 // Now we have 2 - test getcount.
176 $contribution = $this->callAPISuccess('contribution', 'getcount', array());
177 $this->assertEquals(2, $contribution);
178 // Test id only format.
179 $contribution = $this->callAPISuccess('contribution', 'get', array(
180 'id' => $this->_contribution['id'],
181 'format.only_id' => 1,
182 ));
183 $this->assertEquals($this->_contribution['id'], $contribution, print_r($contribution, TRUE));
184 // Test id only format.
185 $contribution = $this->callAPISuccess('contribution', 'get', array(
186 'id' => $contribution2['id'],
187 'format.only_id' => 1,
188 ));
189 $this->assertEquals($contribution2['id'], $contribution);
190 // Test id as field.
191 $contribution = $this->callAPISuccess('contribution', 'get', array(
192 'id' => $this->_contribution['id'],
193 ));
194 $this->assertEquals(1, $contribution['count']);
195
196 // Test get by contact id works.
197 $contribution = $this->callAPISuccess('contribution', 'get', array('contact_id' => $this->_individualId));
198
199 $this->assertEquals(2, $contribution['count']);
200 $this->callAPISuccess('Contribution', 'Delete', array(
201 'id' => $this->_contribution['id'],
202 ));
203 $this->callAPISuccess('Contribution', 'Delete', array(
204 'id' => $contribution2['id'],
205 ));
206 }
207
208 /**
209 * Test that test contributions can be retrieved.
210 */
211 public function testGetTestContribution() {
212 $this->callAPISuccess('Contribution', 'create', array_merge($this->_params, array('is_test' => 1)));
213 $this->callAPISuccessGetSingle('Contribution', array('is_test' => 1));
214 }
215
216 /**
217 * We need to ensure previous tested behaviour still works as part of the api contract.
218 */
219 public function testGetContributionLegacyBehaviour() {
220 $p = array(
221 'contact_id' => $this->_individualId,
222 'receive_date' => '2010-01-20',
223 'total_amount' => 100.00,
224 'contribution_type_id' => $this->_financialTypeId,
225 'non_deductible_amount' => 10.00,
226 'fee_amount' => 5.00,
227 'net_amount' => 95.00,
228 'trxn_id' => 23456,
229 'invoice_id' => 78910,
230 'source' => 'SSF',
231 'contribution_status_id' => 1,
232 );
233 $this->_contribution = $this->callAPISuccess('Contribution', 'create', $p);
234
235 $params = array(
236 'contribution_id' => $this->_contribution['id'],
237 );
238 $contribution = $this->callAPIAndDocument('contribution', 'get', $params, __FUNCTION__, __FILE__);
239 $financialParams['id'] = $this->_financialTypeId;
240 $default = NULL;
241 CRM_Financial_BAO_FinancialType::retrieve($financialParams, $default);
242
243 $this->assertEquals(1, $contribution['count']);
244 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId);
245 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'], $this->_financialTypeId);
246 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_type_id'], $this->_financialTypeId);
247 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
248 $this->assertEquals($contribution['values'][$contribution['id']]['non_deductible_amount'], 10.00);
249 $this->assertEquals($contribution['values'][$contribution['id']]['fee_amount'], 5.00);
250 $this->assertEquals($contribution['values'][$contribution['id']]['net_amount'], 95.00);
251 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 23456);
252 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 78910);
253 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_source'], 'SSF');
254 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status'], 'Completed');
255
256 // Create a second contribution - we are testing that 'id' gets the right contribution id (not the contact id).
257 $p['trxn_id'] = '3847';
258 $p['invoice_id'] = '3847';
259
260 $contribution2 = $this->callAPISuccess('contribution', 'create', $p);
261
262 // now we have 2 - test getcount
263 $contribution = $this->callAPISuccess('contribution', 'getcount', array());
264 $this->assertEquals(2, $contribution);
265 //test id only format
266 $contribution = $this->callAPISuccess('contribution', 'get', array(
267 'id' => $this->_contribution['id'],
268 'format.only_id' => 1,
269 ));
270 $this->assertEquals($this->_contribution['id'], $contribution, print_r($contribution, TRUE));
271 //test id only format
272 $contribution = $this->callAPISuccess('contribution', 'get', array(
273 'id' => $contribution2['id'],
274 'format.only_id' => 1,
275 ));
276 $this->assertEquals($contribution2['id'], $contribution);
277 $contribution = $this->callAPISuccess('contribution', 'get', array(
278 'id' => $this->_contribution['id'],
279 ));
280 //test id as field
281 $this->assertEquals(1, $contribution['count']);
282 // $this->assertEquals($this->_contribution['id'], $contribution['id'] ) ;
283 //test get by contact id works
284 $contribution = $this->callAPISuccess('contribution', 'get', array('contact_id' => $this->_individualId));
285
286 $this->assertEquals(2, $contribution['count']);
287 $this->callAPISuccess('Contribution', 'Delete', array(
288 'id' => $this->_contribution['id'],
289 ));
290 $this->callAPISuccess('Contribution', 'Delete', array(
291 'id' => $contribution2['id'],
292 ));
293 }
294
295 /**
296 * Create an contribution_id=FALSE and financial_type_id=Donation.
297 */
298 public function testCreateEmptyContributionIDUseDonation() {
299 $params = array(
300 'contribution_id' => FALSE,
301 'contact_id' => 1,
302 'total_amount' => 1,
303 'check_permissions' => FALSE,
304 'financial_type_id' => 'Donation',
305 );
306 $this->callAPISuccess('contribution', 'create', $params);
307 }
308
309 /**
310 * Check with complete array + custom field.
311 *
312 * Note that the test is written on purpose without any
313 * variables specific to participant so it can be replicated into other entities
314 * and / or moved to the automated test suite
315 */
316 public function testCreateWithCustom() {
317 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, __FILE__);
318
319 $params = $this->_params;
320 $params['custom_' . $ids['custom_field_id']] = "custom string";
321
322 $result = $this->callAPIAndDocument($this->_entity, 'create', $params, __FUNCTION__, __FILE__);
323 $this->assertEquals($result['id'], $result['values'][$result['id']]['id']);
324 $check = $this->callAPISuccess($this->_entity, 'get', array(
325 'return.custom_' . $ids['custom_field_id'] => 1,
326 'id' => $result['id'],
327 ));
328 $this->customFieldDelete($ids['custom_field_id']);
329 $this->customGroupDelete($ids['custom_group_id']);
330 $this->assertEquals("custom string", $check['values'][$check['id']]['custom_' . $ids['custom_field_id']]);
331 }
332
333 /**
334 * Check with complete array + custom field.
335 *
336 * Note that the test is written on purpose without any
337 * variables specific to participant so it can be replicated into other entities
338 * and / or moved to the automated test suite
339 */
340 public function testCreateGetFieldsWithCustom() {
341 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, __FILE__);
342 $idsContact = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, 'ContactTest.php');
343 $result = $this->callAPISuccess('Contribution', 'getfields', array());
344 $this->assertArrayHasKey('custom_' . $ids['custom_field_id'], $result['values']);
345 $this->assertArrayNotHasKey('custom_' . $idsContact['custom_field_id'], $result['values']);
346 $this->customFieldDelete($ids['custom_field_id']);
347 $this->customGroupDelete($ids['custom_group_id']);
348 $this->customFieldDelete($idsContact['custom_field_id']);
349 $this->customGroupDelete($idsContact['custom_group_id']);
350 }
351
352 public function testCreateContributionNoLineItems() {
353
354 $params = array(
355 'contact_id' => $this->_individualId,
356 'receive_date' => '20120511',
357 'total_amount' => 100.00,
358 'financial_type_id' => $this->_financialTypeId,
359 'payment_instrument_id' => 1,
360 'non_deductible_amount' => 10.00,
361 'fee_amount' => 50.00,
362 'net_amount' => 90.00,
363 'trxn_id' => 12345,
364 'invoice_id' => 67890,
365 'source' => 'SSF',
366 'contribution_status_id' => 1,
367 'skipLineItem' => 1,
368 );
369
370 $contribution = $this->callAPISuccess('contribution', 'create', $params);
371 $lineItems = $this->callAPISuccess('line_item', 'get', array(
372 'entity_id' => $contribution['id'],
373 'entity_table' => 'civicrm_contribution',
374 'sequential' => 1,
375 ));
376 $this->assertEquals(0, $lineItems['count']);
377 }
378
379 /**
380 * Test checks that passing in line items suppresses the create mechanism.
381 */
382 public function testCreateContributionChainedLineItems() {
383 $params = array(
384 'contact_id' => $this->_individualId,
385 'receive_date' => '20120511',
386 'total_amount' => 100.00,
387 'financial_type_id' => $this->_financialTypeId,
388 'payment_instrument_id' => 1,
389 'non_deductible_amount' => 10.00,
390 'fee_amount' => 50.00,
391 'net_amount' => 90.00,
392 'trxn_id' => 12345,
393 'invoice_id' => 67890,
394 'source' => 'SSF',
395 'contribution_status_id' => 1,
396 'skipLineItem' => 1,
397 'api.line_item.create' => array(
398 array(
399 'price_field_id' => 1,
400 'qty' => 2,
401 'line_total' => '20',
402 'unit_price' => '10',
403 ),
404 array(
405 'price_field_id' => 1,
406 'qty' => 1,
407 'line_total' => '80',
408 'unit_price' => '80',
409 ),
410 ),
411 );
412
413 $description = "Create Contribution with Nested Line Items.";
414 $subfile = "CreateWithNestedLineItems";
415 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__, __FILE__, $description, $subfile);
416
417 $lineItems = $this->callAPISuccess('line_item', 'get', array(
418 'entity_id' => $contribution['id'],
419 'contribution_id' => $contribution['id'],
420 'entity_table' => 'civicrm_contribution',
421 'sequential' => 1,
422 ));
423 $this->assertEquals(2, $lineItems['count']);
424 }
425
426 public function testCreateContributionOffline() {
427 $params = array(
428 'contact_id' => $this->_individualId,
429 'receive_date' => '20120511',
430 'total_amount' => 100.00,
431 'financial_type_id' => 1,
432 'trxn_id' => 12345,
433 'invoice_id' => 67890,
434 'source' => 'SSF',
435 'contribution_status_id' => 1,
436 );
437
438 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__, __FILE__);
439 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId);
440 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
441 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'], 1);
442 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 12345);
443 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 67890);
444 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF');
445 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status_id'], 1);
446 $lineItems = $this->callAPISuccess('line_item', 'get', array(
447 'entity_id' => $contribution['id'],
448 'contribution_id' => $contribution['id'],
449 'entity_table' => 'civicrm_contribution',
450 'sequential' => 1,
451 ));
452 $this->assertEquals(1, $lineItems['count']);
453 $this->assertEquals($contribution['id'], $lineItems['values'][0]['entity_id']);
454 $this->assertEquals($contribution['id'], $lineItems['values'][0]['contribution_id']);
455 $this->_checkFinancialRecords($contribution, 'offline');
456 $this->contributionGetnCheck($params, $contribution['id']);
457 }
458
459 /**
460 * Test create with valid payment instrument.
461 */
462 public function testCreateContributionWithPaymentInstrument() {
463 $params = $this->_params + array('payment_instrument' => 'EFT');
464 $contribution = $this->callAPISuccess('contribution', 'create', $params);
465 $contribution = $this->callAPISuccess('contribution', 'get', array(
466 'sequential' => 1,
467 'id' => $contribution['id'],
468 ));
469 $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]);
470 $this->assertEquals('EFT', $contribution['values'][0]['payment_instrument']);
471
472 $this->callAPISuccess('contribution', 'create', array(
473 'id' => $contribution['id'],
474 'payment_instrument' => 'Credit Card',
475 ));
476 $contribution = $this->callAPISuccess('contribution', 'get', array(
477 'sequential' => 1,
478 'id' => $contribution['id'],
479 ));
480 $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]);
481 $this->assertEquals('Credit Card', $contribution['values'][0]['payment_instrument']);
482 }
483
484 public function testGetContributionByPaymentInstrument() {
485 $params = $this->_params + array('payment_instrument' => 'EFT');
486 $params2 = $this->_params + array('payment_instrument' => 'Cash');
487 $this->callAPISuccess('contribution', 'create', $params);
488 $this->callAPISuccess('contribution', 'create', $params2);
489 $contribution = $this->callAPISuccess('contribution', 'get', array(
490 'sequential' => 1,
491 'contribution_payment_instrument' => 'Cash',
492 ));
493 $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]);
494 $this->assertEquals('Cash', $contribution['values'][0]['payment_instrument']);
495 $this->assertEquals(1, $contribution['count']);
496 $contribution = $this->callAPISuccess('contribution', 'get', array('sequential' => 1, 'payment_instrument' => 'Cash'));
497 $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]);
498 $this->assertEquals('Cash', $contribution['values'][0]['payment_instrument']);
499 $this->assertEquals(1, $contribution['count']);
500 $contribution = $this->callAPISuccess('contribution', 'get', array(
501 'sequential' => 1,
502 'payment_instrument_id' => 5,
503 ));
504 $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]);
505 $this->assertEquals('EFT', $contribution['values'][0]['payment_instrument']);
506 $this->assertEquals(1, $contribution['count']);
507 $contribution = $this->callAPISuccess('contribution', 'get', array(
508 'sequential' => 1,
509 'payment_instrument' => 'EFT',
510 ));
511 $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]);
512 $this->assertEquals('EFT', $contribution['values'][0]['payment_instrument']);
513 $this->assertEquals(1, $contribution['count']);
514 $contribution = $this->callAPISuccess('contribution', 'create', array(
515 'id' => $contribution['id'],
516 'payment_instrument' => 'Credit Card',
517 ));
518 $contribution = $this->callAPISuccess('contribution', 'get', array('sequential' => 1, 'id' => $contribution['id']));
519 $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]);
520 $this->assertEquals('Credit Card', $contribution['values'][0]['payment_instrument']);
521 $this->assertEquals(1, $contribution['count']);
522 }
523
524 /**
525 * CRM-16227 introduces invoice_id as a parameter.
526 */
527 public function testGetContributionByInvoice() {
528 $this->callAPISuccess('Contribution', 'create', array_merge($this->_params, array('invoice_id' => 'curly')));
529 $this->callAPISuccess('Contribution', 'create', array_merge($this->_params), array('invoice_id' => 'churlish'));
530 $this->callAPISuccessGetCount('Contribution', array(), 2);
531 $this->callAPISuccessGetSingle('Contribution', array('invoice_id' => 'curly'));
532 // The following don't work. They are the format we are trying to introduce but although the form uses this format
533 // CRM_Contact_BAO_Query::convertFormValues puts them into the other format & the where only supports that.
534 // ideally the where clause would support this format (as it does on contact_BAO_Query) and those lines would
535 // come out of convertFormValues
536 // $this->callAPISuccessGetSingle('Contribution', array('invoice_id' => array('LIKE' => '%ish%')));
537 // $this->callAPISuccessGetSingle('Contribution', array('invoice_id' => array('NOT IN' => array('curly'))));
538 // $this->callAPISuccessGetCount('Contribution', array('invoice_id' => array('LIKE' => '%ly%')), 2);
539 // $this->callAPISuccessGetCount('Contribution', array('invoice_id' => array('IN' => array('curly', 'churlish'))),
540 // 2);
541 }
542
543 /**
544 * Test retrieval by total_amount works.
545 *
546 * @throws Exception
547 */
548 public function testGetContributionByTotalAmount() {
549 $this->callAPISuccess('Contribution', 'create', array_merge($this->_params, array('total_amount' => '5')));
550 $this->callAPISuccess('Contribution', 'create', array_merge($this->_params, array('total_amount' => '10')));
551 $this->callAPISuccessGetCount('Contribution', array('total_amount' => 10), 1);
552 $this->callAPISuccessGetCount('Contribution', array('total_amount' => array('>' => 6)), 1);
553 $this->callAPISuccessGetCount('Contribution', array('total_amount' => array('>' => 0)), 2);
554 $this->callAPISuccessGetCount('Contribution', array('total_amount' => array('>' => -5)), 2);
555 $this->callAPISuccessGetCount('Contribution', array('total_amount' => array('<' => 0)), 0);
556 $this->callAPISuccessGetCount('Contribution', array(), 2);
557 }
558
559 /**
560 * Create test with unique field name on source.
561 */
562 public function testCreateContributionSource() {
563
564 $params = array(
565 'contact_id' => $this->_individualId,
566 'receive_date' => date('Ymd'),
567 'total_amount' => 100.00,
568 'financial_type_id' => $this->_financialTypeId,
569 'payment_instrument_id' => 1,
570 'non_deductible_amount' => 10.00,
571 'fee_amount' => 50.00,
572 'net_amount' => 90.00,
573 'trxn_id' => 12345,
574 'invoice_id' => 67890,
575 'contribution_source' => 'SSF',
576 'contribution_status_id' => 1,
577 );
578
579 $contribution = $this->callAPISuccess('contribution', 'create', $params);
580 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
581 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF');
582 }
583
584 /**
585 * Create test with unique field name on source.
586 */
587 public function testCreateDefaultNow() {
588
589 $params = $this->_params;
590 unset($params['receive_date']);
591
592 $contribution = $this->callAPISuccess('contribution', 'create', $params);
593 $contribution = $this->callAPISuccessGetSingle('contribution', array('id' => $contribution['id']));
594 $this->assertEquals(date('Y-m-d'), date('Y-m-d', strtotime($contribution['receive_date'])));
595 }
596
597 /**
598 * Create test with unique field name on source.
599 */
600 public function testCreateContributionSourceInvalidContact() {
601
602 $params = array(
603 'contact_id' => 999,
604 'receive_date' => date('Ymd'),
605 'total_amount' => 100.00,
606 'financial_type_id' => $this->_financialTypeId,
607 'payment_instrument_id' => 1,
608 'non_deductible_amount' => 10.00,
609 'fee_amount' => 50.00,
610 'net_amount' => 90.00,
611 'trxn_id' => 12345,
612 'invoice_id' => 67890,
613 'contribution_source' => 'SSF',
614 'contribution_status_id' => 1,
615 );
616
617 $this->callAPIFailure('contribution', 'create', $params, 'contact_id is not valid : 999');
618 }
619
620 public function testCreateContributionSourceInvalidContContact() {
621
622 $params = array(
623 'contribution_contact_id' => 999,
624 'receive_date' => date('Ymd'),
625 'total_amount' => 100.00,
626 'financial_type_id' => $this->_financialTypeId,
627 'payment_instrument_id' => 1,
628 'non_deductible_amount' => 10.00,
629 'fee_amount' => 50.00,
630 'net_amount' => 90.00,
631 'trxn_id' => 12345,
632 'invoice_id' => 67890,
633 'contribution_source' => 'SSF',
634 'contribution_status_id' => 1,
635 );
636
637 $this->callAPIFailure('contribution', 'create', $params);
638 }
639
640 /**
641 * Test note created correctly.
642 */
643 public function testCreateContributionWithNote() {
644 $description = "Demonstrates creating contribution with Note Entity.";
645 $subfile = "ContributionCreateWithNote";
646 $params = array(
647 'contact_id' => $this->_individualId,
648 'receive_date' => '2012-01-01',
649 'total_amount' => 100.00,
650 'financial_type_id' => $this->_financialTypeId,
651 'payment_instrument_id' => 1,
652 'non_deductible_amount' => 10.00,
653 'fee_amount' => 50.00,
654 'net_amount' => 90.00,
655 'trxn_id' => 12345,
656 'invoice_id' => 67890,
657 'source' => 'SSF',
658 'contribution_status_id' => 1,
659 'note' => 'my contribution note',
660 );
661
662 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__, __FILE__, $description, $subfile);
663 $result = $this->callAPISuccess('note', 'get', array(
664 'entity_table' => 'civicrm_contribution',
665 'entity_id' => $contribution['id'],
666 'sequential' => 1,
667 ));
668 $this->assertEquals('my contribution note', $result['values'][0]['note']);
669 $this->callAPISuccess('contribution', 'delete', array('id' => $contribution['id']));
670 }
671
672 public function testCreateContributionWithNoteUniqueNameAliases() {
673 $params = array(
674 'contact_id' => $this->_individualId,
675 'receive_date' => '2012-01-01',
676 'total_amount' => 100.00,
677 'financial_type_id' => $this->_financialTypeId,
678 'payment_instrument_id' => 1,
679 'non_deductible_amount' => 10.00,
680 'fee_amount' => 50.00,
681 'net_amount' => 90.00,
682 'trxn_id' => 12345,
683 'invoice_id' => 67890,
684 'source' => 'SSF',
685 'contribution_status_id' => 1,
686 'contribution_note' => 'my contribution note',
687 );
688
689 $contribution = $this->callAPISuccess('contribution', 'create', $params);
690 $result = $this->callAPISuccess('note', 'get', array(
691 'entity_table' => 'civicrm_contribution',
692 'entity_id' => $contribution['id'],
693 'sequential' => 1,
694 ));
695 $this->assertEquals('my contribution note', $result['values'][0]['note']);
696 $this->callAPISuccess('contribution', 'delete', array('id' => $contribution['id']));
697 }
698
699 /**
700 * This is the test for creating soft credits.
701 */
702 public function testCreateContributionWithSoftCredit() {
703 $description = "Demonstrates creating contribution with SoftCredit.";
704 $subfile = "ContributionCreateWithSoftCredit";
705 $contact2 = $this->callAPISuccess('Contact', 'create', array(
706 'display_name' => 'superman',
707 'contact_type' => 'Individual',
708 ));
709 $softParams = array(
710 'contact_id' => $contact2['id'],
711 'amount' => 50,
712 'soft_credit_type_id' => 3,
713 );
714
715 $params = $this->_params + array('soft_credit' => array(1 => $softParams));
716 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__, __FILE__, $description, $subfile);
717 $result = $this->callAPISuccess('contribution', 'get', array('return' => 'soft_credit', 'sequential' => 1));
718
719 $this->assertEquals($softParams['contact_id'], $result['values'][0]['soft_credit'][1]['contact_id']);
720 $this->assertEquals($softParams['amount'], $result['values'][0]['soft_credit'][1]['amount']);
721 $this->assertEquals($softParams['soft_credit_type_id'], $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
727 public function testCreateContributionWithSoftCreditDefaults() {
728 $description = "Demonstrates creating contribution with Soft Credit defaults for amount and type.";
729 $subfile = "ContributionCreateWithSoftCreditDefaults";
730 $contact2 = $this->callAPISuccess('Contact', 'create', array(
731 'display_name' => 'superman',
732 'contact_type' => 'Individual',
733 ));
734 $params = $this->_params + array(
735 'soft_credit_to' => $contact2['id'],
736 );
737 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__, __FILE__, $description, $subfile);
738 $result = $this->callAPISuccess('contribution', 'get', array('return' => 'soft_credit', 'sequential' => 1));
739
740 $this->assertEquals($contact2['id'], $result['values'][0]['soft_credit'][1]['contact_id']);
741 // Default soft credit amount = contribution.total_amount
742 $this->assertEquals($this->_params['total_amount'], $result['values'][0]['soft_credit'][1]['amount']);
743 $this->assertEquals(CRM_Core_OptionGroup::getDefaultValue("soft_credit_type"), $result['values'][0]['soft_credit'][1]['soft_credit_type']);
744
745 $this->callAPISuccess('contribution', 'delete', array('id' => $contribution['id']));
746 $this->callAPISuccess('contact', 'delete', array('id' => $contact2['id']));
747 }
748
749 public function testCreateContributionWithHonoreeContact() {
750 $description = "Demonstrates creating contribution with Soft Credit by passing in honor_contact_id.";
751 $subfile = "ContributionCreateWithHonoreeContact";
752 $contact2 = $this->callAPISuccess('Contact', 'create', array(
753 'display_name' => 'superman',
754 'contact_type' => 'Individual',
755 ));
756 $params = $this->_params + array(
757 'honor_contact_id' => $contact2['id'],
758 );
759 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__, __FILE__, $description, $subfile);
760 $result = $this->callAPISuccess('contribution', 'get', array('return' => 'soft_credit', 'sequential' => 1));
761
762 $this->assertEquals($contact2['id'], $result['values'][0]['soft_credit'][1]['contact_id']);
763 // Default soft credit amount = contribution.total_amount
764 // Legacy mode in create api (honor_contact_id param) uses the standard "In Honor of" soft credit type
765 $this->assertEquals($this->_params['total_amount'], $result['values'][0]['soft_credit'][1]['amount']);
766 $this->assertEquals(CRM_Core_OptionGroup::getValue('soft_credit_type', 'in_honor_of', 'name'), $result['values'][0]['soft_credit'][1]['soft_credit_type']);
767
768 $this->callAPISuccess('contribution', 'delete', array('id' => $contribution['id']));
769 $this->callAPISuccess('contact', 'delete', array('id' => $contact2['id']));
770 }
771
772 /**
773 * Test using example code.
774 */
775 public function testContributionCreateExample() {
776 //make sure at least on page exists since there is a truncate in tear down
777 $this->callAPISuccess('contribution_page', 'create', $this->_pageParams);
778 require_once 'api/v3/examples/Contribution/Create.php';
779 $result = contribution_create_example();
780 $id = $result['id'];
781 $expectedResult = contribution_create_expectedresult();
782 $this->checkArrayEquals($expectedResult, $result);
783 $this->contributionDelete($id);
784 }
785
786 /**
787 * Function tests that additional financial records are created when fee amount is recorded.
788 */
789 public function testCreateContributionWithFee() {
790 $params = array(
791 'contact_id' => $this->_individualId,
792 'receive_date' => '20120511',
793 'total_amount' => 100.00,
794 'fee_amount' => 50,
795 'financial_type_id' => 1,
796 'trxn_id' => 12345,
797 'invoice_id' => 67890,
798 'source' => 'SSF',
799 'contribution_status_id' => 1,
800 );
801
802 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__, __FILE__);
803 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId);
804 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
805 $this->assertEquals($contribution['values'][$contribution['id']]['fee_amount'], 50.00);
806 $this->assertEquals($contribution['values'][$contribution['id']]['net_amount'], 50.00);
807 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'], 1);
808 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 12345);
809 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 67890);
810 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF');
811 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status_id'], 1);
812
813 $lineItems = $this->callAPISuccess('line_item', 'get', array(
814
815 'entity_id' => $contribution['id'],
816 'entity_table' => 'civicrm_contribution',
817 'sequential' => 1,
818 ));
819 $this->assertEquals(1, $lineItems['count']);
820 $this->assertEquals($contribution['id'], $lineItems['values'][0]['entity_id']);
821 $this->assertEquals($contribution['id'], $lineItems['values'][0]['contribution_id']);
822 $lineItems = $this->callAPISuccess('line_item', 'get', array(
823
824 'entity_id' => $contribution['id'],
825 'contribution_id' => $contribution['id'],
826 'entity_table' => 'civicrm_contribution',
827 'sequential' => 1,
828 ));
829 $this->assertEquals(1, $lineItems['count']);
830 $this->_checkFinancialRecords($contribution, 'feeAmount');
831 }
832
833
834 /**
835 * Function tests that additional financial records are created when online contribution is created.
836 */
837 public function testCreateContributionOnline() {
838 CRM_Financial_BAO_PaymentProcessor::create($this->_processorParams);
839 $contributionPage = $this->callAPISuccess('contribution_page', 'create', $this->_pageParams);
840 $this->assertAPISuccess($contributionPage);
841 $params = array(
842 'contact_id' => $this->_individualId,
843 'receive_date' => '20120511',
844 'total_amount' => 100.00,
845 'financial_type_id' => 1,
846 'contribution_page_id' => $contributionPage['id'],
847 'payment_processor' => 1,
848 'trxn_id' => 12345,
849 'invoice_id' => 67890,
850 'source' => 'SSF',
851 'contribution_status_id' => 1,
852
853 );
854
855 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__, __FILE__);
856 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId);
857 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
858 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'], 1);
859 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 12345);
860 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 67890);
861 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF');
862 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status_id'], 1);
863 $this->_checkFinancialRecords($contribution, 'online');
864 }
865
866 /**
867 * Check handling of financial type.
868 *
869 * In the interests of removing financial type / contribution type checks from
870 * legacy format function lets test that the api is doing this for us
871 */
872 public function testCreateInvalidFinancialType() {
873 $params = $this->_params;
874 $params['financial_type_id'] = 99999;
875 $this->callAPIFailure($this->_entity, 'create', $params, "'99999' is not a valid option for field financial_type_id");
876 }
877
878 /**
879 * Check handling of financial type.
880 *
881 * In the interests of removing financial type / contribution type checks from
882 * legacy format function lets test that the api is doing this for us
883 */
884 public function testValidNamedFinancialType() {
885 $params = $this->_params;
886 $params['financial_type_id'] = 'Donation';
887 $this->callAPISuccess($this->_entity, 'create', $params);
888 }
889
890 /**
891 * Tests that additional financial records are created.
892 *
893 * Checks when online contribution with pay later option is created
894 */
895 public function testCreateContributionPayLaterOnline() {
896 CRM_Financial_BAO_PaymentProcessor::create($this->_processorParams);
897 $this->_pageParams['is_pay_later'] = 1;
898 $contributionPage = $this->callAPISuccess('contribution_page', 'create', $this->_pageParams);
899 $this->assertAPISuccess($contributionPage);
900 $params = array(
901 'contact_id' => $this->_individualId,
902 'receive_date' => '20120511',
903 'total_amount' => 100.00,
904 'financial_type_id' => 1,
905 'contribution_page_id' => $contributionPage['id'],
906 'trxn_id' => 12345,
907 'is_pay_later' => 1,
908 'invoice_id' => 67890,
909 'source' => 'SSF',
910 'contribution_status_id' => 2,
911
912 );
913
914 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__, __FILE__);
915 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId);
916 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
917 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'], 1);
918 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 12345);
919 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 67890);
920 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF');
921 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status_id'], 2);
922 $this->_checkFinancialRecords($contribution, 'payLater');
923 }
924
925 /**
926 * Function tests that additional financial records are created for online contribution with pending option.
927 */
928 public function testCreateContributionPendingOnline() {
929 $paymentProcessor = CRM_Financial_BAO_PaymentProcessor::create($this->_processorParams);
930 $contributionPage = $this->callAPISuccess('contribution_page', 'create', $this->_pageParams);
931 $this->assertAPISuccess($contributionPage);
932 $params = array(
933 'contact_id' => $this->_individualId,
934 'receive_date' => '20120511',
935 'total_amount' => 100.00,
936 'financial_type_id' => 1,
937 'contribution_page_id' => $contributionPage['id'],
938 'trxn_id' => 12345,
939 'invoice_id' => 67890,
940 'source' => 'SSF',
941 'contribution_status_id' => 2,
942 );
943
944 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__, __FILE__);
945 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId);
946 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
947 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'], 1);
948 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 12345);
949 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 67890);
950 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF');
951 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status_id'], 2);
952 $this->_checkFinancialRecords($contribution, 'pending');
953 }
954
955 /**
956 * Test that BAO defaults work.
957 */
958 public function testCreateBAODefaults() {
959 unset($this->_params['contribution_source_id'], $this->_params['payment_instrument_id']);
960 $contribution = $this->callAPISuccess('contribution', 'create', $this->_params);
961 $contribution = $this->callAPISuccess('contribution', 'getsingle', array(
962 'id' => $contribution['id'],
963 'api.contribution.delete' => 1,
964 ));
965 $this->assertEquals(1, $contribution['contribution_status_id']);
966 $this->assertEquals('Check', $contribution['payment_instrument']);
967 }
968
969 /**
970 * Function tests that line items, financial records are updated when contribution amount is changed.
971 */
972 public function testCreateUpdateContributionChangeTotal() {
973 $contribution = $this->callAPISuccess('contribution', 'create', $this->_params);
974 $lineItems = $this->callAPISuccess('line_item', 'getvalue', array(
975
976 'entity_id' => $contribution['id'],
977 'entity_table' => 'civicrm_contribution',
978 'sequential' => 1,
979 'return' => 'line_total',
980 ));
981 $this->assertEquals('100.00', $lineItems);
982 $trxnAmount = $this->_getFinancialTrxnAmount($contribution['id']);
983 // Financial trxn SUM = 100 + 5 (fee)
984 $this->assertEquals('105.00', $trxnAmount);
985 $newParams = array(
986
987 'id' => $contribution['id'],
988 'total_amount' => '125',
989 );
990 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
991
992 $lineItems = $this->callAPISuccess('line_item', 'getvalue', array(
993
994 'entity_id' => $contribution['id'],
995 'entity_table' => 'civicrm_contribution',
996 'sequential' => 1,
997 'return' => 'line_total',
998 ));
999
1000 $this->assertEquals('125.00', $lineItems);
1001 $trxnAmount = $this->_getFinancialTrxnAmount($contribution['id']);
1002
1003 // Financial trxn SUM = 125 + 5 (fee).
1004 $this->assertEquals('130.00', $trxnAmount);
1005 $this->assertEquals('125.00', $this->_getFinancialItemAmount($contribution['id']));
1006 }
1007
1008 /**
1009 * Function tests that line items, financial records are updated when pay later contribution is received.
1010 */
1011 public function testCreateUpdateContributionPayLater() {
1012 $contribParams = array(
1013 'contact_id' => $this->_individualId,
1014 'receive_date' => '2012-01-01',
1015 'total_amount' => 100.00,
1016 'financial_type_id' => $this->_financialTypeId,
1017 'payment_instrument_id' => 1,
1018 'contribution_status_id' => 2,
1019 'is_pay_later' => 1,
1020
1021 );
1022 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
1023
1024 $newParams = array_merge($contribParams, array(
1025 'id' => $contribution['id'],
1026 'contribution_status_id' => 1,
1027 )
1028 );
1029 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1030 $contribution = $contribution['values'][$contribution['id']];
1031 $this->assertEquals($contribution['contribution_status_id'], '1');
1032 $this->_checkFinancialItem($contribution['id'], 'paylater');
1033 $this->_checkFinancialTrxn($contribution, 'payLater');
1034 }
1035
1036 /**
1037 * Function tests that financial records are updated when Payment Instrument is changed.
1038 */
1039 public function testCreateUpdateContributionPaymentInstrument() {
1040 $instrumentId = $this->_addPaymentInstrument();
1041 $contribParams = array(
1042 'contact_id' => $this->_individualId,
1043 'total_amount' => 100.00,
1044 'financial_type_id' => $this->_financialTypeId,
1045 'payment_instrument_id' => 4,
1046 'contribution_status_id' => 1,
1047
1048 );
1049 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
1050
1051 $newParams = array_merge($contribParams, array(
1052 'id' => $contribution['id'],
1053 'payment_instrument_id' => $instrumentId,
1054 )
1055 );
1056 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1057 $this->assertAPISuccess($contribution);
1058 $this->_checkFinancialTrxn($contribution, 'paymentInstrument', $instrumentId);
1059 }
1060
1061 /**
1062 * Function tests that financial records are updated when Payment Instrument is changed when amount is negative.
1063 */
1064 public function testCreateUpdateNegativeContributionPaymentInstrument() {
1065 $instrumentId = $this->_addPaymentInstrument();
1066 $contribParams = array(
1067 'contact_id' => $this->_individualId,
1068 'total_amount' => -100.00,
1069 'financial_type_id' => $this->_financialTypeId,
1070 'payment_instrument_id' => 4,
1071 'contribution_status_id' => 1,
1072
1073 );
1074 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
1075
1076 $newParams = array_merge($contribParams, array(
1077 'id' => $contribution['id'],
1078 'payment_instrument_id' => $instrumentId,
1079 )
1080 );
1081 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1082 $this->assertAPISuccess($contribution);
1083 $this->_checkFinancialTrxn($contribution, 'paymentInstrument', $instrumentId, array('total_amount' => '-100.00'));
1084 }
1085
1086 /**
1087 * Function tests that financial records are added when Contribution is Refunded.
1088 */
1089 public function testCreateUpdateContributionRefund() {
1090 $contributionParams = array(
1091 'contact_id' => $this->_individualId,
1092 'receive_date' => '2012-01-01',
1093 'total_amount' => 100.00,
1094 'financial_type_id' => $this->_financialTypeId,
1095 'payment_instrument_id' => 4,
1096 'contribution_status_id' => 1,
1097 'trxn_id' => 'original_payment',
1098 );
1099 $contribution = $this->callAPISuccess('contribution', 'create', $contributionParams);
1100 $newParams = array_merge($contributionParams, array(
1101 'id' => $contribution['id'],
1102 'contribution_status_id' => 'Refunded',
1103 'cancel_date' => '2015-01-01 09:00',
1104 'refund_trxn_id' => 'the refund',
1105 )
1106 );
1107
1108 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1109 $this->_checkFinancialTrxn($contribution, 'refund');
1110 $this->_checkFinancialItem($contribution['id'], 'refund');
1111 $this->assertEquals('original_payment', $this->callAPISuccessGetValue('Contribution', array(
1112 'id' => $contribution['id'],
1113 'return' => 'trxn_id',
1114 )));
1115 }
1116
1117 /**
1118 * Refund a contribution for a financial type with a contra account.
1119 *
1120 * CRM-17951 the contra account is a financial account with a relationship to a
1121 * financial type. It is not always configured but should be reflected
1122 * in the financial_trxn & financial_item table if it is.
1123 */
1124 public function testCreateUpdateChargebackContributionDefaultAccount() {
1125 $contribution = $this->callAPISuccess('Contribution', 'create', $this->_params);
1126 $this->callAPISuccess('Contribution', 'create', array(
1127 'id' => $contribution['id'],
1128 'contribution_status_id' => 'Chargeback',
1129 ));
1130 $this->callAPISuccessGetSingle('Contribution', array('contribution_status_id' => 'Chargeback'));
1131
1132 $lineItems = $this->callAPISuccessGetSingle('LineItem', array(
1133 'contribution_id' => $contribution['id'],
1134 'api.FinancialItem.getsingle' => array('amount' => array('<' => 0)),
1135 ));
1136 $this->assertEquals(1, $lineItems['api.FinancialItem.getsingle']['financial_account_id']);
1137 $this->callAPISuccessGetSingle('FinancialTrxn', array(
1138 'total_amount' => -100,
1139 'status_id' => 'Chargeback',
1140 'to_financial_account_id' => 6,
1141 ));
1142 }
1143
1144 /**
1145 * Refund a contribution for a financial type with a contra account.
1146 *
1147 * CRM-17951 the contra account is a financial account with a relationship to a
1148 * financial type. It is not always configured but should be reflected
1149 * in the financial_trxn & financial_item table if it is.
1150 */
1151 public function testCreateUpdateChargebackContributionCustomAccount() {
1152 $financialAccount = $this->callAPISuccess('FinancialAccount', 'create', array(
1153 'name' => 'Chargeback Account',
1154 'is_active' => TRUE,
1155 ));
1156
1157 $entityFinancialAccount = $this->callAPISuccess('EntityFinancialAccount', 'create', array(
1158 'entity_id' => $this->_financialTypeId,
1159 'entity_table' => 'civicrm_financial_type',
1160 'account_relationship' => 'Chargeback Account is',
1161 'financial_account_id' => 'Chargeback Account',
1162 ));
1163
1164 $contribution = $this->callAPISuccess('Contribution', 'create', $this->_params);
1165 $this->callAPISuccess('Contribution', 'create', array(
1166 'id' => $contribution['id'],
1167 'contribution_status_id' => 'Chargeback',
1168 ));
1169 $this->callAPISuccessGetSingle('Contribution', array('contribution_status_id' => 'Chargeback'));
1170
1171 $lineItems = $this->callAPISuccessGetSingle('LineItem', array(
1172 'contribution_id' => $contribution['id'],
1173 'api.FinancialItem.getsingle' => array('amount' => array('<' => 0)),
1174 ));
1175 $this->assertEquals($financialAccount['id'], $lineItems['api.FinancialItem.getsingle']['financial_account_id']);
1176
1177 $this->callAPISuccess('Contribution', 'delete', array('id' => $contribution['id']));
1178 $this->callAPISuccess('EntityFinancialAccount', 'delete', array('id' => $entityFinancialAccount['id']));
1179 $this->callAPISuccess('FinancialAccount', 'delete', array('id' => $financialAccount['id']));
1180 }
1181
1182 /**
1183 * Refund a contribution for a financial type with a contra account.
1184 *
1185 * CRM-17951 the contra account is a financial account with a relationship to a
1186 * financial type. It is not always configured but should be reflected
1187 * in the financial_trxn & financial_item table if it is.
1188 */
1189 public function testCreateUpdateRefundContributionConfiguredContraAccount() {
1190 $financialAccount = $this->callAPISuccess('FinancialAccount', 'create', array(
1191 'name' => 'Refund Account',
1192 'is_active' => TRUE,
1193 ));
1194
1195 $entityFinancialAccount = $this->callAPISuccess('EntityFinancialAccount', 'create', array(
1196 'entity_id' => $this->_financialTypeId,
1197 'entity_table' => 'civicrm_financial_type',
1198 'account_relationship' => 'Credit/Contra Revenue Account is',
1199 'financial_account_id' => 'Refund Account',
1200 ));
1201
1202 $contribution = $this->callAPISuccess('Contribution', 'create', $this->_params);
1203 $this->callAPISuccess('Contribution', 'create', array(
1204 'id' => $contribution['id'],
1205 'contribution_status_id' => 'Refunded',
1206 ));
1207
1208 $lineItems = $this->callAPISuccessGetSingle('LineItem', array(
1209 'contribution_id' => $contribution['id'],
1210 'api.FinancialItem.getsingle' => array('amount' => array('<' => 0)),
1211 ));
1212 $this->assertEquals($financialAccount['id'], $lineItems['api.FinancialItem.getsingle']['financial_account_id']);
1213
1214 $this->callAPISuccess('Contribution', 'delete', array('id' => $contribution['id']));
1215 $this->callAPISuccess('EntityFinancialAccount', 'delete', array('id' => $entityFinancialAccount['id']));
1216 $this->callAPISuccess('FinancialAccount', 'delete', array('id' => $financialAccount['id']));
1217 }
1218
1219 /**
1220 * Function tests that trxn_id is set when passed in.
1221 *
1222 * Here we ensure that the civicrm_financial_trxn.trxn_id & the civicrm_contribution.trxn_id are set
1223 * when trxn_id is passed in.
1224 */
1225 public function testCreateUpdateContributionRefundTrxnIDPassedIn() {
1226 $contributionParams = array(
1227 'contact_id' => $this->_individualId,
1228 'receive_date' => '2012-01-01',
1229 'total_amount' => 100.00,
1230 'financial_type_id' => $this->_financialTypeId,
1231 'payment_instrument_id' => 4,
1232 'contribution_status_id' => 1,
1233 'trxn_id' => 'original_payment',
1234 );
1235 $contribution = $this->callAPISuccess('contribution', 'create', $contributionParams);
1236 $newParams = array_merge($contributionParams, array(
1237 'id' => $contribution['id'],
1238 'contribution_status_id' => 'Refunded',
1239 'cancel_date' => '2015-01-01 09:00',
1240 'trxn_id' => 'the refund',
1241 )
1242 );
1243
1244 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1245 $this->_checkFinancialTrxn($contribution, 'refund');
1246 $this->_checkFinancialItem($contribution['id'], 'refund');
1247 $this->assertEquals('the refund', $this->callAPISuccessGetValue('Contribution', array(
1248 'id' => $contribution['id'],
1249 'return' => 'trxn_id',
1250 )));
1251 }
1252
1253 /**
1254 * Function tests that trxn_id is set when passed in.
1255 *
1256 * Here we ensure that the civicrm_contribution.trxn_id is set
1257 * when trxn_id is passed in but if refund_trxn_id is different then that
1258 * is kept for the refund transaction.
1259 */
1260 public function testCreateUpdateContributionRefundRefundAndTrxnIDPassedIn() {
1261 $contributionParams = array(
1262 'contact_id' => $this->_individualId,
1263 'receive_date' => '2012-01-01',
1264 'total_amount' => 100.00,
1265 'financial_type_id' => $this->_financialTypeId,
1266 'payment_instrument_id' => 4,
1267 'contribution_status_id' => 1,
1268 'trxn_id' => 'original_payment',
1269 );
1270 $contribution = $this->callAPISuccess('contribution', 'create', $contributionParams);
1271 $newParams = array_merge($contributionParams, array(
1272 'id' => $contribution['id'],
1273 'contribution_status_id' => 'Refunded',
1274 'cancel_date' => '2015-01-01 09:00',
1275 'trxn_id' => 'cont id',
1276 'refund_trxn_id' => 'the refund',
1277 )
1278 );
1279
1280 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1281 $this->_checkFinancialTrxn($contribution, 'refund');
1282 $this->_checkFinancialItem($contribution['id'], 'refund');
1283 $this->assertEquals('cont id', $this->callAPISuccessGetValue('Contribution', array(
1284 'id' => $contribution['id'],
1285 'return' => 'trxn_id',
1286 )));
1287 }
1288
1289 /**
1290 * Function tests that refund_trxn_id is set when passed in empty.
1291 *
1292 * Here we ensure that the civicrm_contribution.trxn_id is set
1293 * when trxn_id is passed in but if refund_trxn_id isset but empty then that
1294 * is kept for the refund transaction.
1295 */
1296 public function testCreateUpdateContributionRefundRefundNullTrxnIDPassedIn() {
1297 $contributionParams = array(
1298 'contact_id' => $this->_individualId,
1299 'receive_date' => '2012-01-01',
1300 'total_amount' => 100.00,
1301 'financial_type_id' => $this->_financialTypeId,
1302 'payment_instrument_id' => 4,
1303 'contribution_status_id' => 1,
1304 'trxn_id' => 'original_payment',
1305 );
1306 $contribution = $this->callAPISuccess('contribution', 'create', $contributionParams);
1307 $newParams = array_merge($contributionParams, array(
1308 'id' => $contribution['id'],
1309 'contribution_status_id' => 'Refunded',
1310 'cancel_date' => '2015-01-01 09:00',
1311 'trxn_id' => 'cont id',
1312 'refund_trxn_id' => '',
1313 )
1314 );
1315
1316 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1317 $this->_checkFinancialTrxn($contribution, 'refund', NULL, array('trxn_id' => NULL));
1318 $this->_checkFinancialItem($contribution['id'], 'refund');
1319 $this->assertEquals('cont id', $this->callAPISuccessGetValue('Contribution', array(
1320 'id' => $contribution['id'],
1321 'return' => 'trxn_id',
1322 )));
1323 }
1324
1325 /**
1326 * Function tests invalid contribution status change.
1327 */
1328 public function testCreateUpdateContributionInValidStatusChange() {
1329 $contribParams = array(
1330 'contact_id' => 1,
1331 'receive_date' => '2012-01-01',
1332 'total_amount' => 100.00,
1333 'financial_type_id' => 1,
1334 'payment_instrument_id' => 1,
1335 'contribution_status_id' => 1,
1336 );
1337 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
1338 $newParams = array_merge($contribParams, array(
1339 'id' => $contribution['id'],
1340 'contribution_status_id' => 2,
1341 )
1342 );
1343 $this->callAPIFailure('contribution', 'create', $newParams, ts('Cannot change contribution status from Completed to Pending.'));
1344
1345 }
1346
1347 /**
1348 * Function tests that financial records are added when Pending Contribution is Canceled.
1349 */
1350 public function testCreateUpdateContributionCancelPending() {
1351 $contribParams = array(
1352 'contact_id' => $this->_individualId,
1353 'receive_date' => '2012-01-01',
1354 'total_amount' => 100.00,
1355 'financial_type_id' => $this->_financialTypeId,
1356 'payment_instrument_id' => 1,
1357 'contribution_status_id' => 2,
1358 'is_pay_later' => 1,
1359
1360 );
1361 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
1362 $newParams = array_merge($contribParams, array(
1363 'id' => $contribution['id'],
1364 'contribution_status_id' => 3,
1365 'cancel_date' => '2012-02-02 09:00',
1366 )
1367 );
1368 //Check if trxn_date is same as cancel_date.
1369 $checkTrxnDate = array(
1370 'trxn_date' => '2012-02-02 09:00:00',
1371 );
1372 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1373 $this->_checkFinancialTrxn($contribution, 'cancelPending', NULL, $checkTrxnDate);
1374 $this->_checkFinancialItem($contribution['id'], 'cancelPending');
1375 }
1376
1377 /**
1378 * Function tests that financial records are added when Financial Type is Changed.
1379 */
1380 public function testCreateUpdateContributionChangeFinancialType() {
1381 $contribParams = array(
1382 'contact_id' => $this->_individualId,
1383 'receive_date' => '2012-01-01',
1384 'total_amount' => 100.00,
1385 'financial_type_id' => 1,
1386 'payment_instrument_id' => 1,
1387 'contribution_status_id' => 1,
1388
1389 );
1390 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
1391 $newParams = array_merge($contribParams, array(
1392 'id' => $contribution['id'],
1393 'financial_type_id' => 3,
1394 )
1395 );
1396 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1397 $this->_checkFinancialTrxn($contribution, 'changeFinancial');
1398 $this->_checkFinancialItem($contribution['id'], 'changeFinancial');
1399 }
1400
1401 /**
1402 * Test that update does not change status id CRM-15105.
1403 */
1404 public function testCreateUpdateWithoutChangingPendingStatus() {
1405 $contribution = $this->callAPISuccess('contribution', 'create', array_merge($this->_params, array('contribution_status_id' => 2)));
1406 $this->callAPISuccess('contribution', 'create', array('id' => $contribution['id'], 'source' => 'new source'));
1407 $contribution = $this->callAPISuccess('contribution', 'getsingle', array(
1408 'id' => $contribution['id'],
1409 'api.contribution.delete' => 1,
1410 ));
1411 $this->assertEquals(2, $contribution['contribution_status_id']);
1412 }
1413
1414 /**
1415 * Test Updating a Contribution.
1416 *
1417 * CHANGE: we require the API to do an incremental update
1418 */
1419 public function testCreateUpdateContribution() {
1420
1421 $contributionID = $this->contributionCreate(array(
1422 'contact_id' => $this->_individualId,
1423 'trxn_id' => 212355,
1424 'financial_type_id' => $this->_financialTypeId,
1425 'invoice_id' => 'old_invoice',
1426 ));
1427 $old_params = array(
1428 'contribution_id' => $contributionID,
1429 );
1430 $original = $this->callAPISuccess('contribution', 'get', $old_params);
1431 $this->assertEquals($original['id'], $contributionID);
1432 //set up list of old params, verify
1433
1434 //This should not be required on update:
1435 $old_contact_id = $original['values'][$contributionID]['contact_id'];
1436 $old_payment_instrument = $original['values'][$contributionID]['instrument_id'];
1437 $old_fee_amount = $original['values'][$contributionID]['fee_amount'];
1438 $old_source = $original['values'][$contributionID]['contribution_source'];
1439
1440 $old_trxn_id = $original['values'][$contributionID]['trxn_id'];
1441 $old_invoice_id = $original['values'][$contributionID]['invoice_id'];
1442
1443 //check against values in CiviUnitTestCase::createContribution()
1444 $this->assertEquals($old_contact_id, $this->_individualId);
1445 $this->assertEquals($old_fee_amount, 5.00);
1446 $this->assertEquals($old_source, 'SSF');
1447 $this->assertEquals($old_trxn_id, 212355);
1448 $this->assertEquals($old_invoice_id, 'old_invoice');
1449 $params = array(
1450 'id' => $contributionID,
1451 'contact_id' => $this->_individualId,
1452 'total_amount' => 110.00,
1453 'financial_type_id' => $this->_financialTypeId,
1454 'non_deductible_amount' => 10.00,
1455 'net_amount' => 100.00,
1456 'contribution_status_id' => 1,
1457 'note' => 'Donating for Nobel Cause',
1458
1459 );
1460
1461 $contribution = $this->callAPISuccess('contribution', 'create', $params);
1462
1463 $new_params = array(
1464 'contribution_id' => $contribution['id'],
1465
1466 );
1467 $contribution = $this->callAPISuccess('contribution', 'get', $new_params);
1468
1469 $this->assertEquals($contribution['values'][$contributionID]['contact_id'], $this->_individualId);
1470 $this->assertEquals($contribution['values'][$contributionID]['total_amount'], 110.00);
1471 $this->assertEquals($contribution['values'][$contributionID]['financial_type_id'], $this->_financialTypeId);
1472 $this->assertEquals($contribution['values'][$contributionID]['instrument_id'], $old_payment_instrument);
1473 $this->assertEquals($contribution['values'][$contributionID]['non_deductible_amount'], 10.00);
1474 $this->assertEquals($contribution['values'][$contributionID]['fee_amount'], $old_fee_amount);
1475 $this->assertEquals($contribution['values'][$contributionID]['net_amount'], 100.00);
1476 $this->assertEquals($contribution['values'][$contributionID]['trxn_id'], $old_trxn_id);
1477 $this->assertEquals($contribution['values'][$contributionID]['invoice_id'], $old_invoice_id);
1478 $this->assertEquals($contribution['values'][$contributionID]['contribution_source'], $old_source);
1479 $this->assertEquals($contribution['values'][$contributionID]['contribution_status'], 'Completed');
1480 $params = array(
1481 'contribution_id' => $contributionID,
1482
1483 );
1484 $result = $this->callAPISuccess('contribution', 'delete', $params);
1485 $this->assertAPISuccess($result);
1486 }
1487
1488 ///////////////// civicrm_contribution_delete methods
1489
1490 /**
1491 * Attempt (but fail) to delete a contribution without parameters.
1492 */
1493 public function testDeleteEmptyParamsContribution() {
1494 $params = array();
1495 $this->callAPIFailure('contribution', 'delete', $params);
1496 }
1497
1498 public function testDeleteParamsNotArrayContribution() {
1499 $params = 'contribution_id= 1';
1500 $contribution = $this->callAPIFailure('contribution', 'delete', $params);
1501 $this->assertEquals($contribution['error_message'], 'Input variable `params` is not an array');
1502 }
1503
1504 public function testDeleteWrongParamContribution() {
1505 $params = array(
1506 'contribution_source' => 'SSF',
1507
1508 );
1509 $this->callAPIFailure('contribution', 'delete', $params);
1510 }
1511
1512 public function testDeleteContribution() {
1513 $contributionID = $this->contributionCreate(array(
1514 'contact_id' => $this->_individualId,
1515 'financial_type_id' => $this->_financialTypeId,
1516 ));
1517 $params = array(
1518 'id' => $contributionID,
1519 );
1520 $this->callAPIAndDocument('contribution', 'delete', $params, __FUNCTION__, __FILE__);
1521 }
1522
1523 /**
1524 * Test civicrm_contribution_search with empty params.
1525 *
1526 * All available contributions expected.
1527 */
1528 public function testSearchEmptyParams() {
1529 $params = array();
1530
1531 $p = array(
1532 'contact_id' => $this->_individualId,
1533 'receive_date' => date('Ymd'),
1534 'total_amount' => 100.00,
1535 'financial_type_id' => $this->_financialTypeId,
1536 'non_deductible_amount' => 10.00,
1537 'fee_amount' => 5.00,
1538 'net_amount' => 95.00,
1539 'trxn_id' => 23456,
1540 'invoice_id' => 78910,
1541 'source' => 'SSF',
1542 'contribution_status_id' => 1,
1543
1544 );
1545 $contribution = $this->callAPISuccess('contribution', 'create', $p);
1546
1547 $result = $this->callAPISuccess('contribution', 'get', $params);
1548 // We're taking the first element.
1549 $res = $result['values'][$contribution['id']];
1550
1551 $this->assertEquals($p['contact_id'], $res['contact_id']);
1552 $this->assertEquals($p['total_amount'], $res['total_amount']);
1553 $this->assertEquals($p['financial_type_id'], $res['financial_type_id']);
1554 $this->assertEquals($p['net_amount'], $res['net_amount']);
1555 $this->assertEquals($p['non_deductible_amount'], $res['non_deductible_amount']);
1556 $this->assertEquals($p['fee_amount'], $res['fee_amount']);
1557 $this->assertEquals($p['trxn_id'], $res['trxn_id']);
1558 $this->assertEquals($p['invoice_id'], $res['invoice_id']);
1559 $this->assertEquals($p['source'], $res['contribution_source']);
1560 // contribution_status_id = 1 => Completed
1561 $this->assertEquals('Completed', $res['contribution_status']);
1562
1563 $this->contributionDelete($contribution['id']);
1564 }
1565
1566 /**
1567 * Test civicrm_contribution_search. Success expected.
1568 */
1569 public function testSearch() {
1570 $p1 = array(
1571 'contact_id' => $this->_individualId,
1572 'receive_date' => date('Ymd'),
1573 'total_amount' => 100.00,
1574 'financial_type_id' => $this->_financialTypeId,
1575 'non_deductible_amount' => 10.00,
1576 'contribution_status_id' => 1,
1577
1578 );
1579 $contribution1 = $this->callAPISuccess('contribution', 'create', $p1);
1580
1581 $p2 = array(
1582 'contact_id' => $this->_individualId,
1583 'receive_date' => date('Ymd'),
1584 'total_amount' => 200.00,
1585 'financial_type_id' => $this->_financialTypeId,
1586 'non_deductible_amount' => 20.00,
1587 'trxn_id' => 5454565,
1588 'invoice_id' => 1212124,
1589 'fee_amount' => 50.00,
1590 'net_amount' => 60.00,
1591 'contribution_status_id' => 2,
1592
1593 );
1594 $contribution2 = $this->callAPISuccess('contribution', 'create', $p2);
1595
1596 $params = array(
1597 'contribution_id' => $contribution2['id'],
1598
1599 );
1600 $result = $this->callAPISuccess('contribution', 'get', $params);
1601 $res = $result['values'][$contribution2['id']];
1602
1603 $this->assertEquals($p2['contact_id'], $res['contact_id']);
1604 $this->assertEquals($p2['total_amount'], $res['total_amount']);
1605 $this->assertEquals($p2['financial_type_id'], $res['financial_type_id']);
1606 $this->assertEquals($p2['net_amount'], $res['net_amount']);
1607 $this->assertEquals($p2['non_deductible_amount'], $res['non_deductible_amount']);
1608 $this->assertEquals($p2['fee_amount'], $res['fee_amount']);
1609 $this->assertEquals($p2['trxn_id'], $res['trxn_id']);
1610 $this->assertEquals($p2['invoice_id'], $res['invoice_id']);
1611 // contribution_status_id = 2 => Pending
1612 $this->assertEquals('Pending', $res['contribution_status']);
1613
1614 $this->contributionDelete($contribution1['id']);
1615 $this->contributionDelete($contribution2['id']);
1616 }
1617
1618 /**
1619 * Test completing a transaction via the API.
1620 *
1621 * Note that we are creating a logged in user because email goes out from
1622 * that person
1623 */
1624 public function testCompleteTransaction() {
1625 $mut = new CiviMailUtils($this, TRUE);
1626 $this->swapMessageTemplateForTestTemplate();
1627 $this->createLoggedInUser();
1628 $params = array_merge($this->_params, array('contribution_status_id' => 2));
1629 $contribution = $this->callAPISuccess('contribution', 'create', $params);
1630 $this->callAPISuccess('contribution', 'completetransaction', array(
1631 'id' => $contribution['id'],
1632 ));
1633 $contribution = $this->callAPISuccess('contribution', 'getsingle', array('id' => $contribution['id']));
1634 $this->assertEquals('SSF', $contribution['contribution_source']);
1635 $this->assertEquals('Completed', $contribution['contribution_status']);
1636 $this->assertEquals(date('Y-m-d'), date('Y-m-d', strtotime($contribution['receipt_date'])));
1637 $mut->checkMailLog(array(
1638 'email:::anthony_anderson@civicrm.org',
1639 'is_monetary:::1',
1640 'amount:::100.00',
1641 'currency:::USD',
1642 'receive_date:::' . date('Ymd', strtotime($contribution['receive_date'])),
1643 "receipt_date:::\n",
1644 'contributeMode:::notify',
1645 'title:::Contribution',
1646 'displayName:::Mr. Anthony Anderson II',
1647 ));
1648 $mut->stop();
1649 $this->revertTemplateToReservedTemplate();
1650 }
1651
1652 /**
1653 * Test to ensure mail is sent on chosing pay later
1654 */
1655 public function testpayLater() {
1656 $mut = new CiviMailUtils($this, TRUE);
1657 $this->swapMessageTemplateForTestTemplate();
1658 $this->createLoggedInUser();
1659
1660 // create contribution page first
1661 $contributionPageParams = array(
1662 'title' => 'Help Support CiviCRM!',
1663 'financial_type_id' => 1,
1664 'is_monetary' => TRUE,
1665 'is_pay_later' => 1,
1666 'is_quick_config' => TRUE,
1667 'pay_later_text' => 'I will send payment by check',
1668 'pay_later_receipt' => 'This is a pay later reciept',
1669 'is_allow_other_amount' => 1,
1670 'min_amount' => 10.00,
1671 'max_amount' => 10000.00,
1672 'goal_amount' => 100000.00,
1673 'is_email_receipt' => 1,
1674 'is_active' => 1,
1675 'amount_block_is_active' => 1,
1676 'currency' => 'USD',
1677 'is_billing_required' => 0,
1678 );
1679 $contributionPageResult = $this->callAPISuccess('contribution_page', 'create', $contributionPageParams);
1680
1681 // submit form values
1682 $priceSet = $this->callAPISuccess('price_set', 'getsingle', array('name' => 'default_contribution_amount'));
1683 $params = array(
1684 'id' => $contributionPageResult['id'],
1685 'contact_id' => $this->_individualId,
1686 'email-5' => 'anthony_anderson@civicrm.org',
1687 'payment_processor_id' => 0,
1688 'amount' => 100.00,
1689 'tax_amount' => '',
1690 'currencyID' => 'USD',
1691 'is_pay_later' => 1,
1692 'invoiceID' => 'f28e1ddc86f8c4a0ff5bcf46393e4bc8',
1693 'is_quick_config' => 1,
1694 'description' => 'Online Contribution: Help Support CiviCRM!',
1695 'price_set_id' => $priceSet['id'],
1696 );
1697 $this->callAPISuccess('contribution_page', 'submit', $params);
1698
1699 $mut->checkMailLog(array(
1700 'is_pay_later:::1',
1701 'email:::anthony_anderson@civicrm.org',
1702 'pay_later_receipt:::' . $contributionPageParams['pay_later_receipt'],
1703 'displayName:::Mr. Anthony Anderson II',
1704 'contributionPageId:::' . $contributionPageResult['id'],
1705 'title:::' . $contributionPageParams['title'],
1706 'amount:::' . $params['amount'],
1707 ));
1708 $mut->stop();
1709 $this->revertTemplateToReservedTemplate();
1710 }
1711
1712
1713 /**
1714 * Test to check whether contact billing address is used when no contribution address
1715 */
1716 public function testBillingAddress() {
1717 $mut = new CiviMailUtils($this, TRUE);
1718 $this->swapMessageTemplateForTestTemplate();
1719 $this->createLoggedInUser();
1720
1721 //Scenario 1: When Contact don't have any address
1722 $params = array_merge($this->_params, array('contribution_status_id' => 2));
1723 $contribution = $this->callAPISuccess('contribution', 'create', $params);
1724 $this->callAPISuccess('contribution', 'completetransaction', array(
1725 'id' => $contribution['id'],
1726 ));
1727 $mut->checkMailLog(array(
1728 'address:::',
1729 ));
1730
1731 // Scenario 2: Contribution using address
1732 $address = $this->callAPISuccess('address', 'create', array(
1733 'street_address' => 'contribution billing st',
1734 'location_type_id' => 2,
1735 'contact_id' => $this->_params['contact_id'],
1736 ));
1737 $params = array_merge($this->_params, array(
1738 'contribution_status_id' => 2,
1739 'address_id' => $address['id'],
1740 )
1741 );
1742 $contribution = $this->callAPISuccess('contribution', 'create', $params);
1743 $this->callAPISuccess('contribution', 'completetransaction', array(
1744 'id' => $contribution['id'],
1745 ));
1746 $mut->checkMailLog(array(
1747 'address:::contribution billing st',
1748 ));
1749
1750 // Scenario 3: Contribution wtth no address but contact has a billing address
1751 $this->callAPISuccess('address', 'create', array(
1752 'id' => $address['id'],
1753 'street_address' => 'is billing st',
1754 'contact_id' => $this->_params['contact_id'],
1755 ));
1756 $params = array_merge($this->_params, array('contribution_status_id' => 2));
1757 $contribution = $this->callAPISuccess('contribution', 'create', $params);
1758 $this->callAPISuccess('contribution', 'completetransaction', array(
1759 'id' => $contribution['id'],
1760 ));
1761 $mut->checkMailLog(array(
1762 'address:::is billing st',
1763 ));
1764
1765 $mut->stop();
1766 $this->revertTemplateToReservedTemplate();
1767 }
1768
1769 /**
1770 * Test completing a transaction via the API.
1771 *
1772 * Note that we are creating a logged in user because email goes out from
1773 * that person
1774 */
1775 public function testCompleteTransactionFeeAmount() {
1776 $this->createLoggedInUser();
1777 $params = array_merge($this->_params, array('contribution_status_id' => 2));
1778 $contribution = $this->callAPISuccess('contribution', 'create', $params);
1779 $this->callAPISuccess('contribution', 'completetransaction', array(
1780 'id' => $contribution['id'],
1781 'fee_amount' => '.56',
1782 'trxn_id' => '7778888',
1783 ));
1784 $contribution = $this->callAPISuccess('contribution', 'getsingle', array('id' => $contribution['id'], 'sequential' => 1));
1785 $this->assertEquals('Completed', $contribution['contribution_status']);
1786 $this->assertEquals('7778888', $contribution['trxn_id']);
1787 $this->assertEquals('.56', $contribution['fee_amount']);
1788 $this->assertEquals('99.44', $contribution['net_amount']);
1789 }
1790
1791 /**
1792 * CRM-19126 Add test to verify when complete transaction is called tax amount is not changed
1793 */
1794 public function testCheckTaxAmount() {
1795 $contact = $this->createLoggedInUser();
1796 $financialType = $this->callAPISuccess('financial_type', 'create', array(
1797 'name' => 'Test taxable financial Type',
1798 'is_reserved' => 0,
1799 'is_active' => 1,
1800 ));
1801 $financialAccount = $this->callAPISuccess('financial_account', 'create', array(
1802 'name' => 'Test Tax financial account ',
1803 'contact_id' => $contact,
1804 'financial_account_type_id' => 2,
1805 'is_tax' => 1,
1806 'tax_rate' => 5.00,
1807 'is_reserved' => 0,
1808 'is_active' => 1,
1809 'is_default' => 0,
1810 ));
1811 $financialTypeId = $financialType['id'];
1812 $financialAccountId = $financialAccount['id'];
1813 $financialAccountParams = array(
1814 'entity_table' => 'civicrm_financial_type',
1815 'entity_id' => $financialTypeId,
1816 'account_relationship' => 10,
1817 'financial_account_id' => $financialAccountId,
1818 );
1819 CRM_Financial_BAO_FinancialTypeAccount::add($financialAccountParams);
1820 $taxRates = CRM_Core_PseudoConstant::getTaxRates();
1821 $params = array_merge($this->_params, array('contribution_status_id' => 2, 'financial_type_id' => $financialTypeId));
1822 $contribution = $this->callAPISuccess('contribution', 'create', $params);
1823 $contribution1 = $this->callAPISuccess('contribution', 'get', array('id' => $contribution['id'], 'return' => 'tax_amount', 'sequential' => 1));
1824 $this->callAPISuccess('contribution', 'completetransaction', array(
1825 'id' => $contribution['id'],
1826 'trxn_id' => '777788888',
1827 'fee_amount' => '6.00',
1828 ));
1829 $contribution2 = $this->callAPISuccess('contribution', 'get', array('id' => $contribution['id'], 'return' => array('tax_amount', 'fee_amount', 'net_amount'), 'sequential' => 1));
1830 $this->assertEquals($contribution1['values'][0]['tax_amount'], $contribution2['values'][0]['tax_amount']);
1831 $this->assertEquals('6.00', $contribution2['values'][0]['fee_amount']);
1832 $this->assertEquals('99.00', $contribution2['values'][0]['net_amount']);
1833 }
1834
1835 /**
1836 * Test repeat contribution successfully creates line item.
1837 */
1838 public function testRepeatTransaction() {
1839 $originalContribution = $this->setUpRepeatTransaction($recurParams = array(), 'single');
1840 $this->callAPISuccess('contribution', 'repeattransaction', array(
1841 'original_contribution_id' => $originalContribution['id'],
1842 'contribution_status_id' => 'Completed',
1843 'trxn_id' => uniqid(),
1844 ));
1845 $lineItemParams = array(
1846 'entity_id' => $originalContribution['id'],
1847 'sequential' => 1,
1848 'return' => array(
1849 'entity_table',
1850 'qty',
1851 'unit_price',
1852 'line_total',
1853 'label',
1854 'financial_type_id',
1855 'deductible_amount',
1856 'price_field_value_id',
1857 'price_field_id',
1858 ),
1859 );
1860 $lineItem1 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
1861 'entity_id' => $originalContribution['id'],
1862 )));
1863 $lineItem2 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
1864 'entity_id' => $originalContribution['id'] + 1,
1865 )));
1866 unset($lineItem1['values'][0]['id'], $lineItem1['values'][0]['entity_id']);
1867 unset($lineItem2['values'][0]['id'], $lineItem2['values'][0]['entity_id']);
1868 $this->assertEquals($lineItem1['values'][0], $lineItem2['values'][0]);
1869 $this->_checkFinancialRecords(array('id' => $originalContribution['id'] + 1), 'online');
1870 $this->quickCleanUpFinancialEntities();
1871 }
1872
1873 /**
1874 * Test repeat contribution successfully creates line items (plural).
1875 */
1876 public function testRepeatTransactionLineItems() {
1877 // CRM-19309
1878 $originalContribution = $this->setUpRepeatTransaction($recurParams = array(), 'multiple');
1879 $this->callAPISuccess('contribution', 'repeattransaction', array(
1880 'original_contribution_id' => $originalContribution['id'],
1881 'contribution_status_id' => 'Completed',
1882 'trxn_id' => uniqid(),
1883 ));
1884
1885 $lineItemParams = array(
1886 'entity_id' => $originalContribution['id'],
1887 'sequential' => 1,
1888 'return' => array(
1889 'entity_table',
1890 'qty',
1891 'unit_price',
1892 'line_total',
1893 'label',
1894 'financial_type_id',
1895 'deductible_amount',
1896 'price_field_value_id',
1897 'price_field_id',
1898 ),
1899 );
1900 $lineItem1 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
1901 'entity_id' => $originalContribution['id'],
1902 )));
1903 $lineItem2 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
1904 'entity_id' => $originalContribution['id'] + 1,
1905 )));
1906
1907 // unset id and entity_id for all of them to be able to compare the lineItems:
1908 unset($lineItem1['values'][0]['id'], $lineItem1['values'][0]['entity_id']);
1909 unset($lineItem2['values'][0]['id'], $lineItem2['values'][0]['entity_id']);
1910 $this->assertEquals($lineItem1['values'][0], $lineItem2['values'][0]);
1911
1912 unset($lineItem1['values'][1]['id'], $lineItem1['values'][1]['entity_id']);
1913 unset($lineItem2['values'][1]['id'], $lineItem2['values'][1]['entity_id']);
1914 $this->assertEquals($lineItem1['values'][1], $lineItem2['values'][1]);
1915
1916 // CRM-19309 so in future we also want to:
1917 // check that financial_line_items have been created for entity_id 3 and 4;
1918
1919 $this->callAPISuccessGetCount('FinancialItem', array('description' => 'Sales Tax', 'amount' => 0), 0);
1920 $this->quickCleanUpFinancialEntities();
1921 }
1922
1923 /**
1924 * Test repeat contribution successfully creates is_test transaction.
1925 */
1926 public function testRepeatTransactionIsTest() {
1927 $this->_params['is_test'] = 1;
1928 $originalContribution = $this->setUpRepeatTransaction(array('is_test' => 1), 'single');
1929
1930 $this->callAPISuccess('contribution', 'repeattransaction', array(
1931 'original_contribution_id' => $originalContribution['id'],
1932 'contribution_status_id' => 'Completed',
1933 'trxn_id' => uniqid(),
1934 ));
1935 $this->callAPISuccessGetCount('Contribution', array('contribution_test' => 1), 2);
1936 }
1937
1938 /**
1939 * Test repeat contribution passed in status.
1940 */
1941 public function testRepeatTransactionPassedInStatus() {
1942 $originalContribution = $this->setUpRepeatTransaction($recurParams = array(), 'single');
1943
1944 $this->callAPISuccess('contribution', 'repeattransaction', array(
1945 'original_contribution_id' => $originalContribution['id'],
1946 'contribution_status_id' => 'Pending',
1947 'trxn_id' => uniqid(),
1948 ));
1949 $this->callAPISuccessGetCount('Contribution', array('contribution_status_id' => 2), 1);
1950 }
1951
1952 /**
1953 * Test repeat contribution accepts recur_id instead of original_contribution_id.
1954 */
1955 public function testRepeatTransactionAcceptRecurID() {
1956 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array(
1957 'contact_id' => $this->_individualId,
1958 'installments' => '12',
1959 'frequency_interval' => '1',
1960 'amount' => '100',
1961 'contribution_status_id' => 1,
1962 'start_date' => '2012-01-01 00:00:00',
1963 'currency' => 'USD',
1964 'frequency_unit' => 'month',
1965 'payment_processor_id' => $this->paymentProcessorID,
1966 ));
1967 $this->callAPISuccess('contribution', 'create', array_merge(
1968 $this->_params,
1969 array('contribution_recur_id' => $contributionRecur['id']))
1970 );
1971
1972 $this->callAPISuccess('contribution', 'repeattransaction', array(
1973 'contribution_recur_id' => $contributionRecur['id'],
1974 'contribution_status_id' => 'Completed',
1975 'trxn_id' => uniqid(),
1976 ));
1977
1978 $this->quickCleanUpFinancialEntities();
1979 }
1980
1981 /**
1982 * CRM-16397 test appropriate action if total amount has changed for single line items.
1983 */
1984 public function testRepeatTransactionAlteredAmount() {
1985 $paymentProcessorID = $this->paymentProcessorCreate();
1986 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array(
1987 'contact_id' => $this->_individualId,
1988 'installments' => '12',
1989 'frequency_interval' => '1',
1990 'amount' => '500',
1991 'contribution_status_id' => 1,
1992 'start_date' => '2012-01-01 00:00:00',
1993 'currency' => 'USD',
1994 'frequency_unit' => 'month',
1995 'payment_processor_id' => $paymentProcessorID,
1996 ));
1997 $originalContribution = $this->callAPISuccess('contribution', 'create', array_merge(
1998 $this->_params,
1999 array(
2000 'contribution_recur_id' => $contributionRecur['id'],
2001 ))
2002 );
2003
2004 $this->callAPISuccess('contribution', 'repeattransaction', array(
2005 'original_contribution_id' => $originalContribution['id'],
2006 'contribution_status_id' => 'Completed',
2007 'trxn_id' => uniqid(),
2008 'total_amount' => '400',
2009 'fee_amount' => 50,
2010 ));
2011
2012 $lineItemParams = array(
2013 'entity_id' => $originalContribution['id'],
2014 'sequential' => 1,
2015 'return' => array(
2016 'entity_table',
2017 'qty',
2018 'unit_price',
2019 'line_total',
2020 'label',
2021 'financial_type_id',
2022 'deductible_amount',
2023 'price_field_value_id',
2024 'price_field_id',
2025 ),
2026 );
2027 $this->callAPISuccessGetSingle('contribution', array(
2028 'total_amount' => 400,
2029 'fee_amount' => 50,
2030 'net_amount' => 350,
2031 ));
2032 $lineItem1 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
2033 'entity_id' => $originalContribution['id'],
2034 )));
2035 $expectedLineItem = array_merge(
2036 $lineItem1['values'][0], array(
2037 'line_total' => '400.00',
2038 'unit_price' => '400.00',
2039 )
2040 );
2041
2042 $lineItem2 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
2043 'entity_id' => $originalContribution['id'] + 1,
2044 )));
2045
2046 unset($expectedLineItem['id'], $expectedLineItem['entity_id']);
2047 unset($lineItem2['values'][0]['id'], $lineItem2['values'][0]['entity_id']);
2048 $this->assertEquals($expectedLineItem, $lineItem2['values'][0]);
2049 }
2050
2051 /**
2052 * CRM-17718 test appropriate action if financial type has changed for single line items.
2053 */
2054 public function testRepeatTransactionPassedInFinancialType() {
2055 $originalContribution = $this->setUpRecurringContribution();
2056
2057 $this->callAPISuccess('contribution', 'repeattransaction', array(
2058 'original_contribution_id' => $originalContribution['id'],
2059 'contribution_status_id' => 'Completed',
2060 'trxn_id' => uniqid(),
2061 'financial_type_id' => 2,
2062 ));
2063 $lineItemParams = array(
2064 'entity_id' => $originalContribution['id'],
2065 'sequential' => 1,
2066 'return' => array(
2067 'entity_table',
2068 'qty',
2069 'unit_price',
2070 'line_total',
2071 'label',
2072 'financial_type_id',
2073 'deductible_amount',
2074 'price_field_value_id',
2075 'price_field_id',
2076 ),
2077 );
2078
2079 $this->callAPISuccessGetSingle('contribution', array(
2080 'total_amount' => 100,
2081 'financial_type_id' => 2,
2082 ));
2083 $lineItem1 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
2084 'entity_id' => $originalContribution['id'],
2085 )));
2086 $expectedLineItem = array_merge(
2087 $lineItem1['values'][0], array(
2088 'line_total' => '100.00',
2089 'unit_price' => '100.00',
2090 'financial_type_id' => 2,
2091 'contribution_type_id' => 2,
2092 )
2093 );
2094 $lineItem2 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
2095 'entity_id' => $originalContribution['id'] + 1,
2096 )));
2097 unset($expectedLineItem['id'], $expectedLineItem['entity_id']);
2098 unset($lineItem2['values'][0]['id'], $lineItem2['values'][0]['entity_id']);
2099 $this->assertEquals($expectedLineItem, $lineItem2['values'][0]);
2100 }
2101
2102 /**
2103 * CRM-17718 test appropriate action if financial type has changed for single line items.
2104 */
2105 public function testRepeatTransactionUpdatedFinancialType() {
2106 $originalContribution = $this->setUpRecurringContribution(array(), array('financial_type_id' => 2));
2107
2108 $this->callAPISuccess('contribution', 'repeattransaction', array(
2109 'contribution_recur_id' => $originalContribution['id'],
2110 'contribution_status_id' => 'Completed',
2111 'trxn_id' => uniqid(),
2112 ));
2113 $lineItemParams = array(
2114 'entity_id' => $originalContribution['id'],
2115 'sequential' => 1,
2116 'return' => array(
2117 'entity_table',
2118 'qty',
2119 'unit_price',
2120 'line_total',
2121 'label',
2122 'financial_type_id',
2123 'deductible_amount',
2124 'price_field_value_id',
2125 'price_field_id',
2126 ),
2127 );
2128
2129 $this->callAPISuccessGetSingle('contribution', array(
2130 'total_amount' => 100,
2131 'financial_type_id' => 2,
2132 ));
2133 $lineItem1 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
2134 'entity_id' => $originalContribution['id'],
2135 )));
2136 $expectedLineItem = array_merge(
2137 $lineItem1['values'][0], array(
2138 'line_total' => '100.00',
2139 'unit_price' => '100.00',
2140 'financial_type_id' => 2,
2141 'contribution_type_id' => 2,
2142 )
2143 );
2144
2145 $lineItem2 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
2146 'entity_id' => $originalContribution['id'] + 1,
2147 )));
2148 unset($expectedLineItem['id'], $expectedLineItem['entity_id']);
2149 unset($lineItem2['values'][0]['id'], $lineItem2['values'][0]['entity_id']);
2150 $this->assertEquals($expectedLineItem, $lineItem2['values'][0]);
2151 }
2152
2153 /**
2154 * CRM-16397 test appropriate action if campaign has been passed in.
2155 */
2156 public function testRepeatTransactionPassedInCampaign() {
2157 $paymentProcessorID = $this->paymentProcessorCreate();
2158 $campaignID = $this->campaignCreate();
2159 $campaignID2 = $this->campaignCreate();
2160 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array(
2161 'contact_id' => $this->_individualId,
2162 'installments' => '12',
2163 'frequency_interval' => '1',
2164 'amount' => '100',
2165 'contribution_status_id' => 1,
2166 'start_date' => '2012-01-01 00:00:00',
2167 'currency' => 'USD',
2168 'frequency_unit' => 'month',
2169 'payment_processor_id' => $paymentProcessorID,
2170 ));
2171 $originalContribution = $this->callAPISuccess('contribution', 'create', array_merge(
2172 $this->_params,
2173 array(
2174 'contribution_recur_id' => $contributionRecur['id'],
2175 'campaign_id' => $campaignID,
2176 ))
2177 );
2178
2179 $this->callAPISuccess('contribution', 'repeattransaction', array(
2180 'original_contribution_id' => $originalContribution['id'],
2181 'contribution_status_id' => 'Completed',
2182 'trxn_id' => uniqid(),
2183 'campaign_id' => $campaignID2,
2184 ));
2185
2186 $this->callAPISuccessGetSingle('contribution', array(
2187 'total_amount' => 100,
2188 'campaign_id' => $campaignID2,
2189 ));
2190 }
2191
2192 /**
2193 * CRM-17718 campaign stored on contribution recur gets priority.
2194 *
2195 * This reflects the fact we permit people to update them.
2196 */
2197 public function testRepeatTransactionUpdatedCampaign() {
2198 $paymentProcessorID = $this->paymentProcessorCreate();
2199 $campaignID = $this->campaignCreate();
2200 $campaignID2 = $this->campaignCreate();
2201 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array(
2202 'contact_id' => $this->_individualId,
2203 'installments' => '12',
2204 'frequency_interval' => '1',
2205 'amount' => '100',
2206 'contribution_status_id' => 1,
2207 'start_date' => '2012-01-01 00:00:00',
2208 'currency' => 'USD',
2209 'frequency_unit' => 'month',
2210 'payment_processor_id' => $paymentProcessorID,
2211 'campaign_id' => $campaignID,
2212 ));
2213 $originalContribution = $this->callAPISuccess('contribution', 'create', array_merge(
2214 $this->_params,
2215 array(
2216 'contribution_recur_id' => $contributionRecur['id'],
2217 'campaign_id' => $campaignID2,
2218 ))
2219 );
2220
2221 $this->callAPISuccess('contribution', 'repeattransaction', array(
2222 'original_contribution_id' => $originalContribution['id'],
2223 'contribution_status_id' => 'Completed',
2224 'trxn_id' => uniqid(),
2225 ));
2226
2227 $this->callAPISuccessGetSingle('contribution', array(
2228 'total_amount' => 100,
2229 'campaign_id' => $campaignID,
2230 ));
2231 }
2232
2233 /**
2234 * Test completing a transaction does not 'mess' with net amount (CRM-15960).
2235 */
2236 public function testCompleteTransactionNetAmountOK() {
2237 $this->createLoggedInUser();
2238 $params = array_merge($this->_params, array('contribution_status_id' => 2));
2239 unset($params['net_amount']);
2240 $contribution = $this->callAPISuccess('contribution', 'create', $params);
2241 $this->callAPISuccess('contribution', 'completetransaction', array(
2242 'id' => $contribution['id'],
2243 ));
2244 $contribution = $this->callAPISuccess('contribution', 'getsingle', array('id' => $contribution['id']));
2245 $this->assertEquals('Completed', $contribution['contribution_status']);
2246 $this->assertTrue(($contribution['total_amount'] - $contribution['net_amount']) == $contribution['fee_amount']);
2247 }
2248
2249 /**
2250 * CRM-14151 - Test completing a transaction via the API.
2251 */
2252 public function testCompleteTransactionWithReceiptDateSet() {
2253 $this->swapMessageTemplateForTestTemplate();
2254 $mut = new CiviMailUtils($this, TRUE);
2255 $this->createLoggedInUser();
2256 $params = array_merge($this->_params, array('contribution_status_id' => 2, 'receipt_date' => 'now'));
2257 $contribution = $this->callAPISuccess('contribution', 'create', $params);
2258 $this->callAPISuccess('contribution', 'completetransaction', array('id' => $contribution['id'], 'trxn_date' => date('Y-m-d')));
2259 $contribution = $this->callAPISuccess('contribution', 'get', array('id' => $contribution['id'], 'sequential' => 1));
2260 $this->assertEquals('Completed', $contribution['values'][0]['contribution_status']);
2261 $this->assertEquals(date('Y-m-d'), date('Y-m-d', strtotime($contribution['values'][0]['receive_date'])));
2262 $mut->checkMailLog(array(
2263 'Receipt - Contribution',
2264 'receipt_date:::' . date('Ymd'),
2265 ));
2266 $mut->stop();
2267 $this->revertTemplateToReservedTemplate();
2268 }
2269
2270
2271 /**
2272 * Complete the transaction using the template with all the possible.
2273 */
2274 public function testCompleteTransactionWithTestTemplate() {
2275 $this->swapMessageTemplateForTestTemplate();
2276 $contribution = $this->setUpForCompleteTransaction();
2277 $this->callAPISuccess('contribution', 'completetransaction', array(
2278 'id' => $contribution['id'],
2279 'trxn_date' => date('2011-04-09'),
2280 'trxn_id' => 'kazam',
2281 ));
2282 $receive_date = $this->callAPISuccess('Contribution', 'getvalue', array('id' => $contribution['id'], 'return' => 'receive_date'));
2283 $this->mut->checkMailLog(array(
2284 'email:::anthony_anderson@civicrm.org',
2285 'is_monetary:::1',
2286 'amount:::100.00',
2287 'currency:::USD',
2288 'receive_date:::' . date('Ymd', strtotime($receive_date)),
2289 'receipt_date:::' . date('Ymd'),
2290 'contributeMode:::notify',
2291 'title:::Contribution',
2292 'displayName:::Mr. Anthony Anderson II',
2293 'trxn_id:::kazam',
2294 'contactID:::' . $this->_params['contact_id'],
2295 'contributionID:::' . $contribution['id'],
2296 'financialTypeId:::1',
2297 'financialTypeName:::Donation',
2298 ));
2299 $this->mut->stop();
2300 $this->revertTemplateToReservedTemplate();
2301 }
2302
2303 /**
2304 * Complete the transaction using the template with all the possible.
2305 */
2306 public function testCompleteTransactionContributionPageFromAddress() {
2307 $contributionPage = $this->callAPISuccess('ContributionPage', 'create', array(
2308 'receipt_from_name' => 'Mickey Mouse',
2309 'receipt_from_email' => 'mickey@mouse.com',
2310 'title' => "Test Contribution Page",
2311 'financial_type_id' => 1,
2312 'currency' => 'NZD',
2313 'goal_amount' => 50,
2314 'is_pay_later' => 1,
2315 'is_monetary' => TRUE,
2316 'is_email_receipt' => TRUE,
2317 ));
2318 $this->_params['contribution_page_id'] = $contributionPage['id'];
2319 $contribution = $this->setUpForCompleteTransaction();
2320 $this->callAPISuccess('contribution', 'completetransaction', array('id' => $contribution['id']));
2321 $this->mut->checkMailLog(array(
2322 'mickey@mouse.com',
2323 'Mickey Mouse <',
2324 ));
2325 $this->mut->stop();
2326 }
2327
2328 /**
2329 * Test completing first transaction in a recurring series.
2330 *
2331 * The status should be set to 'in progress' and the next scheduled payment date calculated.
2332 */
2333 public function testCompleteTransactionSetStatusToInProgress() {
2334 $paymentProcessorID = $this->paymentProcessorCreate();
2335 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array(
2336 'contact_id' => $this->_individualId,
2337 'installments' => '12',
2338 'frequency_interval' => '1',
2339 'amount' => '500',
2340 'contribution_status_id' => 'Pending',
2341 'start_date' => '2012-01-01 00:00:00',
2342 'currency' => 'USD',
2343 'frequency_unit' => 'month',
2344 'payment_processor_id' => $paymentProcessorID,
2345 ));
2346 $contribution = $this->callAPISuccess('contribution', 'create', array_merge(
2347 $this->_params,
2348 array(
2349 'contribution_recur_id' => $contributionRecur['id'],
2350 'contribution_status_id' => 'Pending',
2351 ))
2352 );
2353 $this->callAPISuccess('Contribution', 'completetransaction', array('id' => $contribution));
2354 $contributionRecur = $this->callAPISuccessGetSingle('ContributionRecur', array(
2355 'id' => $contributionRecur['id'],
2356 'return' => array('next_sched_contribution_date', 'contribution_status_id'),
2357 ));
2358 $this->assertEquals(5, $contributionRecur['contribution_status_id']);
2359 $this->assertEquals(date('Y-m-d 00:00:00', strtotime('+1 month')), $contributionRecur['next_sched_contribution_date']);
2360 }
2361
2362 /**
2363 * Test completing a pledge with the completeTransaction api..
2364 *
2365 * Note that we are creating a logged in user because email goes out from
2366 * that person.
2367 */
2368 public function testCompleteTransactionUpdatePledgePayment() {
2369 $this->swapMessageTemplateForTestTemplate();
2370 $mut = new CiviMailUtils($this, TRUE);
2371 $mut->clearMessages();
2372 $this->createLoggedInUser();
2373 $contributionID = $this->createPendingPledgeContribution();
2374 $this->callAPISuccess('contribution', 'completetransaction', array(
2375 'id' => $contributionID,
2376 'trxn_date' => '1 Feb 2013',
2377 ));
2378 $pledge = $this->callAPISuccessGetSingle('Pledge', array(
2379 'id' => $this->_ids['pledge'],
2380 ));
2381 $this->assertEquals('Completed', $pledge['pledge_status']);
2382
2383 $status = $this->callAPISuccessGetValue('PledgePayment', array(
2384 'pledge_id' => $this->_ids['pledge'],
2385 'return' => 'status_id',
2386 ));
2387 $this->assertEquals(1, $status);
2388 $mut->checkMailLog(array(
2389 'amount:::500.00',
2390 'receive_date:::20130201000000',
2391 "receipt_date:::\n",
2392 ));
2393 $mut->stop();
2394 $this->revertTemplateToReservedTemplate();
2395 }
2396
2397 /**
2398 * Test completing a transaction with an event via the API.
2399 *
2400 * Note that we are creating a logged in user because email goes out from
2401 * that person
2402 */
2403 public function testCompleteTransactionWithParticipantRecord() {
2404 $mut = new CiviMailUtils($this, TRUE);
2405 $mut->clearMessages();
2406 $this->createLoggedInUser();
2407 $contributionID = $this->createPendingParticipantContribution();
2408 $this->callAPISuccess('contribution', 'completetransaction', array(
2409 'id' => $contributionID,
2410 )
2411 );
2412 $participantStatus = $this->callAPISuccessGetValue('participant', array(
2413 'id' => $this->_ids['participant'],
2414 'return' => 'participant_status_id',
2415 ));
2416 $this->assertEquals(1, $participantStatus);
2417 $mut->checkMailLog(array(
2418 'Annual CiviCRM meet',
2419 'Event',
2420 'This letter is a confirmation that your registration has been received and your status has been updated to Registered.',
2421 ));
2422 $mut->stop();
2423 }
2424
2425 /**
2426 * Test membership is renewed when transaction completed.
2427 */
2428 public function testCompleteTransactionMembershipPriceSet() {
2429 $this->createPriceSetWithPage('membership');
2430 $stateOfGrace = $this->callAPISuccess('MembershipStatus', 'getvalue', array(
2431 'name' => 'Grace',
2432 'return' => 'id')
2433 );
2434 $this->setUpPendingContribution($this->_ids['price_field_value'][0]);
2435 $membership = $this->callAPISuccess('membership', 'getsingle', array('id' => $this->_ids['membership']));
2436 $logs = $this->callAPISuccess('MembershipLog', 'get', array(
2437 'membership_id' => $this->_ids['membership'],
2438 ));
2439 $this->assertEquals(1, $logs['count']);
2440 $this->assertEquals($stateOfGrace, $membership['status_id']);
2441 $this->callAPISuccess('contribution', 'completetransaction', array('id' => $this->_ids['contribution']));
2442 $membership = $this->callAPISuccess('membership', 'getsingle', array('id' => $this->_ids['membership']));
2443 $this->assertEquals(date('Y-m-d', strtotime('yesterday + 1 year')), $membership['end_date']);
2444 $this->callAPISuccessGetSingle('LineItem', array(
2445 'entity_id' => $this->_ids['membership'],
2446 'entity_table' => 'civicrm_membership',
2447 ));
2448 $logs = $this->callAPISuccess('MembershipLog', 'get', array(
2449 'membership_id' => $this->_ids['membership'],
2450 ));
2451 $this->assertEquals(2, $logs['count']);
2452 $this->assertNotEquals($stateOfGrace, $logs['values'][2]['status_id']);
2453 $this->cleanUpAfterPriceSets();
2454 }
2455
2456 /**
2457 * Test membership is renewed when transaction completed.
2458 */
2459 public function testCompleteTransactionMembershipPriceSetTwoTerms() {
2460 $this->createPriceSetWithPage('membership');
2461 $this->setUpPendingContribution($this->_ids['price_field_value'][1]);
2462 $this->callAPISuccess('contribution', 'completetransaction', array('id' => $this->_ids['contribution']));
2463 $membership = $this->callAPISuccess('membership', 'getsingle', array('id' => $this->_ids['membership']));
2464 $this->assertEquals(date('Y-m-d', strtotime('yesterday + 2 years')), $membership['end_date']);
2465 $this->cleanUpAfterPriceSets();
2466 }
2467
2468 public function cleanUpAfterPriceSets() {
2469 $this->quickCleanUpFinancialEntities();
2470 $this->contactDelete($this->_ids['contact']);
2471 }
2472
2473
2474 /**
2475 * Create price set with contribution test for test setup.
2476 *
2477 * This could be merged with 4.5 function setup in api_v3_ContributionPageTest::setUpContributionPage
2478 * on parent class at some point (fn is not in 4.4).
2479 *
2480 * @param $entity
2481 * @param array $params
2482 */
2483 public function createPriceSetWithPage($entity, $params = array()) {
2484 $membershipTypeID = $this->membershipTypeCreate();
2485 $contributionPageResult = $this->callAPISuccess('contribution_page', 'create', array(
2486 'title' => "Test Contribution Page",
2487 'financial_type_id' => 1,
2488 'currency' => 'NZD',
2489 'goal_amount' => 50,
2490 'is_pay_later' => 1,
2491 'is_monetary' => TRUE,
2492 'is_email_receipt' => FALSE,
2493 ));
2494 $priceSet = $this->callAPISuccess('price_set', 'create', array(
2495 'is_quick_config' => 0,
2496 'extends' => 'CiviMember',
2497 'financial_type_id' => 1,
2498 'title' => 'my Page',
2499 ));
2500 $priceSetID = $priceSet['id'];
2501
2502 CRM_Price_BAO_PriceSet::addTo('civicrm_contribution_page', $contributionPageResult['id'], $priceSetID);
2503 $priceField = $this->callAPISuccess('price_field', 'create', array(
2504 'price_set_id' => $priceSetID,
2505 'label' => 'Goat Breed',
2506 'html_type' => 'Radio',
2507 ));
2508 $priceFieldValue = $this->callAPISuccess('price_field_value', 'create', array(
2509 'price_set_id' => $priceSetID,
2510 'price_field_id' => $priceField['id'],
2511 'label' => 'Long Haired Goat',
2512 'amount' => 20,
2513 'financial_type_id' => 'Donation',
2514 'membership_type_id' => $membershipTypeID,
2515 'membership_num_terms' => 1,
2516 )
2517 );
2518 $this->_ids['price_field_value'] = array($priceFieldValue['id']);
2519 $priceFieldValue = $this->callAPISuccess('price_field_value', 'create', array(
2520 'price_set_id' => $priceSetID,
2521 'price_field_id' => $priceField['id'],
2522 'label' => 'Shoe-eating Goat',
2523 'amount' => 10,
2524 'financial_type_id' => 'Donation',
2525 'membership_type_id' => $membershipTypeID,
2526 'membership_num_terms' => 2,
2527 )
2528 );
2529 $this->_ids['price_field_value'][] = $priceFieldValue['id'];
2530 $this->_ids['price_set'] = $priceSetID;
2531 $this->_ids['contribution_page'] = $contributionPageResult['id'];
2532 $this->_ids['price_field'] = array($priceField['id']);
2533
2534 $this->_ids['membership_type'] = $membershipTypeID;
2535 }
2536
2537 /**
2538 * Set up a pending transaction with a specific price field id.
2539 *
2540 * @param int $priceFieldValueID
2541 */
2542 public function setUpPendingContribution($priceFieldValueID) {
2543 $contactID = $this->individualCreate();
2544 $membership = $this->callAPISuccess('membership', 'create', array(
2545 'contact_id' => $contactID,
2546 'membership_type_id' => $this->_ids['membership_type'],
2547 'start_date' => 'yesterday - 1 year',
2548 'end_date' => 'yesterday',
2549 'join_date' => 'yesterday - 1 year',
2550 ));
2551 $contribution = $this->callAPISuccess('contribution', 'create', array(
2552 'domain_id' => 1,
2553 'contact_id' => $contactID,
2554 'receive_date' => date('Ymd'),
2555 'total_amount' => 100.00,
2556 'financial_type_id' => 1,
2557 'payment_instrument_id' => 'Credit Card',
2558 'non_deductible_amount' => 10.00,
2559 'trxn_id' => 'jdhfi88',
2560 'invoice_id' => 'djfhiewuyr',
2561 'source' => 'SSF',
2562 'contribution_status_id' => 2,
2563 'contribution_page_id' => $this->_ids['contribution_page'],
2564 'api.membership_payment.create' => array('membership_id' => $membership['id']),
2565 ));
2566
2567 $this->callAPISuccess('line_item', 'create', array(
2568 'entity_id' => $contribution['id'],
2569 'entity_table' => 'civicrm_contribution',
2570 'contribution_id' => $contribution['id'],
2571 'price_field_id' => $this->_ids['price_field'][0],
2572 'qty' => 1,
2573 'unit_price' => 20,
2574 'line_total' => 20,
2575 'financial_type_id' => 1,
2576 'price_field_value_id' => $priceFieldValueID,
2577 ));
2578 $this->_ids['contact'] = $contactID;
2579 $this->_ids['contribution'] = $contribution['id'];
2580 $this->_ids['membership'] = $membership['id'];
2581 }
2582
2583 /**
2584 * Test sending a mail via the API.
2585 */
2586 public function testSendMail() {
2587 $mut = new CiviMailUtils($this, TRUE);
2588 $contribution = $this->callAPISuccess('contribution', 'create', $this->_params);
2589 $this->callAPISuccess('contribution', 'sendconfirmation', array(
2590 'id' => $contribution['id'],
2591 'receipt_from_email' => 'api@civicrm.org',
2592 )
2593 );
2594 $mut->checkMailLog(array(
2595 '$ 100.00',
2596 'Contribution Information',
2597 'Please print this confirmation for your records',
2598 ), array(
2599 'Event',
2600 )
2601 );
2602
2603 $this->checkCreditCardDetails($mut, $contribution['id']);
2604 $mut->stop();
2605 }
2606
2607 /**
2608 * Check credit card details in sent mail via API
2609 *
2610 * @param $mut obj CiviMailUtils instance
2611 * @param int $contributionID Contribution ID
2612 *
2613 */
2614 public function checkCreditCardDetails($mut, $contributionID) {
2615 $contribution = $this->callAPISuccess('contribution', 'create', $this->_params);
2616 $this->callAPISuccess('contribution', 'sendconfirmation', array(
2617 'id' => $contributionID,
2618 'receipt_from_email' => 'api@civicrm.org',
2619 'payment_processor_id' => $this->paymentProcessorID,
2620 )
2621 );
2622 $mut->checkMailLog(array(
2623 'Credit Card Information', // credit card header
2624 'Billing Name and Address', // billing header
2625 'anthony_anderson@civicrm.org', // billing name
2626 ), array(
2627 'Event',
2628 )
2629 );
2630 }
2631
2632 /**
2633 * Test sending a mail via the API.
2634 */
2635 public function testSendMailEvent() {
2636 $mut = new CiviMailUtils($this, TRUE);
2637 $contribution = $this->callAPISuccess('contribution', 'create', $this->_params);
2638 $event = $this->eventCreate(array(
2639 'is_email_confirm' => 1,
2640 'confirm_from_email' => 'test@civicrm.org',
2641 ));
2642 $this->_eventID = $event['id'];
2643 $participantParams = array(
2644 'contact_id' => $this->_individualId,
2645 'event_id' => $this->_eventID,
2646 'status_id' => 1,
2647 'role_id' => 1,
2648 // to ensure it matches later on
2649 'register_date' => '2007-07-21 00:00:00',
2650 'source' => 'Online Event Registration: API Testing',
2651
2652 );
2653 $participant = $this->callAPISuccess('participant', 'create', $participantParams);
2654 $this->callAPISuccess('participant_payment', 'create', array(
2655 'participant_id' => $participant['id'],
2656 'contribution_id' => $contribution['id'],
2657 ));
2658 $this->callAPISuccess('contribution', 'sendconfirmation', array(
2659 'id' => $contribution['id'],
2660 'receipt_from_email' => 'api@civicrm.org',
2661 )
2662 );
2663
2664 $mut->checkMailLog(array(
2665 'Annual CiviCRM meet',
2666 'Event',
2667 'To: "Mr. Anthony Anderson II" <anthony_anderson@civicrm.org>',
2668 ), array()
2669 );
2670 $mut->stop();
2671 }
2672
2673 /**
2674 * This function does a GET & compares the result against the $params.
2675 *
2676 * Use as a double check on Creates.
2677 *
2678 * @param array $params
2679 * @param int $id
2680 * @param bool $delete
2681 */
2682 public function contributionGetnCheck($params, $id, $delete = TRUE) {
2683
2684 $contribution = $this->callAPISuccess('Contribution', 'Get', array(
2685 'id' => $id,
2686
2687 ));
2688
2689 if ($delete) {
2690 $this->callAPISuccess('contribution', 'delete', array('id' => $id));
2691 }
2692 $this->assertAPISuccess($contribution, 0);
2693 $values = $contribution['values'][$contribution['id']];
2694 $params['receive_date'] = date('Y-m-d H:i:s', strtotime($params['receive_date']));
2695 // this is not returned in id format
2696 unset($params['payment_instrument_id']);
2697 $params['contribution_source'] = $params['source'];
2698 unset($params['source']);
2699 foreach ($params as $key => $value) {
2700 $this->assertEquals($value, $values[$key], $key . " value: $value doesn't match " . print_r($values, TRUE));
2701 }
2702 }
2703
2704 /**
2705 * Create a pending contribution & linked pending pledge record.
2706 */
2707 public function createPendingPledgeContribution() {
2708
2709 $pledgeID = $this->pledgeCreate(array('contact_id' => $this->_individualId, 'installments' => 1, 'amount' => 500));
2710 $this->_ids['pledge'] = $pledgeID;
2711 $contribution = $this->callAPISuccess('contribution', 'create', array_merge($this->_params, array(
2712 'contribution_status_id' => 'Pending',
2713 'total_amount' => 500,
2714 ))
2715 );
2716 $paymentID = $this->callAPISuccessGetValue('PledgePayment', array(
2717 'options' => array('limit' => 1),
2718 'return' => 'id',
2719 ));
2720 $this->callAPISuccess('PledgePayment', 'create', array(
2721 'id' => $paymentID,
2722 'contribution_id' =>
2723 $contribution['id'],
2724 'status_id' => 'Pending',
2725 'scheduled_amount' => 500,
2726 ));
2727
2728 return $contribution['id'];
2729 }
2730
2731 /**
2732 * Create a pending contribution & linked pending participant record (along with an event).
2733 */
2734 public function createPendingParticipantContribution() {
2735 $event = $this->eventCreate(array('is_email_confirm' => 1, 'confirm_from_email' => 'test@civicrm.org'));
2736 $participantID = $this->participantCreate(array('event_id' => $event['id'], 'status_id' => 6));
2737 $this->_ids['participant'] = $participantID;
2738 $params = array_merge($this->_params, array('contribution_status_id' => 2, 'financial_type_id' => 'Event Fee'));
2739 $contribution = $this->callAPISuccess('contribution', 'create', $params);
2740 $this->callAPISuccess('participant_payment', 'create', array(
2741 'contribution_id' => $contribution['id'],
2742 'participant_id' => $participantID,
2743 ));
2744 $this->callAPISuccess('line_item', 'get', array(
2745 'entity_id' => $contribution['id'],
2746 'entity_table' => 'civicrm_contribution',
2747 'api.line_item.create' => array(
2748 'entity_id' => $participantID,
2749 'entity_table' => 'civicrm_participant',
2750 ),
2751 ));
2752 return $contribution['id'];
2753 }
2754
2755 /**
2756 * Get financial transaction amount.
2757 *
2758 * @param int $contId
2759 *
2760 * @return null|string
2761 */
2762 public function _getFinancialTrxnAmount($contId) {
2763 $query = "SELECT
2764 SUM( ft.total_amount ) AS total
2765 FROM civicrm_financial_trxn AS ft
2766 LEFT JOIN civicrm_entity_financial_trxn AS ceft ON ft.id = ceft.financial_trxn_id
2767 WHERE ceft.entity_table = 'civicrm_contribution'
2768 AND ceft.entity_id = {$contId}";
2769
2770 $result = CRM_Core_DAO::singleValueQuery($query);
2771 return $result;
2772 }
2773
2774 /**
2775 * @param int $contId
2776 *
2777 * @return null|string
2778 */
2779 public function _getFinancialItemAmount($contId) {
2780 $lineItem = key(CRM_Price_BAO_LineItem::getLineItems($contId, 'contribution'));
2781 $query = "SELECT
2782 SUM(amount)
2783 FROM civicrm_financial_item
2784 WHERE entity_table = 'civicrm_line_item'
2785 AND entity_id = {$lineItem}";
2786 $result = CRM_Core_DAO::singleValueQuery($query);
2787 return $result;
2788 }
2789
2790 /**
2791 * @param int $contId
2792 * @param $context
2793 */
2794 public function _checkFinancialItem($contId, $context) {
2795 if ($context != 'paylater') {
2796 $params = array(
2797 'entity_id' => $contId,
2798 'entity_table' => 'civicrm_contribution',
2799 );
2800 $trxn = current(CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($params, TRUE));
2801 $entityParams = array(
2802 'financial_trxn_id' => $trxn['financial_trxn_id'],
2803 'entity_table' => 'civicrm_financial_item',
2804 );
2805 $entityTrxn = current(CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($entityParams));
2806 $params = array(
2807 'id' => $entityTrxn['entity_id'],
2808 );
2809 }
2810 if ($context == 'paylater') {
2811 $lineItems = CRM_Price_BAO_LineItem::getLineItems($contId, 'contribution');
2812 foreach ($lineItems as $key => $item) {
2813 $params = array(
2814 'entity_id' => $key,
2815 'entity_table' => 'civicrm_line_item',
2816 );
2817 $compareParams = array('status_id' => 1);
2818 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $params, $compareParams);
2819 }
2820 }
2821 elseif ($context == 'refund') {
2822 $compareParams = array(
2823 'status_id' => 1,
2824 'financial_account_id' => 1,
2825 'amount' => -100,
2826 );
2827 }
2828 elseif ($context == 'cancelPending') {
2829 $compareParams = array(
2830 'status_id' => 3,
2831 'financial_account_id' => 1,
2832 'amount' => -100,
2833 );
2834 }
2835 elseif ($context == 'changeFinancial') {
2836 $lineKey = key(CRM_Price_BAO_LineItem::getLineItems($contId, 'contribution'));
2837 $params = array(
2838 'entity_id' => $lineKey,
2839 'amount' => -100,
2840 );
2841 $compareParams = array(
2842 'financial_account_id' => 1,
2843 );
2844 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $params, $compareParams);
2845 $params = array(
2846 'financial_account_id' => 3,
2847 'entity_id' => $lineKey,
2848 );
2849 $compareParams = array(
2850 'amount' => 100,
2851 );
2852 }
2853 if ($context != 'paylater') {
2854 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $params, $compareParams);
2855 }
2856 }
2857
2858 /**
2859 * Check financial transaction.
2860 *
2861 * @todo break this down into sensible functions - most calls to it only use a few lines out of the big if.
2862 *
2863 * @param array $contribution
2864 * @param string $context
2865 * @param int $instrumentId
2866 * @param array $extraParams
2867 */
2868 public function _checkFinancialTrxn($contribution, $context, $instrumentId = NULL, $extraParams = array()) {
2869 $trxnParams = array(
2870 'entity_id' => $contribution['id'],
2871 'entity_table' => 'civicrm_contribution',
2872 );
2873 $trxn = current(CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($trxnParams, TRUE));
2874 $params = array(
2875 'id' => $trxn['financial_trxn_id'],
2876 );
2877 if ($context == 'payLater') {
2878 $relationTypeId = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Accounts Receivable Account is' "));
2879 $compareParams = array(
2880 'status_id' => 1,
2881 'from_financial_account_id' => CRM_Contribute_PseudoConstant::financialAccountType($contribution['financial_type_id'], $relationTypeId),
2882 );
2883 }
2884 elseif ($context == 'refund') {
2885 $compareParams = array(
2886 'to_financial_account_id' => 6,
2887 'total_amount' => -100,
2888 'status_id' => 7,
2889 'trxn_date' => '2015-01-01 09:00:00',
2890 'trxn_id' => 'the refund',
2891 );
2892 }
2893 elseif ($context == 'cancelPending') {
2894 $compareParams = array(
2895 'to_financial_account_id' => 7,
2896 'total_amount' => -100,
2897 'status_id' => 3,
2898 );
2899 }
2900 elseif ($context == 'changeFinancial' || $context == 'paymentInstrument') {
2901 $entityParams = array(
2902 'entity_id' => $contribution['id'],
2903 'entity_table' => 'civicrm_contribution',
2904 'amount' => -100,
2905 );
2906 $trxn = current(CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($entityParams));
2907 $trxnParams1 = array(
2908 'id' => $trxn['financial_trxn_id'],
2909 );
2910 if (empty($extraParams)) {
2911 $compareParams = array(
2912 'total_amount' => -100,
2913 'status_id' => 1,
2914 );
2915 }
2916 else {
2917 $compareParams = array(
2918 'total_amount' => 100,
2919 'status_id' => 1,
2920 );
2921 }
2922 if ($context == 'paymentInstrument') {
2923 $compareParams += array(
2924 'to_financial_account_id' => CRM_Financial_BAO_FinancialTypeAccount::getInstrumentFinancialAccount(4),
2925 'payment_instrument_id' => 4,
2926 );
2927 }
2928 else {
2929 $compareParams['to_financial_account_id'] = 12;
2930 }
2931 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialTrxn', $trxnParams1, array_merge($compareParams, $extraParams));
2932 $compareParams['total_amount'] = 100;
2933 if ($context == 'paymentInstrument') {
2934 $compareParams['to_financial_account_id'] = CRM_Financial_BAO_FinancialTypeAccount::getInstrumentFinancialAccount($instrumentId);
2935 $compareParams['payment_instrument_id'] = $instrumentId;
2936 }
2937 else {
2938 $compareParams['to_financial_account_id'] = 12;
2939 }
2940 }
2941
2942 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialTrxn', $params, array_merge($compareParams, $extraParams));
2943 }
2944
2945 /**
2946 * @return mixed
2947 */
2948 public function _addPaymentInstrument() {
2949 $gId = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionGroup', 'payment_instrument', 'id', 'name');
2950 $optionParams = array(
2951 'option_group_id' => $gId,
2952 'label' => 'Test Card',
2953 'name' => 'Test Card',
2954 'value' => '6',
2955 'weight' => '6',
2956 'is_active' => 1,
2957 );
2958 $optionValue = $this->callAPISuccess('option_value', 'create', $optionParams);
2959 $relationTypeId = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Asset Account is' "));
2960 $financialParams = array(
2961 'entity_table' => 'civicrm_option_value',
2962 'entity_id' => $optionValue['id'],
2963 'account_relationship' => $relationTypeId,
2964 'financial_account_id' => 7,
2965 );
2966 CRM_Financial_BAO_FinancialTypeAccount::add($financialParams, CRM_Core_DAO::$_nullArray);
2967 $this->assertNotEmpty($optionValue['values'][$optionValue['id']]['value']);
2968 return $optionValue['values'][$optionValue['id']]['value'];
2969 }
2970
2971 /**
2972 * Set up the basic recurring contribution for tests.
2973 *
2974 * @param array $generalParams
2975 * Parameters that can be merged into the recurring AND the contribution.
2976 *
2977 * @param array $recurParams
2978 * Parameters to merge into the recur only.
2979 *
2980 * @return array|int
2981 */
2982 protected function setUpRecurringContribution($generalParams = array(), $recurParams = array()) {
2983 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array_merge(array(
2984 'contact_id' => $this->_individualId,
2985 'installments' => '12',
2986 'frequency_interval' => '1',
2987 'amount' => '100',
2988 'contribution_status_id' => 1,
2989 'start_date' => '2012-01-01 00:00:00',
2990 'currency' => 'USD',
2991 'frequency_unit' => 'month',
2992 'payment_processor_id' => $this->paymentProcessorID,
2993 ), $generalParams, $recurParams));
2994 $originalContribution = $this->callAPISuccess('contribution', 'create', array_merge(
2995 $this->_params,
2996 array(
2997 'contribution_recur_id' => $contributionRecur['id'],
2998 ), $generalParams)
2999 );
3000 return $originalContribution;
3001 }
3002
3003 /**
3004 * Set up a repeat transaction.
3005 *
3006 * @param array $recurParams
3007 *
3008 * @return array
3009 */
3010 protected function setUpRepeatTransaction($recurParams = array(), $flag) {
3011 $paymentProcessorID = $this->paymentProcessorCreate();
3012 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array_merge(array(
3013 'contact_id' => $this->_individualId,
3014 'installments' => '12',
3015 'frequency_interval' => '1',
3016 'amount' => '500',
3017 'contribution_status_id' => 1,
3018 'start_date' => '2012-01-01 00:00:00',
3019 'currency' => 'USD',
3020 'frequency_unit' => 'month',
3021 'payment_processor_id' => $paymentProcessorID,
3022 ), $recurParams));
3023
3024 $originalContribution = '';
3025 if ($flag == 'multiple') {
3026 // CRM-19309 create a contribution + also add in line_items (plural):
3027 $originalContribution = $this->callAPISuccess('contribution', 'create', array_merge(
3028 $this->_params,
3029 array(
3030 'contribution_recur_id' => $contributionRecur['id'],
3031 'skipLineItem' => 1,
3032 'api.line_item.create' => array(
3033 array(
3034 'price_field_id' => 1,
3035 'qty' => 2,
3036 'line_total' => '20',
3037 'unit_price' => '10',
3038 'financial_type_id' => 1,
3039 ),
3040 array(
3041 'price_field_id' => 1,
3042 'qty' => 1,
3043 'line_total' => '80',
3044 'unit_price' => '80',
3045 'financial_type_id' => 2,
3046 ),
3047 ),
3048 )
3049 )
3050 );
3051 }
3052 elseif ($flag == 'single') {
3053 $originalContribution = $this->callAPISuccess('contribution', 'create', array_merge(
3054 $this->_params,
3055 array('contribution_recur_id' => $contributionRecur['id']))
3056 );
3057 }
3058
3059 return $originalContribution;
3060 }
3061
3062 /**
3063 * Common set up routine.
3064 *
3065 * @return array
3066 */
3067 protected function setUpForCompleteTransaction() {
3068 $this->mut = new CiviMailUtils($this, TRUE);
3069 $this->createLoggedInUser();
3070 $params = array_merge($this->_params, array('contribution_status_id' => 2, 'receipt_date' => 'now'));
3071 $contribution = $this->callAPISuccess('contribution', 'create', $params);
3072 return $contribution;
3073 }
3074
3075 }