Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
39de6fd5 | 4 | | CiviCRM version 4.6 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
06a1bc01 | 6 | | Copyright CiviCRM LLC (c) 2004-2014 | |
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 | +--------------------------------------------------------------------+ | |
26 | */ | |
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); | |
83 | } | |
84 | ||
85 | /** | |
86 | * Ensure that overrides in $civicrm_setting apply when | |
87 | * using getItem($group). | |
88 | */ | |
00be9182 | 89 | public function testGetItemGroup_Override() { |
6a488035 TO |
90 | global $civicrm_setting; |
91 | $civicrm_setting[CRM_Core_BAO_Setting::DIRECTORY_PREFERENCES_NAME]['imageUploadDir'] = '/test/override'; | |
92 | $values = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::DIRECTORY_PREFERENCES_NAME); | |
93 | $this->assertEquals('/test/override', $values['imageUploadDir']); | |
94 | } | |
95 | ||
96 | /** | |
97 | * Ensure that overrides in $civicrm_setting apply when | |
98 | * when using retrieveDirectoryAndURLPreferences(). | |
99 | */ | |
00be9182 | 100 | public function testRetrieveDirectoryAndURLPreferences_Override() { |
6a488035 TO |
101 | global $civicrm_setting; |
102 | $civicrm_setting[CRM_Core_BAO_Setting::DIRECTORY_PREFERENCES_NAME]['imageUploadDir'] = '/test/override'; | |
103 | ||
104 | $params = array(); | |
105 | CRM_Core_BAO_Setting::retrieveDirectoryAndURLPreferences($params); | |
106 | $this->assertEquals('/test/override', $params['imageUploadDir']); | |
107 | } | |
108 | ||
109 | /** | |
110 | * This test checks that CRM_Core_BAO_Setting::updateSettingsFromMetaData(); | |
111 | * 1) Removes 'maxAttachments' from config (because 'prefetch' is not set in the metadata it should | |
112 | * be removed | |
113 | * 2) for current domain setting max_attachments is set to the value that $config->maxAttachments | |
114 | * had (6) | |
115 | * 3) for other domain (2) max_attachments is set to the configured default (3) | |
116 | * | |
117 | * | |
118 | **/ | |
00be9182 | 119 | public function testConvertAndFillSettings() { |
6a488035 TO |
120 | $settings = array('maxAttachments' => 6); |
121 | CRM_Core_BAO_ConfigSetting::add($settings); | |
122 | $config = CRM_Core_Config::singleton(TRUE, TRUE); | |
123 | $this->assertEquals(6, $config->maxAttachments); | |
eb40b5a4 | 124 | $checkSQL = "SELECT count(*) FROM civicrm_domain WHERE config_backend LIKE '%\"maxAttachments\";i:6%' AND id = 1 |
6a488035 TO |
125 | "; |
126 | $checkresult = CRM_Core_DAO::singleValueQuery($checkSQL); | |
127 | $this->assertEquals(1, $checkresult, "Check that maxAttachments has been saved to database not just stored in config"); | |
eb40b5a4 | 128 | $sql = " DELETE FROM civicrm_setting WHERE name = 'max_attachments'"; |
eb40b5a4 | 129 | CRM_Core_DAO::executeQuery($sql); |
6a488035 | 130 | |
f64cd471 | 131 | $currentDomain = CRM_Core_Config::domainID(); |
eb40b5a4 | 132 | // we are setting up an artificial situation here as we are trying to drive out |
133 | // previous memory of this setting so we need to flush it out | |
6c6e6187 | 134 | $cachekey = CRM_Core_BAO_Setting::inCache('CiviCRM Preferences', 'max_attachments', NULL, NULL, TRUE, $currentDomain); |
eb40b5a4 | 135 | CRM_Core_BAO_Setting::flushCache($cachekey); |
136 | CRM_Core_BAO_Setting::updateSettingsFromMetaData(); | |
6a488035 TO |
137 | //check current domain |
138 | $value = civicrm_api('setting', 'getvalue', array( | |
139 | 'version' => 3, | |
140 | 'name' => 'max_attachments', | |
141 | 'group' => CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, | |
142 | )); | |
143 | ||
144 | $this->assertEquals(6, $value); | |
145 | // check alternate domain | |
146 | $value = civicrm_api('setting', 'getvalue', array( | |
147 | 'version' => 3, | |
148 | 'name' => 'max_attachments', | |
149 | 'group' => CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, | |
150 | 'domain_id' => 2 | |
151 | )); | |
152 | ||
153 | $this->assertEquals(3, $value); | |
154 | ||
155 | //some caching inconsistency here | |
156 | $config = CRM_Core_Config::singleton(TRUE, TRUE); | |
aa3dc61e E |
157 | $maxAttachments = empty($config->maxAttachments) ? NULL : $config->maxAttachments; |
158 | $this->assertEmpty($maxAttachments, "Config item still Set to $maxAttachments | |
6a488035 TO |
159 | . This works fine when test run alone"); |
160 | } | |
161 | ||
162 | /** | |
163 | * Ensure that overrides in $civicrm_setting apply when | |
164 | * when using retrieveDirectoryAndURLPreferences(). | |
165 | */ | |
00be9182 | 166 | public function testConvertConfigToSettingNoPrefetch() { |
6a488035 TO |
167 | $settings = array('maxAttachments' => 6); |
168 | CRM_Core_BAO_ConfigSetting::add($settings); | |
169 | $config = CRM_Core_Config::singleton(TRUE, TRUE); | |
170 | $this->assertEquals(6, $config->maxAttachments); | |
171 | ||
172 | CRM_Core_BAO_Setting::convertConfigToSetting('max_attachments'); | |
173 | $value = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, 'max_attachments'); | |
174 | $this->assertEquals(6, $value); | |
175 | ||
aa3dc61e | 176 | $this->callAPISuccess('system', 'flush', array()); |
6a488035 | 177 | $config = CRM_Core_Config::singleton(TRUE, TRUE); |
aa3dc61e E |
178 | $maxAttachments = empty($config->maxAttachments) ? NULL : $config->maxAttachments; |
179 | $this->assertEmpty($maxAttachments); | |
6a488035 TO |
180 | } |
181 | ||
182 | /* | |
183 | * Check that setting is converted without config value being removed | |
184 | * | |
00be9182 | 185 | public function testConvertConfigToSettingPrefetch() { |
6a488035 TO |
186 | $settings = array('debug' => 1); |
187 | CRM_Core_BAO_ConfigSetting::add($settings); | |
188 | $config = CRM_Core_Config::singleton(true, true); | |
189 | $this->assertEquals(1, $config->debug); | |
190 | CRM_Core_BAO_Setting::convertConfigToSetting('debug_is_enabled'); | |
191 | $value = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::DEBUG_PREFERENCES_NAME, 'debug_is_enabled'); | |
192 | $this->assertEquals(1, $value); | |
193 | civicrm_api('system', 'flush', array('version' => 3)); | |
194 | $config = CRM_Core_Config::singleton(true, true); | |
195 | $this->assertEmpty($config->debug); | |
196 | } | |
197 | */ | |
198 | ||
076d8c82 TO |
199 | /** |
200 | * Ensure that on_change callbacks fire. | |
201 | * | |
202 | * Note: api_v3_SettingTest::testOnChange and CRM_Core_BAO_SettingTest::testOnChange | |
203 | * are very similar, but they exercise different codepaths. The first uses the API | |
204 | * and setItems [plural]; the second uses setItem [singular]. | |
205 | */ | |
00be9182 | 206 | public function testOnChange() { |
076d8c82 TO |
207 | global $_testOnChange_hookCalls; |
208 | $this->setMockSettingsMetaData(array( | |
209 | 'onChangeExample' => array( | |
210 | 'group_name' => 'CiviCRM Preferences', | |
211 | 'group' => 'core', | |
212 | 'name' => 'onChangeExample', | |
213 | 'type' => 'Array', | |
214 | 'quick_form_type' => 'Element', | |
215 | 'html_type' => 'advmultiselect', | |
216 | 'default' => array('CiviEvent', 'CiviContribute'), | |
217 | 'add' => '4.4', | |
218 | 'title' => 'List of Components', | |
219 | 'is_domain' => '1', | |
220 | 'is_contact' => 0, | |
221 | 'description' => NULL, | |
222 | 'help_text' => NULL, | |
481a74f4 | 223 | 'on_change' => array(// list of callbacks |
076d8c82 TO |
224 | array(__CLASS__, '_testOnChange_onChangeExample') |
225 | ), | |
226 | ), | |
227 | )); | |
228 | ||
229 | // set initial value | |
230 | $_testOnChange_hookCalls = array('count' => 0); | |
231 | CRM_Core_BAO_Setting::setItem( | |
232 | array('First', 'Value'), | |
233 | 'CiviCRM Preferences', | |
234 | 'onChangeExample' | |
235 | ); | |
236 | $this->assertEquals(1, $_testOnChange_hookCalls['count']); | |
237 | $this->assertEquals(array('First', 'Value'), $_testOnChange_hookCalls['newValue']); | |
238 | $this->assertEquals('List of Components', $_testOnChange_hookCalls['metadata']['title']); | |
239 | ||
240 | // change value | |
241 | $_testOnChange_hookCalls = array('count' => 0); | |
242 | CRM_Core_BAO_Setting::setItem( | |
243 | array('Second', 'Value'), | |
244 | 'CiviCRM Preferences', | |
245 | 'onChangeExample' | |
246 | ); | |
247 | $this->assertEquals(1, $_testOnChange_hookCalls['count']); | |
248 | $this->assertEquals(array('First', 'Value'), $_testOnChange_hookCalls['oldValue']); | |
249 | $this->assertEquals(array('Second', 'Value'), $_testOnChange_hookCalls['newValue']); | |
250 | $this->assertEquals('List of Components', $_testOnChange_hookCalls['metadata']['title']); | |
251 | } | |
252 | ||
253 | /** | |
254 | * Mock callback for a setting's on_change handler | |
255 | * | |
256 | * @param $oldValue | |
257 | * @param $newValue | |
258 | * @param $metadata | |
259 | */ | |
00be9182 | 260 | public static function _testOnChange_onChangeExample($oldValue, $newValue, $metadata) { |
076d8c82 TO |
261 | global $_testOnChange_hookCalls; |
262 | $_testOnChange_hookCalls['count']++; | |
263 | $_testOnChange_hookCalls['oldValue'] = $oldValue; | |
264 | $_testOnChange_hookCalls['newValue'] = $newValue; | |
265 | $_testOnChange_hookCalls['metadata'] = $metadata; | |
266 | } | |
267 | ||
6a488035 | 268 | } |