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