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