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
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',
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', array());
79 $this->quickCleanup(array('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 * Check getfields works.
95 public function testGetFields() {
96 $description = 'Demonstrate return from getfields - see subfolder for variants';
97 $result = $this->callAPIAndDocument('setting', 'getfields', array(), __FUNCTION__
, __FILE__
, $description);
98 $this->assertArrayHasKey('customCSSURL', $result['values']);
100 $description = 'Demonstrate return from getfields';
101 $result = $this->callAPISuccess('setting', 'getfields', array());
102 $this->assertArrayHasKey('customCSSURL', $result['values']);
103 $this->callAPISuccess('system', 'flush', array());
107 * Let's check it's loading from cache by meddling with the cache
109 public function testGetFieldsCaching() {
110 $settingsMetadata = array();
111 Civi
::cache('settings')->set('settingsMetadata_' . \CRM_Core_Config
::domainID() . '_', $settingsMetadata);
112 Civi
::cache('settings')->set(\Civi\Core\SettingsMetadata
::ALL
, $settingsMetadata);
113 $result = $this->callAPISuccess('setting', 'getfields', array());
114 $this->assertArrayNotHasKey('customCSSURL', $result['values']);
115 $this->quickCleanup(array('civicrm_cache'));
116 Civi
::cache('settings')->flush();
119 public function testGetFieldsFilters() {
120 $params = array('name' => 'advanced_search_options');
121 $result = $this->callAPISuccess('setting', 'getfields', $params);
122 $this->assertArrayNotHasKey('customCSSURL', $result['values']);
123 $this->assertArrayHasKey('advanced_search_options', $result['values']);
127 * Test that getfields will filter on group.
129 public function testGetFieldsGroupFilters() {
130 $params = array('filters' => array('group' => 'multisite'));
131 $result = $this->callAPISuccess('setting', 'getfields', $params);
132 $this->assertArrayNotHasKey('customCSSURL', $result['values']);
133 $this->assertArrayHasKey('domain_group_id', $result['values']);
137 * Ensure that on_change callbacks fire.
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].
143 public function testOnChange() {
144 global $_testOnChange_hookCalls;
145 $this->setMockSettingsMetaData(array(
146 'onChangeExample' => array(
147 'group_name' => 'CiviCRM Preferences',
149 'name' => 'onChangeExample',
151 'quick_form_type' => 'Element',
152 'html_type' => 'advmultiselect',
153 'default' => array('CiviEvent', 'CiviContribute'),
155 'title' => 'List of Components',
158 'description' => NULL,
160 'on_change' => array(// list of callbacks
161 array(__CLASS__
, '_testOnChange_onChangeExample'),
167 $_testOnChange_hookCalls = array('count' => 0);
168 $this->callAPISuccess('setting', 'create', array(
169 'onChangeExample' => array('First', 'Value'),
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']);
176 $_testOnChange_hookCalls = array('count' => 0);
177 $this->callAPISuccess('setting', 'create', array(
178 'onChangeExample' => array('Second', 'Value'),
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']);
187 * Mock callback for a setting's on_change handler
193 public static function _testOnChange_onChangeExample($oldValue, $newValue, $metadata) {
194 global $_testOnChange_hookCalls;
195 $_testOnChange_hookCalls['count']++
;
196 $_testOnChange_hookCalls['oldValue'] = $oldValue;
197 $_testOnChange_hookCalls['newValue'] = $newValue;
198 $_testOnChange_hookCalls['metadata'] = $metadata;
202 * Check getfields works.
204 public function testCreateSetting() {
205 $description = "Shows setting a variable for a given domain - if no domain is set current is assumed.";
208 'domain_id' => $this->_domainID2
,
209 'uniq_email_per_site' => 1,
211 $result = $this->callAPIAndDocument('setting', 'create', $params, __FUNCTION__
, __FILE__
);
213 $params = array('uniq_email_per_site' => 1);
214 $description = "Shows setting a variable for a current domain.";
215 $result = $this->callAPIAndDocument('setting', 'create', $params, __FUNCTION__
, __FILE__
, $description, 'CreateSettingCurrentDomain');
216 $this->assertArrayHasKey(CRM_Core_Config
::domainID(), $result['values']);
220 * Check getfields works.
222 public function testCreateInvalidSettings() {
224 'domain_id' => $this->_domainID2
,
227 $result = $this->callAPIFailure('setting', 'create', $params);
231 * Check invalid settings rejected -
233 public function testCreateInvalidURLSettings() {
235 'domain_id' => $this->_domainID2
,
236 'userFrameworkResourceURL' => 'dfhkd hfd',
238 $result = $this->callAPIFailure('setting', 'create', $params);
240 'domain_id' => $this->_domainID2
,
241 'userFrameworkResourceURL' => 'http://blah.com',
243 $result = $this->callAPISuccess('setting', 'create', $params);
247 * Check getfields works.
249 public function testCreateInvalidBooleanSettings() {
251 'domain_id' => $this->_domainID2
,
252 'track_civimail_replies' => 'dfhkdhfd',
254 $result = $this->callAPIFailure('setting', 'create', $params);
256 $params = array('track_civimail_replies' => '0');
257 $result = $this->callAPISuccess('setting', 'create', $params);
258 $getResult = $this->callAPISuccess('setting', 'get', $params);
259 $this->assertEquals(0, $getResult['values'][$this->_currentDomain
]['track_civimail_replies']);
261 $getResult = $this->callAPISuccess('setting', 'get', $params);
262 $this->assertEquals(0, $getResult['values'][$this->_currentDomain
]['track_civimail_replies']);
264 'domain_id' => $this->_domainID2
,
265 'track_civimail_replies' => '1',
267 $result = $this->callAPISuccess('setting', 'create', $params);
268 $getResult = $this->callAPISuccess('setting', 'get', $params);
269 $this->assertEquals(1, $getResult['values'][$this->_domainID2
]['track_civimail_replies']);
272 'domain_id' => $this->_domainID2
,
273 'track_civimail_replies' => 'TRUE',
275 $result = $this->callAPISuccess('setting', 'create', $params);
276 $getResult = $this->callAPISuccess('setting', 'get', $params);
278 $this->assertEquals(1, $getResult['values'][$this->_domainID2
]['track_civimail_replies'], "check TRUE is converted to 1");
282 * Check getfields works.
284 public function testCreateSettingMultipleDomains() {
285 $description = "Shows setting a variable for all domains.";
288 'domain_id' => 'all',
289 'uniq_email_per_site' => 1,
291 $result = $this->callAPIAndDocument('setting', 'create', $params, __FUNCTION__
, __FILE__
, $description, 'CreateAllDomains');
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.');
299 'domain_id' => 'all',
300 'return' => 'uniq_email_per_site',
302 // we'll check it with a 'get'
303 $description = "Shows getting a variable for all domains.";
304 $result = $this->callAPIAndDocument('setting', 'get', $params, __FUNCTION__
, __FILE__
, $description, 'GetAllDomains');
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']);
311 'domain_id' => array(1, 3),
312 'uniq_email_per_site' => 0,
314 $description = "Shows setting a variable for specified domains.";
315 $result = $this->callAPIAndDocument('setting', 'create', $params, __FUNCTION__
, __FILE__
, $description, 'CreateSpecifiedDomains');
317 $this->assertEquals(0, $result['values'][3]['uniq_email_per_site']);
318 $this->assertEquals(0, $result['values'][1]['uniq_email_per_site']);
320 'domain_id' => array(1, 2),
321 'return' => array('uniq_email_per_site'),
323 $description = "Shows getting a variable for specified domains.";
324 $result = $this->callAPIAndDocument('setting', 'get', $params, __FUNCTION__
, __FILE__
, $description, 'GetSpecifiedDomains');
325 $this->assertEquals(1, $result['values'][2]['uniq_email_per_site']);
326 $this->assertEquals(0, $result['values'][1]['uniq_email_per_site']);
330 public function testGetSetting() {
332 'domain_id' => $this->_domainID2
,
333 'return' => 'uniq_email_per_site',
335 $description = "Shows get setting a variable for a given domain - if no domain is set current is assumed.";
337 $result = $this->callAPIAndDocument('setting', 'get', $params, __FUNCTION__
, __FILE__
);
340 'return' => 'uniq_email_per_site',
342 $description = "Shows getting a variable for a current domain.";
343 $result = $this->callAPIAndDocument('setting', 'get', $params, __FUNCTION__
, __FILE__
, $description, 'GetSettingCurrentDomain');
344 $this->assertArrayHasKey(CRM_Core_Config
::domainID(), $result['values']);
348 * Check that setting defined in extension can be retrieved.
350 public function testGetExtensionSetting() {
351 $this->hookClass
->setHook('civicrm_alterSettingsFolders', array($this, 'setExtensionMetadata'));
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__');
356 Civi
::cache('settings')->flush();
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'));
360 $this->assertEquals('keyset', Civi
::settings()->get('test_key'));
361 $result = $this->callAPISuccess('setting', 'getvalue', array('name' => 'test_key', 'group' => 'Test Settings'));
362 $this->assertEquals('keyset', $result);
366 * Setting api should set & fetch settings stored in config as well as those in settings table
368 public function testSetConfigSetting() {
369 $config = CRM_Core_Config
::singleton();
370 $this->assertFalse($config->debug
== 1);
373 'domain_id' => $this->_domainID2
,
374 'debug_enabled' => 1,
376 $result = $this->callAPISuccess('setting', 'create', $params);
378 $this->assertEquals(1, Civi
::settings($this->_domainID2
)->get('debug_enabled'));
380 CRM_Core_BAO_Domain
::setDomain($this->_domainID2
);
381 $config = CRM_Core_Config
::singleton(TRUE, TRUE);
382 CRM_Core_BAO_Domain
::resetDomain();
383 $this->assertEquals(1, $config->debug
);
387 * Setting api should set & fetch settings stored in config as well as those in settings table
389 public function testGetConfigSetting() {
390 $settings = $this->callAPISuccess('setting', 'get', array(
391 'name' => 'defaultCurrency',
395 $this->assertEquals('USD', $settings['values'][0]['defaultCurrency']);
399 * Setting api should set & fetch settings stored in config as well as those in settings table
401 public function testGetSetConfigSettingMultipleDomains() {
402 $settings = $this->callAPISuccess('setting', 'create', array(
403 'defaultCurrency' => 'USD',
404 'domain_id' => $this->_currentDomain
,
407 $settings = $this->callAPISuccess('setting', 'create', array(
408 'defaultCurrency' => 'CAD',
409 'domain_id' => $this->_domainID2
,
412 $settings = $this->callAPISuccess('setting', 'get', array(
413 'return' => 'defaultCurrency',
414 'domain_id' => 'all',
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");
424 * Use getValue against a config setting.
426 public function testGetValueConfigSetting() {
428 'name' => 'monetaryThousandSeparator',
429 'group' => 'Localization Setting',
431 $result = $this->callAPISuccess('setting', 'getvalue', $params);
432 $this->assertEquals(',', $result);
435 public function testGetValue() {
437 'name' => 'petition_contacts',
438 'group' => 'Campaign Preferences',
440 $description = "Demonstrates getvalue action - intended for runtime use as better caching than get.";
442 $result = $this->callAPIAndDocument('setting', 'getvalue', $params, __FUNCTION__
, __FILE__
, $description);
443 $this->assertEquals('Petition Contacts', $result);
446 public function testGetDefaults() {
447 $description = "Gets defaults setting a variable for a given domain - if no domain is set current is assumed.";
450 'name' => 'address_format',
452 $result = $this->callAPIAndDocument('setting', 'getdefaults', $params, __FUNCTION__
, __FILE__
, $description, 'GetDefaults');
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']);
454 $params = array('name' => 'mailing_format');
455 $result = $this->callAPISuccess('setting', 'getdefaults', $params);
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']);
461 * Function tests reverting a specific parameter.
463 public function testRevert() {
465 'address_format' => 'xyz',
466 'mailing_format' => 'bcs',
468 $result = $this->callAPISuccess('setting', 'create', $params);
469 $this->assertAPISuccess($result, "in line " . __LINE__
);
470 $revertParams = array(
471 'name' => 'address_format',
473 $result = $this->callAPISuccess('setting', 'get', $params);
475 $this->assertEquals('xyz', $result['values'][CRM_Core_Config
::domainID()]['address_format']);
476 $description = "Demonstrates reverting a parameter to default value.";
477 $result = $this->callAPIAndDocument('setting', 'revert', $revertParams, __FUNCTION__
, __FILE__
, $description, '');
478 //make sure it's reverted
479 $result = $this->callAPISuccess('setting', 'get', $params);
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']);
482 'return' => array('mailing_format'),
484 $result = $this->callAPISuccess('setting', 'get', $params);
485 //make sure it's unchanged
486 $this->assertEquals('bcs', $result['values'][CRM_Core_Config
::domainID()]['mailing_format']);
490 * Tests reverting ALL parameters (specific domain)
492 public function testRevertAll() {
494 'address_format' => 'xyz',
495 'mailing_format' => 'bcs',
497 $result = $this->callAPISuccess('setting', 'create', $params);
498 $revertParams = array();
499 $result = $this->callAPISuccess('setting', 'get', $params);
501 $this->assertEquals('xyz', $result['values'][CRM_Core_Config
::domainID()]['address_format']);
503 $this->callAPISuccess('setting', 'revert', $revertParams);
504 //make sure it's reverted
505 $result = $this->callAPISuccess('setting', 'get', array('group' => 'core'));
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']);
511 * Settings should respect their defaults
513 public function testDefaults() {
515 'name' => 'B Team Domain',
517 $dom = $this->callAPISuccess('domain', 'create', $domparams);
519 'domain_id' => 'all',
521 $result = $this->callAPISuccess('setting', 'get', $params);
523 'address_format' => 'xyz',
524 'mailing_format' => 'bcs',
525 'domain_id' => $this->_domainID2
,
527 $result = $this->callAPISuccess('setting', 'create', $params);
529 'domain_id' => $dom['id'],
531 $result = $this->callAPISuccess('setting', 'get', $params);
532 $this->assertAPISuccess($result, "in line " . __LINE__
);
533 $this->assertEquals('Unconfirmed', $result['values'][$dom['id']]['tag_unconfirmed']);
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).
537 $result = $this->callAPISuccess('setting', 'fill', $params);
538 $this->assertAPISuccess($result, "in line " . __LINE__
);
539 $result = $this->callAPISuccess('setting', 'get', $params);
540 $this->assertAPISuccess($result, "in line " . __LINE__
);
541 $this->assertArrayHasKey('tag_unconfirmed', $result['values'][$dom['id']]);
543 // Setting has NULL default. Not returned.
544 //$this->assertArrayHasKey('extensionsDir', $result['values'][$dom['id']]);
546 $this->assertEquals('Unconfirmed', $result['values'][$dom['id']]['tag_unconfirmed']);