CRM-13072 upgrade Paritipant Payment test classes to pass
[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';
26212228 47 public $_eNoticeCompliant = FALSE;
430ae6dd
TO
48 public $DBResetRequired = TRUE;
49
50 function setUp() {
6a488035
TO
51 $this->_apiversion = 3;
52 parent::setUp();
53 $tablesToTruncate = array(
54 'civicrm_contribution',
55 'civicrm_contact', 'civicrm_pledge',
56 );
57
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);
63 }
64
65 function tearDown() {
66 $tablesToTruncate = array(
67 'civicrm_contribution',
68 'civicrm_contact',
69 'civicrm_pledge',
70 'civicrm_pledge_payment',
71 'civicrm_line_item',
72 );
73
74 $this->quickCleanup($tablesToTruncate);
75 $this->contributionTypeDelete();
76 }
77
78 function testGetPledgePayment() {
79 $params = array(
80 'version' => $this->_apiversion,
81 );
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__);
86 }
87
88 /*
89 * Test that passing in a single variable works
90 */
91 function testGetSinglePledgePayment() {
92
93
94 $createparams = array(
95 'contact_id' => $this->_individualId,
96 'pledge_id' => $this->_pledgeID,
97 'contribution_id' => $this->_contributionID,
98 'version' => $this->_apiversion,
99 'status_id' => 1,
100 );
101 $createResult = civicrm_api('pledge_payment', 'create', $createparams);
102 $this->assertEquals(0, $createResult['is_error'], " in line " . __LINE__);
103 $params = array(
104 'version' => $this->_apiversion,
105 'contribution_id' => $this->_contributionID,
106 );
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__);
110 }
111
112 /*
113 * Test that passing in a single variable works:: status_id
114 */
115 function testGetSinglePledgePaymentByStatusID() {
116
117
118 $createparams = array(
119 'contact_id' => $this->_individualId,
120 'pledge_id' => $this->_pledgeID,
121 'contribution_id' => $this->_contributionID,
122 'version' => $this->_apiversion,
123 'status_id' => 1,
124 );
125 $createResult = civicrm_api('pledge_payment', 'create', $createparams);
126 $this->assertEquals(0, $createResult['is_error'], " in line " . __LINE__);
127 $params = array(
128 'version' => $this->_apiversion,
129 'status_id' => 1,
130 );
131
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__);
135 }
136
137 /*
138 * Test that creating a payment will add the contribution ID
139 */
140 function testCreatePledgePayment() {
141 //check that 5 pledge payments exist at the start
142 $getParams = array(
143 'version' => $this->_apiversion,
144 );
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__);
148
149 //test the pledge_payment_create function
150 $params = array(
151 'contact_id' => $this->_individualId,
152 'pledge_id' => $this->_pledgeID,
153 'contribution_id' => $this->_contributionID,
154 'version' => $this->_apiversion,
155 'status_id' => 1,
156 'actual_amount' => 20,
157 );
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__);
161
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__);
166
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__);
172
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(
175 'version' => 3,
176 'total_amount' => 20,
177 'contact_id' => $this->_individualId,
178 'financial_type_id' => $this->_contributionTypeId,
179 );
180 $contribution = civicrm_api('contribution', 'create', $contributionParams);
181
182 $this->assertEquals(0, $contribution['is_error'], " in line " . __LINE__);
183
184 $params['contribution_id'] = $contribution['id'];
185
186
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__);
194 }
195
196 /*
197 * test checks behaviour when more payments are created than should be possible
198 */
199 function testCreatePledgePaymentAllCreated() {
200 $params = array(
201 'version' => 3,
202 'pledge_id' => $this->_pledgeID,
203 'status_id' => 1,
204 );
205 // create one more pledge than there are spaces for
206 $i = 0;
207 while ($i <= 5) {
208 $contributionParams = array(
209 'version' => 3,
210 'total_amount' => 20,
211 'contact_id' => $this->_individualId,
212 'financial_type_id' => $this->_contributionTypeId,
213 );
214 $contribution = civicrm_api('contribution', 'create', $contributionParams);
215
216 $this->assertEquals(0, $contribution['is_error'], " in line " . __LINE__);
217
218 $params['contribution_id'] = $contribution['id'];
219 $resultCont2 = civicrm_api('pledge_payment', 'create', $params);
220 $i++;
221 }
222 // check that only 5 exist & we got an error setting the 6th
223 $result = civicrm_api('PledgePayment', 'Get', array(
224 'version' => 3,
225 'pledge_id' => $this->_pledgeID,
226 ));
227
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']);
231
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);
b6708aeb 237
6a488035
TO
238 $this->assertEquals(0, $resultcreatenew['is_error'], "in line " . __LINE__);
239 $result = civicrm_api('PledgePayment', 'Get', array(
240 'version' => 3,
241 'pledge_id' => $this->_pledgeID,
242 ));
243
244 $this->assertEquals(6, $result['count']);
245 }
246 /*
247 * Test that creating a payment will add the contribution ID where only one pledge payment
248 * in schedule
249 */
250 function testCreatePledgePaymentWhereOnlyOnePayment() {
251
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,
264 'installments' => 1,
265 'sequential' => 1,
266 'version' => $this->_apiversion,
267 );
268
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__);
272
273 //test the pledge_payment_create function
274 $params = array(
275 'contact_id' => $this->_individualId,
276 'pledge_id' => $pledge['id'],
277 'contribution_id' => $contributionID,
278 'version' => $this->_apiversion,
279 'status_id' => 1,
280 'actual_amount' => 20,
281 );
282 $result = civicrm_api('pledge_payment', 'create', $params);
283
284 $this->assertEquals(0, $result['is_error'], " in line " . __LINE__);
285
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,
289 ));
290 $this->assertEquals(1, $afterAdd['count'], " in line " . __LINE__);
291 }
292
293 function testUpdatePledgePayment() {
294 $params = array(
295 'pledge_id' => $this->_pledgeID,
296 'contribution_id' => $this->_contributionID,
297 'version' => $this->_apiversion,
298 'status_id' => 2,
299 'actual_amount' => 20,
300 );
301 $result = civicrm_api('pledge_payment', 'create', $params);
302 $updateparams = array(
303 'id' => $result['id'],
304 'status_id' => 1,
305 'version' => $this->_apiversion,
306 );
307
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);
312 }
313
314 function testDeletePledgePayment() {
315 $params = array(
316 'contact_id' => $this->_individualId,
317 'pledge_id' => $this->_pledgeID,
318 'contribution_id' => $this->_contributionID,
319 'version' => $this->_apiversion,
320 'status_id' => 1,
321 'sequential' => 1,
322 'actual_amount' => 20,
323 );
324 $pledgePayment = civicrm_api('pledge_payment', 'create', $params);
325
326 $deleteParams = array(
327 'id' => $pledgePayment['id'],
328 'version' => $this->_apiversion,
329 );
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__);
333 }
334
335 function testGetFields() {
336 $result = civicrm_api('PledgePayment', 'GetFields', array());
337 $this->assertType('array', $result);
338 }
339}
340