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;
51 $this->_apiversion
= 3;
53 $tablesToTruncate = array(
54 'civicrm_contribution',
55 'civicrm_contact', 'civicrm_pledge',
58 $this->quickCleanup($tablesToTruncate);
59 $this->_contributionTypeId
= $this->contributionTypeCreate();
60 $this->_individualId
= $this->individualCreate(NULL);
61 $this->_pledgeID
= $this->pledgeCreate($this->_individualId
);
62 $this->_contributionID
= $this->contributionCreate($this->_individualId
, $this->_contributionTypeId
);
66 $tablesToTruncate = array(
67 'civicrm_contribution',
70 'civicrm_pledge_payment',
74 $this->quickCleanup($tablesToTruncate);
75 $this->contributionTypeDelete();
78 function testGetPledgePayment() {
80 'version' => $this->_apiversion
,
82 $result = civicrm_api('pledge_payment', 'get', $params);
83 $this->documentMe($params, $result, __FUNCTION__
, __FILE__
);
84 $this->assertEquals(0, $result['is_error'], " in line " . __LINE__
);
85 $this->assertEquals(5, $result['count'], " in line " . __LINE__
);
89 * Test that passing in a single variable works
91 function testGetSinglePledgePayment() {
94 $createparams = array(
95 'contact_id' => $this->_individualId
,
96 'pledge_id' => $this->_pledgeID
,
97 'contribution_id' => $this->_contributionID
,
98 'version' => $this->_apiversion
,
101 $createResult = civicrm_api('pledge_payment', 'create', $createparams);
102 $this->assertEquals(0, $createResult['is_error'], " in line " . __LINE__
);
104 'version' => $this->_apiversion
,
105 'contribution_id' => $this->_contributionID
,
107 $result = civicrm_api('pledge_payment', 'get', $params);
108 $this->assertEquals(0, $result['is_error'], " in line " . __LINE__
);
109 $this->assertEquals(1, $result['count'], " in line " . __LINE__
);
113 * Test that passing in a single variable works:: status_id
115 function testGetSinglePledgePaymentByStatusID() {
118 $createparams = array(
119 'contact_id' => $this->_individualId
,
120 'pledge_id' => $this->_pledgeID
,
121 'contribution_id' => $this->_contributionID
,
122 'version' => $this->_apiversion
,
125 $createResult = civicrm_api('pledge_payment', 'create', $createparams);
126 $this->assertEquals(0, $createResult['is_error'], " in line " . __LINE__
);
128 'version' => $this->_apiversion
,
132 $result = civicrm_api('pledge_payment', 'get', $params);
133 $this->assertEquals(0, $result['is_error'], " in line " . __LINE__
);
134 $this->assertEquals(1, $result['count'], " in line " . __LINE__
);
138 * Test that creating a payment will add the contribution ID
140 function testCreatePledgePayment() {
141 //check that 5 pledge payments exist at the start
143 'version' => $this->_apiversion
,
145 $beforeAdd = civicrm_api('pledge_payment', 'get', $getParams);
146 $this->assertEquals(0, $beforeAdd['is_error'], " in line " . __LINE__
);
147 $this->assertEquals(5, $beforeAdd['count'], " in line " . __LINE__
);
149 //test the pledge_payment_create function
151 'contact_id' => $this->_individualId
,
152 'pledge_id' => $this->_pledgeID
,
153 'contribution_id' => $this->_contributionID
,
154 'version' => $this->_apiversion
,
156 'actual_amount' => 20,
158 $result = civicrm_api('pledge_payment', 'create', $params);
159 $this->documentMe($params, $result, __FUNCTION__
, __FILE__
);
160 $this->assertEquals(0, $result['is_error'], " in line " . __LINE__
);
162 //check existing updated not new one created - 'create' means add contribution_id in this context
163 $afterAdd = civicrm_api('pledge_payment', 'get', $getParams);
164 $this->assertEquals(0, $beforeAdd['is_error'], " in line " . __LINE__
);
165 $this->assertEquals(5, $afterAdd['count'], " in line " . __LINE__
);
167 //get the created payment & check it out
168 $getParams['id'] = $result['id'];
169 $getIndPayment = civicrm_api('pledge_payment', 'get', $getParams);
170 $this->assertEquals(1, $getIndPayment['count'], " in line " . __LINE__
);
171 $this->assertEquals(20, $getIndPayment['values'][$result['id']]['actual_amount'], " in line " . __LINE__
);
173 //create a second pledge payment - need a contribution first &can't use the CiviUnitTest case function as invoice is hard-coded
174 $contributionParams = array(
176 'total_amount' => 20,
177 'contact_id' => $this->_individualId
,
178 'financial_type_id' => $this->_contributionTypeId
,
180 $contribution = civicrm_api('contribution', 'create', $contributionParams);
182 $this->assertEquals(0, $contribution['is_error'], " in line " . __LINE__
);
184 $params['contribution_id'] = $contribution['id'];
187 $resultCont2 = civicrm_api('pledge_payment', 'create', $params);
188 $this->assertEquals(0, $resultCont2['is_error'], " in line " . __LINE__
);
189 //make sure original is untouched & has not been updated
190 $this->assertGreaterThan($result['id'], $resultCont2['id'], " in line " . __LINE__
);
191 $getIndPaymentAgain = civicrm_api('pledge_payment', 'get', $getParams);
192 $this->assertEquals(1, $getIndPaymentAgain['count'], " in line " . __LINE__
);
193 $this->assertEquals($this->_contributionID
, $getIndPaymentAgain['values'][$result['id']]['contribution_id'], " in line " . __LINE__
);
197 * test checks behaviour when more payments are created than should be possible
199 function testCreatePledgePaymentAllCreated() {
202 'pledge_id' => $this->_pledgeID
,
205 // create one more pledge than there are spaces for
208 $contributionParams = array(
210 'total_amount' => 20,
211 'contact_id' => $this->_individualId
,
212 'financial_type_id' => $this->_contributionTypeId
,
214 $contribution = civicrm_api('contribution', 'create', $contributionParams);
216 $this->assertEquals(0, $contribution['is_error'], " in line " . __LINE__
);
218 $params['contribution_id'] = $contribution['id'];
219 $resultCont2 = civicrm_api('pledge_payment', 'create', $params);
222 // check that only 5 exist & we got an error setting the 6th
223 $result = civicrm_api('PledgePayment', 'Get', array(
225 'pledge_id' => $this->_pledgeID
,
228 $this->assertEquals(5, $result['count']);
229 $this->assertEquals(1, $resultCont2['is_error']);
230 $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']);
232 $params['option.create_new'] = 1;
233 $params['scheduled_amount'] = 20;
234 $params['scheduled_date'] = '20131212';
235 $resultcreatenew = civicrm_api('pledge_payment', 'create', $params);
236 $this->assertAPISuccess($resultcreatenew);
238 $this->assertEquals(0, $resultcreatenew['is_error'], "in line " . __LINE__
);
239 $result = civicrm_api('PledgePayment', 'Get', array(
241 'pledge_id' => $this->_pledgeID
,
244 $this->assertEquals(6, $result['count']);
247 * Test that creating a payment will add the contribution ID where only one pledge payment
250 function testCreatePledgePaymentWhereOnlyOnePayment() {
252 $pledgeParams = array(
253 'contact_id' => $this->_individualId
,
254 'pledge_create_date' => date('Ymd'),
255 'start_date' => date('Ymd'),
256 'scheduled_date' => $this->scheduled_date
,
257 'pledge_amount' => 100.00,
258 'pledge_status_id' => '2',
259 'pledge_financial_type_id' => '1',
260 'pledge_original_installment_amount' => 20,
261 'frequency_interval' => 5,
262 'frequency_unit' => 'year',
263 'frequency_day' => 15,
266 'version' => $this->_apiversion
,
269 $contributionID = $this->contributionCreate($this->_individualId
, $this->_contributionTypeId
, 45, 45);
270 $pledge = civicrm_api('Pledge', 'Create', $pledgeParams);
271 $this->assertEquals(0, $pledge['is_error'], " in line " . __LINE__
);
273 //test the pledge_payment_create function
275 'contact_id' => $this->_individualId
,
276 'pledge_id' => $pledge['id'],
277 'contribution_id' => $contributionID,
278 'version' => $this->_apiversion
,
280 'actual_amount' => 20,
282 $result = civicrm_api('pledge_payment', 'create', $params);
284 $this->assertEquals(0, $result['is_error'], " in line " . __LINE__
);
286 //check existing updated not new one created - 'create' means add contribution_id in this context
287 $afterAdd = civicrm_api('pledge_payment', 'get', array(
288 'version' => 3, 'contribution_id' => $contributionID,
290 $this->assertEquals(1, $afterAdd['count'], " in line " . __LINE__
);
293 function testUpdatePledgePayment() {
295 'pledge_id' => $this->_pledgeID
,
296 'contribution_id' => $this->_contributionID
,
297 'version' => $this->_apiversion
,
299 'actual_amount' => 20,
301 $result = civicrm_api('pledge_payment', 'create', $params);
302 $updateparams = array(
303 'id' => $result['id'],
305 'version' => $this->_apiversion
,
308 $result = civicrm_api('pledge_payment', 'update', $updateparams);
309 $this->documentMe($params, $result, __FUNCTION__
, __FILE__
);
310 $this->assertAPISuccess($result, ' in line ' . __LINE__
);
311 $this->getAndCheck(array_merge($params,$updateparams), $result['id'], $this->_entity
);
314 function testDeletePledgePayment() {
316 'contact_id' => $this->_individualId
,
317 'pledge_id' => $this->_pledgeID
,
318 'contribution_id' => $this->_contributionID
,
319 'version' => $this->_apiversion
,
322 'actual_amount' => 20,
324 $pledgePayment = civicrm_api('pledge_payment', 'create', $params);
326 $deleteParams = array(
327 'id' => $pledgePayment['id'],
328 'version' => $this->_apiversion
,
330 $result = civicrm_api('pledge_payment', 'delete', $deleteParams);
331 $this->documentMe($deleteParams, $result, __FUNCTION__
, __FILE__
);
332 $this->assertEquals(0, $result['is_error'], " in line " . __LINE__
);
335 function testGetFields() {
336 $result = civicrm_api('PledgePayment', 'GetFields', array());
337 $this->assertType('array', $result);