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