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