3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
13 * Test APIv3 civicrm_contribute_* functions
15 * @package CiviCRM_APIv3
16 * @subpackage API_ContributionSoft
19 class api_v3_ContributionSoftTest
extends CiviUnitTestCase
{
22 * The hard credit contact.
26 protected $_individualId;
29 * The first soft credit contact.
33 protected $_softIndividual1Id;
36 * The second soft credit contact.
40 protected $_softIndividual2Id;
41 protected $_contributionId;
42 protected $_financialTypeId = 1;
43 protected $_apiversion = 3;
44 protected $_entity = 'Contribution';
48 public function setUp() {
50 $this->useTransaction(TRUE);
52 $this->_individualId
= $this->individualCreate();
53 $this->_softIndividual1Id
= $this->individualCreate();
54 $this->_softIndividual2Id
= $this->individualCreate();
55 $this->_contributionId
= $this->contributionCreate(['contact_id' => $this->_individualId
]);
57 $this->processorCreate();
59 'contact_id' => $this->_individualId
,
60 'receive_date' => '20120511',
61 'total_amount' => 100.00,
62 'financial_type_id' => $this->_financialTypeId
,
63 'non_deductible_amount' => 10.00,
65 'net_amount' => 95.00,
67 'contribution_status_id' => 1,
69 $this->_processorParams
= [
72 'payment_processor_type_id' => 10,
73 'financial_account_id' => 12,
76 'url_site' => 'http://dummy.com',
77 'url_recur' => 'http://dummy.com',
85 * @todo - this might be better broken down into more smaller tests
87 public function testGetContributionSoft() {
88 //We don't test for PCP fields because there's no PCP API, so we can't create campaigns.
90 'contribution_id' => $this->_contributionId
,
91 'contact_id' => $this->_softIndividual1Id
,
94 'soft_credit_type_id' => 4,
97 $this->_softcontribution
= $this->callAPISuccess('contribution_soft', 'create', $p);
99 'id' => $this->_softcontribution
['id'],
101 $softcontribution = $this->callAPIAndDocument('contribution_soft', 'get', $params, __FUNCTION__
, __FILE__
);
102 $this->assertEquals(1, $softcontribution['count']);
103 $this->assertEquals($softcontribution['values'][$this->_softcontribution
['id']]['contribution_id'], $this->_contributionId
);
104 $this->assertEquals($softcontribution['values'][$this->_softcontribution
['id']]['contact_id'], $this->_softIndividual1Id
);
105 $this->assertEquals($softcontribution['values'][$this->_softcontribution
['id']]['amount'], '10.00');
106 $this->assertEquals($softcontribution['values'][$this->_softcontribution
['id']]['currency'], 'USD');
107 $this->assertEquals($softcontribution['values'][$this->_softcontribution
['id']]['soft_credit_type_id'], 4);
109 //create a second soft contribution on the same hard contribution - we are testing that 'id' gets the right soft contribution id (not the contribution id)
110 $p['contact_id'] = $this->_softIndividual2Id
;
111 $this->_softcontribution2
= $this->callAPISuccess('contribution_soft', 'create', $p);
113 // now we have 2 - test getcount
114 $softcontribution = $this->callAPISuccess('contribution_soft', 'getcount', []);
115 $this->assertEquals(2, $softcontribution);
117 //check first contribution
118 $result = $this->callAPISuccess('contribution_soft', 'get', [
119 'id' => $this->_softcontribution
['id'],
121 $this->assertEquals(1, $result['count']);
122 $this->assertEquals($this->_softcontribution
['id'], $result['id']);
123 $this->assertEquals($this->_softcontribution
['id'], $result['id'], print_r($softcontribution, TRUE));
125 //test id only format - second soft credit
126 $resultID2 = $this->callAPISuccess('contribution_soft', 'get', [
127 'id' => $this->_softcontribution2
['id'],
128 'format.only_id' => 1,
130 $this->assertEquals($this->_softcontribution2
['id'], $resultID2);
132 //test get by contact id works
133 $result = $this->callAPISuccess('contribution_soft', 'get', [
134 'contact_id' => $this->_softIndividual2Id
,
136 $this->assertEquals(1, $result['count']);
138 $this->callAPISuccess('contribution_soft', 'Delete', [
139 'id' => $this->_softcontribution
['id'],
141 // check one soft credit remains
143 $this->callAPISuccess('contribution_soft', 'getcount', [], $expectedCount);
145 //check id is same as 2
146 $this->assertEquals($this->_softcontribution2
['id'], $this->callAPISuccess('contribution_soft', 'getvalue', ['return' => 'id']));
148 $this->callAPISuccess('ContributionSoft', 'Delete', [
149 'id' => $this->_softcontribution2
['id'],
154 * civicrm_contribution_soft.
156 public function testCreateEmptyParamsContributionSoft() {
157 $softcontribution = $this->callAPIFailure('contribution_soft', 'create', [],
158 'Mandatory key(s) missing from params array: contribution_id, amount, contact_id'
162 public function testCreateParamsWithoutRequiredKeysContributionSoft() {
163 $softcontribution = $this->callAPIFailure('contribution_soft', 'create', [],
164 'Mandatory key(s) missing from params array: contribution_id, amount, contact_id'
168 public function testCreateContributionSoftInvalidContact() {
171 'contribution_id' => $this->_contributionId
,
176 $softcontribution = $this->callAPIFailure('contribution_soft', 'create', $params,
177 'contact_id is not valid : 999'
181 public function testCreateContributionSoftInvalidContributionId() {
183 'contribution_id' => 999999,
184 'contact_id' => $this->_softIndividual1Id
,
189 $softcontribution = $this->callAPIFailure('contribution_soft', 'create', $params,
190 'contribution_id is not valid : 999999'
195 * Function tests that additional financial records are created when fee amount is recorded.
197 public function testCreateContributionSoft() {
199 'contribution_id' => $this->_contributionId
,
200 'contact_id' => $this->_softIndividual1Id
,
203 'soft_credit_type_id' => 5,
206 $softcontribution = $this->callAPIAndDocument('contribution_soft', 'create', $params, __FUNCTION__
, __FILE__
);
207 $this->assertEquals($softcontribution['values'][$softcontribution['id']]['contribution_id'], $this->_contributionId
);
208 $this->assertEquals($softcontribution['values'][$softcontribution['id']]['contact_id'], $this->_softIndividual1Id
);
209 $this->assertEquals($softcontribution['values'][$softcontribution['id']]['amount'], '10.00');
210 $this->assertEquals($softcontribution['values'][$softcontribution['id']]['currency'], 'USD');
211 $this->assertEquals($softcontribution['values'][$softcontribution['id']]['soft_credit_type_id'], 5);
215 * To Update Soft Contribution.
218 public function testCreateUpdateContributionSoft() {
219 //create a soft credit
221 'contribution_id' => $this->_contributionId
,
222 'contact_id' => $this->_softIndividual1Id
,
225 'soft_credit_type_id' => 6,
228 $softcontribution = $this->callAPISuccess('contribution_soft', 'create', $params);
229 $softcontributionID = $softcontribution['id'];
232 'contribution_soft_id' => $softcontributionID,
234 $original = $this->callAPISuccess('contribution_soft', 'get', $old_params);
235 //Make sure it came back
236 $this->assertEquals($original['id'], $softcontributionID);
237 //set up list of old params, verify
238 $old_contribution_id = $original['values'][$softcontributionID]['contribution_id'];
239 $old_contact_id = $original['values'][$softcontributionID]['contact_id'];
240 $old_amount = $original['values'][$softcontributionID]['amount'];
241 $old_currency = $original['values'][$softcontributionID]['currency'];
242 $old_soft_credit_type_id = $original['values'][$softcontributionID]['soft_credit_type_id'];
244 //check against original values
245 $this->assertEquals($old_contribution_id, $this->_contributionId
);
246 $this->assertEquals($old_contact_id, $this->_softIndividual1Id
);
247 $this->assertEquals($old_amount, 10.00);
248 $this->assertEquals($old_currency, 'USD');
249 $this->assertEquals($old_soft_credit_type_id, 6);
251 'id' => $softcontributionID,
252 'contribution_id' => $this->_contributionId
,
253 'contact_id' => $this->_softIndividual1Id
,
256 'soft_credit_type_id' => 7,
259 $softcontribution = $this->callAPISuccess('contribution_soft', 'create', $params);
262 'id' => $softcontribution['id'],
264 $softcontribution = $this->callAPISuccess('contribution_soft', 'get', $new_params);
265 //check against original values
266 $this->assertEquals($softcontribution['values'][$softcontributionID]['contribution_id'], $this->_contributionId
);
267 $this->assertEquals($softcontribution['values'][$softcontributionID]['contact_id'], $this->_softIndividual1Id
);
268 $this->assertEquals($softcontribution['values'][$softcontributionID]['amount'], 7.00);
269 $this->assertEquals($softcontribution['values'][$softcontributionID]['currency'], 'CAD');
270 $this->assertEquals($softcontribution['values'][$softcontributionID]['soft_credit_type_id'], 7);
273 'id' => $softcontributionID,
275 $this->callAPISuccess('contribution_soft', 'delete', $params);
279 * civicrm_contribution_soft_delete methods.
282 public function testDeleteEmptyParamsContributionSoft() {
284 $softcontribution = $this->callAPIFailure('contribution_soft', 'delete', $params);
287 public function testDeleteWrongParamContributionSoft() {
289 'contribution_source' => 'SSF',
291 $this->callAPIFailure('contribution_soft', 'delete', $params);
294 public function testDeleteContributionSoft() {
295 //create a soft credit
297 'contribution_id' => $this->_contributionId
,
298 'contact_id' => $this->_softIndividual1Id
,
303 $softcontribution = $this->callAPISuccess('contribution_soft', 'create', $params);
304 $softcontributionID = $softcontribution['id'];
306 'id' => $softcontributionID,
308 $this->callAPIAndDocument('contribution_soft', 'delete', $params, __FUNCTION__
, __FILE__
);
311 ///////////////// civicrm_contribution_search methods
314 * Test civicrm_contribution_search with empty params.
315 * All available contributions expected.
317 public function testSearchEmptyParams() {
319 'contribution_id' => $this->_contributionId
,
320 'contact_id' => $this->_softIndividual1Id
,
324 $softcontribution = $this->callAPISuccess('contribution_soft', 'create', $p);
326 $result = $this->callAPISuccess('contribution_soft', 'get', []);
327 // We're taking the first element.
328 $res = $result['values'][$softcontribution['id']];
330 $this->assertEquals($p['contribution_id'], $res['contribution_id']);
331 $this->assertEquals($p['contact_id'], $res['contact_id']);
332 $this->assertEquals($p['amount'], $res['amount']);
333 $this->assertEquals($p['currency'], $res['currency']);
337 * Test civicrm_contribution_soft_search. Success expected.
339 public function testSearch() {
341 'contribution_id' => $this->_contributionId
,
342 'contact_id' => $this->_softIndividual1Id
,
346 $softcontribution1 = $this->callAPISuccess('contribution_soft', 'create', $p1);
349 'contribution_id' => $this->_contributionId
,
350 'contact_id' => $this->_softIndividual2Id
,
354 $softcontribution2 = $this->callAPISuccess('contribution_soft', 'create', $p2);
357 'id' => $softcontribution2['id'],
359 $result = $this->callAPISuccess('contribution_soft', 'get', $params);
360 $res = $result['values'][$softcontribution2['id']];
362 $this->assertEquals($p2['contribution_id'], $res['contribution_id']);
363 $this->assertEquals($p2['contact_id'], $res['contact_id']);
364 $this->assertEquals($p2['amount'], $res['amount']);
365 $this->assertEquals($p2['currency'], $res['currency']);