3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2018 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
29 * Test APIv3 civicrm_contribute_* functions
31 * @package CiviCRM_APIv3
32 * @subpackage API_ContributionSoft
35 class api_v3_ContributionSoftTest
extends CiviUnitTestCase
{
38 * The hard credit contact.
42 protected $_individualId;
45 * The first soft credit contact.
49 protected $_softIndividual1Id;
52 * The second soft credit contact.
56 protected $_softIndividual2Id;
57 protected $_contributionId;
58 protected $_financialTypeId = 1;
59 protected $_apiversion = 3;
60 protected $_entity = 'Contribution';
65 public function setUp() {
67 $this->useTransaction(TRUE);
69 $this->_individualId
= $this->individualCreate();
70 $this->_softIndividual1Id
= $this->individualCreate();
71 $this->_softIndividual2Id
= $this->individualCreate();
72 $this->_contributionId
= $this->contributionCreate(array('contact_id' => $this->_individualId
));
74 $this->processorCreate();
75 $this->_params
= array(
76 'contact_id' => $this->_individualId
,
77 'receive_date' => '20120511',
78 'total_amount' => 100.00,
79 'financial_type_id' => $this->_financialTypeId
,
80 'non_deductible_amount' => 10.00,
82 'net_amount' => 95.00,
84 'contribution_status_id' => 1,
86 $this->_processorParams
= array(
89 'payment_processor_type_id' => 10,
90 'financial_account_id' => 12,
93 'url_site' => 'http://dummy.com',
94 'url_recur' => 'http://dummy.com',
102 * @todo - this might be better broken down into more smaller tests
104 public function testGetContributionSoft() {
105 //We don't test for PCP fields because there's no PCP API, so we can't create campaigns.
107 'contribution_id' => $this->_contributionId
,
108 'contact_id' => $this->_softIndividual1Id
,
111 'soft_credit_type_id' => 4,
114 $this->_softcontribution
= $this->callAPISuccess('contribution_soft', 'create', $p);
116 'id' => $this->_softcontribution
['id'],
118 $softcontribution = $this->callAPIAndDocument('contribution_soft', 'get', $params, __FUNCTION__
, __FILE__
);
119 $this->assertEquals(1, $softcontribution['count']);
120 $this->assertEquals($softcontribution['values'][$this->_softcontribution
['id']]['contribution_id'], $this->_contributionId
);
121 $this->assertEquals($softcontribution['values'][$this->_softcontribution
['id']]['contact_id'], $this->_softIndividual1Id
);
122 $this->assertEquals($softcontribution['values'][$this->_softcontribution
['id']]['amount'], '10.00');
123 $this->assertEquals($softcontribution['values'][$this->_softcontribution
['id']]['currency'], 'USD');
124 $this->assertEquals($softcontribution['values'][$this->_softcontribution
['id']]['soft_credit_type_id'], 4);
126 //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)
127 $p['contact_id'] = $this->_softIndividual2Id
;
128 $this->_softcontribution2
= $this->callAPISuccess('contribution_soft', 'create', $p);
130 // now we have 2 - test getcount
131 $softcontribution = $this->callAPISuccess('contribution_soft', 'getcount', array());
132 $this->assertEquals(2, $softcontribution);
134 //check first contribution
135 $result = $this->callAPISuccess('contribution_soft', 'get', array(
136 'id' => $this->_softcontribution
['id'],
138 $this->assertEquals(1, $result['count']);
139 $this->assertEquals($this->_softcontribution
['id'], $result['id']);
140 $this->assertEquals($this->_softcontribution
['id'], $result['id'], print_r($softcontribution, TRUE));
142 //test id only format - second soft credit
143 $resultID2 = $this->callAPISuccess('contribution_soft', 'get', array(
144 'id' => $this->_softcontribution2
['id'],
145 'format.only_id' => 1,
147 $this->assertEquals($this->_softcontribution2
['id'], $resultID2);
149 //test get by contact id works
150 $result = $this->callAPISuccess('contribution_soft', 'get', array(
151 'contact_id' => $this->_softIndividual2Id
,
154 $this->assertEquals(1, $result['count']);
156 $this->callAPISuccess('contribution_soft', 'Delete', array(
157 'id' => $this->_softcontribution
['id'],
159 // check one soft credit remains
161 $this->callAPISuccess('contribution_soft', 'getcount', array(), $expectedCount);
163 //check id is same as 2
164 $this->assertEquals($this->_softcontribution2
['id'], $this->callAPISuccess('contribution_soft', 'getvalue', array('return' => 'id')));
166 $this->callAPISuccess('ContributionSoft', 'Delete', array(
167 'id' => $this->_softcontribution2
['id'],
173 * civicrm_contribution_soft.
175 public function testCreateEmptyParamsContributionSoft() {
176 $softcontribution = $this->callAPIFailure('contribution_soft', 'create', array(),
177 'Mandatory key(s) missing from params array: contribution_id, amount, contact_id'
181 public function testCreateParamsWithoutRequiredKeysContributionSoft() {
182 $softcontribution = $this->callAPIFailure('contribution_soft', 'create', array(),
183 'Mandatory key(s) missing from params array: contribution_id, amount, contact_id'
187 public function testCreateContributionSoftInvalidContact() {
190 'contribution_id' => $this->_contributionId
,
195 $softcontribution = $this->callAPIFailure('contribution_soft', 'create', $params,
196 'contact_id is not valid : 999'
200 public function testCreateContributionSoftInvalidContributionId() {
202 'contribution_id' => 999999,
203 'contact_id' => $this->_softIndividual1Id
,
208 $softcontribution = $this->callAPIFailure('contribution_soft', 'create', $params,
209 'contribution_id is not valid : 999999'
214 * Function tests that additional financial records are created when fee amount is recorded.
216 public function testCreateContributionSoft() {
218 'contribution_id' => $this->_contributionId
,
219 'contact_id' => $this->_softIndividual1Id
,
222 'soft_credit_type_id' => 5,
225 $softcontribution = $this->callAPIAndDocument('contribution_soft', 'create', $params, __FUNCTION__
, __FILE__
);
226 $this->assertEquals($softcontribution['values'][$softcontribution['id']]['contribution_id'], $this->_contributionId
);
227 $this->assertEquals($softcontribution['values'][$softcontribution['id']]['contact_id'], $this->_softIndividual1Id
);
228 $this->assertEquals($softcontribution['values'][$softcontribution['id']]['amount'], '10.00');
229 $this->assertEquals($softcontribution['values'][$softcontribution['id']]['currency'], 'USD');
230 $this->assertEquals($softcontribution['values'][$softcontribution['id']]['soft_credit_type_id'], 5);
234 * To Update Soft Contribution.
237 public function testCreateUpdateContributionSoft() {
238 //create a soft credit
240 'contribution_id' => $this->_contributionId
,
241 'contact_id' => $this->_softIndividual1Id
,
244 'soft_credit_type_id' => 6,
247 $softcontribution = $this->callAPISuccess('contribution_soft', 'create', $params);
248 $softcontributionID = $softcontribution['id'];
251 'contribution_soft_id' => $softcontributionID,
253 $original = $this->callAPISuccess('contribution_soft', 'get', $old_params);
254 //Make sure it came back
255 $this->assertEquals($original['id'], $softcontributionID);
256 //set up list of old params, verify
257 $old_contribution_id = $original['values'][$softcontributionID]['contribution_id'];
258 $old_contact_id = $original['values'][$softcontributionID]['contact_id'];
259 $old_amount = $original['values'][$softcontributionID]['amount'];
260 $old_currency = $original['values'][$softcontributionID]['currency'];
261 $old_soft_credit_type_id = $original['values'][$softcontributionID]['soft_credit_type_id'];
263 //check against original values
264 $this->assertEquals($old_contribution_id, $this->_contributionId
);
265 $this->assertEquals($old_contact_id, $this->_softIndividual1Id
);
266 $this->assertEquals($old_amount, 10.00);
267 $this->assertEquals($old_currency, 'USD');
268 $this->assertEquals($old_soft_credit_type_id, 6);
270 'id' => $softcontributionID,
271 'contribution_id' => $this->_contributionId
,
272 'contact_id' => $this->_softIndividual1Id
,
275 'soft_credit_type_id' => 7,
278 $softcontribution = $this->callAPISuccess('contribution_soft', 'create', $params);
281 'id' => $softcontribution['id'],
283 $softcontribution = $this->callAPISuccess('contribution_soft', 'get', $new_params);
284 //check against original values
285 $this->assertEquals($softcontribution['values'][$softcontributionID]['contribution_id'], $this->_contributionId
);
286 $this->assertEquals($softcontribution['values'][$softcontributionID]['contact_id'], $this->_softIndividual1Id
);
287 $this->assertEquals($softcontribution['values'][$softcontributionID]['amount'], 7.00);
288 $this->assertEquals($softcontribution['values'][$softcontributionID]['currency'], 'CAD');
289 $this->assertEquals($softcontribution['values'][$softcontributionID]['soft_credit_type_id'], 7);
292 'id' => $softcontributionID,
294 $this->callAPISuccess('contribution_soft', 'delete', $params);
298 * civicrm_contribution_soft_delete methods.
301 public function testDeleteEmptyParamsContributionSoft() {
303 $softcontribution = $this->callAPIFailure('contribution_soft', 'delete', $params);
306 public function testDeleteWrongParamContributionSoft() {
308 'contribution_source' => 'SSF',
310 $this->callAPIFailure('contribution_soft', 'delete', $params);
313 public function testDeleteContributionSoft() {
314 //create a soft credit
316 'contribution_id' => $this->_contributionId
,
317 'contact_id' => $this->_softIndividual1Id
,
322 $softcontribution = $this->callAPISuccess('contribution_soft', 'create', $params);
323 $softcontributionID = $softcontribution['id'];
325 'id' => $softcontributionID,
327 $this->callAPIAndDocument('contribution_soft', 'delete', $params, __FUNCTION__
, __FILE__
);
330 ///////////////// civicrm_contribution_search methods
333 * Test civicrm_contribution_search with empty params.
334 * All available contributions expected.
336 public function testSearchEmptyParams() {
338 'contribution_id' => $this->_contributionId
,
339 'contact_id' => $this->_softIndividual1Id
,
343 $softcontribution = $this->callAPISuccess('contribution_soft', 'create', $p);
345 $result = $this->callAPISuccess('contribution_soft', 'get', array());
346 // We're taking the first element.
347 $res = $result['values'][$softcontribution['id']];
349 $this->assertEquals($p['contribution_id'], $res['contribution_id']);
350 $this->assertEquals($p['contact_id'], $res['contact_id']);
351 $this->assertEquals($p['amount'], $res['amount']);
352 $this->assertEquals($p['currency'], $res['currency']);
356 * Test civicrm_contribution_soft_search. Success expected.
358 public function testSearch() {
360 'contribution_id' => $this->_contributionId
,
361 'contact_id' => $this->_softIndividual1Id
,
365 $softcontribution1 = $this->callAPISuccess('contribution_soft', 'create', $p1);
368 'contribution_id' => $this->_contributionId
,
369 'contact_id' => $this->_softIndividual2Id
,
373 $softcontribution2 = $this->callAPISuccess('contribution_soft', 'create', $p2);
376 'id' => $softcontribution2['id'],
378 $result = $this->callAPISuccess('contribution_soft', 'get', $params);
379 $res = $result['values'][$softcontribution2['id']];
381 $this->assertEquals($p2['contribution_id'], $res['contribution_id']);
382 $this->assertEquals($p2['contact_id'], $res['contact_id']);
383 $this->assertEquals($p2['amount'], $res['amount']);
384 $this->assertEquals($p2['currency'], $res['currency']);