3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
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 $_contactID;
44 protected $_currentDomain;
45 protected $_domainID2;
46 protected $_domainID3;
48 public function setUp() {
51 'name' => 'Default Domain Name',
52 'domain_version' => '4.7',
54 $result = $this->callAPISuccess('domain', 'get', $params);
55 if (empty($result['id'])) {
56 $result = $this->callAPISuccess('domain', 'create', $params);
59 $params['name'] = 'Second Domain';
60 $result = $this->callAPISuccess('domain', 'get', $params);
61 if (empty($result['id'])) {
62 $result = $this->callAPISuccess('domain', 'create', $params);
64 $this->_domainID2
= $result['id'];
65 $params['name'] = 'A-team domain';
66 $result = $this->callAPISuccess('domain', 'get', $params);
67 if (empty($result['id'])) {
68 $result = $this->callAPISuccess('domain', 'create', $params);
70 $this->_domainID3
= $result['id'];
71 $this->_currentDomain
= CRM_Core_Config
::domainID();
72 $this->hookClass
= CRM_Utils_Hook
::singleton();
75 public function tearDown() {
76 CRM_Utils_Hook
::singleton()->reset();
78 $this->callAPISuccess('system', 'flush', []);
79 $this->quickCleanup(['civicrm_domain']);
83 * Set additional settings into metadata (implements hook)
84 * @param array $metaDataFolders
86 public function setExtensionMetadata(&$metaDataFolders) {
88 $metaDataFolders[] = $civicrm_root . '/tests/phpunit/api/v3/settings';
93 * @dataProvider versionThreeAndFour
95 public function testGetFields($version) {
96 $this->_apiversion
= $version;
97 $description = 'Demonstrate return from getfields - see subfolder for variants';
98 $result = $this->callAPIAndDocument('setting', 'getfields', [], __FUNCTION__
, __FILE__
, $description);
99 $this->assertArrayHasKey('customCSSURL', $result['values']);
101 $description = 'Demonstrate return from getfields';
102 $result = $this->callAPISuccess('setting', 'getfields', []);
103 $this->assertArrayHasKey('customCSSURL', $result['values']);
104 $this->callAPISuccess('system', 'flush', []);
108 * Let's check it's loading from cache by meddling with the cache
109 * @param int $version
110 * @dataProvider versionThreeAndFour
112 public function testGetFieldsCaching($version) {
113 $this->_apiversion
= $version;
114 $settingsMetadata = [];
115 Civi
::cache('settings')->set('settingsMetadata_' . \CRM_Core_Config
::domainID() . '_', $settingsMetadata);
116 $result = $this->callAPISuccess('setting', 'getfields', []);
117 $this->assertArrayNotHasKey('customCSSURL', $result['values']);
118 $this->quickCleanup(['civicrm_cache']);
119 Civi
::cache('settings')->flush();
123 * @param int $version
124 * @dataProvider versionThreeAndFour
126 public function testGetFieldsFilters($version) {
127 $this->_apiversion
= $version;
128 $params = ['name' => 'advanced_search_options'];
129 $result = $this->callAPISuccess('setting', 'getfields', $params);
130 $this->assertArrayNotHasKey('customCSSURL', $result['values']);
131 $this->assertArrayHasKey('advanced_search_options', $result['values']);
135 * Test that getfields will filter on group.
136 * @param int $version
137 * @dataProvider versionThreeAndFour
139 public function testGetFieldsGroupFilters($version) {
140 $this->_apiversion
= $version;
141 $params = ['filters' => ['group' => 'multisite']];
142 $result = $this->callAPISuccess('setting', 'getfields', $params);
143 $this->assertArrayNotHasKey('customCSSURL', $result['values']);
144 $this->assertArrayHasKey('domain_group_id', $result['values']);
148 * Ensure that on_change callbacks fire.
150 * Note: api_v3_SettingTest::testOnChange and CRM_Core_BAO_SettingTest::testOnChange
151 * are very similar, but they exercise different codepaths. The first uses the API
152 * and setItems [plural]; the second uses setItem [singular].
153 * @param int $version
154 * @dataProvider versionThreeAndFour
156 public function testOnChange($version) {
157 $this->_apiversion
= $version;
158 global $_testOnChange_hookCalls;
159 $this->setMockSettingsMetaData([
160 'onChangeExample' => [
161 'group_name' => 'CiviCRM Preferences',
163 'name' => 'onChangeExample',
165 'quick_form_type' => 'Element',
166 'html_type' => 'advmultiselect',
167 'default' => ['CiviEvent', 'CiviContribute'],
169 'title' => 'List of Components',
172 'description' => NULL,
176 [__CLASS__
, '_testOnChange_onChangeExample'],
182 $_testOnChange_hookCalls = ['count' => 0];
183 $this->callAPISuccess('setting', 'create', [
184 'onChangeExample' => ['First', 'Value'],
186 $this->assertEquals(1, $_testOnChange_hookCalls['count']);
187 $this->assertEquals(['First', 'Value'], $_testOnChange_hookCalls['newValue']);
188 $this->assertEquals('List of Components', $_testOnChange_hookCalls['metadata']['title']);
191 $_testOnChange_hookCalls = ['count' => 0];
192 $this->callAPISuccess('setting', 'create', [
193 'onChangeExample' => ['Second', 'Value'],
195 $this->assertEquals(1, $_testOnChange_hookCalls['count']);
196 $this->assertEquals(['First', 'Value'], $_testOnChange_hookCalls['oldValue']);
197 $this->assertEquals(['Second', 'Value'], $_testOnChange_hookCalls['newValue']);
198 $this->assertEquals('List of Components', $_testOnChange_hookCalls['metadata']['title']);
202 * Mock callback for a setting's on_change handler
208 public static function _testOnChange_onChangeExample($oldValue, $newValue, $metadata) {
209 global $_testOnChange_hookCalls;
210 $_testOnChange_hookCalls['count']++
;
211 $_testOnChange_hookCalls['oldValue'] = $oldValue;
212 $_testOnChange_hookCalls['newValue'] = $newValue;
213 $_testOnChange_hookCalls['metadata'] = $metadata;
217 * @param int $version
218 * @dataProvider versionThreeAndFour
220 public function testCreateSetting($version) {
221 $this->_apiversion
= $version;
222 $description = "Shows setting a variable for a given domain - if no domain is set current is assumed.";
225 'domain_id' => $this->_domainID2
,
226 'uniq_email_per_site' => 1,
228 $result = $this->callAPIAndDocument('setting', 'create', $params, __FUNCTION__
, __FILE__
);
230 $params = ['uniq_email_per_site' => 1];
231 $description = "Shows setting a variable for a current domain.";
232 $result = $this->callAPIAndDocument('setting', 'create', $params, __FUNCTION__
, __FILE__
, $description, 'CreateSettingCurrentDomain');
233 $this->assertArrayHasKey(CRM_Core_Config
::domainID(), $result['values']);
237 * @param int $version
238 * @dataProvider versionThreeAndFour
240 public function testCreateInvalidSettings($version) {
241 $this->_apiversion
= $version;
243 'domain_id' => $this->_domainID2
,
246 $result = $this->callAPIFailure('setting', 'create', $params);
250 * Check invalid settings rejected -
251 * @param int $version
252 * @dataProvider versionThreeAndFour
254 public function testCreateInvalidURLSettings($version) {
255 $this->_apiversion
= $version;
257 'domain_id' => $this->_domainID2
,
258 'userFrameworkResourceURL' => 'dfhkd hfd',
260 $result = $this->callAPIFailure('setting', 'create', $params);
262 'domain_id' => $this->_domainID2
,
263 'userFrameworkResourceURL' => 'http://blah.com',
265 $result = $this->callAPISuccess('setting', 'create', $params);
269 * @param int $version
270 * @dataProvider versionThreeAndFour
272 public function testCreateInvalidBooleanSettings($version) {
273 $this->_apiversion
= $version;
275 'domain_id' => $this->_domainID2
,
276 'track_civimail_replies' => 'dfhkdhfd',
278 $result = $this->callAPIFailure('setting', 'create', $params);
280 $params = ['track_civimail_replies' => '0'];
281 $result = $this->callAPISuccess('setting', 'create', $params);
282 $getResult = $this->callAPISuccess('setting', 'get');
283 $this->assertEquals(0, $getResult['values'][$this->_currentDomain
]['track_civimail_replies']);
285 $getResult = $this->callAPISuccess('setting', 'get');
286 $this->assertEquals(0, $getResult['values'][$this->_currentDomain
]['track_civimail_replies']);
288 'domain_id' => $this->_domainID2
,
289 'track_civimail_replies' => '1',
291 $result = $this->callAPISuccess('setting', 'create', $params);
292 $getResult = $this->callAPISuccess('setting', 'get', ['domain_id' => $this->_domainID2
]);
293 $this->assertEquals(1, $getResult['values'][$this->_domainID2
]['track_civimail_replies']);
296 'domain_id' => $this->_domainID2
,
297 'track_civimail_replies' => 'TRUE',
299 $result = $this->callAPISuccess('setting', 'create', $params);
300 $getResult = $this->callAPISuccess('setting', 'get', ['domain_id' => $this->_domainID2
]);
302 $this->assertEquals(1, $getResult['values'][$this->_domainID2
]['track_civimail_replies'], "check TRUE is converted to 1");
306 * @param int $version
307 * @dataProvider versionThreeAndFour
309 public function testCreateSettingMultipleDomains($version) {
310 $this->_apiversion
= $version;
311 $description = "Shows setting a variable for all domains.";
314 'domain_id' => 'all',
315 'uniq_email_per_site' => 1,
317 $result = $this->callAPIAndDocument('setting', 'create', $params, __FUNCTION__
, __FILE__
, $description, 'CreateAllDomains');
319 $this->assertEquals(1, $result['values'][2]['uniq_email_per_site']);
320 $this->assertEquals(1, $result['values'][1]['uniq_email_per_site']);
321 $this->assertArrayHasKey(3, $result['values'], 'Domain create probably failed Debug this IF domain test is passing');
322 $this->assertEquals(1, $result['values'][3]['uniq_email_per_site'], 'failed to set setting for domain 3.');
325 'domain_id' => 'all',
326 'return' => 'uniq_email_per_site',
328 // we'll check it with a 'get'
329 $description = "Shows getting a variable for all domains.";
330 $result = $this->callAPIAndDocument('setting', 'get', $params, __FUNCTION__
, __FILE__
, $description, 'GetAllDomains');
332 $this->assertEquals(1, $result['values'][2]['uniq_email_per_site']);
333 $this->assertEquals(1, $result['values'][1]['uniq_email_per_site']);
334 $this->assertEquals(1, $result['values'][3]['uniq_email_per_site']);
337 'domain_id' => [1, 3],
338 'uniq_email_per_site' => 0,
340 $description = "Shows setting a variable for specified domains.";
341 $result = $this->callAPIAndDocument('setting', 'create', $params, __FUNCTION__
, __FILE__
, $description, 'CreateSpecifiedDomains');
343 $this->assertEquals(0, $result['values'][3]['uniq_email_per_site']);
344 $this->assertEquals(0, $result['values'][1]['uniq_email_per_site']);
346 'domain_id' => [1, 2],
347 'return' => ['uniq_email_per_site'],
349 $description = "Shows getting a variable for specified domains.";
350 $result = $this->callAPIAndDocument('setting', 'get', $params, __FUNCTION__
, __FILE__
, $description, 'GetSpecifiedDomains');
351 $this->assertEquals(1, $result['values'][2]['uniq_email_per_site']);
352 $this->assertEquals(0, $result['values'][1]['uniq_email_per_site']);
357 * @param int $version
358 * @dataProvider versionThreeAndFour
360 public function testGetSetting($version) {
361 $this->_apiversion
= $version;
363 'domain_id' => $this->_domainID2
,
364 'return' => 'uniq_email_per_site',
366 $description = "Shows get setting a variable for a given domain - if no domain is set current is assumed.";
368 $result = $this->callAPIAndDocument('setting', 'get', $params, __FUNCTION__
, __FILE__
);
371 'return' => 'uniq_email_per_site',
373 $description = "Shows getting a variable for a current domain.";
374 $result = $this->callAPIAndDocument('setting', 'get', $params, __FUNCTION__
, __FILE__
, $description, 'GetSettingCurrentDomain');
375 $this->assertArrayHasKey(CRM_Core_Config
::domainID(), $result['values']);
379 * Check that setting defined in extension can be retrieved.
380 * @param int $version
381 * @dataProvider versionThreeAndFour
383 public function testGetExtensionSetting($version) {
384 $this->_apiversion
= $version;
385 $this->hookClass
->setHook('civicrm_alterSettingsFolders', [$this, 'setExtensionMetadata']);
387 Civi
::cache('settings')->flush();
388 $fields = $this->callAPISuccess('setting', 'getfields', ['filters' => ['group_name' => 'Test Settings']]);
389 $this->assertArrayHasKey('test_key', $fields['values']);
390 $this->callAPISuccess('setting', 'create', ['test_key' => 'keyset']);
391 $this->assertEquals('keyset', Civi
::settings()->get('test_key'));
392 $result = $this->callAPISuccess('setting', 'getvalue', ['name' => 'test_key', 'group' => 'Test Settings']);
393 $this->assertEquals('keyset', $result);
397 * Setting api should set & fetch settings stored in config as well as those in settings table
398 * @param int $version
399 * @dataProvider versionThreeAndFour
401 public function testGetConfigSetting($version) {
402 $this->_apiversion
= $version;
403 $settings = $this->callAPISuccess('setting', 'get', [
404 'name' => 'defaultCurrency',
407 $this->assertEquals('USD', $settings['values'][0]['defaultCurrency']);
411 * Setting api should set & fetch settings stored in config as well as those in settings table
412 * @param int $version
413 * @dataProvider versionThreeAndFour
415 public function testGetSetConfigSettingMultipleDomains($version) {
416 $this->_apiversion
= $version;
417 $settings = $this->callAPISuccess('setting', 'create', [
418 'defaultCurrency' => 'USD',
419 'domain_id' => $this->_currentDomain
,
421 $settings = $this->callAPISuccess('setting', 'create', [
422 'defaultCurrency' => 'CAD',
423 'domain_id' => $this->_domainID2
,
425 $settings = $this->callAPISuccess('setting', 'get', [
426 'return' => 'defaultCurrency',
427 'domain_id' => 'all',
429 $this->assertEquals('USD', $settings['values'][$this->_currentDomain
]['defaultCurrency']);
430 $this->assertEquals('CAD', $settings['values'][$this->_domainID2
]['defaultCurrency'],
431 "second domain (id {$this->_domainID2} ) should be set to CAD. First dom was {$this->_currentDomain} & was USD");
436 * Use getValue against a config setting.
437 * @param int $version
438 * @dataProvider versionThreeAndFour
440 public function testGetValueConfigSetting($version) {
441 $this->_apiversion
= $version;
443 'name' => 'monetaryThousandSeparator',
444 'group' => 'Localization Setting',
446 $result = $this->callAPISuccess('setting', 'getvalue', $params);
447 $this->assertEquals(',', $result);
451 * @param int $version
452 * @dataProvider versionThreeAndFour
454 public function testGetValue($version) {
455 $this->_apiversion
= $version;
457 'name' => 'petition_contacts',
458 'group' => 'Campaign Preferences',
460 $description = "Demonstrates getvalue action - intended for runtime use as better caching than get.";
462 $result = $this->callAPIAndDocument('setting', 'getvalue', $params, __FUNCTION__
, __FILE__
, $description);
463 $this->assertEquals('Petition Contacts', $result);
467 * V3 only - no api4 equivalent.
469 public function testGetDefaults() {
470 $description = "Gets defaults setting a variable for a given domain - if no domain is set current is assumed.";
473 'name' => 'address_format',
475 $result = $this->callAPIAndDocument('setting', 'getdefaults', $params, __FUNCTION__
, __FILE__
, $description, 'GetDefaults');
476 $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']);
477 $params = ['name' => 'mailing_format'];
478 $result = $this->callAPISuccess('setting', 'getdefaults', $params);
479 $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']);
480 $this->assertArrayHasKey(CRM_Core_Config
::domainID(), $result['values']);
484 * Function tests reverting a specific parameter.
485 * @param int $version
486 * @dataProvider versionThreeAndFour
488 public function testRevert($version) {
489 $this->_apiversion
= $version;
491 'address_format' => 'xyz',
492 'mailing_format' => 'bcs',
494 $result = $this->callAPISuccess('setting', 'create', $params);
495 $this->assertAPISuccess($result, "in line " . __LINE__
);
497 'name' => 'address_format',
499 $result = $this->callAPISuccess('setting', 'get');
501 $this->assertEquals('xyz', $result['values'][CRM_Core_Config
::domainID()]['address_format']);
502 $description = "Demonstrates reverting a parameter to default value.";
503 $result = $this->callAPIAndDocument('setting', 'revert', $revertParams, __FUNCTION__
, __FILE__
, $description, '');
504 //make sure it's reverted
505 $result = $this->callAPISuccess('setting', 'get');
506 $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']);
508 'return' => ['mailing_format'],
510 $result = $this->callAPISuccess('setting', 'get', $params);
511 //make sure it's unchanged
512 $this->assertEquals('bcs', $result['values'][CRM_Core_Config
::domainID()]['mailing_format']);
516 * Tests reverting ALL parameters (specific domain)
519 public function testRevertAll() {
521 'address_format' => 'xyz',
522 'mailing_format' => 'bcs',
524 $result = $this->callAPISuccess('setting', 'create', $params);
526 $result = $this->callAPISuccess('setting', 'get', $params);
528 $this->assertEquals('xyz', $result['values'][CRM_Core_Config
::domainID()]['address_format']);
530 $this->callAPISuccess('setting', 'revert', $revertParams);
531 //make sure it's reverted
532 $result = $this->callAPISuccess('setting', 'get', ['group' => 'core']);
533 $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']);
534 $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']);
538 * Settings should respect their defaults
539 * V3 only - no fill action in v4
541 public function testDefaults() {
543 'name' => 'B Team Domain',
544 'domain_version' => '4.7',
546 $dom = $this->callAPISuccess('domain', 'create', $domparams);
548 'domain_id' => 'all',
550 $result = $this->callAPISuccess('setting', 'get', $params);
552 'address_format' => 'xyz',
553 'mailing_format' => 'bcs',
554 'domain_id' => $this->_domainID2
,
556 $result = $this->callAPISuccess('setting', 'create', $params);
558 'domain_id' => $dom['id'],
560 $result = $this->callAPISuccess('setting', 'get', $params);
561 $this->assertAPISuccess($result, "in line " . __LINE__
);
562 $this->assertEquals('Unconfirmed', $result['values'][$dom['id']]['tag_unconfirmed']);
564 // The 'fill' operation is no longer necessary, but third parties might still use it, so let's
565 // make sure it doesn't do anything weird (crashing or breaking values).
566 $result = $this->callAPISuccess('setting', 'fill', $params);
567 $this->assertAPISuccess($result, "in line " . __LINE__
);
568 $result = $this->callAPISuccess('setting', 'get', $params);
569 $this->assertAPISuccess($result, "in line " . __LINE__
);
570 $this->assertArrayHasKey('tag_unconfirmed', $result['values'][$dom['id']]);
572 // Setting has NULL default. Not returned.
573 //$this->assertArrayHasKey('extensionsDir', $result['values'][$dom['id']]);
575 $this->assertEquals('Unconfirmed', $result['values'][$dom['id']]['tag_unconfirmed']);
579 * Test to set isProductionEnvironment
580 * @param int $version
581 * @dataProvider versionThreeAndFour
583 public function testSetCivicrmEnvironment($version) {
584 $this->_apiversion
= $version;
585 global $civicrm_setting;
586 unset($civicrm_setting[CRM_Core_BAO_Setting
::DEVELOPER_PREFERENCES_NAME
]['environment']);
587 Civi
::service('settings_manager')->useMandatory();
589 'environment' => 'Staging',
591 $result = $this->callAPISuccess('Setting', 'create', $params);
593 'name' => 'environment',
594 'group' => 'Developer Preferences',
596 $result = $this->callAPISuccess('Setting', 'getvalue', $params);
597 $this->assertEquals('Staging', $result);
599 $civicrm_setting[CRM_Core_BAO_Setting
::DEVELOPER_PREFERENCES_NAME
]['environment'] = 'Production';
600 Civi
::service('settings_manager')->useMandatory();
601 $result = $this->callAPISuccess('Setting', 'getvalue', $params);
602 $this->assertEquals('Production', $result);