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 $config = CRM_Core_Config
::singleton();
255 $config->userPermissionClass
->permissions
= array(
257 'access CiviContribute',
258 'edit contributions',
259 'delete in CiviContribute',
260 'add contributions of type Donation',
261 'delete contributions of type Donation',
263 $this->callAPIFailure('contribution', 'create', $params, 'Error in call to LineItem_create : You do not have permission to create this line item');
265 // Check that the entire contribution has rolled back.
266 $contribution = $this->callAPISuccess('contribution', 'get', array());
267 $this->assertEquals(0, $contribution['count']);
269 CRM_Financial_BAO_FinancialType
::$_availableFinancialTypes = NULL;
271 $config = CRM_Core_Config
::singleton();
272 $config->userPermissionClass
->permissions
= array_merge($config->userPermissionClass
->permissions
, array(
273 'add contributions of type Member Dues',
274 'view contributions of type Donation',
275 'view contributions of type Member Dues',
276 'delete contributions of type Member Dues',
278 $contribution = $this->callAPISuccess('contribution', 'create', $params);
280 $lineItemParams = array(
281 'contribution_id' => $contribution['id'],
282 'entity_table' => 'civicrm_contribution',
284 $lineItems = $this->callAPISuccess('LineItem', 'get', $lineItemParams);
285 $this->assertEquals(3, $lineItems['count']);
286 $this->assertEquals(100.00, $lineItems['values'][3]['line_total']);
287 $this->assertEquals(20, $lineItems['values'][4]['line_total']);
288 $this->assertEquals(80, $lineItems['values'][5]['line_total']);
289 $this->assertEquals(1, $lineItems['values'][3]['financial_type_id']);
290 $this->assertEquals(1, $lineItems['values'][4]['financial_type_id']);
291 $this->assertEquals(2, $lineItems['values'][5]['financial_type_id']);
293 $this->callAPISuccess('Contribution', 'Delete', array(
294 'id' => $contribution['id'],
299 * Test that acl contributions can be edited.
301 public function testEditACLContribution() {
303 $contribution = $this->callAPISuccess('Contribution', 'create', $this->_params
);
306 'id' => $contribution['id'],
307 'check_permissions' => TRUE,
308 'total_amount' => 200.00,
310 $config = CRM_Core_Config
::singleton();
311 $config->userPermissionClass
->permissions
= array(
313 'access CiviContribute',
314 'edit contributions',
315 'view contributions of type Donation',
317 $this->callAPIFailure('Contribution', 'create', $params);
319 $config->userPermissionClass
->permissions
[] = 'edit contributions of type Donation';
320 $contribution = $this->callAPISuccess('Contribution', 'create', $params);
322 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 200.00);
326 * Test that acl contributions can be deleted.
328 public function testDeleteACLContribution() {
330 $config = CRM_Core_Config
::singleton();
331 $config->userPermissionClass
->permissions
= array(
333 'access CiviContribute',
335 'add contributions of type Donation',
337 $contribution = $this->callAPISuccess('Contribution', 'create', $this->_params
);
340 'contribution_id' => $contribution['id'],
341 'check_permissions' => TRUE,
343 $config->userPermissionClass
->permissions
[3] = 'delete in CiviContribute';
344 $this->callAPIFailure('Contribution', 'delete', $params);
346 $config->userPermissionClass
->permissions
[] = 'delete contributions of type Donation';
347 $contribution = $this->callAPISuccess('Contribution', 'delete', $params);
349 $this->assertEquals($contribution['count'], 1);