Merge pull request #13289 from mfb/pear-mail
[civicrm-core.git] / tests / phpunit / CRMTraits / Financial / FinancialACLTrait.php
CommitLineData
dbaa9d7d 1<?php
2/*
3 +--------------------------------------------------------------------+
2fe49090 4 | CiviCRM version 5 |
dbaa9d7d 5 +--------------------------------------------------------------------+
6b83d5bd 6 | Copyright CiviCRM LLC (c) 2004-2019 |
dbaa9d7d 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 * Trait FinancialACLTrait
30 *
31 * Trait for working with Financial ACLs in tests
32 */
33trait CRMTraits_Financial_FinancialACLTrait {
34
35 /**
36 * Enable financial ACLs.
37 */
38 protected function enableFinancialACLs() {
39 $contributeSettings = Civi::settings()->get('contribution_invoice_settings');
40 $this->callAPISuccess('Setting', 'create', [
2fc43bbd 41 'contribution_invoice_settings' => array_merge($contributeSettings, ['acl_financial_type' => TRUE]),
42 'acl_financial_type' => TRUE,
dbaa9d7d 43 ]);
44 unset(\Civi::$statics['CRM_Financial_BAO_FinancialType']);
45 }
46
47 /**
48 * Disable financial ACLs.
49 */
50 protected function disableFinancialACLs() {
51 $contributeSettings = Civi::settings()->get('contribution_invoice_settings');
52 $this->callAPISuccess('Setting', 'create', [
2fc43bbd 53 'contribution_invoice_settings' => array_merge($contributeSettings, ['acl_financial_type' => FALSE]),
54 'acl_financial_type' => FALSE,
dbaa9d7d 55 ]);
56 unset(\Civi::$statics['CRM_Financial_BAO_FinancialType']);
57 }
58
59 /**
60 * Create a logged in user limited by ACL permissions.
61 *
62 * @param array $aclPermissions
63 * Array of ACL permissions in the format
64 * [[$action, $financialType], [$action, $financialType])
c77f8667 65 *
66 * @return int Contact ID
dbaa9d7d 67 */
68 protected function createLoggedInUserWithFinancialACL($aclPermissions = [['view', 'Donation']]) {
faba82fc 69 CRM_Core_Config::singleton()->userPermissionClass->permissions = ['access CiviCRM', 'view all contacts'];
c77f8667 70 $contactID = $this->createLoggedInUser();
dbaa9d7d 71 $this->addFinancialAclPermissions($aclPermissions);
c77f8667 72 return $contactID;
dbaa9d7d 73 }
74
75 /**
76 * Add a permission to the financial ACLs.
77 *
78 * @param array $aclPermissions
79 * Array of ACL permissions in the format
80 * [[$action, $financialType], [$action, $financialType])
81 */
82 protected function addFinancialAclPermissions($aclPermissions) {
83 $permissions = CRM_Core_Config::singleton()->userPermissionClass->permissions;
84 foreach ($aclPermissions as $aclPermission) {
85 $permissions[] = $aclPermission[0] . ' contributions of type ' . $aclPermission[1];
86 }
87 $this->setPermissions($permissions);
88 }
89
90 /**
91 * Add a permission to the permissions array.
92 *
93 * @param array $permissions
94 * Array of permissions to add - e.g. ['access CiviCRM','access CiviContribute'],
95 */
96 protected function addPermissions($permissions) {
97 $permissions = array_merge(CRM_Core_Config::singleton()->userPermissionClass->permissions, $permissions);
98 $this->setPermissions($permissions);
99 }
100
101 /**
102 * Set ACL permissions, overwriting any existing ones.
103 *
104 * @param array $permissions
105 * Array of permissions e.g ['access CiviCRM','access CiviContribute'],
106 */
107 protected function setPermissions($permissions) {
108 CRM_Core_Config::singleton()->userPermissionClass->permissions = $permissions;
109 if (isset(\Civi::$statics['CRM_Financial_BAO_FinancialType'])) {
110 unset(\Civi::$statics['CRM_Financial_BAO_FinancialType']);
111 }
112 }
113
114}