3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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 $DBResetRequired = TRUE;
49 public function setUp() {
51 $this->_individualId
= $this->individualCreate();
52 $this->_pledgeID
= $this->pledgeCreate($this->_individualId
);
53 $this->_contributionID
= $this->contributionCreate($this->_individualId
);
56 public function tearDown() {
57 $tablesToTruncate = array(
58 'civicrm_contribution',
61 'civicrm_pledge_payment',
65 $this->quickCleanup($tablesToTruncate);
68 public function testGetPledgePayment() {
70 $result = $this->callAPIAndDocument('pledge_payment', 'get', $params, __FUNCTION__
, __FILE__
);
71 $this->assertEquals(5, $result['count'], " in line " . __LINE__
);
75 * Test that passing in a single variable works
77 public function testGetSinglePledgePayment() {
78 $createparams = array(
79 'contact_id' => $this->_individualId
,
80 'pledge_id' => $this->_pledgeID
,
81 'contribution_id' => $this->_contributionID
,
84 $createResult = $this->callAPISuccess('pledge_payment', 'create', $createparams);
86 'contribution_id' => $this->_contributionID
,
88 $result = $this->callAPISuccess('pledge_payment', 'get', $params);
89 $this->assertEquals(1, $result['count'], " in line " . __LINE__
);
93 * Test that passing in a single variable works:: status_id
95 public function testGetSinglePledgePaymentByStatusID() {
96 $createparams = array(
97 'contact_id' => $this->_individualId
,
98 'pledge_id' => $this->_pledgeID
,
99 'contribution_id' => $this->_contributionID
,
102 $createResult = $this->callAPISuccess('pledge_payment', 'create', $createparams);
107 $result = $this->callAPISuccess('pledge_payment', 'get', $params);
108 $this->assertEquals(1, $result['count'], " in line " . __LINE__
);
112 * Test that creating a payment will add the contribution ID
114 public function testCreatePledgePayment() {
115 //check that 5 pledge payments exist at the start
116 $beforeAdd = $this->callAPISuccess('pledge_payment', 'get', array());
117 $this->assertEquals(5, $beforeAdd['count'], " in line " . __LINE__
);
119 //test the pledge_payment_create function
121 'contact_id' => $this->_individualId
,
122 'pledge_id' => $this->_pledgeID
,
123 'contribution_id' => $this->_contributionID
,
125 'actual_amount' => 20,
127 $result = $this->callAPIAndDocument('pledge_payment', 'create', $params, __FUNCTION__
, __FILE__
);
129 //check existing updated not new one created - 'create' means add contribution_id in this context
130 $afterAdd = $this->callAPISuccess('pledge_payment', 'get', array());
131 $this->assertEquals(5, $afterAdd['count'], " in line " . __LINE__
);
133 //get the created payment & check it out
134 $getParams['id'] = $result['id'];
135 $getIndPayment = $this->callAPISuccess('pledge_payment', 'get', $getParams);
136 $this->assertEquals(1, $getIndPayment['count'], " in line " . __LINE__
);
137 $this->assertEquals(20, $getIndPayment['values'][$result['id']]['actual_amount'], " in line " . __LINE__
);
139 //create a second pledge payment - need a contribution first &can't use the CiviUnitTest case function as invoice is hard-coded
140 $contributionParams = array(
141 'total_amount' => 20,
142 'contact_id' => $this->_individualId
,
143 'financial_type_id' => $this->_financialTypeId
,
145 $contribution = $this->callAPISuccess('contribution', 'create', $contributionParams);
146 $params['contribution_id'] = $contribution['id'];
148 $resultCont2 = $this->callAPISuccess('pledge_payment', 'create', $params);
149 //make sure original is untouched & has not been updated
150 $this->assertGreaterThan($result['id'], $resultCont2['id'], " in line " . __LINE__
);
151 $getIndPaymentAgain = $this->callAPISuccess('pledge_payment', 'get', $getParams);
152 $this->assertEquals(1, $getIndPaymentAgain['count'], " in line " . __LINE__
);
153 $this->assertEquals($this->_contributionID
, $getIndPaymentAgain['values'][$result['id']]['contribution_id'], " in line " . __LINE__
);
157 * Test checks behaviour when more payments are created than should be possible
159 public function testCreatePledgePaymentAllCreated() {
161 'pledge_id' => $this->_pledgeID
,
164 // create one more pledge than there are spaces for
167 $contributionParams = array(
168 'total_amount' => 20,
169 'contact_id' => $this->_individualId
,
170 'financial_type_id' => $this->_financialTypeId
,
172 $contribution = $this->callAPISuccess('contribution', 'create', $contributionParams);
174 $params['contribution_id'] = $contribution['id'];
176 $resultCont2 = civicrm_api('pledge_payment', 'create', $params +
array('version' => $this->_apiversion
));
179 // check that only 5 exist & we got an error setting the 6th
180 $result = $this->callAPISuccess('PledgePayment', 'Get', array(
181 'pledge_id' => $this->_pledgeID
,
183 // the last one above should result in an error
184 $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']);
185 $this->assertEquals(5, $result['count']);
187 $params['option.create_new'] = 1;
188 $params['scheduled_amount'] = 20;
189 $params['scheduled_date'] = '20131212';
190 $resultcreatenew = $this->callAPISuccess('pledge_payment', 'create', $params);
191 $result = $this->callAPISuccess('PledgePayment', 'Get', array(
192 'pledge_id' => $this->_pledgeID
,
195 $this->assertEquals(6, $result['count']);
199 * Test that creating a payment will add the contribution ID where only one pledge payment
202 public function testCreatePledgePaymentWhereOnlyOnePayment() {
203 $pledgeParams = array(
204 'contact_id' => $this->_individualId
,
205 'pledge_create_date' => date('Ymd'),
206 'start_date' => date('Ymd'),
207 'scheduled_date' => 'first day 2015',
208 'pledge_amount' => 100.00,
209 'pledge_status_id' => '2',
210 'pledge_financial_type_id' => '1',
211 'pledge_original_installment_amount' => 20,
212 'frequency_interval' => 5,
213 'frequency_unit' => 'year',
214 'frequency_day' => 15,
219 $contributionID = $this->contributionCreate($this->_individualId
, $this->_financialTypeId
, 45, 45);
220 $pledge = $this->callAPISuccess('Pledge', 'Create', $pledgeParams);
222 //test the pledge_payment_create function
224 'contact_id' => $this->_individualId
,
225 'pledge_id' => $pledge['id'],
226 'contribution_id' => $contributionID,
228 'actual_amount' => 20,
230 $result = $this->callAPISuccess('pledge_payment', 'create', $params);
232 //check existing updated not new one created - 'create' means add contribution_id in this context
233 $afterAdd = $this->callAPISuccess('pledge_payment', 'get', array(
234 'contribution_id' => $contributionID,
236 $this->assertEquals(1, $afterAdd['count'], " in line " . __LINE__
);
239 public function testUpdatePledgePayment() {
241 'pledge_id' => $this->_pledgeID
,
242 'contribution_id' => $this->_contributionID
,
244 'actual_amount' => 20,
246 $result = $this->callAPISuccess('pledge_payment', 'create', $params);
247 $updateparams = array(
248 'id' => $result['id'],
252 $result = $this->callAPIAndDocument('pledge_payment', 'update', $updateparams, __FUNCTION__
, __FILE__
);
253 $this->getAndCheck(array_merge($params, $updateparams), $result['id'], $this->_entity
);
256 public function testDeletePledgePayment() {
258 'contact_id' => $this->_individualId
,
259 'pledge_id' => $this->_pledgeID
,
260 'contribution_id' => $this->_contributionID
,
263 'actual_amount' => 20,
265 $pledgePayment = $this->callAPISuccess('pledge_payment', 'create', $params);
267 $deleteParams = array(
268 'id' => $pledgePayment['id'],
270 $result = $this->callAPIAndDocument('pledge_payment', 'delete', $deleteParams, __FUNCTION__
, __FILE__
);
273 public function testGetFields() {
274 $result = $this->callAPISuccess('PledgePayment', 'GetFields', array());
275 $this->assertType('array', $result);