Update Unit test styling to cover the future coder version
[civicrm-core.git] / tests / phpunit / api / v3 / ContributionSoftTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
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 * @group headless
34 */
35 class api_v3_ContributionSoftTest extends CiviUnitTestCase {
36
37 /**
38 * The hard credit contact.
39 *
40 * @var int
41 */
42 protected $_individualId;
43
44 /**
45 * The first soft credit contact.
46 *
47 * @var int
48 */
49 protected $_softIndividual1Id;
50
51 /**
52 * The second soft credit contact.
53 *
54 * @var int
55 */
56 protected $_softIndividual2Id;
57 protected $_contributionId;
58 protected $_financialTypeId = 1;
59 protected $_apiversion = 3;
60 protected $_entity = 'Contribution';
61 public $debug = 0;
62 protected $_params;
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 $this->assertEquals(1, $result['count']);
153
154 $this->callAPISuccess('contribution_soft', 'Delete', array(
155 'id' => $this->_softcontribution['id'],
156 ));
157 // check one soft credit remains
158 $expectedCount = 1;
159 $this->callAPISuccess('contribution_soft', 'getcount', array(), $expectedCount);
160
161 //check id is same as 2
162 $this->assertEquals($this->_softcontribution2['id'], $this->callAPISuccess('contribution_soft', 'getvalue', array('return' => 'id')));
163
164 $this->callAPISuccess('ContributionSoft', 'Delete', array(
165 'id' => $this->_softcontribution2['id'],
166 ));
167 }
168
169 /**
170 * civicrm_contribution_soft.
171 */
172 public function testCreateEmptyParamsContributionSoft() {
173 $softcontribution = $this->callAPIFailure('contribution_soft', 'create', array(),
174 'Mandatory key(s) missing from params array: contribution_id, amount, contact_id'
175 );
176 }
177
178 public function testCreateParamsWithoutRequiredKeysContributionSoft() {
179 $softcontribution = $this->callAPIFailure('contribution_soft', 'create', array(),
180 'Mandatory key(s) missing from params array: contribution_id, amount, contact_id'
181 );
182 }
183
184 public function testCreateContributionSoftInvalidContact() {
185 $params = array(
186 'contact_id' => 999,
187 'contribution_id' => $this->_contributionId,
188 'amount' => 10.00,
189 'currency' => 'USD',
190 );
191
192 $softcontribution = $this->callAPIFailure('contribution_soft', 'create', $params,
193 'contact_id is not valid : 999'
194 );
195 }
196
197 public function testCreateContributionSoftInvalidContributionId() {
198 $params = array(
199 'contribution_id' => 999999,
200 'contact_id' => $this->_softIndividual1Id,
201 'amount' => 10.00,
202 'currency' => 'USD',
203 );
204
205 $softcontribution = $this->callAPIFailure('contribution_soft', 'create', $params,
206 'contribution_id is not valid : 999999'
207 );
208 }
209
210 /**
211 * Function tests that additional financial records are created when fee amount is recorded.
212 */
213 public function testCreateContributionSoft() {
214 $params = array(
215 'contribution_id' => $this->_contributionId,
216 'contact_id' => $this->_softIndividual1Id,
217 'amount' => 10.00,
218 'currency' => 'USD',
219 'soft_credit_type_id' => 5,
220 );
221
222 $softcontribution = $this->callAPIAndDocument('contribution_soft', 'create', $params, __FUNCTION__, __FILE__);
223 $this->assertEquals($softcontribution['values'][$softcontribution['id']]['contribution_id'], $this->_contributionId);
224 $this->assertEquals($softcontribution['values'][$softcontribution['id']]['contact_id'], $this->_softIndividual1Id);
225 $this->assertEquals($softcontribution['values'][$softcontribution['id']]['amount'], '10.00');
226 $this->assertEquals($softcontribution['values'][$softcontribution['id']]['currency'], 'USD');
227 $this->assertEquals($softcontribution['values'][$softcontribution['id']]['soft_credit_type_id'], 5);
228 }
229
230 /**
231 * To Update Soft Contribution.
232 *
233 */
234 public function testCreateUpdateContributionSoft() {
235 //create a soft credit
236 $params = array(
237 'contribution_id' => $this->_contributionId,
238 'contact_id' => $this->_softIndividual1Id,
239 'amount' => 10.00,
240 'currency' => 'USD',
241 'soft_credit_type_id' => 6,
242 );
243
244 $softcontribution = $this->callAPISuccess('contribution_soft', 'create', $params);
245 $softcontributionID = $softcontribution['id'];
246
247 $old_params = array(
248 'contribution_soft_id' => $softcontributionID,
249 );
250 $original = $this->callAPISuccess('contribution_soft', 'get', $old_params);
251 //Make sure it came back
252 $this->assertEquals($original['id'], $softcontributionID);
253 //set up list of old params, verify
254 $old_contribution_id = $original['values'][$softcontributionID]['contribution_id'];
255 $old_contact_id = $original['values'][$softcontributionID]['contact_id'];
256 $old_amount = $original['values'][$softcontributionID]['amount'];
257 $old_currency = $original['values'][$softcontributionID]['currency'];
258 $old_soft_credit_type_id = $original['values'][$softcontributionID]['soft_credit_type_id'];
259
260 //check against original values
261 $this->assertEquals($old_contribution_id, $this->_contributionId);
262 $this->assertEquals($old_contact_id, $this->_softIndividual1Id);
263 $this->assertEquals($old_amount, 10.00);
264 $this->assertEquals($old_currency, 'USD');
265 $this->assertEquals($old_soft_credit_type_id, 6);
266 $params = array(
267 'id' => $softcontributionID,
268 'contribution_id' => $this->_contributionId,
269 'contact_id' => $this->_softIndividual1Id,
270 'amount' => 7.00,
271 'currency' => 'CAD',
272 'soft_credit_type_id' => 7,
273 );
274
275 $softcontribution = $this->callAPISuccess('contribution_soft', 'create', $params);
276
277 $new_params = array(
278 'id' => $softcontribution['id'],
279 );
280 $softcontribution = $this->callAPISuccess('contribution_soft', 'get', $new_params);
281 //check against original values
282 $this->assertEquals($softcontribution['values'][$softcontributionID]['contribution_id'], $this->_contributionId);
283 $this->assertEquals($softcontribution['values'][$softcontributionID]['contact_id'], $this->_softIndividual1Id);
284 $this->assertEquals($softcontribution['values'][$softcontributionID]['amount'], 7.00);
285 $this->assertEquals($softcontribution['values'][$softcontributionID]['currency'], 'CAD');
286 $this->assertEquals($softcontribution['values'][$softcontributionID]['soft_credit_type_id'], 7);
287
288 $params = array(
289 'id' => $softcontributionID,
290 );
291 $this->callAPISuccess('contribution_soft', 'delete', $params);
292 }
293
294 /**
295 * civicrm_contribution_soft_delete methods.
296 *
297 */
298 public function testDeleteEmptyParamsContributionSoft() {
299 $params = array();
300 $softcontribution = $this->callAPIFailure('contribution_soft', 'delete', $params);
301 }
302
303 public function testDeleteWrongParamContributionSoft() {
304 $params = array(
305 'contribution_source' => 'SSF',
306 );
307 $this->callAPIFailure('contribution_soft', 'delete', $params);
308 }
309
310 public function testDeleteContributionSoft() {
311 //create a soft credit
312 $params = array(
313 'contribution_id' => $this->_contributionId,
314 'contact_id' => $this->_softIndividual1Id,
315 'amount' => 10.00,
316 'currency' => 'USD',
317 );
318
319 $softcontribution = $this->callAPISuccess('contribution_soft', 'create', $params);
320 $softcontributionID = $softcontribution['id'];
321 $params = array(
322 'id' => $softcontributionID,
323 );
324 $this->callAPIAndDocument('contribution_soft', 'delete', $params, __FUNCTION__, __FILE__);
325 }
326
327 ///////////////// civicrm_contribution_search methods
328
329 /**
330 * Test civicrm_contribution_search with empty params.
331 * All available contributions expected.
332 */
333 public function testSearchEmptyParams() {
334 $p = array(
335 'contribution_id' => $this->_contributionId,
336 'contact_id' => $this->_softIndividual1Id,
337 'amount' => 10.00,
338 'currency' => 'USD',
339 );
340 $softcontribution = $this->callAPISuccess('contribution_soft', 'create', $p);
341
342 $result = $this->callAPISuccess('contribution_soft', 'get', array());
343 // We're taking the first element.
344 $res = $result['values'][$softcontribution['id']];
345
346 $this->assertEquals($p['contribution_id'], $res['contribution_id']);
347 $this->assertEquals($p['contact_id'], $res['contact_id']);
348 $this->assertEquals($p['amount'], $res['amount']);
349 $this->assertEquals($p['currency'], $res['currency']);
350 }
351
352 /**
353 * Test civicrm_contribution_soft_search. Success expected.
354 */
355 public function testSearch() {
356 $p1 = array(
357 'contribution_id' => $this->_contributionId,
358 'contact_id' => $this->_softIndividual1Id,
359 'amount' => 10.00,
360 'currency' => 'USD',
361 );
362 $softcontribution1 = $this->callAPISuccess('contribution_soft', 'create', $p1);
363
364 $p2 = array(
365 'contribution_id' => $this->_contributionId,
366 'contact_id' => $this->_softIndividual2Id,
367 'amount' => 25.00,
368 'currency' => 'CAD',
369 );
370 $softcontribution2 = $this->callAPISuccess('contribution_soft', 'create', $p2);
371
372 $params = array(
373 'id' => $softcontribution2['id'],
374 );
375 $result = $this->callAPISuccess('contribution_soft', 'get', $params);
376 $res = $result['values'][$softcontribution2['id']];
377
378 $this->assertEquals($p2['contribution_id'], $res['contribution_id']);
379 $this->assertEquals($p2['contact_id'], $res['contact_id']);
380 $this->assertEquals($p2['amount'], $res['amount']);
381 $this->assertEquals($p2['currency'], $res['currency']);
382 }
383
384 }