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 * Ensure that on_change callbacks fire.
131 * Note: api_v3_SettingTest::testOnChange and CRM_Core_BAO_SettingTest::testOnChange
132 * are very similar, but they exercise different codepaths. The first uses the API
133 * and setItems [plural]; the second uses setItem [singular].
135 function testOnChange() {
136 global $_testOnChange_hookCalls;
137 $this->setMockSettingsMetaData(array(
138 'onChangeExample' => array(
139 'group_name' => 'CiviCRM Preferences',
141 'name' => 'onChangeExample',
143 'quick_form_type' => 'Element',
144 'html_type' => 'advmultiselect',
145 'default' => array('CiviEvent', 'CiviContribute'),
147 'title' => 'List of Components',
150 'description' => NULL,
152 'on_change' => array( // list of callbacks
153 array(__CLASS__
, '_testOnChange_onChangeExample')
159 $_testOnChange_hookCalls = array('count' => 0);
160 $this->callAPISuccess('setting', 'create', array(
161 'onChangeExample' => array('First', 'Value'),
163 $this->assertEquals(1, $_testOnChange_hookCalls['count']);
164 $this->assertEquals(array('First', 'Value'), $_testOnChange_hookCalls['newValue']);
165 $this->assertEquals('List of Components', $_testOnChange_hookCalls['metadata']['title']);
168 $_testOnChange_hookCalls = array('count' => 0);
169 $this->callAPISuccess('setting', 'create', array(
170 'onChangeExample' => array('Second', 'Value'),
172 $this->assertEquals(1, $_testOnChange_hookCalls['count']);
173 $this->assertEquals(array('First', 'Value'), $_testOnChange_hookCalls['oldValue']);
174 $this->assertEquals(array('Second', 'Value'), $_testOnChange_hookCalls['newValue']);
175 $this->assertEquals('List of Components', $_testOnChange_hookCalls['metadata']['title']);
179 * Mock callback for a setting's on_change handler
185 static function _testOnChange_onChangeExample($oldValue, $newValue, $metadata) {
186 global $_testOnChange_hookCalls;
187 $_testOnChange_hookCalls['count']++
;
188 $_testOnChange_hookCalls['oldValue'] = $oldValue;
189 $_testOnChange_hookCalls['newValue'] = $newValue;
190 $_testOnChange_hookCalls['metadata'] = $metadata;
194 * check getfields works
196 function testCreateSetting() {
197 $description = "shows setting a variable for a given domain - if no domain is set current is assumed";
200 'domain_id' => $this->_domainID2
,
201 'uniq_email_per_site' => 1,
203 $result = $this->callAPIAndDocument('setting', 'create', $params, __FUNCTION__
, __FILE__
);
205 $params = array('uniq_email_per_site' => 1,);
206 $description = "shows setting a variable for a current domain";
207 $result = $this->callAPIAndDocument('setting', 'create', $params, __FUNCTION__
, __FILE__
, $description, 'CreateSettingCurrentDomain');
208 $this->assertArrayHasKey(CRM_Core_Config
::domainID(), $result['values']);
212 * check getfields works
214 function testCreateInvalidSettings() {
217 'domain_id' => $this->_domainID2
,
220 $result = $this->callAPIFailure('setting', 'create', $params);
224 * check invalid settings rejected -
227 function testCreateInvalidURLSettings() {
230 'domain_id' => $this->_domainID2
,
231 'userFrameworkResourceURL' => 'dfhkdhfd',
233 $result = $this->callAPIFailure('setting', 'create', $params);
235 'domain_id' => $this->_domainID2
,
236 'userFrameworkResourceURL' => 'http://blah.com',
238 $result = $this->callAPISuccess('setting', 'create', $params);
242 * check getfields works
244 function testCreateInvalidBooleanSettings() {
247 'domain_id' => $this->_domainID2
,
248 'track_civimail_replies' => 'dfhkdhfd',
250 $result = $this->callAPIFailure('setting', 'create', $params);
252 $params = array('track_civimail_replies' => '0',);
253 $result = $this->callAPISuccess('setting', 'create', $params);
254 $getResult = $this->callAPISuccess('setting','get',$params);
255 $this->assertEquals(0, $getResult['values'][$this->_currentDomain
]['track_civimail_replies']);
257 $getResult = $this->callAPISuccess('setting','get',$params);
258 $this->assertEquals(0, $getResult['values'][$this->_currentDomain
]['track_civimail_replies']);
259 $params = array( 'domain_id' => $this->_domainID2
,
260 'track_civimail_replies' => '1',
262 $result = $this->callAPISuccess('setting', 'create', $params);
263 $getResult = $this->callAPISuccess('setting','get',$params);
264 $this->assertEquals(1, $getResult['values'][$this->_domainID2
]['track_civimail_replies']);
267 'domain_id' => $this->_domainID2
,
268 'track_civimail_replies' => 'TRUE',
270 $result = $this->callAPISuccess('setting', 'create', $params);
271 $getResult = $this->callAPISuccess('setting','get',$params);
273 $this->assertEquals(1, $getResult['values'][$this->_domainID2
]['track_civimail_replies'], "check TRUE is converted to 1");
279 * check getfields works
281 function testCreateSettingMultipleDomains() {
282 $description = "shows setting a variable for all domains";
285 'domain_id' => 'all',
286 'uniq_email_per_site' => 1,
288 $result = $this->callAPIAndDocument('setting', 'create', $params, __FUNCTION__
, __FILE__
,$description, 'CreateAllDomains');
290 $this->assertEquals(1, $result['values'][2]['uniq_email_per_site']);
291 $this->assertEquals(1, $result['values'][1]['uniq_email_per_site']);
292 $this->assertArrayHasKey(3, $result['values'], 'Domain create probably failed Debug this IF domain test is passing');
293 $this->assertEquals(1, $result['values'][3]['uniq_email_per_site'], 'failed to set setting for domain 3.');
296 'domain_id' => 'all',
297 'return' => 'uniq_email_per_site'
299 // we'll check it with a 'get'
300 $description = "shows getting a variable for all domains";
301 $result = $this->callAPIAndDocument('setting', 'get', $params, __FUNCTION__
, __FILE__
,$description, 'GetAllDomains', 'Get');
303 $this->assertEquals(1, $result['values'][2]['uniq_email_per_site']);
304 $this->assertEquals(1, $result['values'][1]['uniq_email_per_site']);
305 $this->assertEquals(1, $result['values'][3]['uniq_email_per_site']);
308 'domain_id' => array(1,3),
309 'uniq_email_per_site' => 0,
311 $description = "shows setting a variable for specified domains";
312 $result = $this->callAPIAndDocument('setting', 'create', $params, __FUNCTION__
, __FILE__
,$description, 'CreateSpecifiedDomains');
314 $this->assertEquals(0, $result['values'][3]['uniq_email_per_site']);
315 $this->assertEquals(0, $result['values'][1]['uniq_email_per_site']);
317 'domain_id' => array(1,2),
318 'return' => array('uniq_email_per_site'),
320 $description = "shows getting a variable for specified domains";
321 $result = $this->callAPIAndDocument('setting', 'get', $params, __FUNCTION__
, __FILE__
,$description, 'GetSpecifiedDomains', 'Get');
322 $this->assertEquals(1, $result['values'][2]['uniq_email_per_site']);
323 $this->assertEquals(0, $result['values'][1]['uniq_email_per_site']);
327 function testGetSetting() {
330 'domain_id' => $this->_domainID2
,
331 'return' => 'uniq_email_per_site',
333 $description = "shows get setting a variable for a given domain - if no domain is set current is assumed";
335 $result = $this->callAPIAndDocument('setting', 'get', $params, __FUNCTION__
, __FILE__
);
338 'return' => 'uniq_email_per_site',
340 $description = "shows getting a variable for a current domain";
341 $result = $this->callAPIAndDocument('setting', 'get', $params, __FUNCTION__
, __FILE__
, $description, 'GetSettingCurrentDomain');
342 $this->assertArrayHasKey(CRM_Core_Config
::domainID(), $result['values']);
345 * setting api should set & fetch settings stored in config as well as those in settings table
347 function testSetConfigSetting() {
348 $config = CRM_Core_Config
::singleton();
349 $this->assertFalse($config->debug
== 1);
351 'domain_id' => $this->_domainID2
,
352 'debug_enabled' => 1,
354 $result = $this->callAPISuccess('setting', 'create', $params);
355 CRM_Core_BAO_Domain
::setDomain($this->_domainID2
);
356 $config = CRM_Core_Config
::singleton(TRUE, TRUE);
357 CRM_Core_BAO_Domain
::resetDomain();
358 $this->assertTrue($config->debug
== 1);
359 // this should NOT be stored in the settings table now - only in config
360 $sql = " SELECT count(*) as c FROM civicrm_setting WHERE name LIKE '%debug%'";
361 $dao = CRM_Core_DAO
::executeQuery($sql);
363 $this->assertEquals($dao->c
, 0);
366 * setting api should set & fetch settings stored in config as well as those in settings table
368 function testGetConfigSetting() {
369 $settings = $this->callAPISuccess('setting', 'get', array(
370 'name' => 'defaultCurrency', 'sequential' => 1,)
372 $this->assertEquals('USD', $settings['values'][0]['defaultCurrency']);
376 * setting api should set & fetch settings stored in config as well as those in settings table
378 function testGetSetConfigSettingMultipleDomains() {
379 $settings = $this->callAPISuccess('setting', 'create', array(
380 'defaultCurrency' => 'USD', 'domain_id' => $this->_currentDomain
)
382 $settings = $this->callAPISuccess('setting', 'create', array(
383 'defaultCurrency' => 'CAD', 'domain_id' => $this->_domainID2
)
385 $settings = $this->callAPISuccess('setting', 'get', array(
386 'return' => 'defaultCurrency', 'domain_id' => 'all',
389 $this->assertEquals('USD', $settings['values'][$this->_currentDomain
]['defaultCurrency']);
390 $this->assertEquals('CAD', $settings['values'][$this->_domainID2
]['defaultCurrency'],
391 "second domain (id {$this->_domainID2} ) should be set to CAD. First dom was {$this->_currentDomain} & was USD");
396 * Use getValue against a config setting
398 function testGetValueConfigSetting() {
399 $params = array( 'name' => 'monetaryThousandSeparator',
400 'group' => 'Localization Setting',
402 $result = $this->callAPISuccess('setting', 'getvalue', $params);
403 $this->assertEquals(',', $result);
406 function testGetValue() {
407 $params = array( 'name' => 'petition_contacts',
408 'group' => 'Campaign Preferences'
410 $description = "Demonstrates getvalue action - intended for runtime use as better caching than get";
412 $result = $this->callAPIAndDocument('setting', 'getvalue', $params, __FUNCTION__
, __FILE__
, $description);
413 $this->assertEquals('Petition Contacts', $result);
416 function testGetDefaults() {
417 $description = "gets defaults setting a variable for a given domain - if no domain is set current is assumed";
420 'name' => 'address_format',
422 $result = $this->callAPIAndDocument('setting', 'getdefaults', $params, __FUNCTION__
, __FILE__
,$description,'GetDefaults','getdefaults');
423 $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']);
424 $params = array('name' => 'mailing_format',);
425 $result = $this->callAPISuccess('setting', 'getdefaults', $params);
426 $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']);
427 $this->assertArrayHasKey(CRM_Core_Config
::domainID(), $result['values']);
430 * Function tests reverting a specific parameter
432 function testRevert() {
434 $params = array( 'address_format' => 'xyz',
435 'mailing_format' => 'bcs',
437 $result = $this->callAPISuccess('setting', 'create', $params);
438 $this->assertAPISuccess($result, "in line " . __LINE__
);
439 $revertParams = array( 'name' => 'address_format'
441 $result = $this->callAPISuccess('setting', 'get', $params);
443 $this->assertEquals('xyz', $result['values'][CRM_Core_Config
::domainID()]['address_format']);
444 $description = "Demonstrates reverting a parameter to default value";
445 $result = $this->callAPIAndDocument('setting', 'revert', $revertParams, __FUNCTION__
, __FILE__
,$description,'','revert');
446 //make sure it's reverted
447 $result = $this->callAPISuccess('setting', 'get', $params);
448 $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']);
449 $params = array( 'return' => array('mailing_format'),
451 $result = $this->callAPISuccess('setting', 'get', $params);
452 //make sure it's unchanged
453 $this->assertEquals('bcs', $result['values'][CRM_Core_Config
::domainID()]['mailing_format']);
456 * Tests reverting ALL parameters (specific domain)
458 function testRevertAll() {
460 $params = array( 'address_format' => 'xyz',
461 'mailing_format' => 'bcs',
463 $result = $this->callAPISuccess('setting', 'create', $params);
464 $revertParams = array( );
465 $result = $this->callAPISuccess('setting', 'get', $params);
467 $this->assertEquals('xyz', $result['values'][CRM_Core_Config
::domainID()]['address_format']);
469 $this->callAPISuccess('setting', 'revert', $revertParams);
470 //make sure it's reverted
471 $result = $this->callAPISuccess('setting', 'get', array('group' => 'core'));
472 $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']);
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']);
477 * Tests filling missing params
479 function testFill() {
481 'name' => 'B Team Domain',
483 $dom = $this->callAPISuccess('domain', 'create', $domparams);
484 $params = array( 'domain_id' => 'all',
486 $result = $this->callAPISuccess('setting', 'get', $params);
487 $params = array( 'address_format' => 'xyz',
488 'mailing_format' => 'bcs',
489 'domain_id' => $this->_domainID2
,
491 $result = $this->callAPISuccess('setting', 'create', $params);
492 $params = array( 'domain_id' => $dom['id'],
494 $result = $this->callAPISuccess('setting', 'get', $params);
495 $this->assertAPISuccess($result, "in line " . __LINE__
);
496 $this->assertArrayNotHasKey('tag_unconfirmed', $result['values'][$dom['id']],'setting for domain 3 should not be set. Debug this IF domain test is passing');
497 $result = $this->callAPISuccess('setting', 'fill', $params);
498 $this->assertAPISuccess($result, "in line " . __LINE__
);
499 $result = $this->callAPISuccess('setting', 'get', $params);
500 $this->assertAPISuccess($result, "in line " . __LINE__
);
501 $this->assertArrayHasKey('tag_unconfirmed', $result['values'][$dom['id']]);
502 $this->assertArrayHasKey('extensionsDir', $result['values'][$dom['id']]);
503 $this->assertEquals('Unconfirmed', $result['values'][$dom['id']]['tag_unconfirmed']);