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