CRM-19672 - fix Settings code wierdness in some circumstances
[civicrm-core.git] / tests / phpunit / api / v3 / SettingTest.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
81621fee 4 | CiviCRM version 4.7 |
6a488035 5 +--------------------------------------------------------------------+
15a4309a 6 | Copyright CiviCRM LLC (c) 2004-2017 |
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;
354 // the caching of data to all duplicates the caching of data to the empty string
355 CRM_Core_BAO_Cache::setItem($data, 'CiviCRM setting Spec', 'All');
356 CRM_Core_BAO_Cache::setItem($data, 'CiviCRM setting Specs', 'settingsMetadata__');
3a84c0ab 357 Civi::cache('settings')->flush();
ee7b49c4
E
358 $fields = $this->callAPISuccess('setting', 'getfields', array('filters' => array('group_name' => 'Test Settings')));
359 $this->assertArrayHasKey('test_key', $fields['values']);
360 $this->callAPISuccess('setting', 'create', array('test_key' => 'keyset'));
3a84c0ab 361 $this->assertEquals('keyset', Civi::settings()->get('test_key'));
ee7b49c4
E
362 $result = $this->callAPISuccess('setting', 'getvalue', array('name' => 'test_key', 'group' => 'Test Settings'));
363 $this->assertEquals('keyset', $result);
364 }
c490a46a 365
6a488035 366 /**
100fef9d 367 * Setting api should set & fetch settings stored in config as well as those in settings table
6a488035 368 */
00be9182 369 public function testGetConfigSetting() {
7fbb4198 370 $settings = $this->callAPISuccess('setting', 'get', array(
6c6e6187 371 'name' => 'defaultCurrency',
5896d037
TO
372 'sequential' => 1,
373 )
6a488035 374 );
6a488035
TO
375 $this->assertEquals('USD', $settings['values'][0]['defaultCurrency']);
376 }
2ba45310 377
378 /**
100fef9d 379 * Setting api should set & fetch settings stored in config as well as those in settings table
2ba45310 380 */
00be9182 381 public function testGetSetConfigSettingMultipleDomains() {
7fbb4198 382 $settings = $this->callAPISuccess('setting', 'create', array(
6c6e6187 383 'defaultCurrency' => 'USD',
21dfd5f5 384 'domain_id' => $this->_currentDomain,
5896d037 385 )
2ba45310 386 );
7fbb4198 387 $settings = $this->callAPISuccess('setting', 'create', array(
6c6e6187 388 'defaultCurrency' => 'CAD',
21dfd5f5 389 'domain_id' => $this->_domainID2,
5896d037 390 )
2ba45310 391 );
7fbb4198 392 $settings = $this->callAPISuccess('setting', 'get', array(
6c6e6187 393 'return' => 'defaultCurrency',
5896d037 394 'domain_id' => 'all',
2ba45310 395 )
396 );
397 $this->assertEquals('USD', $settings['values'][$this->_currentDomain]['defaultCurrency']);
398 $this->assertEquals('CAD', $settings['values'][$this->_domainID2]['defaultCurrency'],
399 "second domain (id {$this->_domainID2} ) should be set to CAD. First dom was {$this->_currentDomain} & was USD");
400
401 }
402
c490a46a 403 /**
eceb18cc 404 * Use getValue against a config setting.
c490a46a 405 */
00be9182 406 public function testGetValueConfigSetting() {
6c6e6187 407 $params = array(
5896d037 408 'name' => 'monetaryThousandSeparator',
6a488035
TO
409 'group' => 'Localization Setting',
410 );
7fbb4198 411 $result = $this->callAPISuccess('setting', 'getvalue', $params);
6a488035
TO
412 $this->assertEquals(',', $result);
413 }
414
00be9182 415 public function testGetValue() {
6c6e6187 416 $params = array(
5896d037 417 'name' => 'petition_contacts',
21dfd5f5 418 'group' => 'Campaign Preferences',
6a488035 419 );
5c49fee0 420 $description = "Demonstrates getvalue action - intended for runtime use as better caching than get.";
7fbb4198 421
422 $result = $this->callAPIAndDocument('setting', 'getvalue', $params, __FUNCTION__, __FILE__, $description);
423 $this->assertEquals('Petition Contacts', $result);
6a488035
TO
424 }
425
00be9182 426 public function testGetDefaults() {
5c49fee0 427 $description = "Gets defaults setting a variable for a given domain - if no domain is set current is assumed.";
6a488035 428
7fbb4198 429 $params = array(
6a488035
TO
430 'name' => 'address_format',
431 );
a828d7b8 432 $result = $this->callAPIAndDocument('setting', 'getdefaults', $params, __FUNCTION__, __FILE__, $description, 'GetDefaults');
6a488035 433 $this->assertEquals("{contact.address_name}\n{contact.street_address}\n{contact.supplemental_address_1}\n{contact.supplemental_address_2}\n{contact.city}{, }{contact.state_province}{ }{contact.postal_code}\n{contact.country}", $result['values'][CRM_Core_Config::domainID()]['address_format']);
6c6e6187 434 $params = array('name' => 'mailing_format');
7fbb4198 435 $result = $this->callAPISuccess('setting', 'getdefaults', $params);
6a488035
TO
436 $this->assertEquals("{contact.addressee}\n{contact.street_address}\n{contact.supplemental_address_1}\n{contact.supplemental_address_2}\n{contact.city}{, }{contact.state_province}{ }{contact.postal_code}\n{contact.country}", $result['values'][CRM_Core_Config::domainID()]['mailing_format']);
437 $this->assertArrayHasKey(CRM_Core_Config::domainID(), $result['values']);
438 }
c490a46a
CW
439
440 /**
eceb18cc 441 * Function tests reverting a specific parameter.
6a488035 442 */
00be9182 443 public function testRevert() {
6c6e6187 444 $params = array(
5896d037 445 'address_format' => 'xyz',
6a488035
TO
446 'mailing_format' => 'bcs',
447 );
7fbb4198 448 $result = $this->callAPISuccess('setting', 'create', $params);
6a488035 449 $this->assertAPISuccess($result, "in line " . __LINE__);
6c6e6187 450 $revertParams = array(
21dfd5f5 451 'name' => 'address_format',
6a488035 452 );
7fbb4198 453 $result = $this->callAPISuccess('setting', 'get', $params);
6a488035
TO
454 //make sure it's set
455 $this->assertEquals('xyz', $result['values'][CRM_Core_Config::domainID()]['address_format']);
5c49fee0 456 $description = "Demonstrates reverting a parameter to default value.";
a828d7b8 457 $result = $this->callAPIAndDocument('setting', 'revert', $revertParams, __FUNCTION__, __FILE__, $description, '');
6a488035 458 //make sure it's reverted
7fbb4198 459 $result = $this->callAPISuccess('setting', 'get', $params);
6a488035 460 $this->assertEquals("{contact.address_name}\n{contact.street_address}\n{contact.supplemental_address_1}\n{contact.supplemental_address_2}\n{contact.city}{, }{contact.state_province}{ }{contact.postal_code}\n{contact.country}", $result['values'][CRM_Core_Config::domainID()]['address_format']);
6c6e6187 461 $params = array(
5896d037 462 'return' => array('mailing_format'),
6a488035 463 );
7fbb4198 464 $result = $this->callAPISuccess('setting', 'get', $params);
6a488035
TO
465 //make sure it's unchanged
466 $this->assertEquals('bcs', $result['values'][CRM_Core_Config::domainID()]['mailing_format']);
467 }
c490a46a
CW
468
469 /**
6a488035
TO
470 * Tests reverting ALL parameters (specific domain)
471 */
00be9182 472 public function testRevertAll() {
6c6e6187 473 $params = array(
5896d037 474 'address_format' => 'xyz',
c490a46a 475 'mailing_format' => 'bcs',
6a488035 476 );
7fbb4198 477 $result = $this->callAPISuccess('setting', 'create', $params);
6c6e6187 478 $revertParams = array();
7fbb4198 479 $result = $this->callAPISuccess('setting', 'get', $params);
6a488035
TO
480 //make sure it's set
481 $this->assertEquals('xyz', $result['values'][CRM_Core_Config::domainID()]['address_format']);
482
7fbb4198 483 $this->callAPISuccess('setting', 'revert', $revertParams);
6a488035 484 //make sure it's reverted
7fbb4198 485 $result = $this->callAPISuccess('setting', 'get', array('group' => 'core'));
6a488035
TO
486 $this->assertEquals("{contact.address_name}\n{contact.street_address}\n{contact.supplemental_address_1}\n{contact.supplemental_address_2}\n{contact.city}{, }{contact.state_province}{ }{contact.postal_code}\n{contact.country}", $result['values'][CRM_Core_Config::domainID()]['address_format']);
487 $this->assertEquals("{contact.addressee}\n{contact.street_address}\n{contact.supplemental_address_1}\n{contact.supplemental_address_2}\n{contact.city}{, }{contact.state_province}{ }{contact.postal_code}\n{contact.country}", $result['values'][CRM_Core_Config::domainID()]['mailing_format']);
488 }
489
c490a46a 490 /**
3a84c0ab 491 * Settings should respect their defaults
c490a46a 492 */
3a84c0ab 493 public function testDefaults() {
6a488035
TO
494 $domparams = array(
495 'name' => 'B Team Domain',
609900e9 496 'domain_version' => '4.7',
c490a46a 497 );
7fbb4198 498 $dom = $this->callAPISuccess('domain', 'create', $domparams);
5896d037
TO
499 $params = array(
500 'domain_id' => 'all',
6a488035 501 );
7fbb4198 502 $result = $this->callAPISuccess('setting', 'get', $params);
6c6e6187 503 $params = array(
5896d037 504 'address_format' => 'xyz',
c490a46a
CW
505 'mailing_format' => 'bcs',
506 'domain_id' => $this->_domainID2,
6a488035 507 );
7fbb4198 508 $result = $this->callAPISuccess('setting', 'create', $params);
6c6e6187 509 $params = array(
5896d037 510 'domain_id' => $dom['id'],
6a488035 511 );
7fbb4198 512 $result = $this->callAPISuccess('setting', 'get', $params);
6a488035 513 $this->assertAPISuccess($result, "in line " . __LINE__);
3a84c0ab
TO
514 $this->assertEquals('Unconfirmed', $result['values'][$dom['id']]['tag_unconfirmed']);
515
516 // The 'fill' operation is no longer necessary, but third parties might still use it, so let's
517 // make sure it doesn't do anything weird (crashing or breaking values).
7fbb4198 518 $result = $this->callAPISuccess('setting', 'fill', $params);
6a488035 519 $this->assertAPISuccess($result, "in line " . __LINE__);
7fbb4198 520 $result = $this->callAPISuccess('setting', 'get', $params);
6a488035
TO
521 $this->assertAPISuccess($result, "in line " . __LINE__);
522 $this->assertArrayHasKey('tag_unconfirmed', $result['values'][$dom['id']]);
72174371
TO
523
524 // Setting has NULL default. Not returned.
525 //$this->assertArrayHasKey('extensionsDir', $result['values'][$dom['id']]);
526
6a488035
TO
527 $this->assertEquals('Unconfirmed', $result['values'][$dom['id']]['tag_unconfirmed']);
528 }
96025800 529
6a488035 530}