Merge pull request #9746 from vedantrathore/master
[civicrm-core.git] / tests / phpunit / api / v3 / FinancialTypeACLTest.php
CommitLineData
147f0967
E
1<?php
2/*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
15a4309a 6 | Copyright CiviCRM LLC (c) 2004-2017 |
147f0967
E
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
147f0967
E
28/**
29 * Test APIv3 civicrm_contribute_* functions
30 *
31 * @package CiviCRM_APIv3
32 * @subpackage API_Contribution
33 */
34class 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() {
e2054d83 67 parent::setUp();
147f0967
E
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'));
87e14d27
E
112 CRM_Financial_BAO_FinancialType::$_availableFinancialTypes = array();
113 CRM_Financial_BAO_FinancialType::$_statusACLFt = array();
a01a8920
E
114 $params = array(
115 'domain_id' => 1,
116 'contribution_invoice_settings' => array('acl_financial_type' => 0),
117 );
147f0967
E
118 }
119
120 public function setACL() {
f4a331f4
E
121 CRM_Financial_BAO_FinancialType::$_availableFinancialTypes = array();
122 CRM_Financial_BAO_FinancialType::$_statusACLFt = array();
147f0967
E
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 */
8b92461a 133 public function testCreateACLContribution() {
147f0967
E
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']);
50d8cef8 157 $config->userPermissionClass->permissions[] = 'add contributions of type Donation';
147f0967 158 $contribution = $this->callAPISuccess('contribution', 'create', $p);
9716cd7c 159
147f0967
E
160 $params = array(
161 'contribution_id' => $contribution['id'],
162 );
9716cd7c 163
147f0967
E
164 $config->userPermissionClass->permissions = array(
165 'access CiviCRM',
166 'access CiviContribute',
167 'edit contributions',
168 'view contributions of type Donation',
9716cd7c 169 'delete contributions of type Donation',
147f0967 170 );
9716cd7c 171
147f0967 172 $contribution = $this->callAPISuccess('contribution', 'get', $params);
3007d344 173
147f0967
E
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();
50d8cef8
PN
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 );
147f0967
E
202 $contribution = $this->callAPISuccess('Contribution', 'create', $this->_params);
203
204 $params = array(
b8f2d49b 205 'id' => $contribution['id'],
147f0967
E
206 'check_permissions' => TRUE,
207 );
147f0967 208 $contribution = $this->callAPISuccess('contribution', 'get', $params);
147f0967 209 $this->assertEquals($contribution['count'], 0);
9716cd7c 210
b8f2d49b
E
211 CRM_Financial_BAO_FinancialType::$_availableFinancialTypes = NULL;
212
50d8cef8 213 $config->userPermissionClass->permissions[3] = 'view contributions of type Donation';
147f0967 214 $contribution = $this->callAPISuccess('contribution', 'get', $params);
9716cd7c 215
147f0967
E
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,
147f0967
E
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
5be22f39 254 $config = CRM_Core_Config::singleton();
147f0967
E
255 $config->userPermissionClass->permissions = array(
256 'access CiviCRM',
257 'access CiviContribute',
258 'edit contributions',
d8bf43ff 259 'delete in CiviContribute',
147f0967 260 'add contributions of type Donation',
d8bf43ff 261 'delete contributions of type Donation',
147f0967 262 );
5be22f39 263 $this->callAPIFailure('contribution', 'create', $params, 'Error in call to LineItem_create : You do not have permission to create this line item');
9716cd7c 264
8b92461a
E
265 // Check that the entire contribution has rolled back.
266 $contribution = $this->callAPISuccess('contribution', 'get', array());
267 $this->assertEquals(0, $contribution['count']);
dee383fc 268
8b92461a 269 CRM_Financial_BAO_FinancialType::$_availableFinancialTypes = NULL;
dee383fc 270
5be22f39 271 $config = CRM_Core_Config::singleton();
50d8cef8 272 $config->userPermissionClass->permissions = array_merge($config->userPermissionClass->permissions, array(
147f0967 273 'add contributions of type Member Dues',
b8f2d49b
E
274 'view contributions of type Donation',
275 'view contributions of type Member Dues',
d8bf43ff 276 'delete contributions of type Member Dues',
50d8cef8 277 ));
5be22f39 278 $contribution = $this->callAPISuccess('contribution', 'create', $params);
dee383fc
E
279
280 $lineItemParams = array(
281 'contribution_id' => $contribution['id'],
282 'entity_table' => 'civicrm_contribution',
283 );
284 $lineItems = $this->callAPISuccess('LineItem', 'get', $lineItemParams);
285 $this->assertEquals(3, $lineItems['count']);
d8bf43ff
E
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']);
147f0967 292
147f0967
E
293 $this->callAPISuccess('Contribution', 'Delete', array(
294 'id' => $contribution['id'],
295 ));
296 }
297
be23f122
E
298 /**
299 * Test that acl contributions can be edited.
300 */
301 public function testEditACLContribution() {
302 $this->setACL();
303 $contribution = $this->callAPISuccess('Contribution', 'create', $this->_params);
304
305 $params = array(
306 'id' => $contribution['id'],
307 'check_permissions' => TRUE,
308 'total_amount' => 200.00,
309 );
5be22f39 310 $config = CRM_Core_Config::singleton();
be23f122
E
311 $config->userPermissionClass->permissions = array(
312 'access CiviCRM',
313 'access CiviContribute',
314 'edit contributions',
b8f2d49b 315 'view contributions of type Donation',
be23f122 316 );
5be22f39 317 $this->callAPIFailure('Contribution', 'create', $params);
9716cd7c 318
f4a331f4 319 $config->userPermissionClass->permissions[] = 'edit contributions of type Donation';
be23f122
E
320 $contribution = $this->callAPISuccess('Contribution', 'create', $params);
321
322 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 200.00);
323 }
324
325 /**
326 * Test that acl contributions can be deleted.
327 */
328 public function testDeleteACLContribution() {
329 $this->setACL();
5be22f39 330 $config = CRM_Core_Config::singleton();
50d8cef8
PN
331 $config->userPermissionClass->permissions = array(
332 'access CiviCRM',
333 'access CiviContribute',
334 'view all contacts',
335 'add contributions of type Donation',
336 );
be23f122
E
337 $contribution = $this->callAPISuccess('Contribution', 'create', $this->_params);
338
339 $params = array(
340 'contribution_id' => $contribution['id'],
341 'check_permissions' => TRUE,
342 );
50d8cef8 343 $config->userPermissionClass->permissions[3] = 'delete in CiviContribute';
5be22f39 344 $this->callAPIFailure('Contribution', 'delete', $params);
9716cd7c 345
50d8cef8 346 $config->userPermissionClass->permissions[] = 'delete contributions of type Donation';
be23f122 347 $contribution = $this->callAPISuccess('Contribution', 'delete', $params);
3007d344 348
be23f122
E
349 $this->assertEquals($contribution['count'], 1);
350 }
3007d344 351
147f0967 352}