Merge pull request #17253 from mattwire/utf8convertblocksize
[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 /**
19 * Original value of civicrm_setting global.
20 * @var array
21 */
22 private $origSetting;
23
24 public function setUp() {
25 parent::setUp();
26 global $civicrm_setting;
27 $this->origSetting = $civicrm_setting;
28 CRM_Utils_Cache::singleton()->clear();
29 }
30
31 /**
32 * Clean up after test.
33 *
34 * @throws \CRM_Core_Exception
35 */
36 public function tearDown() {
37 global $civicrm_setting;
38 $civicrm_setting = $this->origSetting;
39 $this->quickCleanup(['civicrm_contribution']);
40 CRM_Utils_Cache::singleton()->clear();
41 parent::tearDown();
42 }
43
44 /**
45 * Test that enabling a valid component works.
46 */
47 public function testEnableComponentValid() {
48 CRM_Core_Config::singleton(TRUE, TRUE);
49 $result = CRM_Core_BAO_ConfigSetting::enableComponent('CiviCampaign');
50 $this->assertTrue($result);
51 }
52
53 /**
54 * Test that we get a success result if we try to enable an enabled component.
55 */
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);
61 }
62
63 /**
64 * Test that we get a false result if we try to enable an invalid component.
65 */
66 public function testEnableComponentInvalid() {
67 CRM_Core_Config::singleton(TRUE, TRUE);
68 $result = CRM_Core_BAO_ConfigSetting::enableComponent('CiviFake');
69 $this->assertFalse($result);
70 }
71
72 /**
73 * Test temporary retrieval & setting of converted settings.
74 *
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.
80 *
81 * https://lab.civicrm.org/dev/core/issues/1558
82 *
83 * @throws \CRM_Core_Exception
84 */
85 public function testHandlingOfContributionInvoiceSetting() {
86 $contributionSettings = [
87 'invoice_prefix' => 'G_',
88 'credit_notes_prefix' => 'XX_',
89 'due_date' => '20',
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.
95 'invoicing' => 1,
96 'is_email_pdf' => '1',
97 ];
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);
105
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'));
114 }
115
116 /**
117 * Ensure that overrides in $civicrm_setting apply when
118 * using getItem($group,$name).
119 */
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);
126
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);
145 }
146
147 /**
148 * Ensure that overrides in $civicrm_setting apply when
149 * using getItem($group).
150 */
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']);
157 }
158
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);
164 }
165
166 /**
167 * Ensure that on_change callbacks fire.
168 *
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].
172 */
173 public function testOnChange() {
174 global $_testOnChange_hookCalls;
175 $this->setMockSettingsMetaData([
176 'onChangeExample' => [
177 'group_name' => 'CiviCRM Preferences',
178 'group' => 'core',
179 'name' => 'onChangeExample',
180 'type' => 'Array',
181 'quick_form_type' => 'Element',
182 'html_type' => 'advmultiselect',
183 'default' => ['CiviEvent', 'CiviContribute'],
184 'add' => '4.4',
185 'title' => 'List of Components',
186 'is_domain' => '1',
187 'is_contact' => 0,
188 'description' => NULL,
189 'help_text' => NULL,
190 // list of callbacks
191 'on_change' => [
192 [__CLASS__, '_testOnChange_onChangeExample'],
193 ],
194 ],
195 ]);
196
197 // set initial value
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']);
203
204 // change value
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']);
211 }
212
213 /**
214 * Mock callback for a setting's on_change handler
215 *
216 * @param $oldValue
217 * @param $newValue
218 * @param $metadata
219 */
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;
226 }
227
228 /**
229 * Test to set isProductionEnvironment
230 *
231 */
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);
241 }
242
243 /**
244 * Test that options defined as a pseudoconstant can be converted to options.
245 */
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]);
250 }
251
252 }