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