3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
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 Civi
::service('settings_manager')->useMandatory();
82 $value = CRM_Core_BAO_Setting
::getItem(CRM_Core_BAO_Setting
::DIRECTORY_PREFERENCES_NAME
, 'imageUploadDir');
83 $this->assertEquals('/test/override', $value);
85 // CRM-14974 test suite
86 $civicrm_setting['Test Preferences']['overrideSetting'] = '/test/override';
87 Civi
::service('settings_manager')->useMandatory();
88 $values = CRM_Core_BAO_Setting
::getItem('Test Preferences');
89 $this->assertEquals('/test/override', $values['overrideSetting']);
90 CRM_Core_BAO_Setting
::setItem('/test/database', 'Test Preferences', 'databaseSetting');
91 $values = CRM_Core_BAO_Setting
::getItem('Test Preferences');
92 $this->assertEquals('/test/override', $values['overrideSetting']);
93 $this->assertEquals('/test/database', $values['databaseSetting']);
94 $civicrm_setting['Test Preferences']['databaseSetting'] = '/test/dataride';
95 Civi
::service('settings_manager')->useMandatory();
96 $values = CRM_Core_BAO_Setting
::getItem('Test Preferences');
97 $this->assertEquals('/test/override', $values['overrideSetting']);
98 $this->assertEquals('/test/dataride', $values['databaseSetting']);
99 // CRM-14974 tear down
100 unset($civicrm_setting['Test Preferences']);
101 $query = "DELETE FROM civicrm_setting WHERE name IN ('overrideSetting', 'databaseSetting');";
102 CRM_Core_DAO
::executeQuery($query);
106 * Ensure that overrides in $civicrm_setting apply when
107 * using getItem($group).
109 public function testGetItemGroup_Override() {
110 global $civicrm_setting;
111 $civicrm_setting[CRM_Core_BAO_Setting
::DIRECTORY_PREFERENCES_NAME
]['imageUploadDir'] = '/test/override';
112 Civi
::service('settings_manager')->useMandatory();
113 $values = CRM_Core_BAO_Setting
::getItem(CRM_Core_BAO_Setting
::DIRECTORY_PREFERENCES_NAME
);
114 $this->assertEquals('/test/override', $values['imageUploadDir']);
117 public function testDefaults() {
118 CRM_Core_DAO
::executeQuery('DELETE FROM civicrm_setting WHERE name = "max_attachments"');
119 Civi
::service('settings_manager')->flush();
120 $this->assertEquals(3, Civi
::settings()->get('max_attachments'));
121 $this->assertEquals(3, CRM_Core_Config
::singleton()->maxAttachments
);
125 * Ensure that on_change callbacks fire.
127 * Note: api_v3_SettingTest::testOnChange and CRM_Core_BAO_SettingTest::testOnChange
128 * are very similar, but they exercise different codepaths. The first uses the API
129 * and setItems [plural]; the second uses setItem [singular].
131 public function testOnChange() {
132 global $_testOnChange_hookCalls;
133 $this->setMockSettingsMetaData(array(
134 'onChangeExample' => array(
135 'group_name' => 'CiviCRM Preferences',
137 'name' => 'onChangeExample',
139 'quick_form_type' => 'Element',
140 'html_type' => 'advmultiselect',
141 'default' => array('CiviEvent', 'CiviContribute'),
143 'title' => 'List of Components',
146 'description' => NULL,
148 'on_change' => array(// list of callbacks
149 array(__CLASS__
, '_testOnChange_onChangeExample'),
155 $_testOnChange_hookCalls = array('count' => 0);
156 CRM_Core_BAO_Setting
::setItem(
157 array('First', 'Value'),
158 'CiviCRM Preferences',
161 $this->assertEquals(1, $_testOnChange_hookCalls['count']);
162 $this->assertEquals(array('First', 'Value'), $_testOnChange_hookCalls['newValue']);
163 $this->assertEquals('List of Components', $_testOnChange_hookCalls['metadata']['title']);
166 $_testOnChange_hookCalls = array('count' => 0);
167 CRM_Core_BAO_Setting
::setItem(
168 array('Second', 'Value'),
169 'CiviCRM Preferences',
172 $this->assertEquals(1, $_testOnChange_hookCalls['count']);
173 $this->assertEquals(array('First', 'Value'), $_testOnChange_hookCalls['oldValue']);
174 $this->assertEquals(array('Second', 'Value'), $_testOnChange_hookCalls['newValue']);
175 $this->assertEquals('List of Components', $_testOnChange_hookCalls['metadata']['title']);
179 * Mock callback for a setting's on_change handler
185 public static function _testOnChange_onChangeExample($oldValue, $newValue, $metadata) {
186 global $_testOnChange_hookCalls;
187 $_testOnChange_hookCalls['count']++
;
188 $_testOnChange_hookCalls['oldValue'] = $oldValue;
189 $_testOnChange_hookCalls['newValue'] = $newValue;
190 $_testOnChange_hookCalls['metadata'] = $metadata;