Merge pull request #10030 from eileenmcnaughton/test
[civicrm-core.git] / tests / phpunit / api / v3 / PledgePaymentTest.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
81621fee 4 | CiviCRM version 4.7 |
6a488035 5 +--------------------------------------------------------------------+
15a4309a 6 | Copyright CiviCRM LLC (c) 2004-2017 |
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 +--------------------------------------------------------------------+
d25dd0ee 26 */
6a488035
TO
27
28/**
29 * Test class for Pledge API - civicrm_pledge_*
30 *
6c6e6187 31 * @package CiviCRM_APIv3
acb109b7 32 * @group headless
6a488035 33 */
6a488035
TO
34class api_v3_PledgePaymentTest extends CiviUnitTestCase {
35
36 /**
fe482240 37 * Assume empty database with just civicrm_data.
6a488035
TO
38 */
39 protected $_individualId;
40 protected $_pledgeID;
256f4425 41 protected $_apiversion = 3;
6a488035 42 protected $_contributionID;
256f4425 43 protected $_financialTypeId = 1;
6a488035 44 protected $_entity = 'PledgePayment';
430ae6dd
TO
45 public $DBResetRequired = TRUE;
46
00be9182 47 public function setUp() {
6a488035 48 parent::setUp();
e4d5f1e2 49 $this->_individualId = $this->individualCreate();
5210fa35 50 $this->_pledgeID = $this->pledgeCreate(array('contact_id' => $this->_individualId));
c3a3074f 51 $this->_contributionID = $this->contributionCreate(array('contact_id' => $this->_individualId));
6a488035
TO
52 }
53
00be9182 54 public function tearDown() {
6a488035
TO
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);
6a488035
TO
64 }
65
00be9182 66 public function testGetPledgePayment() {
256f4425 67 $params = array();
68 $result = $this->callAPIAndDocument('pledge_payment', 'get', $params, __FUNCTION__, __FILE__);
6a488035
TO
69 $this->assertEquals(5, $result['count'], " in line " . __LINE__);
70 }
71
c490a46a 72 /**
eceb18cc 73 * Test that passing in a single variable works.
c490a46a 74 */
00be9182 75 public function testGetSinglePledgePayment() {
6a488035
TO
76 $createparams = array(
77 'contact_id' => $this->_individualId,
78 'pledge_id' => $this->_pledgeID,
79 'contribution_id' => $this->_contributionID,
6a488035
TO
80 'status_id' => 1,
81 );
256f4425 82 $createResult = $this->callAPISuccess('pledge_payment', 'create', $createparams);
6a488035 83 $params = array(
6a488035
TO
84 'contribution_id' => $this->_contributionID,
85 );
256f4425 86 $result = $this->callAPISuccess('pledge_payment', 'get', $params);
6a488035
TO
87 $this->assertEquals(1, $result['count'], " in line " . __LINE__);
88 }
89
c490a46a
CW
90 /**
91 * Test that passing in a single variable works:: status_id
92 */
00be9182 93 public function testGetSinglePledgePaymentByStatusID() {
6a488035
TO
94 $createparams = array(
95 'contact_id' => $this->_individualId,
96 'pledge_id' => $this->_pledgeID,
97 'contribution_id' => $this->_contributionID,
6a488035
TO
98 'status_id' => 1,
99 );
256f4425 100 $createResult = $this->callAPISuccess('pledge_payment', 'create', $createparams);
6a488035 101 $params = array(
6a488035
TO
102 'status_id' => 1,
103 );
104
256f4425 105 $result = $this->callAPISuccess('pledge_payment', 'get', $params);
6a488035
TO
106 $this->assertEquals(1, $result['count'], " in line " . __LINE__);
107 }
108
c490a46a 109 /**
eceb18cc 110 * Test that creating a payment will add the contribution ID.
c490a46a 111 */
00be9182 112 public function testCreatePledgePayment() {
6a488035 113 //check that 5 pledge payments exist at the start
256f4425 114 $beforeAdd = $this->callAPISuccess('pledge_payment', 'get', array());
6a488035
TO
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,
6a488035
TO
122 'status_id' => 1,
123 'actual_amount' => 20,
124 );
256f4425 125 $result = $this->callAPIAndDocument('pledge_payment', 'create', $params, __FUNCTION__, __FILE__);
6a488035
TO
126
127 //check existing updated not new one created - 'create' means add contribution_id in this context
256f4425 128 $afterAdd = $this->callAPISuccess('pledge_payment', 'get', array());
6a488035
TO
129 $this->assertEquals(5, $afterAdd['count'], " in line " . __LINE__);
130
131 //get the created payment & check it out
132 $getParams['id'] = $result['id'];
256f4425 133 $getIndPayment = $this->callAPISuccess('pledge_payment', 'get', $getParams);
6a488035
TO
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(
6a488035
TO
139 'total_amount' => 20,
140 'contact_id' => $this->_individualId,
256f4425 141 'financial_type_id' => $this->_financialTypeId,
6a488035 142 );
256f4425 143 $contribution = $this->callAPISuccess('contribution', 'create', $contributionParams);
6a488035
TO
144 $params['contribution_id'] = $contribution['id'];
145
256f4425 146 $resultCont2 = $this->callAPISuccess('pledge_payment', 'create', $params);
6a488035
TO
147 //make sure original is untouched & has not been updated
148 $this->assertGreaterThan($result['id'], $resultCont2['id'], " in line " . __LINE__);
256f4425 149 $getIndPaymentAgain = $this->callAPISuccess('pledge_payment', 'get', $getParams);
6a488035
TO
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
c490a46a 154 /**
eceb18cc 155 * Test checks behaviour when more payments are created than should be possible.
c490a46a 156 */
00be9182 157 public function testCreatePledgePaymentAllCreated() {
6a488035 158 $params = array(
6a488035
TO
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(
6a488035
TO
166 'total_amount' => 20,
167 'contact_id' => $this->_individualId,
256f4425 168 'financial_type_id' => $this->_financialTypeId,
6a488035 169 );
256f4425 170 $contribution = $this->callAPISuccess('contribution', 'create', $contributionParams);
6a488035
TO
171
172 $params['contribution_id'] = $contribution['id'];
256f4425 173
174 $resultCont2 = civicrm_api('pledge_payment', 'create', $params + array('version' => $this->_apiversion));
6a488035
TO
175 $i++;
176 }
177 // check that only 5 exist & we got an error setting the 6th
256f4425 178 $result = $this->callAPISuccess('PledgePayment', 'Get', array(
6a488035
TO
179 'pledge_id' => $this->_pledgeID,
180 ));
256f4425 181 // the last one above should result in an error
6a488035 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']);
256f4425 183 $this->assertEquals(5, $result['count']);
6a488035
TO
184
185 $params['option.create_new'] = 1;
186 $params['scheduled_amount'] = 20;
187 $params['scheduled_date'] = '20131212';
256f4425 188 $resultcreatenew = $this->callAPISuccess('pledge_payment', 'create', $params);
189 $result = $this->callAPISuccess('PledgePayment', 'Get', array(
92915c55
TO
190 'pledge_id' => $this->_pledgeID,
191 ));
6a488035
TO
192
193 $this->assertEquals(6, $result['count']);
194 }
6a488035 195
c490a46a 196 /**
fe482240 197 * Test that creating a payment adds the contribution ID where only one pledge payment is in schedule.
c490a46a 198 */
00be9182 199 public function testCreatePledgePaymentWhereOnlyOnePayment() {
6a488035
TO
200 $pledgeParams = array(
201 'contact_id' => $this->_individualId,
202 'pledge_create_date' => date('Ymd'),
203 'start_date' => date('Ymd'),
cd6ca96a 204 'scheduled_date' => 'first day 2015',
6a488035
TO
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,
6a488035
TO
214 );
215
78ab0ca4 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 ));
256f4425 222 $pledge = $this->callAPISuccess('Pledge', 'Create', $pledgeParams);
6a488035
TO
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,
6a488035
TO
229 'status_id' => 1,
230 'actual_amount' => 20,
231 );
256f4425 232 $result = $this->callAPISuccess('pledge_payment', 'create', $params);
6a488035
TO
233
234 //check existing updated not new one created - 'create' means add contribution_id in this context
256f4425 235 $afterAdd = $this->callAPISuccess('pledge_payment', 'get', array(
236 'contribution_id' => $contributionID,
92915c55 237 ));
6a488035
TO
238 $this->assertEquals(1, $afterAdd['count'], " in line " . __LINE__);
239 }
240
00be9182 241 public function testUpdatePledgePayment() {
6a488035
TO
242 $params = array(
243 'pledge_id' => $this->_pledgeID,
244 'contribution_id' => $this->_contributionID,
6a488035
TO
245 'status_id' => 2,
246 'actual_amount' => 20,
247 );
256f4425 248 $result = $this->callAPISuccess('pledge_payment', 'create', $params);
6a488035
TO
249 $updateparams = array(
250 'id' => $result['id'],
251 'status_id' => 1,
6a488035
TO
252 );
253
256f4425 254 $result = $this->callAPIAndDocument('pledge_payment', 'update', $updateparams, __FUNCTION__, __FILE__);
6c6e6187 255 $this->getAndCheck(array_merge($params, $updateparams), $result['id'], $this->_entity);
6a488035
TO
256 }
257
00be9182 258 public function testDeletePledgePayment() {
6a488035
TO
259 $params = array(
260 'contact_id' => $this->_individualId,
261 'pledge_id' => $this->_pledgeID,
262 'contribution_id' => $this->_contributionID,
6a488035
TO
263 'status_id' => 1,
264 'sequential' => 1,
265 'actual_amount' => 20,
266 );
256f4425 267 $pledgePayment = $this->callAPISuccess('pledge_payment', 'create', $params);
6a488035
TO
268
269 $deleteParams = array(
270 'id' => $pledgePayment['id'],
6a488035 271 );
256f4425 272 $result = $this->callAPIAndDocument('pledge_payment', 'delete', $deleteParams, __FUNCTION__, __FILE__);
6a488035
TO
273 }
274
00be9182 275 public function testGetFields() {
256f4425 276 $result = $this->callAPISuccess('PledgePayment', 'GetFields', array());
6a488035
TO
277 $this->assertType('array', $result);
278 }
96025800 279
6a488035 280}