3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
13 * Class CRM_Core_BAO_SettingTest
16 class CRM_Core_BAO_SettingTest
extends CiviUnitTestCase
{
18 public function setUp() {
20 global $civicrm_setting;
21 $this->origSetting
= $civicrm_setting;
22 CRM_Utils_Cache
::singleton()->flush();
25 public function tearDown() {
26 global $civicrm_setting;
27 $civicrm_setting = $this->origSetting
;
28 CRM_Utils_Cache
::singleton()->flush();
32 public function testEnableComponentValid() {
33 $config = CRM_Core_Config
::singleton(TRUE, TRUE);
35 $result = CRM_Core_BAO_ConfigSetting
::enableComponent('CiviCampaign');
37 $this->assertTrue($result);
40 public function testEnableComponentAlreadyPresent() {
41 $config = CRM_Core_Config
::singleton(TRUE, TRUE);
43 $result = CRM_Core_BAO_ConfigSetting
::enableComponent('CiviCampaign');
44 $result = CRM_Core_BAO_ConfigSetting
::enableComponent('CiviCampaign');
46 $this->assertTrue($result);
49 public function testEnableComponentInvalid() {
50 $config = CRM_Core_Config
::singleton(TRUE, TRUE);
52 $result = CRM_Core_BAO_ConfigSetting
::enableComponent('CiviFake');
54 $this->assertFalse($result);
58 * Ensure that overrides in $civicrm_setting apply when
59 * using getItem($group,$name).
61 public function testGetItem_Override() {
62 global $civicrm_setting;
63 $civicrm_setting[CRM_Core_BAO_Setting
::DIRECTORY_PREFERENCES_NAME
]['imageUploadDir'] = '/test/override';
64 Civi
::service('settings_manager')->useMandatory();
65 $value = CRM_Core_BAO_Setting
::getItem(CRM_Core_BAO_Setting
::DIRECTORY_PREFERENCES_NAME
, 'imageUploadDir');
66 $this->assertEquals('/test/override', $value);
68 // CRM-14974 test suite
69 $civicrm_setting['Test Preferences']['overrideSetting'] = '/test/override';
70 Civi
::service('settings_manager')->useMandatory();
71 $values = CRM_Core_BAO_Setting
::getItem('Test Preferences');
72 $this->assertEquals('/test/override', $values['overrideSetting']);
73 Civi
::settings()->set('databaseSetting', '/test/database');
74 $values = CRM_Core_BAO_Setting
::getItem('Test Preferences');
75 $this->assertEquals('/test/override', $values['overrideSetting']);
76 $this->assertEquals('/test/database', $values['databaseSetting']);
77 $civicrm_setting['Test Preferences']['databaseSetting'] = '/test/dataride';
78 Civi
::service('settings_manager')->useMandatory();
79 $values = CRM_Core_BAO_Setting
::getItem('Test Preferences');
80 $this->assertEquals('/test/override', $values['overrideSetting']);
81 $this->assertEquals('/test/dataride', $values['databaseSetting']);
82 // CRM-14974 tear down
83 unset($civicrm_setting['Test Preferences']);
84 $query = "DELETE FROM civicrm_setting WHERE name IN ('overrideSetting', 'databaseSetting');";
85 CRM_Core_DAO
::executeQuery($query);
89 * Ensure that overrides in $civicrm_setting apply when
90 * using getItem($group).
92 public function testGetItemGroup_Override() {
93 global $civicrm_setting;
94 $civicrm_setting[CRM_Core_BAO_Setting
::DIRECTORY_PREFERENCES_NAME
]['imageUploadDir'] = '/test/override';
95 Civi
::service('settings_manager')->useMandatory();
96 $values = CRM_Core_BAO_Setting
::getItem(CRM_Core_BAO_Setting
::DIRECTORY_PREFERENCES_NAME
);
97 $this->assertEquals('/test/override', $values['imageUploadDir']);
100 public function testDefaults() {
101 CRM_Core_DAO
::executeQuery('DELETE FROM civicrm_setting WHERE name = "max_attachments"');
102 Civi
::service('settings_manager')->flush();
103 $this->assertEquals(3, Civi
::settings()->get('max_attachments'));
104 $this->assertEquals(3, CRM_Core_Config
::singleton()->maxAttachments
);
108 * Ensure that on_change callbacks fire.
110 * Note: api_v3_SettingTest::testOnChange and CRM_Core_BAO_SettingTest::testOnChange
111 * are very similar, but they exercise different codepaths. The first uses the API
112 * and setItems [plural]; the second uses setItem [singular].
114 public function testOnChange() {
115 global $_testOnChange_hookCalls;
116 $this->setMockSettingsMetaData([
117 'onChangeExample' => [
118 'group_name' => 'CiviCRM Preferences',
120 'name' => 'onChangeExample',
122 'quick_form_type' => 'Element',
123 'html_type' => 'advmultiselect',
124 'default' => ['CiviEvent', 'CiviContribute'],
126 'title' => 'List of Components',
129 'description' => NULL,
133 [__CLASS__
, '_testOnChange_onChangeExample'],
139 $_testOnChange_hookCalls = ['count' => 0];
140 Civi
::settings()->set('onChangeExample', ['First', 'Value']);
141 $this->assertEquals(1, $_testOnChange_hookCalls['count']);
142 $this->assertEquals(['First', 'Value'], $_testOnChange_hookCalls['newValue']);
143 $this->assertEquals('List of Components', $_testOnChange_hookCalls['metadata']['title']);
146 $_testOnChange_hookCalls = ['count' => 0];
147 Civi
::settings()->set('onChangeExample', ['Second', 'Value']);
148 $this->assertEquals(1, $_testOnChange_hookCalls['count']);
149 $this->assertEquals(['First', 'Value'], $_testOnChange_hookCalls['oldValue']);
150 $this->assertEquals(['Second', 'Value'], $_testOnChange_hookCalls['newValue']);
151 $this->assertEquals('List of Components', $_testOnChange_hookCalls['metadata']['title']);
155 * Mock callback for a setting's on_change handler
161 public static function _testOnChange_onChangeExample($oldValue, $newValue, $metadata) {
162 global $_testOnChange_hookCalls;
163 $_testOnChange_hookCalls['count']++
;
164 $_testOnChange_hookCalls['oldValue'] = $oldValue;
165 $_testOnChange_hookCalls['newValue'] = $newValue;
166 $_testOnChange_hookCalls['metadata'] = $metadata;
170 * Test to set isProductionEnvironment
173 public function testSetCivicrmEnvironment() {
174 Civi
::settings()->set('environment', 'Staging');
175 $values = Civi
::settings()->get('environment');
176 $this->assertEquals('Staging', $values);
177 global $civicrm_setting;
178 $civicrm_setting[CRM_Core_BAO_Setting
::DEVELOPER_PREFERENCES_NAME
]['environment'] = 'Development';
179 Civi
::service('settings_manager')->useMandatory();
180 $environment = CRM_Core_Config
::environment();
181 $this->assertEquals('Development', $environment);