3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2018 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
29 * Test APIv3 civicrm_setting_* functions
31 * @package CiviCRM_APIv3
32 * @subpackage API_Core
36 * Class contains api test cases for civicrm settings
40 class api_v3_SettingTest
extends CiviUnitTestCase
{
42 protected $_apiversion = 3;
43 protected $_contactID;
45 protected $_currentDomain;
46 protected $_domainID2;
47 protected $_domainID3;
49 public function setUp() {
52 'name' => 'Default Domain Name',
53 'domain_version' => '4.7',
55 $result = $this->callAPISuccess('domain', 'get', $params);
56 if (empty($result['id'])) {
57 $result = $this->callAPISuccess('domain', 'create', $params);
60 $params['name'] = 'Second Domain';
61 $result = $this->callAPISuccess('domain', 'get', $params);
62 if (empty($result['id'])) {
63 $result = $this->callAPISuccess('domain', 'create', $params);
65 $this->_domainID2
= $result['id'];
66 $params['name'] = 'A-team domain';
67 $result = $this->callAPISuccess('domain', 'get', $params);
68 if (empty($result['id'])) {
69 $result = $this->callAPISuccess('domain', 'create', $params);
71 $this->_domainID3
= $result['id'];
72 $this->_currentDomain
= CRM_Core_Config
::domainID();
73 $this->hookClass
= CRM_Utils_Hook
::singleton();
76 public function tearDown() {
77 CRM_Utils_Hook
::singleton()->reset();
79 $this->callAPISuccess('system', 'flush', array());
80 $this->quickCleanup(array('civicrm_domain'));
84 * Set additional settings into metadata (implements hook)
85 * @param array $metaDataFolders
87 public function setExtensionMetadata(&$metaDataFolders) {
89 $metaDataFolders[] = $civicrm_root . '/tests/phpunit/api/v3/settings';
94 * Check getfields works.
96 public function testGetFields() {
97 $description = 'Demonstrate return from getfields - see subfolder for variants';
98 $result = $this->callAPIAndDocument('setting', 'getfields', array(), __FUNCTION__
, __FILE__
, $description);
99 $this->assertArrayHasKey('customCSSURL', $result['values']);
101 $description = 'Demonstrate return from getfields';
102 $result = $this->callAPISuccess('setting', 'getfields', array());
103 $this->assertArrayHasKey('customCSSURL', $result['values']);
104 $this->callAPISuccess('system', 'flush', array());
108 * Let's check it's loading from cache by meddling with the cache
110 public function testGetFieldsCaching() {
111 $settingsMetadata = array();
112 Civi
::cache('settings')->set('settingsMetadata_' . \CRM_Core_Config
::domainID() . '_', $settingsMetadata);
113 Civi
::cache('settings')->set(\Civi\Core\SettingsMetadata
::ALL
, $settingsMetadata);
114 $result = $this->callAPISuccess('setting', 'getfields', array());
115 $this->assertArrayNotHasKey('customCSSURL', $result['values']);
116 $this->quickCleanup(array('civicrm_cache'));
117 Civi
::cache('settings')->flush();
120 public function testGetFieldsFilters() {
121 $params = array('name' => 'advanced_search_options');
122 $result = $this->callAPISuccess('setting', 'getfields', $params);
123 $this->assertArrayNotHasKey('customCSSURL', $result['values']);
124 $this->assertArrayHasKey('advanced_search_options', $result['values']);
128 * Test that getfields will filter on group.
130 public function testGetFieldsGroupFilters() {
131 $params = array('filters' => array('group' => 'multisite'));
132 $result = $this->callAPISuccess('setting', 'getfields', $params);
133 $this->assertArrayNotHasKey('customCSSURL', $result['values']);
134 $this->assertArrayHasKey('domain_group_id', $result['values']);
138 * Ensure that on_change callbacks fire.
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].
144 public function testOnChange() {
145 global $_testOnChange_hookCalls;
146 $this->setMockSettingsMetaData(array(
147 'onChangeExample' => array(
148 'group_name' => 'CiviCRM Preferences',
150 'name' => 'onChangeExample',
152 'quick_form_type' => 'Element',
153 'html_type' => 'advmultiselect',
154 'default' => array('CiviEvent', 'CiviContribute'),
156 'title' => 'List of Components',
159 'description' => NULL,
161 'on_change' => array(// list of callbacks
162 array(__CLASS__
, '_testOnChange_onChangeExample'),
168 $_testOnChange_hookCalls = array('count' => 0);
169 $this->callAPISuccess('setting', 'create', array(
170 'onChangeExample' => array('First', 'Value'),
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']);
177 $_testOnChange_hookCalls = array('count' => 0);
178 $this->callAPISuccess('setting', 'create', array(
179 'onChangeExample' => array('Second', 'Value'),
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']);
188 * Mock callback for a setting's on_change handler
194 public static function _testOnChange_onChangeExample($oldValue, $newValue, $metadata) {
195 global $_testOnChange_hookCalls;
196 $_testOnChange_hookCalls['count']++
;
197 $_testOnChange_hookCalls['oldValue'] = $oldValue;
198 $_testOnChange_hookCalls['newValue'] = $newValue;
199 $_testOnChange_hookCalls['metadata'] = $metadata;
203 * Check getfields works.
205 public function testCreateSetting() {
206 $description = "Shows setting a variable for a given domain - if no domain is set current is assumed.";
209 'domain_id' => $this->_domainID2
,
210 'uniq_email_per_site' => 1,
212 $result = $this->callAPIAndDocument('setting', 'create', $params, __FUNCTION__
, __FILE__
);
214 $params = array('uniq_email_per_site' => 1);
215 $description = "Shows setting a variable for a current domain.";
216 $result = $this->callAPIAndDocument('setting', 'create', $params, __FUNCTION__
, __FILE__
, $description, 'CreateSettingCurrentDomain');
217 $this->assertArrayHasKey(CRM_Core_Config
::domainID(), $result['values']);
221 * Check getfields works.
223 public function testCreateInvalidSettings() {
225 'domain_id' => $this->_domainID2
,
228 $result = $this->callAPIFailure('setting', 'create', $params);
232 * Check invalid settings rejected -
234 public function testCreateInvalidURLSettings() {
236 'domain_id' => $this->_domainID2
,
237 'userFrameworkResourceURL' => 'dfhkd hfd',
239 $result = $this->callAPIFailure('setting', 'create', $params);
241 'domain_id' => $this->_domainID2
,
242 'userFrameworkResourceURL' => 'http://blah.com',
244 $result = $this->callAPISuccess('setting', 'create', $params);
248 * Check getfields works.
250 public function testCreateInvalidBooleanSettings() {
252 'domain_id' => $this->_domainID2
,
253 'track_civimail_replies' => 'dfhkdhfd',
255 $result = $this->callAPIFailure('setting', 'create', $params);
257 $params = array('track_civimail_replies' => '0');
258 $result = $this->callAPISuccess('setting', 'create', $params);
259 $getResult = $this->callAPISuccess('setting', 'get', $params);
260 $this->assertEquals(0, $getResult['values'][$this->_currentDomain
]['track_civimail_replies']);
262 $getResult = $this->callAPISuccess('setting', 'get', $params);
263 $this->assertEquals(0, $getResult['values'][$this->_currentDomain
]['track_civimail_replies']);
265 'domain_id' => $this->_domainID2
,
266 'track_civimail_replies' => '1',
268 $result = $this->callAPISuccess('setting', 'create', $params);
269 $getResult = $this->callAPISuccess('setting', 'get', $params);
270 $this->assertEquals(1, $getResult['values'][$this->_domainID2
]['track_civimail_replies']);
273 'domain_id' => $this->_domainID2
,
274 'track_civimail_replies' => 'TRUE',
276 $result = $this->callAPISuccess('setting', 'create', $params);
277 $getResult = $this->callAPISuccess('setting', 'get', $params);
279 $this->assertEquals(1, $getResult['values'][$this->_domainID2
]['track_civimail_replies'], "check TRUE is converted to 1");
283 * Check getfields works.
285 public function testCreateSettingMultipleDomains() {
286 $description = "Shows setting a variable for all domains.";
289 'domain_id' => 'all',
290 'uniq_email_per_site' => 1,
292 $result = $this->callAPIAndDocument('setting', 'create', $params, __FUNCTION__
, __FILE__
, $description, 'CreateAllDomains');
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.');
300 'domain_id' => 'all',
301 'return' => 'uniq_email_per_site',
303 // we'll check it with a 'get'
304 $description = "Shows getting a variable for all domains.";
305 $result = $this->callAPIAndDocument('setting', 'get', $params, __FUNCTION__
, __FILE__
, $description, 'GetAllDomains');
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']);
312 'domain_id' => array(1, 3),
313 'uniq_email_per_site' => 0,
315 $description = "Shows setting a variable for specified domains.";
316 $result = $this->callAPIAndDocument('setting', 'create', $params, __FUNCTION__
, __FILE__
, $description, 'CreateSpecifiedDomains');
318 $this->assertEquals(0, $result['values'][3]['uniq_email_per_site']);
319 $this->assertEquals(0, $result['values'][1]['uniq_email_per_site']);
321 'domain_id' => array(1, 2),
322 'return' => array('uniq_email_per_site'),
324 $description = "Shows getting a variable for specified domains.";
325 $result = $this->callAPIAndDocument('setting', 'get', $params, __FUNCTION__
, __FILE__
, $description, 'GetSpecifiedDomains');
326 $this->assertEquals(1, $result['values'][2]['uniq_email_per_site']);
327 $this->assertEquals(0, $result['values'][1]['uniq_email_per_site']);
331 public function testGetSetting() {
333 'domain_id' => $this->_domainID2
,
334 'return' => 'uniq_email_per_site',
336 $description = "Shows get setting a variable for a given domain - if no domain is set current is assumed.";
338 $result = $this->callAPIAndDocument('setting', 'get', $params, __FUNCTION__
, __FILE__
);
341 'return' => 'uniq_email_per_site',
343 $description = "Shows getting a variable for a current domain.";
344 $result = $this->callAPIAndDocument('setting', 'get', $params, __FUNCTION__
, __FILE__
, $description, 'GetSettingCurrentDomain');
345 $this->assertArrayHasKey(CRM_Core_Config
::domainID(), $result['values']);
349 * Check that setting defined in extension can be retrieved.
351 public function testGetExtensionSetting() {
352 $this->hookClass
->setHook('civicrm_alterSettingsFolders', array($this, 'setExtensionMetadata'));
354 Civi
::cache('settings')->flush();
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'));
358 $this->assertEquals('keyset', Civi
::settings()->get('test_key'));
359 $result = $this->callAPISuccess('setting', 'getvalue', array('name' => 'test_key', 'group' => 'Test Settings'));
360 $this->assertEquals('keyset', $result);
364 * Setting api should set & fetch settings stored in config as well as those in settings table
366 public function testGetConfigSetting() {
367 $settings = $this->callAPISuccess('setting', 'get', array(
368 'name' => 'defaultCurrency',
372 $this->assertEquals('USD', $settings['values'][0]['defaultCurrency']);
376 * Setting api should set & fetch settings stored in config as well as those in settings table
378 public function testGetSetConfigSettingMultipleDomains() {
379 $settings = $this->callAPISuccess('setting', 'create', array(
380 'defaultCurrency' => 'USD',
381 'domain_id' => $this->_currentDomain
,
384 $settings = $this->callAPISuccess('setting', 'create', array(
385 'defaultCurrency' => 'CAD',
386 'domain_id' => $this->_domainID2
,
389 $settings = $this->callAPISuccess('setting', 'get', array(
390 'return' => 'defaultCurrency',
391 'domain_id' => 'all',
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");
401 * Use getValue against a config setting.
403 public function testGetValueConfigSetting() {
405 'name' => 'monetaryThousandSeparator',
406 'group' => 'Localization Setting',
408 $result = $this->callAPISuccess('setting', 'getvalue', $params);
409 $this->assertEquals(',', $result);
412 public function testGetValue() {
414 'name' => 'petition_contacts',
415 'group' => 'Campaign Preferences',
417 $description = "Demonstrates getvalue action - intended for runtime use as better caching than get.";
419 $result = $this->callAPIAndDocument('setting', 'getvalue', $params, __FUNCTION__
, __FILE__
, $description);
420 $this->assertEquals('Petition Contacts', $result);
423 public function testGetDefaults() {
424 $description = "Gets defaults setting a variable for a given domain - if no domain is set current is assumed.";
427 'name' => 'address_format',
429 $result = $this->callAPIAndDocument('setting', 'getdefaults', $params, __FUNCTION__
, __FILE__
, $description, 'GetDefaults');
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']);
431 $params = array('name' => 'mailing_format');
432 $result = $this->callAPISuccess('setting', 'getdefaults', $params);
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']);
434 $this->assertArrayHasKey(CRM_Core_Config
::domainID(), $result['values']);
438 * Function tests reverting a specific parameter.
440 public function testRevert() {
442 'address_format' => 'xyz',
443 'mailing_format' => 'bcs',
445 $result = $this->callAPISuccess('setting', 'create', $params);
446 $this->assertAPISuccess($result, "in line " . __LINE__
);
447 $revertParams = array(
448 'name' => 'address_format',
450 $result = $this->callAPISuccess('setting', 'get', $params);
452 $this->assertEquals('xyz', $result['values'][CRM_Core_Config
::domainID()]['address_format']);
453 $description = "Demonstrates reverting a parameter to default value.";
454 $result = $this->callAPIAndDocument('setting', 'revert', $revertParams, __FUNCTION__
, __FILE__
, $description, '');
455 //make sure it's reverted
456 $result = $this->callAPISuccess('setting', 'get', $params);
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']);
459 'return' => array('mailing_format'),
461 $result = $this->callAPISuccess('setting', 'get', $params);
462 //make sure it's unchanged
463 $this->assertEquals('bcs', $result['values'][CRM_Core_Config
::domainID()]['mailing_format']);
467 * Tests reverting ALL parameters (specific domain)
469 public function testRevertAll() {
471 'address_format' => 'xyz',
472 'mailing_format' => 'bcs',
474 $result = $this->callAPISuccess('setting', 'create', $params);
475 $revertParams = array();
476 $result = $this->callAPISuccess('setting', 'get', $params);
478 $this->assertEquals('xyz', $result['values'][CRM_Core_Config
::domainID()]['address_format']);
480 $this->callAPISuccess('setting', 'revert', $revertParams);
481 //make sure it's reverted
482 $result = $this->callAPISuccess('setting', 'get', array('group' => 'core'));
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']);
488 * Settings should respect their defaults
490 public function testDefaults() {
492 'name' => 'B Team Domain',
493 'domain_version' => '4.7',
495 $dom = $this->callAPISuccess('domain', 'create', $domparams);
497 'domain_id' => 'all',
499 $result = $this->callAPISuccess('setting', 'get', $params);
501 'address_format' => 'xyz',
502 'mailing_format' => 'bcs',
503 'domain_id' => $this->_domainID2
,
505 $result = $this->callAPISuccess('setting', 'create', $params);
507 'domain_id' => $dom['id'],
509 $result = $this->callAPISuccess('setting', 'get', $params);
510 $this->assertAPISuccess($result, "in line " . __LINE__
);
511 $this->assertEquals('Unconfirmed', $result['values'][$dom['id']]['tag_unconfirmed']);
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).
515 $result = $this->callAPISuccess('setting', 'fill', $params);
516 $this->assertAPISuccess($result, "in line " . __LINE__
);
517 $result = $this->callAPISuccess('setting', 'get', $params);
518 $this->assertAPISuccess($result, "in line " . __LINE__
);
519 $this->assertArrayHasKey('tag_unconfirmed', $result['values'][$dom['id']]);
521 // Setting has NULL default. Not returned.
522 //$this->assertArrayHasKey('extensionsDir', $result['values'][$dom['id']]);
524 $this->assertEquals('Unconfirmed', $result['values'][$dom['id']]['tag_unconfirmed']);
528 * Test to set isProductionEnvironment
531 public function testSetCivicrmEnvironment() {
533 'environment' => 'Staging',
535 $result = $this->callAPISuccess('Setting', 'create', $params);
537 'name' => 'environment',
538 'group' => 'Developer Preferences',
540 $result = $this->callAPISuccess('Setting', 'getvalue', $params);
541 $this->assertEquals('Staging', $result);
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);