Merge pull request #1328 from eileenmcnaughton/master
[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;
256f4425 43 protected $_apiversion = 3;
6a488035 44 protected $_contributionID;
256f4425 45 protected $_financialTypeId = 1;
6a488035 46 protected $_entity = 'PledgePayment';
26212228 47 public $_eNoticeCompliant = FALSE;
430ae6dd
TO
48 public $DBResetRequired = TRUE;
49
50 function setUp() {
6a488035 51 parent::setUp();
6a488035
TO
52 $this->_individualId = $this->individualCreate(NULL);
53 $this->_pledgeID = $this->pledgeCreate($this->_individualId);
256f4425 54 $this->_contributionID = $this->contributionCreate($this->_individualId);
6a488035
TO
55 }
56
57 function tearDown() {
58 $tablesToTruncate = array(
59 'civicrm_contribution',
60 'civicrm_contact',
61 'civicrm_pledge',
62 'civicrm_pledge_payment',
63 'civicrm_line_item',
64 );
65
66 $this->quickCleanup($tablesToTruncate);
6a488035
TO
67 }
68
69 function testGetPledgePayment() {
256f4425 70 $params = array();
71 $result = $this->callAPIAndDocument('pledge_payment', 'get', $params, __FUNCTION__, __FILE__);
6a488035
TO
72 $this->assertEquals(5, $result['count'], " in line " . __LINE__);
73 }
74
75 /*
76 * Test that passing in a single variable works
77 */
78 function testGetSinglePledgePayment() {
79
80
81 $createparams = array(
82 'contact_id' => $this->_individualId,
83 'pledge_id' => $this->_pledgeID,
84 'contribution_id' => $this->_contributionID,
6a488035
TO
85 'status_id' => 1,
86 );
256f4425 87 $createResult = $this->callAPISuccess('pledge_payment', 'create', $createparams);
6a488035 88 $params = array(
6a488035
TO
89 'contribution_id' => $this->_contributionID,
90 );
256f4425 91 $result = $this->callAPISuccess('pledge_payment', 'get', $params);
6a488035
TO
92 $this->assertEquals(1, $result['count'], " in line " . __LINE__);
93 }
94
95 /*
96 * Test that passing in a single variable works:: status_id
97 */
98 function testGetSinglePledgePaymentByStatusID() {
99
100
101 $createparams = array(
102 'contact_id' => $this->_individualId,
103 'pledge_id' => $this->_pledgeID,
104 'contribution_id' => $this->_contributionID,
6a488035
TO
105 'status_id' => 1,
106 );
256f4425 107 $createResult = $this->callAPISuccess('pledge_payment', 'create', $createparams);
6a488035 108 $params = array(
6a488035
TO
109 'status_id' => 1,
110 );
111
256f4425 112 $result = $this->callAPISuccess('pledge_payment', 'get', $params);
6a488035
TO
113 $this->assertEquals(1, $result['count'], " in line " . __LINE__);
114 }
115
116 /*
117 * Test that creating a payment will add the contribution ID
118 */
119 function testCreatePledgePayment() {
120 //check that 5 pledge payments exist at the start
256f4425 121 $beforeAdd = $this->callAPISuccess('pledge_payment', 'get', array());
6a488035
TO
122 $this->assertEquals(5, $beforeAdd['count'], " in line " . __LINE__);
123
124 //test the pledge_payment_create function
125 $params = array(
126 'contact_id' => $this->_individualId,
127 'pledge_id' => $this->_pledgeID,
128 'contribution_id' => $this->_contributionID,
6a488035
TO
129 'status_id' => 1,
130 'actual_amount' => 20,
131 );
256f4425 132 $result = $this->callAPIAndDocument('pledge_payment', 'create', $params, __FUNCTION__, __FILE__);
6a488035
TO
133
134 //check existing updated not new one created - 'create' means add contribution_id in this context
256f4425 135 $afterAdd = $this->callAPISuccess('pledge_payment', 'get', array());
6a488035
TO
136 $this->assertEquals(5, $afterAdd['count'], " in line " . __LINE__);
137
138 //get the created payment & check it out
139 $getParams['id'] = $result['id'];
256f4425 140 $getIndPayment = $this->callAPISuccess('pledge_payment', 'get', $getParams);
6a488035
TO
141 $this->assertEquals(1, $getIndPayment['count'], " in line " . __LINE__);
142 $this->assertEquals(20, $getIndPayment['values'][$result['id']]['actual_amount'], " in line " . __LINE__);
143
144 //create a second pledge payment - need a contribution first &can't use the CiviUnitTest case function as invoice is hard-coded
145 $contributionParams = array(
6a488035
TO
146 'total_amount' => 20,
147 'contact_id' => $this->_individualId,
256f4425 148 'financial_type_id' => $this->_financialTypeId,
6a488035 149 );
256f4425 150 $contribution = $this->callAPISuccess('contribution', 'create', $contributionParams);
6a488035
TO
151 $params['contribution_id'] = $contribution['id'];
152
153
256f4425 154 $resultCont2 = $this->callAPISuccess('pledge_payment', 'create', $params);
6a488035
TO
155 //make sure original is untouched & has not been updated
156 $this->assertGreaterThan($result['id'], $resultCont2['id'], " in line " . __LINE__);
256f4425 157 $getIndPaymentAgain = $this->callAPISuccess('pledge_payment', 'get', $getParams);
6a488035
TO
158 $this->assertEquals(1, $getIndPaymentAgain['count'], " in line " . __LINE__);
159 $this->assertEquals($this->_contributionID, $getIndPaymentAgain['values'][$result['id']]['contribution_id'], " in line " . __LINE__);
160 }
161
162 /*
163 * test checks behaviour when more payments are created than should be possible
164 */
165 function testCreatePledgePaymentAllCreated() {
166 $params = array(
6a488035
TO
167 'pledge_id' => $this->_pledgeID,
168 'status_id' => 1,
169 );
170 // create one more pledge than there are spaces for
171 $i = 0;
172 while ($i <= 5) {
173 $contributionParams = array(
6a488035
TO
174 'total_amount' => 20,
175 'contact_id' => $this->_individualId,
256f4425 176 'financial_type_id' => $this->_financialTypeId,
6a488035 177 );
256f4425 178 $contribution = $this->callAPISuccess('contribution', 'create', $contributionParams);
6a488035
TO
179
180 $params['contribution_id'] = $contribution['id'];
256f4425 181
182 $resultCont2 = civicrm_api('pledge_payment', 'create', $params + array('version' => $this->_apiversion));
6a488035
TO
183 $i++;
184 }
185 // check that only 5 exist & we got an error setting the 6th
256f4425 186 $result = $this->callAPISuccess('PledgePayment', 'Get', array(
6a488035
TO
187 'pledge_id' => $this->_pledgeID,
188 ));
256f4425 189 // the last one above should result in an error
6a488035 190 $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']);
256f4425 191 $this->assertEquals(5, $result['count']);
6a488035
TO
192
193 $params['option.create_new'] = 1;
194 $params['scheduled_amount'] = 20;
195 $params['scheduled_date'] = '20131212';
256f4425 196 $resultcreatenew = $this->callAPISuccess('pledge_payment', 'create', $params);
197 $result = $this->callAPISuccess('PledgePayment', 'Get', array(
6a488035
TO
198 'pledge_id' => $this->_pledgeID,
199 ));
200
201 $this->assertEquals(6, $result['count']);
202 }
203 /*
204 * Test that creating a payment will add the contribution ID where only one pledge payment
205 * in schedule
206 */
207 function testCreatePledgePaymentWhereOnlyOnePayment() {
208
209 $pledgeParams = array(
210 'contact_id' => $this->_individualId,
211 'pledge_create_date' => date('Ymd'),
212 'start_date' => date('Ymd'),
213 'scheduled_date' => $this->scheduled_date,
214 'pledge_amount' => 100.00,
215 'pledge_status_id' => '2',
216 'pledge_financial_type_id' => '1',
217 'pledge_original_installment_amount' => 20,
218 'frequency_interval' => 5,
219 'frequency_unit' => 'year',
220 'frequency_day' => 15,
221 'installments' => 1,
222 'sequential' => 1,
6a488035
TO
223 );
224
256f4425 225 $contributionID = $this->contributionCreate($this->_individualId, $this->_financialTypeId, 45, 45);
226 $pledge = $this->callAPISuccess('Pledge', 'Create', $pledgeParams);
6a488035
TO
227
228 //test the pledge_payment_create function
229 $params = array(
230 'contact_id' => $this->_individualId,
231 'pledge_id' => $pledge['id'],
232 'contribution_id' => $contributionID,
6a488035
TO
233 'status_id' => 1,
234 'actual_amount' => 20,
235 );
256f4425 236 $result = $this->callAPISuccess('pledge_payment', 'create', $params);
6a488035
TO
237
238 //check existing updated not new one created - 'create' means add contribution_id in this context
256f4425 239 $afterAdd = $this->callAPISuccess('pledge_payment', 'get', array(
240 'contribution_id' => $contributionID,
6a488035
TO
241 ));
242 $this->assertEquals(1, $afterAdd['count'], " in line " . __LINE__);
243 }
244
245 function testUpdatePledgePayment() {
246 $params = array(
247 'pledge_id' => $this->_pledgeID,
248 'contribution_id' => $this->_contributionID,
6a488035
TO
249 'status_id' => 2,
250 'actual_amount' => 20,
251 );
256f4425 252 $result = $this->callAPISuccess('pledge_payment', 'create', $params);
6a488035
TO
253 $updateparams = array(
254 'id' => $result['id'],
255 'status_id' => 1,
6a488035
TO
256 );
257
256f4425 258 $result = $this->callAPIAndDocument('pledge_payment', 'update', $updateparams, __FUNCTION__, __FILE__);
6a488035
TO
259 $this->getAndCheck(array_merge($params,$updateparams), $result['id'], $this->_entity);
260 }
261
262 function testDeletePledgePayment() {
263 $params = array(
264 'contact_id' => $this->_individualId,
265 'pledge_id' => $this->_pledgeID,
266 'contribution_id' => $this->_contributionID,
6a488035
TO
267 'status_id' => 1,
268 'sequential' => 1,
269 'actual_amount' => 20,
270 );
256f4425 271 $pledgePayment = $this->callAPISuccess('pledge_payment', 'create', $params);
6a488035
TO
272
273 $deleteParams = array(
274 'id' => $pledgePayment['id'],
6a488035 275 );
256f4425 276 $result = $this->callAPIAndDocument('pledge_payment', 'delete', $deleteParams, __FUNCTION__, __FILE__);
6a488035
TO
277 }
278
279 function testGetFields() {
256f4425 280 $result = $this->callAPISuccess('PledgePayment', 'GetFields', array());
6a488035
TO
281 $this->assertType('array', $result);
282 }
283}
284