3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2017 |
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 // 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__');
357 Civi
::cache('settings')->flush();
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'));
361 $this->assertEquals('keyset', Civi
::settings()->get('test_key'));
362 $result = $this->callAPISuccess('setting', 'getvalue', array('name' => 'test_key', 'group' => 'Test Settings'));
363 $this->assertEquals('keyset', $result);
367 * Setting api should set & fetch settings stored in config as well as those in settings table
369 public function testGetConfigSetting() {
370 $settings = $this->callAPISuccess('setting', 'get', array(
371 'name' => 'defaultCurrency',
375 $this->assertEquals('USD', $settings['values'][0]['defaultCurrency']);
379 * Setting api should set & fetch settings stored in config as well as those in settings table
381 public function testGetSetConfigSettingMultipleDomains() {
382 $settings = $this->callAPISuccess('setting', 'create', array(
383 'defaultCurrency' => 'USD',
384 'domain_id' => $this->_currentDomain
,
387 $settings = $this->callAPISuccess('setting', 'create', array(
388 'defaultCurrency' => 'CAD',
389 'domain_id' => $this->_domainID2
,
392 $settings = $this->callAPISuccess('setting', 'get', array(
393 'return' => 'defaultCurrency',
394 'domain_id' => 'all',
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");
404 * Use getValue against a config setting.
406 public function testGetValueConfigSetting() {
408 'name' => 'monetaryThousandSeparator',
409 'group' => 'Localization Setting',
411 $result = $this->callAPISuccess('setting', 'getvalue', $params);
412 $this->assertEquals(',', $result);
415 public function testGetValue() {
417 'name' => 'petition_contacts',
418 'group' => 'Campaign Preferences',
420 $description = "Demonstrates getvalue action - intended for runtime use as better caching than get.";
422 $result = $this->callAPIAndDocument('setting', 'getvalue', $params, __FUNCTION__
, __FILE__
, $description);
423 $this->assertEquals('Petition Contacts', $result);
426 public function testGetDefaults() {
427 $description = "Gets defaults setting a variable for a given domain - if no domain is set current is assumed.";
430 'name' => 'address_format',
432 $result = $this->callAPIAndDocument('setting', 'getdefaults', $params, __FUNCTION__
, __FILE__
, $description, 'GetDefaults');
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']);
434 $params = array('name' => 'mailing_format');
435 $result = $this->callAPISuccess('setting', 'getdefaults', $params);
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']);
441 * Function tests reverting a specific parameter.
443 public function testRevert() {
445 'address_format' => 'xyz',
446 'mailing_format' => 'bcs',
448 $result = $this->callAPISuccess('setting', 'create', $params);
449 $this->assertAPISuccess($result, "in line " . __LINE__
);
450 $revertParams = array(
451 'name' => 'address_format',
453 $result = $this->callAPISuccess('setting', 'get', $params);
455 $this->assertEquals('xyz', $result['values'][CRM_Core_Config
::domainID()]['address_format']);
456 $description = "Demonstrates reverting a parameter to default value.";
457 $result = $this->callAPIAndDocument('setting', 'revert', $revertParams, __FUNCTION__
, __FILE__
, $description, '');
458 //make sure it's reverted
459 $result = $this->callAPISuccess('setting', 'get', $params);
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']);
462 'return' => array('mailing_format'),
464 $result = $this->callAPISuccess('setting', 'get', $params);
465 //make sure it's unchanged
466 $this->assertEquals('bcs', $result['values'][CRM_Core_Config
::domainID()]['mailing_format']);
470 * Tests reverting ALL parameters (specific domain)
472 public function testRevertAll() {
474 'address_format' => 'xyz',
475 'mailing_format' => 'bcs',
477 $result = $this->callAPISuccess('setting', 'create', $params);
478 $revertParams = array();
479 $result = $this->callAPISuccess('setting', 'get', $params);
481 $this->assertEquals('xyz', $result['values'][CRM_Core_Config
::domainID()]['address_format']);
483 $this->callAPISuccess('setting', 'revert', $revertParams);
484 //make sure it's reverted
485 $result = $this->callAPISuccess('setting', 'get', array('group' => 'core'));
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']);
491 * Settings should respect their defaults
493 public function testDefaults() {
495 'name' => 'B Team Domain',
496 'domain_version' => '4.7',
498 $dom = $this->callAPISuccess('domain', 'create', $domparams);
500 'domain_id' => 'all',
502 $result = $this->callAPISuccess('setting', 'get', $params);
504 'address_format' => 'xyz',
505 'mailing_format' => 'bcs',
506 'domain_id' => $this->_domainID2
,
508 $result = $this->callAPISuccess('setting', 'create', $params);
510 'domain_id' => $dom['id'],
512 $result = $this->callAPISuccess('setting', 'get', $params);
513 $this->assertAPISuccess($result, "in line " . __LINE__
);
514 $this->assertEquals('Unconfirmed', $result['values'][$dom['id']]['tag_unconfirmed']);
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).
518 $result = $this->callAPISuccess('setting', 'fill', $params);
519 $this->assertAPISuccess($result, "in line " . __LINE__
);
520 $result = $this->callAPISuccess('setting', 'get', $params);
521 $this->assertAPISuccess($result, "in line " . __LINE__
);
522 $this->assertArrayHasKey('tag_unconfirmed', $result['values'][$dom['id']]);
524 // Setting has NULL default. Not returned.
525 //$this->assertArrayHasKey('extensionsDir', $result['values'][$dom['id']]);
527 $this->assertEquals('Unconfirmed', $result['values'][$dom['id']]['tag_unconfirmed']);