3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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 +--------------------------------------------------------------------+
28 require_once 'CiviTest/CiviUnitTestCase.php';
31 * Test APIv3 civicrm_contribute_* functions
33 * @package CiviCRM_APIv3
34 * @subpackage API_ContributionSoft
36 class api_v3_ContributionSoftTest
extends CiviUnitTestCase
{
39 * The hard credit contact.
43 protected $_individualId;
46 * The first soft credit contact.
50 protected $_softIndividual1Id;
53 * The second soft credit contact.
57 protected $_softIndividual2Id;
58 protected $_contributionId;
59 protected $_financialTypeId = 1;
60 protected $_apiversion = 3;
61 protected $_entity = 'Contribution';
66 public function setUp() {
68 $this->useTransaction(TRUE);
70 $this->_individualId
= $this->individualCreate();
71 $this->_softIndividual1Id
= $this->individualCreate();
72 $this->_softIndividual2Id
= $this->individualCreate();
73 $this->_contributionId
= $this->contributionCreate(array('contact_id' => $this->_individualId
));
75 $paymentProcessor = $this->processorCreate();
76 $this->_params
= array(
77 'contact_id' => $this->_individualId
,
78 'receive_date' => '20120511',
79 'total_amount' => 100.00,
80 'financial_type_id' => $this->_financialTypeId
,
81 'non_deductible_amount' => 10.00,
83 'net_amount' => 95.00,
85 'contribution_status_id' => 1,
87 $this->_processorParams
= array(
90 'payment_processor_type_id' => 10,
91 'financial_account_id' => 12,
94 'url_site' => 'http://dummy.com',
95 'url_recur' => 'http://dummy.com',
103 * @todo - this might be better broken down into more smaller tests
105 public function testGetContributionSoft() {
106 //We don't test for PCP fields because there's no PCP API, so we can't create campaigns.
108 'contribution_id' => $this->_contributionId
,
109 'contact_id' => $this->_softIndividual1Id
,
112 'soft_credit_type_id' => 4,
115 $this->_softcontribution
= $this->callAPISuccess('contribution_soft', 'create', $p);
117 'id' => $this->_softcontribution
['id'],
119 $softcontribution = $this->callAPIAndDocument('contribution_soft', 'get', $params, __FUNCTION__
, __FILE__
);
120 $this->assertEquals(1, $softcontribution['count']);
121 $this->assertEquals($softcontribution['values'][$this->_softcontribution
['id']]['contribution_id'], $this->_contributionId
);
122 $this->assertEquals($softcontribution['values'][$this->_softcontribution
['id']]['contact_id'], $this->_softIndividual1Id
);
123 $this->assertEquals($softcontribution['values'][$this->_softcontribution
['id']]['amount'], '10.00');
124 $this->assertEquals($softcontribution['values'][$this->_softcontribution
['id']]['currency'], 'USD');
125 $this->assertEquals($softcontribution['values'][$this->_softcontribution
['id']]['soft_credit_type_id'], 4);
127 //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)
128 $p['contact_id'] = $this->_softIndividual2Id
;
129 $this->_softcontribution2
= $this->callAPISuccess('contribution_soft', 'create', $p);
131 // now we have 2 - test getcount
132 $softcontribution = $this->callAPISuccess('contribution_soft', 'getcount', array());
133 $this->assertEquals(2, $softcontribution);
135 //check first contribution
136 $result = $this->callAPISuccess('contribution_soft', 'get', array(
137 'id' => $this->_softcontribution
['id'],
139 $this->assertEquals(1, $result['count']);
140 $this->assertEquals($this->_softcontribution
['id'], $result['id']);
141 $this->assertEquals($this->_softcontribution
['id'], $result['id'], print_r($softcontribution, TRUE));
143 //test id only format - second soft credit
144 $resultID2 = $this->callAPISuccess('contribution_soft', 'get', array(
145 'id' => $this->_softcontribution2
['id'],
146 'format.only_id' => 1,
148 $this->assertEquals($this->_softcontribution2
['id'], $resultID2);
150 //test get by contact id works
151 $result = $this->callAPISuccess('contribution_soft', 'get', array(
152 'contact_id' => $this->_softIndividual2Id
,
155 $this->assertEquals(1, $result['count']);
157 $this->callAPISuccess('contribution_soft', 'Delete', array(
158 'id' => $this->_softcontribution
['id'],
160 // check one soft credit remains
162 $this->callAPISuccess('contribution_soft', 'getcount', array(), $expectedCount);
164 //check id is same as 2
165 $this->assertEquals($this->_softcontribution2
['id'], $this->callAPISuccess('contribution_soft', 'getvalue', array('return' => 'id')));
167 $this->callAPISuccess('ContributionSoft', 'Delete', array(
168 'id' => $this->_softcontribution2
['id'],
174 * civicrm_contribution_soft.
176 public function testCreateEmptyParamsContributionSoft() {
177 $softcontribution = $this->callAPIFailure('contribution_soft', 'create', array(),
178 'Mandatory key(s) missing from params array: contribution_id, amount, contact_id'
182 public function testCreateParamsWithoutRequiredKeysContributionSoft() {
183 $softcontribution = $this->callAPIFailure('contribution_soft', 'create', array(),
184 'Mandatory key(s) missing from params array: contribution_id, amount, contact_id'
188 public function testCreateContributionSoftInvalidContact() {
191 'contribution_id' => $this->_contributionId
,
196 $softcontribution = $this->callAPIFailure('contribution_soft', 'create', $params,
197 'contact_id is not valid : 999'
201 public function testCreateContributionSoftInvalidContributionId() {
203 'contribution_id' => 999999,
204 'contact_id' => $this->_softIndividual1Id
,
209 $softcontribution = $this->callAPIFailure('contribution_soft', 'create', $params,
210 'contribution_id is not valid : 999999'
215 * Function tests that additional financial records are created when fee amount is recorded.
217 public function testCreateContributionSoft() {
219 'contribution_id' => $this->_contributionId
,
220 'contact_id' => $this->_softIndividual1Id
,
223 'soft_credit_type_id' => 5,
226 $softcontribution = $this->callAPIAndDocument('contribution_soft', 'create', $params, __FUNCTION__
, __FILE__
);
227 $this->assertEquals($softcontribution['values'][$softcontribution['id']]['contribution_id'], $this->_contributionId
);
228 $this->assertEquals($softcontribution['values'][$softcontribution['id']]['contact_id'], $this->_softIndividual1Id
);
229 $this->assertEquals($softcontribution['values'][$softcontribution['id']]['amount'], '10.00');
230 $this->assertEquals($softcontribution['values'][$softcontribution['id']]['currency'], 'USD');
231 $this->assertEquals($softcontribution['values'][$softcontribution['id']]['soft_credit_type_id'], 5);
235 * To Update Soft Contribution.
238 public function testCreateUpdateContributionSoft() {
239 //create a soft credit
241 'contribution_id' => $this->_contributionId
,
242 'contact_id' => $this->_softIndividual1Id
,
245 'soft_credit_type_id' => 6,
248 $softcontribution = $this->callAPISuccess('contribution_soft', 'create', $params);
249 $softcontributionID = $softcontribution['id'];
252 'contribution_soft_id' => $softcontributionID,
254 $original = $this->callAPISuccess('contribution_soft', 'get', $old_params);
255 //Make sure it came back
256 $this->assertEquals($original['id'], $softcontributionID);
257 //set up list of old params, verify
258 $old_contribution_id = $original['values'][$softcontributionID]['contribution_id'];
259 $old_contact_id = $original['values'][$softcontributionID]['contact_id'];
260 $old_amount = $original['values'][$softcontributionID]['amount'];
261 $old_currency = $original['values'][$softcontributionID]['currency'];
262 $old_soft_credit_type_id = $original['values'][$softcontributionID]['soft_credit_type_id'];
264 //check against original values
265 $this->assertEquals($old_contribution_id, $this->_contributionId
);
266 $this->assertEquals($old_contact_id, $this->_softIndividual1Id
);
267 $this->assertEquals($old_amount, 10.00);
268 $this->assertEquals($old_currency, 'USD');
269 $this->assertEquals($old_soft_credit_type_id, 6);
271 'id' => $softcontributionID,
272 'contribution_id' => $this->_contributionId
,
273 'contact_id' => $this->_softIndividual1Id
,
276 'soft_credit_type_id' => 7,
279 $softcontribution = $this->callAPISuccess('contribution_soft', 'create', $params);
282 'id' => $softcontribution['id'],
284 $softcontribution = $this->callAPISuccess('contribution_soft', 'get', $new_params);
285 //check against original values
286 $this->assertEquals($softcontribution['values'][$softcontributionID]['contribution_id'], $this->_contributionId
);
287 $this->assertEquals($softcontribution['values'][$softcontributionID]['contact_id'], $this->_softIndividual1Id
);
288 $this->assertEquals($softcontribution['values'][$softcontributionID]['amount'], 7.00);
289 $this->assertEquals($softcontribution['values'][$softcontributionID]['currency'], 'CAD');
290 $this->assertEquals($softcontribution['values'][$softcontributionID]['soft_credit_type_id'], 7);
293 'id' => $softcontributionID,
295 $result = $this->callAPISuccess('contribution_soft', 'delete', $params);
299 * civicrm_contribution_soft_delete methods.
302 public function testDeleteEmptyParamsContributionSoft() {
304 $softcontribution = $this->callAPIFailure('contribution_soft', 'delete', $params);
307 public function testDeleteWrongParamContributionSoft() {
309 'contribution_source' => 'SSF',
311 $softcontribution = $this->callAPIFailure('contribution_soft', 'delete', $params);
314 public function testDeleteContributionSoft() {
315 //create a soft credit
317 'contribution_id' => $this->_contributionId
,
318 'contact_id' => $this->_softIndividual1Id
,
323 $softcontribution = $this->callAPISuccess('contribution_soft', 'create', $params);
324 $softcontributionID = $softcontribution['id'];
326 'id' => $softcontributionID,
328 $result = $this->callAPIAndDocument('contribution_soft', 'delete', $params, __FUNCTION__
, __FILE__
);
331 ///////////////// civicrm_contribution_search methods
334 * Test civicrm_contribution_search with empty params.
335 * All available contributions expected.
337 public function testSearchEmptyParams() {
339 'contribution_id' => $this->_contributionId
,
340 'contact_id' => $this->_softIndividual1Id
,
344 $softcontribution = $this->callAPISuccess('contribution_soft', 'create', $p);
346 $result = $this->callAPISuccess('contribution_soft', 'get', array());
347 // We're taking the first element.
348 $res = $result['values'][$softcontribution['id']];
350 $this->assertEquals($p['contribution_id'], $res['contribution_id']);
351 $this->assertEquals($p['contact_id'], $res['contact_id']);
352 $this->assertEquals($p['amount'], $res['amount']);
353 $this->assertEquals($p['currency'], $res['currency']);
357 * Test civicrm_contribution_soft_search. Success expected.
359 public function testSearch() {
361 'contribution_id' => $this->_contributionId
,
362 'contact_id' => $this->_softIndividual1Id
,
366 $softcontribution1 = $this->callAPISuccess('contribution_soft', 'create', $p1);
369 'contribution_id' => $this->_contributionId
,
370 'contact_id' => $this->_softIndividual2Id
,
374 $softcontribution2 = $this->callAPISuccess('contribution_soft', 'create', $p2);
377 'id' => $softcontribution2['id'],
379 $result = $this->callAPISuccess('contribution_soft', 'get', $params);
380 $res = $result['values'][$softcontribution2['id']];
382 $this->assertEquals($p2['contribution_id'], $res['contribution_id']);
383 $this->assertEquals($p2['contact_id'], $res['contact_id']);
384 $this->assertEquals($p2['amount'], $res['amount']);
385 $this->assertEquals($p2['currency'], $res['currency']);