3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.3 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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 +--------------------------------------------------------------------+
28 require_once 'CiviTest/CiviUnitTestCase.php';
31 * Test class for Pledge API - civicrm_pledge_*
33 * @package CiviCRM_APIv3
36 class api_v3_PledgePaymentTest
extends CiviUnitTestCase
{
39 * Assume empty database with just civicrm_data
41 protected $_individualId;
43 protected $_apiversion = 3;
44 protected $_contributionID;
45 protected $_financialTypeId = 1;
46 protected $_entity = 'PledgePayment';
47 public $_eNoticeCompliant = FALSE;
48 public $DBResetRequired = TRUE;
52 $this->_individualId
= $this->individualCreate(NULL);
53 $this->_pledgeID
= $this->pledgeCreate($this->_individualId
);
54 $this->_contributionID
= $this->contributionCreate($this->_individualId
);
58 $tablesToTruncate = array(
59 'civicrm_contribution',
62 'civicrm_pledge_payment',
66 $this->quickCleanup($tablesToTruncate);
69 function testGetPledgePayment() {
71 $result = $this->callAPIAndDocument('pledge_payment', 'get', $params, __FUNCTION__
, __FILE__
);
72 $this->assertEquals(5, $result['count'], " in line " . __LINE__
);
76 * Test that passing in a single variable works
78 function testGetSinglePledgePayment() {
81 $createparams = array(
82 'contact_id' => $this->_individualId
,
83 'pledge_id' => $this->_pledgeID
,
84 'contribution_id' => $this->_contributionID
,
87 $createResult = $this->callAPISuccess('pledge_payment', 'create', $createparams);
89 'contribution_id' => $this->_contributionID
,
91 $result = $this->callAPISuccess('pledge_payment', 'get', $params);
92 $this->assertEquals(1, $result['count'], " in line " . __LINE__
);
96 * Test that passing in a single variable works:: status_id
98 function testGetSinglePledgePaymentByStatusID() {
101 $createparams = array(
102 'contact_id' => $this->_individualId
,
103 'pledge_id' => $this->_pledgeID
,
104 'contribution_id' => $this->_contributionID
,
107 $createResult = $this->callAPISuccess('pledge_payment', 'create', $createparams);
112 $result = $this->callAPISuccess('pledge_payment', 'get', $params);
113 $this->assertEquals(1, $result['count'], " in line " . __LINE__
);
117 * Test that creating a payment will add the contribution ID
119 function testCreatePledgePayment() {
120 //check that 5 pledge payments exist at the start
121 $beforeAdd = $this->callAPISuccess('pledge_payment', 'get', array());
122 $this->assertEquals(5, $beforeAdd['count'], " in line " . __LINE__
);
124 //test the pledge_payment_create function
126 'contact_id' => $this->_individualId
,
127 'pledge_id' => $this->_pledgeID
,
128 'contribution_id' => $this->_contributionID
,
130 'actual_amount' => 20,
132 $result = $this->callAPIAndDocument('pledge_payment', 'create', $params, __FUNCTION__
, __FILE__
);
134 //check existing updated not new one created - 'create' means add contribution_id in this context
135 $afterAdd = $this->callAPISuccess('pledge_payment', 'get', array());
136 $this->assertEquals(5, $afterAdd['count'], " in line " . __LINE__
);
138 //get the created payment & check it out
139 $getParams['id'] = $result['id'];
140 $getIndPayment = $this->callAPISuccess('pledge_payment', 'get', $getParams);
141 $this->assertEquals(1, $getIndPayment['count'], " in line " . __LINE__
);
142 $this->assertEquals(20, $getIndPayment['values'][$result['id']]['actual_amount'], " in line " . __LINE__
);
144 //create a second pledge payment - need a contribution first &can't use the CiviUnitTest case function as invoice is hard-coded
145 $contributionParams = array(
146 'total_amount' => 20,
147 'contact_id' => $this->_individualId
,
148 'financial_type_id' => $this->_financialTypeId
,
150 $contribution = $this->callAPISuccess('contribution', 'create', $contributionParams);
151 $params['contribution_id'] = $contribution['id'];
154 $resultCont2 = $this->callAPISuccess('pledge_payment', 'create', $params);
155 //make sure original is untouched & has not been updated
156 $this->assertGreaterThan($result['id'], $resultCont2['id'], " in line " . __LINE__
);
157 $getIndPaymentAgain = $this->callAPISuccess('pledge_payment', 'get', $getParams);
158 $this->assertEquals(1, $getIndPaymentAgain['count'], " in line " . __LINE__
);
159 $this->assertEquals($this->_contributionID
, $getIndPaymentAgain['values'][$result['id']]['contribution_id'], " in line " . __LINE__
);
163 * test checks behaviour when more payments are created than should be possible
165 function testCreatePledgePaymentAllCreated() {
167 'pledge_id' => $this->_pledgeID
,
170 // create one more pledge than there are spaces for
173 $contributionParams = array(
174 'total_amount' => 20,
175 'contact_id' => $this->_individualId
,
176 'financial_type_id' => $this->_financialTypeId
,
178 $contribution = $this->callAPISuccess('contribution', 'create', $contributionParams);
180 $params['contribution_id'] = $contribution['id'];
182 $resultCont2 = civicrm_api('pledge_payment', 'create', $params +
array('version' => $this->_apiversion
));
185 // check that only 5 exist & we got an error setting the 6th
186 $result = $this->callAPISuccess('PledgePayment', 'Get', array(
187 'pledge_id' => $this->_pledgeID
,
189 // the last one above should result in an error
190 $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']);
191 $this->assertEquals(5, $result['count']);
193 $params['option.create_new'] = 1;
194 $params['scheduled_amount'] = 20;
195 $params['scheduled_date'] = '20131212';
196 $resultcreatenew = $this->callAPISuccess('pledge_payment', 'create', $params);
197 $result = $this->callAPISuccess('PledgePayment', 'Get', array(
198 'pledge_id' => $this->_pledgeID
,
201 $this->assertEquals(6, $result['count']);
204 * Test that creating a payment will add the contribution ID where only one pledge payment
207 function testCreatePledgePaymentWhereOnlyOnePayment() {
209 $pledgeParams = array(
210 'contact_id' => $this->_individualId
,
211 'pledge_create_date' => date('Ymd'),
212 'start_date' => date('Ymd'),
213 'scheduled_date' => $this->scheduled_date
,
214 'pledge_amount' => 100.00,
215 'pledge_status_id' => '2',
216 'pledge_financial_type_id' => '1',
217 'pledge_original_installment_amount' => 20,
218 'frequency_interval' => 5,
219 'frequency_unit' => 'year',
220 'frequency_day' => 15,
225 $contributionID = $this->contributionCreate($this->_individualId
, $this->_financialTypeId
, 45, 45);
226 $pledge = $this->callAPISuccess('Pledge', 'Create', $pledgeParams);
228 //test the pledge_payment_create function
230 'contact_id' => $this->_individualId
,
231 'pledge_id' => $pledge['id'],
232 'contribution_id' => $contributionID,
234 'actual_amount' => 20,
236 $result = $this->callAPISuccess('pledge_payment', 'create', $params);
238 //check existing updated not new one created - 'create' means add contribution_id in this context
239 $afterAdd = $this->callAPISuccess('pledge_payment', 'get', array(
240 'contribution_id' => $contributionID,
242 $this->assertEquals(1, $afterAdd['count'], " in line " . __LINE__
);
245 function testUpdatePledgePayment() {
247 'pledge_id' => $this->_pledgeID
,
248 'contribution_id' => $this->_contributionID
,
250 'actual_amount' => 20,
252 $result = $this->callAPISuccess('pledge_payment', 'create', $params);
253 $updateparams = array(
254 'id' => $result['id'],
258 $result = $this->callAPIAndDocument('pledge_payment', 'update', $updateparams, __FUNCTION__
, __FILE__
);
259 $this->getAndCheck(array_merge($params,$updateparams), $result['id'], $this->_entity
);
262 function testDeletePledgePayment() {
264 'contact_id' => $this->_individualId
,
265 'pledge_id' => $this->_pledgeID
,
266 'contribution_id' => $this->_contributionID
,
269 'actual_amount' => 20,
271 $pledgePayment = $this->callAPISuccess('pledge_payment', 'create', $params);
273 $deleteParams = array(
274 'id' => $pledgePayment['id'],
276 $result = $this->callAPIAndDocument('pledge_payment', 'delete', $deleteParams, __FUNCTION__
, __FILE__
);
279 function testGetFields() {
280 $result = $this->callAPISuccess('PledgePayment', 'GetFields', array());
281 $this->assertType('array', $result);