Merge pull request #14878 from civicrm/5.16
[civicrm-core.git] / tests / phpunit / CRM / Core / BAO / SettingTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
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 * Class CRM_Core_BAO_SettingTest
30 * @group headless
31 */
32 class CRM_Core_BAO_SettingTest extends CiviUnitTestCase {
33
34 public function setUp() {
35 parent::setUp();
36 global $civicrm_setting;
37 $this->origSetting = $civicrm_setting;
38 CRM_Utils_Cache::singleton()->flush();
39 }
40
41 public function tearDown() {
42 global $civicrm_setting;
43 $civicrm_setting = $this->origSetting;
44 CRM_Utils_Cache::singleton()->flush();
45 parent::tearDown();
46 }
47
48 public function testEnableComponentValid() {
49 $config = CRM_Core_Config::singleton(TRUE, TRUE);
50
51 $result = CRM_Core_BAO_ConfigSetting::enableComponent('CiviCampaign');
52
53 $this->assertTrue($result);
54 }
55
56 public function testEnableComponentAlreadyPresent() {
57 $config = CRM_Core_Config::singleton(TRUE, TRUE);
58
59 $result = CRM_Core_BAO_ConfigSetting::enableComponent('CiviCampaign');
60 $result = CRM_Core_BAO_ConfigSetting::enableComponent('CiviCampaign');
61
62 $this->assertTrue($result);
63 }
64
65 public function testEnableComponentInvalid() {
66 $config = CRM_Core_Config::singleton(TRUE, TRUE);
67
68 $result = CRM_Core_BAO_ConfigSetting::enableComponent('CiviFake');
69
70 $this->assertFalse($result);
71 }
72
73 /**
74 * Ensure that overrides in $civicrm_setting apply when
75 * using getItem($group,$name).
76 */
77 public function testGetItem_Override() {
78 global $civicrm_setting;
79 $civicrm_setting[CRM_Core_BAO_Setting::DIRECTORY_PREFERENCES_NAME]['imageUploadDir'] = '/test/override';
80 Civi::service('settings_manager')->useMandatory();
81 $value = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::DIRECTORY_PREFERENCES_NAME, 'imageUploadDir');
82 $this->assertEquals('/test/override', $value);
83
84 // CRM-14974 test suite
85 $civicrm_setting['Test Preferences']['overrideSetting'] = '/test/override';
86 Civi::service('settings_manager')->useMandatory();
87 $values = CRM_Core_BAO_Setting::getItem('Test Preferences');
88 $this->assertEquals('/test/override', $values['overrideSetting']);
89 Civi::settings()->set('databaseSetting', '/test/database');
90 $values = CRM_Core_BAO_Setting::getItem('Test Preferences');
91 $this->assertEquals('/test/override', $values['overrideSetting']);
92 $this->assertEquals('/test/database', $values['databaseSetting']);
93 $civicrm_setting['Test Preferences']['databaseSetting'] = '/test/dataride';
94 Civi::service('settings_manager')->useMandatory();
95 $values = CRM_Core_BAO_Setting::getItem('Test Preferences');
96 $this->assertEquals('/test/override', $values['overrideSetting']);
97 $this->assertEquals('/test/dataride', $values['databaseSetting']);
98 // CRM-14974 tear down
99 unset($civicrm_setting['Test Preferences']);
100 $query = "DELETE FROM civicrm_setting WHERE name IN ('overrideSetting', 'databaseSetting');";
101 CRM_Core_DAO::executeQuery($query);
102 }
103
104 /**
105 * Ensure that overrides in $civicrm_setting apply when
106 * using getItem($group).
107 */
108 public function testGetItemGroup_Override() {
109 global $civicrm_setting;
110 $civicrm_setting[CRM_Core_BAO_Setting::DIRECTORY_PREFERENCES_NAME]['imageUploadDir'] = '/test/override';
111 Civi::service('settings_manager')->useMandatory();
112 $values = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::DIRECTORY_PREFERENCES_NAME);
113 $this->assertEquals('/test/override', $values['imageUploadDir']);
114 }
115
116 public function testDefaults() {
117 CRM_Core_DAO::executeQuery('DELETE FROM civicrm_setting WHERE name = "max_attachments"');
118 Civi::service('settings_manager')->flush();
119 $this->assertEquals(3, Civi::settings()->get('max_attachments'));
120 $this->assertEquals(3, CRM_Core_Config::singleton()->maxAttachments);
121 }
122
123 /**
124 * Ensure that on_change callbacks fire.
125 *
126 * Note: api_v3_SettingTest::testOnChange and CRM_Core_BAO_SettingTest::testOnChange
127 * are very similar, but they exercise different codepaths. The first uses the API
128 * and setItems [plural]; the second uses setItem [singular].
129 */
130 public function testOnChange() {
131 global $_testOnChange_hookCalls;
132 $this->setMockSettingsMetaData([
133 'onChangeExample' => [
134 'group_name' => 'CiviCRM Preferences',
135 'group' => 'core',
136 'name' => 'onChangeExample',
137 'type' => 'Array',
138 'quick_form_type' => 'Element',
139 'html_type' => 'advmultiselect',
140 'default' => ['CiviEvent', 'CiviContribute'],
141 'add' => '4.4',
142 'title' => 'List of Components',
143 'is_domain' => '1',
144 'is_contact' => 0,
145 'description' => NULL,
146 'help_text' => NULL,
147 // list of callbacks
148 'on_change' => [
149 [__CLASS__, '_testOnChange_onChangeExample'],
150 ],
151 ],
152 ]);
153
154 // set initial value
155 $_testOnChange_hookCalls = ['count' => 0];
156 Civi::settings()->set('onChangeExample', ['First', 'Value']);
157 $this->assertEquals(1, $_testOnChange_hookCalls['count']);
158 $this->assertEquals(['First', 'Value'], $_testOnChange_hookCalls['newValue']);
159 $this->assertEquals('List of Components', $_testOnChange_hookCalls['metadata']['title']);
160
161 // change value
162 $_testOnChange_hookCalls = ['count' => 0];
163 Civi::settings()->set('onChangeExample', ['Second', 'Value']);
164 $this->assertEquals(1, $_testOnChange_hookCalls['count']);
165 $this->assertEquals(['First', 'Value'], $_testOnChange_hookCalls['oldValue']);
166 $this->assertEquals(['Second', 'Value'], $_testOnChange_hookCalls['newValue']);
167 $this->assertEquals('List of Components', $_testOnChange_hookCalls['metadata']['title']);
168 }
169
170 /**
171 * Mock callback for a setting's on_change handler
172 *
173 * @param $oldValue
174 * @param $newValue
175 * @param $metadata
176 */
177 public static function _testOnChange_onChangeExample($oldValue, $newValue, $metadata) {
178 global $_testOnChange_hookCalls;
179 $_testOnChange_hookCalls['count']++;
180 $_testOnChange_hookCalls['oldValue'] = $oldValue;
181 $_testOnChange_hookCalls['newValue'] = $newValue;
182 $_testOnChange_hookCalls['metadata'] = $metadata;
183 }
184
185 /**
186 * Test to set isProductionEnvironment
187 *
188 */
189 public function testSetCivicrmEnvironment() {
190 CRM_Core_BAO_Setting::setItem('Staging', CRM_Core_BAO_Setting::DEVELOPER_PREFERENCES_NAME, 'environment');
191 $values = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::DEVELOPER_PREFERENCES_NAME, 'environment');
192 $this->assertEquals('Staging', $values);
193 global $civicrm_setting;
194 $civicrm_setting[CRM_Core_BAO_Setting::DEVELOPER_PREFERENCES_NAME]['environment'] = 'Development';
195 Civi::service('settings_manager')->useMandatory();
196 $environment = CRM_Core_Config::environment();
197 $this->assertEquals('Development', $environment);
198 }
199
200 }