Add Unit Tests For getunique API get
[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-2018 |
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 $financialAccounts = $this->callAPISuccess('FinancialAccount', 'get', array());
126 foreach ($financialAccounts['values'] as $financialAccount) {
127 if ($financialAccount['name'] == 'Test Tax financial account ' || $financialAccount['name'] == 'Test taxable financial Type') {
128 $entityFinancialTypes = $this->callAPISuccess('EntityFinancialAccount', 'get', array(
129 'financial_account_id' => $financialAccount['id'],
130 ));
131 foreach ($entityFinancialTypes['values'] as $entityFinancialType) {
132 $this->callAPISuccess('EntityFinancialAccount', 'delete', array('id' => $entityFinancialType['id']));
133 }
134 $this->callAPISuccess('FinancialAccount', 'delete', array('id' => $financialAccount['id']));
135 }
136 }
137 }
138
139 /**
140 * Test Get.
141 */
142 public function testGetContribution() {
143 $contributionSettings = $this->enableTaxAndInvoicing();
144 $invoice_prefix = CRM_Contribute_BAO_Contribution::checkContributeSettings('invoice_prefix', TRUE);
145 $p = array(
146 'contact_id' => $this->_individualId,
147 'receive_date' => '2010-01-20',
148 'total_amount' => 100.00,
149 'financial_type_id' => $this->_financialTypeId,
150 'non_deductible_amount' => 10.00,
151 'fee_amount' => 5.00,
152 'net_amount' => 95.00,
153 'trxn_id' => 23456,
154 'invoice_id' => 78910,
155 'source' => 'SSF',
156 'contribution_status_id' => 1,
157 );
158 $this->_contribution = $this->callAPISuccess('contribution', 'create', $p);
159
160 $params = array(
161 'contribution_id' => $this->_contribution['id'],
162 );
163
164 $contributions = $this->callAPIAndDocument('contribution', 'get', $params, __FUNCTION__, __FILE__);
165 $financialParams['id'] = $this->_financialTypeId;
166 $default = NULL;
167 CRM_Financial_BAO_FinancialType::retrieve($financialParams, $default);
168
169 $this->assertEquals(1, $contributions['count']);
170 $contribution = $contributions['values'][$contributions['id']];
171 $this->assertEquals($contribution['contact_id'], $this->_individualId);
172 // Note there was an assertion converting financial_type_id to 'Donation' which wasn't working.
173 // Passing back a string rather than an id seems like an error/cruft.
174 // If it is to be introduced we should discuss.
175 $this->assertEquals($contribution['financial_type_id'], 1);
176 $this->assertEquals($contribution['total_amount'], 100.00);
177 $this->assertEquals($contribution['non_deductible_amount'], 10.00);
178 $this->assertEquals($contribution['fee_amount'], 5.00);
179 $this->assertEquals($contribution['net_amount'], 95.00);
180 $this->assertEquals($contribution['trxn_id'], 23456);
181 $this->assertEquals($contribution['invoice_id'], 78910);
182 $this->assertEquals($contribution['invoice_number'], $invoice_prefix . $contributions['id']);
183 $this->assertEquals($contribution['contribution_source'], 'SSF');
184 $this->assertEquals($contribution['contribution_status'], 'Completed');
185 // Create a second contribution - we are testing that 'id' gets the right contribution id (not the contact id).
186 $p['trxn_id'] = '3847';
187 $p['invoice_id'] = '3847';
188
189 $contribution2 = $this->callAPISuccess('contribution', 'create', $p);
190
191 // Now we have 2 - test getcount.
192 $contribution = $this->callAPISuccess('contribution', 'getcount', array());
193 $this->assertEquals(2, $contribution);
194 // Test id only format.
195 $contribution = $this->callAPISuccess('contribution', 'get', array(
196 'id' => $this->_contribution['id'],
197 'format.only_id' => 1,
198 ));
199 $this->assertEquals($this->_contribution['id'], $contribution, print_r($contribution, TRUE));
200 // Test id only format.
201 $contribution = $this->callAPISuccess('contribution', 'get', array(
202 'id' => $contribution2['id'],
203 'format.only_id' => 1,
204 ));
205 $this->assertEquals($contribution2['id'], $contribution);
206 // Test id as field.
207 $contribution = $this->callAPISuccess('contribution', 'get', array(
208 'id' => $this->_contribution['id'],
209 ));
210 $this->assertEquals(1, $contribution['count']);
211
212 // Test get by contact id works.
213 $contribution = $this->callAPISuccess('contribution', 'get', array('contact_id' => $this->_individualId));
214
215 $this->assertEquals(2, $contribution['count']);
216 $this->callAPISuccess('Contribution', 'Delete', array(
217 'id' => $this->_contribution['id'],
218 ));
219 $this->callAPISuccess('Contribution', 'Delete', array(
220 'id' => $contribution2['id'],
221 ));
222 }
223
224 /**
225 * Test that test contributions can be retrieved.
226 */
227 public function testGetTestContribution() {
228 $this->callAPISuccess('Contribution', 'create', array_merge($this->_params, array('is_test' => 1)));
229 $this->callAPISuccessGetSingle('Contribution', array('is_test' => 1));
230 }
231
232 /**
233 * Test the 'return' param works for all fields.
234 */
235 public function testGetContributionReturnFunctionality() {
236 $params = $this->_params;
237 $params['check_number'] = 'bouncer';
238 $params['payment_instrument_id'] = 'Check';
239 $params['cancel_date'] = 'yesterday';
240 $params['receipt_date'] = 'yesterday';
241 $params['thankyou_date'] = 'yesterday';
242 $params['revenue_recognition_date'] = 'yesterday';
243 $params['amount_level'] = 'Unreasonable';
244 $params['cancel_reason'] = 'You lose sucker';
245 $params['creditnote_id'] = 'sudo rm -rf';
246 $params['tax_amount'] = '1';
247 $address = $this->callAPISuccess('Address', 'create', array(
248 'street_address' => 'Knockturn Alley',
249 'contact_id' => $this->_individualId,
250 'location_type_id' => 'Home',
251 ));
252 $params['address_id'] = $address['id'];
253 $contributionPage = $this->contributionPageCreate();
254 $params['contribution_page_id'] = $contributionPage['id'];
255 $contributionRecur = $this->callAPISuccess('ContributionRecur', 'create', array(
256 'contact_id' => $this->_individualId,
257 'frequency_interval' => 1,
258 'amount' => 5,
259 ));
260 $params['contribution_recur_id'] = $contributionRecur['id'];
261
262 $params['campaign_id'] = $this->campaignCreate();
263
264 $contributionID = $this->contributionCreate($params);
265
266 // update contribution with invoice number
267 $params = array_merge($params, array(
268 'id' => $contributionID,
269 'invoice_number' => CRM_Utils_Array::value('invoice_prefix', Civi::settings()->get('contribution_invoice_settings')) . "" . $contributionID,
270 ));
271 $contributionID = $this->contributionCreate($params);
272
273 $contribution = $this->callAPISuccessGetSingle('Contribution', array('id' => $contributionID));
274 $this->assertEquals('bouncer', $contribution['check_number']);
275 $this->assertEquals('bouncer', $contribution['contribution_check_number']);
276
277 $fields = CRM_Contribute_BAO_Contribution::fields();
278 // Re-add these 2 to the fields to check. They were locked in but the metadata changed so we
279 // need to specify them.
280 $fields['address_id'] = $fields['contribution_address_id'];
281 $fields['check_number'] = $fields['contribution_check_number'];
282
283 $fieldsLockedIn = array(
284 'contribution_id', 'contribution_contact_id', 'financial_type_id', 'contribution_page_id',
285 'payment_instrument_id', 'receive_date', 'non_deductible_amount', 'total_amount',
286 'fee_amount', 'net_amount', 'trxn_id', 'invoice_id', 'currency', 'cancel_date', 'cancel_reason',
287 'receipt_date', 'thankyou_date', 'contribution_source', 'amount_level', 'contribution_recur_id',
288 'is_test', 'is_pay_later', 'contribution_status_id', 'address_id', 'check_number', 'contribution_campaign_id',
289 'creditnote_id', 'tax_amount', 'revenue_recognition_date', 'decoy',
290 );
291 $missingFields = array_diff($fieldsLockedIn, array_keys($fields));
292 // If any of the locked in fields disappear from the $fields array we need to make sure it is still
293 // covered as the test contract now guarantees them in the return array.
294 $this->assertEquals($missingFields, array(29 => 'decoy'), 'A field which was covered by the test contract has changed.');
295 foreach ($fields as $fieldName => $fieldSpec) {
296 $contribution = $this->callAPISuccessGetSingle('Contribution', array('id' => $contributionID, 'return' => $fieldName));
297 $returnField = $fieldName;
298 if ($returnField == 'contribution_contact_id') {
299 $returnField = 'contact_id';
300 }
301 $this->assertTrue((!empty($contribution[$returnField]) || $contribution[$returnField] === "0"), $returnField);
302 }
303 }
304
305 /**
306 * We need to ensure previous tested behaviour still works as part of the api contract.
307 */
308 public function testGetContributionLegacyBehaviour() {
309 $p = array(
310 'contact_id' => $this->_individualId,
311 'receive_date' => '2010-01-20',
312 'total_amount' => 100.00,
313 'contribution_type_id' => $this->_financialTypeId,
314 'non_deductible_amount' => 10.00,
315 'fee_amount' => 5.00,
316 'net_amount' => 95.00,
317 'trxn_id' => 23456,
318 'invoice_id' => 78910,
319 'source' => 'SSF',
320 'contribution_status_id' => 1,
321 );
322 $this->_contribution = $this->callAPISuccess('Contribution', 'create', $p);
323
324 $params = array(
325 'contribution_id' => $this->_contribution['id'],
326 );
327 $contribution = $this->callAPISuccess('contribution', 'get', $params);
328 $financialParams['id'] = $this->_financialTypeId;
329 $default = NULL;
330 CRM_Financial_BAO_FinancialType::retrieve($financialParams, $default);
331
332 $this->assertEquals(1, $contribution['count']);
333 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId);
334 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'], $this->_financialTypeId);
335 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_type_id'], $this->_financialTypeId);
336 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
337 $this->assertEquals($contribution['values'][$contribution['id']]['non_deductible_amount'], 10.00);
338 $this->assertEquals($contribution['values'][$contribution['id']]['fee_amount'], 5.00);
339 $this->assertEquals($contribution['values'][$contribution['id']]['net_amount'], 95.00);
340 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 23456);
341 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 78910);
342 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_source'], 'SSF');
343 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status'], 'Completed');
344
345 // Create a second contribution - we are testing that 'id' gets the right contribution id (not the contact id).
346 $p['trxn_id'] = '3847';
347 $p['invoice_id'] = '3847';
348
349 $contribution2 = $this->callAPISuccess('contribution', 'create', $p);
350
351 // now we have 2 - test getcount
352 $contribution = $this->callAPISuccess('contribution', 'getcount', array());
353 $this->assertEquals(2, $contribution);
354 //test id only format
355 $contribution = $this->callAPISuccess('contribution', 'get', array(
356 'id' => $this->_contribution['id'],
357 'format.only_id' => 1,
358 ));
359 $this->assertEquals($this->_contribution['id'], $contribution, print_r($contribution, TRUE));
360 //test id only format
361 $contribution = $this->callAPISuccess('contribution', 'get', array(
362 'id' => $contribution2['id'],
363 'format.only_id' => 1,
364 ));
365 $this->assertEquals($contribution2['id'], $contribution);
366 $contribution = $this->callAPISuccess('contribution', 'get', array(
367 'id' => $this->_contribution['id'],
368 ));
369 //test id as field
370 $this->assertEquals(1, $contribution['count']);
371 // $this->assertEquals($this->_contribution['id'], $contribution['id'] ) ;
372 //test get by contact id works
373 $contribution = $this->callAPISuccess('contribution', 'get', array('contact_id' => $this->_individualId));
374
375 $this->assertEquals(2, $contribution['count']);
376 $this->callAPISuccess('Contribution', 'Delete', array(
377 'id' => $this->_contribution['id'],
378 ));
379 $this->callAPISuccess('Contribution', 'Delete', array(
380 'id' => $contribution2['id'],
381 ));
382 }
383
384 /**
385 * Create an contribution_id=FALSE and financial_type_id=Donation.
386 */
387 public function testCreateEmptyContributionIDUseDonation() {
388 $params = array(
389 'contribution_id' => FALSE,
390 'contact_id' => 1,
391 'total_amount' => 1,
392 'check_permissions' => FALSE,
393 'financial_type_id' => 'Donation',
394 );
395 $this->callAPISuccess('contribution', 'create', $params);
396 }
397
398 /**
399 * Check with complete array + custom field.
400 *
401 * Note that the test is written on purpose without any
402 * variables specific to participant so it can be replicated into other entities
403 * and / or moved to the automated test suite
404 */
405 public function testCreateWithCustom() {
406 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, __FILE__);
407
408 $params = $this->_params;
409 $params['custom_' . $ids['custom_field_id']] = "custom string";
410
411 $result = $this->callAPIAndDocument($this->_entity, 'create', $params, __FUNCTION__, __FILE__);
412 $this->assertEquals($result['id'], $result['values'][$result['id']]['id']);
413 $check = $this->callAPISuccess($this->_entity, 'get', array(
414 'return.custom_' . $ids['custom_field_id'] => 1,
415 'id' => $result['id'],
416 ));
417 $this->customFieldDelete($ids['custom_field_id']);
418 $this->customGroupDelete($ids['custom_group_id']);
419 $this->assertEquals("custom string", $check['values'][$check['id']]['custom_' . $ids['custom_field_id']]);
420 }
421
422 /**
423 * Check with complete array + custom field.
424 *
425 * Note that the test is written on purpose without any
426 * variables specific to participant so it can be replicated into other entities
427 * and / or moved to the automated test suite
428 */
429 public function testCreateGetFieldsWithCustom() {
430 $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, __FILE__);
431 $idsContact = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, 'ContactTest.php');
432 $result = $this->callAPISuccess('Contribution', 'getfields', array());
433 $this->assertArrayHasKey('custom_' . $ids['custom_field_id'], $result['values']);
434 $this->assertArrayNotHasKey('custom_' . $idsContact['custom_field_id'], $result['values']);
435 $this->customFieldDelete($ids['custom_field_id']);
436 $this->customGroupDelete($ids['custom_group_id']);
437 $this->customFieldDelete($idsContact['custom_field_id']);
438 $this->customGroupDelete($idsContact['custom_group_id']);
439 }
440
441 public function testCreateContributionNoLineItems() {
442
443 $params = array(
444 'contact_id' => $this->_individualId,
445 'receive_date' => '20120511',
446 'total_amount' => 100.00,
447 'financial_type_id' => $this->_financialTypeId,
448 'payment_instrument_id' => 1,
449 'non_deductible_amount' => 10.00,
450 'fee_amount' => 50.00,
451 'net_amount' => 90.00,
452 'trxn_id' => 12345,
453 'invoice_id' => 67890,
454 'source' => 'SSF',
455 'contribution_status_id' => 1,
456 'skipLineItem' => 1,
457 );
458
459 $contribution = $this->callAPISuccess('contribution', 'create', $params);
460 $lineItems = $this->callAPISuccess('line_item', 'get', array(
461 'entity_id' => $contribution['id'],
462 'entity_table' => 'civicrm_contribution',
463 'sequential' => 1,
464 ));
465 $this->assertEquals(0, $lineItems['count']);
466 }
467
468 /**
469 * Test checks that passing in line items suppresses the create mechanism.
470 */
471 public function testCreateContributionChainedLineItems() {
472 $params = array(
473 'contact_id' => $this->_individualId,
474 'receive_date' => '20120511',
475 'total_amount' => 100.00,
476 'financial_type_id' => $this->_financialTypeId,
477 'payment_instrument_id' => 1,
478 'non_deductible_amount' => 10.00,
479 'fee_amount' => 50.00,
480 'net_amount' => 90.00,
481 'trxn_id' => 12345,
482 'invoice_id' => 67890,
483 'source' => 'SSF',
484 'contribution_status_id' => 1,
485 'skipLineItem' => 1,
486 'api.line_item.create' => array(
487 array(
488 'price_field_id' => 1,
489 'qty' => 2,
490 'line_total' => '20',
491 'unit_price' => '10',
492 ),
493 array(
494 'price_field_id' => 1,
495 'qty' => 1,
496 'line_total' => '80',
497 'unit_price' => '80',
498 ),
499 ),
500 );
501
502 $description = "Create Contribution with Nested Line Items.";
503 $subfile = "CreateWithNestedLineItems";
504 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__, __FILE__, $description, $subfile);
505
506 $lineItems = $this->callAPISuccess('line_item', 'get', array(
507 'entity_id' => $contribution['id'],
508 'contribution_id' => $contribution['id'],
509 'entity_table' => 'civicrm_contribution',
510 'sequential' => 1,
511 ));
512 $this->assertEquals(2, $lineItems['count']);
513 }
514
515 public function testCreateContributionOffline() {
516 $params = array(
517 'contact_id' => $this->_individualId,
518 'receive_date' => '20120511',
519 'total_amount' => 100.00,
520 'financial_type_id' => 1,
521 'trxn_id' => 12345,
522 'invoice_id' => 67890,
523 'source' => 'SSF',
524 'contribution_status_id' => 1,
525 );
526
527 $contribution = $this->callAPISuccess('contribution', 'create', $params);
528 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId);
529 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
530 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'], 1);
531 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 12345);
532 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 67890);
533 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF');
534 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status_id'], 1);
535 $lineItems = $this->callAPISuccess('line_item', 'get', array(
536 'entity_id' => $contribution['id'],
537 'contribution_id' => $contribution['id'],
538 'entity_table' => 'civicrm_contribution',
539 'sequential' => 1,
540 ));
541 $this->assertEquals(1, $lineItems['count']);
542 $this->assertEquals($contribution['id'], $lineItems['values'][0]['entity_id']);
543 $this->assertEquals($contribution['id'], $lineItems['values'][0]['contribution_id']);
544 $this->_checkFinancialRecords($contribution, 'offline');
545 $this->contributionGetnCheck($params, $contribution['id']);
546 }
547
548 /**
549 * Test create with valid payment instrument.
550 */
551 public function testCreateContributionWithPaymentInstrument() {
552 $params = $this->_params + array('payment_instrument' => 'EFT');
553 $contribution = $this->callAPISuccess('contribution', 'create', $params);
554 $contribution = $this->callAPISuccess('contribution', 'get', array(
555 'sequential' => 1,
556 'id' => $contribution['id'],
557 ));
558 $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]);
559 $this->assertEquals('EFT', $contribution['values'][0]['payment_instrument']);
560
561 $this->callAPISuccess('contribution', 'create', array(
562 'id' => $contribution['id'],
563 'payment_instrument' => 'Credit Card',
564 ));
565 $contribution = $this->callAPISuccess('contribution', 'get', array(
566 'sequential' => 1,
567 'id' => $contribution['id'],
568 ));
569 $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]);
570 $this->assertEquals('Credit Card', $contribution['values'][0]['payment_instrument']);
571 }
572
573 public function testGetContributionByPaymentInstrument() {
574 $params = $this->_params + array('payment_instrument' => 'EFT');
575 $params2 = $this->_params + array('payment_instrument' => 'Cash');
576 $this->callAPISuccess('contribution', 'create', $params);
577 $this->callAPISuccess('contribution', 'create', $params2);
578 $contribution = $this->callAPISuccess('contribution', 'get', array(
579 'sequential' => 1,
580 'contribution_payment_instrument' => 'Cash',
581 ));
582 $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]);
583 $this->assertEquals('Cash', $contribution['values'][0]['payment_instrument']);
584 $this->assertEquals(1, $contribution['count']);
585 $contribution = $this->callAPISuccess('contribution', 'get', array('sequential' => 1, 'payment_instrument' => 'Cash'));
586 $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]);
587 $this->assertEquals('Cash', $contribution['values'][0]['payment_instrument']);
588 $this->assertEquals(1, $contribution['count']);
589 $contribution = $this->callAPISuccess('contribution', 'get', array(
590 'sequential' => 1,
591 'payment_instrument_id' => 5,
592 ));
593 $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]);
594 $this->assertEquals('EFT', $contribution['values'][0]['payment_instrument']);
595 $this->assertEquals(1, $contribution['count']);
596 $contribution = $this->callAPISuccess('contribution', 'get', array(
597 'sequential' => 1,
598 'payment_instrument' => 'EFT',
599 ));
600 $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]);
601 $this->assertEquals('EFT', $contribution['values'][0]['payment_instrument']);
602 $this->assertEquals(1, $contribution['count']);
603 $contribution = $this->callAPISuccess('contribution', 'create', array(
604 'id' => $contribution['id'],
605 'payment_instrument' => 'Credit Card',
606 ));
607 $contribution = $this->callAPISuccess('contribution', 'get', array('sequential' => 1, 'id' => $contribution['id']));
608 $this->assertArrayHasKey('payment_instrument', $contribution['values'][0]);
609 $this->assertEquals('Credit Card', $contribution['values'][0]['payment_instrument']);
610 $this->assertEquals(1, $contribution['count']);
611 }
612
613 /**
614 * CRM-16227 introduces invoice_id as a parameter.
615 */
616 public function testGetContributionByInvoice() {
617 $this->callAPISuccess('Contribution', 'create', array_merge($this->_params, array('invoice_id' => 'curly')));
618 $this->callAPISuccess('Contribution', 'create', array_merge($this->_params), array('invoice_id' => 'churlish'));
619 $this->callAPISuccessGetCount('Contribution', array(), 2);
620 $this->callAPISuccessGetSingle('Contribution', array('invoice_id' => 'curly'));
621 // The following don't work. They are the format we are trying to introduce but although the form uses this format
622 // CRM_Contact_BAO_Query::convertFormValues puts them into the other format & the where only supports that.
623 // ideally the where clause would support this format (as it does on contact_BAO_Query) and those lines would
624 // come out of convertFormValues
625 // $this->callAPISuccessGetSingle('Contribution', array('invoice_id' => array('LIKE' => '%ish%')));
626 // $this->callAPISuccessGetSingle('Contribution', array('invoice_id' => array('NOT IN' => array('curly'))));
627 // $this->callAPISuccessGetCount('Contribution', array('invoice_id' => array('LIKE' => '%ly%')), 2);
628 // $this->callAPISuccessGetCount('Contribution', array('invoice_id' => array('IN' => array('curly', 'churlish'))),
629 // 2);
630 }
631
632 /**
633 * Check the credit note retrieval is case insensitive.
634 */
635 public function testGetCreditNoteCaseInsensitive() {
636 $this->contributionCreate(array('contact_id' => $this->_individualId));
637 $this->contributionCreate(array('creditnote_id' => 'cN1234', 'contact_id' => $this->_individualId, 'invoice_id' => rand(), 'trxn_id' => rand()));
638 $contribution = $this->callAPISuccess('Contribution', 'getsingle', array('creditnote_id' => 'CN1234'));
639 $this->assertEquals($contribution['creditnote_id'], 'cN1234');
640 }
641
642 /**
643 * Test retrieval by total_amount works.
644 *
645 * @throws Exception
646 */
647 public function testGetContributionByTotalAmount() {
648 $this->callAPISuccess('Contribution', 'create', array_merge($this->_params, array('total_amount' => '5')));
649 $this->callAPISuccess('Contribution', 'create', array_merge($this->_params, array('total_amount' => '10')));
650 $this->callAPISuccessGetCount('Contribution', array('total_amount' => 10), 1);
651 $this->callAPISuccessGetCount('Contribution', array('total_amount' => array('>' => 6)), 1);
652 $this->callAPISuccessGetCount('Contribution', array('total_amount' => array('>' => 0)), 2);
653 $this->callAPISuccessGetCount('Contribution', array('total_amount' => array('>' => -5)), 2);
654 $this->callAPISuccessGetCount('Contribution', array('total_amount' => array('<' => 0)), 0);
655 $this->callAPISuccessGetCount('Contribution', array(), 2);
656 }
657
658 /**
659 * Create test with unique field name on source.
660 */
661 public function testCreateContributionSource() {
662
663 $params = array(
664 'contact_id' => $this->_individualId,
665 'receive_date' => date('Ymd'),
666 'total_amount' => 100.00,
667 'financial_type_id' => $this->_financialTypeId,
668 'payment_instrument_id' => 1,
669 'non_deductible_amount' => 10.00,
670 'fee_amount' => 50.00,
671 'net_amount' => 90.00,
672 'trxn_id' => 12345,
673 'invoice_id' => 67890,
674 'contribution_source' => 'SSF',
675 'contribution_status_id' => 1,
676 );
677
678 $contribution = $this->callAPISuccess('contribution', 'create', $params);
679 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
680 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF');
681 }
682
683 /**
684 * Create test with unique field name on source.
685 *
686 * @param string $thousandSeparator
687 * punctuation used to refer to thousands.
688 *
689 * @dataProvider getThousandSeparators
690 */
691 public function testCreateDefaultNow($thousandSeparator) {
692 $this->setCurrencySeparators($thousandSeparator);
693 $params = $this->_params;
694 unset($params['receive_date'], $params['net_amount']);
695
696 $params['total_amount'] = $this->formatMoneyInput(5000.77);
697 $params['fee_amount'] = $this->formatMoneyInput(.77);
698 $params['skipCleanMoney'] = FALSE;
699
700 $contribution = $this->callAPISuccess('contribution', 'create', $params);
701 $contribution = $this->callAPISuccessGetSingle('contribution', array('id' => $contribution['id']));
702 $this->assertEquals(date('Y-m-d'), date('Y-m-d', strtotime($contribution['receive_date'])));
703 $this->assertEquals(5000.77, $contribution['total_amount'], 'failed to handle ' . $this->formatMoneyInput(5000.77));
704 $this->assertEquals(.77, $contribution['fee_amount']);
705 $this->assertEquals(5000, $contribution['net_amount']);
706 }
707
708 /**
709 * Create test with unique field name on source.
710 */
711 public function testCreateContributionNullOutThankyouDate() {
712
713 $params = $this->_params;
714 $params['thankyou_date'] = 'yesterday';
715
716 $contribution = $this->callAPISuccess('contribution', 'create', $params);
717 $contribution = $this->callAPISuccessGetSingle('contribution', array('id' => $contribution['id']));
718 $this->assertEquals(date('Y-m-d', strtotime('yesterday')), date('Y-m-d', strtotime($contribution['thankyou_date'])));
719
720 $params['thankyou_date'] = 'null';
721 $contribution = $this->callAPISuccess('contribution', 'create', $params);
722 $contribution = $this->assertTrue(empty($contribution['thankyou_date']));
723 }
724
725 /**
726 * Create test with unique field name on source.
727 */
728 public function testCreateContributionSourceInvalidContact() {
729
730 $params = array(
731 'contact_id' => 999,
732 'receive_date' => date('Ymd'),
733 'total_amount' => 100.00,
734 'financial_type_id' => $this->_financialTypeId,
735 'payment_instrument_id' => 1,
736 'non_deductible_amount' => 10.00,
737 'fee_amount' => 50.00,
738 'net_amount' => 90.00,
739 'trxn_id' => 12345,
740 'invoice_id' => 67890,
741 'contribution_source' => 'SSF',
742 'contribution_status_id' => 1,
743 );
744
745 $this->callAPIFailure('contribution', 'create', $params, 'contact_id is not valid : 999');
746 }
747
748 public function testCreateContributionSourceInvalidContContact() {
749
750 $params = array(
751 'contribution_contact_id' => 999,
752 'receive_date' => date('Ymd'),
753 'total_amount' => 100.00,
754 'financial_type_id' => $this->_financialTypeId,
755 'payment_instrument_id' => 1,
756 'non_deductible_amount' => 10.00,
757 'fee_amount' => 50.00,
758 'net_amount' => 90.00,
759 'trxn_id' => 12345,
760 'invoice_id' => 67890,
761 'contribution_source' => 'SSF',
762 'contribution_status_id' => 1,
763 );
764
765 $this->callAPIFailure('contribution', 'create', $params);
766 }
767
768 /**
769 * Test note created correctly.
770 */
771 public function testCreateContributionWithNote() {
772 $description = "Demonstrates creating contribution with Note Entity.";
773 $subfile = "ContributionCreateWithNote";
774 $params = array(
775 'contact_id' => $this->_individualId,
776 'receive_date' => '2012-01-01',
777 'total_amount' => 100.00,
778 'financial_type_id' => $this->_financialTypeId,
779 'payment_instrument_id' => 1,
780 'non_deductible_amount' => 10.00,
781 'fee_amount' => 50.00,
782 'net_amount' => 90.00,
783 'trxn_id' => 12345,
784 'invoice_id' => 67890,
785 'source' => 'SSF',
786 'contribution_status_id' => 1,
787 'note' => 'my contribution note',
788 );
789
790 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__, __FILE__, $description, $subfile);
791 $result = $this->callAPISuccess('note', 'get', array(
792 'entity_table' => 'civicrm_contribution',
793 'entity_id' => $contribution['id'],
794 'sequential' => 1,
795 ));
796 $this->assertEquals('my contribution note', $result['values'][0]['note']);
797 $this->callAPISuccess('contribution', 'delete', array('id' => $contribution['id']));
798 }
799
800 public function testCreateContributionWithNoteUniqueNameAliases() {
801 $params = array(
802 'contact_id' => $this->_individualId,
803 'receive_date' => '2012-01-01',
804 'total_amount' => 100.00,
805 'financial_type_id' => $this->_financialTypeId,
806 'payment_instrument_id' => 1,
807 'non_deductible_amount' => 10.00,
808 'fee_amount' => 50.00,
809 'net_amount' => 90.00,
810 'trxn_id' => 12345,
811 'invoice_id' => 67890,
812 'source' => 'SSF',
813 'contribution_status_id' => 1,
814 'contribution_note' => 'my contribution note',
815 );
816
817 $contribution = $this->callAPISuccess('contribution', 'create', $params);
818 $result = $this->callAPISuccess('note', 'get', array(
819 'entity_table' => 'civicrm_contribution',
820 'entity_id' => $contribution['id'],
821 'sequential' => 1,
822 ));
823 $this->assertEquals('my contribution note', $result['values'][0]['note']);
824 $this->callAPISuccess('contribution', 'delete', array('id' => $contribution['id']));
825 }
826
827 /**
828 * This is the test for creating soft credits.
829 */
830 public function testCreateContributionWithSoftCredit() {
831 $description = "Demonstrates creating contribution with SoftCredit.";
832 $subfile = "ContributionCreateWithSoftCredit";
833 $contact2 = $this->callAPISuccess('Contact', 'create', array(
834 'display_name' => 'superman',
835 'contact_type' => 'Individual',
836 ));
837 $softParams = array(
838 'contact_id' => $contact2['id'],
839 'amount' => 50,
840 'soft_credit_type_id' => 3,
841 );
842
843 $params = $this->_params + array('soft_credit' => array(1 => $softParams));
844 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__, __FILE__, $description, $subfile);
845 $result = $this->callAPISuccess('contribution', 'get', array('return' => 'soft_credit', 'sequential' => 1));
846
847 $this->assertEquals($softParams['contact_id'], $result['values'][0]['soft_credit'][1]['contact_id']);
848 $this->assertEquals($softParams['amount'], $result['values'][0]['soft_credit'][1]['amount']);
849 $this->assertEquals($softParams['soft_credit_type_id'], $result['values'][0]['soft_credit'][1]['soft_credit_type']);
850
851 $this->callAPISuccess('contribution', 'delete', array('id' => $contribution['id']));
852 $this->callAPISuccess('contact', 'delete', array('id' => $contact2['id']));
853 }
854
855 public function testCreateContributionWithSoftCreditDefaults() {
856 $description = "Demonstrates creating contribution with Soft Credit defaults for amount and type.";
857 $subfile = "ContributionCreateWithSoftCreditDefaults";
858 $contact2 = $this->callAPISuccess('Contact', 'create', array(
859 'display_name' => 'superman',
860 'contact_type' => 'Individual',
861 ));
862 $params = $this->_params + array(
863 'soft_credit_to' => $contact2['id'],
864 );
865 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__, __FILE__, $description, $subfile);
866 $result = $this->callAPISuccess('contribution', 'get', array('return' => 'soft_credit', 'sequential' => 1));
867
868 $this->assertEquals($contact2['id'], $result['values'][0]['soft_credit'][1]['contact_id']);
869 // Default soft credit amount = contribution.total_amount
870 $this->assertEquals($this->_params['total_amount'], $result['values'][0]['soft_credit'][1]['amount']);
871 $this->assertEquals(CRM_Core_OptionGroup::getDefaultValue("soft_credit_type"), $result['values'][0]['soft_credit'][1]['soft_credit_type']);
872
873 $this->callAPISuccess('contribution', 'delete', array('id' => $contribution['id']));
874 $this->callAPISuccess('contact', 'delete', array('id' => $contact2['id']));
875 }
876
877 /**
878 * Test creating contribution with Soft Credit by passing in honor_contact_id.
879 */
880 public function testCreateContributionWithHonoreeContact() {
881 $description = "Demonstrates creating contribution with Soft Credit by passing in honor_contact_id.";
882 $subfile = "ContributionCreateWithHonoreeContact";
883 $contact2 = $this->callAPISuccess('Contact', 'create', array(
884 'display_name' => 'superman',
885 'contact_type' => 'Individual',
886 ));
887 $params = $this->_params + array(
888 'honor_contact_id' => $contact2['id'],
889 );
890 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__, __FILE__, $description, $subfile);
891 $result = $this->callAPISuccess('contribution', 'get', array('return' => 'soft_credit', 'sequential' => 1));
892
893 $this->assertEquals($contact2['id'], $result['values'][0]['soft_credit'][1]['contact_id']);
894 // Default soft credit amount = contribution.total_amount
895 // Legacy mode in create api (honor_contact_id param) uses the standard "In Honor of" soft credit type
896 $this->assertEquals($this->_params['total_amount'], $result['values'][0]['soft_credit'][1]['amount']);
897 $softCreditValueTypeID = $result['values'][0]['soft_credit'][1]['soft_credit_type'];
898 $this->assertEquals('in_honor_of', CRM_Core_PseudoConstant::getName('CRM_Contribute_BAO_ContributionSoft', 'soft_credit_type_id', $softCreditValueTypeID));
899
900 $this->callAPISuccess('contribution', 'delete', array('id' => $contribution['id']));
901 $this->callAPISuccess('contact', 'delete', array('id' => $contact2['id']));
902 }
903
904 /**
905 * Test using example code.
906 */
907 public function testContributionCreateExample() {
908 //make sure at least on page exists since there is a truncate in tear down
909 $this->callAPISuccess('contribution_page', 'create', $this->_pageParams);
910 require_once 'api/v3/examples/Contribution/Create.php';
911 $result = contribution_create_example();
912 $id = $result['id'];
913 $expectedResult = contribution_create_expectedresult();
914 $this->checkArrayEquals($expectedResult, $result);
915 $this->contributionDelete($id);
916 }
917
918 /**
919 * Function tests that additional financial records are created when fee amount is recorded.
920 */
921 public function testCreateContributionWithFee() {
922 $params = array(
923 'contact_id' => $this->_individualId,
924 'receive_date' => '20120511',
925 'total_amount' => 100.00,
926 'fee_amount' => 50,
927 'financial_type_id' => 1,
928 'trxn_id' => 12345,
929 'invoice_id' => 67890,
930 'source' => 'SSF',
931 'contribution_status_id' => 1,
932 );
933
934 $contribution = $this->callAPISuccess('contribution', 'create', $params);
935 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId);
936 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
937 $this->assertEquals($contribution['values'][$contribution['id']]['fee_amount'], 50.00);
938 $this->assertEquals($contribution['values'][$contribution['id']]['net_amount'], 50.00);
939 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'], 1);
940 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 12345);
941 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 67890);
942 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF');
943 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status_id'], 1);
944
945 $lineItems = $this->callAPISuccess('line_item', 'get', array(
946
947 'entity_id' => $contribution['id'],
948 'entity_table' => 'civicrm_contribution',
949 'sequential' => 1,
950 ));
951 $this->assertEquals(1, $lineItems['count']);
952 $this->assertEquals($contribution['id'], $lineItems['values'][0]['entity_id']);
953 $this->assertEquals($contribution['id'], $lineItems['values'][0]['contribution_id']);
954 $lineItems = $this->callAPISuccess('line_item', 'get', array(
955
956 'entity_id' => $contribution['id'],
957 'contribution_id' => $contribution['id'],
958 'entity_table' => 'civicrm_contribution',
959 'sequential' => 1,
960 ));
961 $this->assertEquals(1, $lineItems['count']);
962 $this->_checkFinancialRecords($contribution, 'feeAmount');
963 }
964
965
966 /**
967 * Function tests that additional financial records are created when online contribution is created.
968 */
969 public function testCreateContributionOnline() {
970 CRM_Financial_BAO_PaymentProcessor::create($this->_processorParams);
971 $contributionPage = $this->callAPISuccess('contribution_page', 'create', $this->_pageParams);
972 $this->assertAPISuccess($contributionPage);
973 $params = array(
974 'contact_id' => $this->_individualId,
975 'receive_date' => '20120511',
976 'total_amount' => 100.00,
977 'financial_type_id' => 1,
978 'contribution_page_id' => $contributionPage['id'],
979 'payment_processor' => $this->paymentProcessorID,
980 'trxn_id' => 12345,
981 'invoice_id' => 67890,
982 'source' => 'SSF',
983 'contribution_status_id' => 1,
984
985 );
986
987 $contribution = $this->callAPISuccess('contribution', 'create', $params);
988 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId);
989 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
990 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'], 1);
991 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 12345);
992 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 67890);
993 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF');
994 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status_id'], 1);
995 $contribution['payment_instrument_id'] = $this->callAPISuccessGetValue('PaymentProcessor', array(
996 'id' => $this->paymentProcessorID,
997 'return' => 'payment_instrument_id',
998 ));
999 $this->_checkFinancialRecords($contribution, 'online');
1000 }
1001
1002 /**
1003 * Check handling of financial type.
1004 *
1005 * In the interests of removing financial type / contribution type checks from
1006 * legacy format function lets test that the api is doing this for us
1007 */
1008 public function testCreateInvalidFinancialType() {
1009 $params = $this->_params;
1010 $params['financial_type_id'] = 99999;
1011 $this->callAPIFailure($this->_entity, 'create', $params, "'99999' is not a valid option for field financial_type_id");
1012 }
1013
1014 /**
1015 * Check handling of financial type.
1016 *
1017 * In the interests of removing financial type / contribution type checks from
1018 * legacy format function lets test that the api is doing this for us
1019 */
1020 public function testValidNamedFinancialType() {
1021 $params = $this->_params;
1022 $params['financial_type_id'] = 'Donation';
1023 $this->callAPISuccess($this->_entity, 'create', $params);
1024 }
1025
1026 /**
1027 * Tests that additional financial records are created.
1028 *
1029 * Checks when online contribution with pay later option is created
1030 */
1031 public function testCreateContributionPayLaterOnline() {
1032 CRM_Financial_BAO_PaymentProcessor::create($this->_processorParams);
1033 $this->_pageParams['is_pay_later'] = 1;
1034 $contributionPage = $this->callAPISuccess('contribution_page', 'create', $this->_pageParams);
1035 $this->assertAPISuccess($contributionPage);
1036 $params = array(
1037 'contact_id' => $this->_individualId,
1038 'receive_date' => '20120511',
1039 'total_amount' => 100.00,
1040 'financial_type_id' => 1,
1041 'contribution_page_id' => $contributionPage['id'],
1042 'trxn_id' => 12345,
1043 'is_pay_later' => 1,
1044 'invoice_id' => 67890,
1045 'source' => 'SSF',
1046 'contribution_status_id' => 2,
1047
1048 );
1049
1050 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__, __FILE__);
1051 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId);
1052 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
1053 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'], 1);
1054 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 12345);
1055 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 67890);
1056 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF');
1057 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status_id'], 2);
1058 $this->_checkFinancialRecords($contribution, 'payLater');
1059 }
1060
1061 /**
1062 * Function tests that additional financial records are created for online contribution with pending option.
1063 */
1064 public function testCreateContributionPendingOnline() {
1065 CRM_Financial_BAO_PaymentProcessor::create($this->_processorParams);
1066 $contributionPage = $this->callAPISuccess('contribution_page', 'create', $this->_pageParams);
1067 $this->assertAPISuccess($contributionPage);
1068 $params = array(
1069 'contact_id' => $this->_individualId,
1070 'receive_date' => '20120511',
1071 'total_amount' => 100.00,
1072 'financial_type_id' => 1,
1073 'contribution_page_id' => $contributionPage['id'],
1074 'trxn_id' => 12345,
1075 'invoice_id' => 67890,
1076 'source' => 'SSF',
1077 'contribution_status_id' => 2,
1078 );
1079
1080 $contribution = $this->callAPISuccess('contribution', 'create', $params);
1081 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId);
1082 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
1083 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'], 1);
1084 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 12345);
1085 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 67890);
1086 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF');
1087 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status_id'], 2);
1088 $this->_checkFinancialRecords($contribution, 'pending');
1089 }
1090
1091 /**
1092 * Test that BAO defaults work.
1093 */
1094 public function testCreateBAODefaults() {
1095 unset($this->_params['contribution_source_id'], $this->_params['payment_instrument_id']);
1096 $contribution = $this->callAPISuccess('contribution', 'create', $this->_params);
1097 $contribution = $this->callAPISuccess('contribution', 'getsingle', array(
1098 'id' => $contribution['id'],
1099 'api.contribution.delete' => 1,
1100 ));
1101 $this->assertEquals(1, $contribution['contribution_status_id']);
1102 $this->assertEquals('Check', $contribution['payment_instrument']);
1103 }
1104
1105 /**
1106 * Function tests that line items, financial records are updated when contribution amount is changed.
1107 */
1108 public function testCreateUpdateContributionChangeTotal() {
1109 $contribution = $this->callAPISuccess('contribution', 'create', $this->_params);
1110 $lineItems = $this->callAPISuccess('line_item', 'getvalue', array(
1111
1112 'entity_id' => $contribution['id'],
1113 'entity_table' => 'civicrm_contribution',
1114 'sequential' => 1,
1115 'return' => 'line_total',
1116 ));
1117 $this->assertEquals('100.00', $lineItems);
1118 $trxnAmount = $this->_getFinancialTrxnAmount($contribution['id']);
1119 // Financial trxn SUM = 100 + 5 (fee)
1120 $this->assertEquals('105.00', $trxnAmount);
1121 $newParams = array(
1122
1123 'id' => $contribution['id'],
1124 'total_amount' => '125',
1125 );
1126 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1127
1128 $lineItems = $this->callAPISuccess('line_item', 'getvalue', array(
1129
1130 'entity_id' => $contribution['id'],
1131 'entity_table' => 'civicrm_contribution',
1132 'sequential' => 1,
1133 'return' => 'line_total',
1134 ));
1135
1136 $this->assertEquals('125.00', $lineItems);
1137 $trxnAmount = $this->_getFinancialTrxnAmount($contribution['id']);
1138
1139 // Financial trxn SUM = 125 + 5 (fee).
1140 $this->assertEquals('130.00', $trxnAmount);
1141 $this->assertEquals('125.00', $this->_getFinancialItemAmount($contribution['id']));
1142 }
1143
1144 /**
1145 * Function tests that line items, financial records are updated when pay later contribution is received.
1146 */
1147 public function testCreateUpdateContributionPayLater() {
1148 $contribParams = array(
1149 'contact_id' => $this->_individualId,
1150 'receive_date' => '2012-01-01',
1151 'total_amount' => 100.00,
1152 'financial_type_id' => $this->_financialTypeId,
1153 'payment_instrument_id' => 1,
1154 'contribution_status_id' => 2,
1155 'is_pay_later' => 1,
1156
1157 );
1158 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
1159
1160 $newParams = array_merge($contribParams, array(
1161 'id' => $contribution['id'],
1162 'contribution_status_id' => 1,
1163 )
1164 );
1165 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1166 $contribution = $contribution['values'][$contribution['id']];
1167 $this->assertEquals($contribution['contribution_status_id'], '1');
1168 $this->_checkFinancialItem($contribution['id'], 'paylater');
1169 $this->_checkFinancialTrxn($contribution, 'payLater');
1170 }
1171
1172 /**
1173 * Function tests that financial records are updated when Payment Instrument is changed.
1174 */
1175 public function testCreateUpdateContributionPaymentInstrument() {
1176 $instrumentId = $this->_addPaymentInstrument();
1177 $contribParams = array(
1178 'contact_id' => $this->_individualId,
1179 'total_amount' => 100.00,
1180 'financial_type_id' => $this->_financialTypeId,
1181 'payment_instrument_id' => 4,
1182 'contribution_status_id' => 1,
1183
1184 );
1185 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
1186
1187 $newParams = array_merge($contribParams, array(
1188 'id' => $contribution['id'],
1189 'payment_instrument_id' => $instrumentId,
1190 )
1191 );
1192 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1193 $this->assertAPISuccess($contribution);
1194 $this->checkFinancialTrxnPaymentInstrumentChange($contribution['id'], 4, $instrumentId);
1195
1196 // cleanup - delete created payment instrument
1197 $this->_deletedAddedPaymentInstrument();
1198 }
1199
1200 /**
1201 * Function tests that financial records are updated when Payment Instrument is changed when amount is negative.
1202 */
1203 public function testCreateUpdateNegativeContributionPaymentInstrument() {
1204 $instrumentId = $this->_addPaymentInstrument();
1205 $contribParams = array(
1206 'contact_id' => $this->_individualId,
1207 'total_amount' => -100.00,
1208 'financial_type_id' => $this->_financialTypeId,
1209 'payment_instrument_id' => 4,
1210 'contribution_status_id' => 1,
1211
1212 );
1213 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
1214
1215 $newParams = array_merge($contribParams, array(
1216 'id' => $contribution['id'],
1217 'payment_instrument_id' => $instrumentId,
1218 )
1219 );
1220 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1221 $this->assertAPISuccess($contribution);
1222 $this->checkFinancialTrxnPaymentInstrumentChange($contribution['id'], 4, $instrumentId, -100);
1223
1224 // cleanup - delete created payment instrument
1225 $this->_deletedAddedPaymentInstrument();
1226 }
1227
1228 /**
1229 * Function tests that financial records are added when Contribution is Refunded.
1230 */
1231 public function testCreateUpdateContributionRefund() {
1232 $contributionParams = array(
1233 'contact_id' => $this->_individualId,
1234 'receive_date' => '2012-01-01',
1235 'total_amount' => 100.00,
1236 'financial_type_id' => $this->_financialTypeId,
1237 'payment_instrument_id' => 4,
1238 'contribution_status_id' => 1,
1239 'trxn_id' => 'original_payment',
1240 );
1241 $contribution = $this->callAPISuccess('contribution', 'create', $contributionParams);
1242 $newParams = array_merge($contributionParams, array(
1243 'id' => $contribution['id'],
1244 'contribution_status_id' => 'Refunded',
1245 'cancel_date' => '2015-01-01 09:00',
1246 'refund_trxn_id' => 'the refund',
1247 )
1248 );
1249
1250 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1251 $this->_checkFinancialTrxn($contribution, 'refund');
1252 $this->_checkFinancialItem($contribution['id'], 'refund');
1253 $this->assertEquals('original_payment', $this->callAPISuccessGetValue('Contribution', array(
1254 'id' => $contribution['id'],
1255 'return' => 'trxn_id',
1256 )));
1257 }
1258
1259 /**
1260 * Refund a contribution for a financial type with a contra account.
1261 *
1262 * CRM-17951 the contra account is a financial account with a relationship to a
1263 * financial type. It is not always configured but should be reflected
1264 * in the financial_trxn & financial_item table if it is.
1265 */
1266 public function testCreateUpdateChargebackContributionDefaultAccount() {
1267 $contribution = $this->callAPISuccess('Contribution', 'create', $this->_params);
1268 $this->callAPISuccess('Contribution', 'create', array(
1269 'id' => $contribution['id'],
1270 'contribution_status_id' => 'Chargeback',
1271 ));
1272 $this->callAPISuccessGetSingle('Contribution', array('contribution_status_id' => 'Chargeback'));
1273
1274 $lineItems = $this->callAPISuccessGetSingle('LineItem', array(
1275 'contribution_id' => $contribution['id'],
1276 'api.FinancialItem.getsingle' => array('amount' => array('<' => 0)),
1277 ));
1278 $this->assertEquals(1, $lineItems['api.FinancialItem.getsingle']['financial_account_id']);
1279 $this->callAPISuccessGetSingle('FinancialTrxn', array(
1280 'total_amount' => -100,
1281 'status_id' => 'Chargeback',
1282 'to_financial_account_id' => 6,
1283 ));
1284 }
1285
1286 /**
1287 * Refund a contribution for a financial type with a contra account.
1288 *
1289 * CRM-17951 the contra account is a financial account with a relationship to a
1290 * financial type. It is not always configured but should be reflected
1291 * in the financial_trxn & financial_item table if it is.
1292 */
1293 public function testCreateUpdateChargebackContributionCustomAccount() {
1294 $financialAccount = $this->callAPISuccess('FinancialAccount', 'create', array(
1295 'name' => 'Chargeback Account',
1296 'is_active' => TRUE,
1297 ));
1298
1299 $entityFinancialAccount = $this->callAPISuccess('EntityFinancialAccount', 'create', array(
1300 'entity_id' => $this->_financialTypeId,
1301 'entity_table' => 'civicrm_financial_type',
1302 'account_relationship' => 'Chargeback Account is',
1303 'financial_account_id' => 'Chargeback Account',
1304 ));
1305
1306 $contribution = $this->callAPISuccess('Contribution', 'create', $this->_params);
1307 $this->callAPISuccess('Contribution', 'create', array(
1308 'id' => $contribution['id'],
1309 'contribution_status_id' => 'Chargeback',
1310 ));
1311 $this->callAPISuccessGetSingle('Contribution', array('contribution_status_id' => 'Chargeback'));
1312
1313 $lineItems = $this->callAPISuccessGetSingle('LineItem', array(
1314 'contribution_id' => $contribution['id'],
1315 'api.FinancialItem.getsingle' => array('amount' => array('<' => 0)),
1316 ));
1317 $this->assertEquals($financialAccount['id'], $lineItems['api.FinancialItem.getsingle']['financial_account_id']);
1318
1319 $this->callAPISuccess('Contribution', 'delete', array('id' => $contribution['id']));
1320 $this->callAPISuccess('EntityFinancialAccount', 'delete', array('id' => $entityFinancialAccount['id']));
1321 $this->callAPISuccess('FinancialAccount', 'delete', array('id' => $financialAccount['id']));
1322 }
1323
1324 /**
1325 * Refund a contribution for a financial type with a contra account.
1326 *
1327 * CRM-17951 the contra account is a financial account with a relationship to a
1328 * financial type. It is not always configured but should be reflected
1329 * in the financial_trxn & financial_item table if it is.
1330 */
1331 public function testCreateUpdateRefundContributionConfiguredContraAccount() {
1332 $financialAccount = $this->callAPISuccess('FinancialAccount', 'create', array(
1333 'name' => 'Refund Account',
1334 'is_active' => TRUE,
1335 ));
1336
1337 $entityFinancialAccount = $this->callAPISuccess('EntityFinancialAccount', 'create', array(
1338 'entity_id' => $this->_financialTypeId,
1339 'entity_table' => 'civicrm_financial_type',
1340 'account_relationship' => 'Credit/Contra Revenue Account is',
1341 'financial_account_id' => 'Refund Account',
1342 ));
1343
1344 $contribution = $this->callAPISuccess('Contribution', 'create', $this->_params);
1345 $this->callAPISuccess('Contribution', 'create', array(
1346 'id' => $contribution['id'],
1347 'contribution_status_id' => 'Refunded',
1348 ));
1349
1350 $lineItems = $this->callAPISuccessGetSingle('LineItem', array(
1351 'contribution_id' => $contribution['id'],
1352 'api.FinancialItem.getsingle' => array('amount' => array('<' => 0)),
1353 ));
1354 $this->assertEquals($financialAccount['id'], $lineItems['api.FinancialItem.getsingle']['financial_account_id']);
1355
1356 $this->callAPISuccess('Contribution', 'delete', array('id' => $contribution['id']));
1357 $this->callAPISuccess('EntityFinancialAccount', 'delete', array('id' => $entityFinancialAccount['id']));
1358 $this->callAPISuccess('FinancialAccount', 'delete', array('id' => $financialAccount['id']));
1359 }
1360
1361 /**
1362 * Function tests that trxn_id is set when passed in.
1363 *
1364 * Here we ensure that the civicrm_financial_trxn.trxn_id & the civicrm_contribution.trxn_id are set
1365 * when trxn_id is passed in.
1366 */
1367 public function testCreateUpdateContributionRefundTrxnIDPassedIn() {
1368 $contributionParams = array(
1369 'contact_id' => $this->_individualId,
1370 'receive_date' => '2012-01-01',
1371 'total_amount' => 100.00,
1372 'financial_type_id' => $this->_financialTypeId,
1373 'payment_instrument_id' => 4,
1374 'contribution_status_id' => 1,
1375 'trxn_id' => 'original_payment',
1376 );
1377 $contribution = $this->callAPISuccess('contribution', 'create', $contributionParams);
1378 $newParams = array_merge($contributionParams, array(
1379 'id' => $contribution['id'],
1380 'contribution_status_id' => 'Refunded',
1381 'cancel_date' => '2015-01-01 09:00',
1382 'trxn_id' => 'the refund',
1383 )
1384 );
1385
1386 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1387 $this->_checkFinancialTrxn($contribution, 'refund');
1388 $this->_checkFinancialItem($contribution['id'], 'refund');
1389 $this->assertEquals('the refund', $this->callAPISuccessGetValue('Contribution', array(
1390 'id' => $contribution['id'],
1391 'return' => 'trxn_id',
1392 )));
1393 }
1394
1395 /**
1396 * Function tests that trxn_id is set when passed in.
1397 *
1398 * Here we ensure that the civicrm_contribution.trxn_id is set
1399 * when trxn_id is passed in but if refund_trxn_id is different then that
1400 * is kept for the refund transaction.
1401 */
1402 public function testCreateUpdateContributionRefundRefundAndTrxnIDPassedIn() {
1403 $contributionParams = array(
1404 'contact_id' => $this->_individualId,
1405 'receive_date' => '2012-01-01',
1406 'total_amount' => 100.00,
1407 'financial_type_id' => $this->_financialTypeId,
1408 'payment_instrument_id' => 4,
1409 'contribution_status_id' => 1,
1410 'trxn_id' => 'original_payment',
1411 );
1412 $contribution = $this->callAPISuccess('contribution', 'create', $contributionParams);
1413 $newParams = array_merge($contributionParams, array(
1414 'id' => $contribution['id'],
1415 'contribution_status_id' => 'Refunded',
1416 'cancel_date' => '2015-01-01 09:00',
1417 'trxn_id' => 'cont id',
1418 'refund_trxn_id' => 'the refund',
1419 )
1420 );
1421
1422 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1423 $this->_checkFinancialTrxn($contribution, 'refund');
1424 $this->_checkFinancialItem($contribution['id'], 'refund');
1425 $this->assertEquals('cont id', $this->callAPISuccessGetValue('Contribution', array(
1426 'id' => $contribution['id'],
1427 'return' => 'trxn_id',
1428 )));
1429 }
1430
1431 /**
1432 * Function tests that refund_trxn_id is set when passed in empty.
1433 *
1434 * Here we ensure that the civicrm_contribution.trxn_id is set
1435 * when trxn_id is passed in but if refund_trxn_id isset but empty then that
1436 * is kept for the refund transaction.
1437 */
1438 public function testCreateUpdateContributionRefundRefundNullTrxnIDPassedIn() {
1439 $contributionParams = array(
1440 'contact_id' => $this->_individualId,
1441 'receive_date' => '2012-01-01',
1442 'total_amount' => 100.00,
1443 'financial_type_id' => $this->_financialTypeId,
1444 'payment_instrument_id' => 4,
1445 'contribution_status_id' => 1,
1446 'trxn_id' => 'original_payment',
1447 );
1448 $contribution = $this->callAPISuccess('contribution', 'create', $contributionParams);
1449 $newParams = array_merge($contributionParams, array(
1450 'id' => $contribution['id'],
1451 'contribution_status_id' => 'Refunded',
1452 'cancel_date' => '2015-01-01 09:00',
1453 'trxn_id' => 'cont id',
1454 'refund_trxn_id' => '',
1455 )
1456 );
1457
1458 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1459 $this->_checkFinancialTrxn($contribution, 'refund', NULL, array('trxn_id' => NULL));
1460 $this->_checkFinancialItem($contribution['id'], 'refund');
1461 $this->assertEquals('cont id', $this->callAPISuccessGetValue('Contribution', array(
1462 'id' => $contribution['id'],
1463 'return' => 'trxn_id',
1464 )));
1465 }
1466
1467 /**
1468 * Function tests invalid contribution status change.
1469 */
1470 public function testCreateUpdateContributionInValidStatusChange() {
1471 $contribParams = array(
1472 'contact_id' => 1,
1473 'receive_date' => '2012-01-01',
1474 'total_amount' => 100.00,
1475 'financial_type_id' => 1,
1476 'payment_instrument_id' => 1,
1477 'contribution_status_id' => 1,
1478 );
1479 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
1480 $newParams = array_merge($contribParams, array(
1481 'id' => $contribution['id'],
1482 'contribution_status_id' => 2,
1483 )
1484 );
1485 $this->callAPIFailure('contribution', 'create', $newParams, ts('Cannot change contribution status from Completed to Pending.'));
1486
1487 }
1488
1489 /**
1490 * Function tests that financial records are added when Pending Contribution is Canceled.
1491 */
1492 public function testCreateUpdateContributionCancelPending() {
1493 $contribParams = array(
1494 'contact_id' => $this->_individualId,
1495 'receive_date' => '2012-01-01',
1496 'total_amount' => 100.00,
1497 'financial_type_id' => $this->_financialTypeId,
1498 'payment_instrument_id' => 1,
1499 'contribution_status_id' => 2,
1500 'is_pay_later' => 1,
1501
1502 );
1503 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
1504 $newParams = array_merge($contribParams, array(
1505 'id' => $contribution['id'],
1506 'contribution_status_id' => 3,
1507 'cancel_date' => '2012-02-02 09:00',
1508 )
1509 );
1510 //Check if trxn_date is same as cancel_date.
1511 $checkTrxnDate = array(
1512 'trxn_date' => '2012-02-02 09:00:00',
1513 );
1514 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1515 $this->_checkFinancialTrxn($contribution, 'cancelPending', NULL, $checkTrxnDate);
1516 $this->_checkFinancialItem($contribution['id'], 'cancelPending');
1517 }
1518
1519 /**
1520 * Function tests that financial records are added when Financial Type is Changed.
1521 */
1522 public function testCreateUpdateContributionChangeFinancialType() {
1523 $contribParams = array(
1524 'contact_id' => $this->_individualId,
1525 'receive_date' => '2012-01-01',
1526 'total_amount' => 100.00,
1527 'financial_type_id' => 1,
1528 'payment_instrument_id' => 1,
1529 'contribution_status_id' => 1,
1530
1531 );
1532 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
1533 $newParams = array_merge($contribParams, array(
1534 'id' => $contribution['id'],
1535 'financial_type_id' => 3,
1536 )
1537 );
1538 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
1539 $this->_checkFinancialTrxn($contribution, 'changeFinancial');
1540 $this->_checkFinancialItem($contribution['id'], 'changeFinancial');
1541 }
1542
1543 /**
1544 * Test that update does not change status id CRM-15105.
1545 */
1546 public function testCreateUpdateWithoutChangingPendingStatus() {
1547 $contribution = $this->callAPISuccess('contribution', 'create', array_merge($this->_params, array('contribution_status_id' => 2)));
1548 $this->callAPISuccess('contribution', 'create', array('id' => $contribution['id'], 'source' => 'new source'));
1549 $contribution = $this->callAPISuccess('contribution', 'getsingle', array(
1550 'id' => $contribution['id'],
1551 'api.contribution.delete' => 1,
1552 ));
1553 $this->assertEquals(2, $contribution['contribution_status_id']);
1554 }
1555
1556 /**
1557 * Test Updating a Contribution.
1558 *
1559 * CHANGE: we require the API to do an incremental update
1560 */
1561 public function testCreateUpdateContribution() {
1562 $contributionID = $this->contributionCreate(array(
1563 'contact_id' => $this->_individualId,
1564 'trxn_id' => 212355,
1565 'financial_type_id' => $this->_financialTypeId,
1566 'invoice_id' => 'old_invoice',
1567 ));
1568 $old_params = array(
1569 'contribution_id' => $contributionID,
1570 );
1571 $original = $this->callAPISuccess('contribution', 'get', $old_params);
1572 $this->assertEquals($original['id'], $contributionID);
1573 //set up list of old params, verify
1574
1575 //This should not be required on update:
1576 $old_contact_id = $original['values'][$contributionID]['contact_id'];
1577 $old_payment_instrument = $original['values'][$contributionID]['instrument_id'];
1578 $old_fee_amount = $original['values'][$contributionID]['fee_amount'];
1579 $old_source = $original['values'][$contributionID]['contribution_source'];
1580
1581 $old_trxn_id = $original['values'][$contributionID]['trxn_id'];
1582 $old_invoice_id = $original['values'][$contributionID]['invoice_id'];
1583
1584 //check against values in CiviUnitTestCase::createContribution()
1585 $this->assertEquals($old_contact_id, $this->_individualId);
1586 $this->assertEquals($old_fee_amount, 5.00);
1587 $this->assertEquals($old_source, 'SSF');
1588 $this->assertEquals($old_trxn_id, 212355);
1589 $this->assertEquals($old_invoice_id, 'old_invoice');
1590 $params = array(
1591 'id' => $contributionID,
1592 'contact_id' => $this->_individualId,
1593 'total_amount' => 105.00,
1594 'fee_amount' => 7.00,
1595 'financial_type_id' => $this->_financialTypeId,
1596 'non_deductible_amount' => 22.00,
1597 'contribution_status_id' => 1,
1598 'note' => 'Donating for Noble Cause',
1599 );
1600
1601 $contribution = $this->callAPISuccess('contribution', 'create', $params);
1602
1603 $new_params = array(
1604 'contribution_id' => $contribution['id'],
1605
1606 );
1607 $contribution = $this->callAPISuccessGetSingle('contribution', $new_params);
1608
1609 $this->assertEquals($contribution['contact_id'], $this->_individualId);
1610 $this->assertEquals($contribution['total_amount'], 105.00);
1611 $this->assertEquals($contribution['financial_type_id'], $this->_financialTypeId);
1612 $this->assertEquals($contribution['financial_type'], 'Donation');
1613 $this->assertEquals($contribution['instrument_id'], $old_payment_instrument);
1614 $this->assertEquals($contribution['non_deductible_amount'], 22.00);
1615 $this->assertEquals($contribution['fee_amount'], 7.00);
1616 $this->assertEquals($contribution['trxn_id'], $old_trxn_id);
1617 $this->assertEquals($contribution['invoice_id'], $old_invoice_id);
1618 $this->assertEquals($contribution['contribution_source'], $old_source);
1619 $this->assertEquals($contribution['contribution_status'], 'Completed');
1620
1621 $this->assertEquals($contribution['net_amount'], $contribution['total_amount'] - $contribution['fee_amount']);
1622
1623 $params = array(
1624 'contribution_id' => $contributionID,
1625
1626 );
1627 $result = $this->callAPISuccess('contribution', 'delete', $params);
1628 $this->assertAPISuccess($result);
1629 }
1630
1631 /**
1632 * Attempt (but fail) to delete a contribution without parameters.
1633 */
1634 public function testDeleteEmptyParamsContribution() {
1635 $params = array();
1636 $this->callAPIFailure('contribution', 'delete', $params);
1637 }
1638
1639 public function testDeleteParamsNotArrayContribution() {
1640 $params = 'contribution_id= 1';
1641 $contribution = $this->callAPIFailure('contribution', 'delete', $params);
1642 $this->assertEquals($contribution['error_message'], 'Input variable `params` is not an array');
1643 }
1644
1645 public function testDeleteWrongParamContribution() {
1646 $params = array(
1647 'contribution_source' => 'SSF',
1648
1649 );
1650 $this->callAPIFailure('contribution', 'delete', $params);
1651 }
1652
1653 public function testDeleteContribution() {
1654 $contributionID = $this->contributionCreate(array(
1655 'contact_id' => $this->_individualId,
1656 'financial_type_id' => $this->_financialTypeId,
1657 ));
1658 $params = array(
1659 'id' => $contributionID,
1660 );
1661 $this->callAPIAndDocument('contribution', 'delete', $params, __FUNCTION__, __FILE__);
1662 }
1663
1664 /**
1665 * Test civicrm_contribution_search with empty params.
1666 *
1667 * All available contributions expected.
1668 */
1669 public function testSearchEmptyParams() {
1670 $params = array();
1671
1672 $p = array(
1673 'contact_id' => $this->_individualId,
1674 'receive_date' => date('Ymd'),
1675 'total_amount' => 100.00,
1676 'financial_type_id' => $this->_financialTypeId,
1677 'non_deductible_amount' => 10.00,
1678 'fee_amount' => 5.00,
1679 'net_amount' => 95.00,
1680 'trxn_id' => 23456,
1681 'invoice_id' => 78910,
1682 'source' => 'SSF',
1683 'contribution_status_id' => 1,
1684
1685 );
1686 $contribution = $this->callAPISuccess('contribution', 'create', $p);
1687
1688 $result = $this->callAPISuccess('contribution', 'get', $params);
1689 // We're taking the first element.
1690 $res = $result['values'][$contribution['id']];
1691
1692 $this->assertEquals($p['contact_id'], $res['contact_id']);
1693 $this->assertEquals($p['total_amount'], $res['total_amount']);
1694 $this->assertEquals($p['financial_type_id'], $res['financial_type_id']);
1695 $this->assertEquals($p['net_amount'], $res['net_amount']);
1696 $this->assertEquals($p['non_deductible_amount'], $res['non_deductible_amount']);
1697 $this->assertEquals($p['fee_amount'], $res['fee_amount']);
1698 $this->assertEquals($p['trxn_id'], $res['trxn_id']);
1699 $this->assertEquals($p['invoice_id'], $res['invoice_id']);
1700 $this->assertEquals($p['source'], $res['contribution_source']);
1701 // contribution_status_id = 1 => Completed
1702 $this->assertEquals('Completed', $res['contribution_status']);
1703
1704 $this->contributionDelete($contribution['id']);
1705 }
1706
1707 /**
1708 * Test civicrm_contribution_search. Success expected.
1709 */
1710 public function testSearch() {
1711 $p1 = array(
1712 'contact_id' => $this->_individualId,
1713 'receive_date' => date('Ymd'),
1714 'total_amount' => 100.00,
1715 'financial_type_id' => $this->_financialTypeId,
1716 'non_deductible_amount' => 10.00,
1717 'contribution_status_id' => 1,
1718
1719 );
1720 $contribution1 = $this->callAPISuccess('contribution', 'create', $p1);
1721
1722 $p2 = array(
1723 'contact_id' => $this->_individualId,
1724 'receive_date' => date('Ymd'),
1725 'total_amount' => 200.00,
1726 'financial_type_id' => $this->_financialTypeId,
1727 'non_deductible_amount' => 20.00,
1728 'trxn_id' => 5454565,
1729 'invoice_id' => 1212124,
1730 'fee_amount' => 50.00,
1731 'net_amount' => 60.00,
1732 'contribution_status_id' => 2,
1733
1734 );
1735 $contribution2 = $this->callAPISuccess('contribution', 'create', $p2);
1736
1737 $params = array(
1738 'contribution_id' => $contribution2['id'],
1739
1740 );
1741 $result = $this->callAPISuccess('contribution', 'get', $params);
1742 $res = $result['values'][$contribution2['id']];
1743
1744 $this->assertEquals($p2['contact_id'], $res['contact_id']);
1745 $this->assertEquals($p2['total_amount'], $res['total_amount']);
1746 $this->assertEquals($p2['financial_type_id'], $res['financial_type_id']);
1747 $this->assertEquals($p2['net_amount'], $res['net_amount']);
1748 $this->assertEquals($p2['non_deductible_amount'], $res['non_deductible_amount']);
1749 $this->assertEquals($p2['fee_amount'], $res['fee_amount']);
1750 $this->assertEquals($p2['trxn_id'], $res['trxn_id']);
1751 $this->assertEquals($p2['invoice_id'], $res['invoice_id']);
1752 // contribution_status_id = 2 => Pending
1753 $this->assertEquals('Pending', $res['contribution_status']);
1754
1755 $this->contributionDelete($contribution1['id']);
1756 $this->contributionDelete($contribution2['id']);
1757 }
1758
1759 /**
1760 * Test completing a transaction via the API.
1761 *
1762 * Note that we are creating a logged in user because email goes out from
1763 * that person
1764 */
1765 public function testCompleteTransaction() {
1766 $mut = new CiviMailUtils($this, TRUE);
1767 $this->swapMessageTemplateForTestTemplate();
1768 $this->createLoggedInUser();
1769 $params = array_merge($this->_params, array('contribution_status_id' => 2));
1770 $contribution = $this->callAPISuccess('contribution', 'create', $params);
1771 $this->callAPISuccess('contribution', 'completetransaction', array(
1772 'id' => $contribution['id'],
1773 ));
1774 $contribution = $this->callAPISuccess('contribution', 'getsingle', array('id' => $contribution['id']));
1775 $this->assertEquals('SSF', $contribution['contribution_source']);
1776 $this->assertEquals('Completed', $contribution['contribution_status']);
1777 $this->assertEquals(date('Y-m-d'), date('Y-m-d', strtotime($contribution['receipt_date'])));
1778 $mut->checkMailLog(array(
1779 'email:::anthony_anderson@civicrm.org',
1780 'is_monetary:::1',
1781 'amount:::100.00',
1782 'currency:::USD',
1783 'receive_date:::' . date('Ymd', strtotime($contribution['receive_date'])),
1784 "receipt_date:::\n",
1785 'contributeMode:::notify',
1786 'title:::Contribution',
1787 'displayName:::Mr. Anthony Anderson II',
1788 'contributionStatus:::Completed',
1789 ));
1790 $mut->stop();
1791 $this->revertTemplateToReservedTemplate();
1792 }
1793
1794 /**
1795 * Test completing a transaction via the API with a non-USD transaction.
1796 */
1797 public function testCompleteTransactionEuro() {
1798 $mut = new CiviMailUtils($this, TRUE);
1799 $this->swapMessageTemplateForTestTemplate();
1800 $this->createLoggedInUser();
1801 $params = array_merge($this->_params, array('contribution_status_id' => 2, 'currency' => 'EUR'));
1802 $contribution = $this->callAPISuccess('contribution', 'create', $params);
1803
1804 $this->callAPISuccess('contribution', 'completetransaction', array(
1805 'id' => $contribution['id'],
1806 ));
1807
1808 $contribution = $this->callAPISuccess('contribution', 'getsingle', array('id' => $contribution['id']));
1809 $this->assertEquals('SSF', $contribution['contribution_source']);
1810 $this->assertEquals('Completed', $contribution['contribution_status']);
1811 $this->assertEquals(date('Y-m-d'), date('Y-m-d', strtotime($contribution['receipt_date'])));
1812
1813 $entityFinancialTransactions = $this->getFinancialTransactionsForContribution($contribution['id']);
1814 $entityFinancialTransaction = reset($entityFinancialTransactions);
1815 $financialTrxn = $this->callAPISuccessGetSingle('FinancialTrxn', array('id' => $entityFinancialTransaction['financial_trxn_id']));
1816 $this->assertEquals('EUR', $financialTrxn['currency']);
1817
1818 $mut->checkMailLog(array(
1819 'email:::anthony_anderson@civicrm.org',
1820 'is_monetary:::1',
1821 'amount:::100.00',
1822 'currency:::EUR',
1823 'receive_date:::' . date('Ymd', strtotime($contribution['receive_date'])),
1824 "receipt_date:::\n",
1825 'contributeMode:::notify',
1826 'title:::Contribution',
1827 'displayName:::Mr. Anthony Anderson II',
1828 'contributionStatus:::Completed',
1829 ));
1830 $mut->stop();
1831 $this->revertTemplateToReservedTemplate();
1832 }
1833
1834 /**
1835 * Test to ensure mail is sent on chosing pay later
1836 */
1837 public function testpayLater() {
1838 $mut = new CiviMailUtils($this, TRUE);
1839 $this->swapMessageTemplateForTestTemplate();
1840 $this->createLoggedInUser();
1841
1842 // create contribution page first
1843 $contributionPageParams = array(
1844 'title' => 'Help Support CiviCRM!',
1845 'financial_type_id' => 1,
1846 'is_monetary' => TRUE,
1847 'is_pay_later' => 1,
1848 'is_quick_config' => TRUE,
1849 'pay_later_text' => 'I will send payment by check',
1850 'pay_later_receipt' => 'This is a pay later reciept',
1851 'is_allow_other_amount' => 1,
1852 'min_amount' => 10.00,
1853 'max_amount' => 10000.00,
1854 'goal_amount' => 100000.00,
1855 'is_email_receipt' => 1,
1856 'is_active' => 1,
1857 'amount_block_is_active' => 1,
1858 'currency' => 'USD',
1859 'is_billing_required' => 0,
1860 );
1861 $contributionPageResult = $this->callAPISuccess('contribution_page', 'create', $contributionPageParams);
1862
1863 // submit form values
1864 $priceSet = $this->callAPISuccess('price_set', 'getsingle', array('name' => 'default_contribution_amount'));
1865 $params = array(
1866 'id' => $contributionPageResult['id'],
1867 'contact_id' => $this->_individualId,
1868 'email-5' => 'anthony_anderson@civicrm.org',
1869 'payment_processor_id' => 0,
1870 'amount' => 100.00,
1871 'tax_amount' => '',
1872 'currencyID' => 'USD',
1873 'is_pay_later' => 1,
1874 'invoiceID' => 'f28e1ddc86f8c4a0ff5bcf46393e4bc8',
1875 'is_quick_config' => 1,
1876 'description' => 'Online Contribution: Help Support CiviCRM!',
1877 'price_set_id' => $priceSet['id'],
1878 );
1879 $this->callAPISuccess('contribution_page', 'submit', $params);
1880
1881 $mut->checkMailLog(array(
1882 'is_pay_later:::1',
1883 'email:::anthony_anderson@civicrm.org',
1884 'pay_later_receipt:::' . $contributionPageParams['pay_later_receipt'],
1885 'displayName:::Mr. Anthony Anderson II',
1886 'contributionPageId:::' . $contributionPageResult['id'],
1887 'title:::' . $contributionPageParams['title'],
1888 'amount:::' . $params['amount'],
1889 ));
1890 $mut->stop();
1891 $this->revertTemplateToReservedTemplate();
1892 }
1893
1894 /**
1895 * Test to check whether contact billing address is used when no contribution address
1896 */
1897 public function testBillingAddress() {
1898 $mut = new CiviMailUtils($this, TRUE);
1899 $this->swapMessageTemplateForTestTemplate();
1900 $this->createLoggedInUser();
1901
1902 //Scenario 1: When Contact don't have any address
1903 $params = array_merge($this->_params, array('contribution_status_id' => 2));
1904 $contribution = $this->callAPISuccess('contribution', 'create', $params);
1905 $this->callAPISuccess('contribution', 'completetransaction', array(
1906 'id' => $contribution['id'],
1907 ));
1908 $mut->checkMailLog(array(
1909 'address:::',
1910 ));
1911
1912 // Scenario 2: Contribution using address
1913 $address = $this->callAPISuccess('address', 'create', array(
1914 'street_address' => 'contribution billing st',
1915 'location_type_id' => 2,
1916 'contact_id' => $this->_params['contact_id'],
1917 ));
1918 $params = array_merge($this->_params, array(
1919 'contribution_status_id' => 2,
1920 'address_id' => $address['id'],
1921 )
1922 );
1923 $contribution = $this->callAPISuccess('contribution', 'create', $params);
1924 $this->callAPISuccess('contribution', 'completetransaction', array(
1925 'id' => $contribution['id'],
1926 ));
1927 $mut->checkMailLog(array(
1928 'address:::contribution billing st',
1929 ));
1930
1931 // Scenario 3: Contribution wtth no address but contact has a billing address
1932 $this->callAPISuccess('address', 'create', array(
1933 'id' => $address['id'],
1934 'street_address' => 'is billing st',
1935 'contact_id' => $this->_params['contact_id'],
1936 ));
1937 $params = array_merge($this->_params, array('contribution_status_id' => 2));
1938 $contribution = $this->callAPISuccess('contribution', 'create', $params);
1939 $this->callAPISuccess('contribution', 'completetransaction', array(
1940 'id' => $contribution['id'],
1941 ));
1942 $mut->checkMailLog(array(
1943 'address:::is billing st',
1944 ));
1945
1946 $mut->stop();
1947 $this->revertTemplateToReservedTemplate();
1948 }
1949
1950 /**
1951 * Test completing a transaction via the API.
1952 *
1953 * Note that we are creating a logged in user because email goes out from
1954 * that person
1955 */
1956 public function testCompleteTransactionFeeAmount() {
1957 $this->createLoggedInUser();
1958 $params = array_merge($this->_params, array('contribution_status_id' => 2));
1959 $contribution = $this->callAPISuccess('contribution', 'create', $params);
1960 $this->callAPISuccess('contribution', 'completetransaction', array(
1961 'id' => $contribution['id'],
1962 'fee_amount' => '.56',
1963 'trxn_id' => '7778888',
1964 ));
1965 $contribution = $this->callAPISuccess('contribution', 'getsingle', array('id' => $contribution['id'], 'sequential' => 1));
1966 $this->assertEquals('Completed', $contribution['contribution_status']);
1967 $this->assertEquals('7778888', $contribution['trxn_id']);
1968 $this->assertEquals('.56', $contribution['fee_amount']);
1969 $this->assertEquals('99.44', $contribution['net_amount']);
1970 }
1971
1972 /**
1973 * CRM-19126 Add test to verify when complete transaction is called tax amount is not changed.
1974 *
1975 * @param string $thousandSeparator
1976 * punctuation used to refer to thousands.
1977 *
1978 * @dataProvider getThousandSeparators
1979 */
1980 public function testCheckTaxAmount($thousandSeparator) {
1981 $this->setCurrencySeparators($thousandSeparator);
1982 $contact = $this->createLoggedInUser();
1983 $financialType = $this->callAPISuccess('financial_type', 'create', array(
1984 'name' => 'Test taxable financial Type',
1985 'is_reserved' => 0,
1986 'is_active' => 1,
1987 ));
1988 $financialAccount = $this->callAPISuccess('financial_account', 'create', array(
1989 'name' => 'Test Tax financial account ',
1990 'contact_id' => $contact,
1991 'financial_account_type_id' => 2,
1992 'is_tax' => 1,
1993 'tax_rate' => 5.00,
1994 'is_reserved' => 0,
1995 'is_active' => 1,
1996 'is_default' => 0,
1997 ));
1998 $financialTypeId = $financialType['id'];
1999 $financialAccountId = $financialAccount['id'];
2000 $financialAccountParams = array(
2001 'entity_table' => 'civicrm_financial_type',
2002 'entity_id' => $financialTypeId,
2003 'account_relationship' => 10,
2004 'financial_account_id' => $financialAccountId,
2005 );
2006 CRM_Financial_BAO_FinancialTypeAccount::add($financialAccountParams);
2007
2008 $params = array_merge($this->_params, array('contribution_status_id' => 2, 'financial_type_id' => $financialTypeId));
2009 $contribution = $this->callAPISuccess('contribution', 'create', $params);
2010 $contribution1 = $this->callAPISuccess('contribution', 'get', array('id' => $contribution['id'], 'return' => 'tax_amount', 'sequential' => 1));
2011 $this->callAPISuccess('contribution', 'completetransaction', array(
2012 'id' => $contribution['id'],
2013 'trxn_id' => '777788888',
2014 'fee_amount' => '6.00',
2015 ));
2016 $contribution2 = $this->callAPISuccess('contribution', 'get', array('id' => $contribution['id'], 'return' => array('tax_amount', 'fee_amount', 'net_amount'), 'sequential' => 1));
2017 $this->assertEquals($contribution1['values'][0]['tax_amount'], $contribution2['values'][0]['tax_amount']);
2018 $this->assertEquals('6.00', $contribution2['values'][0]['fee_amount']);
2019 $this->assertEquals('99.00', $contribution2['values'][0]['net_amount']);
2020 }
2021
2022 /**
2023 * Test repeat contribution successfully creates line item.
2024 */
2025 public function testRepeatTransaction() {
2026 $originalContribution = $this->setUpRepeatTransaction($recurParams = array(), 'single');
2027 $this->callAPISuccess('contribution', 'repeattransaction', array(
2028 'original_contribution_id' => $originalContribution['id'],
2029 'contribution_status_id' => 'Completed',
2030 'trxn_id' => uniqid(),
2031 ));
2032 $lineItemParams = array(
2033 'entity_id' => $originalContribution['id'],
2034 'sequential' => 1,
2035 'return' => array(
2036 'entity_table',
2037 'qty',
2038 'unit_price',
2039 'line_total',
2040 'label',
2041 'financial_type_id',
2042 'deductible_amount',
2043 'price_field_value_id',
2044 'price_field_id',
2045 ),
2046 );
2047 $lineItem1 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
2048 'entity_id' => $originalContribution['id'],
2049 )));
2050 $lineItem2 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
2051 'entity_id' => $originalContribution['id'] + 1,
2052 )));
2053 unset($lineItem1['values'][0]['id'], $lineItem1['values'][0]['entity_id']);
2054 unset($lineItem2['values'][0]['id'], $lineItem2['values'][0]['entity_id']);
2055 $this->assertEquals($lineItem1['values'][0], $lineItem2['values'][0]);
2056 $this->_checkFinancialRecords(array(
2057 'id' => $originalContribution['id'] + 1,
2058 'payment_instrument_id' => $this->callAPISuccessGetValue('PaymentProcessor', array(
2059 'id' => $originalContribution['payment_processor_id'],
2060 'return' => 'payment_instrument_id',
2061 )),
2062 ), 'online');
2063 $this->quickCleanUpFinancialEntities();
2064 }
2065
2066 /**
2067 * Test repeat contribution successfully creates line items (plural).
2068 */
2069 public function testRepeatTransactionLineItems() {
2070 // CRM-19309
2071 $originalContribution = $this->setUpRepeatTransaction($recurParams = array(), 'multiple');
2072 $this->callAPISuccess('contribution', 'repeattransaction', array(
2073 'original_contribution_id' => $originalContribution['id'],
2074 'contribution_status_id' => 'Completed',
2075 'trxn_id' => uniqid(),
2076 ));
2077
2078 $lineItemParams = array(
2079 'entity_id' => $originalContribution['id'],
2080 'sequential' => 1,
2081 'return' => array(
2082 'entity_table',
2083 'qty',
2084 'unit_price',
2085 'line_total',
2086 'label',
2087 'financial_type_id',
2088 'deductible_amount',
2089 'price_field_value_id',
2090 'price_field_id',
2091 ),
2092 );
2093 $lineItem1 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
2094 'entity_id' => $originalContribution['id'],
2095 )));
2096 $lineItem2 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
2097 'entity_id' => $originalContribution['id'] + 1,
2098 )));
2099
2100 // unset id and entity_id for all of them to be able to compare the lineItems:
2101 unset($lineItem1['values'][0]['id'], $lineItem1['values'][0]['entity_id']);
2102 unset($lineItem2['values'][0]['id'], $lineItem2['values'][0]['entity_id']);
2103 $this->assertEquals($lineItem1['values'][0], $lineItem2['values'][0]);
2104
2105 unset($lineItem1['values'][1]['id'], $lineItem1['values'][1]['entity_id']);
2106 unset($lineItem2['values'][1]['id'], $lineItem2['values'][1]['entity_id']);
2107 $this->assertEquals($lineItem1['values'][1], $lineItem2['values'][1]);
2108
2109 // CRM-19309 so in future we also want to:
2110 // check that financial_line_items have been created for entity_id 3 and 4;
2111
2112 $this->callAPISuccessGetCount('FinancialItem', array('description' => 'Sales Tax', 'amount' => 0), 0);
2113 $this->quickCleanUpFinancialEntities();
2114 }
2115
2116 /**
2117 * Test repeat contribution successfully creates is_test transaction.
2118 */
2119 public function testRepeatTransactionIsTest() {
2120 $this->_params['is_test'] = 1;
2121 $originalContribution = $this->setUpRepeatTransaction(array('is_test' => 1), 'single');
2122
2123 $this->callAPISuccess('contribution', 'repeattransaction', array(
2124 'original_contribution_id' => $originalContribution['id'],
2125 'contribution_status_id' => 'Completed',
2126 'trxn_id' => uniqid(),
2127 ));
2128 $this->callAPISuccessGetCount('Contribution', array('contribution_test' => 1), 2);
2129 }
2130
2131 /**
2132 * Test repeat contribution passed in status.
2133 */
2134 public function testRepeatTransactionPassedInStatus() {
2135 $originalContribution = $this->setUpRepeatTransaction($recurParams = array(), 'single');
2136
2137 $this->callAPISuccess('contribution', 'repeattransaction', array(
2138 'original_contribution_id' => $originalContribution['id'],
2139 'contribution_status_id' => 'Pending',
2140 'trxn_id' => uniqid(),
2141 ));
2142 $this->callAPISuccessGetCount('Contribution', array('contribution_status_id' => 2), 1);
2143 }
2144
2145 /**
2146 * Test repeat contribution accepts recur_id instead of original_contribution_id.
2147 */
2148 public function testRepeatTransactionAcceptRecurID() {
2149 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array(
2150 'contact_id' => $this->_individualId,
2151 'installments' => '12',
2152 'frequency_interval' => '1',
2153 'amount' => '100',
2154 'contribution_status_id' => 1,
2155 'start_date' => '2012-01-01 00:00:00',
2156 'currency' => 'USD',
2157 'frequency_unit' => 'month',
2158 'payment_processor_id' => $this->paymentProcessorID,
2159 ));
2160 $this->callAPISuccess('contribution', 'create', array_merge(
2161 $this->_params,
2162 array('contribution_recur_id' => $contributionRecur['id']))
2163 );
2164
2165 $this->callAPISuccess('contribution', 'repeattransaction', array(
2166 'contribution_recur_id' => $contributionRecur['id'],
2167 'contribution_status_id' => 'Completed',
2168 'trxn_id' => uniqid(),
2169 ));
2170
2171 $this->quickCleanUpFinancialEntities();
2172 }
2173
2174 /**
2175 * CRM-19873 Test repattransaction if contribution_recur_id is a test.
2176 */
2177 public function testRepeatTransactionTestRecurId() {
2178 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array(
2179 'contact_id' => $this->_individualId,
2180 'frequency_interval' => '1',
2181 'amount' => '1.00',
2182 'contribution_status_id' => 1,
2183 'start_date' => '2017-01-01 00:00:00',
2184 'currency' => 'USD',
2185 'frequency_unit' => 'month',
2186 'payment_processor_id' => $this->paymentProcessorID,
2187 'is_test' => 1,
2188 ));
2189 $this->callAPISuccess('contribution', 'create', array_merge(
2190 $this->_params,
2191 array(
2192 'contribution_recur_id' => $contributionRecur['id'],
2193 'is_test' => 1,
2194 ))
2195 );
2196
2197 $repeatedContribution = $this->callAPISuccess('contribution', 'repeattransaction', array(
2198 'contribution_recur_id' => $contributionRecur['id'],
2199 'contribution_status_id' => 'Completed',
2200 'trxn_id' => uniqid(),
2201 ));
2202
2203 $this->assertEquals($contributionRecur['values'][1]['is_test'], $repeatedContribution['values'][2]['is_test']);
2204 $this->quickCleanUpFinancialEntities();
2205 }
2206 /**
2207 * CRM-19945 Tests repeattransaction is using a completed contribution for the template.
2208 *
2209 * ( Tests membership is renewed after repeattransaction. )
2210 */
2211 public function testRepeatTransactionUsesCompleted() {
2212 list($originalContribution, $membership) = $this->setUpAutoRenewMembership();
2213
2214 $this->callAPISuccess('contribution', 'create', array(
2215 'contact_id' => $originalContribution['values'][1]['contact_id'],
2216 'financial_type_id' => $originalContribution['values'][1]['financial_type_id'],
2217 'total_amount' => $originalContribution['values'][1]['total_amount'],
2218 'contribution_recur_id' => $originalContribution['values'][1]['contribution_recur_id'],
2219 'contribution_status_id' => "Failed",
2220 ));
2221
2222 $this->callAPISuccess('membership', 'create', array(
2223 'id' => $membership['id'],
2224 'end_date' => 'yesterday',
2225 'status_id' => 4,
2226 ));
2227
2228 $contribution = $this->callAPISuccess('contribution', 'repeattransaction', array(
2229 'contribution_recur_id' => $originalContribution['values'][1]['contribution_recur_id'],
2230 'contribution_status_id' => 'Completed',
2231 'trxn_id' => 'bobsled',
2232 ));
2233
2234 $membershipStatusId = $this->callAPISuccess('membership', 'getvalue', array(
2235 'id' => $membership['id'],
2236 'return' => 'status_id',
2237 ));
2238
2239 $this->assertEquals('New', CRM_Core_PseudoConstant::getLabel('CRM_Member_BAO_Membership', 'status_id', $membershipStatusId));
2240
2241 $lineItem = $this->callAPISuccessGetSingle('LineItem', array('contribution_id' => $contribution['id']));
2242 $this->assertEquals('civicrm_membership', $lineItem['entity_table']);
2243 $this->callAPISuccessGetCount('MembershipPayment', array('membership_id' => $membership['id']));
2244 $this->quickCleanUpFinancialEntities();
2245 $this->contactDelete($originalContribution['values'][1]['contact_id']);
2246 }
2247
2248 /**
2249 * CRM-16397 test appropriate action if total amount has changed for single line items.
2250 */
2251 public function testRepeatTransactionAlteredAmount() {
2252 $paymentProcessorID = $this->paymentProcessorCreate();
2253 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array(
2254 'contact_id' => $this->_individualId,
2255 'installments' => '12',
2256 'frequency_interval' => '1',
2257 'amount' => '500',
2258 'contribution_status_id' => 1,
2259 'start_date' => '2012-01-01 00:00:00',
2260 'currency' => 'USD',
2261 'frequency_unit' => 'month',
2262 'payment_processor_id' => $paymentProcessorID,
2263 ));
2264 $originalContribution = $this->callAPISuccess('contribution', 'create', array_merge(
2265 $this->_params,
2266 array(
2267 'contribution_recur_id' => $contributionRecur['id'],
2268 ))
2269 );
2270
2271 $this->callAPISuccess('contribution', 'repeattransaction', array(
2272 'original_contribution_id' => $originalContribution['id'],
2273 'contribution_status_id' => 'Completed',
2274 'trxn_id' => uniqid(),
2275 'total_amount' => '400',
2276 'fee_amount' => 50,
2277 ));
2278
2279 $lineItemParams = array(
2280 'entity_id' => $originalContribution['id'],
2281 'sequential' => 1,
2282 'return' => array(
2283 'entity_table',
2284 'qty',
2285 'unit_price',
2286 'line_total',
2287 'label',
2288 'financial_type_id',
2289 'deductible_amount',
2290 'price_field_value_id',
2291 'price_field_id',
2292 ),
2293 );
2294 $this->callAPISuccessGetSingle('contribution', array(
2295 'total_amount' => 400,
2296 'fee_amount' => 50,
2297 'net_amount' => 350,
2298 ));
2299 $lineItem1 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
2300 'entity_id' => $originalContribution['id'],
2301 )));
2302 $expectedLineItem = array_merge(
2303 $lineItem1['values'][0], array(
2304 'line_total' => '400.00',
2305 'unit_price' => '400.00',
2306 )
2307 );
2308
2309 $lineItem2 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
2310 'entity_id' => $originalContribution['id'] + 1,
2311 )));
2312
2313 unset($expectedLineItem['id'], $expectedLineItem['entity_id']);
2314 unset($lineItem2['values'][0]['id'], $lineItem2['values'][0]['entity_id']);
2315 $this->assertEquals($expectedLineItem, $lineItem2['values'][0]);
2316 }
2317
2318 /**
2319 * CRM-17718 test appropriate action if financial type has changed for single line items.
2320 */
2321 public function testRepeatTransactionPassedInFinancialType() {
2322 $originalContribution = $this->setUpRecurringContribution();
2323
2324 $this->callAPISuccess('contribution', 'repeattransaction', array(
2325 'original_contribution_id' => $originalContribution['id'],
2326 'contribution_status_id' => 'Completed',
2327 'trxn_id' => uniqid(),
2328 'financial_type_id' => 2,
2329 ));
2330 $lineItemParams = array(
2331 'entity_id' => $originalContribution['id'],
2332 'sequential' => 1,
2333 'return' => array(
2334 'entity_table',
2335 'qty',
2336 'unit_price',
2337 'line_total',
2338 'label',
2339 'financial_type_id',
2340 'deductible_amount',
2341 'price_field_value_id',
2342 'price_field_id',
2343 ),
2344 );
2345
2346 $this->callAPISuccessGetSingle('contribution', array(
2347 'total_amount' => 100,
2348 'financial_type_id' => 2,
2349 ));
2350 $lineItem1 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
2351 'entity_id' => $originalContribution['id'],
2352 )));
2353 $expectedLineItem = array_merge(
2354 $lineItem1['values'][0], array(
2355 'line_total' => '100.00',
2356 'unit_price' => '100.00',
2357 'financial_type_id' => 2,
2358 'contribution_type_id' => 2,
2359 )
2360 );
2361 $lineItem2 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
2362 'entity_id' => $originalContribution['id'] + 1,
2363 )));
2364 unset($expectedLineItem['id'], $expectedLineItem['entity_id']);
2365 unset($lineItem2['values'][0]['id'], $lineItem2['values'][0]['entity_id']);
2366 $this->assertEquals($expectedLineItem, $lineItem2['values'][0]);
2367 }
2368
2369 /**
2370 * CRM-17718 test appropriate action if financial type has changed for single line items.
2371 */
2372 public function testRepeatTransactionUpdatedFinancialType() {
2373 $originalContribution = $this->setUpRecurringContribution(array(), array('financial_type_id' => 2));
2374
2375 $this->callAPISuccess('contribution', 'repeattransaction', array(
2376 'contribution_recur_id' => $originalContribution['id'],
2377 'contribution_status_id' => 'Completed',
2378 'trxn_id' => uniqid(),
2379 ));
2380 $lineItemParams = array(
2381 'entity_id' => $originalContribution['id'],
2382 'sequential' => 1,
2383 'return' => array(
2384 'entity_table',
2385 'qty',
2386 'unit_price',
2387 'line_total',
2388 'label',
2389 'financial_type_id',
2390 'deductible_amount',
2391 'price_field_value_id',
2392 'price_field_id',
2393 ),
2394 );
2395
2396 $this->callAPISuccessGetSingle('contribution', array(
2397 'total_amount' => 100,
2398 'financial_type_id' => 2,
2399 ));
2400 $lineItem1 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
2401 'entity_id' => $originalContribution['id'],
2402 )));
2403 $expectedLineItem = array_merge(
2404 $lineItem1['values'][0], array(
2405 'line_total' => '100.00',
2406 'unit_price' => '100.00',
2407 'financial_type_id' => 2,
2408 'contribution_type_id' => 2,
2409 )
2410 );
2411
2412 $lineItem2 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
2413 'entity_id' => $originalContribution['id'] + 1,
2414 )));
2415 unset($expectedLineItem['id'], $expectedLineItem['entity_id']);
2416 unset($lineItem2['values'][0]['id'], $lineItem2['values'][0]['entity_id']);
2417 $this->assertEquals($expectedLineItem, $lineItem2['values'][0]);
2418 }
2419
2420 /**
2421 * CRM-16397 test appropriate action if campaign has been passed in.
2422 */
2423 public function testRepeatTransactionPassedInCampaign() {
2424 $paymentProcessorID = $this->paymentProcessorCreate();
2425 $campaignID = $this->campaignCreate();
2426 $campaignID2 = $this->campaignCreate();
2427 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array(
2428 'contact_id' => $this->_individualId,
2429 'installments' => '12',
2430 'frequency_interval' => '1',
2431 'amount' => '100',
2432 'contribution_status_id' => 1,
2433 'start_date' => '2012-01-01 00:00:00',
2434 'currency' => 'USD',
2435 'frequency_unit' => 'month',
2436 'payment_processor_id' => $paymentProcessorID,
2437 ));
2438 $originalContribution = $this->callAPISuccess('contribution', 'create', array_merge(
2439 $this->_params,
2440 array(
2441 'contribution_recur_id' => $contributionRecur['id'],
2442 'campaign_id' => $campaignID,
2443 ))
2444 );
2445
2446 $this->callAPISuccess('contribution', 'repeattransaction', array(
2447 'original_contribution_id' => $originalContribution['id'],
2448 'contribution_status_id' => 'Completed',
2449 'trxn_id' => uniqid(),
2450 'campaign_id' => $campaignID2,
2451 ));
2452
2453 $this->callAPISuccessGetSingle('contribution', array(
2454 'total_amount' => 100,
2455 'campaign_id' => $campaignID2,
2456 ));
2457 }
2458
2459 /**
2460 * CRM-17718 campaign stored on contribution recur gets priority.
2461 *
2462 * This reflects the fact we permit people to update them.
2463 */
2464 public function testRepeatTransactionUpdatedCampaign() {
2465 $paymentProcessorID = $this->paymentProcessorCreate();
2466 $campaignID = $this->campaignCreate();
2467 $campaignID2 = $this->campaignCreate();
2468 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array(
2469 'contact_id' => $this->_individualId,
2470 'installments' => '12',
2471 'frequency_interval' => '1',
2472 'amount' => '100',
2473 'contribution_status_id' => 1,
2474 'start_date' => '2012-01-01 00:00:00',
2475 'currency' => 'USD',
2476 'frequency_unit' => 'month',
2477 'payment_processor_id' => $paymentProcessorID,
2478 'campaign_id' => $campaignID,
2479 ));
2480 $originalContribution = $this->callAPISuccess('contribution', 'create', array_merge(
2481 $this->_params,
2482 array(
2483 'contribution_recur_id' => $contributionRecur['id'],
2484 'campaign_id' => $campaignID2,
2485 ))
2486 );
2487
2488 $this->callAPISuccess('contribution', 'repeattransaction', array(
2489 'original_contribution_id' => $originalContribution['id'],
2490 'contribution_status_id' => 'Completed',
2491 'trxn_id' => uniqid(),
2492 ));
2493
2494 $this->callAPISuccessGetSingle('contribution', array(
2495 'total_amount' => 100,
2496 'campaign_id' => $campaignID,
2497 ));
2498 }
2499
2500 /**
2501 * CRM-20685 Repeattransaction produces incorrect Financial Type ID (in specific circumstance) - if number of lineItems = 1.
2502 *
2503 * This case happens when the line item & contribution do not have the same type in his initiating transaction.
2504 */
2505 public function testRepeatTransactionUpdatedFinancialTypeAndNotEquals() {
2506 $originalContribution = $this->setUpRecurringContribution(array(), array('financial_type_id' => 2));
2507 // This will made the trick to get the not equals behaviour.
2508 $this->callAPISuccess('line_item', 'create', array('id' => 1, 'financial_type_id' => 4));
2509 $this->callAPISuccess('contribution', 'repeattransaction', array(
2510 'contribution_recur_id' => $originalContribution['id'],
2511 'contribution_status_id' => 'Completed',
2512 'trxn_id' => uniqid(),
2513 ));
2514 $lineItemParams = array(
2515 'entity_id' => $originalContribution['id'],
2516 'sequential' => 1,
2517 'return' => array(
2518 'entity_table',
2519 'qty',
2520 'unit_price',
2521 'line_total',
2522 'label',
2523 'financial_type_id',
2524 'deductible_amount',
2525 'price_field_value_id',
2526 'price_field_id',
2527 ),
2528 );
2529 $this->callAPISuccessGetSingle('contribution', array(
2530 'total_amount' => 100,
2531 'financial_type_id' => 2,
2532 ));
2533 $lineItem1 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
2534 'entity_id' => $originalContribution['id'],
2535 )));
2536 $expectedLineItem = array_merge(
2537 $lineItem1['values'][0], array(
2538 'line_total' => '100.00',
2539 'unit_price' => '100.00',
2540 'financial_type_id' => 4,
2541 'contribution_type_id' => 4,
2542 )
2543 );
2544
2545 $lineItem2 = $this->callAPISuccess('line_item', 'get', array_merge($lineItemParams, array(
2546 'entity_id' => $originalContribution['id'] + 1,
2547 )));
2548 $this->callAPISuccess('line_item', 'create', array('id' => 1, 'financial_type_id' => 1));
2549 unset($expectedLineItem['id'], $expectedLineItem['entity_id']);
2550 unset($lineItem2['values'][0]['id'], $lineItem2['values'][0]['entity_id']);
2551 $this->assertEquals($expectedLineItem, $lineItem2['values'][0]);
2552 }
2553
2554
2555 /**
2556 * Test completing a transaction does not 'mess' with net amount (CRM-15960).
2557 */
2558 public function testCompleteTransactionNetAmountOK() {
2559 $this->createLoggedInUser();
2560 $params = array_merge($this->_params, array('contribution_status_id' => 2));
2561 unset($params['net_amount']);
2562 $contribution = $this->callAPISuccess('contribution', 'create', $params);
2563 $this->callAPISuccess('contribution', 'completetransaction', array(
2564 'id' => $contribution['id'],
2565 ));
2566 $contribution = $this->callAPISuccess('contribution', 'getsingle', array('id' => $contribution['id']));
2567 $this->assertEquals('Completed', $contribution['contribution_status']);
2568 $this->assertTrue(($contribution['total_amount'] - $contribution['net_amount']) == $contribution['fee_amount']);
2569 }
2570
2571 /**
2572 * CRM-14151 - Test completing a transaction via the API.
2573 */
2574 public function testCompleteTransactionWithReceiptDateSet() {
2575 $this->swapMessageTemplateForTestTemplate();
2576 $mut = new CiviMailUtils($this, TRUE);
2577 $this->createLoggedInUser();
2578 $params = array_merge($this->_params, array('contribution_status_id' => 2, 'receipt_date' => 'now'));
2579 $contribution = $this->callAPISuccess('contribution', 'create', $params);
2580 $this->callAPISuccess('contribution', 'completetransaction', array('id' => $contribution['id'], 'trxn_date' => date('Y-m-d')));
2581 $contribution = $this->callAPISuccess('contribution', 'get', array('id' => $contribution['id'], 'sequential' => 1));
2582 $this->assertEquals('Completed', $contribution['values'][0]['contribution_status']);
2583 $this->assertEquals(date('Y-m-d'), date('Y-m-d', strtotime($contribution['values'][0]['receive_date'])));
2584 $mut->checkMailLog(array(
2585 'Receipt - Contribution',
2586 'receipt_date:::' . date('Ymd'),
2587 ));
2588 $mut->stop();
2589 $this->revertTemplateToReservedTemplate();
2590 }
2591
2592 /**
2593 * CRM-1960 - Test to ensure that completetransaction respects the is_email_receipt setting
2594 */
2595 public function testCompleteTransactionWithEmailReceiptInput() {
2596 $contributionPage = $this->createReceiptableContributionPage();
2597
2598 $this->_params['contribution_page_id'] = $contributionPage['id'];
2599 $params = array_merge($this->_params, array('contribution_status_id' => 2));
2600 $contribution = $this->callAPISuccess('contribution', 'create', $params);
2601 // Complete the transaction overriding is_email_receipt to = FALSE
2602 $this->callAPISuccess('contribution', 'completetransaction', array(
2603 'id' => $contribution['id'],
2604 'trxn_date' => date('2011-04-09'),
2605 'trxn_id' => 'kazam',
2606 'is_email_receipt' => 0,
2607 ));
2608 // Check if a receipt was issued
2609 $receipt_date = $this->callAPISuccess('Contribution', 'getvalue', array('id' => $contribution['id'], 'return' => 'receipt_date'));
2610 $this->assertEquals('', $receipt_date);
2611 }
2612
2613 /**
2614 * Test that $is_recur is assigned to the receipt.
2615 */
2616 public function testCompleteTransactionForRecurring() {
2617
2618 $this->swapMessageTemplateForTestTemplate();
2619 $recurring = $this->setUpRecurringContribution();
2620 $contributionPage = $this->createReceiptableContributionPage(array('is_recur' => TRUE, 'recur_frequency_unit' => 'month', 'recur_interval' => 1));
2621
2622 $this->_params['contribution_page_id'] = $contributionPage['id'];
2623 $this->_params['contribution_recur_id'] = $recurring['id'];
2624
2625 $contribution = $this->setUpForCompleteTransaction();
2626
2627 $this->callAPISuccess('contribution', 'completetransaction', array(
2628 'id' => $contribution['id'],
2629 'trxn_date' => date('2011-04-09'),
2630 'trxn_id' => 'kazam',
2631 'is_email_receipt' => 1,
2632 ));
2633
2634 $this->mut->checkMailLog(array(
2635 'is_recur:::1',
2636 'cancelSubscriptionUrl:::' . CIVICRM_UF_BASEURL,
2637 ));
2638 $this->mut->stop();
2639 $this->revertTemplateToReservedTemplate();
2640 }
2641 /**
2642 * CRM-19710 - Test to ensure that completetransaction respects the input for is_email_receipt setting.
2643 *
2644 * If passed in it will override the default from contribution page.
2645 */
2646 public function testCompleteTransactionWithEmailReceiptInputTrue() {
2647 $mut = new CiviMailUtils($this, TRUE);
2648 $this->createLoggedInUser();
2649 // Create a Contribution Page with is_email_receipt = FALSE
2650 $contributionPage = $this->callAPISuccess('ContributionPage', 'create', array(
2651 'receipt_from_name' => 'Mickey Mouse',
2652 'receipt_from_email' => 'mickey@mouse.com',
2653 'title' => "Test Contribution Page",
2654 'financial_type_id' => 1,
2655 'currency' => 'CAD',
2656 'is_monetary' => TRUE,
2657 'is_email_receipt' => 0,
2658 ));
2659 $this->_params['contribution_page_id'] = $contributionPage['id'];
2660 $params = array_merge($this->_params, array('contribution_status_id' => 2, 'receipt_date' => 'now'));
2661 $contribution = $this->callAPISuccess('contribution', 'create', $params);
2662 // Complete the transaction overriding is_email_receipt to = TRUE
2663 $this->callAPISuccess('contribution', 'completetransaction', array(
2664 'id' => $contribution['id'],
2665 'is_email_receipt' => 1,
2666 ));
2667 $mut->checkMailLog(array(
2668 'Please print this receipt for your records.',
2669 ));
2670 $mut->stop();
2671 }
2672
2673 /**
2674 * Complete the transaction using the template with all the possible.
2675 */
2676 public function testCompleteTransactionWithTestTemplate() {
2677 $this->swapMessageTemplateForTestTemplate();
2678 $contribution = $this->setUpForCompleteTransaction();
2679 $this->callAPISuccess('contribution', 'completetransaction', array(
2680 'id' => $contribution['id'],
2681 'trxn_date' => date('2011-04-09'),
2682 'trxn_id' => 'kazam',
2683 ));
2684 $receive_date = $this->callAPISuccess('Contribution', 'getvalue', array('id' => $contribution['id'], 'return' => 'receive_date'));
2685 $this->mut->checkMailLog(array(
2686 'email:::anthony_anderson@civicrm.org',
2687 'is_monetary:::1',
2688 'amount:::100.00',
2689 'currency:::USD',
2690 'receive_date:::' . date('Ymd', strtotime($receive_date)),
2691 'receipt_date:::' . date('Ymd'),
2692 'contributeMode:::notify',
2693 'title:::Contribution',
2694 'displayName:::Mr. Anthony Anderson II',
2695 'trxn_id:::kazam',
2696 'contactID:::' . $this->_params['contact_id'],
2697 'contributionID:::' . $contribution['id'],
2698 'financialTypeId:::1',
2699 'financialTypeName:::Donation',
2700 ));
2701 $this->mut->stop();
2702 $this->revertTemplateToReservedTemplate();
2703 }
2704
2705 /**
2706 * Complete the transaction using the template with all the possible.
2707 */
2708 public function testCompleteTransactionContributionPageFromAddress() {
2709 $contributionPage = $this->callAPISuccess('ContributionPage', 'create', array(
2710 'receipt_from_name' => 'Mickey Mouse',
2711 'receipt_from_email' => 'mickey@mouse.com',
2712 'title' => "Test Contribution Page",
2713 'financial_type_id' => 1,
2714 'currency' => 'NZD',
2715 'goal_amount' => 50,
2716 'is_pay_later' => 1,
2717 'is_monetary' => TRUE,
2718 'is_email_receipt' => TRUE,
2719 ));
2720 $this->_params['contribution_page_id'] = $contributionPage['id'];
2721 $contribution = $this->setUpForCompleteTransaction();
2722 $this->callAPISuccess('contribution', 'completetransaction', array('id' => $contribution['id']));
2723 $this->mut->checkMailLog(array(
2724 'mickey@mouse.com',
2725 'Mickey Mouse <',
2726 ));
2727 $this->mut->stop();
2728 }
2729
2730 /**
2731 * Test completing first transaction in a recurring series.
2732 *
2733 * The status should be set to 'in progress' and the next scheduled payment date calculated.
2734 *
2735 * @dataProvider getScheduledDateData
2736 *
2737 * @param array $dataSet
2738 *
2739 * @throws \Exception
2740 */
2741 public function testCompleteTransactionSetStatusToInProgress($dataSet) {
2742 $paymentProcessorID = $this->paymentProcessorCreate();
2743 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array_merge(array(
2744 'contact_id' => $this->_individualId,
2745 'installments' => '2',
2746 'frequency_interval' => '1',
2747 'amount' => '500',
2748 'contribution_status_id' => 'Pending',
2749 'start_date' => '2012-01-01 00:00:00',
2750 'currency' => 'USD',
2751 'frequency_unit' => 'month',
2752 'payment_processor_id' => $paymentProcessorID,
2753 ), $dataSet['data']));
2754 $contribution = $this->callAPISuccess('contribution', 'create', array_merge(
2755 $this->_params,
2756 array(
2757 'contribution_recur_id' => $contributionRecur['id'],
2758 'contribution_status_id' => 'Pending',
2759 'receive_date' => $dataSet['receive_date'],
2760 ))
2761 );
2762 $this->callAPISuccess('Contribution', 'completetransaction', array(
2763 'id' => $contribution,
2764 'receive_date' => $dataSet['receive_date'],
2765 ));
2766 $contributionRecur = $this->callAPISuccessGetSingle('ContributionRecur', array(
2767 'id' => $contributionRecur['id'],
2768 'return' => array('next_sched_contribution_date', 'contribution_status_id'),
2769 ));
2770 $this->assertEquals(5, $contributionRecur['contribution_status_id']);
2771 $this->assertEquals($dataSet['expected'], $contributionRecur['next_sched_contribution_date']);
2772 $this->callAPISuccess('Contribution', 'create', array_merge(
2773 $this->_params,
2774 array(
2775 'contribution_recur_id' => $contributionRecur['id'],
2776 'contribution_status_id' => 'Completed',
2777 )
2778 ));
2779 $contributionRecur = $this->callAPISuccessGetSingle('ContributionRecur', array(
2780 'id' => $contributionRecur['id'],
2781 'return' => array('contribution_status_id'),
2782 ));
2783 $this->assertEquals(1, $contributionRecur['contribution_status_id']);
2784 }
2785
2786 /**
2787 * Get dates for testing.
2788 *
2789 * @return array
2790 */
2791 public function getScheduledDateData() {
2792 $result = array();
2793 $result[]['2016-08-31-1-month'] = array(
2794 'data' => array(
2795 'start_date' => '2016-08-31',
2796 'frequency_interval' => 1,
2797 'frequency_unit' => 'month',
2798 ),
2799 'receive_date' => '2016-08-31',
2800 'expected' => '2016-10-01 00:00:00',
2801 );
2802 $result[]['2012-01-01-1-month'] = array(
2803 'data' => array(
2804 'start_date' => '2012-01-01',
2805 'frequency_interval' => 1,
2806 'frequency_unit' => 'month',
2807 ),
2808 'receive_date' => '2012-01-01',
2809 'expected' => '2012-02-01 00:00:00',
2810 );
2811 $result[]['2012-01-01-1-month'] = array(
2812 'data' => array(
2813 'start_date' => '2012-01-01',
2814 'frequency_interval' => 1,
2815 'frequency_unit' => 'month',
2816 ),
2817 'receive_date' => '2012-02-29',
2818 'expected' => '2012-03-29 00:00:00',
2819 );
2820 $result['receive_date_includes_time']['2012-01-01-1-month'] = array(
2821 'data' => array(
2822 'start_date' => '2012-01-01',
2823 'frequency_interval' => 1,
2824 'frequency_unit' => 'month',
2825 'next_sched_contribution_date' => '2012-02-29',
2826 ),
2827 'receive_date' => '2012-02-29 16:00:00',
2828 'expected' => '2012-03-29 00:00:00',
2829 );
2830 return $result;
2831 }
2832
2833 /**
2834 * Test completing a pledge with the completeTransaction api..
2835 *
2836 * Note that we are creating a logged in user because email goes out from
2837 * that person.
2838 */
2839 public function testCompleteTransactionUpdatePledgePayment() {
2840 $this->swapMessageTemplateForTestTemplate();
2841 $mut = new CiviMailUtils($this, TRUE);
2842 $mut->clearMessages();
2843 $this->createLoggedInUser();
2844 $contributionID = $this->createPendingPledgeContribution();
2845 $this->callAPISuccess('contribution', 'completetransaction', array(
2846 'id' => $contributionID,
2847 'trxn_date' => '1 Feb 2013',
2848 ));
2849 $pledge = $this->callAPISuccessGetSingle('Pledge', array(
2850 'id' => $this->_ids['pledge'],
2851 ));
2852 $this->assertEquals('Completed', $pledge['pledge_status']);
2853
2854 $status = $this->callAPISuccessGetValue('PledgePayment', array(
2855 'pledge_id' => $this->_ids['pledge'],
2856 'return' => 'status_id',
2857 ));
2858 $this->assertEquals(1, $status);
2859 $mut->checkMailLog(array(
2860 'amount:::500.00',
2861 'receive_date:::20130201000000',
2862 "receipt_date:::\n",
2863 ));
2864 $mut->stop();
2865 $this->revertTemplateToReservedTemplate();
2866 }
2867
2868 /**
2869 * Test completing a transaction with an event via the API.
2870 *
2871 * Note that we are creating a logged in user because email goes out from
2872 * that person
2873 */
2874 public function testCompleteTransactionWithParticipantRecord() {
2875 $mut = new CiviMailUtils($this, TRUE);
2876 $mut->clearMessages();
2877 $this->_individualId = $this->createLoggedInUser();
2878 $contributionID = $this->createPendingParticipantContribution();
2879 $this->callAPISuccess('contribution', 'completetransaction', array(
2880 'id' => $contributionID,
2881 )
2882 );
2883 $participantStatus = $this->callAPISuccessGetValue('participant', array(
2884 'id' => $this->_ids['participant'],
2885 'return' => 'participant_status_id',
2886 ));
2887 $this->assertEquals(1, $participantStatus);
2888
2889 //Assert only three activities are created.
2890 $activities = CRM_Activity_BAO_Activity::getContactActivity($this->_individualId);
2891 $this->assertEquals(3, count($activities));
2892 $activityNames = array_count_values(CRM_Utils_Array::collect('activity_name', $activities));
2893 // record two activities before and after completing payment for Event registration
2894 $this->assertEquals(2, $activityNames['Event Registration']);
2895 // update the original 'Contribution' activity created after completing payment
2896 $this->assertEquals(1, $activityNames['Contribution']);
2897
2898 $mut->checkMailLog(array(
2899 'Annual CiviCRM meet',
2900 'Event',
2901 'This letter is a confirmation that your registration has been received and your status has been updated to Registered.',
2902 ));
2903 $mut->stop();
2904 }
2905
2906 /**
2907 * Test membership is renewed when transaction completed.
2908 */
2909 public function testCompleteTransactionMembershipPriceSet() {
2910 $this->createPriceSetWithPage('membership');
2911 $stateOfGrace = $this->callAPISuccess('MembershipStatus', 'getvalue', array(
2912 'name' => 'Grace',
2913 'return' => 'id')
2914 );
2915 $this->setUpPendingContribution($this->_ids['price_field_value'][0]);
2916 $membership = $this->callAPISuccess('membership', 'getsingle', array('id' => $this->_ids['membership']));
2917 $logs = $this->callAPISuccess('MembershipLog', 'get', array(
2918 'membership_id' => $this->_ids['membership'],
2919 ));
2920 $this->assertEquals(1, $logs['count']);
2921 $this->assertEquals($stateOfGrace, $membership['status_id']);
2922 $this->callAPISuccess('contribution', 'completetransaction', array('id' => $this->_ids['contribution']));
2923 $membership = $this->callAPISuccess('membership', 'getsingle', array('id' => $this->_ids['membership']));
2924 $this->assertEquals(date('Y-m-d', strtotime('yesterday + 1 year')), $membership['end_date']);
2925 $this->callAPISuccessGetSingle('LineItem', array(
2926 'entity_id' => $this->_ids['membership'],
2927 'entity_table' => 'civicrm_membership',
2928 ));
2929 $logs = $this->callAPISuccess('MembershipLog', 'get', array(
2930 'membership_id' => $this->_ids['membership'],
2931 ));
2932 $this->assertEquals(2, $logs['count']);
2933 $this->assertNotEquals($stateOfGrace, $logs['values'][2]['status_id']);
2934 //Assert only three activities are created.
2935 $activities = CRM_Activity_BAO_Activity::getContactActivity($this->_ids['contact']);
2936 $this->assertEquals(3, count($activities));
2937 $activityNames = array_flip(CRM_Utils_Array::collect('activity_name', $activities));
2938 $this->assertArrayHasKey('Contribution', $activityNames);
2939 $this->assertArrayHasKey('Membership Signup', $activityNames);
2940 $this->assertArrayHasKey('Change Membership Status', $activityNames);
2941 $this->cleanUpAfterPriceSets();
2942 }
2943
2944 /**
2945 * Test if renewal activity is create after changing Pending contribution to Completed via offline
2946 */
2947 public function testPendingToCompleteContribution() {
2948 $this->createPriceSetWithPage('membership');
2949 $this->setUpPendingContribution($this->_ids['price_field_value'][0]);
2950 $this->callAPISuccess('membership', 'getsingle', array('id' => $this->_ids['membership']));
2951 // Case 1: Assert that Membership Signup Activity is created on Pending to Completed Contribution via backoffice
2952 $activity = $this->callAPISuccess('Activity', 'get', array(
2953 'activity_type_id' => 'Membership Signup',
2954 'source_record_id' => $this->_ids['membership'],
2955 'status_id' => 'Scheduled',
2956 ));
2957 $this->assertEquals(1, $activity['count']);
2958
2959 // change pending contribution to completed
2960 $form = new CRM_Contribute_Form_Contribution();
2961
2962 $form->_params = array(
2963 'id' => $this->_ids['contribution'],
2964 'total_amount' => 20,
2965 'net_amount' => 20,
2966 'fee_amount' => 0,
2967 'financial_type_id' => 1,
2968 'contact_id' => $this->_individualId,
2969 'contribution_status_id' => 1,
2970 'billing_middle_name' => '',
2971 'billing_last_name' => 'Adams',
2972 'billing_street_address-5' => '790L Lincoln St S',
2973 'billing_city-5' => 'Maryknoll',
2974 'billing_state_province_id-5' => 1031,
2975 'billing_postal_code-5' => 10545,
2976 'billing_country_id-5' => 1228,
2977 'frequency_interval' => 1,
2978 'frequency_unit' => 'month',
2979 'installments' => '',
2980 'hidden_AdditionalDetail' => 1,
2981 'hidden_Premium' => 1,
2982 'from_email_address' => '"civi45" <civi45@civicrm.com>',
2983 'receipt_date' => '',
2984 'receipt_date_time' => '',
2985 'payment_processor_id' => $this->paymentProcessorID,
2986 'currency' => 'USD',
2987 'contribution_page_id' => $this->_ids['contribution_page'],
2988 'contribution_mode' => 'membership',
2989 'source' => 'Membership Signup and Renewal',
2990 );
2991
2992 $form->testSubmit($form->_params, CRM_Core_Action::UPDATE);
2993
2994 // Case 2: After successful payment for Pending backoffice there are three activities created
2995 // 2.a Update status of existing Scheduled Membership Signup (created in step 1) to Completed
2996 $activity = $this->callAPISuccess('Activity', 'get', array(
2997 'activity_type_id' => 'Membership Signup',
2998 'source_record_id' => $this->_ids['membership'],
2999 'status_id' => 'Completed',
3000 ));
3001 $this->assertEquals(1, $activity['count']);
3002 // 2.b Contribution activity created to record successful payment
3003 $activity = $this->callAPISuccess('Activity', 'get', array(
3004 'activity_type_id' => 'Contribution',
3005 'source_record_id' => $this->_ids['contribution'],
3006 'status_id' => 'Completed',
3007 ));
3008 $this->assertEquals(1, $activity['count']);
3009
3010 // 2.c 'Change membership type' activity created to record Membership status change from Grace to Current
3011 $activity = $this->callAPISuccess('Activity', 'get', array(
3012 'activity_type_id' => 'Change Membership Status',
3013 'source_record_id' => $this->_ids['membership'],
3014 'status_id' => 'Completed',
3015 ));
3016 $this->assertEquals(1, $activity['count']);
3017 $this->assertEquals('Status changed from Grace to Current', $activity['values'][$activity['id']]['subject']);
3018
3019 //Create another pending contribution for renewal
3020 $contribution = $this->callAPISuccess('contribution', 'create', array(
3021 'domain_id' => 1,
3022 'contact_id' => $this->_ids['contact'],
3023 'receive_date' => date('Ymd'),
3024 'total_amount' => 20.00,
3025 'financial_type_id' => 1,
3026 'payment_instrument_id' => 'Credit Card',
3027 'non_deductible_amount' => 10.00,
3028 'trxn_id' => 'rdhfi88',
3029 'invoice_id' => 'dofhiewuyr',
3030 'source' => 'SSF',
3031 'contribution_status_id' => 2,
3032 'contribution_page_id' => $this->_ids['contribution_page'],
3033 // We can't rely on contribution api to link line items correctly to membership
3034 'skipLineItem' => TRUE,
3035 'api.membership_payment.create' => array('membership_id' => $this->_ids['membership']),
3036 ));
3037
3038 $this->callAPISuccess('line_item', 'create', array(
3039 'entity_id' => $contribution['id'],
3040 'entity_table' => 'civicrm_contribution',
3041 'contribution_id' => $contribution['id'],
3042 'price_field_id' => $this->_ids['price_field'][0],
3043 'qty' => 1,
3044 'unit_price' => 20,
3045 'line_total' => 20,
3046 'financial_type_id' => 1,
3047 'price_field_value_id' => $this->_ids['price_field_value']['cont'],
3048 ));
3049 $this->callAPISuccess('line_item', 'create', array(
3050 'entity_id' => $this->_ids['membership'],
3051 'entity_table' => 'civicrm_membership',
3052 'contribution_id' => $contribution['id'],
3053 'price_field_id' => $this->_ids['price_field'][0],
3054 'qty' => 1,
3055 'unit_price' => 20,
3056 'line_total' => 20,
3057 'financial_type_id' => 1,
3058 'price_field_value_id' => $this->_ids['price_field_value'][0],
3059 'membership_type_id' => $this->_ids['membership_type'],
3060 ));
3061
3062 //Update it to Failed.
3063 $form->_params['id'] = $contribution['id'];
3064 $form->_params['contribution_status_id'] = 4;
3065
3066 $form->testSubmit($form->_params, CRM_Core_Action::UPDATE);
3067 //Existing membership should not get updated to expired.
3068 $membership = $this->callAPISuccess('membership', 'getsingle', array('id' => $this->_ids['membership']));
3069 $this->assertNotEquals($membership['status_id'], 4);
3070 }
3071
3072 /**
3073 * Test membership is renewed when transaction completed.
3074 */
3075 public function testCompleteTransactionMembershipPriceSetTwoTerms() {
3076 $this->createPriceSetWithPage('membership');
3077 $this->setUpPendingContribution($this->_ids['price_field_value'][1]);
3078 $this->callAPISuccess('contribution', 'completetransaction', array('id' => $this->_ids['contribution']));
3079 $membership = $this->callAPISuccess('membership', 'getsingle', array('id' => $this->_ids['membership']));
3080 $this->assertEquals(date('Y-m-d', strtotime('yesterday + 2 years')), $membership['end_date']);
3081 $this->cleanUpAfterPriceSets();
3082 }
3083
3084 public function cleanUpAfterPriceSets() {
3085 $this->quickCleanUpFinancialEntities();
3086 $this->contactDelete($this->_ids['contact']);
3087 }
3088
3089 /**
3090 * Set up a pending transaction with a specific price field id.
3091 *
3092 * @param int $priceFieldValueID
3093 */
3094 public function setUpPendingContribution($priceFieldValueID, $contriParams = array()) {
3095 $contactID = $this->individualCreate();
3096 $membership = $this->callAPISuccess('membership', 'create', array(
3097 'contact_id' => $contactID,
3098 'membership_type_id' => $this->_ids['membership_type'],
3099 'start_date' => 'yesterday - 1 year',
3100 'end_date' => 'yesterday',
3101 'join_date' => 'yesterday - 1 year',
3102 ));
3103 $contribution = $this->callAPISuccess('contribution', 'create', array_merge(array(
3104 'domain_id' => 1,
3105 'contact_id' => $contactID,
3106 'receive_date' => date('Ymd'),
3107 'total_amount' => 20.00,
3108 'financial_type_id' => 1,
3109 'payment_instrument_id' => 'Credit Card',
3110 'non_deductible_amount' => 10.00,
3111 'trxn_id' => 'jdhfi' . rand(1, 100),
3112 'invoice_id' => 'djfhiew' . rand(5, 100),
3113 'source' => 'SSF',
3114 'contribution_status_id' => 2,
3115 'contribution_page_id' => $this->_ids['contribution_page'],
3116 'api.membership_payment.create' => array('membership_id' => $membership['id']),
3117 ), $contriParams));
3118
3119 $this->callAPISuccess('line_item', 'create', array(
3120 'entity_id' => $contribution['id'],
3121 'entity_table' => 'civicrm_contribution',
3122 'contribution_id' => $contribution['id'],
3123 'price_field_id' => $this->_ids['price_field'][0],
3124 'qty' => 1,
3125 'unit_price' => 20,
3126 'line_total' => 20,
3127 'financial_type_id' => 1,
3128 'price_field_value_id' => $priceFieldValueID,
3129 ));
3130 $this->_ids['contact'] = $contactID;
3131 $this->_ids['contribution'] = $contribution['id'];
3132 $this->_ids['membership'] = $membership['id'];
3133 }
3134
3135 /**
3136 * Test sending a mail via the API.
3137 */
3138 public function testSendMail() {
3139 $mut = new CiviMailUtils($this, TRUE);
3140 $contribution = $this->callAPISuccess('contribution', 'create', $this->_params);
3141 $this->callAPISuccess('contribution', 'sendconfirmation', array(
3142 'id' => $contribution['id'],
3143 'receipt_from_email' => 'api@civicrm.org',
3144 )
3145 );
3146 $mut->checkMailLog(array(
3147 '$ 100.00',
3148 'Contribution Information',
3149 'Please print this confirmation for your records',
3150 ), array(
3151 'Event',
3152 )
3153 );
3154
3155 $this->checkCreditCardDetails($mut, $contribution['id']);
3156 $mut->stop();
3157 }
3158
3159 /**
3160 * Test sending a mail via the API.
3161 * This simulates webform_civicrm using pay later contribution page
3162 */
3163 public function testSendconfirmationPayLater() {
3164 $mut = new CiviMailUtils($this, TRUE);
3165
3166 // Create contribution page
3167 $pageParams = array(
3168 'title' => 'Webform Contributions',
3169 'financial_type_id' => 1,
3170 'contribution_type_id' => 1,
3171 'is_confirm_enabled' => 1,
3172 'is_pay_later' => 1,
3173 'pay_later_text' => 'I will send payment by cheque',
3174 'pay_later_receipt' => 'Send your cheque payable to "CiviCRM LLC" to the office',
3175 );
3176 $contributionPage = $this->callAPISuccess('contribution_page', 'create', $pageParams);
3177
3178 // Create pay later contribution
3179 $contribParams = array(
3180 'contact_id' => $this->_individualId,
3181 'financial_type_id' => 1,
3182 'is_pay_later' => 1,
3183 'contribution_status_id' => 2,
3184 'contribution_page_id' => $contributionPage['id'],
3185 'total_amount' => '10.00',
3186 );
3187 $contribution = $this->callAPISuccess('contribution', 'create', $contribParams);
3188
3189 // Create line item
3190 $lineItemParams = array(
3191 'contribution_id' => $contribution['id'],
3192 'entity_id' => $contribution['id'],
3193 'entity_table' => 'civicrm_contribution',
3194 'label' => 'My lineitem label',
3195 'qty' => 1,
3196 'unit_price' => "10.00",
3197 'line_total' => "10.00",
3198 );
3199 $lineItem = $this->callAPISuccess('lineItem', 'create', $lineItemParams);
3200
3201 // Create email
3202 try {
3203 civicrm_api3('contribution', 'sendconfirmation', array(
3204 'id' => $contribution['id'],
3205 'receipt_from_email' => 'api@civicrm.org',
3206 )
3207 );
3208 }
3209 catch (Exception $e) {
3210 // Need to figure out how to stop this some other day
3211 // We don't care about the Payment Processor because this is Pay Later
3212 // The point of this test is to check we get the pay_later version of the mail
3213 if ($e->getMessage() != "Undefined variable: CRM16923AnUnreliableMethodHasBeenUserToDeterminePaymentProcessorFromContributionPage") {
3214 throw $e;
3215 }
3216 }
3217
3218 // Retrieve mail & check it has the pay_later_receipt info
3219 $mut->getMostRecentEmail('raw');
3220 $mut->checkMailLog(array(
3221 (string) $contribParams['total_amount'],
3222 $pageParams['pay_later_receipt'],
3223 ), array(
3224 'Event',
3225 )
3226 );
3227 $mut->stop();
3228 }
3229
3230
3231 /**
3232 * Check credit card details in sent mail via API
3233 *
3234 * @param $mut obj CiviMailUtils instance
3235 * @param int $contributionID Contribution ID
3236 *
3237 */
3238 public function checkCreditCardDetails($mut, $contributionID) {
3239 $contribution = $this->callAPISuccess('contribution', 'create', $this->_params);
3240 $this->callAPISuccess('contribution', 'sendconfirmation', array(
3241 'id' => $contributionID,
3242 'receipt_from_email' => 'api@civicrm.org',
3243 'payment_processor_id' => $this->paymentProcessorID,
3244 )
3245 );
3246 $mut->checkMailLog(array(
3247 'Credit Card Information', // credit card header
3248 'Billing Name and Address', // billing header
3249 'anthony_anderson@civicrm.org', // billing name
3250 ), array(
3251 'Event',
3252 )
3253 );
3254 }
3255
3256 /**
3257 * Test sending a mail via the API.
3258 */
3259 public function testSendMailEvent() {
3260 $mut = new CiviMailUtils($this, TRUE);
3261 $contribution = $this->callAPISuccess('contribution', 'create', $this->_params);
3262 $event = $this->eventCreate(array(
3263 'is_email_confirm' => 1,
3264 'confirm_from_email' => 'test@civicrm.org',
3265 ));
3266 $this->_eventID = $event['id'];
3267 $participantParams = array(
3268 'contact_id' => $this->_individualId,
3269 'event_id' => $this->_eventID,
3270 'status_id' => 1,
3271 'role_id' => 1,
3272 // to ensure it matches later on
3273 'register_date' => '2007-07-21 00:00:00',
3274 'source' => 'Online Event Registration: API Testing',
3275
3276 );
3277 $participant = $this->callAPISuccess('participant', 'create', $participantParams);
3278 $this->callAPISuccess('participant_payment', 'create', array(
3279 'participant_id' => $participant['id'],
3280 'contribution_id' => $contribution['id'],
3281 ));
3282 $this->callAPISuccess('contribution', 'sendconfirmation', array(
3283 'id' => $contribution['id'],
3284 'receipt_from_email' => 'api@civicrm.org',
3285 )
3286 );
3287
3288 $mut->checkMailLog(array(
3289 'Annual CiviCRM meet',
3290 'Event',
3291 'To: "Mr. Anthony Anderson II" <anthony_anderson@civicrm.org>',
3292 ), array()
3293 );
3294 $mut->stop();
3295 }
3296
3297 /**
3298 * This function does a GET & compares the result against the $params.
3299 *
3300 * Use as a double check on Creates.
3301 *
3302 * @param array $params
3303 * @param int $id
3304 * @param bool $delete
3305 */
3306 public function contributionGetnCheck($params, $id, $delete = TRUE) {
3307
3308 $contribution = $this->callAPISuccess('Contribution', 'Get', array(
3309 'id' => $id,
3310
3311 ));
3312
3313 if ($delete) {
3314 $this->callAPISuccess('contribution', 'delete', array('id' => $id));
3315 }
3316 $this->assertAPISuccess($contribution, 0);
3317 $values = $contribution['values'][$contribution['id']];
3318 $params['receive_date'] = date('Y-m-d H:i:s', strtotime($params['receive_date']));
3319 // this is not returned in id format
3320 unset($params['payment_instrument_id']);
3321 $params['contribution_source'] = $params['source'];
3322 unset($params['source']);
3323 foreach ($params as $key => $value) {
3324 $this->assertEquals($value, $values[$key], $key . " value: $value doesn't match " . print_r($values, TRUE));
3325 }
3326 }
3327
3328 /**
3329 * Create a pending contribution & linked pending pledge record.
3330 */
3331 public function createPendingPledgeContribution() {
3332
3333 $pledgeID = $this->pledgeCreate(array('contact_id' => $this->_individualId, 'installments' => 1, 'amount' => 500));
3334 $this->_ids['pledge'] = $pledgeID;
3335 $contribution = $this->callAPISuccess('contribution', 'create', array_merge($this->_params, array(
3336 'contribution_status_id' => 'Pending',
3337 'total_amount' => 500,
3338 ))
3339 );
3340 $paymentID = $this->callAPISuccessGetValue('PledgePayment', array(
3341 'options' => array('limit' => 1),
3342 'return' => 'id',
3343 ));
3344 $this->callAPISuccess('PledgePayment', 'create', array(
3345 'id' => $paymentID,
3346 'contribution_id' =>
3347 $contribution['id'],
3348 'status_id' => 'Pending',
3349 'scheduled_amount' => 500,
3350 ));
3351
3352 return $contribution['id'];
3353 }
3354
3355 /**
3356 * Create a pending contribution & linked pending participant record (along with an event).
3357 */
3358 public function createPendingParticipantContribution() {
3359 $event = $this->eventCreate(array('is_email_confirm' => 1, 'confirm_from_email' => 'test@civicrm.org'));
3360 $participantID = $this->participantCreate(array('event_id' => $event['id'], 'status_id' => 6, 'contact_id' => $this->_individualId));
3361 $this->_ids['participant'] = $participantID;
3362 $params = array_merge($this->_params, array('contact_id' => $this->_individualId, 'contribution_status_id' => 2, 'financial_type_id' => 'Event Fee'));
3363 $contribution = $this->callAPISuccess('contribution', 'create', $params);
3364 $this->callAPISuccess('participant_payment', 'create', array(
3365 'contribution_id' => $contribution['id'],
3366 'participant_id' => $participantID,
3367 ));
3368 $this->callAPISuccess('line_item', 'get', array(
3369 'entity_id' => $contribution['id'],
3370 'entity_table' => 'civicrm_contribution',
3371 'api.line_item.create' => array(
3372 'entity_id' => $participantID,
3373 'entity_table' => 'civicrm_participant',
3374 ),
3375 ));
3376 return $contribution['id'];
3377 }
3378
3379 /**
3380 * Get financial transaction amount.
3381 *
3382 * @param int $contId
3383 *
3384 * @return null|string
3385 */
3386 public function _getFinancialTrxnAmount($contId) {
3387 $query = "SELECT
3388 SUM( ft.total_amount ) AS total
3389 FROM civicrm_financial_trxn AS ft
3390 LEFT JOIN civicrm_entity_financial_trxn AS ceft ON ft.id = ceft.financial_trxn_id
3391 WHERE ceft.entity_table = 'civicrm_contribution'
3392 AND ceft.entity_id = {$contId}";
3393
3394 $result = CRM_Core_DAO::singleValueQuery($query);
3395 return $result;
3396 }
3397
3398 /**
3399 * @param int $contId
3400 *
3401 * @return null|string
3402 */
3403 public function _getFinancialItemAmount($contId) {
3404 $lineItem = key(CRM_Price_BAO_LineItem::getLineItems($contId, 'contribution'));
3405 $query = "SELECT
3406 SUM(amount)
3407 FROM civicrm_financial_item
3408 WHERE entity_table = 'civicrm_line_item'
3409 AND entity_id = {$lineItem}";
3410 $result = CRM_Core_DAO::singleValueQuery($query);
3411 return $result;
3412 }
3413
3414 /**
3415 * @param int $contId
3416 * @param $context
3417 */
3418 public function _checkFinancialItem($contId, $context) {
3419 if ($context != 'paylater') {
3420 $params = array(
3421 'entity_id' => $contId,
3422 'entity_table' => 'civicrm_contribution',
3423 );
3424 $trxn = current(CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($params, TRUE));
3425 $entityParams = array(
3426 'financial_trxn_id' => $trxn['financial_trxn_id'],
3427 'entity_table' => 'civicrm_financial_item',
3428 );
3429 $entityTrxn = current(CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($entityParams));
3430 $params = array(
3431 'id' => $entityTrxn['entity_id'],
3432 );
3433 }
3434 if ($context == 'paylater') {
3435 $lineItems = CRM_Price_BAO_LineItem::getLineItems($contId, 'contribution');
3436 foreach ($lineItems as $key => $item) {
3437 $params = array(
3438 'entity_id' => $key,
3439 'entity_table' => 'civicrm_line_item',
3440 );
3441 $compareParams = array('status_id' => 1);
3442 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $params, $compareParams);
3443 }
3444 }
3445 elseif ($context == 'refund') {
3446 $compareParams = array(
3447 'status_id' => 1,
3448 'financial_account_id' => 1,
3449 'amount' => -100,
3450 );
3451 }
3452 elseif ($context == 'cancelPending') {
3453 $compareParams = array(
3454 'status_id' => 3,
3455 'financial_account_id' => 1,
3456 'amount' => -100,
3457 );
3458 }
3459 elseif ($context == 'changeFinancial') {
3460 $lineKey = key(CRM_Price_BAO_LineItem::getLineItems($contId, 'contribution'));
3461 $params = array(
3462 'entity_id' => $lineKey,
3463 'amount' => -100,
3464 );
3465 $compareParams = array(
3466 'financial_account_id' => 1,
3467 );
3468 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $params, $compareParams);
3469 $params = array(
3470 'financial_account_id' => 3,
3471 'entity_id' => $lineKey,
3472 );
3473 $compareParams = array(
3474 'amount' => 100,
3475 );
3476 }
3477 if ($context != 'paylater') {
3478 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $params, $compareParams);
3479 }
3480 }
3481
3482 /**
3483 * Check correct financial transaction entries were created for the change in payment instrument.
3484 *
3485 * @param int $contributionID
3486 * @param int $originalInstrumentID
3487 * @param int $newInstrumentID
3488 */
3489 public function checkFinancialTrxnPaymentInstrumentChange($contributionID, $originalInstrumentID, $newInstrumentID, $amount = 100) {
3490
3491 $entityFinancialTrxns = $this->getFinancialTransactionsForContribution($contributionID);
3492
3493 $originalTrxnParams = array(
3494 'to_financial_account_id' => CRM_Financial_BAO_FinancialTypeAccount::getInstrumentFinancialAccount($originalInstrumentID),
3495 'payment_instrument_id' => $originalInstrumentID,
3496 'amount' => $amount,
3497 'status_id' => 1,
3498 );
3499
3500 $reversalTrxnParams = array(
3501 'to_financial_account_id' => CRM_Financial_BAO_FinancialTypeAccount::getInstrumentFinancialAccount($originalInstrumentID),
3502 'payment_instrument_id' => $originalInstrumentID,
3503 'amount' => -$amount,
3504 'status_id' => 1,
3505 );
3506
3507 $newTrxnParams = array(
3508 'to_financial_account_id' => CRM_Financial_BAO_FinancialTypeAccount::getInstrumentFinancialAccount($newInstrumentID),
3509 'payment_instrument_id' => $newInstrumentID,
3510 'amount' => $amount,
3511 'status_id' => 1,
3512 );
3513
3514 foreach (array($originalTrxnParams, $reversalTrxnParams, $newTrxnParams) as $index => $transaction) {
3515 $entityFinancialTrxn = $entityFinancialTrxns[$index];
3516 $this->assertEquals($entityFinancialTrxn['amount'], $transaction['amount']);
3517
3518 $financialTrxn = $this->callAPISuccessGetSingle('FinancialTrxn', array(
3519 'id' => $entityFinancialTrxn['financial_trxn_id'],
3520 ));
3521 $this->assertEquals($transaction['status_id'], $financialTrxn['status_id']);
3522 $this->assertEquals($transaction['amount'], $financialTrxn['total_amount']);
3523 $this->assertEquals($transaction['amount'], $financialTrxn['net_amount']);
3524 $this->assertEquals(0, $financialTrxn['fee_amount']);
3525 $this->assertEquals($transaction['payment_instrument_id'], $financialTrxn['payment_instrument_id']);
3526 $this->assertEquals($transaction['to_financial_account_id'], $financialTrxn['to_financial_account_id']);
3527
3528 // Generic checks.
3529 $this->assertEquals(1, $financialTrxn['is_payment']);
3530 $this->assertEquals('USD', $financialTrxn['currency']);
3531 $this->assertEquals(date('Y-m-d'), date('Y-m-d', strtotime($financialTrxn['trxn_date'])));
3532 }
3533 }
3534
3535 /**
3536 * Check financial transaction.
3537 *
3538 * @todo break this down into sensible functions - most calls to it only use a few lines out of the big if.
3539 *
3540 * @param array $contribution
3541 * @param string $context
3542 * @param int $instrumentId
3543 * @param array $extraParams
3544 */
3545 public function _checkFinancialTrxn($contribution, $context, $instrumentId = NULL, $extraParams = array()) {
3546 $financialTrxns = $this->getFinancialTransactionsForContribution($contribution['id']);
3547 $trxn = array_pop($financialTrxns);
3548
3549 $params = array(
3550 'id' => $trxn['financial_trxn_id'],
3551 );
3552 if ($context == 'payLater') {
3553 $compareParams = array(
3554 'status_id' => 1,
3555 'from_financial_account_id' => CRM_Contribute_PseudoConstant::getRelationalFinancialAccount($contribution['financial_type_id'], 'Accounts Receivable Account is'),
3556 );
3557 }
3558 elseif ($context == 'refund') {
3559 $compareParams = array(
3560 'to_financial_account_id' => 6,
3561 'total_amount' => -100,
3562 'status_id' => 7,
3563 'trxn_date' => '2015-01-01 09:00:00',
3564 'trxn_id' => 'the refund',
3565 );
3566 }
3567 elseif ($context == 'cancelPending') {
3568 $compareParams = array(
3569 'to_financial_account_id' => 7,
3570 'total_amount' => -100,
3571 'status_id' => 3,
3572 );
3573 }
3574 elseif ($context == 'changeFinancial' || $context == 'paymentInstrument') {
3575 // @todo checkFinancialTrxnPaymentInstrumentChange instead for paymentInstrument.
3576 // It does the same thing with greater readability.
3577 // @todo remove handling for
3578
3579 $entityParams = array(
3580 'entity_id' => $contribution['id'],
3581 'entity_table' => 'civicrm_contribution',
3582 'amount' => -100,
3583 );
3584 $trxn = current(CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($entityParams));
3585 $trxnParams1 = array(
3586 'id' => $trxn['financial_trxn_id'],
3587 );
3588 if (empty($extraParams)) {
3589 $compareParams = array(
3590 'total_amount' => -100,
3591 'status_id' => 1,
3592 );
3593 }
3594 else {
3595 $compareParams = array(
3596 'total_amount' => 100,
3597 'status_id' => 1,
3598 );
3599 }
3600 if ($context == 'paymentInstrument') {
3601 $compareParams['to_financial_account_id'] = CRM_Financial_BAO_FinancialTypeAccount::getInstrumentFinancialAccount($instrumentId);
3602 $compareParams['payment_instrument_id'] = $instrumentId;
3603 }
3604 else {
3605 $compareParams['to_financial_account_id'] = 12;
3606 }
3607 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialTrxn', $trxnParams1, array_merge($compareParams, $extraParams));
3608 $compareParams['total_amount'] = 100;
3609 }
3610
3611 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialTrxn', $params, array_merge($compareParams, $extraParams));
3612 }
3613
3614 /**
3615 * @return mixed
3616 */
3617 public function _addPaymentInstrument() {
3618 $gId = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionGroup', 'payment_instrument', 'id', 'name');
3619 $optionParams = array(
3620 'option_group_id' => $gId,
3621 'label' => 'Test Card',
3622 'name' => 'Test Card',
3623 'value' => '6',
3624 'weight' => '6',
3625 'is_active' => 1,
3626 );
3627 $optionValue = $this->callAPISuccess('option_value', 'create', $optionParams);
3628 $relationTypeId = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Asset Account is' "));
3629 $financialParams = array(
3630 'entity_table' => 'civicrm_option_value',
3631 'entity_id' => $optionValue['id'],
3632 'account_relationship' => $relationTypeId,
3633 'financial_account_id' => 7,
3634 );
3635 CRM_Financial_BAO_FinancialTypeAccount::add($financialParams, CRM_Core_DAO::$_nullArray);
3636 $this->assertNotEmpty($optionValue['values'][$optionValue['id']]['value']);
3637 return $optionValue['values'][$optionValue['id']]['value'];
3638 }
3639
3640 public function _deletedAddedPaymentInstrument() {
3641 $result = $this->callAPISuccess('OptionValue', 'get', array(
3642 'option_group_id' => 'payment_instrument',
3643 'name' => 'Test Card',
3644 'value' => '6',
3645 'is_active' => 1,
3646 ));
3647 if ($id = CRM_Utils_Array::value('id', $result)) {
3648 $this->callAPISuccess('OptionValue', 'delete', array('id' => $id));
3649 }
3650 }
3651
3652 /**
3653 * Set up the basic recurring contribution for tests.
3654 *
3655 * @param array $generalParams
3656 * Parameters that can be merged into the recurring AND the contribution.
3657 *
3658 * @param array $recurParams
3659 * Parameters to merge into the recur only.
3660 *
3661 * @return array|int
3662 */
3663 protected function setUpRecurringContribution($generalParams = array(), $recurParams = array()) {
3664 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array_merge(array(
3665 'contact_id' => $this->_individualId,
3666 'installments' => '12',
3667 'frequency_interval' => '1',
3668 'amount' => '100',
3669 'contribution_status_id' => 1,
3670 'start_date' => '2012-01-01 00:00:00',
3671 'currency' => 'USD',
3672 'frequency_unit' => 'month',
3673 'payment_processor_id' => $this->paymentProcessorID,
3674 ), $generalParams, $recurParams));
3675 $originalContribution = $this->callAPISuccess('contribution', 'create', array_merge(
3676 $this->_params,
3677 array(
3678 'contribution_recur_id' => $contributionRecur['id'],
3679 ), $generalParams)
3680 );
3681 return $originalContribution;
3682 }
3683
3684 /**
3685 * Set up a basic auto-renew membership for tests.
3686 *
3687 * @param array $generalParams
3688 * Parameters that can be merged into the recurring AND the contribution.
3689 *
3690 * @param array $recurParams
3691 * Parameters to merge into the recur only.
3692 *
3693 * @return array|int
3694 */
3695 protected function setUpAutoRenewMembership($generalParams = array(), $recurParams = array()) {
3696 $newContact = $this->callAPISuccess('Contact', 'create', array(
3697 'contact_type' => 'Individual',
3698 'sort_name' => 'McTesterson, Testy',
3699 'display_name' => 'Testy McTesterson',
3700 'preferred_language' => 'en_US',
3701 'preferred_mail_format' => 'Both',
3702 'first_name' => 'Testy',
3703 'last_name' => 'McTesterson',
3704 'contact_is_deleted' => '0',
3705 'email_id' => '4',
3706 'email' => 'tmctesterson@example.com',
3707 'on_hold' => '0',
3708 ));
3709 $membershipType = $this->callAPISuccess('MembershipType', 'create', array(
3710 'domain_id' => "Default Domain Name",
3711 'member_of_contact_id' => 1,
3712 'financial_type_id' => "Member Dues",
3713 'duration_unit' => "month",
3714 'duration_interval' => 1,
3715 'period_type' => 'rolling',
3716 'name' => "Standard Member",
3717 'minimum_fee' => 100,
3718 ));
3719 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array_merge(array(
3720 'contact_id' => $newContact['id'],
3721 'installments' => '12',
3722 'frequency_interval' => '1',
3723 'amount' => '100',
3724 'contribution_status_id' => 1,
3725 'start_date' => '2012-01-01 00:00:00',
3726 'currency' => 'USD',
3727 'frequency_unit' => 'month',
3728 'payment_processor_id' => $this->paymentProcessorID,
3729 ), $generalParams, $recurParams));
3730
3731 $membership = $this->callAPISuccess('membership', 'create', array(
3732 'contact_id' => $newContact['id'],
3733 'contribution_recur_id' => $contributionRecur['id'],
3734 'financial_type_id' => "Member Dues",
3735 'membership_type_id' => $membershipType['id'],
3736 'num_terms' => 1,
3737 'skipLineItem' => TRUE,
3738 ));
3739
3740 CRM_Price_BAO_LineItem::getLineItemArray($this->_params, NULL, 'membership', $membershipType['id']);
3741 $originalContribution = $this->callAPISuccess('contribution', 'create', array_merge(
3742 $this->_params,
3743 array(
3744 'contact_id' => $newContact['id'],
3745 'contribution_recur_id' => $contributionRecur['id'],
3746 'financial_type_id' => "Member Dues",
3747 'contribution_status_id' => 1,
3748 'invoice_id' => uniqid(),
3749 ), $generalParams)
3750 );
3751 $lineItem = $this->callAPISuccess('LineItem', 'getsingle', array());
3752 $this->assertEquals('civicrm_membership', $lineItem['entity_table']);
3753 $membership = $this->callAPISuccess('Membership', 'getsingle', array('id' => $lineItem['entity_id']));
3754 $this->callAPISuccess('LineItem', 'getsingle', array());
3755 $this->callAPISuccessGetCount('MembershipPayment', array('membership_id' => $membership['id']), 1);
3756
3757 return array($originalContribution, $membership);
3758 }
3759 /**
3760 * Set up a repeat transaction.
3761 *
3762 * @param array $recurParams
3763 *
3764 * @return array
3765 */
3766 protected function setUpRepeatTransaction($recurParams = array(), $flag, $contributionParams = array()) {
3767 $paymentProcessorID = $this->paymentProcessorCreate();
3768 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array_merge(array(
3769 'contact_id' => $this->_individualId,
3770 'installments' => '12',
3771 'frequency_interval' => '1',
3772 'amount' => '500',
3773 'contribution_status_id' => 1,
3774 'start_date' => '2012-01-01 00:00:00',
3775 'currency' => 'USD',
3776 'frequency_unit' => 'month',
3777 'payment_processor_id' => $paymentProcessorID,
3778 ), $recurParams));
3779
3780 $originalContribution = '';
3781 if ($flag == 'multiple') {
3782 // CRM-19309 create a contribution + also add in line_items (plural):
3783 $params = array_merge($this->_params, $contributionParams);
3784 $originalContribution = $this->callAPISuccess('contribution', 'create', array_merge(
3785 $params,
3786 array(
3787 'contribution_recur_id' => $contributionRecur['id'],
3788 'skipLineItem' => 1,
3789 'api.line_item.create' => array(
3790 array(
3791 'price_field_id' => 1,
3792 'qty' => 2,
3793 'line_total' => '20',
3794 'unit_price' => '10',
3795 'financial_type_id' => 1,
3796 ),
3797 array(
3798 'price_field_id' => 1,
3799 'qty' => 1,
3800 'line_total' => '80',
3801 'unit_price' => '80',
3802 'financial_type_id' => 2,
3803 ),
3804 ),
3805 )
3806 )
3807 );
3808 }
3809 elseif ($flag == 'single') {
3810 $params = array_merge($this->_params, array('contribution_recur_id' => $contributionRecur['id']));
3811 $params = array_merge($params, $contributionParams);
3812 $originalContribution = $this->callAPISuccess('contribution', 'create', $params);
3813 }
3814 $originalContribution['payment_processor_id'] = $paymentProcessorID;
3815 return $originalContribution;
3816 }
3817
3818 /**
3819 * Common set up routine.
3820 *
3821 * @return array
3822 */
3823 protected function setUpForCompleteTransaction() {
3824 $this->mut = new CiviMailUtils($this, TRUE);
3825 $this->createLoggedInUser();
3826 $params = array_merge($this->_params, array('contribution_status_id' => 2, 'receipt_date' => 'now'));
3827 $contribution = $this->callAPISuccess('contribution', 'create', $params);
3828 return $contribution;
3829 }
3830
3831 /**
3832 * Test repeat contribution uses the Payment Processor' payment_instrument setting.
3833 */
3834 public function testRepeatTransactionWithNonCreditCardDefault() {
3835 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array(
3836 'contact_id' => $this->_individualId,
3837 'installments' => '12',
3838 'frequency_interval' => '1',
3839 'amount' => '100',
3840 'contribution_status_id' => 1,
3841 'start_date' => '2012-01-01 00:00:00',
3842 'currency' => 'USD',
3843 'frequency_unit' => 'month',
3844 'payment_processor_id' => $this->paymentProcessorID,
3845 ));
3846 $contribution1 = $this->callAPISuccess('contribution', 'create', array_merge(
3847 $this->_params,
3848 array('contribution_recur_id' => $contributionRecur['id'], 'payment_instrument_id' => 2))
3849 );
3850 $paymentInstruments = CRM_Contribute_PseudoConstant::paymentInstrument('name');
3851 $contribution2 = $this->callAPISuccess('contribution', 'repeattransaction', array(
3852 'contribution_status_id' => 'Completed',
3853 'trxn_id' => uniqid(),
3854 'original_contribution_id' => $contribution1,
3855 ));
3856 $this->assertEquals(array_search('Debit Card', $paymentInstruments), $contribution2['values'][$contribution2['id']]['payment_instrument_id']);
3857 $this->quickCleanUpFinancialEntities();
3858 }
3859
3860 /**
3861 * CRM-20008 Tests repeattransaction creates pending membership.
3862 */
3863 public function testRepeatTransactionPendingMembership() {
3864 list($originalContribution, $membership) = $this->setUpAutoRenewMembership();
3865 $this->callAPISuccess('membership', 'create', array(
3866 'id' => $membership['id'],
3867 'end_date' => 'yesterday',
3868 'status_id' => 'Expired',
3869 ));
3870 $repeatedContribution = $this->callAPISuccess('contribution', 'repeattransaction', array(
3871 'contribution_recur_id' => $originalContribution['values'][1]['contribution_recur_id'],
3872 'contribution_status_id' => 'Pending',
3873 'trxn_id' => uniqid(),
3874 ));
3875 $membershipStatusId = $this->callAPISuccess('membership', 'getvalue', array(
3876 'id' => $membership['id'],
3877 'return' => 'status_id',
3878 ));
3879
3880 // Let's see if the membership payments got created while we're at it.
3881 $membershipPayments = $this->callAPISuccess('MembershipPayment', 'get', array(
3882 'memberhip_id' => $membership['id'],
3883 ));
3884 $this->assertEquals(2, $membershipPayments['count']);
3885
3886 $this->assertEquals('Expired', CRM_Core_PseudoConstant::getLabel('CRM_Member_BAO_Membership', 'status_id', $membershipStatusId));
3887 $this->callAPISuccess('Contribution', 'completetransaction', array('id' => $repeatedContribution['id']));
3888 $membership = $this->callAPISuccessGetSingle('membership', array(
3889 'id' => $membership['id'],
3890 'return' => 'status_id, end_date',
3891 ));
3892 $this->assertEquals('New', CRM_Core_PseudoConstant::getLabel('CRM_Member_BAO_Membership', 'status_id', $membership['status_id']));
3893 $this->assertEquals(date('Y-m-d', strtotime('yesterday + 1 month')), $membership['end_date']);
3894
3895 $this->quickCleanUpFinancialEntities();
3896 $this->contactDelete($originalContribution['values'][1]['contact_id']);
3897 }
3898
3899 /**
3900 * Test sending a mail via the API.
3901 */
3902 public function testSendMailWithAPISetFromDetails() {
3903 $mut = new CiviMailUtils($this, TRUE);
3904 $contribution = $this->callAPISuccess('contribution', 'create', $this->_params);
3905 $this->callAPISuccess('contribution', 'sendconfirmation', array(
3906 'id' => $contribution['id'],
3907 'receipt_from_email' => 'api@civicrm.org',
3908 'receipt_from_name' => 'CiviCRM LLC',
3909 ));
3910 $mut->checkMailLog(array(
3911 'From: CiviCRM LLC <api@civicrm.org>',
3912 'Contribution Information',
3913 'Please print this confirmation for your records',
3914 ), array(
3915 'Event',
3916 )
3917 );
3918 $mut->stop();
3919 }
3920
3921 /**
3922 * Test sending a mail via the API.
3923 */
3924 public function testSendMailWithNoFromSetFallToDomain() {
3925 $this->createLoggedInUser();
3926 $mut = new CiviMailUtils($this, TRUE);
3927 $contribution = $this->callAPISuccess('contribution', 'create', $this->_params);
3928 $this->callAPISuccess('contribution', 'sendconfirmation', array(
3929 'id' => $contribution['id'],
3930 ));
3931 $domain = $this->callAPISuccess('domain', 'getsingle', array('id' => 1));
3932 $mut->checkMailLog(array(
3933 'From: ' . $domain['from_name'] . ' <' . $domain['from_email'] . '>',
3934 'Contribution Information',
3935 'Please print this confirmation for your records',
3936 ), array(
3937 'Event',
3938 )
3939 );
3940 $mut->stop();
3941 }
3942
3943 /**
3944 * Test sending a mail via the API.
3945 */
3946 public function testSendMailWithRepeatTransactionAPIFalltoDomain() {
3947 $this->createLoggedInUser();
3948 $mut = new CiviMailUtils($this, TRUE);
3949 $contribution = $this->setUpRepeatTransaction(array(), 'single');
3950 $this->callAPISuccess('contribution', 'repeattransaction', array(
3951 'contribution_status_id' => 'Completed',
3952 'trxn_id' => uniqid(),
3953 'original_contribution_id' => $contribution,
3954 ));
3955 $domain = $this->callAPISuccess('domain', 'getsingle', array('id' => 1));
3956 $mut->checkMailLog(array(
3957 'From: ' . $domain['from_name'] . ' <' . $domain['from_email'] . '>',
3958 'Contribution Information',
3959 'Please print this confirmation for your records',
3960 ), array(
3961 'Event',
3962 )
3963 );
3964 $mut->stop();
3965 }
3966
3967 /**
3968 * Test sending a mail via the API.
3969 */
3970 public function testSendMailWithRepeatTransactionAPIFalltoContributionPage() {
3971 $mut = new CiviMailUtils($this, TRUE);
3972 $contributionPage = $this->contributionPageCreate(array('receipt_from_name' => 'CiviCRM LLC', 'receipt_from_email' => 'contributionpage@civicrm.org', 'is_email_receipt' => 1));
3973 $paymentProcessorID = $this->paymentProcessorCreate();
3974 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array(
3975 'contact_id' => $this->_individualId,
3976 'installments' => '12',
3977 'frequency_interval' => '1',
3978 'amount' => '500',
3979 'contribution_status_id' => 1,
3980 'start_date' => '2012-01-01 00:00:00',
3981 'currency' => 'USD',
3982 'frequency_unit' => 'month',
3983 'payment_processor_id' => $paymentProcessorID,
3984 ));
3985 $originalContribution = $this->callAPISuccess('contribution', 'create', array_merge(
3986 $this->_params,
3987 array(
3988 'contribution_recur_id' => $contributionRecur['id'],
3989 'contribution_page_id' => $contributionPage['id']))
3990 );
3991 $this->callAPISuccess('contribution', 'repeattransaction', array(
3992 'contribution_status_id' => 'Completed',
3993 'trxn_id' => uniqid(),
3994 'original_contribution_id' => $originalContribution,
3995 )
3996 );
3997 $mut->checkMailLog(array(
3998 'From: CiviCRM LLC <contributionpage@civicrm.org>',
3999 'Contribution Information',
4000 'Please print this confirmation for your records',
4001 ), array(
4002 'Event',
4003 )
4004 );
4005 $mut->stop();
4006 }
4007
4008 /**
4009 * Test sending a mail via the API.
4010 */
4011 public function testSendMailWithRepeatTransactionAPIFalltoSystemFromNoDefaultFrom() {
4012 $mut = new CiviMailUtils($this, TRUE);
4013 $originalContribution = $contribution = $this->setUpRepeatTransaction(array(), 'single');
4014 $fromEmail = $this->CallAPISuccess('optionValue', 'get', array('is_default' => 1, 'option_group_id' => 'from_email_address', 'sequential' => 1));
4015 foreach ($fromEmail['values'] as $from) {
4016 $this->callAPISuccess('optionValue', 'create', array('is_default' => 0, 'id' => $from['id']));
4017 }
4018 $domain = $this->callAPISuccess('domain', 'getsingle', array('id' => CRM_Core_Config::domainID()));
4019 $this->callAPISuccess('contribution', 'repeattransaction', array(
4020 'contribution_status_id' => 'Completed',
4021 'trxn_id' => uniqid(),
4022 'original_contribution_id' => $originalContribution,
4023 )
4024 );
4025 $mut->checkMailLog(array(
4026 'From: ' . $domain['name'] . ' <' . $domain['domain_email'] . '>',
4027 'Contribution Information',
4028 'Please print this confirmation for your records',
4029 ), array(
4030 'Event',
4031 )
4032 );
4033 $mut->stop();
4034 }
4035
4036 /**
4037 * Create a Contribution Page with is_email_receipt = TRUE.
4038 *
4039 * @param array $params
4040 * Params to overwrite with.
4041 *
4042 * @return array|int
4043 */
4044 protected function createReceiptableContributionPage($params = array()) {
4045 $contributionPage = $this->callAPISuccess('ContributionPage', 'create', array_merge(array(
4046 'receipt_from_name' => 'Mickey Mouse',
4047 'receipt_from_email' => 'mickey@mouse.com',
4048 'title' => "Test Contribution Page",
4049 'financial_type_id' => 1,
4050 'currency' => 'CAD',
4051 'is_monetary' => TRUE,
4052 'is_email_receipt' => TRUE,
4053 ), $params));
4054 return $contributionPage;
4055 }
4056
4057 /**
4058 * function to test card_type and pan truncation.
4059 */
4060 public function testCardTypeAndPanTruncation() {
4061 $creditCardTypeIDs = array_flip(CRM_Financial_DAO_FinancialTrxn::buildOptions('card_type_id'));
4062 $contactId = $this->individualCreate();
4063 $params = array(
4064 'contact_id' => $contactId,
4065 'receive_date' => '2016-01-20',
4066 'total_amount' => 100,
4067 'financial_type_id' => 1,
4068 'payment_instrument' => 'Credit Card',
4069 'card_type_id' => $creditCardTypeIDs['Visa'],
4070 'pan_truncation' => 4567,
4071 );
4072 $contribution = $this->callAPISuccess('contribution', 'create', $params);
4073 $lastFinancialTrxnId = CRM_Core_BAO_FinancialTrxn::getFinancialTrxnId($contribution['id'], 'DESC');
4074 $financialTrxn = $this->callAPISuccessGetSingle(
4075 'FinancialTrxn',
4076 array(
4077 'id' => $lastFinancialTrxnId['financialTrxnId'],
4078 'return' => array('card_type_id', 'pan_truncation'),
4079 )
4080 );
4081 $this->assertEquals(CRM_Utils_Array::value('card_type_id', $financialTrxn), $creditCardTypeIDs['Visa']);
4082 $this->assertEquals(CRM_Utils_Array::value('pan_truncation', $financialTrxn), 4567);
4083 $params = array(
4084 'id' => $contribution['id'],
4085 'pan_truncation' => 2345,
4086 'card_type_id' => $creditCardTypeIDs['Amex'],
4087 );
4088 $contribution = $this->callAPISuccess('contribution', 'create', $params);
4089 $financialTrxn = $this->callAPISuccessGetSingle(
4090 'FinancialTrxn',
4091 array(
4092 'id' => $lastFinancialTrxnId['financialTrxnId'],
4093 'return' => array('card_type_id', 'pan_truncation'),
4094 )
4095 );
4096 $this->assertEquals(CRM_Utils_Array::value('card_type_id', $financialTrxn), $creditCardTypeIDs['Amex']);
4097 $this->assertEquals(CRM_Utils_Array::value('pan_truncation', $financialTrxn), 2345);
4098 }
4099
4100 /**
4101 * Test repeat contribution uses non default currency
4102 * @see https://issues.civicrm.org/jira/projects/CRM/issues/CRM-20678
4103 */
4104 public function testRepeatTransactionWithDifferenceCurrency() {
4105 $originalContribution = $this->setUpRepeatTransaction(array('currency' => 'AUD'), 'single', array('currency' => 'AUD'));
4106 $contribution = $this->callAPISuccess('contribution', 'repeattransaction', array(
4107 'original_contribution_id' => $originalContribution['id'],
4108 'contribution_status_id' => 'Completed',
4109 'trxn_id' => uniqid(),
4110 ));
4111 $this->assertEquals('AUD', $contribution['values'][$contribution['id']]['currency']);
4112 }
4113
4114 /**
4115 * Get the financial items for the contribution.
4116 *
4117 * @param int $contributionID
4118 *
4119 * @return array
4120 * Array of associated financial items.
4121 */
4122 protected function getFinancialTransactionsForContribution($contributionID) {
4123 $trxnParams = array(
4124 'entity_id' => $contributionID,
4125 'entity_table' => 'civicrm_contribution',
4126 );
4127 // @todo the following function has naming errors & has a weird signature & appears to
4128 // only be called from test classes. Move into test suite & maybe just use api
4129 // from this function.
4130 return array_merge(CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($trxnParams, FALSE, array()));
4131 }
4132
4133 /**
4134 * Test getunique api call for Contribution entity
4135 */
4136 public function testContributionGetUnique() {
4137 $result = $this->callAPIAndDocument($this->_entity, 'getunique', array(), __FUNCTION__, __FILE__);
4138 $this->assertEquals(2, $result['count']);
4139 $this->assertEquals(array('trxn_id'), $result['values']['UI_contrib_trxn_id']);
4140 $this->assertEquals(array('invoice_id'), $result['values']['UI_contrib_invoice_id']);
4141 }
4142
4143 }