Merge pull request #2163 from totten/master-wp-active
[civicrm-core.git] / tests / phpunit / api / v3 / ContributionSoftTest.php
CommitLineData
5c065cc0
JL
1<?php
2/*
3 +--------------------------------------------------------------------+
232624b1 4 | CiviCRM version 4.4 |
5c065cc0
JL
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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 *
33 * @package CiviCRM_APIv3
34 * @subpackage API_ContributionSoft
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;
51 public $_eNoticeCompliant = TRUE;
b5fb8b6f 52
5c065cc0
JL
53 function setUp() {
54 parent::setUp();
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,
2a7e7183 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
86 function tearDown() {
5c065cc0
JL
87 $this->quickCleanup(array(
88 'civicrm_contribution',
89 'civicrm_event',
90 'civicrm_contribution_page',
91 'civicrm_participant',
92 'civicrm_participant_payment',
93 'civicrm_line_item',
94 'civicrm_financial_trxn',
95 'civicrm_financial_item',
96 'civicrm_entity_financial_trxn',
97 'civicrm_contact',
98 'civicrm_contribution_soft'
99 ));
100 }
101
2a7e7183 102 /**
103 * test get methods
104 * @todo - this might be better broken down into more smaller tests
105 */
5c065cc0
JL
106 function testGetContributionSoft() {
107 //We don't test for PCP fields because there's no PCP API, so we can't create campaigns.
108 $p = array(
109 'contribution_id' => $this->_contributionId,
110 'contact_id' => $this->_softIndividual1Id,
111 'amount' => 10.00,
112 'currency' => 'USD',
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']);
5c065cc0
JL
121 $this->assertEquals($softcontribution['values'][$this->_softcontribution['id']]['contribution_id'], $this->_contributionId, 'In line ' . __LINE__);
122 $this->assertEquals($softcontribution['values'][$this->_softcontribution['id']]['contact_id'], $this->_softIndividual1Id, 'In line ' . __LINE__);
123 $this->assertEquals($softcontribution['values'][$this->_softcontribution['id']]['amount'], '10.00', 'In line ' . __LINE__);
124 $this->assertEquals($softcontribution['values'][$this->_softcontribution['id']]['currency'], 'USD', 'In line ' . __LINE__);
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;
2a7e7183 128 $this->_softcontribution2 = $this->callAPISuccess('contribution_soft', 'create', $p);
5c065cc0 129
5c065cc0 130 // now we have 2 - test getcount
2a7e7183 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'],
5c065cc0 137 ));
2a7e7183 138 $this->assertEquals(1, $result['count'], 'In line ' . __LINE__);
139 $this->assertEquals($this->_softcontribution['id'], $result['id']);
140 $this->assertEquals($this->_softcontribution['id'], $result['id'], print_r($softcontribution,true));
b5fb8b6f 141
5c065cc0 142 //test id only format - second soft credit
2a7e7183 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
5c065cc0 149 //test get by contact id works
2a7e7183 150 $result = $this->callAPISuccess('contribution_soft', 'get', array(
151 'contact_id' => $this->_softIndividual2Id)
152 );
153 $this->assertEquals(1, $result['count'], 'In line ' . __LINE__);
5c065cc0 154
2a7e7183 155 $this->callAPISuccess('contribution_soft', 'Delete', array(
5c065cc0 156 'id' => $this->_softcontribution['id'],
2a7e7183 157 ));
158 // check one soft credit remains
159 $expectedCount = 1;
160 $this->callAPISuccess('contribution_soft', 'getcount', array(), $expectedCount);
161
162 //check id is same as 2
163 $this->assertEquals($this->_softcontribution2['id'], $this->callAPISuccess('contribution_soft', 'getvalue', array('return' => 'id' )));
164
165 $this->callAPISuccess('ContributionSoft', 'Delete', array(
5c065cc0 166 'id' => $this->_softcontribution2['id'],
2a7e7183 167 ));
5c065cc0
JL
168 }
169
170
171 ///////////////// civicrm_contribution_soft
172 function testCreateEmptyParamsContributionSoft() {
2a7e7183 173 $softcontribution = $this->callAPIFailure('contribution_soft', 'create', array(),
174 'Mandatory key(s) missing from params array: contribution_id, amount, contact_id'
175 );
5c065cc0
JL
176 }
177
178 function testCreateParamsWithoutRequiredKeysContributionSoft() {
2a7e7183 179 $softcontribution = $this->callAPIFailure('contribution_soft', 'create', array(),
180 'Mandatory key(s) missing from params array: contribution_id, amount, contact_id'
181 );
5c065cc0 182 }
b5fb8b6f 183
5c065cc0
JL
184 function testCreateContributionSoftInvalidContact() {
185
186 $params = array(
187 'contact_id' => 999,
188 'contribution_id' => $this->_contributionId,
189 'amount' => 10.00,
190 'currency' => 'USD',
5c065cc0
JL
191 );
192
2a7e7183 193 $softcontribution = $this->callAPIFailure('contribution_soft', 'create', $params,
194 'contact_id is not valid : 999'
195 );
5c065cc0 196 }
b5fb8b6f 197
5c065cc0
JL
198 function testCreateContributionSoftInvalidContributionId() {
199
200 $params = array(
201 'contribution_id' => 999999,
202 'contact_id' => $this->_softIndividual1Id,
203 'amount' => 10.00,
204 'currency' => 'USD',
5c065cc0
JL
205 );
206
2a7e7183 207 $softcontribution = $this->callAPIFailure('contribution_soft', 'create', $params,
208 'contribution_id is not valid : 999999'
209 );
5c065cc0
JL
210 }
211
212 /*
213 * Function tests that additional financial records are created when fee amount is recorded
214 */
215 function testCreateContributionSoft() {
216 $params = array(
217 'contribution_id' => $this->_contributionId,
218 'contact_id' => $this->_softIndividual1Id,
219 'amount' => 10.00,
220 'currency' => 'USD',
5c065cc0
JL
221 );
222
2a7e7183 223 $softcontribution = $this->callAPIAndDocument('contribution_soft', 'create', $params, __FUNCTION__, __FILE__);
5c065cc0
JL
224 $this->assertEquals($softcontribution['values'][$softcontribution['id']]['contribution_id'], $this->_contributionId, 'In line ' . __LINE__);
225 $this->assertEquals($softcontribution['values'][$softcontribution['id']]['contact_id'], $this->_softIndividual1Id, 'In line ' . __LINE__);
226 $this->assertEquals($softcontribution['values'][$softcontribution['id']]['amount'], '10.00', 'In line ' . __LINE__);
227 $this->assertEquals($softcontribution['values'][$softcontribution['id']]['currency'], 'USD', 'In line ' . __LINE__);
228 }
b5fb8b6f 229
5c065cc0
JL
230 //To Update Soft Contribution
231 function testCreateUpdateContributionSoft() {
232 //create a soft credit
233 $params = array(
234 'contribution_id' => $this->_contributionId,
235 'contact_id' => $this->_softIndividual1Id,
236 'amount' => 10.00,
237 'currency' => 'USD',
5c065cc0
JL
238 );
239
2a7e7183 240 $softcontribution = $this->callAPISuccess('contribution_soft', 'create', $params);
5c065cc0 241 $softcontributionID = $softcontribution['id'];
b5fb8b6f 242
5c065cc0
JL
243 $old_params = array(
244 'contribution_soft_id' => $softcontributionID,
5c065cc0 245 );
2a7e7183 246 $original = $this->callAPISuccess('contribution_soft', 'get', $old_params);
5c065cc0 247 //Make sure it came back
5c065cc0
JL
248 $this->assertEquals($original['id'], $softcontributionID, 'In line ' . __LINE__);
249 //set up list of old params, verify
250 $old_contribution_id = $original['values'][$softcontributionID]['contribution_id'];
251 $old_contact_id = $original['values'][$softcontributionID]['contact_id'];
252 $old_amount = $original['values'][$softcontributionID]['amount'];
253 $old_currency = $original['values'][$softcontributionID]['currency'];
254
255 //check against original values
256 $this->assertEquals($old_contribution_id, $this->_contributionId, 'In line ' . __LINE__);
257 $this->assertEquals($old_contact_id, $this->_softIndividual1Id, 'In line ' . __LINE__);
258 $this->assertEquals($old_amount, 10.00, 'In line ' . __LINE__);
259 $this->assertEquals($old_currency, 'USD', 'In line ' . __LINE__);
260 $params = array(
261 'id' => $softcontributionID,
262 'contribution_id' => $this->_contributionId,
263 'contact_id' => $this->_softIndividual1Id,
264 'amount' => 7.00,
265 'currency' => 'CAD',
5c065cc0
JL
266 );
267
2a7e7183 268 $softcontribution = $this->callAPISuccess('contribution_soft', 'create', $params);
5c065cc0
JL
269
270 $new_params = array(
271 'id' => $softcontribution['id'],
5c065cc0 272 );
2a7e7183 273 $softcontribution = $this->callAPISuccess('contribution_soft', 'get', $new_params);
5c065cc0
JL
274 //check against original values
275 $this->assertEquals($softcontribution['values'][$softcontributionID]['contribution_id'], $this->_contributionId, 'In line ' . __LINE__);
276 $this->assertEquals($softcontribution['values'][$softcontributionID]['contact_id'], $this->_softIndividual1Id, 'In line ' . __LINE__);
277 $this->assertEquals($softcontribution['values'][$softcontributionID]['amount'], 7.00, 'In line ' . __LINE__);
278 $this->assertEquals($softcontribution['values'][$softcontributionID]['currency'], 'CAD', 'In line ' . __LINE__);
b5fb8b6f 279
5c065cc0
JL
280 $params = array(
281 'id' => $softcontributionID,
5c065cc0 282 );
2a7e7183 283 $result = $this->callAPISuccess('contribution_soft', 'delete', $params);
5c065cc0
JL
284 }
285
286 ///////////////// civicrm_contribution_soft_delete methods
287 function testDeleteEmptyParamsContributionSoft() {
2a7e7183 288 $params = array();
d0e1eff2 289 $softcontribution = $this->callAPIFailure('contribution_soft', 'delete', $params);
5c065cc0
JL
290 }
291
292 function testDeleteWrongParamContributionSoft() {
293 $params = array(
294 'contribution_source' => 'SSF',
5c065cc0 295 );
d0e1eff2 296 $softcontribution = $this->callAPIFailure('contribution_soft', 'delete', $params);
5c065cc0
JL
297 }
298
299 function testDeleteContributionSoft() {
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
JL
318 /**
319 * Test civicrm_contribution_search with empty params.
320 * All available contributions expected.
321 */
322 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 /**
342 * Test civicrm_contribution_soft_search. Success expected.
343 */
344 function testSearch() {
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__);
370 $this->assertEquals($p2['currency'], $res['currency'], 'In line ' . __LINE__ );
371 }
372}
373