3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2017 |
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
34 class api_v3_PledgePaymentTest
extends CiviUnitTestCase
{
37 * Assume empty database with just civicrm_data.
39 protected $_individualId;
41 protected $_apiversion = 3;
42 protected $_contributionID;
43 protected $_financialTypeId = 1;
44 protected $_entity = 'PledgePayment';
45 public $DBResetRequired = TRUE;
47 public function setUp() {
49 $this->_individualId
= $this->individualCreate();
50 $this->_pledgeID
= $this->pledgeCreate(array('contact_id' => $this->_individualId
));
51 $this->_contributionID
= $this->contributionCreate(array('contact_id' => $this->_individualId
));
54 public function tearDown() {
55 $tablesToTruncate = array(
56 'civicrm_contribution',
59 'civicrm_pledge_payment',
63 $this->quickCleanup($tablesToTruncate);
66 public function testGetPledgePayment() {
68 $result = $this->callAPIAndDocument('pledge_payment', 'get', $params, __FUNCTION__
, __FILE__
);
69 $this->assertEquals(5, $result['count'], " in line " . __LINE__
);
73 * Test that passing in a single variable works.
75 public function testGetSinglePledgePayment() {
76 $createparams = array(
77 'contact_id' => $this->_individualId
,
78 'pledge_id' => $this->_pledgeID
,
79 'contribution_id' => $this->_contributionID
,
82 $createResult = $this->callAPISuccess('pledge_payment', 'create', $createparams);
84 'contribution_id' => $this->_contributionID
,
86 $result = $this->callAPISuccess('pledge_payment', 'get', $params);
87 $this->assertEquals(1, $result['count'], " in line " . __LINE__
);
91 * Test that passing in a single variable works:: status_id
93 public function testGetSinglePledgePaymentByStatusID() {
94 $createparams = array(
95 'contact_id' => $this->_individualId
,
96 'pledge_id' => $this->_pledgeID
,
97 'contribution_id' => $this->_contributionID
,
100 $createResult = $this->callAPISuccess('pledge_payment', 'create', $createparams);
105 $result = $this->callAPISuccess('pledge_payment', 'get', $params);
106 $this->assertEquals(1, $result['count'], " in line " . __LINE__
);
110 * Test that creating a payment will add the contribution ID.
112 public function testCreatePledgePayment() {
113 //check that 5 pledge payments exist at the start
114 $beforeAdd = $this->callAPISuccess('pledge_payment', 'get', array());
115 $this->assertEquals(5, $beforeAdd['count'], " in line " . __LINE__
);
117 //test the pledge_payment_create function
119 'contact_id' => $this->_individualId
,
120 'pledge_id' => $this->_pledgeID
,
121 'contribution_id' => $this->_contributionID
,
123 'actual_amount' => 20,
125 $result = $this->callAPIAndDocument('pledge_payment', 'create', $params, __FUNCTION__
, __FILE__
);
127 //check existing updated not new one created - 'create' means add contribution_id in this context
128 $afterAdd = $this->callAPISuccess('pledge_payment', 'get', array());
129 $this->assertEquals(5, $afterAdd['count'], " in line " . __LINE__
);
131 //get the created payment & check it out
132 $getParams['id'] = $result['id'];
133 $getIndPayment = $this->callAPISuccess('pledge_payment', 'get', $getParams);
134 $this->assertEquals(1, $getIndPayment['count'], " in line " . __LINE__
);
135 $this->assertEquals(20, $getIndPayment['values'][$result['id']]['actual_amount'], " in line " . __LINE__
);
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(
139 'total_amount' => 20,
140 'contact_id' => $this->_individualId
,
141 'financial_type_id' => $this->_financialTypeId
,
143 $contribution = $this->callAPISuccess('contribution', 'create', $contributionParams);
144 $params['contribution_id'] = $contribution['id'];
146 $resultCont2 = $this->callAPISuccess('pledge_payment', 'create', $params);
147 //make sure original is untouched & has not been updated
148 $this->assertGreaterThan($result['id'], $resultCont2['id'], " in line " . __LINE__
);
149 $getIndPaymentAgain = $this->callAPISuccess('pledge_payment', 'get', $getParams);
150 $this->assertEquals(1, $getIndPaymentAgain['count'], " in line " . __LINE__
);
151 $this->assertEquals($this->_contributionID
, $getIndPaymentAgain['values'][$result['id']]['contribution_id'], " in line " . __LINE__
);
155 * Test checks behaviour when more payments are created than should be possible.
157 public function testCreatePledgePaymentAllCreated() {
159 'pledge_id' => $this->_pledgeID
,
162 // create one more pledge than there are spaces for
165 $contributionParams = array(
166 'total_amount' => 20,
167 'contact_id' => $this->_individualId
,
168 'financial_type_id' => $this->_financialTypeId
,
170 $contribution = $this->callAPISuccess('contribution', 'create', $contributionParams);
172 $params['contribution_id'] = $contribution['id'];
174 $resultCont2 = civicrm_api('pledge_payment', 'create', $params +
array('version' => $this->_apiversion
));
177 // check that only 5 exist & we got an error setting the 6th
178 $result = $this->callAPISuccess('PledgePayment', 'Get', array(
179 'pledge_id' => $this->_pledgeID
,
181 // the last one above should result in an error
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']);
183 $this->assertEquals(5, $result['count']);
185 $params['option.create_new'] = 1;
186 $params['scheduled_amount'] = 20;
187 $params['scheduled_date'] = '20131212';
188 $resultcreatenew = $this->callAPISuccess('pledge_payment', 'create', $params);
189 $result = $this->callAPISuccess('PledgePayment', 'Get', array(
190 'pledge_id' => $this->_pledgeID
,
193 $this->assertEquals(6, $result['count']);
197 * Test that creating a payment adds the contribution ID where only one pledge payment is in schedule.
199 public function testCreatePledgePaymentWhereOnlyOnePayment() {
200 $pledgeParams = array(
201 'contact_id' => $this->_individualId
,
202 'pledge_create_date' => date('Ymd'),
203 'start_date' => date('Ymd'),
204 'scheduled_date' => 'first day 2015',
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,
216 $contributionID = $this->contributionCreate(array(
217 'contact_id' => $this->_individualId
,
218 'financial_type_id' => $this->_financialTypeId
,
222 $pledge = $this->callAPISuccess('Pledge', 'Create', $pledgeParams);
224 //test the pledge_payment_create function
226 'contact_id' => $this->_individualId
,
227 'pledge_id' => $pledge['id'],
228 'contribution_id' => $contributionID,
230 'actual_amount' => 20,
232 $result = $this->callAPISuccess('pledge_payment', 'create', $params);
234 //check existing updated not new one created - 'create' means add contribution_id in this context
235 $afterAdd = $this->callAPISuccess('pledge_payment', 'get', array(
236 'contribution_id' => $contributionID,
238 $this->assertEquals(1, $afterAdd['count'], " in line " . __LINE__
);
241 public function testUpdatePledgePayment() {
243 'pledge_id' => $this->_pledgeID
,
244 'contribution_id' => $this->_contributionID
,
246 'actual_amount' => 20,
248 $result = $this->callAPISuccess('pledge_payment', 'create', $params);
249 $updateparams = array(
250 'id' => $result['id'],
254 $result = $this->callAPIAndDocument('pledge_payment', 'update', $updateparams, __FUNCTION__
, __FILE__
);
255 $this->getAndCheck(array_merge($params, $updateparams), $result['id'], $this->_entity
);
258 public function testDeletePledgePayment() {
260 'contact_id' => $this->_individualId
,
261 'pledge_id' => $this->_pledgeID
,
262 'contribution_id' => $this->_contributionID
,
265 'actual_amount' => 20,
267 $pledgePayment = $this->callAPISuccess('pledge_payment', 'create', $params);
269 $deleteParams = array(
270 'id' => $pledgePayment['id'],
272 $result = $this->callAPIAndDocument('pledge_payment', 'delete', $deleteParams, __FUNCTION__
, __FILE__
);
275 public function testGetFields() {
276 $result = $this->callAPISuccess('PledgePayment', 'GetFields', array());
277 $this->assertType('array', $result);