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();
33 * Test that enabling a valid component works.
35 public function testEnableComponentValid() {
36 CRM_Core_Config
::singleton(TRUE, TRUE);
37 $result = CRM_Core_BAO_ConfigSetting
::enableComponent('CiviCampaign');
38 $this->assertTrue($result);
42 * Test that we get a success result if we try to enable an enabled component.
44 public function testEnableComponentAlreadyPresent() {
45 CRM_Core_Config
::singleton(TRUE, TRUE);
46 CRM_Core_BAO_ConfigSetting
::enableComponent('CiviCampaign');
47 $result = CRM_Core_BAO_ConfigSetting
::enableComponent('CiviCampaign');
48 $this->assertTrue($result);
52 * Test that we get a false result if we try to enable an invalid component.
54 public function testEnableComponentInvalid() {
55 CRM_Core_Config
::singleton(TRUE, TRUE);
56 $result = CRM_Core_BAO_ConfigSetting
::enableComponent('CiviFake');
57 $this->assertFalse($result);
61 * Test temporary retrieval & setting of converted settings.
63 * As a transitional measure we allow the settings that were munged into
64 * contribution_invoice_setting. This tests that the current method of getting via the 'old' key
65 * works. This will be deprecated & removed over the next few versions but
66 * 1) some extensions use these settings &
67 * 2) there is a lot of work to fix this mess in core so a transitional method makes sense.
69 * https://lab.civicrm.org/dev/core/issues/1558
71 * @throws \CRM_Core_Exception
73 public function testHandlingOfContributionInvoiceSetting() {
74 $contributionSettings = [
75 'invoice_prefix' => 'G_',
76 'credit_notes_prefix' => 'XX_',
78 'due_date_period' => 'weeks',
79 'notes' => '<p>Give me money</p>',
80 'tax_term' => 'Extortion',
81 'tax_display_settings' => 'Exclusive',
83 'is_email_pdf' => '1',
85 Civi
::settings()->set('contribution_invoice_settings', $contributionSettings);
86 $settingsFromGet = Civi
::settings()->get('contribution_invoice_settings');
87 $settingsFromAPI = $this->callAPISuccess('Setting', 'get', ['return' => 'contribution_invoice_settings'])['values'][CRM_Core_Config
::domainID()]['contribution_invoice_settings'];
88 $getVersion = $this->callAPISuccessGetValue('Setting', ['name' => 'contribution_invoice_settings']);
89 $this->assertEquals($settingsFromAPI, $settingsFromGet);
90 $this->assertAPIArrayComparison($getVersion, $settingsFromGet);
91 $this->assertEquals($contributionSettings, $settingsFromGet);
93 // These are the preferred retrieval methods.
94 $this->assertEquals('G_', Civi
::settings()->get('invoice_prefix'));
95 $this->assertEquals('XX_', Civi
::settings()->get('credit_notes_prefix'));
96 $this->assertEquals('20', Civi
::settings()->get('invoice_due_date'));
97 $this->assertEquals('weeks', Civi
::settings()->get('invoice_due_date_period'));
98 $this->assertEquals('<p>Give me money</p>', Civi
::settings()->get('invoice_notes'));
99 $this->assertEquals('Extortion', Civi
::settings()->get('tax_term'));
100 $this->assertEquals('Exclusive', Civi
::settings()->get('tax_display_settings'));
104 * Ensure that overrides in $civicrm_setting apply when
105 * using getItem($group,$name).
107 public function testGetItem_Override() {
108 global $civicrm_setting;
109 $civicrm_setting[CRM_Core_BAO_Setting
::DIRECTORY_PREFERENCES_NAME
]['imageUploadDir'] = '/test/override';
110 Civi
::service('settings_manager')->useMandatory();
111 $value = CRM_Core_BAO_Setting
::getItem(CRM_Core_BAO_Setting
::DIRECTORY_PREFERENCES_NAME
, 'imageUploadDir');
112 $this->assertEquals('/test/override', $value);
114 // CRM-14974 test suite
115 $civicrm_setting['Test Preferences']['overrideSetting'] = '/test/override';
116 Civi
::service('settings_manager')->useMandatory();
117 $values = CRM_Core_BAO_Setting
::getItem('Test Preferences');
118 $this->assertEquals('/test/override', $values['overrideSetting']);
119 Civi
::settings()->set('databaseSetting', '/test/database');
120 $values = CRM_Core_BAO_Setting
::getItem('Test Preferences');
121 $this->assertEquals('/test/override', $values['overrideSetting']);
122 $this->assertEquals('/test/database', $values['databaseSetting']);
123 $civicrm_setting['Test Preferences']['databaseSetting'] = '/test/dataride';
124 Civi
::service('settings_manager')->useMandatory();
125 $values = CRM_Core_BAO_Setting
::getItem('Test Preferences');
126 $this->assertEquals('/test/override', $values['overrideSetting']);
127 $this->assertEquals('/test/dataride', $values['databaseSetting']);
128 // CRM-14974 tear down
129 unset($civicrm_setting['Test Preferences']);
130 $query = "DELETE FROM civicrm_setting WHERE name IN ('overrideSetting', 'databaseSetting');";
131 CRM_Core_DAO
::executeQuery($query);
135 * Ensure that overrides in $civicrm_setting apply when
136 * using getItem($group).
138 public function testGetItemGroup_Override() {
139 global $civicrm_setting;
140 $civicrm_setting[CRM_Core_BAO_Setting
::DIRECTORY_PREFERENCES_NAME
]['imageUploadDir'] = '/test/override';
141 Civi
::service('settings_manager')->useMandatory();
142 $values = CRM_Core_BAO_Setting
::getItem(CRM_Core_BAO_Setting
::DIRECTORY_PREFERENCES_NAME
);
143 $this->assertEquals('/test/override', $values['imageUploadDir']);
146 public function testDefaults() {
147 CRM_Core_DAO
::executeQuery('DELETE FROM civicrm_setting WHERE name = "max_attachments"');
148 Civi
::service('settings_manager')->flush();
149 $this->assertEquals(3, Civi
::settings()->get('max_attachments'));
150 $this->assertEquals(3, CRM_Core_Config
::singleton()->maxAttachments
);
154 * Ensure that on_change callbacks fire.
156 * Note: api_v3_SettingTest::testOnChange and CRM_Core_BAO_SettingTest::testOnChange
157 * are very similar, but they exercise different codepaths. The first uses the API
158 * and setItems [plural]; the second uses setItem [singular].
160 public function testOnChange() {
161 global $_testOnChange_hookCalls;
162 $this->setMockSettingsMetaData([
163 'onChangeExample' => [
164 'group_name' => 'CiviCRM Preferences',
166 'name' => 'onChangeExample',
168 'quick_form_type' => 'Element',
169 'html_type' => 'advmultiselect',
170 'default' => ['CiviEvent', 'CiviContribute'],
172 'title' => 'List of Components',
175 'description' => NULL,
179 [__CLASS__
, '_testOnChange_onChangeExample'],
185 $_testOnChange_hookCalls = ['count' => 0];
186 Civi
::settings()->set('onChangeExample', ['First', 'Value']);
187 $this->assertEquals(1, $_testOnChange_hookCalls['count']);
188 $this->assertEquals(['First', 'Value'], $_testOnChange_hookCalls['newValue']);
189 $this->assertEquals('List of Components', $_testOnChange_hookCalls['metadata']['title']);
192 $_testOnChange_hookCalls = ['count' => 0];
193 Civi
::settings()->set('onChangeExample', ['Second', 'Value']);
194 $this->assertEquals(1, $_testOnChange_hookCalls['count']);
195 $this->assertEquals(['First', 'Value'], $_testOnChange_hookCalls['oldValue']);
196 $this->assertEquals(['Second', 'Value'], $_testOnChange_hookCalls['newValue']);
197 $this->assertEquals('List of Components', $_testOnChange_hookCalls['metadata']['title']);
201 * Mock callback for a setting's on_change handler
207 public static function _testOnChange_onChangeExample($oldValue, $newValue, $metadata) {
208 global $_testOnChange_hookCalls;
209 $_testOnChange_hookCalls['count']++
;
210 $_testOnChange_hookCalls['oldValue'] = $oldValue;
211 $_testOnChange_hookCalls['newValue'] = $newValue;
212 $_testOnChange_hookCalls['metadata'] = $metadata;
216 * Test to set isProductionEnvironment
219 public function testSetCivicrmEnvironment() {
220 Civi
::settings()->set('environment', 'Staging');
221 $values = Civi
::settings()->get('environment');
222 $this->assertEquals('Staging', $values);
223 global $civicrm_setting;
224 $civicrm_setting[CRM_Core_BAO_Setting
::DEVELOPER_PREFERENCES_NAME
]['environment'] = 'Development';
225 Civi
::service('settings_manager')->useMandatory();
226 $environment = CRM_Core_Config
::environment();
227 $this->assertEquals('Development', $environment);