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