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