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