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