3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
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 +--------------------------------------------------------------------+
12 use Civi\Api4\MembershipType
;
15 * Class CRM_Financial_BAO_FinancialTypeTest
18 class CRM_Financial_BAO_FinancialTypeTest
extends CiviUnitTestCase
{
20 public function setUp(): void
{
22 $this->_orgContactID
= $this->organizationCreate();
25 public function tearDown(): void
{
28 CRM_Core_I18n_Schema
::makeSinglelingual('en_US');
30 $this->financialAccountDelete('Donations');
37 public function testAdd() {
39 'name' => 'Donations',
45 $financialType = CRM_Financial_BAO_FinancialType
::add($params, $ids);
46 $result = $this->assertDBNotNull(
47 'CRM_Financial_DAO_FinancialType',
51 'Database check on added financial type record.'
53 $this->assertEquals($result, 'Donations', 'Verify Name for Financial Type');
57 * Check method retrieve().
59 public function testRetrieve() {
61 'name' => 'Donations',
68 CRM_Financial_BAO_FinancialType
::add($params, $ids);
71 $result = CRM_Financial_BAO_FinancialType
::retrieve($params, $defaults);
72 $this->assertEquals($result->name
, 'Donations', 'Verify Name for Financial Type');
76 * Check method setIsActive()
78 public function testSetIsActive() {
80 'name' => 'Donations',
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',
93 'Database check on updated for financial type is_active.'
95 $this->assertEquals($isActive, 0, 'Verify financial types is_active.');
99 * Data provider for testGitLabIssue1108
101 * First we run it without multiLingual mode, then with.
103 * This is because we test table names, which may have been translated in a
104 * multiLingual context.
107 public function multiLingual() {
114 * @dataProvider multiLingual
116 public function testDel($isMultiLingual) {
117 if ($isMultiLingual) {
118 $this->enableMultilingual();
119 CRM_Core_I18n_Schema
::addLocale('fr_FR', 'en_US');
122 'name' => 'Donations',
123 'is_deductible' => 0,
127 $financialType = CRM_Financial_BAO_FinancialType
::add($params, $ids);
129 if ($isMultiLingual) {
131 $dbLocale = '_fr_FR';
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) {
141 $dbLocale = '_en_US';
146 * Set ACLs for Financial Types()
148 public function setACL() {
149 Civi
::settings()->set('acl_financial_type', 1);
153 * Check method testGetAvailableFinancialTypes()
155 public function testGetAvailableFinancialTypes() {
157 $this->setPermissions([
158 'view contributions of type Donation',
159 'view contributions of type Member Dues',
162 CRM_Financial_BAO_FinancialType
::getAvailableFinancialTypes($types);
167 $this->assertEquals($expectedResult, $types, 'Verify that only certain financial types can be retrieved');
169 $this->setPermissions([
170 'view contributions of type Donation',
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');
178 * Check method test getAvailableMembershipTypes()
180 * @throws \API_Exception
182 public function testGetAvailableMembershipTypes(): void
{
183 // Create Membership types
185 'name' => 'Type One',
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',
196 MembershipType
::create()->setValues($params)->execute();
198 $params['name'] = 'Type Two';
199 $params['financial_type_id'] = 2;
200 MembershipType
::create()->setValues($params)->execute();
204 $this->setPermissions([
205 'view contributions of type Donation',
206 'view contributions of type Member Dues',
208 CRM_Financial_BAO_FinancialType
::getAvailableMembershipTypes($types);
213 $this->assertEquals($expectedResult, $types, 'Verify that only certain membership types can be retrieved');
214 $this->setPermissions([
215 'view contributions of type Donation',
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');
223 * Check method testcheckPermissionedLineItems()
225 * @throws \CRM_Core_Exception
226 * @throws \CiviCRM_API3_Exception
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;
236 $priceset = CRM_Price_BAO_PriceSet
::create($paramsSet);
237 $priceSetId = $priceset->id
;
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'
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,
254 'options_per_line' => 1,
255 'is_active' => ['1' => 1, '2' => 1],
256 'price_set_id' => $priceset->id
,
258 'financial_type_id' => 1,
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,
265 'contact_id' => $contactId,
266 'financial_type_id' => 1,
267 'contribution_status_id' => 1,
268 'skipCleanMoney' => TRUE,
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'],
278 'unit_price' => $priceField['amount'],
279 'line_total' => $priceField['amount'],
280 'financial_type_id' => $priceField['financial_type_id'],
283 $contributionParams['line_item'] = $lineItems;
284 $contributions = CRM_Contribute_BAO_Contribution
::create($contributionParams);
285 CRM_Financial_BAO_FinancialType
::$_statusACLFt = [];
288 $this->setPermissions([
289 'view contributions of type Member Dues',
293 CRM_Financial_BAO_FinancialType
::checkPermissionedLineItems($contributions->id
, 'view');
294 $this->fail('Missed expected exception');
296 catch (CRM_Core_Exception
$e) {
297 $this->assertEquals('You do not have permission to access this page.', $e->getMessage());
300 $this->setPermissions([
301 'view contributions of type Donation',
303 $perm = CRM_Financial_BAO_FinancialType
::checkPermissionedLineItems($contributions->id
, 'view');
304 $this->assertEquals($perm, TRUE, 'Verify that lineitems now have permission.');