Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
39de6fd5 | 4 | | CiviCRM version 4.6 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
e7112fa7 | 6 | | Copyright CiviCRM LLC (c) 2004-2015 | |
6a488035 TO |
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 | +--------------------------------------------------------------------+ | |
d25dd0ee | 26 | */ |
6a488035 TO |
27 | |
28 | ||
29 | require_once 'CiviTest/CiviUnitTestCase.php'; | |
e9479dcf EM |
30 | |
31 | /** | |
32 | * Class CRM_Core_BAO_SettingTest | |
33 | */ | |
6a488035 | 34 | class CRM_Core_BAO_SettingTest extends CiviUnitTestCase { |
00be9182 | 35 | public function setUp() { |
6a488035 TO |
36 | parent::setUp(); |
37 | global $civicrm_setting; | |
38 | $this->origSetting = $civicrm_setting; | |
39 | CRM_Utils_Cache::singleton()->flush(); | |
40 | } | |
41 | ||
00be9182 | 42 | public function tearDown() { |
6a488035 TO |
43 | global $civicrm_setting; |
44 | $civicrm_setting = $this->origSetting; | |
45 | CRM_Utils_Cache::singleton()->flush(); | |
46 | parent::tearDown(); | |
47 | } | |
48 | ||
00be9182 | 49 | public function testEnableComponentValid() { |
6a488035 TO |
50 | $config = CRM_Core_Config::singleton(TRUE, TRUE); |
51 | ||
52 | $result = CRM_Core_BAO_ConfigSetting::enableComponent('CiviCampaign'); | |
53 | ||
54 | $this->assertTrue($result); | |
55 | } | |
56 | ||
00be9182 | 57 | public function testEnableComponentAlreadyPresent() { |
6a488035 TO |
58 | $config = CRM_Core_Config::singleton(TRUE, TRUE); |
59 | ||
60 | $result = CRM_Core_BAO_ConfigSetting::enableComponent('CiviCampaign'); | |
61 | $result = CRM_Core_BAO_ConfigSetting::enableComponent('CiviCampaign'); | |
62 | ||
63 | $this->assertTrue($result); | |
64 | } | |
65 | ||
00be9182 | 66 | public function testEnableComponentInvalid() { |
6a488035 TO |
67 | $config = CRM_Core_Config::singleton(TRUE, TRUE); |
68 | ||
69 | $result = CRM_Core_BAO_ConfigSetting::enableComponent('CiviFake'); | |
70 | ||
71 | $this->assertFalse($result); | |
72 | } | |
73 | ||
74 | /** | |
75 | * Ensure that overrides in $civicrm_setting apply when | |
76 | * using getItem($group,$name). | |
77 | */ | |
00be9182 | 78 | public function testGetItem_Override() { |
6a488035 TO |
79 | global $civicrm_setting; |
80 | $civicrm_setting[CRM_Core_BAO_Setting::DIRECTORY_PREFERENCES_NAME]['imageUploadDir'] = '/test/override'; | |
81 | $value = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::DIRECTORY_PREFERENCES_NAME, 'imageUploadDir'); | |
82 | $this->assertEquals('/test/override', $value); | |
bf83df95 | 83 | |
84 | // CRM-14974 test suite | |
85 | $civicrm_setting['Test Preferences']['overrideSetting'] = '/test/override'; | |
86 | $values = CRM_Core_BAO_Setting::getItem('Test Preferences'); | |
87 | $this->assertEquals('/test/override', $values['overrideSetting']); | |
88 | CRM_Core_BAO_Setting::setItem('/test/database', 'Test Preferences', 'databaseSetting'); | |
89 | $values = CRM_Core_BAO_Setting::getItem('Test Preferences'); | |
90 | $this->assertEquals('/test/override', $values['overrideSetting']); | |
91 | $this->assertEquals('/test/database', $values['databaseSetting']); | |
92 | $civicrm_setting['Test Preferences']['databaseSetting'] = '/test/dataride'; | |
93 | $values = CRM_Core_BAO_Setting::getItem('Test Preferences'); | |
94 | $this->assertEquals('/test/override', $values['overrideSetting']); | |
95 | $this->assertEquals('/test/dataride', $values['databaseSetting']); | |
96 | // CRM-14974 tear down | |
97 | unset($civicrm_setting['Test Preferences']); | |
98 | $query = "DELETE FROM civicrm_setting WHERE group_name = 'Test Preferences';"; | |
99 | CRM_Core_DAO::executeQuery($query); | |
6a488035 TO |
100 | } |
101 | ||
102 | /** | |
103 | * Ensure that overrides in $civicrm_setting apply when | |
104 | * using getItem($group). | |
105 | */ | |
00be9182 | 106 | public function testGetItemGroup_Override() { |
6a488035 TO |
107 | global $civicrm_setting; |
108 | $civicrm_setting[CRM_Core_BAO_Setting::DIRECTORY_PREFERENCES_NAME]['imageUploadDir'] = '/test/override'; | |
109 | $values = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::DIRECTORY_PREFERENCES_NAME); | |
110 | $this->assertEquals('/test/override', $values['imageUploadDir']); | |
111 | } | |
112 | ||
113 | /** | |
114 | * Ensure that overrides in $civicrm_setting apply when | |
115 | * when using retrieveDirectoryAndURLPreferences(). | |
116 | */ | |
00be9182 | 117 | public function testRetrieveDirectoryAndURLPreferences_Override() { |
6a488035 TO |
118 | global $civicrm_setting; |
119 | $civicrm_setting[CRM_Core_BAO_Setting::DIRECTORY_PREFERENCES_NAME]['imageUploadDir'] = '/test/override'; | |
120 | ||
121 | $params = array(); | |
122 | CRM_Core_BAO_Setting::retrieveDirectoryAndURLPreferences($params); | |
123 | $this->assertEquals('/test/override', $params['imageUploadDir']); | |
124 | } | |
125 | ||
126 | /** | |
127 | * This test checks that CRM_Core_BAO_Setting::updateSettingsFromMetaData(); | |
128 | * 1) Removes 'maxAttachments' from config (because 'prefetch' is not set in the metadata it should | |
129 | * be removed | |
130 | * 2) for current domain setting max_attachments is set to the value that $config->maxAttachments | |
131 | * had (6) | |
132 | * 3) for other domain (2) max_attachments is set to the configured default (3) | |
e70a7fc0 | 133 | */ |
00be9182 | 134 | public function testConvertAndFillSettings() { |
6a488035 TO |
135 | $settings = array('maxAttachments' => 6); |
136 | CRM_Core_BAO_ConfigSetting::add($settings); | |
137 | $config = CRM_Core_Config::singleton(TRUE, TRUE); | |
138 | $this->assertEquals(6, $config->maxAttachments); | |
eb40b5a4 | 139 | $checkSQL = "SELECT count(*) FROM civicrm_domain WHERE config_backend LIKE '%\"maxAttachments\";i:6%' AND id = 1 |
6a488035 TO |
140 | "; |
141 | $checkresult = CRM_Core_DAO::singleValueQuery($checkSQL); | |
142 | $this->assertEquals(1, $checkresult, "Check that maxAttachments has been saved to database not just stored in config"); | |
eb40b5a4 | 143 | $sql = " DELETE FROM civicrm_setting WHERE name = 'max_attachments'"; |
eb40b5a4 | 144 | CRM_Core_DAO::executeQuery($sql); |
6a488035 | 145 | |
f64cd471 | 146 | $currentDomain = CRM_Core_Config::domainID(); |
eb40b5a4 | 147 | // we are setting up an artificial situation here as we are trying to drive out |
148 | // previous memory of this setting so we need to flush it out | |
6c6e6187 | 149 | $cachekey = CRM_Core_BAO_Setting::inCache('CiviCRM Preferences', 'max_attachments', NULL, NULL, TRUE, $currentDomain); |
eb40b5a4 | 150 | CRM_Core_BAO_Setting::flushCache($cachekey); |
151 | CRM_Core_BAO_Setting::updateSettingsFromMetaData(); | |
6a488035 TO |
152 | //check current domain |
153 | $value = civicrm_api('setting', 'getvalue', array( | |
154 | 'version' => 3, | |
155 | 'name' => 'max_attachments', | |
156 | 'group' => CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, | |
157 | )); | |
158 | ||
159 | $this->assertEquals(6, $value); | |
160 | // check alternate domain | |
161 | $value = civicrm_api('setting', 'getvalue', array( | |
162 | 'version' => 3, | |
163 | 'name' => 'max_attachments', | |
164 | 'group' => CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, | |
21dfd5f5 | 165 | 'domain_id' => 2, |
6a488035 TO |
166 | )); |
167 | ||
168 | $this->assertEquals(3, $value); | |
169 | ||
170 | //some caching inconsistency here | |
171 | $config = CRM_Core_Config::singleton(TRUE, TRUE); | |
aa3dc61e E |
172 | $maxAttachments = empty($config->maxAttachments) ? NULL : $config->maxAttachments; |
173 | $this->assertEmpty($maxAttachments, "Config item still Set to $maxAttachments | |
6a488035 TO |
174 | . This works fine when test run alone"); |
175 | } | |
176 | ||
177 | /** | |
178 | * Ensure that overrides in $civicrm_setting apply when | |
179 | * when using retrieveDirectoryAndURLPreferences(). | |
180 | */ | |
00be9182 | 181 | public function testConvertConfigToSettingNoPrefetch() { |
6a488035 TO |
182 | $settings = array('maxAttachments' => 6); |
183 | CRM_Core_BAO_ConfigSetting::add($settings); | |
184 | $config = CRM_Core_Config::singleton(TRUE, TRUE); | |
185 | $this->assertEquals(6, $config->maxAttachments); | |
186 | ||
187 | CRM_Core_BAO_Setting::convertConfigToSetting('max_attachments'); | |
188 | $value = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, 'max_attachments'); | |
189 | $this->assertEquals(6, $value); | |
190 | ||
aa3dc61e | 191 | $this->callAPISuccess('system', 'flush', array()); |
6a488035 | 192 | $config = CRM_Core_Config::singleton(TRUE, TRUE); |
aa3dc61e E |
193 | $maxAttachments = empty($config->maxAttachments) ? NULL : $config->maxAttachments; |
194 | $this->assertEmpty($maxAttachments); | |
6a488035 TO |
195 | } |
196 | ||
e70a7fc0 | 197 | /* @codingStandardsIgnoreStart |
6a488035 TO |
198 | * Check that setting is converted without config value being removed |
199 | * | |
e70a7fc0 | 200 | public function testConvertConfigToSettingPrefetch() { |
6a488035 TO |
201 | $settings = array('debug' => 1); |
202 | CRM_Core_BAO_ConfigSetting::add($settings); | |
203 | $config = CRM_Core_Config::singleton(true, true); | |
204 | $this->assertEquals(1, $config->debug); | |
205 | CRM_Core_BAO_Setting::convertConfigToSetting('debug_is_enabled'); | |
206 | $value = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::DEBUG_PREFERENCES_NAME, 'debug_is_enabled'); | |
207 | $this->assertEquals(1, $value); | |
208 | civicrm_api('system', 'flush', array('version' => 3)); | |
209 | $config = CRM_Core_Config::singleton(true, true); | |
210 | $this->assertEmpty($config->debug); | |
211 | } | |
e70a7fc0 | 212 | @codingStandardsIgnoreEnd */ |
6a488035 | 213 | |
076d8c82 TO |
214 | /** |
215 | * Ensure that on_change callbacks fire. | |
216 | * | |
217 | * Note: api_v3_SettingTest::testOnChange and CRM_Core_BAO_SettingTest::testOnChange | |
218 | * are very similar, but they exercise different codepaths. The first uses the API | |
219 | * and setItems [plural]; the second uses setItem [singular]. | |
220 | */ | |
00be9182 | 221 | public function testOnChange() { |
076d8c82 TO |
222 | global $_testOnChange_hookCalls; |
223 | $this->setMockSettingsMetaData(array( | |
224 | 'onChangeExample' => array( | |
225 | 'group_name' => 'CiviCRM Preferences', | |
226 | 'group' => 'core', | |
227 | 'name' => 'onChangeExample', | |
228 | 'type' => 'Array', | |
229 | 'quick_form_type' => 'Element', | |
230 | 'html_type' => 'advmultiselect', | |
231 | 'default' => array('CiviEvent', 'CiviContribute'), | |
232 | 'add' => '4.4', | |
233 | 'title' => 'List of Components', | |
234 | 'is_domain' => '1', | |
235 | 'is_contact' => 0, | |
236 | 'description' => NULL, | |
237 | 'help_text' => NULL, | |
481a74f4 | 238 | 'on_change' => array(// list of callbacks |
21dfd5f5 | 239 | array(__CLASS__, '_testOnChange_onChangeExample'), |
076d8c82 TO |
240 | ), |
241 | ), | |
242 | )); | |
243 | ||
244 | // set initial value | |
245 | $_testOnChange_hookCalls = array('count' => 0); | |
246 | CRM_Core_BAO_Setting::setItem( | |
247 | array('First', 'Value'), | |
248 | 'CiviCRM Preferences', | |
249 | 'onChangeExample' | |
250 | ); | |
251 | $this->assertEquals(1, $_testOnChange_hookCalls['count']); | |
252 | $this->assertEquals(array('First', 'Value'), $_testOnChange_hookCalls['newValue']); | |
253 | $this->assertEquals('List of Components', $_testOnChange_hookCalls['metadata']['title']); | |
254 | ||
255 | // change value | |
256 | $_testOnChange_hookCalls = array('count' => 0); | |
257 | CRM_Core_BAO_Setting::setItem( | |
258 | array('Second', 'Value'), | |
259 | 'CiviCRM Preferences', | |
260 | 'onChangeExample' | |
261 | ); | |
262 | $this->assertEquals(1, $_testOnChange_hookCalls['count']); | |
263 | $this->assertEquals(array('First', 'Value'), $_testOnChange_hookCalls['oldValue']); | |
264 | $this->assertEquals(array('Second', 'Value'), $_testOnChange_hookCalls['newValue']); | |
265 | $this->assertEquals('List of Components', $_testOnChange_hookCalls['metadata']['title']); | |
266 | } | |
267 | ||
268 | /** | |
269 | * Mock callback for a setting's on_change handler | |
270 | * | |
271 | * @param $oldValue | |
272 | * @param $newValue | |
273 | * @param $metadata | |
274 | */ | |
00be9182 | 275 | public static function _testOnChange_onChangeExample($oldValue, $newValue, $metadata) { |
076d8c82 TO |
276 | global $_testOnChange_hookCalls; |
277 | $_testOnChange_hookCalls['count']++; | |
278 | $_testOnChange_hookCalls['oldValue'] = $oldValue; | |
279 | $_testOnChange_hookCalls['newValue'] = $newValue; | |
280 | $_testOnChange_hookCalls['metadata'] = $metadata; | |
281 | } | |
282 | ||
6a488035 | 283 | } |