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