Merge pull request #941 from kurund/CRM-12711
[civicrm-core.git] / tests / phpunit / api / v3 / PledgePaymentTest.php
CommitLineData
6a488035
TO
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
28require_once 'CiviTest/CiviUnitTestCase.php';
29
30/**
31 * Test class for Pledge API - civicrm_pledge_*
32 *
33 * @package CiviCRM_APIv3
34 */
35
36class api_v3_PledgePaymentTest extends CiviUnitTestCase {
37
38 /**
39 * Assume empty database with just civicrm_data
40 */
41 protected $_individualId;
42 protected $_pledgeID;
43 protected $_apiversion;
44 protected $_contributionID;
45 protected $_contributionTypeId;
46 protected $_entity = 'PledgePayment';
47
48 public $DBResetRequired = TRUE; function setUp() {
49 $this->_apiversion = 3;
50 parent::setUp();
51 $tablesToTruncate = array(
52 'civicrm_contribution',
53 'civicrm_contact', 'civicrm_pledge',
54 );
55
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);
61 }
62
63 function tearDown() {
64 $tablesToTruncate = array(
65 'civicrm_contribution',
66 'civicrm_contact',
67 'civicrm_pledge',
68 'civicrm_pledge_payment',
69 'civicrm_line_item',
70 );
71
72 $this->quickCleanup($tablesToTruncate);
73 $this->contributionTypeDelete();
74 }
75
76 function testGetPledgePayment() {
77 $params = array(
78 'version' => $this->_apiversion,
79 );
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__);
84 }
85
86 /*
87 * Test that passing in a single variable works
88 */
89 function testGetSinglePledgePayment() {
90
91
92 $createparams = array(
93 'contact_id' => $this->_individualId,
94 'pledge_id' => $this->_pledgeID,
95 'contribution_id' => $this->_contributionID,
96 'version' => $this->_apiversion,
97 'status_id' => 1,
98 );
99 $createResult = civicrm_api('pledge_payment', 'create', $createparams);
100 $this->assertEquals(0, $createResult['is_error'], " in line " . __LINE__);
101 $params = array(
102 'version' => $this->_apiversion,
103 'contribution_id' => $this->_contributionID,
104 );
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__);
108 }
109
110 /*
111 * Test that passing in a single variable works:: status_id
112 */
113 function testGetSinglePledgePaymentByStatusID() {
114
115
116 $createparams = array(
117 'contact_id' => $this->_individualId,
118 'pledge_id' => $this->_pledgeID,
119 'contribution_id' => $this->_contributionID,
120 'version' => $this->_apiversion,
121 'status_id' => 1,
122 );
123 $createResult = civicrm_api('pledge_payment', 'create', $createparams);
124 $this->assertEquals(0, $createResult['is_error'], " in line " . __LINE__);
125 $params = array(
126 'version' => $this->_apiversion,
127 'status_id' => 1,
128 );
129
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__);
133 }
134
135 /*
136 * Test that creating a payment will add the contribution ID
137 */
138 function testCreatePledgePayment() {
139 //check that 5 pledge payments exist at the start
140 $getParams = array(
141 'version' => $this->_apiversion,
142 );
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__);
146
147 //test the pledge_payment_create function
148 $params = array(
149 'contact_id' => $this->_individualId,
150 'pledge_id' => $this->_pledgeID,
151 'contribution_id' => $this->_contributionID,
152 'version' => $this->_apiversion,
153 'status_id' => 1,
154 'actual_amount' => 20,
155 );
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__);
159
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__);
164
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__);
170
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(
173 'version' => 3,
174 'total_amount' => 20,
175 'contact_id' => $this->_individualId,
176 'financial_type_id' => $this->_contributionTypeId,
177 );
178 $contribution = civicrm_api('contribution', 'create', $contributionParams);
179
180 $this->assertEquals(0, $contribution['is_error'], " in line " . __LINE__);
181
182 $params['contribution_id'] = $contribution['id'];
183
184
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__);
192 }
193
194 /*
195 * test checks behaviour when more payments are created than should be possible
196 */
197 function testCreatePledgePaymentAllCreated() {
198 $params = array(
199 'version' => 3,
200 'pledge_id' => $this->_pledgeID,
201 'status_id' => 1,
202 );
203 // create one more pledge than there are spaces for
204 $i = 0;
205 while ($i <= 5) {
206 $contributionParams = array(
207 'version' => 3,
208 'total_amount' => 20,
209 'contact_id' => $this->_individualId,
210 'financial_type_id' => $this->_contributionTypeId,
211 );
212 $contribution = civicrm_api('contribution', 'create', $contributionParams);
213
214 $this->assertEquals(0, $contribution['is_error'], " in line " . __LINE__);
215
216 $params['contribution_id'] = $contribution['id'];
217 $resultCont2 = civicrm_api('pledge_payment', 'create', $params);
218 $i++;
219 }
220 // check that only 5 exist & we got an error setting the 6th
221 $result = civicrm_api('PledgePayment', 'Get', array(
222 'version' => 3,
223 'pledge_id' => $this->_pledgeID,
224 ));
225
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']);
229
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);
235
236 $this->assertEquals(0, $resultcreatenew['is_error'], "in line " . __LINE__);
237 $result = civicrm_api('PledgePayment', 'Get', array(
238 'version' => 3,
239 'pledge_id' => $this->_pledgeID,
240 ));
241
242 $this->assertEquals(6, $result['count']);
243 }
244 /*
245 * Test that creating a payment will add the contribution ID where only one pledge payment
246 * in schedule
247 */
248 function testCreatePledgePaymentWhereOnlyOnePayment() {
249
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,
262 'installments' => 1,
263 'sequential' => 1,
264 'version' => $this->_apiversion,
265 );
266
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__);
270
271 //test the pledge_payment_create function
272 $params = array(
273 'contact_id' => $this->_individualId,
274 'pledge_id' => $pledge['id'],
275 'contribution_id' => $contributionID,
276 'version' => $this->_apiversion,
277 'status_id' => 1,
278 'actual_amount' => 20,
279 );
280 $result = civicrm_api('pledge_payment', 'create', $params);
281
282 $this->assertEquals(0, $result['is_error'], " in line " . __LINE__);
283
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,
287 ));
288 $this->assertEquals(1, $afterAdd['count'], " in line " . __LINE__);
289 }
290
291 function testUpdatePledgePayment() {
292 $params = array(
293 'pledge_id' => $this->_pledgeID,
294 'contribution_id' => $this->_contributionID,
295 'version' => $this->_apiversion,
296 'status_id' => 2,
297 'actual_amount' => 20,
298 );
299 $result = civicrm_api('pledge_payment', 'create', $params);
300 $updateparams = array(
301 'id' => $result['id'],
302 'status_id' => 1,
303 'version' => $this->_apiversion,
304 );
305
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);
310 }
311
312 function testDeletePledgePayment() {
313 $params = array(
314 'contact_id' => $this->_individualId,
315 'pledge_id' => $this->_pledgeID,
316 'contribution_id' => $this->_contributionID,
317 'version' => $this->_apiversion,
318 'status_id' => 1,
319 'sequential' => 1,
320 'actual_amount' => 20,
321 );
322 $pledgePayment = civicrm_api('pledge_payment', 'create', $params);
323
324 $deleteParams = array(
325 'id' => $pledgePayment['id'],
326 'version' => $this->_apiversion,
327 );
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__);
331 }
332
333 function testGetFields() {
334 $result = civicrm_api('PledgePayment', 'GetFields', array());
335 $this->assertType('array', $result);
336 }
337}
338