3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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 * Assume empty database with just civicrm_data
41 protected $_individualId; //the hard credit contact
42 protected $_softIndividual1Id; //the first soft credit contact
43 protected $_softIndividual2Id; //the second soft credit contact
44 protected $_contributionId;
45 protected $_financialTypeId = 1;
46 protected $_apiversion = 3;
47 protected $_entity = 'Contribution';
52 public function setUp() {
54 $this->useTransaction(TRUE);
56 $this->_individualId
= $this->individualCreate();
57 $this->_softIndividual1Id
= $this->individualCreate();
58 $this->_softIndividual2Id
= $this->individualCreate();
59 $this->_contributionId
= $this->contributionCreate($this->_individualId
);
61 $paymentProcessor = $this->processorCreate();
62 $this->_params
= array(
63 'contact_id' => $this->_individualId
,
64 'receive_date' => '20120511',
65 'total_amount' => 100.00,
66 'financial_type_id' => $this->_financialTypeId
,
67 'non_deductible_amount' => 10.00,
69 'net_amount' => 95.00,
71 'contribution_status_id' => 1,
73 $this->_processorParams
= array(
76 'payment_processor_type_id' => 10,
77 'financial_account_id' => 12,
80 'url_site' => 'http://dummy.com',
81 'url_recur' => 'http://dummy.com',
88 * @todo - this might be better broken down into more smaller tests
90 public function testGetContributionSoft() {
91 //We don't test for PCP fields because there's no PCP API, so we can't create campaigns.
93 'contribution_id' => $this->_contributionId
,
94 'contact_id' => $this->_softIndividual1Id
,
97 'soft_credit_type_id' => 4,
100 $this->_softcontribution
= $this->callAPISuccess('contribution_soft', 'create', $p);
102 'id' => $this->_softcontribution
['id'],
104 $softcontribution = $this->callAPIAndDocument('contribution_soft', 'get', $params, __FUNCTION__
, __FILE__
);
105 $this->assertEquals(1, $softcontribution['count']);
106 $this->assertEquals($softcontribution['values'][$this->_softcontribution
['id']]['contribution_id'], $this->_contributionId
, 'In line ' . __LINE__
);
107 $this->assertEquals($softcontribution['values'][$this->_softcontribution
['id']]['contact_id'], $this->_softIndividual1Id
, 'In line ' . __LINE__
);
108 $this->assertEquals($softcontribution['values'][$this->_softcontribution
['id']]['amount'], '10.00', 'In line ' . __LINE__
);
109 $this->assertEquals($softcontribution['values'][$this->_softcontribution
['id']]['currency'], 'USD', 'In line ' . __LINE__
);
110 $this->assertEquals($softcontribution['values'][$this->_softcontribution
['id']]['soft_credit_type_id'], 4, 'In line ' . __LINE__
);
112 //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)
113 $p['contact_id'] = $this->_softIndividual2Id
;
114 $this->_softcontribution2
= $this->callAPISuccess('contribution_soft', 'create', $p);
116 // now we have 2 - test getcount
117 $softcontribution = $this->callAPISuccess('contribution_soft', 'getcount', array());
118 $this->assertEquals(2, $softcontribution);
120 //check first contribution
121 $result = $this->callAPISuccess('contribution_soft', 'get', array(
122 'id' => $this->_softcontribution
['id'],
124 $this->assertEquals(1, $result['count'], 'In line ' . __LINE__
);
125 $this->assertEquals($this->_softcontribution
['id'], $result['id']);
126 $this->assertEquals($this->_softcontribution
['id'], $result['id'], print_r($softcontribution, TRUE));
128 //test id only format - second soft credit
129 $resultID2 = $this->callAPISuccess('contribution_soft', 'get', array(
130 'id' => $this->_softcontribution2
['id'],
131 'format.only_id' => 1,
133 $this->assertEquals($this->_softcontribution2
['id'], $resultID2);
135 //test get by contact id works
136 $result = $this->callAPISuccess('contribution_soft', 'get', array(
137 'contact_id' => $this->_softIndividual2Id
140 $this->assertEquals(1, $result['count'], 'In line ' . __LINE__
);
142 $this->callAPISuccess('contribution_soft', 'Delete', array(
143 'id' => $this->_softcontribution
['id'],
145 // check one soft credit remains
147 $this->callAPISuccess('contribution_soft', 'getcount', array(), $expectedCount);
149 //check id is same as 2
150 $this->assertEquals($this->_softcontribution2
['id'], $this->callAPISuccess('contribution_soft', 'getvalue', array('return' => 'id')));
152 $this->callAPISuccess('ContributionSoft', 'Delete', array(
153 'id' => $this->_softcontribution2
['id'],
158 ///////////////// civicrm_contribution_soft
159 public function testCreateEmptyParamsContributionSoft() {
160 $softcontribution = $this->callAPIFailure('contribution_soft', 'create', array(),
161 'Mandatory key(s) missing from params array: contribution_id, amount, contact_id'
165 public function testCreateParamsWithoutRequiredKeysContributionSoft() {
166 $softcontribution = $this->callAPIFailure('contribution_soft', 'create', array(),
167 'Mandatory key(s) missing from params array: contribution_id, amount, contact_id'
171 public function testCreateContributionSoftInvalidContact() {
175 'contribution_id' => $this->_contributionId
,
180 $softcontribution = $this->callAPIFailure('contribution_soft', 'create', $params,
181 'contact_id is not valid : 999'
185 public function testCreateContributionSoftInvalidContributionId() {
188 'contribution_id' => 999999,
189 'contact_id' => $this->_softIndividual1Id
,
194 $softcontribution = $this->callAPIFailure('contribution_soft', 'create', $params,
195 'contribution_id is not valid : 999999'
200 * Function tests that additional financial records are created when fee amount is recorded
202 public function testCreateContributionSoft() {
204 'contribution_id' => $this->_contributionId
,
205 'contact_id' => $this->_softIndividual1Id
,
208 'soft_credit_type_id' => 5,
211 $softcontribution = $this->callAPIAndDocument('contribution_soft', 'create', $params, __FUNCTION__
, __FILE__
);
212 $this->assertEquals($softcontribution['values'][$softcontribution['id']]['contribution_id'], $this->_contributionId
, 'In line ' . __LINE__
);
213 $this->assertEquals($softcontribution['values'][$softcontribution['id']]['contact_id'], $this->_softIndividual1Id
, 'In line ' . __LINE__
);
214 $this->assertEquals($softcontribution['values'][$softcontribution['id']]['amount'], '10.00', 'In line ' . __LINE__
);
215 $this->assertEquals($softcontribution['values'][$softcontribution['id']]['currency'], 'USD', 'In line ' . __LINE__
);
216 $this->assertEquals($softcontribution['values'][$softcontribution['id']]['soft_credit_type_id'], 5, 'In line ' . __LINE__
);
219 //To Update Soft Contribution
220 public function testCreateUpdateContributionSoft() {
221 //create a soft credit
223 'contribution_id' => $this->_contributionId
,
224 'contact_id' => $this->_softIndividual1Id
,
227 'soft_credit_type_id' => 6,
230 $softcontribution = $this->callAPISuccess('contribution_soft', 'create', $params);
231 $softcontributionID = $softcontribution['id'];
234 'contribution_soft_id' => $softcontributionID,
236 $original = $this->callAPISuccess('contribution_soft', 'get', $old_params);
237 //Make sure it came back
238 $this->assertEquals($original['id'], $softcontributionID, 'In line ' . __LINE__
);
239 //set up list of old params, verify
240 $old_contribution_id = $original['values'][$softcontributionID]['contribution_id'];
241 $old_contact_id = $original['values'][$softcontributionID]['contact_id'];
242 $old_amount = $original['values'][$softcontributionID]['amount'];
243 $old_currency = $original['values'][$softcontributionID]['currency'];
244 $old_soft_credit_type_id = $original['values'][$softcontributionID]['soft_credit_type_id'];
246 //check against original values
247 $this->assertEquals($old_contribution_id, $this->_contributionId
, 'In line ' . __LINE__
);
248 $this->assertEquals($old_contact_id, $this->_softIndividual1Id
, 'In line ' . __LINE__
);
249 $this->assertEquals($old_amount, 10.00, 'In line ' . __LINE__
);
250 $this->assertEquals($old_currency, 'USD', 'In line ' . __LINE__
);
251 $this->assertEquals($old_soft_credit_type_id, 6, 'In line ' . __LINE__
);
253 'id' => $softcontributionID,
254 'contribution_id' => $this->_contributionId
,
255 'contact_id' => $this->_softIndividual1Id
,
258 'soft_credit_type_id' => 7,
261 $softcontribution = $this->callAPISuccess('contribution_soft', 'create', $params);
264 'id' => $softcontribution['id'],
266 $softcontribution = $this->callAPISuccess('contribution_soft', 'get', $new_params);
267 //check against original values
268 $this->assertEquals($softcontribution['values'][$softcontributionID]['contribution_id'], $this->_contributionId
, 'In line ' . __LINE__
);
269 $this->assertEquals($softcontribution['values'][$softcontributionID]['contact_id'], $this->_softIndividual1Id
, 'In line ' . __LINE__
);
270 $this->assertEquals($softcontribution['values'][$softcontributionID]['amount'], 7.00, 'In line ' . __LINE__
);
271 $this->assertEquals($softcontribution['values'][$softcontributionID]['currency'], 'CAD', 'In line ' . __LINE__
);
272 $this->assertEquals($softcontribution['values'][$softcontributionID]['soft_credit_type_id'], 7, 'In line ' . __LINE__
);
275 'id' => $softcontributionID,
277 $result = $this->callAPISuccess('contribution_soft', 'delete', $params);
280 ///////////////// civicrm_contribution_soft_delete methods
281 public function testDeleteEmptyParamsContributionSoft() {
283 $softcontribution = $this->callAPIFailure('contribution_soft', 'delete', $params);
286 public function testDeleteWrongParamContributionSoft() {
288 'contribution_source' => 'SSF',
290 $softcontribution = $this->callAPIFailure('contribution_soft', 'delete', $params);
293 public function testDeleteContributionSoft() {
294 //create a soft credit
296 'contribution_id' => $this->_contributionId
,
297 'contact_id' => $this->_softIndividual1Id
,
302 $softcontribution = $this->callAPISuccess('contribution_soft', 'create', $params);
303 $softcontributionID = $softcontribution['id'];
305 'id' => $softcontributionID,
307 $result = $this->callAPIAndDocument('contribution_soft', 'delete', $params, __FUNCTION__
, __FILE__
);
310 ///////////////// civicrm_contribution_search methods
313 * Test civicrm_contribution_search with empty params.
314 * All available contributions expected.
316 public function testSearchEmptyParams() {
318 'contribution_id' => $this->_contributionId
,
319 'contact_id' => $this->_softIndividual1Id
,
323 $softcontribution = $this->callAPISuccess('contribution_soft', 'create', $p);
325 $result = $this->callAPISuccess('contribution_soft', 'get', array());
326 // We're taking the first element.
327 $res = $result['values'][$softcontribution['id']];
329 $this->assertEquals($p['contribution_id'], $res['contribution_id'], 'In line ' . __LINE__
);
330 $this->assertEquals($p['contact_id'], $res['contact_id'], 'In line ' . __LINE__
);
331 $this->assertEquals($p['amount'], $res['amount'], 'In line ' . __LINE__
);
332 $this->assertEquals($p['currency'], $res['currency'], 'In line ' . __LINE__
);
336 * Test civicrm_contribution_soft_search. Success expected.
338 public function testSearch() {
340 'contribution_id' => $this->_contributionId
,
341 'contact_id' => $this->_softIndividual1Id
,
345 $softcontribution1 = $this->callAPISuccess('contribution_soft', 'create', $p1);
348 'contribution_id' => $this->_contributionId
,
349 'contact_id' => $this->_softIndividual2Id
,
353 $softcontribution2 = $this->callAPISuccess('contribution_soft', 'create', $p2);
356 'id' => $softcontribution2['id'],
358 $result = $this->callAPISuccess('contribution_soft', 'get', $params);
359 $res = $result['values'][$softcontribution2['id']];
361 $this->assertEquals($p2['contribution_id'], $res['contribution_id'], 'In line ' . __LINE__
);
362 $this->assertEquals($p2['contact_id'], $res['contact_id'], 'In line ' . __LINE__
);
363 $this->assertEquals($p2['amount'], $res['amount'], 'In line ' . __LINE__
);
364 $this->assertEquals($p2['currency'], $res['currency'], 'In line ' . __LINE__
);