3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
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;
51 public function setUp() {
54 'name' => 'Default Domain Name',
56 $result = $this->callAPISuccess('domain', 'get', $params);
57 if (empty($result['id'])) {
58 $result = $this->callAPISuccess('domain', 'create', $params);
61 $params['name'] = 'Second Domain';
62 $result = $this->callAPISuccess('domain', 'get', $params);
63 if (empty($result['id'])) {
64 $result = $this->callAPISuccess('domain', 'create', $params);
66 $this->_domainID2
= $result['id'];
67 $params['name'] = 'A-team domain';
68 $result = $this->callAPISuccess('domain', 'get', $params);
69 if (empty($result['id'])) {
70 $result = $this->callAPISuccess('domain', 'create', $params);
72 $this->_domainID3
= $result['id'];
73 $this->_currentDomain
= CRM_Core_Config
::domainID();
74 $this->hookClass
= CRM_Utils_Hook
::singleton();
77 public function tearDown() {
78 CRM_Utils_Hook
::singleton()->reset();
80 $this->callAPISuccess('system', 'flush', array());
81 $this->quickCleanup(array('civicrm_domain'));
85 * Set additional settings into metadata (implements hook)
86 * @param array $metaDataFolders
88 public function setExtensionMetadata(&$metaDataFolders) {
90 $metaDataFolders[] = $civicrm_root . '/tests/phpunit/api/v3/settings';
95 * Check getfields works.
97 public function testGetFields() {
98 $description = 'Demonstrate return from getfields - see subfolder for variants';
99 $result = $this->callAPIAndDocument('setting', 'getfields', array(), __FUNCTION__
, __FILE__
, $description);
100 $this->assertArrayHasKey('customCSSURL', $result['values']);
102 $description = 'Demonstrate return from getfields';
103 $result = $this->callAPISuccess('setting', 'getfields', array());
104 $this->assertArrayHasKey('customCSSURL', $result['values']);
105 $this->callAPISuccess('system', 'flush', array());
109 * Let's check it's loading from cache by meddling with the cache
111 public function testGetFieldsCaching() {
112 $settingsMetadata = array();
113 CRM_Core_BAO_Cache
::setItem($settingsMetadata, 'CiviCRM setting Specs', 'settingsMetadata__');
114 CRM_Core_BAO_Cache
::setItem($settingsMetadata, 'CiviCRM setting Spec', 'All');
115 $result = $this->callAPISuccess('setting', 'getfields', array());
116 $this->assertArrayNotHasKey('customCSSURL', $result['values']);
117 $this->quickCleanup(array('civicrm_cache'));
120 public function testGetFieldsFilters() {
121 $params = array('name' => 'advanced_search_options');
122 $result = $this->callAPISuccess('setting', 'getfields', $params);
123 $this->assertArrayNotHasKey('customCSSURL', $result['values']);
124 $this->assertArrayHasKey('advanced_search_options', $result['values']);
128 * Test that getfields will filter on group.
130 public function testGetFieldsGroupFilters() {
131 $params = array('filters' => array('group' => 'multisite'));
132 $result = $this->callAPISuccess('setting', 'getfields', $params);
133 $this->assertArrayNotHasKey('customCSSURL', $result['values']);
134 $this->assertArrayHasKey('domain_group_id', $result['values']);
138 * Test that getfields will filter on another field (prefetch)
140 public function testGetFieldsPrefetchFilters() {
141 $params = array('filters' => array('prefetch' => 1));
142 $result = $this->callAPISuccess('setting', 'getfields', $params);
143 $this->assertArrayNotHasKey('disable_mandatory_tokens_check', $result['values']);
144 $this->assertArrayHasKey('monetaryDecimalPoint', $result['values']);
148 * Ensure that on_change callbacks fire.
150 * Note: api_v3_SettingTest::testOnChange and CRM_Core_BAO_SettingTest::testOnChange
151 * are very similar, but they exercise different codepaths. The first uses the API
152 * and setItems [plural]; the second uses setItem [singular].
154 public function testOnChange() {
155 global $_testOnChange_hookCalls;
156 $this->setMockSettingsMetaData(array(
157 'onChangeExample' => array(
158 'group_name' => 'CiviCRM Preferences',
160 'name' => 'onChangeExample',
162 'quick_form_type' => 'Element',
163 'html_type' => 'advmultiselect',
164 'default' => array('CiviEvent', 'CiviContribute'),
166 'title' => 'List of Components',
169 'description' => NULL,
171 'on_change' => array(// list of callbacks
172 array(__CLASS__
, '_testOnChange_onChangeExample'),
178 $_testOnChange_hookCalls = array('count' => 0);
179 $this->callAPISuccess('setting', 'create', array(
180 'onChangeExample' => array('First', 'Value'),
182 $this->assertEquals(1, $_testOnChange_hookCalls['count']);
183 $this->assertEquals(array('First', 'Value'), $_testOnChange_hookCalls['newValue']);
184 $this->assertEquals('List of Components', $_testOnChange_hookCalls['metadata']['title']);
187 $_testOnChange_hookCalls = array('count' => 0);
188 $this->callAPISuccess('setting', 'create', array(
189 'onChangeExample' => array('Second', 'Value'),
191 $this->assertEquals(1, $_testOnChange_hookCalls['count']);
192 $this->assertEquals(array('First', 'Value'), $_testOnChange_hookCalls['oldValue']);
193 $this->assertEquals(array('Second', 'Value'), $_testOnChange_hookCalls['newValue']);
194 $this->assertEquals('List of Components', $_testOnChange_hookCalls['metadata']['title']);
198 * Mock callback for a setting's on_change handler
204 public static function _testOnChange_onChangeExample($oldValue, $newValue, $metadata) {
205 global $_testOnChange_hookCalls;
206 $_testOnChange_hookCalls['count']++
;
207 $_testOnChange_hookCalls['oldValue'] = $oldValue;
208 $_testOnChange_hookCalls['newValue'] = $newValue;
209 $_testOnChange_hookCalls['metadata'] = $metadata;
213 * Check getfields works.
215 public function testCreateSetting() {
216 $description = "Shows setting a variable for a given domain - if no domain is set current is assumed.";
219 'domain_id' => $this->_domainID2
,
220 'uniq_email_per_site' => 1,
222 $result = $this->callAPIAndDocument('setting', 'create', $params, __FUNCTION__
, __FILE__
);
224 $params = array('uniq_email_per_site' => 1);
225 $description = "Shows setting a variable for a current domain.";
226 $result = $this->callAPIAndDocument('setting', 'create', $params, __FUNCTION__
, __FILE__
, $description, 'CreateSettingCurrentDomain');
227 $this->assertArrayHasKey(CRM_Core_Config
::domainID(), $result['values']);
231 * Check getfields works.
233 public function testCreateInvalidSettings() {
235 'domain_id' => $this->_domainID2
,
238 $result = $this->callAPIFailure('setting', 'create', $params);
242 * Check invalid settings rejected -
244 public function testCreateInvalidURLSettings() {
246 'domain_id' => $this->_domainID2
,
247 'userFrameworkResourceURL' => 'dfhkdhfd',
249 $result = $this->callAPIFailure('setting', 'create', $params);
251 'domain_id' => $this->_domainID2
,
252 'userFrameworkResourceURL' => 'http://blah.com',
254 $result = $this->callAPISuccess('setting', 'create', $params);
258 * Check getfields works.
260 public function testCreateInvalidBooleanSettings() {
262 'domain_id' => $this->_domainID2
,
263 'track_civimail_replies' => 'dfhkdhfd',
265 $result = $this->callAPIFailure('setting', 'create', $params);
267 $params = array('track_civimail_replies' => '0');
268 $result = $this->callAPISuccess('setting', 'create', $params);
269 $getResult = $this->callAPISuccess('setting', 'get', $params);
270 $this->assertEquals(0, $getResult['values'][$this->_currentDomain
]['track_civimail_replies']);
272 $getResult = $this->callAPISuccess('setting', 'get', $params);
273 $this->assertEquals(0, $getResult['values'][$this->_currentDomain
]['track_civimail_replies']);
275 'domain_id' => $this->_domainID2
,
276 'track_civimail_replies' => '1',
278 $result = $this->callAPISuccess('setting', 'create', $params);
279 $getResult = $this->callAPISuccess('setting', 'get', $params);
280 $this->assertEquals(1, $getResult['values'][$this->_domainID2
]['track_civimail_replies']);
283 'domain_id' => $this->_domainID2
,
284 'track_civimail_replies' => 'TRUE',
286 $result = $this->callAPISuccess('setting', 'create', $params);
287 $getResult = $this->callAPISuccess('setting', 'get', $params);
289 $this->assertEquals(1, $getResult['values'][$this->_domainID2
]['track_civimail_replies'], "check TRUE is converted to 1");
293 * Check getfields works.
295 public function testCreateSettingMultipleDomains() {
296 $description = "Shows setting a variable for all domains.";
299 'domain_id' => 'all',
300 'uniq_email_per_site' => 1,
302 $result = $this->callAPIAndDocument('setting', 'create', $params, __FUNCTION__
, __FILE__
, $description, 'CreateAllDomains');
304 $this->assertEquals(1, $result['values'][2]['uniq_email_per_site']);
305 $this->assertEquals(1, $result['values'][1]['uniq_email_per_site']);
306 $this->assertArrayHasKey(3, $result['values'], 'Domain create probably failed Debug this IF domain test is passing');
307 $this->assertEquals(1, $result['values'][3]['uniq_email_per_site'], 'failed to set setting for domain 3.');
310 'domain_id' => 'all',
311 'return' => 'uniq_email_per_site',
313 // we'll check it with a 'get'
314 $description = "Shows getting a variable for all domains.";
315 $result = $this->callAPIAndDocument('setting', 'get', $params, __FUNCTION__
, __FILE__
, $description, 'GetAllDomains');
317 $this->assertEquals(1, $result['values'][2]['uniq_email_per_site']);
318 $this->assertEquals(1, $result['values'][1]['uniq_email_per_site']);
319 $this->assertEquals(1, $result['values'][3]['uniq_email_per_site']);
322 'domain_id' => array(1, 3),
323 'uniq_email_per_site' => 0,
325 $description = "Shows setting a variable for specified domains.";
326 $result = $this->callAPIAndDocument('setting', 'create', $params, __FUNCTION__
, __FILE__
, $description, 'CreateSpecifiedDomains');
328 $this->assertEquals(0, $result['values'][3]['uniq_email_per_site']);
329 $this->assertEquals(0, $result['values'][1]['uniq_email_per_site']);
331 'domain_id' => array(1, 2),
332 'return' => array('uniq_email_per_site'),
334 $description = "Shows getting a variable for specified domains.";
335 $result = $this->callAPIAndDocument('setting', 'get', $params, __FUNCTION__
, __FILE__
, $description, 'GetSpecifiedDomains');
336 $this->assertEquals(1, $result['values'][2]['uniq_email_per_site']);
337 $this->assertEquals(0, $result['values'][1]['uniq_email_per_site']);
341 public function testGetSetting() {
343 'domain_id' => $this->_domainID2
,
344 'return' => 'uniq_email_per_site',
346 $description = "Shows get setting a variable for a given domain - if no domain is set current is assumed.";
348 $result = $this->callAPIAndDocument('setting', 'get', $params, __FUNCTION__
, __FILE__
);
351 'return' => 'uniq_email_per_site',
353 $description = "Shows getting a variable for a current domain.";
354 $result = $this->callAPIAndDocument('setting', 'get', $params, __FUNCTION__
, __FILE__
, $description, 'GetSettingCurrentDomain');
355 $this->assertArrayHasKey(CRM_Core_Config
::domainID(), $result['values']);
359 * Check that setting defined in extension can be retrieved.
361 public function testGetExtensionSetting() {
362 $this->hookClass
->setHook('civicrm_alterSettingsFolders', array($this, 'setExtensionMetadata'));
364 // the caching of data to all duplicates the caching of data to the empty string
365 CRM_Core_BAO_Cache
::setItem($data, 'CiviCRM setting Spec', 'All');
366 CRM_Core_BAO_Cache
::setItem($data, 'CiviCRM setting Specs', 'settingsMetadata__');
367 $fields = $this->callAPISuccess('setting', 'getfields', array('filters' => array('group_name' => 'Test Settings')));
368 $this->assertArrayHasKey('test_key', $fields['values']);
369 $this->callAPISuccess('setting', 'create', array('test_key' => 'keyset'));
370 $result = $this->callAPISuccess('setting', 'getvalue', array('name' => 'test_key', 'group' => 'Test Settings'));
371 $this->assertEquals('keyset', $result);
375 * Setting api should set & fetch settings stored in config as well as those in settings table
377 public function testSetConfigSetting() {
378 $config = CRM_Core_Config
::singleton();
379 $this->assertFalse($config->debug
== 1);
381 'domain_id' => $this->_domainID2
,
382 'debug_enabled' => 1,
384 $result = $this->callAPISuccess('setting', 'create', $params);
385 CRM_Core_BAO_Domain
::setDomain($this->_domainID2
);
386 $config = CRM_Core_Config
::singleton(TRUE, TRUE);
387 CRM_Core_BAO_Domain
::resetDomain();
388 $this->assertTrue($config->debug
== 1);
389 // this should NOT be stored in the settings table now - only in config
390 $sql = " SELECT count(*) as c FROM civicrm_setting WHERE name LIKE '%maxFileSize%'";
391 $dao = CRM_Core_DAO
::executeQuery($sql);
393 $this->assertEquals($dao->c
, 0);
397 * Setting api should set & fetch settings stored in config as well as those in settings table
399 public function testGetConfigSetting() {
400 $settings = $this->callAPISuccess('setting', 'get', array(
401 'name' => 'defaultCurrency',
405 $this->assertEquals('USD', $settings['values'][0]['defaultCurrency']);
409 * Setting api should set & fetch settings stored in config as well as those in settings table
411 public function testGetSetConfigSettingMultipleDomains() {
412 $settings = $this->callAPISuccess('setting', 'create', array(
413 'defaultCurrency' => 'USD',
414 'domain_id' => $this->_currentDomain
,
417 $settings = $this->callAPISuccess('setting', 'create', array(
418 'defaultCurrency' => 'CAD',
419 'domain_id' => $this->_domainID2
,
422 $settings = $this->callAPISuccess('setting', 'get', array(
423 'return' => 'defaultCurrency',
424 'domain_id' => 'all',
427 $this->assertEquals('USD', $settings['values'][$this->_currentDomain
]['defaultCurrency']);
428 $this->assertEquals('CAD', $settings['values'][$this->_domainID2
]['defaultCurrency'],
429 "second domain (id {$this->_domainID2} ) should be set to CAD. First dom was {$this->_currentDomain} & was USD");
434 * Use getValue against a config setting.
436 public function testGetValueConfigSetting() {
438 'name' => 'monetaryThousandSeparator',
439 'group' => 'Localization Setting',
441 $result = $this->callAPISuccess('setting', 'getvalue', $params);
442 $this->assertEquals(',', $result);
445 public function testGetValue() {
447 'name' => 'petition_contacts',
448 'group' => 'Campaign Preferences',
450 $description = "Demonstrates getvalue action - intended for runtime use as better caching than get.";
452 $result = $this->callAPIAndDocument('setting', 'getvalue', $params, __FUNCTION__
, __FILE__
, $description);
453 $this->assertEquals('Petition Contacts', $result);
456 public function testGetDefaults() {
457 $description = "Gets defaults setting a variable for a given domain - if no domain is set current is assumed.";
460 'name' => 'address_format',
462 $result = $this->callAPIAndDocument('setting', 'getdefaults', $params, __FUNCTION__
, __FILE__
, $description, 'GetDefaults');
463 $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']);
464 $params = array('name' => 'mailing_format');
465 $result = $this->callAPISuccess('setting', 'getdefaults', $params);
466 $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']);
467 $this->assertArrayHasKey(CRM_Core_Config
::domainID(), $result['values']);
471 * Function tests reverting a specific parameter.
473 public function testRevert() {
475 'address_format' => 'xyz',
476 'mailing_format' => 'bcs',
478 $result = $this->callAPISuccess('setting', 'create', $params);
479 $this->assertAPISuccess($result, "in line " . __LINE__
);
480 $revertParams = array(
481 'name' => 'address_format',
483 $result = $this->callAPISuccess('setting', 'get', $params);
485 $this->assertEquals('xyz', $result['values'][CRM_Core_Config
::domainID()]['address_format']);
486 $description = "Demonstrates reverting a parameter to default value.";
487 $result = $this->callAPIAndDocument('setting', 'revert', $revertParams, __FUNCTION__
, __FILE__
, $description, '');
488 //make sure it's reverted
489 $result = $this->callAPISuccess('setting', 'get', $params);
490 $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']);
492 'return' => array('mailing_format'),
494 $result = $this->callAPISuccess('setting', 'get', $params);
495 //make sure it's unchanged
496 $this->assertEquals('bcs', $result['values'][CRM_Core_Config
::domainID()]['mailing_format']);
500 * Tests reverting ALL parameters (specific domain)
502 public function testRevertAll() {
504 'address_format' => 'xyz',
505 'mailing_format' => 'bcs',
507 $result = $this->callAPISuccess('setting', 'create', $params);
508 $revertParams = array();
509 $result = $this->callAPISuccess('setting', 'get', $params);
511 $this->assertEquals('xyz', $result['values'][CRM_Core_Config
::domainID()]['address_format']);
513 $this->callAPISuccess('setting', 'revert', $revertParams);
514 //make sure it's reverted
515 $result = $this->callAPISuccess('setting', 'get', array('group' => 'core'));
516 $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']);
517 $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']);
521 * Tests filling missing params.
523 public function testFill() {
525 'name' => 'B Team Domain',
527 $dom = $this->callAPISuccess('domain', 'create', $domparams);
529 'domain_id' => 'all',
531 $result = $this->callAPISuccess('setting', 'get', $params);
533 'address_format' => 'xyz',
534 'mailing_format' => 'bcs',
535 'domain_id' => $this->_domainID2
,
537 $result = $this->callAPISuccess('setting', 'create', $params);
539 'domain_id' => $dom['id'],
541 $result = $this->callAPISuccess('setting', 'get', $params);
542 $this->assertAPISuccess($result, "in line " . __LINE__
);
543 $this->assertArrayNotHasKey('tag_unconfirmed', $result['values'][$dom['id']], 'setting for domain 3 should not be set. Debug this IF domain test is passing');
544 $result = $this->callAPISuccess('setting', 'fill', $params);
545 $this->assertAPISuccess($result, "in line " . __LINE__
);
546 $result = $this->callAPISuccess('setting', 'get', $params);
547 $this->assertAPISuccess($result, "in line " . __LINE__
);
548 $this->assertArrayHasKey('tag_unconfirmed', $result['values'][$dom['id']]);
549 $this->assertArrayHasKey('extensionsDir', $result['values'][$dom['id']]);
550 $this->assertEquals('Unconfirmed', $result['values'][$dom['id']]['tag_unconfirmed']);