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