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
{
19 * Original value of civicrm_setting global.
24 public function setUp() {
26 global $civicrm_setting;
27 $this->origSetting
= $civicrm_setting;
28 CRM_Utils_Cache
::singleton()->clear();
32 * Clean up after test.
34 * @throws \CRM_Core_Exception
36 public function tearDown() {
37 global $civicrm_setting;
38 $civicrm_setting = $this->origSetting
;
39 $this->quickCleanup(['civicrm_contribution']);
40 CRM_Utils_Cache
::singleton()->clear();
45 * Test that enabling a valid component works.
47 public function testEnableComponentValid() {
48 CRM_Core_Config
::singleton(TRUE, TRUE);
49 $result = CRM_Core_BAO_ConfigSetting
::enableComponent('CiviCampaign');
50 $this->assertTrue($result);
54 * Test that we get a success result if we try to enable an enabled component.
56 public function testEnableComponentAlreadyPresent() {
57 CRM_Core_Config
::singleton(TRUE, TRUE);
58 CRM_Core_BAO_ConfigSetting
::enableComponent('CiviCampaign');
59 $result = CRM_Core_BAO_ConfigSetting
::enableComponent('CiviCampaign');
60 $this->assertTrue($result);
64 * Test that we get a false result if we try to enable an invalid component.
66 public function testEnableComponentInvalid() {
67 CRM_Core_Config
::singleton(TRUE, TRUE);
68 $result = CRM_Core_BAO_ConfigSetting
::enableComponent('CiviFake');
69 $this->assertFalse($result);
73 * Test temporary retrieval & setting of converted settings.
75 * As a transitional measure we allow the settings that were munged into
76 * contribution_invoice_setting. This tests that the current method of getting via the 'old' key
77 * works. This will be deprecated & removed over the next few versions but
78 * 1) some extensions use these settings &
79 * 2) there is a lot of work to fix this mess in core so a transitional method makes sense.
81 * https://lab.civicrm.org/dev/core/issues/1558
83 * @throws \CRM_Core_Exception
85 public function testHandlingOfContributionInvoiceSetting() {
86 $contributionSettings = [
87 'invoice_prefix' => 'G_',
88 'credit_notes_prefix' => 'XX_',
90 'due_date_period' => 'weeks',
91 'notes' => '<p>Give me money</p>',
92 'tax_term' => 'Extortion',
93 'tax_display_settings' => 'Exclusive',
95 'is_email_pdf' => '1',
97 Civi
::settings()->set('contribution_invoice_settings', $contributionSettings);
98 $settingsFromGet = Civi
::settings()->get('contribution_invoice_settings');
99 $settingsFromAPI = $this->callAPISuccess('Setting', 'get', ['return' => 'contribution_invoice_settings'])['values'][CRM_Core_Config
::domainID()]['contribution_invoice_settings'];
100 $getVersion = $this->callAPISuccessGetValue('Setting', ['name' => 'contribution_invoice_settings']);
101 $this->assertEquals($settingsFromAPI, $settingsFromGet);
102 $this->assertAPIArrayComparison($getVersion, $settingsFromGet);
103 $this->assertEquals($contributionSettings, $settingsFromGet);
105 // These are the preferred retrieval methods.
106 $this->assertEquals('G_', Civi
::settings()->get('invoice_prefix'));
107 $this->assertEquals('XX_', Civi
::settings()->get('credit_notes_prefix'));
108 $this->assertEquals('20', Civi
::settings()->get('invoice_due_date'));
109 $this->assertEquals('weeks', Civi
::settings()->get('invoice_due_date_period'));
110 $this->assertEquals('<p>Give me money</p>', Civi
::settings()->get('invoice_notes'));
111 $this->assertEquals('Extortion', Civi
::settings()->get('tax_term'));
112 $this->assertEquals('Exclusive', Civi
::settings()->get('tax_display_settings'));
116 * Ensure that overrides in $civicrm_setting apply when
117 * using getItem($group,$name).
119 public function testGetItem_Override() {
120 global $civicrm_setting;
121 $civicrm_setting[CRM_Core_BAO_Setting
::DIRECTORY_PREFERENCES_NAME
]['imageUploadDir'] = '/test/override';
122 Civi
::service('settings_manager')->useMandatory();
123 $value = CRM_Core_BAO_Setting
::getItem(CRM_Core_BAO_Setting
::DIRECTORY_PREFERENCES_NAME
, 'imageUploadDir');
124 $this->assertEquals('/test/override', $value);
126 // CRM-14974 test suite
127 $civicrm_setting['Test Preferences']['overrideSetting'] = '/test/override';
128 Civi
::service('settings_manager')->useMandatory();
129 $values = CRM_Core_BAO_Setting
::getItem('Test Preferences');
130 $this->assertEquals('/test/override', $values['overrideSetting']);
131 Civi
::settings()->set('databaseSetting', '/test/database');
132 $values = CRM_Core_BAO_Setting
::getItem('Test Preferences');
133 $this->assertEquals('/test/override', $values['overrideSetting']);
134 $this->assertEquals('/test/database', $values['databaseSetting']);
135 $civicrm_setting['Test Preferences']['databaseSetting'] = '/test/dataride';
136 Civi
::service('settings_manager')->useMandatory();
137 $values = CRM_Core_BAO_Setting
::getItem('Test Preferences');
138 $this->assertEquals('/test/override', $values['overrideSetting']);
139 $this->assertEquals('/test/dataride', $values['databaseSetting']);
140 // CRM-14974 tear down
141 unset($civicrm_setting['Test Preferences']);
142 $query = "DELETE FROM civicrm_setting WHERE name IN ('overrideSetting', 'databaseSetting');";
143 CRM_Core_DAO
::executeQuery($query);
147 * Ensure that overrides in $civicrm_setting apply when
148 * using getItem($group).
150 public function testGetItemGroup_Override() {
151 global $civicrm_setting;
152 $civicrm_setting[CRM_Core_BAO_Setting
::DIRECTORY_PREFERENCES_NAME
]['imageUploadDir'] = '/test/override';
153 Civi
::service('settings_manager')->useMandatory();
154 $values = CRM_Core_BAO_Setting
::getItem(CRM_Core_BAO_Setting
::DIRECTORY_PREFERENCES_NAME
);
155 $this->assertEquals('/test/override', $values['imageUploadDir']);
158 public function testDefaults() {
159 CRM_Core_DAO
::executeQuery('DELETE FROM civicrm_setting WHERE name = "max_attachments"');
160 Civi
::service('settings_manager')->flush();
161 $this->assertEquals(3, Civi
::settings()->get('max_attachments'));
162 $this->assertEquals(3, CRM_Core_Config
::singleton()->maxAttachments
);
166 * Ensure that on_change callbacks fire.
168 * Note: api_v3_SettingTest::testOnChange and CRM_Core_BAO_SettingTest::testOnChange
169 * are very similar, but they exercise different codepaths. The first uses the API
170 * and setItems [plural]; the second uses setItem [singular].
172 public function testOnChange() {
173 global $_testOnChange_hookCalls;
174 $this->setMockSettingsMetaData([
175 'onChangeExample' => [
176 'group_name' => 'CiviCRM Preferences',
178 'name' => 'onChangeExample',
180 'quick_form_type' => 'Element',
181 'html_type' => 'advmultiselect',
182 'default' => ['CiviEvent', 'CiviContribute'],
184 'title' => 'List of Components',
187 'description' => NULL,
191 [__CLASS__
, '_testOnChange_onChangeExample'],
197 $_testOnChange_hookCalls = ['count' => 0];
198 Civi
::settings()->set('onChangeExample', ['First', 'Value']);
199 $this->assertEquals(1, $_testOnChange_hookCalls['count']);
200 $this->assertEquals(['First', 'Value'], $_testOnChange_hookCalls['newValue']);
201 $this->assertEquals('List of Components', $_testOnChange_hookCalls['metadata']['title']);
204 $_testOnChange_hookCalls = ['count' => 0];
205 Civi
::settings()->set('onChangeExample', ['Second', 'Value']);
206 $this->assertEquals(1, $_testOnChange_hookCalls['count']);
207 $this->assertEquals(['First', 'Value'], $_testOnChange_hookCalls['oldValue']);
208 $this->assertEquals(['Second', 'Value'], $_testOnChange_hookCalls['newValue']);
209 $this->assertEquals('List of Components', $_testOnChange_hookCalls['metadata']['title']);
213 * Mock callback for a setting's on_change handler
219 public static function _testOnChange_onChangeExample($oldValue, $newValue, $metadata) {
220 global $_testOnChange_hookCalls;
221 $_testOnChange_hookCalls['count']++
;
222 $_testOnChange_hookCalls['oldValue'] = $oldValue;
223 $_testOnChange_hookCalls['newValue'] = $newValue;
224 $_testOnChange_hookCalls['metadata'] = $metadata;
228 * Test to set isProductionEnvironment
231 public function testSetCivicrmEnvironment() {
232 Civi
::settings()->set('environment', 'Staging');
233 $values = Civi
::settings()->get('environment');
234 $this->assertEquals('Staging', $values);
235 global $civicrm_setting;
236 $civicrm_setting[CRM_Core_BAO_Setting
::DEVELOPER_PREFERENCES_NAME
]['environment'] = 'Development';
237 Civi
::service('settings_manager')->useMandatory();
238 $environment = CRM_Core_Config
::environment();
239 $this->assertEquals('Development', $environment);
243 * Test that options defined as a pseudoconstant can be converted to options.
245 public function testPseudoConstants() {
246 $this->contributionPageCreate();
247 $metadata = \Civi\Core\SettingsMetadata
::getMetadata(['name' => ['default_invoice_page']], NULL, TRUE);
248 $this->assertEquals('Test Contribution Page', $metadata['default_invoice_page']['options'][1]);