3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
29 require_once 'CiviTest/CiviUnitTestCase.php';
32 * Class CRM_Core_BAO_SettingTest
34 class CRM_Core_BAO_SettingTest
extends CiviUnitTestCase
{
35 public function setUp() {
37 global $civicrm_setting;
38 $this->origSetting
= $civicrm_setting;
39 CRM_Utils_Cache
::singleton()->flush();
42 public function tearDown() {
43 global $civicrm_setting;
44 $civicrm_setting = $this->origSetting
;
45 CRM_Utils_Cache
::singleton()->flush();
49 public function testEnableComponentValid() {
50 $config = CRM_Core_Config
::singleton(TRUE, TRUE);
52 $result = CRM_Core_BAO_ConfigSetting
::enableComponent('CiviCampaign');
54 $this->assertTrue($result);
57 public function testEnableComponentAlreadyPresent() {
58 $config = CRM_Core_Config
::singleton(TRUE, TRUE);
60 $result = CRM_Core_BAO_ConfigSetting
::enableComponent('CiviCampaign');
61 $result = CRM_Core_BAO_ConfigSetting
::enableComponent('CiviCampaign');
63 $this->assertTrue($result);
66 public function testEnableComponentInvalid() {
67 $config = CRM_Core_Config
::singleton(TRUE, TRUE);
69 $result = CRM_Core_BAO_ConfigSetting
::enableComponent('CiviFake');
71 $this->assertFalse($result);
75 * Ensure that overrides in $civicrm_setting apply when
76 * using getItem($group,$name).
78 public function testGetItem_Override() {
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);
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);
103 * Ensure that overrides in $civicrm_setting apply when
104 * using getItem($group).
106 public function testGetItemGroup_Override() {
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']);
114 * Ensure that overrides in $civicrm_setting apply when
115 * when using retrieveDirectoryAndURLPreferences().
117 public function testRetrieveDirectoryAndURLPreferences_Override() {
118 global $civicrm_setting;
119 $civicrm_setting[CRM_Core_BAO_Setting
::DIRECTORY_PREFERENCES_NAME
]['imageUploadDir'] = '/test/override';
122 CRM_Core_BAO_Setting
::retrieveDirectoryAndURLPreferences($params);
123 $this->assertEquals('/test/override', $params['imageUploadDir']);
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
130 * 2) for current domain setting max_attachments is set to the value that $config->maxAttachments
132 * 3) for other domain (2) max_attachments is set to the configured default (3)
134 public function testConvertAndFillSettings() {
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
);
139 $checkSQL = "SELECT count(*) FROM civicrm_domain WHERE config_backend LIKE '%\"maxAttachments\";i:6%' AND id = 1
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");
143 $sql = " DELETE FROM civicrm_setting WHERE name = 'max_attachments'";
144 CRM_Core_DAO
::executeQuery($sql);
146 $currentDomain = CRM_Core_Config
::domainID();
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
149 $cachekey = CRM_Core_BAO_Setting
::inCache('CiviCRM Preferences', 'max_attachments', NULL, NULL, TRUE, $currentDomain);
150 CRM_Core_BAO_Setting
::flushCache($cachekey);
151 CRM_Core_BAO_Setting
::updateSettingsFromMetaData();
152 //check current domain
153 $value = civicrm_api('setting', 'getvalue', array(
155 'name' => 'max_attachments',
156 'group' => CRM_Core_BAO_Setting
::SYSTEM_PREFERENCES_NAME
,
159 $this->assertEquals(6, $value);
160 // check alternate domain
161 $value = civicrm_api('setting', 'getvalue', array(
163 'name' => 'max_attachments',
164 'group' => CRM_Core_BAO_Setting
::SYSTEM_PREFERENCES_NAME
,
168 $this->assertEquals(3, $value);
170 //some caching inconsistency here
171 $config = CRM_Core_Config
::singleton(TRUE, TRUE);
172 $maxAttachments = empty($config->maxAttachments
) ?
NULL : $config->maxAttachments
;
173 $this->assertEmpty($maxAttachments, "Config item still Set to $maxAttachments
174 . This works fine when test run alone");
178 * Ensure that overrides in $civicrm_setting apply when
179 * when using retrieveDirectoryAndURLPreferences().
181 public function testConvertConfigToSettingNoPrefetch() {
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
);
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);
191 $this->callAPISuccess('system', 'flush', array());
192 $config = CRM_Core_Config
::singleton(TRUE, TRUE);
193 $maxAttachments = empty($config->maxAttachments
) ?
NULL : $config->maxAttachments
;
194 $this->assertEmpty($maxAttachments);
197 /* @codingStandardsIgnoreStart
198 * Check that setting is converted without config value being removed
200 public function testConvertConfigToSettingPrefetch() {
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);
212 @codingStandardsIgnoreEnd */
215 * Ensure that on_change callbacks fire.
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].
221 public function testOnChange() {
222 global $_testOnChange_hookCalls;
223 $this->setMockSettingsMetaData(array(
224 'onChangeExample' => array(
225 'group_name' => 'CiviCRM Preferences',
227 'name' => 'onChangeExample',
229 'quick_form_type' => 'Element',
230 'html_type' => 'advmultiselect',
231 'default' => array('CiviEvent', 'CiviContribute'),
233 'title' => 'List of Components',
236 'description' => NULL,
238 'on_change' => array(// list of callbacks
239 array(__CLASS__
, '_testOnChange_onChangeExample'),
245 $_testOnChange_hookCalls = array('count' => 0);
246 CRM_Core_BAO_Setting
::setItem(
247 array('First', 'Value'),
248 'CiviCRM Preferences',
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']);
256 $_testOnChange_hookCalls = array('count' => 0);
257 CRM_Core_BAO_Setting
::setItem(
258 array('Second', 'Value'),
259 'CiviCRM Preferences',
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']);
269 * Mock callback for a setting's on_change handler
275 public static function _testOnChange_onChangeExample($oldValue, $newValue, $metadata) {
276 global $_testOnChange_hookCalls;
277 $_testOnChange_hookCalls['count']++
;
278 $_testOnChange_hookCalls['oldValue'] = $oldValue;
279 $_testOnChange_hookCalls['newValue'] = $newValue;
280 $_testOnChange_hookCalls['metadata'] = $metadata;