tests/ - Remove get_info()
[civicrm-core.git] / tests / phpunit / api / v3 / SettingTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
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. |
13 | |
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. |
18 | |
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 +--------------------------------------------------------------------+
26 */
27
28 require_once 'CiviTest/CiviUnitTestCase.php';
29
30
31 /**
32 * Test APIv3 civicrm_setting_* functions
33 *
34 * @package CiviCRM_APIv3
35 * @subpackage API_Core
36 */
37
38 /**
39 * Class contains api test cases for civicrm settings
40 *
41 */
42 class api_v3_SettingTest extends CiviUnitTestCase {
43
44 protected $_apiversion = 3;
45 protected $_contactID;
46 protected $_params;
47 protected $_currentDomain;
48 protected $_domainID2;
49 protected $_domainID3;
50
51 /**
52 *
53 */
54 function __construct() {
55 parent::__construct();
56
57 }
58
59 function setUp() {
60 parent::setUp();
61 $params = array(
62 'name' => 'Default Domain Name',
63 );
64 $result = $this->callAPISuccess( 'domain','get',$params);
65 if(empty($result['id'])){
66 $result = $this->callAPISuccess( 'domain','create',$params );
67 }
68
69 $params['name'] = 'Second Domain';
70 $result = $this->callAPISuccess( 'domain','get',$params);
71 if(empty($result['id'])){
72 $result = $this->callAPISuccess( 'domain','create',$params );
73 }
74 $this->_domainID2 = $result['id'];
75 $params['name'] = 'A-team domain';
76 $result = $this->callAPISuccess( 'domain','get',$params);
77 if(empty($result['id'])){
78 $result = $this->callAPISuccess( 'domain','create',$params );
79 }
80 $this->_domainID3 = $result['id'];
81 $this->_currentDomain = CRM_Core_Config::domainID();
82 $this->hookClass = CRM_Utils_Hook::singleton();
83 }
84
85 function tearDown() {
86 CRM_Utils_Hook::singleton()->reset();
87 parent::tearDown();
88 $this->callAPISuccess('system','flush', array());
89 $this->quickCleanup(array('civicrm_domain'));
90 }
91
92 /**
93 * Set additional settings into metadata (implements hook)
94 * @param array $metaDataFolders
95 */
96 function setExtensionMetadata(&$metaDataFolders) {
97 global $civicrm_root;
98 $metaDataFolders[] = $civicrm_root . '/tests/phpunit/api/v3/settings';
99 }
100 /**
101 /**
102 * Check getfields works
103 */
104 function testGetFields() {
105 $description = 'Demonstrate return from getfields - see subfolder for variants';
106 $result = $this->callAPIAndDocument('setting', 'getfields', array(), __FUNCTION__, __FILE__, $description);
107 $this->assertArrayHasKey('customCSSURL', $result['values']);
108
109 $description = 'Demonstrate return from getfields';
110 $result = $this->callAPISuccess('setting', 'getfields', array());
111 $this->assertArrayHasKey('customCSSURL', $result['values']);
112 $this->callAPISuccess('system','flush', array());
113 }
114
115 /**
116 * Let's check it's loading from cache by meddling with the cache
117 */
118 function testGetFieldsCaching() {
119 $settingsMetadata = array();
120 CRM_Core_BAO_Cache::setItem($settingsMetadata,'CiviCRM setting Specs', 'settingsMetadata__');
121 CRM_Core_BAO_Cache::setItem($settingsMetadata,'CiviCRM setting Spec', 'All');
122 $result = $this->callAPISuccess('setting', 'getfields', array());
123 $this->assertArrayNotHasKey('customCSSURL', $result['values']);
124 $this->quickCleanup(array('civicrm_cache'));
125 }
126
127 function testGetFieldsFilters() {
128 $params = array('name' => 'advanced_search_options');
129 $result = $this->callAPISuccess('setting', 'getfields', $params);
130 $this->assertArrayNotHasKey('customCSSURL', $result['values']);
131 $this->assertArrayHasKey('advanced_search_options',$result['values']);
132 }
133
134 /**
135 * Test that getfields will filter on group
136 */
137 function testGetFieldsGroupFilters() {
138 $params = array('filters' => array('group' => 'multisite'));
139 $result = $this->callAPISuccess('setting', 'getfields', $params);
140 $this->assertArrayNotHasKey('customCSSURL', $result['values']);
141 $this->assertArrayHasKey('domain_group_id',$result['values']);
142 }
143
144 /**
145 * Test that getfields will filter on another field (prefetch)
146 */
147 function testGetFieldsPrefetchFilters() {
148 $params = array('filters' => array('prefetch' => 1));
149 $result = $this->callAPISuccess('setting', 'getfields', $params);
150 $this->assertArrayNotHasKey('disable_mandatory_tokens_check', $result['values']);
151 $this->assertArrayHasKey('monetaryDecimalPoint',$result['values']);
152 }
153
154 /**
155 * Ensure that on_change callbacks fire.
156 *
157 * Note: api_v3_SettingTest::testOnChange and CRM_Core_BAO_SettingTest::testOnChange
158 * are very similar, but they exercise different codepaths. The first uses the API
159 * and setItems [plural]; the second uses setItem [singular].
160 */
161 function testOnChange() {
162 global $_testOnChange_hookCalls;
163 $this->setMockSettingsMetaData(array(
164 'onChangeExample' => array(
165 'group_name' => 'CiviCRM Preferences',
166 'group' => 'core',
167 'name' => 'onChangeExample',
168 'type' => 'Array',
169 'quick_form_type' => 'Element',
170 'html_type' => 'advmultiselect',
171 'default' => array('CiviEvent', 'CiviContribute'),
172 'add' => '4.4',
173 'title' => 'List of Components',
174 'is_domain' => '1',
175 'is_contact' => 0,
176 'description' => NULL,
177 'help_text' => NULL,
178 'on_change' => array( // list of callbacks
179 array(__CLASS__, '_testOnChange_onChangeExample')
180 ),
181 ),
182 ));
183
184 // set initial value
185 $_testOnChange_hookCalls = array('count' => 0);
186 $this->callAPISuccess('setting', 'create', array(
187 'onChangeExample' => array('First', 'Value'),
188 ));
189 $this->assertEquals(1, $_testOnChange_hookCalls['count']);
190 $this->assertEquals(array('First', 'Value'), $_testOnChange_hookCalls['newValue']);
191 $this->assertEquals('List of Components', $_testOnChange_hookCalls['metadata']['title']);
192
193 // change value
194 $_testOnChange_hookCalls = array('count' => 0);
195 $this->callAPISuccess('setting', 'create', array(
196 'onChangeExample' => array('Second', 'Value'),
197 ));
198 $this->assertEquals(1, $_testOnChange_hookCalls['count']);
199 $this->assertEquals(array('First', 'Value'), $_testOnChange_hookCalls['oldValue']);
200 $this->assertEquals(array('Second', 'Value'), $_testOnChange_hookCalls['newValue']);
201 $this->assertEquals('List of Components', $_testOnChange_hookCalls['metadata']['title']);
202 }
203
204 /**
205 * Mock callback for a setting's on_change handler
206 *
207 * @param $oldValue
208 * @param $newValue
209 * @param $metadata
210 */
211 static function _testOnChange_onChangeExample($oldValue, $newValue, $metadata) {
212 global $_testOnChange_hookCalls;
213 $_testOnChange_hookCalls['count']++;
214 $_testOnChange_hookCalls['oldValue'] = $oldValue;
215 $_testOnChange_hookCalls['newValue'] = $newValue;
216 $_testOnChange_hookCalls['metadata'] = $metadata;
217 }
218
219 /**
220 * Check getfields works
221 */
222 function testCreateSetting() {
223 $description = "shows setting a variable for a given domain - if no domain is set current is assumed";
224
225 $params = array(
226 'domain_id' => $this->_domainID2,
227 'uniq_email_per_site' => 1,
228 );
229 $result = $this->callAPIAndDocument('setting', 'create', $params, __FUNCTION__, __FILE__);
230
231 $params = array('uniq_email_per_site' => 1,);
232 $description = "shows setting a variable for a current domain";
233 $result = $this->callAPIAndDocument('setting', 'create', $params, __FUNCTION__, __FILE__, $description, 'CreateSettingCurrentDomain');
234 $this->assertArrayHasKey(CRM_Core_Config::domainID(), $result['values']);
235 }
236
237 /**
238 * Check getfields works
239 */
240 function testCreateInvalidSettings() {
241 $params = array(
242 'domain_id' => $this->_domainID2,
243 'invalid_key' => 1,
244 );
245 $result = $this->callAPIFailure('setting', 'create', $params);
246 }
247
248 /**
249 * Check invalid settings rejected -
250 */
251 function testCreateInvalidURLSettings() {
252 $params = array(
253 'domain_id' => $this->_domainID2,
254 'userFrameworkResourceURL' => 'dfhkdhfd',
255 );
256 $result = $this->callAPIFailure('setting', 'create', $params);
257 $params = array(
258 'domain_id' => $this->_domainID2,
259 'userFrameworkResourceURL' => 'http://blah.com',
260 );
261 $result = $this->callAPISuccess('setting', 'create', $params);
262 }
263
264 /**
265 * Check getfields works
266 */
267 function testCreateInvalidBooleanSettings() {
268 $params = array(
269 'domain_id' => $this->_domainID2,
270 'track_civimail_replies' => 'dfhkdhfd',
271 );
272 $result = $this->callAPIFailure('setting', 'create', $params);
273
274 $params = array('track_civimail_replies' => '0',);
275 $result = $this->callAPISuccess('setting', 'create', $params);
276 $getResult = $this->callAPISuccess('setting','get',$params);
277 $this->assertEquals(0, $getResult['values'][$this->_currentDomain]['track_civimail_replies']);
278
279 $getResult = $this->callAPISuccess('setting','get',$params);
280 $this->assertEquals(0, $getResult['values'][$this->_currentDomain]['track_civimail_replies']);
281 $params = array( 'domain_id' => $this->_domainID2,
282 'track_civimail_replies' => '1',
283 );
284 $result = $this->callAPISuccess('setting', 'create', $params);
285 $getResult = $this->callAPISuccess('setting','get',$params);
286 $this->assertEquals(1, $getResult['values'][$this->_domainID2]['track_civimail_replies']);
287
288 $params = array(
289 'domain_id' => $this->_domainID2,
290 'track_civimail_replies' => 'TRUE',
291 );
292 $result = $this->callAPISuccess('setting', 'create', $params);
293 $getResult = $this->callAPISuccess('setting','get',$params);
294
295 $this->assertEquals(1, $getResult['values'][$this->_domainID2]['track_civimail_replies'], "check TRUE is converted to 1");
296 }
297
298 /**
299 * Check getfields works
300 */
301 function testCreateSettingMultipleDomains() {
302 $description = "shows setting a variable for all domains";
303
304 $params = array(
305 'domain_id' => 'all',
306 'uniq_email_per_site' => 1,
307 );
308 $result = $this->callAPIAndDocument('setting', 'create', $params, __FUNCTION__, __FILE__,$description, 'CreateAllDomains');
309
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.');
314
315 $params = array(
316 'domain_id' => 'all',
317 'return' => 'uniq_email_per_site'
318 );
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');
322
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']);
326
327 $params = array(
328 'domain_id' => array(1,3),
329 'uniq_email_per_site' => 0,
330 );
331 $description = "shows setting a variable for specified domains";
332 $result = $this->callAPIAndDocument('setting', 'create', $params, __FUNCTION__, __FILE__,$description, 'CreateSpecifiedDomains');
333
334 $this->assertEquals(0, $result['values'][3]['uniq_email_per_site']);
335 $this->assertEquals(0, $result['values'][1]['uniq_email_per_site']);
336 $params = array(
337 'domain_id' => array(1,2),
338 'return' => array('uniq_email_per_site'),
339 );
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']);
344
345 }
346
347 function testGetSetting() {
348 $params = array(
349 'domain_id' => $this->_domainID2,
350 'return' => 'uniq_email_per_site',
351 );
352 $description = "shows get setting a variable for a given domain - if no domain is set current is assumed";
353
354 $result = $this->callAPIAndDocument('setting', 'get', $params, __FUNCTION__, __FILE__);
355
356 $params = array(
357 'return' => 'uniq_email_per_site',
358 );
359 $description = "shows getting a variable for a current domain";
360 $result = $this->callAPIAndDocument('setting', 'get', $params, __FUNCTION__, __FILE__, $description, 'GetSettingCurrentDomain');
361 $this->assertArrayHasKey(CRM_Core_Config::domainID(), $result['values']);
362 }
363
364 /**
365 * Check that setting defined in extension can be retrieved
366 */
367 function testGetExtensionSetting() {
368 $this->hookClass->setHook('civicrm_alterSettingsFolders', array($this, 'setExtensionMetadata'));
369 $data = NULL;
370 // the caching of data to all duplicates the caching of data to the empty string
371 CRM_Core_BAO_Cache::setItem($data, 'CiviCRM setting Spec', 'All');
372 CRM_Core_BAO_Cache::setItem($data, 'CiviCRM setting Specs', 'settingsMetadata__');
373 $fields = $this->callAPISuccess('setting', 'getfields', array('filters' => array('group_name' => 'Test Settings')));
374 $this->assertArrayHasKey('test_key', $fields['values']);
375 $this->callAPISuccess('setting', 'create', array('test_key' => 'keyset'));
376 $result = $this->callAPISuccess('setting', 'getvalue', array('name' => 'test_key', 'group' => 'Test Settings'));
377 $this->assertEquals('keyset', $result);
378 }
379
380 /**
381 * Setting api should set & fetch settings stored in config as well as those in settings table
382 */
383 function testSetConfigSetting() {
384 $config = CRM_Core_Config::singleton();
385 $this->assertFalse($config->debug == 1);
386 $params = array(
387 'domain_id' => $this->_domainID2,
388 'debug_enabled' => 1,
389 );
390 $result = $this->callAPISuccess('setting', 'create', $params);
391 CRM_Core_BAO_Domain::setDomain($this->_domainID2);
392 $config = CRM_Core_Config::singleton(TRUE, TRUE);
393 CRM_Core_BAO_Domain::resetDomain();
394 $this->assertTrue($config->debug == 1);
395 // this should NOT be stored in the settings table now - only in config
396 $sql = " SELECT count(*) as c FROM civicrm_setting WHERE name LIKE '%maxFileSize%'";
397 $dao = CRM_Core_DAO::executeQuery($sql);
398 $dao->fetch();
399 $this->assertEquals($dao->c, 0);
400 }
401
402 /**
403 * Setting api should set & fetch settings stored in config as well as those in settings table
404 */
405 function testGetConfigSetting() {
406 $settings = $this->callAPISuccess('setting', 'get', array(
407 'name' => 'defaultCurrency', 'sequential' => 1,)
408 );
409 $this->assertEquals('USD', $settings['values'][0]['defaultCurrency']);
410 }
411
412 /**
413 * Setting api should set & fetch settings stored in config as well as those in settings table
414 */
415 function testGetSetConfigSettingMultipleDomains() {
416 $settings = $this->callAPISuccess('setting', 'create', array(
417 'defaultCurrency' => 'USD', 'domain_id' => $this->_currentDomain)
418 );
419 $settings = $this->callAPISuccess('setting', 'create', array(
420 'defaultCurrency' => 'CAD', 'domain_id' => $this->_domainID2)
421 );
422 $settings = $this->callAPISuccess('setting', 'get', array(
423 'return' => 'defaultCurrency', 'domain_id' => 'all',
424 )
425 );
426 $this->assertEquals('USD', $settings['values'][$this->_currentDomain]['defaultCurrency']);
427 $this->assertEquals('CAD', $settings['values'][$this->_domainID2]['defaultCurrency'],
428 "second domain (id {$this->_domainID2} ) should be set to CAD. First dom was {$this->_currentDomain} & was USD");
429
430 }
431
432 /**
433 * Use getValue against a config setting
434 */
435 function testGetValueConfigSetting() {
436 $params = array( 'name' => 'monetaryThousandSeparator',
437 'group' => 'Localization Setting',
438 );
439 $result = $this->callAPISuccess('setting', 'getvalue', $params);
440 $this->assertEquals(',', $result);
441 }
442
443 function testGetValue() {
444 $params = array( 'name' => 'petition_contacts',
445 'group' => 'Campaign Preferences'
446 );
447 $description = "Demonstrates getvalue action - intended for runtime use as better caching than get";
448
449 $result = $this->callAPIAndDocument('setting', 'getvalue', $params, __FUNCTION__, __FILE__, $description);
450 $this->assertEquals('Petition Contacts', $result);
451 }
452
453 function testGetDefaults() {
454 $description = "gets defaults setting a variable for a given domain - if no domain is set current is assumed";
455
456 $params = array(
457 'name' => 'address_format',
458 );
459 $result = $this->callAPIAndDocument('setting', 'getdefaults', $params, __FUNCTION__, __FILE__,$description,'GetDefaults','getdefaults');
460 $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']);
461 $params = array('name' => 'mailing_format',);
462 $result = $this->callAPISuccess('setting', 'getdefaults', $params);
463 $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']);
464 $this->assertArrayHasKey(CRM_Core_Config::domainID(), $result['values']);
465 }
466
467 /**
468 * Function tests reverting a specific parameter
469 */
470 function testRevert() {
471 $params = array( 'address_format' => 'xyz',
472 'mailing_format' => 'bcs',
473 );
474 $result = $this->callAPISuccess('setting', 'create', $params);
475 $this->assertAPISuccess($result, "in line " . __LINE__);
476 $revertParams = array( 'name' => 'address_format'
477 );
478 $result = $this->callAPISuccess('setting', 'get', $params);
479 //make sure it's set
480 $this->assertEquals('xyz', $result['values'][CRM_Core_Config::domainID()]['address_format']);
481 $description = "Demonstrates reverting a parameter to default value";
482 $result = $this->callAPIAndDocument('setting', 'revert', $revertParams, __FUNCTION__, __FILE__,$description,'','revert');
483 //make sure it's reverted
484 $result = $this->callAPISuccess('setting', 'get', $params);
485 $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']);
486 $params = array( 'return' => array('mailing_format'),
487 );
488 $result = $this->callAPISuccess('setting', 'get', $params);
489 //make sure it's unchanged
490 $this->assertEquals('bcs', $result['values'][CRM_Core_Config::domainID()]['mailing_format']);
491 }
492
493 /**
494 * Tests reverting ALL parameters (specific domain)
495 */
496 function testRevertAll() {
497 $params = array( 'address_format' => 'xyz',
498 'mailing_format' => 'bcs',
499 );
500 $result = $this->callAPISuccess('setting', 'create', $params);
501 $revertParams = array( );
502 $result = $this->callAPISuccess('setting', 'get', $params);
503 //make sure it's set
504 $this->assertEquals('xyz', $result['values'][CRM_Core_Config::domainID()]['address_format']);
505
506 $this->callAPISuccess('setting', 'revert', $revertParams);
507 //make sure it's reverted
508 $result = $this->callAPISuccess('setting', 'get', array('group' => 'core'));
509 $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']);
510 $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']);
511 }
512
513 /**
514 * Tests filling missing params
515 */
516 function testFill() {
517 $domparams = array(
518 'name' => 'B Team Domain',
519 );
520 $dom = $this->callAPISuccess('domain', 'create', $domparams);
521 $params = array( 'domain_id' => 'all',
522 );
523 $result = $this->callAPISuccess('setting', 'get', $params);
524 $params = array( 'address_format' => 'xyz',
525 'mailing_format' => 'bcs',
526 'domain_id' => $this->_domainID2,
527 );
528 $result = $this->callAPISuccess('setting', 'create', $params);
529 $params = array( 'domain_id' => $dom['id'],
530 );
531 $result = $this->callAPISuccess('setting', 'get', $params);
532 $this->assertAPISuccess($result, "in line " . __LINE__);
533 $this->assertArrayNotHasKey('tag_unconfirmed', $result['values'][$dom['id']],'setting for domain 3 should not be set. Debug this IF domain test is passing');
534 $result = $this->callAPISuccess('setting', 'fill', $params);
535 $this->assertAPISuccess($result, "in line " . __LINE__);
536 $result = $this->callAPISuccess('setting', 'get', $params);
537 $this->assertAPISuccess($result, "in line " . __LINE__);
538 $this->assertArrayHasKey('tag_unconfirmed', $result['values'][$dom['id']]);
539 $this->assertArrayHasKey('extensionsDir', $result['values'][$dom['id']]);
540 $this->assertEquals('Unconfirmed', $result['values'][$dom['id']]['tag_unconfirmed']);
541 }
542 }
543