CRM-16526 CIVI-3 Bug fixes
[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 '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 CRM_Financial_BAO_FinancialType::$_availableFinancialTypes = NULL;
211
212 $config = &CRM_Core_Config::singleton();
213 $config->userPermissionClass->permissions = array(
214 'access CiviCRM',
215 'access CiviContribute',
216 'view contributions of type Donation',
217 'view debug output',
218 );
219 $contribution = $this->callAPISuccess('contribution', 'get', $params);
220
221 $this->assertEquals($contribution['count'], 1);
222 }
223
224 /**
225 * Test checks that passing in line items suppresses the create mechanism.
226 */
227 public function testCreateACLContributionChainedLineItems() {
228 $this->setACL();
229 $params = array(
230 'contact_id' => $this->_individualId,
231 'receive_date' => '20120511',
232 'total_amount' => 100.00,
233 'financial_type_id' => $this->_financialTypeId,
234 'payment_instrument_id' => 1,
235 'non_deductible_amount' => 10.00,
236 'fee_amount' => 50.00,
237 'net_amount' => 90.00,
238 'source' => 'SSF',
239 'contribution_status_id' => 1,
240 'check_permissions' => TRUE,
241 'api.line_item.create' => array(
242 array(
243 'price_field_id' => 1,
244 'qty' => 2,
245 'line_total' => '20',
246 'unit_price' => '10',
247 'financial_type_id' => 1,
248 ),
249 array(
250 'price_field_id' => 1,
251 'qty' => 1,
252 'line_total' => '80',
253 'unit_price' => '80',
254 'financial_type_id' => 2,
255 ),
256 ),
257 );
258
259 $description = "Create Contribution with Nested Line Items.";
260 $subfile = "CreateWithNestedLineItems";
261 $config = &CRM_Core_Config::singleton();
262 $config->userPermissionClass->permissions = array(
263 'access CiviCRM',
264 'access CiviContribute',
265 'edit contributions',
266 'delete in CiviContribute',
267 'add contributions of type Donation',
268 'delete contributions of type Donation',
269 );
270 $contribution = $this->callAPIFailure('contribution', 'create', $params, 'You do not have permission to create this line item');
271
272 // Check that the entire contribution has rolled back.
273 $contribution = $this->callAPISuccess('contribution', 'get', array());
274 $this->assertEquals(0, $contribution['count']);
275
276 CRM_Financial_BAO_FinancialType::$_availableFinancialTypes = NULL;
277
278 $config = &CRM_Core_Config::singleton();
279 $config->userPermissionClass->permissions = array(
280 'access CiviCRM',
281 'access CiviContribute',
282 'edit contributions',
283 'delete in CiviContribute',
284 'add contributions of type Member Dues',
285 'add contributions of type Donation',
286 'view contributions of type Donation',
287 'view contributions of type Member Dues',
288 'delete contributions of type Donation',
289 'delete contributions of type Member Dues',
290 );
291 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__, __FILE__, $description, $subfile);
292
293 $lineItemParams = array(
294 'contribution_id' => $contribution['id'],
295 'entity_table' => 'civicrm_contribution',
296 );
297 $lineItems = $this->callAPISuccess('LineItem', 'get', $lineItemParams);
298 $this->assertEquals(3, $lineItems['count']);
299 $this->assertEquals(100.00, $lineItems['values'][3]['line_total']);
300 $this->assertEquals(20, $lineItems['values'][4]['line_total']);
301 $this->assertEquals(80, $lineItems['values'][5]['line_total']);
302 $this->assertEquals(1, $lineItems['values'][3]['financial_type_id']);
303 $this->assertEquals(1, $lineItems['values'][4]['financial_type_id']);
304 $this->assertEquals(2, $lineItems['values'][5]['financial_type_id']);
305
306 $this->callAPISuccess('Contribution', 'Delete', array(
307 'id' => $contribution['id'],
308 ));
309 }
310
311 /**
312 * Test that acl contributions can be edited.
313 */
314 public function testEditACLContribution() {
315 $this->setACL();
316 $contribution = $this->callAPISuccess('Contribution', 'create', $this->_params);
317
318 $params = array(
319 'id' => $contribution['id'],
320 'check_permissions' => TRUE,
321 'total_amount' => 200.00,
322 );
323 $config = &CRM_Core_Config::singleton();
324 $config->userPermissionClass->permissions = array(
325 'access CiviCRM',
326 'access CiviContribute',
327 'edit contributions',
328 'view contributions of type Donation',
329 );
330 $contribution = $this->callAPIFailure('Contribution', 'create', $params);
331
332 $config->userPermissionClass->permissions = array(
333 'access CiviCRM',
334 'access CiviContribute',
335 'edit contributions',
336 'edit contributions of type Donation',
337 );
338 $contribution = $this->callAPISuccess('Contribution', 'create', $params);
339
340 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 200.00);
341 }
342
343 /**
344 * Test that acl contributions can be deleted.
345 */
346 public function testDeleteACLContribution() {
347 $this->setACL();
348 $contribution = $this->callAPISuccess('Contribution', 'create', $this->_params);
349
350 $params = array(
351 'contribution_id' => $contribution['id'],
352 'check_permissions' => TRUE,
353 );
354 $config = &CRM_Core_Config::singleton();
355 $config->userPermissionClass->permissions = array(
356 'access CiviCRM',
357 'access CiviContribute',
358 'delete in CiviContribute',
359 );
360 $contribution = $this->callAPIFailure('Contribution', 'delete', $params);
361
362 $config->userPermissionClass->permissions = array(
363 'access CiviCRM',
364 'access CiviContribute',
365 'delete in CiviContribute',
366 'delete contributions of type Donation',
367 );
368 $contribution = $this->callAPISuccess('Contribution', 'delete', $params);
369
370 $this->assertEquals($contribution['count'], 1);
371 }
372
373 }