3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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;
54 function __construct() {
55 parent
::__construct();
64 'name' => 'Settings Tests',
65 'description' => 'Settings API',
66 'group' => 'CiviCRM API Tests',
73 'name' => 'Default Domain Name',
75 $result = $this->callAPISuccess( 'domain','get',$params);
76 if(empty($result['id'])){
77 $result = $this->callAPISuccess( 'domain','create',$params );
80 $params['name'] = 'Second Domain';
81 $result = $this->callAPISuccess( 'domain','get',$params);
82 if(empty($result['id'])){
83 $result = $this->callAPISuccess( 'domain','create',$params );
85 $this->_domainID2
= $result['id'];
86 $params['name'] = 'A-team domain';
87 $result = $this->callAPISuccess( 'domain','get',$params);
88 if(empty($result['id'])){
89 $result = $this->callAPISuccess( 'domain','create',$params );
91 $this->_domainID3
= $result['id'];
92 $this->_currentDomain
= CRM_Core_Config
::domainID();
93 $this->hookClass
= CRM_Utils_Hook
::singleton();
97 CRM_Utils_Hook
::singleton()->reset();
99 $this->callAPISuccess('system','flush', array());
100 $this->quickCleanup(array('civicrm_domain'));
104 * Set additional settings into metadata (implements hook)
105 * @param array $metaDataFolders
107 function setExtensionMetadata(&$metaDataFolders) {
108 global $civicrm_root;
109 $metaDataFolders[] = $civicrm_root . '/tests/phpunit/api/v3/settings';
113 * Check getfields works
115 function testGetFields() {
116 $description = 'Demonstrate return from getfields - see subfolder for variants';
117 $result = $this->callAPIAndDocument('setting', 'getfields', array(), __FUNCTION__
, __FILE__
, $description);
118 $this->assertArrayHasKey('customCSSURL', $result['values']);
120 $description = 'Demonstrate return from getfields';
121 $result = $this->callAPISuccess('setting', 'getfields', array());
122 $this->assertArrayHasKey('customCSSURL', $result['values']);
123 $this->callAPISuccess('system','flush', array());
127 * Let's check it's loading from cache by meddling with the cache
129 function testGetFieldsCaching() {
130 $settingsMetadata = array();
131 CRM_Core_BAO_Cache
::setItem($settingsMetadata,'CiviCRM setting Specs', 'settingsMetadata__');
132 CRM_Core_BAO_Cache
::setItem($settingsMetadata,'CiviCRM setting Spec', 'All');
133 $result = $this->callAPISuccess('setting', 'getfields', array());
134 $this->assertArrayNotHasKey('customCSSURL', $result['values']);
135 $this->quickCleanup(array('civicrm_cache'));
138 function testGetFieldsFilters() {
139 $params = array('name' => 'advanced_search_options');
140 $result = $this->callAPISuccess('setting', 'getfields', $params);
141 $this->assertArrayNotHasKey('customCSSURL', $result['values']);
142 $this->assertArrayHasKey('advanced_search_options',$result['values']);
146 * Test that getfields will filter on group
148 function testGetFieldsGroupFilters() {
149 $params = array('filters' => array('group' => 'multisite'));
150 $result = $this->callAPISuccess('setting', 'getfields', $params);
151 $this->assertArrayNotHasKey('customCSSURL', $result['values']);
152 $this->assertArrayHasKey('domain_group_id',$result['values']);
156 * Test that getfields will filter on another field (prefetch)
158 function testGetFieldsPrefetchFilters() {
159 $params = array('filters' => array('prefetch' => 1));
160 $result = $this->callAPISuccess('setting', 'getfields', $params);
161 $this->assertArrayNotHasKey('disable_mandatory_tokens_check', $result['values']);
162 $this->assertArrayHasKey('monetaryDecimalPoint',$result['values']);
166 * Ensure that on_change callbacks fire.
168 * Note: api_v3_SettingTest::testOnChange and CRM_Core_BAO_SettingTest::testOnChange
169 * are very similar, but they exercise different codepaths. The first uses the API
170 * and setItems [plural]; the second uses setItem [singular].
172 function testOnChange() {
173 global $_testOnChange_hookCalls;
174 $this->setMockSettingsMetaData(array(
175 'onChangeExample' => array(
176 'group_name' => 'CiviCRM Preferences',
178 'name' => 'onChangeExample',
180 'quick_form_type' => 'Element',
181 'html_type' => 'advmultiselect',
182 'default' => array('CiviEvent', 'CiviContribute'),
184 'title' => 'List of Components',
187 'description' => NULL,
189 'on_change' => array( // list of callbacks
190 array(__CLASS__
, '_testOnChange_onChangeExample')
196 $_testOnChange_hookCalls = array('count' => 0);
197 $this->callAPISuccess('setting', 'create', array(
198 'onChangeExample' => array('First', 'Value'),
200 $this->assertEquals(1, $_testOnChange_hookCalls['count']);
201 $this->assertEquals(array('First', 'Value'), $_testOnChange_hookCalls['newValue']);
202 $this->assertEquals('List of Components', $_testOnChange_hookCalls['metadata']['title']);
205 $_testOnChange_hookCalls = array('count' => 0);
206 $this->callAPISuccess('setting', 'create', array(
207 'onChangeExample' => array('Second', 'Value'),
209 $this->assertEquals(1, $_testOnChange_hookCalls['count']);
210 $this->assertEquals(array('First', 'Value'), $_testOnChange_hookCalls['oldValue']);
211 $this->assertEquals(array('Second', 'Value'), $_testOnChange_hookCalls['newValue']);
212 $this->assertEquals('List of Components', $_testOnChange_hookCalls['metadata']['title']);
216 * Mock callback for a setting's on_change handler
222 static function _testOnChange_onChangeExample($oldValue, $newValue, $metadata) {
223 global $_testOnChange_hookCalls;
224 $_testOnChange_hookCalls['count']++
;
225 $_testOnChange_hookCalls['oldValue'] = $oldValue;
226 $_testOnChange_hookCalls['newValue'] = $newValue;
227 $_testOnChange_hookCalls['metadata'] = $metadata;
231 * Check getfields works
233 function testCreateSetting() {
234 $description = "shows setting a variable for a given domain - if no domain is set current is assumed";
237 'domain_id' => $this->_domainID2
,
238 'uniq_email_per_site' => 1,
240 $result = $this->callAPIAndDocument('setting', 'create', $params, __FUNCTION__
, __FILE__
);
242 $params = array('uniq_email_per_site' => 1,);
243 $description = "shows setting a variable for a current domain";
244 $result = $this->callAPIAndDocument('setting', 'create', $params, __FUNCTION__
, __FILE__
, $description, 'CreateSettingCurrentDomain');
245 $this->assertArrayHasKey(CRM_Core_Config
::domainID(), $result['values']);
249 * Check getfields works
251 function testCreateInvalidSettings() {
253 'domain_id' => $this->_domainID2
,
256 $result = $this->callAPIFailure('setting', 'create', $params);
260 * Check invalid settings rejected -
262 function testCreateInvalidURLSettings() {
264 'domain_id' => $this->_domainID2
,
265 'userFrameworkResourceURL' => 'dfhkdhfd',
267 $result = $this->callAPIFailure('setting', 'create', $params);
269 'domain_id' => $this->_domainID2
,
270 'userFrameworkResourceURL' => 'http://blah.com',
272 $result = $this->callAPISuccess('setting', 'create', $params);
276 * Check getfields works
278 function testCreateInvalidBooleanSettings() {
280 'domain_id' => $this->_domainID2
,
281 'track_civimail_replies' => 'dfhkdhfd',
283 $result = $this->callAPIFailure('setting', 'create', $params);
285 $params = array('track_civimail_replies' => '0',);
286 $result = $this->callAPISuccess('setting', 'create', $params);
287 $getResult = $this->callAPISuccess('setting','get',$params);
288 $this->assertEquals(0, $getResult['values'][$this->_currentDomain
]['track_civimail_replies']);
290 $getResult = $this->callAPISuccess('setting','get',$params);
291 $this->assertEquals(0, $getResult['values'][$this->_currentDomain
]['track_civimail_replies']);
292 $params = array( 'domain_id' => $this->_domainID2
,
293 'track_civimail_replies' => '1',
295 $result = $this->callAPISuccess('setting', 'create', $params);
296 $getResult = $this->callAPISuccess('setting','get',$params);
297 $this->assertEquals(1, $getResult['values'][$this->_domainID2
]['track_civimail_replies']);
300 'domain_id' => $this->_domainID2
,
301 'track_civimail_replies' => 'TRUE',
303 $result = $this->callAPISuccess('setting', 'create', $params);
304 $getResult = $this->callAPISuccess('setting','get',$params);
306 $this->assertEquals(1, $getResult['values'][$this->_domainID2
]['track_civimail_replies'], "check TRUE is converted to 1");
310 * Check getfields works
312 function testCreateSettingMultipleDomains() {
313 $description = "shows setting a variable for all domains";
316 'domain_id' => 'all',
317 'uniq_email_per_site' => 1,
319 $result = $this->callAPIAndDocument('setting', 'create', $params, __FUNCTION__
, __FILE__
,$description, 'CreateAllDomains');
321 $this->assertEquals(1, $result['values'][2]['uniq_email_per_site']);
322 $this->assertEquals(1, $result['values'][1]['uniq_email_per_site']);
323 $this->assertArrayHasKey(3, $result['values'], 'Domain create probably failed Debug this IF domain test is passing');
324 $this->assertEquals(1, $result['values'][3]['uniq_email_per_site'], 'failed to set setting for domain 3.');
327 'domain_id' => 'all',
328 'return' => 'uniq_email_per_site'
330 // we'll check it with a 'get'
331 $description = "shows getting a variable for all domains";
332 $result = $this->callAPIAndDocument('setting', 'get', $params, __FUNCTION__
, __FILE__
,$description, 'GetAllDomains', 'Get');
334 $this->assertEquals(1, $result['values'][2]['uniq_email_per_site']);
335 $this->assertEquals(1, $result['values'][1]['uniq_email_per_site']);
336 $this->assertEquals(1, $result['values'][3]['uniq_email_per_site']);
339 'domain_id' => array(1,3),
340 'uniq_email_per_site' => 0,
342 $description = "shows setting a variable for specified domains";
343 $result = $this->callAPIAndDocument('setting', 'create', $params, __FUNCTION__
, __FILE__
,$description, 'CreateSpecifiedDomains');
345 $this->assertEquals(0, $result['values'][3]['uniq_email_per_site']);
346 $this->assertEquals(0, $result['values'][1]['uniq_email_per_site']);
348 'domain_id' => array(1,2),
349 'return' => array('uniq_email_per_site'),
351 $description = "shows getting a variable for specified domains";
352 $result = $this->callAPIAndDocument('setting', 'get', $params, __FUNCTION__
, __FILE__
,$description, 'GetSpecifiedDomains', 'Get');
353 $this->assertEquals(1, $result['values'][2]['uniq_email_per_site']);
354 $this->assertEquals(0, $result['values'][1]['uniq_email_per_site']);
358 function testGetSetting() {
360 'domain_id' => $this->_domainID2
,
361 'return' => 'uniq_email_per_site',
363 $description = "shows get setting a variable for a given domain - if no domain is set current is assumed";
365 $result = $this->callAPIAndDocument('setting', 'get', $params, __FUNCTION__
, __FILE__
);
368 'return' => 'uniq_email_per_site',
370 $description = "shows getting a variable for a current domain";
371 $result = $this->callAPIAndDocument('setting', 'get', $params, __FUNCTION__
, __FILE__
, $description, 'GetSettingCurrentDomain');
372 $this->assertArrayHasKey(CRM_Core_Config
::domainID(), $result['values']);
376 * Check that setting defined in extension can be retrieved
378 function testGetExtensionSetting() {
379 $this->hookClass
->setHook('civicrm_alterSettingsFolders', array($this, 'setExtensionMetadata'));
381 // the caching of data to all duplicates the caching of data to the empty string
382 CRM_Core_BAO_Cache
::setItem($data, 'CiviCRM setting Spec', 'All');
383 CRM_Core_BAO_Cache
::setItem($data, 'CiviCRM setting Specs', 'settingsMetadata__');
384 $fields = $this->callAPISuccess('setting', 'getfields', array('filters' => array('group_name' => 'Test Settings')));
385 $this->assertArrayHasKey('test_key', $fields['values']);
386 $this->callAPISuccess('setting', 'create', array('test_key' => 'keyset'));
387 $result = $this->callAPISuccess('setting', 'getvalue', array('name' => 'test_key', 'group' => 'Test Settings'));
388 $this->assertEquals('keyset', $result);
392 * Setting api should set & fetch settings stored in config as well as those in settings table
394 function testSetConfigSetting() {
395 $config = CRM_Core_Config
::singleton();
396 $this->assertFalse($config->debug
== 1);
398 'domain_id' => $this->_domainID2
,
399 'debug_enabled' => 1,
401 $result = $this->callAPISuccess('setting', 'create', $params);
402 CRM_Core_BAO_Domain
::setDomain($this->_domainID2
);
403 $config = CRM_Core_Config
::singleton(TRUE, TRUE);
404 CRM_Core_BAO_Domain
::resetDomain();
405 $this->assertTrue($config->debug
== 1);
406 // this should NOT be stored in the settings table now - only in config
407 $sql = " SELECT count(*) as c FROM civicrm_setting WHERE name LIKE '%maxFileSize%'";
408 $dao = CRM_Core_DAO
::executeQuery($sql);
410 $this->assertEquals($dao->c
, 0);
414 * Setting api should set & fetch settings stored in config as well as those in settings table
416 function testGetConfigSetting() {
417 $settings = $this->callAPISuccess('setting', 'get', array(
418 'name' => 'defaultCurrency', 'sequential' => 1,)
420 $this->assertEquals('USD', $settings['values'][0]['defaultCurrency']);
424 * Setting api should set & fetch settings stored in config as well as those in settings table
426 function testGetSetConfigSettingMultipleDomains() {
427 $settings = $this->callAPISuccess('setting', 'create', array(
428 'defaultCurrency' => 'USD', 'domain_id' => $this->_currentDomain
)
430 $settings = $this->callAPISuccess('setting', 'create', array(
431 'defaultCurrency' => 'CAD', 'domain_id' => $this->_domainID2
)
433 $settings = $this->callAPISuccess('setting', 'get', array(
434 'return' => 'defaultCurrency', 'domain_id' => 'all',
437 $this->assertEquals('USD', $settings['values'][$this->_currentDomain
]['defaultCurrency']);
438 $this->assertEquals('CAD', $settings['values'][$this->_domainID2
]['defaultCurrency'],
439 "second domain (id {$this->_domainID2} ) should be set to CAD. First dom was {$this->_currentDomain} & was USD");
444 * Use getValue against a config setting
446 function testGetValueConfigSetting() {
447 $params = array( 'name' => 'monetaryThousandSeparator',
448 'group' => 'Localization Setting',
450 $result = $this->callAPISuccess('setting', 'getvalue', $params);
451 $this->assertEquals(',', $result);
454 function testGetValue() {
455 $params = array( 'name' => 'petition_contacts',
456 'group' => 'Campaign Preferences'
458 $description = "Demonstrates getvalue action - intended for runtime use as better caching than get";
460 $result = $this->callAPIAndDocument('setting', 'getvalue', $params, __FUNCTION__
, __FILE__
, $description);
461 $this->assertEquals('Petition Contacts', $result);
464 function testGetDefaults() {
465 $description = "gets defaults setting a variable for a given domain - if no domain is set current is assumed";
468 'name' => 'address_format',
470 $result = $this->callAPIAndDocument('setting', 'getdefaults', $params, __FUNCTION__
, __FILE__
,$description,'GetDefaults','getdefaults');
471 $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']);
472 $params = array('name' => 'mailing_format',);
473 $result = $this->callAPISuccess('setting', 'getdefaults', $params);
474 $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']);
475 $this->assertArrayHasKey(CRM_Core_Config
::domainID(), $result['values']);
479 * Function tests reverting a specific parameter
481 function testRevert() {
482 $params = array( 'address_format' => 'xyz',
483 'mailing_format' => 'bcs',
485 $result = $this->callAPISuccess('setting', 'create', $params);
486 $this->assertAPISuccess($result, "in line " . __LINE__
);
487 $revertParams = array( 'name' => 'address_format'
489 $result = $this->callAPISuccess('setting', 'get', $params);
491 $this->assertEquals('xyz', $result['values'][CRM_Core_Config
::domainID()]['address_format']);
492 $description = "Demonstrates reverting a parameter to default value";
493 $result = $this->callAPIAndDocument('setting', 'revert', $revertParams, __FUNCTION__
, __FILE__
,$description,'','revert');
494 //make sure it's reverted
495 $result = $this->callAPISuccess('setting', 'get', $params);
496 $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']);
497 $params = array( 'return' => array('mailing_format'),
499 $result = $this->callAPISuccess('setting', 'get', $params);
500 //make sure it's unchanged
501 $this->assertEquals('bcs', $result['values'][CRM_Core_Config
::domainID()]['mailing_format']);
505 * Tests reverting ALL parameters (specific domain)
507 function testRevertAll() {
508 $params = array( 'address_format' => 'xyz',
509 'mailing_format' => 'bcs',
511 $result = $this->callAPISuccess('setting', 'create', $params);
512 $revertParams = array( );
513 $result = $this->callAPISuccess('setting', 'get', $params);
515 $this->assertEquals('xyz', $result['values'][CRM_Core_Config
::domainID()]['address_format']);
517 $this->callAPISuccess('setting', 'revert', $revertParams);
518 //make sure it's reverted
519 $result = $this->callAPISuccess('setting', 'get', array('group' => 'core'));
520 $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']);
521 $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']);
525 * Tests filling missing params
527 function testFill() {
529 'name' => 'B Team Domain',
531 $dom = $this->callAPISuccess('domain', 'create', $domparams);
532 $params = array( 'domain_id' => 'all',
534 $result = $this->callAPISuccess('setting', 'get', $params);
535 $params = array( 'address_format' => 'xyz',
536 'mailing_format' => 'bcs',
537 'domain_id' => $this->_domainID2
,
539 $result = $this->callAPISuccess('setting', 'create', $params);
540 $params = array( 'domain_id' => $dom['id'],
542 $result = $this->callAPISuccess('setting', 'get', $params);
543 $this->assertAPISuccess($result, "in line " . __LINE__
);
544 $this->assertArrayNotHasKey('tag_unconfirmed', $result['values'][$dom['id']],'setting for domain 3 should not be set. Debug this IF domain test is passing');
545 $result = $this->callAPISuccess('setting', 'fill', $params);
546 $this->assertAPISuccess($result, "in line " . __LINE__
);
547 $result = $this->callAPISuccess('setting', 'get', $params);
548 $this->assertAPISuccess($result, "in line " . __LINE__
);
549 $this->assertArrayHasKey('tag_unconfirmed', $result['values'][$dom['id']]);
550 $this->assertArrayHasKey('extensionsDir', $result['values'][$dom['id']]);
551 $this->assertEquals('Unconfirmed', $result['values'][$dom['id']]['tag_unconfirmed']);