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();
91 $this->callAPISuccess('system','flush', array());
92 $this->quickCleanup(array('civicrm_domain'));
96 * check getfields works
98 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 function testGetFieldsCaching() {
113 $settingsMetadata = array();
114 CRM_Core_BAO_Cache
::setItem($settingsMetadata,'CiviCRM setting Specs', 'settingsMetadata__');
115 CRM_Core_BAO_Cache
::setItem($settingsMetadata,'CiviCRM setting Spec', 'All');
116 $result = $this->callAPISuccess('setting', 'getfields', array());
117 $this->assertArrayNotHasKey('customCSSURL', $result['values']);
118 $this->quickCleanup(array('civicrm_cache'));
121 function testGetFieldsFilters() {
122 $params = array('name' => 'advanced_search_options');
123 $result = $this->callAPISuccess('setting', 'getfields', $params);
124 $this->assertArrayNotHasKey('customCSSURL', $result['values']);
125 $this->assertArrayHasKey('advanced_search_options',$result['values']);
129 * Test that getfields will filter on group
131 function testGetFieldsGroupFilters() {
132 $params = array('filters' => array('group' => 'multisite'));
133 $result = $this->callAPISuccess('setting', 'getfields', $params);
134 $this->assertArrayNotHasKey('customCSSURL', $result['values']);
135 $this->assertArrayHasKey('domain_group_id',$result['values']);
139 * Test that getfields will filter on another field (prefetch)
141 function testGetFieldsPrefetchFilters() {
142 $params = array('filters' => array('prefetch' => 1));
143 $result = $this->callAPISuccess('setting', 'getfields', $params);
144 $this->assertArrayNotHasKey('disable_mandatory_tokens_check', $result['values']);
145 $this->assertArrayHasKey('monetaryDecimalPoint',$result['values']);
149 * Ensure that on_change callbacks fire.
151 * Note: api_v3_SettingTest::testOnChange and CRM_Core_BAO_SettingTest::testOnChange
152 * are very similar, but they exercise different codepaths. The first uses the API
153 * and setItems [plural]; the second uses setItem [singular].
155 function testOnChange() {
156 global $_testOnChange_hookCalls;
157 $this->setMockSettingsMetaData(array(
158 'onChangeExample' => array(
159 'group_name' => 'CiviCRM Preferences',
161 'name' => 'onChangeExample',
163 'quick_form_type' => 'Element',
164 'html_type' => 'advmultiselect',
165 'default' => array('CiviEvent', 'CiviContribute'),
167 'title' => 'List of Components',
170 'description' => NULL,
172 'on_change' => array( // list of callbacks
173 array(__CLASS__
, '_testOnChange_onChangeExample')
179 $_testOnChange_hookCalls = array('count' => 0);
180 $this->callAPISuccess('setting', 'create', array(
181 'onChangeExample' => array('First', 'Value'),
183 $this->assertEquals(1, $_testOnChange_hookCalls['count']);
184 $this->assertEquals(array('First', 'Value'), $_testOnChange_hookCalls['newValue']);
185 $this->assertEquals('List of Components', $_testOnChange_hookCalls['metadata']['title']);
188 $_testOnChange_hookCalls = array('count' => 0);
189 $this->callAPISuccess('setting', 'create', array(
190 'onChangeExample' => array('Second', 'Value'),
192 $this->assertEquals(1, $_testOnChange_hookCalls['count']);
193 $this->assertEquals(array('First', 'Value'), $_testOnChange_hookCalls['oldValue']);
194 $this->assertEquals(array('Second', 'Value'), $_testOnChange_hookCalls['newValue']);
195 $this->assertEquals('List of Components', $_testOnChange_hookCalls['metadata']['title']);
199 * Mock callback for a setting's on_change handler
205 static function _testOnChange_onChangeExample($oldValue, $newValue, $metadata) {
206 global $_testOnChange_hookCalls;
207 $_testOnChange_hookCalls['count']++
;
208 $_testOnChange_hookCalls['oldValue'] = $oldValue;
209 $_testOnChange_hookCalls['newValue'] = $newValue;
210 $_testOnChange_hookCalls['metadata'] = $metadata;
214 * check getfields works
216 function testCreateSetting() {
217 $description = "shows setting a variable for a given domain - if no domain is set current is assumed";
220 'domain_id' => $this->_domainID2
,
221 'uniq_email_per_site' => 1,
223 $result = $this->callAPIAndDocument('setting', 'create', $params, __FUNCTION__
, __FILE__
);
225 $params = array('uniq_email_per_site' => 1,);
226 $description = "shows setting a variable for a current domain";
227 $result = $this->callAPIAndDocument('setting', 'create', $params, __FUNCTION__
, __FILE__
, $description, 'CreateSettingCurrentDomain');
228 $this->assertArrayHasKey(CRM_Core_Config
::domainID(), $result['values']);
232 * check getfields works
234 function testCreateInvalidSettings() {
237 'domain_id' => $this->_domainID2
,
240 $result = $this->callAPIFailure('setting', 'create', $params);
244 * check invalid settings rejected -
247 function testCreateInvalidURLSettings() {
250 'domain_id' => $this->_domainID2
,
251 'userFrameworkResourceURL' => 'dfhkdhfd',
253 $result = $this->callAPIFailure('setting', 'create', $params);
255 'domain_id' => $this->_domainID2
,
256 'userFrameworkResourceURL' => 'http://blah.com',
258 $result = $this->callAPISuccess('setting', 'create', $params);
262 * check getfields works
264 function testCreateInvalidBooleanSettings() {
267 'domain_id' => $this->_domainID2
,
268 'track_civimail_replies' => 'dfhkdhfd',
270 $result = $this->callAPIFailure('setting', 'create', $params);
272 $params = array('track_civimail_replies' => '0',);
273 $result = $this->callAPISuccess('setting', 'create', $params);
274 $getResult = $this->callAPISuccess('setting','get',$params);
275 $this->assertEquals(0, $getResult['values'][$this->_currentDomain
]['track_civimail_replies']);
277 $getResult = $this->callAPISuccess('setting','get',$params);
278 $this->assertEquals(0, $getResult['values'][$this->_currentDomain
]['track_civimail_replies']);
279 $params = array( 'domain_id' => $this->_domainID2
,
280 'track_civimail_replies' => '1',
282 $result = $this->callAPISuccess('setting', 'create', $params);
283 $getResult = $this->callAPISuccess('setting','get',$params);
284 $this->assertEquals(1, $getResult['values'][$this->_domainID2
]['track_civimail_replies']);
287 'domain_id' => $this->_domainID2
,
288 'track_civimail_replies' => 'TRUE',
290 $result = $this->callAPISuccess('setting', 'create', $params);
291 $getResult = $this->callAPISuccess('setting','get',$params);
293 $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() {
350 'domain_id' => $this->_domainID2
,
351 'return' => 'uniq_email_per_site',
353 $description = "shows get setting a variable for a given domain - if no domain is set current is assumed";
355 $result = $this->callAPIAndDocument('setting', 'get', $params, __FUNCTION__
, __FILE__
);
358 'return' => 'uniq_email_per_site',
360 $description = "shows getting a variable for a current domain";
361 $result = $this->callAPIAndDocument('setting', 'get', $params, __FUNCTION__
, __FILE__
, $description, 'GetSettingCurrentDomain');
362 $this->assertArrayHasKey(CRM_Core_Config
::domainID(), $result['values']);
365 * setting api should set & fetch settings stored in config as well as those in settings table
367 function testSetConfigSetting() {
368 $config = CRM_Core_Config
::singleton();
369 $this->assertFalse($config->debug
== 1);
371 'domain_id' => $this->_domainID2
,
372 'debug_enabled' => 1,
374 $result = $this->callAPISuccess('setting', 'create', $params);
375 CRM_Core_BAO_Domain
::setDomain($this->_domainID2
);
376 $config = CRM_Core_Config
::singleton(TRUE, TRUE);
377 CRM_Core_BAO_Domain
::resetDomain();
378 $this->assertTrue($config->debug
== 1);
379 // this should NOT be stored in the settings table now - only in config
380 $sql = " SELECT count(*) as c FROM civicrm_setting WHERE name LIKE '%debug%'";
381 $dao = CRM_Core_DAO
::executeQuery($sql);
383 $this->assertEquals($dao->c
, 0);
386 * setting api should set & fetch settings stored in config as well as those in settings table
388 function testGetConfigSetting() {
389 $settings = $this->callAPISuccess('setting', 'get', array(
390 'name' => 'defaultCurrency', 'sequential' => 1,)
392 $this->assertEquals('USD', $settings['values'][0]['defaultCurrency']);
396 * setting api should set & fetch settings stored in config as well as those in settings table
398 function testGetSetConfigSettingMultipleDomains() {
399 $settings = $this->callAPISuccess('setting', 'create', array(
400 'defaultCurrency' => 'USD', 'domain_id' => $this->_currentDomain
)
402 $settings = $this->callAPISuccess('setting', 'create', array(
403 'defaultCurrency' => 'CAD', 'domain_id' => $this->_domainID2
)
405 $settings = $this->callAPISuccess('setting', 'get', array(
406 'return' => 'defaultCurrency', 'domain_id' => 'all',
409 $this->assertEquals('USD', $settings['values'][$this->_currentDomain
]['defaultCurrency']);
410 $this->assertEquals('CAD', $settings['values'][$this->_domainID2
]['defaultCurrency'],
411 "second domain (id {$this->_domainID2} ) should be set to CAD. First dom was {$this->_currentDomain} & was USD");
416 * Use getValue against a config setting
418 function testGetValueConfigSetting() {
419 $params = array( 'name' => 'monetaryThousandSeparator',
420 'group' => 'Localization Setting',
422 $result = $this->callAPISuccess('setting', 'getvalue', $params);
423 $this->assertEquals(',', $result);
426 function testGetValue() {
427 $params = array( 'name' => 'petition_contacts',
428 'group' => 'Campaign Preferences'
430 $description = "Demonstrates getvalue action - intended for runtime use as better caching than get";
432 $result = $this->callAPIAndDocument('setting', 'getvalue', $params, __FUNCTION__
, __FILE__
, $description);
433 $this->assertEquals('Petition Contacts', $result);
436 function testGetDefaults() {
437 $description = "gets defaults setting a variable for a given domain - if no domain is set current is assumed";
440 'name' => 'address_format',
442 $result = $this->callAPIAndDocument('setting', 'getdefaults', $params, __FUNCTION__
, __FILE__
,$description,'GetDefaults','getdefaults');
443 $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']);
444 $params = array('name' => 'mailing_format',);
445 $result = $this->callAPISuccess('setting', 'getdefaults', $params);
446 $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']);
447 $this->assertArrayHasKey(CRM_Core_Config
::domainID(), $result['values']);
450 * Function tests reverting a specific parameter
452 function testRevert() {
454 $params = array( 'address_format' => 'xyz',
455 'mailing_format' => 'bcs',
457 $result = $this->callAPISuccess('setting', 'create', $params);
458 $this->assertAPISuccess($result, "in line " . __LINE__
);
459 $revertParams = array( 'name' => 'address_format'
461 $result = $this->callAPISuccess('setting', 'get', $params);
463 $this->assertEquals('xyz', $result['values'][CRM_Core_Config
::domainID()]['address_format']);
464 $description = "Demonstrates reverting a parameter to default value";
465 $result = $this->callAPIAndDocument('setting', 'revert', $revertParams, __FUNCTION__
, __FILE__
,$description,'','revert');
466 //make sure it's reverted
467 $result = $this->callAPISuccess('setting', 'get', $params);
468 $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']);
469 $params = array( 'return' => array('mailing_format'),
471 $result = $this->callAPISuccess('setting', 'get', $params);
472 //make sure it's unchanged
473 $this->assertEquals('bcs', $result['values'][CRM_Core_Config
::domainID()]['mailing_format']);
476 * Tests reverting ALL parameters (specific domain)
478 function testRevertAll() {
480 $params = array( 'address_format' => 'xyz',
481 'mailing_format' => 'bcs',
483 $result = $this->callAPISuccess('setting', 'create', $params);
484 $revertParams = array( );
485 $result = $this->callAPISuccess('setting', 'get', $params);
487 $this->assertEquals('xyz', $result['values'][CRM_Core_Config
::domainID()]['address_format']);
489 $this->callAPISuccess('setting', 'revert', $revertParams);
490 //make sure it's reverted
491 $result = $this->callAPISuccess('setting', 'get', array('group' => 'core'));
492 $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']);
493 $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']);
497 * Tests filling missing params
499 function testFill() {
501 'name' => 'B Team Domain',
503 $dom = $this->callAPISuccess('domain', 'create', $domparams);
504 $params = array( 'domain_id' => 'all',
506 $result = $this->callAPISuccess('setting', 'get', $params);
507 $params = array( 'address_format' => 'xyz',
508 'mailing_format' => 'bcs',
509 'domain_id' => $this->_domainID2
,
511 $result = $this->callAPISuccess('setting', 'create', $params);
512 $params = array( 'domain_id' => $dom['id'],
514 $result = $this->callAPISuccess('setting', 'get', $params);
515 $this->assertAPISuccess($result, "in line " . __LINE__
);
516 $this->assertArrayNotHasKey('tag_unconfirmed', $result['values'][$dom['id']],'setting for domain 3 should not be set. Debug this IF domain test is passing');
517 $result = $this->callAPISuccess('setting', 'fill', $params);
518 $this->assertAPISuccess($result, "in line " . __LINE__
);
519 $result = $this->callAPISuccess('setting', 'get', $params);
520 $this->assertAPISuccess($result, "in line " . __LINE__
);
521 $this->assertArrayHasKey('tag_unconfirmed', $result['values'][$dom['id']]);
522 $this->assertArrayHasKey('extensionsDir', $result['values'][$dom['id']]);
523 $this->assertEquals('Unconfirmed', $result['values'][$dom['id']]['tag_unconfirmed']);