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