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