3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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);
86 * Ensure that overrides in $civicrm_setting apply when
87 * using getItem($group).
89 public function testGetItemGroup_Override() {
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']);
97 * Ensure that overrides in $civicrm_setting apply when
98 * when using retrieveDirectoryAndURLPreferences().
100 public function testRetrieveDirectoryAndURLPreferences_Override() {
101 global $civicrm_setting;
102 $civicrm_setting[CRM_Core_BAO_Setting
::DIRECTORY_PREFERENCES_NAME
]['imageUploadDir'] = '/test/override';
105 CRM_Core_BAO_Setting
::retrieveDirectoryAndURLPreferences($params);
106 $this->assertEquals('/test/override', $params['imageUploadDir']);
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
113 * 2) for current domain setting max_attachments is set to the value that $config->maxAttachments
115 * 3) for other domain (2) max_attachments is set to the configured default (3)
119 public function testConvertAndFillSettings() {
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
);
124 $checkSQL = "SELECT count(*) FROM civicrm_domain WHERE config_backend LIKE '%\"maxAttachments\";i:6%' AND id = 1
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");
128 $sql = " DELETE FROM civicrm_setting WHERE name = 'max_attachments'";
129 CRM_Core_DAO
::executeQuery($sql);
131 $currentDomain = CRM_Core_Config
::domainID();
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
134 $cachekey = CRM_Core_BAO_Setting
::inCache('CiviCRM Preferences', 'max_attachments', NULL, NULL, TRUE, $currentDomain);
135 CRM_Core_BAO_Setting
::flushCache($cachekey);
136 CRM_Core_BAO_Setting
::updateSettingsFromMetaData();
137 //check current domain
138 $value = civicrm_api('setting', 'getvalue', array(
140 'name' => 'max_attachments',
141 'group' => CRM_Core_BAO_Setting
::SYSTEM_PREFERENCES_NAME
,
144 $this->assertEquals(6, $value);
145 // check alternate domain
146 $value = civicrm_api('setting', 'getvalue', array(
148 'name' => 'max_attachments',
149 'group' => CRM_Core_BAO_Setting
::SYSTEM_PREFERENCES_NAME
,
153 $this->assertEquals(3, $value);
155 //some caching inconsistency here
156 $config = CRM_Core_Config
::singleton(TRUE, TRUE);
157 $maxAttachments = empty($config->maxAttachments
) ?
NULL : $config->maxAttachments
;
158 $this->assertEmpty($maxAttachments, "Config item still Set to $maxAttachments
159 . This works fine when test run alone");
163 * Ensure that overrides in $civicrm_setting apply when
164 * when using retrieveDirectoryAndURLPreferences().
166 public function testConvertConfigToSettingNoPrefetch() {
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
);
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);
176 $this->callAPISuccess('system', 'flush', array());
177 $config = CRM_Core_Config
::singleton(TRUE, TRUE);
178 $maxAttachments = empty($config->maxAttachments
) ?
NULL : $config->maxAttachments
;
179 $this->assertEmpty($maxAttachments);
183 * Check that setting is converted without config value being removed
185 public function testConvertConfigToSettingPrefetch() {
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);
200 * Ensure that on_change callbacks fire.
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].
206 public function testOnChange() {
207 global $_testOnChange_hookCalls;
208 $this->setMockSettingsMetaData(array(
209 'onChangeExample' => array(
210 'group_name' => 'CiviCRM Preferences',
212 'name' => 'onChangeExample',
214 'quick_form_type' => 'Element',
215 'html_type' => 'advmultiselect',
216 'default' => array('CiviEvent', 'CiviContribute'),
218 'title' => 'List of Components',
221 'description' => NULL,
223 'on_change' => array( // list of callbacks
224 array(__CLASS__
, '_testOnChange_onChangeExample')
230 $_testOnChange_hookCalls = array('count' => 0);
231 CRM_Core_BAO_Setting
::setItem(
232 array('First', 'Value'),
233 'CiviCRM Preferences',
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']);
241 $_testOnChange_hookCalls = array('count' => 0);
242 CRM_Core_BAO_Setting
::setItem(
243 array('Second', 'Value'),
244 'CiviCRM Preferences',
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']);
254 * Mock callback for a setting's on_change handler
260 public static function _testOnChange_onChangeExample($oldValue, $newValue, $metadata) {
261 global $_testOnChange_hookCalls;
262 $_testOnChange_hookCalls['count']++
;
263 $_testOnChange_hookCalls['oldValue'] = $oldValue;
264 $_testOnChange_hookCalls['newValue'] = $newValue;
265 $_testOnChange_hookCalls['metadata'] = $metadata;