3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.3 |
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;
50 public $_eNoticeCompliant = TRUE;
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']);
128 * check getfields works
130 function testCreateSetting() {
131 $description = "shows setting a variable for a given domain - if no domain is set current is assumed";
134 'domain_id' => $this->_domainID2
,
135 'uniq_email_per_site' => 1,
137 $result = $this->callAPIAndDocument('setting', 'create', $params, __FUNCTION__
, __FILE__
);
139 $params = array('uniq_email_per_site' => 1,);
140 $description = "shows setting a variable for a current domain";
141 $result = $this->callAPIAndDocument('setting', 'create', $params, __FUNCTION__
, __FILE__
, $description, 'CreateSettingCurrentDomain');
142 $this->assertArrayHasKey(CRM_Core_Config
::domainID(), $result['values']);
146 * check getfields works
148 function testCreateInvalidSettings() {
151 'domain_id' => $this->_domainID2
,
154 $result = $this->callAPIFailure('setting', 'create', $params);
158 * check invalid settings rejected -
161 function testCreateInvalidURLSettings() {
164 'domain_id' => $this->_domainID2
,
165 'userFrameworkResourceURL' => 'dfhkdhfd',
167 $result = $this->callAPIFailure('setting', 'create', $params);
169 'domain_id' => $this->_domainID2
,
170 'userFrameworkResourceURL' => 'http://blah.com',
172 $result = $this->callAPISuccess('setting', 'create', $params);
176 * check getfields works
178 function testCreateInvalidBooleanSettings() {
181 'domain_id' => $this->_domainID2
,
182 'track_civimail_replies' => 'dfhkdhfd',
184 $result = $this->callAPIFailure('setting', 'create', $params);
186 $params = array('track_civimail_replies' => '0',);
187 $result = $this->callAPISuccess('setting', 'create', $params);
188 $getResult = $this->callAPISuccess('setting','get',$params);
189 $this->assertEquals(0, $getResult['values'][$this->_currentDomain
]['track_civimail_replies']);
191 $getResult = $this->callAPISuccess('setting','get',$params);
192 $this->assertEquals(0, $getResult['values'][$this->_currentDomain
]['track_civimail_replies']);
193 $params = array( 'domain_id' => $this->_domainID2
,
194 'track_civimail_replies' => '1',
196 $result = $this->callAPISuccess('setting', 'create', $params);
197 $getResult = $this->callAPISuccess('setting','get',$params);
198 $this->assertEquals(1, $getResult['values'][$this->_domainID2
]['track_civimail_replies']);
201 'domain_id' => $this->_domainID2
,
202 'track_civimail_replies' => 'TRUE',
204 $result = $this->callAPISuccess('setting', 'create', $params);
205 $getResult = $this->callAPISuccess('setting','get',$params);
207 $this->assertEquals(1, $getResult['values'][$this->_domainID2
]['track_civimail_replies'], "check TRUE is converted to 1");
213 * check getfields works
215 function testCreateSettingMultipleDomains() {
216 $description = "shows setting a variable for all domains";
219 'domain_id' => 'all',
220 'uniq_email_per_site' => 1,
222 $result = $this->callAPIAndDocument('setting', 'create', $params, __FUNCTION__
, __FILE__
,$description, 'CreateAllDomains');
224 $this->assertEquals(1, $result['values'][2]['uniq_email_per_site']);
225 $this->assertEquals(1, $result['values'][1]['uniq_email_per_site']);
226 $this->assertArrayHasKey(3, $result['values'], 'Domain create probably failed Debug this IF domain test is passing');
227 $this->assertEquals(1, $result['values'][3]['uniq_email_per_site'], 'failed to set setting for domain 3.');
230 'domain_id' => 'all',
231 'return' => 'uniq_email_per_site'
233 // we'll check it with a 'get'
234 $description = "shows getting a variable for all domains";
235 $result = $this->callAPIAndDocument('setting', 'get', $params, __FUNCTION__
, __FILE__
,$description, 'GetAllDomains', 'Get');
237 $this->assertEquals(1, $result['values'][2]['uniq_email_per_site']);
238 $this->assertEquals(1, $result['values'][1]['uniq_email_per_site']);
239 $this->assertEquals(1, $result['values'][3]['uniq_email_per_site']);
242 'domain_id' => array(1,3),
243 'uniq_email_per_site' => 0,
245 $description = "shows setting a variable for specified domains";
246 $result = $this->callAPIAndDocument('setting', 'create', $params, __FUNCTION__
, __FILE__
,$description, 'CreateSpecifiedDomains');
248 $this->assertEquals(0, $result['values'][3]['uniq_email_per_site']);
249 $this->assertEquals(0, $result['values'][1]['uniq_email_per_site']);
251 'domain_id' => array(1,2),
252 'return' => array('uniq_email_per_site'),
254 $description = "shows getting a variable for specified domains";
255 $result = $this->callAPIAndDocument('setting', 'get', $params, __FUNCTION__
, __FILE__
,$description, 'GetSpecifiedDomains', 'Get');
256 $this->assertEquals(1, $result['values'][2]['uniq_email_per_site']);
257 $this->assertEquals(0, $result['values'][1]['uniq_email_per_site']);
261 function testGetSetting() {
264 'domain_id' => $this->_domainID2
,
265 'return' => 'uniq_email_per_site',
267 $description = "shows get setting a variable for a given domain - if no domain is set current is assumed";
269 $result = $this->callAPIAndDocument('setting', 'get', $params, __FUNCTION__
, __FILE__
);
272 'return' => 'uniq_email_per_site',
274 $description = "shows getting a variable for a current domain";
275 $result = $this->callAPIAndDocument('setting', 'get', $params, __FUNCTION__
, __FILE__
, $description, 'GetSettingCurrentDomain');
276 $this->assertArrayHasKey(CRM_Core_Config
::domainID(), $result['values']);
279 * setting api should set & fetch settings stored in config as well as those in settings table
281 function testSetConfigSetting() {
282 $config = CRM_Core_Config
::singleton();
283 $this->assertFalse($config->debug
== 1);
285 'domain_id' => $this->_domainID2
,
286 'debug_enabled' => 1,
288 $result = $this->callAPISuccess('setting', 'create', $params);
289 CRM_Core_BAO_Domain
::setDomain($this->_domainID2
);
290 $config = CRM_Core_Config
::singleton(TRUE, TRUE);
291 CRM_Core_BAO_Domain
::resetDomain();
292 $this->assertTrue($config->debug
== 1);
293 // this should NOT be stored in the settings table now - only in config
294 $sql = " SELECT count(*) as c FROM civicrm_setting WHERE name LIKE '%debug%'";
295 $dao = CRM_Core_DAO
::executeQuery($sql);
297 $this->assertEquals($dao->c
, 0);
300 * setting api should set & fetch settings stored in config as well as those in settings table
302 function testGetConfigSetting() {
303 $settings = $this->callAPISuccess('setting', 'get', array(
304 'name' => 'defaultCurrency', 'sequential' => 1,)
306 $this->assertEquals('USD', $settings['values'][0]['defaultCurrency']);
310 * setting api should set & fetch settings stored in config as well as those in settings table
312 function testGetSetConfigSettingMultipleDomains() {
313 $settings = $this->callAPISuccess('setting', 'create', array(
314 'defaultCurrency' => 'USD', 'domain_id' => $this->_currentDomain
)
316 $settings = $this->callAPISuccess('setting', 'create', array(
317 'defaultCurrency' => 'CAD', 'domain_id' => $this->_domainID2
)
319 $settings = $this->callAPISuccess('setting', 'get', array(
320 'return' => 'defaultCurrency', 'domain_id' => 'all',
323 $this->assertEquals('USD', $settings['values'][$this->_currentDomain
]['defaultCurrency']);
324 $this->assertEquals('CAD', $settings['values'][$this->_domainID2
]['defaultCurrency'],
325 "second domain (id {$this->_domainID2} ) should be set to CAD. First dom was {$this->_currentDomain} & was USD");
330 * Use getValue against a config setting
332 function testGetValueConfigSetting() {
333 $params = array( 'name' => 'monetaryThousandSeparator',
334 'group' => 'Localization Setting',
336 $result = $this->callAPISuccess('setting', 'getvalue', $params);
337 $this->assertEquals(',', $result);
340 function testGetValue() {
341 $params = array( 'name' => 'petition_contacts',
342 'group' => 'Campaign Preferences'
344 $description = "Demonstrates getvalue action - intended for runtime use as better caching than get";
346 $result = $this->callAPIAndDocument('setting', 'getvalue', $params, __FUNCTION__
, __FILE__
, $description);
347 $this->assertEquals('Petition Contacts', $result);
350 function testGetDefaults() {
351 $description = "gets defaults setting a variable for a given domain - if no domain is set current is assumed";
354 'name' => 'address_format',
356 $result = $this->callAPIAndDocument('setting', 'getdefaults', $params, __FUNCTION__
, __FILE__
,$description,'GetDefaults','getdefaults');
357 $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']);
358 $params = array('name' => 'mailing_format',);
359 $result = $this->callAPISuccess('setting', 'getdefaults', $params);
360 $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']);
361 $this->assertArrayHasKey(CRM_Core_Config
::domainID(), $result['values']);
364 * Function tests reverting a specific parameter
366 function testRevert() {
368 $params = array( 'address_format' => 'xyz',
369 'mailing_format' => 'bcs',
371 $result = $this->callAPISuccess('setting', 'create', $params);
372 $this->assertAPISuccess($result, "in line " . __LINE__
);
373 $revertParams = array( 'name' => 'address_format'
375 $result = $this->callAPISuccess('setting', 'get', $params);
377 $this->assertEquals('xyz', $result['values'][CRM_Core_Config
::domainID()]['address_format']);
378 $description = "Demonstrates reverting a parameter to default value";
379 $result = $this->callAPIAndDocument('setting', 'revert', $revertParams, __FUNCTION__
, __FILE__
,$description,'','revert');
380 //make sure it's reverted
381 $result = $this->callAPISuccess('setting', 'get', $params);
382 $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']);
383 $params = array( 'return' => array('mailing_format'),
385 $result = $this->callAPISuccess('setting', 'get', $params);
386 //make sure it's unchanged
387 $this->assertEquals('bcs', $result['values'][CRM_Core_Config
::domainID()]['mailing_format']);
390 * Tests reverting ALL parameters (specific domain)
392 function testRevertAll() {
394 $params = array( 'address_format' => 'xyz',
395 'mailing_format' => 'bcs',
397 $result = $this->callAPISuccess('setting', 'create', $params);
398 $revertParams = array( );
399 $result = $this->callAPISuccess('setting', 'get', $params);
401 $this->assertEquals('xyz', $result['values'][CRM_Core_Config
::domainID()]['address_format']);
403 $this->callAPISuccess('setting', 'revert', $revertParams);
404 //make sure it's reverted
405 $result = $this->callAPISuccess('setting', 'get', array('group' => 'core'));
406 $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']);
407 $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']);
411 * Tests filling missing params
413 function testFill() {
415 'name' => 'B Team Domain',
417 $dom = $this->callAPISuccess('domain', 'create', $domparams);
418 $params = array( 'domain_id' => 'all',
420 $result = $this->callAPISuccess('setting', 'get', $params);
421 $params = array( 'address_format' => 'xyz',
422 'mailing_format' => 'bcs',
423 'domain_id' => $this->_domainID2
,
425 $result = $this->callAPISuccess('setting', 'create', $params);
426 $params = array( 'domain_id' => $dom['id'],
428 $result = $this->callAPISuccess('setting', 'get', $params);
429 $this->assertAPISuccess($result, "in line " . __LINE__
);
430 $this->assertArrayNotHasKey('tag_unconfirmed', $result['values'][$dom['id']],'setting for domain 3 should not be set. Debug this IF domain test is passing');
431 $result = $this->callAPISuccess('setting', 'fill', $params);
432 $this->assertAPISuccess($result, "in line " . __LINE__
);
433 $result = $this->callAPISuccess('setting', 'get', $params);
434 $this->assertAPISuccess($result, "in line " . __LINE__
);
435 $this->assertArrayHasKey('tag_unconfirmed', $result['values'][$dom['id']]);
436 $this->assertArrayHasKey('extensionsDir', $result['values'][$dom['id']]);
437 $this->assertEquals('Unconfirmed', $result['values'][$dom['id']]['tag_unconfirmed']);