3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
29 * Test class for Pledge API - civicrm_pledge_*
31 * @package CiviCRM_APIv3
33 class api_v3_PledgePaymentTest
extends CiviUnitTestCase
{
36 * Assume empty database with just civicrm_data.
38 protected $_individualId;
40 protected $_apiversion = 3;
41 protected $_contributionID;
42 protected $_financialTypeId = 1;
43 protected $_entity = 'PledgePayment';
44 public $DBResetRequired = TRUE;
46 public function setUp() {
48 $this->_individualId
= $this->individualCreate();
49 $this->_pledgeID
= $this->pledgeCreate($this->_individualId
);
50 $this->_contributionID
= $this->contributionCreate(array('contact_id' => $this->_individualId
));
53 public function tearDown() {
54 $tablesToTruncate = array(
55 'civicrm_contribution',
58 'civicrm_pledge_payment',
62 $this->quickCleanup($tablesToTruncate);
65 public function testGetPledgePayment() {
67 $result = $this->callAPIAndDocument('pledge_payment', 'get', $params, __FUNCTION__
, __FILE__
);
68 $this->assertEquals(5, $result['count'], " in line " . __LINE__
);
72 * Test that passing in a single variable works.
74 public function testGetSinglePledgePayment() {
75 $createparams = array(
76 'contact_id' => $this->_individualId
,
77 'pledge_id' => $this->_pledgeID
,
78 'contribution_id' => $this->_contributionID
,
81 $createResult = $this->callAPISuccess('pledge_payment', 'create', $createparams);
83 'contribution_id' => $this->_contributionID
,
85 $result = $this->callAPISuccess('pledge_payment', 'get', $params);
86 $this->assertEquals(1, $result['count'], " in line " . __LINE__
);
90 * Test that passing in a single variable works:: status_id
92 public function testGetSinglePledgePaymentByStatusID() {
93 $createparams = array(
94 'contact_id' => $this->_individualId
,
95 'pledge_id' => $this->_pledgeID
,
96 'contribution_id' => $this->_contributionID
,
99 $createResult = $this->callAPISuccess('pledge_payment', 'create', $createparams);
104 $result = $this->callAPISuccess('pledge_payment', 'get', $params);
105 $this->assertEquals(1, $result['count'], " in line " . __LINE__
);
109 * Test that creating a payment will add the contribution ID.
111 public function testCreatePledgePayment() {
112 //check that 5 pledge payments exist at the start
113 $beforeAdd = $this->callAPISuccess('pledge_payment', 'get', array());
114 $this->assertEquals(5, $beforeAdd['count'], " in line " . __LINE__
);
116 //test the pledge_payment_create function
118 'contact_id' => $this->_individualId
,
119 'pledge_id' => $this->_pledgeID
,
120 'contribution_id' => $this->_contributionID
,
122 'actual_amount' => 20,
124 $result = $this->callAPIAndDocument('pledge_payment', 'create', $params, __FUNCTION__
, __FILE__
);
126 //check existing updated not new one created - 'create' means add contribution_id in this context
127 $afterAdd = $this->callAPISuccess('pledge_payment', 'get', array());
128 $this->assertEquals(5, $afterAdd['count'], " in line " . __LINE__
);
130 //get the created payment & check it out
131 $getParams['id'] = $result['id'];
132 $getIndPayment = $this->callAPISuccess('pledge_payment', 'get', $getParams);
133 $this->assertEquals(1, $getIndPayment['count'], " in line " . __LINE__
);
134 $this->assertEquals(20, $getIndPayment['values'][$result['id']]['actual_amount'], " in line " . __LINE__
);
136 //create a second pledge payment - need a contribution first &can't use the CiviUnitTest case function as invoice is hard-coded
137 $contributionParams = array(
138 'total_amount' => 20,
139 'contact_id' => $this->_individualId
,
140 'financial_type_id' => $this->_financialTypeId
,
142 $contribution = $this->callAPISuccess('contribution', 'create', $contributionParams);
143 $params['contribution_id'] = $contribution['id'];
145 $resultCont2 = $this->callAPISuccess('pledge_payment', 'create', $params);
146 //make sure original is untouched & has not been updated
147 $this->assertGreaterThan($result['id'], $resultCont2['id'], " in line " . __LINE__
);
148 $getIndPaymentAgain = $this->callAPISuccess('pledge_payment', 'get', $getParams);
149 $this->assertEquals(1, $getIndPaymentAgain['count'], " in line " . __LINE__
);
150 $this->assertEquals($this->_contributionID
, $getIndPaymentAgain['values'][$result['id']]['contribution_id'], " in line " . __LINE__
);
154 * Test checks behaviour when more payments are created than should be possible.
156 public function testCreatePledgePaymentAllCreated() {
158 'pledge_id' => $this->_pledgeID
,
161 // create one more pledge than there are spaces for
164 $contributionParams = array(
165 'total_amount' => 20,
166 'contact_id' => $this->_individualId
,
167 'financial_type_id' => $this->_financialTypeId
,
169 $contribution = $this->callAPISuccess('contribution', 'create', $contributionParams);
171 $params['contribution_id'] = $contribution['id'];
173 $resultCont2 = civicrm_api('pledge_payment', 'create', $params +
array('version' => $this->_apiversion
));
176 // check that only 5 exist & we got an error setting the 6th
177 $result = $this->callAPISuccess('PledgePayment', 'Get', array(
178 'pledge_id' => $this->_pledgeID
,
180 // the last one above should result in an error
181 $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']);
182 $this->assertEquals(5, $result['count']);
184 $params['option.create_new'] = 1;
185 $params['scheduled_amount'] = 20;
186 $params['scheduled_date'] = '20131212';
187 $resultcreatenew = $this->callAPISuccess('pledge_payment', 'create', $params);
188 $result = $this->callAPISuccess('PledgePayment', 'Get', array(
189 'pledge_id' => $this->_pledgeID
,
192 $this->assertEquals(6, $result['count']);
196 * Test that creating a payment adds the contribution ID where only one pledge payment is in schedule.
198 public function testCreatePledgePaymentWhereOnlyOnePayment() {
199 $pledgeParams = array(
200 'contact_id' => $this->_individualId
,
201 'pledge_create_date' => date('Ymd'),
202 'start_date' => date('Ymd'),
203 'scheduled_date' => 'first day 2015',
204 'pledge_amount' => 100.00,
205 'pledge_status_id' => '2',
206 'pledge_financial_type_id' => '1',
207 'pledge_original_installment_amount' => 20,
208 'frequency_interval' => 5,
209 'frequency_unit' => 'year',
210 'frequency_day' => 15,
215 $contributionID = $this->contributionCreate(array(
216 'contact_id' => $this->_individualId
,
217 'financial_type_id' => $this->_financialTypeId
,
221 $pledge = $this->callAPISuccess('Pledge', 'Create', $pledgeParams);
223 //test the pledge_payment_create function
225 'contact_id' => $this->_individualId
,
226 'pledge_id' => $pledge['id'],
227 'contribution_id' => $contributionID,
229 'actual_amount' => 20,
231 $result = $this->callAPISuccess('pledge_payment', 'create', $params);
233 //check existing updated not new one created - 'create' means add contribution_id in this context
234 $afterAdd = $this->callAPISuccess('pledge_payment', 'get', array(
235 'contribution_id' => $contributionID,
237 $this->assertEquals(1, $afterAdd['count'], " in line " . __LINE__
);
240 public function testUpdatePledgePayment() {
242 'pledge_id' => $this->_pledgeID
,
243 'contribution_id' => $this->_contributionID
,
245 'actual_amount' => 20,
247 $result = $this->callAPISuccess('pledge_payment', 'create', $params);
248 $updateparams = array(
249 'id' => $result['id'],
253 $result = $this->callAPIAndDocument('pledge_payment', 'update', $updateparams, __FUNCTION__
, __FILE__
);
254 $this->getAndCheck(array_merge($params, $updateparams), $result['id'], $this->_entity
);
257 public function testDeletePledgePayment() {
259 'contact_id' => $this->_individualId
,
260 'pledge_id' => $this->_pledgeID
,
261 'contribution_id' => $this->_contributionID
,
264 'actual_amount' => 20,
266 $pledgePayment = $this->callAPISuccess('pledge_payment', 'create', $params);
268 $deleteParams = array(
269 'id' => $pledgePayment['id'],
271 $result = $this->callAPIAndDocument('pledge_payment', 'delete', $deleteParams, __FUNCTION__
, __FILE__
);
274 public function testGetFields() {
275 $result = $this->callAPISuccess('PledgePayment', 'GetFields', array());
276 $this->assertType('array', $result);