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