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