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 +--------------------------------------------------------------------+
28 require_once 'CiviTest/CiviUnitTestCase.php';
32 * Test APIv3 civicrm_setting_* functions
34 * @package CiviCRM_APIv3
35 * @subpackage API_Core
39 * Class contains api test cases for civicrm settings
42 class api_v3_SettingTest
extends CiviUnitTestCase
{
44 protected $_apiversion = 3;
45 protected $_contactID;
47 protected $_currentDomain;
48 protected $_domainID2;
49 protected $_domainID3;
51 public function setUp() {
54 'name' => 'Default Domain Name',
55 'domain_version' => '4.7',
57 $result = $this->callAPISuccess('domain', 'get', $params);
58 if (empty($result['id'])) {
59 $result = $this->callAPISuccess('domain', 'create', $params);
62 $params['name'] = 'Second Domain';
63 $result = $this->callAPISuccess('domain', 'get', $params);
64 if (empty($result['id'])) {
65 $result = $this->callAPISuccess('domain', 'create', $params);
67 $this->_domainID2
= $result['id'];
68 $params['name'] = 'A-team domain';
69 $result = $this->callAPISuccess('domain', 'get', $params);
70 if (empty($result['id'])) {
71 $result = $this->callAPISuccess('domain', 'create', $params);
73 $this->_domainID3
= $result['id'];
74 $this->_currentDomain
= CRM_Core_Config
::domainID();
75 $this->hookClass
= CRM_Utils_Hook
::singleton();
78 public function tearDown() {
79 CRM_Utils_Hook
::singleton()->reset();
81 $this->callAPISuccess('system', 'flush', array());
82 $this->quickCleanup(array('civicrm_domain'));
86 * Set additional settings into metadata (implements hook)
87 * @param array $metaDataFolders
89 public function setExtensionMetadata(&$metaDataFolders) {
91 $metaDataFolders[] = $civicrm_root . '/tests/phpunit/api/v3/settings';
96 * Check getfields works.
98 public function testGetFields() {
99 $description = 'Demonstrate return from getfields - see subfolder for variants';
100 $result = $this->callAPIAndDocument('setting', 'getfields', array(), __FUNCTION__
, __FILE__
, $description);
101 $this->assertArrayHasKey('customCSSURL', $result['values']);
103 $description = 'Demonstrate return from getfields';
104 $result = $this->callAPISuccess('setting', 'getfields', array());
105 $this->assertArrayHasKey('customCSSURL', $result['values']);
106 $this->callAPISuccess('system', 'flush', array());
110 * Let's check it's loading from cache by meddling with the cache
112 public function testGetFieldsCaching() {
113 $settingsMetadata = array();
114 Civi
::cache('settings')->set('settingsMetadata_' . \CRM_Core_Config
::domainID() . '_', $settingsMetadata);
115 Civi
::cache('settings')->set(\Civi\Core\SettingsMetadata
::ALL
, $settingsMetadata);
116 $result = $this->callAPISuccess('setting', 'getfields', array());
117 $this->assertArrayNotHasKey('customCSSURL', $result['values']);
118 $this->quickCleanup(array('civicrm_cache'));
119 Civi
::cache('settings')->flush();
122 public function testGetFieldsFilters() {
123 $params = array('name' => 'advanced_search_options');
124 $result = $this->callAPISuccess('setting', 'getfields', $params);
125 $this->assertArrayNotHasKey('customCSSURL', $result['values']);
126 $this->assertArrayHasKey('advanced_search_options', $result['values']);
130 * Test that getfields will filter on group.
132 public function testGetFieldsGroupFilters() {
133 $params = array('filters' => array('group' => 'multisite'));
134 $result = $this->callAPISuccess('setting', 'getfields', $params);
135 $this->assertArrayNotHasKey('customCSSURL', $result['values']);
136 $this->assertArrayHasKey('domain_group_id', $result['values']);
140 * Ensure that on_change callbacks fire.
142 * Note: api_v3_SettingTest::testOnChange and CRM_Core_BAO_SettingTest::testOnChange
143 * are very similar, but they exercise different codepaths. The first uses the API
144 * and setItems [plural]; the second uses setItem [singular].
146 public function testOnChange() {
147 global $_testOnChange_hookCalls;
148 $this->setMockSettingsMetaData(array(
149 'onChangeExample' => array(
150 'group_name' => 'CiviCRM Preferences',
152 'name' => 'onChangeExample',
154 'quick_form_type' => 'Element',
155 'html_type' => 'advmultiselect',
156 'default' => array('CiviEvent', 'CiviContribute'),
158 'title' => 'List of Components',
161 'description' => NULL,
163 'on_change' => array(// list of callbacks
164 array(__CLASS__
, '_testOnChange_onChangeExample'),
170 $_testOnChange_hookCalls = array('count' => 0);
171 $this->callAPISuccess('setting', 'create', array(
172 'onChangeExample' => array('First', 'Value'),
174 $this->assertEquals(1, $_testOnChange_hookCalls['count']);
175 $this->assertEquals(array('First', 'Value'), $_testOnChange_hookCalls['newValue']);
176 $this->assertEquals('List of Components', $_testOnChange_hookCalls['metadata']['title']);
179 $_testOnChange_hookCalls = array('count' => 0);
180 $this->callAPISuccess('setting', 'create', array(
181 'onChangeExample' => array('Second', 'Value'),
183 $this->assertEquals(1, $_testOnChange_hookCalls['count']);
184 $this->assertEquals(array('First', 'Value'), $_testOnChange_hookCalls['oldValue']);
185 $this->assertEquals(array('Second', 'Value'), $_testOnChange_hookCalls['newValue']);
186 $this->assertEquals('List of Components', $_testOnChange_hookCalls['metadata']['title']);
190 * Mock callback for a setting's on_change handler
196 public static function _testOnChange_onChangeExample($oldValue, $newValue, $metadata) {
197 global $_testOnChange_hookCalls;
198 $_testOnChange_hookCalls['count']++
;
199 $_testOnChange_hookCalls['oldValue'] = $oldValue;
200 $_testOnChange_hookCalls['newValue'] = $newValue;
201 $_testOnChange_hookCalls['metadata'] = $metadata;
205 * Check getfields works.
207 public function testCreateSetting() {
208 $description = "Shows setting a variable for a given domain - if no domain is set current is assumed.";
211 'domain_id' => $this->_domainID2
,
212 'uniq_email_per_site' => 1,
214 $result = $this->callAPIAndDocument('setting', 'create', $params, __FUNCTION__
, __FILE__
);
216 $params = array('uniq_email_per_site' => 1);
217 $description = "Shows setting a variable for a current domain.";
218 $result = $this->callAPIAndDocument('setting', 'create', $params, __FUNCTION__
, __FILE__
, $description, 'CreateSettingCurrentDomain');
219 $this->assertArrayHasKey(CRM_Core_Config
::domainID(), $result['values']);
223 * Check getfields works.
225 public function testCreateInvalidSettings() {
227 'domain_id' => $this->_domainID2
,
230 $result = $this->callAPIFailure('setting', 'create', $params);
234 * Check invalid settings rejected -
236 public function testCreateInvalidURLSettings() {
238 'domain_id' => $this->_domainID2
,
239 'userFrameworkResourceURL' => 'dfhkd*hfd',
241 $result = $this->callAPIFailure('setting', 'create', $params);
243 'domain_id' => $this->_domainID2
,
244 'userFrameworkResourceURL' => 'http://blah.com',
246 $result = $this->callAPISuccess('setting', 'create', $params);
250 * Check getfields works.
252 public function testCreateInvalidBooleanSettings() {
254 'domain_id' => $this->_domainID2
,
255 'track_civimail_replies' => 'dfhkdhfd',
257 $result = $this->callAPIFailure('setting', 'create', $params);
259 $params = array('track_civimail_replies' => '0');
260 $result = $this->callAPISuccess('setting', 'create', $params);
261 $getResult = $this->callAPISuccess('setting', 'get', $params);
262 $this->assertEquals(0, $getResult['values'][$this->_currentDomain
]['track_civimail_replies']);
264 $getResult = $this->callAPISuccess('setting', 'get', $params);
265 $this->assertEquals(0, $getResult['values'][$this->_currentDomain
]['track_civimail_replies']);
267 'domain_id' => $this->_domainID2
,
268 'track_civimail_replies' => '1',
270 $result = $this->callAPISuccess('setting', 'create', $params);
271 $getResult = $this->callAPISuccess('setting', 'get', $params);
272 $this->assertEquals(1, $getResult['values'][$this->_domainID2
]['track_civimail_replies']);
275 'domain_id' => $this->_domainID2
,
276 'track_civimail_replies' => 'TRUE',
278 $result = $this->callAPISuccess('setting', 'create', $params);
279 $getResult = $this->callAPISuccess('setting', 'get', $params);
281 $this->assertEquals(1, $getResult['values'][$this->_domainID2
]['track_civimail_replies'], "check TRUE is converted to 1");
285 * Check getfields works.
287 public function testCreateSettingMultipleDomains() {
288 $description = "Shows setting a variable for all domains.";
291 'domain_id' => 'all',
292 'uniq_email_per_site' => 1,
294 $result = $this->callAPIAndDocument('setting', 'create', $params, __FUNCTION__
, __FILE__
, $description, 'CreateAllDomains');
296 $this->assertEquals(1, $result['values'][2]['uniq_email_per_site']);
297 $this->assertEquals(1, $result['values'][1]['uniq_email_per_site']);
298 $this->assertArrayHasKey(3, $result['values'], 'Domain create probably failed Debug this IF domain test is passing');
299 $this->assertEquals(1, $result['values'][3]['uniq_email_per_site'], 'failed to set setting for domain 3.');
302 'domain_id' => 'all',
303 'return' => 'uniq_email_per_site',
305 // we'll check it with a 'get'
306 $description = "Shows getting a variable for all domains.";
307 $result = $this->callAPIAndDocument('setting', 'get', $params, __FUNCTION__
, __FILE__
, $description, 'GetAllDomains');
309 $this->assertEquals(1, $result['values'][2]['uniq_email_per_site']);
310 $this->assertEquals(1, $result['values'][1]['uniq_email_per_site']);
311 $this->assertEquals(1, $result['values'][3]['uniq_email_per_site']);
314 'domain_id' => array(1, 3),
315 'uniq_email_per_site' => 0,
317 $description = "Shows setting a variable for specified domains.";
318 $result = $this->callAPIAndDocument('setting', 'create', $params, __FUNCTION__
, __FILE__
, $description, 'CreateSpecifiedDomains');
320 $this->assertEquals(0, $result['values'][3]['uniq_email_per_site']);
321 $this->assertEquals(0, $result['values'][1]['uniq_email_per_site']);
323 'domain_id' => array(1, 2),
324 'return' => array('uniq_email_per_site'),
326 $description = "Shows getting a variable for specified domains.";
327 $result = $this->callAPIAndDocument('setting', 'get', $params, __FUNCTION__
, __FILE__
, $description, 'GetSpecifiedDomains');
328 $this->assertEquals(1, $result['values'][2]['uniq_email_per_site']);
329 $this->assertEquals(0, $result['values'][1]['uniq_email_per_site']);
333 public function testGetSetting() {
335 'domain_id' => $this->_domainID2
,
336 'return' => 'uniq_email_per_site',
338 $description = "Shows get setting a variable for a given domain - if no domain is set current is assumed.";
340 $result = $this->callAPIAndDocument('setting', 'get', $params, __FUNCTION__
, __FILE__
);
343 'return' => 'uniq_email_per_site',
345 $description = "Shows getting a variable for a current domain.";
346 $result = $this->callAPIAndDocument('setting', 'get', $params, __FUNCTION__
, __FILE__
, $description, 'GetSettingCurrentDomain');
347 $this->assertArrayHasKey(CRM_Core_Config
::domainID(), $result['values']);
351 * Check that setting defined in extension can be retrieved.
353 public function testGetExtensionSetting() {
354 $this->hookClass
->setHook('civicrm_alterSettingsFolders', array($this, 'setExtensionMetadata'));
356 // the caching of data to all duplicates the caching of data to the empty string
357 CRM_Core_BAO_Cache
::setItem($data, 'CiviCRM setting Spec', 'All');
358 CRM_Core_BAO_Cache
::setItem($data, 'CiviCRM setting Specs', 'settingsMetadata__');
359 Civi
::cache('settings')->flush();
360 $fields = $this->callAPISuccess('setting', 'getfields', array('filters' => array('group_name' => 'Test Settings')));
361 $this->assertArrayHasKey('test_key', $fields['values']);
362 $this->callAPISuccess('setting', 'create', array('test_key' => 'keyset'));
363 $this->assertEquals('keyset', Civi
::settings()->get('test_key'));
364 $result = $this->callAPISuccess('setting', 'getvalue', array('name' => 'test_key', 'group' => 'Test Settings'));
365 $this->assertEquals('keyset', $result);
369 * Setting api should set & fetch settings stored in config as well as those in settings table
371 public function testSetConfigSetting() {
372 $config = CRM_Core_Config
::singleton();
373 $this->assertFalse($config->debug
== 1);
376 'domain_id' => $this->_domainID2
,
377 'debug_enabled' => 1,
379 $result = $this->callAPISuccess('setting', 'create', $params);
381 $this->assertEquals(1, Civi
::settings($this->_domainID2
)->get('debug_enabled'));
383 CRM_Core_BAO_Domain
::setDomain($this->_domainID2
);
384 $config = CRM_Core_Config
::singleton(TRUE, TRUE);
385 CRM_Core_BAO_Domain
::resetDomain();
386 $this->assertEquals(1, $config->debug
);
390 * Setting api should set & fetch settings stored in config as well as those in settings table
392 public function testGetConfigSetting() {
393 $settings = $this->callAPISuccess('setting', 'get', array(
394 'name' => 'defaultCurrency',
398 $this->assertEquals('USD', $settings['values'][0]['defaultCurrency']);
402 * Setting api should set & fetch settings stored in config as well as those in settings table
404 public function testGetSetConfigSettingMultipleDomains() {
405 $settings = $this->callAPISuccess('setting', 'create', array(
406 'defaultCurrency' => 'USD',
407 'domain_id' => $this->_currentDomain
,
410 $settings = $this->callAPISuccess('setting', 'create', array(
411 'defaultCurrency' => 'CAD',
412 'domain_id' => $this->_domainID2
,
415 $settings = $this->callAPISuccess('setting', 'get', array(
416 'return' => 'defaultCurrency',
417 'domain_id' => 'all',
420 $this->assertEquals('USD', $settings['values'][$this->_currentDomain
]['defaultCurrency']);
421 $this->assertEquals('CAD', $settings['values'][$this->_domainID2
]['defaultCurrency'],
422 "second domain (id {$this->_domainID2} ) should be set to CAD. First dom was {$this->_currentDomain} & was USD");
427 * Use getValue against a config setting.
429 public function testGetValueConfigSetting() {
431 'name' => 'monetaryThousandSeparator',
432 'group' => 'Localization Setting',
434 $result = $this->callAPISuccess('setting', 'getvalue', $params);
435 $this->assertEquals(',', $result);
438 public function testGetValue() {
440 'name' => 'petition_contacts',
441 'group' => 'Campaign Preferences',
443 $description = "Demonstrates getvalue action - intended for runtime use as better caching than get.";
445 $result = $this->callAPIAndDocument('setting', 'getvalue', $params, __FUNCTION__
, __FILE__
, $description);
446 $this->assertEquals('Petition Contacts', $result);
449 public function testGetDefaults() {
450 $description = "Gets defaults setting a variable for a given domain - if no domain is set current is assumed.";
453 'name' => 'address_format',
455 $result = $this->callAPIAndDocument('setting', 'getdefaults', $params, __FUNCTION__
, __FILE__
, $description, 'GetDefaults');
456 $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']);
457 $params = array('name' => 'mailing_format');
458 $result = $this->callAPISuccess('setting', 'getdefaults', $params);
459 $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']);
460 $this->assertArrayHasKey(CRM_Core_Config
::domainID(), $result['values']);
464 * Function tests reverting a specific parameter.
466 public function testRevert() {
468 'address_format' => 'xyz',
469 'mailing_format' => 'bcs',
471 $result = $this->callAPISuccess('setting', 'create', $params);
472 $this->assertAPISuccess($result, "in line " . __LINE__
);
473 $revertParams = array(
474 'name' => 'address_format',
476 $result = $this->callAPISuccess('setting', 'get', $params);
478 $this->assertEquals('xyz', $result['values'][CRM_Core_Config
::domainID()]['address_format']);
479 $description = "Demonstrates reverting a parameter to default value.";
480 $result = $this->callAPIAndDocument('setting', 'revert', $revertParams, __FUNCTION__
, __FILE__
, $description, '');
481 //make sure it's reverted
482 $result = $this->callAPISuccess('setting', 'get', $params);
483 $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']);
485 'return' => array('mailing_format'),
487 $result = $this->callAPISuccess('setting', 'get', $params);
488 //make sure it's unchanged
489 $this->assertEquals('bcs', $result['values'][CRM_Core_Config
::domainID()]['mailing_format']);
493 * Tests reverting ALL parameters (specific domain)
495 public function testRevertAll() {
497 'address_format' => 'xyz',
498 'mailing_format' => 'bcs',
500 $result = $this->callAPISuccess('setting', 'create', $params);
501 $revertParams = array();
502 $result = $this->callAPISuccess('setting', 'get', $params);
504 $this->assertEquals('xyz', $result['values'][CRM_Core_Config
::domainID()]['address_format']);
506 $this->callAPISuccess('setting', 'revert', $revertParams);
507 //make sure it's reverted
508 $result = $this->callAPISuccess('setting', 'get', array('group' => 'core'));
509 $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']);
510 $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']);
514 * Settings should respect their defaults
516 public function testDefaults() {
518 'name' => 'B Team Domain',
519 'domain_version' => '4.7',
521 $dom = $this->callAPISuccess('domain', 'create', $domparams);
523 'domain_id' => 'all',
525 $result = $this->callAPISuccess('setting', 'get', $params);
527 'address_format' => 'xyz',
528 'mailing_format' => 'bcs',
529 'domain_id' => $this->_domainID2
,
531 $result = $this->callAPISuccess('setting', 'create', $params);
533 'domain_id' => $dom['id'],
535 $result = $this->callAPISuccess('setting', 'get', $params);
536 $this->assertAPISuccess($result, "in line " . __LINE__
);
537 $this->assertEquals('Unconfirmed', $result['values'][$dom['id']]['tag_unconfirmed']);
539 // The 'fill' operation is no longer necessary, but third parties might still use it, so let's
540 // make sure it doesn't do anything weird (crashing or breaking values).
541 $result = $this->callAPISuccess('setting', 'fill', $params);
542 $this->assertAPISuccess($result, "in line " . __LINE__
);
543 $result = $this->callAPISuccess('setting', 'get', $params);
544 $this->assertAPISuccess($result, "in line " . __LINE__
);
545 $this->assertArrayHasKey('tag_unconfirmed', $result['values'][$dom['id']]);
547 // Setting has NULL default. Not returned.
548 //$this->assertArrayHasKey('extensionsDir', $result['values'][$dom['id']]);
550 $this->assertEquals('Unconfirmed', $result['values'][$dom['id']]['tag_unconfirmed']);