Merge pull request #23467 from christianwach/event-caps
[civicrm-core.git] / tests / phpunit / CRM / Financial / BAO / FinancialTypeTest.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
7d61e75f 4 | Copyright CiviCRM LLC. All rights reserved. |
6a488035 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 |
6a488035 9 +--------------------------------------------------------------------+
d25dd0ee 10 */
6a488035 11
722682e0
EM
12use Civi\Api4\MembershipType;
13
e9479dcf
EM
14/**
15 * Class CRM_Financial_BAO_FinancialTypeTest
acb109b7 16 * @group headless
e9479dcf 17 */
6a488035
TO
18class CRM_Financial_BAO_FinancialTypeTest extends CiviUnitTestCase {
19
0b49aa04 20 public function setUp(): void {
6a488035 21 parent::setUp();
da3709a1 22 $this->_orgContactID = $this->organizationCreate();
6a488035
TO
23 }
24
dd09ee0c 25 public function tearDown(): void {
02e9e315
SL
26 global $dbLocale;
27 if ($dbLocale) {
28 CRM_Core_I18n_Schema::makeSinglelingual('en_US');
29 }
f17d75bb 30 $this->financialAccountDelete('Donations');
bc4d424d 31 parent::tearDown();
f17d75bb
PN
32 }
33
6a488035 34 /**
8d35246a 35 * Check method add().
6a488035 36 */
00be9182 37 public function testAdd() {
9099cab3 38 $params = [
6a488035
TO
39 'name' => 'Donations',
40 'is_active' => 1,
41 'is_deductible' => 1,
42 'is_reserved' => 1,
9099cab3
CW
43 ];
44 $ids = [];
6a488035
TO
45 $financialType = CRM_Financial_BAO_FinancialType::add($params, $ids);
46 $result = $this->assertDBNotNull(
47 'CRM_Financial_DAO_FinancialType',
6c6e6187 48 $financialType->id,
6a488035
TO
49 'name',
50 'id',
51 'Database check on added financial type record.'
52 );
481a74f4 53 $this->assertEquals($result, 'Donations', 'Verify Name for Financial Type');
6a488035
TO
54 }
55
56 /**
8d35246a 57 * Check method retrieve().
6a488035 58 */
00be9182 59 public function testRetrieve() {
9099cab3 60 $params = [
6a488035
TO
61 'name' => 'Donations',
62 'is_active' => 1,
63 'is_deductible' => 1,
64 'is_reserved' => 1,
9099cab3 65 ];
6a488035 66
9099cab3 67 $ids = [];
6a488035
TO
68 CRM_Financial_BAO_FinancialType::add($params, $ids);
69
9099cab3 70 $defaults = [];
6a488035 71 $result = CRM_Financial_BAO_FinancialType::retrieve($params, $defaults);
f17d75bb 72 $this->assertEquals($result->name, 'Donations', 'Verify Name for Financial Type');
6a488035
TO
73 }
74
75 /**
100fef9d 76 * Check method setIsActive()
6a488035 77 */
00be9182 78 public function testSetIsActive() {
9099cab3 79 $params = [
f17d75bb 80 'name' => 'Donations',
6a488035
TO
81 'is_deductible' => 0,
82 'is_active' => 1,
9099cab3
CW
83 ];
84 $ids = [];
6a488035
TO
85 $financialType = CRM_Financial_BAO_FinancialType::add($params, $ids);
86 $result = CRM_Financial_BAO_FinancialType::setIsActive($financialType->id, 0);
6c6e6187 87 $this->assertEquals($result, TRUE, 'Verify financial type record updation for is_active.');
6a488035
TO
88 $isActive = $this->assertDBNotNull(
89 'CRM_Financial_DAO_FinancialType',
6c6e6187 90 $financialType->id,
6a488035
TO
91 'is_active',
92 'id',
93 'Database check on updated for financial type is_active.'
94 );
f17d75bb 95 $this->assertEquals($isActive, 0, 'Verify financial types is_active.');
6a488035
TO
96 }
97
02e9e315
SL
98 /**
99 * Data provider for testGitLabIssue1108
100 *
101 * First we run it without multiLingual mode, then with.
102 *
103 * This is because we test table names, which may have been translated in a
104 * multiLingual context.
105 *
106 */
107 public function multiLingual() {
108 return [[0], [1]];
109 }
110
6a488035 111 /**
100fef9d 112 * Check method del()
02e9e315
SL
113 *
114 * @dataProvider multiLingual
6a488035 115 */
02e9e315
SL
116 public function testDel($isMultiLingual) {
117 if ($isMultiLingual) {
118 $this->enableMultilingual();
119 CRM_Core_I18n_Schema::addLocale('fr_FR', 'en_US');
120 }
9099cab3 121 $params = [
f17d75bb 122 'name' => 'Donations',
6a488035
TO
123 'is_deductible' => 0,
124 'is_active' => 1,
9099cab3
CW
125 ];
126 $ids = [];
6a488035
TO
127 $financialType = CRM_Financial_BAO_FinancialType::add($params, $ids);
128
02e9e315
SL
129 if ($isMultiLingual) {
130 global $dbLocale;
131 $dbLocale = '_fr_FR';
132 }
6a488035 133 CRM_Financial_BAO_FinancialType::del($financialType->id);
9099cab3 134 $params = ['id' => $financialType->id];
6a488035 135 $result = CRM_Financial_BAO_FinancialType::retrieve($params, $defaults);
6c6e6187 136 $this->assertEquals(empty($result), TRUE, 'Verify financial types record deletion.');
02e9e315
SL
137 $results = CRM_Core_DAO::executeQuery("SELECT * FROM civicrm_entity_financial_account WHERE entity_id = %1", [1 => [$financialType->id, 'Positive']])->fetchAll();
138 $this->assertEquals(empty($results), TRUE, 'Assert related entity financial account has been deleted as well');
139 if ($isMultiLingual) {
140 global $dbLocale;
141 $dbLocale = '_en_US';
142 }
6a488035 143 }
96025800 144
da3709a1
E
145 /**
146 * Set ACLs for Financial Types()
147 */
148 public function setACL() {
b6588f6a 149 Civi::settings()->set('acl_financial_type', 1);
da3709a1
E
150 }
151
152 /**
8d35246a 153 * Check method testGetAvailableFinancialTypes()
da3709a1 154 */
8d35246a 155 public function testGetAvailableFinancialTypes() {
da3709a1 156 $this->setACL();
9099cab3 157 $this->setPermissions([
da3709a1
E
158 'view contributions of type Donation',
159 'view contributions of type Member Dues',
9099cab3
CW
160 ]);
161 $types = [];
da3709a1 162 CRM_Financial_BAO_FinancialType::getAvailableFinancialTypes($types);
9099cab3 163 $expectedResult = [
87e130bb
EM
164 1 => 'Donation',
165 2 => 'Member Dues',
9099cab3 166 ];
da3709a1 167 $this->assertEquals($expectedResult, $types, 'Verify that only certain financial types can be retrieved');
8d35246a 168
9099cab3 169 $this->setPermissions([
da3709a1 170 'view contributions of type Donation',
9099cab3 171 ]);
da3709a1
E
172 unset($expectedResult[2]);
173 CRM_Financial_BAO_FinancialType::getAvailableFinancialTypes($types);
174 $this->assertEquals($expectedResult, $types, 'Verify that removing permission for a financial type restricts the available financial types');
175 }
176
177 /**
722682e0
EM
178 * Check method test getAvailableMembershipTypes()
179 *
180 * @throws \API_Exception
da3709a1 181 */
722682e0 182 public function testGetAvailableMembershipTypes(): void {
da3709a1 183 // Create Membership types
9099cab3 184 $params = [
da3709a1
E
185 'name' => 'Type One',
186 'domain_id' => 1,
187 'minimum_fee' => 10,
188 'duration_unit' => 'year',
189 'member_of_contact_id' => $this->_orgContactID,
190 'period_type' => 'fixed',
191 'duration_interval' => 1,
192 'financial_type_id' => 1,
193 'visibility' => 'Public',
194 'is_active' => 1,
9099cab3 195 ];
722682e0 196 MembershipType::create()->setValues($params)->execute();
da3709a1
E
197 // Add another
198 $params['name'] = 'Type Two';
199 $params['financial_type_id'] = 2;
722682e0 200 MembershipType::create()->setValues($params)->execute();
da3709a1
E
201
202 $this->setACL();
8d35246a 203
9099cab3 204 $this->setPermissions([
da3709a1
E
205 'view contributions of type Donation',
206 'view contributions of type Member Dues',
9099cab3 207 ]);
da3709a1 208 CRM_Financial_BAO_FinancialType::getAvailableMembershipTypes($types);
9099cab3 209 $expectedResult = [
da3709a1
E
210 1 => "Type One",
211 2 => "Type Two",
9099cab3 212 ];
da3709a1 213 $this->assertEquals($expectedResult, $types, 'Verify that only certain membership types can be retrieved');
9099cab3 214 $this->setPermissions([
da3709a1 215 'view contributions of type Donation',
9099cab3 216 ]);
da3709a1
E
217 unset($expectedResult[2]);
218 CRM_Financial_BAO_FinancialType::getAvailableMembershipTypes($types);
219 $this->assertEquals($expectedResult, $types, 'Verify that removing permission for a financial type restricts the available membership types');
220 }
0f6eb8c9 221
bf01d873
E
222 /**
223 * Check method testcheckPermissionedLineItems()
beb414cc 224 *
225 * @throws \CRM_Core_Exception
226 * @throws \CiviCRM_API3_Exception
bf01d873 227 */
beb414cc 228 public function testCheckPermissionedLineItems() {
6ae19242 229 $contactId = $this->individualCreate();
bf01d873
E
230 $paramsSet['title'] = 'Price Set' . substr(sha1(rand()), 0, 4);
231 $paramsSet['name'] = CRM_Utils_String::titleToVar($paramsSet['title']);
232 $paramsSet['is_active'] = TRUE;
233 $paramsSet['financial_type_id'] = 1;
234 $paramsSet['extends'] = 1;
235
236 $priceset = CRM_Price_BAO_PriceSet::create($paramsSet);
237 $priceSetId = $priceset->id;
238
239 //Checking for priceset added in the table.
240 $this->assertDBCompareValue('CRM_Price_BAO_PriceSet', $priceSetId, 'title',
241 'id', $paramsSet['title'], 'Check DB for created priceset'
242 );
9099cab3 243 $paramsField = [
bf01d873
E
244 'label' => 'Price Field',
245 'name' => CRM_Utils_String::titleToVar('Price Field'),
246 'html_type' => 'CheckBox',
9099cab3
CW
247 'option_label' => ['1' => 'Price Field 1', '2' => 'Price Field 2'],
248 'option_value' => ['1' => 100, '2' => 200],
249 'option_name' => ['1' => 'Price Field 1', '2' => 'Price Field 2'],
250 'option_weight' => ['1' => 1, '2' => 2],
251 'option_amount' => ['1' => 100, '2' => 200],
bf01d873
E
252 'is_display_amounts' => 1,
253 'weight' => 1,
254 'options_per_line' => 1,
9099cab3 255 'is_active' => ['1' => 1, '2' => 1],
bf01d873
E
256 'price_set_id' => $priceset->id,
257 'is_enter_qty' => 1,
258 'financial_type_id' => 1,
9099cab3 259 ];
bf01d873 260 $priceField = CRM_Price_BAO_PriceField::create($paramsField);
9099cab3
CW
261 $priceFields = $this->callAPISuccess('PriceFieldValue', 'get', ['price_field_id' => $priceField->id]);
262 $contributionParams = [
bf01d873
E
263 'total_amount' => 300,
264 'currency' => 'USD',
265 'contact_id' => $contactId,
266 'financial_type_id' => 1,
267 'contribution_status_id' => 1,
d2da7b20 268 'skipCleanMoney' => TRUE,
9099cab3 269 ];
bf01d873
E
270
271 foreach ($priceFields['values'] as $key => $priceField) {
9099cab3 272 $lineItems[1][$key] = [
bf01d873
E
273 'price_field_id' => $priceField['price_field_id'],
274 'price_field_value_id' => $priceField['id'],
275 'label' => $priceField['label'],
276 'field_title' => $priceField['label'],
277 'qty' => 1,
278 'unit_price' => $priceField['amount'],
279 'line_total' => $priceField['amount'],
280 'financial_type_id' => $priceField['financial_type_id'],
9099cab3 281 ];
bf01d873
E
282 }
283 $contributionParams['line_item'] = $lineItems;
284 $contributions = CRM_Contribute_BAO_Contribution::create($contributionParams);
9099cab3 285 CRM_Financial_BAO_FinancialType::$_statusACLFt = [];
bf01d873 286 $this->setACL();
8d35246a 287
9099cab3 288 $this->setPermissions([
bf01d873 289 'view contributions of type Member Dues',
9099cab3 290 ]);
bf01d873
E
291
292 try {
8d35246a 293 CRM_Financial_BAO_FinancialType::checkPermissionedLineItems($contributions->id, 'view');
beb414cc 294 $this->fail('Missed expected exception');
bf01d873 295 }
ba968e38 296 catch (CRM_Core_Exception $e) {
297 $this->assertEquals('You do not have permission to access this page.', $e->getMessage());
bf01d873 298 }
8d35246a 299
9099cab3 300 $this->setPermissions([
bf01d873 301 'view contributions of type Donation',
9099cab3 302 ]);
bf01d873
E
303 $perm = CRM_Financial_BAO_FinancialType::checkPermissionedLineItems($contributions->id, 'view');
304 $this->assertEquals($perm, TRUE, 'Verify that lineitems now have permission.');
305 }
306
232624b1 307}