Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
7d61e75f | 4 | | Copyright CiviCRM LLC. All rights reserved. | |
6a488035 | 5 | | | |
7d61e75f TO |
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 | | |
6a488035 | 9 | +--------------------------------------------------------------------+ |
d25dd0ee | 10 | */ |
6a488035 | 11 | |
e9479dcf EM |
12 | /** |
13 | * Class CRM_Core_BAO_SettingTest | |
acb109b7 | 14 | * @group headless |
e9479dcf | 15 | */ |
6a488035 | 16 | class CRM_Core_BAO_SettingTest extends CiviUnitTestCase { |
39b959db | 17 | |
356013b6 | 18 | /** |
19 | * Original value of civicrm_setting global. | |
20 | * @var array | |
21 | */ | |
22 | private $origSetting; | |
23 | ||
00be9182 | 24 | public function setUp() { |
6a488035 TO |
25 | parent::setUp(); |
26 | global $civicrm_setting; | |
27 | $this->origSetting = $civicrm_setting; | |
356013b6 | 28 | CRM_Utils_Cache::singleton()->clear(); |
6a488035 TO |
29 | } |
30 | ||
356013b6 | 31 | /** |
32 | * Clean up after test. | |
33 | * | |
34 | * @throws \CRM_Core_Exception | |
35 | */ | |
00be9182 | 36 | public function tearDown() { |
6a488035 TO |
37 | global $civicrm_setting; |
38 | $civicrm_setting = $this->origSetting; | |
356013b6 | 39 | $this->quickCleanup(['civicrm_contribution']); |
40 | CRM_Utils_Cache::singleton()->clear(); | |
6a488035 TO |
41 | parent::tearDown(); |
42 | } | |
43 | ||
ff6f993e | 44 | /** |
45 | * Test that enabling a valid component works. | |
46 | */ | |
00be9182 | 47 | public function testEnableComponentValid() { |
ff6f993e | 48 | CRM_Core_Config::singleton(TRUE, TRUE); |
6a488035 | 49 | $result = CRM_Core_BAO_ConfigSetting::enableComponent('CiviCampaign'); |
6a488035 TO |
50 | $this->assertTrue($result); |
51 | } | |
52 | ||
ff6f993e | 53 | /** |
54 | * Test that we get a success result if we try to enable an enabled component. | |
55 | */ | |
00be9182 | 56 | public function testEnableComponentAlreadyPresent() { |
ff6f993e | 57 | CRM_Core_Config::singleton(TRUE, TRUE); |
58 | CRM_Core_BAO_ConfigSetting::enableComponent('CiviCampaign'); | |
6a488035 | 59 | $result = CRM_Core_BAO_ConfigSetting::enableComponent('CiviCampaign'); |
6a488035 TO |
60 | $this->assertTrue($result); |
61 | } | |
62 | ||
ff6f993e | 63 | /** |
64 | * Test that we get a false result if we try to enable an invalid component. | |
65 | */ | |
00be9182 | 66 | public function testEnableComponentInvalid() { |
ff6f993e | 67 | CRM_Core_Config::singleton(TRUE, TRUE); |
6a488035 | 68 | $result = CRM_Core_BAO_ConfigSetting::enableComponent('CiviFake'); |
6a488035 TO |
69 | $this->assertFalse($result); |
70 | } | |
71 | ||
ff6f993e | 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', | |
ecda7f33 | 94 | // NOTE: This form of `invoicing` is accepted, but it may be normalized to the idiomatic form with a nested array. |
ff6f993e | 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); | |
ecda7f33 | 104 | $this->assertEquals(['invoicing' => ['invoicing' => 1]] + $contributionSettings, $settingsFromGet); |
ff6f993e | 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 | ||
6a488035 TO |
116 | /** |
117 | * Ensure that overrides in $civicrm_setting apply when | |
118 | * using getItem($group,$name). | |
119 | */ | |
00be9182 | 120 | public function testGetItem_Override() { |
6a488035 TO |
121 | global $civicrm_setting; |
122 | $civicrm_setting[CRM_Core_BAO_Setting::DIRECTORY_PREFERENCES_NAME]['imageUploadDir'] = '/test/override'; | |
8eb5d071 | 123 | Civi::service('settings_manager')->useMandatory(); |
6a488035 TO |
124 | $value = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::DIRECTORY_PREFERENCES_NAME, 'imageUploadDir'); |
125 | $this->assertEquals('/test/override', $value); | |
bf83df95 | 126 | |
127 | // CRM-14974 test suite | |
128 | $civicrm_setting['Test Preferences']['overrideSetting'] = '/test/override'; | |
8eb5d071 | 129 | Civi::service('settings_manager')->useMandatory(); |
bf83df95 | 130 | $values = CRM_Core_BAO_Setting::getItem('Test Preferences'); |
131 | $this->assertEquals('/test/override', $values['overrideSetting']); | |
08ef4ddd | 132 | Civi::settings()->set('databaseSetting', '/test/database'); |
bf83df95 | 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'; | |
8eb5d071 | 137 | Civi::service('settings_manager')->useMandatory(); |
bf83df95 | 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']); | |
f806379b | 143 | $query = "DELETE FROM civicrm_setting WHERE name IN ('overrideSetting', 'databaseSetting');"; |
bf83df95 | 144 | CRM_Core_DAO::executeQuery($query); |
6a488035 TO |
145 | } |
146 | ||
147 | /** | |
148 | * Ensure that overrides in $civicrm_setting apply when | |
149 | * using getItem($group). | |
150 | */ | |
00be9182 | 151 | public function testGetItemGroup_Override() { |
6a488035 TO |
152 | global $civicrm_setting; |
153 | $civicrm_setting[CRM_Core_BAO_Setting::DIRECTORY_PREFERENCES_NAME]['imageUploadDir'] = '/test/override'; | |
8eb5d071 | 154 | Civi::service('settings_manager')->useMandatory(); |
6a488035 TO |
155 | $values = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::DIRECTORY_PREFERENCES_NAME); |
156 | $this->assertEquals('/test/override', $values['imageUploadDir']); | |
157 | } | |
158 | ||
76bd16ab TO |
159 | public function testDefaults() { |
160 | CRM_Core_DAO::executeQuery('DELETE FROM civicrm_setting WHERE name = "max_attachments"'); | |
e1d39824 | 161 | Civi::service('settings_manager')->flush(); |
76bd16ab TO |
162 | $this->assertEquals(3, Civi::settings()->get('max_attachments')); |
163 | $this->assertEquals(3, CRM_Core_Config::singleton()->maxAttachments); | |
6a488035 | 164 | } |
6a488035 | 165 | |
076d8c82 TO |
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 | */ | |
00be9182 | 173 | public function testOnChange() { |
076d8c82 | 174 | global $_testOnChange_hookCalls; |
9099cab3 CW |
175 | $this->setMockSettingsMetaData([ |
176 | 'onChangeExample' => [ | |
076d8c82 TO |
177 | 'group_name' => 'CiviCRM Preferences', |
178 | 'group' => 'core', | |
179 | 'name' => 'onChangeExample', | |
180 | 'type' => 'Array', | |
181 | 'quick_form_type' => 'Element', | |
182 | 'html_type' => 'advmultiselect', | |
9099cab3 | 183 | 'default' => ['CiviEvent', 'CiviContribute'], |
076d8c82 TO |
184 | 'add' => '4.4', |
185 | 'title' => 'List of Components', | |
186 | 'is_domain' => '1', | |
187 | 'is_contact' => 0, | |
188 | 'description' => NULL, | |
189 | 'help_text' => NULL, | |
39b959db | 190 | // list of callbacks |
9099cab3 CW |
191 | 'on_change' => [ |
192 | [__CLASS__, '_testOnChange_onChangeExample'], | |
193 | ], | |
194 | ], | |
195 | ]); | |
076d8c82 TO |
196 | |
197 | // set initial value | |
9099cab3 CW |
198 | $_testOnChange_hookCalls = ['count' => 0]; |
199 | Civi::settings()->set('onChangeExample', ['First', 'Value']); | |
076d8c82 | 200 | $this->assertEquals(1, $_testOnChange_hookCalls['count']); |
9099cab3 | 201 | $this->assertEquals(['First', 'Value'], $_testOnChange_hookCalls['newValue']); |
076d8c82 TO |
202 | $this->assertEquals('List of Components', $_testOnChange_hookCalls['metadata']['title']); |
203 | ||
204 | // change value | |
9099cab3 CW |
205 | $_testOnChange_hookCalls = ['count' => 0]; |
206 | Civi::settings()->set('onChangeExample', ['Second', 'Value']); | |
076d8c82 | 207 | $this->assertEquals(1, $_testOnChange_hookCalls['count']); |
9099cab3 CW |
208 | $this->assertEquals(['First', 'Value'], $_testOnChange_hookCalls['oldValue']); |
209 | $this->assertEquals(['Second', 'Value'], $_testOnChange_hookCalls['newValue']); | |
076d8c82 TO |
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 | */ | |
00be9182 | 220 | public static function _testOnChange_onChangeExample($oldValue, $newValue, $metadata) { |
076d8c82 TO |
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 | ||
f008885c E |
228 | /** |
229 | * Test to set isProductionEnvironment | |
230 | * | |
231 | */ | |
232 | public function testSetCivicrmEnvironment() { | |
b6588f6a | 233 | Civi::settings()->set('environment', 'Staging'); |
234 | $values = Civi::settings()->get('environment'); | |
f008885c | 235 | $this->assertEquals('Staging', $values); |
8a078f99 SL |
236 | global $civicrm_setting; |
237 | $civicrm_setting[CRM_Core_BAO_Setting::DEVELOPER_PREFERENCES_NAME]['environment'] = 'Development'; | |
238 | Civi::service('settings_manager')->useMandatory(); | |
f008885c E |
239 | $environment = CRM_Core_Config::environment(); |
240 | $this->assertEquals('Development', $environment); | |
241 | } | |
242 | ||
356013b6 | 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 | ||
6a488035 | 252 | } |