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