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