Merge pull request #14833 from seamuslee001/ids_ip_logging_improvements
[civicrm-core.git] / tests / phpunit / api / v3 / FinancialTypeACLTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
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 use CRMTraits_Financial_FinancialACLTrait;
37
38 protected $_individualId;
39 protected $_contribution;
40 protected $_financialTypeId = 1;
41 protected $_apiversion;
42 protected $_entity = 'Contribution';
43 public $debug = 0;
44 protected $_params;
45 protected $_ids = [];
46 protected $_pageParams = [];
47
48 /**
49 * Parameters to create payment processor.
50 *
51 * @var array
52 */
53 protected $_processorParams = [];
54
55 /**
56 * ID of created event.
57 *
58 * @var int
59 */
60 protected $_eventID;
61
62 /**
63 * Setup function.
64 */
65 public function setUp() {
66 parent::setUp();
67
68 $this->_apiversion = 3;
69 $this->_individualId = $this->individualCreate();
70 $this->_params = [
71 'contact_id' => $this->_individualId,
72 'receive_date' => '20120511',
73 'total_amount' => 100.00,
74 'financial_type_id' => $this->_financialTypeId,
75 'non_deductible_amount' => 10.00,
76 'fee_amount' => 5.00,
77 'net_amount' => 95.00,
78 'source' => 'SSF',
79 'contribution_status_id' => 1,
80 ];
81 $this->_processorParams = [
82 'domain_id' => 1,
83 'name' => 'Dummy',
84 'payment_processor_type_id' => 10,
85 'financial_account_id' => 12,
86 'is_active' => 1,
87 'user_name' => '',
88 'url_site' => 'http://dummy.com',
89 'url_recur' => 'http://dummy.com',
90 'billing_mode' => 1,
91 ];
92 $this->_pageParams = [
93 'title' => 'Test Contribution Page',
94 'financial_type_id' => 1,
95 'currency' => 'USD',
96 'financial_account_id' => 1,
97 'payment_processor' => $this->processorCreate(),
98 'is_active' => 1,
99 'is_allow_other_amount' => 1,
100 'min_amount' => 10,
101 'max_amount' => 1000,
102 ];
103 }
104
105 /**
106 * Clean up after each test.
107 *
108 * @throws \Exception
109 */
110 public function tearDown() {
111 $this->quickCleanUpFinancialEntities();
112 $this->quickCleanup(['civicrm_uf_match']);
113 $this->disableFinancialACLs();
114 parent::tearDown();
115 }
116
117 /**
118 * Test Get.
119 */
120 public function testCreateACLContribution() {
121 $this->enableFinancialACLs();
122 $p = [
123 'contact_id' => $this->_individualId,
124 'receive_date' => '2010-01-20',
125 'total_amount' => 100.00,
126 'financial_type_id' => $this->_financialTypeId,
127 'non_deductible_amount' => 10.00,
128 'fee_amount' => 5.00,
129 'net_amount' => 95.00,
130 'trxn_id' => 23456,
131 'invoice_id' => 78910,
132 'source' => 'SSF',
133 'contribution_status_id' => 1,
134 'check_permissions' => TRUE,
135 ];
136
137 $this->setPermissions([
138 'access CiviCRM',
139 'access CiviContribute',
140 'edit contributions',
141 ]);
142 $result = $this->callAPIFailure('contribution', 'create', $p);
143 $this->assertEquals('You do not have permission to create this contribution', $result['error_message']);
144 $this->addFinancialAclPermissions([['add', 'Donation']]);
145
146 $contribution = $this->callAPISuccess('contribution', 'create', $p);
147
148 $params = [
149 'contribution_id' => $contribution['id'],
150 ];
151
152 $this->setPermissions([
153 'access CiviCRM',
154 'access CiviContribute',
155 'edit contributions',
156 'view contributions of type Donation',
157 'delete contributions of type Donation',
158 ]);
159
160 $contribution = $this->callAPISuccess('contribution', 'get', $params);
161
162 $this->assertEquals(1, $contribution['count']);
163 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId);
164 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'], 1);
165 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 100.00);
166 $this->assertEquals($contribution['values'][$contribution['id']]['non_deductible_amount'], 10.00);
167 $this->assertEquals($contribution['values'][$contribution['id']]['fee_amount'], 5.00);
168 $this->assertEquals($contribution['values'][$contribution['id']]['net_amount'], 95.00);
169 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 23456);
170 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 78910);
171 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_source'], 'SSF');
172 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status'], 'Completed');
173 $this->callAPISuccess('Contribution', 'Delete', [
174 'id' => $contribution['id'],
175 ]);
176 }
177
178 /**
179 * Test that acl contributions can be retrieved.
180 */
181 public function testGetACLContribution() {
182 $this->enableFinancialACLs();
183
184 $this->setPermissions([
185 'access CiviCRM',
186 'access CiviContribute',
187 'view all contacts',
188 'add contributions of type Donation',
189 ]);
190 $contribution = $this->callAPISuccess('Contribution', 'create', $this->_params);
191 $this->callAPISuccess('Contribution', 'create', array_merge($this->_params, ['financial_type_id' => 'Member Dues']));
192
193 $params = [
194 'id' => $contribution['id'],
195 'check_permissions' => TRUE,
196 ];
197 $contribution = $this->callAPISuccess('contribution', 'get', $params);
198 $this->assertEquals($contribution['count'], 0);
199
200 $this->addFinancialAclPermissions([['view', 'Donation']]);
201 $this->callAPISuccessGetSingle('contribution', $params);
202 $this->callAPISuccessGetCount('contribution', ['financial_type_id' => 'Member Dues', 'check_permissions' => 1], 0);
203 $this->callAPISuccessGetCount('contribution', ['financial_type_id' => 'Member Dues'], 1);
204 }
205
206 /**
207 * Test checks that passing in line items suppresses the create mechanism.
208 */
209 public function testCreateACLContributionChainedLineItems() {
210 $this->enableFinancialACLs();
211 $params = [
212 'contact_id' => $this->_individualId,
213 'receive_date' => '20120511',
214 'total_amount' => 100.00,
215 'financial_type_id' => $this->_financialTypeId,
216 'payment_instrument_id' => 1,
217 'non_deductible_amount' => 10.00,
218 'fee_amount' => 50.00,
219 'net_amount' => 90.00,
220 'source' => 'SSF',
221 'contribution_status_id' => 1,
222 'check_permissions' => TRUE,
223 'api.line_item.create' => [
224 [
225 'price_field_id' => 1,
226 'qty' => 2,
227 'line_total' => '20',
228 'unit_price' => '10',
229 'financial_type_id' => 1,
230 ],
231 [
232 'price_field_id' => 1,
233 'qty' => 1,
234 'line_total' => '80',
235 'unit_price' => '80',
236 'financial_type_id' => 2,
237 ],
238 ],
239 ];
240
241 $this->setPermissions([
242 'access CiviCRM',
243 'access CiviContribute',
244 'edit contributions',
245 'delete in CiviContribute',
246 'add contributions of type Donation',
247 'delete contributions of type Donation',
248 ]);
249 $this->callAPIFailure('contribution', 'create', $params, 'Error in call to LineItem_create : You do not have permission to create this line item');
250
251 // Check that the entire contribution has rolled back.
252 $contribution = $this->callAPISuccess('contribution', 'get', []);
253 $this->assertEquals(0, $contribution['count']);
254
255 $this->addFinancialAclPermissions([
256 ['add', 'Member Dues'],
257 ['view', 'Donation'],
258 ['view', 'Member Dues'],
259 ['delete', 'Member Dues'],
260 ]);
261 $contribution = $this->callAPISuccess('contribution', 'create', $params);
262
263 $lineItemParams = [
264 'contribution_id' => $contribution['id'],
265 'entity_table' => 'civicrm_contribution',
266 ];
267 $lineItems = $this->callAPISuccess('LineItem', 'get', $lineItemParams);
268 $this->assertEquals(3, $lineItems['count']);
269 $this->assertEquals(100.00, $lineItems['values'][3]['line_total']);
270 $this->assertEquals(20, $lineItems['values'][4]['line_total']);
271 $this->assertEquals(80, $lineItems['values'][5]['line_total']);
272 $this->assertEquals(1, $lineItems['values'][3]['financial_type_id']);
273 $this->assertEquals(1, $lineItems['values'][4]['financial_type_id']);
274 $this->assertEquals(2, $lineItems['values'][5]['financial_type_id']);
275
276 $this->callAPISuccess('Contribution', 'Delete', [
277 'id' => $contribution['id'],
278 ]);
279 }
280
281 /**
282 * Test that acl contributions can be edited.
283 */
284 public function testEditACLContribution() {
285 $this->enableFinancialACLs();
286 $contribution = $this->callAPISuccess('Contribution', 'create', $this->_params);
287
288 $params = [
289 'id' => $contribution['id'],
290 'check_permissions' => TRUE,
291 'total_amount' => 200.00,
292 ];
293
294 $this->setPermissions([
295 'access CiviCRM',
296 'access CiviContribute',
297 'edit contributions',
298 'view contributions of type Donation',
299 ]);
300 $this->callAPIFailure('Contribution', 'create', $params);
301
302 $this->addFinancialAclPermissions([['edit', 'Donation']]);
303 $contribution = $this->callAPISuccess('Contribution', 'create', $params);
304
305 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 200.00);
306 }
307
308 /**
309 * Test that acl contributions can be deleted.
310 */
311 public function testDeleteACLContribution() {
312 $this->enableFinancialACLs();
313
314 $this->setPermissions([
315 'access CiviCRM',
316 'access CiviContribute',
317 'view all contacts',
318 'add contributions of type Donation',
319 ]);
320 $contribution = $this->callAPISuccess('Contribution', 'create', $this->_params);
321
322 $params = [
323 'contribution_id' => $contribution['id'],
324 'check_permissions' => TRUE,
325 ];
326 $this->addPermissions(['delete in CiviContribute']);
327 $this->callAPIFailure('Contribution', 'delete', $params);
328
329 $this->addFinancialAclPermissions([['delete', 'Donation']]);
330 $contribution = $this->callAPISuccess('Contribution', 'delete', $params);
331
332 $this->assertEquals($contribution['count'], 1);
333 }
334
335 }