Merge pull request #21945 from masetto/profile-data-attribute
[civicrm-core.git] / tests / phpunit / CRM / Financial / BAO / FinancialTypeTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
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 |
9 +--------------------------------------------------------------------+
10 */
11
12 use Civi\Api4\MembershipType;
13
14 /**
15 * Class CRM_Financial_BAO_FinancialTypeTest
16 * @group headless
17 */
18 class CRM_Financial_BAO_FinancialTypeTest extends CiviUnitTestCase {
19
20 public function setUp(): void {
21 parent::setUp();
22 $this->_orgContactID = $this->organizationCreate();
23 }
24
25 public function tearDown(): void {
26 global $dbLocale;
27 if ($dbLocale) {
28 CRM_Core_I18n_Schema::makeSinglelingual('en_US');
29 }
30 $this->financialAccountDelete('Donations');
31 parent::tearDown();
32 }
33
34 /**
35 * Check method add().
36 */
37 public function testAdd() {
38 $params = [
39 'name' => 'Donations',
40 'is_active' => 1,
41 'is_deductible' => 1,
42 'is_reserved' => 1,
43 ];
44 $ids = [];
45 $financialType = CRM_Financial_BAO_FinancialType::add($params, $ids);
46 $result = $this->assertDBNotNull(
47 'CRM_Financial_DAO_FinancialType',
48 $financialType->id,
49 'name',
50 'id',
51 'Database check on added financial type record.'
52 );
53 $this->assertEquals($result, 'Donations', 'Verify Name for Financial Type');
54 }
55
56 /**
57 * Check method retrieve().
58 */
59 public function testRetrieve() {
60 $params = [
61 'name' => 'Donations',
62 'is_active' => 1,
63 'is_deductible' => 1,
64 'is_reserved' => 1,
65 ];
66
67 $ids = [];
68 CRM_Financial_BAO_FinancialType::add($params, $ids);
69
70 $defaults = [];
71 $result = CRM_Financial_BAO_FinancialType::retrieve($params, $defaults);
72 $this->assertEquals($result->name, 'Donations', 'Verify Name for Financial Type');
73 }
74
75 /**
76 * Check method setIsActive()
77 */
78 public function testSetIsActive() {
79 $params = [
80 'name' => 'Donations',
81 'is_deductible' => 0,
82 'is_active' => 1,
83 ];
84 $ids = [];
85 $financialType = CRM_Financial_BAO_FinancialType::add($params, $ids);
86 $result = CRM_Financial_BAO_FinancialType::setIsActive($financialType->id, 0);
87 $this->assertEquals($result, TRUE, 'Verify financial type record updation for is_active.');
88 $isActive = $this->assertDBNotNull(
89 'CRM_Financial_DAO_FinancialType',
90 $financialType->id,
91 'is_active',
92 'id',
93 'Database check on updated for financial type is_active.'
94 );
95 $this->assertEquals($isActive, 0, 'Verify financial types is_active.');
96 }
97
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
111 /**
112 * Check method del()
113 *
114 * @dataProvider multiLingual
115 */
116 public function testDel($isMultiLingual) {
117 if ($isMultiLingual) {
118 $this->enableMultilingual();
119 CRM_Core_I18n_Schema::addLocale('fr_FR', 'en_US');
120 }
121 $params = [
122 'name' => 'Donations',
123 'is_deductible' => 0,
124 'is_active' => 1,
125 ];
126 $ids = [];
127 $financialType = CRM_Financial_BAO_FinancialType::add($params, $ids);
128
129 if ($isMultiLingual) {
130 global $dbLocale;
131 $dbLocale = '_fr_FR';
132 }
133 CRM_Financial_BAO_FinancialType::del($financialType->id);
134 $params = ['id' => $financialType->id];
135 $result = CRM_Financial_BAO_FinancialType::retrieve($params, $defaults);
136 $this->assertEquals(empty($result), TRUE, 'Verify financial types record deletion.');
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 }
143 }
144
145 /**
146 * Set ACLs for Financial Types()
147 */
148 public function setACL() {
149 Civi::settings()->set('acl_financial_type', 1);
150 }
151
152 /**
153 * Check method testGetAvailableFinancialTypes()
154 */
155 public function testGetAvailableFinancialTypes() {
156 $this->setACL();
157 $this->setPermissions([
158 'view contributions of type Donation',
159 'view contributions of type Member Dues',
160 ]);
161 $types = [];
162 CRM_Financial_BAO_FinancialType::getAvailableFinancialTypes($types);
163 $expectedResult = [
164 1 => 'Donation',
165 2 => 'Member Dues',
166 ];
167 $this->assertEquals($expectedResult, $types, 'Verify that only certain financial types can be retrieved');
168
169 $this->setPermissions([
170 'view contributions of type Donation',
171 ]);
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 /**
178 * Check method test getAvailableMembershipTypes()
179 *
180 * @throws \API_Exception
181 */
182 public function testGetAvailableMembershipTypes(): void {
183 // Create Membership types
184 $params = [
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,
195 ];
196 MembershipType::create()->setValues($params)->execute();
197 // Add another
198 $params['name'] = 'Type Two';
199 $params['financial_type_id'] = 2;
200 MembershipType::create()->setValues($params)->execute();
201
202 $this->setACL();
203
204 $this->setPermissions([
205 'view contributions of type Donation',
206 'view contributions of type Member Dues',
207 ]);
208 CRM_Financial_BAO_FinancialType::getAvailableMembershipTypes($types);
209 $expectedResult = [
210 1 => "Type One",
211 2 => "Type Two",
212 ];
213 $this->assertEquals($expectedResult, $types, 'Verify that only certain membership types can be retrieved');
214 $this->setPermissions([
215 'view contributions of type Donation',
216 ]);
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 }
221
222 /**
223 * Check method testcheckPermissionedLineItems()
224 *
225 * @throws \CRM_Core_Exception
226 * @throws \CiviCRM_API3_Exception
227 */
228 public function testCheckPermissionedLineItems() {
229 $contactId = $this->individualCreate();
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 );
243 $paramsField = [
244 'label' => 'Price Field',
245 'name' => CRM_Utils_String::titleToVar('Price Field'),
246 'html_type' => 'CheckBox',
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],
252 'is_display_amounts' => 1,
253 'weight' => 1,
254 'options_per_line' => 1,
255 'is_active' => ['1' => 1, '2' => 1],
256 'price_set_id' => $priceset->id,
257 'is_enter_qty' => 1,
258 'financial_type_id' => 1,
259 ];
260 $priceField = CRM_Price_BAO_PriceField::create($paramsField);
261 $priceFields = $this->callAPISuccess('PriceFieldValue', 'get', ['price_field_id' => $priceField->id]);
262 $contributionParams = [
263 'total_amount' => 300,
264 'currency' => 'USD',
265 'contact_id' => $contactId,
266 'financial_type_id' => 1,
267 'contribution_status_id' => 1,
268 'skipCleanMoney' => TRUE,
269 ];
270
271 foreach ($priceFields['values'] as $key => $priceField) {
272 $lineItems[1][$key] = [
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'],
281 ];
282 }
283 $contributionParams['line_item'] = $lineItems;
284 $contributions = CRM_Contribute_BAO_Contribution::create($contributionParams);
285 CRM_Financial_BAO_FinancialType::$_statusACLFt = [];
286 $this->setACL();
287
288 $this->setPermissions([
289 'view contributions of type Member Dues',
290 ]);
291
292 try {
293 CRM_Financial_BAO_FinancialType::checkPermissionedLineItems($contributions->id, 'view');
294 $this->fail('Missed expected exception');
295 }
296 catch (CRM_Core_Exception $e) {
297 $this->assertEquals('You do not have permission to access this page.', $e->getMessage());
298 }
299
300 $this->setPermissions([
301 'view contributions of type Donation',
302 ]);
303 $perm = CRM_Financial_BAO_FinancialType::checkPermissionedLineItems($contributions->id, 'view');
304 $this->assertEquals($perm, TRUE, 'Verify that lineitems now have permission.');
305 }
306
307 }