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