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';
31 * Test class for Pledge API - civicrm_pledge_*
33 * @package CiviCRM_APIv3
36 class api_v3_PledgePaymentTest
extends CiviUnitTestCase
{
39 * Assume empty database with just civicrm_data
41 protected $_individualId;
43 protected $_apiversion;
44 protected $_contributionID;
45 protected $_contributionTypeId;
46 protected $_entity = 'PledgePayment';
48 public $DBResetRequired = TRUE; function setUp() {
49 $this->_apiversion
= 3;
51 $tablesToTruncate = array(
52 'civicrm_contribution',
53 'civicrm_contact', 'civicrm_pledge',
56 $this->quickCleanup($tablesToTruncate);
57 $this->_contributionTypeId
= $this->contributionTypeCreate();
58 $this->_individualId
= $this->individualCreate(NULL);
59 $this->_pledgeID
= $this->pledgeCreate($this->_individualId
);
60 $this->_contributionID
= $this->contributionCreate($this->_individualId
, $this->_contributionTypeId
);
64 $tablesToTruncate = array(
65 'civicrm_contribution',
68 'civicrm_pledge_payment',
72 $this->quickCleanup($tablesToTruncate);
73 $this->contributionTypeDelete();
76 function testGetPledgePayment() {
78 'version' => $this->_apiversion
,
80 $result = civicrm_api('pledge_payment', 'get', $params);
81 $this->documentMe($params, $result, __FUNCTION__
, __FILE__
);
82 $this->assertEquals(0, $result['is_error'], " in line " . __LINE__
);
83 $this->assertEquals(5, $result['count'], " in line " . __LINE__
);
87 * Test that passing in a single variable works
89 function testGetSinglePledgePayment() {
92 $createparams = array(
93 'contact_id' => $this->_individualId
,
94 'pledge_id' => $this->_pledgeID
,
95 'contribution_id' => $this->_contributionID
,
96 'version' => $this->_apiversion
,
99 $createResult = civicrm_api('pledge_payment', 'create', $createparams);
100 $this->assertEquals(0, $createResult['is_error'], " in line " . __LINE__
);
102 'version' => $this->_apiversion
,
103 'contribution_id' => $this->_contributionID
,
105 $result = civicrm_api('pledge_payment', 'get', $params);
106 $this->assertEquals(0, $result['is_error'], " in line " . __LINE__
);
107 $this->assertEquals(1, $result['count'], " in line " . __LINE__
);
111 * Test that passing in a single variable works:: status_id
113 function testGetSinglePledgePaymentByStatusID() {
116 $createparams = array(
117 'contact_id' => $this->_individualId
,
118 'pledge_id' => $this->_pledgeID
,
119 'contribution_id' => $this->_contributionID
,
120 'version' => $this->_apiversion
,
123 $createResult = civicrm_api('pledge_payment', 'create', $createparams);
124 $this->assertEquals(0, $createResult['is_error'], " in line " . __LINE__
);
126 'version' => $this->_apiversion
,
130 $result = civicrm_api('pledge_payment', 'get', $params);
131 $this->assertEquals(0, $result['is_error'], " in line " . __LINE__
);
132 $this->assertEquals(1, $result['count'], " in line " . __LINE__
);
136 * Test that creating a payment will add the contribution ID
138 function testCreatePledgePayment() {
139 //check that 5 pledge payments exist at the start
141 'version' => $this->_apiversion
,
143 $beforeAdd = civicrm_api('pledge_payment', 'get', $getParams);
144 $this->assertEquals(0, $beforeAdd['is_error'], " in line " . __LINE__
);
145 $this->assertEquals(5, $beforeAdd['count'], " in line " . __LINE__
);
147 //test the pledge_payment_create function
149 'contact_id' => $this->_individualId
,
150 'pledge_id' => $this->_pledgeID
,
151 'contribution_id' => $this->_contributionID
,
152 'version' => $this->_apiversion
,
154 'actual_amount' => 20,
156 $result = civicrm_api('pledge_payment', 'create', $params);
157 $this->documentMe($params, $result, __FUNCTION__
, __FILE__
);
158 $this->assertEquals(0, $result['is_error'], " in line " . __LINE__
);
160 //check existing updated not new one created - 'create' means add contribution_id in this context
161 $afterAdd = civicrm_api('pledge_payment', 'get', $getParams);
162 $this->assertEquals(0, $beforeAdd['is_error'], " in line " . __LINE__
);
163 $this->assertEquals(5, $afterAdd['count'], " in line " . __LINE__
);
165 //get the created payment & check it out
166 $getParams['id'] = $result['id'];
167 $getIndPayment = civicrm_api('pledge_payment', 'get', $getParams);
168 $this->assertEquals(1, $getIndPayment['count'], " in line " . __LINE__
);
169 $this->assertEquals(20, $getIndPayment['values'][$result['id']]['actual_amount'], " in line " . __LINE__
);
171 //create a second pledge payment - need a contribution first &can't use the CiviUnitTest case function as invoice is hard-coded
172 $contributionParams = array(
174 'total_amount' => 20,
175 'contact_id' => $this->_individualId
,
176 'financial_type_id' => $this->_contributionTypeId
,
178 $contribution = civicrm_api('contribution', 'create', $contributionParams);
180 $this->assertEquals(0, $contribution['is_error'], " in line " . __LINE__
);
182 $params['contribution_id'] = $contribution['id'];
185 $resultCont2 = civicrm_api('pledge_payment', 'create', $params);
186 $this->assertEquals(0, $resultCont2['is_error'], " in line " . __LINE__
);
187 //make sure original is untouched & has not been updated
188 $this->assertGreaterThan($result['id'], $resultCont2['id'], " in line " . __LINE__
);
189 $getIndPaymentAgain = civicrm_api('pledge_payment', 'get', $getParams);
190 $this->assertEquals(1, $getIndPaymentAgain['count'], " in line " . __LINE__
);
191 $this->assertEquals($this->_contributionID
, $getIndPaymentAgain['values'][$result['id']]['contribution_id'], " in line " . __LINE__
);
195 * test checks behaviour when more payments are created than should be possible
197 function testCreatePledgePaymentAllCreated() {
200 'pledge_id' => $this->_pledgeID
,
203 // create one more pledge than there are spaces for
206 $contributionParams = array(
208 'total_amount' => 20,
209 'contact_id' => $this->_individualId
,
210 'financial_type_id' => $this->_contributionTypeId
,
212 $contribution = civicrm_api('contribution', 'create', $contributionParams);
214 $this->assertEquals(0, $contribution['is_error'], " in line " . __LINE__
);
216 $params['contribution_id'] = $contribution['id'];
217 $resultCont2 = civicrm_api('pledge_payment', 'create', $params);
220 // check that only 5 exist & we got an error setting the 6th
221 $result = civicrm_api('PledgePayment', 'Get', array(
223 'pledge_id' => $this->_pledgeID
,
226 $this->assertEquals(5, $result['count']);
227 $this->assertEquals(1, $resultCont2['is_error']);
228 $this->assertEquals("There are no unmatched payment on this pledge. Pass in the pledge_payment id to specify one or 'option.create_new' to create one", $resultCont2['error_message']);
230 $params['option.create_new'] = 1;
231 $params['scheduled_amount'] = 20;
232 $params['scheduled_date'] = '20131212';
233 $resultcreatenew = civicrm_api('pledge_payment', 'create', $params);
234 $this->assertAPISuccess($resultcreatenew);
236 $this->assertEquals(0, $resultcreatenew['is_error'], "in line " . __LINE__
);
237 $result = civicrm_api('PledgePayment', 'Get', array(
239 'pledge_id' => $this->_pledgeID
,
242 $this->assertEquals(6, $result['count']);
245 * Test that creating a payment will add the contribution ID where only one pledge payment
248 function testCreatePledgePaymentWhereOnlyOnePayment() {
250 $pledgeParams = array(
251 'contact_id' => $this->_individualId
,
252 'pledge_create_date' => date('Ymd'),
253 'start_date' => date('Ymd'),
254 'scheduled_date' => $this->scheduled_date
,
255 'pledge_amount' => 100.00,
256 'pledge_status_id' => '2',
257 'pledge_financial_type_id' => '1',
258 'pledge_original_installment_amount' => 20,
259 'frequency_interval' => 5,
260 'frequency_unit' => 'year',
261 'frequency_day' => 15,
264 'version' => $this->_apiversion
,
267 $contributionID = $this->contributionCreate($this->_individualId
, $this->_contributionTypeId
, 45, 45);
268 $pledge = civicrm_api('Pledge', 'Create', $pledgeParams);
269 $this->assertEquals(0, $pledge['is_error'], " in line " . __LINE__
);
271 //test the pledge_payment_create function
273 'contact_id' => $this->_individualId
,
274 'pledge_id' => $pledge['id'],
275 'contribution_id' => $contributionID,
276 'version' => $this->_apiversion
,
278 'actual_amount' => 20,
280 $result = civicrm_api('pledge_payment', 'create', $params);
282 $this->assertEquals(0, $result['is_error'], " in line " . __LINE__
);
284 //check existing updated not new one created - 'create' means add contribution_id in this context
285 $afterAdd = civicrm_api('pledge_payment', 'get', array(
286 'version' => 3, 'contribution_id' => $contributionID,
288 $this->assertEquals(1, $afterAdd['count'], " in line " . __LINE__
);
291 function testUpdatePledgePayment() {
293 'pledge_id' => $this->_pledgeID
,
294 'contribution_id' => $this->_contributionID
,
295 'version' => $this->_apiversion
,
297 'actual_amount' => 20,
299 $result = civicrm_api('pledge_payment', 'create', $params);
300 $updateparams = array(
301 'id' => $result['id'],
303 'version' => $this->_apiversion
,
306 $result = civicrm_api('pledge_payment', 'update', $updateparams);
307 $this->documentMe($params, $result, __FUNCTION__
, __FILE__
);
308 $this->assertAPISuccess($result, ' in line ' . __LINE__
);
309 $this->getAndCheck(array_merge($params,$updateparams), $result['id'], $this->_entity
);
312 function testDeletePledgePayment() {
314 'contact_id' => $this->_individualId
,
315 'pledge_id' => $this->_pledgeID
,
316 'contribution_id' => $this->_contributionID
,
317 'version' => $this->_apiversion
,
320 'actual_amount' => 20,
322 $pledgePayment = civicrm_api('pledge_payment', 'create', $params);
324 $deleteParams = array(
325 'id' => $pledgePayment['id'],
326 'version' => $this->_apiversion
,
328 $result = civicrm_api('pledge_payment', 'delete', $deleteParams);
329 $this->documentMe($deleteParams, $result, __FUNCTION__
, __FILE__
);
330 $this->assertEquals(0, $result['is_error'], " in line " . __LINE__
);
333 function testGetFields() {
334 $result = civicrm_api('PledgePayment', 'GetFields', array());
335 $this->assertType('array', $result);