Merge pull request #12178 from jitendrapurohit/membership-4
[civicrm-core.git] / tests / phpunit / api / v3 / SettingTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
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 Civi::cache('settings')->flush();
355 $fields = $this->callAPISuccess('setting', 'getfields', array('filters' => array('group_name' => 'Test Settings')));
356 $this->assertArrayHasKey('test_key', $fields['values']);
357 $this->callAPISuccess('setting', 'create', array('test_key' => 'keyset'));
358 $this->assertEquals('keyset', Civi::settings()->get('test_key'));
359 $result = $this->callAPISuccess('setting', 'getvalue', array('name' => 'test_key', 'group' => 'Test Settings'));
360 $this->assertEquals('keyset', $result);
361 }
362
363 /**
364 * Setting api should set & fetch settings stored in config as well as those in settings table
365 */
366 public function testGetConfigSetting() {
367 $settings = $this->callAPISuccess('setting', 'get', array(
368 'name' => 'defaultCurrency',
369 'sequential' => 1,
370 )
371 );
372 $this->assertEquals('USD', $settings['values'][0]['defaultCurrency']);
373 }
374
375 /**
376 * Setting api should set & fetch settings stored in config as well as those in settings table
377 */
378 public function testGetSetConfigSettingMultipleDomains() {
379 $settings = $this->callAPISuccess('setting', 'create', array(
380 'defaultCurrency' => 'USD',
381 'domain_id' => $this->_currentDomain,
382 )
383 );
384 $settings = $this->callAPISuccess('setting', 'create', array(
385 'defaultCurrency' => 'CAD',
386 'domain_id' => $this->_domainID2,
387 )
388 );
389 $settings = $this->callAPISuccess('setting', 'get', array(
390 'return' => 'defaultCurrency',
391 'domain_id' => 'all',
392 )
393 );
394 $this->assertEquals('USD', $settings['values'][$this->_currentDomain]['defaultCurrency']);
395 $this->assertEquals('CAD', $settings['values'][$this->_domainID2]['defaultCurrency'],
396 "second domain (id {$this->_domainID2} ) should be set to CAD. First dom was {$this->_currentDomain} & was USD");
397
398 }
399
400 /**
401 * Use getValue against a config setting.
402 */
403 public function testGetValueConfigSetting() {
404 $params = array(
405 'name' => 'monetaryThousandSeparator',
406 'group' => 'Localization Setting',
407 );
408 $result = $this->callAPISuccess('setting', 'getvalue', $params);
409 $this->assertEquals(',', $result);
410 }
411
412 public function testGetValue() {
413 $params = array(
414 'name' => 'petition_contacts',
415 'group' => 'Campaign Preferences',
416 );
417 $description = "Demonstrates getvalue action - intended for runtime use as better caching than get.";
418
419 $result = $this->callAPIAndDocument('setting', 'getvalue', $params, __FUNCTION__, __FILE__, $description);
420 $this->assertEquals('Petition Contacts', $result);
421 }
422
423 public function testGetDefaults() {
424 $description = "Gets defaults setting a variable for a given domain - if no domain is set current is assumed.";
425
426 $params = array(
427 'name' => 'address_format',
428 );
429 $result = $this->callAPIAndDocument('setting', 'getdefaults', $params, __FUNCTION__, __FILE__, $description, 'GetDefaults');
430 $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']);
431 $params = array('name' => 'mailing_format');
432 $result = $this->callAPISuccess('setting', 'getdefaults', $params);
433 $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']);
434 $this->assertArrayHasKey(CRM_Core_Config::domainID(), $result['values']);
435 }
436
437 /**
438 * Function tests reverting a specific parameter.
439 */
440 public function testRevert() {
441 $params = array(
442 'address_format' => 'xyz',
443 'mailing_format' => 'bcs',
444 );
445 $result = $this->callAPISuccess('setting', 'create', $params);
446 $this->assertAPISuccess($result, "in line " . __LINE__);
447 $revertParams = array(
448 'name' => 'address_format',
449 );
450 $result = $this->callAPISuccess('setting', 'get', $params);
451 //make sure it's set
452 $this->assertEquals('xyz', $result['values'][CRM_Core_Config::domainID()]['address_format']);
453 $description = "Demonstrates reverting a parameter to default value.";
454 $result = $this->callAPIAndDocument('setting', 'revert', $revertParams, __FUNCTION__, __FILE__, $description, '');
455 //make sure it's reverted
456 $result = $this->callAPISuccess('setting', 'get', $params);
457 $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']);
458 $params = array(
459 'return' => array('mailing_format'),
460 );
461 $result = $this->callAPISuccess('setting', 'get', $params);
462 //make sure it's unchanged
463 $this->assertEquals('bcs', $result['values'][CRM_Core_Config::domainID()]['mailing_format']);
464 }
465
466 /**
467 * Tests reverting ALL parameters (specific domain)
468 */
469 public function testRevertAll() {
470 $params = array(
471 'address_format' => 'xyz',
472 'mailing_format' => 'bcs',
473 );
474 $result = $this->callAPISuccess('setting', 'create', $params);
475 $revertParams = array();
476 $result = $this->callAPISuccess('setting', 'get', $params);
477 //make sure it's set
478 $this->assertEquals('xyz', $result['values'][CRM_Core_Config::domainID()]['address_format']);
479
480 $this->callAPISuccess('setting', 'revert', $revertParams);
481 //make sure it's reverted
482 $result = $this->callAPISuccess('setting', 'get', array('group' => 'core'));
483 $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']);
484 $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']);
485 }
486
487 /**
488 * Settings should respect their defaults
489 */
490 public function testDefaults() {
491 $domparams = array(
492 'name' => 'B Team Domain',
493 'domain_version' => '4.7',
494 );
495 $dom = $this->callAPISuccess('domain', 'create', $domparams);
496 $params = array(
497 'domain_id' => 'all',
498 );
499 $result = $this->callAPISuccess('setting', 'get', $params);
500 $params = array(
501 'address_format' => 'xyz',
502 'mailing_format' => 'bcs',
503 'domain_id' => $this->_domainID2,
504 );
505 $result = $this->callAPISuccess('setting', 'create', $params);
506 $params = array(
507 'domain_id' => $dom['id'],
508 );
509 $result = $this->callAPISuccess('setting', 'get', $params);
510 $this->assertAPISuccess($result, "in line " . __LINE__);
511 $this->assertEquals('Unconfirmed', $result['values'][$dom['id']]['tag_unconfirmed']);
512
513 // The 'fill' operation is no longer necessary, but third parties might still use it, so let's
514 // make sure it doesn't do anything weird (crashing or breaking values).
515 $result = $this->callAPISuccess('setting', 'fill', $params);
516 $this->assertAPISuccess($result, "in line " . __LINE__);
517 $result = $this->callAPISuccess('setting', 'get', $params);
518 $this->assertAPISuccess($result, "in line " . __LINE__);
519 $this->assertArrayHasKey('tag_unconfirmed', $result['values'][$dom['id']]);
520
521 // Setting has NULL default. Not returned.
522 //$this->assertArrayHasKey('extensionsDir', $result['values'][$dom['id']]);
523
524 $this->assertEquals('Unconfirmed', $result['values'][$dom['id']]['tag_unconfirmed']);
525 }
526
527 /**
528 * Test to set isProductionEnvironment
529 *
530 */
531 public function testSetCivicrmEnvironment() {
532 $params = array(
533 'environment' => 'Staging',
534 );
535 $result = $this->callAPISuccess('Setting', 'create', $params);
536 $params = array(
537 'name' => 'environment',
538 'group' => 'Developer Preferences',
539 );
540 $result = $this->callAPISuccess('Setting', 'getvalue', $params);
541 $this->assertEquals('Staging', $result);
542
543 global $civicrm_setting;
544 $civicrm_setting[CRM_Core_BAO_Setting::DEVELOPER_PREFERENCES_NAME]['environment'] = 'Production';
545 Civi::service('settings_manager')->useMandatory();
546 $result = $this->callAPISuccess('Setting', 'getvalue', $params);
547 $this->assertEquals('Production', $result);
548 }
549
550 }