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