44012ca0c7b755715990ce889703c35885f2d4ba
[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-2015 |
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 require_once 'CiviTest/CiviUnitTestCase.php';
29 require_once 'CiviTest/CiviMailUtils.php';
30
31
32 /**
33 * Test APIv3 civicrm_contribute_* functions
34 *
35 * @package CiviCRM_APIv3
36 * @subpackage API_Contribution
37 */
38 class api_v3_FinancialTypeACLTest extends CiviUnitTestCase {
39
40 /**
41 * Assume empty database with just civicrm_data.
42 */
43 protected $_individualId;
44 protected $_contribution;
45 protected $_financialTypeId = 1;
46 protected $_apiversion;
47 protected $_entity = 'Contribution';
48 public $debug = 0;
49 protected $_params;
50 protected $_ids = array();
51 protected $_pageParams = array();
52
53 /**
54 * Parameters to create payment processor.
55 *
56 * @var array
57 */
58 protected $_processorParams = array();
59
60 /**
61 * ID of created event.
62 *
63 * @var int
64 */
65 protected $_eventID;
66
67 /**
68 * Setup function.
69 */
70 public function setUp() {
71 //parent::setUp();
72
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,
81 'fee_amount' => 5.00,
82 'net_amount' => 95.00,
83 'source' => 'SSF',
84 'contribution_status_id' => 1,
85 );
86 $this->_processorParams = array(
87 'domain_id' => 1,
88 'name' => 'Dummy',
89 'payment_processor_type_id' => 10,
90 'financial_account_id' => 12,
91 'is_active' => 1,
92 'user_name' => '',
93 'url_site' => 'http://dummy.com',
94 'url_recur' => 'http://dummy.com',
95 'billing_mode' => 1,
96 );
97 $this->_pageParams = array(
98 'title' => 'Test Contribution Page',
99 'financial_type_id' => 1,
100 'currency' => 'USD',
101 'financial_account_id' => 1,
102 'payment_processor' => $this->processorCreate(),
103 'is_active' => 1,
104 'is_allow_other_amount' => 1,
105 'min_amount' => 10,
106 'max_amount' => 1000,
107 );
108 }
109
110 /**
111 * Clean up after each test.
112 */
113 public function tearDown() {
114 $this->quickCleanUpFinancialEntities();
115 $this->quickCleanup(array('civicrm_uf_match'));
116 }
117
118 public function setACL() {
119 $params = array(
120 'domain_id' => 1,
121 'contribution_invoice_settings' => array('acl_financial_type' => 1),
122 );
123 $this->callAPISuccess('setting', 'create', $params);
124 }
125
126 /**
127 * Test Get.
128 */
129 public function testCreateACLContribution() {
130 $this->setACL();
131 $p = array(
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,
139 'trxn_id' => 23456,
140 'invoice_id' => 78910,
141 'source' => 'SSF',
142 'contribution_status_id' => 1,
143 'check_permissions' => TRUE,
144 );
145 $config = &CRM_Core_Config::singleton();
146 $config->userPermissionClass->permissions = array(
147 'access CiviCRM',
148 'access CiviContribute',
149 'edit contributions',
150 );
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(
154 'access CiviCRM',
155 'access CiviContribute',
156 'edit contributions',
157 'add contributions of type Donation',
158 );
159 $contribution = $this->callAPISuccess('contribution', 'create', $p);
160
161 $params = array(
162 'contribution_id' => $contribution['id'],
163 );
164
165 $config->userPermissionClass->permissions = array(
166 'access CiviCRM',
167 'access CiviContribute',
168 'edit contributions',
169 'view contributions of type Donation',
170 'delete contributions of type Donation',
171 );
172
173 $contribution = $this->callAPISuccess('contribution', 'get', $params);
174
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'],
188 ));
189 }
190
191 /**
192 * Test that acl contributions can be retrieved.
193 */
194 public function testGetACLContribution() {
195 $this->setACL();
196 $contribution = $this->callAPISuccess('Contribution', 'create', $this->_params);
197
198 $params = array(
199 'contribution_id' => $contribution['id'],
200 'check_permissions' => TRUE,
201 );
202 $config = &CRM_Core_Config::singleton();
203 $config->userPermissionClass->permissions = array(
204 'access CiviCRM',
205 'access CiviContribute',
206 );
207 $contribution = $this->callAPISuccess('contribution', 'get', $params);
208 $this->assertEquals($contribution['count'], 0);
209
210 $config->userPermissionClass->permissions = array(
211 'access CiviCRM',
212 'access CiviContribute',
213 'view contributions of type Donation',
214 );
215 $contribution = $this->callAPISuccess('contribution', 'get', $params);
216
217 $this->assertEquals($contribution['count'], 1);
218 }
219
220 /**
221 * Test checks that passing in line items suppresses the create mechanism.
222 */
223 public function testCreateACLContributionChainedLineItems() {
224 $this->setACL();
225 $params = array(
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,
234 'source' => 'SSF',
235 'contribution_status_id' => 1,
236 'check_permissions' => TRUE,
237 'api.line_item.create' => array(
238 array(
239 'price_field_id' => 1,
240 'qty' => 2,
241 'line_total' => '20',
242 'unit_price' => '10',
243 'financial_type_id' => 1,
244 ),
245 array(
246 'price_field_id' => 1,
247 'qty' => 1,
248 'line_total' => '80',
249 'unit_price' => '80',
250 'financial_type_id' => 2,
251 ),
252 ),
253 );
254
255 $description = "Create Contribution with Nested Line Items.";
256 $subfile = "CreateWithNestedLineItems";
257 $config = &CRM_Core_Config::singleton();
258 $config->userPermissionClass->permissions = array(
259 'access CiviCRM',
260 'access CiviContribute',
261 'edit contributions',
262 'delete in CiviContribute',
263 'add contributions of type Donation',
264 'delete contributions of type Donation',
265 );
266 $contribution = $this->callAPIFailure('contribution', 'create', $params, 'You do not have permission to create this line item');
267
268 // Check that the entire contribution has rolled back.
269 $contribution = $this->callAPISuccess('contribution', 'get', array());
270 $this->assertEquals(0, $contribution['count']);
271
272 CRM_Financial_BAO_FinancialType::$_availableFinancialTypes = NULL;
273
274 $config = &CRM_Core_Config::singleton();
275 $config->userPermissionClass->permissions = array(
276 'access CiviCRM',
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',
284 );
285 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__, __FILE__, $description, $subfile);
286
287 $lineItemParams = array(
288 'contribution_id' => $contribution['id'],
289 'entity_table' => 'civicrm_contribution',
290 );
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']);
299
300 $this->callAPISuccess('Contribution', 'Delete', array(
301 'id' => $contribution['id'],
302 ));
303 }
304
305 /**
306 * Test that acl contributions can be edited.
307 */
308 public function testEditACLContribution() {
309 $this->setACL();
310 $contribution = $this->callAPISuccess('Contribution', 'create', $this->_params);
311
312 $params = array(
313 'id' => $contribution['id'],
314 'check_permissions' => TRUE,
315 'total_amount' => 200.00,
316 );
317 $config = &CRM_Core_Config::singleton();
318 $config->userPermissionClass->permissions = array(
319 'access CiviCRM',
320 'access CiviContribute',
321 'edit contributions',
322 );
323 $contribution = $this->callAPIFailure('Contribution', 'create', $params);
324
325 $config->userPermissionClass->permissions = array(
326 'access CiviCRM',
327 'access CiviContribute',
328 'edit contributions',
329 'edit contributions of type Donation',
330 );
331 $contribution = $this->callAPISuccess('Contribution', 'create', $params);
332
333 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 200.00);
334 }
335
336 /**
337 * Test that acl contributions can be deleted.
338 */
339 public function testDeleteACLContribution() {
340 $this->setACL();
341 $contribution = $this->callAPISuccess('Contribution', 'create', $this->_params);
342
343 $params = array(
344 'contribution_id' => $contribution['id'],
345 'check_permissions' => TRUE,
346 );
347 $config = &CRM_Core_Config::singleton();
348 $config->userPermissionClass->permissions = array(
349 'access CiviCRM',
350 'access CiviContribute',
351 'delete in CiviContribute',
352 );
353 $contribution = $this->callAPIFailure('Contribution', 'delete', $params);
354
355 $config->userPermissionClass->permissions = array(
356 'access CiviCRM',
357 'access CiviContribute',
358 'delete in CiviContribute',
359 'delete contributions of type Donation',
360 );
361 $contribution = $this->callAPISuccess('Contribution', 'delete', $params);
362
363 $this->assertEquals($contribution['count'], 1);
364 }
365
366 }