Merge branch '4.6' of https://github.com/civicrm/civicrm-core
[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 require_once 'CiviTest/CiviUnitTestCase.php';
29
30 /**
31 * Test APIv3 civicrm_contribute_* functions
32 *
33 * @package CiviCRM_APIv3
34 * @subpackage API_ContributionSoft
35 */
36 class api_v3_ContributionSoftTest extends CiviUnitTestCase {
37
38 /**
39 * The hard credit contact.
40 *
41 * @var int
42 */
43 protected $_individualId;
44
45 /**
46 * The first soft credit contact.
47 *
48 * @var int
49 */
50 protected $_softIndividual1Id;
51
52 /**
53 * The second soft credit contact.
54 *
55 * @var int
56 */
57 protected $_softIndividual2Id;
58 protected $_contributionId;
59 protected $_financialTypeId = 1;
60 protected $_apiversion = 3;
61 protected $_entity = 'Contribution';
62 public $debug = 0;
63 protected $_params;
64
65
66 public function setUp() {
67 parent::setUp();
68 $this->useTransaction(TRUE);
69
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));
74
75 $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,
82 'fee_amount' => 5.00,
83 'net_amount' => 95.00,
84 'source' => 'SSF',
85 'contribution_status_id' => 1,
86 );
87 $this->_processorParams = array(
88 'domain_id' => 1,
89 'name' => 'Dummy',
90 'payment_processor_type_id' => 10,
91 'financial_account_id' => 12,
92 'is_active' => 1,
93 'user_name' => '',
94 'url_site' => 'http://dummy.com',
95 'url_recur' => 'http://dummy.com',
96 'billing_mode' => 1,
97 );
98 }
99
100 /**
101 * Test get methods.
102 *
103 * @todo - this might be better broken down into more smaller tests
104 */
105 public function testGetContributionSoft() {
106 //We don't test for PCP fields because there's no PCP API, so we can't create campaigns.
107 $p = array(
108 'contribution_id' => $this->_contributionId,
109 'contact_id' => $this->_softIndividual1Id,
110 'amount' => 10.00,
111 'currency' => 'USD',
112 'soft_credit_type_id' => 4,
113 );
114
115 $this->_softcontribution = $this->callAPISuccess('contribution_soft', 'create', $p);
116 $params = array(
117 'id' => $this->_softcontribution['id'],
118 );
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);
126
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);
130
131 // now we have 2 - test getcount
132 $softcontribution = $this->callAPISuccess('contribution_soft', 'getcount', array());
133 $this->assertEquals(2, $softcontribution);
134
135 //check first contribution
136 $result = $this->callAPISuccess('contribution_soft', 'get', array(
137 'id' => $this->_softcontribution['id'],
138 ));
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));
142
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,
147 ));
148 $this->assertEquals($this->_softcontribution2['id'], $resultID2);
149
150 //test get by contact id works
151 $result = $this->callAPISuccess('contribution_soft', 'get', array(
152 'contact_id' => $this->_softIndividual2Id,
153 )
154 );
155 $this->assertEquals(1, $result['count']);
156
157 $this->callAPISuccess('contribution_soft', 'Delete', array(
158 'id' => $this->_softcontribution['id'],
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(
168 'id' => $this->_softcontribution2['id'],
169 ));
170 }
171
172
173 /**
174 * civicrm_contribution_soft.
175 */
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'
179 );
180 }
181
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'
185 );
186 }
187
188 public function testCreateContributionSoftInvalidContact() {
189 $params = array(
190 'contact_id' => 999,
191 'contribution_id' => $this->_contributionId,
192 'amount' => 10.00,
193 'currency' => 'USD',
194 );
195
196 $softcontribution = $this->callAPIFailure('contribution_soft', 'create', $params,
197 'contact_id is not valid : 999'
198 );
199 }
200
201 public function testCreateContributionSoftInvalidContributionId() {
202 $params = array(
203 'contribution_id' => 999999,
204 'contact_id' => $this->_softIndividual1Id,
205 'amount' => 10.00,
206 'currency' => 'USD',
207 );
208
209 $softcontribution = $this->callAPIFailure('contribution_soft', 'create', $params,
210 'contribution_id is not valid : 999999'
211 );
212 }
213
214 /**
215 * Function tests that additional financial records are created when fee amount is recorded.
216 */
217 public function testCreateContributionSoft() {
218 $params = array(
219 'contribution_id' => $this->_contributionId,
220 'contact_id' => $this->_softIndividual1Id,
221 'amount' => 10.00,
222 'currency' => 'USD',
223 'soft_credit_type_id' => 5,
224 );
225
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);
232 }
233
234 /**
235 * To Update Soft Contribution.
236 *
237 */
238 public function testCreateUpdateContributionSoft() {
239 //create a soft credit
240 $params = array(
241 'contribution_id' => $this->_contributionId,
242 'contact_id' => $this->_softIndividual1Id,
243 'amount' => 10.00,
244 'currency' => 'USD',
245 'soft_credit_type_id' => 6,
246 );
247
248 $softcontribution = $this->callAPISuccess('contribution_soft', 'create', $params);
249 $softcontributionID = $softcontribution['id'];
250
251 $old_params = array(
252 'contribution_soft_id' => $softcontributionID,
253 );
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'];
263
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);
270 $params = array(
271 'id' => $softcontributionID,
272 'contribution_id' => $this->_contributionId,
273 'contact_id' => $this->_softIndividual1Id,
274 'amount' => 7.00,
275 'currency' => 'CAD',
276 'soft_credit_type_id' => 7,
277 );
278
279 $softcontribution = $this->callAPISuccess('contribution_soft', 'create', $params);
280
281 $new_params = array(
282 'id' => $softcontribution['id'],
283 );
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);
291
292 $params = array(
293 'id' => $softcontributionID,
294 );
295 $result = $this->callAPISuccess('contribution_soft', 'delete', $params);
296 }
297
298 /**
299 * civicrm_contribution_soft_delete methods.
300 *
301 */
302 public function testDeleteEmptyParamsContributionSoft() {
303 $params = array();
304 $softcontribution = $this->callAPIFailure('contribution_soft', 'delete', $params);
305 }
306
307 public function testDeleteWrongParamContributionSoft() {
308 $params = array(
309 'contribution_source' => 'SSF',
310 );
311 $softcontribution = $this->callAPIFailure('contribution_soft', 'delete', $params);
312 }
313
314 public function testDeleteContributionSoft() {
315 //create a soft credit
316 $params = array(
317 'contribution_id' => $this->_contributionId,
318 'contact_id' => $this->_softIndividual1Id,
319 'amount' => 10.00,
320 'currency' => 'USD',
321 );
322
323 $softcontribution = $this->callAPISuccess('contribution_soft', 'create', $params);
324 $softcontributionID = $softcontribution['id'];
325 $params = array(
326 'id' => $softcontributionID,
327 );
328 $result = $this->callAPIAndDocument('contribution_soft', 'delete', $params, __FUNCTION__, __FILE__);
329 }
330
331 ///////////////// civicrm_contribution_search methods
332
333 /**
334 * Test civicrm_contribution_search with empty params.
335 * All available contributions expected.
336 */
337 public function testSearchEmptyParams() {
338 $p = array(
339 'contribution_id' => $this->_contributionId,
340 'contact_id' => $this->_softIndividual1Id,
341 'amount' => 10.00,
342 'currency' => 'USD',
343 );
344 $softcontribution = $this->callAPISuccess('contribution_soft', 'create', $p);
345
346 $result = $this->callAPISuccess('contribution_soft', 'get', array());
347 // We're taking the first element.
348 $res = $result['values'][$softcontribution['id']];
349
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']);
354 }
355
356 /**
357 * Test civicrm_contribution_soft_search. Success expected.
358 */
359 public function testSearch() {
360 $p1 = array(
361 'contribution_id' => $this->_contributionId,
362 'contact_id' => $this->_softIndividual1Id,
363 'amount' => 10.00,
364 'currency' => 'USD',
365 );
366 $softcontribution1 = $this->callAPISuccess('contribution_soft', 'create', $p1);
367
368 $p2 = array(
369 'contribution_id' => $this->_contributionId,
370 'contact_id' => $this->_softIndividual2Id,
371 'amount' => 25.00,
372 'currency' => 'CAD',
373 );
374 $softcontribution2 = $this->callAPISuccess('contribution_soft', 'create', $p2);
375
376 $params = array(
377 'id' => $softcontribution2['id'],
378 );
379 $result = $this->callAPISuccess('contribution_soft', 'get', $params);
380 $res = $result['values'][$softcontribution2['id']];
381
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']);
386 }
387
388 }