Merge pull request #17055 from mattwire/customgroupfield_id
[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 'invoicing' => 1,
95 'is_email_pdf' => '1',
96 ];
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);
104
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'));
113 }
114
115 /**
116 * Ensure that overrides in $civicrm_setting apply when
117 * using getItem($group,$name).
118 */
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);
125
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);
144 }
145
146 /**
147 * Ensure that overrides in $civicrm_setting apply when
148 * using getItem($group).
149 */
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']);
156 }
157
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);
163 }
164
165 /**
166 * Ensure that on_change callbacks fire.
167 *
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].
171 */
172 public function testOnChange() {
173 global $_testOnChange_hookCalls;
174 $this->setMockSettingsMetaData([
175 'onChangeExample' => [
176 'group_name' => 'CiviCRM Preferences',
177 'group' => 'core',
178 'name' => 'onChangeExample',
179 'type' => 'Array',
180 'quick_form_type' => 'Element',
181 'html_type' => 'advmultiselect',
182 'default' => ['CiviEvent', 'CiviContribute'],
183 'add' => '4.4',
184 'title' => 'List of Components',
185 'is_domain' => '1',
186 'is_contact' => 0,
187 'description' => NULL,
188 'help_text' => NULL,
189 // list of callbacks
190 'on_change' => [
191 [__CLASS__, '_testOnChange_onChangeExample'],
192 ],
193 ],
194 ]);
195
196 // set initial value
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']);
202
203 // change value
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']);
210 }
211
212 /**
213 * Mock callback for a setting's on_change handler
214 *
215 * @param $oldValue
216 * @param $newValue
217 * @param $metadata
218 */
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;
225 }
226
227 /**
228 * Test to set isProductionEnvironment
229 *
230 */
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);
240 }
241
242 /**
243 * Test that options defined as a pseudoconstant can be converted to options.
244 */
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]);
249 }
250
251 }