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