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