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