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