3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.4 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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 function __construct() {
52 parent
::__construct();
58 'name' => 'Settings Tests',
59 'description' => 'Settings API',
60 'group' => 'CiviCRM API Tests',
67 'name' => 'Default Domain Name',
69 $result = $this->callAPISuccess( 'domain','get',$params);
70 if(empty($result['id'])){
71 $result = $this->callAPISuccess( 'domain','create',$params );
74 $params['name'] = 'Second Domain';
75 $result = $this->callAPISuccess( 'domain','get',$params);
76 if(empty($result['id'])){
77 $result = $this->callAPISuccess( 'domain','create',$params );
79 $this->_domainID2
= $result['id'];
80 $params['name'] = 'A-team domain';
81 $result = $this->callAPISuccess( 'domain','get',$params);
82 if(empty($result['id'])){
83 $result = $this->callAPISuccess( 'domain','create',$params );
85 $this->_domainID3
= $result['id'];
86 $this->_currentDomain
= CRM_Core_Config
::domainID();
87 $this->hookClass
= CRM_Utils_Hook
::singleton();
91 CRM_Utils_Hook
::singleton()->reset();
93 $this->callAPISuccess('system','flush', array());
94 $this->quickCleanup(array('civicrm_domain'));
98 * Set additional settings into metadata (implements hook)
99 * @param array $metaDataFolders
101 function setExtensionMetadata(&$metaDataFolders) {
102 global $civicrm_root;
103 $metaDataFolders[] = $civicrm_root . '/tests/phpunit/api/v3/settings';
107 * check getfields works
109 function testGetFields() {
110 $description = 'Demonstrate return from getfields - see subfolder for variants';
111 $result = $this->callAPIAndDocument('setting', 'getfields', array(), __FUNCTION__
, __FILE__
, $description);
112 $this->assertArrayHasKey('customCSSURL', $result['values']);
114 $description = 'Demonstrate return from getfields';
115 $result = $this->callAPISuccess('setting', 'getfields', array());
116 $this->assertArrayHasKey('customCSSURL', $result['values']);
117 $this->callAPISuccess('system','flush', array());
121 * let's check it's loading from cache by meddling with the cache
123 function testGetFieldsCaching() {
124 $settingsMetadata = array();
125 CRM_Core_BAO_Cache
::setItem($settingsMetadata,'CiviCRM setting Specs', 'settingsMetadata__');
126 CRM_Core_BAO_Cache
::setItem($settingsMetadata,'CiviCRM setting Spec', 'All');
127 $result = $this->callAPISuccess('setting', 'getfields', array());
128 $this->assertArrayNotHasKey('customCSSURL', $result['values']);
129 $this->quickCleanup(array('civicrm_cache'));
132 function testGetFieldsFilters() {
133 $params = array('name' => 'advanced_search_options');
134 $result = $this->callAPISuccess('setting', 'getfields', $params);
135 $this->assertArrayNotHasKey('customCSSURL', $result['values']);
136 $this->assertArrayHasKey('advanced_search_options',$result['values']);
140 * Test that getfields will filter on group
142 function testGetFieldsGroupFilters() {
143 $params = array('filters' => array('group' => 'multisite'));
144 $result = $this->callAPISuccess('setting', 'getfields', $params);
145 $this->assertArrayNotHasKey('customCSSURL', $result['values']);
146 $this->assertArrayHasKey('domain_group_id',$result['values']);
150 * Test that getfields will filter on another field (prefetch)
152 function testGetFieldsPrefetchFilters() {
153 $params = array('filters' => array('prefetch' => 1));
154 $result = $this->callAPISuccess('setting', 'getfields', $params);
155 $this->assertArrayNotHasKey('disable_mandatory_tokens_check', $result['values']);
156 $this->assertArrayHasKey('monetaryDecimalPoint',$result['values']);
160 * Ensure that on_change callbacks fire.
162 * Note: api_v3_SettingTest::testOnChange and CRM_Core_BAO_SettingTest::testOnChange
163 * are very similar, but they exercise different codepaths. The first uses the API
164 * and setItems [plural]; the second uses setItem [singular].
166 function testOnChange() {
167 global $_testOnChange_hookCalls;
168 $this->setMockSettingsMetaData(array(
169 'onChangeExample' => array(
170 'group_name' => 'CiviCRM Preferences',
172 'name' => 'onChangeExample',
174 'quick_form_type' => 'Element',
175 'html_type' => 'advmultiselect',
176 'default' => array('CiviEvent', 'CiviContribute'),
178 'title' => 'List of Components',
181 'description' => NULL,
183 'on_change' => array( // list of callbacks
184 array(__CLASS__
, '_testOnChange_onChangeExample')
190 $_testOnChange_hookCalls = array('count' => 0);
191 $this->callAPISuccess('setting', 'create', array(
192 'onChangeExample' => array('First', 'Value'),
194 $this->assertEquals(1, $_testOnChange_hookCalls['count']);
195 $this->assertEquals(array('First', 'Value'), $_testOnChange_hookCalls['newValue']);
196 $this->assertEquals('List of Components', $_testOnChange_hookCalls['metadata']['title']);
199 $_testOnChange_hookCalls = array('count' => 0);
200 $this->callAPISuccess('setting', 'create', array(
201 'onChangeExample' => array('Second', 'Value'),
203 $this->assertEquals(1, $_testOnChange_hookCalls['count']);
204 $this->assertEquals(array('First', 'Value'), $_testOnChange_hookCalls['oldValue']);
205 $this->assertEquals(array('Second', 'Value'), $_testOnChange_hookCalls['newValue']);
206 $this->assertEquals('List of Components', $_testOnChange_hookCalls['metadata']['title']);
210 * Mock callback for a setting's on_change handler
216 static function _testOnChange_onChangeExample($oldValue, $newValue, $metadata) {
217 global $_testOnChange_hookCalls;
218 $_testOnChange_hookCalls['count']++
;
219 $_testOnChange_hookCalls['oldValue'] = $oldValue;
220 $_testOnChange_hookCalls['newValue'] = $newValue;
221 $_testOnChange_hookCalls['metadata'] = $metadata;
225 * check getfields works
227 function testCreateSetting() {
228 $description = "shows setting a variable for a given domain - if no domain is set current is assumed";
231 'domain_id' => $this->_domainID2
,
232 'uniq_email_per_site' => 1,
234 $result = $this->callAPIAndDocument('setting', 'create', $params, __FUNCTION__
, __FILE__
);
236 $params = array('uniq_email_per_site' => 1,);
237 $description = "shows setting a variable for a current domain";
238 $result = $this->callAPIAndDocument('setting', 'create', $params, __FUNCTION__
, __FILE__
, $description, 'CreateSettingCurrentDomain');
239 $this->assertArrayHasKey(CRM_Core_Config
::domainID(), $result['values']);
243 * check getfields works
245 function testCreateInvalidSettings() {
248 'domain_id' => $this->_domainID2
,
251 $result = $this->callAPIFailure('setting', 'create', $params);
255 * check invalid settings rejected -
258 function testCreateInvalidURLSettings() {
261 'domain_id' => $this->_domainID2
,
262 'userFrameworkResourceURL' => 'dfhkdhfd',
264 $result = $this->callAPIFailure('setting', 'create', $params);
266 'domain_id' => $this->_domainID2
,
267 'userFrameworkResourceURL' => 'http://blah.com',
269 $result = $this->callAPISuccess('setting', 'create', $params);
273 * check getfields works
275 function testCreateInvalidBooleanSettings() {
278 'domain_id' => $this->_domainID2
,
279 'track_civimail_replies' => 'dfhkdhfd',
281 $result = $this->callAPIFailure('setting', 'create', $params);
283 $params = array('track_civimail_replies' => '0',);
284 $result = $this->callAPISuccess('setting', 'create', $params);
285 $getResult = $this->callAPISuccess('setting','get',$params);
286 $this->assertEquals(0, $getResult['values'][$this->_currentDomain
]['track_civimail_replies']);
288 $getResult = $this->callAPISuccess('setting','get',$params);
289 $this->assertEquals(0, $getResult['values'][$this->_currentDomain
]['track_civimail_replies']);
290 $params = array( 'domain_id' => $this->_domainID2
,
291 'track_civimail_replies' => '1',
293 $result = $this->callAPISuccess('setting', 'create', $params);
294 $getResult = $this->callAPISuccess('setting','get',$params);
295 $this->assertEquals(1, $getResult['values'][$this->_domainID2
]['track_civimail_replies']);
298 'domain_id' => $this->_domainID2
,
299 'track_civimail_replies' => 'TRUE',
301 $result = $this->callAPISuccess('setting', 'create', $params);
302 $getResult = $this->callAPISuccess('setting','get',$params);
304 $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() {
361 'domain_id' => $this->_domainID2
,
362 'return' => 'uniq_email_per_site',
364 $description = "shows get setting a variable for a given domain - if no domain is set current is assumed";
366 $result = $this->callAPIAndDocument('setting', 'get', $params, __FUNCTION__
, __FILE__
);
369 'return' => 'uniq_email_per_site',
371 $description = "shows getting a variable for a current domain";
372 $result = $this->callAPIAndDocument('setting', 'get', $params, __FUNCTION__
, __FILE__
, $description, 'GetSettingCurrentDomain');
373 $this->assertArrayHasKey(CRM_Core_Config
::domainID(), $result['values']);
377 * Check that setting defined in extension can be retrieved
379 function testGetExtensionSetting() {
380 $this->hookClass
->setHook('civicrm_alterSettingsFolders', array($this, 'setExtensionMetadata'));
382 // the caching of data to all duplicates the caching of data to the empty string
383 CRM_Core_BAO_Cache
::setItem($data, 'CiviCRM setting Spec', 'All');
384 CRM_Core_BAO_Cache
::setItem($data, 'CiviCRM setting Specs', 'settingsMetadata__');
385 $fields = $this->callAPISuccess('setting', 'getfields', array('filters' => array('group_name' => 'Test Settings')));
386 $this->assertArrayHasKey('test_key', $fields['values']);
387 $this->callAPISuccess('setting', 'create', array('test_key' => 'keyset'));
388 $result = $this->callAPISuccess('setting', 'getvalue', array('name' => 'test_key', 'group' => 'Test Settings'));
389 $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 '%debug%'";
408 $dao = CRM_Core_DAO
::executeQuery($sql);
410 $this->assertEquals($dao->c
, 0);
413 * setting api should set & fetch settings stored in config as well as those in settings table
415 function testGetConfigSetting() {
416 $settings = $this->callAPISuccess('setting', 'get', array(
417 'name' => 'defaultCurrency', 'sequential' => 1,)
419 $this->assertEquals('USD', $settings['values'][0]['defaultCurrency']);
423 * setting api should set & fetch settings stored in config as well as those in settings table
425 function testGetSetConfigSettingMultipleDomains() {
426 $settings = $this->callAPISuccess('setting', 'create', array(
427 'defaultCurrency' => 'USD', 'domain_id' => $this->_currentDomain
)
429 $settings = $this->callAPISuccess('setting', 'create', array(
430 'defaultCurrency' => 'CAD', 'domain_id' => $this->_domainID2
)
432 $settings = $this->callAPISuccess('setting', 'get', array(
433 'return' => 'defaultCurrency', 'domain_id' => 'all',
436 $this->assertEquals('USD', $settings['values'][$this->_currentDomain
]['defaultCurrency']);
437 $this->assertEquals('CAD', $settings['values'][$this->_domainID2
]['defaultCurrency'],
438 "second domain (id {$this->_domainID2} ) should be set to CAD. First dom was {$this->_currentDomain} & was USD");
443 * Use getValue against a config setting
445 function testGetValueConfigSetting() {
446 $params = array( 'name' => 'monetaryThousandSeparator',
447 'group' => 'Localization Setting',
449 $result = $this->callAPISuccess('setting', 'getvalue', $params);
450 $this->assertEquals(',', $result);
453 function testGetValue() {
454 $params = array( 'name' => 'petition_contacts',
455 'group' => 'Campaign Preferences'
457 $description = "Demonstrates getvalue action - intended for runtime use as better caching than get";
459 $result = $this->callAPIAndDocument('setting', 'getvalue', $params, __FUNCTION__
, __FILE__
, $description);
460 $this->assertEquals('Petition Contacts', $result);
463 function testGetDefaults() {
464 $description = "gets defaults setting a variable for a given domain - if no domain is set current is assumed";
467 'name' => 'address_format',
469 $result = $this->callAPIAndDocument('setting', 'getdefaults', $params, __FUNCTION__
, __FILE__
,$description,'GetDefaults','getdefaults');
470 $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']);
471 $params = array('name' => 'mailing_format',);
472 $result = $this->callAPISuccess('setting', 'getdefaults', $params);
473 $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']);
474 $this->assertArrayHasKey(CRM_Core_Config
::domainID(), $result['values']);
477 * Function tests reverting a specific parameter
479 function testRevert() {
481 $params = array( 'address_format' => 'xyz',
482 'mailing_format' => 'bcs',
484 $result = $this->callAPISuccess('setting', 'create', $params);
485 $this->assertAPISuccess($result, "in line " . __LINE__
);
486 $revertParams = array( 'name' => 'address_format'
488 $result = $this->callAPISuccess('setting', 'get', $params);
490 $this->assertEquals('xyz', $result['values'][CRM_Core_Config
::domainID()]['address_format']);
491 $description = "Demonstrates reverting a parameter to default value";
492 $result = $this->callAPIAndDocument('setting', 'revert', $revertParams, __FUNCTION__
, __FILE__
,$description,'','revert');
493 //make sure it's reverted
494 $result = $this->callAPISuccess('setting', 'get', $params);
495 $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']);
496 $params = array( 'return' => array('mailing_format'),
498 $result = $this->callAPISuccess('setting', 'get', $params);
499 //make sure it's unchanged
500 $this->assertEquals('bcs', $result['values'][CRM_Core_Config
::domainID()]['mailing_format']);
503 * Tests reverting ALL parameters (specific domain)
505 function testRevertAll() {
507 $params = array( 'address_format' => 'xyz',
508 'mailing_format' => 'bcs',
510 $result = $this->callAPISuccess('setting', 'create', $params);
511 $revertParams = array( );
512 $result = $this->callAPISuccess('setting', 'get', $params);
514 $this->assertEquals('xyz', $result['values'][CRM_Core_Config
::domainID()]['address_format']);
516 $this->callAPISuccess('setting', 'revert', $revertParams);
517 //make sure it's reverted
518 $result = $this->callAPISuccess('setting', 'get', array('group' => 'core'));
519 $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']);
520 $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']);
524 * Tests filling missing params
526 function testFill() {
528 'name' => 'B Team Domain',
530 $dom = $this->callAPISuccess('domain', 'create', $domparams);
531 $params = array( 'domain_id' => 'all',
533 $result = $this->callAPISuccess('setting', 'get', $params);
534 $params = array( 'address_format' => 'xyz',
535 'mailing_format' => 'bcs',
536 'domain_id' => $this->_domainID2
,
538 $result = $this->callAPISuccess('setting', 'create', $params);
539 $params = array( 'domain_id' => $dom['id'],
541 $result = $this->callAPISuccess('setting', 'get', $params);
542 $this->assertAPISuccess($result, "in line " . __LINE__
);
543 $this->assertArrayNotHasKey('tag_unconfirmed', $result['values'][$dom['id']],'setting for domain 3 should not be set. Debug this IF domain test is passing');
544 $result = $this->callAPISuccess('setting', 'fill', $params);
545 $this->assertAPISuccess($result, "in line " . __LINE__
);
546 $result = $this->callAPISuccess('setting', 'get', $params);
547 $this->assertAPISuccess($result, "in line " . __LINE__
);
548 $this->assertArrayHasKey('tag_unconfirmed', $result['values'][$dom['id']]);
549 $this->assertArrayHasKey('extensionsDir', $result['values'][$dom['id']]);
550 $this->assertEquals('Unconfirmed', $result['values'][$dom['id']]['tag_unconfirmed']);