2901412599e2514b53dd836f124b86caf7218821
[civicrm-core.git] / tests / phpunit / CRM / Core / BAO / SettingTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
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 +--------------------------------------------------------------------+
10 */
11
12 /**
13 * Class CRM_Core_BAO_SettingTest
14 * @group headless
15 */
16 class CRM_Core_BAO_SettingTest extends CiviUnitTestCase {
17
18 public function setUp() {
19 parent::setUp();
20 global $civicrm_setting;
21 $this->origSetting = $civicrm_setting;
22 CRM_Utils_Cache::singleton()->flush();
23 }
24
25 public function tearDown() {
26 global $civicrm_setting;
27 $civicrm_setting = $this->origSetting;
28 CRM_Utils_Cache::singleton()->flush();
29 parent::tearDown();
30 }
31
32 /**
33 * Test that enabling a valid component works.
34 */
35 public function testEnableComponentValid() {
36 CRM_Core_Config::singleton(TRUE, TRUE);
37 $result = CRM_Core_BAO_ConfigSetting::enableComponent('CiviCampaign');
38 $this->assertTrue($result);
39 }
40
41 /**
42 * Test that we get a success result if we try to enable an enabled component.
43 */
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);
49 }
50
51 /**
52 * Test that we get a false result if we try to enable an invalid component.
53 */
54 public function testEnableComponentInvalid() {
55 CRM_Core_Config::singleton(TRUE, TRUE);
56 $result = CRM_Core_BAO_ConfigSetting::enableComponent('CiviFake');
57 $this->assertFalse($result);
58 }
59
60 /**
61 * Test temporary retrieval & setting of converted settings.
62 *
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.
68 *
69 * https://lab.civicrm.org/dev/core/issues/1558
70 *
71 * @throws \CRM_Core_Exception
72 */
73 public function testHandlingOfContributionInvoiceSetting() {
74 $contributionSettings = [
75 'invoice_prefix' => 'G_',
76 'credit_notes_prefix' => 'XX_',
77 'due_date' => '20',
78 'due_date_period' => 'weeks',
79 'notes' => '<p>Give me money</p>',
80 'tax_term' => 'Extortion',
81 'tax_display_settings' => 'Exclusive',
82 'invoicing' => 1,
83 'is_email_pdf' => '1',
84 ];
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);
92
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'));
101 }
102
103 /**
104 * Ensure that overrides in $civicrm_setting apply when
105 * using getItem($group,$name).
106 */
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);
113
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);
132 }
133
134 /**
135 * Ensure that overrides in $civicrm_setting apply when
136 * using getItem($group).
137 */
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']);
144 }
145
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);
151 }
152
153 /**
154 * Ensure that on_change callbacks fire.
155 *
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].
159 */
160 public function testOnChange() {
161 global $_testOnChange_hookCalls;
162 $this->setMockSettingsMetaData([
163 'onChangeExample' => [
164 'group_name' => 'CiviCRM Preferences',
165 'group' => 'core',
166 'name' => 'onChangeExample',
167 'type' => 'Array',
168 'quick_form_type' => 'Element',
169 'html_type' => 'advmultiselect',
170 'default' => ['CiviEvent', 'CiviContribute'],
171 'add' => '4.4',
172 'title' => 'List of Components',
173 'is_domain' => '1',
174 'is_contact' => 0,
175 'description' => NULL,
176 'help_text' => NULL,
177 // list of callbacks
178 'on_change' => [
179 [__CLASS__, '_testOnChange_onChangeExample'],
180 ],
181 ],
182 ]);
183
184 // set initial value
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']);
190
191 // change value
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']);
198 }
199
200 /**
201 * Mock callback for a setting's on_change handler
202 *
203 * @param $oldValue
204 * @param $newValue
205 * @param $metadata
206 */
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;
213 }
214
215 /**
216 * Test to set isProductionEnvironment
217 *
218 */
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);
228 }
229
230 }