3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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
39 class api_v3_SettingTest
extends CiviUnitTestCase
{
41 protected $_apiversion = 3;
42 protected $_contactID;
44 protected $_currentDomain;
45 protected $_domainID2;
46 protected $_domainID3;
48 public function setUp() {
51 'name' => 'Default Domain Name',
53 $result = $this->callAPISuccess('domain', 'get', $params);
54 if (empty($result['id'])) {
55 $result = $this->callAPISuccess('domain', 'create', $params);
58 $params['name'] = 'Second Domain';
59 $result = $this->callAPISuccess('domain', 'get', $params);
60 if (empty($result['id'])) {
61 $result = $this->callAPISuccess('domain', 'create', $params);
63 $this->_domainID2
= $result['id'];
64 $params['name'] = 'A-team domain';
65 $result = $this->callAPISuccess('domain', 'get', $params);
66 if (empty($result['id'])) {
67 $result = $this->callAPISuccess('domain', 'create', $params);
69 $this->_domainID3
= $result['id'];
70 $this->_currentDomain
= CRM_Core_Config
::domainID();
71 $this->hookClass
= CRM_Utils_Hook
::singleton();
74 public function tearDown() {
75 CRM_Utils_Hook
::singleton()->reset();
77 $this->callAPISuccess('system', 'flush', array());
78 $this->quickCleanup(array('civicrm_domain'));
82 * Set additional settings into metadata (implements hook)
83 * @param array $metaDataFolders
85 public function setExtensionMetadata(&$metaDataFolders) {
87 $metaDataFolders[] = $civicrm_root . '/tests/phpunit/api/v3/settings';
92 * Check getfields works.
94 public function testGetFields() {
95 $description = 'Demonstrate return from getfields - see subfolder for variants';
96 $result = $this->callAPIAndDocument('setting', 'getfields', array(), __FUNCTION__
, __FILE__
, $description);
97 $this->assertArrayHasKey('customCSSURL', $result['values']);
99 $description = 'Demonstrate return from getfields';
100 $result = $this->callAPISuccess('setting', 'getfields', array());
101 $this->assertArrayHasKey('customCSSURL', $result['values']);
102 $this->callAPISuccess('system', 'flush', array());
106 * Let's check it's loading from cache by meddling with the cache
108 public function testGetFieldsCaching() {
109 $settingsMetadata = array();
110 Civi
::cache('settings')->set('settingsMetadata_' . \CRM_Core_Config
::domainID() . '_', $settingsMetadata);
111 Civi
::cache('settings')->set(\Civi\Core\SettingsMetadata
::ALL
, $settingsMetadata);
112 $result = $this->callAPISuccess('setting', 'getfields', array());
113 $this->assertArrayNotHasKey('customCSSURL', $result['values']);
114 $this->quickCleanup(array('civicrm_cache'));
115 Civi
::cache('settings')->flush();
118 public function testGetFieldsFilters() {
119 $params = array('name' => 'advanced_search_options');
120 $result = $this->callAPISuccess('setting', 'getfields', $params);
121 $this->assertArrayNotHasKey('customCSSURL', $result['values']);
122 $this->assertArrayHasKey('advanced_search_options', $result['values']);
126 * Test that getfields will filter on group.
128 public function testGetFieldsGroupFilters() {
129 $params = array('filters' => array('group' => 'multisite'));
130 $result = $this->callAPISuccess('setting', 'getfields', $params);
131 $this->assertArrayNotHasKey('customCSSURL', $result['values']);
132 $this->assertArrayHasKey('domain_group_id', $result['values']);
136 * Ensure that on_change callbacks fire.
138 * Note: api_v3_SettingTest::testOnChange and CRM_Core_BAO_SettingTest::testOnChange
139 * are very similar, but they exercise different codepaths. The first uses the API
140 * and setItems [plural]; the second uses setItem [singular].
142 public function testOnChange() {
143 global $_testOnChange_hookCalls;
144 $this->setMockSettingsMetaData(array(
145 'onChangeExample' => array(
146 'group_name' => 'CiviCRM Preferences',
148 'name' => 'onChangeExample',
150 'quick_form_type' => 'Element',
151 'html_type' => 'advmultiselect',
152 'default' => array('CiviEvent', 'CiviContribute'),
154 'title' => 'List of Components',
157 'description' => NULL,
159 'on_change' => array(// list of callbacks
160 array(__CLASS__
, '_testOnChange_onChangeExample'),
166 $_testOnChange_hookCalls = array('count' => 0);
167 $this->callAPISuccess('setting', 'create', array(
168 'onChangeExample' => array('First', 'Value'),
170 $this->assertEquals(1, $_testOnChange_hookCalls['count']);
171 $this->assertEquals(array('First', 'Value'), $_testOnChange_hookCalls['newValue']);
172 $this->assertEquals('List of Components', $_testOnChange_hookCalls['metadata']['title']);
175 $_testOnChange_hookCalls = array('count' => 0);
176 $this->callAPISuccess('setting', 'create', array(
177 'onChangeExample' => array('Second', 'Value'),
179 $this->assertEquals(1, $_testOnChange_hookCalls['count']);
180 $this->assertEquals(array('First', 'Value'), $_testOnChange_hookCalls['oldValue']);
181 $this->assertEquals(array('Second', 'Value'), $_testOnChange_hookCalls['newValue']);
182 $this->assertEquals('List of Components', $_testOnChange_hookCalls['metadata']['title']);
186 * Mock callback for a setting's on_change handler
192 public static function _testOnChange_onChangeExample($oldValue, $newValue, $metadata) {
193 global $_testOnChange_hookCalls;
194 $_testOnChange_hookCalls['count']++
;
195 $_testOnChange_hookCalls['oldValue'] = $oldValue;
196 $_testOnChange_hookCalls['newValue'] = $newValue;
197 $_testOnChange_hookCalls['metadata'] = $metadata;
201 * Check getfields works.
203 public function testCreateSetting() {
204 $description = "Shows setting a variable for a given domain - if no domain is set current is assumed.";
207 'domain_id' => $this->_domainID2
,
208 'uniq_email_per_site' => 1,
210 $result = $this->callAPIAndDocument('setting', 'create', $params, __FUNCTION__
, __FILE__
);
212 $params = array('uniq_email_per_site' => 1);
213 $description = "Shows setting a variable for a current domain.";
214 $result = $this->callAPIAndDocument('setting', 'create', $params, __FUNCTION__
, __FILE__
, $description, 'CreateSettingCurrentDomain');
215 $this->assertArrayHasKey(CRM_Core_Config
::domainID(), $result['values']);
219 * Check getfields works.
221 public function testCreateInvalidSettings() {
223 'domain_id' => $this->_domainID2
,
226 $result = $this->callAPIFailure('setting', 'create', $params);
230 * Check invalid settings rejected -
232 public function testCreateInvalidURLSettings() {
234 'domain_id' => $this->_domainID2
,
235 'userFrameworkResourceURL' => 'dfhkd hfd',
237 $result = $this->callAPIFailure('setting', 'create', $params);
239 'domain_id' => $this->_domainID2
,
240 'userFrameworkResourceURL' => 'http://blah.com',
242 $result = $this->callAPISuccess('setting', 'create', $params);
246 * Check getfields works.
248 public function testCreateInvalidBooleanSettings() {
250 'domain_id' => $this->_domainID2
,
251 'track_civimail_replies' => 'dfhkdhfd',
253 $result = $this->callAPIFailure('setting', 'create', $params);
255 $params = array('track_civimail_replies' => '0');
256 $result = $this->callAPISuccess('setting', 'create', $params);
257 $getResult = $this->callAPISuccess('setting', 'get', $params);
258 $this->assertEquals(0, $getResult['values'][$this->_currentDomain
]['track_civimail_replies']);
260 $getResult = $this->callAPISuccess('setting', 'get', $params);
261 $this->assertEquals(0, $getResult['values'][$this->_currentDomain
]['track_civimail_replies']);
263 'domain_id' => $this->_domainID2
,
264 'track_civimail_replies' => '1',
266 $result = $this->callAPISuccess('setting', 'create', $params);
267 $getResult = $this->callAPISuccess('setting', 'get', $params);
268 $this->assertEquals(1, $getResult['values'][$this->_domainID2
]['track_civimail_replies']);
271 'domain_id' => $this->_domainID2
,
272 'track_civimail_replies' => 'TRUE',
274 $result = $this->callAPISuccess('setting', 'create', $params);
275 $getResult = $this->callAPISuccess('setting', 'get', $params);
277 $this->assertEquals(1, $getResult['values'][$this->_domainID2
]['track_civimail_replies'], "check TRUE is converted to 1");
281 * Check getfields works.
283 public function testCreateSettingMultipleDomains() {
284 $description = "Shows setting a variable for all domains.";
287 'domain_id' => 'all',
288 'uniq_email_per_site' => 1,
290 $result = $this->callAPIAndDocument('setting', 'create', $params, __FUNCTION__
, __FILE__
, $description, 'CreateAllDomains');
292 $this->assertEquals(1, $result['values'][2]['uniq_email_per_site']);
293 $this->assertEquals(1, $result['values'][1]['uniq_email_per_site']);
294 $this->assertArrayHasKey(3, $result['values'], 'Domain create probably failed Debug this IF domain test is passing');
295 $this->assertEquals(1, $result['values'][3]['uniq_email_per_site'], 'failed to set setting for domain 3.');
298 'domain_id' => 'all',
299 'return' => 'uniq_email_per_site',
301 // we'll check it with a 'get'
302 $description = "Shows getting a variable for all domains.";
303 $result = $this->callAPIAndDocument('setting', 'get', $params, __FUNCTION__
, __FILE__
, $description, 'GetAllDomains');
305 $this->assertEquals(1, $result['values'][2]['uniq_email_per_site']);
306 $this->assertEquals(1, $result['values'][1]['uniq_email_per_site']);
307 $this->assertEquals(1, $result['values'][3]['uniq_email_per_site']);
310 'domain_id' => array(1, 3),
311 'uniq_email_per_site' => 0,
313 $description = "Shows setting a variable for specified domains.";
314 $result = $this->callAPIAndDocument('setting', 'create', $params, __FUNCTION__
, __FILE__
, $description, 'CreateSpecifiedDomains');
316 $this->assertEquals(0, $result['values'][3]['uniq_email_per_site']);
317 $this->assertEquals(0, $result['values'][1]['uniq_email_per_site']);
319 'domain_id' => array(1, 2),
320 'return' => array('uniq_email_per_site'),
322 $description = "Shows getting a variable for specified domains.";
323 $result = $this->callAPIAndDocument('setting', 'get', $params, __FUNCTION__
, __FILE__
, $description, 'GetSpecifiedDomains');
324 $this->assertEquals(1, $result['values'][2]['uniq_email_per_site']);
325 $this->assertEquals(0, $result['values'][1]['uniq_email_per_site']);
329 public function testGetSetting() {
331 'domain_id' => $this->_domainID2
,
332 'return' => 'uniq_email_per_site',
334 $description = "Shows get setting a variable for a given domain - if no domain is set current is assumed.";
336 $result = $this->callAPIAndDocument('setting', 'get', $params, __FUNCTION__
, __FILE__
);
339 'return' => 'uniq_email_per_site',
341 $description = "Shows getting a variable for a current domain.";
342 $result = $this->callAPIAndDocument('setting', 'get', $params, __FUNCTION__
, __FILE__
, $description, 'GetSettingCurrentDomain');
343 $this->assertArrayHasKey(CRM_Core_Config
::domainID(), $result['values']);
347 * Check that setting defined in extension can be retrieved.
349 public function testGetExtensionSetting() {
350 $this->hookClass
->setHook('civicrm_alterSettingsFolders', array($this, 'setExtensionMetadata'));
352 // the caching of data to all duplicates the caching of data to the empty string
353 CRM_Core_BAO_Cache
::setItem($data, 'CiviCRM setting Spec', 'All');
354 CRM_Core_BAO_Cache
::setItem($data, 'CiviCRM setting Specs', 'settingsMetadata__');
355 Civi
::cache('settings')->flush();
356 $fields = $this->callAPISuccess('setting', 'getfields', array('filters' => array('group_name' => 'Test Settings')));
357 $this->assertArrayHasKey('test_key', $fields['values']);
358 $this->callAPISuccess('setting', 'create', array('test_key' => 'keyset'));
359 $this->assertEquals('keyset', Civi
::settings()->get('test_key'));
360 $result = $this->callAPISuccess('setting', 'getvalue', array('name' => 'test_key', 'group' => 'Test Settings'));
361 $this->assertEquals('keyset', $result);
365 * Setting api should set & fetch settings stored in config as well as those in settings table
367 public function testSetConfigSetting() {
368 $config = CRM_Core_Config
::singleton();
369 $this->assertFalse($config->debug
== 1);
372 'domain_id' => $this->_domainID2
,
373 'debug_enabled' => 1,
375 $result = $this->callAPISuccess('setting', 'create', $params);
377 $this->assertEquals(1, Civi
::settings($this->_domainID2
)->get('debug_enabled'));
379 CRM_Core_BAO_Domain
::setDomain($this->_domainID2
);
380 $config = CRM_Core_Config
::singleton(TRUE, TRUE);
381 CRM_Core_BAO_Domain
::resetDomain();
382 $this->assertEquals(1, $config->debug
);
386 * Setting api should set & fetch settings stored in config as well as those in settings table
388 public function testGetConfigSetting() {
389 $settings = $this->callAPISuccess('setting', 'get', array(
390 'name' => 'defaultCurrency',
394 $this->assertEquals('USD', $settings['values'][0]['defaultCurrency']);
398 * Setting api should set & fetch settings stored in config as well as those in settings table
400 public function testGetSetConfigSettingMultipleDomains() {
401 $settings = $this->callAPISuccess('setting', 'create', array(
402 'defaultCurrency' => 'USD',
403 'domain_id' => $this->_currentDomain
,
406 $settings = $this->callAPISuccess('setting', 'create', array(
407 'defaultCurrency' => 'CAD',
408 'domain_id' => $this->_domainID2
,
411 $settings = $this->callAPISuccess('setting', 'get', array(
412 'return' => 'defaultCurrency',
413 'domain_id' => 'all',
416 $this->assertEquals('USD', $settings['values'][$this->_currentDomain
]['defaultCurrency']);
417 $this->assertEquals('CAD', $settings['values'][$this->_domainID2
]['defaultCurrency'],
418 "second domain (id {$this->_domainID2} ) should be set to CAD. First dom was {$this->_currentDomain} & was USD");
423 * Use getValue against a config setting.
425 public function testGetValueConfigSetting() {
427 'name' => 'monetaryThousandSeparator',
428 'group' => 'Localization Setting',
430 $result = $this->callAPISuccess('setting', 'getvalue', $params);
431 $this->assertEquals(',', $result);
434 public function testGetValue() {
436 'name' => 'petition_contacts',
437 'group' => 'Campaign Preferences',
439 $description = "Demonstrates getvalue action - intended for runtime use as better caching than get.";
441 $result = $this->callAPIAndDocument('setting', 'getvalue', $params, __FUNCTION__
, __FILE__
, $description);
442 $this->assertEquals('Petition Contacts', $result);
445 public function testGetDefaults() {
446 $description = "Gets defaults setting a variable for a given domain - if no domain is set current is assumed.";
449 'name' => 'address_format',
451 $result = $this->callAPIAndDocument('setting', 'getdefaults', $params, __FUNCTION__
, __FILE__
, $description, 'GetDefaults');
452 $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']);
453 $params = array('name' => 'mailing_format');
454 $result = $this->callAPISuccess('setting', 'getdefaults', $params);
455 $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']);
456 $this->assertArrayHasKey(CRM_Core_Config
::domainID(), $result['values']);
460 * Function tests reverting a specific parameter.
462 public function testRevert() {
464 'address_format' => 'xyz',
465 'mailing_format' => 'bcs',
467 $result = $this->callAPISuccess('setting', 'create', $params);
468 $this->assertAPISuccess($result, "in line " . __LINE__
);
469 $revertParams = array(
470 'name' => 'address_format',
472 $result = $this->callAPISuccess('setting', 'get', $params);
474 $this->assertEquals('xyz', $result['values'][CRM_Core_Config
::domainID()]['address_format']);
475 $description = "Demonstrates reverting a parameter to default value.";
476 $result = $this->callAPIAndDocument('setting', 'revert', $revertParams, __FUNCTION__
, __FILE__
, $description, '');
477 //make sure it's reverted
478 $result = $this->callAPISuccess('setting', 'get', $params);
479 $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']);
481 'return' => array('mailing_format'),
483 $result = $this->callAPISuccess('setting', 'get', $params);
484 //make sure it's unchanged
485 $this->assertEquals('bcs', $result['values'][CRM_Core_Config
::domainID()]['mailing_format']);
489 * Tests reverting ALL parameters (specific domain)
491 public function testRevertAll() {
493 'address_format' => 'xyz',
494 'mailing_format' => 'bcs',
496 $result = $this->callAPISuccess('setting', 'create', $params);
497 $revertParams = array();
498 $result = $this->callAPISuccess('setting', 'get', $params);
500 $this->assertEquals('xyz', $result['values'][CRM_Core_Config
::domainID()]['address_format']);
502 $this->callAPISuccess('setting', 'revert', $revertParams);
503 //make sure it's reverted
504 $result = $this->callAPISuccess('setting', 'get', array('group' => 'core'));
505 $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']);
506 $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']);
510 * Settings should respect their defaults
512 public function testDefaults() {
514 'name' => 'B Team Domain',
516 $dom = $this->callAPISuccess('domain', 'create', $domparams);
518 'domain_id' => 'all',
520 $result = $this->callAPISuccess('setting', 'get', $params);
522 'address_format' => 'xyz',
523 'mailing_format' => 'bcs',
524 'domain_id' => $this->_domainID2
,
526 $result = $this->callAPISuccess('setting', 'create', $params);
528 'domain_id' => $dom['id'],
530 $result = $this->callAPISuccess('setting', 'get', $params);
531 $this->assertAPISuccess($result, "in line " . __LINE__
);
532 $this->assertEquals('Unconfirmed', $result['values'][$dom['id']]['tag_unconfirmed']);
534 // The 'fill' operation is no longer necessary, but third parties might still use it, so let's
535 // make sure it doesn't do anything weird (crashing or breaking values).
536 $result = $this->callAPISuccess('setting', 'fill', $params);
537 $this->assertAPISuccess($result, "in line " . __LINE__
);
538 $result = $this->callAPISuccess('setting', 'get', $params);
539 $this->assertAPISuccess($result, "in line " . __LINE__
);
540 $this->assertArrayHasKey('tag_unconfirmed', $result['values'][$dom['id']]);
542 // Setting has NULL default. Not returned.
543 //$this->assertArrayHasKey('extensionsDir', $result['values'][$dom['id']]);
545 $this->assertEquals('Unconfirmed', $result['values'][$dom['id']]['tag_unconfirmed']);