Fix civicrm_country template file
[civicrm-core.git] / tests / phpunit / CRM / Core / BAO / SettingTest.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
81621fee 4 | CiviCRM version 4.7 |
6a488035 5 +--------------------------------------------------------------------+
fa938177 6 | Copyright CiviCRM LLC (c) 2004-2016 |
6a488035
TO
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 +--------------------------------------------------------------------+
d25dd0ee 26 */
6a488035 27
e9479dcf
EM
28/**
29 * Class CRM_Core_BAO_SettingTest
acb109b7 30 * @group headless
e9479dcf 31 */
6a488035 32class CRM_Core_BAO_SettingTest extends CiviUnitTestCase {
00be9182 33 public function setUp() {
6a488035
TO
34 parent::setUp();
35 global $civicrm_setting;
36 $this->origSetting = $civicrm_setting;
37 CRM_Utils_Cache::singleton()->flush();
38 }
39
00be9182 40 public function tearDown() {
6a488035
TO
41 global $civicrm_setting;
42 $civicrm_setting = $this->origSetting;
43 CRM_Utils_Cache::singleton()->flush();
44 parent::tearDown();
45 }
46
00be9182 47 public function testEnableComponentValid() {
6a488035
TO
48 $config = CRM_Core_Config::singleton(TRUE, TRUE);
49
50 $result = CRM_Core_BAO_ConfigSetting::enableComponent('CiviCampaign');
51
52 $this->assertTrue($result);
53 }
54
00be9182 55 public function testEnableComponentAlreadyPresent() {
6a488035
TO
56 $config = CRM_Core_Config::singleton(TRUE, TRUE);
57
58 $result = CRM_Core_BAO_ConfigSetting::enableComponent('CiviCampaign');
59 $result = CRM_Core_BAO_ConfigSetting::enableComponent('CiviCampaign');
60
61 $this->assertTrue($result);
62 }
63
00be9182 64 public function testEnableComponentInvalid() {
6a488035
TO
65 $config = CRM_Core_Config::singleton(TRUE, TRUE);
66
67 $result = CRM_Core_BAO_ConfigSetting::enableComponent('CiviFake');
68
69 $this->assertFalse($result);
70 }
71
72 /**
73 * Ensure that overrides in $civicrm_setting apply when
74 * using getItem($group,$name).
75 */
00be9182 76 public function testGetItem_Override() {
6a488035
TO
77 global $civicrm_setting;
78 $civicrm_setting[CRM_Core_BAO_Setting::DIRECTORY_PREFERENCES_NAME]['imageUploadDir'] = '/test/override';
8eb5d071 79 Civi::service('settings_manager')->useMandatory();
6a488035
TO
80 $value = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::DIRECTORY_PREFERENCES_NAME, 'imageUploadDir');
81 $this->assertEquals('/test/override', $value);
bf83df95 82
83 // CRM-14974 test suite
84 $civicrm_setting['Test Preferences']['overrideSetting'] = '/test/override';
8eb5d071 85 Civi::service('settings_manager')->useMandatory();
bf83df95 86 $values = CRM_Core_BAO_Setting::getItem('Test Preferences');
87 $this->assertEquals('/test/override', $values['overrideSetting']);
08ef4ddd 88 Civi::settings()->set('databaseSetting', '/test/database');
bf83df95 89 $values = CRM_Core_BAO_Setting::getItem('Test Preferences');
90 $this->assertEquals('/test/override', $values['overrideSetting']);
91 $this->assertEquals('/test/database', $values['databaseSetting']);
92 $civicrm_setting['Test Preferences']['databaseSetting'] = '/test/dataride';
8eb5d071 93 Civi::service('settings_manager')->useMandatory();
bf83df95 94 $values = CRM_Core_BAO_Setting::getItem('Test Preferences');
95 $this->assertEquals('/test/override', $values['overrideSetting']);
96 $this->assertEquals('/test/dataride', $values['databaseSetting']);
97 // CRM-14974 tear down
98 unset($civicrm_setting['Test Preferences']);
f806379b 99 $query = "DELETE FROM civicrm_setting WHERE name IN ('overrideSetting', 'databaseSetting');";
bf83df95 100 CRM_Core_DAO::executeQuery($query);
6a488035
TO
101 }
102
103 /**
104 * Ensure that overrides in $civicrm_setting apply when
105 * using getItem($group).
106 */
00be9182 107 public function testGetItemGroup_Override() {
6a488035
TO
108 global $civicrm_setting;
109 $civicrm_setting[CRM_Core_BAO_Setting::DIRECTORY_PREFERENCES_NAME]['imageUploadDir'] = '/test/override';
8eb5d071 110 Civi::service('settings_manager')->useMandatory();
6a488035
TO
111 $values = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::DIRECTORY_PREFERENCES_NAME);
112 $this->assertEquals('/test/override', $values['imageUploadDir']);
113 }
114
76bd16ab
TO
115 public function testDefaults() {
116 CRM_Core_DAO::executeQuery('DELETE FROM civicrm_setting WHERE name = "max_attachments"');
e1d39824 117 Civi::service('settings_manager')->flush();
76bd16ab
TO
118 $this->assertEquals(3, Civi::settings()->get('max_attachments'));
119 $this->assertEquals(3, CRM_Core_Config::singleton()->maxAttachments);
6a488035 120 }
6a488035 121
076d8c82
TO
122 /**
123 * Ensure that on_change callbacks fire.
124 *
125 * Note: api_v3_SettingTest::testOnChange and CRM_Core_BAO_SettingTest::testOnChange
126 * are very similar, but they exercise different codepaths. The first uses the API
127 * and setItems [plural]; the second uses setItem [singular].
128 */
00be9182 129 public function testOnChange() {
076d8c82
TO
130 global $_testOnChange_hookCalls;
131 $this->setMockSettingsMetaData(array(
132 'onChangeExample' => array(
133 'group_name' => 'CiviCRM Preferences',
134 'group' => 'core',
135 'name' => 'onChangeExample',
136 'type' => 'Array',
137 'quick_form_type' => 'Element',
138 'html_type' => 'advmultiselect',
139 'default' => array('CiviEvent', 'CiviContribute'),
140 'add' => '4.4',
141 'title' => 'List of Components',
142 'is_domain' => '1',
143 'is_contact' => 0,
144 'description' => NULL,
145 'help_text' => NULL,
481a74f4 146 'on_change' => array(// list of callbacks
21dfd5f5 147 array(__CLASS__, '_testOnChange_onChangeExample'),
076d8c82
TO
148 ),
149 ),
150 ));
151
152 // set initial value
153 $_testOnChange_hookCalls = array('count' => 0);
08ef4ddd 154 Civi::settings()->set('onChangeExample', array('First', 'Value'));
076d8c82
TO
155 $this->assertEquals(1, $_testOnChange_hookCalls['count']);
156 $this->assertEquals(array('First', 'Value'), $_testOnChange_hookCalls['newValue']);
157 $this->assertEquals('List of Components', $_testOnChange_hookCalls['metadata']['title']);
158
159 // change value
160 $_testOnChange_hookCalls = array('count' => 0);
08ef4ddd 161 Civi::settings()->set('onChangeExample', array('Second', 'Value'));
076d8c82
TO
162 $this->assertEquals(1, $_testOnChange_hookCalls['count']);
163 $this->assertEquals(array('First', 'Value'), $_testOnChange_hookCalls['oldValue']);
164 $this->assertEquals(array('Second', 'Value'), $_testOnChange_hookCalls['newValue']);
165 $this->assertEquals('List of Components', $_testOnChange_hookCalls['metadata']['title']);
166 }
167
168 /**
169 * Mock callback for a setting's on_change handler
170 *
171 * @param $oldValue
172 * @param $newValue
173 * @param $metadata
174 */
00be9182 175 public static function _testOnChange_onChangeExample($oldValue, $newValue, $metadata) {
076d8c82
TO
176 global $_testOnChange_hookCalls;
177 $_testOnChange_hookCalls['count']++;
178 $_testOnChange_hookCalls['oldValue'] = $oldValue;
179 $_testOnChange_hookCalls['newValue'] = $newValue;
180 $_testOnChange_hookCalls['metadata'] = $metadata;
181 }
182
6a488035 183}