Merge pull request #7797 from JKingsnorth/CRM-17977
[civicrm-core.git] / tests / phpunit / api / v3 / FinancialTypeACLTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2016 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
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. |
13 | |
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. |
18 | |
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 +--------------------------------------------------------------------+
26 */
27
28 /**
29 * Test APIv3 civicrm_contribute_* functions
30 *
31 * @package CiviCRM_APIv3
32 * @subpackage API_Contribution
33 */
34 class api_v3_FinancialTypeACLTest extends CiviUnitTestCase {
35
36 /**
37 * Assume empty database with just civicrm_data.
38 */
39 protected $_individualId;
40 protected $_contribution;
41 protected $_financialTypeId = 1;
42 protected $_apiversion;
43 protected $_entity = 'Contribution';
44 public $debug = 0;
45 protected $_params;
46 protected $_ids = array();
47 protected $_pageParams = array();
48
49 /**
50 * Parameters to create payment processor.
51 *
52 * @var array
53 */
54 protected $_processorParams = array();
55
56 /**
57 * ID of created event.
58 *
59 * @var int
60 */
61 protected $_eventID;
62
63 /**
64 * Setup function.
65 */
66 public function setUp() {
67 parent::setUp();
68
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,
77 'fee_amount' => 5.00,
78 'net_amount' => 95.00,
79 'source' => 'SSF',
80 'contribution_status_id' => 1,
81 );
82 $this->_processorParams = array(
83 'domain_id' => 1,
84 'name' => 'Dummy',
85 'payment_processor_type_id' => 10,
86 'financial_account_id' => 12,
87 'is_active' => 1,
88 'user_name' => '',
89 'url_site' => 'http://dummy.com',
90 'url_recur' => 'http://dummy.com',
91 'billing_mode' => 1,
92 );
93 $this->_pageParams = array(
94 'title' => 'Test Contribution Page',
95 'financial_type_id' => 1,
96 'currency' => 'USD',
97 'financial_account_id' => 1,
98 'payment_processor' => $this->processorCreate(),
99 'is_active' => 1,
100 'is_allow_other_amount' => 1,
101 'min_amount' => 10,
102 'max_amount' => 1000,
103 );
104 }
105
106 /**
107 * Clean up after each test.
108 */
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();
114 $params = array(
115 'domain_id' => 1,
116 'contribution_invoice_settings' => array('acl_financial_type' => 0),
117 );
118 }
119
120 public function setACL() {
121 CRM_Financial_BAO_FinancialType::$_availableFinancialTypes = array();
122 CRM_Financial_BAO_FinancialType::$_statusACLFt = array();
123 $params = array(
124 'domain_id' => 1,
125 'contribution_invoice_settings' => array('acl_financial_type' => 1),
126 );
127 $this->callAPISuccess('setting', 'create', $params);
128 }
129
130 /**
131 * Test Get.
132 */
133 public function testCreateACLContribution() {
134 $this->setACL();
135 $p = array(
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,
143 'trxn_id' => 23456,
144 'invoice_id' => 78910,
145 'source' => 'SSF',
146 'contribution_status_id' => 1,
147 'check_permissions' => TRUE,
148 );
149 $config = &CRM_Core_Config::singleton();
150 $config->userPermissionClass->permissions = array(
151 'access CiviCRM',
152 'access CiviContribute',
153 'edit contributions',
154 );
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);
159
160 $params = array(
161 'contribution_id' => $contribution['id'],
162 );
163
164 $config->userPermissionClass->permissions = array(
165 'access CiviCRM',
166 'access CiviContribute',
167 'edit contributions',
168 'view contributions of type Donation',
169 'delete contributions of type Donation',
170 );
171
172 $contribution = $this->callAPISuccess('contribution', 'get', $params);
173
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'],
187 ));
188 }
189
190 /**
191 * Test that acl contributions can be retrieved.
192 */
193 public function testGetACLContribution() {
194 $this->setACL();
195 $config = &CRM_Core_Config::singleton();
196 $config->userPermissionClass->permissions = array(
197 'access CiviCRM',
198 'access CiviContribute',
199 'view all contacts',
200 'add contributions of type Donation',
201 );
202 $contribution = $this->callAPISuccess('Contribution', 'create', $this->_params);
203
204 $params = array(
205 'id' => $contribution['id'],
206 'check_permissions' => TRUE,
207 );
208 $contribution = $this->callAPISuccess('contribution', 'get', $params);
209 $this->assertEquals($contribution['count'], 0);
210
211 CRM_Financial_BAO_FinancialType::$_availableFinancialTypes = NULL;
212
213 $config->userPermissionClass->permissions[3] = 'view contributions of type Donation';
214 $contribution = $this->callAPISuccess('contribution', 'get', $params);
215
216 $this->assertEquals($contribution['count'], 1);
217 }
218
219 /**
220 * Test checks that passing in line items suppresses the create mechanism.
221 */
222 public function testCreateACLContributionChainedLineItems() {
223 $this->setACL();
224 $params = array(
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,
233 'source' => 'SSF',
234 'contribution_status_id' => 1,
235 'check_permissions' => TRUE,
236 'api.line_item.create' => array(
237 array(
238 'price_field_id' => 1,
239 'qty' => 2,
240 'line_total' => '20',
241 'unit_price' => '10',
242 'financial_type_id' => 1,
243 ),
244 array(
245 'price_field_id' => 1,
246 'qty' => 1,
247 'line_total' => '80',
248 'unit_price' => '80',
249 'financial_type_id' => 2,
250 ),
251 ),
252 );
253
254 $description = "Create Contribution with Nested Line Items.";
255 $subfile = "CreateWithNestedLineItems";
256 $config = &CRM_Core_Config::singleton();
257 $config->userPermissionClass->permissions = array(
258 'access CiviCRM',
259 'access CiviContribute',
260 'edit contributions',
261 'delete in CiviContribute',
262 'add contributions of type Donation',
263 'delete contributions of type Donation',
264 );
265 $contribution = $this->callAPIFailure('contribution', 'create', $params, 'You do not have permission to create this line item');
266
267 // Check that the entire contribution has rolled back.
268 $contribution = $this->callAPISuccess('contribution', 'get', array());
269 $this->assertEquals(0, $contribution['count']);
270
271 CRM_Financial_BAO_FinancialType::$_availableFinancialTypes = NULL;
272
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',
279 ));
280 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__, __FILE__, $description, $subfile);
281
282 $lineItemParams = array(
283 'contribution_id' => $contribution['id'],
284 'entity_table' => 'civicrm_contribution',
285 );
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']);
294
295 $this->callAPISuccess('Contribution', 'Delete', array(
296 'id' => $contribution['id'],
297 ));
298 }
299
300 /**
301 * Test that acl contributions can be edited.
302 */
303 public function testEditACLContribution() {
304 $this->setACL();
305 $contribution = $this->callAPISuccess('Contribution', 'create', $this->_params);
306
307 $params = array(
308 'id' => $contribution['id'],
309 'check_permissions' => TRUE,
310 'total_amount' => 200.00,
311 );
312 $config = &CRM_Core_Config::singleton();
313 $config->userPermissionClass->permissions = array(
314 'access CiviCRM',
315 'access CiviContribute',
316 'edit contributions',
317 'view contributions of type Donation',
318 );
319 $contribution = $this->callAPIFailure('Contribution', 'create', $params);
320
321 $config->userPermissionClass->permissions[] = 'edit contributions of type Donation';
322 $contribution = $this->callAPISuccess('Contribution', 'create', $params);
323
324 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 200.00);
325 }
326
327 /**
328 * Test that acl contributions can be deleted.
329 */
330 public function testDeleteACLContribution() {
331 $this->setACL();
332 $config = &CRM_Core_Config::singleton();
333 $config->userPermissionClass->permissions = array(
334 'access CiviCRM',
335 'access CiviContribute',
336 'view all contacts',
337 'add contributions of type Donation',
338 );
339 $contribution = $this->callAPISuccess('Contribution', 'create', $this->_params);
340
341 $params = array(
342 'contribution_id' => $contribution['id'],
343 'check_permissions' => TRUE,
344 );
345 $config->userPermissionClass->permissions[3] = 'delete in CiviContribute';
346 $contribution = $this->callAPIFailure('Contribution', 'delete', $params);
347
348 $config->userPermissionClass->permissions[] = 'delete contributions of type Donation';
349 $contribution = $this->callAPISuccess('Contribution', 'delete', $params);
350
351 $this->assertEquals($contribution['count'], 1);
352 }
353
354 }