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 APIv3 civicrm_contribute_* functions
31 * @package CiviCRM_APIv3
32 * @subpackage API_Contribution
34 class api_v3_FinancialTypeACLTest
extends CiviUnitTestCase
{
37 * Assume empty database with just civicrm_data.
39 protected $_individualId;
40 protected $_contribution;
41 protected $_financialTypeId = 1;
42 protected $_apiversion;
43 protected $_entity = 'Contribution';
46 protected $_ids = array();
47 protected $_pageParams = array();
50 * Parameters to create payment processor.
54 protected $_processorParams = array();
57 * ID of created event.
66 public function setUp() {
69 $this->_apiversion
= 3;
70 $this->_individualId
= $this->individualCreate();
71 $this->_params
= array(
72 'contact_id' => $this->_individualId
,
73 'receive_date' => '20120511',
74 'total_amount' => 100.00,
75 'financial_type_id' => $this->_financialTypeId
,
76 'non_deductible_amount' => 10.00,
78 'net_amount' => 95.00,
80 'contribution_status_id' => 1,
82 $this->_processorParams
= array(
85 'payment_processor_type_id' => 10,
86 'financial_account_id' => 12,
89 'url_site' => 'http://dummy.com',
90 'url_recur' => 'http://dummy.com',
93 $this->_pageParams
= array(
94 'title' => 'Test Contribution Page',
95 'financial_type_id' => 1,
97 'financial_account_id' => 1,
98 'payment_processor' => $this->processorCreate(),
100 'is_allow_other_amount' => 1,
102 'max_amount' => 1000,
107 * Clean up after each test.
109 public function tearDown() {
110 $this->quickCleanUpFinancialEntities();
111 $this->quickCleanup(array('civicrm_uf_match'));
112 CRM_Financial_BAO_FinancialType
::$_availableFinancialTypes = array();
113 CRM_Financial_BAO_FinancialType
::$_statusACLFt = array();
116 'contribution_invoice_settings' => array('acl_financial_type' => 0),
120 public function setACL() {
121 CRM_Financial_BAO_FinancialType
::$_availableFinancialTypes = array();
122 CRM_Financial_BAO_FinancialType
::$_statusACLFt = array();
125 'contribution_invoice_settings' => array('acl_financial_type' => 1),
127 $this->callAPISuccess('setting', 'create', $params);
133 public function testCreateACLContribution() {
136 'contact_id' => $this->_individualId
,
137 'receive_date' => '2010-01-20',
138 'total_amount' => 100.00,
139 'financial_type_id' => $this->_financialTypeId
,
140 'non_deductible_amount' => 10.00,
141 'fee_amount' => 5.00,
142 'net_amount' => 95.00,
144 'invoice_id' => 78910,
146 'contribution_status_id' => 1,
147 'check_permissions' => TRUE,
149 $config = &CRM_Core_Config
::singleton();
150 $config->userPermissionClass
->permissions
= array(
152 'access CiviContribute',
153 'edit contributions',
155 $result = $this->callAPIFailure('contribution', 'create', $p);
156 $this->assertEquals('You do not have permission to create this contribution', $result['error_message']);
157 $config->userPermissionClass
->permissions
[] = 'add contributions of type Donation';
158 $contribution = $this->callAPISuccess('contribution', 'create', $p);
161 'contribution_id' => $contribution['id'],
164 $config->userPermissionClass
->permissions
= array(
166 'access CiviContribute',
167 'edit contributions',
168 'view contributions of type Donation',
169 'delete contributions of type Donation',
172 $contribution = $this->callAPISuccess('contribution', 'get', $params);
174 $this->assertEquals(1, $contribution['count']);
175 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId
);
176 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'], 1);
177 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
178 $this->assertEquals($contribution['values'][$contribution['id']]['non_deductible_amount'], 10.00);
179 $this->assertEquals($contribution['values'][$contribution['id']]['fee_amount'], 5.00);
180 $this->assertEquals($contribution['values'][$contribution['id']]['net_amount'], 95.00);
181 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 23456);
182 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 78910);
183 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_source'], 'SSF');
184 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status'], 'Completed');
185 $this->callAPISuccess('Contribution', 'Delete', array(
186 'id' => $contribution['id'],
191 * Test that acl contributions can be retrieved.
193 public function testGetACLContribution() {
195 $config = &CRM_Core_Config
::singleton();
196 $config->userPermissionClass
->permissions
= array(
198 'access CiviContribute',
200 'add contributions of type Donation',
202 $contribution = $this->callAPISuccess('Contribution', 'create', $this->_params
);
205 'id' => $contribution['id'],
206 'check_permissions' => TRUE,
208 $contribution = $this->callAPISuccess('contribution', 'get', $params);
209 $this->assertEquals($contribution['count'], 0);
211 CRM_Financial_BAO_FinancialType
::$_availableFinancialTypes = NULL;
213 $config->userPermissionClass
->permissions
[3] = 'view contributions of type Donation';
214 $contribution = $this->callAPISuccess('contribution', 'get', $params);
216 $this->assertEquals($contribution['count'], 1);
220 * Test checks that passing in line items suppresses the create mechanism.
222 public function testCreateACLContributionChainedLineItems() {
225 'contact_id' => $this->_individualId
,
226 'receive_date' => '20120511',
227 'total_amount' => 100.00,
228 'financial_type_id' => $this->_financialTypeId
,
229 'payment_instrument_id' => 1,
230 'non_deductible_amount' => 10.00,
231 'fee_amount' => 50.00,
232 'net_amount' => 90.00,
234 'contribution_status_id' => 1,
235 'check_permissions' => TRUE,
236 'api.line_item.create' => array(
238 'price_field_id' => 1,
240 'line_total' => '20',
241 'unit_price' => '10',
242 'financial_type_id' => 1,
245 'price_field_id' => 1,
247 'line_total' => '80',
248 'unit_price' => '80',
249 'financial_type_id' => 2,
254 $description = "Create Contribution with Nested Line Items.";
255 $subfile = "CreateWithNestedLineItems";
256 $config = &CRM_Core_Config
::singleton();
257 $config->userPermissionClass
->permissions
= array(
259 'access CiviContribute',
260 'edit contributions',
261 'delete in CiviContribute',
262 'add contributions of type Donation',
263 'delete contributions of type Donation',
265 $contribution = $this->callAPIFailure('contribution', 'create', $params, 'Error in call to LineItem_create : You do not have permission to create this line item');
267 // Check that the entire contribution has rolled back.
268 $contribution = $this->callAPISuccess('contribution', 'get', array());
269 $this->assertEquals(0, $contribution['count']);
271 CRM_Financial_BAO_FinancialType
::$_availableFinancialTypes = NULL;
273 $config = &CRM_Core_Config
::singleton();
274 $config->userPermissionClass
->permissions
= array_merge($config->userPermissionClass
->permissions
, array(
275 'add contributions of type Member Dues',
276 'view contributions of type Donation',
277 'view contributions of type Member Dues',
278 'delete contributions of type Member Dues',
280 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__
, __FILE__
, $description, $subfile);
282 $lineItemParams = array(
283 'contribution_id' => $contribution['id'],
284 'entity_table' => 'civicrm_contribution',
286 $lineItems = $this->callAPISuccess('LineItem', 'get', $lineItemParams);
287 $this->assertEquals(3, $lineItems['count']);
288 $this->assertEquals(100.00, $lineItems['values'][3]['line_total']);
289 $this->assertEquals(20, $lineItems['values'][4]['line_total']);
290 $this->assertEquals(80, $lineItems['values'][5]['line_total']);
291 $this->assertEquals(1, $lineItems['values'][3]['financial_type_id']);
292 $this->assertEquals(1, $lineItems['values'][4]['financial_type_id']);
293 $this->assertEquals(2, $lineItems['values'][5]['financial_type_id']);
295 $this->callAPISuccess('Contribution', 'Delete', array(
296 'id' => $contribution['id'],
301 * Test that acl contributions can be edited.
303 public function testEditACLContribution() {
305 $contribution = $this->callAPISuccess('Contribution', 'create', $this->_params
);
308 'id' => $contribution['id'],
309 'check_permissions' => TRUE,
310 'total_amount' => 200.00,
312 $config = &CRM_Core_Config
::singleton();
313 $config->userPermissionClass
->permissions
= array(
315 'access CiviContribute',
316 'edit contributions',
317 'view contributions of type Donation',
319 $contribution = $this->callAPIFailure('Contribution', 'create', $params);
321 $config->userPermissionClass
->permissions
[] = 'edit contributions of type Donation';
322 $contribution = $this->callAPISuccess('Contribution', 'create', $params);
324 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 200.00);
328 * Test that acl contributions can be deleted.
330 public function testDeleteACLContribution() {
332 $config = &CRM_Core_Config
::singleton();
333 $config->userPermissionClass
->permissions
= array(
335 'access CiviContribute',
337 'add contributions of type Donation',
339 $contribution = $this->callAPISuccess('Contribution', 'create', $this->_params
);
342 'contribution_id' => $contribution['id'],
343 'check_permissions' => TRUE,
345 $config->userPermissionClass
->permissions
[3] = 'delete in CiviContribute';
346 $contribution = $this->callAPIFailure('Contribution', 'delete', $params);
348 $config->userPermissionClass
->permissions
[] = 'delete contributions of type Donation';
349 $contribution = $this->callAPISuccess('Contribution', 'delete', $params);
351 $this->assertEquals($contribution['count'], 1);