3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.3 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
28 require_once 'CiviTest/CiviUnitTestCase.php';
29 //FIXME:This existed in ContributionTest, don't think it's necessary
30 //require_once 'CiviTest/CiviMailUtils.php';
34 * Test APIv3 civicrm_contribute_* functions
36 * @package CiviCRM_APIv3
37 * @subpackage API_ContributionSoft
40 class api_v3_ContributionSoftTest
extends CiviUnitTestCase
{
43 * Assume empty database with just civicrm_data
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';
54 public $_eNoticeCompliant = TRUE;
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
);
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,
73 'net_amount' => 95.00,
75 'contribution_status_id' => 1,
76 'version' => $this->_apiversion
,
78 $this->_processorParams
= array(
81 'payment_processor_type_id' => 10,
82 'financial_account_id' => 12,
85 'url_site' => 'http://dummy.com',
86 'url_recur' => 'http://dummy.com',
89 // $this->_pageParams = array(
91 // 'title' => 'Test Contribution Page',
92 // 'financial_type_id' => 1,
93 // 'currency' => 'USD',
94 // 'financial_account_id' => 1,
95 // 'payment_processor' => $paymentProcessor->id,
97 // 'is_allow_other_amount' => 1,
98 // 'min_amount' => 10,
99 // 'max_amount' => 1000,
103 function tearDown() {
105 $this->contributionTypeDelete();
106 $this->quickCleanup(array(
107 'civicrm_contribution',
109 'civicrm_contribution_page',
110 'civicrm_participant',
111 'civicrm_participant_payment',
113 'civicrm_financial_trxn',
114 'civicrm_financial_item',
115 'civicrm_entity_financial_trxn',
117 'civicrm_contribution_soft'
121 function testGetEmptyParamsContributionSoft() {
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');
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');
135 function testGetContributionSoft() {
136 //We don't test for PCP fields because there's no PCP API, so we can't create campaigns.
138 'contribution_id' => $this->_contributionId
,
139 'contact_id' => $this->_softIndividual1Id
,
142 'version' => $this->_apiversion
,
145 $this->_softcontribution
= civicrm_api('contribution_soft', 'create', $p);
146 $this->assertEquals($this->_softcontribution
['is_error'], 0, 'In line ' . __LINE__
);
148 'id' => $this->_softcontribution
['id'],
149 'version' => $this->_apiversion
,
151 $softcontribution = civicrm_api('contribution_soft', 'get', $params);
152 $this->assertAPISuccess($softcontribution, 'In line ' . __LINE__
);
153 $this->assertEquals(1,$softcontribution['count']);
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__
);
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__
);
167 'version' => $this->_apiversion
,
169 // now we have 2 - test getcount
170 $softcontribution = civicrm_api('contribution_soft', 'getcount', array(
171 'version' => $this->_apiversion
,
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,
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,
190 $this->assertEquals($this->_softcontribution2
['id'], $softcontribution);
191 $softcontribution = civicrm_api('contribution_soft', 'get', array(
192 'version' => $this->_apiversion
,
193 'id' => $this->_softcontribution
['id'],
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));
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
,
208 civicrm_api('Contribution', 'Delete', array(
209 'id' => $this->_softcontribution2
['id'],
210 'version' => $this->_apiversion
,
215 ///////////////// civicrm_contribution_soft
216 function testCreateEmptyParamsContributionSoft() {
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__
);
225 function testCreateParamsNotArrayContributionSoft() {
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');
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');
240 function testCreateContributionSoftInvalidContact() {
244 'contribution_id' => $this->_contributionId
,
247 'version' => $this->_apiversion
,
250 $softcontribution = civicrm_api('contribution_soft', 'create', $params);
251 $this->assertEquals($softcontribution['error_message'], 'contact_id is not valid : 999', 'In line ' . __LINE__
);
254 function testCreateContributionSoftInvalidContributionId() {
257 'contribution_id' => 999999,
258 'contact_id' => $this->_softIndividual1Id
,
261 'version' => $this->_apiversion
,
264 $softcontribution = civicrm_api('contribution_soft', 'create', $params);
265 $this->assertEquals($softcontribution['error_message'], 'contribution_id is not valid : 999999', 'In line ' . __LINE__
);
269 * Function tests that additional financial records are created when fee amount is recorded
271 function testCreateContributionSoft() {
273 'contribution_id' => $this->_contributionId
,
274 'contact_id' => $this->_softIndividual1Id
,
277 'version' => $this->_apiversion
,
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__
);
289 * Test using example code
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);
305 //To Update Soft Contribution
306 function testCreateUpdateContributionSoft() {
307 //create a soft credit
309 'contribution_id' => $this->_contributionId
,
310 'contact_id' => $this->_softIndividual1Id
,
313 'version' => $this->_apiversion
,
316 $softcontribution = civicrm_api('contribution_soft', 'create', $params);
317 $softcontributionID = $softcontribution['id'];
320 'contribution_soft_id' => $softcontributionID,
321 'version' => $this->_apiversion
,
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'];
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__
);
339 'id' => $softcontributionID,
340 'contribution_id' => $this->_contributionId
,
341 'contact_id' => $this->_softIndividual1Id
,
344 'version' => $this->_apiversion
,
347 $softcontribution = civicrm_api('contribution_soft', 'create', $params);
350 'id' => $softcontribution['id'],
351 'version' => $this->_apiversion
,
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__
);
361 'id' => $softcontributionID,
362 'version' => $this->_apiversion
,
364 $result = civicrm_api('contribution_soft', 'delete', $params);
365 $this->assertAPISuccess($result, 'in line' . __LINE__
);
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);
375 function testDeleteParamsNotArrayContributionSoft() {
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');
382 function testDeleteWrongParamContributionSoft() {
384 'contribution_source' => 'SSF',
385 'version' => $this->_apiversion
,
387 $softcontribution = civicrm_api('contribution_soft', 'delete', $params);
388 $this->assertEquals($softcontribution['is_error'], 1);
391 function testDeleteContributionSoft() {
392 //create a soft credit
394 'contribution_id' => $this->_contributionId
,
395 'contact_id' => $this->_softIndividual1Id
,
398 'version' => $this->_apiversion
,
401 $softcontribution = civicrm_api('contribution_soft', 'create', $params);
402 $softcontributionID = $softcontribution['id'];
404 'id' => $softcontributionID,
405 'version' => $this->_apiversion
,
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__
);
412 ///////////////// civicrm_contribution_search methods
415 * Test civicrm_contribution_soft_search with wrong params type
417 function testSearchWrongParamsType() {
418 $params = 'a string';
419 $result = civicrm_api('contribution_soft', 'get', $params);
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__
);
426 * Test civicrm_contribution_search with empty params.
427 * All available contributions expected.
429 function testSearchEmptyParams() {
430 $params = array('version' => $this->_apiversion
);
433 'contribution_id' => $this->_contributionId
,
434 'contact_id' => $this->_softIndividual1Id
,
437 'version' => $this->_apiversion
,
439 $softcontribution = civicrm_api('contribution_soft', 'create', $p);
441 $result = civicrm_api('contribution_soft', 'get', $params);
442 // We're taking the first element.
443 $res = $result['values'][$softcontribution['id']];
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__
);
452 * Test civicrm_contribution_soft_search. Success expected.
454 function testSearch() {
456 'contribution_id' => $this->_contributionId
,
457 'contact_id' => $this->_softIndividual1Id
,
460 'version' => $this->_apiversion
,
462 $softcontribution1 = civicrm_api('contribution_soft', 'create', $p1);
465 'contribution_id' => $this->_contributionId
,
466 'contact_id' => $this->_softIndividual2Id
,
469 'version' => $this->_apiversion
,
471 $softcontribution2 = civicrm_api('contribution_soft', 'create', $p2);
474 'id' => $softcontribution2['id'],
475 'version' => $this->_apiversion
,
477 $result = civicrm_api('contribution_soft', 'get', $params);
478 $res = $result['values'][$softcontribution2['id']];
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__
);