3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
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';
64 public function setUp() {
66 $this->useTransaction(TRUE);
68 $this->_individualId
= $this->individualCreate();
69 $this->_softIndividual1Id
= $this->individualCreate();
70 $this->_softIndividual2Id
= $this->individualCreate();
71 $this->_contributionId
= $this->contributionCreate(array('contact_id' => $this->_individualId
));
73 $this->processorCreate();
74 $this->_params
= array(
75 'contact_id' => $this->_individualId
,
76 'receive_date' => '20120511',
77 'total_amount' => 100.00,
78 'financial_type_id' => $this->_financialTypeId
,
79 'non_deductible_amount' => 10.00,
81 'net_amount' => 95.00,
83 'contribution_status_id' => 1,
85 $this->_processorParams
= array(
88 'payment_processor_type_id' => 10,
89 'financial_account_id' => 12,
92 'url_site' => 'http://dummy.com',
93 'url_recur' => 'http://dummy.com',
101 * @todo - this might be better broken down into more smaller tests
103 public function testGetContributionSoft() {
104 //We don't test for PCP fields because there's no PCP API, so we can't create campaigns.
106 'contribution_id' => $this->_contributionId
,
107 'contact_id' => $this->_softIndividual1Id
,
110 'soft_credit_type_id' => 4,
113 $this->_softcontribution
= $this->callAPISuccess('contribution_soft', 'create', $p);
115 'id' => $this->_softcontribution
['id'],
117 $softcontribution = $this->callAPIAndDocument('contribution_soft', 'get', $params, __FUNCTION__
, __FILE__
);
118 $this->assertEquals(1, $softcontribution['count']);
119 $this->assertEquals($softcontribution['values'][$this->_softcontribution
['id']]['contribution_id'], $this->_contributionId
);
120 $this->assertEquals($softcontribution['values'][$this->_softcontribution
['id']]['contact_id'], $this->_softIndividual1Id
);
121 $this->assertEquals($softcontribution['values'][$this->_softcontribution
['id']]['amount'], '10.00');
122 $this->assertEquals($softcontribution['values'][$this->_softcontribution
['id']]['currency'], 'USD');
123 $this->assertEquals($softcontribution['values'][$this->_softcontribution
['id']]['soft_credit_type_id'], 4);
125 //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)
126 $p['contact_id'] = $this->_softIndividual2Id
;
127 $this->_softcontribution2
= $this->callAPISuccess('contribution_soft', 'create', $p);
129 // now we have 2 - test getcount
130 $softcontribution = $this->callAPISuccess('contribution_soft', 'getcount', array());
131 $this->assertEquals(2, $softcontribution);
133 //check first contribution
134 $result = $this->callAPISuccess('contribution_soft', 'get', array(
135 'id' => $this->_softcontribution
['id'],
137 $this->assertEquals(1, $result['count']);
138 $this->assertEquals($this->_softcontribution
['id'], $result['id']);
139 $this->assertEquals($this->_softcontribution
['id'], $result['id'], print_r($softcontribution, TRUE));
141 //test id only format - second soft credit
142 $resultID2 = $this->callAPISuccess('contribution_soft', 'get', array(
143 'id' => $this->_softcontribution2
['id'],
144 'format.only_id' => 1,
146 $this->assertEquals($this->_softcontribution2
['id'], $resultID2);
148 //test get by contact id works
149 $result = $this->callAPISuccess('contribution_soft', 'get', array(
150 'contact_id' => $this->_softIndividual2Id
,
152 $this->assertEquals(1, $result['count']);
154 $this->callAPISuccess('contribution_soft', 'Delete', array(
155 'id' => $this->_softcontribution
['id'],
157 // check one soft credit remains
159 $this->callAPISuccess('contribution_soft', 'getcount', array(), $expectedCount);
161 //check id is same as 2
162 $this->assertEquals($this->_softcontribution2
['id'], $this->callAPISuccess('contribution_soft', 'getvalue', array('return' => 'id')));
164 $this->callAPISuccess('ContributionSoft', 'Delete', array(
165 'id' => $this->_softcontribution2
['id'],
170 * civicrm_contribution_soft.
172 public function testCreateEmptyParamsContributionSoft() {
173 $softcontribution = $this->callAPIFailure('contribution_soft', 'create', array(),
174 'Mandatory key(s) missing from params array: contribution_id, amount, contact_id'
178 public function testCreateParamsWithoutRequiredKeysContributionSoft() {
179 $softcontribution = $this->callAPIFailure('contribution_soft', 'create', array(),
180 'Mandatory key(s) missing from params array: contribution_id, amount, contact_id'
184 public function testCreateContributionSoftInvalidContact() {
187 'contribution_id' => $this->_contributionId
,
192 $softcontribution = $this->callAPIFailure('contribution_soft', 'create', $params,
193 'contact_id is not valid : 999'
197 public function testCreateContributionSoftInvalidContributionId() {
199 'contribution_id' => 999999,
200 'contact_id' => $this->_softIndividual1Id
,
205 $softcontribution = $this->callAPIFailure('contribution_soft', 'create', $params,
206 'contribution_id is not valid : 999999'
211 * Function tests that additional financial records are created when fee amount is recorded.
213 public function testCreateContributionSoft() {
215 'contribution_id' => $this->_contributionId
,
216 'contact_id' => $this->_softIndividual1Id
,
219 'soft_credit_type_id' => 5,
222 $softcontribution = $this->callAPIAndDocument('contribution_soft', 'create', $params, __FUNCTION__
, __FILE__
);
223 $this->assertEquals($softcontribution['values'][$softcontribution['id']]['contribution_id'], $this->_contributionId
);
224 $this->assertEquals($softcontribution['values'][$softcontribution['id']]['contact_id'], $this->_softIndividual1Id
);
225 $this->assertEquals($softcontribution['values'][$softcontribution['id']]['amount'], '10.00');
226 $this->assertEquals($softcontribution['values'][$softcontribution['id']]['currency'], 'USD');
227 $this->assertEquals($softcontribution['values'][$softcontribution['id']]['soft_credit_type_id'], 5);
231 * To Update Soft Contribution.
234 public function testCreateUpdateContributionSoft() {
235 //create a soft credit
237 'contribution_id' => $this->_contributionId
,
238 'contact_id' => $this->_softIndividual1Id
,
241 'soft_credit_type_id' => 6,
244 $softcontribution = $this->callAPISuccess('contribution_soft', 'create', $params);
245 $softcontributionID = $softcontribution['id'];
248 'contribution_soft_id' => $softcontributionID,
250 $original = $this->callAPISuccess('contribution_soft', 'get', $old_params);
251 //Make sure it came back
252 $this->assertEquals($original['id'], $softcontributionID);
253 //set up list of old params, verify
254 $old_contribution_id = $original['values'][$softcontributionID]['contribution_id'];
255 $old_contact_id = $original['values'][$softcontributionID]['contact_id'];
256 $old_amount = $original['values'][$softcontributionID]['amount'];
257 $old_currency = $original['values'][$softcontributionID]['currency'];
258 $old_soft_credit_type_id = $original['values'][$softcontributionID]['soft_credit_type_id'];
260 //check against original values
261 $this->assertEquals($old_contribution_id, $this->_contributionId
);
262 $this->assertEquals($old_contact_id, $this->_softIndividual1Id
);
263 $this->assertEquals($old_amount, 10.00);
264 $this->assertEquals($old_currency, 'USD');
265 $this->assertEquals($old_soft_credit_type_id, 6);
267 'id' => $softcontributionID,
268 'contribution_id' => $this->_contributionId
,
269 'contact_id' => $this->_softIndividual1Id
,
272 'soft_credit_type_id' => 7,
275 $softcontribution = $this->callAPISuccess('contribution_soft', 'create', $params);
278 'id' => $softcontribution['id'],
280 $softcontribution = $this->callAPISuccess('contribution_soft', 'get', $new_params);
281 //check against original values
282 $this->assertEquals($softcontribution['values'][$softcontributionID]['contribution_id'], $this->_contributionId
);
283 $this->assertEquals($softcontribution['values'][$softcontributionID]['contact_id'], $this->_softIndividual1Id
);
284 $this->assertEquals($softcontribution['values'][$softcontributionID]['amount'], 7.00);
285 $this->assertEquals($softcontribution['values'][$softcontributionID]['currency'], 'CAD');
286 $this->assertEquals($softcontribution['values'][$softcontributionID]['soft_credit_type_id'], 7);
289 'id' => $softcontributionID,
291 $this->callAPISuccess('contribution_soft', 'delete', $params);
295 * civicrm_contribution_soft_delete methods.
298 public function testDeleteEmptyParamsContributionSoft() {
300 $softcontribution = $this->callAPIFailure('contribution_soft', 'delete', $params);
303 public function testDeleteWrongParamContributionSoft() {
305 'contribution_source' => 'SSF',
307 $this->callAPIFailure('contribution_soft', 'delete', $params);
310 public function testDeleteContributionSoft() {
311 //create a soft credit
313 'contribution_id' => $this->_contributionId
,
314 'contact_id' => $this->_softIndividual1Id
,
319 $softcontribution = $this->callAPISuccess('contribution_soft', 'create', $params);
320 $softcontributionID = $softcontribution['id'];
322 'id' => $softcontributionID,
324 $this->callAPIAndDocument('contribution_soft', 'delete', $params, __FUNCTION__
, __FILE__
);
327 ///////////////// civicrm_contribution_search methods
330 * Test civicrm_contribution_search with empty params.
331 * All available contributions expected.
333 public function testSearchEmptyParams() {
335 'contribution_id' => $this->_contributionId
,
336 'contact_id' => $this->_softIndividual1Id
,
340 $softcontribution = $this->callAPISuccess('contribution_soft', 'create', $p);
342 $result = $this->callAPISuccess('contribution_soft', 'get', array());
343 // We're taking the first element.
344 $res = $result['values'][$softcontribution['id']];
346 $this->assertEquals($p['contribution_id'], $res['contribution_id']);
347 $this->assertEquals($p['contact_id'], $res['contact_id']);
348 $this->assertEquals($p['amount'], $res['amount']);
349 $this->assertEquals($p['currency'], $res['currency']);
353 * Test civicrm_contribution_soft_search. Success expected.
355 public function testSearch() {
357 'contribution_id' => $this->_contributionId
,
358 'contact_id' => $this->_softIndividual1Id
,
362 $softcontribution1 = $this->callAPISuccess('contribution_soft', 'create', $p1);
365 'contribution_id' => $this->_contributionId
,
366 'contact_id' => $this->_softIndividual2Id
,
370 $softcontribution2 = $this->callAPISuccess('contribution_soft', 'create', $p2);
373 'id' => $softcontribution2['id'],
375 $result = $this->callAPISuccess('contribution_soft', 'get', $params);
376 $res = $result['values'][$softcontribution2['id']];
378 $this->assertEquals($p2['contribution_id'], $res['contribution_id']);
379 $this->assertEquals($p2['contact_id'], $res['contact_id']);
380 $this->assertEquals($p2['amount'], $res['amount']);
381 $this->assertEquals($p2['currency'], $res['currency']);