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(): void
{
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(): void
{
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',
94 // NOTE: This form of `invoicing` is accepted, but it may be normalized to the idiomatic form with a nested array.
96 'is_email_pdf' => '1',
98 Civi
::settings()->set('contribution_invoice_settings', $contributionSettings);
99 $settingsFromGet = Civi
::settings()->get('contribution_invoice_settings');
100 $settingsFromAPI = $this->callAPISuccess('Setting', 'get', ['return' => 'contribution_invoice_settings'])['values'][CRM_Core_Config
::domainID()]['contribution_invoice_settings'];
101 $getVersion = $this->callAPISuccessGetValue('Setting', ['name' => 'contribution_invoice_settings']);
102 $this->assertEquals($settingsFromAPI, $settingsFromGet);
103 $this->assertAPIArrayComparison($getVersion, $settingsFromGet);
104 $this->assertEquals(['invoicing' => ['invoicing' => 1]] +
$contributionSettings, $settingsFromGet);
106 // These are the preferred retrieval methods.
107 $this->assertEquals('G_', Civi
::settings()->get('invoice_prefix'));
108 $this->assertEquals('XX_', Civi
::settings()->get('credit_notes_prefix'));
109 $this->assertEquals('20', Civi
::settings()->get('invoice_due_date'));
110 $this->assertEquals('weeks', Civi
::settings()->get('invoice_due_date_period'));
111 $this->assertEquals('<p>Give me money</p>', Civi
::settings()->get('invoice_notes'));
112 $this->assertEquals('Extortion', Civi
::settings()->get('tax_term'));
113 $this->assertEquals('Exclusive', Civi
::settings()->get('tax_display_settings'));
117 * Ensure that overrides in $civicrm_setting apply when
118 * using getItem($group,$name).
120 public function testGetItem_Override() {
121 global $civicrm_setting;
122 $civicrm_setting[CRM_Core_BAO_Setting
::DIRECTORY_PREFERENCES_NAME
]['imageUploadDir'] = '/test/override';
123 Civi
::service('settings_manager')->useMandatory();
124 $value = CRM_Core_BAO_Setting
::getItem(CRM_Core_BAO_Setting
::DIRECTORY_PREFERENCES_NAME
, 'imageUploadDir');
125 $this->assertEquals('/test/override', $value);
127 // CRM-14974 test suite
128 $civicrm_setting['Test Preferences']['overrideSetting'] = '/test/override';
129 Civi
::service('settings_manager')->useMandatory();
130 $values = CRM_Core_BAO_Setting
::getItem('Test Preferences');
131 $this->assertEquals('/test/override', $values['overrideSetting']);
132 Civi
::settings()->set('databaseSetting', '/test/database');
133 $values = CRM_Core_BAO_Setting
::getItem('Test Preferences');
134 $this->assertEquals('/test/override', $values['overrideSetting']);
135 $this->assertEquals('/test/database', $values['databaseSetting']);
136 $civicrm_setting['Test Preferences']['databaseSetting'] = '/test/dataride';
137 Civi
::service('settings_manager')->useMandatory();
138 $values = CRM_Core_BAO_Setting
::getItem('Test Preferences');
139 $this->assertEquals('/test/override', $values['overrideSetting']);
140 $this->assertEquals('/test/dataride', $values['databaseSetting']);
141 // CRM-14974 tear down
142 unset($civicrm_setting['Test Preferences']);
143 $query = "DELETE FROM civicrm_setting WHERE name IN ('overrideSetting', 'databaseSetting');";
144 CRM_Core_DAO
::executeQuery($query);
148 * Ensure that overrides in $civicrm_setting apply when
149 * using getItem($group).
151 public function testGetItemGroup_Override() {
152 global $civicrm_setting;
153 $civicrm_setting[CRM_Core_BAO_Setting
::DIRECTORY_PREFERENCES_NAME
]['imageUploadDir'] = '/test/override';
154 Civi
::service('settings_manager')->useMandatory();
155 $values = CRM_Core_BAO_Setting
::getItem(CRM_Core_BAO_Setting
::DIRECTORY_PREFERENCES_NAME
);
156 $this->assertEquals('/test/override', $values['imageUploadDir']);
159 public function testDefaults() {
160 CRM_Core_DAO
::executeQuery('DELETE FROM civicrm_setting WHERE name = "max_attachments"');
161 Civi
::service('settings_manager')->flush();
162 $this->assertEquals(3, Civi
::settings()->get('max_attachments'));
163 $this->assertEquals(3, CRM_Core_Config
::singleton()->maxAttachments
);
167 * Ensure that on_change callbacks fire.
169 * Note: api_v3_SettingTest::testOnChange and CRM_Core_BAO_SettingTest::testOnChange
170 * are very similar, but they exercise different codepaths. The first uses the API
171 * and setItems [plural]; the second uses setItem [singular].
173 public function testOnChange() {
174 global $_testOnChange_hookCalls;
175 $this->setMockSettingsMetaData([
176 'onChangeExample' => [
177 'group_name' => 'CiviCRM Preferences',
179 'name' => 'onChangeExample',
181 'quick_form_type' => 'Element',
182 'html_type' => 'advmultiselect',
183 'default' => ['CiviEvent', 'CiviContribute'],
185 'title' => 'List of Components',
188 'description' => NULL,
192 [__CLASS__
, '_testOnChange_onChangeExample'],
198 $_testOnChange_hookCalls = ['count' => 0];
199 Civi
::settings()->set('onChangeExample', ['First', 'Value']);
200 $this->assertEquals(1, $_testOnChange_hookCalls['count']);
201 $this->assertEquals(['First', 'Value'], $_testOnChange_hookCalls['newValue']);
202 $this->assertEquals('List of Components', $_testOnChange_hookCalls['metadata']['title']);
205 $_testOnChange_hookCalls = ['count' => 0];
206 Civi
::settings()->set('onChangeExample', ['Second', 'Value']);
207 $this->assertEquals(1, $_testOnChange_hookCalls['count']);
208 $this->assertEquals(['First', 'Value'], $_testOnChange_hookCalls['oldValue']);
209 $this->assertEquals(['Second', 'Value'], $_testOnChange_hookCalls['newValue']);
210 $this->assertEquals('List of Components', $_testOnChange_hookCalls['metadata']['title']);
214 * Mock callback for a setting's on_change handler
220 public static function _testOnChange_onChangeExample($oldValue, $newValue, $metadata) {
221 global $_testOnChange_hookCalls;
222 $_testOnChange_hookCalls['count']++
;
223 $_testOnChange_hookCalls['oldValue'] = $oldValue;
224 $_testOnChange_hookCalls['newValue'] = $newValue;
225 $_testOnChange_hookCalls['metadata'] = $metadata;
229 * Test to set isProductionEnvironment
232 public function testSetCivicrmEnvironment() {
233 Civi
::settings()->set('environment', 'Staging');
234 $values = Civi
::settings()->get('environment');
235 $this->assertEquals('Staging', $values);
236 global $civicrm_setting;
237 $civicrm_setting[CRM_Core_BAO_Setting
::DEVELOPER_PREFERENCES_NAME
]['environment'] = 'Development';
238 Civi
::service('settings_manager')->useMandatory();
239 $environment = CRM_Core_Config
::environment();
240 $this->assertEquals('Development', $environment);
244 * Test that options defined as a pseudoconstant can be converted to options.
246 public function testPseudoConstants() {
247 $this->contributionPageCreate();
248 $metadata = \Civi\Core\SettingsMetadata
::getMetadata(['name' => ['default_invoice_page']], NULL, TRUE);
249 $this->assertEquals('Test Contribution Page', $metadata['default_invoice_page']['options'][1]);