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