3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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';
29 require_once 'CiviTest/CiviMailUtils.php';
33 * Test APIv3 civicrm_contribute_* functions
35 * @package CiviCRM_APIv3
36 * @subpackage API_Contribution
38 class api_v3_FinancialTypeACLTest
extends CiviUnitTestCase
{
41 * Assume empty database with just civicrm_data.
43 protected $_individualId;
44 protected $_contribution;
45 protected $_financialTypeId = 1;
46 protected $_apiversion;
47 protected $_entity = 'Contribution';
50 protected $_ids = array();
51 protected $_pageParams = array();
54 * Parameters to create payment processor.
58 protected $_processorParams = array();
61 * ID of created event.
70 public function setUp() {
73 $this->_apiversion
= 3;
74 $this->_individualId
= $this->individualCreate();
75 $this->_params
= array(
76 'contact_id' => $this->_individualId
,
77 'receive_date' => '20120511',
78 'total_amount' => 100.00,
79 'financial_type_id' => $this->_financialTypeId
,
80 'non_deductible_amount' => 10.00,
82 'net_amount' => 95.00,
84 'contribution_status_id' => 1,
86 $this->_processorParams
= array(
89 'payment_processor_type_id' => 10,
90 'financial_account_id' => 12,
93 'url_site' => 'http://dummy.com',
94 'url_recur' => 'http://dummy.com',
97 $this->_pageParams
= array(
98 'title' => 'Test Contribution Page',
99 'financial_type_id' => 1,
101 'financial_account_id' => 1,
102 'payment_processor' => $this->processorCreate(),
104 'is_allow_other_amount' => 1,
106 'max_amount' => 1000,
111 * Clean up after each test.
113 public function tearDown() {
114 $this->quickCleanUpFinancialEntities();
115 $this->quickCleanup(array('civicrm_uf_match'));
118 public function setACL() {
121 'contribution_invoice_settings' => array('acl_financial_type' => 1),
123 $this->callAPISuccess('setting', 'create', $params);
129 public function testCreateACLContribution() {
132 'contact_id' => $this->_individualId
,
133 'receive_date' => '2010-01-20',
134 'total_amount' => 100.00,
135 'financial_type_id' => $this->_financialTypeId
,
136 'non_deductible_amount' => 10.00,
137 'fee_amount' => 5.00,
138 'net_amount' => 95.00,
140 'invoice_id' => 78910,
142 'contribution_status_id' => 1,
143 'check_permissions' => TRUE,
145 $config = &CRM_Core_Config
::singleton();
146 $config->userPermissionClass
->permissions
= array(
148 'access CiviContribute',
149 'edit contributions',
151 $result = $this->callAPIFailure('contribution', 'create', $p);
152 $this->assertEquals('You do not have permission to create this contribution', $result['error_message']);
153 $config->userPermissionClass
->permissions
= array(
155 'access CiviContribute',
156 'edit contributions',
157 'add contributions of type Donation',
159 $contribution = $this->callAPISuccess('contribution', 'create', $p);
162 'contribution_id' => $contribution['id'],
165 $config->userPermissionClass
->permissions
= array(
167 'access CiviContribute',
168 'edit contributions',
169 'view contributions of type Donation',
170 'delete contributions of type Donation',
173 $contribution = $this->callAPISuccess('contribution', 'get', $params);
175 $this->assertEquals(1, $contribution['count']);
176 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId
);
177 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'], 1);
178 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
179 $this->assertEquals($contribution['values'][$contribution['id']]['non_deductible_amount'], 10.00);
180 $this->assertEquals($contribution['values'][$contribution['id']]['fee_amount'], 5.00);
181 $this->assertEquals($contribution['values'][$contribution['id']]['net_amount'], 95.00);
182 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 23456);
183 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 78910);
184 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_source'], 'SSF');
185 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status'], 'Completed');
186 $this->callAPISuccess('Contribution', 'Delete', array(
187 'id' => $contribution['id'],
192 * Test that acl contributions can be retrieved.
194 public function testGetACLContribution() {
196 $contribution = $this->callAPISuccess('Contribution', 'create', $this->_params
);
199 'contribution_id' => $contribution['id'],
200 'check_permissions' => TRUE,
202 $config = &CRM_Core_Config
::singleton();
203 $config->userPermissionClass
->permissions
= array(
205 'access CiviContribute',
207 $contribution = $this->callAPISuccess('contribution', 'get', $params);
208 $this->assertEquals($contribution['count'], 0);
210 $config->userPermissionClass
->permissions
= array(
212 'access CiviContribute',
213 'view contributions of type Donation',
215 $contribution = $this->callAPISuccess('contribution', 'get', $params);
217 $this->assertEquals($contribution['count'], 1);
221 * Test checks that passing in line items suppresses the create mechanism.
223 public function testCreateACLContributionChainedLineItems() {
226 'contact_id' => $this->_individualId
,
227 'receive_date' => '20120511',
228 'total_amount' => 100.00,
229 'financial_type_id' => $this->_financialTypeId
,
230 'payment_instrument_id' => 1,
231 'non_deductible_amount' => 10.00,
232 'fee_amount' => 50.00,
233 'net_amount' => 90.00,
235 'contribution_status_id' => 1,
236 'check_permissions' => TRUE,
237 'api.line_item.create' => array(
239 'price_field_id' => 1,
241 'line_total' => '20',
242 'unit_price' => '10',
243 'financial_type_id' => 1,
246 'price_field_id' => 1,
248 'line_total' => '80',
249 'unit_price' => '80',
250 'financial_type_id' => 2,
255 $description = "Create Contribution with Nested Line Items.";
256 $subfile = "CreateWithNestedLineItems";
257 $config = &CRM_Core_Config
::singleton();
258 $config->userPermissionClass
->permissions
= array(
260 'access CiviContribute',
261 'edit contributions',
262 'delete in CiviContribute',
263 'add contributions of type Donation',
264 'delete contributions of type Donation',
266 $contribution = $this->callAPIFailure('contribution', 'create', $params, 'You do not have permission to create this line item');
268 // Check that the entire contribution has rolled back.
269 $contribution = $this->callAPISuccess('contribution', 'get', array());
270 $this->assertEquals(0, $contribution['count']);
272 CRM_Financial_BAO_FinancialType
::$_availableFinancialTypes = NULL;
274 $config = &CRM_Core_Config
::singleton();
275 $config->userPermissionClass
->permissions
= array(
277 'access CiviContribute',
278 'edit contributions',
279 'delete in CiviContribute',
280 'add contributions of type Member Dues',
281 'add contributions of type Donation',
282 'delete contributions of type Donation',
283 'delete contributions of type Member Dues',
285 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__
, __FILE__
, $description, $subfile);
287 $lineItemParams = array(
288 'contribution_id' => $contribution['id'],
289 'entity_table' => 'civicrm_contribution',
291 $lineItems = $this->callAPISuccess('LineItem', 'get', $lineItemParams);
292 $this->assertEquals(3, $lineItems['count']);
293 $this->assertEquals(100.00, $lineItems['values'][3]['line_total']);
294 $this->assertEquals(20, $lineItems['values'][4]['line_total']);
295 $this->assertEquals(80, $lineItems['values'][5]['line_total']);
296 $this->assertEquals(1, $lineItems['values'][3]['financial_type_id']);
297 $this->assertEquals(1, $lineItems['values'][4]['financial_type_id']);
298 $this->assertEquals(2, $lineItems['values'][5]['financial_type_id']);
300 $this->callAPISuccess('Contribution', 'Delete', array(
301 'id' => $contribution['id'],
306 * Test that acl contributions can be edited.
308 public function testEditACLContribution() {
310 $contribution = $this->callAPISuccess('Contribution', 'create', $this->_params
);
313 'id' => $contribution['id'],
314 'check_permissions' => TRUE,
315 'total_amount' => 200.00,
317 $config = &CRM_Core_Config
::singleton();
318 $config->userPermissionClass
->permissions
= array(
320 'access CiviContribute',
321 'edit contributions',
323 $contribution = $this->callAPIFailure('Contribution', 'create', $params);
325 $config->userPermissionClass
->permissions
= array(
327 'access CiviContribute',
328 'edit contributions',
329 'edit contributions of type Donation',
331 $contribution = $this->callAPISuccess('Contribution', 'create', $params);
333 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 200.00);
337 * Test that acl contributions can be deleted.
339 public function testDeleteACLContribution() {
341 $contribution = $this->callAPISuccess('Contribution', 'create', $this->_params
);
344 'contribution_id' => $contribution['id'],
345 'check_permissions' => TRUE,
347 $config = &CRM_Core_Config
::singleton();
348 $config->userPermissionClass
->permissions
= array(
350 'access CiviContribute',
351 'delete in CiviContribute',
353 $contribution = $this->callAPIFailure('Contribution', 'delete', $params);
355 $config->userPermissionClass
->permissions
= array(
357 'access CiviContribute',
358 'delete in CiviContribute',
359 'delete contributions of type Donation',
361 $contribution = $this->callAPISuccess('Contribution', 'delete', $params);
363 $this->assertEquals($contribution['count'], 1);