3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
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();
62 'name' => 'Default Domain Name',
64 $result = $this->callAPISuccess( 'domain','get',$params);
65 if(empty($result['id'])){
66 $result = $this->callAPISuccess( 'domain','create',$params );
69 $params['name'] = 'Second Domain';
70 $result = $this->callAPISuccess( 'domain','get',$params);
71 if(empty($result['id'])){
72 $result = $this->callAPISuccess( 'domain','create',$params );
74 $this->_domainID2
= $result['id'];
75 $params['name'] = 'A-team domain';
76 $result = $this->callAPISuccess( 'domain','get',$params);
77 if(empty($result['id'])){
78 $result = $this->callAPISuccess( 'domain','create',$params );
80 $this->_domainID3
= $result['id'];
81 $this->_currentDomain
= CRM_Core_Config
::domainID();
82 $this->hookClass
= CRM_Utils_Hook
::singleton();
86 CRM_Utils_Hook
::singleton()->reset();
88 $this->callAPISuccess('system','flush', array());
89 $this->quickCleanup(array('civicrm_domain'));
93 * Set additional settings into metadata (implements hook)
94 * @param array $metaDataFolders
96 function setExtensionMetadata(&$metaDataFolders) {
98 $metaDataFolders[] = $civicrm_root . '/tests/phpunit/api/v3/settings';
102 * Check getfields works
104 function testGetFields() {
105 $description = 'Demonstrate return from getfields - see subfolder for variants';
106 $result = $this->callAPIAndDocument('setting', 'getfields', array(), __FUNCTION__
, __FILE__
, $description);
107 $this->assertArrayHasKey('customCSSURL', $result['values']);
109 $description = 'Demonstrate return from getfields';
110 $result = $this->callAPISuccess('setting', 'getfields', array());
111 $this->assertArrayHasKey('customCSSURL', $result['values']);
112 $this->callAPISuccess('system','flush', array());
116 * Let's check it's loading from cache by meddling with the cache
118 function testGetFieldsCaching() {
119 $settingsMetadata = array();
120 CRM_Core_BAO_Cache
::setItem($settingsMetadata,'CiviCRM setting Specs', 'settingsMetadata__');
121 CRM_Core_BAO_Cache
::setItem($settingsMetadata,'CiviCRM setting Spec', 'All');
122 $result = $this->callAPISuccess('setting', 'getfields', array());
123 $this->assertArrayNotHasKey('customCSSURL', $result['values']);
124 $this->quickCleanup(array('civicrm_cache'));
127 function testGetFieldsFilters() {
128 $params = array('name' => 'advanced_search_options');
129 $result = $this->callAPISuccess('setting', 'getfields', $params);
130 $this->assertArrayNotHasKey('customCSSURL', $result['values']);
131 $this->assertArrayHasKey('advanced_search_options',$result['values']);
135 * Test that getfields will filter on group
137 function testGetFieldsGroupFilters() {
138 $params = array('filters' => array('group' => 'multisite'));
139 $result = $this->callAPISuccess('setting', 'getfields', $params);
140 $this->assertArrayNotHasKey('customCSSURL', $result['values']);
141 $this->assertArrayHasKey('domain_group_id',$result['values']);
145 * Test that getfields will filter on another field (prefetch)
147 function testGetFieldsPrefetchFilters() {
148 $params = array('filters' => array('prefetch' => 1));
149 $result = $this->callAPISuccess('setting', 'getfields', $params);
150 $this->assertArrayNotHasKey('disable_mandatory_tokens_check', $result['values']);
151 $this->assertArrayHasKey('monetaryDecimalPoint',$result['values']);
155 * Ensure that on_change callbacks fire.
157 * Note: api_v3_SettingTest::testOnChange and CRM_Core_BAO_SettingTest::testOnChange
158 * are very similar, but they exercise different codepaths. The first uses the API
159 * and setItems [plural]; the second uses setItem [singular].
161 function testOnChange() {
162 global $_testOnChange_hookCalls;
163 $this->setMockSettingsMetaData(array(
164 'onChangeExample' => array(
165 'group_name' => 'CiviCRM Preferences',
167 'name' => 'onChangeExample',
169 'quick_form_type' => 'Element',
170 'html_type' => 'advmultiselect',
171 'default' => array('CiviEvent', 'CiviContribute'),
173 'title' => 'List of Components',
176 'description' => NULL,
178 'on_change' => array( // list of callbacks
179 array(__CLASS__
, '_testOnChange_onChangeExample')
185 $_testOnChange_hookCalls = array('count' => 0);
186 $this->callAPISuccess('setting', 'create', array(
187 'onChangeExample' => array('First', 'Value'),
189 $this->assertEquals(1, $_testOnChange_hookCalls['count']);
190 $this->assertEquals(array('First', 'Value'), $_testOnChange_hookCalls['newValue']);
191 $this->assertEquals('List of Components', $_testOnChange_hookCalls['metadata']['title']);
194 $_testOnChange_hookCalls = array('count' => 0);
195 $this->callAPISuccess('setting', 'create', array(
196 'onChangeExample' => array('Second', 'Value'),
198 $this->assertEquals(1, $_testOnChange_hookCalls['count']);
199 $this->assertEquals(array('First', 'Value'), $_testOnChange_hookCalls['oldValue']);
200 $this->assertEquals(array('Second', 'Value'), $_testOnChange_hookCalls['newValue']);
201 $this->assertEquals('List of Components', $_testOnChange_hookCalls['metadata']['title']);
205 * Mock callback for a setting's on_change handler
211 static function _testOnChange_onChangeExample($oldValue, $newValue, $metadata) {
212 global $_testOnChange_hookCalls;
213 $_testOnChange_hookCalls['count']++
;
214 $_testOnChange_hookCalls['oldValue'] = $oldValue;
215 $_testOnChange_hookCalls['newValue'] = $newValue;
216 $_testOnChange_hookCalls['metadata'] = $metadata;
220 * Check getfields works
222 function testCreateSetting() {
223 $description = "shows setting a variable for a given domain - if no domain is set current is assumed";
226 'domain_id' => $this->_domainID2
,
227 'uniq_email_per_site' => 1,
229 $result = $this->callAPIAndDocument('setting', 'create', $params, __FUNCTION__
, __FILE__
);
231 $params = array('uniq_email_per_site' => 1,);
232 $description = "shows setting a variable for a current domain";
233 $result = $this->callAPIAndDocument('setting', 'create', $params, __FUNCTION__
, __FILE__
, $description, 'CreateSettingCurrentDomain');
234 $this->assertArrayHasKey(CRM_Core_Config
::domainID(), $result['values']);
238 * Check getfields works
240 function testCreateInvalidSettings() {
242 'domain_id' => $this->_domainID2
,
245 $result = $this->callAPIFailure('setting', 'create', $params);
249 * Check invalid settings rejected -
251 function testCreateInvalidURLSettings() {
253 'domain_id' => $this->_domainID2
,
254 'userFrameworkResourceURL' => 'dfhkdhfd',
256 $result = $this->callAPIFailure('setting', 'create', $params);
258 'domain_id' => $this->_domainID2
,
259 'userFrameworkResourceURL' => 'http://blah.com',
261 $result = $this->callAPISuccess('setting', 'create', $params);
265 * Check getfields works
267 function testCreateInvalidBooleanSettings() {
269 'domain_id' => $this->_domainID2
,
270 'track_civimail_replies' => 'dfhkdhfd',
272 $result = $this->callAPIFailure('setting', 'create', $params);
274 $params = array('track_civimail_replies' => '0',);
275 $result = $this->callAPISuccess('setting', 'create', $params);
276 $getResult = $this->callAPISuccess('setting','get',$params);
277 $this->assertEquals(0, $getResult['values'][$this->_currentDomain
]['track_civimail_replies']);
279 $getResult = $this->callAPISuccess('setting','get',$params);
280 $this->assertEquals(0, $getResult['values'][$this->_currentDomain
]['track_civimail_replies']);
281 $params = array( 'domain_id' => $this->_domainID2
,
282 'track_civimail_replies' => '1',
284 $result = $this->callAPISuccess('setting', 'create', $params);
285 $getResult = $this->callAPISuccess('setting','get',$params);
286 $this->assertEquals(1, $getResult['values'][$this->_domainID2
]['track_civimail_replies']);
289 'domain_id' => $this->_domainID2
,
290 'track_civimail_replies' => 'TRUE',
292 $result = $this->callAPISuccess('setting', 'create', $params);
293 $getResult = $this->callAPISuccess('setting','get',$params);
295 $this->assertEquals(1, $getResult['values'][$this->_domainID2
]['track_civimail_replies'], "check TRUE is converted to 1");
299 * Check getfields works
301 function testCreateSettingMultipleDomains() {
302 $description = "shows setting a variable for all domains";
305 'domain_id' => 'all',
306 'uniq_email_per_site' => 1,
308 $result = $this->callAPIAndDocument('setting', 'create', $params, __FUNCTION__
, __FILE__
,$description, 'CreateAllDomains');
310 $this->assertEquals(1, $result['values'][2]['uniq_email_per_site']);
311 $this->assertEquals(1, $result['values'][1]['uniq_email_per_site']);
312 $this->assertArrayHasKey(3, $result['values'], 'Domain create probably failed Debug this IF domain test is passing');
313 $this->assertEquals(1, $result['values'][3]['uniq_email_per_site'], 'failed to set setting for domain 3.');
316 'domain_id' => 'all',
317 'return' => 'uniq_email_per_site'
319 // we'll check it with a 'get'
320 $description = "shows getting a variable for all domains";
321 $result = $this->callAPIAndDocument('setting', 'get', $params, __FUNCTION__
, __FILE__
,$description, 'GetAllDomains', 'Get');
323 $this->assertEquals(1, $result['values'][2]['uniq_email_per_site']);
324 $this->assertEquals(1, $result['values'][1]['uniq_email_per_site']);
325 $this->assertEquals(1, $result['values'][3]['uniq_email_per_site']);
328 'domain_id' => array(1,3),
329 'uniq_email_per_site' => 0,
331 $description = "shows setting a variable for specified domains";
332 $result = $this->callAPIAndDocument('setting', 'create', $params, __FUNCTION__
, __FILE__
,$description, 'CreateSpecifiedDomains');
334 $this->assertEquals(0, $result['values'][3]['uniq_email_per_site']);
335 $this->assertEquals(0, $result['values'][1]['uniq_email_per_site']);
337 'domain_id' => array(1,2),
338 'return' => array('uniq_email_per_site'),
340 $description = "shows getting a variable for specified domains";
341 $result = $this->callAPIAndDocument('setting', 'get', $params, __FUNCTION__
, __FILE__
,$description, 'GetSpecifiedDomains', 'Get');
342 $this->assertEquals(1, $result['values'][2]['uniq_email_per_site']);
343 $this->assertEquals(0, $result['values'][1]['uniq_email_per_site']);
347 function testGetSetting() {
349 'domain_id' => $this->_domainID2
,
350 'return' => 'uniq_email_per_site',
352 $description = "shows get setting a variable for a given domain - if no domain is set current is assumed";
354 $result = $this->callAPIAndDocument('setting', 'get', $params, __FUNCTION__
, __FILE__
);
357 'return' => 'uniq_email_per_site',
359 $description = "shows getting a variable for a current domain";
360 $result = $this->callAPIAndDocument('setting', 'get', $params, __FUNCTION__
, __FILE__
, $description, 'GetSettingCurrentDomain');
361 $this->assertArrayHasKey(CRM_Core_Config
::domainID(), $result['values']);
365 * Check that setting defined in extension can be retrieved
367 function testGetExtensionSetting() {
368 $this->hookClass
->setHook('civicrm_alterSettingsFolders', array($this, 'setExtensionMetadata'));
370 // the caching of data to all duplicates the caching of data to the empty string
371 CRM_Core_BAO_Cache
::setItem($data, 'CiviCRM setting Spec', 'All');
372 CRM_Core_BAO_Cache
::setItem($data, 'CiviCRM setting Specs', 'settingsMetadata__');
373 $fields = $this->callAPISuccess('setting', 'getfields', array('filters' => array('group_name' => 'Test Settings')));
374 $this->assertArrayHasKey('test_key', $fields['values']);
375 $this->callAPISuccess('setting', 'create', array('test_key' => 'keyset'));
376 $result = $this->callAPISuccess('setting', 'getvalue', array('name' => 'test_key', 'group' => 'Test Settings'));
377 $this->assertEquals('keyset', $result);
381 * Setting api should set & fetch settings stored in config as well as those in settings table
383 function testSetConfigSetting() {
384 $config = CRM_Core_Config
::singleton();
385 $this->assertFalse($config->debug
== 1);
387 'domain_id' => $this->_domainID2
,
388 'debug_enabled' => 1,
390 $result = $this->callAPISuccess('setting', 'create', $params);
391 CRM_Core_BAO_Domain
::setDomain($this->_domainID2
);
392 $config = CRM_Core_Config
::singleton(TRUE, TRUE);
393 CRM_Core_BAO_Domain
::resetDomain();
394 $this->assertTrue($config->debug
== 1);
395 // this should NOT be stored in the settings table now - only in config
396 $sql = " SELECT count(*) as c FROM civicrm_setting WHERE name LIKE '%maxFileSize%'";
397 $dao = CRM_Core_DAO
::executeQuery($sql);
399 $this->assertEquals($dao->c
, 0);
403 * Setting api should set & fetch settings stored in config as well as those in settings table
405 function testGetConfigSetting() {
406 $settings = $this->callAPISuccess('setting', 'get', array(
407 'name' => 'defaultCurrency', 'sequential' => 1,)
409 $this->assertEquals('USD', $settings['values'][0]['defaultCurrency']);
413 * Setting api should set & fetch settings stored in config as well as those in settings table
415 function testGetSetConfigSettingMultipleDomains() {
416 $settings = $this->callAPISuccess('setting', 'create', array(
417 'defaultCurrency' => 'USD', 'domain_id' => $this->_currentDomain
)
419 $settings = $this->callAPISuccess('setting', 'create', array(
420 'defaultCurrency' => 'CAD', 'domain_id' => $this->_domainID2
)
422 $settings = $this->callAPISuccess('setting', 'get', array(
423 'return' => 'defaultCurrency', 'domain_id' => 'all',
426 $this->assertEquals('USD', $settings['values'][$this->_currentDomain
]['defaultCurrency']);
427 $this->assertEquals('CAD', $settings['values'][$this->_domainID2
]['defaultCurrency'],
428 "second domain (id {$this->_domainID2} ) should be set to CAD. First dom was {$this->_currentDomain} & was USD");
433 * Use getValue against a config setting
435 function testGetValueConfigSetting() {
436 $params = array( 'name' => 'monetaryThousandSeparator',
437 'group' => 'Localization Setting',
439 $result = $this->callAPISuccess('setting', 'getvalue', $params);
440 $this->assertEquals(',', $result);
443 function testGetValue() {
444 $params = array( 'name' => 'petition_contacts',
445 'group' => 'Campaign Preferences'
447 $description = "Demonstrates getvalue action - intended for runtime use as better caching than get";
449 $result = $this->callAPIAndDocument('setting', 'getvalue', $params, __FUNCTION__
, __FILE__
, $description);
450 $this->assertEquals('Petition Contacts', $result);
453 function testGetDefaults() {
454 $description = "gets defaults setting a variable for a given domain - if no domain is set current is assumed";
457 'name' => 'address_format',
459 $result = $this->callAPIAndDocument('setting', 'getdefaults', $params, __FUNCTION__
, __FILE__
,$description,'GetDefaults','getdefaults');
460 $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']);
461 $params = array('name' => 'mailing_format',);
462 $result = $this->callAPISuccess('setting', 'getdefaults', $params);
463 $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']);
464 $this->assertArrayHasKey(CRM_Core_Config
::domainID(), $result['values']);
468 * Function tests reverting a specific parameter
470 function testRevert() {
471 $params = array( 'address_format' => 'xyz',
472 'mailing_format' => 'bcs',
474 $result = $this->callAPISuccess('setting', 'create', $params);
475 $this->assertAPISuccess($result, "in line " . __LINE__
);
476 $revertParams = array( 'name' => 'address_format'
478 $result = $this->callAPISuccess('setting', 'get', $params);
480 $this->assertEquals('xyz', $result['values'][CRM_Core_Config
::domainID()]['address_format']);
481 $description = "Demonstrates reverting a parameter to default value";
482 $result = $this->callAPIAndDocument('setting', 'revert', $revertParams, __FUNCTION__
, __FILE__
,$description,'','revert');
483 //make sure it's reverted
484 $result = $this->callAPISuccess('setting', 'get', $params);
485 $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']);
486 $params = array( 'return' => array('mailing_format'),
488 $result = $this->callAPISuccess('setting', 'get', $params);
489 //make sure it's unchanged
490 $this->assertEquals('bcs', $result['values'][CRM_Core_Config
::domainID()]['mailing_format']);
494 * Tests reverting ALL parameters (specific domain)
496 function testRevertAll() {
497 $params = array( '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 * Tests filling missing params
516 function testFill() {
518 'name' => 'B Team Domain',
520 $dom = $this->callAPISuccess('domain', 'create', $domparams);
521 $params = array( 'domain_id' => 'all',
523 $result = $this->callAPISuccess('setting', 'get', $params);
524 $params = array( 'address_format' => 'xyz',
525 'mailing_format' => 'bcs',
526 'domain_id' => $this->_domainID2
,
528 $result = $this->callAPISuccess('setting', 'create', $params);
529 $params = array( 'domain_id' => $dom['id'],
531 $result = $this->callAPISuccess('setting', 'get', $params);
532 $this->assertAPISuccess($result, "in line " . __LINE__
);
533 $this->assertArrayNotHasKey('tag_unconfirmed', $result['values'][$dom['id']],'setting for domain 3 should not be set. Debug this IF domain test is passing');
534 $result = $this->callAPISuccess('setting', 'fill', $params);
535 $this->assertAPISuccess($result, "in line " . __LINE__
);
536 $result = $this->callAPISuccess('setting', 'get', $params);
537 $this->assertAPISuccess($result, "in line " . __LINE__
);
538 $this->assertArrayHasKey('tag_unconfirmed', $result['values'][$dom['id']]);
539 $this->assertArrayHasKey('extensionsDir', $result['values'][$dom['id']]);
540 $this->assertEquals('Unconfirmed', $result['values'][$dom['id']]['tag_unconfirmed']);