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