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',
70 $result = civicrm_api( 'domain','get',$params);
71 if(empty($result['id'])){
72 $result = civicrm_api( 'domain','create',$params );
73 $this->assertAPISuccess($result);
76 $params['name'] = 'Second Domain';
77 $result = civicrm_api( 'domain','get',$params);
78 if(empty($result['id'])){
79 $result = civicrm_api( 'domain','create',$params );
80 $this->assertAPISuccess($result);
82 $this->_domainID2
= $result['id'];
83 $params['name'] = 'A-team domain';
84 $result = civicrm_api( 'domain','get',$params);
85 if(empty($result['id'])){
86 $result = civicrm_api( 'domain','create',$params );
87 $this->assertAPISuccess($result);
89 $this->_domainID3
= $result['id'];
90 $this->_currentDomain
= CRM_Core_Config
::domainID();
95 $this->quickCleanup(array('civicrm_domain'));
96 civicrm_api('system','flush', array('version' => $this->_apiversion
));
100 * check getfields works
102 function testGetFields() {
103 $params = array('version' => $this->_apiversion
);
104 $result = civicrm_api('setting', 'getfields', $params);
105 $description = 'Demonstrate return from getfields - see subfolder for variants';
106 $this->documentMe($params, $result, __FUNCTION__
, __FILE__
, $description,'', 'getfields');
107 $this->assertAPISuccess($result, "in line " . __LINE__
);
108 $this->assertArrayHasKey('customCSSURL', $result['values']);
110 $description = 'Demonstrate return from getfields';
111 $result = civicrm_api('setting', 'getfields', array('version' => $this->_apiversion
));
112 // $this->documentMe($params, $result, __FUNCTION__, __FILE__, $description, 'GetFieldsGroup');
113 $this->assertAPISuccess($result, "in line " . __LINE__
);
114 $this->assertArrayHasKey('customCSSURL', $result['values']);
115 civicrm_api('system','flush', array('version' => $this->_apiversion
));
119 * let's check it's loading from cache by meddling with the cache
121 function testGetFieldsCaching() {
122 $settingsMetadata = array();
123 CRM_Core_BAO_Cache
::setItem($settingsMetadata,'CiviCRM setting Specs', 'settingsMetadata__');
124 CRM_Core_BAO_Cache
::setItem($settingsMetadata,'CiviCRM setting Spec', 'All');
125 $result = civicrm_api('setting', 'getfields', array('version' => $this->_apiversion
));
126 $this->assertAPISuccess($result, "in line " . __LINE__
);
127 $this->assertArrayNotHasKey('customCSSURL', $result['values']);
128 $this->quickCleanup(array('civicrm_cache'));
131 function testGetFieldsFilters() {
132 $params = array('version' => $this->_apiversion
);
133 $filters = array('name' => 'advanced_search_options');
134 $result = civicrm_api('setting', 'getfields', $params +
$filters);
135 $this->assertAPISuccess($result, ' in LINE ' . __LINE__
);
137 $this->assertArrayNotHasKey('customCSSURL', $result['values']);
138 $this->assertArrayHasKey('advanced_search_options',$result['values']);
141 * check getfields works
143 function testCreateSetting() {
145 $params = array('version' => $this->_apiversion
,
146 'domain_id' => $this->_domainID2
,
147 'uniq_email_per_site' => 1,
149 $result = civicrm_api('setting', 'create', $params);
150 $description = "shows setting a variable for a given domain - if no domain is set current is assumed";
151 $this->documentMe($params, $result, __FUNCTION__
, __FILE__
);
152 $this->assertAPISuccess($result, "in line " . __LINE__
);
154 $params = array('version' => $this->_apiversion
,
155 'uniq_email_per_site' => 1,
157 $result = civicrm_api('setting', 'create', $params);
158 $description = "shows setting a variable for a current domain";
159 $this->documentMe($params, $result, __FUNCTION__
, __FILE__
, $description, 'CreateSettingCurrentDomain');
160 $this->assertAPISuccess($result, "in line " . __LINE__
);
161 $this->assertArrayHasKey(CRM_Core_Config
::domainID(), $result['values']);
165 * check getfields works
167 function testCreateInvalidSettings() {
169 $params = array('version' => $this->_apiversion
,
170 'domain_id' => $this->_domainID2
,
173 $result = $this->callAPIFailure('setting', 'create', $params);
177 * check invalid settings rejected -
180 function testCreateInvalidURLSettings() {
182 $params = array('version' => $this->_apiversion
,
183 'domain_id' => $this->_domainID2
,
184 'userFrameworkResourceURL' => 'dfhkdhfd',
186 $result = $this->callAPIFailure('setting', 'create', $params);
187 $params = array('version' => $this->_apiversion
,
188 'domain_id' => $this->_domainID2
,
189 'userFrameworkResourceURL' => 'http://blah.com',
191 $result = civicrm_api('setting', 'create', $params);
192 $this->assertAPISuccess($result);
197 * check getfields works
199 function testCreateInvalidBooleanSettings() {
201 $params = array('version' => $this->_apiversion
,
202 'domain_id' => $this->_domainID2
,
203 'track_civimail_replies' => 'dfhkdhfd',
205 $result = $this->callAPIFailure('setting', 'create', $params);
207 $params = array('version' => $this->_apiversion
,
208 'track_civimail_replies' => '0',
210 $result = civicrm_api('setting', 'create', $params);
211 $getResult = civicrm_api('setting','get',$params);
212 $this->assertEquals(0, $getResult['values'][$this->_currentDomain
]['track_civimail_replies']);
214 $this->assertAPISuccess($result);
215 $getResult = civicrm_api('setting','get',$params);
216 $this->assertEquals(0, $getResult['values'][$this->_currentDomain
]['track_civimail_replies']);
218 'version' => $this->_apiversion
,
219 'domain_id' => $this->_domainID2
,
220 'track_civimail_replies' => '1',
222 $result = civicrm_api('setting', 'create', $params);
223 $this->assertAPISuccess($result);
224 $getResult = civicrm_api('setting','get',$params);
225 $this->assertEquals(1, $getResult['values'][$this->_domainID2
]['track_civimail_replies']);
227 $params = array('version' => $this->_apiversion
,
228 'domain_id' => $this->_domainID2
,
229 'track_civimail_replies' => 'TRUE',
231 $result = civicrm_api('setting', 'create', $params);
232 $this->assertAPISuccess($result);
233 $getResult = civicrm_api('setting','get',$params);
235 $this->assertEquals(1, $getResult['values'][$this->_domainID2
]['track_civimail_replies'], "check TRUE is converted to 1");
241 * check getfields works
243 function testCreateSettingMultipleDomains() {
245 $params = array('version' => $this->_apiversion
,
246 'domain_id' => 'all',
247 'uniq_email_per_site' => 1,
249 $result = civicrm_api('setting', 'create', $params);
250 $description = "shows setting a variable for all domains";
251 $this->documentMe($params, $result, __FUNCTION__
, __FILE__
,$description, 'CreateAllDomains');
252 $this->assertAPISuccess($result, "in line " . __LINE__
);
253 $this->assertEquals(1, $result['values'][2]['uniq_email_per_site']);
254 $this->assertEquals(1, $result['values'][1]['uniq_email_per_site']);
255 $this->assertArrayHasKey(3, $result['values'], 'Domain create probably failed Debug this IF domain test is passing');
256 $this->assertEquals(1, $result['values'][3]['uniq_email_per_site'], 'failed to set setting for domain 3.');
258 $params = array('version' => $this->_apiversion
,
259 'domain_id' => 'all',
260 'return' => 'uniq_email_per_site'
262 // we'll check it with a 'get'
263 $result = civicrm_api('setting', 'get', $params);
264 $description = "shows getting a variable for all domains";
265 $this->documentMe($params, $result, __FUNCTION__
, __FILE__
,$description, 'GetAllDomains', 'Get');
266 $this->assertAPISuccess($result, "in line " . __LINE__
);
267 $this->assertEquals(1, $result['values'][2]['uniq_email_per_site']);
268 $this->assertEquals(1, $result['values'][1]['uniq_email_per_site']);
269 $this->assertEquals(1, $result['values'][3]['uniq_email_per_site']);
271 $params = array('version' => $this->_apiversion
,
272 'domain_id' => array(1,3),
273 'uniq_email_per_site' => 0,
275 $result = civicrm_api('setting', 'create', $params);
276 $description = "shows setting a variable for specified domains";
277 $this->documentMe($params, $result, __FUNCTION__
, __FILE__
,$description, 'CreateSpecifiedDomains');
278 $this->assertAPISuccess($result, "in line " . __LINE__
);
279 $this->assertEquals(0, $result['values'][3]['uniq_email_per_site']);
280 $this->assertEquals(0, $result['values'][1]['uniq_email_per_site']);
281 $params = array('version' => $this->_apiversion
,
282 'domain_id' => array(1,2),
283 'return' => array('uniq_email_per_site'),
285 $result = civicrm_api('setting', 'get', $params);
286 $description = "shows getting a variable for specified domains";
287 $this->documentMe($params, $result, __FUNCTION__
, __FILE__
,$description, 'GetSpecifiedDomains', 'Get');
288 $this->assertAPISuccess($result, "in line " . __LINE__
);
289 $this->assertEquals(1, $result['values'][2]['uniq_email_per_site']);
290 $this->assertEquals(0, $result['values'][1]['uniq_email_per_site']);
294 function testGetSetting() {
296 $params = array('version' => $this->_apiversion
,
297 'domain_id' => $this->_domainID2
,
298 'return' => 'uniq_email_per_site',
300 $result = civicrm_api('setting', 'get', $params);
301 $description = "shows get setting a variable for a given domain - if no domain is set current is assumed";
302 $this->documentMe($params, $result, __FUNCTION__
, __FILE__
);
303 $this->assertAPISuccess($result, "in line " . __LINE__
);
306 'version' => $this->_apiversion
,
307 'return' => 'uniq_email_per_site',
309 $result = civicrm_api('setting', 'get', $params);
310 $description = "shows getting a variable for a current domain";
311 $this->documentMe($params, $result, __FUNCTION__
, __FILE__
, $description, 'GetSettingCurrentDomain');
312 $this->assertAPISuccess($result, "in line " . __LINE__
);
313 $this->assertArrayHasKey(CRM_Core_Config
::domainID(), $result['values']);
316 * setting api should set & fetch settings stored in config as well as those in settings table
318 function testSetConfigSetting() {
319 $config = CRM_Core_Config
::singleton();
320 $this->assertFalse($config->debug
== 1);
321 $params = array('version' => $this->_apiversion
,
322 'domain_id' => $this->_domainID2
,
323 'debug_enabled' => 1,
325 $result = civicrm_api('setting', 'create', $params);
326 $this->assertAPISuccess($result, "in line " . __LINE__
);
327 CRM_Core_BAO_Domain
::setDomain($this->_domainID2
);
328 $config = CRM_Core_Config
::singleton(TRUE, TRUE);
329 CRM_Core_BAO_Domain
::resetDomain();
330 $this->assertTrue($config->debug
== 1);
331 // this should NOT be stored in the settings table now - only in config
332 $sql = " SELECT count(*) as c FROM civicrm_setting WHERE name LIKE '%debug%'";
333 $dao = CRM_Core_DAO
::executeQuery($sql);
335 $this->assertEquals($dao->c
, 0);
338 * setting api should set & fetch settings stored in config as well as those in settings table
340 function testGetConfigSetting() {
341 $settings = civicrm_api('setting', 'get', array(
342 'name' => 'defaultCurrency',
343 'version' => $this->_apiversion
,
346 $this->assertAPISuccess($settings);
347 $this->assertEquals('USD', $settings['values'][0]['defaultCurrency']);
351 * setting api should set & fetch settings stored in config as well as those in settings table
353 function testGetSetConfigSettingMultipleDomains() {
354 $settings = civicrm_api('setting', 'create', array(
355 'defaultCurrency' => 'USD',
356 'version' => $this->_apiversion
,
357 'domain_id' => $this->_currentDomain
)
359 $settings = civicrm_api('setting', 'create', array(
360 'defaultCurrency' => 'CAD',
361 'version' => $this->_apiversion
,
362 'domain_id' => $this->_domainID2
)
364 $this->assertAPISuccess($settings);
365 $settings = civicrm_api('setting', 'get', array(
366 'return' => 'defaultCurrency',
367 'version' => $this->_apiversion
,
368 'domain_id' => 'all',
371 $this->assertEquals('USD', $settings['values'][$this->_currentDomain
]['defaultCurrency']);
372 $this->assertEquals('CAD', $settings['values'][$this->_domainID2
]['defaultCurrency'],
373 "second domain (id {$this->_domainID2} ) should be set to CAD. First dom was {$this->_currentDomain} & was USD");
378 * Use getValue against a config setting
380 function testGetValueConfigSetting() {
382 'version' => $this->_apiversion
,
383 'name' => 'monetaryThousandSeparator',
384 'group' => 'Localization Setting',
386 $result = civicrm_api('setting', 'getvalue', $params);
387 $this->assertEquals(',', $result);
390 function testGetValue() {
392 'version' => $this->_apiversion
,
393 'name' => 'petition_contacts',
394 'group' => 'Campaign Preferences'
396 $result = civicrm_api('setting', 'getvalue', $params);
397 $this->assertEquals('Petition Contacts', $result);
398 $description = "Demonstrates getvalue action - intended for runtime use as better caching than get";
399 $this->documentMe($params, $result, __FUNCTION__
, __FILE__
, $description);
402 function testGetDefaults() {
404 $params = array('version' => $this->_apiversion
,
405 'name' => 'address_format',
407 $result = civicrm_api('setting', 'getdefaults', $params);
408 $description = "gets defaults setting a variable for a given domain - if no domain is set current is assumed";
409 $this->documentMe($params, $result, __FUNCTION__
, __FILE__
,$description,'GetDefaults','getdefaults');
410 $this->assertAPISuccess($result, "in line " . __LINE__
);
411 $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']);
412 $params = array('version' => $this->_apiversion
,
413 'name' => 'mailing_format',
415 $result = civicrm_api('setting', 'getdefaults', $params);
416 $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']);
417 $this->assertArrayHasKey(CRM_Core_Config
::domainID(), $result['values']);
420 * Function tests reverting a specific parameter
422 function testRevert() {
425 'version' => $this->_apiversion
,
426 'address_format' => 'xyz',
427 'mailing_format' => 'bcs',
429 $result = civicrm_api('setting', 'create', $params);
430 $this->assertAPISuccess($result, "in line " . __LINE__
);
431 $revertParams = array(
432 'version' => $this->_apiversion
,
433 'name' => 'address_format'
435 $result = civicrm_api('setting', 'get', $params);
437 $this->assertEquals('xyz', $result['values'][CRM_Core_Config
::domainID()]['address_format']);
438 $description = "Demonstrates reverting a parameter to default value";
439 $result = civicrm_api('setting', 'revert', $revertParams);
440 $this->documentMe($revertParams, $result, __FUNCTION__
, __FILE__
,$description,'','revert');
441 //make sure it's reverted
442 $result = civicrm_api('setting', 'get', $params);
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']);
445 'version' => $this->_apiversion
,
446 'return' => array('mailing_format'),
448 $result = civicrm_api('setting', 'get', $params);
449 //make sure it's unchanged
450 $this->assertEquals('bcs', $result['values'][CRM_Core_Config
::domainID()]['mailing_format']);
453 * Tests reverting ALL parameters (specific domain)
455 function testRevertAll() {
458 'version' => $this->_apiversion
,
459 'address_format' => 'xyz',
460 'mailing_format' => 'bcs',
462 $result = civicrm_api('setting', 'create', $params);
463 $this->assertAPISuccess($result, "in line " . __LINE__
);
464 $revertParams = array(
465 'version' => $this->_apiversion
,
467 $result = civicrm_api('setting', 'get', $params);
469 $this->assertEquals('xyz', $result['values'][CRM_Core_Config
::domainID()]['address_format']);
471 civicrm_api('setting', 'revert', $revertParams);
472 //make sure it's reverted
473 $result = civicrm_api('setting', 'get', array('version' => 3, 'group' => 'core'));
474 $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']);
475 $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']);
479 * Tests filling missing params
481 function testFill() {
483 'name' => 'B Team Domain',
486 $dom = civicrm_api('domain', 'create', $domparams);
488 'version' => $this->_apiversion
,
489 'domain_id' => 'all',
491 $result = civicrm_api('setting', 'get', $params);
493 'version' => $this->_apiversion
,
494 'address_format' => 'xyz',
495 'mailing_format' => 'bcs',
496 'domain_id' => $this->_domainID2
,
498 $result = civicrm_api('setting', 'create', $params);
500 'version' => $this->_apiversion
,
501 'domain_id' => $dom['id'],
503 $result = civicrm_api('setting', 'get', $params);
504 $this->assertAPISuccess($result, "in line " . __LINE__
);
505 $this->assertArrayNotHasKey('tag_unconfirmed', $result['values'][$dom['id']],'setting for domain 3 should not be set. Debug this IF domain test is passing');
506 $result = civicrm_api('setting', 'fill', $params);
507 $this->assertAPISuccess($result, "in line " . __LINE__
);
508 $result = civicrm_api('setting', 'get', $params);
509 $this->assertAPISuccess($result, "in line " . __LINE__
);
510 $this->assertArrayHasKey('tag_unconfirmed', $result['values'][$dom['id']]);
511 $this->assertArrayHasKey('extensionsDir', $result['values'][$dom['id']]);
512 $this->assertEquals('Unconfirmed', $result['values'][$dom['id']]['tag_unconfirmed']);