Merge pull request #647 from PalanteJon/soft-credit-api-3
[civicrm-core.git] / tests / phpunit / api / v3 / ContributionSoftTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.3 |
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
28 require_once 'CiviTest/CiviUnitTestCase.php';
29 //FIXME:This existed in ContributionTest, don't think it's necessary
30 //require_once 'CiviTest/CiviMailUtils.php';
31
32
33 /**
34 * Test APIv3 civicrm_contribute_* functions
35 *
36 * @package CiviCRM_APIv3
37 * @subpackage API_ContributionSoft
38 */
39
40 class api_v3_ContributionSoftTest extends CiviUnitTestCase {
41
42 /**
43 * Assume empty database with just civicrm_data
44 */
45 protected $_individualId; //the hard credit contact
46 protected $_softIndividual1Id; //the first soft credit contact
47 protected $_softIndividual2Id; //the second soft credit contact
48 protected $_contributionId;
49 protected $_contributionTypeId = 1;
50 protected $_apiversion;
51 protected $_entity = 'Contribution';
52 public $debug = 0;
53 protected $_params;
54 public $_eNoticeCompliant = TRUE;
55
56 function setUp() {
57 parent::setUp();
58
59 $this->_apiversion = 3;
60 $this->_individualId = $this->individualCreate();
61 $this->_softIndividual1Id = $this->individualCreate();
62 $this->_softIndividual2Id = $this->individualCreate();
63 $this->_contributionId = $this->contributionCreate($this->_individualId);
64
65 $paymentProcessor = $this->processorCreate();
66 $this->_params = array(
67 'contact_id' => $this->_individualId,
68 'receive_date' => '20120511',
69 'total_amount' => 100.00,
70 'financial_type_id' => $this->_contributionTypeId,
71 'non_deductible_amount' => 10.00,
72 'fee_amount' => 5.00,
73 'net_amount' => 95.00,
74 'source' => 'SSF',
75 'contribution_status_id' => 1,
76 'version' => $this->_apiversion,
77 );
78 $this->_processorParams = array(
79 'domain_id' => 1,
80 'name' => 'Dummy',
81 'payment_processor_type_id' => 10,
82 'financial_account_id' => 12,
83 'is_active' => 1,
84 'user_name' => '',
85 'url_site' => 'http://dummy.com',
86 'url_recur' => 'http://dummy.com',
87 'billing_mode' => 1,
88 );
89 // $this->_pageParams = array(
90 // 'version' => 3,
91 // 'title' => 'Test Contribution Page',
92 // 'financial_type_id' => 1,
93 // 'currency' => 'USD',
94 // 'financial_account_id' => 1,
95 // 'payment_processor' => $paymentProcessor->id,
96 // 'is_active' => 1,
97 // 'is_allow_other_amount' => 1,
98 // 'min_amount' => 10,
99 // 'max_amount' => 1000,
100 // );
101 }
102
103 function tearDown() {
104
105 $this->contributionTypeDelete();
106 $this->quickCleanup(array(
107 'civicrm_contribution',
108 'civicrm_event',
109 'civicrm_contribution_page',
110 'civicrm_participant',
111 'civicrm_participant_payment',
112 'civicrm_line_item',
113 'civicrm_financial_trxn',
114 'civicrm_financial_item',
115 'civicrm_entity_financial_trxn',
116 'civicrm_contact',
117 'civicrm_contribution_soft'
118 ));
119 }
120
121 function testGetEmptyParamsContributionSoft() {
122 $params = array();
123 $contribution = civicrm_api('contribution_soft', 'get', $params);
124 $this->assertEquals($contribution['is_error'], 1);
125 $this->assertEquals($contribution['error_message'], 'Mandatory key(s) missing from params array: version');
126 }
127
128 function testGetParamsNotArrayContributionSoft() {
129 $params = 'contact_id= 1';
130 $contribution = civicrm_api('contribution', 'get', $params);
131 $this->assertEquals($contribution['is_error'], 1);
132 $this->assertEquals($contribution['error_message'], 'Input variable `params` is not an array');
133 }
134
135 function testGetContributionSoft() {
136 //We don't test for PCP fields because there's no PCP API, so we can't create campaigns.
137 $p = array(
138 'contribution_id' => $this->_contributionId,
139 'contact_id' => $this->_softIndividual1Id,
140 'amount' => 10.00,
141 'currency' => 'USD',
142 'version' => $this->_apiversion,
143 );
144
145 $this->_softcontribution = civicrm_api('contribution_soft', 'create', $p);
146 $this->assertEquals($this->_softcontribution['is_error'], 0, 'In line ' . __LINE__);
147 $params = array(
148 'id' => $this->_softcontribution['id'],
149 'version' => $this->_apiversion,
150 );
151 $softcontribution = civicrm_api('contribution_soft', 'get', $params);
152 $this->assertAPISuccess($softcontribution, 'In line ' . __LINE__);
153 $this->assertEquals(1,$softcontribution['count']);
154
155 $this->documentMe($params, $softcontribution, __FUNCTION__, __FILE__);
156 $this->assertEquals($softcontribution['values'][$this->_softcontribution['id']]['contribution_id'], $this->_contributionId, 'In line ' . __LINE__);
157 $this->assertEquals($softcontribution['values'][$this->_softcontribution['id']]['contact_id'], $this->_softIndividual1Id, 'In line ' . __LINE__);
158 $this->assertEquals($softcontribution['values'][$this->_softcontribution['id']]['amount'], '10.00', 'In line ' . __LINE__);
159 $this->assertEquals($softcontribution['values'][$this->_softcontribution['id']]['currency'], 'USD', 'In line ' . __LINE__);
160
161 //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)
162 $p['contact_id'] = $this->_softIndividual2Id;
163 $this->_softcontribution2 = civicrm_api('contribution_soft', 'create', $p);
164 $this->assertAPISuccess($this->_softcontribution2, 'In line ' . __LINE__);
165
166 $params = array(
167 'version' => $this->_apiversion,
168 );
169 // now we have 2 - test getcount
170 $softcontribution = civicrm_api('contribution_soft', 'getcount', array(
171 'version' => $this->_apiversion,
172 ));
173
174 $this->assertEquals(2, $softcontribution);
175 //test id only format
176 $softcontribution = civicrm_api('contribution_soft', 'get', array
177 ('version' => $this->_apiversion,
178 'id' => $this->_softcontribution['id'],
179 'format.only_id' => 1,
180 )
181 );
182 $this->assertEquals($this->_softcontribution['id'], $softcontribution, print_r($softcontribution,true) . " in line " . __LINE__);
183 //test id only format - second soft credit
184 $softcontribution = civicrm_api('contribution_soft', 'get', array
185 ('version' => $this->_apiversion,
186 'id' => $this->_softcontribution2['id'],
187 'format.only_id' => 1,
188 )
189 );
190 $this->assertEquals($this->_softcontribution2['id'], $softcontribution);
191 $softcontribution = civicrm_api('contribution_soft', 'get', array(
192 'version' => $this->_apiversion,
193 'id' => $this->_softcontribution['id'],
194 ));
195 //test id as field
196 $this->assertAPISuccess($softcontribution, 'In line ' . __LINE__);
197 $this->assertEquals(1, $softcontribution['count'], 'In line ' . __LINE__);
198 $this->assertEquals($this->_softcontribution['id'], $softcontribution['id'] ) ;
199 //test get by contact id works
200 $softcontribution = civicrm_api('contribution_soft', 'get', array('version' => $this->_apiversion, 'contact_id' => $this->_softIndividual2Id));
201 $this->assertAPISuccess($softcontribution, 'In line ' . __LINE__ . "get with contact_id" . print_r(array('version' => $this->_apiversion, 'contact_id' => $this->_softIndividual2Id), TRUE));
202
203 $this->assertEquals(1, $softcontribution['count'], 'In line ' . __LINE__);
204 civicrm_api('contribution_soft', 'Delete', array(
205 'id' => $this->_softcontribution['id'],
206 'version' => $this->_apiversion,
207 ));
208 civicrm_api('Contribution', 'Delete', array(
209 'id' => $this->_softcontribution2['id'],
210 'version' => $this->_apiversion,
211 ));
212 }
213
214
215 ///////////////// civicrm_contribution_soft
216 function testCreateEmptyParamsContributionSoft() {
217
218
219 $params = array('version' => $this->_apiversion);
220 $softcontribution = civicrm_api('contribution_soft', 'create', $params);
221 $this->assertEquals($softcontribution['is_error'], 1, 'In line ' . __LINE__);
222 $this->assertEquals($softcontribution['error_message'], 'Mandatory key(s) missing from params array: contribution_id, amount, contact_id', 'In line ' . __LINE__);
223 }
224
225 function testCreateParamsNotArrayContributionSoft() {
226
227 $params = 'contact_id= 1';
228 $softcontribution = civicrm_api('contribution_soft', 'create', $params);
229 $this->assertEquals($softcontribution['is_error'], 1);
230 $this->assertEquals($softcontribution['error_message'], 'Input variable `params` is not an array');
231 }
232
233 function testCreateParamsWithoutRequiredKeysContributionSoft() {
234 $params = array('version' => 3);
235 $softcontribution = civicrm_api('contribution_soft', 'create', $params);
236 $this->assertEquals($softcontribution['is_error'], 1);
237 $this->assertEquals($softcontribution['error_message'], 'Mandatory key(s) missing from params array: contribution_id, amount, contact_id');
238 }
239
240 function testCreateContributionSoftInvalidContact() {
241
242 $params = array(
243 'contact_id' => 999,
244 'contribution_id' => $this->_contributionId,
245 'amount' => 10.00,
246 'currency' => 'USD',
247 'version' => $this->_apiversion,
248 );
249
250 $softcontribution = civicrm_api('contribution_soft', 'create', $params);
251 $this->assertEquals($softcontribution['error_message'], 'contact_id is not valid : 999', 'In line ' . __LINE__);
252 }
253
254 function testCreateContributionSoftInvalidContributionId() {
255
256 $params = array(
257 'contribution_id' => 999999,
258 'contact_id' => $this->_softIndividual1Id,
259 'amount' => 10.00,
260 'currency' => 'USD',
261 'version' => $this->_apiversion,
262 );
263
264 $softcontribution = civicrm_api('contribution_soft', 'create', $params);
265 $this->assertEquals($softcontribution['error_message'], 'contribution_id is not valid : 999999', 'In line ' . __LINE__);
266 }
267
268 /*
269 * Function tests that additional financial records are created when fee amount is recorded
270 */
271 function testCreateContributionSoft() {
272 $params = array(
273 'contribution_id' => $this->_contributionId,
274 'contact_id' => $this->_softIndividual1Id,
275 'amount' => 10.00,
276 'currency' => 'USD',
277 'version' => $this->_apiversion,
278 );
279
280 $softcontribution = civicrm_api('contribution_soft', 'create', $params);
281 $this->documentMe($params, $softcontribution, __FUNCTION__, __FILE__);
282 $this->assertEquals($softcontribution['values'][$softcontribution['id']]['contribution_id'], $this->_contributionId, 'In line ' . __LINE__);
283 $this->assertEquals($softcontribution['values'][$softcontribution['id']]['contact_id'], $this->_softIndividual1Id, 'In line ' . __LINE__);
284 $this->assertEquals($softcontribution['values'][$softcontribution['id']]['amount'], '10.00', 'In line ' . __LINE__);
285 $this->assertEquals($softcontribution['values'][$softcontribution['id']]['currency'], 'USD', 'In line ' . __LINE__);
286 }
287
288 /**
289 * Test using example code
290 */
291 // function testContributionSoftCreateExample() {
292 // //make sure at least one page exists since there is a truncate in tear down
293 // $page = civicrm_api('contribution_page', 'create', $this->_pageParams);
294 // $this->assertAPISuccess($page);
295 // //FIXME: Can't written until ContributionSoftDelete is written
296 // require_once 'api/v3/examples/ContributionSoftCreate.php';
297 // $result = contribution_soft_create_example();
298 // $this->assertAPISuccess($result);
299 // $contributionId = $result['id'];
300 // $expectedResult = contribution_soft_create_expectedresult();
301 // $this->checkArrayEquals($result, $expectedResult);
302 // $this->contributionDelete($contributionId);
303 // }
304 //
305 //To Update Soft Contribution
306 function testCreateUpdateContributionSoft() {
307 //create a soft credit
308 $params = array(
309 'contribution_id' => $this->_contributionId,
310 'contact_id' => $this->_softIndividual1Id,
311 'amount' => 10.00,
312 'currency' => 'USD',
313 'version' => $this->_apiversion,
314 );
315
316 $softcontribution = civicrm_api('contribution_soft', 'create', $params);
317 $softcontributionID = $softcontribution['id'];
318
319 $old_params = array(
320 'contribution_soft_id' => $softcontributionID,
321 'version' => $this->_apiversion,
322 );
323 $original = civicrm_api('contribution_soft', 'get', $old_params);
324 //Make sure it came back
325 $this->assertTrue(empty($original['is_error']), 'In line ' . __LINE__);
326 $this->assertEquals($original['id'], $softcontributionID, 'In line ' . __LINE__);
327 //set up list of old params, verify
328 $old_contribution_id = $original['values'][$softcontributionID]['contribution_id'];
329 $old_contact_id = $original['values'][$softcontributionID]['contact_id'];
330 $old_amount = $original['values'][$softcontributionID]['amount'];
331 $old_currency = $original['values'][$softcontributionID]['currency'];
332
333 //check against original values
334 $this->assertEquals($old_contribution_id, $this->_contributionId, 'In line ' . __LINE__);
335 $this->assertEquals($old_contact_id, $this->_softIndividual1Id, 'In line ' . __LINE__);
336 $this->assertEquals($old_amount, 10.00, 'In line ' . __LINE__);
337 $this->assertEquals($old_currency, 'USD', 'In line ' . __LINE__);
338 $params = array(
339 'id' => $softcontributionID,
340 'contribution_id' => $this->_contributionId,
341 'contact_id' => $this->_softIndividual1Id,
342 'amount' => 7.00,
343 'currency' => 'CAD',
344 'version' => $this->_apiversion,
345 );
346
347 $softcontribution = civicrm_api('contribution_soft', 'create', $params);
348
349 $new_params = array(
350 'id' => $softcontribution['id'],
351 'version' => $this->_apiversion,
352 );
353 $softcontribution = civicrm_api('contribution_soft', 'get', $new_params);
354 //check against original values
355 $this->assertEquals($softcontribution['values'][$softcontributionID]['contribution_id'], $this->_contributionId, 'In line ' . __LINE__);
356 $this->assertEquals($softcontribution['values'][$softcontributionID]['contact_id'], $this->_softIndividual1Id, 'In line ' . __LINE__);
357 $this->assertEquals($softcontribution['values'][$softcontributionID]['amount'], 7.00, 'In line ' . __LINE__);
358 $this->assertEquals($softcontribution['values'][$softcontributionID]['currency'], 'CAD', 'In line ' . __LINE__);
359
360 $params = array(
361 'id' => $softcontributionID,
362 'version' => $this->_apiversion,
363 );
364 $result = civicrm_api('contribution_soft', 'delete', $params);
365 $this->assertEquals($result['is_error'], 0, 'in line' . __LINE__);
366 }
367
368 ///////////////// civicrm_contribution_soft_delete methods
369 function testDeleteEmptyParamsContributionSoft() {
370 $params = array('version' => $this->_apiversion);
371 $softcontribution = civicrm_api('contribution_soft', 'delete', $params);
372 $this->assertEquals($softcontribution['is_error'], 1);
373 }
374
375 function testDeleteParamsNotArrayContributionSoft() {
376 $params = 'id= 1';
377 $softcontribution = civicrm_api('contribution_soft', 'delete', $params);
378 $this->assertEquals($softcontribution['is_error'], 1);
379 $this->assertEquals($softcontribution['error_message'], 'Input variable `params` is not an array');
380 }
381
382 function testDeleteWrongParamContributionSoft() {
383 $params = array(
384 'contribution_source' => 'SSF',
385 'version' => $this->_apiversion,
386 );
387 $softcontribution = civicrm_api('contribution_soft', 'delete', $params);
388 $this->assertEquals($softcontribution['is_error'], 1);
389 }
390
391 function testDeleteContributionSoft() {
392 //create a soft credit
393 $params = array(
394 'contribution_id' => $this->_contributionId,
395 'contact_id' => $this->_softIndividual1Id,
396 'amount' => 10.00,
397 'currency' => 'USD',
398 'version' => $this->_apiversion,
399 );
400
401 $softcontribution = civicrm_api('contribution_soft', 'create', $params);
402 $softcontributionID = $softcontribution['id'];
403 $params = array(
404 'id' => $softcontributionID,
405 'version' => $this->_apiversion,
406 );
407 $result = civicrm_api('contribution_soft', 'delete', $params);
408 $this->documentMe($params, $result, __FUNCTION__, __FILE__);
409 $this->assertEquals($result['is_error'], 0, 'In line ' . __LINE__);
410 }
411
412 ///////////////// civicrm_contribution_search methods
413
414 /**
415 * Test civicrm_contribution_soft_search with wrong params type
416 */
417 function testSearchWrongParamsType() {
418 $params = 'a string';
419 $result = civicrm_api('contribution_soft', 'get', $params);
420
421 $this->assertEquals($result['is_error'], 1, 'In line ' . __LINE__);
422 $this->assertEquals($result['error_message'], 'Input variable `params` is not an array', 'In line ' . __LINE__);
423 }
424
425 /**
426 * Test civicrm_contribution_search with empty params.
427 * All available contributions expected.
428 */
429 function testSearchEmptyParams() {
430 $params = array('version' => $this->_apiversion);
431
432 $p = array(
433 'contribution_id' => $this->_contributionId,
434 'contact_id' => $this->_softIndividual1Id,
435 'amount' => 10.00,
436 'currency' => 'USD',
437 'version' => $this->_apiversion,
438 );
439 $softcontribution = civicrm_api('contribution_soft', 'create', $p);
440
441 $result = civicrm_api('contribution_soft', 'get', $params);
442 // We're taking the first element.
443 $res = $result['values'][$softcontribution['id']];
444
445 $this->assertEquals($p['contribution_id'], $res['contribution_id'], 'In line ' . __LINE__);
446 $this->assertEquals($p['contact_id'], $res['contact_id'], 'In line ' . __LINE__);
447 $this->assertEquals($p['amount'], $res['amount'], 'In line ' . __LINE__);
448 $this->assertEquals($p['currency'], $res['currency'], 'In line ' . __LINE__);
449 }
450
451 /**
452 * Test civicrm_contribution_soft_search. Success expected.
453 */
454 function testSearch() {
455 $p1 = array(
456 'contribution_id' => $this->_contributionId,
457 'contact_id' => $this->_softIndividual1Id,
458 'amount' => 10.00,
459 'currency' => 'USD',
460 'version' => $this->_apiversion,
461 );
462 $softcontribution1 = civicrm_api('contribution_soft', 'create', $p1);
463
464 $p2 = array(
465 'contribution_id' => $this->_contributionId,
466 'contact_id' => $this->_softIndividual2Id,
467 'amount' => 25.00,
468 'currency' => 'CAD',
469 'version' => $this->_apiversion,
470 );
471 $softcontribution2 = civicrm_api('contribution_soft', 'create', $p2);
472
473 $params = array(
474 'id' => $softcontribution2['id'],
475 'version' => $this->_apiversion,
476 );
477 $result = civicrm_api('contribution_soft', 'get', $params);
478 $res = $result['values'][$softcontribution2['id']];
479
480 $this->assertEquals($p2['contribution_id'], $res['contribution_id'], 'In line ' . __LINE__);
481 $this->assertEquals($p2['contact_id'], $res['contact_id'], 'In line ' . __LINE__);
482 $this->assertEquals($p2['amount'], $res['amount'], 'In line ' . __LINE__);
483 $this->assertEquals($p2['currency'], $res['currency'], 'In line ' . __LINE__ );
484 }
485 }
486