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 | |
6a488035 TO |
12 | /** |
13 | * Test APIv3 civicrm_setting_* functions | |
14 | * | |
6c6e6187 TO |
15 | * @package CiviCRM_APIv3 |
16 | * @subpackage API_Core | |
6a488035 TO |
17 | */ |
18 | ||
19 | /** | |
20 | * Class contains api test cases for civicrm settings | |
21 | * | |
acb109b7 | 22 | * @group headless |
6a488035 TO |
23 | */ |
24 | class api_v3_SettingTest extends CiviUnitTestCase { | |
25 | ||
6a488035 TO |
26 | protected $_contactID; |
27 | protected $_params; | |
28 | protected $_currentDomain; | |
29 | protected $_domainID2; | |
30 | protected $_domainID3; | |
b7c9bc4c | 31 | |
00be9182 | 32 | public function setUp() { |
6a488035 | 33 | parent::setUp(); |
9099cab3 | 34 | $params = [ |
6a488035 | 35 | 'name' => 'Default Domain Name', |
609900e9 | 36 | 'domain_version' => '4.7', |
9099cab3 | 37 | ]; |
5896d037 TO |
38 | $result = $this->callAPISuccess('domain', 'get', $params); |
39 | if (empty($result['id'])) { | |
40 | $result = $this->callAPISuccess('domain', 'create', $params); | |
6a488035 TO |
41 | } |
42 | ||
43 | $params['name'] = 'Second Domain'; | |
5896d037 TO |
44 | $result = $this->callAPISuccess('domain', 'get', $params); |
45 | if (empty($result['id'])) { | |
46 | $result = $this->callAPISuccess('domain', 'create', $params); | |
6a488035 TO |
47 | } |
48 | $this->_domainID2 = $result['id']; | |
49 | $params['name'] = 'A-team domain'; | |
5896d037 TO |
50 | $result = $this->callAPISuccess('domain', 'get', $params); |
51 | if (empty($result['id'])) { | |
52 | $result = $this->callAPISuccess('domain', 'create', $params); | |
6a488035 TO |
53 | } |
54 | $this->_domainID3 = $result['id']; | |
55 | $this->_currentDomain = CRM_Core_Config::domainID(); | |
ee7b49c4 | 56 | $this->hookClass = CRM_Utils_Hook::singleton(); |
6a488035 TO |
57 | } |
58 | ||
00be9182 | 59 | public function tearDown() { |
ee7b49c4 | 60 | CRM_Utils_Hook::singleton()->reset(); |
6a488035 | 61 | parent::tearDown(); |
9099cab3 CW |
62 | $this->callAPISuccess('system', 'flush', []); |
63 | $this->quickCleanup(['civicrm_domain']); | |
6a488035 TO |
64 | } |
65 | ||
ee7b49c4 E |
66 | /** |
67 | * Set additional settings into metadata (implements hook) | |
68 | * @param array $metaDataFolders | |
69 | */ | |
00be9182 | 70 | public function setExtensionMetadata(&$metaDataFolders) { |
ee7b49c4 E |
71 | global $civicrm_root; |
72 | $metaDataFolders[] = $civicrm_root . '/tests/phpunit/api/v3/settings'; | |
73 | } | |
5896d037 | 74 | |
ee7b49c4 | 75 | /** |
2d932085 CW |
76 | * @param int $version |
77 | * @dataProvider versionThreeAndFour | |
6a488035 | 78 | */ |
2d932085 CW |
79 | public function testGetFields($version) { |
80 | $this->_apiversion = $version; | |
6a488035 | 81 | $description = 'Demonstrate return from getfields - see subfolder for variants'; |
9099cab3 | 82 | $result = $this->callAPIAndDocument('setting', 'getfields', [], __FUNCTION__, __FILE__, $description); |
6a488035 TO |
83 | $this->assertArrayHasKey('customCSSURL', $result['values']); |
84 | ||
85 | $description = 'Demonstrate return from getfields'; | |
9099cab3 | 86 | $result = $this->callAPISuccess('setting', 'getfields', []); |
6a488035 | 87 | $this->assertArrayHasKey('customCSSURL', $result['values']); |
9099cab3 | 88 | $this->callAPISuccess('system', 'flush', []); |
6a488035 TO |
89 | } |
90 | ||
91 | /** | |
100fef9d | 92 | * Let's check it's loading from cache by meddling with the cache |
2d932085 CW |
93 | * @param int $version |
94 | * @dataProvider versionThreeAndFour | |
6a488035 | 95 | */ |
2d932085 CW |
96 | public function testGetFieldsCaching($version) { |
97 | $this->_apiversion = $version; | |
9099cab3 | 98 | $settingsMetadata = []; |
e1d39824 | 99 | Civi::cache('settings')->set('settingsMetadata_' . \CRM_Core_Config::domainID() . '_', $settingsMetadata); |
9099cab3 | 100 | $result = $this->callAPISuccess('setting', 'getfields', []); |
6a488035 | 101 | $this->assertArrayNotHasKey('customCSSURL', $result['values']); |
9099cab3 | 102 | $this->quickCleanup(['civicrm_cache']); |
72174371 | 103 | Civi::cache('settings')->flush(); |
6a488035 TO |
104 | } |
105 | ||
2d932085 CW |
106 | /** |
107 | * @param int $version | |
108 | * @dataProvider versionThreeAndFour | |
109 | */ | |
110 | public function testGetFieldsFilters($version) { | |
111 | $this->_apiversion = $version; | |
9099cab3 | 112 | $params = ['name' => 'advanced_search_options']; |
7fbb4198 | 113 | $result = $this->callAPISuccess('setting', 'getfields', $params); |
6a488035 | 114 | $this->assertArrayNotHasKey('customCSSURL', $result['values']); |
6c6e6187 | 115 | $this->assertArrayHasKey('advanced_search_options', $result['values']); |
4ed37178 E |
116 | } |
117 | ||
118 | /** | |
eceb18cc | 119 | * Test that getfields will filter on group. |
2d932085 CW |
120 | * @param int $version |
121 | * @dataProvider versionThreeAndFour | |
4ed37178 | 122 | */ |
2d932085 CW |
123 | public function testGetFieldsGroupFilters($version) { |
124 | $this->_apiversion = $version; | |
9099cab3 | 125 | $params = ['filters' => ['group' => 'multisite']]; |
4ed37178 E |
126 | $result = $this->callAPISuccess('setting', 'getfields', $params); |
127 | $this->assertArrayNotHasKey('customCSSURL', $result['values']); | |
6c6e6187 | 128 | $this->assertArrayHasKey('domain_group_id', $result['values']); |
4ed37178 E |
129 | } |
130 | ||
076d8c82 TO |
131 | /** |
132 | * Ensure that on_change callbacks fire. | |
133 | * | |
134 | * Note: api_v3_SettingTest::testOnChange and CRM_Core_BAO_SettingTest::testOnChange | |
135 | * are very similar, but they exercise different codepaths. The first uses the API | |
136 | * and setItems [plural]; the second uses setItem [singular]. | |
2d932085 CW |
137 | * @param int $version |
138 | * @dataProvider versionThreeAndFour | |
076d8c82 | 139 | */ |
2d932085 CW |
140 | public function testOnChange($version) { |
141 | $this->_apiversion = $version; | |
076d8c82 | 142 | global $_testOnChange_hookCalls; |
9099cab3 CW |
143 | $this->setMockSettingsMetaData([ |
144 | 'onChangeExample' => [ | |
076d8c82 TO |
145 | 'group_name' => 'CiviCRM Preferences', |
146 | 'group' => 'core', | |
147 | 'name' => 'onChangeExample', | |
148 | 'type' => 'Array', | |
149 | 'quick_form_type' => 'Element', | |
150 | 'html_type' => 'advmultiselect', | |
9099cab3 | 151 | 'default' => ['CiviEvent', 'CiviContribute'], |
076d8c82 TO |
152 | 'add' => '4.4', |
153 | 'title' => 'List of Components', | |
154 | 'is_domain' => '1', | |
155 | 'is_contact' => 0, | |
156 | 'description' => NULL, | |
157 | 'help_text' => NULL, | |
39b959db | 158 | // list of callbacks |
9099cab3 CW |
159 | 'on_change' => [ |
160 | [__CLASS__, '_testOnChange_onChangeExample'], | |
161 | ], | |
162 | ], | |
163 | ]); | |
076d8c82 TO |
164 | |
165 | // set initial value | |
9099cab3 CW |
166 | $_testOnChange_hookCalls = ['count' => 0]; |
167 | $this->callAPISuccess('setting', 'create', [ | |
168 | 'onChangeExample' => ['First', 'Value'], | |
169 | ]); | |
076d8c82 | 170 | $this->assertEquals(1, $_testOnChange_hookCalls['count']); |
9099cab3 | 171 | $this->assertEquals(['First', 'Value'], $_testOnChange_hookCalls['newValue']); |
076d8c82 TO |
172 | $this->assertEquals('List of Components', $_testOnChange_hookCalls['metadata']['title']); |
173 | ||
174 | // change value | |
9099cab3 CW |
175 | $_testOnChange_hookCalls = ['count' => 0]; |
176 | $this->callAPISuccess('setting', 'create', [ | |
177 | 'onChangeExample' => ['Second', 'Value'], | |
178 | ]); | |
076d8c82 | 179 | $this->assertEquals(1, $_testOnChange_hookCalls['count']); |
9099cab3 CW |
180 | $this->assertEquals(['First', 'Value'], $_testOnChange_hookCalls['oldValue']); |
181 | $this->assertEquals(['Second', 'Value'], $_testOnChange_hookCalls['newValue']); | |
076d8c82 TO |
182 | $this->assertEquals('List of Components', $_testOnChange_hookCalls['metadata']['title']); |
183 | } | |
184 | ||
185 | /** | |
186 | * Mock callback for a setting's on_change handler | |
187 | * | |
188 | * @param $oldValue | |
189 | * @param $newValue | |
190 | * @param $metadata | |
191 | */ | |
00be9182 | 192 | public static function _testOnChange_onChangeExample($oldValue, $newValue, $metadata) { |
076d8c82 TO |
193 | global $_testOnChange_hookCalls; |
194 | $_testOnChange_hookCalls['count']++; | |
195 | $_testOnChange_hookCalls['oldValue'] = $oldValue; | |
196 | $_testOnChange_hookCalls['newValue'] = $newValue; | |
197 | $_testOnChange_hookCalls['metadata'] = $metadata; | |
198 | } | |
199 | ||
6a488035 | 200 | /** |
2d932085 CW |
201 | * @param int $version |
202 | * @dataProvider versionThreeAndFour | |
6a488035 | 203 | */ |
2d932085 CW |
204 | public function testCreateSetting($version) { |
205 | $this->_apiversion = $version; | |
5c49fee0 | 206 | $description = "Shows setting a variable for a given domain - if no domain is set current is assumed."; |
6a488035 | 207 | |
9099cab3 | 208 | $params = [ |
c490a46a CW |
209 | 'domain_id' => $this->_domainID2, |
210 | 'uniq_email_per_site' => 1, | |
9099cab3 | 211 | ]; |
7fbb4198 | 212 | $result = $this->callAPIAndDocument('setting', 'create', $params, __FUNCTION__, __FILE__); |
6a488035 | 213 | |
9099cab3 | 214 | $params = ['uniq_email_per_site' => 1]; |
5c49fee0 | 215 | $description = "Shows setting a variable for a current domain."; |
7fbb4198 | 216 | $result = $this->callAPIAndDocument('setting', 'create', $params, __FUNCTION__, __FILE__, $description, 'CreateSettingCurrentDomain'); |
6a488035 TO |
217 | $this->assertArrayHasKey(CRM_Core_Config::domainID(), $result['values']); |
218 | } | |
219 | ||
220 | /** | |
2d932085 CW |
221 | * @param int $version |
222 | * @dataProvider versionThreeAndFour | |
6a488035 | 223 | */ |
2d932085 CW |
224 | public function testCreateInvalidSettings($version) { |
225 | $this->_apiversion = $version; | |
9099cab3 | 226 | $params = [ |
c490a46a CW |
227 | 'domain_id' => $this->_domainID2, |
228 | 'invalid_key' => 1, | |
9099cab3 | 229 | ]; |
c490a46a CW |
230 | $result = $this->callAPIFailure('setting', 'create', $params); |
231 | } | |
6a488035 | 232 | |
c490a46a | 233 | /** |
100fef9d | 234 | * Check invalid settings rejected - |
2d932085 CW |
235 | * @param int $version |
236 | * @dataProvider versionThreeAndFour | |
c490a46a | 237 | */ |
2d932085 CW |
238 | public function testCreateInvalidURLSettings($version) { |
239 | $this->_apiversion = $version; | |
9099cab3 | 240 | $params = [ |
c490a46a | 241 | 'domain_id' => $this->_domainID2, |
a5521960 | 242 | 'userFrameworkResourceURL' => 'dfhkd hfd', |
9099cab3 | 243 | ]; |
d0e1eff2 | 244 | $result = $this->callAPIFailure('setting', 'create', $params); |
9099cab3 | 245 | $params = [ |
c490a46a CW |
246 | 'domain_id' => $this->_domainID2, |
247 | 'userFrameworkResourceURL' => 'http://blah.com', | |
9099cab3 | 248 | ]; |
c490a46a CW |
249 | $result = $this->callAPISuccess('setting', 'create', $params); |
250 | } | |
251 | ||
252 | /** | |
2d932085 CW |
253 | * @param int $version |
254 | * @dataProvider versionThreeAndFour | |
c490a46a | 255 | */ |
2d932085 CW |
256 | public function testCreateInvalidBooleanSettings($version) { |
257 | $this->_apiversion = $version; | |
9099cab3 | 258 | $params = [ |
c490a46a CW |
259 | 'domain_id' => $this->_domainID2, |
260 | 'track_civimail_replies' => 'dfhkdhfd', | |
9099cab3 | 261 | ]; |
c490a46a CW |
262 | $result = $this->callAPIFailure('setting', 'create', $params); |
263 | ||
9099cab3 | 264 | $params = ['track_civimail_replies' => '0']; |
c490a46a | 265 | $result = $this->callAPISuccess('setting', 'create', $params); |
2d932085 | 266 | $getResult = $this->callAPISuccess('setting', 'get'); |
c490a46a CW |
267 | $this->assertEquals(0, $getResult['values'][$this->_currentDomain]['track_civimail_replies']); |
268 | ||
2d932085 | 269 | $getResult = $this->callAPISuccess('setting', 'get'); |
c490a46a | 270 | $this->assertEquals(0, $getResult['values'][$this->_currentDomain]['track_civimail_replies']); |
9099cab3 | 271 | $params = [ |
5896d037 | 272 | 'domain_id' => $this->_domainID2, |
c490a46a | 273 | 'track_civimail_replies' => '1', |
9099cab3 | 274 | ]; |
c490a46a | 275 | $result = $this->callAPISuccess('setting', 'create', $params); |
2d932085 | 276 | $getResult = $this->callAPISuccess('setting', 'get', ['domain_id' => $this->_domainID2]); |
c490a46a CW |
277 | $this->assertEquals(1, $getResult['values'][$this->_domainID2]['track_civimail_replies']); |
278 | ||
9099cab3 | 279 | $params = [ |
c490a46a CW |
280 | 'domain_id' => $this->_domainID2, |
281 | 'track_civimail_replies' => 'TRUE', | |
9099cab3 | 282 | ]; |
c490a46a | 283 | $result = $this->callAPISuccess('setting', 'create', $params); |
2d932085 | 284 | $getResult = $this->callAPISuccess('setting', 'get', ['domain_id' => $this->_domainID2]); |
c490a46a CW |
285 | |
286 | $this->assertEquals(1, $getResult['values'][$this->_domainID2]['track_civimail_replies'], "check TRUE is converted to 1"); | |
287 | } | |
6a488035 TO |
288 | |
289 | /** | |
2d932085 CW |
290 | * @param int $version |
291 | * @dataProvider versionThreeAndFour | |
6a488035 | 292 | */ |
2d932085 CW |
293 | public function testCreateSettingMultipleDomains($version) { |
294 | $this->_apiversion = $version; | |
5c49fee0 | 295 | $description = "Shows setting a variable for all domains."; |
6a488035 | 296 | |
9099cab3 | 297 | $params = [ |
c490a46a CW |
298 | 'domain_id' => 'all', |
299 | 'uniq_email_per_site' => 1, | |
9099cab3 | 300 | ]; |
6c6e6187 | 301 | $result = $this->callAPIAndDocument('setting', 'create', $params, __FUNCTION__, __FILE__, $description, 'CreateAllDomains'); |
7fbb4198 | 302 | |
6a488035 TO |
303 | $this->assertEquals(1, $result['values'][2]['uniq_email_per_site']); |
304 | $this->assertEquals(1, $result['values'][1]['uniq_email_per_site']); | |
305 | $this->assertArrayHasKey(3, $result['values'], 'Domain create probably failed Debug this IF domain test is passing'); | |
306 | $this->assertEquals(1, $result['values'][3]['uniq_email_per_site'], 'failed to set setting for domain 3.'); | |
307 | ||
9099cab3 | 308 | $params = [ |
c490a46a | 309 | 'domain_id' => 'all', |
21dfd5f5 | 310 | 'return' => 'uniq_email_per_site', |
9099cab3 | 311 | ]; |
6a488035 | 312 | // we'll check it with a 'get' |
5c49fee0 | 313 | $description = "Shows getting a variable for all domains."; |
a828d7b8 | 314 | $result = $this->callAPIAndDocument('setting', 'get', $params, __FUNCTION__, __FILE__, $description, 'GetAllDomains'); |
7fbb4198 | 315 | |
6a488035 TO |
316 | $this->assertEquals(1, $result['values'][2]['uniq_email_per_site']); |
317 | $this->assertEquals(1, $result['values'][1]['uniq_email_per_site']); | |
318 | $this->assertEquals(1, $result['values'][3]['uniq_email_per_site']); | |
319 | ||
9099cab3 CW |
320 | $params = [ |
321 | 'domain_id' => [1, 3], | |
c490a46a | 322 | 'uniq_email_per_site' => 0, |
9099cab3 | 323 | ]; |
5c49fee0 | 324 | $description = "Shows setting a variable for specified domains."; |
6c6e6187 | 325 | $result = $this->callAPIAndDocument('setting', 'create', $params, __FUNCTION__, __FILE__, $description, 'CreateSpecifiedDomains'); |
7fbb4198 | 326 | |
6a488035 TO |
327 | $this->assertEquals(0, $result['values'][3]['uniq_email_per_site']); |
328 | $this->assertEquals(0, $result['values'][1]['uniq_email_per_site']); | |
9099cab3 CW |
329 | $params = [ |
330 | 'domain_id' => [1, 2], | |
331 | 'return' => ['uniq_email_per_site'], | |
332 | ]; | |
5c49fee0 | 333 | $description = "Shows getting a variable for specified domains."; |
a828d7b8 | 334 | $result = $this->callAPIAndDocument('setting', 'get', $params, __FUNCTION__, __FILE__, $description, 'GetSpecifiedDomains'); |
6a488035 TO |
335 | $this->assertEquals(1, $result['values'][2]['uniq_email_per_site']); |
336 | $this->assertEquals(0, $result['values'][1]['uniq_email_per_site']); | |
337 | ||
338 | } | |
339 | ||
2d932085 CW |
340 | /** |
341 | * @param int $version | |
342 | * @dataProvider versionThreeAndFour | |
343 | */ | |
344 | public function testGetSetting($version) { | |
345 | $this->_apiversion = $version; | |
9099cab3 | 346 | $params = [ |
7fbb4198 | 347 | 'domain_id' => $this->_domainID2, |
348 | 'return' => 'uniq_email_per_site', | |
9099cab3 | 349 | ]; |
5c49fee0 | 350 | $description = "Shows get setting a variable for a given domain - if no domain is set current is assumed."; |
7fbb4198 | 351 | |
6c6e6187 | 352 | $result = $this->callAPIAndDocument('setting', 'get', $params, __FUNCTION__, __FILE__); |
6a488035 | 353 | |
9099cab3 | 354 | $params = [ |
6a488035 | 355 | 'return' => 'uniq_email_per_site', |
9099cab3 | 356 | ]; |
5c49fee0 | 357 | $description = "Shows getting a variable for a current domain."; |
6c6e6187 | 358 | $result = $this->callAPIAndDocument('setting', 'get', $params, __FUNCTION__, __FILE__, $description, 'GetSettingCurrentDomain'); |
6a488035 TO |
359 | $this->assertArrayHasKey(CRM_Core_Config::domainID(), $result['values']); |
360 | } | |
ee7b49c4 E |
361 | |
362 | /** | |
eceb18cc | 363 | * Check that setting defined in extension can be retrieved. |
2d932085 CW |
364 | * @param int $version |
365 | * @dataProvider versionThreeAndFour | |
ee7b49c4 | 366 | */ |
2d932085 CW |
367 | public function testGetExtensionSetting($version) { |
368 | $this->_apiversion = $version; | |
9099cab3 | 369 | $this->hookClass->setHook('civicrm_alterSettingsFolders', [$this, 'setExtensionMetadata']); |
ee7b49c4 | 370 | $data = NULL; |
3a84c0ab | 371 | Civi::cache('settings')->flush(); |
9099cab3 | 372 | $fields = $this->callAPISuccess('setting', 'getfields', ['filters' => ['group_name' => 'Test Settings']]); |
ee7b49c4 | 373 | $this->assertArrayHasKey('test_key', $fields['values']); |
9099cab3 | 374 | $this->callAPISuccess('setting', 'create', ['test_key' => 'keyset']); |
3a84c0ab | 375 | $this->assertEquals('keyset', Civi::settings()->get('test_key')); |
9099cab3 | 376 | $result = $this->callAPISuccess('setting', 'getvalue', ['name' => 'test_key', 'group' => 'Test Settings']); |
ee7b49c4 E |
377 | $this->assertEquals('keyset', $result); |
378 | } | |
c490a46a | 379 | |
6a488035 | 380 | /** |
100fef9d | 381 | * Setting api should set & fetch settings stored in config as well as those in settings table |
2d932085 CW |
382 | * @param int $version |
383 | * @dataProvider versionThreeAndFour | |
6a488035 | 384 | */ |
2d932085 CW |
385 | public function testGetConfigSetting($version) { |
386 | $this->_apiversion = $version; | |
9099cab3 | 387 | $settings = $this->callAPISuccess('setting', 'get', [ |
39b959db SL |
388 | 'name' => 'defaultCurrency', |
389 | 'sequential' => 1, | |
9099cab3 | 390 | ]); |
6a488035 TO |
391 | $this->assertEquals('USD', $settings['values'][0]['defaultCurrency']); |
392 | } | |
2ba45310 | 393 | |
394 | /** | |
100fef9d | 395 | * Setting api should set & fetch settings stored in config as well as those in settings table |
2d932085 CW |
396 | * @param int $version |
397 | * @dataProvider versionThreeAndFour | |
2ba45310 | 398 | */ |
2d932085 CW |
399 | public function testGetSetConfigSettingMultipleDomains($version) { |
400 | $this->_apiversion = $version; | |
9099cab3 | 401 | $settings = $this->callAPISuccess('setting', 'create', [ |
39b959db SL |
402 | 'defaultCurrency' => 'USD', |
403 | 'domain_id' => $this->_currentDomain, | |
9099cab3 CW |
404 | ]); |
405 | $settings = $this->callAPISuccess('setting', 'create', [ | |
39b959db SL |
406 | 'defaultCurrency' => 'CAD', |
407 | 'domain_id' => $this->_domainID2, | |
9099cab3 CW |
408 | ]); |
409 | $settings = $this->callAPISuccess('setting', 'get', [ | |
39b959db SL |
410 | 'return' => 'defaultCurrency', |
411 | 'domain_id' => 'all', | |
9099cab3 | 412 | ]); |
2ba45310 | 413 | $this->assertEquals('USD', $settings['values'][$this->_currentDomain]['defaultCurrency']); |
414 | $this->assertEquals('CAD', $settings['values'][$this->_domainID2]['defaultCurrency'], | |
415 | "second domain (id {$this->_domainID2} ) should be set to CAD. First dom was {$this->_currentDomain} & was USD"); | |
416 | ||
417 | } | |
418 | ||
c490a46a | 419 | /** |
eceb18cc | 420 | * Use getValue against a config setting. |
2d932085 CW |
421 | * @param int $version |
422 | * @dataProvider versionThreeAndFour | |
c490a46a | 423 | */ |
2d932085 CW |
424 | public function testGetValueConfigSetting($version) { |
425 | $this->_apiversion = $version; | |
9099cab3 | 426 | $params = [ |
5896d037 | 427 | 'name' => 'monetaryThousandSeparator', |
6a488035 | 428 | 'group' => 'Localization Setting', |
9099cab3 | 429 | ]; |
7fbb4198 | 430 | $result = $this->callAPISuccess('setting', 'getvalue', $params); |
6a488035 TO |
431 | $this->assertEquals(',', $result); |
432 | } | |
433 | ||
2d932085 CW |
434 | /** |
435 | * @param int $version | |
436 | * @dataProvider versionThreeAndFour | |
437 | */ | |
438 | public function testGetValue($version) { | |
439 | $this->_apiversion = $version; | |
9099cab3 | 440 | $params = [ |
5896d037 | 441 | 'name' => 'petition_contacts', |
21dfd5f5 | 442 | 'group' => 'Campaign Preferences', |
9099cab3 | 443 | ]; |
5c49fee0 | 444 | $description = "Demonstrates getvalue action - intended for runtime use as better caching than get."; |
7fbb4198 | 445 | |
446 | $result = $this->callAPIAndDocument('setting', 'getvalue', $params, __FUNCTION__, __FILE__, $description); | |
447 | $this->assertEquals('Petition Contacts', $result); | |
6a488035 TO |
448 | } |
449 | ||
2d932085 CW |
450 | /** |
451 | * V3 only - no api4 equivalent. | |
452 | */ | |
00be9182 | 453 | public function testGetDefaults() { |
5c49fee0 | 454 | $description = "Gets defaults setting a variable for a given domain - if no domain is set current is assumed."; |
6a488035 | 455 | |
9099cab3 | 456 | $params = [ |
6a488035 | 457 | 'name' => 'address_format', |
9099cab3 | 458 | ]; |
a828d7b8 | 459 | $result = $this->callAPIAndDocument('setting', 'getdefaults', $params, __FUNCTION__, __FILE__, $description, 'GetDefaults'); |
207f62c6 | 460 | $this->assertEquals("{contact.address_name}\n{contact.street_address}\n{contact.supplemental_address_1}\n{contact.supplemental_address_2}\n{contact.supplemental_address_3}\n{contact.city}{, }{contact.state_province}{ }{contact.postal_code}\n{contact.country}", $result['values'][CRM_Core_Config::domainID()]['address_format']); |
9099cab3 | 461 | $params = ['name' => 'mailing_format']; |
7fbb4198 | 462 | $result = $this->callAPISuccess('setting', 'getdefaults', $params); |
207f62c6 | 463 | $this->assertEquals("{contact.addressee}\n{contact.street_address}\n{contact.supplemental_address_1}\n{contact.supplemental_address_2}\n{contact.supplemental_address_3}\n{contact.city}{, }{contact.state_province}{ }{contact.postal_code}\n{contact.country}", $result['values'][CRM_Core_Config::domainID()]['mailing_format']); |
6a488035 TO |
464 | $this->assertArrayHasKey(CRM_Core_Config::domainID(), $result['values']); |
465 | } | |
c490a46a CW |
466 | |
467 | /** | |
eceb18cc | 468 | * Function tests reverting a specific parameter. |
2d932085 CW |
469 | * @param int $version |
470 | * @dataProvider versionThreeAndFour | |
6a488035 | 471 | */ |
2d932085 CW |
472 | public function testRevert($version) { |
473 | $this->_apiversion = $version; | |
9099cab3 | 474 | $params = [ |
5896d037 | 475 | 'address_format' => 'xyz', |
6a488035 | 476 | 'mailing_format' => 'bcs', |
9099cab3 | 477 | ]; |
7fbb4198 | 478 | $result = $this->callAPISuccess('setting', 'create', $params); |
6a488035 | 479 | $this->assertAPISuccess($result, "in line " . __LINE__); |
9099cab3 | 480 | $revertParams = [ |
21dfd5f5 | 481 | 'name' => 'address_format', |
9099cab3 | 482 | ]; |
2d932085 | 483 | $result = $this->callAPISuccess('setting', 'get'); |
6a488035 TO |
484 | //make sure it's set |
485 | $this->assertEquals('xyz', $result['values'][CRM_Core_Config::domainID()]['address_format']); | |
5c49fee0 | 486 | $description = "Demonstrates reverting a parameter to default value."; |
a828d7b8 | 487 | $result = $this->callAPIAndDocument('setting', 'revert', $revertParams, __FUNCTION__, __FILE__, $description, ''); |
6a488035 | 488 | //make sure it's reverted |
2d932085 | 489 | $result = $this->callAPISuccess('setting', 'get'); |
207f62c6 | 490 | $this->assertEquals("{contact.address_name}\n{contact.street_address}\n{contact.supplemental_address_1}\n{contact.supplemental_address_2}\n{contact.supplemental_address_3}\n{contact.city}{, }{contact.state_province}{ }{contact.postal_code}\n{contact.country}", $result['values'][CRM_Core_Config::domainID()]['address_format']); |
9099cab3 CW |
491 | $params = [ |
492 | 'return' => ['mailing_format'], | |
493 | ]; | |
7fbb4198 | 494 | $result = $this->callAPISuccess('setting', 'get', $params); |
6a488035 TO |
495 | //make sure it's unchanged |
496 | $this->assertEquals('bcs', $result['values'][CRM_Core_Config::domainID()]['mailing_format']); | |
497 | } | |
c490a46a CW |
498 | |
499 | /** | |
6a488035 | 500 | * Tests reverting ALL parameters (specific domain) |
2d932085 | 501 | * Api3 only. |
6a488035 | 502 | */ |
00be9182 | 503 | public function testRevertAll() { |
9099cab3 | 504 | $params = [ |
5896d037 | 505 | 'address_format' => 'xyz', |
c490a46a | 506 | 'mailing_format' => 'bcs', |
9099cab3 | 507 | ]; |
7fbb4198 | 508 | $result = $this->callAPISuccess('setting', 'create', $params); |
9099cab3 | 509 | $revertParams = []; |
7fbb4198 | 510 | $result = $this->callAPISuccess('setting', 'get', $params); |
6a488035 TO |
511 | //make sure it's set |
512 | $this->assertEquals('xyz', $result['values'][CRM_Core_Config::domainID()]['address_format']); | |
513 | ||
7fbb4198 | 514 | $this->callAPISuccess('setting', 'revert', $revertParams); |
6a488035 | 515 | //make sure it's reverted |
9099cab3 | 516 | $result = $this->callAPISuccess('setting', 'get', ['group' => 'core']); |
207f62c6 AS |
517 | $this->assertEquals("{contact.address_name}\n{contact.street_address}\n{contact.supplemental_address_1}\n{contact.supplemental_address_2}\n{contact.supplemental_address_3}\n{contact.city}{, }{contact.state_province}{ }{contact.postal_code}\n{contact.country}", $result['values'][CRM_Core_Config::domainID()]['address_format']); |
518 | $this->assertEquals("{contact.addressee}\n{contact.street_address}\n{contact.supplemental_address_1}\n{contact.supplemental_address_2}\n{contact.supplemental_address_3}\n{contact.city}{, }{contact.state_province}{ }{contact.postal_code}\n{contact.country}", $result['values'][CRM_Core_Config::domainID()]['mailing_format']); | |
6a488035 TO |
519 | } |
520 | ||
c490a46a | 521 | /** |
3a84c0ab | 522 | * Settings should respect their defaults |
2d932085 | 523 | * V3 only - no fill action in v4 |
c490a46a | 524 | */ |
3a84c0ab | 525 | public function testDefaults() { |
9099cab3 | 526 | $domparams = [ |
6a488035 | 527 | 'name' => 'B Team Domain', |
609900e9 | 528 | 'domain_version' => '4.7', |
9099cab3 | 529 | ]; |
7fbb4198 | 530 | $dom = $this->callAPISuccess('domain', 'create', $domparams); |
9099cab3 | 531 | $params = [ |
5896d037 | 532 | 'domain_id' => 'all', |
9099cab3 | 533 | ]; |
7fbb4198 | 534 | $result = $this->callAPISuccess('setting', 'get', $params); |
9099cab3 | 535 | $params = [ |
5896d037 | 536 | 'address_format' => 'xyz', |
c490a46a CW |
537 | 'mailing_format' => 'bcs', |
538 | 'domain_id' => $this->_domainID2, | |
9099cab3 | 539 | ]; |
7fbb4198 | 540 | $result = $this->callAPISuccess('setting', 'create', $params); |
9099cab3 | 541 | $params = [ |
5896d037 | 542 | 'domain_id' => $dom['id'], |
9099cab3 | 543 | ]; |
7fbb4198 | 544 | $result = $this->callAPISuccess('setting', 'get', $params); |
6a488035 | 545 | $this->assertAPISuccess($result, "in line " . __LINE__); |
3a84c0ab TO |
546 | $this->assertEquals('Unconfirmed', $result['values'][$dom['id']]['tag_unconfirmed']); |
547 | ||
548 | // The 'fill' operation is no longer necessary, but third parties might still use it, so let's | |
549 | // make sure it doesn't do anything weird (crashing or breaking values). | |
7fbb4198 | 550 | $result = $this->callAPISuccess('setting', 'fill', $params); |
6a488035 | 551 | $this->assertAPISuccess($result, "in line " . __LINE__); |
7fbb4198 | 552 | $result = $this->callAPISuccess('setting', 'get', $params); |
6a488035 TO |
553 | $this->assertAPISuccess($result, "in line " . __LINE__); |
554 | $this->assertArrayHasKey('tag_unconfirmed', $result['values'][$dom['id']]); | |
72174371 TO |
555 | |
556 | // Setting has NULL default. Not returned. | |
557 | //$this->assertArrayHasKey('extensionsDir', $result['values'][$dom['id']]); | |
558 | ||
6a488035 TO |
559 | $this->assertEquals('Unconfirmed', $result['values'][$dom['id']]['tag_unconfirmed']); |
560 | } | |
96025800 | 561 | |
f008885c E |
562 | /** |
563 | * Test to set isProductionEnvironment | |
2d932085 CW |
564 | * @param int $version |
565 | * @dataProvider versionThreeAndFour | |
f008885c | 566 | */ |
2d932085 CW |
567 | public function testSetCivicrmEnvironment($version) { |
568 | $this->_apiversion = $version; | |
569 | global $civicrm_setting; | |
570 | unset($civicrm_setting[CRM_Core_BAO_Setting::DEVELOPER_PREFERENCES_NAME]['environment']); | |
571 | Civi::service('settings_manager')->useMandatory(); | |
9099cab3 | 572 | $params = [ |
f008885c | 573 | 'environment' => 'Staging', |
9099cab3 | 574 | ]; |
f008885c | 575 | $result = $this->callAPISuccess('Setting', 'create', $params); |
9099cab3 | 576 | $params = [ |
f008885c E |
577 | 'name' => 'environment', |
578 | 'group' => 'Developer Preferences', | |
9099cab3 | 579 | ]; |
f008885c E |
580 | $result = $this->callAPISuccess('Setting', 'getvalue', $params); |
581 | $this->assertEquals('Staging', $result); | |
582 | ||
f008885c E |
583 | $civicrm_setting[CRM_Core_BAO_Setting::DEVELOPER_PREFERENCES_NAME]['environment'] = 'Production'; |
584 | Civi::service('settings_manager')->useMandatory(); | |
585 | $result = $this->callAPISuccess('Setting', 'getvalue', $params); | |
586 | $this->assertEquals('Production', $result); | |
587 | } | |
588 | ||
6a488035 | 589 | } |