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