Commit | Line | Data |
---|---|---|
5c065cc0 JL |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
06a1bc01 | 4 | | CiviCRM version 4.5 | |
5c065cc0 | 5 | +--------------------------------------------------------------------+ |
06a1bc01 | 6 | | Copyright CiviCRM LLC (c) 2004-2014 | |
5c065cc0 JL |
7 | +--------------------------------------------------------------------+ |
8 | | This file is a part of CiviCRM. | | |
9 | | | | |
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. | | |
13 | | | | |
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. | | |
18 | | | | |
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 | +--------------------------------------------------------------------+ | |
26 | */ | |
27 | ||
28 | require_once 'CiviTest/CiviUnitTestCase.php'; | |
5c065cc0 JL |
29 | |
30 | /** | |
31 | * Test APIv3 civicrm_contribute_* functions | |
32 | * | |
33 | * @package CiviCRM_APIv3 | |
34 | * @subpackage API_ContributionSoft | |
35 | */ | |
36 | ||
37 | class api_v3_ContributionSoftTest extends CiviUnitTestCase { | |
38 | ||
39 | /** | |
40 | * Assume empty database with just civicrm_data | |
41 | */ | |
42 | protected $_individualId; //the hard credit contact | |
43 | protected $_softIndividual1Id; //the first soft credit contact | |
44 | protected $_softIndividual2Id; //the second soft credit contact | |
45 | protected $_contributionId; | |
2a7e7183 | 46 | protected $_financialTypeId = 1; |
47 | protected $_apiversion = 3; | |
5c065cc0 JL |
48 | protected $_entity = 'Contribution'; |
49 | public $debug = 0; | |
50 | protected $_params; | |
b7c9bc4c | 51 | |
b5fb8b6f | 52 | |
5c065cc0 JL |
53 | function setUp() { |
54 | parent::setUp(); | |
55 | ||
5c065cc0 JL |
56 | $this->_individualId = $this->individualCreate(); |
57 | $this->_softIndividual1Id = $this->individualCreate(); | |
58 | $this->_softIndividual2Id = $this->individualCreate(); | |
59 | $this->_contributionId = $this->contributionCreate($this->_individualId); | |
b5fb8b6f | 60 | |
5c065cc0 JL |
61 | $paymentProcessor = $this->processorCreate(); |
62 | $this->_params = array( | |
63 | 'contact_id' => $this->_individualId, | |
64 | 'receive_date' => '20120511', | |
65 | 'total_amount' => 100.00, | |
2a7e7183 | 66 | 'financial_type_id' => $this->_financialTypeId, |
5c065cc0 JL |
67 | 'non_deductible_amount' => 10.00, |
68 | 'fee_amount' => 5.00, | |
69 | 'net_amount' => 95.00, | |
70 | 'source' => 'SSF', | |
71 | 'contribution_status_id' => 1, | |
5c065cc0 JL |
72 | ); |
73 | $this->_processorParams = array( | |
74 | 'domain_id' => 1, | |
75 | 'name' => 'Dummy', | |
76 | 'payment_processor_type_id' => 10, | |
77 | 'financial_account_id' => 12, | |
78 | 'is_active' => 1, | |
79 | 'user_name' => '', | |
80 | 'url_site' => 'http://dummy.com', | |
81 | 'url_recur' => 'http://dummy.com', | |
82 | 'billing_mode' => 1, | |
83 | ); | |
5c065cc0 JL |
84 | } |
85 | ||
86 | function tearDown() { | |
5c065cc0 JL |
87 | $this->quickCleanup(array( |
88 | 'civicrm_contribution', | |
89 | 'civicrm_event', | |
90 | 'civicrm_contribution_page', | |
91 | 'civicrm_participant', | |
92 | 'civicrm_participant_payment', | |
93 | 'civicrm_line_item', | |
94 | 'civicrm_financial_trxn', | |
95 | 'civicrm_financial_item', | |
96 | 'civicrm_entity_financial_trxn', | |
97 | 'civicrm_contact', | |
98 | 'civicrm_contribution_soft' | |
99 | )); | |
100 | } | |
101 | ||
2a7e7183 | 102 | /** |
103 | * test get methods | |
104 | * @todo - this might be better broken down into more smaller tests | |
105 | */ | |
5c065cc0 JL |
106 | function testGetContributionSoft() { |
107 | //We don't test for PCP fields because there's no PCP API, so we can't create campaigns. | |
108 | $p = array( | |
109 | 'contribution_id' => $this->_contributionId, | |
110 | 'contact_id' => $this->_softIndividual1Id, | |
111 | 'amount' => 10.00, | |
112 | 'currency' => 'USD', | |
51fa20cb | 113 | 'soft_credit_type_id' => 4, |
5c065cc0 | 114 | ); |
b5fb8b6f | 115 | |
2a7e7183 | 116 | $this->_softcontribution = $this->callAPISuccess('contribution_soft', 'create', $p); |
5c065cc0 JL |
117 | $params = array( |
118 | 'id' => $this->_softcontribution['id'], | |
5c065cc0 | 119 | ); |
2a7e7183 | 120 | $softcontribution = $this->callAPIAndDocument('contribution_soft', 'get', $params, __FUNCTION__, __FILE__); |
121 | $this->assertEquals(1, $softcontribution['count']); | |
5c065cc0 JL |
122 | $this->assertEquals($softcontribution['values'][$this->_softcontribution['id']]['contribution_id'], $this->_contributionId, 'In line ' . __LINE__); |
123 | $this->assertEquals($softcontribution['values'][$this->_softcontribution['id']]['contact_id'], $this->_softIndividual1Id, 'In line ' . __LINE__); | |
124 | $this->assertEquals($softcontribution['values'][$this->_softcontribution['id']]['amount'], '10.00', 'In line ' . __LINE__); | |
125 | $this->assertEquals($softcontribution['values'][$this->_softcontribution['id']]['currency'], 'USD', 'In line ' . __LINE__); | |
51fa20cb | 126 | $this->assertEquals($softcontribution['values'][$this->_softcontribution['id']]['soft_credit_type_id'], 4, 'In line ' . __LINE__); |
5c065cc0 JL |
127 | |
128 | //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) | |
129 | $p['contact_id'] = $this->_softIndividual2Id; | |
2a7e7183 | 130 | $this->_softcontribution2 = $this->callAPISuccess('contribution_soft', 'create', $p); |
5c065cc0 | 131 | |
5c065cc0 | 132 | // now we have 2 - test getcount |
2a7e7183 | 133 | $softcontribution = $this->callAPISuccess('contribution_soft', 'getcount', array()); |
134 | $this->assertEquals(2, $softcontribution); | |
135 | ||
136 | //check first contribution | |
137 | $result = $this->callAPISuccess('contribution_soft', 'get', array( | |
138 | 'id' => $this->_softcontribution['id'], | |
5c065cc0 | 139 | )); |
2a7e7183 | 140 | $this->assertEquals(1, $result['count'], 'In line ' . __LINE__); |
141 | $this->assertEquals($this->_softcontribution['id'], $result['id']); | |
142 | $this->assertEquals($this->_softcontribution['id'], $result['id'], print_r($softcontribution,true)); | |
b5fb8b6f | 143 | |
5c065cc0 | 144 | //test id only format - second soft credit |
2a7e7183 | 145 | $resultID2 = $this->callAPISuccess('contribution_soft', 'get', array( |
146 | 'id' => $this->_softcontribution2['id'], | |
147 | 'format.only_id' => 1, | |
148 | )); | |
149 | $this->assertEquals($this->_softcontribution2['id'], $resultID2); | |
150 | ||
5c065cc0 | 151 | //test get by contact id works |
2a7e7183 | 152 | $result = $this->callAPISuccess('contribution_soft', 'get', array( |
153 | 'contact_id' => $this->_softIndividual2Id) | |
154 | ); | |
155 | $this->assertEquals(1, $result['count'], 'In line ' . __LINE__); | |
5c065cc0 | 156 | |
2a7e7183 | 157 | $this->callAPISuccess('contribution_soft', 'Delete', array( |
5c065cc0 | 158 | 'id' => $this->_softcontribution['id'], |
2a7e7183 | 159 | )); |
160 | // check one soft credit remains | |
161 | $expectedCount = 1; | |
162 | $this->callAPISuccess('contribution_soft', 'getcount', array(), $expectedCount); | |
163 | ||
164 | //check id is same as 2 | |
165 | $this->assertEquals($this->_softcontribution2['id'], $this->callAPISuccess('contribution_soft', 'getvalue', array('return' => 'id' ))); | |
166 | ||
167 | $this->callAPISuccess('ContributionSoft', 'Delete', array( | |
5c065cc0 | 168 | 'id' => $this->_softcontribution2['id'], |
2a7e7183 | 169 | )); |
5c065cc0 JL |
170 | } |
171 | ||
172 | ||
173 | ///////////////// civicrm_contribution_soft | |
174 | function testCreateEmptyParamsContributionSoft() { | |
2a7e7183 | 175 | $softcontribution = $this->callAPIFailure('contribution_soft', 'create', array(), |
176 | 'Mandatory key(s) missing from params array: contribution_id, amount, contact_id' | |
177 | ); | |
5c065cc0 JL |
178 | } |
179 | ||
180 | function testCreateParamsWithoutRequiredKeysContributionSoft() { | |
2a7e7183 | 181 | $softcontribution = $this->callAPIFailure('contribution_soft', 'create', array(), |
182 | 'Mandatory key(s) missing from params array: contribution_id, amount, contact_id' | |
183 | ); | |
5c065cc0 | 184 | } |
b5fb8b6f | 185 | |
5c065cc0 JL |
186 | function testCreateContributionSoftInvalidContact() { |
187 | ||
188 | $params = array( | |
189 | 'contact_id' => 999, | |
190 | 'contribution_id' => $this->_contributionId, | |
191 | 'amount' => 10.00, | |
192 | 'currency' => 'USD', | |
5c065cc0 JL |
193 | ); |
194 | ||
2a7e7183 | 195 | $softcontribution = $this->callAPIFailure('contribution_soft', 'create', $params, |
196 | 'contact_id is not valid : 999' | |
197 | ); | |
5c065cc0 | 198 | } |
b5fb8b6f | 199 | |
5c065cc0 JL |
200 | function testCreateContributionSoftInvalidContributionId() { |
201 | ||
202 | $params = array( | |
203 | 'contribution_id' => 999999, | |
204 | 'contact_id' => $this->_softIndividual1Id, | |
205 | 'amount' => 10.00, | |
206 | 'currency' => 'USD', | |
5c065cc0 JL |
207 | ); |
208 | ||
2a7e7183 | 209 | $softcontribution = $this->callAPIFailure('contribution_soft', 'create', $params, |
210 | 'contribution_id is not valid : 999999' | |
211 | ); | |
5c065cc0 JL |
212 | } |
213 | ||
214 | /* | |
215 | * Function tests that additional financial records are created when fee amount is recorded | |
216 | */ | |
217 | function testCreateContributionSoft() { | |
218 | $params = array( | |
219 | 'contribution_id' => $this->_contributionId, | |
220 | 'contact_id' => $this->_softIndividual1Id, | |
221 | 'amount' => 10.00, | |
222 | 'currency' => 'USD', | |
51fa20cb | 223 | 'soft_credit_type_id' => 5, |
5c065cc0 JL |
224 | ); |
225 | ||
2a7e7183 | 226 | $softcontribution = $this->callAPIAndDocument('contribution_soft', 'create', $params, __FUNCTION__, __FILE__); |
5c065cc0 JL |
227 | $this->assertEquals($softcontribution['values'][$softcontribution['id']]['contribution_id'], $this->_contributionId, 'In line ' . __LINE__); |
228 | $this->assertEquals($softcontribution['values'][$softcontribution['id']]['contact_id'], $this->_softIndividual1Id, 'In line ' . __LINE__); | |
229 | $this->assertEquals($softcontribution['values'][$softcontribution['id']]['amount'], '10.00', 'In line ' . __LINE__); | |
230 | $this->assertEquals($softcontribution['values'][$softcontribution['id']]['currency'], 'USD', 'In line ' . __LINE__); | |
51fa20cb | 231 | $this->assertEquals($softcontribution['values'][$softcontribution['id']]['soft_credit_type_id'], 5, 'In line ' . __LINE__); |
5c065cc0 | 232 | } |
b5fb8b6f | 233 | |
5c065cc0 JL |
234 | //To Update Soft Contribution |
235 | function testCreateUpdateContributionSoft() { | |
236 | //create a soft credit | |
237 | $params = array( | |
238 | 'contribution_id' => $this->_contributionId, | |
239 | 'contact_id' => $this->_softIndividual1Id, | |
240 | 'amount' => 10.00, | |
241 | 'currency' => 'USD', | |
51fa20cb | 242 | 'soft_credit_type_id' => 6, |
5c065cc0 JL |
243 | ); |
244 | ||
2a7e7183 | 245 | $softcontribution = $this->callAPISuccess('contribution_soft', 'create', $params); |
5c065cc0 | 246 | $softcontributionID = $softcontribution['id']; |
b5fb8b6f | 247 | |
5c065cc0 JL |
248 | $old_params = array( |
249 | 'contribution_soft_id' => $softcontributionID, | |
5c065cc0 | 250 | ); |
2a7e7183 | 251 | $original = $this->callAPISuccess('contribution_soft', 'get', $old_params); |
5c065cc0 | 252 | //Make sure it came back |
5c065cc0 JL |
253 | $this->assertEquals($original['id'], $softcontributionID, 'In line ' . __LINE__); |
254 | //set up list of old params, verify | |
255 | $old_contribution_id = $original['values'][$softcontributionID]['contribution_id']; | |
256 | $old_contact_id = $original['values'][$softcontributionID]['contact_id']; | |
257 | $old_amount = $original['values'][$softcontributionID]['amount']; | |
258 | $old_currency = $original['values'][$softcontributionID]['currency']; | |
51fa20cb | 259 | $old_soft_credit_type_id = $original['values'][$softcontributionID]['soft_credit_type_id']; |
5c065cc0 JL |
260 | |
261 | //check against original values | |
262 | $this->assertEquals($old_contribution_id, $this->_contributionId, 'In line ' . __LINE__); | |
263 | $this->assertEquals($old_contact_id, $this->_softIndividual1Id, 'In line ' . __LINE__); | |
264 | $this->assertEquals($old_amount, 10.00, 'In line ' . __LINE__); | |
265 | $this->assertEquals($old_currency, 'USD', 'In line ' . __LINE__); | |
51fa20cb | 266 | $this->assertEquals($old_soft_credit_type_id, 6 , 'In line ' . __LINE__); |
5c065cc0 JL |
267 | $params = array( |
268 | 'id' => $softcontributionID, | |
269 | 'contribution_id' => $this->_contributionId, | |
270 | 'contact_id' => $this->_softIndividual1Id, | |
271 | 'amount' => 7.00, | |
272 | 'currency' => 'CAD', | |
51fa20cb | 273 | 'soft_credit_type_id' => 7, |
5c065cc0 JL |
274 | ); |
275 | ||
2a7e7183 | 276 | $softcontribution = $this->callAPISuccess('contribution_soft', 'create', $params); |
5c065cc0 JL |
277 | |
278 | $new_params = array( | |
279 | 'id' => $softcontribution['id'], | |
5c065cc0 | 280 | ); |
2a7e7183 | 281 | $softcontribution = $this->callAPISuccess('contribution_soft', 'get', $new_params); |
5c065cc0 JL |
282 | //check against original values |
283 | $this->assertEquals($softcontribution['values'][$softcontributionID]['contribution_id'], $this->_contributionId, 'In line ' . __LINE__); | |
284 | $this->assertEquals($softcontribution['values'][$softcontributionID]['contact_id'], $this->_softIndividual1Id, 'In line ' . __LINE__); | |
285 | $this->assertEquals($softcontribution['values'][$softcontributionID]['amount'], 7.00, 'In line ' . __LINE__); | |
286 | $this->assertEquals($softcontribution['values'][$softcontributionID]['currency'], 'CAD', 'In line ' . __LINE__); | |
51fa20cb | 287 | $this->assertEquals($softcontribution['values'][$softcontributionID]['soft_credit_type_id'], 7, 'In line ' . __LINE__); |
b5fb8b6f | 288 | |
5c065cc0 JL |
289 | $params = array( |
290 | 'id' => $softcontributionID, | |
5c065cc0 | 291 | ); |
2a7e7183 | 292 | $result = $this->callAPISuccess('contribution_soft', 'delete', $params); |
5c065cc0 JL |
293 | } |
294 | ||
295 | ///////////////// civicrm_contribution_soft_delete methods | |
296 | function testDeleteEmptyParamsContributionSoft() { | |
2a7e7183 | 297 | $params = array(); |
d0e1eff2 | 298 | $softcontribution = $this->callAPIFailure('contribution_soft', 'delete', $params); |
5c065cc0 JL |
299 | } |
300 | ||
301 | function testDeleteWrongParamContributionSoft() { | |
302 | $params = array( | |
303 | 'contribution_source' => 'SSF', | |
5c065cc0 | 304 | ); |
d0e1eff2 | 305 | $softcontribution = $this->callAPIFailure('contribution_soft', 'delete', $params); |
5c065cc0 JL |
306 | } |
307 | ||
308 | function testDeleteContributionSoft() { | |
309 | //create a soft credit | |
310 | $params = array( | |
311 | 'contribution_id' => $this->_contributionId, | |
312 | 'contact_id' => $this->_softIndividual1Id, | |
313 | 'amount' => 10.00, | |
314 | 'currency' => 'USD', | |
5c065cc0 JL |
315 | ); |
316 | ||
2a7e7183 | 317 | $softcontribution = $this->callAPISuccess('contribution_soft', 'create', $params); |
5c065cc0 JL |
318 | $softcontributionID = $softcontribution['id']; |
319 | $params = array( | |
320 | 'id' => $softcontributionID, | |
5c065cc0 | 321 | ); |
2a7e7183 | 322 | $result = $this->callAPIAndDocument('contribution_soft', 'delete', $params, __FUNCTION__, __FILE__); |
5c065cc0 JL |
323 | } |
324 | ||
325 | ///////////////// civicrm_contribution_search methods | |
326 | ||
5c065cc0 JL |
327 | /** |
328 | * Test civicrm_contribution_search with empty params. | |
329 | * All available contributions expected. | |
330 | */ | |
331 | function testSearchEmptyParams() { | |
5c065cc0 JL |
332 | $p = array( |
333 | 'contribution_id' => $this->_contributionId, | |
334 | 'contact_id' => $this->_softIndividual1Id, | |
335 | 'amount' => 10.00, | |
336 | 'currency' => 'USD', | |
5c065cc0 | 337 | ); |
2a7e7183 | 338 | $softcontribution = $this->callAPISuccess('contribution_soft', 'create', $p); |
5c065cc0 | 339 | |
2a7e7183 | 340 | $result = $this->callAPISuccess('contribution_soft', 'get', array()); |
5c065cc0 JL |
341 | // We're taking the first element. |
342 | $res = $result['values'][$softcontribution['id']]; | |
343 | ||
344 | $this->assertEquals($p['contribution_id'], $res['contribution_id'], 'In line ' . __LINE__); | |
345 | $this->assertEquals($p['contact_id'], $res['contact_id'], 'In line ' . __LINE__); | |
346 | $this->assertEquals($p['amount'], $res['amount'], 'In line ' . __LINE__); | |
347 | $this->assertEquals($p['currency'], $res['currency'], 'In line ' . __LINE__); | |
348 | } | |
349 | ||
350 | /** | |
351 | * Test civicrm_contribution_soft_search. Success expected. | |
352 | */ | |
353 | function testSearch() { | |
354 | $p1 = array( | |
355 | 'contribution_id' => $this->_contributionId, | |
356 | 'contact_id' => $this->_softIndividual1Id, | |
357 | 'amount' => 10.00, | |
358 | 'currency' => 'USD', | |
5c065cc0 | 359 | ); |
2a7e7183 | 360 | $softcontribution1 = $this->callAPISuccess('contribution_soft', 'create', $p1); |
5c065cc0 JL |
361 | |
362 | $p2 = array( | |
363 | 'contribution_id' => $this->_contributionId, | |
364 | 'contact_id' => $this->_softIndividual2Id, | |
365 | 'amount' => 25.00, | |
366 | 'currency' => 'CAD', | |
5c065cc0 | 367 | ); |
2a7e7183 | 368 | $softcontribution2 = $this->callAPISuccess('contribution_soft', 'create', $p2); |
5c065cc0 JL |
369 | |
370 | $params = array( | |
371 | 'id' => $softcontribution2['id'], | |
5c065cc0 | 372 | ); |
2a7e7183 | 373 | $result = $this->callAPISuccess('contribution_soft', 'get', $params); |
5c065cc0 JL |
374 | $res = $result['values'][$softcontribution2['id']]; |
375 | ||
376 | $this->assertEquals($p2['contribution_id'], $res['contribution_id'], 'In line ' . __LINE__); | |
377 | $this->assertEquals($p2['contact_id'], $res['contact_id'], 'In line ' . __LINE__); | |
378 | $this->assertEquals($p2['amount'], $res['amount'], 'In line ' . __LINE__); | |
379 | $this->assertEquals($p2['currency'], $res['currency'], 'In line ' . __LINE__ ); | |
380 | } | |
381 | } | |
382 |